@cursor/july 0.1.76 → 0.1.78

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 (128) hide show
  1. package/dist/channels/origin/origin-webhook.d.ts +23 -2
  2. package/dist/channels/origin/origin-webhook.d.ts.map +1 -1
  3. package/dist/channels/origin/origin-webhook.js +82 -20
  4. package/dist/docs/404.html +2 -2
  5. package/dist/docs/ab.html +3 -3
  6. package/dist/docs/assets/{app.b8bhB2SV.js → app.BsA7t7rS.js} +1 -1
  7. package/dist/docs/assets/chunks/@localSearchIndexroot.Dcs6boxD.js +1 -0
  8. package/dist/docs/assets/chunks/{VPLocalSearchBox.lLSeIyWw.js → VPLocalSearchBox.B1hHhktM.js} +1 -1
  9. package/dist/docs/assets/chunks/{theme.C_owesa-.js → theme.DLpLSsQh.js} +2 -2
  10. package/dist/docs/assets/{storage.md.D8NQ389l.js → storage.md.ks1u64_R.js} +3 -3
  11. package/dist/docs/assets/storage.md.ks1u64_R.lean.js +1 -0
  12. package/dist/docs/building-with-agents.html +3 -3
  13. package/dist/docs/concepts.html +3 -3
  14. package/dist/docs/deployment.html +3 -3
  15. package/dist/docs/evals.html +3 -3
  16. package/dist/docs/example-agents/approval-buddy.html +3 -3
  17. package/dist/docs/example-agents/benny.html +3 -3
  18. package/dist/docs/example-agents/bugbot.html +3 -3
  19. package/dist/docs/example-agents/codebase-wiki.html +3 -3
  20. package/dist/docs/example-agents/codeowners-review.html +3 -3
  21. package/dist/docs/example-agents/concierge.html +3 -3
  22. package/dist/docs/example-agents/fsd.html +3 -3
  23. package/dist/docs/example-agents/index.html +3 -3
  24. package/dist/docs/example-agents/knowledge-base.html +3 -3
  25. package/dist/docs/example-agents/oncall.html +3 -3
  26. package/dist/docs/example-agents/security-reviewer.html +3 -3
  27. package/dist/docs/example-agents/slack-agent.html +3 -3
  28. package/dist/docs/example-agents/weather-agent.html +3 -3
  29. package/dist/docs/guides/agent-to-agent.html +3 -3
  30. package/dist/docs/guides/cloud-runtime.html +3 -3
  31. package/dist/docs/guides/convert-automation.html +3 -3
  32. package/dist/docs/guides/github.html +3 -3
  33. package/dist/docs/guides/human-in-the-loop.html +3 -3
  34. package/dist/docs/guides/mcp-oauth.html +3 -3
  35. package/dist/docs/guides/opentelemetry.html +3 -3
  36. package/dist/docs/guides/slack.html +3 -3
  37. package/dist/docs/guides/webhooks.html +3 -3
  38. package/dist/docs/hashmap.json +1 -1
  39. package/dist/docs/hillclimbing.html +3 -3
  40. package/dist/docs/index.html +3 -3
  41. package/dist/docs/quickstart.html +3 -3
  42. package/dist/docs/reference/agent-config.html +3 -3
  43. package/dist/docs/reference/artifacts.html +3 -3
  44. package/dist/docs/reference/channels.html +3 -3
  45. package/dist/docs/reference/cli.html +3 -3
  46. package/dist/docs/reference/connections.html +3 -3
  47. package/dist/docs/reference/hooks.html +3 -3
  48. package/dist/docs/reference/http-api.html +3 -3
  49. package/dist/docs/reference/instructions.html +3 -3
  50. package/dist/docs/reference/playground.html +3 -3
  51. package/dist/docs/reference/project-layout.html +3 -3
  52. package/dist/docs/reference/prompt.html +3 -3
  53. package/dist/docs/reference/schedules.html +3 -3
  54. package/dist/docs/reference/sessions.html +3 -3
  55. package/dist/docs/reference/skills.html +3 -3
  56. package/dist/docs/reference/subagents.html +3 -3
  57. package/dist/docs/reference/tools.html +3 -3
  58. package/dist/docs/scaffolding-agents.html +3 -3
  59. package/dist/docs/storage.html +7 -7
  60. package/dist/docs/templates/agentic-owners.html +3 -3
  61. package/dist/docs/templates/demo.html +3 -3
  62. package/dist/docs/templates/pr-autofixer.html +3 -3
  63. package/dist/docs/templates/security-reviewer.html +3 -3
  64. package/dist/docs/templates/triage.html +3 -3
  65. package/dist/docs/troubleshooting.html +3 -3
  66. package/dist/index.d.ts +0 -1
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +0 -1
  69. package/dist/internal/discovery.d.ts.map +1 -1
  70. package/dist/internal/discovery.js +3 -21
  71. package/dist/internal/distribution.d.ts.map +1 -1
  72. package/dist/internal/distribution.js +0 -1
  73. package/dist/internal/session-engine.d.ts +1 -60
  74. package/dist/internal/session-engine.d.ts.map +1 -1
  75. package/dist/internal/session-engine.js +10 -109
  76. package/dist/kv/memory-kv.d.ts +1 -10
  77. package/dist/kv/memory-kv.d.ts.map +1 -1
  78. package/dist/kv/memory-kv.js +1 -10
  79. package/dist/kv.d.ts +2 -3
  80. package/dist/kv.d.ts.map +1 -1
  81. package/dist/kv.js +2 -3
  82. package/dist/playground/assets/{index-Bfwz2yLN.css → index-DJzcIeGt.css} +1 -1
  83. package/dist/playground/assets/{index-BWqjJcRo.js → index-K3Sxyx0I.js} +48 -45
  84. package/dist/playground/index.html +2 -2
  85. package/dist/storage-backends/cursor-hosted.js +3 -3
  86. package/dist/storage-protocol.d.ts +3 -8
  87. package/dist/storage-protocol.d.ts.map +1 -1
  88. package/dist/storage-protocol.js +4 -10
  89. package/dist/storage.d.ts +5 -11
  90. package/dist/storage.d.ts.map +1 -1
  91. package/dist/storage.js +3 -4
  92. package/dist/types.d.ts +5 -34
  93. package/dist/types.d.ts.map +1 -1
  94. package/docs/storage.md +11 -44
  95. package/package.json +1 -8
  96. package/src/channels/origin/origin-webhook.ts +111 -25
  97. package/src/index.ts +0 -6
  98. package/src/internal/discovery.ts +3 -27
  99. package/src/internal/distribution.ts +0 -1
  100. package/src/internal/session-engine.ts +1 -168
  101. package/src/kv/memory-kv.ts +1 -10
  102. package/src/kv.ts +2 -3
  103. package/src/storage-backends/cursor-hosted.ts +3 -3
  104. package/src/storage-protocol.ts +4 -10
  105. package/src/storage.ts +6 -12
  106. package/src/types.ts +5 -35
  107. package/dist/docs/assets/chunks/@localSearchIndexroot.DGZeXVqn.js +0 -1
  108. package/dist/docs/assets/storage.md.D8NQ389l.lean.js +0 -1
  109. package/dist/internal/durable-session-store.d.ts +0 -60
  110. package/dist/internal/durable-session-store.d.ts.map +0 -1
  111. package/dist/internal/durable-session-store.js +0 -131
  112. package/dist/internal/ledger-serving.d.ts +0 -169
  113. package/dist/internal/ledger-serving.d.ts.map +0 -1
  114. package/dist/internal/ledger-serving.js +0 -393
  115. package/dist/ledger/janitor.d.ts +0 -60
  116. package/dist/ledger/janitor.d.ts.map +0 -1
  117. package/dist/ledger/janitor.js +0 -137
  118. package/dist/ledger/session-ledger.d.ts +0 -293
  119. package/dist/ledger/session-ledger.d.ts.map +0 -1
  120. package/dist/ledger/session-ledger.js +0 -614
  121. package/dist/ledger.d.ts +0 -12
  122. package/dist/ledger.d.ts.map +0 -1
  123. package/dist/ledger.js +0 -9
  124. package/src/internal/durable-session-store.ts +0 -134
  125. package/src/internal/ledger-serving.ts +0 -531
  126. package/src/ledger/janitor.ts +0 -193
  127. package/src/ledger/session-ledger.ts +0 -839
  128. package/src/ledger.ts +0 -44
