Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(agent-providers): report a faulted session's last events before the host stops

raullopez-sandbox8d9a0fc72707
diff --git a/console/packages/agent-providers/src/host/shared-host.test.ts b/console/packages/agent-providers/src/host/shared-host.test.ts
index 9c38a9eb7..c27148228 100644
--- a/console/packages/agent-providers/src/host/shared-host.test.ts
+++ b/console/packages/agent-providers/src/host/shared-host.test.ts
@@ -1401,3 +1401,37 @@ it('posts for a room turn that ended without the agent posting, and not for one
expect(await host.stop()).toBeNull();
}
});
+
+it('reports the turn a fault ended, and the fault, before it stops', async () => {
+ const host = await start({ rooms: true });
+ await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'hi') });
+ await until(() => expect(host.turns).toHaveLength(1));
+ const turnId = host.turns[0]!.turnId;
+ // Switch is unreachable while the provider fails: nothing goes out until it is back.
+ host.switchCore.state.unavailable = true;
+ host.emit({
+ type: 'turn.completed',
+ turnId,
+ outcome: 'error',
+ message: 'model not supported',
+ usage: [],
+ });
+ host.emit({ type: 'session.state.changed', status: 'error' });
+ await new Promise((resolve) => setTimeout(resolve, 600));
+ host.switchCore.state.unavailable = false;
+
+ const reported = (kind: string, status: string) =>
+ host.switchCore.calls.some((c) => {
+ const row = c.body as { kind: string; status: string; turn_id: string };
+ return (
+ c.path.endsWith('/activity') &&
+ row.kind === kind &&
+ row.status === status &&
+ row.turn_id === turnId
+ );
+ });
+ await until(() => expect(reported('turn', 'error')).toBe(true));
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+ // The host stopped on its own, faulted, once the reports were out.
+ expect(String(await host.stop())).toContain('HOST_FAULTED');
+});
diff --git a/console/packages/agent-providers/src/host/shared-host.ts b/console/packages/agent-providers/src/host/shared-host.ts
index c1b9d1bb5..343b680ea 100644
--- a/console/packages/agent-providers/src/host/shared-host.ts
+++ b/console/packages/agent-providers/src/host/shared-host.ts
@@ -67,6 +67,9 @@ import {
type VendorServers,
} from './vendor-mcp';
+/** How long a faulted host waits for its last reports to reach Switch before it stops. */
+const FINAL_REPORT_MS = 5_000;
+
/**
* A session run on behalf of its agent.
*
@@ -460,6 +463,9 @@ export async function runSharedHost(
await send({ kind: 'activity', row });
return true;
};
+ // One report at a time: the loop below and the last one before a fault share the cursor.
+ let reportChain: Promise<void> = Promise.resolve();
+ const reportInTurn = () => (reportChain = reportChain.catch(() => {}).then(report));
const report = async () => {
if (reportingUnsupported) return;
const { events } = host!.replay(reporter.cursor);
@@ -957,7 +963,7 @@ export async function runSharedHost(
await reportStart();
let force = true;
while (!executionSignal.aborted) {
- await report();
+ await reportInTurn();
await applyOutcomes(force);
force = false;
await waker.idleReporting(250, executionSignal);
@@ -970,10 +976,19 @@ export async function runSharedHost(
if (reportingFailure) throw reportingFailure;
const status = host.snapshot().session.status;
if (status === 'stopped') break;
- if (status === 'error' && !host.resetDecisionPending)
+ if (status === 'error' && !host.resetDecisionPending) {
+ // What faulted it, and the turn that ended with it, are what Switch most
+ // needs to show; stopping would drop them unreported, the turn left running.
+ await Promise.race([
+ reportInTurn().catch((error: unknown) =>
+ console.warn(`Could not report this session's last events: ${String(error)}`)
+ ),
+ delay(FINAL_REPORT_MS, undefined, { signal: executionSignal }).catch(() => {}),
+ ]);
throw new Error(
'HOST_FAULTED: Provider execution failed. Inspect the transcript before recovery.'
);
+ }
if (host.resetDecisionPending) {
heldForDecision = true;
const first = rooms?.pending()[0];