diff --git a/CHANGELOG.md b/CHANGELOG.md index c034fc4d0..d63883f54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,8 +20,8 @@ release's per-component sections are labelled beneath its heading. ### archie-gateway - Archie's chat replies lead with the answer, keep long evidence last, and treat a pasted error as a request to diagnose it. Archie checks its own earlier actions before describing them, and treats the managed repositories as the source of truth over any local checkout or mirror. -- Conversation titles name what the conversation is about, never the mood, and wait for a message with a subject. -- Session memory never records passwords, tokens or other secrets. +- Conversation titles are written from your message and Archie's reply, name what the conversation is about rather than the mood, and wait for an exchange with a subject. +- Session memory never records passwords, tokens or other secrets, and adds only facts it does not already hold, so memory stops filling with restated duplicates. ## [1.62.0] - 2026-11-10 diff --git a/internal/app/archiegateway/chat_setup.go b/internal/app/archiegateway/chat_setup.go index 15e2a8298..4807490ee 100644 --- a/internal/app/archiegateway/chat_setup.go +++ b/internal/app/archiegateway/chat_setup.go @@ -368,10 +368,10 @@ func newChatTitleGenerator(s chatSetup) gateway.TitleGenerator { return &chatTitleGenerator{log: s.Log, llm: s.LLM, chatModels: s.ChatModels, usage: s.Usage} } -func (g *chatTitleGenerator) GenerateTitle(ctx context.Context, sessionID, firstMessage string) (string, error) { +func (g *chatTitleGenerator) GenerateTitle(ctx context.Context, sessionID, message, reply string) (string, error) { messages := []chat.Message{ {Role: chat.RoleSystem, Content: prompts.SessionTitle()}, - {Role: chat.RoleUser, Content: firstMessage}, + {Role: chat.RoleUser, Content: prompts.SessionTitleExchange(message, reply)}, } // No tools: a title is a single completion. The active model is read // at call time so a /model switch applies to titles too. diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index eaf8abad5..5dcb05da7 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -295,7 +295,7 @@ func (r *Router) route(ctx context.Context, in Inbound) (string, error) { } reply, err := r.LLM(ctx, in) if err == nil { - r.maybeAutoTitle(ctx, in.Message) + r.maybeAutoTitle(ctx, in.Message, reply) } return reply, err } @@ -435,7 +435,7 @@ func (r *Router) streamTurn(ctx context.Context, in Inbound, stream TurnStream) } reply, err := r.LLMStream(ctx, in, stream) if err == nil { - r.maybeAutoTitle(ctx, in.Message) + r.maybeAutoTitle(ctx, in.Message, reply) } return reply, err } diff --git a/internal/gateway/session_commands.go b/internal/gateway/session_commands.go index c0e603784..f5c38aa1d 100644 --- a/internal/gateway/session_commands.go +++ b/internal/gateway/session_commands.go @@ -404,7 +404,7 @@ func (r *Router) handleRetry(ctx context.Context, msg messaging.Message) (string replay.ConversationID = msg.ConversationID reply, err := r.LLM(ctx, Inbound{Message: replay}) if err == nil { - r.maybeAutoTitle(ctx, replay) + r.maybeAutoTitle(ctx, replay, reply) } return reply, err } diff --git a/internal/gateway/session_titles.go b/internal/gateway/session_titles.go index aa32a196d..bc2ae64a0 100644 --- a/internal/gateway/session_titles.go +++ b/internal/gateway/session_titles.go @@ -11,7 +11,19 @@ import ( // TitleGenerator proposes a title for an untitled session. It must not write // to the store. An empty title declines; errors leave the session untitled. type TitleGenerator interface { - GenerateTitle(ctx context.Context, sessionID, firstMessage string) (string, error) + GenerateTitle(ctx context.Context, sessionID, message, reply string) (string, error) +} + +// titleReplyBytes bounds how much of the reply a title proposal reads: the +// opening of a reply names its subject, and a title is not worth more tokens. +const titleReplyBytes = 2000 + +// clipTitleReply cuts reply to titleReplyBytes on a rune boundary. +func clipTitleReply(reply string) string { + if len(reply) <= titleReplyBytes { + return reply + } + return strings.ToValidUTF8(reply[:titleReplyBytes], "") } // maxTitleRunes caps how long a generated session title may be. Titles @@ -23,8 +35,8 @@ const maxTitleRunes = 60 const titleGenerationTimeout = 30 * time.Second // maybeAutoTitle proposes a title in the background after a successful turn -// on an untitled session, using the triggering message. -func (r *Router) maybeAutoTitle(ctx context.Context, msg messaging.Message) { +// on an untitled session, from the triggering message and its reply. +func (r *Router) maybeAutoTitle(ctx context.Context, msg messaging.Message, reply string) { if r.Titles == nil || r.sessionTracker == nil { return } @@ -32,12 +44,12 @@ func (r *Router) maybeAutoTitle(ctx context.Context, msg messaging.Message) { if sessionID == "" || !r.claimTitleInFlight(sessionID) { return } - go r.generateTitle(context.WithoutCancel(ctx), sessionID, msg.Text) + go r.generateTitle(context.WithoutCancel(ctx), sessionID, msg.Text, clipTitleReply(reply)) } // generateTitle runs one title proposal to completion: ask the // generator, then persist the result if the session is still untitled. -func (r *Router) generateTitle(ctx context.Context, sessionID, firstMessage string) { +func (r *Router) generateTitle(ctx context.Context, sessionID, message, reply string) { defer r.releaseTitleInFlight(sessionID) // A proposal runs outside the turn lane, which is where the repo @@ -67,7 +79,7 @@ func (r *Router) generateTitle(ctx context.Context, sessionID, firstMessage stri if sc == nil || sc.Title != "" { return } - title, err := r.Titles.GenerateTitle(gctx, sessionID, firstMessage) + title, err := r.Titles.GenerateTitle(gctx, sessionID, message, reply) if err != nil { return } diff --git a/internal/infrastructure/sessioncurator/sessioncurator.go b/internal/infrastructure/sessioncurator/sessioncurator.go index ea06b4a1b..3259d24b2 100644 --- a/internal/infrastructure/sessioncurator/sessioncurator.go +++ b/internal/infrastructure/sessioncurator/sessioncurator.go @@ -158,7 +158,18 @@ func (c *Curator) reviewOne(ctx context.Context, engine domainmemory.MemoryEngin }, nil } - text, err := c.askModel(ctx, msgs) + // Agent-user scope is what chat turns read back, so it is both what the + // model must not repeat and where new facts go. + scope := domainmemory.Scope{ + Kind: domainmemory.ScopeAgentUser, + Agent: domainmemory.AgentID(sess.AgentID), + User: domainmemory.IdentityID(participant), + } + known, err := knownFacts(ctx, engine, scope) + if err != nil { + return nil, err + } + text, err := c.askModel(ctx, msgs, known) if err != nil { // A real model-call failure (provider down, timeout) is a Pass // error: it says nothing about whether this session had anything @@ -179,12 +190,6 @@ func (c *Curator) reviewOne(ctx context.Context, engine domainmemory.MemoryEngin facts = facts[:maxObservationsPerSession] } - // Write to agent-user scope, which chat turns read back. - scope := domainmemory.Scope{ - Kind: domainmemory.ScopeAgentUser, - Agent: domainmemory.AgentID(sess.AgentID), - User: domainmemory.IdentityID(participant), - } for _, fact := range facts { if _, err := engine.Create(ctx, domainmemory.NewRecord{ Scope: scope, @@ -238,9 +243,9 @@ func sessionParticipant(msgs []curator.ConversationMessage) (participant, reason // model's raw response text. A non-nil error here is a real failure // (provider down, timeout) -- distinct from parseFacts failing on a // response that came back but wasn't valid JSON. -func (c *Curator) askModel(ctx context.Context, msgs []curator.ConversationMessage) (string, error) { +func (c *Curator) askModel(ctx context.Context, msgs []curator.ConversationMessage, known []string) (string, error) { chatMsgs := make([]curator.Message, 0, len(msgs)+1) - chatMsgs = append(chatMsgs, curator.Message{Role: "system", Content: prompts.SessionMemory()}) + chatMsgs = append(chatMsgs, curator.Message{Role: "system", Content: prompts.SessionMemory(known)}) for _, m := range msgs { chatMsgs = append(chatMsgs, curator.Message{Role: m.Role, Content: m.Content}) } @@ -252,6 +257,29 @@ func (c *Curator) askModel(ctx context.Context, msgs []curator.ConversationMessa return res.Text, nil } +// knownFactsBytes bounds the remembered facts shown to the model, newest +// first, so a large memory cannot crowd out the excerpt being reviewed. +const knownFactsBytes = 8 << 10 + +// knownFacts returns the newest facts already remembered in scope, up to +// knownFactsBytes. +func knownFacts(ctx context.Context, engine domainmemory.MemoryEngine, scope domainmemory.Scope) ([]string, error) { + records, err := engine.Query(ctx, domainmemory.Query{Scopes: []domainmemory.Scope{scope}, Limit: 200}) + if err != nil { + return nil, fmt.Errorf("sessioncurator: read remembered facts: %w", err) + } + var known []string + size := 0 + for _, r := range records { + size += len(r.Content) + if size > knownFactsBytes { + break + } + known = append(known, r.Content) + } + return known, nil +} + // parseFacts parses the model's response as a JSON array of strings. func parseFacts(text string) ([]string, error) { var facts []string diff --git a/internal/prompts/prompts.go b/internal/prompts/prompts.go index e0935c4d8..8fc6ec9f0 100644 --- a/internal/prompts/prompts.go +++ b/internal/prompts/prompts.go @@ -61,8 +61,17 @@ func Soul() string { return read("soul.md") + "\n" } // SessionTitle instructs the model to title a conversation. func SessionTitle() string { return read("session-title.md") } -// SessionMemory instructs the session-memory curator to extract durable facts. -func SessionMemory() string { return read("session-memory.md") } +// SessionTitleExchange is the exchange a title is written from: the +// operator's message and Archie's reply. +func SessionTitleExchange(message, reply string) string { + return render("session-title-exchange.tmpl", struct{ Message, Reply string }{message, reply}) +} + +// SessionMemory instructs the session-memory curator to extract durable facts +// that known, the facts already remembered, does not hold. +func SessionMemory(known []string) string { + return render("session-memory.tmpl", struct{ Known []string }{known}) +} // CuratorPass is the user turn of a curator pass that names no reason. func CuratorPass() string { return read("curator-pass.md") } diff --git a/internal/prompts/session-memory.md b/internal/prompts/session-memory.tmpl similarity index 71% rename from internal/prompts/session-memory.md rename to internal/prompts/session-memory.tmpl index 521c27dd0..c320f6f2f 100644 --- a/internal/prompts/session-memory.md +++ b/internal/prompts/session-memory.tmpl @@ -1,3 +1,10 @@ Review this conversation excerpt. List any durable facts, preferences or corrections about the user or their work that are worth remembering for future conversations: not the flow of the conversation, not anything already obvious from context, not one-off task details. Never record passwords, tokens, keys, recovery codes or other secrets, even when they appear in the excerpt. +{{- with .Known}} + +These are already remembered. Do not list them again or restate them in other words. List only facts that are new, or that correct one of these: +{{- range .}} +- {{.}} +{{- end}} +{{- end}} Respond with ONLY a JSON array of short strings, one per fact. Respond with [] if nothing in this excerpt is worth keeping. No other text. diff --git a/internal/prompts/session-title-exchange.tmpl b/internal/prompts/session-title-exchange.tmpl new file mode 100644 index 000000000..49dbbaf25 --- /dev/null +++ b/internal/prompts/session-title-exchange.tmpl @@ -0,0 +1,7 @@ + +{{.Message}} + + + +{{.Reply}} + diff --git a/internal/prompts/session-title.md b/internal/prompts/session-title.md index 193f77fc4..90d9cde6c 100644 --- a/internal/prompts/session-title.md +++ b/internal/prompts/session-title.md @@ -1,10 +1,9 @@ -You write short titles for conversation logs. +You write short titles for conversations between an operator and Archie. -Given a user message from a new conversation, write a title for the -conversation. Requirements: +You are given the operator's message and Archie's reply. Write a title for the conversation. Requirements: - 3 to 6 words, under 60 characters -- Name the subject: the system, task, error or goal the message is about -- Never describe the user's mood or tone +- Name the subject: the system, task, error or goal the exchange is about +- Never describe the operator's mood or tone - No quotes, no bullet, no trailing period - Reply with the title only, nothing else -- If the message names no subject (an exclamation, a greeting), reply NONE +- If neither names a subject (a greeting and its answer), reply NONE