tryharness 0.7.67 → 0.7.69

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 (54) hide show
  1. package/lib/boardFileCommands.mjs +5 -7
  2. package/lib/boardFiles.mjs +5 -0
  3. package/lib/boardProtocol.json +50 -64
  4. package/lib/boardWaits.mjs +6 -0
  5. package/lib/cards.mjs +13 -1
  6. package/lib/comfyExecution.mjs +545 -0
  7. package/lib/context.mjs +34 -20
  8. package/lib/contextWorker.mjs +199 -0
  9. package/lib/coordinator.mjs +224 -22
  10. package/lib/daemon.mjs +196 -14
  11. package/lib/dispatchContract.json +313 -0
  12. package/lib/dispatchTools.js +87 -0
  13. package/lib/dispatches.mjs +975 -0
  14. package/lib/environment.mjs +54 -69
  15. package/lib/files.mjs +35 -0
  16. package/lib/frames.mjs +5 -9
  17. package/lib/goalCommands.mjs +13 -10
  18. package/lib/kept.mjs +131 -0
  19. package/lib/landed.mjs +1 -34
  20. package/lib/library.mjs +18 -11
  21. package/lib/live.mjs +30 -11
  22. package/lib/localAgentAsk.mjs +27 -4
  23. package/lib/localStack.mjs +17 -13
  24. package/lib/machineRoutes.mjs +5 -0
  25. package/lib/machineRuns.mjs +2 -1
  26. package/lib/mcp-stdio.mjs +42 -1
  27. package/lib/openHere.mjs +36 -0
  28. package/lib/openRequests.mjs +56 -0
  29. package/lib/ownMachines.mjs +232 -24
  30. package/lib/phones.mjs +29 -4
  31. package/lib/places.mjs +44 -11
  32. package/lib/portCalls.mjs +54 -0
  33. package/lib/promptDecider.mjs +471 -0
  34. package/lib/promptFlow.mjs +562 -0
  35. package/lib/promptScopes.mjs +670 -0
  36. package/lib/provenance.mjs +3 -2
  37. package/lib/runAuthority.mjs +14 -2
  38. package/lib/runBrief.mjs +2 -2
  39. package/lib/runWorker.mjs +25 -5
  40. package/lib/runner.mjs +62 -6
  41. package/lib/sealed.mjs +37 -0
  42. package/lib/sessionControl.mjs +47 -14
  43. package/lib/sessionIndex.mjs +11 -3
  44. package/lib/store.mjs +362 -40
  45. package/lib/swarm.mjs +85 -34
  46. package/lib/swarmMemory.mjs +39 -16
  47. package/lib/taskCommands.mjs +125 -1
  48. package/lib/team.json +10 -0
  49. package/lib/team.mjs +287 -26
  50. package/lib/threadBriefs.mjs +43 -31
  51. package/lib/threadRuns.mjs +47 -3
  52. package/npm-shrinkwrap.json +14 -14
  53. package/package.json +4 -4
  54. package/release.json +55 -43
