@north-light/crouter 0.3.251 → 0.3.252

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 (48) hide show
  1. package/dist/builtin-memory/02-turn-lifecycle/00-ending-a-turn.md +1 -5
  2. package/dist/builtin-memory/05-kinds/review/security-findings.md +1 -1
  3. package/dist/clients/attach/__tests__/completion-frecency.test.d.ts +1 -0
  4. package/dist/clients/attach/__tests__/completion-frecency.test.js +35 -0
  5. package/dist/clients/attach/__tests__/ref-autocomplete.test.js +3 -1
  6. package/dist/clients/attach/__tests__/titled-editor-preview.test.js +2 -1
  7. package/dist/clients/attach/input/completion-frecency.d.ts +45 -0
  8. package/dist/clients/attach/input/completion-frecency.js +141 -0
  9. package/dist/clients/attach/input/controller.d.ts +17 -0
  10. package/dist/clients/attach/input/controller.js +40 -0
  11. package/dist/clients/attach/input/ref-autocomplete.d.ts +3 -1
  12. package/dist/clients/attach/input/ref-autocomplete.js +49 -8
  13. package/dist/clients/attach/input/titled-editor.d.ts +3 -0
  14. package/dist/clients/attach/input/titled-editor.js +5 -0
  15. package/dist/clients/attach/session/editor-inventory.d.ts +3 -0
  16. package/dist/clients/attach/session/editor-inventory.js +1 -1
  17. package/dist/clients/attach/session/input-wiring.d.ts +3 -0
  18. package/dist/clients/attach/session/input-wiring.js +2 -0
  19. package/dist/clients/attach/viewer.js +578 -578
  20. package/dist/core/__tests__/canvas-inbox-watcher-hold.test.js +4 -2
  21. package/dist/core/__tests__/canvas-inbox-watcher-naming.test.js +4 -1
  22. package/dist/core/__tests__/canvas-inbox-watcher.test.js +4 -1
  23. package/dist/core/__tests__/fixtures/fake-engine.d.ts +3 -1
  24. package/dist/core/__tests__/fixtures/fake-engine.js +7 -1
  25. package/dist/core/__tests__/integration/deferred-no-wake.test.js +4 -1
  26. package/dist/core/__tests__/migration.test.js +57 -11
  27. package/dist/core/__tests__/seam/broker-provider-retry.test.js +17 -2
  28. package/dist/core/__tests__/seam/dormancy-release.test.js +1 -0
  29. package/dist/core/__tests__/watchdog-abort-arms-retry.test.js +9 -2
  30. package/dist/core/canvas/migrations.js +63 -24
  31. package/dist/core/runtime/broker/engine-drive.js +7 -1
  32. package/dist/core/runtime/broker/fault-retry.js +31 -13
  33. package/dist/core/runtime/broker/held-deferred-inbox.d.ts +4 -0
  34. package/dist/core/runtime/broker/held-deferred-inbox.js +7 -2
  35. package/dist/core/runtime/broker.js +19 -0
  36. package/dist/core/runtime/close.js +2 -2
  37. package/dist/core/runtime/fault.js +8 -5
  38. package/dist/core/runtime/recycle.js +63 -41
  39. package/dist/core/runtime/reset.d.ts +1 -1
  40. package/dist/core/runtime/reset.js +25 -2
  41. package/dist/daemon/__tests__/helpers/source-daemon.js +1 -0
  42. package/dist/daemon/api/handlers/messages.js +1 -1
  43. package/dist/daemon/api/handlers/reports.js +1 -1
  44. package/dist/daemon/cron/sinks.js +1 -1
  45. package/dist/daemon/messaging/node-message.js +1 -1
  46. package/dist/pi-extensions/canvas-inbox-watcher.js +5 -2
  47. package/package.json +1 -1
  48. package/runtime.lock.json +2 -2
@@ -47,6 +47,7 @@ import { ToolGroupTracker } from './broker/tool-groups.js';
47
47
  import { onNodeNamed } from './broker/node-named.js';
48
48
  import { FaultRetry } from './broker/fault-retry.js';
