feat(agent): per-session agent inbox (discussion #558)

A coordinator can leave text for a worker without typing into its pane.

- Store (src/web/agent-inbox.ts): read until ack by id, seen only for the
  receiver, 16 KiB/message, 512 KiB + 200 messages per inbox, full = 409
  CONFLICT (never drops the oldest), 12 h TTL from each message's createdAt.
- Routes (routes/inbox-routes.ts): POST/GET /api/sessions/:id/inbox
  (?wait= parks while nothing is unseen), POST .../inbox/ack (receiver
  only, 403 otherwise), GET /api/agent-inbox/summary (owner-scoped counts,
  not in toState()). Caller = resolveParentSessionId().
- Inbox waiters are the wait registry's third kind: same caps, ended by
  cancelAll/cancelEverything.
- Opt-in snapshot (agentInboxPersistEnabled, synced, default off):
  agent-inbox.json via dataPath(), 0600 tmp+rename, off deletes it, boot
  prune only after a successful restore. Dropped on delete, kept on detach.
- CLI: codeman agent post | inbox [--wait] | ack, INBOX column in ls.
- waitCapacityResponse/abortOnClientHangUp moved to route-helpers.ts
  unchanged.
- Docs: api-reference, README, CLAUDE.md, architecture-invariants, skill +
  plugin mirror.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
RandalixandClaude Opus 5.5 committed 2026-10-23 12:45:48 +02:00
1 parent 002de32a08
commit c6dfaa2cac
34 files changed
+2308 -83

No files matched your search

+63
View File
@@ -1088,3 +1088,66 @@ describe('SessionWaitRegistry: teardown and leak prevention', () => {
expect(reg.totalWaiterCount()).toBe(0);
});
});
describe('inbox waiters', () => {
it('resolve with posted:true on notifyInbox, and only for that session', async () => {
const reg = new SessionWaitRegistry();
const a = reg.waitForInbox('a', { timeoutMs: 5_000 });
const b = reg.waitForInbox('b', { timeoutMs: 50 });
expect(reg.notifyInbox('a')).toBe(1);
await expect(a).resolves.toMatchObject({ posted: true, timedOut: false, ended: false });
await expect(b).resolves.toMatchObject({ posted: false, timedOut: true });
expect(reg.totalWaiterCount()).toBe(0);
});
it('count toward the per-session, per-owner and total caps like the other kinds', async () => {
const reg = new SessionWaitRegistry({ maxWaitersPerSession: 2, maxWaitersPerOwner: 3, maxWaitersTotal: 4 });
const pending = [
reg.waitForSignal('s', { until: ['stop'], timeoutMs: 5_000, owner: 'alice' }),
reg.waitForInbox('s', { timeoutMs: 5_000, owner: 'alice' }),
];
expect(() => reg.assertCapacity('s', 'alice')).toThrow(WaitCapacityError);
expect(() => reg.waitForInbox('s', { timeoutMs: 5_000 })).toThrow(/scope: session/);
pending.push(reg.waitForInbox('t', { timeoutMs: 5_000, owner: 'alice' }));
expect(() => reg.waitForInbox('u', { timeoutMs: 5_000, owner: 'alice' })).toThrow(/scope: owner/);
pending.push(reg.waitForInbox('u', { timeoutMs: 5_000, owner: 'bob' }));
expect(() => reg.waitForInbox('v', { timeoutMs: 5_000 })).toThrow(/scope: total/);
expect(reg.waiterCount('s')).toBe(2);
reg.stop();
await Promise.all(pending);
expect(reg.ownerWaiterCount('alice')).toBe(0);
});
it('counts receiver waiters separately (the summary’s parked)', () => {
const reg = new SessionWaitRegistry();
void reg.waitForInbox('s', { timeoutMs: 5_000, receiver: false });
expect(reg.inboxWaiterCount('s')).toBe(1);
expect(reg.inboxWaiterCount('s', true)).toBe(0);
void reg.waitForInbox('s', { timeoutMs: 5_000, receiver: true });
expect(reg.inboxWaiterCount('s', true)).toBe(1);
reg.stop();
});
it('cancelAll ends them with ended:true, and an abort frees the slot at once', async () => {
const reg = new SessionWaitRegistry();
const ended = reg.waitForInbox('s', { timeoutMs: 5_000 });
expect(reg.cancelAll('s')).toBe(1);
await expect(ended).resolves.toMatchObject({ ended: true, aborted: false, posted: false });
const controller = new AbortController();
const aborted = reg.waitForInbox('s', { timeoutMs: 5_000, abortSignal: controller.signal });
expect(reg.inboxWaiterCount('s')).toBe(1);
controller.abort();
await expect(aborted).resolves.toMatchObject({ ended: true, aborted: true });
expect(reg.inboxWaiterCount('s')).toBe(0);
});
it('after stop() a new inbox wait resolves at once with ended:true', async () => {
const reg = new SessionWaitRegistry();
const parked = reg.waitForInbox('s', { timeoutMs: 5_000 });
reg.stop();
await expect(parked).resolves.toMatchObject({ ended: true });
await expect(reg.waitForInbox('s', { timeoutMs: 5_000 })).resolves.toMatchObject({ ended: true, posted: false });
expect(reg.totalWaiterCount()).toBe(0);
});
});