@animalabs/agent-framework 0.15.0 → 0.16.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.
Files changed (96) hide show
  1. package/README.md +22 -1
  2. package/dist/src/agent.d.ts +32 -0
  3. package/dist/src/agent.d.ts.map +1 -1
  4. package/dist/src/agent.js +55 -0
  5. package/dist/src/agent.js.map +1 -1
  6. package/dist/src/api/server.d.ts +9 -0
  7. package/dist/src/api/server.d.ts.map +1 -1
  8. package/dist/src/api/server.js +156 -0
  9. package/dist/src/api/server.js.map +1 -1
  10. package/dist/src/api/types.d.ts +15 -0
  11. package/dist/src/api/types.d.ts.map +1 -1
  12. package/dist/src/framework.d.ts +418 -3
  13. package/dist/src/framework.d.ts.map +1 -1
  14. package/dist/src/framework.js +1782 -68
  15. package/dist/src/framework.js.map +1 -1
  16. package/dist/src/gate/event-gate.d.ts +16 -0
  17. package/dist/src/gate/event-gate.d.ts.map +1 -1
  18. package/dist/src/gate/event-gate.js +41 -3
  19. package/dist/src/gate/event-gate.js.map +1 -1
  20. package/dist/src/index.d.ts +4 -1
  21. package/dist/src/index.d.ts.map +1 -1
  22. package/dist/src/index.js +3 -1
  23. package/dist/src/index.js.map +1 -1
  24. package/dist/src/mcpl/channel-registry.d.ts +172 -0
  25. package/dist/src/mcpl/channel-registry.d.ts.map +1 -1
  26. package/dist/src/mcpl/channel-registry.js +444 -2
  27. package/dist/src/mcpl/channel-registry.js.map +1 -1
  28. package/dist/src/mcpl/server-connection.d.ts +9 -0
  29. package/dist/src/mcpl/server-connection.d.ts.map +1 -1
  30. package/dist/src/mcpl/server-connection.js +71 -13
  31. package/dist/src/mcpl/server-connection.js.map +1 -1
  32. package/dist/src/modules/history/index.d.ts +89 -13
  33. package/dist/src/modules/history/index.d.ts.map +1 -1
  34. package/dist/src/modules/history/index.js +655 -24
  35. package/dist/src/modules/history/index.js.map +1 -1
  36. package/dist/src/operator-log.d.ts +78 -0
  37. package/dist/src/operator-log.d.ts.map +1 -0
  38. package/dist/src/operator-log.js +102 -0
  39. package/dist/src/operator-log.js.map +1 -0
  40. package/dist/src/recovery/discord-awareness-outbox.d.ts +10 -0
  41. package/dist/src/recovery/discord-awareness-outbox.d.ts.map +1 -1
  42. package/dist/src/recovery/discord-awareness-outbox.js +20 -0
  43. package/dist/src/recovery/discord-awareness-outbox.js.map +1 -1
  44. package/dist/src/types/framework.d.ts +24 -0
  45. package/dist/src/types/framework.d.ts.map +1 -1
  46. package/dist/src/types/trace.d.ts +36 -0
  47. package/dist/src/types/trace.d.ts.map +1 -1
  48. package/dist/test/agent-runtime-settings.test.js +199 -1
  49. package/dist/test/agent-runtime-settings.test.js.map +1 -1
  50. package/dist/test/api-admin-gate.test.d.ts +2 -0
  51. package/dist/test/api-admin-gate.test.d.ts.map +1 -0
  52. package/dist/test/api-admin-gate.test.js +116 -0
  53. package/dist/test/api-admin-gate.test.js.map +1 -0
  54. package/dist/test/channel-label-history.test.d.ts +2 -0
  55. package/dist/test/channel-label-history.test.d.ts.map +1 -0
  56. package/dist/test/channel-label-history.test.js +424 -0
  57. package/dist/test/channel-label-history.test.js.map +1 -0
  58. package/dist/test/discord-awareness-outbox.test.js +16 -0
  59. package/dist/test/discord-awareness-outbox.test.js.map +1 -1
  60. package/dist/test/event-gate.test.js +44 -0
  61. package/dist/test/event-gate.test.js.map +1 -1
  62. package/dist/test/helpers/quiesce-crash-child.d.ts +2 -0
  63. package/dist/test/helpers/quiesce-crash-child.d.ts.map +1 -0
  64. package/dist/test/helpers/quiesce-crash-child.js +156 -0
  65. package/dist/test/helpers/quiesce-crash-child.js.map +1 -0
  66. package/dist/test/history-module-overview.test.d.ts +2 -0
  67. package/dist/test/history-module-overview.test.d.ts.map +1 -0
  68. package/dist/test/history-module-overview.test.js +711 -0
  69. package/dist/test/history-module-overview.test.js.map +1 -0
  70. package/dist/test/history-module.test.js +146 -0
  71. package/dist/test/history-module.test.js.map +1 -1
  72. package/dist/test/host-quiesce.test.d.ts +2 -0
  73. package/dist/test/host-quiesce.test.d.ts.map +1 -0
  74. package/dist/test/host-quiesce.test.js +863 -0
  75. package/dist/test/host-quiesce.test.js.map +1 -0
  76. package/dist/test/operator-surgery.test.d.ts +2 -0
  77. package/dist/test/operator-surgery.test.d.ts.map +1 -0
  78. package/dist/test/operator-surgery.test.js +398 -0
  79. package/dist/test/operator-surgery.test.js.map +1 -0
  80. package/dist/test/puppet-tool-call.test.js +5 -0
  81. package/dist/test/puppet-tool-call.test.js.map +1 -1
  82. package/dist/tsconfig.tsbuildinfo +1 -1
  83. package/package.json +2 -2
  84. package/src/agent.ts +69 -0
  85. package/src/api/server.ts +159 -2
  86. package/src/api/types.ts +15 -0
  87. package/src/framework.ts +2081 -89
  88. package/src/gate/event-gate.ts +47 -4
  89. package/src/index.ts +5 -1
  90. package/src/mcpl/channel-registry.ts +495 -2
  91. package/src/mcpl/server-connection.ts +73 -13
  92. package/src/modules/history/index.ts +689 -25
  93. package/src/operator-log.ts +132 -0
  94. package/src/recovery/discord-awareness-outbox.ts +19 -0
  95. package/src/types/framework.ts +27 -0
  96. package/src/types/trace.ts +42 -0
@@ -1,7 +1,7 @@
1
- import { join } from 'node:path';
1
+ import { dirname, join } from 'node:path';
2
2
  import { INLINE_WITHHELD_TEXT, classifyBlock, isInlineContradiction, referenceRegistry, referenceStubOrNull } from './mcpl/references.js';
3
3
  import { ReferenceFetcher, DEFAULT_FETCH_MAX_BYTES, EAGER_FETCH_TIMEOUT_MS } from './mcpl/reference-fetcher.js';
4
- import { appendFileSync, mkdirSync } from 'node:fs';
4
+ import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
5
5
  import { JsStore } from '@animalabs/chronicle';
6
6
  import { MembraneError } from '@animalabs/membrane';
7
7
  import { ContextManager, PassthroughStrategy, WindowedPassthroughStrategy } from '@animalabs/context-manager';
@@ -284,7 +284,23 @@ import { EventGate, formatShadowWarning } from './gate/event-gate.js';
284
284
  import { UsageTracker } from './usage/usage-tracker.js';
285
285
  import { formatZonedDateTime, resolveTimeZone } from './timezone.js';
286
286
  import { DEFAULT_DISCORD_AWARENESS_EMOJI, DiscordAwarenessOutbox, defaultDiscordAwarenessOutboxPath, extractDiscordAwarenessRefs, } from './recovery/discord-awareness-outbox.js';
287
+ import { OperatorActionError, OperatorLog, capIds, defaultOperatorLogPath, } from './operator-log.js';
287
288
  const FRAMEWORK_STATE_ID = 'framework/state';
289
+ /** Snapshot slot mirroring context writes deferred while quiesced (#122). */
290
+ const DEFERRED_WRITES_ID = 'framework/deferred-writes';
291
+ /** Above this serialized size the deferred-write mirror is skipped (kept in
292
+ * memory only) rather than rewriting a multi-MB snapshot per deferral. */
293
+ const DEFERRED_WRITES_PERSIST_CAP_BYTES = 4 * 1024 * 1024;
294
+ const bySeq = (a, b) => a.seq - b.seq;
295
+ /** Stamp a deferred write's durable id into the metadata it is stored with
296
+ * (boot recovery dedups replays by it). Idempotent for an already-stamped
297
+ * message. */
298
+ function withDeferredWriteId(metadata, id) {
299
+ return { ...(metadata ?? {}), deferredWriteId: id };
300
+ }
301
+ function isTurnContinuation(reason) {
302
+ return reason === 'context_budget_restart' || reason === 'tool_results_ready';
303
+ }
288
304
  const CONVERSATION_ROUTER_STATE_ID = 'framework/conversation-router';
289
305
  const INFERENCE_LOG_ID = 'framework/inference-log';
290
306
  const PROCESS_LOG_ID = 'framework/process-log';
@@ -308,6 +324,37 @@ class DiscordAwarenessAccountingError extends Error {
308
324
  this.name = 'DiscordAwarenessAccountingError';
309
325
  }
310
326
  }
327
+ /**
328
+ * Thrown by `updateAgentRuntimeSettings` when an immediate budget change would
329
+ * put the agent in an un-compilable layout: the folded floor renders more
330
+ * tokens than the hard budget admits, so every subsequent compile would
331
+ * OverBudget-wedge the agent (2026-08-21: mythos, 550k→260k set live against a
332
+ * ~450k floor — hours of hard-down). Overridable with `{ allowInfeasible: true }`.
333
+ * Paced descents never throw this: a non-immediate decrease leaves the compile
334
+ * budget untouched and converges only as far as the floor allows.
335
+ */
336
+ export class BudgetPreflightError extends Error {
337
+ preview;
338
+ constructor(message, preview) {
339
+ super(message);
340
+ this.preview = preview;
341
+ this.name = 'BudgetPreflightError';
342
+ }
343
+ }
344
+ /**
345
+ * Thrown by `resume()` when the current runtime settings do not compile for
346
+ * one or more agents — returning to service would OverBudget-wedge them on
347
+ * the first wake. Drain quarantine / advance merges to lower the floor, or
348
+ * pass `{ force: true }` after deciding the verdicts are acceptable.
349
+ */
350
+ export class ResumeBlockedError extends Error {
351
+ verdicts;
352
+ constructor(message, verdicts) {
353
+ super(message);
354
+ this.verdicts = verdicts;
355
+ this.name = 'ResumeBlockedError';
356
+ }
357
+ }
311
358
  /**
312
359
  * Default inference policy - infer if any request exists for the agent.
313
360
  */
@@ -316,6 +363,64 @@ class DefaultInferencePolicy {
316
363
  return requests.some((r) => r.agentName === agentName);
317
364
  }
318
365
  }
