feat: per-group queue, SQLite state, graceful shutdown (#111)

* fix: wire up queue processMessagesFn before recovery to prevent silent message loss

recoverPendingMessages() was called after startMessageLoop(), which meant:
1. Recovery could race with the message loop's first iteration
2. processMessagesFn was set inside startMessageLoop, so recovery
   enqueues would fire runForGroup with processMessagesFn still null,
   silently skipping message processing

Move setProcessMessagesFn and recoverPendingMessages before startMessageLoop
so the queue is fully wired before any messages are enqueued.

https://claude.ai/code/session_01PCY8zNjDa2N29jvBAV5vfL

* feat: structured agent output to fix infinite retry on silent responses (#113)

Use Agent SDK's outputFormat with json_schema to get typed responses
from the agent. The agent now returns { status: 'responded' | 'silent',
userMessage?, internalLog? } instead of a plain string. This fixes a
critical bug where a null/empty agent response caused infinite 5-second
retry loops by conflating "nothing to say" with "error".

- Agent runner: add AGENT_RESPONSE_SCHEMA and parse structured_output
- Host: advance lastAgentTimestamp on both responded AND silent status
- GroupQueue: add exponential backoff (5s-80s) with max 5 retries for
  actual errors, replacing unbounded fixed-interval retries

https://claude.ai/code/session_014SLc8MxP9BYhEhDCLox9U8

Co-authored-by: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
gavrielc
2026-02-06 18:54:26 +02:00
committed by GitHub
parent 03df69e9b5
commit ae177156ec
5 changed files with 115 additions and 36 deletions

View File

@@ -17,9 +17,35 @@ interface ContainerInput {
isScheduledTask?: boolean;
}
interface AgentResponse {
status: 'responded' | 'silent';
userMessage?: string;
internalLog?: string;
}
const AGENT_RESPONSE_SCHEMA = {
type: 'object',
properties: {
status: {
type: 'string',
enum: ['responded', 'silent'],
description: 'Use "responded" when you have a message for the user. Use "silent" when the messages don\'t require a response (e.g. the conversation is between other people and doesn\'t involve you, or no trigger/mention was directed at you).',
},
userMessage: {
type: 'string',
description: 'The message to send to the user. Required when status is "responded".',
},
internalLog: {
type: 'string',
description: 'Optional internal note about why you chose this status (for logging, not shown to users).',
},
},
required: ['status'],
} as const;
interface ContainerOutput {
status: 'success' | 'error';
result: string | null;
result: AgentResponse | null;
newSessionId?: string;
error?: string;
}
@@ -222,7 +248,7 @@ async function main(): Promise<void> {
isMain: input.isMain
});
let result: string | null = null;
let result: AgentResponse | null = null;
let newSessionId: string | undefined;
// Add context for scheduled tasks
@@ -253,6 +279,10 @@ async function main(): Promise<void> {
},
hooks: {
PreCompact: [{ hooks: [createPreCompactHook()] }]
},
outputFormat: {
type: 'json_schema',
schema: AGENT_RESPONSE_SCHEMA,
}
}
})) {
@@ -261,15 +291,25 @@ async function main(): Promise<void> {
log(`Session initialized: ${newSessionId}`);
}
if ('result' in message && message.result) {
result = message.result as string;
if (message.type === 'result') {
if (message.subtype === 'success' && message.structured_output) {
result = message.structured_output as AgentResponse;
log(`Agent result: status=${result.status}${result.internalLog ? `, log=${result.internalLog}` : ''}`);
} else if (message.subtype === 'error_max_structured_output_retries') {
// Agent couldn't produce valid structured output — fall back to text result
log('Agent failed to produce structured output, falling back to text');
const textResult = 'result' in message ? (message as { result?: string }).result : null;
if (textResult) {
result = { status: 'responded', userMessage: textResult };
}
}
}
}
log('Agent completed successfully');
writeOutput({
status: 'success',
result,
result: result ?? { status: 'silent' },
newSessionId
});