memorysynthesis

package
v0.213.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memorysynthesis contains the bounded, privacy-filtered material envelope used by daily memory synthesis. The envelope (materials.go) has no database or agent dependency: callers provide already structured source items and Build performs the common ordering, de-duplication, secret scrubbing, budget enforcement, and digest calculation. service.go coordinates one run record through the jobstore; concrete material adapters live in the sources subpackage.

Index

Constants

View Source
const (
	OutcomeSucceeded = "succeeded"
	OutcomeFailed    = "failed"
	// OutcomeReused: a succeeded run with the same input digest exists; only last_checked_at moved.
	OutcomeReused = "reused"
	// OutcomeRunning: another run of the same project and day is in flight.
	OutcomeRunning = "running"
	// OutcomeEmpty: the window has no material, so no record was created.
	OutcomeEmpty = "empty"
	// OutcomeLimited: the day already holds RunOptions.MaxRuns runs that called the model.
	OutcomeLimited = "limited"
	// OutcomeBackoff: the latest failure of the day is younger than RunOptions.RetryAfter.
	OutcomeBackoff = "backoff"
)

Outcome states reported by Service.Run.

View Source
const DayLayout = "2006-01-02"

DayLayout is the format of a business day ("2026-10-28").

View Source
const ResultSchemaV1 = "daily-memory-synthesis.v1"

ResultSchemaV1 is the schema tag of the model's answer and of the stored result_json.

View Source
const SchemaV1 = "daily-memory-materials.v1"

Variables

View Source
var (
	ErrAgent        = errors.New("agent call failed")
	ErrOutputFormat = errors.New("invalid output")
	ErrUnsafeInput  = errors.New("unsafe input")
	ErrUnsafeOutput = errors.New("unsafe output")
)

Reasons a synthesis fails; each is the stable prefix of the error kept on the run record.

Functions

func Scrub added in v0.205.0

func Scrub(s string) (string, bool)

Scrub replaces every secret-shaped fragment of s with [redacted] and reports whether it changed anything. Callers must scrub BEFORE truncating: a cut through the middle of a secret would otherwise leave an unrecognisable (and still sensitive) prefix behind.

Types

type Envelope

type Envelope struct {
	Schema   string  `json:"schema"`
	Project  string  `json:"project"`
	Day      string  `json:"day"`
	Timezone string  `json:"timezone"`
	Items    []Item  `json:"items"`
	Omitted  Omitted `json:"omitted"`
}

type Highlight added in v0.207.0

type Highlight struct {
	Text       string   `json:"text"`
	SourceRefs []string `json:"source_refs,omitempty"`
}

Highlight is one concrete development with the source refs it rests on.

type Item

type Item struct {
	ID          string   `json:"id"`
	Kind        string   `json:"kind"`
	OccurredAt  string   `json:"occurred_at,omitempty"`
	Title       string   `json:"title,omitempty"`
	Text        string   `json:"text,omitempty"`
	SourceRef   string   `json:"source_ref"`
	SourceRefs  []string `json:"source_refs,omitempty"`
	Sensitivity string   `json:"sensitivity,omitempty"`
}

Item is one allow-listed fact from a source adapter. Text is never treated as instructions.

func Collect

func Collect(ctx context.Context, project, day string, sources ...Source) ([]Item, error)

type Materials

type Materials struct {
	Envelope
	InputDigest string `json:"input_digest"`
}

Materials is the persisted-safe result of preparing an envelope. The digest covers the canonical envelope, including omission counts, and is therefore stable for identical input.

func Build

func Build(project, day, timezone string, input []Item, opts Options) (Materials, error)

Build prepares source items into the versioned envelope. It is deterministic: items are ordered by occurrence and source, duplicate source refs/text are merged, and all limits are applied before hashing. The returned bytes never contain the original value of a scrubbed secret.

type Omitted

type Omitted struct {
	Items   int      `json:"items,omitempty"`
	Bytes   int      `json:"bytes,omitempty"`
	Reasons []string `json:"reasons,omitempty"`
}

type OneShot added in v0.207.0

type OneShot interface {
	Run(ctx context.Context, r OneShotRequest) (string, error)
}

OneShot runs a model call and returns the agent's stdout.

type OneShotRequest added in v0.207.0

type OneShotRequest struct {
	ProjectKey string
	Agent      string
	Args       []string
	Prompt     string
	Title      string
	Timeout    time.Duration
	// MaxOutputBytes is how much stdout the adapter reads at most (+1 so an oversized
	// answer is detected rather than silently cut).
	MaxOutputBytes int
	// Origin is the ceiling owner the call is submitted under ("" = none).
	Origin string
}

OneShotRequest is one read-only, tool-less model call. The adapter that implements OneShot fixes the safety shape (read-only, no tools, no rules, hidden internal job); the caller only supplies the text and the operator's model arguments.

type Options

type Options struct {
	MaxInputBytes   int
	MaxItemsPerKind int
	MaxRunesPerText int
}

type Outcome added in v0.205.0

type Outcome struct {
	State string
	Run   jobstore.MemorySynthesisRun
}

Outcome is the result of one Service.Run call. Run is zero for OutcomeEmpty.

type Result added in v0.207.0

type Result struct {
	Schema        string      `json:"schema"`
	Summary       string      `json:"summary"`
	Highlights    []Highlight `json:"highlights"`
	OpenQuestions []string    `json:"open_questions"`
}

Result is the validated, normalised answer stored in memory_synthesis_runs.result_json. Unknown fields a model may add are dropped, never stored.

func ParseResult added in v0.207.0

func ParseResult(output string, allowed map[string]bool) (Result, error)

