pi-agent-swarm 0.3.0 → 0.5.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.
@@ -0,0 +1,155 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import type { LeaderRecyclingPolicy } from "./leader-recycling.js";
3
+ import type { SwarmMessage } from "./protocol.js";
4
+
5
+ export const LEADER_HANDOFF_BRIDGE_TTL_MS = 30_000;
6
+ export const LEADER_HANDOFF_BRIDGE_FALLBACK_TTL_MS = 30 * 60_000;
7
+ export const LEADER_HANDOFF_BRIDGE_APPLY_TTL_MS = 10 * 60_000;
8
+ export const LEADER_HANDOFF_BRIDGE_SYMBOL = Symbol.for("pi-agent-swarm/leader-handoff-bridge");
9
+
10
+ export interface LeaderHandoffBridgeInput {
11
+ lineageId: string;
12
+ generation: number;
13
+ replacementCount: number;
14
+ policy: LeaderRecyclingPolicy;
15
+ handoffPath: string;
16
+ previousSessionFile: string;
17
+ previousSessionId: string;
18
+ name?: string;
19
+ color?: string;
20
+ model?: string;
21
+ thinkingLevel?: string;
22
+ invite?: string;
23
+ groupDirectory?: string;
24
+ acceptsRequests: boolean;
25
+ nextAction: string;
26
+ /** Distinguishes automatic failures from an explicit /swarm handoff across newSession. */
27
+ automatic?: boolean;
28
+ editorDraft?: string;
29
+ deferredFollowUps?: SwarmMessage[];
30
+ }
31
+
32
+ export interface LeaderHandoffBridgeEntry extends LeaderHandoffBridgeInput {
33
+ token: string;
34
+ }
35
+
36
+ export interface LeaderHandoffBridge {
37
+ put(value: LeaderHandoffBridgeInput, now?: number): LeaderHandoffBridgeEntry;
38
+ take(token: string, previousSessionFile: string, now?: number): LeaderHandoffBridgeEntry | undefined;
39
+ peek(token: string, previousSessionFile: string, now?: number): LeaderHandoffBridgeEntry | undefined;
40
+ arm(token: string, previousSessionFile: string, now?: number, ttlMs?: number): LeaderHandoffBridgeEntry | undefined;
41
+ }
42
+
43
+ type StoredLeaderHandoff = {
44
+ entry: LeaderHandoffBridgeEntry;
45
+ expiresAt: number;
46
+ };
47
+
48
+ type LeaderHandoffBridgeGlobal = typeof globalThis & {
49
+ [LEADER_HANDOFF_BRIDGE_SYMBOL]?: LeaderHandoffBridge;
50
+ };
51
+
52
+ function bridgeKey(token: string, previousSessionFile: string): string {
53
+ return `${token}\u0000${previousSessionFile}`;
54
+ }
55
+
56
+ function createLeaderHandoffBridge(): LeaderHandoffBridge {
57
+ const entries = new Map<string, StoredLeaderHandoff>();
58
+ const purge = (now: number): void => {
59
+ for (const [key, value] of entries) {
60
+ if (now >= value.expiresAt) entries.delete(key);
61
+ }
62
+ };
63
+ const peek: LeaderHandoffBridge["peek"] = (token, previousSessionFile, now = Date.now()) => {
64
+ purge(now);
65
+ return entries.get(bridgeKey(token, previousSessionFile))?.entry;
66
+ };
67
+ return {
68
+ put(value, now = Date.now()) {
69
+ purge(now);
70
+ // JSON serialization rejects cycles/BigInt and ensures no SessionManager or runtime object is retained.
71
+ const data = JSON.parse(JSON.stringify(value)) as LeaderHandoffBridgeInput;
72
+ const entry: LeaderHandoffBridgeEntry = {
73
+ ...data,
74
+ token: randomBytes(24).toString("base64url"),
75
+ };
76
+ entries.set(bridgeKey(entry.token, entry.previousSessionFile), {
77
+ entry,
78
+ // Reset normally removes staged entries; this finite fallback bounds secrets after an interrupted lifecycle.
79
+ expiresAt: now + LEADER_HANDOFF_BRIDGE_FALLBACK_TTL_MS,
80
+ });
81
+ return entry;
82
+ },
83
+ peek,
84
+ arm(token, previousSessionFile, now = Date.now(), ttlMs = LEADER_HANDOFF_BRIDGE_TTL_MS) {
85
+ const entry = peek(token, previousSessionFile, now);
86
+ if (!entry) return undefined;
87
+ const stored = entries.get(bridgeKey(token, previousSessionFile));
88
+ if (stored) stored.expiresAt = now + ttlMs;
89
+ return entry;
90
+ },
91
+ take(token, previousSessionFile, now = Date.now()) {
92
+ const key = bridgeKey(token, previousSessionFile);
93
+ const entry = peek(token, previousSessionFile, now);
94
+ entries.delete(key);
95
+ return entry;
96
+ },
97
+ };
98
+ }
99
+
100
+ const bridgeGlobal = globalThis as LeaderHandoffBridgeGlobal;
101
+ bridgeGlobal[LEADER_HANDOFF_BRIDGE_SYMBOL] ??= createLeaderHandoffBridge();
102
+
103
+ /** Process-local only: invites and editor drafts are never persisted by this bridge. */
104
+ export const leaderHandoffBridge = bridgeGlobal[LEADER_HANDOFF_BRIDGE_SYMBOL];
105
+
106
+ export type LeaderHandoffSuccessorRegistration = {
107
+ token: string;
108
+ previousSessionFile: string;
109
+ lineageId: string;
110
+ generation: number;
111
+ previousSessionId?: string;
112
+ groupDirectory?: string;
113
+ /** The handoff that produced this successor came from an automatic advisory. */
114
+ automatic?: boolean;
115
+ };
116
+ const SUCCESSOR_REGISTRATIONS_SYMBOL = Symbol.for("pi-agent-swarm/leader-successor-registrations");
117
+ type SuccessorRegistrationGlobal = typeof globalThis & {
118
+ [SUCCESSOR_REGISTRATIONS_SYMBOL]?: WeakMap<object, LeaderHandoffSuccessorRegistration>;
119
+ };
120
+ const successorGlobal = globalThis as SuccessorRegistrationGlobal;
121
+ successorGlobal[SUCCESSOR_REGISTRATIONS_SYMBOL] ??= new WeakMap<object, LeaderHandoffSuccessorRegistration>();
122
+ const successorRegistrations = successorGlobal[SUCCESSOR_REGISTRATIONS_SYMBOL];
123
+
124
+ /** setup() runs before session_start; key only by the successor's new SessionManager. */
125
+ export function registerLeaderHandoffSuccessor(
126
+ sessionManager: object,
127
+ token: string,
128
+ previousSessionFile: string,
129
+ lineageId: string,
130
+ generation: number,
131
+ previousSessionId?: string,
132
+ groupDirectory?: string,
133
+ automatic?: boolean,
134
+ ): void {
135
+ successorRegistrations.set(sessionManager, {
136
+ token,
137
+ previousSessionFile,
138
+ lineageId,
139
+ generation,
140
+ previousSessionId,
141
+ groupDirectory,
142
+ automatic,
143
+ });
144
+ }
145
+
146
+ export function takeLeaderHandoffSuccessor(
147
+ sessionManager: object,
148
+ previousSessionFile?: string,
149
+ ): LeaderHandoffSuccessorRegistration | undefined {
150
+ const registration = successorRegistrations.get(sessionManager);
151
+ if (!registration || (previousSessionFile && registration.previousSessionFile !== previousSessionFile))
152
+ return undefined;
153
+ successorRegistrations.delete(sessionManager);
154
+ return registration;
155
+ }