macro-agent 0.1.12 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/agent-manager-v2.d.ts.map +1 -1
- package/dist/agent/agent-manager-v2.js +240 -7
- package/dist/agent/agent-manager-v2.js.map +1 -1
- package/dist/agent/types.d.ts +47 -0
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/agent/types.js.map +1 -1
- package/dist/boot-v2.d.ts +33 -0
- package/dist/boot-v2.d.ts.map +1 -1
- package/dist/boot-v2.js +142 -11
- package/dist/boot-v2.js.map +1 -1
- package/dist/cli/inbox-mcp-proxy.d.ts +36 -0
- package/dist/cli/inbox-mcp-proxy.d.ts.map +1 -0
- package/dist/cli/inbox-mcp-proxy.js +51 -0
- package/dist/cli/inbox-mcp-proxy.js.map +1 -0
- package/dist/dispatch/loadout-translation.d.ts +100 -0
- package/dist/dispatch/loadout-translation.d.ts.map +1 -0
- package/dist/dispatch/loadout-translation.js +90 -0
- package/dist/dispatch/loadout-translation.js.map +1 -0
- package/dist/dispatch/mail-inbound-consumer.d.ts +89 -0
- package/dist/dispatch/mail-inbound-consumer.d.ts.map +1 -0
- package/dist/dispatch/mail-inbound-consumer.js +261 -0
- package/dist/dispatch/mail-inbound-consumer.js.map +1 -0
- package/dist/dispatch/mail-inbound-reuse-consumer.d.ts +75 -0
- package/dist/dispatch/mail-inbound-reuse-consumer.d.ts.map +1 -0
- package/dist/dispatch/mail-inbound-reuse-consumer.js +325 -0
- package/dist/dispatch/mail-inbound-reuse-consumer.js.map +1 -0
- package/dist/dispatch/permission-evaluator.d.ts +68 -0
- package/dist/dispatch/permission-evaluator.d.ts.map +1 -0
- package/dist/dispatch/permission-evaluator.js +159 -0
- package/dist/dispatch/permission-evaluator.js.map +1 -0
- package/dist/dispatch/permission-overlay.d.ts +64 -0
- package/dist/dispatch/permission-overlay.d.ts.map +1 -0
- package/dist/dispatch/permission-overlay.js +72 -0
- package/dist/dispatch/permission-overlay.js.map +1 -0
- package/dist/dispatch/permissions-handler.d.ts +71 -0
- package/dist/dispatch/permissions-handler.d.ts.map +1 -0
- package/dist/dispatch/permissions-handler.js +83 -0
- package/dist/dispatch/permissions-handler.js.map +1 -0
- package/dist/dispatch/spawn-agent-handler.d.ts +84 -0
- package/dist/dispatch/spawn-agent-handler.d.ts.map +1 -0
- package/dist/dispatch/spawn-agent-handler.js +85 -0
- package/dist/dispatch/spawn-agent-handler.js.map +1 -0
- package/dist/lifecycle/handlers-v2.d.ts +7 -0
- package/dist/lifecycle/handlers-v2.d.ts.map +1 -1
- package/dist/lifecycle/handlers-v2.js +27 -0
- package/dist/lifecycle/handlers-v2.js.map +1 -1
- package/dist/map/lifecycle-bridge.d.ts +18 -0
- package/dist/map/lifecycle-bridge.d.ts.map +1 -1
- package/dist/map/lifecycle-bridge.js +23 -1
- package/dist/map/lifecycle-bridge.js.map +1 -1
- package/dist/map/mail-bridge.d.ts +55 -0
- package/dist/map/mail-bridge.d.ts.map +1 -0
- package/dist/map/mail-bridge.js +115 -0
- package/dist/map/mail-bridge.js.map +1 -0
- package/dist/map/sidecar.d.ts.map +1 -1
- package/dist/map/sidecar.js +245 -1
- package/dist/map/sidecar.js.map +1 -1
- package/dist/map/types.d.ts +15 -0
- package/dist/map/types.d.ts.map +1 -1
- package/dist/mcp/tools/done-v2.d.ts.map +1 -1
- package/dist/mcp/tools/done-v2.js +1 -0
- package/dist/mcp/tools/done-v2.js.map +1 -1
- package/dist/teams/team-loader.d.ts.map +1 -1
- package/dist/teams/team-loader.js.map +1 -1
- package/dist/teams/team-runtime-v2.d.ts.map +1 -1
- package/dist/teams/team-runtime-v2.js +2 -0
- package/dist/teams/team-runtime-v2.js.map +1 -1
- package/package.json +6 -5
- package/src/agent/__tests__/agent-manager-v2.permission-interception.test.ts +296 -0
- package/src/agent/__tests__/agent-manager-v2.permissions.test.ts +233 -0
- package/src/agent/agent-manager-v2.ts +268 -8
- package/src/agent/types.ts +51 -0
- package/src/boot-v2.ts +190 -12
- package/src/cli/inbox-mcp-proxy.ts +56 -0
- package/src/dispatch/CLAUDE.md +129 -0
- package/src/dispatch/__tests__/loadout-translation.test.ts +141 -0
- package/src/dispatch/__tests__/mail-inbound-consumer.integration.test.ts +519 -0
- package/src/dispatch/__tests__/mail-inbound-consumer.test.ts +589 -0
- package/src/dispatch/__tests__/mail-inbound-reuse-consumer.test.ts +575 -0
- package/src/dispatch/__tests__/permission-evaluator.test.ts +196 -0
- package/src/dispatch/__tests__/permission-overlay.test.ts +56 -0
- package/src/dispatch/__tests__/permissions-handler.test.ts +168 -0
- package/src/dispatch/__tests__/spawn-agent-handler.test.ts +282 -0
- package/src/dispatch/loadout-translation.ts +138 -0
- package/src/dispatch/mail-inbound-consumer.ts +397 -0
- package/src/dispatch/mail-inbound-reuse-consumer.ts +479 -0
- package/src/dispatch/permission-evaluator.ts +191 -0
- package/src/dispatch/permission-overlay.ts +89 -0
- package/src/dispatch/permissions-handler.ts +112 -0
- package/src/dispatch/spawn-agent-handler.ts +160 -0
- package/src/lifecycle/handlers-v2.ts +34 -0
- package/src/map/__tests__/lifecycle-bridge.test.ts +64 -0
- package/src/map/__tests__/mail-bridge.test.ts +196 -0
- package/src/map/lifecycle-bridge.ts +48 -2
- package/src/map/mail-bridge.ts +203 -0
- package/src/map/sidecar.ts +346 -1
- package/src/map/types.ts +21 -0
- package/src/mcp/tools/done-v2.ts +1 -0
- package/src/teams/team-loader.ts +3 -1
- package/src/teams/team-runtime-v2.ts +2 -0
- package/dist/workspace/dataplane-adapter.d.ts +0 -260
- package/dist/workspace/dataplane-adapter.d.ts.map +0 -1
- package/dist/workspace/dataplane-adapter.js +0 -416
- package/dist/workspace/dataplane-adapter.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadout-translation.js","sourceRoot":"","sources":["../../src/dispatch/loadout-translation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAu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G,EAAE,EAAE,IAAI,GAAG,EAAE,EAAE,GAAG,GAAG,EAAE,EAAE,GAAG,KAAK,CAAC;IAClD,OAAO,cAAc;QACnB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,EAAE,GAAG,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC,EAAE;QAChD,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,GAAG,CAAC,EAAE,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mail-Inbound Consumer
|
|
3
|
+
*
|
|
4
|
+
* Receives hub-driven `x-dispatch/work` envelopes from the local agent-inbox
|
|
5
|
+
* and spawns a worker agent to handle them — without requiring the optional
|
|
6
|
+
* outbound swarm-dispatch orchestrator (`config.dispatch.enabled`).
|
|
7
|
+
*
|
|
8
|
+
* This makes mail-inbound dispatch a **default capability** of macro-agent: as
|
|
9
|
+
* long as the MAP sidecar is connected and the inbox is running, any hub that
|
|
10
|
+
* delivers work via `mail/turn.received` will be served.
|
|
11
|
+
*
|
|
12
|
+
* ## Data flow
|
|
13
|
+
*
|
|
14
|
+
* hub sends `mail/turn.received`
|
|
15
|
+
* → mail-bridge translates {type,body} → {schema,data} + _conversationId
|
|
16
|
+
* → inboxAdapter delivers to local inbox (recipient = dispatcherAgentId)
|
|
17
|
+
* → inbox.events fires "inbox.message"
|
|
18
|
+
* → consumer classifies: schema === 'x-dispatch/work'?
|
|
19
|
+
* yes → spawn worker via agentManager.spawn()
|
|
20
|
+
* → record agentId → conversationId in side map
|
|
21
|
+
* → worker calls done(summary="…SENTINEL…")
|
|
22
|
+
* → handlers-v2 stores _lastSummary in agentStore metadata (parentId null branch)
|
|
23
|
+
* → agentManager.onLifecycleEvent fires "stopped"
|
|
24
|
+
* → consumer reads _lastSummary + conversationId
|
|
25
|
+
* → mapSidecar.postMailTurn(conversationId, agentId, summary) [fire-and-forget]
|
|
26
|
+
*
|
|
27
|
+
* @module dispatch/mail-inbound-consumer
|
|
28
|
+
*/
|
|
29
|
+
import type { AgentManager } from "../agent/agent-manager.js";
|
|
30
|
+
import type { AgentStore } from "../agent/agent-store.js";
|
|
31
|
+
export interface InboxEvents {
|
|
32
|
+
on(event: "inbox.message", listener: (event: InboxMessageEvent) => void): void;
|
|
33
|
+
off?(event: "inbox.message", listener: (event: InboxMessageEvent) => void): void;
|
|
34
|
+
removeListener?(event: "inbox.message", listener: (event: InboxMessageEvent) => void): void;
|
|
35
|
+
}
|
|
36
|
+
export interface InboxMessageEvent {
|
|
37
|
+
/** The inbox recipient agent ID. */
|
|
38
|
+
agentId: string;
|
|
39
|
+
message: {
|
|
40
|
+
id?: string;
|
|
41
|
+
content?: unknown;
|
|
42
|
+
sender_id?: string;
|
|
43
|
+
thread_tag?: string;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export interface MailInboundSidecar {
|
|
47
|
+
postMailTurn?(conversationId: string, participantId: string, content: string): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
export interface MailInboundConsumerOptions {
|
|
50
|
+
/**
|
|
51
|
+
* The inbox agent ID that mail-bridge delivers envelopes to.
|
|
52
|
+
* Typically `dispatcher:<claimantId>` when the outbound orchestrator
|
|
53
|
+
* is also running, or a dedicated ID when it is not.
|
|
54
|
+
*/
|
|
55
|
+
dispatcherAgentId: string;
|
|
56
|
+
/** Raw inbox event emitter (from inboxAdapter.getInbox().events). */
|
|
57
|
+
inboxEvents: InboxEvents;
|
|
58
|
+
/** Agent lifecycle manager — used to spawn workers. */
|
|
59
|
+
agentManager: AgentManager;
|
|
60
|
+
/** Agent store — used to read _lastSummary after the agent stops. */
|
|
61
|
+
agentStore: AgentStore;
|
|
62
|
+
/**
|
|
63
|
+
* Optional sidecar reference. Populated after step 13 in boot-v2 via
|
|
64
|
+
* the shared systemRef — the consumer accesses it lazily at reply time
|
|
65
|
+
* so it works even though the sidecar is created after the consumer.
|
|
66
|
+
*/
|
|
67
|
+
getSidecar: () => MailInboundSidecar | null | undefined;
|
|
68
|
+
/** Optional logger (default: console.log). */
|
|
69
|
+
log?: (msg: string) => void;
|
|
70
|
+
}
|
|
71
|
+
export interface MailInboundConsumerStats {
|
|
72
|
+
/** Count of envelopes dropped because they lacked a taskId. */
|
|
73
|
+
droppedMalformed: number;
|
|
74
|
+
/** Number of distinct taskIds currently tracked for dedup. */
|
|
75
|
+
seenTaskIds: number;
|
|
76
|
+
}
|
|
77
|
+
export interface MailInboundConsumer {
|
|
78
|
+
stop(): void;
|
|
79
|
+
/** Snapshot of consumer-level counters for observability. */
|
|
80
|
+
stats(): MailInboundConsumerStats;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Wire the mail-inbound consumer.
|
|
84
|
+
*
|
|
85
|
+
* Returns a `stop()` handle that detaches all listeners.
|
|
86
|
+
* Safe to call multiple times (idempotent cleanup).
|
|
87
|
+
*/
|
|
88
|
+
export declare function createMailInboundConsumer(opts: MailInboundConsumerOptions): MailInboundConsumer;
|
|
89
|
+
//# sourceMappingURL=mail-inbound-consumer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mail-inbound-consumer.d.ts","sourceRoot":"","sources":["../../src/dispatch/mail-inbound-consumer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAQ1D,MAAM,WAAW,WAAW;IAC1B,EAAE,CAAC,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,GAAG,IAAI,CAAC;IAC/E,GAAG,CAAC,CAAC,KAAK,EAAE,eAAe,EAAE,QAAQ,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BAAyB,CACvC,IAAI,EAAE,0BAA0B,GAC/B,mBAAmB,CAwRrB"}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mail-Inbound Consumer
|
|
3
|
+
*
|
|
4
|
+
* Receives hub-driven `x-dispatch/work` envelopes from the local agent-inbox
|
|
5
|
+
* and spawns a worker agent to handle them — without requiring the optional
|
|
6
|
+
* outbound swarm-dispatch orchestrator (`config.dispatch.enabled`).
|
|
7
|
+
*
|
|
8
|
+
* This makes mail-inbound dispatch a **default capability** of macro-agent: as
|
|
9
|
+
* long as the MAP sidecar is connected and the inbox is running, any hub that
|
|
10
|
+
* delivers work via `mail/turn.received` will be served.
|
|
11
|
+
*
|
|
12
|
+
* ## Data flow
|
|
13
|
+
*
|
|
14
|
+
* hub sends `mail/turn.received`
|
|
15
|
+
* → mail-bridge translates {type,body} → {schema,data} + _conversationId
|
|
16
|
+
* → inboxAdapter delivers to local inbox (recipient = dispatcherAgentId)
|
|
17
|
+
* → inbox.events fires "inbox.message"
|
|
18
|
+
* → consumer classifies: schema === 'x-dispatch/work'?
|
|
19
|
+
* yes → spawn worker via agentManager.spawn()
|
|
20
|
+
* → record agentId → conversationId in side map
|
|
21
|
+
* → worker calls done(summary="…SENTINEL…")
|
|
22
|
+
* → handlers-v2 stores _lastSummary in agentStore metadata (parentId null branch)
|
|
23
|
+
* → agentManager.onLifecycleEvent fires "stopped"
|
|
24
|
+
* → consumer reads _lastSummary + conversationId
|
|
25
|
+
* → mapSidecar.postMailTurn(conversationId, agentId, summary) [fire-and-forget]
|
|
26
|
+
*
|
|
27
|
+
* @module dispatch/mail-inbound-consumer
|
|
28
|
+
*/
|
|
29
|
+
import { loadoutToSpawnOptions } from "./loadout-translation.js";
|
|
30
|
+
// ─────────────────────────────────────────────────────────────────
|
|
31
|
+
// Implementation
|
|
32
|
+
// ─────────────────────────────────────────────────────────────────
|
|
33
|
+
/**
|
|
34
|
+
* Wire the mail-inbound consumer.
|
|
35
|
+
*
|
|
36
|
+
* Returns a `stop()` handle that detaches all listeners.
|
|
37
|
+
* Safe to call multiple times (idempotent cleanup).
|
|
38
|
+
*/
|
|
39
|
+
export function createMailInboundConsumer(opts) {
|
|
40
|
+
const { dispatcherAgentId, inboxEvents, agentManager, agentStore, getSidecar, log = (msg) => console.log(msg), } = opts;
|
|
41
|
+
// ── Side-channel maps ────────────────────────────────────────
|
|
42
|
+
// agentId → conversationId: populated when a worker is spawned for a
|
|
43
|
+
// mail-inbound envelope; read when the agent's stopped event fires.
|
|
44
|
+
const agentConversationMap = new Map();
|
|
45
|
+
// taskId → expiresAt: idempotency guard keyed on the dispatch envelope's
|
|
46
|
+
// task identifier. The local inbox can re-fire `inbox.message` for the
|
|
47
|
+
// same logical delivery — without this guard, a single bridged turn would
|
|
48
|
+
// trigger N concurrent spawn() calls, each producing a long-lived ACP
|
|
49
|
+
// subprocess.
|
|
50
|
+
//
|
|
51
|
+
// Bounded by TTL so the map cannot grow unbounded over a long-running
|
|
52
|
+
// deployment. SEEN_TASK_TTL_MS is generous (1 hour) — re-deliveries within
|
|
53
|
+
// that window are dropped, beyond it the dedup expires and a stale retry
|
|
54
|
+
// could legitimately re-spawn (preferable to permanent memory growth).
|
|
55
|
+
const SEEN_TASK_TTL_MS = 60 * 60 * 1000;
|
|
56
|
+
const seenTaskIds = new Map();
|
|
57
|
+
function pruneSeenTaskIds() {
|
|
58
|
+
const now = Date.now();
|
|
59
|
+
for (const [id, expiresAt] of seenTaskIds) {
|
|
60
|
+
if (expiresAt <= now)
|
|
61
|
+
seenTaskIds.delete(id);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// Counter for envelopes dropped because they are malformed (no taskId).
|
|
65
|
+
// Surfaced via the consumer handle's stats() method so operators can
|
|
66
|
+
// distinguish "no work" from "work is broken".
|
|
67
|
+
let droppedMalformedCount = 0;
|
|
68
|
+
log(`[mail-inbound] Consumer ready — listening for x-dispatch/work envelopes ` +
|
|
69
|
+
`(recipient=${dispatcherAgentId})`);
|
|
70
|
+
// ── Inbox message listener ───────────────────────────────────
|
|
71
|
+
const onMessage = (event) => {
|
|
72
|
+
// Only handle messages delivered to our dispatcher recipient.
|
|
73
|
+
if (event.agentId !== dispatcherAgentId)
|
|
74
|
+
return;
|
|
75
|
+
const content = event.message?.content;
|
|
76
|
+
if (content?.schema !== "x-dispatch/work")
|
|
77
|
+
return;
|
|
78
|
+
const data = content.data;
|
|
79
|
+
if (!data?.taskId) {
|
|
80
|
+
droppedMalformedCount++;
|
|
81
|
+
log(`[mail-inbound] Dropping malformed envelope (no taskId, total=${droppedMalformedCount}) — ` +
|
|
82
|
+
`keys=${Object.keys(data ?? {}).join(',')} from=${event.message?.sender_id ?? '?'}`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const taskId = data.taskId;
|
|
86
|
+
pruneSeenTaskIds();
|
|
87
|
+
const seenExpiresAt = seenTaskIds.get(taskId);
|
|
88
|
+
if (seenExpiresAt !== undefined && seenExpiresAt > Date.now()) {
|
|
89
|
+
// Already spawned a worker for this dispatch within the dedup window
|
|
90
|
+
// — silently ignore the re-delivery. The hub treats dispatch as
|
|
91
|
+
// exactly-once on the worker side, so dropping is correct.
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
seenTaskIds.set(taskId, Date.now() + SEEN_TASK_TTL_MS);
|
|
95
|
+
const conversationId = content._conversationId;
|
|
96
|
+
const prompt = data.prompt ?? data.content ?? "";
|
|
97
|
+
// Validate the envelope's role against the local role registry. Unknown
|
|
98
|
+
// role names (e.g. team-role-ref roles like 'executor' surfaced by hubs
|
|
99
|
+
// that don't share macro-agent's role taxonomy) silently fall back to
|
|
100
|
+
// GenericRole inside `resolveRole`, which has `lifecycle.type='persistent'`
|
|
101
|
+
// and no system-prompt instruction to call `done()`. That breaks the
|
|
102
|
+
// mail-reply path because the worker stops without writing
|
|
103
|
+
// `_lastSummary`, so we end up logging "Worker stopped but _lastSummary
|
|
104
|
+
// is empty — no reply turn posted" and the hub never sees the answer.
|
|
105
|
+
//
|
|
106
|
+
// Use 'worker' as the fallback (ephemeral lifecycle + LIFECYCLE.DONE
|
|
107
|
+
// capability + system prompt that mandates `done()`) so unknown roles
|
|
108
|
+
// get a sensible default that completes the reply round-trip.
|
|
109
|
+
const requestedRole = data.role;
|
|
110
|
+
const roleRegistry = agentManager.getRoleRegistry?.();
|
|
111
|
+
const knownRole = requestedRole && roleRegistry?.getRole(requestedRole) !== undefined;
|
|
112
|
+
const role = knownRole ? requestedRole : "worker";
|
|
113
|
+
if (requestedRole && !knownRole) {
|
|
114
|
+
log(`[mail-inbound] Unknown role '${requestedRole}' for taskId=${taskId} — falling back to 'worker'`);
|
|
115
|
+
}
|
|
116
|
+
// Loadout-derived structured fields ride in the envelope. We prefer the
|
|
117
|
+
// canonical top-level `data.loadout` slot (Step 3 of the ACP+lifecycle
|
|
118
|
+
// plan) but fall back to the legacy `data.metadata.permissions` shape
|
|
119
|
+
// for one deprecation cycle so older hubs that haven't rolled the new
|
|
120
|
+
// wire shape continue to work.
|
|
121
|
+
//
|
|
122
|
+
// `loadoutToSpawnOptions` is shared with the new `dispatch/spawn-agent`
|
|
123
|
+
// MAP handler so both wire paths produce identical spawn options.
|
|
124
|
+
//
|
|
125
|
+
// `fullAutonomous: true` because mail-inbound workers have no human in
|
|
126
|
+
// the loop to answer `ask` rules — collapse them to `allow` (vs. the
|
|
127
|
+
// safer `deny` default for spawns where a human might still be reached).
|
|
128
|
+
let wireLoadout = data.loadout;
|
|
129
|
+
if (!wireLoadout) {
|
|
130
|
+
const legacyPermissions = data.metadata?.permissions;
|
|
131
|
+
const legacyMcpProviders = data.metadata?.mcpProviders;
|
|
132
|
+
if (legacyPermissions || legacyMcpProviders) {
|
|
133
|
+
wireLoadout = {
|
|
134
|
+
...(legacyPermissions ? { permissions: legacyPermissions } : {}),
|
|
135
|
+
...(legacyMcpProviders ? { mcpProviders: legacyMcpProviders } : {}),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const spawnLoadoutOpts = loadoutToSpawnOptions(wireLoadout, {
|
|
140
|
+
fullAutonomous: true,
|
|
141
|
+
});
|
|
142
|
+
log(`[mail-inbound] Received x-dispatch/work taskId=${taskId} ` +
|
|
143
|
+
`conv=${conversationId ?? "(none)"} role=${role}` +
|
|
144
|
+
(spawnLoadoutOpts.permissions
|
|
145
|
+
? ` permissions=${JSON.stringify(spawnLoadoutOpts.permissions)}`
|
|
146
|
+
: ""));
|
|
147
|
+
// Spawn is async — fire and forget. Errors are logged, not thrown.
|
|
148
|
+
agentManager
|
|
149
|
+
.spawn({
|
|
150
|
+
task: prompt,
|
|
151
|
+
task_id: taskId,
|
|
152
|
+
role,
|
|
153
|
+
parent: null,
|
|
154
|
+
// Mail-inbound dispatch workers run sandboxed — strip the host's
|
|
155
|
+
// user-level Claude setting sources so installed plugin MCP servers
|
|
156
|
+
// (claude-code-swarm, oh-my-claudecode, …) don't auto-load and hang
|
|
157
|
+
// session/new on environments where the host services aren't reachable.
|
|
158
|
+
isolatedSettings: true,
|
|
159
|
+
...spawnLoadoutOpts,
|
|
160
|
+
})
|
|
161
|
+
.then(async (spawned) => {
|
|
162
|
+
log(`[mail-inbound] Spawned worker agentId=${spawned.id} for taskId=${taskId}`);
|
|
163
|
+
if (conversationId) {
|
|
164
|
+
agentConversationMap.set(spawned.id, conversationId);
|
|
165
|
+
}
|
|
166
|
+
// Spawn only creates an idle ACP session — the task lives in the
|
|
167
|
+
// system prompt as instructions. To get the model to actually do
|
|
168
|
+
// the work, send the prompt as a user message via promptUntilDone.
|
|
169
|
+
// This drives the worker to completion (done() called) so the
|
|
170
|
+
// lifecycle stopped listener below fires and posts the reply
|
|
171
|
+
// back to the hub. Fire-and-forget; errors are logged.
|
|
172
|
+
try {
|
|
173
|
+
await agentManager.promptUntilDone(spawned.id, prompt, {
|
|
174
|
+
maxFollowUps: 0,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
log(`[mail-inbound] promptUntilDone failed for agentId=${spawned.id}: ` +
|
|
179
|
+
`${err.message ?? String(err)}`);
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
.catch((err) => {
|
|
183
|
+
log(`[mail-inbound] Spawn failed for taskId=${taskId}: ${err.message ?? String(err)}`);
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
inboxEvents.on("inbox.message", onMessage);
|
|
187
|
+
// ── Lifecycle stopped listener ───────────────────────────────
|
|
188
|
+
const unsubscribeLifecycle = agentManager.onLifecycleEvent((event) => {
|
|
189
|
+
if (event.type !== "stopped")
|
|
190
|
+
return;
|
|
191
|
+
const agentId = event.agent.id;
|
|
192
|
+
const conversationId = agentConversationMap.get(agentId);
|
|
193
|
+
if (!conversationId)
|
|
194
|
+
return; // not a mail-inbound worker we spawned
|
|
195
|
+
agentConversationMap.delete(agentId);
|
|
196
|
+
// Read the summary stored by handlers-v2 for parentless workers.
|
|
197
|
+
const record = agentStore.getAgent(agentId);
|
|
198
|
+
const summary = record?.metadata?._lastSummary;
|
|
199
|
+
if (!summary) {
|
|
200
|
+
log(`[mail-inbound] Worker agentId=${agentId} stopped but _lastSummary is empty — ` +
|
|
201
|
+
`no reply turn posted`);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
log(`[mail-inbound] Worker agentId=${agentId} stopped — posting reply to ` +
|
|
205
|
+
`conv=${conversationId}`);
|
|
206
|
+
const sidecar = getSidecar();
|
|
207
|
+
if (!sidecar?.postMailTurn) {
|
|
208
|
+
log(`[mail-inbound] No sidecar/postMailTurn — reply turn dropped`);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
sidecar.postMailTurn(conversationId, agentId, summary)
|
|
212
|
+
.then(() => {
|
|
213
|
+
// Clear the stored summary so it can't replay if the same agentId
|
|
214
|
+
// is ever reused for another dispatch (the AgentManager generally
|
|
215
|
+
// mints fresh ids, but this is cheap insurance against a future
|
|
216
|
+
// change).
|
|
217
|
+
try {
|
|
218
|
+
const existingMeta = agentStore.getAgent(agentId)?.metadata ?? {};
|
|
219
|
+
const { _lastSummary: _drop, ...rest } = existingMeta;
|
|
220
|
+
void _drop;
|
|
221
|
+
agentStore.updateAgent(agentId, { metadata: rest });
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
// best-effort — store may be closing during shutdown
|
|
225
|
+
}
|
|
226
|
+
})
|
|
227
|
+
.catch(() => {
|
|
228
|
+
// best-effort — hub may be temporarily unreachable
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
// ── Cleanup ──────────────────────────────────────────────────
|
|
232
|
+
let stopped = false;
|
|
233
|
+
return {
|
|
234
|
+
stop() {
|
|
235
|
+
if (stopped)
|
|
236
|
+
return;
|
|
237
|
+
stopped = true;
|
|
238
|
+
try {
|
|
239
|
+
if (inboxEvents.off) {
|
|
240
|
+
inboxEvents.off("inbox.message", onMessage);
|
|
241
|
+
}
|
|
242
|
+
else if (inboxEvents.removeListener) {
|
|
243
|
+
inboxEvents.removeListener("inbox.message", onMessage);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
// best effort
|
|
248
|
+
}
|
|
249
|
+
unsubscribeLifecycle();
|
|
250
|
+
log(`[mail-inbound] Consumer stopped`);
|
|
251
|
+
},
|
|
252
|
+
stats() {
|
|
253
|
+
pruneSeenTaskIds();
|
|
254
|
+
return {
|
|
255
|
+
droppedMalformed: droppedMalformedCount,
|
|
256
|
+
seenTaskIds: seenTaskIds.size,
|
|
257
|
+
};
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
//# sourceMappingURL=mail-inbound-consumer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mail-inbound-consumer.js","sourceRoot":"","sources":["../../src/dispatch/mail-inbound-consumer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAIH,OAAO,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AAE,GAAG,YAAuC,CAAC;gBACjF,KAAK,KAAK,CAAC;gBACX,UAAU,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;YACtD,CAAC;YAAC,MAAM,CAAC;gBACP,qDAAqD;YACvD,CAAC;QACH,CAAC,CAAC;aACD,KAAK,CAAC,GAAG,EAAE;YACV,mDAAmD;QACrD,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,gEAAgE;IAChE,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,OAAO;QACL,IAAI;YACF,IAAI,OAAO;gBAAE,OAAO;YACpB,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,CAAC;gBACH,IAAI,WAAW,CAAC,GAAG,EAAE,CAAC;oBACpB,WAAW,CAAC,GAAG,CAAC,eAAe,EAAE,SAAS,CAAC,CAAC;gBAC9C,CAAC;qBAAM,IAAI,WAAW,CAAC,cAAc,EAAE,CAAC;oBACtC,WAAW,CAAC,cAAc,CAAC,eAAe,EAAE,SAAS,CAAC,CAAC;gBACzD,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,cAAc;YAChB,CAAC;YACD,oBAAoB,EAAE,CAAC;YACvB,GAAG,CAAC,iCAAiC,CAAC,CAAC;QACzC,CAAC;QACD,KAAK;YACH,gBAAgB,EAAE,CAAC;YACnB,OAAO;gBACL,gBAAgB,EAAE,qBAAqB;gBACvC,WAAW,EAAE,WAAW,CAAC,IAAI;aAC9B,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mail-Inbound Reuse Consumer
|
|
3
|
+
*
|
|
4
|
+
* Receives hub-driven `x-dispatch/work` envelopes addressed to **non-sidecar**
|
|
5
|
+
* agents — long-lived team workers, coordinators, etc. — and drives them
|
|
6
|
+
* through the dispatch turn using their existing session, then posts the
|
|
7
|
+
* summary back as a mail turn.
|
|
8
|
+
*
|
|
9
|
+
* Mirrors `mail-inbound-consumer.ts` but with three semantic differences:
|
|
10
|
+
*
|
|
11
|
+
* 1. Filters envelopes addressed to ANY non-sidecar agent (the existing
|
|
12
|
+
* consumer filters for the dispatcher recipient).
|
|
13
|
+
* 2. Does **not** spawn — it drives the existing agent's session via
|
|
14
|
+
* `agentManager.prompt(agentId, prompt)` and watches for `done()` in
|
|
15
|
+
* the update stream.
|
|
16
|
+
* 3. Tracks `inflightDispatches` per agentId. A second envelope arriving
|
|
17
|
+
* while the same agent is already processing a dispatch is rejected
|
|
18
|
+
* with `recipient_busy` so the orchestrator can retry against another
|
|
19
|
+
* agent (or fall back to fresh-spawn). Reject is **dispatch-scoped**
|
|
20
|
+
* — non-dispatch work on the agent (peer messages, user chat) does
|
|
21
|
+
* NOT trigger the busy reject; that work stacks naturally.
|
|
22
|
+
*
|
|
23
|
+
* Reply path: captures `args.summary` from the done() tool call's rawInput
|
|
24
|
+
* directly off the update stream, so it works for both parented and
|
|
25
|
+
* parentless target agents (the parented branch in `handlers-v2` does NOT
|
|
26
|
+
* stash `_lastSummary` — only parentless agents do — but we don't need
|
|
27
|
+
* that path because we observe done() in-stream).
|
|
28
|
+
*
|
|
29
|
+
* @module dispatch/mail-inbound-reuse-consumer
|
|
30
|
+
*/
|
|
31
|
+
import type { AgentManager } from "../agent/agent-manager.js";
|
|
32
|
+
import type { AgentStore } from "../agent/agent-store.js";
|
|
33
|
+
import type { InboxEvents, MailInboundSidecar } from "./mail-inbound-consumer.js";
|
|
34
|
+
export interface MailInboundReuseConsumerOptions {
|
|
35
|
+
/**
|
|
36
|
+
* The sidecar agent ID. Envelopes addressed to this id are handled by
|
|
37
|
+
* the original `mail-inbound-consumer` (fresh-spawn path); the reuse
|
|
38
|
+
* consumer ignores them so the two consumers' filters don't overlap.
|
|
39
|
+
*/
|
|
40
|
+
dispatcherAgentId: string;
|
|
41
|
+
/** Raw inbox event emitter (from inboxAdapter.getInbox().events). */
|
|
42
|
+
inboxEvents: InboxEvents;
|
|
43
|
+
/** Agent lifecycle manager — used to drive the existing session. */
|
|
44
|
+
agentManager: AgentManager;
|
|
45
|
+
/** Agent store — used to confirm the target agent is running. */
|
|
46
|
+
agentStore: AgentStore;
|
|
47
|
+
/**
|
|
48
|
+
* Optional sidecar reference. Populated after step 13 in boot-v2 via
|
|
49
|
+
* the shared systemRef. The consumer accesses it lazily at reply time.
|
|
50
|
+
*/
|
|
51
|
+
getSidecar: () => MailInboundSidecar | null | undefined;
|
|
52
|
+
/** Optional logger (default: console.log). */
|
|
53
|
+
log?: (msg: string) => void;
|
|
54
|
+
}
|
|
55
|
+
export interface MailInboundReuseConsumerStats {
|
|
56
|
+
/** Count of envelopes dropped because they lacked a taskId. */
|
|
57
|
+
droppedMalformed: number;
|
|
58
|
+
/** Number of distinct taskIds currently tracked for dedup. */
|
|
59
|
+
seenTaskIds: number;
|
|
60
|
+
/** Number of rejects emitted because the target agent was already busy with a dispatch. */
|
|
61
|
+
busyRejects: number;
|
|
62
|
+
/** Currently in-flight dispatches keyed by agentId. */
|
|
63
|
+
inflightCount: number;
|
|
64
|
+
}
|
|
65
|
+
export interface MailInboundReuseConsumer {
|
|
66
|
+
stop(): void;
|
|
67
|
+
stats(): MailInboundReuseConsumerStats;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Wire the mail-inbound reuse consumer.
|
|
71
|
+
*
|
|
72
|
+
* Returns a `stop()` handle that detaches the inbox listener.
|
|
73
|
+
*/
|
|
74
|
+
export declare function createMailInboundReuseConsumer(opts: MailInboundReuseConsumerOptions): MailInboundReuseConsumer;
|
|
75
|
+
//# sourceMappingURL=mail-inbound-reuse-consumer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mail-inbound-reuse-consumer.d.ts","sourceRoot":"","sources":["../../src/dispatch/mail-inbound-reuse-consumer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAE1D,OAAO,KAAK,EACV,WAAW,EAEX,kBAAkB,EACnB,MAAM,4BAA4B,CAAC;AAUpC,MAAM,WAAW,+BAA+B;IAC9C;;;;OAIG;IACH,iBAAiB,EAAE,MAAM,CAAC;IAE1B,qEAAqE;IACrE,WAAW,EAAE,WAAW,CAAC;IAEzB,oEAAoE;IACpE,YAAY,EAAE,YAAY,CAAC;IAE3B,iEAAiE;IACjE,UAAU,EAAE,UAAU,CAAC;IAEvB;;;OAGG;IACH,UAAU,EAAE,MAAM,kBAAkB,GAAG,IAAI,GAAG,SAAS,CAAC;IAExD,8CAA8C;IAC9C,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7B;AAED,MAAM,WAAW,6BAA6B;IAC5C,+DAA+D;IAC/D,gBAAgB,EAAE,MAAM,CAAC;IACzB,8DAA8D;IAC9D,WAAW,EAAE,MAAM,CAAC;IACpB,2FAA2F;IAC3F,WAAW,EAAE,MAAM,CAAC;IACpB,uDAAuD;IACvD,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,wBAAwB;IACvC,IAAI,IAAI,IAAI,CAAC;IACb,KAAK,IAAI,6BAA6B,CAAC;CACxC;AAUD;;;;GAIG;AACH,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,+BAA+B,GACpC,wBAAwB,CAoX1B"}
|