@adhdev/daemon-core 1.0.18-rc.8 → 1.0.18

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 (67) hide show
  1. package/dist/cli-adapters/provider-cli-shared.d.ts +33 -0
  2. package/dist/commands/cli-manager.d.ts +9 -0
  3. package/dist/commands/upgrade-helper.d.ts +1 -0
  4. package/dist/commands/windows-atomic-upgrade.d.ts +5 -0
  5. package/dist/config/mesh-json-config.d.ts +62 -0
  6. package/dist/index.d.ts +4 -2
  7. package/dist/index.js +2309 -689
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.mjs +2277 -666
  10. package/dist/index.mjs.map +1 -1
  11. package/dist/mesh/mesh-active-work.d.ts +1 -1
  12. package/dist/mesh/mesh-ledger.d.ts +1 -1
  13. package/dist/mesh/mesh-node-identity.d.ts +23 -0
  14. package/dist/mesh/mesh-refine-gates.d.ts +89 -0
  15. package/dist/mesh/mesh-remote-event-pull.d.ts +3 -0
  16. package/dist/mesh/mesh-work-queue.d.ts +24 -1
  17. package/dist/providers/auto-approve-modes.d.ts +14 -0
  18. package/dist/providers/chat-message-normalization.d.ts +22 -0
  19. package/dist/providers/cli-provider-instance-types.d.ts +2 -0
  20. package/dist/providers/cli-provider-instance.d.ts +76 -0
  21. package/dist/providers/contracts.d.ts +17 -0
  22. package/dist/providers/provider-schema.d.ts +1 -0
  23. package/dist/providers/sdk/v1/builders/cli/detect-status.d.ts +14 -0
  24. package/dist/providers/types/interactive-prompt.d.ts +18 -0
  25. package/dist/repo-mesh-types.d.ts +38 -6
  26. package/dist/shared-types.d.ts +4 -2
  27. package/package.json +3 -3
  28. package/src/cli-adapters/cli-state-engine.ts +17 -1
  29. package/src/cli-adapters/provider-cli-adapter.ts +2 -6
  30. package/src/cli-adapters/provider-cli-shared.ts +41 -0
  31. package/src/commands/cli-manager.ts +72 -8
  32. package/src/commands/high-family/mesh-events.ts +13 -2
  33. package/src/commands/med-family/mesh-crud.ts +155 -0
  34. package/src/commands/med-family/mesh-queue.ts +74 -1
  35. package/src/commands/router-refine.ts +71 -4
  36. package/src/commands/router.ts +8 -0
  37. package/src/commands/upgrade-helper.ts +78 -15
  38. package/src/commands/windows-atomic-upgrade.ts +146 -38
  39. package/src/config/mesh-json-config.ts +103 -0
  40. package/src/index.ts +21 -1
  41. package/src/mesh/coordinator-prompt.ts +2 -1
  42. package/src/mesh/mesh-active-work.ts +11 -1
  43. package/src/mesh/mesh-completion-synthesis.ts +12 -1
  44. package/src/mesh/mesh-event-classify.ts +19 -0
  45. package/src/mesh/mesh-event-forwarding.ts +64 -6
  46. package/src/mesh/mesh-events-utils.ts +42 -0
  47. package/src/mesh/mesh-ledger.ts +5 -0
  48. package/src/mesh/mesh-node-identity.ts +49 -0
  49. package/src/mesh/mesh-queue-assignment.ts +210 -11
  50. package/src/mesh/mesh-reconcile-loop.ts +257 -3
  51. package/src/mesh/mesh-refine-gates.ts +300 -0
  52. package/src/mesh/mesh-remote-event-pull.ts +68 -47
  53. package/src/mesh/mesh-work-queue.ts +85 -2
  54. package/src/providers/auto-approve-modes.ts +97 -0
  55. package/src/providers/chat-message-normalization.ts +53 -0
  56. package/src/providers/cli-provider-instance-types.ts +28 -0
  57. package/src/providers/cli-provider-instance.ts +394 -28
  58. package/src/providers/contracts.ts +25 -1
  59. package/src/providers/provider-schema.ts +83 -0
  60. package/src/providers/sdk/v1/builders/cli/detect-status.ts +23 -0
  61. package/src/providers/sdk/v1/builders/cli/parse-approval.ts +20 -0
  62. package/src/providers/sdk/v1/schemas/cli/provider.schema.json +44 -0
  63. package/src/providers/types/interactive-prompt.ts +77 -0
  64. package/src/repo-mesh-types.ts +112 -12
  65. package/src/shared-types.ts +9 -1
  66. package/src/status/reporter.ts +1 -0
  67. package/src/status/snapshot.ts +2 -0
@@ -12,11 +12,12 @@ import * as fs from 'fs';
12
12
  import { normalizeInputEnvelope, type ProviderModule, flattenContent, type InputEnvelope } from './contracts.js';
13
13
  import { assertProviderSupportsDeclaredInput, getEffectiveMessageInputSupport } from './provider-input-support.js';
14
14
  import type { ProviderInstance, ProviderState, ProviderEvent, InstanceContext, ProviderErrorReason, HotChatSessionState, SessionModalState } from './provider-instance.js';
15
- import { normalizeInteractivePrompt, normalizeInteractivePromptResponse, type InteractivePrompt } from './types/interactive-prompt.js';
15
+ import { normalizeInteractivePrompt, normalizeInteractivePromptResponse, resolveInteractivePromptResponse, type InteractivePrompt } from './types/interactive-prompt.js';
16
16
  import { ProviderCliAdapter } from '../cli-adapters/provider-cli-adapter.js';
17
17
  import { shortHash } from '../system/hash.js';
18
18
  import type { CliProviderModule } from '../cli-adapters/provider-cli-adapter.js';
19
19
  import type { MeshSendKeyItem, MeshSendKeyName } from '../cli-adapters/provider-cli-shared.js';
20
+ import { isPurePtyTranscriptProvider } from '../cli-adapters/provider-cli-shared.js';
20
21
  import { createCliAdapter } from './spec/route.js';
21
22
  import type { PtyRuntimeMetadata, PtyTransportFactory } from '../cli-adapters/pty-transport.js';