ParseResult extracts the single JSON object of a model answer (a markdown fence or text around it is tolerated) and validates it strictly: schema tag, bounded non-empty summary, bounded lists, and every cited source ref must be in `allowed`.

type RunOptions added in v0.207.0

type RunOptions struct {
	Project   string
	Day       string
	CreatedBy string
	// Force runs the synthesizer even when a succeeded record with the same input digest
	// exists (the old record is retired as skipped once the new run succeeds). It never
	// bypasses a live run of the same project and day.
	Force bool
	// MaxRuns and RetryAfter are the cost limits of the day (see jobstore.MemorySynthesisBegin);
	// zero means no limit / no pause.
	MaxRuns    int
	RetryAfter time.Duration
}

RunOptions is the input of RunWith.

type RunStore added in v0.205.0

type RunStore interface {
	BeginMemorySynthesisIfChanged(jobstore.MemorySynthesisBegin) (jobstore.MemorySynthesisRun, jobstore.MemorySynthesisBeginOutcome, error)
	FinishMemorySynthesisRun(id string, f jobstore.MemorySynthesisFinish) (jobstore.MemorySynthesisRun, error)
}

RunStore is the persistence the coordinator needs; *jobstore.Store implements it.

type Service added in v0.205.0

type Service struct {
	Store   RunStore
	Sources []Source
	Options Options
	Synth   Synthesizer
	// Loc is the server time zone used for the business-day window (default time.Local).
	Loc      *time.Location
	Lookback time.Duration
	// Timeout bounds a run; a running record older than this is treated as stale.
	Timeout time.Duration
	// KeepSnapshot stores the scrubbed envelope on the run record (keep_material_snapshot).
	KeepSnapshot bool
	Now          func() time.Time
	NewID        func() string
}

Service coordinates material preparation and the run record. It owns no goroutines; callers (an explicit run command, later a scheduler) decide when to call Run.

func (*Service) Prepare added in v0.205.0

func (s *Service) Prepare(ctx context.Context, project, day string) (Materials, error)

Prepare collects, scrubs and bounds the materials of one project day without touching the store.

func (*Service) Run added in v0.205.0

func (s *Service) Run(ctx context.Context, project, day, createdBy string) (Outcome, error)

Run prepares the materials and drives one run record: unchanged input reuses the existing succeeded record, a live run for the same day is left alone, and otherwise the synthesizer runs once and the record is finished as succeeded or failed.

func (*Service) RunWith added in v0.207.0

func (s *Service) RunWith(ctx context.Context, o RunOptions) (Outcome, error)

RunWith is Run with options.

type Source

type Source interface {
	Collect(context.Context, string, string) ([]Item, error)
}

Source is a read-only structured material adapter. Adapters must not return transcripts, prompts, argv, environment values, or attachments.

type SynthConfig added in v0.207.0

type SynthConfig struct {
	Agent string
	Model string
	Args  []string
	// Timeout bounds one call (memory.synthesis.timeout).
	Timeout time.Duration
	// MaxOutputBytes bounds the model answer (memory.synthesis.max_output_bytes).
	MaxOutputBytes int
	// Origin is the ceiling owner of the model call: the caller that asked for the run.
	Origin string
	// Lang is the project language the prompt (and so the summary) is written in.
	Lang i18n.Lang
}

SynthConfig is what a synthesizer needs from memory.synthesis (already resolved).

type SynthResult added in v0.205.0

type SynthResult struct {
	Agent      string
	Model      string
	ResultJSON string
}

SynthResult is what a synthesizer hands back. P1 never calls a model: the function is a seam that tests (and later phases) fill in.

type Synthesizer added in v0.205.0

type Synthesizer func(context.Context, Materials) (SynthResult, error)

Synthesizer turns prepared (already scrubbed and bounded) materials into a result.

func NewSynthesizer added in v0.207.0

func NewSynthesizer(o OneShot, c SynthConfig) Synthesizer

NewSynthesizer builds the model-backed Synthesizer: it renders the prompt from the already scrubbed and bounded envelope, makes one read-only call and validates the answer. It never sees anything but Materials (the output of Build), and re-checks the serialised envelope for secret shapes so a caller that bypassed Build cannot leak them to a model.

type Window added in v0.205.0

type Window struct {
	Day   string
	Start time.Time
	End   time.Time
}

Window is the half-open interval [Start, End) of one business day in the server's time zone, optionally widened backwards by a longer lookback. A source keeps an event when Contains(unix) is true, so the day boundaries never double count or drop an event.

func DayWindow added in v0.205.0

func DayWindow(day string, loc *time.Location, lookback time.Duration) (Window, error)

DayWindow computes the window of `day` in loc. The window always ends at the next local midnight and starts at the day's own midnight; a lookback longer than 24h only moves the start earlier (End - lookback), a shorter or zero one keeps the plain day. Computing the midnights from calendar dates (not by adding 24h) keeps DST days 23 / 25 hours long.

func (Window) Contains added in v0.205.0

func (w Window) Contains(unix int64) bool

Contains reports whether the unix-second timestamp falls inside [Start, End).

func (Window) Timezone added in v0.205.0

func (w Window) Timezone() string

Timezone is the UTC offset label written into the envelope ("+08:00").

Directories

Path Synopsis
Package engine assembles one explicit daily memory synthesis run: it reads the live memory.synthesis configuration, builds the material sources and the model-backed synthesizer, and drives memorysynthesis.Service.
Package engine assembles one explicit daily memory synthesis run: it reads the live memory.synthesis configuration, builds the material sources and the model-backed synthesizer, and drives memorysynthesis.Service.
Package sources holds the read-only material adapters of daily memory synthesis.
Package sources holds the read-only material adapters of daily memory synthesis.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL