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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tangle-network/agent-runtime",
"version": "0.5.3",
"version": "0.5.4",
"description": "Reusable runtime lifecycle for domain-specific agents.",
"homepage": "https://github.com/tangle-network/agent-runtime#readme",
"repository": {
Expand Down
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,18 @@ export async function* runAgentTaskStream<TInput extends AgentBackendInput = Age
const message = err instanceof Error ? err.message : String(err)
session = touchSession({ ...session, status: options.signal?.aborted ? 'aborted' : 'failed' })
await store?.put(session)
await options.backend.stop?.(session, message)
let stopErrorMessage: string | undefined
try {
await options.backend.stop?.(session, message)
} catch (stopErr) {
stopErrorMessage = stopErr instanceof Error ? stopErr.message : String(stopErr)
}
const backendError = streamEvent({
type: 'backend_error',
task,
session,
backend: options.backend.kind,
message,
message: stopErrorMessage ? `${message}; backend stop failed: ${stopErrorMessage}` : message,
recoverable: !options.signal?.aborted,
})
await store?.appendEvent?.(session.id, backendError)
Expand All @@ -536,7 +541,9 @@ export async function* runAgentTaskStream<TInput extends AgentBackendInput = Age
const taskEnd = streamEvent({ type: 'task_end', task, status, reason: message })
await store?.appendEvent?.(session.id, taskEnd)
yield taskEnd
yield streamEvent({ type: 'final', task, session, status, reason: message, text: finalText || undefined })
const final = streamEvent({ type: 'final', task, session, status, reason: message, text: finalText || undefined })
await store?.appendEvent?.(session.id, final)
yield final
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ describe('runAgentTask', () => {
})

it('stops a backend and emits failed final event when streaming throws', async () => {
const store = new InMemoryRuntimeSessionStore()
const stopped: string[] = []
const backend: AgentExecutionBackend = {
kind: 'failing-harness',
Expand All @@ -527,9 +528,13 @@ describe('runAgentTask', () => {
const events = await collect(runAgentTaskStream({
task: { id: 'failing-task', intent: 'run', requiredKnowledge: [readyReq] },
backend,
sessionStore: store,
sessionId: 'failing-session',
}))

expect(stopped).toEqual(['sandbox lost'])
expect(store.get('failing-session')?.status).toBe('failed')
expect(store.listEvents('failing-session').at(-1)).toMatchObject({ type: 'final', status: 'failed' })
expect(events.find((event) => event.type === 'backend_error')).toMatchObject({
type: 'backend_error',
backend: 'failing-harness',
Expand All @@ -538,6 +543,32 @@ describe('runAgentTask', () => {
})
expect(events.at(-1)).toMatchObject({ type: 'final', status: 'failed', text: 'partial' })
})

it('preserves the stream failure when backend cleanup also fails', async () => {
const backend: AgentExecutionBackend = {
kind: 'cleanup-fails',
stop: () => {
throw new Error('cleanup refused')
},
async *stream() {
throw new Error('primary stream failure')
},
}
const events = await collect(runAgentTaskStream({
task: { id: 'cleanup-failure-task', intent: 'run', requiredKnowledge: [readyReq] },
backend,
}))

expect(events.find((event) => event.type === 'backend_error')).toMatchObject({
type: 'backend_error',
message: 'primary stream failure; backend stop failed: cleanup refused',
})
expect(events.at(-1)).toMatchObject({
type: 'final',
status: 'failed',
reason: 'primary stream failure',
})
})
})

async function collect(iterable: AsyncIterable<RuntimeStreamEvent>): Promise<RuntimeStreamEvent[]> {
Expand Down