@parall/agent-core 1.44.0 → 1.46.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.
Files changed (62) hide show
  1. package/dist/channel-capability.d.ts +2 -0
  2. package/dist/channel-capability.d.ts.map +1 -1
  3. package/dist/channel-capability.js +15 -0
  4. package/dist/dispatch-adapter.d.ts +9 -0
  5. package/dist/dispatch-adapter.d.ts.map +1 -1
  6. package/dist/event-format.d.ts.map +1 -1
  7. package/dist/event-format.js +27 -7
  8. package/dist/fork-session-finalizer.d.ts +65 -0
  9. package/dist/fork-session-finalizer.d.ts.map +1 -0
  10. package/dist/fork-session-finalizer.js +70 -0
  11. package/dist/gateway-base.d.ts +55 -0
  12. package/dist/gateway-base.d.ts.map +1 -1
  13. package/dist/gateway-base.js +640 -263
  14. package/dist/gateway-lane-flow.d.ts +75 -5
  15. package/dist/gateway-lane-flow.d.ts.map +1 -1
  16. package/dist/gateway-lane-flow.js +240 -18
  17. package/dist/http-keepalive.d.ts +4 -0
  18. package/dist/http-keepalive.d.ts.map +1 -0
  19. package/dist/http-keepalive.js +33 -0
  20. package/dist/index.d.ts +2 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +7 -1
  23. package/dist/lane-ledger.d.ts +8 -0
  24. package/dist/lane-ledger.d.ts.map +1 -1
  25. package/dist/lane-ledger.js +14 -0
  26. package/dist/session-lifecycle.d.ts +198 -0
  27. package/dist/session-lifecycle.d.ts.map +1 -0
  28. package/dist/session-lifecycle.js +446 -0
  29. package/dist/skills/parall-clips.d.ts +1 -1
  30. package/dist/skills/parall-clips.d.ts.map +1 -1
  31. package/dist/skills/parall-clips.js +3 -0
  32. package/dist/skills/parall-schedules.d.ts +1 -1
  33. package/dist/skills/parall-schedules.d.ts.map +1 -1
  34. package/dist/skills/parall-schedules.js +1 -1
  35. package/dist/skills/parall-tasks.d.ts +1 -1
  36. package/dist/skills/parall-tasks.d.ts.map +1 -1
  37. package/dist/skills/parall-tasks.js +21 -5
  38. package/dist/step-persister.d.ts +66 -0
  39. package/dist/step-persister.d.ts.map +1 -0
  40. package/dist/step-persister.js +116 -0
  41. package/dist/step-retry-queue.d.ts +91 -0
  42. package/dist/step-retry-queue.d.ts.map +1 -0
  43. package/dist/step-retry-queue.js +259 -0
  44. package/dist/types.d.ts +1 -1
  45. package/dist/types.d.ts.map +1 -1
  46. package/package.json +3 -2
  47. package/src/channel-capability.ts +16 -0
  48. package/src/dispatch-adapter.ts +10 -0
  49. package/src/event-format.ts +27 -7
  50. package/src/fork-session-finalizer.ts +122 -0
  51. package/src/gateway-base.ts +747 -331
  52. package/src/gateway-lane-flow.ts +275 -18
  53. package/src/http-keepalive.ts +36 -0
  54. package/src/index.ts +7 -1
  55. package/src/lane-ledger.ts +14 -0
  56. package/src/session-lifecycle.ts +552 -0
  57. package/src/skills/parall-clips.ts +3 -0
  58. package/src/skills/parall-schedules.ts +1 -1
  59. package/src/skills/parall-tasks.ts +21 -5
  60. package/src/step-persister.ts +161 -0
  61. package/src/step-retry-queue.ts +296 -0
  62. package/src/types.ts +2 -1