366
+ const SURGERY_SCAN_WINDOW = 500;
367
+ /**
368
+ * Slot index of `messageId` on the active branch, or -1. Scans from the tail
369
+ * in blob-free windows: operator targets are almost always recent, and a
370
+ * full `getAllMessages()` re-inflates every attachment on the branch.
371
+ */
372
+ function locateMessageIndex(cm, messageId) {
373
+ const hit = locateMessageIndices(cm, new Set([messageId])).get(messageId);
374
+ return hit ? hit.index : -1;
375
+ }
376
+ /** Tail-first scan resolving many ids at once; stops as soon as all are found. */
377
+ function locateMessageIndices(cm, ids) {
378
+ const found = new Map();
379
+ const total = cm.getMessageCount();
380
+ for (let end = total; end > 0 && found.size < ids.size; end -= SURGERY_SCAN_WINDOW) {
381
+ const start = Math.max(0, end - SURGERY_SCAN_WINDOW);
382
+ const { messages, startIndex } = cm.getMessageWindow(start, end - start, { resolveBlobs: false });
383
+ for (let i = messages.length - 1; i >= 0; i--) {
384
+ const id = String(messages[i].id);
385
+ if (ids.has(id) && !found.has(id)) {
386
+ found.set(id, {
387
+ index: startIndex + i,
388
+ ...(messages[i].bodyGroupId ? { bodyGroupId: messages[i].bodyGroupId } : {}),
389
+ });
390
+ }
391
+ }
392
+ }
393
+ return found;
394
+ }
395
+ /**
396
+ * Inclusive slot range of the body-group run containing `index` (the slot
397
+ * itself when it is not sharded). Delegates to the store's own
398
+ * `alignToBodyGroups` edge walk — one contiguity assumption, not two.
399
+ */
400
+ function bodyGroupRun(cm, index) {
401
+ const win = cm.getMessageWindow(index, 1, { alignToBodyGroups: true, resolveBlobs: false });
402
+ const from = win.startIndex;
403
+ const to = win.startIndex + win.messages.length - 1;
404
+ return {
405
+ from,
406
+ to,
407
+ fromId: String(win.messages[0].id),
408
+ toId: String(win.messages[win.messages.length - 1].id),
409
+ group: win.messages[0].bodyGroupId !== undefined,
410
+ };
411
+ }
412
+ function describeRequester(r) {
413
+ if (!r)
414
+ return 'unknown';
415
+ return `${r.name ?? r.id ?? 'unknown'} (${r.via})`;
416
+ }
417
+ function hostCommandRequester(serverId, params) {
418
+ return {
419
+ via: `host-command:${serverId}`,
420
+ ...(params.requesterName ? { name: params.requesterName } : {}),
421
+ ...(params.requesterId ? { id: params.requesterId } : {}),
422
+ };
423
+ }
319
424
  function isPermanentDiscordReactionFailure(message) {
320
425
  return /unknown message|unknown channel|missing access|missing permissions|missing permission|cannot access|channel .* not found|message .* not found/i
321
426
  .test(message);
@@ -421,6 +526,17 @@ export class AgentFramework {
421
526
  activeTriggerChannels = new Map();
422
527
  running = false;
423
528
  loopPromise = null;
529
+ /** Quiesce/maintenance mode (issue #122). DELIBERATELY separate from
530
+ * `running`, which is overloaded as the runLoop condition, the maintenance
531
+ * admission guard, AND the per-tick loop condition — expressing "paused" by
532
+ * clearing `running` would kill the very maintenance machinery quiesce
533
+ * exists to keep hot, plus store sync and tool-result processing. While
534
+ * quiesced: runLoop, syncTimer, maintenanceTimer, and the watchdog all keep
535
+ * running; only new turns (wake gate in processInferenceRequests) and MCPL
536
+ * data planes are held. */
537
+ quiesced = false;
538
+ quiesceReason;
539
+ quiescedAt;
424
540
  traceListeners = [];
425
541
  syncIntervalMs;
426
542
  syncTimer = null;
@@ -591,6 +707,36 @@ export class AgentFramework {
591
707
  subconsciousConfig = null;
592
708
  /** Tune-out coordinator (issue #77); non-null iff subconscious + channels. */
593
709
  tuneOutCoordinator = null;
710
+ /** Agents an abandon could not cancel (token held, no stream) — surfaced
711
+ * in HostModeStatus until the next quiesce/resume. */
712
+ lastUnabandonable = [];
713
+ /** True while the deferred-write queue has a persisted mirror (quiesced). */
714
+ deferredWritesPersisted = false;
715
+ deferredWritesCapWarned = false;
716
+ /** Branch-independent recovery files (see FrameworkConfig.hostModePath /
717
+ * deferredWritesPath). Undefined → branch-local slot fallback. */
718
+ hostModePath;
719
+ deferredWritesPath;
720
+ recoveryFallbackWarned = false;
721
+ /**
722
+ * Deferred writes that have been handed to a context manager but whose
723
+ * chronicle state is NOT yet durably synced. They stay in the durable
724
+ * recovery queue until `ackDeferredWrites()` syncs the store — chronicle
725
+ * persists the slot-chain head only on sync(), so acknowledging on append
726
+ * alone would let a hard exit forget an accepted message.
727
+ */
728
+ unackedDeferredWrites = [];
729
+ /** Monotonic order stamp for deferred writes: the durable queue is always
730
+ * written, restored, drained and flushed in `seq` order, whatever the
731
+ * pending/un-acked split — a re-deferred entry keeps its place. */
732
+ deferredSeq = 0;
733
+ /**
734
+ * Per target agent: the store's message count when the OLDEST currently
735
+ * un-acked entry for it was handed off. Persisted with the queue so a boot
736
+ * after an interrupted flush knows exactly which slot range the batch
737
+ * could occupy and dedups against all of it — never a fixed tail.
738
+ */
739
+ deferredScanFrom = new Map();
594
740
  deferredMessages = [];
595
741
  // Turn-alive markers (2026-07-31 Mythos phantom-skip incident): agentName →
596
742
  // token of the turn currently in progress. Set at startAgentStream ENTRY —
@@ -702,6 +848,16 @@ export class AgentFramework {
702
848
  discordAwarenessOutbox = null;
703
849
  discordAwarenessEmoji = DEFAULT_DISCORD_AWARENESS_EMOJI;
704
850
  discordAwarenessDeadlineMs = DEFAULT_DISCORD_AWARENESS_DEADLINE_MS;
851
+ /** Durable JSONL record of operator-initiated mutations (see operator-log.ts). */
852
+ operatorLog;
853
+ /**
854
+ * Store-wide admission barrier while a live surgery (rollback/suppress)
855
+ * holds the store. Per-agent turn tokens cover the agents registered when
856
+ * the reservation was taken; this flag covers everyone else — an ephemeral
857
+ * agent admitted mid-switch, a wake for an agent created later — at the
858
+ * scheduler, ephemeral admission and puppet entry points.
859
+ */
860
+ surgeryHold = null;
705
861
  /** Serialize per-server drains so reconnect and an online undo cannot race. */
706
862
  discordAwarenessDrains = new Map();
707
863
  /** Framework-global inference gate; older generations cannot release it. */
@@ -713,7 +869,8 @@ export class AgentFramework {
713
869
  usageTracker;
714
870
  /** Presentation-only wall-clock zone; persistence remains UTC/epoch. */
715
871
  timeZone;
716
- constructor(store, ownsStore, membrane, inferencePolicy, errorPolicy, syncIntervalMs, maintenanceIntervalMs, processLoggingPersist, processLoggingBroadcast, timeZone, discordAwarenessOutbox, discordAwarenessEmoji, discordAwarenessDeadlineMs) {
872
+ constructor(store, ownsStore, membrane, inferencePolicy, errorPolicy, syncIntervalMs, maintenanceIntervalMs, processLoggingPersist, processLoggingBroadcast, timeZone, discordAwarenessOutbox, discordAwarenessEmoji, discordAwarenessDeadlineMs, operatorLog = new OperatorLog(undefined)) {
873
+ this.operatorLog = operatorLog;
717
874
  this.store = store;
718
875
  this.ownsStore = ownsStore;
719
876
  this.membrane = membrane;
@@ -774,6 +931,12 @@ export class AgentFramework {
774
931
  catch {
775
932
  // Already registered
776
933
  }
934
+ try {
935
+ store.registerState({ id: DEFERRED_WRITES_ID, strategy: 'snapshot' });
936
+ }
937
+ catch {
938
+ // Already registered
939
+ }
777
940
  try {
778
941
  store.registerState({
779
942
  id: INFERENCE_LOG_ID,
@@ -816,7 +979,16 @@ export class AgentFramework {
816
979
  const discordAwarenessOutbox = discordAwarenessOutboxPath
817
980
  ? new DiscordAwarenessOutbox(discordAwarenessOutboxPath)
818
981
  : null;
819
- const framework = new AgentFramework(store, ownsStore, config.membrane, config.inferencePolicy ?? new DefaultInferencePolicy(), config.errorPolicy ?? new DefaultErrorPolicy(), config.syncIntervalMs ?? DEFAULT_SYNC_INTERVAL_MS, config.maintenanceIntervalMs ?? DEFAULT_MAINTENANCE_INTERVAL_MS, processLoggingPersist, processLoggingBroadcast, resolveTimeZone(config.timeZone), discordAwarenessOutbox, config.discordAwarenessEmoji ?? DEFAULT_DISCORD_AWARENESS_EMOJI, normalizeDiscordAwarenessDeadline(config.discordAwarenessDeadlineMs));
982
+ const operatorLogPath = config.operatorLogPath === false
983
+ ? undefined
984
+ : config.operatorLogPath
985
+ ?? (config.storePath ? defaultOperatorLogPath(config.storePath) : undefined);
986
+ if (operatorLogPath === undefined && config.operatorLogPath !== false) {
987
+ // An audit log that is silently off is worse than none: say so once.
988
+ console.error('[operator-log] disabled: no storePath and no operatorLogPath configured — ' +
989
+ 'operator actions will only be visible as operator:action traces');
990
+ }
991
+ const framework = new AgentFramework(store, ownsStore, config.membrane, config.inferencePolicy ?? new DefaultInferencePolicy(), config.errorPolicy ?? new DefaultErrorPolicy(), config.syncIntervalMs ?? DEFAULT_SYNC_INTERVAL_MS, config.maintenanceIntervalMs ?? DEFAULT_MAINTENANCE_INTERVAL_MS, processLoggingPersist, processLoggingBroadcast, resolveTimeZone(config.timeZone), discordAwarenessOutbox, config.discordAwarenessEmoji ?? DEFAULT_DISCORD_AWARENESS_EMOJI, normalizeDiscordAwarenessDeadline(config.discordAwarenessDeadlineMs), new OperatorLog(operatorLogPath));
820
992
  // If an offline recovery process crashed after switching Chronicle but
821
993
  // before committing its prepared marker batch, the active branch is the
822
994
  // commit record. Promote it now; no quarantined content is read.
@@ -936,6 +1108,56 @@ export class AgentFramework {
936
1108
  for (const [agentName, cap] of Object.entries(framework.readPersistedToolResultInlineCaps())) {
937
1109
  framework.toolResultInlineMaxCharsOverride.set(agentName, cap);
938
1110
  }
1111
+ // Restore persisted quiesce mode (issue #122) BEFORE initializeMcpl: the
1112
+ // flag must be set before any data-plane barrier completion can run, or
1113
+ // the startup funnel would open the data planes on a host that shut down
1114
+ // mid-maintenance. Staged connections boot with both planes closed, so a
1115
+ // quiesced boot needs no re-pause — completeMcplDataPlaneGate consults
1116
+ // the flag and holds data planes (control planes come up normally). The
1117
+ // gate already exists at this point, so the suppression is wired here too.
1118
+ //
1119
+ // Both records live OUTSIDE branch history (recovery/ files next to the
1120
+ // store): a historical rollback must not be able to erase the marker of
1121
+ // the very surgery it belongs to, nor orphan the writes deferred by it.
1122
+ framework.hostModePath = config.hostModePath
1123
+ ?? (config.storePath ? join(config.storePath, 'recovery', 'host-mode.json') : undefined);
1124
+ framework.deferredWritesPath = config.deferredWritesPath
1125
+ ?? (config.storePath ? join(config.storePath, 'recovery', 'deferred-writes.json') : undefined);
1126
+ {
1127
+ const hostMode = framework.readHostMode();
1128
+ // Deferred writes are recovered REGARDLESS of the mode flag: a crash
1129
+ // between a resume's flag clear and the end of its flush must not
1130
+ // strand accepted messages. Per-message acks mean only the remainder
1131
+ // replays. Serving boot with a remainder → flush it right now (no turn
1132
+ // is alive yet, so this is a safe boundary).
1133
+ const restoredWrites = framework.restorePersistedDeferredWrites();
1134
+ if (!hostMode?.quiesced && restoredWrites > 0) {
1135
+ console.error(`[host-mode] ${restoredWrites} deferred context write(s) found at a serving boot ` +
1136
+ `(an earlier resume did not finish its flush) — landing them now`);
1137
+ await framework.flushDeferredWrites('boot-recovery');
1138
+ }
1139
+ if (hostMode?.quiesced) {
1140
+ framework.quiesced = true;
1141
+ framework.quiesceReason = hostMode.reason;
1142
+ framework.quiescedAt = hostMode.since;
1143
+ framework.eventGate?.setQuiesced(true);
1144
+ console.error(`[host-mode] ============================================================\n` +
1145
+ `[host-mode] BOOTING QUIESCED (persisted${hostMode.reason ? `: ${hostMode.reason}` : ''}, ` +
1146
+ `since ${hostMode.since ? new Date(hostMode.since).toISOString() : 'unknown'}).\n` +
1147
+ `[host-mode] No wakes will start turns and MCPL data planes stay paused\n` +
1148
+ `[host-mode] until resume() — via host/command, the API server, or the\n` +
1149
+ `[host-mode] framework API. Wakes parked before the restart were in-memory\n` +
1150
+ `[host-mode] and are gone; recurring sources re-deliver on their own cadence.\n` +
1151
+ `[host-mode] ${restoredWrites} deferred context write(s) restored from the store;\n` +
1152
+ `[host-mode] they land at resume().\n` +
1153
+ `[host-mode] ============================================================`);
1154
+ framework.emitTrace({
1155
+ type: 'host:quiesced_boot',
1156
+ ...(hostMode.reason ? { reason: hostMode.reason } : {}),
1157
+ ...(hostMode.since !== undefined ? { since: hostMode.since } : {}),
1158
+ });
1159
+ }
1160
+ }
939
1161
  // Initialize MCPL subsystems if configured
940
1162
  if (config.mcplServers && config.mcplServers.length > 0) {
941
1163
  // Validate tool prefixes: no collisions with module names or between servers
@@ -1040,6 +1262,9 @@ export class AgentFramework {
1040
1262
  */
1041
1263
  async stop() {
1042
1264
  this.running = false;
1265
+ // Flushed-but-unsynced deferred writes: sync and ack now, while the
1266
+ // store is still open, rather than leaving them to a reboot replay.
1267
+ this.ackDeferredWrites();
1043
1268
  this.providerAdmissionClosed = true;
1044
1269
  this.queue.close();
1045
1270
  this.tuneOutCoordinator?.stop();
@@ -1766,10 +1991,60 @@ export class AgentFramework {
1766
1991
  const agent = this.agents.get(agentName);
1767
1992
  if (!agent)
1768
1993
  throw new Error(`Unknown agent: ${agentName}`);
1994
+ // Feasibility preflight — only a budget-bearing patch can wedge, and only
1995
+ // on the immediate path (a paced descent leaves the compile budget alone
1996
+ // and converges only as far as the folded floor allows). Boot restore
1997
+ // deliberately bypasses this wrapper (createAgent → restoreRuntimeSettings),
1998
+ // so a persisted-but-now-infeasible budget can never brick startup.
1999
+ if (patch.contextBudgetTokens !== undefined) {
2000
+ const preview = this.previewAgentRuntimeSettings(agentName, patch);
2001
+ if (preview.available && preview.effective) {
2002
+ const e = preview.effective;
2003
+ // Block only a LOWERING that doesn't fit. A no-op rewrite or an
2004
+ // increase on an already-over-floor agent never makes things worse —
2005
+ // refusing those would throw on exactly the wedged population the
2006
+ // guard exists to protect (and block stepwise remediation).
2007
+ const lowering = patch.contextBudgetTokens < preview.liveBudgetTokens;
2008
+ if (preview.path === 'immediate' && !e.fits && lowering) {
2009
+ const msg = `[budget-preflight] contextBudgetTokens=${patch.contextBudgetTokens} would NOT fit ` +
2010
+ `${agentName}: the folded floor renders ${e.finalTokens} tokens against hard budget ` +
2011
+ `${e.budgetTokens} (head=${e.headTokens ?? '?'} tail=${e.tailTokens ?? '?'} ` +
2012
+ `middle=${e.middleTokens ?? '?'}, deepest L${e.deepestLevel ?? '?'}` +
2013
+ `${e.exhausted ? ', picker exhausted' : ''}). Every compile at this budget would ` +
2014
+ `OverBudget-wedge the agent. Lower the floor first (drain compression quarantine / ` +
2015
+ `advance the merge ladder), use a paced descent (no \`immediate\`), or pass allowInfeasible.`;
2016
+ if (!opts?.allowInfeasible)
2017
+ throw new BudgetPreflightError(msg, preview);
2018
+ console.warn(`${msg} — applying anyway (allowInfeasible).`);
2019
+ }
2020
+ else if (preview.path === 'immediate' && !e.fits) {
2021
+ console.warn(`[budget-preflight] ${agentName} remains over the folded floor at ` +
2022
+ `${patch.contextBudgetTokens} (floor renders ${e.finalTokens}); this patch does ` +
2023
+ `not lower the budget, so applying — drain quarantine / advance merges to clear it.`);
2024
+ }
2025
+ else if (preview.path === 'paced' && preview.advisory && !preview.advisory.fits) {
2026
+ console.warn(`[budget-preflight] paced descent for ${agentName} targets ` +
2027
+ `${preview.advisory.targetTokens} tokens, below the current folded floor — the ` +
2028
+ `transition will converge only as far as the floor allows (drain quarantine / ` +
2029
+ `advance merges to go lower). Applying; the live compile budget is unchanged.`);
2030
+ }
2031
+ }
2032
+ else if (preview.reason) {
2033
+ console.warn(`[budget-preflight] preview unavailable for ${agentName} (${preview.reason}); ` +
2034
+ `applying without preflight`);
2035
+ }
2036
+ }
1769
2037
  const result = agent.updateRuntimeSettings(patch);
1770
2038
  if (opts?.persist !== false) {
1771
2039
  this.persistAgentRuntimeSettings(agentName, agent.getRuntimeSettingsOverrides());
1772
2040
  }
2041
+ this.recordOperatorAction({
2042
+ kind: 'settings-update',
2043
+ agent: agentName,
2044
+ ...(opts?.requester ? { requester: opts.requester } : {}),
2045
+ ...(opts?.note ? { note: opts.note } : {}),
2046
+ params: { patch: { ...patch }, persist: opts?.persist !== false },
2047
+ });
1773
2048
  return result;
1774
2049
  }
1775
2050
  resetAgentRuntimeSettings(agentName, keys, opts) {
@@ -1780,16 +2055,616 @@ export class AgentFramework {
1780
2055
  if (opts?.persist !== false) {
1781
2056
  this.persistAgentRuntimeSettings(agentName, agent.getRuntimeSettingsOverrides());
1782
2057
  }
2058
+ this.recordOperatorAction({
2059
+ kind: 'settings-reset',
2060
+ agent: agentName,
2061
+ ...(opts?.requester ? { requester: opts.requester } : {}),
2062
+ ...(opts?.note ? { note: opts.note } : {}),
2063
+ params: { keys: keys ?? 'all', persist: opts?.persist !== false },
2064
+ });
1783
2065
  return result;
1784
2066
  }
1785
- cancelAgentRuntimeSettingsTransition(agentName) {
2067
+ cancelAgentRuntimeSettingsTransition(agentName, opts) {
1786
2068
  const agent = this.agents.get(agentName);
1787
2069
  if (!agent)
1788
2070
  throw new Error(`Unknown agent: ${agentName}`);
1789
2071
  const result = agent.cancelRuntimeSettingsTransition();
1790
2072
  this.persistAgentRuntimeSettings(agentName, agent.getRuntimeSettingsOverrides());
2073
+ this.recordOperatorAction({
2074
+ kind: 'settings-cancel-transition',
2075
+ agent: agentName,
2076
+ ...(opts?.requester ? { requester: opts.requester } : {}),
2077
+ });
1791
2078
  return result;
1792
2079
  }
2080
+ // -------------------------------------------------------------------------
2081
+ // Host quiesce / maintenance mode (issue #122)
2082
+ //
2083
+ // Pause the inference thread and MCPL data planes while keeping the
2084
+ // framework + context managers + membrane loaded and hot, so maintenance
2085
+ // (compression ticks, refold, quarantine drains, budget descents) runs
2086
+ // through the exact machinery the agent uses live — with live config, live
2087
+ // tool definitions, and full llm-calls logging — instead of offline rigs
2088
+ // that re-derive all of it and drift.
2089
+ //
2090
+ // While quiesced: runLoop, store sync, the maintenance timer, and the
2091
+ // liveness watchdog all keep running. Wakes park in pendingRequests
2092
+ // (coalesced per reason) and fire at resume; MCPL events buffer on the
2093
+ // paused data planes; gate debounces buffer in the gate. Background
2094
+ // code-execution scripts are NOT stopped (they hold no turn token) — they
2095
+ // are reported in the status so the operator sees what still acts.
2096
+ //
2097
+ // NOTE: do not call quiesce() from inside a queue event handler — the drain
2098
+ // wait depends on the event loop continuing to run.
2099
+ // -------------------------------------------------------------------------
2100
+ /**
2101
+ * Wakes that passed the scheduler and now own provider admission while
2102
+ * waiting for an in-flight auxiliary call to settle — no turn token yet,
2103
+ * so `activeTurnTokens` alone would call the host drained while a turn is
2104
+ * one promise-resolution away from starting. The admission continuation
2105
+ * rechecks quiesce and requeues, but until it runs the wake is live.
2106
+ */
2107
+ parkedAdmissionCount() {
2108
+ let n = 0;
2109
+ for (const [agentName, gate] of this.providerGates) {
2110
+ if (gate.primaryDepth > 0 && !this.activeTurnTokens.has(agentName))
2111
+ n++;
2112
+ }
2113
+ return n;
2114
+ }
2115
+ getHostModeStatus() {
2116
+ const parkedAdmissions = this.parkedAdmissionCount();
2117
+ return {
2118
+ quiesced: this.quiesced,
2119
+ ...(this.quiesceReason ? { reason: this.quiesceReason } : {}),
2120
+ ...(this.quiescedAt !== undefined ? { since: this.quiescedAt } : {}),
2121
+ drained: this.activeTurnTokens.size === 0 && parkedAdmissions === 0,
2122
+ activeTurns: this.activeTurnTokens.size,
2123
+ parkedAdmissions,
2124
+ gatedRequests: this.pendingRequests.filter((r) => !isTurnContinuation(r.reason)).length,
2125
+ backgroundScripts: [...this.backgroundScripts.values()]
2126
+ .filter((record) => record.status === 'running').length,
2127
+ deferredWrites: this.deferredMessages.length,
2128
+ ...(this.lastUnabandonable.length > 0 ? { unabandonable: [...this.lastUnabandonable] } : {}),
2129
+ };
2130
+ }
2131
+ /** Drain window bounds shared by every ingress (framework API, WS, HTTP,
2132
+ * host/command). Below the floor a drain is meaningless; above the ceiling
2133
+ * an awaited call would poll for days. */
2134
+ static QUIESCE_TIMEOUT_MIN_MS = 1_000;
2135
+ static QUIESCE_TIMEOUT_MAX_MS = 600_000;
2136
+ static QUIESCE_TIMEOUT_DEFAULT_MS = 120_000;
2137
+ /**
2138
+ * Enter quiesce mode: persist the flag, suppress gate deliveries, pause all
2139
+ * MCPL data planes, and wait for in-flight turns to settle (turn-alive is
2140
+ * `activeTurnTokens`, which spans dequeue → settled teardown). Idempotent.
2141
+ *
2142
+ * On drain timeout the host STAYS quiesced (`drained: false` in the
2143
+ * result); with `abandon: true` the undrained streams are cancelled via a
2144
+ * dedicated cancel kind that settles the turn without feeding the
2145
+ * inference-failure accounting.
2146
+ */
2147
+ async quiesce(opts) {
2148
+ if (this.quiesced) {
2149
+ // Idempotent — EXCEPT abandon escalation: a first quiesce that timed
2150
+ // out undrained must be escalatable with a second quiesce({abandon})
2151
+ // without resume()+re-quiesce (which would reopen data planes and
2152
+ // release parked wakes mid-surgery).
2153
+ if (opts?.abandon && this.activeTurnTokens.size > 0) {
2154
+ const unabandonable = await this.abandonActiveTurns();
2155
+ this.emitTrace({
2156
+ type: 'host:quiesce',
2157
+ ...(this.quiesceReason ? { reason: this.quiesceReason } : {}),
2158
+ drained: this.activeTurnTokens.size === 0,
2159
+ activeTurns: this.activeTurnTokens.size,
2160
+ abandoned: true,
2161
+ ...(unabandonable.length > 0 ? { unabandonable } : {}),
2162
+ });
2163
+ }
2164
+ return this.getHostModeStatus();
2165
+ }
2166
+ this.quiesced = true;
2167
+ this.quiesceReason = opts?.reason;
2168
+ this.quiescedAt = Date.now();
2169
+ this.lastUnabandonable = [];
2170
+ this.persistHostMode({
2171
+ quiesced: true,
2172
+ ...(opts?.reason ? { reason: opts.reason } : {}),
2173
+ since: this.quiescedAt,
2174
+ });
2175
+ console.error(`[host-mode] quiescing${opts?.reason ? ` (${opts.reason})` : ''}: parking wakes, ` +
2176
+ `pausing MCPL data planes, draining ${this.activeTurnTokens.size} in-flight turn(s)`);
2177
+ this.eventGate?.setQuiesced(true);
2178
+ for (const connection of this.mcplServerRegistry?.getAllServers() ?? []) {
2179
+ connection.pauseDataPlane();
2180
+ }
2181
+ // Clamp HERE, for every ingress. NaN-proof too: timeoutMs arrives via
2182
+ // Number()/blind casts, and Math.max(1_000, NaN) is NaN — which would
2183
+ // silently collapse the drain window to zero (`Date.now() < NaN` is
2184
+ // false). The ceiling matters because this call is awaited: an
2185
+ // unbounded value polls the event loop for as long as it says.
2186
+ const rawTimeout = opts?.timeoutMs;
2187
+ const timeoutMs = typeof rawTimeout === 'number' && Number.isFinite(rawTimeout)
2188
+ ? Math.max(AgentFramework.QUIESCE_TIMEOUT_MIN_MS, Math.min(AgentFramework.QUIESCE_TIMEOUT_MAX_MS, Math.floor(rawTimeout)))
2189
+ : AgentFramework.QUIESCE_TIMEOUT_DEFAULT_MS;
2190
+ const deadline = Date.now() + timeoutMs;
2191
+ // Drain = no turn token AND no wake parked on provider admission (the
2192
+ // latter becomes a turn the moment its auxiliary settles unless the
2193
+ // continuation's quiesce recheck requeues it — wait for that to happen).
2194
+ while (this.quiesced &&
2195
+ (this.activeTurnTokens.size > 0 || this.parkedAdmissionCount() > 0) &&
2196
+ Date.now() < deadline) {
2197
+ await new Promise((resolve) => setTimeout(resolve, 250));
2198
+ }
2199
+ // A concurrent resume() ended this window while we waited. Nothing here
2200
+ // may act any more — an abandon now would cancel a turn that started
2201
+ // legitimately AFTER the resume, and the trace/log would describe a mode
2202
+ // the host is no longer in.
2203
+ if (!this.quiesced) {
2204
+ console.error('[host-mode] quiesce superseded by a concurrent resume during drain');
2205
+ return this.getHostModeStatus();
2206
+ }
2207
+ let abandoned = false;
2208
+ let unabandonable = [];
2209
+ if (this.activeTurnTokens.size > 0 && opts?.abandon) {
2210
+ abandoned = true;
2211
+ unabandonable = await this.abandonActiveTurns();
2212
+ if (!this.quiesced)
2213
+ return this.getHostModeStatus();
2214
+ }
2215
+ const status = this.getHostModeStatus();
2216
+ this.emitTrace({
2217
+ type: 'host:quiesce',
2218
+ ...(this.quiesceReason ? { reason: this.quiesceReason } : {}),
2219
+ drained: status.drained,
2220
+ activeTurns: status.activeTurns,
2221
+ ...(status.parkedAdmissions > 0 ? { parkedAdmissions: status.parkedAdmissions } : {}),
2222
+ ...(abandoned ? { abandoned: true } : {}),
2223
+ ...(unabandonable.length > 0 ? { unabandonable } : {}),
2224
+ });
2225
+ console.error(status.drained
2226
+ ? `[host-mode] quiesced — drained, ${status.gatedRequests} wake(s) parked`
2227
+ : `[host-mode] quiesced but drain ${abandoned ? 'needed abandon and' : 'timed out —'} ` +
2228
+ `${status.activeTurns} turn(s) still alive` +
2229
+ (status.parkedAdmissions > 0
2230
+ ? `, ${status.parkedAdmissions} wake(s) parked on provider admission`
2231
+ : ''));
2232
+ return status;
2233
+ }
2234
+ /** Cancel every turn-alive stream via the quiesce_abandoned kind and give
2235
+ * teardown a bounded grace to settle. Shared by the drain-timeout path and
2236
+ * the already-quiesced escalation path. */
2237
+ async abandonActiveTurns() {
2238
+ const unabandonable = [];
2239
+ for (const agentName of [...this.activeTurnTokens.keys()]) {
2240
+ const agent = this.agents.get(agentName);
2241
+ const state = agent?.state;
2242
+ const stream = state && 'stream' in state ? state.stream : undefined;
2243
+ if (agent && stream) {
2244
+ console.error(`[host-mode] abandoning in-flight turn for ${agentName}`);
2245
+ this.frameworkCancelledStreams.set(`${agent.name}:${agent.streamId}`, 'quiesce_abandoned');
2246
+ stream.cancel();
2247
+ }
2248
+ else {
2249
+ // Token held, no stream to cancel: the turn is between dequeue and
2250
+ // stream registration (hooks/compile), or it is a puppet tool turn,
2251
+ // which never has one. Say so — silence here reads as "abandon acted
2252
+ // and teardown is slow", which is a different operator decision.
2253
+ unabandonable.push(agentName);
2254
+ console.error(`[host-mode] cannot abandon turn for ${agentName}: turn token held with no stream ` +
2255
+ `(status=${state?.status ?? 'unknown'}) — it must settle on its own`);
2256
+ }
2257
+ }
2258
+ // Bounded grace for the cancelled streams' teardown to settle.
2259
+ const grace = Date.now() + 10_000;
2260
+ while (this.quiesced && this.activeTurnTokens.size > 0 && Date.now() < grace) {
2261
+ await new Promise((resolve) => setTimeout(resolve, 250));
2262
+ }
2263
+ // Only turns still alive after the grace are worth reporting.
2264
+ this.lastUnabandonable = unabandonable.filter((name) => this.activeTurnTokens.has(name));
2265
+ return this.lastUnabandonable;
2266
+ }
2267
+ /**
2268
+ * Leave quiesce mode. Gates on a FRESH feasibility preview of every agent's
2269
+ * CURRENT settings — maintenance may have moved the folded floor and the
2270
+ * branch generation, so verdicts are computed at resume time, never reused.
2271
+ * Throws ResumeBlockedError (with all failing verdicts) unless `force`.
2272
+ * Unavailable previews warn and pass — refusing to resume because the
2273
+ * strategy cannot preview would hold hosts hostage to a diagnostic.
2274
+ */
2275
+ async resume(opts) {
2276
+ if (!this.quiesced)
2277
+ return this.getHostModeStatus();
2278
+ // The canonical operator flow is `maintain; resume`, and a timer-driven
2279
+ // pass may still be mid-flight: previewContext refuses to overlap one
2280
+ // ("already running"), which used to degrade the gate to "no opinion".
2281
+ // Wait for the pass first, then retry a few times for the case where a
2282
+ // fresh pass started in between.
2283
+ if (this.maintenancePass) {
2284
+ await this.maintenancePass.catch(() => { });
2285
+ }
2286
+ const failing = [];
2287
+ for (const agentName of this.agents.keys()) {
2288
+ let preview = this.previewAgentRuntimeSettings(agentName);
2289
+ for (let attempt = 0; attempt < 8 && !preview.available && preview.reason === 'preview_in_flight'; attempt++) {
2290
+ await new Promise((resolve) => setTimeout(resolve, 250));
2291
+ if (this.maintenancePass)
2292
+ await this.maintenancePass.catch(() => { });
2293
+ preview = this.previewAgentRuntimeSettings(agentName);
2294
+ }
2295
+ if (preview.available && preview.effective && !preview.effective.fits) {
2296
+ failing.push({ agentName, preview });
2297
+ }
2298
+ else if (!preview.available && preview.reason !== 'no_preview_support') {
2299
+ console.warn(`[host-mode] resume: feasibility preview unavailable for ${agentName} ` +
2300
+ `(${preview.reason}) — proceeding without a verdict`);
2301
+ }
2302
+ }
2303
+ if (failing.length > 0 && !opts?.force) {
2304
+ const detail = failing.map(({ agentName, preview }) => `${agentName}: folded floor ${preview.effective.finalTokens} > hard budget ` +
2305
+ `${preview.effective.budgetTokens}` +
2306
+ (preview.transition === 'blocked'
2307
+ ? ` (transition blocked: ${preview.transitionReason ?? 'unknown'})`
2308
+ : '')).join('; ');
2309
+ throw new ResumeBlockedError(`resume refused: returning to service would OverBudget-wedge — ${detail}. ` +
2310
+ `Drain compression quarantine / advance the merge ladder to lower the floor ` +
2311
+ `(maintenanceTick()), or resume({ force: true }).`, failing);
2312
+ }
2313
+ if (failing.length > 0) {
2314
+ console.warn(`[host-mode] resume FORCED past ${failing.length} failing feasibility verdict(s)`);
2315
+ }
2316
+ const releasedRequests = this.pendingRequests.length;
2317
+ // In-memory first: addMessage must stop deferring so the flush below can
2318
+ // land. The DURABLE flag is cleared only after the flush completes — a
2319
+ // crash in between then boots quiesced with the un-acked remainder
2320
+ // restored (and a serving boot recovers any remainder anyway).
2321
+ this.quiesced = false;
2322
+ this.quiesceReason = undefined;
2323
+ this.quiescedAt = undefined;
2324
+ this.lastUnabandonable = [];
2325
+ this.eventGate?.setQuiesced(false);
2326
+ try {
2327
+ // Flush context writes deferred by the quiesce window (module events,
2328
+ // api message.send) — with no turn alive there is no other flush
2329
+ // point. Per-message try/catch and per-message durable ack (see
2330
+ // flushDeferredWrites); nothing here may reach the plane reopen below.
2331
+ await this.flushDeferredWrites('resume');
2332
+ this.persistHostMode(null);
2333
+ }
2334
+ finally {
2335
+ // Reopen MCPL data planes through the existing barrier funnel — NOT a
2336
+ // bespoke ready() loop. The funnel inherits completeMcplDataPlaneGate's
2337
+ // nested-install guard (a flushed tools-list-changed can install a
2338
+ // newer barrier mid-flush), drains any awareness work accumulated
2339
+ // during the window before opening, and replaces a stale failed
2340
+ // barrier by identity. In `finally` so that nothing above can leave
2341
+ // the host un-quiesced with every data plane still paused.
2342
+ if (this.mcplServerRegistry) {
2343
+ const barrier = this.installMcplDataPlaneGate();
2344
+ this.releaseMcplDataPlaneGate(barrier);
2345
+ try {
2346
+ await barrier.promise;
2347
+ this.completeMcplDataPlaneGate(barrier);
2348
+ }
2349
+ catch (error) {
2350
+ // The host IS resumed — don't rethrow. failMcplDataPlaneGate
2351
+ // recycles the connections, and their reconnect flows re-run the
2352
+ // funnel with quiesced=false, self-healing the data planes.
2353
+ await this.failMcplDataPlaneGate(barrier, 'quiesce resume', error);
2354
+ }
2355
+ }
2356
+ this.emitTrace({
2357
+ type: 'host:resume',
2358
+ ...(opts?.force && failing.length > 0 ? { forced: true } : {}),
2359
+ releasedRequests,
2360
+ });
2361
+ // NOTE: parked wakes and gate buffers are in-memory only — a restart
2362
+ // mid-quiesce boots quiesced but with these queues empty (sources like
2363
+ // heartbeats re-deliver on their own cadence; one-shot wakes are lost).
2364
+ // Deferred context WRITES are persisted while quiesced and survive.
2365
+ console.error(`[host-mode] resumed — ${releasedRequests} parked wake(s) released`);
2366
+ }
2367
+ return this.getHostModeStatus();
2368
+ }
2369
+ /**
2370
+ * Land every deferred write whose target has no turn alive, one at a time,
2371
+ * acknowledging each durably as it lands (the persisted queue is rewritten
2372
+ * after every message). A crash mid-flush therefore replays exactly the
2373
+ * un-acked remainder at the next boot — no loss, no duplicates. A write
2374
+ * the store REJECTS is logged and acked too: replaying a poison message
2375
+ * forever would wedge every later boot on it.
2376
+ *
2377
+ * Per-target: an agent whose turn is still alive keeps ITS messages
2378
+ * deferred for its own turn boundary; everyone else's flush now.
2379
+ */
2380
+ async flushDeferredWrites(label) {
2381
+ if (this.deferredMessages.length === 0) {
2382
+ this.persistDeferredWrites();
2383
+ return;
2384
+ }
2385
+ const keep = [];
2386
+ const flush = [];
2387
+ for (const msg of this.deferredMessages) {
2388
+ const target = msg.forAgent ?? this.primaryAgentName;
2389
+ (target && this.activeTurnTokens.has(target) ? keep : flush).push(msg);
2390
+ }
2391
+ // Hand-off: the batch leaves the pending queue and enters the un-acked
2392
+ // set. The DURABLE queue (pending + un-acked) is unchanged by this, so a
2393
+ // crash anywhere below replays the whole batch, and the deferredWriteId
2394
+ // stamped on each stored message lets boot skip the ones that did land.
2395
+ flush.sort(bySeq);
2396
+ this.deferredMessages = keep;
2397
+ this.handOffDeferredWrites(flush);
2398
+ let stored = 0;
2399
+ for (const msg of flush) {
2400
+ try {
2401
+ this.addMessage(msg.participant, msg.content, msg.metadata, {
2402
+ deferredWriteId: msg.id,
2403
+ ...(msg.forAgent ? { forAgent: msg.forAgent } : {}),
2404
+ });
2405
+ stored++;
2406
+ }
2407
+ catch (err) {
2408
+ console.error(`[host-mode] ${label}: failed to store a deferred context write ` +
2409
+ `(participant=${msg.participant}, forAgent=${msg.forAgent ?? 'primary'}):`, err);
2410
+ }
2411
+ }
2412
+ // Ack = sync the chronicle FIRST, then rewrite the durable queue without
2413
+ // the batch. Never the other way round.
2414
+ this.ackDeferredWrites();
2415
+ console.error(`[host-mode] ${label}: flushed ${stored}/${flush.length} deferred context write(s)` +
2416
+ (keep.length > 0 ? `, ${keep.length} kept for a still-alive turn` : ''));
2417
+ }
2418
+ /**
2419
+ * Acknowledge every deferred write that has been handed to a context
2420
+ * manager: sync the chronicle so the appended slots are durable, and only
2421
+ * then drop them from the recovery queue. If the sync fails they stay in
2422
+ * the durable queue (the next ack, or stop(), retries); a reboot in that
2423
+ * state replays them, deduplicated by `deferredWriteId` for any that did
2424
+ * reach disk. Idempotent and cheap when nothing is un-acked.
2425
+ */
2426
+ ackDeferredWrites() {
2427
+ if (this.unackedDeferredWrites.length === 0)
2428
+ return;
2429
+ // Nothing durable to reconcile against unless the queue was persisted.
2430
+ if (!this.deferredWritesPersisted && !this.quiesced) {
2431
+ this.unackedDeferredWrites = [];
2432
+ return;
2433
+ }
2434
+ try {
2435
+ this.store.sync();
2436
+ }
2437
+ catch (err) {
2438
+ console.error(`[host-mode] chronicle sync failed — ${this.unackedDeferredWrites.length} flushed deferred ` +
2439
+ `write(s) stay in the recovery queue until a sync succeeds:`, err);
2440
+ return;
2441
+ }
2442
+ this.unackedDeferredWrites = [];
2443
+ this.deferredScanFrom.clear();
2444
+ this.persistDeferredWrites();
2445
+ }
2446
+ /**
2447
+ * Move drained entries into the un-acked set and record, per target
2448
+ * agent, the store position they will be appended after — then persist
2449
+ * that receipt BEFORE any of them is written. Boot recovery scans from the
2450
+ * recorded position to the tail, so every member of an interrupted batch
2451
+ * is visible to the dedup, however large the batch.
2452
+ */
2453
+ handOffDeferredWrites(entries) {
2454
+ if (entries.length === 0)
2455
+ return;
2456
+ const durable = this.deferredWritesPersisted || this.quiesced;
2457
+ if (!durable)
2458
+ return; // memory-only deferrals: nothing to reconcile at boot
2459
+ for (const entry of entries) {
2460
+ const target = entry.forAgent ?? this.primaryAgentName;
2461
+ const agent = target ? this.agents.get(target) : undefined;
2462
+ if (!agent)
2463
+ continue;
2464
+ let count = 0;
2465
+ try {
2466
+ const cm = agent.getContextManager();
2467
+ count = typeof cm.getMessageCount === 'function' ? cm.getMessageCount() : cm.getAllMessages().length;
2468
+ }
2469
+ catch {
2470
+ count = 0;
2471
+ }
2472
+ const prev = this.deferredScanFrom.get(target);
2473
+ this.deferredScanFrom.set(target, prev === undefined ? count : Math.min(prev, count));
2474
+ }
2475
+ this.unackedDeferredWrites.push(...entries);
2476
+ this.persistDeferredWrites();
2477
+ }
2478
+ /**
2479
+ * Which of `ids` already exist in some agent's store as a landed deferred
2480
+ * write (`metadata.deferredWriteId`). Scans each context manager's recent
2481
+ * tail blob-free — an interrupted flush is always within the last few
2482
+ * hundred slots — and tolerates facades without windowed reads.
2483
+ */
2484
+ landedDeferredWriteIds(ids, scanFrom) {
2485
+ const landed = new Set();
2486
+ if (ids.size === 0)
2487
+ return landed;
2488
+ const WINDOW = 500;
2489
+ for (const agent of this.agents.values()) {
2490
+ try {
2491
+ const cm = agent.getContextManager();
2492
+ const consider = (m) => {
2493
+ const id = m.metadata?.deferredWriteId;
2494
+ if (typeof id === 'string' && ids.has(id))
2495
+ landed.add(id);
2496
+ };
2497
+ if (typeof cm.getMessageCount === 'function' && typeof cm.getMessageWindow === 'function') {
2498
+ const total = cm.getMessageCount();
2499
+ // From the receipt position (0 = whole store when unknown) to the
2500
+ // tail, in blob-free windows — the batch can only be after it.
2501
+ const from = Math.min(scanFrom.get(agent.name) ?? 0, total);
2502
+ for (let start = from; start < total; start += WINDOW) {
2503
+ const win = cm.getMessageWindow(start, Math.min(WINDOW, total - start), { resolveBlobs: false });
2504
+ for (const m of win.messages)
2505
+ consider(m);
2506
+ }
2507
+ }
2508
+ else {
2509
+ for (const m of cm.getAllMessages())
2510
+ consider(m);
2511
+ }
2512
+ }
2513
+ catch (err) {
2514
+ console.error(`[host-mode] could not scan ${agent.name} for landed deferred writes:`, err);
2515
+ }
2516
+ }
2517
+ return landed;
2518
+ }
2519
+ /** Atomic JSON write for the recovery files (tmp + rename, 0600) — the
2520
+ * same idiom as the Discord awareness outbox. */
2521
+ writeRecoveryFile(path, document) {
2522
+ mkdirSync(dirname(path), { recursive: true });
2523
+ const temporary = `${path}.${process.pid}.${randomUUID()}.tmp`;
2524
+ writeFileSync(temporary, `${JSON.stringify(document, null, 2)}\n`, { mode: 0o600 });
2525
+ renameSync(temporary, path);
2526
+ }
2527
+ readRecoveryFile(path) {
2528
+ if (!existsSync(path))
2529
+ return undefined;
2530
+ return JSON.parse(readFileSync(path, 'utf8'));
2531
+ }
2532
+ warnRecoveryFallbackOnce() {
2533
+ if (this.recoveryFallbackWarned)
2534
+ return;
2535
+ this.recoveryFallbackWarned = true;
2536
+ console.error('[host-mode] no storePath / hostModePath / deferredWritesPath: quiesce state and deferred ' +
2537
+ 'writes persist in the branch-local framework/state slot — a historical rollback while ' +
2538
+ 'quiesced can erase them; configure the recovery paths');
2539
+ }
2540
+ /**
2541
+ * Mirror the deferred-write queue durably while quiesced (and until an
2542
+ * already-persisted queue drains). The quiesce flag itself is persisted
2543
+ * because a crash mid-surgery is an expected event; the messages withheld
2544
+ * BECAUSE of that surgery would otherwise die with the process — and the
2545
+ * window is operator-length, not turn-length. Lives in the
2546
+ * branch-independent recovery file so a rollback cannot orphan it.
2547
+ * Deferrals outside a quiesce window (turn-alive, mid-tool-cycle) are
2548
+ * still memory-only: they flush within the turn, as before.
2549
+ */
2550
+ persistDeferredWrites() {
2551
+ try {
2552
+ // The durable queue is everything not yet acked: writes still pending
2553
+ // AND writes handed to a context manager whose sync has not happened.
2554
+ // Always in original deferral order, whatever the pending/un-acked
2555
+ // split: a re-deferred entry must not jump the queue.
2556
+ const durable = [...this.unackedDeferredWrites, ...this.deferredMessages].sort(bySeq);
2557
+ const scanFrom = Object.fromEntries(this.deferredScanFrom);
2558
+ if ((this.quiesced || this.deferredWritesPersisted) && durable.length > 0) {
2559
+ const payload = JSON.stringify(durable);
2560
+ if (payload.length > DEFERRED_WRITES_PERSIST_CAP_BYTES) {
2561
+ if (!this.deferredWritesCapWarned) {
2562
+ this.deferredWritesCapWarned = true;
2563
+ console.error(`[host-mode] deferred context writes exceed ${DEFERRED_WRITES_PERSIST_CAP_BYTES} bytes ` +
2564
+ `serialized (${durable.length} message(s)) — kept in memory only; ` +
2565
+ `a crash before resume loses them`);
2566
+ }
2567
+ return;
2568
+ }
2569
+ if (this.deferredWritesPath) {
2570
+ this.writeRecoveryFile(this.deferredWritesPath, { version: 2, pending: durable, scanFrom });
2571
+ }
2572
+ else {
2573
+ this.warnRecoveryFallbackOnce();
2574
+ this.store.setStateJson(DEFERRED_WRITES_ID, { version: 2, pending: durable, scanFrom });
2575
+ }
2576
+ this.deferredWritesPersisted = true;
2577
+ }
2578
+ else if (this.deferredWritesPersisted) {
2579
+ if (this.deferredWritesPath) {
2580
+ this.writeRecoveryFile(this.deferredWritesPath, { version: 1, pending: [] });
2581
+ }
2582
+ else {
2583
+ this.store.setStateJson(DEFERRED_WRITES_ID, []);
2584
+ }
2585
+ this.deferredWritesPersisted = false;
2586
+ }
2587
+ }
2588
+ catch (err) {
2589
+ console.error('[host-mode] failed to persist deferred context writes:', err);
2590
+ }
2591
+ }
2592
+ restorePersistedDeferredWrites() {
2593
+ try {
2594
+ let raw;
2595
+ if (this.deferredWritesPath && existsSync(this.deferredWritesPath)) {
2596
+ raw = this.readRecoveryFile(this.deferredWritesPath);
2597
+ }
2598
+ else {
2599
+ // Stores that predate the recovery file (or store-only configs).
2600
+ raw = this.store.getStateJson(DEFERRED_WRITES_ID);
2601
+ }
2602
+ // v1 slot payloads were a bare array; v1/v2 files and v2 slots are
2603
+ // `{ pending, scanFrom? }`.
2604
+ const doc = Array.isArray(raw) ? { pending: raw } : raw;
2605
+ const data = doc?.pending;
2606
+ if (!Array.isArray(data) || data.length === 0)
2607
+ return 0;
2608
+ const scanFrom = new Map();
2609
+ if (doc?.scanFrom && typeof doc.scanFrom === 'object') {
2610
+ for (const [k, v] of Object.entries(doc.scanFrom)) {
2611
+ if (typeof v === 'number' && Number.isFinite(v))
2612
+ scanFrom.set(k, Math.max(0, Math.floor(v)));
2613
+ }
2614
+ }
2615
+ const candidates = data
2616
+ .filter((m) => !!m && typeof m === 'object' && typeof m.participant === 'string'
2617
+ && Array.isArray(m.content))
2618
+ .map((m, i) => ({
2619
+ ...m,
2620
+ id: typeof m.id === 'string' ? m.id : randomUUID(),
2621
+ // Files that predate `seq` are in write order already.
2622
+ seq: typeof m.seq === 'number' ? m.seq : i + 1,
2623
+ }))
2624
+ .sort(bySeq);
2625
+ this.deferredSeq = Math.max(this.deferredSeq, ...candidates.map((m) => m.seq));
2626
+ // Exactly-once: a message that landed before its ack was written is
2627
+ // already in the store under its deferredWriteId — do not replay it.
2628
+ // The scan covers every slot the interrupted batch could occupy (from
2629
+ // the receipt's per-agent position), or the whole store when the
2630
+ // receipt predates the position field.
2631
+ const landed = this.landedDeferredWriteIds(new Set(candidates.map((m) => m.id)), scanFrom);
2632
+ const restored = candidates.filter((m) => !landed.has(m.id));
2633
+ if (landed.size > 0) {
2634
+ console.error(`[host-mode] ${landed.size} deferred context write(s) had already landed before their ack ` +
2635
+ `(interrupted flush) — skipped, not replayed`);
2636
+ }
2637
+ this.deferredMessages.push(...restored);
2638
+ this.deferredWritesPersisted = true;
2639
+ // Rewrite the durable queue without the already-landed entries so a
2640
+ // second interrupted boot does not re-scan them.
2641
+ if (landed.size > 0)
2642
+ this.persistDeferredWrites();
2643
+ return restored.length;
2644
+ }
2645
+ catch (err) {
2646
+ console.error('[host-mode] failed to restore persisted deferred context writes:', err);
2647
+ return 0;
2648
+ }
2649
+ }
2650
+ /**
2651
+ * Operator-driven maintenance: run (or join) one bounded compression pass —
2652
+ * the same runQueuedMaintenance the timer drives, so tool definitions are
2653
+ * refreshed and ticks are bounded/serialized identically — and return the
2654
+ * maintenance snapshot. Works while quiesced by design: `running` stays
2655
+ * true in quiesce mode precisely so this machinery stays hot.
2656
+ */
2657
+ async maintenanceTick() {
2658
+ if (!this.running) {
2659
+ // startQueuedMaintenance no-ops on a stopped/never-started framework —
2660
+ // a bare snapshot would masquerade as a completed pass, so say so.
2661
+ console.warn('[host-mode] maintenanceTick: framework is not running — no pass executed');
2662
+ return { ...this.getContextMaintenanceSnapshot(), ran: false };
2663
+ }
2664
+ this.startQueuedMaintenance();
2665
+ await this.maintenancePass;
2666
+ return { ...this.getContextMaintenanceSnapshot(), ran: true };
2667
+ }
1793
2668
  /** Counts-only context-maintenance diagnostics for authenticated debug UIs. */
1794
2669
  getContextMaintenanceSnapshot() {
1795
2670
  const agents = [...this.agents.values()].map((agent) => {
@@ -1907,6 +2782,90 @@ export class AgentFramework {
1907
2782
  // large store, and older context-manager builds ignore the argument.
1908
2783
  return cm.previewContext({ maxTokens: budgetTokens, reserveForResponse: agent.maxTokens }, overrides, opts);
1909
2784
  }
2785
+ /**
2786
+ * Feasibility preview for a runtime-settings patch — or, with no patch, for
2787
+ * the agent's CURRENT settings (the resume-gate case: quiesce maintenance
2788
+ * may have moved the folded floor and the branch generation, so the verdict
2789
+ * must be computed fresh at resume time).
2790
+ *
2791
+ * Models the patch through the same immediate-vs-paced semantics as the
2792
+ * apply path (`Agent.planRuntimeSettings`): a non-immediate budget decrease
2793
+ * is previewed at the UNCHANGED live compile budget, with a best-effort
2794
+ * advisory verdict at the descent target. This is what the superseded
2795
+ * `feat/budget-preflight-guard` got wrong — it previewed the patch value as
2796
+ * the compile budget and rejected safe-by-construction paced descents.
2797
+ *
2798
+ * Never throws for preview-layer reasons: strategy without previewContext,
2799
+ * preview overlap, stale branch generation, and the hierarchical (non-
2800
+ * adaptive) path all come back as `{available: false, reason}`. Unknown
2801
+ * agent still throws, matching the sibling accessors.
2802
+ */
2803
+ previewAgentRuntimeSettings(agentName, patch) {
2804
+ const agent = this.agents.get(agentName);
2805
+ if (!agent)
2806
+ throw new Error(`Unknown agent: ${agentName}`);
2807
+ const plan = agent.planRuntimeSettings(patch);
2808
+ const settings = agent.getRuntimeSettings();
2809
+ const base = {
2810
+ available: false,
2811
+ path: plan.path,
2812
+ liveBudgetTokens: plan.liveBudgetTokens,
2813
+ transition: settings.transition,
2814
+ ...(settings.transitionReason ? { transitionReason: settings.transitionReason } : {}),
2815
+ };
2816
+ const runPreview = (budgetTokens) => {
2817
+ try {
2818
+ const result = this.previewContextSettings(agentName, budgetTokens, Object.keys(plan.overrides).length > 0 ? plan.overrides : undefined);
2819
+ if (result === null || typeof result !== 'object') {
2820
+ return { reason: 'no_preview_support' };
2821
+ }
2822
+ return { result: result };
2823
+ }
2824
+ catch (error) {
2825
+ const message = error instanceof Error ? error.message : String(error);
2826
+ // Structured mapping of the strategy's known throw paths. Message
2827
+ // matching is fragile by nature, so unknown messages surface verbatim
2828
+ // as the reason rather than being swallowed or rethrown.
2829
+ if (message.includes('requires reinitialization for the current branch generation')) {
2830
+ return { reason: 'branch_generation_changed' };
2831
+ }
2832
+ if (message.includes('already running; previews must not overlap')) {
2833
+ return { reason: 'preview_in_flight' };
2834
+ }
2835
+ if (message.includes('requires adaptiveResolution')) {
2836
+ return { reason: 'no_adaptive_resolution' };
2837
+ }
2838
+ return { reason: message };
2839
+ }
2840
+ };
2841
+ const effective = runPreview(plan.effectiveBudgetTokens);
2842
+ if (!effective.result) {
2843
+ return { ...base, reason: effective.reason };
2844
+ }
2845
+ const preview = {
2846
+ ...base,
2847
+ available: true,
2848
+ effective: {
2849
+ ...effective.result,
2850
+ budgetTokens: plan.effectiveBudgetTokens,
2851
+ fits: effective.result.fits === true,
2852
+ finalTokens: Number(effective.result.finalTokens ?? 0),
2853
+ },
2854
+ };
2855
+ if (plan.advisoryTargetTokens !== undefined) {
2856
+ // Best-effort: an advisory failure never flips `available` — the
2857
+ // effective verdict above is the one that gates anything.
2858
+ const advisory = runPreview(plan.advisoryTargetTokens);
2859
+ if (advisory.result) {
2860
+ preview.advisory = {
2861
+ ...advisory.result,
2862
+ targetTokens: plan.advisoryTargetTokens,
2863
+ fits: advisory.result.fits === true,
2864
+ };
2865
+ }
2866
+ }
2867
+ return preview;
2868
+ }
1910
2869
  /**
1911
2870
  * Check if process logging is enabled.
1912
2871
  */
@@ -2066,6 +3025,81 @@ export class AgentFramework {
2066
3025
  state.agentRuntimeSettings = all;
2067
3026
  this.store.setStateJson(FRAMEWORK_STATE_ID, state);
2068
3027
  }
3028
+ /** Persist (or clear, with null) the host quiesce mode. First process-global
3029
+ * key in the framework/state slot: quiesce survives a restart BY DESIGN — a
3030
+ * crash mid-surgery must come back up NOT serving against a half-repaired
3031
+ * store. Resume is always reachable (public method, WS/HTTP, control-plane
3032
+ * host/command), and every quiesced boot logs a loud banner. */
3033
+ persistHostMode(mode) {
3034
+ if (this.hostModePath) {
3035
+ // Branch-independent: the recovery file is the record. (`quiesced:
3036
+ // false` is written explicitly rather than deleting the file so that
3037
+ // its presence, not its content, decides precedence over the legacy
3038
+ // branch-local slot below.)
3039
+ this.writeRecoveryFile(this.hostModePath, mode === null ? { version: 1, quiesced: false, clearedAt: Date.now() } : { version: 1, ...mode });
3040
+ if (mode === null) {
3041
+ // Also drop any legacy slot marker so a downgrade cannot resurrect it.
3042
+ try {
3043
+ const data = this.store.getStateJson(FRAMEWORK_STATE_ID);
3044
+ if (data && typeof data === 'object' && 'hostMode' in data) {
3045
+ const state = { ...data };
3046
+ delete state.hostMode;
3047
+ this.store.setStateJson(FRAMEWORK_STATE_ID, state);
3048
+ }
3049
+ }
3050
+ catch { /* best effort */ }
3051
+ }
3052
+ }
3053
+ else {
3054
+ this.warnRecoveryFallbackOnce();
3055
+ const data = this.store.getStateJson(FRAMEWORK_STATE_ID);
3056
+ const state = (data && typeof data === 'object' ? data : {});
3057
+ if (mode === null)
3058
+ delete state.hostMode;
3059
+ else
3060
+ state.hostMode = { ...mode };
3061
+ this.store.setStateJson(FRAMEWORK_STATE_ID, state);
3062
+ }
3063
+ // The whole feature rests on the flag surviving a crash — close the
3064
+ // window between the write and the next periodic sync.
3065
+ try {
3066
+ this.store.sync();
3067
+ }
3068
+ catch (err) {
3069
+ console.error('[host-mode] store sync after host-mode change failed:', err);
3070
+ }
3071
+ }
3072
+ /** The recovery file wins whenever it exists; the branch-local slot is
3073
+ * read only for stores that predate it (or store-only configs). */
3074
+ readHostMode() {
3075
+ try {
3076
+ let record = null;
3077
+ if (this.hostModePath && existsSync(this.hostModePath)) {
3078
+ const doc = this.readRecoveryFile(this.hostModePath);
3079
+ if (doc && typeof doc === 'object')
3080
+ record = doc;
3081
+ }
3082
+ else {
3083
+ const data = this.store.getStateJson(FRAMEWORK_STATE_ID);
3084
+ if (!data || typeof data !== 'object')
3085
+ return null;
3086
+ const mode = data.hostMode;
3087
+ if (!mode || typeof mode !== 'object')
3088
+ return null;
3089
+ record = mode;
3090
+ }
3091
+ if (!record)
3092
+ return null;
3093
+ return {
3094
+ quiesced: record.quiesced === true,
3095
+ ...(typeof record.reason === 'string' ? { reason: record.reason } : {}),
3096
+ ...(typeof record.since === 'number' ? { since: record.since } : {}),
3097
+ };
3098
+ }
3099
+ catch {
3100
+ return null;
3101
+ }
3102
+ }
2069
3103
  /**
2070
3104
  * Create an ephemeral agent that is NOT registered in the main event loop.
2071
3105
  *
@@ -2176,18 +3210,36 @@ export class AgentFramework {
2176
3210
  * The task message should already be in the context manager.
2177
3211
  */
2178
3212
  async runEphemeralToCompletion(agent, contextManager, watchdogs) {
3213
+ // Refused at admission while quiesced rather than requeued: the caller
3214
+ // awaits the settle promise under an idle-stall watchdog, so a gate-parked
3215
+ // ephemeral would ride out its timeout and reject with a misleading
3216
+ // "stalled" error hours later. A clear refusal now beats that. Checked
3217
+ // before the generation ticket is consumed so the same (agent, cm) pair
3218
+ // can be retried after resume().
3219
+ if (this.quiesced) {
3220
+ throw new Error(`framework is quiesced${this.quiesceReason ? ` (${this.quiesceReason})` : ''}: ` +
3221
+ `refusing new ephemeral run for ${agent.name} — resume() first`);
3222
+ }
2179
3223
  // Only a fresh object returned by createEphemeralAgent may enter this path.
2180
3224
  // Never overwrite a resident/conversation owner or a concurrent run: their
2181
3225
  // cleanup is name-keyed and could cancel/deregister the legitimate owner.
2182
3226
  if (this.ephemeralCandidates.get(agent) !== contextManager) {
2183
3227
  throw new Error(`Ephemeral agent "${agent.name}" has no fresh generation ticket from this framework`);
2184
3228
  }
3229
+ // A live surgery holds the whole store: an agent admitted now would
3230
+ // compile against a history that is being replaced under it. Refuse
3231
+ // BEFORE consuming the ticket so the caller can retry once it completes.
3232
+ if (this.surgeryHold) {
3233
+ throw new Error(`Ephemeral agent "${agent.name}" refused: the store is under live ${this.surgeryHold.verb} ` +
3234
+ `for ${this.surgeryHold.agentName} — retry when it completes`);
3235
+ }
2185
3236
  // Consume before any await/registration: the exact (Agent, ContextManager)
2186
3237
  // generation is one-shot even if startup later fails.
2187
3238
  this.ephemeralCandidates.delete(agent);
2188
3239
  if (this.agents.has(agent.name) || this.ephemeralRuns.has(agent.name)) {
2189
3240
  throw new Error(`Ephemeral agent "${agent.name}" is already registered or running`);
2190
3241
  }
3242
+ // Register temporarily so the event loop can drive it
2191
3243
  this.agents.set(agent.name, agent);
2192
3244
  const run = {
2193
3245
  settle: this.createDeferred(),
@@ -2890,8 +3942,15 @@ export class AgentFramework {
2890
3942
  source: 'admin',
2891
3943
  timestamp: Date.now(),
2892
3944
  });
2893
- console.error(`[nudge] agent=${name} queued by=${requestedBy ?? 'unknown'} status=${agentStatus}`);
2894
- return { ok: true, agentName: name, agentStatus };
3945
+ console.error(`[nudge] agent=${name} queued by=${requestedBy ?? 'unknown'} status=${agentStatus}` +
3946
+ (this.quiesced ? ' (host quiesced — parked until resume)' : ''));
3947
+ this.recordOperatorAction({
3948
+ kind: 'nudge',
3949
+ agent: name,
3950
+ requester: { via: 'nudge', ...(requestedBy ? { name: requestedBy } : {}) },
3951
+ result: { agentStatus, ...(this.quiesced ? { quiesced: true } : {}) },
3952
+ });
3953
+ return { ok: true, agentName: name, agentStatus, ...(this.quiesced ? { quiesced: true } : {}) };
2895
3954
  }
2896
3955
  /**
2897
3956
  * Handle a `host/command` request from an MCPL surface server (e.g. a
@@ -2904,14 +3963,65 @@ export class AgentFramework {
2904
3963
  *
2905
3964
  * nudge — run inference on the current context with NO new events
2906
3965
  * (see `nudgeAgent`).
3966
+ *
3967
+ * quiesce — enter host maintenance mode (issue #122): park wakes, pause
3968
+ * MCPL data planes, drain in-flight turns (optionally `abandon` on
3969
+ * timeout). Host-scoped, no agentName.
3970
+ *
3971
+ * resume — leave maintenance mode behind a fresh feasibility preview of
3972
+ * every agent's current settings (`force` overrides a failing verdict).
3973
+ *
3974
+ * maintain — run (or join) one bounded compression/maintenance pass and
3975
+ * return the snapshot; the operator lever for draining quarantine or
3976
+ * advancing merges during a quiesce window.
3977
+ *
3978
+ * host-status — report the quiesce state (drained / parked wakes /
3979
+ * running background scripts).
2907
3980
  */
2908
3981
  async handleHostCommand(serverId, params) {
2909
3982
  if (params.command !== 'undo' &&
2910
3983
  params.command !== 'hide' &&
2911
3984
  params.command !== 'unstick' &&
2912
- params.command !== 'nudge') {
3985
+ params.command !== 'nudge' &&
3986
+ params.command !== 'quiesce' &&
3987
+ params.command !== 'resume' &&
3988
+ params.command !== 'maintain' &&
3989
+ params.command !== 'host-status') {
2913
3990
  return { ok: false, error: `Unknown host command: ${String(params.command)}` };
2914
3991
  }
3992
+ // Host-scoped verbs (issue #122) — no agent resolution.
3993
+ const requester = params.requesterName ?? params.requesterId ?? `mcpl:${serverId}`;
3994
+ if (params.command === 'quiesce') {
3995
+ // quiesce() clamps timeoutMs to [1s, 10m] itself — one clamp for every ingress.
3996
+ console.error(`[host-command] quiesce by=${requester} (server=${serverId})`);
3997
+ const hostMode = await this.quiesce({
3998
+ reason: params.reason ?? `host/command by ${requester}`,
3999
+ ...(params.timeoutMs !== undefined ? { timeoutMs: params.timeoutMs } : {}),
4000
+ ...(params.abandon ? { abandon: true } : {}),
4001
+ });
4002
+ return { ok: true, hostMode };
4003
+ }
4004
+ if (params.command === 'resume') {
4005
+ console.error(`[host-command] resume by=${requester} (server=${serverId})`);
4006
+ try {
4007
+ const hostMode = await this.resume(params.force ? { force: true } : undefined);
4008
+ return { ok: true, hostMode };
4009
+ }
4010
+ catch (error) {
4011
+ if (error instanceof ResumeBlockedError) {
4012
+ return { ok: false, error: error.message, hostMode: this.getHostModeStatus() };
4013
+ }
4014
+ throw error;
4015
+ }
4016
+ }
4017
+ if (params.command === 'maintain') {
4018
+ console.error(`[host-command] maintain by=${requester} (server=${serverId})`);
4019
+ const maintenance = await this.maintenanceTick();
4020
+ return { ok: true, maintenance, hostMode: this.getHostModeStatus() };
4021
+ }
4022
+ if (params.command === 'host-status') {
4023
+ return { ok: true, hostMode: this.getHostModeStatus() };
4024
+ }
2915
4025
  const agentName = params.agentName ?? [...this.agents.keys()][0];
2916
4026
  if (!agentName || !this.agents.has(agentName)) {
2917
4027
  return { ok: false, error: `Unknown agent: ${String(agentName)}` };
@@ -2920,7 +4030,7 @@ export class AgentFramework {
2920
4030
  // event, no context mutation (see nudgeAgent).
2921
4031
  if (params.command === 'nudge') {
2922
4032
  const r = this.nudgeAgent(agentName, params.requesterName ?? params.requesterId ?? `mcpl:${serverId}`);
2923
- return { ok: r.ok, error: r.error, agentStatus: r.agentStatus };
4033
+ return { ok: r.ok, error: r.error, agentStatus: r.agentStatus, ...(r.quiesced ? { quiesced: true } : {}) };
2924
4034
  }
2925
4035
  // unstick: force the refusal-rewind loop on demand (even if the agent's
2926
4036
  // autoRewind toggle is off). Redacts the turn that fed the refusal and
@@ -2948,7 +4058,14 @@ export class AgentFramework {
2948
4058
  });
2949
4059
  console.error(`[unstick] agent=${agentName} started cap=${cap} ` +
2950
4060
  `by=${params.requesterName ?? params.requesterId ?? 'unknown'} (server=${serverId})`);
2951
- return { ok: true, started: true, cap };
4061
+ this.recordOperatorAction({
4062
+ kind: 'unstick',
4063
+ agent: agentName,
4064
+ requester: hostCommandRequester(serverId, params),
4065
+ params: { cap },
4066
+ result: { started: true, ...(this.quiesced ? { quiesced: true } : {}) },
4067
+ });
4068
+ return { ok: true, started: true, cap, ...(this.quiesced ? { quiesced: true } : {}) };
2952
4069
  }
2953
4070
  // hide: redact a single message (or an inclusive range) from the active
2954
4071
  // branch, addressed by Discord message id. Unlike undo this is a
@@ -2993,9 +4110,23 @@ export class AgentFramework {
2993
4110
  }
2994
4111
  const [lo, hi] = fromIdx <= toIdx ? [fromIdx, toIdx] : [toIdx, fromIdx];
2995
4112
  const refs = refsIn(lo, hi);
4113
+ const rangeIds = all.slice(lo, hi + 1).map((m) => String(m.id));
2996
4114
  cm.removeMessages(all[lo].id, all[hi].id);
2997
4115
  console.error(`[host-command] hide agent=${agentName} range removed=${hi - lo + 1} ` +
2998
4116
  `(${params.fromMessageId}..${params.toMessageId}) by=${params.requesterName ?? params.requesterId ?? 'unknown'} (server=${serverId})`);
4117
+ const loggedRange = capIds(rangeIds);
4118
+ this.recordOperatorAction({
4119
+ kind: 'hide',
4120
+ agent: agentName,
4121
+ requester: hostCommandRequester(serverId, params),
4122
+ params: { fromMessageId: params.fromMessageId, toMessageId: params.toMessageId },
4123
+ result: {
4124
+ branch: this.store.currentBranch().name,
4125
+ hidden: hi - lo + 1,
4126
+ removedIds: loggedRange.ids,
4127
+ ...(loggedRange.truncated ? { removedIdsTruncated: true } : {}),
4128
+ },
4129
+ });
2999
4130
  return {
3000
4131
  ok: true,
3001
4132
  hidden: hi - lo + 1,
@@ -3004,9 +4135,17 @@ export class AgentFramework {
3004
4135
  };
3005
4136
  }
3006
4137
  const refs = refsIn(fromIdx, fromIdx);
4138
+ const hiddenId = String(all[fromIdx].id);
3007
4139
  cm.removeMessage(all[fromIdx].id);
3008
4140
  console.error(`[host-command] hide agent=${agentName} removed=1 (${params.fromMessageId}) ` +
3009
4141
  `by=${params.requesterName ?? params.requesterId ?? 'unknown'} (server=${serverId})`);
4142
+ this.recordOperatorAction({
4143
+ kind: 'hide',
4144
+ agent: agentName,
4145
+ requester: hostCommandRequester(serverId, params),
4146
+ params: { fromMessageId: params.fromMessageId },
4147
+ result: { branch: this.store.currentBranch().name, hidden: 1, removedIds: [hiddenId] },
4148
+ });
3010
4149
  return {
3011
4150
  ok: true,
3012
4151
  hidden: 1,
@@ -3029,55 +4168,40 @@ export class AgentFramework {
3029
4168
  }
3030
4169
  const n = Math.max(1, Math.min(50, Math.floor(params.messages)));
3031
4170
  const cm = agent.getContextManager();
3032
- const allMessages = cm.getAllMessages();
3033
- if (n >= allMessages.length) {
4171
+ const total = cm.getMessageCount();
4172
+ if (n >= total) {
3034
4173
  return {
3035
4174
  ok: false,
3036
- error: `Cannot remove ${n} message(s) — history has ${allMessages.length}; at least one must remain.`,
4175
+ error: `Cannot remove ${n} message(s) — history has ${total}; at least one must remain.`,
4176
+ };
4177
+ }
4178
+ const target = cm.getMessageWindow(total - 1 - n, 1, { resolveBlobs: false }).messages[0];
4179
+ try {
4180
+ const r = await this.rollbackToMessage(agentName, {
4181
+ messageId: String(target.id),
4182
+ branchName: `undo-msgs/${agentName}/${Date.now()}`,
4183
+ requester: hostCommandRequester(serverId, params),
4184
+ });
4185
+ return {
4186
+ ok: true,
4187
+ messagesRemoved: r.messagesRemoved,
4188
+ removedRefs: r.removedRefs,
4189
+ lastVisible: r.lastVisible,
4190
+ };
4191
+ }
4192
+ catch (error) {
4193
+ return {
4194
+ ok: false,
4195
+ error: error instanceof Error ? error.message : String(error),
4196
+ ...(error instanceof OperatorActionError ? { code: error.code } : {}),
3037
4197
  };
3038
4198
  }
3039
- const target = allMessages[allMessages.length - 1 - n];
3040
- const discarded = allMessages.slice(allMessages.length - n);
3041
- const removedRefs = extractDiscordAwarenessRefs(discarded);
3042
- const sourceBranch = this.store.currentBranch().name;
3043
- const targetBranch = `undo-msgs/${agentName}/${Date.now()}`;
3044
- // Prepare the external side effect before switching Chronicle. If the
3045
- // process dies after the switch but before activate(), startup promotes
3046
- // this batch by matching targetBranch to the active branch.
3047
- const markerBatch = this.discordAwarenessOutbox?.prepare({
3048
- agentName,
3049
- sourceBranch,
3050
- targetBranch,
3051
- refs: removedRefs,
3052
- emoji: this.discordAwarenessEmoji,
3053
- }) ?? null;
3054
- const branchName = cm.branchAt(target.id, targetBranch);
3055
- await cm.switchBranch(branchName);
3056
- if (markerBatch)
3057
- this.discordAwarenessOutbox.activate(markerBatch.id);
3058
- await this.syncDiscordAwarenessMarkers();
3059
- // Materialize config files from the new branch (fire-and-forget; gate
3060
- // picks up via mtime) — mirrors undoLastTurn.
3061
- const wsUndo = this.moduleRegistry.getModule('workspace');
3062
- if (wsUndo && 'materializeMount' in wsUndo) {
3063
- wsUndo
3064
- .materializeMount('_config')
3065
- .catch(() => { });
3066
- }
3067
- console.error(`[host-command] undo-messages agent=${agentName} removed=${n} branch=${branchName}` +
3068
- ` by=${params.requesterName ?? params.requesterId ?? 'unknown'} (server=${serverId})`);
3069
- return {
3070
- ok: true,
3071
- messagesRemoved: n,
3072
- removedRefs,
3073
- lastVisible: await this.lastVisiblePreview(agentName),
3074
- };
3075
4199
  }
3076
4200
  const requested = Math.max(1, Math.min(20, Math.floor(params.turns ?? 1)));
3077
4201
  let undone = 0;
3078
4202
  try {
3079
4203
  for (let i = 0; i < requested; i++) {
3080
- const r = this.undoLastTurn(agentName);
4204
+ const r = this.undoLastTurn(agentName, hostCommandRequester(serverId, params));
3081
4205
  if (!r.undone)
3082
4206
  break;
3083
4207
  undone++;
@@ -3367,7 +4491,7 @@ export class AgentFramework {
3367
4491
  * The undone branch is saved so `redo()` can restore it.
3368
4492
  * Returns the checkpoint that was undone, or null if nothing to undo.
3369
4493
  */
3370
- undoLastTurn(agentName) {
4494
+ undoLastTurn(agentName, requester) {
3371
4495
  const agent = this.agents.get(agentName);
3372
4496
  if (!agent) {
3373
4497
  throw new Error(`Unknown agent: ${agentName}`);
@@ -3404,6 +4528,13 @@ export class AgentFramework {
3404
4528
  fromBranch: currentBranch.name,
3405
4529
  toBranch: undoBranchName,
3406
4530
  });
4531
+ this.recordOperatorAction({
4532
+ kind: 'undo-turn',
4533
+ agent: agentName,
4534
+ ...(requester ? { requester } : {}),
4535
+ params: { turnIndex: checkpoint.turnIndex },
4536
+ result: { sourceBranch: currentBranch.name, targetBranch: undoBranchName },
4537
+ });
3407
4538
  return {
3408
4539
  undone: true,
3409
4540
  turnIndex: checkpoint.turnIndex,
@@ -3417,7 +4548,7 @@ export class AgentFramework {
3417
4548
  * Switches back to the branch that was active before the last undo.
3418
4549
  * Returns false if there's nothing to redo.
3419
4550
  */
3420
- redo(agentName) {
4551
+ redo(agentName, requester) {
3421
4552
  const agent = this.agents.get(agentName);
3422
4553
  if (!agent) {
3423
4554
  throw new Error(`Unknown agent: ${agentName}`);
@@ -3447,12 +4578,424 @@ export class AgentFramework {
3447
4578
  fromBranch: currentBranch.name,
3448
4579
  toBranch: branchName,
3449
4580
  });
4581
+ this.recordOperatorAction({
4582
+ kind: 'redo-turn',
4583
+ agent: agentName,
4584
+ ...(requester ? { requester } : {}),
4585
+ result: { sourceBranch: currentBranch.name, targetBranch: branchName },
4586
+ });
3450
4587
  return {
3451
4588
  redone: true,
3452
4589
  fromBranch: currentBranch.name,
3453
4590
  toBranch: branchName,
3454
4591
  };
3455
4592
  }
4593
+ // ---------------------------------------------------------------------------
4594
+ // Live operator surgery — rollback / suppress on the OPEN store, without a
4595
+ // restart — plus the durable action log every operator mutation writes to.
4596
+ //
4597
+ // Both follow the same idiom the offline surgeries use (recovery/
4598
+ // offline-branch.ts): fork first, mutate the fork, and make the fork the
4599
+ // active branch. The parent branch keeps everything, so restore is a
4600
+ // checkout. Mutations are refused (never queued) while the agent is not
4601
+ // idle — quiesce the host first if it is busy.
4602
+ // ---------------------------------------------------------------------------
4603
+ /**
4604
+ * Append a record to the operator action log and broadcast it as an
4605
+ * `operator:action` trace. Hosts use this directly for actions the
4606
+ * framework does not own (e.g. a WebUI toggling quiesce). Never throws.
4607
+ */
4608
+ recordOperatorAction(input) {
4609
+ // Observability never fails the action: OperatorLog.append never throws,
4610
+ // and a misbehaving trace listener is contained here.
4611
+ const entry = this.operatorLog.append(input);
4612
+ try {
4613
+ this.emitTrace({
4614
+ type: 'operator:action',
4615
+ kind: entry.kind,
4616
+ ...(entry.agent !== undefined ? { agentName: entry.agent } : {}),
4617
+ ...(entry.requester !== undefined ? { requester: entry.requester } : {}),
4618
+ ...(entry.note !== undefined ? { note: entry.note } : {}),
4619
+ ...(entry.params !== undefined ? { params: entry.params } : {}),
4620
+ ...(entry.result !== undefined ? { result: entry.result } : {}),
4621
+ ...(entry.error !== undefined ? { error: entry.error } : {}),
4622
+ });
4623
+ }
4624
+ catch {
4625
+ // swallowed by design
4626
+ }
4627
+ return entry;
4628
+ }
4629
+ /** Newest `limit` operator-log entries, oldest first. */
4630
+ getOperatorLog(opts) {
4631
+ return this.operatorLog.readTail(opts?.limit ?? 100);
4632
+ }
4633
+ /** Where the operator log is written, or undefined when disabled. */
4634
+ getOperatorLogPath() {
4635
+ return this.operatorLog.path;
4636
+ }
4637
+ /**
4638
+ * The surgery gate — STORE-wide, not per agent. A branch switch moves the
4639
+ * active chronicle branch for every context manager sharing this store,
4640
+ * so an in-flight turn on *any* agent would have its next writes land on
4641
+ * a history its request was never compiled against. Every agent must be
4642
+ * idle with no turn alive (`state.status === 'idle'` alone is not enough:
4643
+ * status reads idle from dequeue until the stream registers and again
4644
+ * during teardown — the scheduler's own busy test is status + turn-alive,
4645
+ * the predicate `puppetToolCall` uses).
4646
+ *
4647
+ * Passing the gate RESERVES the store for the caller: a turn token is
4648
+ * held for every agent until the returned release runs, so no wake — via
4649
+ * the scheduler or a parked provider admission, both of which re-test
4650
+ * turn-alive — can start a turn while the switch is awaited. Release also
4651
+ * flushes messages that deferred behind the reservation, as the puppet
4652
+ * does. Always release in `finally`: a token nobody clears wedges the fleet.
4653
+ */
4654
+ reserveStoreForSurgery(verb, agentName) {
4655
+ if (this.surgeryHold) {
4656
+ throw new OperatorActionError('agent-busy', `Cannot ${verb}: a live ${this.surgeryHold.verb} for ${this.surgeryHold.agentName} is already in progress`);
4657
+ }
4658
+ const busy = [];
4659
+ for (const [name, a] of this.agents) {
4660
+ if (a.state.status !== 'idle')
4661
+ busy.push(`${name} is ${a.state.status}`);
4662
+ else if (this.activeTurnTokens.has(name))
4663
+ busy.push(`${name} is idle+turn-alive`);
4664
+ }
4665
+ for (const name of this.activeTurnTokens.keys()) {
4666
+ if (!this.agents.has(name))
4667
+ busy.push(`${name} is turn-alive`);
4668
+ }
4669
+ if (busy.length > 0) {
4670
+ throw new OperatorActionError('agent-busy', `Cannot ${verb} while ${busy.join(', ')} — every agent sharing the store must be idle (quiesce the host first)`);
4671
+ }
4672
+ const reserved = new Map();
4673
+ for (const name of this.agents.keys()) {
4674
+ const token = this.nextTurnToken++;
4675
+ this.activeTurnTokens.set(name, token);
4676
+ reserved.set(name, token);
4677
+ }
4678
+ // The token snapshot only covers agents that exist now; the hold covers
4679
+ // arrivals during the awaited switch (see surgeryHold).
4680
+ const hold = { verb, agentName, since: Date.now() };
4681
+ this.surgeryHold = hold;
4682
+ let released = false;
4683
+ return () => {
4684
+ if (released)
4685
+ return;
4686
+ released = true;
4687
+ if (this.surgeryHold === hold)
4688
+ this.surgeryHold = null;
4689
+ for (const [name, token] of reserved) {
4690
+ if (this.activeTurnTokens.get(name) === token)
4691
+ this.activeTurnTokens.delete(name);
4692
+ }
4693
+ // Writers that deferred behind the reservation land now, on the branch
4694
+ // the operator chose (or the restored source) — the same end-of-turn
4695
+ // flush driveStream/puppet perform, under the same guard.
4696
+ if (this.deferredMessages.length > 0 && this.pendingAssistantBlocks.size === 0) {
4697
+ for (const name of reserved.keys()) {
4698
+ if (this.activeTurnTokens.has(name))
4699
+ continue;
4700
+ for (const msg of this.drainDeferredFor(name)) {
4701
+ try {
4702
+ this.addMessage(msg.participant, msg.content, msg.metadata, msg.forAgent ? { forAgent: msg.forAgent } : undefined);
4703
+ }
4704
+ catch (error) {
4705
+ console.error(`[operator] post-surgery deferred flush failed for ${name}: ${error instanceof Error ? error.message : String(error)}`);
4706
+ }
4707
+ }
4708
+ }
4709
+ }
4710
+ };
4711
+ }
4712
+ /**
4713
+ * After a failed switch or redaction: put the store back on `sourceBranch`
4714
+ * if it moved, and return a sentence saying what actually happened. Never
4715
+ * throws — the caller reports the original failure with this appended.
4716
+ */
4717
+ async restoreSourceBranch(cm, sourceBranch, failedBranch, agentName) {
4718
+ const current = this.store.currentBranch().name;
4719
+ if (current !== failedBranch)
4720
+ return `active branch is ${current}`;
4721
+ try {
4722
+ await cm.switchBranch(sourceBranch);
4723
+ return `active branch restored to ${sourceBranch}`;
4724
+ }
4725
+ catch (restoreError) {
4726
+ const detail = restoreError instanceof Error ? restoreError.message : String(restoreError);
4727
+ const status = `RESTORE TO ${sourceBranch} ALSO FAILED (${detail}); agent is still on ${failedBranch}`;
4728
+ console.error(`[operator] agent=${agentName} ${status}`);
4729
+ return status;
4730
+ }
4731
+ }
4732
+ /**
4733
+ * Roll the active branch back so `messageId` becomes its tail: fork the
4734
+ * chronicle at that message (origin-sequence time-travel branch) and switch
4735
+ * to the fork. Everything after the message stays on the source branch.
4736
+ * Discord messages that left the live context get awareness markers via the
4737
+ * durable outbox, exactly as message-granular `undo` does.
4738
+ *
4739
+ * Throws `OperatorActionError` (`agent-busy`, `unknown-message`, …) — the
4740
+ * agent must be idle; nothing is queued.
4741
+ */
4742
+ async rollbackToMessage(agentName, opts) {
4743
+ const logBase = {
4744
+ kind: 'rollback',
4745
+ agent: agentName,
4746
+ ...(opts.requester ? { requester: opts.requester } : {}),
4747
+ ...(opts.note ? { note: opts.note } : {}),
4748
+ params: { messageId: opts.messageId },
4749
+ };
4750
+ try {
4751
+ const agent = this.agents.get(agentName);
4752
+ if (!agent)
4753
+ throw new OperatorActionError('unknown-agent', `Unknown agent: ${agentName}`);
4754
+ const cm = agent.getContextManager();
4755
+ const total = cm.getMessageCount();
4756
+ const requestedIndex = locateMessageIndex(cm, opts.messageId);
4757
+ if (requestedIndex < 0) {
4758
+ throw new OperatorActionError('unknown-message', `Message ${opts.messageId} is not on the active branch`);
4759
+ }
4760
+ // Never bisect a body group: chronicle refuses that for removals
4761
+ // (byte-faithful reassembly needs the whole run) and branchAt has no
4762
+ // such guard of its own. Snap to the run's last shard.
4763
+ const run = bodyGroupRun(cm, requestedIndex);
4764
+ const targetIndex = run.to;
4765
+ const tailMessageId = run.toId;
4766
+ const messagesRemoved = total - targetIndex - 1;
4767
+ if (messagesRemoved <= 0) {
4768
+ throw new OperatorActionError('invalid', `Message ${opts.messageId} is already the tail of the active branch`);
4769
+ }
4770
+ const discarded = cm.getMessageWindow(targetIndex + 1, messagesRemoved, { resolveBlobs: false }).messages;
4771
+ const removedRefs = extractDiscordAwarenessRefs(discarded);
4772
+ const sourceBranch = this.store.currentBranch().name;
4773
+ const targetBranch = opts.branchName ?? `rollback/${agentName}/${Date.now()}`;
4774
+ if (targetBranch === sourceBranch) {
4775
+ throw new OperatorActionError('invalid', 'Rollback branch name must differ from the active branch');
4776
+ }
4777
+ // Gate + reserve the whole store (see reserveStoreForSurgery); held
4778
+ // until the switch has landed or been rolled back.
4779
+ const release = this.reserveStoreForSurgery('roll back', agentName);
4780
+ let branchName;
4781
+ try {
4782
+ // Prepare the external side effect before switching Chronicle. If the
4783
+ // process dies after the switch but before activate(), startup
4784
+ // promotes this batch by matching targetBranch to the active branch.
4785
+ // If the fork/switch itself throws, the batch is retired so it cannot
4786
+ // arm a later branch that happens to reuse the name.
4787
+ const markerBatch = this.discordAwarenessOutbox?.prepare({
4788
+ agentName,
4789
+ sourceBranch,
4790
+ targetBranch,
4791
+ refs: removedRefs,
4792
+ emoji: this.discordAwarenessEmoji,
4793
+ }) ?? null;
4794
+ try {
4795
+ branchName = cm.branchAt(tailMessageId, targetBranch);
4796
+ await cm.switchBranch(branchName);
4797
+ }
4798
+ catch (error) {
4799
+ // switchBranch moves the chronicle branch BEFORE awaiting strategy
4800
+ // initialization, so a rejection here can leave the store on the
4801
+ // new branch with an uninitialized strategy. Retire the batch and
4802
+ // go back to the source; keep the branch for diagnosis.
4803
+ if (markerBatch)
4804
+ this.discordAwarenessOutbox.discard(markerBatch.id);
4805
+ const restored = await this.restoreSourceBranch(cm, sourceBranch, targetBranch, agentName);
4806
+ throw new OperatorActionError('failed', `Rollback failed; ${restored}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
4807
+ }
4808
+ if (markerBatch)
4809
+ this.discordAwarenessOutbox.activate(markerBatch.id);
4810
+ await this.syncDiscordAwarenessMarkers();
4811
+ this.materializeConfigMountAfterBranchSwitch();
4812
+ }
4813
+ finally {
4814
+ release();
4815
+ }
4816
+ console.error(`[operator] rollback agent=${agentName} to=${tailMessageId}` +
4817
+ `${tailMessageId !== opts.messageId ? ` (requested ${opts.messageId}, snapped to end of body group)` : ''}` +
4818
+ ` removed=${messagesRemoved} branch=${branchName} by=${describeRequester(opts.requester)}`);
4819
+ this.recordOperatorAction({
4820
+ ...logBase,
4821
+ result: {
4822
+ sourceBranch,
4823
+ targetBranch: branchName,
4824
+ tailMessageId,
4825
+ messagesRemoved,
4826
+ discordRefs: removedRefs.length,
4827
+ },
4828
+ });
4829
+ return {
4830
+ agentName,
4831
+ sourceBranch,
4832
+ targetBranch: branchName,
4833
+ messagesRemoved,
4834
+ tailMessageId,
4835
+ removedRefs,
4836
+ lastVisible: await this.lastVisiblePreview(agentName),
4837
+ };
4838
+ }
4839
+ catch (error) {
4840
+ this.recordOperatorAction({ ...logBase, error: error instanceof Error ? error.message : String(error) });
4841
+ throw error;
4842
+ }
4843
+ }
4844
+ /**
4845
+ * Suppress specific messages from the live context without touching the
4846
+ * source branch: fork at the current head (`suppress/<agent>/<ts>`), redact
4847
+ * the messages on the fork, switch to it. Shards of a body group are always
4848
+ * removed together. Discord originals get awareness markers via the outbox;
4849
+ * the batch is `explicit`-activated only after every removal succeeded, and
4850
+ * a crash mid-way is finished at next boot (resumePreparedDiscordSuppressions).
4851
+ *
4852
+ * Not retroactive over derived state: a message already folded into an
4853
+ * autobiographical summary stays in that summary — roll back to before it
4854
+ * entered if that matters.
4855
+ */
4856
+ async suppressMessages(agentName, opts) {
4857
+ const requestedIds = [...new Set(opts.messageIds.map(String))];
4858
+ const logBase = {
4859
+ kind: 'suppress',
4860
+ agent: agentName,
4861
+ ...(opts.requester ? { requester: opts.requester } : {}),
4862
+ ...(opts.note ? { note: opts.note } : {}),
4863
+ params: { messageIds: requestedIds },
4864
+ };
4865
+ try {
4866
+ const agent = this.agents.get(agentName);
4867
+ if (!agent)
4868
+ throw new OperatorActionError('unknown-agent', `Unknown agent: ${agentName}`);
4869
+ if (requestedIds.length === 0)
4870
+ throw new OperatorActionError('invalid', 'No message ids given');
4871
+ const cm = agent.getContextManager();
4872
+ const total = cm.getMessageCount();
4873
+ const located = locateMessageIndices(cm, new Set(requestedIds));
4874
+ const missing = requestedIds.filter((id) => !located.has(id));
4875
+ if (missing.length > 0) {
4876
+ throw new OperatorActionError('unknown-message', `Not on the active branch: ${missing.slice(0, 5).join(', ')}${missing.length > 5 ? ` (+${missing.length - 5})` : ''}`);
4877
+ }
4878
+ // Build removal intervals (index-ordered, newest first). A shard expands
4879
+ // to its whole body group — chronicle refuses to bisect one — and a
4880
+ // group interval is always removed as a RANGE: `removeMessage` refuses
4881
+ // any sharded message even when the group is down to one shard.
4882
+ const intervals = new Map();
4883
+ for (const id of requestedIds) {
4884
+ const run = bodyGroupRun(cm, located.get(id).index);
4885
+ intervals.set(`${run.from}-${run.to}`, run);
4886
+ }
4887
+ const ordered = [...intervals.values()].sort((a, b) => b.from - a.from);
4888
+ const messagesRemoved = ordered.reduce((n, iv) => n + (iv.to - iv.from + 1), 0);
4889
+ if (messagesRemoved >= total) {
4890
+ throw new OperatorActionError('invalid', `Cannot suppress all ${total} message(s) — at least one must remain`);
4891
+ }
4892
+ const targeted = ordered.flatMap((iv) => cm.getMessageWindow(iv.from, iv.to - iv.from + 1, { resolveBlobs: false }).messages);
4893
+ const removedIds = targeted.map((m) => String(m.id));
4894
+ const removedRefs = extractDiscordAwarenessRefs(targeted);
4895
+ const sourceBranch = this.store.currentBranch().name;
4896
+ const targetBranch = opts.branchName ?? `suppress/${agentName}/${Date.now()}`;
4897
+ if (targetBranch === sourceBranch) {
4898
+ throw new OperatorActionError('invalid', 'Suppression branch name must differ from the active branch');
4899
+ }
4900
+ // Gate + reserve the whole store (see reserveStoreForSurgery); held
4901
+ // until the fork is fully redacted or the source is restored.
4902
+ const release = this.reserveStoreForSurgery('suppress', agentName);
4903
+ let createdBranch;
4904
+ try {
4905
+ const markerBatch = this.discordAwarenessOutbox?.prepare({
4906
+ agentName,
4907
+ sourceBranch,
4908
+ targetBranch,
4909
+ refs: removedRefs,
4910
+ emoji: this.discordAwarenessEmoji,
4911
+ // Seeing targetBranch active does not prove the removals finished;
4912
+ // only this operation activates the batch, after the last redaction.
4913
+ activationPolicy: 'explicit',
4914
+ suppressionIntervals: ordered.map((iv) => ({ fromId: iv.fromId, toId: iv.toId })),
4915
+ }) ?? null;
4916
+ // Anything short of activation retires the batch: a prepared explicit
4917
+ // batch re-arms through preparedSuppressionsForBranch the moment its
4918
+ // target branch (or a descendant) becomes active — e.g. an operator
4919
+ // opening the failed fork to look — and an un-completable resume
4920
+ // there aborts framework start.
4921
+ const retireBatch = () => {
4922
+ if (markerBatch)
4923
+ this.discordAwarenessOutbox.discard(markerBatch.id);
4924
+ };
4925
+ try {
4926
+ createdBranch = await cm.fork(targetBranch);
4927
+ }
4928
+ catch (error) {
4929
+ // fork() switches the chronicle branch before awaiting strategy
4930
+ // initialization; a rejection can leave the store on the new
4931
+ // branch. Retire the batch and go back to the source.
4932
+ retireBatch();
4933
+ const restored = await this.restoreSourceBranch(cm, sourceBranch, targetBranch, agentName);
4934
+ throw new OperatorActionError('failed', `Suppression failed creating ${targetBranch}; ${restored}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
4935
+ }
4936
+ try {
4937
+ for (const iv of ordered) {
4938
+ if (iv.group || iv.fromId !== iv.toId)
4939
+ cm.removeMessages(iv.fromId, iv.toId);
4940
+ else
4941
+ cm.removeMessage(iv.fromId);
4942
+ }
4943
+ if (markerBatch)
4944
+ this.discordAwarenessOutbox.activate(markerBatch.id);
4945
+ }
4946
+ catch (error) {
4947
+ // A partially suppressed branch is not safe to serve from. Keep it
4948
+ // for diagnosis, put the agent back on the untouched source — and
4949
+ // say honestly whether that restore happened.
4950
+ retireBatch();
4951
+ const restored = await this.restoreSourceBranch(cm, sourceBranch, createdBranch, agentName);
4952
+ throw new OperatorActionError('failed', `Suppression failed on ${createdBranch}; ${restored}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
4953
+ }
4954
+ await this.syncDiscordAwarenessMarkers();
4955
+ this.materializeConfigMountAfterBranchSwitch();
4956
+ }
4957
+ finally {
4958
+ release();
4959
+ }
4960
+ console.error(`[operator] suppress agent=${agentName} removed=${messagesRemoved} branch=${createdBranch} ` +
4961
+ `by=${describeRequester(opts.requester)}`);
4962
+ const loggedIds = capIds(removedIds);
4963
+ this.recordOperatorAction({
4964
+ ...logBase,
4965
+ result: {
4966
+ sourceBranch,
4967
+ targetBranch: createdBranch,
4968
+ messagesRemoved,
4969
+ removedIds: loggedIds.ids,
4970
+ ...(loggedIds.truncated ? { removedIdsTruncated: true } : {}),
4971
+ discordRefs: removedRefs.length,
4972
+ },
4973
+ });
4974
+ return {
4975
+ agentName,
4976
+ sourceBranch,
4977
+ targetBranch: createdBranch,
4978
+ messagesRemoved,
4979
+ removedIds,
4980
+ removedRefs,
4981
+ lastVisible: await this.lastVisiblePreview(agentName),
4982
+ };
4983
+ }
4984
+ catch (error) {
4985
+ this.recordOperatorAction({ ...logBase, error: error instanceof Error ? error.message : String(error) });
4986
+ throw error;
4987
+ }
4988
+ }
4989
+ /** Re-materialize config files from the (new) active branch — fire and
4990
+ * forget; the gate picks changes up via mtime. Mirrors undoLastTurn. */
4991
+ materializeConfigMountAfterBranchSwitch() {
4992
+ const ws = this.moduleRegistry.getModule('workspace');
4993
+ if (ws && 'materializeMount' in ws) {
4994
+ ws
4995
+ .materializeMount('_config')
4996
+ .catch(() => { });
4997
+ }
4998
+ }
3456
4999
  /**
3457
5000
  * Check if undo/redo is available for an agent.
3458
5001
  */
@@ -3576,7 +5119,10 @@ export class AgentFramework {
3576
5119
  while (!this.queue.isEmpty ||
3577
5120
  // Direct inference requests (e.g. runEphemeralToCompletion) bypass the
3578
5121
  // event queue — without this the loop can exit before they're drained.
3579
- this.pendingRequests.length > 0 ||
5122
+ // While quiesced, gate-parked requests are NOT progress and must not
5123
+ // block idle (only a budget restart can still run); identity-equivalent
5124
+ // to `.length > 0` when not quiesced.
5125
+ this.pendingRequests.some((r) => !this.quiesced || isTurnContinuation(r.reason)) ||
3580
5126
  this.activeStreams.size > 0 ||
3581
5127
  Array.from(this.agents.values()).some((a) => a.state.status !== 'idle')) {
3582
5128
  await this.processNextEvent();
@@ -3805,7 +5351,7 @@ export class AgentFramework {
3805
5351
  const deferred = this.drainDeferredFor(agent.name);
3806
5352
  if (deferred.length > 0) {
3807
5353
  for (const msg of deferred) {
3808
- agent.getContextManager().addMessage(msg.participant, msg.content, msg.metadata);
5354
+ agent.getContextManager().addMessage(msg.participant, msg.content, withDeferredWriteId(msg.metadata, msg.id));
3809
5355
  // Injection guards: tool blocks would corrupt the tool-cycle
3810
5356
  // structure the membrane enforces, and a message named as the
3811
5357
  // agent itself would render as an ASSISTANT turn on the wire
@@ -3822,6 +5368,9 @@ export class AgentFramework {
3822
5368
  }
3823
5369
  }
3824
5370
  }
5371
+ // Deferred writes handed to the store above: sync, then drop them
5372
+ // from the durable recovery queue (no-op unless one is persisted).
5373
+ this.ackDeferredWrites();
3825
5374
  // A newly injected CONVERSATIONAL message begins a new
3826
5375
  // conversational round inside the same provider inference. Remember
3827
5376
  // that boundary for driveStream: an explicit send in the preceding
@@ -4717,16 +6266,60 @@ export class AgentFramework {
4717
6266
  // This mirrors how the restart has always overwritten activeStreams
4718
6267
  // rather than waiting for the old stream's teardown.
4719
6268
  const budgetRestart = requests.find((r) => r.reason === 'context_budget_restart');
6269
+ // Host quiesce (issue #122): park wakes instead of starting turns —
6270
+ // the requeue mirrors the busy path below, so requests survive and
6271
+ // fire at resume. A context-budget restart passes through: it
6272
+ // CONTINUES a turn whose token is held (see the deadlock note below),
6273
+ // and quiesce's drain is exactly the phase where such a turn must be
6274
+ // allowed to finish. Parked requests are coalesced per reason (newest
6275
+ // kept): heartbeat/gate wakes arrive once per tick and nothing else
6276
+ // bounds a multi-day maintenance window's accumulation.
6277
+ if (this.quiesced) {
6278
+ // Continuations of a held turn (budget restart, non-streaming tool
6279
+ // results) pass; everything else parks.
6280
+ const passThrough = requests.filter((r) => isTurnContinuation(r.reason));
6281
+ const parked = requests.filter((r) => !isTurnContinuation(r.reason));
6282
+ // Coalesce per (reason, addressed): every channel wake shares one
6283
+ // reason, so keying on reason alone would let an ambient message
6284
+ // parked later evict a DM/mention parked earlier — and the
6285
+ // addressed-outranks-ambient trigger selection below would then
6286
+ // route the resumed turn into the wrong channel.
6287
+ const newestByKey = new Map();
6288
+ for (const request of parked) {
6289
+ const key = `${request.reason}|${request.addressed ? 'a' : 'n'}`;
6290
+ const prev = newestByKey.get(key);
6291
+ if (!prev || request.timestamp >= prev.timestamp) {
6292
+ newestByKey.set(key, request);
6293
+ }
6294
+ }
6295
+ this.pendingRequests.push(...newestByKey.values());
6296
+ if (passThrough.length === 0)
6297
+ continue;
6298
+ // The continuation continues its held turn; its parked siblings must
6299
+ // NOT be consumed by that turn — they were re-parked above, so narrow
6300
+ // the batch to the continuation(s) alone before the trigger selection.
6301
+ requests.length = 0;
6302
+ requests.push(...passThrough);
6303
+ }
4720
6304
  const turnAlive = !budgetRestart && this.activeTurnTokens.has(agentName);
4721
6305
  const providerGate = this.providerGates?.get(agentName);
4722
6306
  // A primary can own provider admission while yielding to an already in-flight
4723
6307
  // auxiliary call. Keep this agent's later wakes queued, but do not block the
4724
6308
  // framework event loop or other residents while that auxiliary call settles.
4725
6309
  const providerPrimaryWaiting = (providerGate?.primaryDepth ?? 0) > 0 && !this.activeTurnTokens.has(agentName);
4726
- if (providerPrimaryWaiting || turnAlive || agent.state.status === 'inferring' || agent.state.status === 'streaming' || agent.state.status === 'waiting_for_tools') {
4727
- // Re-queue requests, but warn if they've been pending too long
6310
+ // A live surgery (rollback/suppress) holds the whole store: no turn may
6311
+ // start for ANY agent — including one registered after the reservation's
6312
+ // token snapshot — until the switch has landed or been rolled back.
6313
+ // Truthiness, not `!== null`: prototype-built harnesses (and any field
6314
+ // added later) leave the slot undefined, which must read as "not held".
6315
+ const surgeryHeld = !!this.surgeryHold;
6316
+ if (surgeryHeld || providerPrimaryWaiting || turnAlive || agent.state.status === 'inferring' || agent.state.status === 'streaming' || agent.state.status === 'waiting_for_tools') {
6317
+ // Re-queue requests, but warn if they've been pending too long.
6318
+ // Suppressed while quiesced: a drain-phase busy requeue is expected,
6319
+ // not a wedge tell.
4728
6320
  const oldest = Math.min(...requests.map(r => r.timestamp));
4729
- if (now - oldest > STALE_REQUEST_MS &&
6321
+ if (!this.quiesced &&
6322
+ now - oldest > STALE_REQUEST_MS &&
4730
6323
  (this.staleWarnAt.get(agentName) ?? 0) < now - 60_000) {
4731
6324
  // Trace and stderr share the throttle. The old code throttled only
4732
6325
  // stderr, so a long-running tool call generated one trace per poll
@@ -4737,9 +6330,11 @@ export class AgentFramework {
4737
6330
  // read 'idle' while a turn's teardown is pending, and a LEAKED turn
4738
6331
  // token would look exactly like this — permanently requeued wakes.
4739
6332
  // This line is the wedge's tell (idle+turn-alive, forever).
4740
- const shownStatus = turnAlive && agent.state.status === 'idle'
4741
- ? 'idle+turn-alive'
4742
- : agent.state.status;
6333
+ const shownStatus = surgeryHeld && agent.state.status === 'idle'
6334
+ ? `idle+surgery(${this.surgeryHold.verb})`
6335
+ : turnAlive && agent.state.status === 'idle'
6336
+ ? 'idle+turn-alive'
6337
+ : agent.state.status;
4743
6338
  this.emitTrace({
4744
6339
  type: 'inference:request_stale',
4745
6340
  agentName,
@@ -5282,6 +6877,20 @@ export class AgentFramework {
5282
6877
  console.error(`[provider-admission] ${agent.name}: turn-alive while parked — wake requeued, not started`);
5283
6878
  return;
5284
6879
  }
6880
+ // Same gap, second hazard (#122): the host may have QUIESCED while
6881
+ // this wake was parked. The scheduler's quiesce gate ran before the
6882
+ // park; starting here would bypass it and run a turn against a
6883
+ // context the operator believes frozen. Give admission back and
6884
+ // requeue — the scheduler re-parks it (coalesced) until resume.
6885
+ // Continuations of a held turn pass, as they do at the gate.
6886
+ if (this.quiesced && !isTurnContinuation(trigger?.reason ?? '')) {
6887
+ this.releasePrimaryProviderGate(agent.name);
6888
+ this.pendingRequests.push(trigger ?? {
6889
+ agentName: agent.name, reason: 'provider-admission:requeue', source: 'scheduler', timestamp: Date.now(),
6890
+ });
6891
+ console.error(`[provider-admission] ${agent.name}: host quiesced while parked — wake requeued, not started`);
6892
+ return;
6893
+ }
5285
6894
  await this.startAgentStream(agent, trigger, attempt, true);
5286
6895
  }).catch((error) => {
5287
6896
  this.releasePrimaryProviderGate(agent.name);
@@ -5356,7 +6965,7 @@ export class AgentFramework {
5356
6965
  // poison message — or a transient store-write failure — must not
5357
6966
  // abort the turn or drop the messages behind it in the queue.
5358
6967
  try {
5359
- const id = agent.getContextManager().addMessage(msg.participant, msg.content, msg.metadata);
6968
+ const id = agent.getContextManager().addMessage(msg.participant, msg.content, withDeferredWriteId(msg.metadata, msg.id));
5360
6969
  this.emitTrace({ type: 'message:added', messageId: id, source: 'deferred-flush:turn-start' });
5361
6970
  }
5362
6971
  catch (err) {
@@ -5364,6 +6973,7 @@ export class AgentFramework {
5364
6973
  `(participant=${msg.participant}):`, err);
5365
6974
  }
5366
6975
  }
6976
+ this.ackDeferredWrites();
5367
6977
  }
5368
6978
  }
5369
6979
  // Record turn checkpoint before inference (only on first attempt, not retries)
@@ -6419,6 +8029,40 @@ export class AgentFramework {
6419
8029
  break;
6420
8030
  this.abortAgentScript(agent.name, 'stream error');
6421
8031
  agent.reset();
8032
+ // A quiesce-abandoned cancel may surface as `error` instead of
8033
+ // `aborted` depending on how the stream implementation reports
8034
+ // the cancellation. Same contract as the aborted branch: settle
8035
+ // honestly, no errorPolicy retry (which would relaunch inference
8036
+ // mid-maintenance-window), no inference:exhausted (which feeds
8037
+ // the failure streak / hard-down / poison-history accounting).
8038
+ {
8039
+ const cancelKey = `${agent.name}:${myStreamId}`;
8040
+ if (this.frameworkCancelledStreams.get(cancelKey) === 'quiesce_abandoned') {
8041
+ this.frameworkCancelledStreams.delete(cancelKey);
8042
+ // Same terminal as the `aborted` twin: §10.5 lifecycle reads
8043
+ // 'aborted' (not 'completed'), and the gate is released only
8044
+ // for THIS agent instance — a name re-registered meanwhile
8045
+ // (ephemeral disposal, conversation replacement) owns its own
8046
+ // gate liveness. The inference-log terminal was already
8047
+ // written at the top of this case (`Stream error`).
8048
+ lifecyclePhase = 'aborted';
8049
+ this.settleAgent(agent.name, {
8050
+ stopReason: 'exhausted',
8051
+ speech: '',
8052
+ error: 'Turn abandoned by operator quiesce',
8053
+ });
8054
+ this.emitTrace({
8055
+ type: 'inference:aborted',
8056
+ agentName: agent.name,
8057
+ durationMs,
8058
+ reason: 'quiesce_abandoned',
8059
+ });
8060
+ if (this.agents.get(agent.name) === agent && agent.streamId === myStreamId) {
8061
+ this.eventGate?.onInferenceEnded(agent.name);
8062
+ }
8063
+ break;
8064
+ }
8065
+ }
6422
8066
  if (ownsProviderGate && this.holdProviderAcceleration(agent, err, trigger)) {
6423
8067
  lifecyclePhase = 'failed';
6424
8068
  if (this.agents.get(agent.name) === agent && agent.streamId === myStreamId)
@@ -6476,6 +8120,30 @@ export class AgentFramework {
6476
8120
  else if (this.agents.get(agent.name) === agent && agent.streamId === myStreamId) {
6477
8121
  this.eventGate?.onInferenceEnded(agent.name);
6478
8122
  }
8123
+ if (cancelKind === 'quiesce_abandoned') {
8124
+ // Operator-initiated cancel during a quiesce drain. Settle
8125
+ // the turn honestly — deliberately NO inference:exhausted:
8126
+ // that trace centrally drives the consecutive-failure
8127
+ // streak, hard-down ops alerts, and the poison-history
8128
+ // breaker, none of which an operator cancel represents.
8129
+ // inference:aborted carries the observability instead.
8130
+ if (agent.streamId === myStreamId) {
8131
+ this.abortAgentScript(agent.name, 'turn abandoned by quiesce');
8132
+ agent.reset();
8133
+ this.settleAgent(agent.name, {
8134
+ stopReason: 'exhausted',
8135
+ speech: '',
8136
+ error: 'Turn abandoned by operator quiesce',
8137
+ });
8138
+ }
8139
+ this.emitTrace({
8140
+ type: 'inference:aborted',
8141
+ agentName: agent.name,
8142
+ durationMs: Date.now() - startTime,
8143
+ reason: 'quiesce_abandoned',
8144
+ });
8145
+ return;
8146
+ }
6479
8147
  // endTurn IS a logical turn end — earlier rounds may have
6480
8148
  // live-routed prose (narrate → skip_reply is a real shape),
6481
8149
  // so settle the delivery chain and drop the receipt. A
@@ -6749,8 +8417,12 @@ export class AgentFramework {
6749
8417
  if (frameReachedTerminal && this.deferredMessages.length > 0 && this.pendingAssistantBlocks.size === 0) {
6750
8418
  const deferred = this.drainDeferredFor(agent.name);
6751
8419
  for (const msg of deferred) {
6752
- this.addMessage(msg.participant, msg.content, msg.metadata, msg.forAgent ? { forAgent: msg.forAgent } : undefined);
8420
+ this.addMessage(msg.participant, msg.content, msg.metadata, {
8421
+ deferredWriteId: msg.id,
8422
+ ...(msg.forAgent ? { forAgent: msg.forAgent } : {}),
8423
+ });
6753
8424
  }
8425
+ this.ackDeferredWrites();
6754
8426
  }
6755
8427
  }
6756
8428
  }
@@ -6816,6 +8488,9 @@ export class AgentFramework {
6816
8488
  // Idle AND no turn alive: status reads 'idle' from dequeue until the
6817
8489
  // stream registers, and again while a turn's teardown is pending
6818
8490
  // (the scheduler's own busy test, 'idle+turn-alive').
8491
+ if (this.surgeryHold) {
8492
+ throw new Error(`puppet refused: the store is under live ${this.surgeryHold.verb} for ${this.surgeryHold.agentName} — retry when it completes`);
8493
+ }
6819
8494
  if (agent.state.status !== 'idle' || this.activeTurnTokens.has(agentName)) {
6820
8495
  const shown = agent.state.status === 'idle' ? 'idle+turn-alive' : agent.state.status;
6821
8496
  throw new Error(`puppet refused: agent ${agentName} is ${shown} (requires idle — ` +
@@ -6881,7 +8556,7 @@ export class AgentFramework {
6881
8556
  cm.addMessage('user', blocks);
6882
8557
  }
6883
8558
  else {
6884
- this.deferredMessages.push({ participant: agentName, content: toolUse, forAgent: agentName }, { participant: 'user', content: blocks, forAgent: agentName });
8559
+ this.deferredMessages.push({ id: randomUUID(), seq: ++this.deferredSeq, participant: agentName, content: toolUse, forAgent: agentName }, { id: randomUUID(), seq: ++this.deferredSeq, participant: 'user', content: blocks, forAgent: agentName });
6885
8560
  }
6886
8561
  this.emitTrace({
6887
8562
  type: 'puppet:tool-call',
@@ -6909,8 +8584,12 @@ export class AgentFramework {
6909
8584
  if (!this.activeTurnTokens.has(agentName)
6910
8585
  && this.deferredMessages.length > 0 && this.pendingAssistantBlocks.size === 0) {
6911
8586
  for (const msg of this.drainDeferredFor(agentName)) {
6912
- this.addMessage(msg.participant, msg.content, msg.metadata, msg.forAgent ? { forAgent: msg.forAgent } : undefined);
8587
+ this.addMessage(msg.participant, msg.content, msg.metadata, {
8588
+ deferredWriteId: msg.id,
8589
+ ...(msg.forAgent ? { forAgent: msg.forAgent } : {}),
8590
+ });
6913
8591
  }
8592
+ this.ackDeferredWrites();
6914
8593
  }
6915
8594
  }
6916
8595
  }
@@ -8010,6 +9689,10 @@ export class AgentFramework {
8010
9689
  // their own wake), at the agent's next tool boundary (where they are
8011
9690
  // ALSO injected into the live stream — hear-while-acting), or in
8012
9691
  // driveStream's finally when the turn ends.
9692
+ // Quiesce (issue #122): the window exists to run compression/refold
9693
+ // against a FROZEN context — module events and api message.send must not
9694
+ // append mid-surgery. Deferred here, flushed by resume(). tool_result
9695
+ // still lands (a draining turn's continuation depends on it).
8013
9696
  const hasToolResult = content.some(b => b.type === 'tool_result');
8014
9697
  // Explicitly-targeted deliveries scope the tool-cycle check to the
8015
9698
  // target (pendingAssistantBlocks is keyed by agent); the default path
@@ -8019,13 +9702,25 @@ export class AgentFramework {
8019
9702
  ? this.pendingAssistantBlocks.has(agent.name)
8020
9703
  : this.pendingAssistantBlocks.size > 0;
8021
9704
  if (!hasToolResult &&
8022
- (midToolCycle ||
9705
+ (this.quiesced ||
9706
+ midToolCycle ||
8023
9707
  this.activeTurnTokens.has(agent.name) ||
8024
9708
  this.activeStreams.has(agent.name))) {
8025
- this.deferredMessages.push({ participant, content, metadata, forAgent: opts?.forAgent });
9709
+ // A re-deferral of a drained entry keeps its id and leaves the un-acked
9710
+ // set: one logical message, one durable identity — never two entries.
9711
+ const id = opts?.deferredWriteId ?? randomUUID();
9712
+ let seq;
9713
+ if (opts?.deferredWriteId) {
9714
+ const prior = this.unackedDeferredWrites.find((m) => m.id === id);
9715
+ seq = prior?.seq;
9716
+ this.unackedDeferredWrites = this.unackedDeferredWrites.filter((m) => m.id !== id);
9717
+ }
9718
+ this.deferredMessages.push({ id, seq: seq ?? ++this.deferredSeq, participant, content, metadata, forAgent: opts?.forAgent });
9719
+ if (this.quiesced || this.deferredWritesPersisted)
9720
+ this.persistDeferredWrites();
8026
9721
  return ''; // Deferred — flushed at the target's next boundary
8027
9722
  }
8028
- return agent.getContextManager().addMessage(participant, content, metadata);
9723
+ return agent.getContextManager().addMessage(participant, content, opts?.deferredWriteId ? withDeferredWriteId(metadata, opts.deferredWriteId) : metadata);
8029
9724
  }
8030
9725
  /**
8031
9726
  * Splice out the deferred messages addressed to `agentName` (explicitly
@@ -8042,6 +9737,12 @@ export class AgentFramework {
8042
9737
  (target === agentName ? mine : rest).push(msg);
8043
9738
  }
8044
9739
  this.deferredMessages = rest;
9740
+ mine.sort(bySeq);
9741
+ // The drained messages are about to be written by the caller; they stay
9742
+ // in the DURABLE queue (as un-acked) until the caller's
9743
+ // ackDeferredWrites() has synced the chronicle. Removing them here would
9744
+ // acknowledge before the write is durable.
9745
+ this.handOffDeferredWrites(mine);
8045
9746
  return mine;
8046
9747
  }
8047
9748
  editMessage(id, content) {
@@ -8850,9 +10551,22 @@ export class AgentFramework {
8850
10551
  for (const connection of this.mcplServerRegistry?.getAllServers() ?? []) {
8851
10552
  // ready() can synchronously flush a nested list-change notification that
8852
10553
  // installs a newer global generation. Never let this older completion
8853
- // release any remaining server behind that newer gate.
10554
+ // release any remaining server behind that newer gate. (The quiesce
10555
+ // check sits AFTER this guard: readyControlPlane can also synchronously
10556
+ // flush a control event that installs a newer barrier.)
8854
10557
  if (this.discordAwarenessBarrier !== null)
8855
10558
  return false;
10559
+ // Host quiesce (issue #122): this is the single point every barrier
10560
+ // completion and reconnect flow funnels through, so holding here keeps
10561
+ // data planes paused across awareness drains and reconnects for the
10562
+ // whole window. Control planes come up normally (host/command rides the
10563
+ // control plane, so resume stays deliverable); resume() performs the
10564
+ // real ready() flush through this same funnel once its feasibility gate
10565
+ // passes and the flag is cleared.
10566
+ if (this.quiesced) {
10567
+ connection.readyControlPlane();
10568
+ continue;
10569
+ }
8856
10570
  connection.ready();
8857
10571
  }
8858
10572
  return this.discordAwarenessBarrier === null;