mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-01 16:59:32 +01:00
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:
1 parent
002de32a08
commit
c6dfaa2cac
34 files changed
+2308
-83
No files matched your search
@@ -7,11 +7,16 @@
|
||||
* lets a request *block* until something happens instead, so an orchestrating
|
||||
* agent can `curl` and wait. Plan: `docs/agent-control-plan.md`.
|
||||
*
|
||||
* Two waiter kinds:
|
||||
* Three waiter kinds:
|
||||
* - **signal waiters** resolve on the first of a requested set of lifecycle
|
||||
* signals (`idle`, `working`, `stop`, `blocked`, `exit`).
|
||||
* - **output waiters** resolve when a literal string appears in a session's
|
||||
* ANSI-stripped output.
|
||||
* - **inbox waiters** resolve when a message is posted to the session's agent
|
||||
* inbox (`agent-inbox.ts`). The registry knows nothing about messages: the
|
||||
* route decides whether to park (nothing unseen) and `notifyInbox()` is the
|
||||
* post's "something arrived". Kept here so `GET .../inbox?wait=` is charged to
|
||||
* the same per-session / per-owner / total caps as the other two.
|
||||
*
|
||||
* ## What "ANSI-stripped output" means here
|
||||
*
|
||||
@@ -390,6 +395,12 @@ export interface OutputWaitResult extends WaitResultBase {
|
||||
snippet: string | null;
|
||||
}
|
||||
|
||||
/** Outcome of an inbox wait. */
|
||||
export interface InboxWaitResult extends WaitResultBase {
|
||||
/** True when a post released the wait (the caller should read the inbox now). */
|
||||
posted: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown when a waiter would exceed a configured cap.
|
||||
*
|
||||
@@ -458,6 +469,15 @@ export interface OutputWaitOptions extends WaitOptionsBase {
|
||||
initialText?: string;
|
||||
}
|
||||
|
||||
export interface InboxWaitOptions extends WaitOptionsBase {
|
||||
/**
|
||||
* The caller IS the receiving session (as resolved by the route), not a coordinator
|
||||
* peeking. Counted separately so the inbox summary's `parked` means "the receiver is
|
||||
* listening", which is what a sender deciding whether to nudge needs to know.
|
||||
*/
|
||||
receiver?: boolean;
|
||||
}
|
||||
|
||||
export interface SessionWaitRegistryOptions {
|
||||
maxWaitersPerSession?: number;
|
||||
maxWaitersPerOwner?: number;
|
||||
@@ -483,6 +503,11 @@ interface SignalWaiter extends WaiterBase {
|
||||
settle: (result: SignalWaitResult) => void;
|
||||
}
|
||||
|
||||
interface InboxWaiter extends WaiterBase {
|
||||
receiver: boolean;
|
||||
settle: (result: InboxWaitResult) => void;
|
||||
}
|
||||
|
||||
interface OutputWaiter extends WaiterBase {
|
||||
/** Original-case needle, kept for reporting. */
|
||||
needle: string;
|
||||
@@ -505,6 +530,7 @@ interface OutputWaiter extends WaiterBase {
|
||||
export class SessionWaitRegistry {
|
||||
private readonly signalWaiters = new Map<string, Set<SignalWaiter>>();
|
||||
private readonly outputWaiters = new Map<string, Set<OutputWaiter>>();
|
||||
private readonly inboxWaiters = new Map<string, Set<InboxWaiter>>();
|
||||
/**
|
||||
* Live waiter count per owner. A derived count would mean walking every waiter in
|
||||
* the process on each registration, so it is maintained incrementally instead:
|
||||
@@ -544,20 +570,34 @@ export class SessionWaitRegistry {
|
||||
return this.outputWaiters.get(sessionId)?.size ?? 0;
|
||||
}
|
||||
|
||||
/** Pending waiters of both kinds for a session. */
|
||||
waiterCount(sessionId: string): number {
|
||||
return this.signalWaiterCount(sessionId) + this.outputWaiterCount(sessionId);
|
||||
/**
|
||||
* Pending inbox waiters for a session; with `receiverOnly`, only those whose caller
|
||||
* is the receiving session itself (the inbox summary's `parked`).
|
||||
*/
|
||||
inboxWaiterCount(sessionId: string, receiverOnly = false): number {
|
||||
const set = this.inboxWaiters.get(sessionId);
|
||||
if (!set) return 0;
|
||||
if (!receiverOnly) return set.size;
|
||||
let count = 0;
|
||||
for (const waiter of set) if (waiter.receiver) count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/** Pending waiters of both kinds across every session. */
|
||||
/** Pending waiters of every kind for a session. */
|
||||
waiterCount(sessionId: string): number {
|
||||
return this.signalWaiterCount(sessionId) + this.outputWaiterCount(sessionId) + this.inboxWaiterCount(sessionId);
|
||||
}
|
||||
|
||||
/** Pending waiters of every kind across every session. */
|
||||
totalWaiterCount(): number {
|
||||
let total = 0;
|
||||
for (const set of this.signalWaiters.values()) total += set.size;
|
||||
for (const set of this.outputWaiters.values()) total += set.size;
|
||||
for (const set of this.inboxWaiters.values()) total += set.size;
|
||||
return total;
|
||||
}
|
||||
|
||||
/** Pending waiters of both kinds charged to one owner. */
|
||||
/** Pending waiters of every kind charged to one owner. */
|
||||
ownerWaiterCount(owner: string): number {
|
||||
return this.ownerCounts.get(owner) ?? 0;
|
||||
}
|
||||
@@ -920,6 +960,105 @@ export class SessionWaitRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Inbox waits ──
|
||||
|
||||
/**
|
||||
* Block until a message is posted to this session's inbox, the timeout elapses, or
|
||||
* the session ends.
|
||||
*
|
||||
* There is no "already satisfied" shortcut here: whether the inbox holds unseen mail
|
||||
* is the store's business, so the route checks that first and only calls this when
|
||||
* there is nothing to hand out. A post that lands between that check and this call
|
||||
* is not lost either: both run in the same tick, and `notifyInbox` is called by the
|
||||
* post route after the message is stored.
|
||||
*
|
||||
* @throws {WaitCapacityError} when a waiter cap is already reached.
|
||||
*/
|
||||
waitForInbox(sessionId: string, options: InboxWaitOptions): Promise<InboxWaitResult> {
|
||||
const base = { posted: false, timedOut: false, immediate: false, waitedMs: 0, timeoutMs: options.timeoutMs };
|
||||
if (options.abortSignal?.aborted) return Promise.resolve({ ...base, ended: true, aborted: true });
|
||||
if (this.stopped) return Promise.resolve({ ...base, ended: true, aborted: false });
|
||||
|
||||
this.assertCapacity(sessionId, options.owner);
|
||||
|
||||
return new Promise<InboxWaitResult>((resolve) => {
|
||||
let set = this.inboxWaiters.get(sessionId);
|
||||
if (!set) {
|
||||
set = new Set<InboxWaiter>();
|
||||
this.inboxWaiters.set(sessionId, set);
|
||||
}
|
||||
|
||||
const waiter: InboxWaiter = {
|
||||
receiver: options.receiver === true,
|
||||
startedAt: Date.now(),
|
||||
timeoutMs: options.timeoutMs,
|
||||
owner: options.owner,
|
||||
timer: setTimeout(() => {
|
||||
this.removeInboxWaiter(sessionId, waiter);
|
||||
resolve({
|
||||
posted: false,
|
||||
timedOut: true,
|
||||
immediate: false,
|
||||
ended: false,
|
||||
aborted: false,
|
||||
waitedMs: Date.now() - waiter.startedAt,
|
||||
timeoutMs: waiter.timeoutMs,
|
||||
});
|
||||
}, options.timeoutMs),
|
||||
settle: resolve,
|
||||
};
|
||||
set.add(waiter);
|
||||
this.chargeOwner(options.owner);
|
||||
waiter.detachAbort = this.attachAbort(options.abortSignal, () => {
|
||||
this.removeInboxWaiter(sessionId, waiter);
|
||||
resolve({
|
||||
posted: false,
|
||||
timedOut: false,
|
||||
immediate: false,
|
||||
ended: true,
|
||||
aborted: true,
|
||||
waitedMs: Date.now() - waiter.startedAt,
|
||||
timeoutMs: waiter.timeoutMs,
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A message was posted to this session's inbox: release every inbox waiter on it.
|
||||
*
|
||||
* @returns how many waiters were resolved.
|
||||
*/
|
||||
notifyInbox(sessionId: string): number {
|
||||
const set = this.inboxWaiters.get(sessionId);
|
||||
if (!set || set.size === 0) return 0;
|
||||
let resolved = 0;
|
||||
for (const waiter of [...set]) {
|
||||
this.removeInboxWaiter(sessionId, waiter);
|
||||
waiter.settle({
|
||||
posted: true,
|
||||
timedOut: false,
|
||||
immediate: false,
|
||||
ended: false,
|
||||
aborted: false,
|
||||
waitedMs: Date.now() - waiter.startedAt,
|
||||
timeoutMs: waiter.timeoutMs,
|
||||
});
|
||||
resolved++;
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
private removeInboxWaiter(sessionId: string, waiter: InboxWaiter): void {
|
||||
clearTimeout(waiter.timer);
|
||||
waiter.detachAbort?.();
|
||||
const set = this.inboxWaiters.get(sessionId);
|
||||
if (!set) return;
|
||||
if (!set.delete(waiter)) return;
|
||||
this.releaseOwner(waiter.owner);
|
||||
if (set.size === 0) this.inboxWaiters.delete(sessionId);
|
||||
}
|
||||
|
||||
// ── Teardown ──
|
||||
|
||||
/**
|
||||
@@ -969,6 +1108,23 @@ export class SessionWaitRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
const inbox = this.inboxWaiters.get(sessionId);
|
||||
if (inbox) {
|
||||
for (const waiter of [...inbox]) {
|
||||
this.removeInboxWaiter(sessionId, waiter);
|
||||
waiter.settle({
|
||||
posted: false,
|
||||
timedOut: false,
|
||||
immediate: false,
|
||||
ended: true,
|
||||
aborted: false,
|
||||
waitedMs: Date.now() - waiter.startedAt,
|
||||
timeoutMs: waiter.timeoutMs,
|
||||
});
|
||||
resolved++;
|
||||
}
|
||||
}
|
||||
|
||||
this.pendingAnsi.delete(sessionId);
|
||||
return resolved;
|
||||
}
|
||||
@@ -999,7 +1155,7 @@ export class SessionWaitRegistry {
|
||||
cancelEverything(): number {
|
||||
this.stopped = true;
|
||||
let resolved = 0;
|
||||
const ids = new Set([...this.signalWaiters.keys(), ...this.outputWaiters.keys()]);
|
||||
const ids = new Set([...this.signalWaiters.keys(), ...this.outputWaiters.keys(), ...this.inboxWaiters.keys()]);
|
||||
for (const id of ids) resolved += this.cancelAll(id);
|
||||
return resolved;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user