@@ -0,0 +1,198 @@
1
+ /**
2
+ * SessionLifecycleCoordinator — the single entry point for session status
3
+ * writes (active / idle / closed).
4
+ *
5
+ * SCOPE — this is a **process-local mitigation**, not a distributed fence.
6
+ * It serializes and orders the lifecycle writes THIS process issues. It does
7
+ * NOT order across processes, and it cannot fix a write that times out
8
+ * client-side but lands server-side later, nor an old turn's step retry
9
+ * landing during a new turn. The structural fix is a server-side turn fence
10
+ * (design tracked in #1879 — durable step outbox + turn fence); until that
11
+ * ships, this closes the in-process interleavings that produced the
12
+ * 2026-07-13 repro.
13
+ *
14
+ * The gateway used to fire active and idle PATCHes independently
15
+ * (fire-and-forget + detached retries). Two independent HTTP requests have
16
+ * no ordering: turn 1's in-flight idle could land AFTER turn 2's active and
17
+ * demote a running session — an epoch check on retry TIMERS cannot cancel a
18
+ * request already on the wire. The coordinator closes that class by being a
19
+ * serialized desired-state reconciler:
20
+ *
21
+ * - Per session it stores only the LATEST desired state plus a monotonic
22
+ * generation (bumped by beginTurn and closeSession).
23
+ * - At most ONE lifecycle write is in flight per session; when it settles
24
+ * the loop re-reads the latest desired state and reconciles — so a
25
+ * begin(turn N+1), or a closeSession, that arrives while an earlier write
26
+ * is in flight simply waits for it to settle and is written AFTER it.
27
+ * - finishTurn carries the TurnHandle from its beginTurn; a stale handle
28
+ * (generation superseded) is ignored outright.
29
+ * - Retries re-run the reconcile loop against the LATEST desired state —
30
+ * they never replay a captured command, so a pending idle retry is
31
+ * naturally superseded by a new turn.
32
+ * - Errors are CLASSIFIED (ports, defaults are conservative): transient →
33
+ * bounded backoff; permanent (4xx) → warn + abandon that desired state (a
34
+ * later turn retries with a fresh generation); session-stale (the server
35
+ * considers the session terminal) → drop the session, never write again.
36
+ * - beginTurn / closeSession resolve once one write attempt covering their
37
+ * generation has SETTLED (success or failure — bounded by the write's own
38
+ * timeout, plus at most one in-flight predecessor). The gateway awaits
39
+ * beginTurn before persisting the turn's first AgentStep, so a reused idle
40
+ * session is reconciled to active before steps arrive and the server's
41
+ * presence guard cannot swallow the turn's activity. A failed active write
42
+ * warns and lets the step flow continue.
43
+ * - closeSession's first settle carries a STRUCTURED outcome (`closed` /
44
+ * `stale` / `retrying` / `permanent_failure` / `disposed`) plus a
45
+ * `terminal` promise that resolves when the close reaches a terminal fate
46
+ * — a caller holding cleanup behind the close (ForkSessionFinalizer) can
47
+ * keep ownership through `retrying` instead of mistaking "one attempt
48
+ * settled" for "close completed".
49
+ * - Entries are RECLAIMED (no unbounded Map growth on long-lived daemons):
50
+ * dropSession removes the entry immediately when idle, or at the end of an
51
+ * in-flight write; a `closed` that landed OR failed permanently reclaims
52
+ * too — the permanent failure is not hidden by the reclaim, it is reported
53
+ * through the close's outcome/terminal before the entry goes away.
54
+ * - dispose() is absorbing: every later public command is a no-op that never
55
+ * re-creates an entry, issues a write, or arms a timer.
56
+ *
57
+ * The write port is infrastructure (ParallClient.updateAgentSession);
58
+ * retries, generations, error classification and the state machine live here.
59
+ */
60
+ export type SessionStatusPayload = {
61
+ status: 'active';
62
+ trigger_message_id?: string;
63
+ } | {
64
+ status: 'idle';
65
+ } | {
66
+ status: 'closed';
67
+ };
68
+ export type TurnHandle = {
69
+ sessionId: string;
70
+ generation: number;
71
+ };
72
+ /**
73
+ * Terminal fate of a closeSession: `closed` — the write landed; `stale` —
74
+ * the server already considers the session terminal (equivalent outcome for
75
+ * the caller); `permanent_failure` — a 4xx or retry exhaustion; the close
76
+ * will never be written and the server-side session may be left non-closed;
77
+ * `disposed` — the coordinator shut down before the close settled.
78
+ */
79
+ export type SessionCloseOutcome = 'closed' | 'stale' | 'permanent_failure' | 'disposed';
80
+ /** First-settle view: `retrying` means a transient failure settled the first
81
+ * bounded attempt and background reconciliation continues — await `terminal`
82
+ * for the eventual fate. */
83
+ export type SessionCloseSettle = SessionCloseOutcome | 'retrying';
84
+ export type SessionCloseResult = {
85
+ /** Outcome of the first bounded settle (one write attempt covering the close). */
86
+ outcome: SessionCloseSettle;
87
+ /**
88
+ * Resolves when the close reaches a terminal state. Already resolved (to
89
+ * `outcome`) unless `outcome === 'retrying'`.
90
+ */
91
+ terminal: Promise<SessionCloseOutcome>;
92
+ };
93
+ export type SessionLifecycleScheduler = {
94
+ setTimeout(fn: () => void, ms: number): unknown;
95
+ clearTimeout(timer: unknown): void;
96
+ };
97
+ export type SessionLifecycleOpts = {
98
+ write: (sessionId: string, payload: SessionStatusPayload) => Promise<void>;
99
+ /**
100
+ * Transient failure worth retrying (timeout / network / 5xx). Anything
101
+ * else is treated as permanent: warn + abandon (no retry loop against a
102
+ * 400/401/403/404). Default: nothing is retryable — callers that want
103
+ * retries must classify.
104
+ */
105
+ isRetryable?: (err: unknown) => boolean;
106
+ /**
107
+ * The server considers this session terminal (409 SESSION_NOT_LIVE /
108
+ * INVALID_TRANSITION). The session is dropped — no further lifecycle
109
+ * writes, ever. Default: never stale.
110
+ */
111
+ isSessionStale?: (err: unknown) => boolean;
112
+ log?: {
113
+ warn: (msg: string) => void;
114
+ };
115
+ /** Test port; default wraps global timers and unrefs them. */
116
+ scheduler?: SessionLifecycleScheduler;
117
+ /** Reconcile retry backoff. Default 5s→15s→45s→135s→405s (~10 min). */
118
+ retryDelaysMs?: number[];
119
+ };
120
+ export declare class SessionLifecycleCoordinator {
121
+ private readonly opts;
122
+ private readonly sessions;
123
+ private readonly scheduler;
124
+ private readonly retryDelays;
125
+ private nextGeneration;
126
+ private disposed;
127
+ constructor(opts: SessionLifecycleOpts);
128
+ /** Sessions currently tracked — reclaimed entries are gone (leak probe). */
129
+ trackedSessions(): number;
130
+ /**
131
+ * Declare a new turn. Resolves once one write attempt covering this turn's
132
+ * generation has settled (bounded: at most one in-flight predecessor plus
133
+ * one active write, each bounded by the write port's own timeout). Always
134
+ * resolves — a failed active write is warned and reconciled in the
135
+ * background, never blocks the caller.
136
+ */
137
+ beginTurn(sessionId: string, triggerMessageId?: string): Promise<TurnHandle>;
138
+ /**
139
+ * Declare the turn finished. A stale handle (a newer turn — or a close —
140
+ * has bumped this session's generation) is ignored: finish(turn N) can
141
+ * never override begin(turn N+1). Reconciliation runs detached.
142
+ */
143
+ finishTurn(handle: TurnHandle): void;
144
+ /**
145
+ * Terminal transition (fork teardown). Serialized behind any in-flight
146
+ * write — a `closed` can never race the turn's idle — and reclaimed once
147
+ * settled. Resolves after one bounded settle with a STRUCTURED outcome:
148
+ * `closed` / `stale` / `permanent_failure` are terminal; `retrying` means
149
+ * background reconciliation continues — the caller keeps ownership of the
150
+ * session until `terminal` resolves. A close is never silently "done":
151
+ * every fate is observable.
152
+ */
153
+ closeSession(sessionId: string): Promise<SessionCloseResult>;
154
+ /**
155
+ * Session is dead (stale / superseded) — no further lifecycle writes, and
156
+ * the entry is reclaimed (immediately when idle; at the end of an in-flight
157
+ * write otherwise, unless a new turn revives it in the meantime).
158
+ */
159
+ dropSession(sessionId: string): void;
160
+ /** Every tracked session is dead (server closed them all — new_session). */
161
+ dropAllSessions(): void;
162
+ /**
163
+ * One best-effort reconcile pass over unsettled sessions within a hard
164
+ * deadline (shutdown). Returns the number still unsettled.
165
+ */
166
+ flush(deadlineMs: number): Promise<number>;
167
+ dispose(): void;
168
+ /**
169
+ * Get-or-create the entry and take the next generation. A dropped entry is
170
+ * REUSED (not replaced): its `writing` flag keeps the single-writer chain
171
+ * intact if a pre-drop write is still on the wire, and the generation stays
172
+ * monotonic across the drop boundary.
173
+ */
174
+ private upsert;
175
+ private waitFor;
176
+ private isReconciled;
177
+ private isAbandoned;
178
+ /** Nothing more to do for the current desired state (written or given up). */
179
+ private isSettled;
180
+ private pump;
181
+ private writeLoop;
182
+ private classify;
183
+ /**
184
+ * Drop the entry when it can never write again: dropped (and no write on
185
+ * the wire), a terminal `closed` that landed, or a `closed` that failed
186
+ * permanently (its fate has been reported via closeSession's outcome — an
187
+ * entry kept around would be an unowned leak, not a recovery path). Called
188
+ * after every write settles and from dropSession — this is what keeps the
189
+ * Map bounded on a long-lived daemon churning fork sessions.
190
+ */
191
+ private reclaim;
192
+ private scheduleRetry;
193
+ private cancelRetry;
194
+ private resolveWaiters;
195
+ /** Resolve closeSession terminal waiters covered by this settle, once. */
196
+ private settleCloseTerminal;
197
+ }
198
+ //# sourceMappingURL=session-lifecycle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-lifecycle.d.ts","sourceRoot":"","sources":["../src/session-lifecycle.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,446 @@
1
+ /**
2
+ * SessionLifecycleCoordinator — the single entry point for session status
3
+ * writes (active / idle / closed).
4
+ *
5
+ * SCOPE — this is a **process-local mitigation**, not a distributed fence.
6
+ * It serializes and orders the lifecycle writes THIS process issues. It does
7
+ * NOT order across processes, and it cannot fix a write that times out
8
+ * client-side but lands server-side later, nor an old turn's step retry
9
+ * landing during a new turn. The structural fix is a server-side turn fence
10
+ * (design tracked in #1879 — durable step outbox + turn fence); until that
11
+ * ships, this closes the in-process interleavings that produced the
12
+ * 2026-07-13 repro.
13
+ *
14
+ * The gateway used to fire active and idle PATCHes independently
15
+ * (fire-and-forget + detached retries). Two independent HTTP requests have
16
+ * no ordering: turn 1's in-flight idle could land AFTER turn 2's active and
17
+ * demote a running session — an epoch check on retry TIMERS cannot cancel a
18
+ * request already on the wire. The coordinator closes that class by being a
19
+ * serialized desired-state reconciler:
20
+ *
21
+ * - Per session it stores only the LATEST desired state plus a monotonic
22
+ * generation (bumped by beginTurn and closeSession).
23
+ * - At most ONE lifecycle write is in flight per session; when it settles
24
+ * the loop re-reads the latest desired state and reconciles — so a
25
+ * begin(turn N+1), or a closeSession, that arrives while an earlier write
26
+ * is in flight simply waits for it to settle and is written AFTER it.
27
+ * - finishTurn carries the TurnHandle from its beginTurn; a stale handle
28
+ * (generation superseded) is ignored outright.
29
+ * - Retries re-run the reconcile loop against the LATEST desired state —
30
+ * they never replay a captured command, so a pending idle retry is
31
+ * naturally superseded by a new turn.
32
+ * - Errors are CLASSIFIED (ports, defaults are conservative): transient →
33
+ * bounded backoff; permanent (4xx) → warn + abandon that desired state (a
34
+ * later turn retries with a fresh generation); session-stale (the server
35
+ * considers the session terminal) → drop the session, never write again.
36
+ * - beginTurn / closeSession resolve once one write attempt covering their
37
+ * generation has SETTLED (success or failure — bounded by the write's own
38
+ * timeout, plus at most one in-flight predecessor). The gateway awaits
39
+ * beginTurn before persisting the turn's first AgentStep, so a reused idle
40
+ * session is reconciled to active before steps arrive and the server's
41
+ * presence guard cannot swallow the turn's activity. A failed active write
42
+ * warns and lets the step flow continue.
43
+ * - closeSession's first settle carries a STRUCTURED outcome (`closed` /
44
+ * `stale` / `retrying` / `permanent_failure` / `disposed`) plus a
45
+ * `terminal` promise that resolves when the close reaches a terminal fate
46
+ * — a caller holding cleanup behind the close (ForkSessionFinalizer) can
47
+ * keep ownership through `retrying` instead of mistaking "one attempt
48
+ * settled" for "close completed".
49
+ * - Entries are RECLAIMED (no unbounded Map growth on long-lived daemons):
50
+ * dropSession removes the entry immediately when idle, or at the end of an
51
+ * in-flight write; a `closed` that landed OR failed permanently reclaims
52
+ * too — the permanent failure is not hidden by the reclaim, it is reported
53
+ * through the close's outcome/terminal before the entry goes away.
54
+ * - dispose() is absorbing: every later public command is a no-op that never
55
+ * re-creates an entry, issues a write, or arms a timer.
56
+ *
57
+ * The write port is infrastructure (ParallClient.updateAgentSession);
58
+ * retries, generations, error classification and the state machine live here.
59
+ */
60
+ const DEFAULT_RETRY_DELAYS_MS = [5_000, 15_000, 45_000, 135_000, 405_000];
61
+ const defaultScheduler = {
62
+ setTimeout(fn, ms) {
63
+ const timer = setTimeout(fn, ms);
64
+ timer.unref?.();
65
+ return timer;
66
+ },
67
+ clearTimeout(timer) {
68
+ clearTimeout(timer);
69
+ },
70
+ };
71
+ export class SessionLifecycleCoordinator {
72
+ opts;
73
+ sessions = new Map();
74
+ scheduler;
75
+ retryDelays;
76
+ // Generations are coordinator-global and strictly increasing, so a handle
77
+ // from before an entry was reclaimed can never match a REBUILT entry for
78
+ // the same session id (per-entry counters restart at 1 — an ABA hazard
79
+ // where a stale finish would demote a fresh turn).
80
+ nextGeneration = 0;
81
+ disposed = false;
82
+ constructor(opts) {
83
+ this.opts = opts;
84
+ this.scheduler = opts.scheduler ?? defaultScheduler;
85
+ this.retryDelays = opts.retryDelaysMs?.length ? opts.retryDelaysMs : DEFAULT_RETRY_DELAYS_MS;
86
+ }
87
+ /** Sessions currently tracked — reclaimed entries are gone (leak probe). */
88
+ trackedSessions() {
89
+ return this.sessions.size;
90
+ }
91
+ /**
92
+ * Declare a new turn. Resolves once one write attempt covering this turn's
93
+ * generation has settled (bounded: at most one in-flight predecessor plus
94
+ * one active write, each bounded by the write port's own timeout). Always
95
+ * resolves — a failed active write is warned and reconciled in the
96
+ * background, never blocks the caller.
97
+ */
98
+ async beginTurn(sessionId, triggerMessageId) {
99
+ // Dispose is absorbing: never touch the map (an upsert here would
100
+ // re-create an entry nothing will ever reclaim), never write.
101
+ // Generation 0 can't match any live entry, so the handle is inert.
102
+ if (this.disposed)
103
+ return { sessionId, generation: 0 };
104
+ const entry = this.upsert(sessionId);
105
+ entry.desired = 'active';
106
+ entry.triggerMessageId = triggerMessageId;
107
+ const generation = entry.generation;
108
+ const settled = this.waitFor(entry, generation);
109
+ this.pump(sessionId);
110
+ await settled;
111
+ return { sessionId, generation };
112
+ }
113
+ /**
114
+ * Declare the turn finished. A stale handle (a newer turn — or a close —
115
+ * has bumped this session's generation) is ignored: finish(turn N) can
116
+ * never override begin(turn N+1). Reconciliation runs detached.
117
+ */
118
+ finishTurn(handle) {
119
+ if (this.disposed)
120
+ return;
121
+ const entry = this.sessions.get(handle.sessionId);
122
+ if (!entry || entry.dropped || entry.generation !== handle.generation)
123
+ return;
124
+ if (entry.desired === 'closed')
125
+ return;
126
+ entry.desired = 'idle';
127
+ entry.retryAttempt = 0;
128
+ this.cancelRetry(entry);
129
+ this.pump(handle.sessionId);
130
+ }
131
+ /**
132
+ * Terminal transition (fork teardown). Serialized behind any in-flight
133
+ * write — a `closed` can never race the turn's idle — and reclaimed once
134
+ * settled. Resolves after one bounded settle with a STRUCTURED outcome:
135
+ * `closed` / `stale` / `permanent_failure` are terminal; `retrying` means
136
+ * background reconciliation continues — the caller keeps ownership of the
137
+ * session until `terminal` resolves. A close is never silently "done":
138
+ * every fate is observable.
139
+ */
140
+ async closeSession(sessionId) {
141
+ if (this.disposed) {
142
+ return { outcome: 'disposed', terminal: Promise.resolve('disposed') };
143
+ }
144
+ const entry = this.upsert(sessionId);
145
+ entry.desired = 'closed';
146
+ entry.triggerMessageId = undefined;
147
+ const generation = entry.generation;
148
+ const terminal = new Promise((resolve) => {
149
+ entry.closeWaiters.push({ generation, resolve });
150
+ });
151
+ const settled = this.waitFor(entry, generation);
152
+ this.pump(sessionId);
153
+ const settle = await settled;
154
+ return { outcome: closeSettleFor(settle), terminal };
155
+ }
156
+ /**
157
+ * Session is dead (stale / superseded) — no further lifecycle writes, and
158
+ * the entry is reclaimed (immediately when idle; at the end of an in-flight
159
+ * write otherwise, unless a new turn revives it in the meantime).
160
+ */
161
+ dropSession(sessionId) {
162
+ const entry = this.sessions.get(sessionId);
163
+ if (!entry)
164
+ return;
165
+ entry.dropped = true;
166
+ this.cancelRetry(entry);
167
+ this.resolveWaiters(entry, Number.POSITIVE_INFINITY, 'dropped');
168
+ this.reclaim(sessionId, entry);
169
+ }
170
+ /** Every tracked session is dead (server closed them all — new_session). */
171
+ dropAllSessions() {
172
+ for (const sessionId of [...this.sessions.keys()])
173
+ this.dropSession(sessionId);
174
+ }
175
+ /**
176
+ * One best-effort reconcile pass over unsettled sessions within a hard
177
+ * deadline (shutdown). Returns the number still unsettled.
178
+ */
179
+ async flush(deadlineMs) {
180
+ const deadline = Date.now() + deadlineMs;
181
+ for (const [sessionId, entry] of [...this.sessions.entries()]) {
182
+ if (this.disposed)
183
+ break;
184
+ if (entry.dropped || this.isSettled(entry))
185
+ continue;
186
+ this.cancelRetry(entry);
187
+ entry.retryAttempt = 0;
188
+ const timeLeft = deadline - Date.now();
189
+ if (timeLeft <= 0)
190
+ break;
191
+ const settled = this.waitFor(entry, entry.generation);
192
+ this.pump(sessionId);
193
+ await raceWithDeadline(settled, timeLeft);
194
+ }
195
+ let remaining = 0;
196
+ for (const entry of this.sessions.values()) {
197
+ if (!entry.dropped && !this.isSettled(entry))
198
+ remaining += 1;
199
+ }
200
+ return remaining;
201
+ }
202
+ dispose() {
203
+ this.disposed = true;
204
+ for (const entry of this.sessions.values()) {
205
+ this.cancelRetry(entry);
206
+ this.resolveWaiters(entry, Number.POSITIVE_INFINITY, 'disposed');
207
+ this.settleCloseTerminal(entry, 'disposed');
208
+ }
209
+ this.sessions.clear();
210
+ }
211
+ // --- internals ----------------------------------------------------------
212
+ /**
213
+ * Get-or-create the entry and take the next generation. A dropped entry is
214
+ * REUSED (not replaced): its `writing` flag keeps the single-writer chain
215
+ * intact if a pre-drop write is still on the wire, and the generation stays
216
+ * monotonic across the drop boundary.
217
+ */
218
+ upsert(sessionId) {
219
+ let entry = this.sessions.get(sessionId);
220
+ if (!entry) {
221
+ entry = {
222
+ desired: 'active',
223
+ generation: 0,
224
+ writing: false,
225
+ retryAttempt: 0,
226
+ waiters: [],
227
+ closeWaiters: [],
228
+ dropped: false,
229
+ };
230
+ this.sessions.set(sessionId, entry);
231
+ }
232
+ entry.dropped = false;
233
+ this.nextGeneration += 1;
234
+ entry.generation = this.nextGeneration;
235
+ entry.retryAttempt = 0;
236
+ this.cancelRetry(entry);
237
+ return entry;
238
+ }
239
+ waitFor(entry, generation) {
240
+ return new Promise((resolve) => {
241
+ entry.waiters.push({ generation, resolve });
242
+ });
243
+ }
244
+ isReconciled(entry) {
245
+ return (entry.written !== undefined &&
246
+ entry.written.state === entry.desired &&
247
+ entry.written.generation === entry.generation);
248
+ }
249
+ isAbandoned(entry) {
250
+ return (entry.abandoned !== undefined &&
251
+ entry.abandoned.state === entry.desired &&
252
+ entry.abandoned.generation === entry.generation);
253
+ }
254
+ /** Nothing more to do for the current desired state (written or given up). */
255
+ isSettled(entry) {
256
+ return this.isReconciled(entry) || this.isAbandoned(entry);
257
+ }
258
+ pump(sessionId) {
259
+ const entry = this.sessions.get(sessionId);
260
+ if (!entry || entry.writing || entry.dropped || this.disposed)
261
+ return;
262
+ if (this.isSettled(entry)) {
263
+ this.resolveWaiters(entry, entry.generation, this.isReconciled(entry) ? 'ok' : 'permanent');
264
+ this.reclaim(sessionId, entry);
265
+ return;
266
+ }
267
+ entry.writing = true;
268
+ void this.writeLoop(sessionId, entry);
269
+ }
270
+ async writeLoop(sessionId, entry) {
271
+ try {
272
+ while (!entry.dropped && !this.disposed && !this.isSettled(entry)) {
273
+ const snapshot = {
274
+ state: entry.desired,
275
+ generation: entry.generation,
276
+ trigger: entry.triggerMessageId,
277
+ };
278
+ let outcome = 'ok';
279
+ try {
280
+ await this.opts.write(sessionId, payloadFor(snapshot));
281
+ entry.written = { state: snapshot.state, generation: snapshot.generation };
282
+ entry.retryAttempt = 0;
283
+ if (snapshot.state === 'closed') {
284
+ this.settleCloseTerminal(entry, 'closed', snapshot.generation);
285
+ }
286
+ }
287
+ catch (err) {
288
+ outcome = this.classify(err);
289
+ this.opts.log?.warn(`session ${sessionId} ${snapshot.state} write failed (${outcome}, gen ${snapshot.generation}): ${String(err)}`);
290
+ }
291
+ // A settle — success or failure — resolves every waiter this
292
+ // snapshot covers (the bounded beginTurn / closeSession contract).
293
+ this.resolveWaiters(entry, snapshot.generation, outcome);
294
+ if (outcome === 'stale') {
295
+ // Terminal server-side: never write to this session again.
296
+ entry.dropped = true;
297
+ this.cancelRetry(entry);
298
+ this.settleCloseTerminal(entry, 'stale');
299
+ this.resolveWaiters(entry, Number.POSITIVE_INFINITY, 'stale');
300
+ return;
301
+ }
302
+ const desiredUnchanged = snapshot.generation === entry.generation && snapshot.state === entry.desired;
303
+ if (outcome === 'permanent' && desiredUnchanged) {
304
+ // No retry loop against a 4xx — a later turn (fresh generation)
305
+ // un-abandons this session automatically.
306
+ entry.abandoned = { state: snapshot.state, generation: snapshot.generation };
307
+ if (snapshot.state === 'closed') {
308
+ this.settleCloseTerminal(entry, 'permanent_failure', snapshot.generation);
309
+ }
310
+ return;
311
+ }
312
+ if (outcome === 'retry' && desiredUnchanged && !entry.dropped && !this.disposed) {
313
+ // The retry re-enters pump and replays the LATEST desired state,
314
+ // never this snapshot.
315
+ this.scheduleRetry(sessionId, entry);
316
+ return;
317
+ }
318
+ // Success, or the desired state advanced while writing: loop and
319
+ // reconcile the fresh state immediately.
320
+ }
321
+ }
322
+ finally {
323
+ entry.writing = false;
324
+ this.reclaim(sessionId, entry);
325
+ }
326
+ }
327
+ classify(err) {
328
+ if (this.opts.isSessionStale?.(err))
329
+ return 'stale';
330
+ if (this.opts.isRetryable?.(err))
331
+ return 'retry';
332
+ return 'permanent';
333
+ }
334
+ /**
335
+ * Drop the entry when it can never write again: dropped (and no write on
336
+ * the wire), a terminal `closed` that landed, or a `closed` that failed
337
+ * permanently (its fate has been reported via closeSession's outcome — an
338
+ * entry kept around would be an unowned leak, not a recovery path). Called
339
+ * after every write settles and from dropSession — this is what keeps the
340
+ * Map bounded on a long-lived daemon churning fork sessions.
341
+ */
342
+ reclaim(sessionId, entry) {
343
+ if (this.sessions.get(sessionId) !== entry)
344
+ return;
345
+ if (entry.writing)
346
+ return; // the write's finally will re-run this
347
+ const closedLanded = entry.desired === 'closed' &&
348
+ entry.written?.state === 'closed' &&
349
+ entry.written.generation === entry.generation;
350
+ const closedAbandoned = entry.desired === 'closed' &&
351
+ entry.abandoned?.state === 'closed' &&
352
+ entry.abandoned.generation === entry.generation;
353
+ if (!entry.dropped && !closedLanded && !closedAbandoned)
354
+ return;
355
+ this.cancelRetry(entry);
356
+ this.settleCloseTerminal(entry, closedLanded ? 'closed' : entry.dropped ? 'stale' : 'permanent_failure');
357
+ this.resolveWaiters(entry, Number.POSITIVE_INFINITY, closedLanded ? 'ok' : entry.dropped ? 'dropped' : 'permanent');
358
+ this.sessions.delete(sessionId);
359
+ }
360
+ scheduleRetry(sessionId, entry) {
361
+ if (entry.retryTimer !== undefined)
362
+ return;
363
+ if (entry.retryAttempt >= this.retryDelays.length) {
364
+ this.opts.log?.warn(`session ${sessionId} lifecycle reconcile giving up after ${entry.retryAttempt} retries (desired=${entry.desired})`);
365
+ entry.abandoned = { state: entry.desired, generation: entry.generation };
366
+ if (entry.desired === 'closed') {
367
+ this.settleCloseTerminal(entry, 'permanent_failure');
368
+ }
369
+ return;
370
+ }
371
+ const delay = this.retryDelays[entry.retryAttempt] ?? 5_000;
372
+ entry.retryAttempt += 1;
373
+ entry.retryTimer = this.scheduler.setTimeout(() => {
374
+ entry.retryTimer = undefined;
375
+ this.pump(sessionId);
376
+ }, delay);
377
+ }
378
+ cancelRetry(entry) {
379
+ if (entry.retryTimer !== undefined) {
380
+ this.scheduler.clearTimeout(entry.retryTimer);
381
+ entry.retryTimer = undefined;
382
+ }
383
+ }
384
+ resolveWaiters(entry, upToGeneration, settle) {
385
+ if (entry.waiters.length === 0)
386
+ return;
387
+ const remaining = [];
388
+ for (const waiter of entry.waiters) {
389
+ if (waiter.generation <= upToGeneration)
390
+ waiter.resolve(settle);
391
+ else
392
+ remaining.push(waiter);
393
+ }
394
+ entry.waiters = remaining;
395
+ }
396
+ /** Resolve closeSession terminal waiters covered by this settle, once. */
397
+ settleCloseTerminal(entry, outcome, upToGeneration = Number.POSITIVE_INFINITY) {
398
+ if (entry.closeWaiters.length === 0)
399
+ return;
400
+ const remaining = [];
401
+ for (const waiter of entry.closeWaiters) {
402
+ if (waiter.generation <= upToGeneration)
403
+ waiter.resolve(outcome);
404
+ else
405
+ remaining.push(waiter);
406
+ }
407
+ entry.closeWaiters = remaining;
408
+ }
409
+ }
410
+ /** Map a first-settle write outcome onto the closeSession vocabulary. */
411
+ function closeSettleFor(settle) {
412
+ switch (settle) {
413
+ case 'ok':
414
+ return 'closed';
415
+ case 'retry':
416
+ return 'retrying';
417
+ case 'stale':
418
+ case 'dropped':
419
+ return 'stale';
420
+ case 'disposed':
421
+ return 'disposed';
422
+ default:
423
+ return 'permanent_failure';
424
+ }
425
+ }
426
+ function payloadFor(snapshot) {
427
+ if (snapshot.state === 'active') {
428
+ return {
429
+ status: 'active',
430
+ ...(snapshot.trigger ? { trigger_message_id: snapshot.trigger } : {}),
431
+ };
432
+ }
433
+ return snapshot.state === 'idle' ? { status: 'idle' } : { status: 'closed' };
434
+ }
435
+ async function raceWithDeadline(work, ms) {
436
+ let timer;
437
+ const timeout = new Promise((resolve) => {
438
+ timer = setTimeout(() => resolve('timeout'), Math.max(0, ms));
439
+ });
440
+ try {
441
+ return await Promise.race([work, timeout]);
442
+ }
443
+ finally {
444
+ clearTimeout(timer);
445
+ }
446
+ }
@@ -1,2 +1,2 @@
1
- export declare const PARALL_CLIPS_SKILL = "# Parall Clips\n\nClips are packaged capabilities that let agents operate external systems \u2014\nAPIs and websites \u2014 through named commands installed in the org.\n\n## Discover\n\n```bash\nparall clip list # clips installed in this org\nparall clip info <alias> # commands, params, version\n```\n\n## Invoke\n\n```bash\nparall clip invoke <alias> <command> [input] [--timeout <ms>] # timeout default 30s\n# input: JSON string or plain text, per the command's params in `info`\nparall clip invoke github-tools list-repos '{\"org\":\"acme\"}'\n```\n\nResults are JSON on stdout; failures print an error.\n\n## Behavior rules\n\n- An authorization error (clip not bound to you) is a fail-fast: ask the\n clip's owner or an admin to bind it \u2014 do not retry or work around it.\n- If the executing runtime is offline or the call times out, report that\n plainly; do not queue, and never fabricate a result for a run that errored.\n- A clip may act through a person's real logged-in account \u2014 outward,\n irreversible, or spending actions (post, order, delete, pay) get the same\n caution as any shared-state change: confirm when intent isn't explicit.\n- Reach for `parall clip list` whenever a task needs capabilities beyond\n built-in tools.\n";
1
+ export declare const PARALL_CLIPS_SKILL = "# Parall Clips\n\nClips are packaged capabilities that let agents operate external systems \u2014\nAPIs and websites \u2014 through named commands installed in the org.\n\n## Discover\n\n```bash\nparall clip list # clips installed in this org\nparall clip info <alias> # commands, params, version\n```\n\n## Invoke\n\n```bash\nparall clip invoke <alias> <command> [input] [--timeout <ms>] # timeout default 30s\n# input: JSON string or plain text, per the command's params in `info`\nparall clip invoke github-tools list-repos '{\"org\":\"acme\"}'\n```\n\nResults are JSON on stdout; failures print an error.\n\n## Behavior rules\n\n- An authorization error (clip not bound to you) is a fail-fast: ask the\n clip's owner or an admin to bind it \u2014 do not retry or work around it.\n- If the executing runtime is offline or the call times out, report that\n plainly; do not queue, and never fabricate a result for a run that errored.\n- Hosted browser activation is handled by the CLI: it waits (bounded) while a\n cold hosted browser starts, so if the invoke still fails, report the error \u2014\n do not blind-retry in a loop.\n- A clip may act through a person's real logged-in account \u2014 outward,\n irreversible, or spending actions (post, order, delete, pay) get the same\n caution as any shared-state change: confirm when intent isn't explicit.\n- Reach for `parall clip list` whenever a task needs capabilities beyond\n built-in tools.\n";
2
2
  //# sourceMappingURL=parall-clips.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parall-clips.d.ts","sourceRoot":"","sources":["../../src/skills/parall-clips.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,gwCAiC9B,CAAC"}
1
+ {"version":3,"file":"parall-clips.d.ts","sourceRoot":"","sources":["../../src/skills/parall-clips.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,q8CAoC9B,CAAC"}
@@ -26,6 +26,9 @@ Results are JSON on stdout; failures print an error.
26
26
  clip's owner or an admin to bind it — do not retry or work around it.
27
27
  - If the executing runtime is offline or the call times out, report that
28
28
  plainly; do not queue, and never fabricate a result for a run that errored.
29
+ - Hosted browser activation is handled by the CLI: it waits (bounded) while a
30
+ cold hosted browser starts, so if the invoke still fails, report the error —
31
+ do not blind-retry in a loop.
29
32
  - A clip may act through a person's real logged-in account — outward,
30
33
  irreversible, or spending actions (post, order, delete, pay) get the same
31
34
  caution as any shared-state change: confirm when intent isn't explicit.