@@ -0,0 +1,545 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import http from "node:http";
3
+ import { WebSocket } from "ws";
4
+
5
+ const digest = (value) =>
6
+ createHash("sha256").update(JSON.stringify(value)).digest("hex");
7
+ const day = () => {
8
+ const value = new Date();
9
+ value.setHours(0, 0, 0, 0);
10
+ return value.toISOString();
11
+ };
12
+ const key = (ref) => `comfy-execution:${ref}`;
13
+ const exact = (value, names) =>
14
+ value &&
15
+ typeof value === "object" &&
16
+ Object.keys(value).length === names.length &&
17
+ names.every((name) => Object.hasOwn(value, name));
18
+ const MAX_IMAGE = 5 * 1024 * 1024;
19
+ // A numeric loopback service is reached directly, independent of proxy settings.
20
+ const localRequest = (origin, route, options = {}) =>
21
+ new Promise((resolve, reject) => {
22
+ const outgoing = http.request(
23
+ origin + route,
24
+ {
25
+ method: options.method ?? "GET",
26
+ headers: options.headers,
27
+ agent: false,
28
+ },
29
+ (body) =>
30
+ resolve({
31
+ body,
32
+ status: body.statusCode,
33
+ ok: body.statusCode >= 200 && body.statusCode < 300,
34
+ })
35
+ );
36
+ outgoing.on("error", reject);
37
+ outgoing.end(options.body);
38
+ });
39
+
40
+ /** A registered local GPU operation, never a model-supplied URL or workflow.
41
+ * The worker owns its policy, submit intent and physical execution receipt.
42
+ */
43
+ export function createComfyExecution({
44
+ store,
45
+ configuration,
46
+ machine,
47
+ publish,
48
+ }) {
49
+ const active = new Map();
50
+ const observerRecovery = new Set();
51
+ let closing = false;
52
+ const reconciling = new Map();
53
+ const read = (ref) => JSON.parse(store.meta(key(ref)) || "null");
54
+ const save = (job) => store.meta(key(job.operation_ref), JSON.stringify(job));
55
+ const policy = () => {
56
+ const config = configuration();
57
+ if (
58
+ config?.enabled !== true ||
59
+ !Number.isInteger(config.dailyRenderLimit) ||
60
+ config.dailyRenderLimit < 1
61
+ )
62
+ throw new Error("ComfyUI execution is not enabled on this computer");
63
+ const origin = new URL(config.origin);
64
+ if (
65
+ origin.protocol !== "http:" ||
66
+ !["127.0.0.1", "[::1]"].includes(origin.hostname) ||
67
+ origin.username ||
68
+ origin.password ||
69
+ origin.pathname !== "/" ||
70
+ origin.search ||
71
+ origin.hash
72
+ )
73
+ throw new Error(
74
+ "ComfyUI must be registered at a numeric loopback origin"
75
+ );
76
+ return {
77
+ origin: origin.origin,
78
+ limit: config.dailyRenderLimit,
79
+ checkpoint: config.checkpoint ?? null,
80
+ revision: digest(config),
81
+ };
82
+ };
83
+ const request = async (route, { allowError = false, ...options } = {}) => {
84
+ const current = policy();
85
+ const response = await localRequest(current.origin, route, options);
86
+ if (!response.ok && !allowError)
87
+ throw Object.assign(
88
+ new Error(`ComfyUI returned HTTP ${response.status}`),
89
+ { http_status: response.status }
90
+ );
91
+ return response;
92
+ };
93
+ const text = async (response) => {
94
+ const chunks = [];
95
+ let size = 0;
96
+ for await (const chunk of response.body) {
97
+ size += chunk.length;
98
+ if (size > 1024 * 1024)
99
+ throw new Error("The ComfyUI control reply is too large");
100
+ chunks.push(chunk);
101
+ }
102
+ return Buffer.concat(chunks).toString("utf8");
103
+ };
104
+ const json = async (route) => JSON.parse(await text(await request(route)));
105
+ const cancelAtProvider = async (job) => {
106
+ let fresh = read(job.operation_ref);
107
+ if (!fresh.prompt_id) {
108
+ const response = await localRequest(job.origin, "/queue");
109
+ const queue = JSON.parse(await text(response));
110
+ const item = [
111
+ ...(queue.queue_pending ?? []),
112
+ ...(queue.queue_running ?? []),
113
+ ].find(
114
+ (entry) => entry?.[3]?.harness_operation_ref === job.operation_ref
115
+ );
116
+ if (!item) return fresh;
117
+ fresh = { ...read(job.operation_ref), prompt_id: item[1] };
118
+ save(fresh);
119
+ }
120
+ // The provider ignores running ids here; never interrupt another workflow.
121
+ const response = await localRequest(job.origin, "/queue", {
122
+ method: "POST",
123
+ headers: { "content-type": "application/json" },
124
+ body: JSON.stringify({ delete: [fresh.prompt_id] }),
125
+ });
126
+ await text(response);
127
+ if (!response.ok)
128
+ throw new Error("ComfyUI did not confirm queued cancellation");
129
+ return fresh;
130
+ };
131
+ const catalog = async () => {
132
+ try {
133
+ const current = policy();
134
+ const info = await json("/object_info/CheckpointLoaderSimple");
135
+ const models =
136
+ info.CheckpointLoaderSimple?.input?.required?.ckpt_name?.[0];
137
+ const checkpoint =
138
+ current.checkpoint ??
139
+ (Array.isArray(models) ? [...models].sort()[0] : null);
140
+ if (typeof checkpoint !== "string" || !models?.includes(checkpoint))
141
+ throw new Error("No registered checkpoint is available");
142
+ return {
143
+ ok: true,
144
+ capability_ref: "capability:comfy-image@1",
145
+ policy_ref: `comfy:${current.revision}`,
146
+ checkpoint,
147
+ computer: machine().id,
148
+ label: machine().label,
149
+ };
150
+ } catch (error) {
151
+ return { ok: false, error: error.message };
152
+ }
153
+ };
154
+ const result = (job) => {
155
+ const { image: _image, published_state: _published, ...receipt } = job;
156
+ return receipt;
157
+ };
158
+ const tell = async (job) => {
159
+ const answer = await publish(result(job)).catch(() => null);
160
+ const fresh = read(job.operation_ref);
161
+ if (answer?.ok && !answer.queued && fresh?.state === job.state)
162
+ save({ ...fresh, published_state: job.state });
163
+ };
164
+ const finish = async (job) => {
165
+ if (job.policy_ref !== `comfy:${policy().revision}` || job.cancelled)
166
+ throw new Error("The worker permission changed");
167
+ const history = await json(`/history/${encodeURIComponent(job.prompt_id)}`);
168
+ const entry = history[job.prompt_id];
169
+ if (!entry) return job;
170
+ if (entry.prompt?.[3]?.harness_operation_ref !== job.operation_ref)
171
+ throw new Error("ComfyUI history does not bind this operation");
172
+ if (entry.status?.status_str === "error") {
173
+ const failed = {
174
+ ...job,
175
+ state: "failed",
176
+ error: "ComfyUI execution failed",
177
+ };
178
+ save(failed);
179
+ active.get(job.operation_ref)?.close();
180
+ await tell(failed);
181
+ return failed;
182
+ }
183
+ if (!entry.status?.completed) return job;
184
+ const image = entry.outputs?.["9"]?.images?.[0];
185
+ if (
186
+ !image ||
187
+ image.type !== "output" ||
188
+ !/^[\w.-]+\.png$/.test(image.filename) ||
189
+ typeof image.subfolder !== "string" ||
190
+ image.subfolder.split(/[\\/]/).some((part) => part.startsWith("."))
191
+ )
192
+ throw new Error("The workflow returned an unsupported image reference");
193
+ const query = new URLSearchParams({
194
+ filename: image.filename,
195
+ subfolder: image.subfolder,
196
+ type: "output",
197
+ });
198
+ const response = await request(`/view?${query}`);
199
+ const chunks = [];
200
+ let size = 0;
201
+ for await (const chunk of response.body) {
202
+ size += chunk.length;
203
+ if (size > MAX_IMAGE) {
204
+ response.body.destroy();
205
+ throw new Error("Image exceeds the artifact limit");
206
+ }
207
+ chunks.push(chunk);
208
+ }
209
+ const bytes = Buffer.concat(chunks);
210
+ if (
211
+ !bytes
212
+ .subarray(0, 8)
213
+ .equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))
214
+ )
215
+ throw new Error("The returned artifact is not a PNG");
216
+ const next = {
217
+ ...job,
218
+ state: "done",
219
+ image: bytes.toString("base64"),
220
+ sha256: createHash("sha256").update(bytes).digest("hex"),
221
+ bytes: bytes.length,
222
+ mime: "image/png",
223
+ name: image.filename,
224
+ };
225
+ if (
226
+ read(job.operation_ref).cancelled ||
227
+ job.policy_ref !== `comfy:${policy().revision}`
228
+ )
229
+ throw new Error("Execution authority changed before delivery");
230
+ save(next);
231
+ active.get(job.operation_ref)?.close();
232
+ await tell(next);
233
+ return next;
234
+ };
235
+ const reconcileOnce = async (job) => {
236
+ job = read(job.operation_ref);
237
+ if (["done", "failed", "cancelled"].includes(job.state)) return job;
238
+ if (job.policy_ref !== `comfy:${policy().revision}`)
239
+ return {
240
+ ...job,
241
+ state: "unknown",
242
+ error: "The worker permission changed",
243
+ };
244
+ if (!job.prompt_id) {
245
+ const history = await json("/history");
246
+ const queue = await json("/queue");
247
+ const candidate = [
248
+ ...Object.values(history).map((x) => x.prompt),
249
+ ...(queue.queue_running ?? []),
250
+ ...(queue.queue_pending ?? []),
251
+ ].find((x) => x?.[3]?.harness_operation_ref === job.operation_ref);
252
+ if (!candidate) return job;
253
+ job = { ...job, prompt_id: candidate[1], state: "running" };
254
+ save(job);
255
+ }
256
+ return finish(job);
257
+ };
258
+ const reconcile = (job) => {
259
+ if (reconciling.has(job.operation_ref))
260
+ return reconciling.get(job.operation_ref);
261
+ const work = reconcileOnce(job).finally(() =>
262
+ reconciling.delete(job.operation_ref)
263
+ );
264
+ reconciling.set(job.operation_ref, work);
265
+ return work;
266
+ };
267
+ const watch = (job, socket) => {
268
+ socket.on("message", () => {
269
+ void reconcile(read(job.operation_ref)).catch(() => {});
270
+ });
271
+ socket.on("error", () => {});
272
+ socket.on("close", () => {
273
+ if (active.get(job.operation_ref) !== socket) return;
274
+ active.delete(job.operation_ref);
275
+ const fresh = read(job.operation_ref);
276
+ if (fresh?.state === "submitting") {
277
+ save({
278
+ ...fresh,
279
+ state: "unknown",
280
+ error: "The ComfyUI connection closed before submit was confirmed",
281
+ });
282
+ void tell(read(job.operation_ref));
283
+ }
284
+ if (
285
+ !closing &&
286
+ !["done", "failed", "cancelled"].includes(fresh?.state) &&
287
+ !observerRecovery.has(job.operation_ref)
288
+ ) {
289
+ // One event-driven observer replacement, with no new submit or deadline.
290
+ // A failed replacement leaves the original attempt for reconciliation.
291
+ observerRecovery.add(job.operation_ref);
292
+ try {
293
+ observe(read(job.operation_ref));
294
+ } catch {}
295
+ }
296
+ });
297
+ };
298
+ const observe = (job) => {
299
+ if (
300
+ closing ||
301
+ active.has(job.operation_ref) ||
302
+ ["done", "failed", "cancelled"].includes(job.state)
303
+ )
304
+ return;
305
+ if (job.policy_ref !== `comfy:${policy().revision}`) return;
306
+ const socket = new WebSocket(
307
+ job.origin.replace("http:", "ws:") +
308
+ `/ws?clientId=${encodeURIComponent(job.operation_ref)}`
309
+ );
310
+ active.set(job.operation_ref, socket);
311
+ watch(job, socket);
312
+ socket.once(
313
+ "open",
314
+ () => void reconcile(read(job.operation_ref)).catch(() => {})
315
+ );
316
+ };
317
+ const submit = async (call, from) => {
318
+ if (
319
+ !exact(call, [
320
+ "operation",
321
+ "operation_ref",
322
+ "prompt",
323
+ "policy_ref",
324
+ "computer",
325
+ "owner_epoch",
326
+ ]) ||
327
+ call.operation !== "submit" ||
328
+ !/^render:[a-f0-9-]{36}$/.test(call.operation_ref) ||
329
+ typeof call.prompt !== "string" ||
330
+ !call.prompt.trim() ||
331
+ call.prompt.length > 16000 ||
332
+ call.owner_epoch !== 1 ||
333
+ call.computer !== machine().id
334
+ )
335
+ return { ok: false, code: "execution_schema" };
336
+ const previous = read(call.operation_ref);
337
+ if (previous)
338
+ return previous.from === from &&
339
+ previous.prompt === call.prompt &&
340
+ previous.policy_ref === call.policy_ref
341
+ ? { ok: true, execution: result(await reconcile(previous)) }
342
+ : { ok: false, code: "execution_conflict" };
343
+ const offered = await catalog();
344
+ if (!offered.ok || offered.policy_ref !== call.policy_ref)
345
+ return { ok: false, code: "capability_changed" };
346
+ const current = policy();
347
+ const quotaKey = `comfy-quota:${day()}`;
348
+ const count = Number(store.meta(quotaKey) || 0);
349
+ if (count >= current.limit) return { ok: false, code: "render_budget" };
350
+ const job = {
351
+ ...call,
352
+ from,
353
+ state: "submitting",
354
+ computer_label: machine().label,
355
+ attempt_ref: `attempt:${randomUUID()}`,
356
+ checkpoint: offered.checkpoint,
357
+ origin: current.origin,
358
+ };
359
+ store.dispatchTransaction(() => {
360
+ if (
361
+ read(call.operation_ref) ||
362
+ Number(store.meta(quotaKey) || 0) !== count ||
363
+ policy().revision !== current.revision
364
+ )
365
+ throw new Error("Execution authority changed");
366
+ save(job);
367
+ store.meta(quotaKey, String(count + 1));
368
+ const refs = JSON.parse(store.meta("comfy-executions") || "[]");
369
+ store.meta(
370
+ "comfy-executions",
371
+ JSON.stringify([...refs, job.operation_ref])
372
+ );
373
+ });
374
+ // Start progress before submit, so completion cannot race past the observer.
375
+ const socket = new WebSocket(
376
+ current.origin.replace("http:", "ws:") +
377
+ `/ws?clientId=${encodeURIComponent(call.operation_ref)}`
378
+ );
379
+ active.set(job.operation_ref, socket);
380
+ watch(job, socket);
381
+ socket.once("open", () => {
382
+ void (async () => {
383
+ try {
384
+ if (policy().revision !== current.revision)
385
+ throw new Error("Execution authority changed");
386
+ if (read(job.operation_ref).cancelled) return;
387
+ const workflow = {
388
+ 1: {
389
+ class_type: "CheckpointLoaderSimple",
390
+ inputs: { ckpt_name: offered.checkpoint },
391
+ },
392
+ 2: {
393
+ class_type: "CLIPTextEncode",
394
+ inputs: { text: call.prompt, clip: ["1", 1] },
395
+ },
396
+ 3: {
397
+ class_type: "CLIPTextEncode",
398
+ inputs: { text: "", clip: ["1", 1] },
399
+ },
400
+ 4: {
401
+ class_type: "EmptyLatentImage",
402
+ inputs: { width: 512, height: 512, batch_size: 1 },
403
+ },
404
+ 5: {
405
+ class_type: "KSampler",
406
+ inputs: {
407
+ model: ["1", 0],
408
+ positive: ["2", 0],
409
+ negative: ["3", 0],
410
+ latent_image: ["4", 0],
411
+ seed: 0,
412
+ steps: 20,
413
+ cfg: 7,
414
+ sampler_name: "euler",
415
+ scheduler: "normal",
416
+ denoise: 1,
417
+ },
418
+ },
419
+ 8: {
420
+ class_type: "VAEDecode",
421
+ inputs: { samples: ["5", 0], vae: ["1", 2] },
422
+ },
423
+ 9: {
424
+ class_type: "SaveImage",
425
+ inputs: {
426
+ images: ["8", 0],
427
+ filename_prefix: `harness_${call.operation_ref.slice(7)}`,
428
+ },
429
+ },
430
+ };
431
+ const response = await request("/prompt", {
432
+ allowError: true,
433
+ method: "POST",
434
+ headers: { "content-type": "application/json" },
435
+ body: JSON.stringify({
436
+ client_id: call.operation_ref,
437
+ prompt: workflow,
438
+ extra_data: { harness_operation_ref: call.operation_ref },
439
+ }),
440
+ });
441
+ const raw = await text(response);
442
+ store.meta(
443
+ `comfy-response:${job.operation_ref}`,
444
+ JSON.stringify({ http_status: response.status, body: raw })
445
+ );
446
+ if (!response.ok)
447
+ throw Object.assign(
448
+ new Error(`ComfyUI returned HTTP ${response.status}`),
449
+ { http_status: response.status }
450
+ );
451
+ const reply = JSON.parse(raw);
452
+ if (
453
+ reply.error ||
454
+ (reply.node_errors &&
455
+ Object.keys(reply.node_errors).length &&
456
+ !reply.prompt_id)
457
+ ) {
458
+ const failed = {
459
+ ...read(job.operation_ref),
460
+ state: "failed",
461
+ error: "ComfyUI rejected the prepared workflow",
462
+ };
463
+ save(failed);
464
+ socket.close();
465
+ await tell(failed);
466
+ return;
467
+ }
468
+ if (typeof reply.prompt_id !== "string")
469
+ throw new Error("ComfyUI did not return an execution identity");
470
+ const fresh = read(job.operation_ref);
471
+ if (fresh.cancelled) {
472
+ save({ ...fresh, prompt_id: reply.prompt_id });
473
+ await cancelAtProvider(read(job.operation_ref));
474
+ return;
475
+ }
476
+ if (fresh.state !== "done")
477
+ save({ ...fresh, state: "running", prompt_id: reply.prompt_id });
478
+ await reconcile(read(job.operation_ref));
479
+ } catch (error) {
480
+ const fresh = read(job.operation_ref);
481
+ if (fresh.state === "cancelled") {
482
+ await cancelAtProvider(fresh).catch(() => {});
483
+ return;
484
+ }
485
+ if (fresh.state === "done") return;
486
+ const unknown = {
487
+ ...read(job.operation_ref),
488
+ state: error.http_status === 400 ? "failed" : "unknown",
489
+ error: error.message,
490
+ };
491
+ save(unknown);
492
+ await tell(unknown);
493
+ // Read receipts after an ambiguous submit; never POST it again.
494
+ if (unknown.state === "unknown")
495
+ await reconcile(unknown).catch(() => {});
496
+ }
497
+ })();
498
+ });
499
+ return { ok: true, execution: result(job) };
500
+ };
501
+ const answer = async (call, from) => {
502
+ try {
503
+ if (call?.operation === "catalog") return catalog();
504
+ if (call?.operation === "submit") return submit(call, from);
505
+ const job = read(call?.operation_ref);
506
+ if (!job || job.from !== from)
507
+ return { ok: false, code: "execution_missing" };
508
+ if (call.operation === "cancel") {
509
+ const next = { ...job, cancelled: true, state: "cancelled" };
510
+ save(next);
511
+ active.get(job.operation_ref)?.close();
512
+ // Delete only this operation's queued item. Never interrupt another running workflow.
513
+ return { ok: true, execution: result(await cancelAtProvider(next)) };
514
+ }
515
+ if (call.operation === "status")
516
+ return { ok: true, execution: result(await reconcile(job)) };
517
+ if (call.operation === "image" && job.state === "done")
518
+ return { ok: true, data: job.image, sha256: job.sha256 };
519
+ return { ok: false, code: "execution_operation" };
520
+ } catch (error) {
521
+ return { ok: false, code: "execution_unavailable", error: error.message };
522
+ }
523
+ };
524
+ return {
525
+ answer,
526
+ resume: async () => {
527
+ for (const ref of JSON.parse(store.meta("comfy-executions") || "[]")) {
528
+ const job = read(ref);
529
+ if (!job || ["cancelled", "failed"].includes(job.state)) continue;
530
+ try {
531
+ const next = await reconcile(job);
532
+ if (next.state === "done") {
533
+ if (next.published_state !== "done") await tell(next);
534
+ continue;
535
+ }
536
+ observe(next);
537
+ } catch {}
538
+ }
539
+ },
540
+ close: () => {
541
+ closing = true;
542
+ for (const socket of active.values()) socket.close();
543
+ },
544
+ };
545
+ }
package/lib/context.mjs CHANGED
@@ -23,6 +23,7 @@ import {
23
23
  } from "./learn.mjs";