@@ -1,393 +0,0 @@
1
- /**
2
- * Ledger-mode serving seam (`serving: "ledger"` on `defineAgent`): an event
3
- * pump that routes every engine `send` through the KV session ledger
4
- * instead of starting turns directly.
5
- *
6
- * Delivery: ensureSession (continuation key, as today) → principal gate
7
- * (an existing session rejects a sender that is not its principal BEFORE
8
- * anything is parked; mirrors the resident `samePrincipal` check) →
9
- * appendEvent (payload + CAS — the source ack point moves after this) →
10
- * claim → drain → run the drained turns through the resident engine path →
11
- * commit (heartbeats while turns run) → repeat while the commit reports
12
- * pending. A deliver that finds the lease held walks away (the holder's
13
- * commit sees the append); the janitor recovers sessions whose holder
14
- * died. A foreign-principal envelope that reaches the mailbox anyway is
15
- * consumed and committed away at drain (warn log), never replayed.
16
- *
17
- * v1 scope: the mailbox envelope carries `message`, `auth`, `title`,
18
- * `state`, `mode`, `purpose`, and the isolation-relevant per-send
19
- * overrides `workspaceDir` and `cloud` — the two options
20
- * `resolveSessionRuntime` and harness-cwd selection read. Dropping
21
- * those is a sandbox escape, not a feature gap: without them a drained
22
- * GitHub/Slack turn falls back to the local runtime with the default
23
- * harness cwd, so untrusted channel content drives tools on the serve
24
- * host instead of the cloud VM / channel worktree. `coalesceSourceTs`
25
- * (when present) is used as the drain-ordering `sourceTs`. Deferred
26
- * (documented, not silently faked — deliver logs a warning naming what
27
- * it dropped): `workspaceFiles` and per-turn `images` (content
28
- * injection, and their base64 payloads can blow the mailbox blob past
29
- * hosted KV value caps), `sdkAgentId` seeding, and `admission`
30
- * semantics (both `"preempt"` and Slack `"coalesce"` nudge bookkeeping
31
- * — ledger draining already serializes turns, but nothing is
32
- * interrupted or coalesced through the mailbox).
33
- */
34
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
35
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
36
- return new (P || (P = Promise))(function (resolve, reject) {
37
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
38
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
39
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
40
- step((generator = generator.apply(thisArg, _arguments || [])).next());
41
- });
42
- };
43
- import { randomBytes, randomUUID } from "node:crypto";
44
- import { createLedgerJanitor } from "../ledger/janitor.js";
45
- import { HEARTBEAT_INTERVAL_MS, LedgerFencedError, } from "../ledger/session-ledger.js";
46
- import { SessionAuthError, samePrincipal } from "./auth.js";
47
- import { describeError } from "./describe-error.js";
48
- import { newContinuationKey } from "./ids.js";
49
- export class LedgerServing {
50
- constructor(options) {
51
- var _a, _b, _c;
52
- /** One pump per ledger session — the in-process face of the lease. */
53
- this.pumps = new Map();
54
- /** Fire-and-forget re-pump checks, awaited on close. */
55
- this.background = new Set();
56
- this.ledger = options.ledger;
57
- this.deliverTurn = options.deliverTurn;
58
- this.findSession = options.findSession;
59
- this.logger = (_a = options.logger) !== null && _a !== void 0 ? _a : (() => { });
60
- this.heartbeatIntervalMs = (_b = options.heartbeatIntervalMs) !== null && _b !== void 0 ? _b : HEARTBEAT_INTERVAL_MS;
61
- this.clock = (_c = options.clock) !== null && _c !== void 0 ? _c : Date.now;
62
- this.janitor = createLedgerJanitor(Object.assign(Object.assign({ ledger: options.ledger, onSession: (claimed) => this.serveClaimed(claimed),
63
- // Consumed mailbox blobs (full message envelopes) would otherwise
64
- // accumulate forever; the default TTL (1h) is far beyond any lease
65
- // or turn horizon, so only truly dead blobs are collected.
66
- orphanSweep: {} }, (options.janitorIntervalMs === undefined
67
- ? {}
68
- : { intervalMs: options.janitorIntervalMs })), { onError: (error, sessionId) => {
69
- this.logger(`[ledger] janitor error${sessionId === undefined ? "" : ` (session ${sessionId})`}: ${describeError(error)}`);
70
- }, clock: this.clock }));
71
- }
72
- /** Start the janitor loop (recovers expired-lease sessions). */
73
- start() {
74
- this.janitor.start();
75
- }
76
- /** Stop the janitor and settle in-flight pumps and re-pump checks. */
77
- close() {
78
- return __awaiter(this, void 0, void 0, function* () {
79
- // Awaiting stop() matters: an in-flight janitor pass can claim a
80
- // session and register a pump after this method snapshots the maps.
81
- yield this.janitor.stop();
82
- yield Promise.allSettled([...this.pumps.values()]);
83
- yield Promise.allSettled([...this.background]);
84
- });
85
- }
86
- /**
87
- * Ledger-mode `send`: append the message to the session mailbox, then
88
- * serve pending events (or walk away if another holder is already
89
- * serving them). Resolves with the engine session handle once one
90
- * exists — the ack point is the durable append, not turn completion —
91
- * so HTTP create/follow-up can return a session id while the holder is
92
- * still in flight. When this process wins the claim, the pump keeps
93
- * draining in the background; callers that need the turn to finish use
94
- * `waitForCompletion` (and the lease releases on the pump's commit).
95
- * When another holder is serving the session and no engine session
96
- * exists on this pod, the delivery is still a success — the event is
97
- * durably parked and the holder's commit (or the janitor) drains it —
98
- * so the resolved handle is a parked stand-in carrying the durable
99
- * session identity.
100
- */
101
- deliver(channelId, message, options) {
102
- return __awaiter(this, void 0, void 0, function* () {
103
- var _a, _b, _c;
104
- const continuationToken = (_a = options.continuationToken) !== null && _a !== void 0 ? _a : newContinuationKey();
105
- this.warnOnDroppedOptions(channelId, options);
106
- const ensured = yield this.ledger.ensureSession(`${channelId}/${continuationToken}`);
107
- const ledgerSessionId = ensured.session.sessionId;
108
- // Principal gate BEFORE the durable append: once an envelope is
109
- // parked it WILL be drained, so a foreign sender must be rejected
110
- // here — not on the resident path after the mailbox write. A new
111
- // ledger session has no owner yet: the creating sender becomes it,
112
- // exactly as on resident session creation.
113
- if (!ensured.created) {
114
- const existing = yield this.findSession(channelId, continuationToken);
115
- if (existing !== undefined) {
116
- if (!samePrincipal(existing.info.auth, (_b = options.auth) !== null && _b !== void 0 ? _b : null)) {
117
- throw new SessionAuthError(existing.id);
118
- }
119
- }
120
- // No handle: the engine session does not exist yet (the first
121
- // parked event has not run — the lease holder creates it) or a
122
- // cross-pod restore transiently failed. Park rather than fail
123
- // closed: rejecting here would bounce the legitimate owner racing
124
- // the session's first turn. This is not fail-open — the resident
125
- // path re-checks the principal when the event drains, and the
126
- // drain treats a SessionAuthError as consume-and-commit, so a
127
- // foreign envelope that parks through this window costs one warn
128
- // line and never actuates, bricks, or replays the session.
129
- }
130
- const envelope = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ channelId,
131
- continuationToken,
132
- message, auth: (_c = options.auth) !== null && _c !== void 0 ? _c : null }, (options.title === undefined ? {} : { title: options.title })), (options.state === undefined ? {} : { state: options.state })), (options.mode === undefined ? {} : { mode: options.mode })), (options.purpose === undefined ? {} : { purpose: options.purpose })), (options.workspaceDir === undefined
133
- ? {}
134
- : { workspaceDir: options.workspaceDir })), (options.cloud === undefined ? {} : { cloud: options.cloud }));
135
- yield this.ledger.appendEvent(ledgerSessionId, {
136
- eventId: `evt_${randomBytes(12).toString("hex")}`,
137
- envelope: envelope,
138
- sourceTs: this.resolveSourceTs(options),
139
- });
140
- // Ack point: the event is durable (mailbox blob + ledger pending).
141
- // Do not await the pump: awaiting turns here would block HTTP create
142
- // until the first turn committed, so a client could never park a
143
- // mid-turn follow-up and a second pod would see a released lease.
144
- const serving = this.servePending(ledgerSessionId);
145
- return this.resolveDeliverHandle({
146
- channelId,
147
- continuationToken,
148
- ledgerSessionId,
149
- envelope,
150
- created: ensured.created,
151
- serving,
152
- });
153
- });
154
- }
155
- /**
156
- * The stand-in handle for a delivery whose event is parked while
157
- * another holder serves the session: real durable identity, no local
158
- * turn. `waitForCompletion` resolves immediately — the parked event's
159
- * turn runs on the holder, so there is nothing local to await (the
160
- * same contract as a coalesced send, whose outcome is not its own
161
- * message's turn). The event stream and re-keying live wherever the
162
- * turn runs, so both fail loudly here.
163
- */
164
- parkedSession(ledgerSessionId, envelope, isNew) {
165
- var _a;
166
- const unavailable = (what) => new Error(`ledger serving: ${what} is unavailable for session ${ledgerSessionId}: the parked event runs on the session's current lease holder, not this process`);
167
- return {
168
- id: ledgerSessionId,
169
- continuationToken: envelope.continuationToken,
170
- isNew,
171
- info: Object.assign(Object.assign(Object.assign({ id: ledgerSessionId, channelId: envelope.channelId, mode: (_a = envelope.mode) !== null && _a !== void 0 ? _a : "chat", auth: envelope.auth }, (envelope.title === undefined ? {} : { title: envelope.title })), (envelope.purpose === undefined
172
- ? {}
173
- : { purpose: envelope.purpose })), { continuationKey: envelope.continuationToken }),
174
- getEventStream: () => Promise.reject(unavailable("the event stream")),
175
- waitForCompletion: () => __awaiter(this, void 0, void 0, function* () { return ({ status: "finished" }); }),
176
- setContinuationToken: () => Promise.reject(unavailable("re-keying the continuation token")),
177
- };
178
- }
179
- /**
180
- * Return a handle as soon as the engine session exists, without waiting
181
- * for the pump to commit. If the pump settles without creating a
182
- * session (walk-away, or a throw before admit), surface that: rethrow
183
- * a failed claimant turn, otherwise the parked stand-in.
184
- */
185
- resolveDeliverHandle(args) {
186
- return __awaiter(this, void 0, void 0, function* () {
187
- const { channelId, continuationToken, ledgerSessionId, envelope, created, serving, } = args;
188
- for (;;) {
189
- const handle = yield this.findSession(channelId, continuationToken);
190
- if (handle !== undefined) {
191
- return handle;
192
- }
193
- const pump = this.pumps.get(ledgerSessionId);
194
- if (pump !== undefined) {
195
- yield Promise.race([
196
- new Promise((resolve) => {
197
- setTimeout(resolve, 15);
198
- }),
199
- pump.then(() => undefined, () => undefined),
200
- ]);
201
- continue;
202
- }
203
- const createdLate = yield this.findSession(channelId, continuationToken);
204
- if (createdLate !== undefined) {
205
- return createdLate;
206
- }
207
- yield serving;
208
- this.logger(`[ledger] deliver on channel "${channelId}": event parked for session ${ledgerSessionId} (continuation ${continuationToken}); another holder is serving it and no local session exists — its commit (or the janitor) drains the event`);
209
- return this.parkedSession(ledgerSessionId, envelope, created);
210
- }
211
- });
212
- }
213
- /**
214
- * Drain ordering: prefer the real source time over this pod's receipt
215
- * time. `coalesceSourceTs` is a Slack-style seconds string
216
- * (`"1712345678.000100"`); normalize sub-epoch-ms values to ms so they
217
- * stay comparable with the `Date.now()` fallback.
218
- */
219
- resolveSourceTs(options) {
220
- if (options.coalesceSourceTs !== undefined) {
221
- const parsed = Number(options.coalesceSourceTs);
222
- if (Number.isFinite(parsed) && parsed > 0) {
223
- return parsed < 1e11 ? Math.round(parsed * 1000) : parsed;
224
- }
225
- }
226
- return this.clock();
227
- }
228
- /**
229
- * One warning per delivery naming the send options the mailbox does not
230
- * replay — better a loud line than an author silently losing images or
231
- * admission semantics on a ledger-mode agent.
232
- */
233
- warnOnDroppedOptions(channelId, options) {
234
- const dropped = ["images", "sdkAgentId", "admission", "workspaceFiles"].filter((name) => options[name] !== undefined);
235
- if (dropped.length > 0) {
236
- this.logger(`[ledger] deliver on channel "${channelId}": dropping send options not replayed through the mailbox: ${dropped.join(", ")}`);
237
- }
238
- }
239
- /**
240
- * Serve a session's pending events. When a pump is already running the
241
- * caller walks away (the holder's commit re-reads pending) — except
242
- * that the holder's *final* commit may have released before this
243
- * append landed, so a re-check is chained behind it. Deliver never
244
- * awaits this promise for its full lifetime: awaiting a new pump would
245
- * block HTTP until the turn committed, and awaiting a running pump
246
- * would deadlock a send issued from inside one of its turns.
247
- */
248
- servePending(ledgerSessionId) {
249
- const running = this.pumps.get(ledgerSessionId);
250
- if (running !== undefined) {
251
- const recheck = running
252
- .catch(() => { })
253
- .then(() => this.repumpIfPending(ledgerSessionId))
254
- .catch(() => { });
255
- this.background.add(recheck);
256
- void recheck.finally(() => {
257
- this.background.delete(recheck);
258
- });
259
- return Promise.resolve();
260
- }
261
- return this.track(ledgerSessionId, () => this.runPump(ledgerSessionId));
262
- }
263
- /** Claim-or-defer entry used after an append and after a pump settles. */
264
- runPump(ledgerSessionId) {
265
- return __awaiter(this, void 0, void 0, function* () {
266
- const claim = yield this.ledger.claim(ledgerSessionId);
267
- if (!claim.claimed) {
268
- return;
269
- }
270
- yield this.runClaimedTurns(ledgerSessionId, claim.lease);
271
- });
272
- }
273
- /** Janitor hand-off: the claim already happened, just run the turns. */
274
- serveClaimed(claimed) {
275
- return this.track(claimed.sessionId, () => this.runClaimedTurns(claimed.sessionId, claimed.lease));
276
- }
277
- /**
278
- * Re-pump after the previous holder settled, covering the release window
279
- * where an append landed after that holder's final commit.
280
- */
281
- repumpIfPending(ledgerSessionId) {
282
- return __awaiter(this, void 0, void 0, function* () {
283
- if (this.pumps.has(ledgerSessionId)) {
284
- return;
285
- }
286
- const snapshot = yield this.ledger.readLedger(ledgerSessionId);
287
- if (snapshot === undefined || snapshot.pending.length === 0) {
288
- return;
289
- }
290
- if (snapshot.lease !== null && snapshot.lease.expiresAt > this.clock()) {
291
- return;
292
- }
293
- yield this.track(ledgerSessionId, () => this.runPump(ledgerSessionId));
294
- });
295
- }
296
- /** Register a pump in the per-session map for its lifetime. */
297
- track(ledgerSessionId, run) {
298
- const tracked = run().finally(() => {
299
- if (this.pumps.get(ledgerSessionId) === tracked) {
300
- this.pumps.delete(ledgerSessionId);
301
- }
302
- });
303
- this.pumps.set(ledgerSessionId, tracked);
304
- return tracked;
305
- }
306
- /**
307
- * The drain → turn → commit loop for a held lease. Heartbeats extend the
308
- * lease while turns run; a fenced heartbeat (or fenced commit) means a
309
- * new holder took over — discard without committing; the new holder
310
- * re-drains (at-least-once actuation, exactly-once ledger apply).
311
- */
312
- runClaimedTurns(ledgerSessionId, lease) {
313
- return __awaiter(this, void 0, void 0, function* () {
314
- var _a;
315
- let fenced = false;
316
- const heartbeat = setInterval(() => {
317
- this.ledger.heartbeat(ledgerSessionId, lease.generation).then((result) => {
318
- if (!result.ok) {
319
- fenced = true;
320
- }
321
- }, () => {
322
- // Transient KV failure: the next beat retries within the TTL.
323
- });
324
- }, this.heartbeatIntervalMs);
325
- (_a = heartbeat.unref) === null || _a === void 0 ? void 0 : _a.call(heartbeat);
326
- try {
327
- for (;;) {
328
- if (fenced) {
329
- return;
330
- }
331
- const { head, events } = yield this.ledger.drain(ledgerSessionId);
332
- if (events.length === 0) {
333
- yield this.ledger.release(ledgerSessionId, lease.generation);
334
- return;
335
- }
336
- for (const event of events) {
337
- if (fenced) {
338
- return;
339
- }
340
- const envelope = event.envelope;
341
- try {
342
- const session = yield this.deliverTurn(envelope);
343
- yield session.waitForCompletion();
344
- }
345
- catch (error) {
346
- if (!(error instanceof SessionAuthError)) {
347
- throw error;
348
- }
349
- // Defense in depth behind deliver's principal gate: a parked
350
- // envelope from a foreign principal (gate raced the session's
351
- // first turn, or a pre-gate build wrote it) is consumed and
352
- // committed away like any drained event. Aborting the batch
353
- // instead would replay the already-actuated turns before it
354
- // and re-throw on every future drain — one poison event must
355
- // cost a log line, never a stuck session.
356
- this.logger(`[ledger] session ${ledgerSessionId}: discarding parked event ${event.eventId} — its sender is not the session's principal (${describeError(error)})`);
357
- }
358
- }
359
- const commit = yield this.ledger.commit(ledgerSessionId, {
360
- drainedEventIds: events.map((event) => event.eventId),
361
- generation: lease.generation,
362
- expectedHead: head,
363
- commitId: randomUUID(),
364
- });
365
- if (commit.leaseReleased) {
366
- return;
367
- }
368
- }
369
- }
370
- catch (error) {
371
- if (error instanceof LedgerFencedError) {
372
- this.logger(`[ledger] session ${ledgerSessionId} fenced mid-turn (generation ${lease.generation}); discarding — the new holder re-drains`);
373
- return;
374
- }
375
- // The turn never committed; free the session for the next
376
- // append-driven claim instead of making it wait out the lease TTL
377
- // (and the janitor cadence). Best-effort: a fenced holder's
378
- // release is a no-op, and the events stay pending either way.
379
- yield this.ledger
380
- .release(ledgerSessionId, lease.generation)
381
- .catch(() => { });
382
- throw error;
383
- }
384
- finally {
385
- clearInterval(heartbeat);
386
- }
387
- });
388
- }
389
- }
390
- /** Mint the lease holder id for this process. */
391
- export function newLedgerPodId() {
392
- return `pod_${randomBytes(6).toString("hex")}`;
393
- }
@@ -1,60 +0,0 @@
1
- /**
2
- * The ledger janitor: lazy crash recovery has a hole — a session with
3
- * pending events and no future events would wait forever — so serving runs
4
- * this loop. It scans `ledger/` by prefix, finds pending-with-expired-lease
5
- * sessions, claims them, and hands each claim to the execution callback.
6
- * Self-host runs the same loop; it is part of the library, not the control
7
- * plane.
8
- *
9
- * Nothing starts on construction: call `start()` for the interval loop or
10
- * `runOnce()` for an explicit pass (what unit tests use).
11
- */
12
- import type { LedgerLease, SessionLedger } from "./session-ledger.js";
13
- export declare const DEFAULT_JANITOR_INTERVAL_MS = 15000;
14
- export interface JanitorClaimedSession {
15
- sessionId: string;
16
- lease: LedgerLease;
17
- head: number;
18
- pending: string[];
19
- }
20
- export interface JanitorRunResult {
21
- /** Ledgers scanned this pass. */
22
- scanned: number;
23
- /** Sessions this pass claimed and handed to the callback. */
24
- claimed: JanitorClaimedSession[];
25
- /** Orphan blob ids deleted (only when the sweep is enabled). */
26
- orphansDeleted: string[];
27
- }
28
- export interface LedgerJanitorOptions {
29
- ledger: SessionLedger;
30
- /**
31
- * Executes a recovered session: drain, run the turn, commit. Awaited so
32
- * one pass never claims more sessions than it can serve.
33
- */
34
- onSession: (session: JanitorClaimedSession) => Promise<void> | void;
35
- /** Scan cadence for `start()` (default {@link DEFAULT_JANITOR_INTERVAL_MS}). */
36
- intervalMs?: number;
37
- /**
38
- * Also sweep unreferenced mailbox blobs each pass. Off by default —
39
- * enable with `{ olderThanMs }` (see `SessionLedger.sweepOrphans`).
40
- */
41
- orphanSweep?: {
42
- olderThanMs?: number;
43
- };
44
- /** Called with per-session or sweep failures; the pass continues. */
45
- onError?: (error: unknown, sessionId?: string) => void;
46
- /** Time source for lease-expiry checks (default `Date.now`). */
47
- clock?: () => number;
48
- }
49
- export interface LedgerJanitor {
50
- start(): void;
51
- /**
52
- * Clear the interval AND await any in-flight pass: a running pass can
53
- * claim a session and spawn a turn, so shutdown must not proceed while
54
- * one is mid-scan.
55
- */
56
- stop(): Promise<void>;
57
- runOnce(): Promise<JanitorRunResult>;
58
- }
59
- export declare function createLedgerJanitor(options: LedgerJanitorOptions): LedgerJanitor;
60
- //# sourceMappingURL=janitor.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"janitor.d.ts","sourceRoot":"","sources":["../../src/ledger/janitor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAEV,WAAW,EACX,aAAa,EACd,MAAM,qBAAqB,CAAC;AAE7B,eAAO,MAAM,2BAA2B,QAAS,CAAC;AAElD,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,WAAW,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC/B,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,6DAA6D;IAC7D,OAAO,EAAE,qBAAqB,EAAE,CAAC;IACjC,gEAAgE;IAChE,cAAc,EAAE,MAAM,EAAE,CAAC;CAC1B;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,aAAa,CAAC;IACtB;;;OAGG;IACH,SAAS,EAAE,CAAC,OAAO,EAAE,qBAAqB,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACpE,gFAAgF;IAChF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,WAAW,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACvC,qEAAqE;IACrE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IACvD,gEAAgE;IAChE,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B,KAAK,IAAI,IAAI,CAAC;IACd;;;;OAIG;IACH,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtB,OAAO,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAAC;CACtC;AAED,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,oBAAoB,GAC5B,aAAa,CA2Hf"}
@@ -1,137 +0,0 @@
1
- /**
2
- * The ledger janitor: lazy crash recovery has a hole — a session with
3
- * pending events and no future events would wait forever — so serving runs
4
- * this loop. It scans `ledger/` by prefix, finds pending-with-expired-lease
5
- * sessions, claims them, and hands each claim to the execution callback.
6
- * Self-host runs the same loop; it is part of the library, not the control
7
- * plane.
8
- *
9
- * Nothing starts on construction: call `start()` for the interval loop or
10
- * `runOnce()` for an explicit pass (what unit tests use).
11
- */
12
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
- return new (P || (P = Promise))(function (resolve, reject) {
15
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
- step((generator = generator.apply(thisArg, _arguments || [])).next());
19
- });
20
- };
21
- export const DEFAULT_JANITOR_INTERVAL_MS = 15000;
22
- export function createLedgerJanitor(options) {
23
- var _a, _b, _c;
24
- const intervalMs = (_a = options.intervalMs) !== null && _a !== void 0 ? _a : DEFAULT_JANITOR_INTERVAL_MS;
25
- const clock = (_b = options.clock) !== null && _b !== void 0 ? _b : Date.now;
26
- const reportError = (_c = options.onError) !== null && _c !== void 0 ? _c : (() => {
27
- // Recovery is best-effort per pass; an unobserved failure is retried
28
- // on the next scan.
29
- });
30
- let timer;
31
- let running = false;
32
- /** The settled-shape promise of the pass currently scanning, if any. */
33
- let inFlight;
34
- const runPass = () => __awaiter(this, void 0, void 0, function* () {
35
- // An unparseable ledger record is reported and skipped: it degrades
36
- // that one session's recovery, never the whole agent's pass.
37
- const ledgers = yield options.ledger.listLedgers((key, error) => {
38
- reportError(new Error(`janitor scan: skipping unparseable ledger record at ${key}: ${String(error instanceof Error ? error.message : error)}`));
39
- });
40
- const claimed = [];
41
- for (const snapshot of ledgers) {
42
- if (snapshot.pending.length === 0) {
43
- continue;
44
- }
45
- if (snapshot.lease !== null && snapshot.lease.expiresAt > clock()) {
46
- continue;
47
- }
48
- let claim;
49
- try {
50
- claim = yield options.ledger.claim(snapshot.sessionId);
51
- }
52
- catch (error) {
53
- reportError(error, snapshot.sessionId);
54
- continue;
55
- }
56
- if (!claim.claimed) {
57
- continue;
58
- }
59
- const session = {
60
- sessionId: snapshot.sessionId,
61
- lease: claim.lease,
62
- head: claim.head,
63
- pending: claim.pending,
64
- };
65
- claimed.push(session);
66
- try {
67
- yield options.onSession(session);
68
- }
69
- catch (error) {
70
- reportError(error, snapshot.sessionId);
71
- }
72
- }
73
- let orphansDeleted = [];
74
- if (options.orphanSweep !== undefined) {
75
- try {
76
- const swept = yield options.ledger.sweepOrphans({
77
- olderThanMs: options.orphanSweep.olderThanMs,
78
- });
79
- orphansDeleted = swept.deleted;
80
- }
81
- catch (error) {
82
- reportError(error);
83
- }
84
- }
85
- return { scanned: ledgers.length, claimed, orphansDeleted };
86
- });
87
- const runOnce = () => {
88
- const pass = runPass();
89
- const settled = pass.then(() => undefined, () => undefined);
90
- inFlight = settled;
91
- void settled.finally(() => {
92
- if (inFlight === settled) {
93
- inFlight = undefined;
94
- }
95
- });
96
- return pass;
97
- };
98
- const tick = () => __awaiter(this, void 0, void 0, function* () {
99
- // Skip a tick rather than overlap passes when a scan outlasts the
100
- // cadence — overlapping claims would just fence each other.
101
- if (running) {
102
- return;
103
- }
104
- running = true;
105
- try {
106
- yield runOnce();
107
- }
108
- catch (error) {
109
- reportError(error);
110
- }
111
- finally {
112
- running = false;
113
- }
114
- });
115
- return {
116
- start() {
117
- var _a;
118
- if (timer !== undefined) {
119
- return;
120
- }
121
- timer = setInterval(() => {
122
- void tick();
123
- }, intervalMs);
124
- (_a = timer.unref) === null || _a === void 0 ? void 0 : _a.call(timer);
125
- },
126
- stop() {
127
- return __awaiter(this, void 0, void 0, function* () {
128
- if (timer !== undefined) {
129
- clearInterval(timer);
130
- timer = undefined;
131
- }
132
- yield inFlight;
133
- });
134
- },
135
- runOnce,
136
- };
137
- }