49
49
  import { createTurnAdmissionGate } from './broker/turn-admission.js';
50
+ import { BROKER_IDLE_TURN_START } from './broker/held-deferred-inbox.js';
50
51
  import { EventProjection } from './broker/event-projection.js';
51
52
  import { createFrameDispatchContext, handleFrame, } from './broker/frame-dispatch.js';
52
53
  import { hydratePersistedAdvertisedCommandMessages, installAdvertisedCommandInvocationContract, } from './advertised-command-invocation.js';
@@ -327,6 +328,24 @@ export async function runBroker(nodeId, startupAt = process.hrtime.bigint(), sta
327
328
  registry.broadcast({ type: 'model_changed', model: isUnknownModel(liveSession.model) ? undefined : liveSession.model, spec });
328
329
  };
329
330
  const turnAdmission = createTurnAdmissionGate();
331
+ // The inbox watcher is Jiti-loaded and cannot import this native module's
332
+ // gate. Its idle delivery therefore calls this narrow process-global bridge:
333
+ // acquire admission first, then re-read the live session before prompt() so
334
+ // a viewer turn that won the race is joined rather than double-started.
335
+ const startIdleInboxTurn = (text) => {
336
+ void turnAdmission.admit((release) => {
337
+ const liveSession = rebind.session();
338
+ return liveSession
339
+ .prompt(text, {
340
+ preflightResult: release,
341
+ ...(liveSession.isStreaming ? { streamingBehavior: 'steer' } : {}),
342
+ })
343
+ .catch((error) => {
344
+ emitEvent({ level: 'error', event: 'broker.inbox.idle_prompt_failed', error });
345
+ });
346
+ });
347
+ };
348
+ globalThis[BROKER_IDLE_TURN_START] = startIdleInboxTurn;
330
349
  faultRetry = new FaultRetry({
331
350
  nodeId,
332
351
  cfg,
@@ -49,12 +49,12 @@ export function fanDoctrineWake(fromId, subscribers, label, data, tier = 'normal
49
49
  try {
50
50
  const notice = { from: fromId, tier, kind: 'message', label, data };
51
51
  if (sub.active) {
52
- appendInbox(sub.node_id, notice);
52
+ const entry = appendInbox(sub.node_id, notice);
53
53
  // A wake-capable notice consumes the subscriber's cancel-on-wake
54
54
  // deadline: the thing it was waiting for arrived. `deferred` is the
55
55
  // exception — it never wakes a node, riding the next natural cycle
56
56
  // instead, so the deadline it was racing must survive.
57
- if (tier !== 'deferred')
57
+ if (entry.tier !== 'deferred')
58
58
  cancelCronsOnWake(sub.node_id);
59
59
  }
60
60
  else
@@ -229,11 +229,14 @@ export function clearFault(nodeId, opts) {
229
229
  if (opts?.link !== undefined && current.link !== opts.link)
230
230
  return false;
231
231
  rmSync(faultPath(nodeId));
232
- if (opts?.preserveProviderRetryEpisode !== true) {
233
- const episode = currentProviderRetryEpisode(nodeId);
234
- if (episode?.fault.since === current.since)
235
- rmSync(providerRetryPath(nodeId), { force: true });
236
- }
232
+ const episode = currentProviderRetryEpisode(nodeId);
233
+ // turn_end precedes agent_settled. An admitted retry keeps its durable
234
+ // coordinate while the broker classifies an error settlement; an invalidated
235
+ // coordinate also remains durable so replacement startup cannot replay it.
236
+ if (opts?.preserveProviderRetryEpisode !== true &&
237
+ episode?.fault.since === current.since &&
238
+ episode.state === 'pending')
239
+ rmSync(providerRetryPath(nodeId), { force: true });
237
240
  const operationId = resolveOperationId();
238
241
  emitEvent({
239
242
  level: 'info',
@@ -5,8 +5,8 @@
5
5
  //
6
6
  // 1. Finalize — push the agent's last surfaced message as a `final` report so
7
7
  // every subscriber/manager waiting on it is unblocked, and mark it done.
8
- // 2. Close — tear the agent's broker engine down (the pane is only a viewer).
9
- // 3. Recycle — boot a fresh resident broker root the pane re-attaches to.
8
+ // 2. Recycle — boot a fresh resident broker root and prove it accepts viewers.
9
+ // 3. Close — tear the finalized broker down, then re-attach the pane to the replacement.
10
10
  //
11
11
  // NOT to be confused with `node lifecycle demote` (flip-to-terminal IN PLACE, which keeps
12
12
  // the agent focused and running): recycle ENDS this agent and boots a brand-new
@@ -17,7 +17,7 @@
17
17
  // message) — falling back to a short note when it never reported.
18
18
  import { readdirSync, readFileSync, statSync } from 'node:fs';
19
19
  import { join } from 'node:path';
20
- import { getNode, setPresence, setFocusOccupant, fullName } from '../canvas/index.js';
20
+ import { getNode, setPresence, setFocusOccupant, fullName, recordLaunch, recordPid } from '../canvas/index.js';
21
21
  import { reportsDir } from '../canvas/paths.js';
22
22
  import { pushFinal } from '../feed/feed.js';
23
23
  import { spawnNode, rootOfSpine } from './nodes.js';
@@ -25,6 +25,7 @@ import { buildLaunchSpecAsync, buildPiArgv } from './launch.js';
25
25
  import { focusOf, respawnPaneSync, setPaneOption } from './placement.js';
26
26
  import { waitForBrokerViewSocket, viewerSplitEnv } from './placement-tmux.js';
27
27
  import { headlessBrokerHost } from './host.js';
28
+ import { transition } from './lifecycle.js';
28
29
  import { ensureDaemon } from '../../daemon/manage.js';
29
30
  /** The agent's most recent surfaced message: the newest reports/*.md body with
30
31
  * its YAML frontmatter stripped. Empty string when the node never reported. */
@@ -74,23 +75,11 @@ export async function recycleNode(nodeId, callerPane) {
74
75
  finalized = true;
75
76
  }
76
77
  catch { /* recycle the pane even if the report failed */ }
77
- // The node's pane is a VIEWER (`crtr surface attach`), not its engine: the engine is
78
- // the detached broker process, so respawn-pane -k below would only kill the
79
- // viewer, never the engine. Tear the broker PROCESS down so it exits and
80
- // releases the sole .jsonl writer. Status is already flipped done by pushFinal
81
- // above (crash-safe order: the daemon won't revive a done node).
82
- try {
83
- headlessBrokerHost.teardown(nodeId);
84
- }
85
- catch { /* best-effort */ }
86
- // Capture M's focus viewport (if any) BEFORE nulling — the fresh root inherits
87
- // it (the SAME focus row + pane). The demoted node no longer holds a pane: it is
88
- // being reclaimed.
78
+ // Capture M's focus viewport (if any) before booting its replacement. The
79
+ // old broker remains live until the new broker proves it can accept a viewer;
80
+ // a failed replacement must not strand this finished conversation without an
81
+ // engine.
89
82
  const f = focusOf(nodeId);
90
- try {
91
- setPresence(nodeId, { pane: null, window: null, tmux_session: null });
92
- }
93
- catch { /* best-effort */ }
94
83
  // 2 + 3. Recycle — boot a fresh resident BROKER root for the SAME pane: the
95
84
  // viewer pane re-attaches to the fresh broker (broker-is-the-host — the viewer
96
85
  // pane stays a viewer, never becomes an engine pane).
@@ -113,36 +102,69 @@ export async function recycleNode(nodeId, callerPane) {
113
102
  profile_id: meta.profile_id,
114
103
  launch,
115
104
  });
116
- // Hand the viewport to the fresh root: reuse M's focus row over the SAME pane
117
- // (respawn-pane -k below keeps the %id), so the user keeps watching this slot.
105
+ const fresh = getNode(root.node_id);
106
+ const inv = buildPiArgv(fresh);
107
+ // CRTR_SUBTREE groups the fresh root's subtree; FRONT_DOOR is set by the broker
108
+ // host itself, so it is not added here.
109
+ inv.env = { ...inv.env, CRTR_SUBTREE: rootOfSpine(root.node_id) };
110
+ if (!(await launchRecycledBroker(fresh, inv))) {
111
+ return { recycled: false, finalized, newRoot: root.node_id, delivered };
112
+ }
113
+ // COMMIT after replacement readiness: the node's pane is a VIEWER, so tear
114
+ // down the old detached broker only now that the fresh broker can serve it.
115
+ try {
116
+ headlessBrokerHost.teardown(nodeId);
117
+ }
118
+ catch { /* best-effort */ }
119
+ try {
120
+ setPresence(nodeId, { pane: null, window: null, tmux_session: null });
121
+ }
122
+ catch { /* best-effort */ }
118
123
  if (f !== null) {
119
124
  try {
120
125
  setFocusOccupant(f.focus_id, root.node_id);
121
126
  }
122
127
  catch { /* best-effort */ }
123
128
  }
124
- const fresh = getNode(root.node_id);
125
- const inv = buildPiArgv(fresh);
126
- // CRTR_SUBTREE groups the fresh root's subtree; FRONT_DOOR is set by the broker
127
- // host itself, so it is not added here.
128
- inv.env = { ...inv.env, CRTR_SUBTREE: rootOfSpine(root.node_id) };
129
- const ok = await recycleBrokerViewer(fresh, pane, inv);
129
+ const ok = respawnRecycledViewer(fresh, pane);
130
130
  return { recycled: ok, finalized, newRoot: root.node_id, delivered };
131
131
  }
132
- /** Recycle a BROKER root into `pane`: the fresh root is broker-hosted, so its
133
- * engine runs in a DETACHED broker, not the pane. Birth-launch that broker via
134
- * the Host seam (mirrors spawnChild's birth path — the host records its pid),
135
- * wait for its view.sock to accept, then respawn the pane in place to the VIEWER
136
- * `crtr surface attach to <root>`. The pane stays a viewer (attach self-tags it
137
- * `@crtr_node`); it never hosts the engine. Returns false (recycle reports the
138
- * pane was not respawned) when the broker never serves — the fresh root row
139
- * still exists, broker-hosted, for the daemon to revive. */
140
- async function recycleBrokerViewer(fresh, pane, inv) {
141
- const placed = headlessBrokerHost.launch(fresh.node_id, inv, { cwd: fresh.cwd, name: fullName(fresh), resuming: false });
142
- if (placed.pid === null)
143
- return false;
144
- if (!(await waitForBrokerViewSocket(fresh.node_id, placed.exited)))
145
- return false;
132
+ /** Launch a recycled root and prove its viewer socket before recycling the old
133
+ * broker. This direct birth path writes the same launch/pid coordinates as
134
+ * reviveNode, and a failed readiness probe terminalizes and tears it down. */
135
+ async function launchRecycledBroker(fresh, inv) {
136
+ let placed;
137
+ try {
138
+ placed = headlessBrokerHost.launch(fresh.node_id, inv, { cwd: fresh.cwd, name: fullName(fresh), resuming: false });
139
+ if (placed.pid === null)
140
+ throw new Error('broker host returned no pid');
141
+ recordLaunch(fresh.node_id, new Date().toISOString());
142
+ recordPid(fresh.node_id, placed.pid);
143
+ if (await waitForBrokerViewSocket(fresh.node_id, placed.exited))
144
+ return true;
145
+ }
146
+ catch {
147
+ // The replacement never became usable; the finished root's broker remains
148
+ // intact and this half-born row cannot remain active without a fleet handle.
149
+ }
150
+ try {
151
+ transitionReplacementToDead(fresh.node_id);
152
+ }
153
+ catch { /* best-effort */ }
154
+ try {
155
+ headlessBrokerHost.teardown(fresh.node_id);
156
+ }
157
+ catch { /* best-effort */ }
158
+ return false;
159
+ }
160
+ function transitionReplacementToDead(nodeId) {
161
+ // spawnNode births active rows, so crash is the authoritative failed-launch
162
+ // outcome whether the host returned no pid or exited before readiness.
163
+ const replacement = getNode(nodeId);
164
+ if (replacement?.status === 'active' || replacement?.status === 'idle')
165
+ transition(nodeId, 'crash');
166
+ }
167
+ function respawnRecycledViewer(fresh, pane) {
146
168
  // Clear the finalized node's stale `@crtr_node` tag before respawn so the tag
147
169
  // never names a done node during the gap before the new `crtr surface attach` re-tags
148
170
  // on connect. Node ids are shell-safe identifiers; no quoting needed.
@@ -25,7 +25,7 @@ export interface RelaunchDeps {
25
25
  }>) => boolean | Promise<boolean>;
26
26
  /** Re-exec the viewer pane onto the new node. Default: respawnPaneSync. */
27
27
  respawnViewer?: typeof respawnPaneSync;
28
- /** Tear the old broker down. Default: headlessBrokerHost.teardown. */
28
+ /** Tear down a broker after a failed replacement or a committed old-root replacement. */
29
29
  teardownBroker?: typeof headlessBrokerHost.teardown;
30
30
  }
31
31
  export interface RelaunchRootResult {
@@ -20,7 +20,7 @@
20
20
  // reserved for finish).
21
21
  //
22
22
  // Best-effort throughout: a tmux/fs failure on one node never aborts the reap.
23
- import { getNode, updateNode, fullName, closeFocusRow, view, } from '../canvas/index.js';
23
+ import { getNode, updateNode, recordLaunch, recordPid, fullName, closeFocusRow, view, } from '../canvas/index.js';
24
24
  import { transition } from './lifecycle.js';
25
25
  import { headlessBrokerHost, requestBrokerTeardown } from './host.js';
26
26
  import { tearDownNode, focusOf, registerViewerFocus, respawnPaneSync, windowOfPane, renameWindow, } from './placement.js';
@@ -129,7 +129,30 @@ export async function relaunchRoot(oldId, deps = {}) {
129
129
  transition(newMeta.node_id, 'crash');
130
130
  return null;
131
131
  }
132
- await waitForViewSocket(newMeta.node_id, placed.exited); // best-effort; attach auto-redials on miss
132
+ // A replacement is a managed broker just like a revive: the fleet owns its
133
+ // real exit, while the row gets the matching launch and pid coordinates before
134
+ // readiness can observe an early exit.
135
+ recordLaunch(newMeta.node_id, new Date().toISOString());
136
+ recordPid(newMeta.node_id, placed.pid);
137
+ let ready = false;
138
+ try {
139
+ ready = await waitForViewSocket(newMeta.node_id, placed.exited);
140
+ }
141
+ catch {
142
+ // A readiness probe error means the replacement is not proven usable.
143
+ }
144
+ if (!ready) {
145
+ // Do not let a broker that can still bind later sit behind a dead row, and
146
+ // do not commit any old-root teardown without a usable replacement.
147
+ const replacement = getNode(newMeta.node_id);
148
+ if (replacement?.status === 'active' || replacement?.status === 'idle')
149
+ transition(newMeta.node_id, 'crash');
150
+ try {
151
+ teardownBroker(newMeta.node_id);
152
+ }
153
+ catch { /* best-effort */ }
154
+ return null;
155
+ }
133
156
  // --- COMMIT: park + reap the old root, re-point the viewer, kill the old
134
157
  // broker. Past this point the new node is the live root. ---
135
158
  reapDescendants(oldId); // old workers → canceled + torn down
@@ -57,6 +57,7 @@ export function startSourceDaemon(fixture) {
57
57
  };
58
58
  for (const key of [
59
59
  'CRTR_NODE_ID',
60
+ 'CRTR_SUBTREE',
60
61
  'CRTR_KIND',
61
62
  'CRTR_MODE',
62
63
  'CRTR_LIFECYCLE',
@@ -327,7 +327,7 @@ async function handleMessage(ctx) {
327
327
  const entry = deliver();
328
328
  // Deferred mail normally waits for a natural cycle, but a frozen row already
329
329
  // owes a thaw; consume its deadline when that durable future wake arrives.
330
- if (tier !== 'deferred' || meta.frozen_at !== null)
330
+ if (entry.tier !== 'deferred' || meta.frozen_at !== null)
331
331
  cancelCronsOnWake(id);
332
332
  // A wake-capable tier revives a dormant target so its inbox-watcher delivers
333
333
  // this; deferred never wakes — it rides the target's next natural cycle. The
@@ -33,7 +33,7 @@ function parsePushBody(body) {
33
33
  if (deliveryTier !== undefined && deliveryTier !== 'deferred' && deliveryTier !== 'normal' && deliveryTier !== 'urgent') {
34
34
  throw usage(`invalid delivery_tier: ${String(deliveryTier)} (expected deferred|normal|urgent)`);
35
35
  }
36
- if (tier === 'final' && deliveryTier !== undefined) {
36
+ if (tier !== 'update' && deliveryTier !== undefined) {
37
37
  throw usage('delivery_tier is only valid for update reports');
38
38
  }
39
39
  if (typeof text !== 'string' || text.trim() === '') {
@@ -31,7 +31,7 @@ function deliverNodeSink(c, target, stdout) {
31
31
  label: `⏰ cron ${c.name}`,
32
32
  data: { body: stdout },
33
33
  });
34
- if (c.tier !== 'deferred' || meta.frozen_at !== null)
34
+ if (entry.tier !== 'deferred' || meta.frozen_at !== null)
35
35
  cancelCronsOnWake(target);
36
36
  // The APPENDED tier decides the wake, not the cron's requested one: a deferred
37
37
  // cron aimed at a terminal node was raised out of `deferred` by the append.
@@ -59,7 +59,7 @@ export async function deliverNodeMessage(args) {
59
59
  label: args.label,
60
60
  data: { ...args.data, body: args.body },
61
61
  }));
62
- if (args.mode !== 'quiet' || target.frozen_at !== null)
62
+ if (entry.tier !== 'deferred' || target.frozen_at !== null)
63
63
  cancelCronsOnWake(args.node_id);
64
64
  // The APPENDED entry's tier decides the wake: a quiet send to a terminal
65
65
  // target was raised out of `deferred` by the append, and must be delivered.
@@ -23,7 +23,7 @@ import { errorClassFromError } from '../core/events/errors.js';
23
23
  import { operationIdContext } from '../core/events/operation-id.js';
24
24
  import { situationalContextEnvelope, SITUATIONAL_CONTEXT_CUSTOM_TYPE } from '../core/runtime/situational-context.js';
25
25
  import { isProfilePaused } from '../core/profiles/manifest.js';
26
- import { HELD_DEFERRED_INBOX_PROMPT_JOIN, } from '../core/runtime/broker/held-deferred-inbox.js';
26
+ import { brokerIdleTurnStart, HELD_DEFERRED_INBOX_PROMPT_JOIN, } from '../core/runtime/broker/held-deferred-inbox.js';
27
27
  const MAX_PENDING_HANDOFFS = 64;
28
28
  const MAX_SOURCE_OPERATION_IDS = 16;
29
29
  let currentWatcher;
@@ -738,7 +738,10 @@ class CanvasInboxWatcher {
738
738
  this.pi.sendMessage({ customType: SITUATIONAL_CONTEXT_CUSTOM_TYPE, content: card, display: true }, route === 'idle' ? {} : { deliverAs: route });
739
739
  }
740
740
  if (route === 'idle') {
741
- this.pi.sendUserMessage(content);
741
+ const start = brokerIdleTurnStart();
742
+ if (start === undefined)
743
+ throw new Error('broker idle-turn admission is unavailable');
744
+ start(content);
742
745
  }
743
746
  else {
744
747
  this.pi.sendUserMessage(content, { deliverAs: route });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/crouter",
3
- "version": "0.3.251",
3
+ "version": "0.3.252",
4
4
  "description": "crtr — agent runtime with memory, plugins, and marketplaces",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/runtime.lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@north-light/crouter",
3
- "version": "0.3.251",
3
+ "version": "0.3.252",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@north-light/crouter",
9
- "version": "0.3.251",
9
+ "version": "0.3.252",
10
10
  "hasInstallScript": true,
11
11
  "license": "MIT",
12
12
  "dependencies": {