@lightblues/jig 0.0.4 → 0.0.5

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.
@@ -94,6 +94,7 @@ export declare class PaseoThreadRunner<TResult = unknown> {
94
94
  private o;
95
95
  private quietMs;
96
96
  private tailLimit;
97
+ private waitMs;
97
98
  private log;
98
99
  constructor(o: PaseoThreadRunnerOptions<TResult>);
99
100
  running(threadId: string): boolean;
@@ -116,7 +117,10 @@ export declare class PaseoThreadRunner<TResult = unknown> {
116
117
  /**
117
118
  * After a restart: a message the host sent and has no answer for is looked up in its agent's timeline. A turn
118
119
  * still running is followed (`following`); one that ended is stored (`finished` / `failed`); a message the
119
- * agent never got fails, unless it was sent within `graceMs` (`pending`).
120
+ * agent never got fails, unless it was sent within `graceMs`: then the run stays registered (`pending`) and the
121
+ * runner keeps looking, so it resolves by itself to `following` / `finished` / `failed` (`RUN_ERROR` and
122
+ * `saveError`) without the host doing anything. Calling it again for the same run while pending answers
123
+ * `pending`; another run on the thread answers `busy`.
120
124
  */
121
125
  recover(req: RecoverRequest): Promise<RecoverResult>;
122
126
  close(): void;
@@ -136,6 +140,9 @@ export declare class PaseoThreadRunner<TResult = unknown> {
136
140
  * the held events the tail did not have. An ended turn is finished instead.
137
141
  */
138
142
  private regap;
143
+ /** A pending recovery: look again every `waitMs` until the message shows up or the deadline passes. */
144
+ private waitFor;
145
+ private checkWaiting;
139
146
  /** Reads the agent and its tail and rebuilds the run; a full page without our message is read again, deeper. With `keep`, a tail without our message is an error rather than "not found". */
140
147
  private retail;
141
148
  /** Finishes a turn the re-tail found ended, with the end event held meanwhile when there is one. */
@@ -14,6 +14,7 @@ export class PaseoThreadRunner {
14
14
  o;
15
15
  quietMs;
16
16
  tailLimit;
17
+ waitMs;
17
18
  log;
18
19
  constructor(o) {
19
20
  if (typeof o.filter !== "function")
@@ -21,6 +22,7 @@ export class PaseoThreadRunner {
21
22
  this.o = o;
22
23
  this.quietMs = o.quietMs ?? 60_000;
23
24
  this.tailLimit = o.tailLimit ?? 300;
25
+ this.waitMs = Math.max(100, Math.min(2000, this.quietMs / 4));
24
26
  this.log = o.log ?? ((m) => console.error(m));
25
27
  }
26
28
  running(threadId) {
@@ -77,12 +79,16 @@ export class PaseoThreadRunner {
77
79
  /**
78
80
  * After a restart: a message the host sent and has no answer for is looked up in its agent's timeline. A turn
79
81
  * still running is followed (`following`); one that ended is stored (`finished` / `failed`); a message the
80
- * agent never got fails, unless it was sent within `graceMs` (`pending`).
82
+ * agent never got fails, unless it was sent within `graceMs`: then the run stays registered (`pending`) and the
83
+ * runner keeps looking, so it resolves by itself to `following` / `finished` / `failed` (`RUN_ERROR` and
84
+ * `saveError`) without the host doing anything. Calling it again for the same run while pending answers
85
+ * `pending`; another run on the thread answers `busy`.
81
86
  */
82
87
  async recover(req) {
83
- if (this.runs.has(req.threadId))
84
- return "busy";
85
- const run = this.newRun(req.threadId, req.runId, req.agentId, req.metadata, true);
88
+ const cur = this.runs.get(req.threadId);
89
+ if (cur)
90
+ return cur.waiting && cur.runId === req.runId ? "pending" : "busy";
91
+ const run = this.newRun(req.threadId, req.runId, req.agentId, req.metadata, false);
86
92
  this.runs.set(run.threadId, run);
87
93
  try {
88
94
  // Hold events from the first one: the turn may end while we subscribe.
@@ -90,10 +96,15 @@ export class PaseoThreadRunner {
90
96
  run.unsub = await this.o.daemon.subscribe(req.agentId, (e) => this.onEvent(run, e));
91
97
  const t = await this.retail(run);
92
98
  if (!t.found) {
93
- this.cleanup(run);
94
99
  const sent = req.sentAt == null ? 0 : new Date(req.sentAt).getTime();
95
- if (Date.now() - sent < (req.graceMs ?? 60_000))
100
+ const deadline = sent + (req.graceMs ?? 60_000);
101
+ if (Date.now() < deadline) {
102
+ run.waiting = { deadline };
103
+ run.buffer = null;
104
+ this.waitFor(run);
96
105
  return "pending";
106
+ }
107
+ this.cleanup(run);
97
108
  await this.o.store.saveError(req.threadId, { runId: req.runId, agentId: req.agentId, error: "the agent never got the message" });
98
109
  return "failed";
99
110
  }
@@ -115,7 +126,7 @@ export class PaseoThreadRunner {
115
126
  }
116
127
  // ---- internals
117
128
  newRun(threadId, runId, agentId, metadata, ours) {
118
- return { threadId, runId, agentId, metadata, turn: new TurnFolder(), ours, turnId: null, epoch: null, seq: -1, buffer: null, again: null, sentAt: null, stopping: false, unsub: null, last: Date.now(), watch: null, done: false };
129
+ return { threadId, runId, agentId, metadata, turn: new TurnFolder(), ours, turnId: null, epoch: null, seq: -1, buffer: null, again: null, sentAt: null, waiting: null, stopping: false, unsub: null, last: Date.now(), watch: null, done: false };
119
130
  }
120
131
  started(run) {
121
132
  return { type: EventType.RUN_STARTED, threadId: run.threadId, runId: run.runId, ...(run.metadata ? { metadata: run.metadata } : {}) };
@@ -248,6 +259,42 @@ export class PaseoThreadRunner {
248
259
  this.fail(run, `${why}: ${e.message}`);
249
260
  }
250
261
  }
262
+ /** A pending recovery: look again every `waitMs` until the message shows up or the deadline passes. */
263
+ waitFor(run) {
264
+ if (run.watch || run.done)
265
+ return;
266
+ run.watch = setInterval(() => void this.checkWaiting(run), this.waitMs);
267
+ run.watch.unref();
268
+ }
269
+ async checkWaiting(run) {
270
+ if (run.done || !run.waiting || run.buffer)
271
+ return;
272
+ try {
273
+ const t = await this.retail(run);
274
+ if (run.done || !run.waiting)
275
+ return;
276
+ if (!t.found) {
277
+ if (Date.now() < run.waiting.deadline)
278
+ return;
279
+ return this.fail(run, "the agent never got the message");
280
+ }
281
+ const waiting = run.waiting;
282
+ run.waiting = null;
283
+ if (run.watch)
284
+ clearInterval(run.watch);
285
+ run.watch = null;
286
+ if (t.running) {
287
+ this.watch(run);
288
+ this.drain(run, true);
289
+ return;
290
+ }
291
+ void waiting;
292
+ await this.finishHeld(run);
293
+ }
294
+ catch (e) {
295
+ this.log(`agent-paseo thread ${run.threadId}: waiting: ${e.message}`);
296
+ }
297
+ }
251
298
  /** Reads the agent and its tail and rebuilds the run; a full page without our message is read again, deeper. With `keep`, a tail without our message is an error rather than "not found". */
252
299
  async retail(run, keep = false) {
253
300
  const a = await this.o.daemon.agent(run.agentId);
@@ -323,6 +370,7 @@ export class PaseoThreadRunner {
323
370
  }
324
371
  cleanup(run) {
325
372
  run.done = true;
373
+ run.waiting = null;
326
374
  run.unsub?.();
327
375
  run.unsub = null;
328
376
  if (run.watch)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightblues/jig",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Reusable parts for my apps: agent runtimes (Paseo, pi) that speak AG-UI, a thin thread-following client, and more as they prove reusable.",
5
5
  "type": "module",
6
6
  "license": "MIT",