Skip to content

Commit c9a6429

Browse files
Sync public snapshot from freebuff-private
1 parent 30a4b22 commit c9a6429

4 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎packages/agent-runtime/src/run-agent-step.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ import {
152152
expireMessages,
153153
frameSteeringContent,
154154
USER_INTERRUPTED_MESSAGE,
155+
noteWithCause,
156+
stopCause,
155157
} from './util/messages'
156158
import {
157159
estimateContextTokens,
@@ -2676,7 +2678,11 @@ export async function loopAgentSteps(
26762678
currentAgentState.messageHistory = [
26772679
...currentAgentState.messageHistory,
26782680
userMessage(
2679-
withSystemTags(USER_INTERRUPTED_MESSAGE),
2681+
withSystemTags(
2682+
noteWithCause(USER_INTERRUPTED_MESSAGE, {
2683+
cause: stopCause(signal.reason),
2684+
}),
2685+
),
26802686
),
26812687
]
26822688

@@ -2775,7 +2781,9 @@ export async function loopAgentSteps(
27752781
currentAgentState.messageHistory = [
27762782
...currentAgentState.messageHistory,
27772783
userMessage(
2778-
withSystemTags(refused ? USER_PROMPT_REFUSED_NOTE : TURN_FAILED_NOTE),
2784+
withSystemTags(
2785+
refused ? USER_PROMPT_REFUSED_NOTE : noteWithCause(TURN_FAILED_NOTE),
2786+
),
27792787
),
27802788
]
27812789
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, it } from 'bun:test'
2+
3+
import { noteWithCause, stopCause, USER_INTERRUPTED_MESSAGE } from '../messages'
4+
5+
// The daily trace report reads both: a runner restart must not read as the
6+
// user's Stop, and a stop must not read as happening when the user next wrote.
7+
describe('the turn stop note', () => {
8+
it('says why, when the host gave a cause, and always when', () => {
9+
const at = Date.parse('2026-11-28T01:02:03Z')
10+
expect(
11+
noteWithCause(USER_INTERRUPTED_MESSAGE, {
12+
cause: stopCause(new DOMException('runner_restart', 'AbortError')),
13+
at,
14+
}),
15+
).toBe(
16+
`${USER_INTERRUPTED_MESSAGE} (cause: runner_restart; at 2026-11-28T01:02:03.000Z)`,
17+
)
18+
expect(noteWithCause(USER_INTERRUPTED_MESSAGE, { at })).toBe(
19+
`${USER_INTERRUPTED_MESSAGE} (at 2026-11-28T01:02:03.000Z)`,
20+
)
21+
})
22+
23+
it('gives no cause for the CLI’s Esc, a bare abort or anything else', () => {
24+
expect(stopCause('user-interrupt')).toBeUndefined()
25+
expect(
26+
stopCause(new DOMException('This operation was aborted', 'AbortError')),
27+
).toBeUndefined()
28+
expect(stopCause(new Error('runner_restart'))).toBeUndefined()
29+
expect(stopCause(undefined)).toBeUndefined()
30+
})
31+
})

‎packages/agent-runtime/src/util/messages.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ export function withSystemInstructionTags(str: string): string {
123123
export const USER_INTERRUPTED_MESSAGE =
124124
"User interrupted the response. The assistant's previous work has been preserved."
125125

126+
/**
127+
* Why a host stopped a run, when it says: an AbortError `signal.reason` whose
128+
* message is a lowercase word (the Cloud worker's `runner_restart`,
129+
* `time_limit`, ...). The CLI's Esc (`user-interrupt`), a bare abort and
130+
* anything else give none.
131+
*/
132+
export function stopCause(reason: unknown): string | undefined {
133+
const message =
134+
reason instanceof Error && reason.name === 'AbortError'
135+
? reason.message
136+
: undefined
137+
return message && /^[a-z_]{1,40}$/.test(message) ? message : undefined
138+
}
139+
140+
/**
141+
* A runtime note with why and when it happened appended: the note reaches the
142+
* model (and the trace) with the next request, so without its own time a
143+
* stop reads as happening when the user next wrote (docs/daily-trace-report.md).
144+
* The opening stays the note's, so matchers on it still match.
145+
*/
146+
export function noteWithCause(
147+
note: string,
148+
params: { cause?: string; at?: number } = {},
149+
): string {
150+
const at = new Date(params.at ?? Date.now()).toISOString()
151+
return `${note} (${params.cause ? `cause: ${params.cause}; ` : ''}at ${at})`
152+
}
153+
126154
export function withSystemTags(str: string): string {
127155
return `<system>${str}${closeXml('system')}`
128156
}

‎sdk/src/run.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,11 @@ export function toUserInterruptedRunState(runState: RunState): RunState | undefi
574574
if (!sessionState) return undefined
575575
const previousHistory = sessionState.mainAgentState.messageHistory
576576
const interrupted = withSystemTags(USER_INTERRUPTED_MESSAGE)
577+
// The runtime's own note also says when (and maybe why) the turn stopped.
578+
const isInterrupted = (text: string | undefined) =>
579+
!!text?.startsWith(`<system>${USER_INTERRUPTED_MESSAGE}`)
577580
const last = userMessageText(previousHistory.at(-1))
578-
if (last === interrupted) return runState
581+
if (isInterrupted(last)) return runState
579582

580583
const appended =
581584
runState.output?.type === 'error' && runState.output.message
@@ -584,7 +587,7 @@ export function toUserInterruptedRunState(runState: RunState): RunState | undefi
584587
if (!appended || last !== appended) return runState
585588

586589
const nextHistory = previousHistory.slice(0, -1)
587-
if (userMessageText(nextHistory.at(-1)) !== interrupted) {
590+
if (!isInterrupted(userMessageText(nextHistory.at(-1)))) {
588591
nextHistory.push({
589592
role: 'user' as const,
590593
content: [{ type: 'text' as const, text: interrupted }],

0 commit comments

Comments
 (0)