Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/agent-core/src/harness/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export {
ensureHarnessSessionInit,
appendHarnessTurn,
readHarnessHistory,
writeHarnessSessionTitle,
type HarnessSessionInitOpts,
type AppendHarnessTurnOpts,
type WriteHarnessSessionTitleOpts,
} from './mirror.js'
export { buildReplaySeed, type BuildReplaySeedOpts } from './replay.js'
export {
Expand Down
36 changes: 36 additions & 0 deletions packages/agent-core/src/harness/mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,42 @@ export interface AppendHarnessTurnOpts {
firstTitle?: string
}

export interface WriteHarnessSessionTitleOpts {
sessionId: string
projectId?: string
/** Already-normalized title (e.g., from `HarnessSession.getTitle()`). */
title: string
}

/**
* Persist a harness session's title to meta.json. Called from the
* server's `set_session_title` handler so the title survives a client
* reload — the `title_update` SessionEvent only updates connected
* clients, and on reconnect both `buildSessionList` (index-backed) and
* `listProjectSessions` (meta.json-backed) read from disk. Without this,
* `appendHarnessTurn`'s first-text fallback overwrites the model's
* chosen title.
*
* Expects a pre-normalized title — the in-memory `setTitle` already
* trims / truncates to 60 chars, so callers should pass `getTitle()`
* rather than the raw tool argument. No-op if meta.json doesn't exist,
* the title is empty, or the value is unchanged.
*/
export function writeHarnessSessionTitle(opts: WriteHarnessSessionTitleOpts): void {
if (!opts.title) return
const dir = resolveSessionDir(opts.sessionId, opts.projectId)
const metaPath = join(dir, 'meta.json')
if (!existsSync(metaPath)) return
try {
const meta: SessionMeta = JSON.parse(readFileSync(metaPath, 'utf-8'))
if (meta.title === opts.title) return
meta.title = opts.title
writeFileSync(metaPath, JSON.stringify(meta, null, 2), 'utf-8')
} catch (err) {
log.warn({ err, sessionId: opts.sessionId }, 'failed to write title to meta.json')
}
}

/**
* Append a synthesized turn's messages to messages.jsonl and update
* meta.json (messageCount, lastActiveAt, title if still empty).
Expand Down
1 change: 1 addition & 0 deletions packages/agent-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export {
ensureHarnessSessionInit,
appendHarnessTurn,
readHarnessHistory,
writeHarnessSessionTitle,
buildReplaySeed,
extractHarnessMemoriesFromMirror,
buildMcpSpawnConfig,
Expand Down
44 changes: 38 additions & 6 deletions packages/agent-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ import {
resolveModel,
resumeSession,
synthesizeHarnessTurn,
writeHarnessSessionTitle,
} from '@anton/agent-core'
import { CONNECTOR_FACTORIES, ConnectorManager } from '@anton/connectors'
import { createLogger } from '@anton/logger'
Expand Down Expand Up @@ -2101,9 +2102,27 @@ export class AgentServer {
// few lines below. Both HarnessSession and CodexHarnessSession
// expose `setTitle(title: string)` which emits `title_update`.
onSetTitle: (title: string) => {
const s = this.sessions.get(id)
if (!s || !('setTitle' in s) || typeof s.setTitle !== 'function') return
;(s as { setTitle: (t: string) => void }).setTitle(title)
const s = this.sessions.get(id) as
| { setTitle?: (t: string) => void; getTitle?: () => string }
| undefined
if (!s || typeof s.setTitle !== 'function') return
s.setTitle(title)
// Persist to meta.json so the title survives a client reload —
// the `title_update` event only updates connected clients, and
// on reconnect the server reads titles from disk (buildSessionList
// + listProjectSessions). We read back via getTitle() so disk and
// memory stay in lockstep even if setTitle ever changes its
// normalization rules.
const normalized = typeof s.getTitle === 'function' ? s.getTitle() : title
try {
writeHarnessSessionTitle({
sessionId: id,
projectId: harnessProjectId,
title: normalized,
})
} catch (err) {
log.warn({ err, sessionId: id }, 'failed to persist harness session title')
}
},
})

Expand Down Expand Up @@ -2193,11 +2212,24 @@ export class AgentServer {
const firstText = turn.events.find((e) => e.type === 'text') as
| { content: string }
| undefined
// Prefer the session's own title — it reflects `set_session_title`
// when the model called it, or the turn-0 user-message seed
// otherwise. Fall back to the assistant's first text snippet only
// if the session somehow has no title yet (e.g., a `turnIndex > 0`
// replay edge case). This avoids meta.json latching onto AI prose
// like "I'm checking both of your Google Calendar accounts..." as
// the conversation title.
const sessionForTitle = this.sessions.get(id) as
| { getTitle?: () => string }
| undefined
const sessionTitle =
typeof sessionForTitle?.getTitle === 'function' ? sessionForTitle.getTitle() : ''
const turnTitle = sessionTitle || firstText?.content
appendHarnessTurn({
sessionId: id,
projectId: mirrorProjectId,
messages,
firstTitle: firstText?.content,
firstTitle: turnTitle,
})

// Capture update_project_context tool-result from this turn.
Expand Down Expand Up @@ -2228,9 +2260,9 @@ export class AgentServer {
}
}

if (mirrorProjectId && firstText?.content) {
if (mirrorProjectId && turnTitle) {
try {
const title = firstText.content.slice(0, 60).split('\n')[0]
const title = turnTitle.slice(0, 60).split('\n')[0]
const sessionSummary = projectContextUpdate?.sessionSummary || title
if (projectContextUpdate?.projectSummary) {
updateProjectContext(mirrorProjectId, 'summary', projectContextUpdate.projectSummary)
Expand Down