24
24
  import { repositoryOf } from "./worktrees.mjs";
25
25
  import { createLibrary } from "./library.mjs";
26
+ import { mentions } from "./frames.mjs";
26
27
  import { oneLine } from "./oneLine.mjs";
27
28
 
28
29
  // The context engine. It keeps the atlas of the code the swarm works in, remembers what every coding session on this
@@ -231,7 +232,8 @@ export function createContextEngine({
231
232
 
232
233
  const roots = async () => {
233
234
  const places = [];
234
- for (const place of known()) places.push(await keptPlaceOf(place.path));
235
+ for (const place of await known())
236
+ places.push(await keptPlaceOf(place.path));
235
237
  return [...new Set(places.map((place) => place?.root).filter(Boolean))];
236
238
  };
237
239
 
@@ -257,7 +259,7 @@ export function createContextEngine({
257
259
  ).toISOString();
258
260
  const trees = new Map();
259
261
  let added = 0;
260
- for (const session of store.sessionSources({ since })) {
262
+ for (const session of await store.sessionSources({ since })) {
261
263
  // A session whose source file the agent has since deleted is read from the relay's own copy, once.
262
264
  let mtime = 0;
263
265
  try {
@@ -273,7 +275,7 @@ export function createContextEngine({
273
275
  ? await readTrail(session.agent, session.source)
274
276
  : trailFromTranscript(
275
277
  session.agent,
276
- store.sessionTranscript(session.agent, session.session_id),
278
+ await store.sessionTranscript(session.agent, session.session_id),
277
279
  {
278
280
  cwd: session.cwd,
279
281
  startedAt: Date.parse(session.started_at) || null,
@@ -344,9 +346,9 @@ export function createContextEngine({
344
346
  refreshing ||= (async () => {
345
347
  const power = inference.power;
346
348
  // A first pass over a repository waits for the charger; small catch-ups run on battery too unless it is low.
347
- if (power.paused()) return { skipped: "power" };
349
+ if (await power.paused()) return { skipped: "power" };
348
350
  const first = !atlas.roots().length;
349
- if (first && !power.onCharger())
351
+ if (first && !(await power.onCharger()))
350
352
  return { skipped: "waiting for the charger" };
351
353
  let files = 0;
352
354
  for (const root of await roots()) {
@@ -361,7 +363,7 @@ export function createContextEngine({
361
363
  await embedRuns("background");
362
364
  // The first pass over the person's folders waits for the charger too; after it, changes are read as they come.
363
365
  const read =
364
- !library.status().folderFiles && !power.onCharger()
366
+ !(await library.status()).folderFiles && !(await power.onCharger())
365
367
  ? { skipped: "waiting for the charger" }
366
368
  : await library.refresh({ lane: "background" });
367
369
  return { files, runs, pieces, library: read };
@@ -487,6 +489,20 @@ export function createContextEngine({
487
489
  };
488
490
  };
489
491
 
492
+ /**
493
+ * A folder's files as last indexed ([{ path, blob }]); [] outside a repository, or before its first index, which
494
+ * this starts. A task frame asks without waiting, and a run's pack has indexed its folder just before.
495
+ */
496
+ const scopeOf = (folder) => {
497
+ const place = folder && keptPlaces.get(folder)?.place;
498
+ const rows = place && scopes.get(place.top);
499
+ if (!rows && folder)
500
+ void keptPlaceOf(folder)
501
+ .then((found) => found && indexTree(found.top))
502
+ .catch((error) => log(`context engine: ${error.message}`));
503
+ return rows ?? [];
504
+ };
505
+
490
506
  /** Search for the agents' tool: files and earlier sessions in a repository, best first. */
491
507
  const search = async ({ query, folder, limit = 10 }) => {
492
508
  const result = await pack({ brief: query, folder, lane: "interactive" });
@@ -513,8 +529,8 @@ export function createContextEngine({
513
529
  if (
514
530
  !force &&
515
531
  (Date.now() - last < LEARN_EVERY_MS ||
516
- !inference.power.onCharger() ||
517
- inference.power.paused())
532
+ !(await inference.power.onCharger()) ||
533
+ (await inference.power.paused()))
518
534
  )
519
535
  return null;
520
536
  await refresh();
@@ -629,15 +645,13 @@ export function createContextEngine({
629
645
  * A folder's files as last indexed ([{ path, blob }]); [] outside a repository, or before its first index, which
630
646
  * this starts. A task frame asks without waiting, and a run's pack has indexed its folder just before.
631
647
  */
632
- scope: (folder) => {
633
- const place = folder && keptPlaces.get(folder)?.place;
634
- const rows = place && scopes.get(place.top);
635
- if (!rows && folder)
636
- void keptPlaceOf(folder)
637
- .then((found) => found && indexTree(found.top))
638
- .catch((error) => log(`context engine: ${error.message}`));
639
- return rows ?? [];
640
- },
648
+ scope: scopeOf,
649
+ /**
650
+ * Files and code named in the person's words, resolved in a folder's files as last indexed (frames.mjs mentions):
651
+ * { files, symbols }.
652
+ */
653
+ mentions: (text, folder) =>
654
+ mentions(text, { scope: scopeOf(folder), atlas }),
641
655
  pack,
642
656
  search,
643
657
  /** The person's files and the threads' files closest to a question (library.mjs search). */
@@ -654,10 +668,10 @@ export function createContextEngine({
654
668
  return null;
655
669
  }
656
670
  },
657
- status: () => ({
671
+ status: async () => ({
658
672
  roots: atlas.roots(),
659
673
  pieces: { waiting: atlas.missing(ATLAS_VERSION) },
660
- library: library.status(),
674
+ library: await library.status(),
661
675
  model: model
662
676
  ? {
663
677
  trainedAt: model.trainedAt,
@@ -665,7 +679,7 @@ export function createContextEngine({
665
679
  runs: model.runs,
666
680
  }
667
681
  : null,
668
- device: inference.device(),
682
+ device: await inference.device(),
669
683
  }),
670
684
  start() {
671
685
  const tick = () => {