22
23
  import { StatusMonitor } from './status-monitor.js';
@@ -56,6 +57,7 @@ import {
56
57
  STATUS_HYDRATION_TAIL_LIMIT,
57
58
  COMPLETED_FINALIZATION_RETRY_MS,
58
59
  COMPLETED_FINALIZATION_MAX_WAIT_MS,
60
+ CANON_C_MISSING_ASSISTANT_MIN_ELAPSED_MS,
59
61
  NATIVE_HISTORY_MESH_IDLE_SETTLE_MS,
60
62
  PTY_PARSED_FINAL_ASSISTANT_QUIET_DWELL_MS,
61
63
  ANTIGRAVITY_HOLD_QUIET_DWELL_MS,
@@ -73,6 +75,7 @@ import type {
73
75
  } from './cli-provider-instance-types.js';
74
76
  import { mergeConversationMessages, buildExternalTranscriptProbe } from './cli-provider-transcript-merge.js';
75
77
  import { getEffectDedupKey, formatApprovalRequestMessage, formatMarkerTimestamp } from './cli-provider-effect-format.js';
78
+ import { resolveProviderAutoApproveMode, type ResolvedAutoApproveMode } from './auto-approve-modes.js';
76
79
 
77
80
  // Re-export moved public symbols so existing importers (index.ts, tests) keep
78
81
  // their `./cli-provider-instance.js` path. Pure move — no behavior change.
@@ -331,6 +334,18 @@ export class CliProviderInstance implements ProviderInstance {
331
334
  // MESH_WORKER_STALL_REFIRE_COOLDOWN_MS between successive emissions across
332
335
  // anchor re-arms (the per-anchor guard only covers a single continuous stall).
333
336
  private meshStallLastFiredAt = -1;
337
+ // KIMI-MESH-COMPLETION-EMIT (axis 1): the native-source transcript progress
338
+ // fingerprint (record count + source mtime) observed the LAST time the stall
339
+ // watchdog checked a native-source provider (transcriptAuthority=provider — e.g.
340
+ // kimi wire.jsonl). A pure-PTY native-source worker running a long, screen-quiet
341
+ // tool (no viewport bytes for minutes) looks stalled by the lastOutputAt clock
342
+ // even though its transcript file is still growing. Before firing the stall, we
343
+ // compare the current transcript fingerprint against this snapshot; if the
344
+ // transcript advanced, the "stall" is a false positive of PTY-render stasis, so
345
+ // we re-arm the anchor instead of paging the coordinator (whose no_progress
346
+ // handling can lead to the worker being stopped mid-work → completion never
347
+ // emitted). null = not yet sampled for this session/anchor.
348
+ private meshStallNativeTranscriptSample: { msgCount: number; sourceMtimeMs: number } | null = null;
334
349
  // FALSE-IDLE continuity epoch: monotonically bumped on EVERY entry into a busy
335
350
  // phase (→generating or →waiting_approval). The completedDebouncePending snapshots
336
351
  // this value at arm time (busyEpochAtArm); the flush guard requires it UNCHANGED
@@ -376,10 +391,12 @@ export class CliProviderInstance implements ProviderInstance {
376
391
  // `waiting_choice` overlay in getState(); the raw adapter status stays idle/generating,
377
392
  // so detectStatusTransition()'s status-keyed arms never fire and no push-worthy
378
393
  // agent:* event is emitted — the owner misses the "ACTION REQUIRED" prompt when the
379
- // app is backgrounded. We emit one agent:waiting_approval on ENTRY into the prompt
380
- // state (reusing the existing server push path, no cloud change) and dedupe on this
381
- // key so repeated status ticks with the same prompt do not re-fire. '' means no
382
- // prompt is currently active (cleared when the prompt is answered/gone).
394
+ // app is backgrounded. We emit one agent:waiting_choice on ENTRY into the prompt
395
+ // state (its own coordinator event, NOT the approval channel a multi-choice
396
+ // question is answered with mesh_answer_question, never mesh_approve) carrying the
397
+ // FULL InteractivePrompt payload, and dedupe on this key so repeated status ticks
398
+ // with the same prompt do not re-fire. '' means no prompt is currently active
399
+ // (cleared when the prompt is answered/gone).
383
400
  private lastInteractivePromptEventKey = '';
384
401
  private autoApproveBusy = false;
385
402
  private autoApproveBusyTimer: NodeJS.Timeout | null = null;
@@ -1272,7 +1289,16 @@ export class CliProviderInstance implements ProviderInstance {
1272
1289
  }
1273
1290
  } else if (event === 'interactive_prompt_response' && data) {
1274
1291
  try {
1275
- const response = normalizeInteractivePromptResponse(data);
1292
+ // mesh_answer_question (mission f1d25e11) sends a coordinator-friendly answer
1293
+ // form (per-question select by label/index) that must be resolved against the
1294
+ // AUTHORITATIVE active prompt held here. When the active prompt is present and
1295
+ // the promptId matches, resolve it; otherwise fall back to the strict keyed
1296
+ // form (dashboard local answers already send that shape).
1297
+ const response = (this.activeInteractivePrompt
1298
+ && this.activeInteractivePrompt.promptId === (data as { promptId?: unknown })?.promptId
1299
+ && Array.isArray((data as { answers?: unknown })?.answers))
1300
+ ? resolveInteractivePromptResponse(this.activeInteractivePrompt, data)
1301
+ : normalizeInteractivePromptResponse(data);
1276
1302
  if (this.activeInteractivePrompt?.promptId === response.promptId) {
1277
1303
  this.activeInteractivePrompt = null;
1278
1304
  }
@@ -1406,6 +1432,15 @@ export class CliProviderInstance implements ProviderInstance {
1406
1432
  */
1407
1433
  private lastCompletionSummary: { content: string; receivedAt: number; sourceTimestampMs?: number } | null = null;
1408
1434
 
1435
+ // KIMI-MESH-COMPLETION-EMIT (axis 2, double-emit guard): the (taskId, wall-clock)
1436
+ // of the most recent agent:generating_completed this instance emitted, stamped by
1437
+ // emitGeneratingCompleted. The pre-cleanup completion flush
1438
+ // (flushMeshCompletionBeforeCleanup, driven by cli-manager's exit monitor) reads
1439
+ // this to refuse a SECOND completion for a turn whose completion already fired —
1440
+ // so a worker that finished cleanly and is simply being auto-cleaned never emits a
1441
+ // duplicate. taskId '' covers an ad-hoc (no-task) turn. null = none emitted yet.
1442
+ private lastEmittedCompletion: { taskId: string; at: number } | null = null;
1443
+
1409
1444
  private async enforceFreshSessionLaunchIfNeeded(): Promise<void> {
1410
1445
  const scriptName = getForcedNewSessionScriptName(this.provider, this.launchMode);
1411
1446
  if (!scriptName) return;
@@ -2084,6 +2119,11 @@ export class CliProviderInstance implements ProviderInstance {
2084
2119
  reason: 'missing_final_assistant',
2085
2120
  terminal: (this.provider as any).requiresFinalAssistantBeforeIdle === true,
2086
2121
  allowTimeout: allowMissingAssistantTimeout,
2122
+ // PTY-parsed provider (codex-cli): no external transcript trails the idle
2123
+ // transition, so the CANON-C decoupled emit must observe the min-elapsed floor
2124
+ // rather than fire at the first-poll waitedMs. (claude-cli's external-native
2125
+ // branch above is exempt — its transcript legitimately lands moments later.)
2126
+ noExternalTranscriptSource: true,
2087
2127
  };
2088
2128
  }
2089
2129
  }
@@ -2387,6 +2427,52 @@ export class CliProviderInstance implements ProviderInstance {
2387
2427
  const stalledMs = now - this.meshStallAnchorAt;
2388
2428
  if (stalledMs < threshold) return;
2389
2429
 
2430
+ // KIMI-MESH-COMPLETION-EMIT (axis 1): transcript-aware stall for native-source
2431
+ // providers. A pure-PTY native-source worker (transcriptAuthority=provider,
2432
+ // e.g. kimi) running a long, screen-quiet tool emits no viewport bytes for
2433
+ // minutes, so the lastOutputAt clock above reads it as stalled — but its
2434
+ // authoritative transcript (wire.jsonl) is still being appended. Firing
2435
+ // monitor:no_progress here surfaces a false stall to the coordinator whose
2436
+ // downstream handling can stop the worker mid-work, killing the emit window
2437
+ // before the real completion propagates. So before firing, sample the native
2438
+ // transcript's progress fingerprint: if it advanced (more records OR a fresher
2439
+ // source mtime) since the last sample, the worker is demonstrably alive — treat
2440
+ // it as progress. Re-arm the anchor to `now` and reset the episode so the clock
2441
+ // restarts from this proven-live moment; only a transcript that is ALSO static
2442
+ // falls through to the genuine-stall fire below. No-op for pure-PTY providers
2443
+ // (sample is null → unchanged behavior).
2444
+ const nativeSample = this.sampleNativeTranscriptProgress();
2445
+ if (nativeSample) {
2446
+ const prev = this.meshStallNativeTranscriptSample;
2447
+ const advanced = !prev
2448
+ || nativeSample.msgCount > prev.msgCount
2449
+ || nativeSample.sourceMtimeMs > prev.sourceMtimeMs;
2450
+ this.meshStallNativeTranscriptSample = nativeSample;
2451
+ if (advanced) {
2452
+ if (this.isMeshWorkerSession()) {
2453
+ traceMeshEventDrop('mesh_worker_stall_transcript_advancing', this.meshTraceCtx('monitor:no_progress'),
2454
+ `msgCount=${nativeSample.msgCount} sourceMtime=${nativeSample.sourceMtimeMs} (PTY quiet ${Math.round(stalledMs / 1000)}s but transcript advancing)`);
2455
+ }
2456
+ this.meshStallAnchorAt = now;
2457
+ this.meshStallEmittedForAnchor = false;
2458
+ return;
2459
+ }
2460
+ }
2461
+
2462
+ // KIMI-PURE-PTY-COMPLETION-EMIT (Fix 3): before firing a false stall, check the
2463
+ // pure-PTY class (no native transcript → nativeSample was null above). A finished
2464
+ // pure-PTY worker sits at a static idle prompt with an in-turn final assistant
2465
+ // message but never emitted generating_completed — the status-agnostic watchdog
2466
+ // would misread that quiet as a wedge. If the PTY transcript proves a finished
2467
+ // turn, emit the missing completion and SUPPRESS the stall (mark the anchor
2468
+ // emitted so this static idle is not re-checked every tick). No-op for
2469
+ // native-source providers and for a genuinely mid-turn / wedged worker with no
2470
+ // final assistant → the real stall fires below unchanged.
2471
+ if (this.tryReconcilePurePtyCompletionForStall(observedStatus)) {
2472
+ this.meshStallEmittedForAnchor = true;
2473
+ return;
2474
+ }
2475
+
2390
2476
  // FALSE-STALL-WATCHDOG-OVERFIRE (fix E): per-session refire cooldown. Mark
2391
2477
  // this anchor emitted regardless so a still-static anchor does not re-check
2392
2478
  // every tick, but suppress the actual coordinator notification (and the
@@ -2437,6 +2523,193 @@ export class CliProviderInstance implements ProviderInstance {
2437
2523
  this.meshStallEmittedForAnchor = false;
2438
2524
  this.meshStallTurnActiveLast = undefined;
2439
2525
  this.meshStallLastFiredAt = -1;
2526
+ this.meshStallNativeTranscriptSample = null;
2527
+ }
2528
+
2529
+ /**
2530
+ * KIMI-MESH-COMPLETION-EMIT (axis 1). For a native-source provider
2531
+ * (transcriptAuthority=provider — its authoritative history is an on-disk
2532
+ * transcript file, e.g. kimi's wire.jsonl), sample the transcript's current
2533
+ * progress fingerprint (record count + source-file mtime) WITHOUT parsing the
2534
+ * PTY. Used by the stall watchdog to distinguish "PTY render is quiet but the
2535
+ * worker is still doing long tool work (transcript growing)" from a genuine
2536
+ * wedge. Returns null for a pure-PTY provider (no native source) or when the
2537
+ * source cannot be resolved this tick — the caller then falls back to the
2538
+ * unchanged lastOutputAt-only judgment.
2539
+ *
2540
+ * Generalized on the provider's native-source flag, NOT a hardcoded provider
2541
+ * type, so every current and future pure-PTY long-tool native-source provider
2542
+ * benefits. Cheap enough for the stall path: it runs only at the stall threshold
2543
+ * (≥180s of PTY stasis), never on the routine 5s tick.
2544
+ */
2545
+ private sampleNativeTranscriptProgress(): { msgCount: number; sourceMtimeMs: number } | null {
2546
+ if (!isNativeSourceCanonicalHistory(this.provider?.nativeHistory)) return null;
2547
+ // readExternalCompletionMessages resolves this session's OWN native-source
2548
+ // conversation (providerSessionId / persisted pin / floor claim) and, as a
2549
+ // side effect, refreshes this.lastExternalCompletionProbe with the source
2550
+ // path, mtime, and message count. Reusing it keeps the resolution logic in
2551
+ // one place and immune to the antigravity-style session-id quirks.
2552
+ let messages: unknown[] | null = null;
2553
+ try {
2554
+ messages = this.readExternalCompletionMessages();
2555
+ } catch {
2556
+ return null; // best-effort: an unresolved transcript → fall back to PTY clock
2557
+ }
2558
+ const probe = this.lastExternalCompletionProbe;
2559
+ if (!probe) return null;
2560
+ return {
2561
+ msgCount: typeof probe.msgCount === 'number' ? probe.msgCount : (Array.isArray(messages) ? messages.length : 0),
2562
+ sourceMtimeMs: typeof probe.sourceMtimeMs === 'number' ? probe.sourceMtimeMs : 0,
2563
+ };
2564
+ }
2565
+
2566
+ /**
2567
+ * KIMI-MESH-COMPLETION-EMIT (axis 2). Last-chance completion emit for a mesh
2568
+ * DELEGATED worker whose PTY has already exited (e.g. killed by a false stall)
2569
+ * and is about to be auto-cleaned (cli-manager.startCliExitMonitor →
2570
+ * removeInstance closes the event-emit window forever). If the worker actually
2571
+ * FINISHED its assigned turn — its authoritative native transcript holds a final
2572
+ * assistant message for the injected task — but the completion event never fired
2573
+ * (the stall-kill happened before the FSM's idle transition), emit it now so the
2574
+ * coordinator learns the task completed instead of waiting ~180s for the reconcile
2575
+ * transcript-poll to reclaim it.
2576
+ *
2577
+ * Scope & guards (must all hold to emit):
2578
+ * • mesh worker session only — a normal standalone session's ordinary exit is
2579
+ * never synthesized (isMeshWorkerSession()).
2580
+ * • DOUBLE-EMIT guard — refuse if this turn's completion already fired
2581
+ * (lastEmittedCompletion matches the current taskId). A worker that completed
2582
+ * cleanly and is merely being cleaned up never double-emits.
2583
+ * • evidence gate — reuse the same final-assistant/turn-scoped summary machinery
2584
+ * as the normal completion path (completionFinalSummary over the native
2585
+ * transcript). No in-turn assistant summary ⇒ no proof of completion ⇒ leave
2586
+ * the reclaim path to handle a genuinely-unfinished worker.
2587
+ *
2588
+ * Returns true when a synthetic completion was emitted, false otherwise. Safe to
2589
+ * call unconditionally from the cleanup path.
2590
+ */
2591
+ flushMeshCompletionBeforeCleanup(): boolean {
2592
+ if (!this.isMeshWorkerSession()) return false;
2593
+ const taskId = this.completingTurnTaskId();
2594
+
2595
+ // DOUBLE-EMIT guard: this turn's completion already fired — never re-emit.
2596
+ if (this.lastEmittedCompletion && this.lastEmittedCompletion.taskId === (taskId ?? '')) {
2597
+ return false;
2598
+ }
2599
+
2600
+ // Evidence gate: the injected task's own turn must have genuinely started
2601
+ // (guards against synthesizing a completion off a reused session's stale tail
2602
+ // before this task ran) and the native transcript must hold an in-turn final
2603
+ // assistant summary. turnStartedAt anchors the turn-scoped read.
2604
+ if (!this.injectedTaskHasStartedGenerating()) return false;
2605
+ const turnStartedAt = typeof (this.adapter as any)?.currentTurnStartedAt === 'number'
2606
+ ? (this.adapter as any).currentTurnStartedAt as number
2607
+ : this.meshTaskInjectedAt || undefined;
2608
+ let parsedMessages: unknown;
2609
+ try {
2610
+ parsedMessages = this.adapter?.getScriptParsedStatus?.()?.messages;
2611
+ } catch { parsedMessages = undefined; }
2612
+ let finalSummary: string | undefined;
2613
+ try {
2614
+ finalSummary = this.completionFinalSummary(parsedMessages, turnStartedAt);
2615
+ } catch { finalSummary = undefined; }
2616
+ // No in-turn final assistant evidence → not a proven turn-end. Defer to the
2617
+ // coordinator's reclaim/reconcile path rather than fabricate a completion.
2618
+ if (!finalSummary) return false;
2619
+
2620
+ LOG.warn('CLI', `[${this.type}] emitting pre-cleanup mesh completion for session ${this.instanceId} `
2621
+ + `task=${taskId ?? '(none)'} — PTY exited before the completion event fired but the native transcript `
2622
+ + `shows a finished turn; synthesizing completion so the coordinator is not left waiting for reclaim.`);
2623
+ if (this.isMeshWorkerSession()) {
2624
+ traceMeshEventStage('fired', this.meshTraceCtx(), 'pre_cleanup_transcript_completion');
2625
+ }
2626
+ this.emitGeneratingCompleted({
2627
+ chatTitle: '',
2628
+ duration: undefined,
2629
+ timestamp: Date.now(),
2630
+ taskId,
2631
+ finalSummary,
2632
+ evidenceLevel: 'transcript',
2633
+ completionDiagnostic: { source: 'pre_cleanup_transcript_completion' },
2634
+ });
2635
+ return true;
2636
+ }
2637
+
2638
+ /**
2639
+ * KIMI-PURE-PTY-COMPLETION-EMIT (Fix 3): stall-path completion reconcile for the
2640
+ * PURE-PTY transcript class (kimi and kin — no native transcript, no provider
2641
+ * authority; see isPurePtyTranscriptProvider). Such a worker whose
2642
+ * generating_completed never emitted (the onTurnStarted idle→idle collapse Fix 1
2643
+ * fixes at the source) sits at a STATIC idle prompt: its PTY output stops the
2644
+ * instant the answer is rendered, so the status-agnostic no-progress watchdog
2645
+ * (checkMeshWorkerStall) reads the finished-but-quiet session as a stall and would
2646
+ * false-fire monitor:no_progress. The native-transcript reconcile
2647
+ * (sampleNativeTranscriptProgress) does NOT cover this class (no native source →
2648
+ * null sample), so the stall path needs its own guard.
2649
+ *
2650
+ * Called from checkMeshWorkerStall just before the fire. Returns true when the
2651
+ * session is a finished pure-PTY turn — idle, no pending response, and a PTY-parsed
2652
+ * in-turn final assistant summary — in which case it emits the missing
2653
+ * generating_completed (idempotent: a real/late emit writes the terminal ledger and
2654
+ * the coordinator's reconcile makes any duplicate a no-op) and the caller SUPPRESSES
2655
+ * the stall. Returns false for every other class/state (native-source provider, a
2656
+ * genuinely mid-turn or wedged worker with no final assistant), so a real stall still
2657
+ * fires unchanged.
2658
+ *
2659
+ * Evidence bar is identical to flushMeshCompletionBeforeCleanup: injected task's turn
2660
+ * genuinely started + an in-turn final assistant summary. Conservative by
2661
+ * construction — no summary ⇒ no proof of completion ⇒ return false and let the real
2662
+ * stall fire.
2663
+ */
2664
+ private tryReconcilePurePtyCompletionForStall(observedStatus: string): boolean {
2665
+ // Only the pure-PTY class — native-source providers are handled by
2666
+ // sampleNativeTranscriptProgress above.
2667
+ if (!isPurePtyTranscriptProvider(this.provider)) return false;
2668
+ // A finished turn is idle with nothing pending. A generating/waiting session is
2669
+ // mid-turn (the real stall path should evaluate it), so never complete it here.
2670
+ if (observedStatus !== 'idle') return false;
2671
+ if (this.hasAdapterPendingResponse()) return false;
2672
+
2673
+ const taskId = this.completingTurnTaskId();
2674
+ // DOUBLE-EMIT guard: this turn's completion already fired — never re-emit.
2675
+ if (this.lastEmittedCompletion && this.lastEmittedCompletion.taskId === (taskId ?? '')) {
2676
+ return false;
2677
+ }
2678
+ // The injected task's own turn must have genuinely started (guards against a
2679
+ // reused session's stale tail before this task ran).
2680
+ if (!this.injectedTaskHasStartedGenerating()) return false;
2681
+
2682
+ const turnStartedAt = typeof (this.adapter as any)?.currentTurnStartedAt === 'number'
2683
+ ? (this.adapter as any).currentTurnStartedAt as number
2684
+ : this.meshTaskInjectedAt || undefined;
2685
+ let parsedMessages: unknown;
2686
+ try {
2687
+ parsedMessages = this.adapter?.getScriptParsedStatus?.()?.messages;
2688
+ } catch { parsedMessages = undefined; }
2689
+ let finalSummary: string | undefined;
2690
+ try {
2691
+ finalSummary = this.completionFinalSummary(parsedMessages, turnStartedAt);
2692
+ } catch { finalSummary = undefined; }
2693
+ // No in-turn final assistant evidence → not a proven turn-end. Let the real
2694
+ // stall fire so a genuinely-wedged worker is still surfaced.
2695
+ if (!finalSummary) return false;
2696
+
2697
+ LOG.warn('CLI', `[${this.type}] reconciling pure-PTY mesh completion from the stall path for session ${this.instanceId} `
2698
+ + `task=${taskId ?? '(none)'} — PTY is idle-quiet with an in-turn final assistant message but the completion `
2699
+ + `event never fired (pure-PTY provider); emitting it instead of a false monitor:no_progress.`);
2700
+ if (this.isMeshWorkerSession()) {
2701
+ traceMeshEventStage('fired', this.meshTraceCtx(), 'stall_pure_pty_transcript_completion');
2702
+ }
2703
+ this.emitGeneratingCompleted({
2704
+ chatTitle: '',
2705
+ duration: undefined,
2706
+ timestamp: Date.now(),
2707
+ taskId,
2708
+ finalSummary,
2709
+ evidenceLevel: 'transcript',
2710
+ completionDiagnostic: { source: 'stall_pure_pty_transcript_completion' },
2711
+ });
2712
+ return true;
2440
2713
  }
2441
2714
 
2442
2715
  /**
@@ -2508,7 +2781,7 @@ export class CliProviderInstance implements ProviderInstance {
2508
2781
  * resolveModal, so a plain turn that never saw an approval always returns false.
2509
2782
  */
2510
2783
  private inApprovalResumeGrace(now = Date.now()): boolean {
2511
- if (!this.isAutonomousMeshSession() || !this.shouldAutoApprove()) return false;
2784
+ if (!this.isAutonomousMeshSession() || !this.shouldUsePtyAutoApprove()) return false;
2512
2785
  const resolvedAt = typeof (this.adapter as any)?.lastApprovalResolvedAt === 'number'
2513
2786
  ? (this.adapter as any).lastApprovalResolvedAt as number
2514
2787
  : 0;
@@ -2625,7 +2898,7 @@ export class CliProviderInstance implements ProviderInstance {
2625
2898
  }
2626
2899
 
2627
2900
  const latestStatus = this.adapter.getStatus({ allowParse: false });
2628
- const latestAutoApproveActive = latestStatus.status === 'waiting_approval' && this.shouldAutoApprove();
2901
+ const latestAutoApproveActive = latestStatus.status === 'waiting_approval' && this.shouldUsePtyAutoApprove();
2629
2902
  const latestVisibleStatus = latestAutoApproveActive || this.autoApproveBusy ? 'generating' : latestStatus.status;
2630
2903
  LOG.debug('CLI', `[${this.type}] flush attempt: adapterStatus=${latestStatus.status} latestVisible=${latestVisibleStatus} generatingStartedAt=${this.generatingStartedAt} isWaitingForResponse=${!!(this.adapter as any)?.isWaitingForResponse} hasPartial=${!!this.adapter.getPartialResponse?.()}`);
2631
2904
  if (latestVisibleStatus !== 'idle') {
@@ -2790,6 +3063,43 @@ export class CliProviderInstance implements ProviderInstance {
2790
3063
  this.scheduleCompletedDebounceFlush(COMPLETED_FINALIZATION_RETRY_MS);
2791
3064
  return;
2792
3065
  }
3066
+ // (CANON-C EARLY-EMIT FLOOR) The transcript-evidence gate would otherwise fire its
3067
+ // decoupled-immediate completion at ANY waitedMs — including the ~13s a codex PLAIN
3068
+ // terminal `missing_final_assistant` block reaches on the first completion-gate poll,
3069
+ // with NO final assistant ever observed. That stamps a weak evidenceLevel=insufficient
3070
+ // completion the coordinator marks reviewRecommended but does not auto-hold, racing the
3071
+ // transcript before it can land and pre-empting the 180s mesh-worker stall watchdog.
3072
+ // Impose a minimum dwell — but ONLY for the no-external-transcript case
3073
+ // (noExternalTranscriptSource): a PTY-parsed provider has no separate transcript that
3074
+ // will land to upgrade the weak emit, so firing immediately is a pure timing guess. A
3075
+ // native-source block (claude-cli external-native) is deliberately NOT floored: its
3076
+ // transcript legitimately trails the idle transition by a write and the CANON-C
3077
+ // immediate emit is correct (upgraded by the reconcile). Keep holding (re-probe each
3078
+ // retry — the block clears for a GENUINE emit the moment the assistant turn lands) until
3079
+ // either the floor is met or the 30s cap force-releases the weak completion below.
3080
+ // Scoped to mesh worker sessions (the only place allowTimeout is set), so interactive
3081
+ // completion timing is untouched.
3082
+ if (isTranscriptEvidenceGate
3083
+ && blockReason === 'missing_final_assistant'
3084
+ && block.noExternalTranscriptSource === true
3085
+ && waitedMs < CANON_C_MISSING_ASSISTANT_MIN_ELAPSED_MS) {
3086
+ if (pending.loggedBlockReason !== 'canon_c_min_elapsed_floor') {
3087
+ LOG.info('CLI', `[${this.type}] holding CANON-C decoupled emit until min-elapsed floor (waitedMs=${waitedMs} floor=${CANON_C_MISSING_ASSISTANT_MIN_ELAPSED_MS}); no final assistant yet (${blockReason})`);
3088
+ if (this.isMeshWorkerSession()) {
3089
+ traceMeshEventDrop('completion_gate_hold', this.meshTraceCtx(), `canon_c_min_elapsed_floor waited=${waitedMs}ms`);
3090
+ }
3091
+ if (this.completionTraceOn()) this.recordCompletionGateTrace('hold', {
3092
+ blockReason,
3093
+ latestVisibleStatus,
3094
+ terminal: block.terminal === true,
3095
+ canonCMinElapsedFloor: true,
3096
+ waitedMs,
3097
+ });
3098
+ pending.loggedBlockReason = 'canon_c_min_elapsed_floor';
3099
+ }
3100
+ this.scheduleCompletedDebounceFlush(COMPLETED_FINALIZATION_RETRY_MS);
3101
+ return;
3102
+ }
2793
3103
  // (ANTIGRAVITY-30S-CAP-PREMATURE) The 30s cap has been reached for an antigravity
2794
3104
  // `holdForTranscript` block. The cap releases on ELAPSED TIME, not proof-of-idle — but
2795
3105
  // antigravity is native-source (assistant answer in native-history) with a PTY-derived
@@ -2943,6 +3253,10 @@ export class CliProviderInstance implements ProviderInstance {
2943
3253
  if (summary) {
2944
3254
  this.lastCompletionSummary = { content: summary, receivedAt: opts.timestamp };
2945
3255
  }
3256
+ // KIMI-MESH-COMPLETION-EMIT (axis 2, double-emit guard): record that THIS turn's
3257
+ // completion has now been emitted, keyed by its taskId, so the pre-cleanup
3258
+ // completion flush never fires a duplicate for the same turn.
3259
+ this.lastEmittedCompletion = { taskId: typeof opts.taskId === 'string' ? opts.taskId : '', at: Date.now() };
2946
3260
  this.pushEvent({
2947
3261
  event: 'agent:generating_completed',
2948
3262
  chatTitle: opts.chatTitle,
@@ -2987,7 +3301,7 @@ export class CliProviderInstance implements ProviderInstance {
2987
3301
  private stabilizeFlappingApprovalStatus(adapterStatus: any, now = Date.now()): any {
2988
3302
  // Only autonomous auto-approving mesh sessions are subject to the delegated flap;
2989
3303
  // never overlay for attended/foreground/non-mesh sessions.
2990
- if (!this.isAutonomousMeshSession() || !this.shouldAutoApprove()) return adapterStatus;
3304
+ if (!this.isAutonomousMeshSession() || !this.shouldUsePtyAutoApprove()) return adapterStatus;
2991
3305
 
2992
3306
  const rawStatus = adapterStatus?.status;
2993
3307
  const resolvedAt = typeof (this.adapter as any)?.lastApprovalResolvedAt === 'number'
@@ -3034,6 +3348,13 @@ export class CliProviderInstance implements ProviderInstance {
3034
3348
  }
3035
3349
 
3036
3350
  private maybeAutoApproveStatus(adapterStatus: any, now = Date.now()): boolean {
3351
+ // launch-args modes grant approval in the CLI process itself and therefore
3352
+ // must never enter the PTY modal parser/fire/settle/mask/nudge subsystem.
3353
+ // post-boot-command is reserved and resolves inactive in v1.
3354
+ if (!this.shouldUsePtyAutoApprove()) {
3355
+ this.resetPtyAutoApproveState();
3356
+ return false;
3357
+ }
3037
3358
  // Manual-attendance suppression (provider-common): when a human is
3038
3359
  // Manual-attendance suppression (provider-common): when a human is
3039
3360
  // actively driving this session from the dashboard, hold auto-approve so
@@ -3045,7 +3366,7 @@ export class CliProviderInstance implements ProviderInstance {
3045
3366
  // would otherwise never re-drive this decision. Background mesh workers
3046
3367
  // are never attended, so their delegated auto-approve is untouched.
3047
3368
  if (adapterStatus?.status === 'waiting_approval'
3048
- && this.shouldAutoApprove()
3369
+ && this.shouldUsePtyAutoApprove()
3049
3370
  && this.manualAttendance.isAttended(now)) {
3050
3371
  this.lastAutoApprovalSignature = '';
3051
3372
  this.pendingAutoApprovalSignature = '';
@@ -3063,7 +3384,7 @@ export class CliProviderInstance implements ProviderInstance {
3063
3384
  }, this.manualAttendance.remainingMs(now) + 20);
3064
3385
  return false;
3065
3386
  }
3066
- const autoApproveActive = adapterStatus?.status === 'waiting_approval' && this.shouldAutoApprove();
3387
+ const autoApproveActive = adapterStatus?.status === 'waiting_approval' && this.shouldUsePtyAutoApprove();
3067
3388
  // Guard re-entry: onStatusChange/getState can observe the same modal multiple
3068
3389
  // times while the PTY absorbs the approval key. Without this flag, repeated
3069
3390
  // snapshots would write stray keys into the input once the modal dismisses.
@@ -3960,24 +4281,38 @@ export class CliProviderInstance implements ProviderInstance {
3960
4281
  this.lastStatus = newStatus;
3961
4282
  }
3962
4283
 
3963
- // INTERACTIVE-PROMPT-PUSH: fire a push-worthy notification when the session
3964
- // ENTERS an AskUserQuestion / waiting_choice state. This is orthogonal to the
3965
- // status-change block above: an AskUserQuestion prompt is surfaced only as a
4284
+ // INTERACTIVE-QUESTION-EMIT: fire a coordinator/push-worthy notification when the
4285
+ // session ENTERS an AskUserQuestion / waiting_choice state. This is orthogonal to
4286
+ // the status-change block above: an AskUserQuestion prompt is surfaced only as a
3966
4287
  // display-only `waiting_choice` overlay in getState() (mirrored here off
3967
4288
  // adapterStatus.activeInteractivePrompt, the exact signal getState uses), while
3968
4289
  // the raw adapter status stays idle/generating — so none of the status-keyed
3969
4290
  // arms above ever emit an agent:* event for it and the owner gets no web-push.
3970
4291
  //
3971
- // We REUSE agent:waiting_approval (the question message as modalMessage, the
3972
- // choice labels as modalButtons) so the existing server push path fires with
3973
- // zero cloud change the semantics ("the agent needs your input") match.
4292
+ // MESH-QUESTION-ANSWER-PATH (mission f1d25e11): this used to REUSE
4293
+ // agent:waiting_approval (question as modalMessage, choice labels as modalButtons).
4294
+ // That mis-classified a multi-choice QUESTION as an approval the coordinator saw
4295
+ // a task_approval_needed ledger row and tried mesh_approve, which cannot answer a
4296
+ // question (it resolves a yes/no modal). We now emit a DISTINCT
4297
+ // agent:waiting_choice event carrying the FULL InteractivePrompt payload
4298
+ // (promptId + every question's header/question/multiSelect and each option's
4299
+ // label/description) so the coordinator can render the choices and answer with the
4300
+ // dedicated mesh_answer_question tool. The rich payload survives the cross-machine
4301
+ // relay (see buildRelayMetadataEvent) so a REMOTE worker's question reaches the
4302
+ // coordinator with its options intact.
4303
+ //
4304
+ // We keep modalMessage/modalButtons on the event too so the existing server
4305
+ // web-push path (which reads those two fields) still fires with no cloud change —
4306
+ // but the payload's `interactivePrompt` is the authoritative, structured signal.
3974
4307
  //
3975
4308
  // Edge-triggered: emit exactly once on entry, keyed on promptId + question text,
3976
4309
  // and reset the key when the prompt clears so a fresh prompt re-fires and a later
3977
4310
  // real completion still flows through the idle arm normally. We do NOT emit when
3978
4311
  // the session is ALSO in a genuine approval state (newStatus === 'waiting_approval'):
3979
- // that arm already emits agent:waiting_approval, and firing here too would double
3980
- // up on the same modal.
4312
+ // that arm already emits agent:waiting_approval. A question (waiting_choice) and a
4313
+ // real approval (waiting_approval) therefore NEVER both fire for the same tick —
4314
+ // the approval arm wins and this arm yields (owner requirement: the two are
4315
+ // mutually exclusive).
3981
4316
  const interactivePrompt = adapterStatus.activeInteractivePrompt ?? null;
3982
4317
  if (interactivePrompt && newStatus !== 'waiting_approval') {
3983
4318
  const firstQuestion = interactivePrompt.questions?.[0];
@@ -3991,7 +4326,14 @@ export class CliProviderInstance implements ProviderInstance {
3991
4326
  : undefined;
3992
4327
  const modalButtons = firstQuestion?.options?.map((option: { label: string }) => option.label);
3993
4328
  this.pushEvent({
3994
- event: 'agent:waiting_approval', chatTitle, timestamp: now,
4329
+ event: 'agent:waiting_choice', chatTitle, timestamp: now,
4330
+ // Structured, authoritative payload — the coordinator renders these and
4331
+ // answers via mesh_answer_question. Carried whole (all questions, all
4332
+ // options) so multi-question / multi-select prompts round-trip fully.
4333
+ interactivePrompt,
4334
+ promptId: interactivePrompt.promptId,
4335
+ multiSelect: firstQuestion?.multiSelect === true,
4336
+ // Push-notification-friendly projection (server push path reads these).
3995
4337
  modalMessage,
3996
4338
  modalButtons,
3997
4339
  });
@@ -4318,15 +4660,37 @@ export class CliProviderInstance implements ProviderInstance {
4318
4660
  get cliType(): string { return this.type; }
4319
4661
  get cliName(): string { return this.provider.name; }
4320
4662
 
4663
+ private resolveAutoApproveMode(): ResolvedAutoApproveMode {
4664
+ return resolveProviderAutoApproveMode(this.provider, this.settings);
4665
+ }
4666
+
4667
+ /** Legacy boolean view retained for internal/test compatibility. */
4321
4668
  private shouldAutoApprove(): boolean {
4322
- if (typeof this.settings.autoApprove === 'boolean') {
4323
- return this.settings.autoApprove;
4669
+ return this.resolveAutoApproveMode().active;
4670
+ }
4671
+
4672
+ private shouldUsePtyAutoApprove(): boolean {
4673
+ const resolved = this.resolveAutoApproveMode();
4674
+ return this.shouldAutoApprove() && resolved.strategy === 'pty-parse-default';
4675
+ }
4676
+
4677
+ private resetPtyAutoApproveState(): void {
4678
+ this.lastAutoApprovalSignature = '';
4679
+ this.pendingAutoApprovalSignature = '';
4680
+ this.pendingAutoApprovalSince = 0;
4681
+ this.autoApproveInactiveSince = 0;
4682
+ this.autoApproveMaskSince = 0;
4683
+ this.stalledApprovalNudgeEpisode = 0;
4684
+ this.autoApproveLastModalSeenAt = 0;
4685
+ this.autoApproveBusy = false;
4686
+ if (this.autoApproveSettleTimer) {
4687
+ clearTimeout(this.autoApproveSettleTimer);
4688
+ this.autoApproveSettleTimer = null;
4324
4689
  }
4325
- const providerDefault = this.provider.settings?.autoApprove?.default;
4326
- if (typeof providerDefault === 'boolean') {
4327
- return providerDefault;
4690
+ if (this.autoApproveBusyTimer) {
4691
+ clearTimeout(this.autoApproveBusyTimer);
4692
+ this.autoApproveBusyTimer = null;
4328
4693
  }
4329
- return false;
4330
4694
  }
4331
4695
 
4332
4696
  /** @see ProviderInstance.noteManualInteraction */
@@ -4353,7 +4717,7 @@ export class CliProviderInstance implements ProviderInstance {
4353
4717
  */
4354
4718
  private autoApproveEffectivelyActive(status: string | undefined, now = Date.now()): boolean {
4355
4719
  return status === 'waiting_approval'
4356
- && this.shouldAutoApprove()
4720
+ && this.shouldUsePtyAutoApprove()
4357
4721
  && !this.manualAttendance.isAttended(now);
4358
4722
  }
4359
4723
 
@@ -4365,7 +4729,8 @@ export class CliProviderInstance implements ProviderInstance {
4365
4729
  // maybeAutoApproveStatus (driven by getState + the recheck timer during a waiting episode);
4366
4730
  // this read is side-effect-free so getStatusMetadata can consult it too.
4367
4731
  private autoApproveMaskStalled(now = Date.now()): boolean {
4368
- return this.autoApproveMaskSince > 0
4732
+ return this.shouldUsePtyAutoApprove()
4733
+ && this.autoApproveMaskSince > 0
4369
4734
  && now - this.autoApproveMaskSince > CliProviderInstance.AUTO_APPROVE_MASK_STALL_MS;
4370
4735
  }
4371
4736
 
@@ -4391,6 +4756,7 @@ export class CliProviderInstance implements ProviderInstance {
4391
4756
  * mask-clock value) so a modal that flaps between parsed/unparsed states is announced once.
4392
4757
  */
4393
4758
  private maybeEmitStalledApprovalNudge(adapterStatus: any, now: number): void {
4759
+ if (!this.shouldUsePtyAutoApprove()) return;
4394
4760
  if (!this.isMeshWorkerSession()) return;
4395
4761
  if (adapterStatus?.status !== 'waiting_approval') return;
4396
4762
  if (!this.autoApproveMaskStalled(now)) return;
@@ -488,6 +488,28 @@ export interface ProviderCompatibilityEntry {
488
488
  scriptDir: string;
489
489
  }
490
490
 
491
+ export type AutoApproveModeStrategy =
492
+ | 'pty-parse-default'
493
+ | 'launch-args'
494
+ | 'post-boot-command';
495
+
496
+ export type AutoApproveModeRisk = 'safe' | 'caution' | 'dangerous';
497
+
498
+ export interface AutoApproveMode {
499
+ id: string;
500
+ label: string;
501
+ strategy: AutoApproveModeStrategy;
502
+ risk: AutoApproveModeRisk;
503
+ warning?: string;
504
+ launchArgs?: string[];
505
+ removeArgs?: string[];
506
+ }
507
+
508
+ export interface AutoApproveModesConfig {
509
+ default: string;
510
+ modes: AutoApproveMode[];
511
+ }
512
+
491
513
  export interface ProviderModule {
492
514
  /** Unique identifier (e.g. 'cline', 'cursor', 'gemini-cli') */
493
515
  type: string;
@@ -519,7 +541,9 @@ export interface ProviderModule {
519
541
  status?: string;
520
542
  /** Inventory/support detail string maintained in adhdev-providers */
521
543
  details?: string;
522
- /** Install instructions (shown when command is missing) */
544
+ /** Provider-specific auto-approve choices and their launch/runtime strategy. */
545
+ autoApproveModes?: AutoApproveModesConfig;
546
+ /** Install instructions (shown when command is missing) */
523
547
  install?: string;
524
548
  /** Custom version detection command (e.g. 'cursor --version', 'claude -v') */
525
549
  versionCommand?: ProviderVersionCommand;