mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
Keep owned task updates from reopening Claude root turns
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { CLAUDE_BACKGROUND_TASK_SILENT_REFRESH_SUBTYPES } from './claude-background-task-frames'
|
||||
import type { ClaudeTaskInvocations } from './claude-task-invocations'
|
||||
import { claudeBackgroundTaskToolUseId } from './claude-background-task-row-lifecycle'
|
||||
import type { AgentJournalTurnScope } from '../../shared/agent-session/journal/agent-session-journal-turn-scope'
|
||||
import type {
|
||||
StructuredAgentSessionEventSink,
|
||||
@@ -96,20 +98,35 @@ export class ClaudeBackgroundTaskRowWriter {
|
||||
}
|
||||
}
|
||||
|
||||
export function claudeBackgroundTaskTurnOpener(
|
||||
journaling: { frame: Record<string, unknown>; observedAt: number } | null,
|
||||
open: boolean,
|
||||
openOutputTurn: ((frame: Record<string, unknown>, observedAt: number) => void) | undefined
|
||||
): () => void {
|
||||
return () => {
|
||||
// Progress and roster ticks refresh a row without flipping an idle chat to working.
|
||||
const subtype = journaling?.frame.subtype
|
||||
if (
|
||||
journaling &&
|
||||
open &&
|
||||
(typeof subtype !== 'string' || !CLAUDE_BACKGROUND_TASK_SILENT_REFRESH_SUBTYPES.has(subtype))
|
||||
) {
|
||||
openOutputTurn?.(journaling.frame, journaling.observedAt)
|
||||
export function createClaudeBackgroundTaskTurnOpener(
|
||||
openOutputTurn: ((frame: Record<string, unknown>, observedAt: number) => void) | undefined,
|
||||
invocations: ClaudeTaskInvocations
|
||||
) {
|
||||
return (
|
||||
journaling: { frame: Record<string, unknown>; observedAt: number } | null,
|
||||
open: boolean,
|
||||
id: string | null,
|
||||
row?: ClaudeBackgroundTaskRow
|
||||
): (() => void) => {
|
||||
const frame = journaling?.frame
|
||||
const invocation =
|
||||
row?.invocation ??
|
||||
(frame && id !== null
|
||||
? (invocations.selection(frame, id)?.invocation ??
|
||||
invocations.get(id, claudeBackgroundTaskToolUseId(frame)))
|
||||
: undefined)
|
||||
return () => {
|
||||
// Task inventory from a known child cannot assert execution of the root.
|
||||
const subtype = frame?.subtype
|
||||
if (
|
||||
journaling &&
|
||||
open &&
|
||||
invocation?.parentTaskId === undefined &&
|
||||
(typeof subtype !== 'string' ||
|
||||
!CLAUDE_BACKGROUND_TASK_SILENT_REFRESH_SUBTYPES.has(subtype))
|
||||
) {
|
||||
openOutputTurn?.(journaling.frame, journaling.observedAt)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import {
|
||||
} from './claude-background-task-memory'
|
||||
import {
|
||||
ClaudeBackgroundTaskRowWriter,
|
||||
claudeBackgroundTaskTurnOpener
|
||||
createClaudeBackgroundTaskTurnOpener
|
||||
} from './claude-background-task-row-writer'
|
||||
import { observeClaudeBackgroundTaskStart } from './claude-background-task-start'
|
||||
import { observeOwnedClaudeBackgroundTaskRoster } from './claude-background-task-roster'
|
||||
@@ -54,6 +54,7 @@ export type ClaudeBackgroundTaskRowsDeps = {
|
||||
export class ClaudeBackgroundTaskRows {
|
||||
private readonly rows = new Map<string, ClaudeBackgroundTaskRow>()
|
||||
private readonly writer: ClaudeBackgroundTaskRowWriter
|
||||
private readonly openTurn: ReturnType<typeof createClaudeBackgroundTaskTurnOpener>
|
||||
private readonly overflowTerminalRows: ClaudeOverflowTerminalRows
|
||||
private readonly ledgers = new ClaudeBackgroundTaskLedgers()
|
||||
private readonly ids = new ClaudeSubagentIds()
|
||||
@@ -63,6 +64,7 @@ export class ClaudeBackgroundTaskRows {
|
||||
constructor(private readonly deps: ClaudeBackgroundTaskRowsDeps) {
|
||||
this.taskInvocations = deps.taskInvocations ?? new ClaudeTaskInvocations()
|
||||
this.now = deps.now ?? (() => Date.now())
|
||||
this.openTurn = createClaudeBackgroundTaskTurnOpener(deps.openOutputTurn, this.taskInvocations)
|
||||
this.writer = new ClaudeBackgroundTaskRowWriter(
|
||||
deps.sink,
|
||||
deps.turnScope,
|
||||
@@ -102,7 +104,8 @@ export class ClaudeBackgroundTaskRows {
|
||||
if (frameId) {
|
||||
this.wakeFrames.set(frameId, true)
|
||||
}
|
||||
this.deps.openOutputTurn?.(message, observedAt)
|
||||
const id = canonicalClaudeBackgroundTaskId(message, this.ids)
|
||||
this.openTurn(this.journaling, true, id)()
|
||||
}
|
||||
try {
|
||||
return this.observeFrame(message)
|
||||
@@ -323,11 +326,6 @@ export class ClaudeBackgroundTaskRows {
|
||||
lifecycle = false
|
||||
): void {
|
||||
attributeClaudeBackgroundTaskRow(id, row)
|
||||
this.writer.write(
|
||||
id,
|
||||
row,
|
||||
claudeBackgroundTaskTurnOpener(this.journaling, openOutputTurn, this.deps.openOutputTurn),
|
||||
lifecycle
|
||||
)
|
||||
this.writer.write(id, row, this.openTurn(this.journaling, openOutputTurn, id, row), lifecycle)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,3 +161,78 @@ describe('a typed background-task row opens the turn it resumes', () => {
|
||||
expect(projected(items())).toBe('idle')
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps an alias-only owned notification idle even when its alias was forwarded at the root', () => {
|
||||
const { translator, items } = harness()
|
||||
spawnToolCall(translator)
|
||||
translator.handle(
|
||||
systemFrame('owned-start', {
|
||||
subtype: 'task_started',
|
||||
task_id: 'owned',
|
||||
tool_use_id: TOOL,
|
||||
parent_task_id: 'owner',
|
||||
task_type: 'local_bash',
|
||||
is_backgrounded: true
|
||||
})
|
||||
)
|
||||
settleTurn(translator)
|
||||
translator.handle(
|
||||
systemFrame('owned-terminal', {
|
||||
subtype: 'task_notification',
|
||||
tool_use_id: TOOL,
|
||||
status: 'completed',
|
||||
summary: 'Finished child'
|
||||
})
|
||||
)
|
||||
expect(items().find((item) => item.itemId.includes('owned'))?.body).toMatchObject({
|
||||
kind: 'message',
|
||||
blocks: expect.arrayContaining([
|
||||
expect.objectContaining({ type: 'background-task', taskId: 'owned', state: 'done' })
|
||||
])
|
||||
})
|
||||
expect(projected(items())).toBe('idle')
|
||||
translator.dispose()
|
||||
})
|
||||
|
||||
it('keeps owned terminal overflow writes idle without a retained row invocation', () => {
|
||||
const { translator, items } = harness()
|
||||
for (let index = 0; index < 65; index += 1) {
|
||||
translator.handle(
|
||||
systemFrame(`owned-start-${index}`, {
|
||||
subtype: 'task_started',
|
||||
task_id: `owned-${index}`,
|
||||
parent_task_id: 'owner',
|
||||
task_type: 'local_bash',
|
||||
is_backgrounded: true
|
||||
})
|
||||
)
|
||||
}
|
||||
settleTurn(translator)
|
||||
translator.handle(
|
||||
systemFrame('overflow-owned-patch', {
|
||||
subtype: 'task_updated',
|
||||
task_id: 'owned-64',
|
||||
patch: { status: 'failed' }
|
||||
})
|
||||
)
|
||||
translator.handle(
|
||||
systemFrame('overflow-owned-orphan', {
|
||||
subtype: 'task_notification',
|
||||
task_id: 'owned-terminal-only',
|
||||
parent_task_id: 'owner',
|
||||
status: 'completed'
|
||||
})
|
||||
)
|
||||
const terminal = items().filter(
|
||||
(item) =>
|
||||
item.body.kind === 'message' &&
|
||||
item.body.blocks.some(
|
||||
(block) =>
|
||||
block.type === 'background-task' &&
|
||||
['owned-64', 'owned-terminal-only'].includes(block.taskId)
|
||||
)
|
||||
)
|
||||
expect(terminal).toHaveLength(2)
|
||||
expect(projected(items())).toBe('idle')
|
||||
translator.dispose()
|
||||
})
|
||||
|
||||
@@ -186,6 +186,7 @@ it('uses the matching durable generation ownership when an earlier run receives
|
||||
}
|
||||
})
|
||||
expect(await deferred.drained()).toEqual({ ok: true })
|
||||
expect(journal.runningTurn()).toBeNull()
|
||||
const prior = journal
|
||||
.snapshot()
|
||||
.items.find(
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import { expect, it } from 'vitest'
|
||||
import {
|
||||
adapterFor,
|
||||
claudeFrame,
|
||||
fakeClaude,
|
||||
identityFor
|
||||
} from './claude-structured-session-test-support'
|
||||
import { PARENT_TASK_STOP } from '../claude-parent-task-frames.test-fixture'
|
||||
import { createQueuedMessageTestRig } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig.test-fixture'
|
||||
import { eventually } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-queued-message-rig-provider.test-fixture'
|
||||
import { HOST_TEST_SESSION as SESSION } from '../../native-chat/agent-session-wire/host/testing/structured-agent-session-host-test-data'
|
||||
import type { AgentChildWorkEvidence } from '../../../shared/agent-status/child-work/agent-status-child-work-evidence'
|
||||
|
||||
it('delivers an ordinary send after an owned roster/start refresh without inventing root work', async () => {
|
||||
const claude = fakeClaude()
|
||||
const evidence: AgentChildWorkEvidence[] = []
|
||||
const rig = await createQueuedMessageTestRig({ agent: 'claude' })
|
||||
const adapter = adapterFor(
|
||||
claude,
|
||||
{},
|
||||
[],
|
||||
[],
|
||||
undefined,
|
||||
undefined,
|
||||
(_sessionId, batch) => evidence.push(...batch),
|
||||
(outcome) => {
|
||||
void rig.host.settleLateDispatch({ ...outcome, sessionId: SESSION })
|
||||
}
|
||||
)
|
||||
let acquired = false
|
||||
try {
|
||||
rig.dispatch.mockImplementation(async (input) => {
|
||||
if (!acquired) {
|
||||
await adapter.acquire({
|
||||
identity: identityFor(SESSION),
|
||||
fence: 1,
|
||||
spawnToken: 'spawn-1',
|
||||
events: rig.providerEvents()
|
||||
})
|
||||
acquired = true
|
||||
}
|
||||
return adapter.dispatch({ ...input, sessionId: SESSION, fence: 1 })
|
||||
})
|
||||
const first = rig.send('start the work')
|
||||
expect(await first.result).toMatchObject({ ok: true })
|
||||
await eventually(async () =>
|
||||
expect((await rig.submission(first.id))?.dispatchState).toBe('accepted')
|
||||
)
|
||||
const connection = claude.connections[0]!
|
||||
claudeFrame(connection, {
|
||||
type: 'assistant',
|
||||
uuid: 'root-answer',
|
||||
parent_tool_use_id: null,
|
||||
message: { role: 'assistant', content: [{ type: 'text', text: 'Started.' }] }
|
||||
})
|
||||
claudeFrame(connection, {
|
||||
type: 'result',
|
||||
subtype: 'success',
|
||||
uuid: 'root-result',
|
||||
parent_tool_use_id: null,
|
||||
duration_ms: 1000
|
||||
})
|
||||
await rig.host.collaboratorsForTests().runtimeState.flushAllEventSinks()
|
||||
expect(rig.host.currentWork(SESSION)!.working()).toBe(false)
|
||||
|
||||
// Recorded CLI roster has no tool alias; the following start enriches that same task.
|
||||
const roster = PARENT_TASK_STOP[2]!.frame
|
||||
claudeFrame(connection, {
|
||||
...roster,
|
||||
tasks: Array.isArray(roster.tasks) ? roster.tasks.slice(1) : []
|
||||
})
|
||||
claudeFrame(connection, PARENT_TASK_STOP[3]!.frame)
|
||||
await rig.host.collaboratorsForTests().runtimeState.flushAllEventSinks()
|
||||
const snapshot = await rig.host.journalSnapshot(SESSION)
|
||||
const children = snapshot.items.filter(
|
||||
(item) =>
|
||||
item.body.kind === 'message' &&
|
||||
item.body.blocks.some(
|
||||
(block) => block.type === 'background-task' && block.taskId === 'shell-child'
|
||||
)
|
||||
)
|
||||
expect(children).toHaveLength(1)
|
||||
expect(children[0]).toMatchObject({ agentId: 'shell-child', parentAgentId: 'agent-parent' })
|
||||
expect(evidence).toContainEqual(
|
||||
expect.objectContaining({
|
||||
type: 'live',
|
||||
child: expect.objectContaining({
|
||||
handle: expect.objectContaining({ id: 'shell-child' }),
|
||||
ownerId: 'agent-parent',
|
||||
stoppable: true
|
||||
})
|
||||
})
|
||||
)
|
||||
const rootAfterChild = rig.host
|
||||
.collaboratorsForTests()
|
||||
.sessions.get(SESSION)!
|
||||
.journal.runningTurn()
|
||||
|
||||
const followup = rig.send('continue normally')
|
||||
expect(await followup.result).toMatchObject({ ok: true })
|
||||
await eventually(async () =>
|
||||
expect((await rig.submission(followup.id))?.handedOverAt).toBeDefined()
|
||||
)
|
||||
expect(rootAfterChild).toBeNull()
|
||||
expect(connection.sent).toHaveLength(2)
|
||||
expect(connection.sent[1]).toMatchObject({
|
||||
message: {
|
||||
content: [{ type: 'text', text: 'continue normally' }]
|
||||
}
|
||||
})
|
||||
} finally {
|
||||
await adapter.closeAll()
|
||||
await rig.dispose()
|
||||
claude.dispose()
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user