@cotal-ai/runtime 0.168.0 → 0.170.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/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/mesh-handler.d.ts +38 -153
- package/dist/mesh-handler.d.ts.map +1 -1
- package/dist/mesh-handler.js +41 -624
- package/dist/mesh-handler.js.map +1 -1
- package/dist/mesh-pause-timers.d.ts +119 -0
- package/dist/mesh-pause-timers.d.ts.map +1 -0
- package/dist/mesh-pause-timers.js +369 -0
- package/dist/mesh-pause-timers.js.map +1 -0
- package/dist/mesh-seat-watch.d.ts +142 -0
- package/dist/mesh-seat-watch.d.ts.map +1 -0
- package/dist/mesh-seat-watch.js +309 -0
- package/dist/mesh-seat-watch.js.map +1 -0
- package/dist/offline-command.d.ts.map +1 -1
- package/dist/offline-command.js +24 -22
- package/dist/offline-command.js.map +1 -1
- package/dist/run-command.d.ts.map +1 -1
- package/dist/run-command.js +21 -27
- package/dist/run-command.js.map +1 -1
- package/dist/run-driver.d.ts +1 -0
- package/dist/run-driver.d.ts.map +1 -1
- package/dist/run-driver.js +20 -15
- package/dist/run-driver.js.map +1 -1
- package/dist/run-host.d.ts.map +1 -1
- package/dist/run-host.js +2 -1
- package/dist/run-host.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/** A run's pause timers: arming or attaching to a checkpoint pause, pushing its deadline,
|
|
2
|
+
* claiming its fire, cancelling it, re-reading a wait's recorded message, and waiting for the
|
|
3
|
+
* pause's one-use settlement. The mesh handler composes it and passes its connections, binding,
|
|
4
|
+
* watcher and run services, each read at its use. */
|
|
5
|
+
import { EpEnvelopeError, chatStream, eptStreamName, eptSubject, handleCheckpointFire, heartbeatCheckpoint, mintCheckpoint, readCheckpointSettle, readCheckpointSpec, readCheckpointStatus, reconcileCheckpointSchedule, resumeCheckpoint, } from "@cotal-ai/core";
|
|
6
|
+
import { Cancelled } from "@cotal-ai/lang";
|
|
7
|
+
import { delay } from "./delay.js";
|
|
8
|
+
import { servedDespiteStarvation } from "./host-starvation.js";
|
|
9
|
+
import { PauseOverdue } from "./run-pause-host.js";
|
|
10
|
+
/** How often a pause that nobody will answer looks for the broker's fire. Same argument as
|
|
11
|
+
* `WAIT_POLL_MS`: the deadline is durable and this is only how late its observation can be. */
|
|
12
|
+
export const FIRE_POLL_MS = 2_000;
|
|
13
|
+
export function decodeMessage(data) {
|
|
14
|
+
try {
|
|
15
|
+
const v = JSON.parse(new TextDecoder().decode(data));
|
|
16
|
+
return v !== null && typeof v === "object" ? v : undefined;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// Someone else's malformed publish is not this run's failure. It does not match, and the wait
|
|
20
|
+
// goes on waiting — which is what would happen if the message had never been sent.
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** The pause timer and settle members of a mesh handler, over the handler's own inputs. */
|
|
25
|
+
export class PauseTimers {
|
|
26
|
+
deps;
|
|
27
|
+
constructor(deps) {
|
|
28
|
+
this.deps = deps;
|
|
29
|
+
}
|
|
30
|
+
get binding() { return this.deps.binding(); }
|
|
31
|
+
get kv() { return this.deps.kv(); }
|
|
32
|
+
get js() { return this.deps.js(); }
|
|
33
|
+
get jsm() { return this.deps.jsm(); }
|
|
34
|
+
get watcher() { return this.deps.watcher(); }
|
|
35
|
+
get services() { return this.deps.services(); }
|
|
36
|
+
get lag() { return this.deps.lag(); }
|
|
37
|
+
get onStarved() { return this.deps.onStarved; }
|
|
38
|
+
now() { return this.deps.now(); }
|
|
39
|
+
withdrawCancelledRelay(goalId, deadlineAt) { return this.deps.withdrawCancelledRelay(goalId, deadlineAt); }
|
|
40
|
+
/**
|
|
41
|
+
* Arm this pause, or ATTACH to the one already recorded under the same token.
|
|
42
|
+
*
|
|
43
|
+
* A mint is idempotent only if the whole spec is identical, so the recorded spec is the
|
|
44
|
+
* authority for BOTH halves of the mint's identity and a resume may not recompute either. The
|
|
45
|
+
* deadline, because `now() + duration` is a different deadline a second later, and the plane
|
|
46
|
+
* reads a different deadline as a different intent. And the HOLDER, because a resumed run does
|
|
47
|
+
* not necessarily arrive under the principal that minted its pause: the CLI mints a fresh holder
|
|
48
|
+
* per invocation, and a cross-host adoption is a different principal by definition. Only the
|
|
49
|
+
* recorded holder itself may mint again — completing its own crash between the spec and the
|
|
50
|
+
* status, where the identical spec is exactly what makes the retry idempotent. Everyone else
|
|
51
|
+
* attaches. Measured before the repair, through the CLI: one `resume` of a checkpoint-parked
|
|
52
|
+
* run re-minted under its own fresh holder, the plane refused ("a token is minted once"), and
|
|
53
|
+
* the interpreter recorded that infrastructure refusal as the step's own failure (L4000) — a
|
|
54
|
+
* stranded run whose journal blames the program.
|
|
55
|
+
*
|
|
56
|
+
* An already-passed deadline cannot be minted at all, correctly, because a due pause is not being
|
|
57
|
+
* armed. It needs its schedule re-emitted at the status's current generation, which is the
|
|
58
|
+
* reconciler's job — the same operation an attaching successor needs, so the two share the exit.
|
|
59
|
+
* A first arm whose deadline has passed has nothing to attach to and raises `PauseOverdue`.
|
|
60
|
+
* A spec with no status that this holder cannot complete is unrepairable from here, so it is
|
|
61
|
+
* raised rather than waited on.
|
|
62
|
+
*/
|
|
63
|
+
async arm(ref, deadline) {
|
|
64
|
+
// #1508: every read and write below rides a client-side deadline, and a deadline that elapsed
|
|
65
|
+
// because THIS PROCESS was off the CPU is not evidence about the plane. See `host-starvation`.
|
|
66
|
+
return await servedDespiteStarvation(() => this.armOnce(ref, deadline), this.lag, `arming the pause ${ref.token}`, this.onStarved);
|
|
67
|
+
}
|
|
68
|
+
/** `arm`, answering `true` when the deadline passed before anything was minted for it. Only a
|
|
69
|
+
* first arm finds that (`PauseOverdue`), and there is then no pause to settle, so a caller whose
|
|
70
|
+
* deadline is its own outcome takes `true` as that outcome. Cancellation still wins. */
|
|
71
|
+
async armUnlessDue(ref, deadline, signal) {
|
|
72
|
+
try {
|
|
73
|
+
await this.arm(ref, deadline);
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (error instanceof PauseOverdue) {
|
|
78
|
+
if (signal.cancelled)
|
|
79
|
+
throw new Cancelled(signal.reason ?? "cancelled");
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async armOnce(ref, deadline) {
|
|
86
|
+
if (this.services)
|
|
87
|
+
return this.services.pauses.arm(ref.token, deadline);
|
|
88
|
+
// Over already: an expiry or an answer landed while this host was away. Nothing to arm, and the
|
|
89
|
+
// caller reads the fact next.
|
|
90
|
+
if ((await readCheckpointSettle(this.jsm, this.binding.space, ref)) !== undefined)
|
|
91
|
+
return;
|
|
92
|
+
const prior = await readCheckpointSpec(this.kv, ref);
|
|
93
|
+
const now = this.now();
|
|
94
|
+
if (prior === undefined) {
|
|
95
|
+
// The first arm: this driver's own mint, under its own holder, at the deadline the caller
|
|
96
|
+
// computed from the duration it was just handed.
|
|
97
|
+
if (deadline <= now)
|
|
98
|
+
throw new PauseOverdue(ref.token, deadline, now);
|
|
99
|
+
await mintCheckpoint(this.kv, this.js, this.binding.space, {
|
|
100
|
+
ref,
|
|
101
|
+
instanceId: this.binding.instanceId,
|
|
102
|
+
epoch: this.binding.epoch,
|
|
103
|
+
holder: this.binding.holder,
|
|
104
|
+
deadline,
|
|
105
|
+
now,
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const mine = prior.holder.id === this.binding.holder.id
|
|
110
|
+
&& prior.holder.lifecycleUid === this.binding.holder.lifecycleUid;
|
|
111
|
+
if (mine && prior.initialDeadline > now) {
|
|
112
|
+
// A retry of this holder's own mint: idempotent-if-identical, and the one path that can
|
|
113
|
+
// repair a crash between the spec and the status, because only an identical spec completes.
|
|
114
|
+
await mintCheckpoint(this.kv, this.js, this.binding.space, {
|
|
115
|
+
ref,
|
|
116
|
+
instanceId: this.binding.instanceId,
|
|
117
|
+
epoch: this.binding.epoch,
|
|
118
|
+
holder: this.binding.holder,
|
|
119
|
+
deadline: prior.initialDeadline,
|
|
120
|
+
now,
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
// ATTACH: the pause exists and is not this attempt's to mint — another holder's pause, or this
|
|
125
|
+
// holder's own come back overdue (a due pause is being collected, not armed). Both need the
|
|
126
|
+
// same thing: the schedule re-emitted at THIS instance's coordinates, so the fire lands where
|
|
127
|
+
// this driver is listening. The settle the caller waits on next is coordinate-free.
|
|
128
|
+
const status = await readCheckpointStatus(this.kv, ref);
|
|
129
|
+
if (status === undefined) {
|
|
130
|
+
throw new Error(`checkpoint "${ref.token}" carries a spec with no status`
|
|
131
|
+
+ `${mine ? "" : ` held by ${prior.holder.id}, and only its own holder's identical re-mint can repair a half-minted pause`}; `
|
|
132
|
+
+ `this pause has to be reconciled on the plane before the run can go on`);
|
|
133
|
+
}
|
|
134
|
+
await reconcileCheckpointSchedule(this.kv, this.js, this.jsm, this.binding.space, {
|
|
135
|
+
ref,
|
|
136
|
+
instanceId: this.binding.instanceId,
|
|
137
|
+
epoch: this.binding.epoch,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** Push a live deadline out — the idle window restarting. The heartbeat CAS-advances the
|
|
141
|
+
* generation before replacing the timer, so a fire from the old one no-ops rather than racing. */
|
|
142
|
+
async push(ref, deadline) {
|
|
143
|
+
if (this.services)
|
|
144
|
+
return this.services.pauses.heartbeat(ref.token, deadline);
|
|
145
|
+
await heartbeatCheckpoint(this.kv, this.js, this.jsm, this.binding.space, {
|
|
146
|
+
ref,
|
|
147
|
+
instanceId: this.binding.instanceId,
|
|
148
|
+
epoch: this.binding.epoch,
|
|
149
|
+
deadline,
|
|
150
|
+
now: this.now(),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Take the broker's `.fire` for this deadline, if it has published one, and let the checkpoint
|
|
155
|
+
* plane judge it. Answers whether the fire SETTLED the pause.
|
|
156
|
+
*
|
|
157
|
+
* A fire is a MESSAGE and a settlement is a FACT; `handleCheckpointFire` is what turns one into
|
|
158
|
+
* the other, and a pause nobody answers ends no other way.
|
|
159
|
+
*
|
|
160
|
+
* READ BY SUBJECT rather than by a subscription, because the fire is a record of a deadline
|
|
161
|
+
* passing and callers here are already polling: `last_by_subj` gives the current one whether it
|
|
162
|
+
* arrived a moment ago or while this host was down, and a resume lands in the second case.
|
|
163
|
+
*
|
|
164
|
+
* IDEMPOTENT BY THE PLANE rather than by bookkeeping here: a fire handed over twice finds the
|
|
165
|
+
* pause settled and declines. The one repeated verdict with a side effect is `re-armed` under
|
|
166
|
+
* owner-behind clock skew, and over-emission is idempotent at the timer writer.
|
|
167
|
+
*/
|
|
168
|
+
async takeFire(ref) {
|
|
169
|
+
if (this.services)
|
|
170
|
+
return this.services.pauses.takeFire(ref.token);
|
|
171
|
+
const subject = eptSubject(this.binding.space, ref.endpoint, this.binding.instanceId, this.binding.epoch, ref.token, "fire");
|
|
172
|
+
const fired = await this.jsm.streams
|
|
173
|
+
.getMessage(eptStreamName(this.binding.space), { last_by_subj: subject })
|
|
174
|
+
.catch((e) => {
|
|
175
|
+
// 10037 is the stream saying there is no message on that subject, which IS the ordinary
|
|
176
|
+
// "the timer has not fired yet" answer. Every other error is the broker refusing or
|
|
177
|
+
// unreachable, and reading those as "not yet" is how a `wait` with a timeout outlives its
|
|
178
|
+
// own durable deadline in silence, which is the failure this method exists to prevent.
|
|
179
|
+
if (e?.code === 10037)
|
|
180
|
+
return null;
|
|
181
|
+
throw e;
|
|
182
|
+
});
|
|
183
|
+
if (fired === null || fired === undefined)
|
|
184
|
+
return false;
|
|
185
|
+
const verdict = await handleCheckpointFire(this.kv, this.js, this.jsm, this.binding.space, {
|
|
186
|
+
ref,
|
|
187
|
+
instanceId: this.binding.instanceId,
|
|
188
|
+
epoch: this.binding.epoch,
|
|
189
|
+
msg: { subject, ...(fired.header !== undefined ? { headers: fired.header } : {}), data: fired.data },
|
|
190
|
+
now: this.now(),
|
|
191
|
+
});
|
|
192
|
+
return verdict.acted;
|
|
193
|
+
}
|
|
194
|
+
/** Has this deadline settled? `undefined` for "no deadline" and for "not yet".
|
|
195
|
+
*
|
|
196
|
+
* A `wait` observes its deadlines by polling this, so the fire is taken on the same poll: the
|
|
197
|
+
* settle fact a `wait` is reading for is one this process has to produce, and reading for it
|
|
198
|
+
* without ever producing it is how a `wait` with a timeout waited past its timeout forever. */
|
|
199
|
+
async expired(ref) {
|
|
200
|
+
if (ref === undefined)
|
|
201
|
+
return undefined;
|
|
202
|
+
if (this.services) {
|
|
203
|
+
const settled = await this.services.pauses.readSettle(ref.token);
|
|
204
|
+
if (settled !== undefined)
|
|
205
|
+
return settled;
|
|
206
|
+
if (!(await this.services.pauses.takeFire(ref.token)))
|
|
207
|
+
return undefined;
|
|
208
|
+
return this.services.pauses.readSettle(ref.token);
|
|
209
|
+
}
|
|
210
|
+
const settled = await readCheckpointSettle(this.jsm, this.binding.space, ref);
|
|
211
|
+
if (settled !== undefined)
|
|
212
|
+
return settled;
|
|
213
|
+
if (!(await this.takeFire(ref)))
|
|
214
|
+
return undefined;
|
|
215
|
+
return await readCheckpointSettle(this.jsm, this.binding.space, ref);
|
|
216
|
+
}
|
|
217
|
+
/** End a deadline that is no longer waited on, by claiming its one-use settlement with no answer.
|
|
218
|
+
* A timer left armed would fire into a run that has moved on; claiming it is how the plane says
|
|
219
|
+
* "this pause is over" without a second mechanism for cancellation. */
|
|
220
|
+
async cancelTimer(ref) {
|
|
221
|
+
if (this.services)
|
|
222
|
+
return this.services.pauses.claim(ref.token);
|
|
223
|
+
const st = await readCheckpointStatus(this.kv, ref);
|
|
224
|
+
if (st?.value.state !== "waiting")
|
|
225
|
+
return;
|
|
226
|
+
try {
|
|
227
|
+
// The presenter is the ARMING holder read off the pause's own record, as everywhere on this
|
|
228
|
+
// plane: a timer minted by a predecessor is still this run's to end after a takeover, and a
|
|
229
|
+
// successor presenting its own identity would be refused (resume is holder-bound, SPEC
|
|
230
|
+
// 13.10). A spec that cannot be read leaves the timer to its own deadline, which the catch
|
|
231
|
+
// below already tolerates: it fires, settles expired, and nobody is reading that token.
|
|
232
|
+
const spec = await readCheckpointSpec(this.kv, ref);
|
|
233
|
+
if (spec === undefined)
|
|
234
|
+
return;
|
|
235
|
+
await resumeCheckpoint(this.kv, this.js, this.jsm, this.binding.space, {
|
|
236
|
+
ref,
|
|
237
|
+
presenter: spec.holder,
|
|
238
|
+
now: this.now(),
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
catch (e) {
|
|
242
|
+
// IT SETTLED UNDERNEATH US is one outcome, and it is the expected one: the deadline won a
|
|
243
|
+
// race it was always allowed to win, the caller has its answer either way, and a claimed
|
|
244
|
+
// timer is not a fact anyone reads. A BROKER THAT REFUSED OR WENT AWAY is a different thing
|
|
245
|
+
// wearing the same silence, and this catch used to swallow both. It still must not replace
|
|
246
|
+
// the caller's answer — a timer left armed fires, settles expired, and nobody reads that
|
|
247
|
+
// token — so the second one is loud on the way past instead.
|
|
248
|
+
const settledAlready = e instanceof EpEnvelopeError
|
|
249
|
+
&& (e.code === "conflict" || e.code === "failed-precondition");
|
|
250
|
+
if (!settledAlready)
|
|
251
|
+
console.error(`run ${this.binding.runId}: the pause ${ref.token} could not be claimed (${e.message}); it will fire and settle expired with nobody reading it`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** The message at a recorded stream sequence — the re-bind path after a crash. */
|
|
255
|
+
async messageAt(seq, requestId) {
|
|
256
|
+
if (this.services) {
|
|
257
|
+
const message = decodeMessage(await this.services.waits.messageAt(requestId, seq));
|
|
258
|
+
if (message === undefined)
|
|
259
|
+
throw new Error(`wait ${requestId}'s recorded message does not decode`);
|
|
260
|
+
return message;
|
|
261
|
+
}
|
|
262
|
+
const m = await this.jsm.streams.getMessage(chatStream(this.binding.space), { seq });
|
|
263
|
+
if (m === null || m === undefined) {
|
|
264
|
+
throw new Error(`the message this wait matched (sequence ${seq}) is no longer on the channel's stream; a recorded match cannot be re-read`);
|
|
265
|
+
}
|
|
266
|
+
const msg = decodeMessage(m.data);
|
|
267
|
+
if (msg === undefined)
|
|
268
|
+
throw new Error(`the message at sequence ${seq} did not decode; a recorded match cannot be re-read`);
|
|
269
|
+
return msg;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Wait for this token's one-use settlement.
|
|
273
|
+
*
|
|
274
|
+
* A settle that ALREADY landed is the ordinary case on a resume: the run crashed while paused and
|
|
275
|
+
* the timer fired, or somebody answered, without it. Reading before waiting is not an
|
|
276
|
+
* optimization, it is the difference between resuming and waiting forever for an event that is
|
|
277
|
+
* already in the past.
|
|
278
|
+
*/
|
|
279
|
+
async settle(ref, signal, relayed = false) {
|
|
280
|
+
// #1508, and the load-bearing half: this is where a `sleep` spends its whole duration, so this
|
|
281
|
+
// is where a starved host was reporting its own scheduling as the effect's failure. The pause
|
|
282
|
+
// and its timer are durable facts on the plane; re-reading them observes the same world, and a
|
|
283
|
+
// sleep whose deadline passed while this process was blocked settles `ok`, late.
|
|
284
|
+
try {
|
|
285
|
+
return await servedDespiteStarvation(() => this.settleOnce(ref, signal), this.lag, `waiting on the pause ${ref.token}`, this.onStarved);
|
|
286
|
+
}
|
|
287
|
+
catch (e) {
|
|
288
|
+
// An `ask` attempt or an escalation may have relayed this token to a seat.
|
|
289
|
+
if (e instanceof Cancelled && relayed)
|
|
290
|
+
await this.withdrawCancelledRelay(ref.token);
|
|
291
|
+
throw e;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
async settleOnce(ref, signal) {
|
|
295
|
+
const already = this.services ? await this.services.pauses.readSettle(ref.token) : await readCheckpointSettle(this.jsm, this.binding.space, ref);
|
|
296
|
+
if (already !== undefined)
|
|
297
|
+
return already;
|
|
298
|
+
// The watcher waits for the FACT. For a pause with an answer the fact arrives because somebody
|
|
299
|
+
// answered; for a pause nobody answers - a `sleep`, or a `checkpoint` whose timeout wins - it
|
|
300
|
+
// arrives only because this process took the fire and the plane wrote it. So the two run
|
|
301
|
+
// together for the life of one wait, and the pump ends when the wait does.
|
|
302
|
+
const wait = { over: false };
|
|
303
|
+
const pump = this.pumpFires(ref, wait);
|
|
304
|
+
try {
|
|
305
|
+
return await Promise.race([
|
|
306
|
+
this.watcher.awaitSettle(ref),
|
|
307
|
+
// A pump that ENDED is not an answer, only one that FAILED is: a failure means this process
|
|
308
|
+
// cannot expire the pause, so it is raised rather than absorbed and the step stays pending.
|
|
309
|
+
pump.then(() => new Promise(() => { })),
|
|
310
|
+
// A cancelled branch stops parking NOW (§7.6): the rejection decides the race first, so a
|
|
311
|
+
// `checkpoint` cannot mistake its own claim below for an answer, and then the claim ends
|
|
312
|
+
// the pause in the world. The claim is not awaited here — a claim that loses its own race
|
|
313
|
+
// is tolerated by `cancelTimer`, and the discharge sweep at the run's completion re-claims
|
|
314
|
+
// idempotently for the case where this process died before the claim landed.
|
|
315
|
+
...(signal === undefined ? [] : [this.settleCancelled(ref, signal)]),
|
|
316
|
+
]);
|
|
317
|
+
}
|
|
318
|
+
finally {
|
|
319
|
+
wait.over = true;
|
|
320
|
+
// THE PUMP IS DRAINED BEFORE THE SETTLE RETURNS (#1460). The race above has already decided
|
|
321
|
+
// — the fact, the failure, or the cancellation is this call's answer — but the pump's
|
|
322
|
+
// in-flight `takeFire` is a journal replay under the run's takeover id, created, read and
|
|
323
|
+
// deleted inside one `replayRunJournal` call with nothing serialising it across two
|
|
324
|
+
// authorities for the same run and lease. A settle that returned past it let a completed
|
|
325
|
+
// `driveRun` hold the replay open after it resolved, and the next reader under the same
|
|
326
|
+
// takeover hit `RunJournalReplayRaced` about a driver that did not exist. So only the RETURN
|
|
327
|
+
// is delayed, until the pump has ended: `wait.over` stops it after its current `takeFire`
|
|
328
|
+
// without starting another, and awaiting it here means no pump fire is left in flight on any
|
|
329
|
+
// exit. A pump that FAILED raises through this await on its way out, exactly as it did
|
|
330
|
+
// through the race; the settle's own answer is decided either way and is what this returns.
|
|
331
|
+
await pump;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/** Reject with `Cancelled` the moment this branch's signal fires, then claim the pause so its
|
|
335
|
+
* armed schedule cannot fire into a run that has moved on. The claim also writes the one-use
|
|
336
|
+
* settle, which is what lets an abandoned `awaitSettle` poll loop see a fact and end. */
|
|
337
|
+
settleCancelled(ref, signal) {
|
|
338
|
+
return new Promise((_, reject) => {
|
|
339
|
+
let fired = false;
|
|
340
|
+
const fire = (reason) => {
|
|
341
|
+
if (fired)
|
|
342
|
+
return;
|
|
343
|
+
fired = true;
|
|
344
|
+
reject(new Cancelled(reason ?? "cancelled"));
|
|
345
|
+
void this.cancelTimer(ref).catch(() => undefined);
|
|
346
|
+
};
|
|
347
|
+
if (signal.cancelled) {
|
|
348
|
+
fire(signal.reason);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
signal.onCancel(fire);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
/** Take this deadline's fire for as long as somebody is waiting on it. */
|
|
355
|
+
async pumpFires(ref, wait) {
|
|
356
|
+
while (!wait.over) {
|
|
357
|
+
await this.takeFire(ref);
|
|
358
|
+
// THE FLAG IS RE-READ AFTER THE FIRE, before the poll sleeps (#1460): `takeFire` is a journal
|
|
359
|
+
// replay under the run's takeover id, so the wait can have ENDED while this one was in
|
|
360
|
+
// flight — and a pump that slept another poll on a wait that is over keeps exactly one more
|
|
361
|
+
// fire in flight past the drain `settleOnce` awaits. Checked here, the pump ends right after
|
|
362
|
+
// its current fire without starting another.
|
|
363
|
+
if (wait.over)
|
|
364
|
+
break;
|
|
365
|
+
await delay(FIRE_POLL_MS);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
//# sourceMappingURL=mesh-pause-timers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mesh-pause-timers.js","sourceRoot":"","sources":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S,CAAC,MAAM,IAAI,WAAW,CAAC,CAAC,CAAC;gBAC7C,KAAK,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YACpD,CAAC,CAAC;YACF,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;gBACrB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBACpB,OAAO;YACT,CAAC;YACD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,0EAA0E;IAClE,KAAK,CAAC,SAAS,CAAC,GAAkB,EAAE,IAAuB;QACjE,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YAClB,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACzB,8FAA8F;YAC9F,uFAAuF;YACvF,4FAA4F;YAC5F,6FAA6F;YAC7F,6CAA6C;YAC7C,IAAI,IAAI,CAAC,IAAI;gBAAE,MAAM;YACrB,MAAM,KAAK,CAAC,YAAY,CAAC,CAAC;QAC5B,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/** A run's seat watch: the checkpoint relay that tells a seat about an escalated pause and
|
|
2
|
+
* withdraws that relay, and the presence watch that reads whether a seat's incarnation is still
|
|
3
|
+
* live, superseded or lapsed. The mesh handler composes it; every input, the handler's own
|
|
4
|
+
* members included, is read through the handler at its use. */
|
|
5
|
+
import { type ActionContext, type EpAttributedReply, type EpVerbTarget, type GoalRef, type Presence, type ResolvedService } from "@cotal-ai/core";
|
|
6
|
+
import { type CheckpointRequest, type EffectContext } from "@cotal-ai/lang";
|
|
7
|
+
import type { KV } from "@nats-io/kv";
|
|
8
|
+
import { type MeshHandlerBinding, type SeatAddress } from "./mesh-handler.js";
|
|
9
|
+
/** One caller's window across polls of the presence death watch. */
|
|
10
|
+
export interface DeathWatch {
|
|
11
|
+
lapsedSince?: number;
|
|
12
|
+
lastReadAt?: number;
|
|
13
|
+
}
|
|
14
|
+
/** What the seat watch reads from the handler that composes it, each at its use. */
|
|
15
|
+
export interface SeatWatchDeps {
|
|
16
|
+
self(): unknown;
|
|
17
|
+
binding(): MeshHandlerBinding;
|
|
18
|
+
roster(): ReadonlyMap<string, {
|
|
19
|
+
uid: string;
|
|
20
|
+
owner?: string;
|
|
21
|
+
actor?: string;
|
|
22
|
+
goalId?: string;
|
|
23
|
+
host?: string;
|
|
24
|
+
}>;
|
|
25
|
+
managerServices(): Map<string, Promise<ResolvedService>>;
|
|
26
|
+
now(): number;
|
|
27
|
+
actionCtx(): Promise<ActionContext>;
|
|
28
|
+
presenceRegistry(): Promise<KV>;
|
|
29
|
+
seatAddress(name: string, uid?: string): {
|
|
30
|
+
address: SeatAddress;
|
|
31
|
+
why?: undefined;
|
|
32
|
+
} | {
|
|
33
|
+
address?: undefined;
|
|
34
|
+
why: "not-in-roster" | "no-floor";
|
|
35
|
+
};
|
|
36
|
+
manager(command: string, instanceId?: string): Promise<ResolvedService>;
|
|
37
|
+
invokeManager(service: ResolvedService, command: string, args: Record<string, unknown> | undefined, opts: {
|
|
38
|
+
target?: EpVerbTarget;
|
|
39
|
+
deadlineMs?: number;
|
|
40
|
+
id?: string;
|
|
41
|
+
}): Promise<EpAttributedReply>;
|
|
42
|
+
relayManagerBeforeSubmit(host: string | undefined, command: string, ref: GoalRef, kind: "turn" | "ask" | "checkpoint", label: string): Promise<ResolvedService>;
|
|
43
|
+
}
|
|
44
|
+
/** The checkpoint relay and presence watch members of a mesh handler, over the handler's own inputs. */
|
|
45
|
+
export declare class SeatWatch {
|
|
46
|
+
private readonly deps;
|
|
47
|
+
constructor(deps: SeatWatchDeps);
|
|
48
|
+
private get handler();
|
|
49
|
+
private get binding();
|
|
50
|
+
private get roster();
|
|
51
|
+
private get managerServices();
|
|
52
|
+
private now;
|
|
53
|
+
private actionCtx;
|
|
54
|
+
private presenceRegistry;
|
|
55
|
+
private seatAddress;
|
|
56
|
+
private manager;
|
|
57
|
+
private invokeManager;
|
|
58
|
+
private relayManagerBeforeSubmit;
|
|
59
|
+
/**
|
|
60
|
+
* An escalated checkpoint, delivered to the agent it names.
|
|
61
|
+
*
|
|
62
|
+
* `to` is legal only with `onExpiry: "escalate"` and the escalation is the SECOND mint, so this
|
|
63
|
+
* runs once per chain at most. The addressee is resolved through the run's own roster: a name
|
|
64
|
+
* this run spawned is a seat and is told, and anything else is a person — the pause is
|
|
65
|
+
* answerable from anywhere by design, its addressee is on the entry, and refusing a program
|
|
66
|
+
* that escalates to a human would be this host narrowing an option the reference leaves open.
|
|
67
|
+
*
|
|
68
|
+
* The relay is the ask's, because the two are the same act: one turn on the seat, carrying what
|
|
69
|
+
* is asked and the token to answer under, settling on the checkpoint plane rather than on the
|
|
70
|
+
* relay's own terminal.
|
|
71
|
+
*/
|
|
72
|
+
relayEscalation(req: CheckpointRequest, ctx: EffectContext, token: string, deadlineAt: number): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Submit one relay to a seat as a `turn` goal, and read its acceptance.
|
|
75
|
+
*
|
|
76
|
+
* IDEMPOTENT BY THE GOAL RECORD, which is also the arbiter when the invoke does not come back:
|
|
77
|
+
* a relay that landed and lost its reply is a relay that landed, and re-submitting it would put
|
|
78
|
+
* a second turn on the seat for one request. A seat the serve boundary reports gone is the
|
|
79
|
+
* agent-down failure (L4002); every other refusal is this endpoint's and is uncatchable.
|
|
80
|
+
*
|
|
81
|
+
* The relay carries the pause's own `deadlineAt` and the manager holds it to that instant: a
|
|
82
|
+
* duration counted from the manager's acceptance would outlive the pause by the submit's trip
|
|
83
|
+
* plus skew and show the seat an attempt whose answer is refused. A relay the manager refuses as
|
|
84
|
+
* already past, and that is past on this clock too, has nobody left to tell, because the pause
|
|
85
|
+
* denies at that same instant; the arm that follows meets the passed deadline and decides the
|
|
86
|
+
* outcome.
|
|
87
|
+
*/
|
|
88
|
+
relayToSeat(seat: SeatAddress, goalId: string, payload: string, deadlineAt: number, kind: "ask" | "checkpoint", step: string): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Withdraw the relay a cancelled step sent to a seat, through the manager's reserved `cancel`
|
|
91
|
+
* (SPEC 13.6 item 4), so the seat is not shown it again. A token no relay rode has no goal and
|
|
92
|
+
* nothing to withdraw, and a relay that already ended is the outcome either way: the manager
|
|
93
|
+
* refuses it with the cached terminal, so a repeated withdrawal is a no-op. Any other refusal
|
|
94
|
+
* throws under the code it was refused with, which leaves a discharge open to retry.
|
|
95
|
+
*
|
|
96
|
+
* Only the manager that accepted the relay holds it, and the class rail reaches any member, so a
|
|
97
|
+
* refusal is final only from the accepter its index entry names. One from another member is
|
|
98
|
+
* re-issued on the class rail, as the spawn discharge's despawn is.
|
|
99
|
+
*/
|
|
100
|
+
withdrawRelay(goalId: string): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Withdraw a cancelled step's relay before the cancellation leaves the step, so the scope it
|
|
103
|
+
* settles shows the seat nothing the run withdrew. A withdrawal that fails is tried again rather
|
|
104
|
+
* than left to the discharge, which runs only when the run ends: until then the relay would stay
|
|
105
|
+
* first on the seat and hold back the run's next turn to it. The retries stop when the relay's
|
|
106
|
+
* deadline passes, because the manager no longer serves it then, and a failure that outlasts it
|
|
107
|
+
* is the step's error. A refusal no retry can change is the step's error at once, since retrying
|
|
108
|
+
* it would only hold the cancelled step in its scope until that deadline: of the refusals, only
|
|
109
|
+
* `unavailable` (a manager still reconciling at boot) and `deadline-exceeded` (a lost reply) are
|
|
110
|
+
* tried again.
|
|
111
|
+
*
|
|
112
|
+
* That deadline is read from the relay's goal, where the accepting manager recorded the instant
|
|
113
|
+
* it stops serving the relay; a withdrawal holds only the goal id. No deadline is known until that
|
|
114
|
+
* goal is read, and the relay may be served until then, so a failed read is tried again as well.
|
|
115
|
+
* A goal record that can never yield a deadline is thrown.
|
|
116
|
+
*/
|
|
117
|
+
withdrawCancelledRelay(goalId: string, deadlineAt?: number): Promise<void>;
|
|
118
|
+
/** Every live presence row {@link isPresenceRow} accepts under its KV key. Foreign bytes in
|
|
119
|
+
* the bucket are skipped — a peer's malformed self-publish must not break another agent's name
|
|
120
|
+
* resolution — and what an ABSENT row means belongs to the caller: `resolveMemberPrincipal`
|
|
121
|
+
* refuses the join loudly, and `waitDown` reads it as the death it is waiting for. */
|
|
122
|
+
presenceRows(): Promise<Presence[]>;
|
|
123
|
+
/** One poll of the presence death watch `wait(down)` and `turn` share. `undefined` means this
|
|
124
|
+
* incarnation is still live, or a lapse has not yet stood for {@link LAPSE_CONFIRM_MS}. A missing
|
|
125
|
+
* incarnation is `"superseded"` when a live row holds the name and `"lapsed"` when nothing does.
|
|
126
|
+
* `watch` is the caller's own window, written here and read on the next poll. An incomplete scan
|
|
127
|
+
* decides nothing and returns `undefined`; every other read failure is raised. */
|
|
128
|
+
presenceDeath(name: string, uid: string, watch: DeathWatch, decided: () => Promise<boolean>): Promise<"superseded" | "lapsed" | undefined>;
|
|
129
|
+
/** Whether a lapse of `name#uid` confirmed from presence stands as the death, asked of the manager
|
|
130
|
+
* first. A lapse is only the seat's heartbeats going quiet: on a loaded host a live seat's presence
|
|
131
|
+
* writer stalls past {@link LAPSE_CONFIRM_MS} while its process goes on working (#2807), and the
|
|
132
|
+
* manager owns that process. Only the manager holding the seat has a running row under its
|
|
133
|
+
* incarnation, so that row keeps it alive whoever reports it. A miss decides only from the manager
|
|
134
|
+
* that allocated it, the committer of its spawn terminal: another member of the class rail knows
|
|
135
|
+
* only its own seats, so the class rail is asked again, as a spawn's discharge asks it. An allocator
|
|
136
|
+
* that does not answer leaves presence as the only witness, which is the case presence is the
|
|
137
|
+
* authority for: the manager died with the seat. The asking can outlast the caller's own
|
|
138
|
+
* cancellation, deadline or terminal, so once `decided` holds nothing stands and no refusal is
|
|
139
|
+
* raised: that authority decides at the caller's next check. */
|
|
140
|
+
private lapseStands;
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=mesh-seat-watch.d.ts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