Documentation
¶
Overview ¶
Package briefing turns a set of open actions into a short, evidence-backed summary.
The contract ¶
Every sentence a briefing shows carries the document ids it is derived from, and the server discards any sentence whose ids it did not itself put in the prompt (see Validate). This is the reason the package exists: a system that says "reply to this" without being able to show which message says so is asking for trust it has not earned. The check is mechanical, not a request in the prompt — the prompt states the rule only because stating it improves compliance.
When nothing survives validation, the result is a deterministic count of the open actions by kind, flagged Degraded so the UI can say "aggregate only". The fallback carries evidence too; the rule has no exceptions.
Cost ¶
Briefings are cached on a hash of (prompt version, model, newest observation time, sorted set of open identity keys). Invalidation is implicit: any change to those inputs changes the key. Summary text is deliberately not part of the key — the same identity_key is the same real-world action even if a re-extraction reworded it.
Privacy ¶
Action summaries, counterpart names, notes, prompts, and raw model output are never logged (issue #194): all of them are restatements of the user's private conversations. Log lines are built from SafeLogFields, which can only emit counts, flags, and a truncated key prefix. Parse failures are reported with lengths and offsets — never with err.Error(), because encoding/json quotes the offending input in its message.
The package has no database or HTTP dependency; the API layer converts store rows into Input and Result into a response.
Index ¶
Constants ¶
const PromptVersion = "briefing-v1"
PromptVersion identifies the prompt this package sends to the model. It is part of the cache key, so bumping it after editing prompt.go invalidates every cached briefing. Without that, a prompt fix would keep serving sentences produced by the previous prompt until the open action set happened to change.
Variables ¶
This section is empty.
Functions ¶
func KeyPrefix ¶
KeyPrefix truncates an identity_key for logging.
The full key is a deterministic hash and is not itself personal data, but it is the join handle to a row whose summary is. Logging only a prefix keeps log lines correlatable without making the log a usable index into the actions table.
Truncation is rune-based so a non-hex value cannot be cut mid-rune and turn into invalid UTF-8 in the log stream.
func SafeLogFields ¶
SafeLogFields returns slog key/value pairs describing a briefing generation with no personal data in them.
The rule this enforces (issue #194): action summaries, counterpart names, resolution notes, prompts, and raw model output must never reach the logs. A briefing's text is a restatement of the user's own conversations, so a single well-meant "log the response we failed to parse" turns the container log — and every log collector behind it — into a copy of the user's private messages.
What is left is enough to operate the feature: how many actions went in, how many sentences came out, how many were discarded for lacking evidence, whether the result was the degraded aggregate, and a truncated key prefix for correlation. Nothing here is free text.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a tiny in-process, capacity-bounded briefing cache keyed by Input.CacheKey.
It is intentionally a map plus an insertion-order slice rather than a real LRU: the capacity is single digits and there is one user, so the eviction policy barely matters, while an extra data structure would be code to maintain for no gain. Entries are lost on restart, which costs exactly one extra LLM call after a deploy.
type Input ¶
type Input struct {
Actions []InputAction
// LatestDocumentAt is the newest observed_at among Actions. It advances
// whenever an action is re-observed, which is the signal that the briefing's
// underlying facts moved.
LatestDocumentAt time.Time
// Model is the LLM model name. Swapping models must not serve the previous
// model's sentences from cache.
Model string
}
Input is everything a briefing is derived from.
func (Input) CacheKey ¶
CacheKey is the content hash of everything that can change a briefing: the prompt version, the model, the newest observation time, and the SET of open identity keys.
Invalidation is entirely implicit — there is no TTL and no explicit purge. A new action, a resolved action, a re-observation, a prompt edit, or a model swap each change the key on its own.
Summary text is deliberately absent. The same identity_key denotes the same real-world action (spec §5.5), so a reworded re-extraction is not a reason to regenerate. The useful side effect is that no personal data enters the hash input, which is why the resulting key is safe to write to logs.
type InputAction ¶
type InputAction struct {
IdentityKey string
Kind string
Summary string
CounterpartName string
DocumentID string
DetectedBy string
DueAt *time.Time
Confidence float64
}
InputAction is one open action as handed to the briefing generator. It is a flat, store-free struct on purpose: this package does no database or HTTP work, so it can be exercised entirely with table-driven tests.
type Result ¶
type Result struct {
Sentences []Sentence
// Degraded is true when the sentences are the deterministic count-only
// fallback rather than model output. The UI surfaces this so a user never
// mistakes an aggregate for a summary.
Degraded bool
// DroppedCount is how many model sentences failed evidence validation. It is
// shown to the user as a trust signal, not hidden as debug output.
DroppedCount int
GeneratedAt time.Time
}
Result is a generated briefing.
func Generate ¶
Generate produces a briefing for in.
It never returns an error for an LLM failure. A briefing is an aid on top of the action list; if the model is down, slow, disabled, or returns something unusable, the caller still gets a usable Result — the deterministic count-only fallback, flagged Degraded so the UI can say so. Propagating the error instead would let a summarisation failure take down the page whose data is already in hand.
A truncated reply (llm.ErrTruncated: generation stopped on the token budget) is treated as a failure, not as content. Its JSON is incomplete by definition, and salvaging a partial answer is precisely how a sentence with no evidence behind it reaches the user.
An empty action set never reaches the model: asking for a summary of nothing reliably produces invention.
func Validate ¶
Validate turns a raw model reply into a Result, discarding every sentence that is not backed by a document the server actually put in the prompt.
This is the whole point of the feature. A briefing that says "call the plumber back" without being able to show which message says so is worse than no briefing: it is a confident claim with nothing behind it. So the check is mechanical rather than a request in the prompt — the server owns the set of ids it showed the model, and anything outside that set is a fabrication by definition.
Discarding is per sentence, not per response: one bad sentence must not take the valid ones with it, or the briefing would be empty in practice.
A non-nil error means the reply could not be decoded at all. The returned Result is still usable (the deterministic fallback), so callers can report the failure without having to invent a response.
type Sentence ¶
type Sentence struct {
Text string `json:"text"`
DocumentIDs []string `json:"document_ids"`
IdentityKeys []string `json:"identity_keys"`
}
Sentence is one briefing sentence together with the evidence that justifies it. DocumentIDs is never empty for a sentence that survived validation — a sentence with no evidence is discarded, not shown.