@livx.cc/agentx 0.99.5 → 0.99.7

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.
package/dist/index.js CHANGED
@@ -1149,11 +1149,11 @@ var init_NodeDiskFilesystem = __esm({
1149
1149
  if (rel === "" || rel.startsWith("..")) return;
1150
1150
  const parts = rel.split(np.sep);
1151
1151
  let cur = this.baseDir;
1152
- const now4 = Date.now();
1152
+ const now3 = Date.now();
1153
1153
  for (let i = 0; i < parts.length; i++) {
1154
1154
  cur = np.join(cur, parts[i]);
1155
1155
  const isLeaf = i === parts.length - 1;
1156
- if (!isLeaf && (this.verified.get(cur) ?? 0) > now4) continue;
1156
+ if (!isLeaf && (this.verified.get(cur) ?? 0) > now3) continue;
1157
1157
  let st;
1158
1158
  try {
1159
1159
  st = await fsp.lstat(cur);
@@ -1163,7 +1163,7 @@ var init_NodeDiskFilesystem = __esm({
1163
1163
  if (st.isSymbolicLink()) throw new Error("File not found: symlink not permitted");
1164
1164
  if (!isLeaf) {
1165
1165
  if (this.verified.size > 1e4) this.verified.clear();
1166
- this.verified.set(cur, now4 + _NodeDiskFilesystem.VERIFY_TTL_MS);
1166
+ this.verified.set(cur, now3 + _NodeDiskFilesystem.VERIFY_TTL_MS);
1167
1167
  }
1168
1168
  }
1169
1169
  }
@@ -4345,12 +4345,12 @@ var Scheduler = class {
4345
4345
  if (this.firing) return;
4346
4346
  this.firing = true;
4347
4347
  try {
4348
- const now4 = this.now();
4348
+ const now3 = this.now();
4349
4349
  for (const job of this.jobs.values()) {
4350
4350
  if (job.status !== "active") continue;
4351
4351
  const due = this.nextFire(job);
4352
- if (due == null || due > now4) continue;
4353
- job.lastRun = now4;
4352
+ if (due == null || due > now3) continue;
4353
+ job.lastRun = now3;
4354
4354
  job.runs++;
4355
4355
  if ("at" in job.trigger) job.status = "done";
4356
4356
  try {
@@ -4883,1597 +4883,2192 @@ var EmotionStream = class {
4883
4883
  }
4884
4884
  };
4885
4885
 
4886
- // src/voice/spokenSplitter.ts
4887
- var OPEN = "<spoken>";
4888
- var CLOSE = "</spoken>";
4889
- var CLOSERS = `"')]}\xBB\u201D\u2019`;
4890
- var hasSpeech = (s) => /[\p{L}\p{N}]/u.test(s);
4891
- var SentenceCoalescer = class _SentenceCoalescer {
4892
- buf = "";
4893
- static isEnd(c) {
4894
- return c === "\n" || c === "." || c === "!" || c === "?" || c === "\u2026";
4886
+ // src/voice/engine.ts
4887
+ init_logging();
4888
+ var log10 = forComponent("VoiceEngine");
4889
+ var realClock = {
4890
+ now: () => performance.now(),
4891
+ setTimeout: (fn, ms) => setTimeout(fn, ms),
4892
+ clearTimeout: (h) => clearTimeout(h)
4893
+ };
4894
+ var forSpeech = (t) => t.replace(/[*`#]+/g, "").replace(/(?<![\p{L}\p{N}])_([^_\n]+)_(?![\p{L}\p{N}])/gu, "$1").replace(/^[ \t]*[-•]\s+/gm, "").replace(/\s*[\u2013\u2014]\s*/g, ", ").replace(/[\u2010\u2011]/g, "-").replace(/\s*\|\s*/g, ", ").replace(/(\d)\s+%/g, "$1%").replace(/\.{3,}/g, ".");
4895
+ var normWord = (w) => w.toLowerCase().replace(/[^a-z0-9]/g, "");
4896
+ var SPEC_TRAILING_OK = /* @__PURE__ */ new Set(["please", "thanks", "thank", "you", "now", "okay", "ok", "kindly", "alright", "then", "though", "right", "yeah"]);
4897
+ function speculationConfirms(spec, final) {
4898
+ const sw = spec.trim().split(/\s+/).map(normWord).filter(Boolean);
4899
+ const fw = final.trim().split(/\s+/).map(normWord).filter(Boolean);
4900
+ if (!sw.length || fw.length < sw.length || fw.length > sw.length + 2) return false;
4901
+ for (let i = 0; i < sw.length - 1; i++) if (sw[i] !== fw[i]) return false;
4902
+ if (!fw[sw.length - 1].startsWith(sw[sw.length - 1])) return false;
4903
+ for (let i = sw.length; i < fw.length; i++) if (!SPEC_TRAILING_OK.has(fw[i])) return false;
4904
+ return true;
4905
+ }
4906
+ var VoiceEngineOptions = class {
4907
+ stt;
4908
+ tts;
4909
+ player;
4910
+ /** a final utterance arrived (endpoint) — host dispatches it as a turn */
4911
+ onUtterance = () => {
4912
+ };
4913
+ /** live partial transcript while listening (host renders the 🎤 line) */
4914
+ onPartial = () => {
4915
+ };
4916
+ onState = () => {
4917
+ };
4918
+ /** user spoke/acted over playback — host aborts the in-flight turn (called AFTER audio is killed).
4919
+ * phase: 'speaking' = cut mid-speech (real interruption); 'drain' = in the final audio tail
4920
+ * (normal turn-taking — hosts shouldn't alarm). */
4921
+ onBargeIn = () => {
4922
+ };
4923
+ /** spoken micro-ack on utterance endpoint (masks LLM TTFT); '' disables */
4924
+ ackPhrase = "";
4925
+ /** ADAPTIVE micro-ack: on an utterance-dispatched turn, speak a short varied ack ONLY if no reflex
4926
+ * delta has arrived after this many ms (masks a slow TTFT without acking every turn — a fixed
4927
+ * per-turn ack was rejected as robotic). First delta / interrupt / hold cancels it. 0 = off. */
4928
+ adaptiveAckMs = 0;
4929
+ /** the adaptive ack actually fired (host can mark the turn as spoken — e.g. suppress dead-air repair) */
4930
+ onAdaptiveAck = () => {
4931
+ };
4932
+ /** Endpoint merge window (ms): hold an endpointed utterance briefly — if speech resumes (spelled
4933
+ * letters, mid-thought pauses), the next utterance MERGES instead of dispatching a truncated one
4934
+ * ("E-L-Y." / "A."). Costs this much latency per turn; 0 disables. */
4935
+ utteranceMergeMs = 350;
4936
+ /** Extended merge window (ms) for utterances that look incomplete (trailing conjunction/filler).
4937
+ * Gives the user time to finish their thought without triggering a model call. */
4938
+ incompleteMergeMs = 1500;
4939
+ /** Grace window (ms) after an utterance dispatches, during which the user's own trailing audio cannot
4940
+ * barge the reply it requested. Soniox keeps finalizing partials past <end>; without this they read
4941
+ * as a barge and abort the fresh turn (live: mid-sentence self-interruption + steps=1→steps=0 double
4942
+ * abort). Short enough that a genuine immediate barge ("no wait—") still lands right after. */
4943
+ bargeGraceMs = 600;
4944
+ /** Barge-in (talk over the assistant to interrupt). true = full-duplex (needs echo cancellation, or
4945
+ * the assistant's own TTS bleeds back and self-interrupts). false = HALF-DUPLEX: the engine is deaf
4946
+ * while audible (speaking + drain tail), so echo can never become a phantom turn — the right mode
4947
+ * when there's no AEC (e.g. the non-VPIO mic fallback) and no headphones. Cost: can't interrupt. */
4948
+ bargeIn = true;
4949
+ /** Filler phrase spoken when holding for an incomplete utterance ('' disables). */
4950
+ holdFiller = "";
4951
+ /** Called when the engine holds an incomplete utterance (host can render a visual cue). */
4952
+ onHold = () => {
4953
+ };
4954
+ /** heuristic (non-AEC) energy barge-in tuning */
4955
+ bargeRmsMult = 2;
4956
+ bargeRmsFloor = 500;
4957
+ /** Overlap turn-taking (AEC tier, needs player.pause/resume) — human phone-call model, driven by
4958
+ * the STT ITSELF (a trained speech classifier) instead of energy thresholds (energy could not
4959
+ * separate residue bursts from speech in every room — hiccup whack-a-mole): a GENUINE partial
4960
+ * (novel words dominate — echo of our own reply is inert) while speaking → PAUSE (exact-sample
4961
+ * hold); partial grows into dominant-novel ≥2 words → cede (interrupt; the LLM re-enters); partial
4962
+ * stalls/endpoints without ceding (backchannel by DURATION, not vocabulary) → resume + drop. false disables. */
4963
+ overlapPause = true;
4964
+ /** no new partial activity for this long while paused → resume, drop the interjection */
4965
+ overlapResumeMs = 700;
4966
+ /** A genuine barge over a LONG reply is defeated by the dominant-novel gate: Meet echoes our own
4967
+ * speech back, so the partial is mostly our words + a few of hers → never "dominant novel" → it
4968
+ * resumes (replaying old audio — the audible "completes the buffer" blip) instead of ceding.
4969
+ * Mechanism-based discriminator: a re-PAUSE this soon after a resume = a persistent human, not an
4970
+ * echo blip (which pauses once and stalls). Cede on the re-pause regardless of the novel gate. */
4971
+ overlapRepauseCedeMs = 1500;
4972
+ /** Speculative ENERGY pre-pause while speaking (AEC tier): two residue gate-passes within 350ms →
4973
+ * pause ~300ms before the STT tokens land. But energy CANNOT separate residue bursts from speech
4974
+ * (the documented whack-a-mole) — so a residue spike during loud playback false-pauses with NO user
4975
+ * speech at all, an audible hiccup. Default OFF: the genuine-gated STT partial is the
4976
+ * mechanism-correct pause trigger; enable only if barge-in onset feels sluggish in a clean-AEC room. */
4977
+ overlapEnergyHold = false;
4978
+ /** SPECULATIVE REFLEX START (the root TTFT fix): a partial transcript that has stopped changing for
4979
+ * this many ms AND carries ≥ speculativeMinWords is "stable" — `onSpeculate` fires so the host can
4980
+ * start the reflex EARLY, ~endpoint+merge (500-850ms) before the final would dispatch. The
4981
+ * speculative call's output is HELD by the host (nothing reaches TTS) until the endpointed final
4982
+ * confirms it (see speculationConfirms). At most one speculation per turn-in-progress. 0 = off. */
4983
+ speculativeMs = 0;
4984
+ /** Minimum word count for a partial to qualify as a speculation trigger. */
4985
+ speculativeMinWords = 4;
4986
+ /** A stable partial (speculativeMs) — the host starts a HELD speculative reflex call. */
4987
+ onSpeculate = () => {
4988
+ };
4989
+ /** AGENT-SIDE BACKCHANNELING (rule-based v1): while LISTENING to a long multi-clause user turn, a
4990
+ * partial that reaches a clause boundary (trailing [,.;!?] or conjunction/filler) and then stays
4991
+ * UNCHANGED for this many ms (a micro-pause — before the silence endpoint fires) triggers a short
4992
+ * quiet TTS blip ("Mm-hm.") on a throwaway context. ZERO floor-claim: no state change, no timers
4993
+ * touched, no turn context — audio passes a narrow gate bypass and a real turn supersedes it via
4994
+ * context rotation. Latin-predominant partials only (Hebrew/mixed text never misfires — the
4995
+ * boundary/conjunction heuristics are English-tuned, so non-Latin turns simply get no blips).
4996
+ * 0 = off (default). ~200-300 recommended: live, Soniox's SEMANTIC endpoint (<end>) lands within
4997
+ * ~300-400ms of a clause pause — a longer stability window loses the race and never fires. */
4998
+ backchannelMs = 0;
4999
+ /** Min gap between blips (rate limit); additionally max 2 blips per user turn-in-progress. */
5000
+ backchannelMinGapMs = 8e3;
5001
+ /** Only multi-clause turns: the partial must carry at least this many words before a blip. */
5002
+ backchannelMinWords = 8;
5003
+ /** A backchannel blip was spoken (host renders a timeline event; the blip is NOT a reply). */
5004
+ onBackchannel = () => {
5005
+ };
5006
+ /** The partial outgrew the speculated text (user kept talking) — the host aborts the speculation
5007
+ * quietly (the endpointed final will also refuse to confirm; this just stops the billing earlier). */
5008
+ onSpeculateAbort = () => {
5009
+ };
5010
+ /** Map inline `[emotion]` tags (emitted by the model, prompt-taught) into Cartesia inline emotion
5011
+ * tags in the spoken transcript (sonic-3 stitches the prosody). false = strip them silently. */
5012
+ emotions = true;
5013
+ /** Show the `[emotion]` tags in the on-screen echo (debug). false = hide (spoken-only). */
5014
+ showEmotions = false;
5015
+ /** Time source for every engine timer/timestamp. Default: real time (identical behavior). */
5016
+ clock = realClock;
5017
+ /** Structured diagnostic event at every decision point (state transitions, dispatch/drop paths,
5018
+ * barge-in, overlap pause/resume, acks, speculation, backchannels, echo swallows). Fire-and-forget:
5019
+ * a throwing handler is caught once and diagnostics disable — engine behavior is never affected. */
5020
+ onDiag = () => {
5021
+ };
5022
+ };
5023
+ var VoiceEngine = class _VoiceEngine {
5024
+ options;
5025
+ state = "idle";
5026
+ stt;
5027
+ tts;
5028
+ player;
5029
+ speaking = false;
5030
+ // audible (deltas flowing OR audio draining)
5031
+ ctxOpen = false;
5032
+ // the current TTS context still accepts deltas (false once end-frame sent)
5033
+ interrupted = false;
5034
+ // barge-in latch: drop in-flight deltas until the next legitimate turn
5035
+ spokeDeltas = false;
5036
+ // a TTS context is open for the current spoken turn
5037
+ clock;
5038
+ drainTimer = null;
5039
+ // heuristic tier state (inert under AEC) — frozen as validated in the experiment
5040
+ echoWords = /* @__PURE__ */ new Set();
5041
+ prevReply = "";
5042
+ reply = "";
5043
+ echoUntil = 0;
5044
+ baseline = 0;
5045
+ hot = 0;
5046
+ suspectUntil = 0;
5047
+ ackAt = 0;
5048
+ // when the micro-ack was spoken — its echo can leak before the AEC filter converges
5049
+ lastAck = "";
5050
+ // the exact ack text last spoken (fixed OR adaptive) — the echo-leak guard matches it
5051
+ ackTimer = null;
5052
+ // one-shot adaptive-ack timer (armed at dispatch, cancelled on first delta)
5053
+ bargeGraceUntil = 0;
5054
+ // no barge-in until this time — the user's OWN trailing audio (after the
5055
+ // utterance that JUST dispatched this turn) must not immediately re-interrupt the reply it requested.
5056
+ pendingUtt = "";
5057
+ // endpointed text held for the merge window
5058
+ mergePath = "direct";
5059
+ // how the pending utterance was assembled (diag)
5060
+ lastGraceDiag = 0;
5061
+ // grace_suppress emitted once per grace window
5062
+ pendingTimer = null;
5063
+ // Duplicate-final guard: STT sometimes re-finalizes the SAME audio a beat later (past the merge
5064
+ // window) — the identical utterance dispatched twice with no reply in between (live: "When you
5065
+ // start" twice). Conservative: only an IDENTICAL (normalized) text, within dupFinalMs, with zero
5066
+ // reply deltas since the first dispatch, is dropped. A user genuinely repeating themselves after
5067
+ // the agent replied (or after 3s) still dispatches.
5068
+ lastDispatchFlat = "";
5069
+ lastDispatchWords = [];
5070
+ lastDispatchAt = 0;
5071
+ repliedSinceDispatch = false;
5072
+ static DUP_FINAL_MS = 3e3;
5073
+ lastInterrupted = null;
5074
+ // overlap (pause) tier state — AEC + pause-capable sinks only
5075
+ pausedAt = 0;
5076
+ lastResumeAt = 0;
5077
+ // when the overlap last resumed from a false alarm — a quick re-pause cedes
5078
+ lastOverlapPartial = "";
5079
+ // change-detection: only NEW partial text counts as activity
5080
+ resumeTimer = null;
5081
+ turnStartAt = 0;
5082
+ // timestamp when the current turn began (for TTFT logging)
5083
+ // speculative reflex trigger state (options.speculativeMs) — see trackSpeculation
5084
+ specPartial = "";
5085
+ // last partial observed (stability = unchanged for speculativeMs)
5086
+ specTimer = null;
5087
+ specText = "";
5088
+ // text handed to onSpeculate ('' = no speculation in flight)
5089
+ specSpent = false;
5090
+ // at most one speculation per turn-in-progress
5091
+ // backchannel blip state (options.backchannelMs) — see trackBackchannel
5092
+ bcPartial = "";
5093
+ // last partial observed (stability = unchanged for backchannelMs)
5094
+ bcTimer = null;
5095
+ bcCount = 0;
5096
+ // blips this user turn-in-progress (max 2; reset at dispatch)
5097
+ bcActive = false;
5098
+ // NARROW audio-gate bypass: blip audio may reach the sink while listening
5099
+ bcActiveTimer = null;
5100
+ // safety: clear the bypass even if the blip's 'done' is lost
5101
+ lastBcAt = 0;
5102
+ lastBcPhrase = "";
5103
+ // exact blip text last spoken — flushUtterance strips its mic echo
5104
+ recentBc = [];
5105
+ // Central speech queue (above the TTS context): complete worker utterances serialize into ONE
5106
+ // playback stream, one-at-a-time, never splicing into the live reflex's open utterance.
5107
+ uttQueue = [];
5108
+ // Per-turn emotion-tag parser (reset on beginSpeech) — converts `[emotion]` → Cartesia inline tags
5109
+ // for TTS, tracks tag-free prose for echo discrimination, and surfaces display text for the screen.
5110
+ emo = null;
5111
+ constructor(options) {
5112
+ this.options = { ...new VoiceEngineOptions(), ...options };
5113
+ const o = this.options;
5114
+ if (!o.stt || !o.tts || !o.player) throw new Error("VoiceEngine needs stt, tts and player (see cli/voice.ts VoiceIO for platform defaults)");
5115
+ this.stt = o.stt;
5116
+ this.tts = o.tts;
5117
+ this.player = o.player;
5118
+ this.clock = o.clock;
4895
5119
  }
4896
- feed(delta) {
4897
- if (delta) this.buf += delta;
4898
- let cut = -1;
4899
- for (let i = 0; i < this.buf.length; i++) if (_SentenceCoalescer.isEnd(this.buf[i])) cut = i;
4900
- if (cut < 0) return "";
4901
- if (this.buf[cut] !== "\n") while (cut + 1 < this.buf.length && CLOSERS.includes(this.buf[cut + 1])) cut++;
4902
- const ready = this.buf.slice(0, cut + 1).trim();
4903
- this.buf = this.buf.slice(cut + 1);
4904
- return hasSpeech(ready) ? ready : "";
5120
+ async start() {
5121
+ this.tts.onAudio = (c) => {
5122
+ if (this.speaking || this.bcActive) this.player.write(c);
5123
+ };
5124
+ this.stt.onPartial = (text) => this.handlePartial(text);
5125
+ this.stt.onUtterance = (text) => this.handleUtterance(text);
5126
+ this.stt.onLevel = (rms) => this.handleLevel(rms);
5127
+ await Promise.all([this.tts.connect(), this.stt.start()]);
5128
+ this.tts.warmup?.();
5129
+ this.setState("listening");
5130
+ log10.debug(`voice I/O up (${this.stt.usingAec ? "AEC" : "heuristic echo"} capture)`);
4905
5131
  }
4906
- flush() {
4907
- const s = this.buf.trim();
4908
- this.buf = "";
4909
- return hasSpeech(s) ? s : "";
5132
+ get usingAec() {
5133
+ return this.stt.usingAec;
4910
5134
  }
4911
- };
4912
- var SpokenSplitter = class {
4913
- buf = "";
4914
- inSpoken = false;
4915
- /** True once any spoken char has ever been emitted (drives the no-spoken fallback). */
4916
- spokeAny = false;
4917
- /** Feed a delta; returns the spoken/detail spans completed by this chunk (either may be ''). */
4918
- feed(delta) {
4919
- this.buf += delta;
4920
- return this.drain(false);
5135
+ /** Flip barge-in at runtime (e.g. the mic fell back to non-VPIO → go half-duplex so echo can't leak). */
5136
+ setBargeIn(on) {
5137
+ this.options.bargeIn = on;
4921
5138
  }
4922
- /** Drain any buffered partial. A trailing `<…` that never completed a tag is emitted as detail. */
4923
- flush() {
4924
- return this.drain(true);
5139
+ /** Show/hide the `[emotion]` debug tags in the echo (next turn's stream picks it up). */
5140
+ setShowEmotions(on) {
5141
+ this.options.showEmotions = on;
4925
5142
  }
4926
- drain(final) {
4927
- let spoken = "";
4928
- let detail = "";
4929
- while (this.buf.length) {
4930
- const tag = this.inSpoken ? CLOSE : OPEN;
4931
- const idx = this.buf.indexOf(tag);
4932
- if (idx >= 0) {
4933
- const text2 = this.buf.slice(0, idx);
4934
- if (this.inSpoken) spoken += text2;
4935
- else detail += text2;
4936
- this.buf = this.buf.slice(idx + tag.length);
4937
- this.inSpoken = !this.inSpoken;
4938
- continue;
4939
- }
4940
- const lt = this.buf.lastIndexOf("<");
4941
- const holdStart = lt >= 0 && tag.startsWith(this.buf.slice(lt)) ? lt : this.buf.length;
4942
- const text = this.buf.slice(0, holdStart);
4943
- if (this.inSpoken) spoken += text;
4944
- else detail += text;
4945
- this.buf = this.buf.slice(holdStart);
4946
- break;
5143
+ /** Diagnostics tap (options.onDiag). Fire-and-forget: a throwing handler disables the tap once —
5144
+ * it can NEVER perturb engine behavior. Protected so VoiceIO can route provider events through it. */
5145
+ diagOn = true;
5146
+ diag(kind, fields) {
5147
+ if (!this.diagOn) return;
5148
+ try {
5149
+ this.options.onDiag({ t: this.clock.now(), kind, ...fields });
5150
+ } catch (e) {
5151
+ this.diagOn = false;
5152
+ log10.debug(`onDiag threw \u2014 diagnostics disabled: ${e instanceof Error ? e.message : e}`);
4947
5153
  }
4948
- if (final && this.buf) {
4949
- if (this.inSpoken) spoken += this.buf;
4950
- else detail += this.buf;
4951
- this.buf = "";
5154
+ }
5155
+ idleWaiters = [];
5156
+ setState(s) {
5157
+ if (this.state === s) return;
5158
+ this.diag("state", { from: this.state, to: s });
5159
+ this.state = s;
5160
+ this.options.onState(s);
5161
+ if (s !== "speaking" && s !== "thinking") {
5162
+ for (const r of this.idleWaiters.splice(0)) r();
4952
5163
  }
4953
- if (spoken.trim()) this.spokeAny = true;
4954
- return { spoken, detail };
4955
5164
  }
4956
- };
4957
-
4958
- // src/duplex.ts
4959
- var log10 = forComponent("DuplexAgent");
4960
- function describeCall(call) {
4961
- const v = call.args && Object.values(call.args).find((x) => typeof x === "string" && x.trim());
4962
- const hint = v ? ` (${String(v).replace(/\s+/g, " ").trim().slice(0, 48)})` : "";
4963
- return `${call.name}${hint}`;
4964
- }
4965
- var DuplexAgentOptions = class {
4966
- /** Any ai.libx.js AIClient — shared by all tiers (routed by model). */
4967
- ai;
4968
- /** The WORKER's filesystem (act + think). If omitted the worker keeps Agent's jailed-disk-at-cwd default. */
4969
- fs;
4970
- // The reflex IS the voice. 120b (not 20b) for channel discipline + instruction-following: the 20b
4971
- // mislabels gpt-oss harmony channels under load, leaking raw analysis into the spoken `final` channel
4972
- // (and misfiring Hold). 120b is the same price tier (~$0.15/$0.60) — the quality/cost trade is free.
4973
- reflexModel = "groq/openai/gpt-oss-120b";
4974
- actModel = "anthropic/claude-sonnet-4-6";
4975
- /** Premium reasoning model. Set to `false` to disable the Think tier entirely. */
4976
- thinkModel = "anthropic/claude-opus-4-8";
4977
- /** Per-worker providerOptions, derived from the worker's actual model at spawn time (IoC — keeps duplex
4978
- * provider-agnostic). Workers override the reflex/main model, so provider-specific options (e.g. cursor's
4979
- * cwd/cursorSession) must be recomputed for the worker's model, never inherited from the main template —
4980
- * leaking cursor options to an anthropic worker is a hard 400. Returns undefined → no providerOptions. */
4981
- providerOptionsFor;
4982
- /** Escape hatches merged over the derived per-agent options. */
4983
- reflexOptions;
4984
- actOptions;
4985
- thinkOptions;
4986
- /** Fresh-context check on each successful Act task: a NEW agent (no self-confirmation bias) re-reads
4987
- * the file state against the brief and fixes any gap before the result is re-voiced. Bounded to one
4988
- * pass; ~2x Act cost so default OFF. The self-verify FOOTER (same context) was measured ineffective —
4989
- * this is the structural fix (see mind/10). Think tasks are pure reasoning, never checked. */
4990
- verifyActTasks = false;
4991
- /** Receives the voice text_delta stream + task lifecycle events. */
4992
- host;
4993
- /** How many recent transcript messages are rendered into a worker's brief. */
4994
- excerptTurns = 6;
4995
- /** Voice register: 'neutral' = clean spoken style; 'conversational' = human-like — fillers,
4996
- * backchannels, impulsive first reactions before content (mimics real duplex conversation). */
4997
- voiceStyle = "neutral";
4998
- /** Teach the model to emit inline `[emotion]` tags for Cartesia emotion control. Only set when the
4999
- * TTS actually speaks them — text-duplex (no TTS) would otherwise print literal tags. */
5000
- emotionTags = false;
5001
- /** Awaited BEFORE a worker spawns — open a per-task checkpoint frame, audit, etc.
5002
- * (post-spawn would race the worker's first edits). */
5003
- onTaskStart;
5004
- /** Re-voice throttled worker progress asides ('[task t1 progress] …') so long tasks aren't dead
5005
- * air. Off by default — each update costs a voice turn (LLM call + speech). */
5006
- progressUpdates = false;
5007
- /** Min ms between progress re-voices per task. */
5008
- progressIntervalMs = 25e3;
5009
- /** Relay worker questions (AskUserQuestion + permission asks via parkQuestion) through the VOICE:
5010
- * the question re-voices as '[task <id> asks] …', the user answers conversationally, and the
5011
- * voice model resolves it with the AnswerTask tool. Off → host.ask passthrough (text menus). */
5012
- askRelay = false;
5013
- /** Parked questions auto-resolve empty after this long (callers map '' to deny/best-judgment). */
5014
- askTimeoutMs = 12e4;
5015
- /** Max retained task records: oldest SETTLED tasks (and their activity tails) are evicted past this,
5016
- * bounding memory over a long-lived session. Running tasks are never evicted. */
5017
- maxTaskRecords = 50;
5018
- /** Host overrides for QuickLook lookups (keyed by `what`). The engine's defaults go through the
5019
- * (possibly jailed) fs — e.g. `.git/**` is deny-listed, so the CLI supplies 'branch' itself. */
5020
- quickLook;
5021
- /** Memory directory/directories on the WORKER fs. If set, the voice agent gets Remember + Recall
5022
- * tools directly (no delegation needed) and implicit capture guidance. */
5023
- memoryDir;
5024
- /** User-scope memory dir for global facts (type=user/feedback). Forwarded to Remember's routing. */
5025
- memoryUserDir;
5026
- };
5027
- var RESERVED_EVENT_MARKER = /\[task\b[^\]\n]*\b(?:completed|failed|progress|asks)\b/i;
5028
- var RESERVED_EVENT_OPENER = /\[\s*task\b/i;
5029
- var VOICE_SYSTEM_PROMPT = 'You are a spoken voice assistant \u2014 the user HEARS everything you say. Use short sentences. One idea per sentence. No markdown, no bullet lists, no code blocks, no headings, no emoji.\nThis holds even when asked to "print", "list", "show", or "make a table" \u2014 there is no screen for the spoken channel. Speak it as flowing prose ("Tuesday is half a meter, Wednesday a bit less\u2026"), or if they truly need it on screen, route it to Act to render. Never emit dashes or pipes into speech.\nKeep turns SHORT \u2014 one to three sentences, then stop. Never lecture, enumerate cases, or add caveats unprompted. Conversation is a fast exchange: give the one thing asked, and let the user pull more if they want it.\nYou have three cognitive tiers \u2014 like a human brain:\n\u2022 YOU (reflex) \u2014 instant, lightweight. Handle greetings, simple questions, status checks, QuickLook.\n\u2022 `Act` \u2014 your hands. A background worker with its own configured tools and access to the user\'s environment (files and shell{{WORKER_WEB}}). Use for reading, editing, searching, running tasks, building \u2014 any real work.\n{{THINK_SLOT}}\nWhen you are unsure whether you can do or access something, do NOT assume and do NOT claim a capability you have not confirmed. To check what you can do, QuickLook `capabilities` (instant \u2014 it lists your worker\'s real tools) and answer from that. Never promise an ability that is not in your capabilities; if it is not there, tell the user plainly you can\'t. To actually DO real work, call `Act`. When the user mentions their project, folder, files, or environment ("this project", "the current folder", "my code"), call `Act` IMMEDIATELY \u2014 do not ask for paths or details the worker can discover itself. Never pretend to have done the work or invent results \u2014 the worker\'s report is your only source.\nYou cannot mute the microphone or stop voice capture yourself \u2014 no tool does it. If the user asks you to stop listening or turn the voice off, never claim you did: tell them to say exactly "voice off" (handled by the app directly), or type /voice.\nYou are NOT a knowledge base. For any question whose answer needs SPECIFIC verifiable facts you do not already have in hand \u2014 how to build/configure/implement something, exact API, library, entitlement, command or option names, current events, or particular numbers, dates, or names \u2014 do NOT answer from your own memory: you will confidently make things up (a fake API, a wrong entitlement, an event that did not happen). Route it to `Act`, which can search and verify, and speak only what its report says. Answer inline ONLY for general conversation, chit-chat, and trivia you are sure of, or facts you can see via QuickLook. When elaborating on a completed task ("tell me more", "the gist"), stay strictly within what that result actually said \u2014 if the user asks for something the result did not cover, that is NEW information: dispatch `Act`, do not improvise.\nALWAYS react before you work: the FIRST thing in your turn is a brief spoken acknowledgement of what you heard and what you are about to do ("got it \u2014 opening that now", "sure, let me pull it up", "okay, checking"). NEVER call a tool (Act, Think, QuickLook) silently \u2014 the user must hear you react before you go quiet to work. After dispatching Act or Think, that same one short sentence IS your turn \u2014 end it and do not wait for the result.\nA completed task speaks its OWN result to the user (the worker voices what matters as it finishes) \u2014 you do NOT re-voice clean task results. A FAILED or INCOMPLETE task still arrives as a "[task t1 failed] \u2026" event for you to handle. The completed result stays in YOUR context \u2014 it is yours to draw on. When the user follows up ("tell me more", "what else", "and?"), answer FROM that result first: you already have the detail, so elaborate on what you have. Do NOT spawn a fresh worker to re-search or re-gather what you were just handed. Re-dispatch ONLY when genuinely new information is needed \u2014 e.g. the user wants the full contents of a SPECIFIC source, which is one WebFetch of that URL, not a brand-new search. "[task t1 progress] \u2026" events are interim status, NOT results \u2014 give at most a half-sentence aside ("still on it \u2014 running tests now") and end your turn. Never present progress as a finished result.\nCRITICAL: while a task is still running you have NO answer yet \u2014 never state a specific result of any kind (a number, size, count, name, path, or value). The real answer arrives ONLY in the "[task \u2026 completed]" event; inventing one meanwhile (a made-up disk size, commit count, etc.) is a serious error. Until then, only acknowledge and wait.\nNever read raw file paths, diffs, or code aloud verbatim.\nDo NOT end every turn with the same canned offer ("want a rundown?", "want the steps?"). Offer once at most; if the user pushes back, repeats themselves, or sounds unsatisfied ("you know what I mean?", "think deeper", "are you sure?"), do NOT re-offer the same thing \u2014 change approach: dispatch `Act`/`Think` to actually dig in, or ask one concrete clarifying question. Repeating a non-answer is worse than silence.\n"[task t1 asks] \u2026" events are QUESTIONS from a background task \u2014 relay to the user in your own words, short, then end your turn. When the user answers, call `AnswerTask` with that id and their answer. NEVER answer on the user\'s behalf for permissions or risky operations; if their reply is ambiguous, confirm first.\nIf the user\'s message sounds INCOMPLETE \u2014 trailing off mid-sentence, a fragment that needs more context ("and then we", "but the problem is"), hesitation fillers ("uh", "um") \u2014 call `Hold` instead of answering. This keeps listening for the rest of their thought. Only respond with substance when you have a complete question or request.\nDispatch discipline: send ONE self-contained task per request \u2014 a single worker with the full brief beats several workers with fragments (each worker starts fresh and re-discovers context). NEVER dispatch a worker just to read files or gather information \u2014 workers explore and discover context themselves; pass on what you already know and let one worker do the whole job. Split into parallel tasks only when the user asks for genuinely independent things. When a task completes, report its result and stop \u2014 do NOT dispatch follow-up work (verification, polish, extras) the user did not ask for, unless the report itself signals failure or doubt.\nDo not fire a second Act/Think for work already in flight, and NEVER spawn a second task to re-count, cross-check, or verify a result a worker already gave you \u2014 trust its answer; a single question gets ONE task. Call `TaskStatus` at most ONCE per turn; if a task is still running, just say "still on it" and end the turn \u2014 never poll it again and again in a loop. Use `CancelTask` when the user asks to stop something.\nPRIORITY: when the user says goodbye or wants to end/finish/wrap up the session ("ok bye", "that\'s all", "let\'s finish", "let\'s end", "goodnight", "exit", "wrap up"), call `ExitSession` IMMEDIATELY \u2014 do not act, do not check status, just exit.\nFor TRIVIAL instant lookups only \u2014 current time, git branch, listing a folder, peeking at a small file, or checking your own `capabilities`/tools \u2014 use `QuickLook` (instant, no task). Whenever the user asks what you can do or whether you have some ability, QuickLook `capabilities` and answer from that \u2014 never guess. Anything requiring searching, reasoning, running commands, or editing goes through `Act`.\n{{MEMORY_SLOT}}\nUser messages may arrive via speech-to-text and can carry transcription artifacts \u2014 odd words, cut-offs, homophones ("for you" vs "folder"). Read for INTENT, not surface text. If a message seems garbled, surprising, or only half-parses, do NOT guess an action or improvise content from it \u2014 briefly confirm what they meant ("did you mean\u2026?") and wait. A one-line confirm beats a confident wrong answer or an invented response to a request you did not actually understand.';
5030
- var THINK_GUIDANCE = "\u2022 `Think` \u2014 your brain. A premium reasoning model, FAR more expensive than Act. Reserve it for open-ended architecture/design questions, or a problem Act already FAILED at. ALL implementation work \u2014 coding, refactoring, debugging, edge cases, tests \u2014 goes to Act; Act is highly capable. Never send the same work to both.";
5031
- var THINK_DISABLED_GUIDANCE = "(Think tier is not available \u2014 use Act for all escalations.)";
5032
- var VOICE_STYLE_CONVERSATIONAL = `Speak like a person in a live conversation, not an assistant reading a script. React first, then deliver: a quick impulsive beat ("oh nice", "hmm, hold on", "ah, got it") before the substance. Use contractions always. Vary sentence length \u2014 some very short. Light fillers and backchannels are fine ("mm-hm", "right", "let's see") but at most one per reply \u2014 never stack them. When you escalate to Act or Think, say it like a human would ("hang on, let me actually dig into that \u2014 gimme a minute") instead of announcing a task. When a result comes back, react to it like you just found out ("okay so \u2014 turns out\u2026"). Match the user's energy: a quick question gets a quick answer \u2014 a few words is a perfectly good turn. Prefer a short answer plus an offer ("want the details?") over covering everything. Never narrate your own mechanics (no "I will now act", no task ids out loud).`;
5033
- var EMOTION_TAGS_GUIDANCE = `EMOTION: your voice is synthesized with emotion control. Prefix a sentence with an inline [emotion] tag, placed directly before the sentence it colors, to shape how it is spoken. Use it ONLY when the emotion genuinely fits the words (it amplifies real feeling, it cannot fake it) \u2014 do not tag every sentence; reserve it for moments that carry feeling, and vary which one you use. You may also drop [laughter] for a natural laugh. Available emotions: ${EMOTIONS.join(", ")}.`;
5034
- var DuplexAgent = class _DuplexAgent {
5035
- options;
5036
- voice;
5037
- tasks = /* @__PURE__ */ new Map();
5038
- queue = Promise.resolve();
5039
- seq = 0;
5040
- pendingEvents = [];
5041
- /** Out-of-band follow-up attribution for the events coalescing into the next flush turn: TRUE iff ≥1 of
5042
- * the tasks being integrated was NON-CLEAN (early-stop/failure). Carried out-of-band on the enqueue call
5043
- * by the caller that KNOWS the outcome — a plain boolean the MODEL CANNOT PERTURB. It is NOT scanned from
5044
- * worker-authored event text (v1: an "Outcome:" substring over-stamped siblings) and NOT keyed on a brief
5045
- * string the reflex re-authors (v2: a paraphrased escalation brief missed the Set → followUp:false →
5046
- * RE-ENABLED unbounded auto-escalation, the dangerous runaway direction). See [[wrong-discriminator]] /
5047
- * [[drive-real-reflex]] / [[fakeaiclient-blind-to-wire-format]]. */
5048
- pendingNonClean = false;
5049
- flushQueued = false;
5050
- /** Per-voice-turn guards (reset by resetTurn at each turn's start). The reflex is a weak model:
5051
- * left unguarded it polls TaskStatus after a dispatch and/or dispatches silently (dead air).
5052
- * Like CC's Task tool, a dispatch is "said my piece, now wait for the push" — these enforce that. */
5053
- turnDispatched = false;
5054
- // an Act/Think fired this turn
5055
- turnBriefs = /* @__PURE__ */ new Set();
5056
- // briefs dispatched this turn (detect identical re-dispatch)
5057
- spokeThisTurn = false;
5058
- // any non-empty text_delta streamed this turn
5059
- heldThisTurn = false;
5060
- // Hold called this turn → turn is INTENTIONALLY silent (suppress reflex text + no dead-air ack)
5061
- nudging = false;
5062
- // re-ack pass in flight: block ALL tools, prevent recursion
5063
- reflexBuf = "";
5064
- // accumulated reflex text this turn (fabricated-event detection)
5065
- reflexForwarded = 0;
5066
- // chars of reflexBuf already forwarded to the host/TTS
5067
- fabricationCut = false;
5068
- // reflex emitted a reserved [task …] marker → suppress its tail
5069
- /** TRUE for the duration of a re-voice turn that is integrating ≥1 NON-CLEAN task (turn-eligibility,
5070
- * carried out-of-band — NOT derived from any worker/brief string). ANY Act/Think dispatched in such a
5071
- * turn is stamped followUp:true. This GUARANTEES the dangerous direction is impossible: a genuine
5072
- * escalation (even one with a paraphrased brief) ALWAYS lands in a non-clean integration turn, so it is
5073
- * ALWAYS recognized as a follow-up and CANNOT re-escalate (one hop). The single-dispatch-per-turn guard
5074
- * means at most one dispatch happens per flush, so realistically "the one dispatch IS the escalation".
5075
- * ACCEPTED SAFE-DIRECTION ERROR: if the reflex instead dispatches FRESH unrelated work during a non-clean
5076
- * flush (rare — and only possible when it batches multiple calls in one step, bypassing the guard), that
5077
- * fresh task is over-stamped followUp:true and forgoes ONE future auto-escalation. That is SAFE (it only
5078
- * ever REMOVES a future escalation, never adds one — no runaway) and is the correct side to err on. */
5079
- turnFollowUp = false;
5080
- /** Hard absolute backstop against runaway regardless of attribution: total automatic escalations across
5081
- * the whole conversation. Once it hits MAX_AUTO_ESCALATIONS, no integration turn offers escalate/re-delegate. */
5082
- autoEscalations = 0;
5083
- static MAX_AUTO_ESCALATIONS = 8;
5084
- /** Parked worker questions awaiting a (voice-relayed) user answer, keyed by ask id. */
5085
- pendingAsks = /* @__PURE__ */ new Map();
5086
- /** Lazily resolved memory tools (async loadMemory runs in initMemory). */
5087
- memoryReady;
5088
- constructor(options) {
5089
- this.options = { ...new DuplexAgentOptions(), ...options };
5090
- const o = this.options;
5091
- if (o.memoryDir && o.fs) {
5092
- this.memoryReady = loadMemory(o.fs, o.memoryDir, { maxWritesPerSession: 10, userDir: o.memoryUserDir });
5093
- }
5094
- const memSlot = o.memoryDir && o.fs ? VOICE_MEMORY_PROMPT : "NEVER claim to have stored, saved, or remembered something durably \u2014 you cannot. Anything the user wants persisted (their name, preferences, notes) must go through Act so a worker writes it to memory.";
5095
- const thinkSlot = o.thinkModel !== false ? THINK_GUIDANCE : THINK_DISABLED_GUIDANCE;
5096
- const workerToolNames = (o.actOptions?.tools ?? []).map((t) => t.name);
5097
- const canSearch = workerToolNames.some((n) => /WebSearch/i.test(n));
5098
- const canFetch = workerToolNames.some((n) => /WebFetch/i.test(n));
5099
- const workerWeb = canSearch ? `, and it CAN search the web and read web pages \u2014 so when the user gives you something specific to look up ("search for X", "find me\u2026", "what's the latest on\u2026"), route it to Act. But a bare capability QUESTION like "can you search the web?" just gets a short spoken "yes, I can" \u2014 do NOT dispatch and NEVER invent a query the user did not give you` : canFetch ? ", and it can fetch a specific web page URL (but cannot search the web)" : "";
5100
- const mcpNames = [
5101
- ...Object.keys(o.actOptions?.providerOptions?.mcpServers ?? {}),
5102
- ...new Set(workerToolNames.filter((n) => n.startsWith("mcp__")).map((n) => n.slice(5).split("__")[0]))
5103
- ];
5104
- const workerMcp = mcpNames.length ? `, and it can use these MCP servers: ${[...new Set(mcpNames)].join(", ")}` + (mcpNames.some((n) => /browser/i.test(n)) ? ' \u2014 including driving a REAL browser (open tabs, navigate, click, screenshot), so answer "yes" if asked whether you can control/drive a browser and route an actual browse to Act' : "") : "";
5105
- const prompt = VOICE_SYSTEM_PROMPT.replace("{{MEMORY_SLOT}}", memSlot).replace("{{THINK_SLOT}}", thinkSlot).replace("{{WORKER_WEB}}", workerWeb + workerMcp) + (o.voiceStyle === "conversational" ? "\n" + VOICE_STYLE_CONVERSATIONAL : "") + (o.emotionTags ? "\n" + EMOTION_TAGS_GUIDANCE : "") + `
5106
- Today's date: ${(/* @__PURE__ */ new Date()).toDateString()}.`;
5107
- const tools = [
5108
- ...o.reflexOptions?.tools ?? [],
5109
- this.actTool(),
5110
- ...o.thinkModel !== false ? [this.thinkTool()] : [],
5111
- this.taskStatusTool(),
5112
- this.cancelTaskTool(),
5113
- this.quickLookTool(),
5114
- this.answerTaskTool(),
5115
- this.holdTool()
5116
- ];
5117
- const host = o.host;
5118
- const voiceHost = host && {
5119
- ask: host.ask ? (q) => host.ask(q) : void 0,
5120
- confirm: host.confirm ? (p, m) => host.confirm(p, m) : void 0,
5121
- notify: (ev) => {
5122
- if (ev?.kind === "text_delta" && typeof ev.message === "string") {
5123
- if (this.heldThisTurn) return;
5124
- if (this.fabricationCut) return;
5125
- const msg = ev.message;
5126
- this.reflexBuf += msg;
5127
- const m = this.reflexBuf.match(RESERVED_EVENT_MARKER) ?? this.reflexBuf.match(RESERVED_EVENT_OPENER);
5128
- if (m) {
5129
- this.fabricationCut = true;
5130
- log10.warn(`reflex fabricated a [task \u2026] event in its spoken stream \u2014 cutting it (kept ${m.index} chars)`);
5131
- const safe = this.reflexBuf.slice(this.reflexForwarded, m.index);
5132
- if (!safe) return;
5133
- if (safe.trim()) this.spokeThisTurn = true;
5134
- host.notify?.({ ...ev, message: safe });
5135
- return;
5136
- }
5137
- const held = this.reflexBuf.length - this.reflexForwarded;
5138
- const partial = held > 0 && /\[\s*t?a?s?k?$/i.test(this.reflexBuf.slice(-Math.min(held, 6)));
5139
- const upto = partial ? this.reflexBuf.length - this.reflexBuf.slice(-6).match(/\[\s*t?a?s?k?$/i)[0].length : this.reflexBuf.length;
5140
- const out = this.reflexBuf.slice(this.reflexForwarded, upto);
5141
- this.reflexForwarded = upto;
5142
- if (!out) return;
5143
- if (out.trim()) this.spokeThisTurn = true;
5144
- host.notify?.({ ...ev, message: out });
5145
- return;
5146
- }
5147
- host.notify?.(ev);
5148
- }
5149
- };
5150
- this.voice = new Agent({
5151
- ai: o.ai,
5152
- fs: new MemFilesystem2(),
5153
- model: o.reflexModel,
5154
- stream: true,
5155
- host: voiceHost,
5156
- // The reflex IS the conversational channel — it confirms ambiguity inline ("did you mean…?"),
5157
- // never via the blocking AskUserQuestion tool (Agent auto-adds it whenever a host is set). Left in,
5158
- // it stalls a voice turn until the kill-switch. Worker questions still reach the user via parkQuestion.
5159
- askUserQuestion: false,
5160
- systemPrompt: prompt,
5161
- instructionFiles: false,
5162
- maxSteps: 8,
5163
- timeoutMs: 3e4,
5164
- ...o.reflexOptions,
5165
- tools,
5166
- // Composed AFTER the spread so the dispatch guard can't be dropped by reflexOptions.
5167
- hooks: composeHooks(this.dispatchGuard(), o.reflexOptions?.hooks)
5168
- });
5169
- }
5170
- /** Resolve memory tools + inject index into voice system prompt (once). */
5171
- async initMemory() {
5172
- if (!this.memoryReady) return;
5173
- const mem = await this.memoryReady;
5174
- this.memoryReady = void 0;
5175
- this.voice.options.tools.push(...mem.tools);
5176
- if (mem.index) this.voice.options.systemPrompt += "\n\n" + mem.index;
5177
- }
5178
- /** Flush any held-back trailing fragment (a possible `[task` opener that never completed) once the
5179
- * turn's stream is done — so a legit message ending in "[t" isn't silently dropped. */
5180
- flushHeldReflexTail() {
5181
- if (this.fabricationCut) return;
5182
- const tail = this.reflexBuf.slice(this.reflexForwarded);
5183
- this.reflexForwarded = this.reflexBuf.length;
5184
- if (!tail) return;
5185
- if (tail.trim()) this.spokeThisTurn = true;
5186
- this.options.host?.notify?.({ kind: "text_delta", message: tail });
5187
- }
5188
- /** Clear the per-turn guards. Called at the head of every voice turn (user send + re-voice flush). */
5189
- resetTurn() {
5190
- this.turnDispatched = false;
5191
- this.turnBriefs.clear();
5192
- this.spokeThisTurn = false;
5193
- this.heldThisTurn = false;
5194
- this.reflexBuf = "";
5195
- this.reflexForwarded = 0;
5196
- this.fabricationCut = false;
5197
- this.turnFollowUp = false;
5198
- this.voice.options.toolChoice = void 0;
5199
- }
5200
- /** preToolUse guard on the reflex: once it has dispatched this turn, a dispatch is "said my piece,
5201
- * now wait for the push" (CC's Task model). Block the temptations — TaskStatus polling and identical
5202
- * re-dispatch — so the only remaining move is to voice a short ack and end. A genuinely NEW Act is
5203
- * still allowed (parallel independent work). During a re-ack pass, block every tool. */
5204
- dispatchGuard() {
5205
- return {
5206
- preToolUse: (call) => {
5207
- if (this.nudging) return { block: true, reason: "Just say one short spoken acknowledgement \u2014 no tools this turn." };
5208
- if (!this.turnDispatched) return;
5209
- if (call.name === "TaskStatus")
5210
- return { block: true, reason: "You just dispatched a task this turn \u2014 do NOT poll. Give one short spoken acknowledgement and end your turn; the result arrives later as a [task \u2026] event." };
5211
- if ((call.name === "Act" || call.name === "Think") && this.turnBriefs.has(String(call.args?.brief ?? "")))
5212
- return { block: true, reason: "You already dispatched this exact task \u2014 acknowledge briefly and end your turn." };
5213
- }
5214
- };
5215
- }
5216
- /** True when the just-finished turn voiced NOTHING — dead air to repair. Two ways this happens:
5217
- * (a) a dispatch with no spoken ack, and (b) an INLINE turn whose `final` channel came back empty —
5218
- * gpt-oss harmony sometimes puts the whole reply in `analysis` (→ thinking_delta, suppressed in
5219
- * voice) and emits an empty `final`, so no text_delta ever streams. Both ship silence; both repair.
5220
- * Requires a host: without one there's no stream to detect speech on (and no one to speak to). */
5221
- get silentTurn() {
5222
- return !!this.options.host && !this.spokeThisTurn && !this.heldThisTurn;
5223
- }
5224
- /** A turn that voiced nothing is dead air. Re-prompt the reflex ONCE so the LLM itself voices a short
5225
- * line (no template). If it STILL says nothing, fall back to a minimal line so silence never ships.
5226
- * Wording adapts to whether work was dispatched (an ack) or the inline reply was simply lost. */
5227
- async ackIfSilent(fallback) {
5228
- const dispatched = this.turnDispatched;
5229
- this.nudging = true;
5230
- try {
5231
- await this.voice.send(fallback ? "[reminder] You said nothing to the user this turn. Tell them, in ONE short spoken sentence, what just happened \u2014 no tools." : dispatched ? "[reminder] You dispatched a task but said nothing to the user. Say ONE short spoken acknowledgement now \u2014 no tools." : "[reminder] You said nothing to the user this turn. Give your ONE short spoken reply now \u2014 no tools.");
5232
- } catch (e) {
5233
- log10.warn(`ack nudge failed: ${e instanceof Error ? e.message : e}`);
5234
- } finally {
5235
- this.nudging = false;
5236
- }
5237
- if (!this.spokeThisTurn)
5238
- this.options.host?.notify?.({ kind: "text_delta", message: fallback ?? (dispatched ? "Okay, on it." : "Sorry, could you say that again?") });
5239
- }
5240
- /** One user turn: the voice agent streams the reply (and may Act/Think). Serialized with re-voice turns. */
5241
- send(content) {
5242
- return this.enqueue(async () => {
5243
- await this.initMemory();
5244
- this.resetTurn();
5245
- const res = await this.voice.send(content);
5246
- this.flushHeldReflexTail();
5247
- if (this.silentTurn) await this.ackIfSilent();
5248
- return res;
5249
- });
5250
- }
5251
- /** Cancel a running background task — shared by the CancelTask tool and the CLI /tasks picker. */
5252
- cancelTask(id) {
5253
- const rec = this.tasks.get(id);
5254
- if (!rec) return `No task '${id}'.`;
5255
- if (rec.status !== "running") return `Task ${rec.id} is already ${rec.status}.`;
5256
- rec.status = "cancelled";
5257
- rec.controller.abort();
5258
- return `Task ${rec.id} (${rec.label}) cancelled.`;
5259
- }
5260
- /** Barge-in: the user took the floor while task(s) were running. Suppress those tasks' remaining SPOKEN
5261
- * delivery so a superseded topic never talks over the new one (the debt-after-jokes regression). The
5262
- * tasks keep running and still fold their result into the transcript — recoverable, just not spoken.
5263
- * Returns the parked ids (for logging). Does NOT cancel: that's a deliberate reflex/user action. */
5264
- parkInFlightDeliveries() {
5265
- const parked = [];
5266
- for (const rec of this.tasks.values())
5267
- if (rec.status === "running" && !rec.deliveryParked) {
5268
- rec.deliveryParked = true;
5269
- parked.push(rec.id);
5270
- }
5271
- return parked;
5272
- }
5273
- /** Resolve when all queued voice turns AND all in-flight worker tasks have settled (tests, graceful shutdown). */
5274
- async idle() {
5275
- while (true) {
5276
- const q = this.queue;
5277
- await q.catch(() => {
5278
- });
5279
- await Promise.all([...this.tasks.values()].map((t) => t.promise));
5280
- if (this.queue === q && ![...this.tasks.values()].some((t) => t.status === "running")) return;
5281
- }
5282
- }
5283
- /** Promise-chain mutex: turns run strictly one at a time; a failed turn doesn't poison the chain. */
5284
- enqueue(fn) {
5285
- const run = this.queue.then(fn, fn);
5286
- this.queue = run.then(() => {
5287
- }, () => {
5288
- });
5289
- return run;
5290
- }
5291
- notify(kind, message, data) {
5292
- this.options.host?.notify?.({ kind, message, data });
5293
- }
5294
- /** Queue a `[task …]` event for re-voicing. Events arriving while the voice is busy coalesce into ONE turn.
5295
- * `nonClean` (out-of-band boolean, set by the caller that KNOWS this event integrates a NON-CLEAN outcome)
5296
- * marks the coalesced flush as a non-clean integration turn — turn-eligibility, never inferred from event
5297
- * text and never keyed on a (re-authored) brief string. Any dispatch in such a turn is a follow-up. */
5298
- queueRevoice(event, nonClean = false) {
5299
- this.pendingEvents.push(event);
5300
- if (nonClean) this.pendingNonClean = true;
5301
- if (this.flushQueued) return;
5302
- this.flushQueued = true;
5303
- void this.enqueue(async () => {
5304
- this.flushQueued = false;
5305
- const events = this.pendingEvents.splice(0);
5306
- const nonCleanTurn = this.pendingNonClean;
5307
- this.pendingNonClean = false;
5308
- if (!events.length) return;
5309
- const failed = events.find((e) => /^\[task\b[^\]\n]*\bfailed\b/i.test(e));
5310
- this.resetTurn();
5311
- this.turnFollowUp = nonCleanTurn;
5312
- await this.voice.send(events.join("\n"));
5313
- this.flushHeldReflexTail();
5314
- if (this.silentTurn) await this.ackIfSilent(failed ? "Sorry, that didn't work \u2014 the task failed." : void 0);
5315
- this.notify("revoice_done", "");
5316
- });
5317
- }
5318
- /** The worker's brief: the Act/Think args + a STATIC text snapshot of the recent conversation.
5319
- * Act briefs get a self-verify footer — the worker's report is trusted without review, so it
5320
- * must check its own work before reporting (nearly free under prompt caching; measured honest:
5321
- * it does NOT fix one-shot logic bugs — see mind/10). Think tasks are pure reasoning — no footer. */
5322
- buildBrief(brief, tier = "act", deliver = true) {
5323
- const recent = this.voice.transcript.filter((m) => (m.role === "user" || m.role === "assistant") && contentText(m.content).trim()).slice(-this.options.excerptTurns).map((m) => `${m.role}: ${contentText(m.content)}`).join("\n");
5324
- const verify = tier === "act" ? "\n\nBefore reporting done: re-read what you changed and check it against EVERY requirement above \u2014 fix any gap first. Your report is trusted without review." : "";
5325
- const deliverContract = deliver ? "\n\n## DELIVER (spoken delivery)\nYou are reporting back to a user who is LISTENING. Stream your work normally \u2014 your prose is the written work record and detail, and is NOT spoken. Wrap anything the user should HEAR in <spoken>\u2026</spoken> tags. LEAD WITH the actual content they asked for: if they asked for a specific piece of content \u2014 a value, a name, the actual lines, the writing itself \u2014 that content goes INSIDE the <spoken> tags, not a remark about it. Your FIRST <spoken> segment is substantive \u2014 never a greeting or an acknowledgement (the front-end has already acked; do not double-ack). Keep spoken text concise and natural for the ear: short sentences, no markdown." + (this.options.emotionTags ? " Inside <spoken>, you may prefix a sentence with an inline [emotion] tag (e.g. [excited], [curious]) to color how it is voiced \u2014 only when it genuinely fits, and vary it; [laughter] gives a natural laugh." : "") : "";
5326
- return (recent ? `${brief}
5327
-
5328
- ## Recent conversation (for context)
5329
- ${recent}` : brief) + verify + deliverContract;
5330
- }
5331
- /** Spawn a detached worker for task `id`; its settlement notifies + enqueues the re-voice turn. */
5332
- spawnWorker(id, label, briefText, tier, brief, followUp) {
5333
- const o = this.options;
5334
- const tierOpts = tier === "think" ? o.thinkOptions : o.actOptions;
5335
- const tierModel = tier === "think" ? o.thinkModel : o.actModel;
5336
- const controller = new AbortController();
5337
- const base = tierOpts?.hooks ?? o.actOptions?.hooks;
5338
- const report = o.progressUpdates ? this.progressReporter(id) : void 0;
5339
- const tail = [];
5340
- const pushTail = (line) => {
5341
- tail.push(line.slice(0, 200));
5342
- if (tail.length > 120) tail.splice(0, tail.length - 120);
5343
- };
5344
- const hooks = {
5345
- ...base,
5346
- preToolUse: async (call, meta) => {
5347
- const d = await base?.preToolUse?.(call, meta);
5348
- pushTail(`\u2699 ${describeCall(call)}`);
5349
- report?.pre(call);
5350
- return d;
5351
- },
5352
- postToolUse: async (call, result, meta) => {
5353
- await base?.postToolUse?.(call, result, meta);
5354
- const last = result?.trim().split("\n").filter(Boolean).pop();
5355
- if (last) pushTail(` \u21B3 ${last}`);
5356
- report?.post(call);
5357
- },
5358
- onToolOutput: (call, chunk, meta) => {
5359
- base?.onToolOutput?.(call, chunk, meta);
5360
- report?.output(chunk);
5361
- }
5362
- };
5363
- const relayAsk = async (q) => {
5364
- const opts = q.options?.length ? ` Options: ${q.options.map((x) => x.label).join(", ")}.` : "";
5365
- const a = await this.parkQuestion(id, `${q.question}${opts}`);
5366
- return a || "(no answer from the user \u2014 use your best judgment and note the assumption)";
5367
- };
5368
- const splitter = new SpokenSplitter();
5369
- const speak = (seg) => {
5370
- if (seg && !this.tasks.get(id)?.deliveryParked) o.host?.notify?.({ kind: "speak_utterance", message: seg });
5371
- };
5372
- const coalescer = new SentenceCoalescer();
5373
- const feedSpoken = (s) => {
5374
- const ready = coalescer.feed(s);
5375
- if (ready) speak(ready);
5376
- };
5377
- const flushSpoken = () => speak(coalescer.flush());
5378
- const askBridge = o.askRelay ? { ask: relayAsk } : o.host?.ask ? { ask: (q) => o.host.ask(q) } : {};
5379
- const workerHost = {
5380
- ...askBridge,
5381
- notify: (ev) => {
5382
- if (ev?.kind === "text_delta" && typeof ev.message === "string") {
5383
- const { spoken, detail } = splitter.feed(ev.message);
5384
- feedSpoken(spoken);
5385
- if (detail.trim()) pushTail(detail.trim());
5386
- return;
5387
- }
5388
- }
5389
- };
5390
- const agentOpts = {
5391
- ai: o.ai,
5392
- fs: o.fs,
5393
- model: tierModel,
5394
- ...tier === "think" ? { reasoning: tierOpts?.reasoning ?? "high" } : {},
5395
- ...tierOpts,
5396
- // Recompute providerOptions for THIS worker's model (after tierOpts so it wins over any inherited
5397
- // main-template value) — prevents cursor-only cwd/cursorSession leaking onto an anthropic worker.
5398
- providerOptions: o.providerOptionsFor?.(tierModel),
5399
- stream: true,
5400
- // worker streams text_delta so the splitter can extract <spoken> live (after tierOpts: never overridden off)
5401
- host: workerHost,
5402
- // carries BOTH ask AND the <spoken>-splitting notify
5403
- ...hooks ? { hooks } : {},
5404
- signal: controller.signal
5405
- // shared with the checker so a cancel tears down both
5406
- };
5407
- const promise = new Agent(agentOpts).run(briefText).then((res) => {
5408
- const { spoken, detail } = splitter.flush();
5409
- feedSpoken(spoken);
5410
- if (detail.trim()) pushTail(detail.trim());
5411
- flushSpoken();
5412
- return res;
5413
- }).then((res) => this.maybeVerify(id, brief, res, tier, agentOpts, askBridge)).then((res) => this.onWorkerSettled(id, res)).catch((err) => this.onWorkerFailed(id, err));
5414
- this.tasks.set(id, { id, label, status: "running", controller, promise, tail, brief, followUp, splitter });
5415
- if (this.tasks.size > this.options.maxTaskRecords)
5416
- for (const [tid, rec] of this.tasks) {
5417
- if (this.tasks.size <= this.options.maxTaskRecords) break;
5418
- if (rec.status !== "running") this.tasks.delete(tid);
5419
- }
5420
- }
5421
- /** Fresh-context check of a successful Act task: a NEW agent (same model/fs/tools, but NO shared
5422
- * conversation context) re-reads the file state against the brief and fixes any gap. The fix lands
5423
- * on the shared fs automatically (workers write fs directly, no overlay), so grading sees the
5424
- * corrected state. Bounded to ONE pass. Off unless `verifyActTasks`; never runs for think/failed/
5425
- * cancelled tasks. Usage is merged so /cost reflects the real (worker + checker) spend. */
5426
- async maybeVerify(id, brief, res, tier, agentOpts, askBridge) {
5427
- if (!this.options.verifyActTasks || tier !== "act" || res.finishReason !== "stop") return res;
5428
- if (this.tasks.get(id)?.status === "cancelled") return res;
5429
- const { stream: _stream, host: _host, ...restOpts } = agentOpts;
5430
- const checkerOpts = {
5431
- ...restOpts,
5432
- ...askBridge.ask ? { host: { ask: askBridge.ask } } : {}
5433
- };
5434
- const checkBrief = `${this.buildBrief(brief, tier, false)}
5435
-
5436
- ## VERIFY MODE
5437
- Another agent just implemented the above. Independently check the CURRENT state of the files against EVERY requirement. Fix any gap you find. If everything is already correct, make NO changes \u2014 do not refactor or improve \u2014 and report "verified".`;
5438
- this.notify("task_verify", `task ${id}: verifying`, { id });
5439
- const cres = await new Agent(checkerOpts).run(checkBrief);
5440
- if (cres.finishReason !== "stop") {
5441
- log10.warn(`task ${id}: verify inconclusive (${cres.finishReason})`);
5442
- this.notify("task_verify", `task ${id}: verify inconclusive (${cres.finishReason})`, { id, finishReason: cres.finishReason });
5443
- }
5444
- const sum = (a = 0, b = 0) => a + b;
5445
- return {
5446
- ...res,
5447
- steps: res.steps + cres.steps,
5448
- // Merge the checker's messages so downstream tool-call/step accounting includes BOTH agents
5449
- // (else a verified task's toolCalls would undercount vs its steps/usage).
5450
- messages: [...res.messages, ...cres.messages],
5451
- usageEstimated: res.usageEstimated || cres.usageEstimated,
5452
- usage: res.usage && cres.usage ? {
5453
- promptTokens: sum(res.usage.promptTokens, cres.usage.promptTokens),
5454
- completionTokens: sum(res.usage.completionTokens, cres.usage.completionTokens),
5455
- totalTokens: sum(res.usage.totalTokens, cres.usage.totalTokens),
5456
- cacheCreationTokens: sum(res.usage.cacheCreationTokens, cres.usage.cacheCreationTokens),
5457
- cacheReadTokens: sum(res.usage.cacheReadTokens, cres.usage.cacheReadTokens)
5458
- } : res.usage ?? cres.usage
5459
- };
5460
- }
5461
- /** Throttled per-task progress: worker tool calls → at most one progress re-voice per interval.
5462
- * Two sources, one throttle: completed steps (post) and a heartbeat for a SINGLE long tool call
5463
- * (pre records the in-flight call; a self-cleaning timer narrates "still inside Bash — 70s").
5464
- * Completion supersedes: nothing is emitted once the task has settled. */
5465
- progressReporter(id) {
5466
- let lastAt = Date.now();
5467
- let steps = 0;
5468
- let inflight = null;
5469
- const due = () => {
5470
- if (this.pendingAsks.size) return void 0;
5471
- const rec = this.tasks.get(id);
5472
- return rec && rec.status === "running" && Date.now() - lastAt >= this.options.progressIntervalMs ? rec : void 0;
5473
- };
5474
- const emit = (rec, line, call) => {
5475
- lastAt = Date.now();
5476
- this.notify("task_progress", `task ${id} (${rec.label}): ${line}`, { id, steps, call: call.name });
5477
- this.queueRevoice(`[task ${id} progress] ${line}`);
5478
- };
5479
- const timer = setInterval(() => {
5480
- const rec = this.tasks.get(id);
5481
- if (!rec || rec.status !== "running") return clearInterval(timer);
5482
- if (!inflight || !due()) return;
5483
- const last = inflight.tail.trim().split("\n").filter(Boolean).pop()?.slice(-80);
5484
- emit(rec, `still inside ${describeCall(inflight.call)} \u2014 ${Math.round((Date.now() - inflight.at) / 1e3)}s on this step${last ? `, last output: ${last}` : ""}`, inflight.call);
5485
- }, Math.max(this.options.progressIntervalMs, 250));
5486
- timer.unref?.();
5487
- return {
5488
- pre: (call) => {
5489
- inflight = { call, at: Date.now(), tail: "" };
5490
- },
5491
- output: (chunk) => {
5492
- if (inflight) inflight.tail = (inflight.tail + chunk).slice(-500);
5493
- },
5494
- // digest only — NEVER re-voices directly
5495
- post: (call) => {
5496
- steps++;
5497
- inflight = null;
5498
- const rec = due();
5499
- if (rec) emit(rec, `still running \u2014 ${steps} steps so far, now: ${describeCall(call)}`, call);
5500
- }
5501
- };
5502
- }
5503
- /** Park a question under `askId` (a task id, or any unique key for permission asks): re-voices
5504
- * '[task <id> asks] …' and resolves with the user's answer via AnswerTask — or '' on timeout/
5505
- * task settle (callers map '' to deny / best-judgment). Workers never block forever. */
5506
- parkQuestion(askId, question) {
5507
- return new Promise((resolve) => {
5508
- let settled = false;
5509
- const finish = (answer) => {
5510
- if (settled) return;
5511
- settled = true;
5512
- clearTimeout(timer);
5513
- this.pendingAsks.delete(askId);
5514
- resolve(answer);
5515
- };
5516
- const timer = setTimeout(() => {
5517
- this.notify("task_ask_timeout", `task ${askId}: question timed out \u2014 proceeding without an answer`);
5518
- finish("");
5519
- }, this.options.askTimeoutMs);
5520
- this.pendingAsks.set(askId, { question, resolve: finish });
5521
- this.notify("task_ask", `task ${askId} asks: ${question}`, { id: askId, question });
5522
- this.queueRevoice(`[task ${askId} asks] ${question}
5523
- (Relay this to the user in your own words. When they answer, call AnswerTask with id "${askId}" and their answer.)`);
5524
- });
5525
- }
5526
- /** Resolve any question a settling/cancelled task left parked (its answer can no longer matter). */
5527
- dropAsk(id) {
5528
- this.pendingAsks.get(id)?.resolve("");
5529
- }
5530
- /** Build the INTEGRATION TURN prompt for a NON-CLEAN settled worker (early stop / failure). A clean
5531
- * success never reaches here — it streams its own `<spoken>` delivery during the run. For a partial
5532
- * or failed result the outcome re-enters the reflex as a decision (like a tool_result flowing back
5533
- * into a normal agent loop): the reflex evaluates the outcome against the original intent and chooses
5534
- * what to do next.
5535
- *
5536
- * Decision branches (the reflex acts on them with EXISTING tools — no new surface):
5537
- * • accept → SPEAK the (partial) result plainly — don't dress a failure up as success.
5538
- * • escalate → call `Think` with the SAME brief — only when Act failed/stalled AND a Think tier
5539
- * exists AND this task wasn't already a follow-up (one hop max). Wires the dead
5540
- * "Reserve Think for a problem Act already FAILED at" promise.
5541
- * • re-delegate→ call `Act` with a CORRECTED brief — for a recoverable error / partial result.
5542
- * • ask → ask the user ONE concrete question if genuinely blocked.
5543
- *
5544
- * Keeps the `[task <id> completed]` / `[task <id> failed]` opener so existing coalescing + the
5545
- * failed-revoice fallback still fire, and the per-event transcript markers stay intact. */
5546
- integrationPrompt(rec, outcome, body, finishReason) {
5547
- const opener = outcome === "error" ? `[task ${rec.id} failed]` : `[task ${rec.id} completed]`;
5548
- const underCap = this.autoEscalations < _DuplexAgent.MAX_AUTO_ESCALATIONS;
5549
- const canEscalate = (outcome === "error" || outcome === "incomplete") && underCap;
5550
- const hasThink = this.options.thinkModel !== false;
5551
- const options = [];
5552
- if (!rec.followUp && canEscalate && hasThink)
5553
- options.push("ESCALATE to the Think tier (call Think with the same brief) if this is a hard/architectural problem the Act worker stalled or failed on");
5554
- if (!rec.followUp && canEscalate)
5555
- options.push("RE-DELEGATE to Act with a corrected brief if the failure looks recoverable (a wrong path, a fixable mistake)");
5556
- options.push("ASK the user one short, concrete question if you genuinely cannot proceed without their input");
5557
- options.push("ACCEPT and tell the user plainly what happened (don't dress a failure up as success)");
5558
- const decision = options.length > 1 ? ` You must decide what to do next \u2014 choose ONE: ${options.map((o, i) => `(${i + 1}) ${o}`).join("; ")}. Pick exactly one and act on it; do not voice this as a finished success.` : ` Tell the user plainly what happened \u2014 do not present this as a finished success.`;
5559
- const state = outcome === "error" ? `the worker FAILED with: ${body}` : `the worker STOPPED EARLY (${finishReason}) \u2014 its result is PARTIAL, not a finished success: ${body}`;
5560
- return `${opener} Original request: "${rec.brief}". Outcome: ${state}.${decision}`;
5165
+ /** Resolve when the engine is no longer speaking (immediate if already idle). */
5166
+ awaitIdle() {
5167
+ if (this.state !== "speaking" && this.state !== "thinking") return Promise.resolve();
5168
+ return new Promise((r) => this.idleWaiters.push(r));
5561
5169
  }
5562
- onWorkerSettled(id, res) {
5563
- this.dropAsk(id);
5564
- const rec = this.tasks.get(id);
5565
- if (res.finishReason === "aborted" || rec.status === "cancelled") {
5566
- rec.status = "cancelled";
5567
- this.notify("task_cancelled", `task ${id} (${rec.label}) cancelled`);
5568
- return;
5170
+ // --- speaking side (host-driven) ---
5171
+ /** open a spoken turn (idempotent — safe from both onUtterance and first-delta paths).
5172
+ * `ack` speaks the configured micro-ack as the context opener (utterance path only —
5173
+ * masks LLM TTFT; re-voice turns begun by their first delta skip it). */
5174
+ beginSpeech(ack = false) {
5175
+ if (this.speaking && this.ctxOpen) return;
5176
+ if (this.drainTimer) {
5177
+ this.clock.clearTimeout(this.drainTimer);
5178
+ this.drainTimer = null;
5569
5179
  }
5570
- if (res.finishReason === "error") {
5571
- const msg = res.error instanceof Error ? res.error.message : String(res.error ?? "unknown error");
5572
- return this.failTask(rec, msg);
5180
+ this.interrupted = false;
5181
+ this.bcSupersede();
5182
+ this.resetOverlap(true);
5183
+ this.diag("tts_turn_begin", { ack, gapless: this.speaking });
5184
+ if (!this.speaking) this.player.markTurn();
5185
+ this.speaking = true;
5186
+ this.ctxOpen = true;
5187
+ this.spokeDeltas = false;
5188
+ this.reply = "";
5189
+ this.emo = this.options.emotions ? new EmotionStream(this.options.showEmotions) : null;
5190
+ this.echoWords = new Set(this.words(this.prevReply));
5191
+ this.tts.newContext();
5192
+ this.clearAckTimer("new_turn");
5193
+ if (ack && this.options.ackPhrase) {
5194
+ this.diag("ack_fired", { phrase: this.options.ackPhrase, adaptive: false });
5195
+ this.tts.speak(this.options.ackPhrase + " ", true);
5196
+ this.spokeDeltas = true;
5197
+ this.ackAt = this.clock.now();
5198
+ this.lastAck = this.options.ackPhrase;
5199
+ for (const w of this.words(this.options.ackPhrase)) this.echoWords.add(w);
5200
+ } else if (ack && this.options.adaptiveAckMs > 0) {
5201
+ this.ackTimer = this.clock.setTimeout(() => {
5202
+ this.ackTimer = null;
5203
+ if (!this.speaking || !this.ctxOpen || this.spokeDeltas || this.interrupted) return;
5204
+ const phrase = this.pickAck();
5205
+ this.diag("ack_fired", { phrase, adaptive: true, waitedMs: this.options.adaptiveAckMs });
5206
+ this.tts.speak(phrase + " ", true);
5207
+ this.spokeDeltas = true;
5208
+ this.ackAt = this.clock.now();
5209
+ this.lastAck = phrase;
5210
+ for (const w of this.words(phrase)) this.echoWords.add(w);
5211
+ this.setState("speaking");
5212
+ this.options.onAdaptiveAck();
5213
+ }, this.options.adaptiveAckMs);
5573
5214
  }
5574
- rec.status = "done";
5575
- rec.result = res.text;
5576
- const incomplete = res.finishReason !== "stop";
5577
- log10.verbose(`task ${id} done (${res.steps} steps${incomplete ? `, INCOMPLETE: ${res.finishReason}` : ""})`);
5578
- this.notify("task_done", `task ${id} (${rec.label}) completed`, {
5579
- id,
5580
- text: res.text,
5581
- usage: res.usage,
5582
- usageEstimated: res.usageEstimated,
5583
- finishReason: res.finishReason,
5584
- steps: res.steps,
5585
- toolCalls: res.messages.filter((m) => m.role === "tool").length
5586
- });
5587
- if (incomplete) {
5588
- return this.queueRevoice(this.integrationPrompt(rec, "incomplete", res.text, res.finishReason), true);
5215
+ if (!this.turnStartAt) this.turnStartAt = this.clock.now();
5216
+ this.setState("thinking");
5217
+ }
5218
+ /** Feed a spoken delta. Returns the on-screen echo text (emotion tags shown/hidden per config) so the
5219
+ * host renders the SAME stream that was parsed for TTS — no second, state-doubling parse. */
5220
+ speakDelta(text) {
5221
+ if (this.interrupted) return "";
5222
+ this.clearAckTimer("first_delta");
5223
+ if (!this.speaking || !this.ctxOpen) this.beginSpeech();
5224
+ let { speech, display, prose } = this.emo ? this.emo.feed(text) : { speech: text, display: text, prose: text };
5225
+ if (this.reply && /[.!?…]$/.test(this.reply) && /^[A-Z]/.test(prose)) {
5226
+ speech = " " + speech;
5227
+ display = " " + display;
5228
+ prose = " " + prose;
5589
5229
  }
5590
- const tail = rec.splitter?.flush();
5591
- if (tail?.spoken && !rec.deliveryParked) this.options.host?.notify?.({ kind: "speak_utterance", message: tail.spoken });
5592
- if (res.text.trim()) this.voice.transcript.push({ role: "assistant", content: res.text });
5593
- if (!rec.splitter?.spokeAny && res.text.trim() && !rec.deliveryParked)
5594
- this.options.host?.notify?.({ kind: "speak_utterance", message: res.text });
5230
+ this.reply += prose;
5231
+ if (prose.trim()) this.repliedSinceDispatch = true;
5232
+ for (const w of this.words(this.reply)) this.echoWords.add(w);
5233
+ this.tts.speak(forSpeech(speech), true);
5234
+ if (!this.spokeDeltas && this.turnStartAt) log10.debug(`ttft: ${Math.round(this.clock.now() - this.turnStartAt)}ms`);
5235
+ this.spokeDeltas = true;
5236
+ this.setState("speaking");
5237
+ return display;
5595
5238
  }
5596
- onWorkerFailed(id, err) {
5597
- this.failTask(this.tasks.get(id), err instanceof Error ? err.message : String(err));
5239
+ /** close the spoken turn (idempotent); stays audible until ALL audio arrived AND playback drains */
5240
+ endSpeech() {
5241
+ this.interrupted = false;
5242
+ this.clearAckTimer("turn_end");
5243
+ if (!this.speaking) return;
5244
+ this.diag("tts_turn_end", { spoke: this.spokeDeltas, replyChars: this.reply.length });
5245
+ this.ctxOpen = false;
5246
+ if (this.emo) {
5247
+ const t = this.emo.flush();
5248
+ this.emo = null;
5249
+ if (t.prose) this.reply += t.prose;
5250
+ if (t.speech) this.tts.speak(forSpeech(t.speech), true);
5251
+ }
5252
+ if (this.reply) this.prevReply = this.reply;
5253
+ const settle = () => {
5254
+ if (this.ctxOpen) {
5255
+ this.drainTimer = null;
5256
+ return;
5257
+ }
5258
+ if (this.pausedAt) {
5259
+ this.drainTimer = this.clock.setTimeout(settle, 250);
5260
+ return;
5261
+ }
5262
+ this.drainTimer = null;
5263
+ this.speaking = false;
5264
+ if (this.turnStartAt) log10.debug(`turn: ${Math.round(this.clock.now() - this.turnStartAt)}ms (incl. playback)`);
5265
+ this.echoUntil = this.clock.now() + 2500;
5266
+ if (!this.usingAec) this.stt.reset();
5267
+ this.setState("listening");
5268
+ if (this.uttQueue.length) this.pumpQueue();
5269
+ };
5270
+ const drainThenSettle = () => {
5271
+ if (this.drainTimer) this.clock.clearTimeout(this.drainTimer);
5272
+ this.drainTimer = this.clock.setTimeout(settle, this.player.drainMs() + 300);
5273
+ };
5274
+ if (this.spokeDeltas) {
5275
+ this.tts.onDone = drainThenSettle;
5276
+ this.tts.end();
5277
+ if (this.drainTimer) this.clock.clearTimeout(this.drainTimer);
5278
+ this.drainTimer = this.clock.setTimeout(drainThenSettle, 15e3);
5279
+ } else drainThenSettle();
5598
5280
  }
5599
- failTask(rec, msg) {
5600
- this.dropAsk(rec.id);
5601
- rec.status = "error";
5602
- rec.result = msg;
5603
- log10.warn(`task ${rec.id} failed: ${msg}`);
5604
- this.notify("task_error", `task ${rec.id} (${rec.label}) failed: ${msg}`);
5605
- this.queueRevoice(this.integrationPrompt(rec, "error", msg, "error"), true);
5281
+ /** text of the reply cut by the last barge-in — consumed by the host to tell the model what
5282
+ * the user did NOT hear. Cleared on read. */
5283
+ takeInterruptedReply() {
5284
+ const r = this.lastInterrupted;
5285
+ this.lastInterrupted = null;
5286
+ return r;
5606
5287
  }
5607
- // --- voice tools (closures over this instance) ---
5608
- /** Live-switch the think tier: `false` disables (removes the Think tool from the voice agent),
5609
- * a model id enables (adds the tool if missing). The system-prompt THINK_SLOT text is frozen at
5610
- * construction — the tool's own description carries the routing guidance, so a live enable works;
5611
- * dispatch()'s think→act fallback covers any straggler calls after a live disable. */
5612
- setThinkModel(model) {
5613
- this.options.thinkModel = model;
5614
- const tools = this.voice.options.tools;
5615
- const i = tools.findIndex((t) => t.name === "Think");
5616
- if (model === false && i >= 0) tools.splice(i, 1);
5617
- else if (model !== false && i < 0) tools.push(this.thinkTool());
5288
+ /** Speak a short filler phrase without starting a model turn (stays in listening mode after). */
5289
+ speakFiller(text) {
5290
+ if (!text || this.speaking) return;
5291
+ const replied = this.repliedSinceDispatch;
5292
+ this.beginSpeech();
5293
+ this.speakDelta(text);
5294
+ this.endSpeech();
5295
+ this.repliedSinceDispatch = replied;
5618
5296
  }
5619
- /** User/programmatic spawn: the CLI's /act and /think commands. Returns the task id.
5620
- * `followUp` marks an automatic escalation/re-delegation (set by the integration turn) so the new
5621
- * task's own integration turn won't escalate again — capping auto-follow-ups to one hop. */
5622
- async dispatch(brief, tier = "act", label, followUp = false) {
5623
- if (tier === "think" && this.options.thinkModel === false) tier = "act";
5624
- if (followUp) this.autoEscalations++;
5625
- const id = `t${++this.seq}`;
5626
- const lbl = label ?? tier;
5627
- await this.options.onTaskStart?.(id, lbl);
5628
- this.spawnWorker(id, lbl, this.buildBrief(brief, tier), tier, brief, followUp);
5629
- this.notify("task_started", `task ${id} (${lbl}) started`, { id, brief, tier });
5630
- return id;
5297
+ /** Enqueue a COMPLETE worker utterance (already-split spoken text) onto the central speech queue.
5298
+ * If nothing is currently speaking it plays immediately; otherwise it queues and plays after the
5299
+ * current utterance fully ends (settle → pumpQueue) — never spliced into an open reflex utterance. */
5300
+ enqueueUtterance(text) {
5301
+ if (!text || !/[\p{L}\p{N}]/u.test(text)) return;
5302
+ this.diag("enqueue_utterance", { chars: text.length, queued: this.uttQueue.length, speaking: this.speaking });
5303
+ this.uttQueue.push(text);
5304
+ if (!this.speaking) this.pumpQueue();
5631
5305
  }
5632
- actTool() {
5633
- return {
5634
- name: "Act",
5635
- description: 'Escalate real work (reading/editing files, searching, running tasks, building) to a standard background worker. Returns immediately with a task id; the result arrives later as a "[task <id> completed]" event. Provide a clear, self-contained `brief` (the worker does not hear the live conversation).',
5636
- parameters: {
5637
- type: "object",
5638
- required: ["brief"],
5639
- properties: {
5640
- brief: { type: "string", description: "full, self-contained instructions for the worker" },
5641
- label: { type: "string", description: "a short (2-4 word) label for the task" }
5642
- }
5643
- },
5644
- run: async ({ brief, label }) => {
5645
- this.turnDispatched = true;
5646
- this.turnBriefs.add(String(brief ?? ""));
5647
- this.voice.options.toolChoice = "none";
5648
- const id = await this.dispatch(String(brief ?? ""), "act", label ? String(label) : void 0, this.turnFollowUp);
5649
- return `Acting on task ${id}. Acknowledge briefly; the result will arrive as a [task ${id} completed] event.`;
5650
- }
5651
- };
5306
+ /** Play the next queued worker utterance as its own one-shot turn (begin → delta → end). Drives the
5307
+ * next one from the settle completion (endSpeech), so utterances serialize without overlap. */
5308
+ pumpQueue() {
5309
+ if (this.speaking) return;
5310
+ const text = this.uttQueue.shift();
5311
+ if (text == null) return;
5312
+ this.beginSpeech();
5313
+ this.speakDelta(text);
5314
+ this.endSpeech();
5652
5315
  }
5653
- thinkTool() {
5654
- return {
5655
- name: "Think",
5656
- description: "Escalate to a premium deep-reasoning agent for complex analysis, architecture decisions, hard debugging, or planning. Same async pattern as Act \u2014 returns a task id. Use when the problem needs careful thought before (or instead of) action. Do not use Think for simple tasks \u2014 Act is cheaper and faster.",
5657
- parameters: {
5658
- type: "object",
5659
- required: ["brief"],
5660
- properties: {
5661
- brief: { type: "string", description: "the question or problem to reason about deeply" },
5662
- label: { type: "string", description: "a short (2-4 word) label for the task" }
5663
- }
5664
- },
5665
- run: async ({ brief, label }) => {
5666
- this.turnDispatched = true;
5667
- this.turnBriefs.add(String(brief ?? ""));
5668
- this.voice.options.toolChoice = "none";
5669
- const id = await this.dispatch(String(brief ?? ""), "think", label ? String(label) : void 0, this.turnFollowUp);
5670
- return `Thinking on task ${id}. Acknowledge briefly; the result will arrive as a [task ${id} completed] event.`;
5671
- }
5672
- };
5316
+ /** Short varied adaptive acks (adaptiveAckMs) — two shape pools picked by the dispatched
5317
+ * utterance (question → thinking-ish, otherwise neutral/on-it), with anti-repetition (never one
5318
+ * of the last 4 used). A 3-phrase round-robin sounded synthetic live ("started with 'hmm' too
5319
+ * many times"). All phrases are sub-second and semantically safe for their shape.
5320
+ * No 'Okay.': a genuine user "Okay." within the 6s squash window would be swallowed as ack echo. */
5321
+ static ACKS_NEUTRAL = ["Mm-hm.", "One sec.", "Right.", "Sec.", "On it.", "Sure, moment.", "Uh, one moment.", "Let me see."];
5322
+ static ACKS_QUESTION = ["Hmm.", "Let me think.", "Hm, let me see.", "Good question.", "Mm, one sec.", "Let's see."];
5323
+ recentAcks = [];
5324
+ pickAck() {
5325
+ const pool = this.lastDispatchWasQuestion ? _VoiceEngine.ACKS_QUESTION : _VoiceEngine.ACKS_NEUTRAL;
5326
+ const fresh = pool.filter((p) => !this.recentAcks.includes(p));
5327
+ const phrase = (fresh.length ? fresh : pool)[Math.floor(Math.random() * (fresh.length ? fresh.length : pool.length))];
5328
+ this.recentAcks.push(phrase);
5329
+ if (this.recentAcks.length > 4) this.recentAcks.shift();
5330
+ return phrase;
5331
+ }
5332
+ lastDispatchWasQuestion = false;
5333
+ clearAckTimer(reason) {
5334
+ if (!this.ackTimer) return;
5335
+ this.clock.clearTimeout(this.ackTimer);
5336
+ this.ackTimer = null;
5337
+ if (reason) this.diag("ack_cancelled", { reason });
5338
+ }
5339
+ /** Cancel a pending adaptive ack without touching the turn — hosts call this when the turn turns out
5340
+ * to be a Hold (intentionally silent; an ack would read as the start of an answer). */
5341
+ cancelPendingAck() {
5342
+ this.clearAckTimer("hold");
5673
5343
  }
5674
- taskStatusTool() {
5675
- return {
5676
- name: "TaskStatus",
5677
- description: "Status of background tasks. Pass `id` for one task, or omit it to list all.",
5678
- parameters: { type: "object", properties: { id: { type: "string" } } },
5679
- run: async ({ id }) => {
5680
- const list = id ? [this.tasks.get(String(id))].filter(Boolean) : [...this.tasks.values()];
5681
- if (!list.length) return id ? `No task '${id}'.` : "No background tasks.";
5682
- return list.map((t) => `${t.id} (${t.label}): ${t.status}`).join("\n");
5683
- }
5684
- };
5344
+ /** barge-in: stop audio NOW, cancel generation, reset for the user's utterance */
5345
+ interrupt() {
5346
+ this.clearAckTimer("interrupt");
5347
+ if (this.uttQueue.length) log10.info(`barge-in dropped ${this.uttQueue.length} queued worker utterance(s)`);
5348
+ const droppedQueued = this.uttQueue.length;
5349
+ this.uttQueue = [];
5350
+ if (!this.speaking && !this.drainTimer) return;
5351
+ this.diag("interrupt", { droppedQueued, ctxOpen: this.ctxOpen, playedMs: Math.round(Math.max(0, this.player.playedMs())) });
5352
+ if (this.drainTimer) {
5353
+ this.clock.clearTimeout(this.drainTimer);
5354
+ this.drainTimer = null;
5355
+ }
5356
+ this.resetOverlap(false);
5357
+ this.lastResumeAt = 0;
5358
+ const heardChars = Math.round(Math.max(0, this.player.playedMs()) / 1e3 * 15);
5359
+ if (this.reply) this.lastInterrupted = { full: this.reply, heard: this.reply.slice(0, heardChars) };
5360
+ this.speaking = false;
5361
+ this.ctxOpen = false;
5362
+ this.interrupted = true;
5363
+ this.suspectUntil = 0;
5364
+ this.echoUntil = this.clock.now() + Math.max(2500, this.player.drainMs() + 3e3);
5365
+ this.tts.cancel();
5366
+ this.player.kill();
5367
+ if (!this.usingAec) this.stt.reset();
5368
+ if (this.reply) this.prevReply = this.reply;
5369
+ this.setState("listening");
5685
5370
  }
5686
- /** Sub-100ms read-only lookups the voice may do itself — everything else stays Act-only.
5687
- * fs-only (no shell; the engine is VFS-abstracted): time, git branch (.git/HEAD read), ls, file
5688
- * head. Output is hard-capped so a lookup can never bloat the skinny voice context. */
5689
- quickLookTool() {
5690
- const CAP = 2e3;
5691
- const kinds = [.../* @__PURE__ */ new Set(["time", "branch", "ls", "file", "capabilities", ...Object.keys(this.options.quickLook ?? {})])];
5692
- return {
5693
- name: "QuickLook",
5694
- description: `Instant read-only lookup \u2014 one of: ${kinds.join(", ")}. For trivial facts only; anything needing search, commands, or reasoning goes through Act.`,
5695
- parameters: {
5696
- type: "object",
5697
- required: ["what"],
5698
- properties: {
5699
- what: { type: "string", enum: kinds, description: "what to look up" },
5700
- path: { type: "string", description: "for ls/file: the path to look at" }
5701
- }
5702
- },
5703
- run: async ({ what, path }) => {
5704
- const fs = this.options.fs;
5705
- try {
5706
- const over = this.options.quickLook?.[String(what)];
5707
- if (over) return await over(path ? String(path) : void 0);
5708
- switch (String(what)) {
5709
- case "capabilities": {
5710
- const actTools = this.options.actOptions?.tools ?? [];
5711
- const names = actTools.map((t) => t.name);
5712
- const mcpServers = Object.keys(this.options.actOptions?.providerOptions?.mcpServers ?? {});
5713
- const mcpNote = mcpServers.length ? ` Plus MCP servers your worker can use: ${mcpServers.join(", ")} (e.g. browser-bridge \u2192 drive a real browser: open tabs, navigate, click, screenshot).` : "";
5714
- if (!names.length)
5715
- return "Your worker uses Act's default local toolset (reading/editing files, running shell commands). No extra tools (e.g. web/internet) are configured; if a request is not a basic file or shell operation, assume you can't do it and say so." + mcpNote;
5716
- const hasFetch = names.some((n) => /WebFetch/i.test(n));
5717
- const hasBrowser = names.some((n) => /browser.*(navigate|click|page|type)/i.test(n));
5718
- const hasSearch = names.some((n) => /(^|_)WebSearch$|search/i.test(n) && !/WebFetch|browser/i.test(n));
5719
- const notes = [];
5720
- if (hasFetch) notes.push("WebFetch retrieves ONE specific URL you are given \u2014 it is not a search engine.");
5721
- if (hasBrowser) notes.push("The browser tools drive a real browser: you CAN open a site and, if needed, navigate to a search engine and search there \u2014 but it is manual and takes a moment, not an instant lookup.");
5722
- else if (!hasSearch && hasFetch) notes.push('You have no general web-search tool, so for an instant "search the web" you can only fetch a URL they provide.');
5723
- const webNote = notes.length ? " NOTE: " + notes.join(" ") : "";
5724
- return `Tools your background worker (Act) can actually use: ${names.join(", ")}. Read each name literally and match the request to a SPECIFIC tool; if none fits, you do NOT have that ability \u2014 say so honestly.` + webNote + mcpNote;
5725
- }
5726
- case "time":
5727
- return (/* @__PURE__ */ new Date()).toString();
5728
- case "branch": {
5729
- if (!fs) return "unavailable (no filesystem)";
5730
- const head = (await fs.readFile(".git/HEAD")).trim();
5731
- return head.startsWith("ref: refs/heads/") ? `branch: ${head.slice("ref: refs/heads/".length)}` : `detached HEAD at ${head.slice(0, 12)}`;
5732
- }
5733
- case "ls": {
5734
- if (!fs) return "unavailable (no filesystem)";
5735
- const names = await fs.readDir(String(path ?? "."));
5736
- return names.slice(0, 50).join("\n") + (names.length > 50 ? `
5737
- \u2026 (+${names.length - 50} more)` : "");
5738
- }
5739
- case "file": {
5740
- if (!fs) return "unavailable (no filesystem)";
5741
- if (!path) return "file lookup needs a path";
5742
- const text = await fs.readFile(String(path));
5743
- return text.length > CAP ? text.slice(0, CAP) + `
5744
- \u2026 (truncated \u2014 ${text.length} chars total; Act for the full file)` : text;
5745
- }
5746
- default:
5747
- return `unknown lookup '${what}'`;
5371
+ stop() {
5372
+ this.uttQueue = [];
5373
+ this.clearAckTimer();
5374
+ if (this.resumeTimer) this.clock.clearTimeout(this.resumeTimer);
5375
+ if (this.pendingTimer) this.clock.clearTimeout(this.pendingTimer);
5376
+ if (this.drainTimer) this.clock.clearTimeout(this.drainTimer);
5377
+ if (this.specTimer) this.clock.clearTimeout(this.specTimer);
5378
+ this.bcSupersede();
5379
+ this.stt.stop();
5380
+ this.player.kill();
5381
+ this.tts.close();
5382
+ this.setState("idle");
5383
+ }
5384
+ // --- listening side (STT-driven) ---
5385
+ words(s) {
5386
+ return s.toLowerCase().replace(/[^a-z0-9\s]/g, "").split(/\s+/).filter((w) => w.length >= 2);
5387
+ }
5388
+ novelWords(text) {
5389
+ return this.words(text).filter((w) => !this.echoWords.has(w));
5390
+ }
5391
+ echoActive() {
5392
+ return this.speaking || this.clock.now() < this.echoUntil;
5393
+ }
5394
+ /** Genuine user speech vs our own bleed (AEC tier): novel words must DOMINATE, not merely exist.
5395
+ * Degraded AEC + an STT mis-hearing manufactures a single novel word out of pure echo (a name or
5396
+ * rare word in our own reply comes back transcribed slightly differently — 1 novel / N words).
5397
+ * A real interjection is mostly novel ("stop", "wait what") — short utterances pass on ratio,
5398
+ * longer ones on count. */
5399
+ genuine(text) {
5400
+ const total = this.words(text).length;
5401
+ const novel = this.novelWords(text).length;
5402
+ return novel > 0 && novel / Math.max(1, total) > 0.5;
5403
+ }
5404
+ handlePartial(text) {
5405
+ if (this.speaking) {
5406
+ if (!this.options.bargeIn) return;
5407
+ if (this.clock.now() < this.bargeGraceUntil) {
5408
+ if (this.lastGraceDiag !== this.bargeGraceUntil) {
5409
+ this.lastGraceDiag = this.bargeGraceUntil;
5410
+ this.diag("grace_suppress", { text: text.slice(0, 60) });
5411
+ }
5412
+ if (!this.echoActive() || (this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) this.options.onPartial(text);
5413
+ return;
5414
+ }
5415
+ if (this.overlapCapable) {
5416
+ const txt = text.trim();
5417
+ if (!txt || txt === this.lastOverlapPartial) return;
5418
+ this.lastOverlapPartial = txt;
5419
+ if (!this.genuine(txt)) {
5420
+ if (this.pausedAt) this.armResume();
5421
+ return;
5422
+ }
5423
+ if (!this.pausedAt) {
5424
+ this.pausedAt = this.clock.now();
5425
+ const sinceResumeMs = this.lastResumeAt ? Math.round(this.clock.now() - this.lastResumeAt) : void 0;
5426
+ this.diag("overlap_pause", { text: txt.slice(0, 60), sinceResumeMs });
5427
+ this.player.pause();
5428
+ if (this.lastResumeAt && this.clock.now() - this.lastResumeAt < this.options.overlapRepauseCedeMs) {
5429
+ const phase = this.ctxOpen ? "speaking" : "drain";
5430
+ this.diag("barge_in", { phase, trigger: "overlap_repause", sinceResumeMs });
5431
+ this.interrupt();
5432
+ this.options.onBargeIn(phase);
5433
+ return;
5748
5434
  }
5749
- } catch (e) {
5750
- return `lookup failed: ${e?.message ?? e}`;
5751
5435
  }
5436
+ if (this.words(txt).length >= 2) {
5437
+ const phase = this.ctxOpen ? "speaking" : "drain";
5438
+ this.diag("barge_in", { phase, trigger: "overlap_cede", novel: this.novelWords(txt).length, total: this.words(txt).length });
5439
+ this.interrupt();
5440
+ this.options.onBargeIn(phase);
5441
+ return;
5442
+ }
5443
+ this.armResume();
5444
+ return;
5752
5445
  }
5753
- };
5754
- }
5755
- answerTaskTool() {
5756
- return {
5757
- name: "AnswerTask",
5758
- description: `Relay the user's answer to a pending question from a background task (the "[task <id> asks]" events). Pass the id from the event and the user's answer.`,
5759
- parameters: {
5760
- type: "object",
5761
- required: ["id", "answer"],
5762
- properties: { id: { type: "string" }, answer: { type: "string", description: "the user's answer, verbatim or faithfully summarized" } }
5763
- },
5764
- run: async ({ id, answer }) => {
5765
- const ask = this.pendingAsks.get(String(id));
5766
- if (!ask) return `No pending question for '${id}' \u2014 it may have been answered already or timed out.`;
5767
- ask.resolve(String(answer ?? ""));
5768
- return `Answer relayed \u2014 task ${id} resumes.`;
5446
+ const barge = this.usingAec ? this.genuine(text) : this.novelWords(text).length >= (this.suspectUntil ? 1 : 2);
5447
+ if (barge) {
5448
+ const phase = this.ctxOpen ? "speaking" : "drain";
5449
+ this.diag("barge_in", { phase, trigger: this.usingAec ? "genuine" : "heuristic_novel", novel: this.novelWords(text).length, total: this.words(text).length });
5450
+ this.interrupt();
5451
+ this.options.onBargeIn(phase);
5769
5452
  }
5770
- };
5453
+ return;
5454
+ }
5455
+ if (this.pendingUtt && text.trim()) {
5456
+ if (this.pendingTimer) this.clock.clearTimeout(this.pendingTimer);
5457
+ this.pendingTimer = this.clock.setTimeout(() => this.flushUtterance(), Math.max(800, this.options.utteranceMergeMs));
5458
+ }
5459
+ this.trackSpeculation(text);
5460
+ this.trackBackchannel(text);
5461
+ if (!this.echoActive() || (this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) this.options.onPartial(text);
5771
5462
  }
5772
- holdTool() {
5773
- return {
5774
- name: "Hold",
5775
- description: 'The user seems mid-thought \u2014 hold the turn (stay listening) instead of answering. Optionally pass a short filler ("mhm", "go on") to speak while waiting. Use when the message sounds incomplete, trailing off, or like they paused to think.',
5776
- parameters: {
5777
- type: "object",
5778
- properties: {
5779
- filler: { type: "string", description: 'optional short filler to speak ("mhm", "go on", "mm-hm")' }
5780
- }
5781
- },
5782
- run: async ({ filler }) => {
5783
- this.heldThisTurn = true;
5784
- if (filler) this.notify("hold_filler", String(filler));
5785
- return "Holding \u2014 listening for the rest of the user's thought. Do not respond further this turn.";
5463
+ /** Speculative-reflex trigger (listening side only — the speaking branch returns before this): a
5464
+ * partial that hasn't CHANGED for speculativeMs and has ≥ speculativeMinWords is stable → fire
5465
+ * onSpeculate once. If the user then keeps talking well past the speculated text, onSpeculateAbort
5466
+ * tells the host to kill the held call early. The confirm/abort DECISION belongs to the host at
5467
+ * dispatch time (speculationConfirms against the real final) — flushUtterance only resets the
5468
+ * trigger for the next turn. Merge windows are untouched: no speculation while an endpointed
5469
+ * utterance is pending (the final would be the MERGED text, which the partial alone never matches). */
5470
+ trackSpeculation(text) {
5471
+ if (!this.options.speculativeMs) return;
5472
+ const t = text.trim();
5473
+ if (this.specText) {
5474
+ if (this.words(t).length > this.words(this.specText).length + 2) {
5475
+ this.specText = "";
5476
+ this.diag("speculate_abort", { reason: "partial_grew", partial: t.slice(0, 60) });
5477
+ this.options.onSpeculateAbort();
5786
5478
  }
5479
+ return;
5480
+ }
5481
+ if (this.specSpent || this.pendingUtt || !t || t === this.specPartial) return;
5482
+ this.specPartial = t;
5483
+ if (this.specTimer) this.clock.clearTimeout(this.specTimer);
5484
+ this.specTimer = this.clock.setTimeout(() => {
5485
+ this.specTimer = null;
5486
+ if (this.speaking || this.specSpent || this.pendingUtt) return;
5487
+ if (this.words(this.specPartial).length < this.options.speculativeMinWords) return;
5488
+ const flat = this.specPartial.toLowerCase().replace(/[^a-z0-9]/g, "");
5489
+ if (flat && this.lastDispatchFlat && this.clock.now() - this.lastDispatchAt < _VoiceEngine.DUP_FINAL_MS && (this.lastDispatchFlat.startsWith(flat) || flat.startsWith(this.lastDispatchFlat))) return;
5490
+ this.specSpent = true;
5491
+ this.specText = this.specPartial;
5492
+ log10.debug(`speculate: "${this.specText.slice(0, 60)}"`);
5493
+ this.diag("speculate", { text: this.specText });
5494
+ this.options.onSpeculate(this.specText);
5495
+ }, this.options.speculativeMs);
5496
+ }
5497
+ /** Backchannel blip pool — short, quiet, semantically inert. Chosen to be transcript-safe: if
5498
+ * imperfect AEC lets a blip reach Soniox MID-user-speech it lands inside their partial stream, so
5499
+ * every phrase is a word whose accidental presence barely hurts a transcript, and flushUtterance
5500
+ * strips an isolated echo of the exact phrase at a clause edge within 2s (stripBackchannelEcho).
5501
+ * 'Okay.'/'Right.' are fine here (unlike ACKS_NEUTRAL's no-Okay rule): a standalone user "Okay."
5502
+ * right after OUR blip is overwhelmingly the blip's echo — the squash guard eating it is the point. */
5503
+ static BACKCHANNELS = ["Mm-hm.", "Uh-huh.", "Right.", "Okay."];
5504
+ /** Backchannel trigger (listening side only — the speaking branch returns before this): a partial
5505
+ * that reached a clause boundary and then stayed UNCHANGED for backchannelMs (a micro-pause, still
5506
+ * BEFORE the silence endpoint) fires a blip — if long enough (≥ backchannelMinWords), predominantly
5507
+ * Latin, rate-limited (backchannelMinGapMs + max 2/turn), and no endpointed text is pending.
5508
+ * Touches nothing else: merge/endpoint/speculation timers and turn state are never affected. */
5509
+ trackBackchannel(text) {
5510
+ if (!this.options.backchannelMs) return;
5511
+ const t = text.trim();
5512
+ if (!t || t === this.bcPartial) return;
5513
+ this.bcPartial = t;
5514
+ if (this.bcTimer) this.clock.clearTimeout(this.bcTimer);
5515
+ this.bcTimer = this.clock.setTimeout(() => {
5516
+ this.bcTimer = null;
5517
+ const p = this.bcPartial;
5518
+ const veto = (reason) => this.diag("backchannel_suppress", { reason });
5519
+ if (this.speaking || this.pendingUtt || this.bcActive) return veto("busy");
5520
+ if (!this.usingAec || !this.options.bargeIn) return veto("half_duplex");
5521
+ if (this.bcCount >= 2 || this.lastBcAt && this.clock.now() - this.lastBcAt < this.options.backchannelMinGapMs) return veto("rate_limited");
5522
+ if (this.words(p).length < this.options.backchannelMinWords) return veto("short");
5523
+ if (!/[,.;:!?…]$/.test(p) && !this.looksIncomplete(p)) return veto("no_boundary");
5524
+ const letters = p.replace(/[^\p{L}]/gu, "");
5525
+ const latin = p.replace(/[^A-Za-z]/g, "");
5526
+ if (!letters || latin.length / letters.length < 0.7) return veto("non_latin");
5527
+ const flat = p.toLowerCase().replace(/[^a-z0-9]/g, "");
5528
+ if (flat && this.lastDispatchFlat && this.clock.now() - this.lastDispatchAt < _VoiceEngine.DUP_FINAL_MS && (this.lastDispatchFlat.startsWith(flat) || flat.startsWith(this.lastDispatchFlat))) return;
5529
+ this.fireBackchannel();
5530
+ }, this.options.backchannelMs);
5531
+ }
5532
+ /** Speak one blip on a THROWAWAY TTS context. Zero floor-claim: no `speaking`, no state change, no
5533
+ * markTurn, no merge/endpoint/barge timers, no repliedSinceDispatch, no utterance queue. Echo
5534
+ * pre-seeding happens BEFORE any audio exists: the blip's words join echoWords (its mic echo is
5535
+ * never "novel"), the ack-squash guard is armed with the exact phrase (a standalone echo final is
5536
+ * swallowed), and the echo window extends so echo-shaped finals stay gated. */
5537
+ fireBackchannel() {
5538
+ const fresh = _VoiceEngine.BACKCHANNELS.filter((x) => !this.recentBc.includes(x));
5539
+ const pool = fresh.length ? fresh : _VoiceEngine.BACKCHANNELS;
5540
+ const phrase = pool[Math.floor(Math.random() * pool.length)];
5541
+ this.recentBc.push(phrase);
5542
+ if (this.recentBc.length > 2) this.recentBc.shift();
5543
+ const now3 = this.clock.now();
5544
+ for (const w of this.words(phrase)) this.echoWords.add(w);
5545
+ this.ackAt = now3;
5546
+ this.lastAck = phrase;
5547
+ this.lastBcAt = now3;
5548
+ this.lastBcPhrase = phrase;
5549
+ this.echoUntil = Math.max(this.echoUntil, now3 + 2500);
5550
+ this.bcCount++;
5551
+ this.bcActive = true;
5552
+ if (this.bcActiveTimer) this.clock.clearTimeout(this.bcActiveTimer);
5553
+ this.bcActiveTimer = this.clock.setTimeout(() => {
5554
+ this.bcActive = false;
5555
+ }, 3e3);
5556
+ this.tts.onDone = () => {
5557
+ this.bcActive = false;
5787
5558
  };
5559
+ this.tts.newContext();
5560
+ this.tts.speak(phrase, false);
5561
+ log10.debug(`backchannel: "${phrase}"`);
5562
+ this.diag("backchannel", { phrase });
5563
+ this.options.onBackchannel(phrase);
5564
+ }
5565
+ /** A real turn (or shutdown) takes over mid-blip: close the audio bypass + stability timer. */
5566
+ bcSupersede() {
5567
+ this.bcActive = false;
5568
+ if (this.bcActiveTimer) {
5569
+ this.clock.clearTimeout(this.bcActiveTimer);
5570
+ this.bcActiveTimer = null;
5571
+ }
5572
+ if (this.bcTimer) {
5573
+ this.clock.clearTimeout(this.bcTimer);
5574
+ this.bcTimer = null;
5575
+ }
5788
5576
  }
5789
- cancelTaskTool() {
5790
- return {
5791
- name: "CancelTask",
5792
- description: "Cancel a running background task by id.",
5793
- parameters: { type: "object", required: ["id"], properties: { id: { type: "string" } } },
5794
- run: async ({ id }) => this.cancelTask(String(id))
5795
- };
5577
+ /** Strip the mic echo of the LAST blip from a dispatching final, conservatively: only the exact
5578
+ * phrase, as an isolated token at a clause edge (start/end of utterance or beside punctuation),
5579
+ * within 2s of the blip. "Right"/"okay" as genuine mid-sentence content words are never touched. */
5580
+ stripBackchannelEcho(text) {
5581
+ if (!this.lastBcPhrase || this.clock.now() - this.lastBcAt > 2e3) return text;
5582
+ const target = normWord(this.lastBcPhrase);
5583
+ if (!target) return text;
5584
+ const toks = text.split(/\s+/);
5585
+ const idx = toks.findIndex((tok, i) => {
5586
+ if (normWord(tok) !== target) return false;
5587
+ if (/\?/.test(tok)) return false;
5588
+ const edgeBefore = i === 0 || /[,.;:!?…]$/.test(toks[i - 1]);
5589
+ const edgeAfter = i === toks.length - 1 || /[,.;:!?…]$/.test(tok);
5590
+ return edgeBefore && edgeAfter;
5591
+ });
5592
+ if (idx < 0) return text;
5593
+ toks.splice(idx, 1);
5594
+ log10.verbose(`stripped backchannel echo "${this.lastBcPhrase}" from dispatching final`);
5595
+ return toks.join(" ");
5796
5596
  }
5797
- };
5798
-
5799
- // src/mcp.ts
5800
- function toResult(result) {
5801
- if (result == null) return { text: "" };
5802
- if (typeof result === "string") return { text: result };
5803
- const content = result.content;
5804
- if (Array.isArray(content)) {
5805
- const texts = [];
5806
- const images = [];
5807
- for (const c of content) {
5808
- if (c?.type === "image" && typeof c.data === "string" && c.mimeType) {
5809
- images.push({ mimeType: c.mimeType, data: c.data });
5810
- } else if (typeof c?.text === "string") {
5811
- texts.push(c.text);
5812
- } else {
5813
- texts.push(JSON.stringify(c));
5597
+ /** Merge a resumed utterance into the pending one, deduping any word-overlap. Soniox re-finalizes
5598
+ * overlapping audio when the silence-timer and the semantic `<end>` both endpoint a growing
5599
+ * utterance (or after a reconnect): the next "utterance" repeats the tail of the previous one, and
5600
+ * a naive `${prev} ${next}` produced the live duplication ("Um, I want to check if Um, I want to
5601
+ * check if…"). Find the longest suffix of `prev`'s words that prefixes `next` and drop it. */
5602
+ mergeUtterance(prev, next) {
5603
+ if (!prev) return next;
5604
+ if (!next) return prev;
5605
+ const pw = prev.split(/\s+/), nw = next.split(/\s+/);
5606
+ const norm2 = _VoiceEngine.normWord;
5607
+ if (pw.length >= 2 && pw.length <= nw.length) {
5608
+ let pfx = true;
5609
+ for (let i = 0; i < pw.length - 1; i++) if (norm2(pw[i]) !== norm2(nw[i])) {
5610
+ pfx = false;
5611
+ break;
5814
5612
  }
5613
+ if (pfx && norm2(nw[pw.length - 1]).startsWith(norm2(pw[pw.length - 1]))) return next;
5815
5614
  }
5816
- const text = texts.join("\n");
5817
- if (text || images.length) return { text, ...images.length ? { images } : {} };
5615
+ const max = Math.min(pw.length, nw.length);
5616
+ for (let k = max; k > 0; k--) {
5617
+ let match = true;
5618
+ for (let i = 0; i < k; i++) if (norm2(pw[pw.length - k + i]) !== norm2(nw[i])) {
5619
+ match = false;
5620
+ break;
5621
+ }
5622
+ if (match) return [...pw, ...nw.slice(k)].join(" ");
5623
+ }
5624
+ return `${prev} ${next}`;
5818
5625
  }
5819
- return { text: JSON.stringify(result) };
5820
- }
5821
- function mcpToolToAgentTool(spec, callTool, prefix = "mcp__") {
5822
- return {
5823
- name: `${prefix}${spec.name}`,
5824
- description: spec.description ?? `MCP tool ${spec.name}`,
5825
- parameters: spec.inputSchema ?? { type: "object", properties: {} },
5826
- async run(args, _ctx) {
5827
- const r = toResult(await callTool(spec.name, args ?? {}));
5828
- return r.images?.length ? r : r.text;
5626
+ static normWord(w) {
5627
+ return normWord(w);
5628
+ }
5629
+ /** Soniox re-finalization of an ALREADY-DISPATCHED utterance, arriving PAST the merge window:
5630
+ * the new final is a strict word-prefix superset of the last dispatch (the last dispatched word
5631
+ * may be a char-prefix of the corresponding new word — a mid-word endpoint), and it lands within
5632
+ * DUP_FINAL_MS of the dispatch. Live: "Hi, please tell me a very short" dispatched, then the full
5633
+ * "…very short joke." re-finalized ~900ms later → dispatched TWICE (two replies, the second to a
5634
+ * question already being answered).
5635
+ * DESIGN — HYBRID at the caller (flushUtterance): this shape check identifies a re-finalization
5636
+ * (a human physically cannot re-speak a ≥3-word sentence plus extra words within 3s of the
5637
+ * previous dispatch; a genuine continuation arrives as NEW words, never as a superset), and
5638
+ * repliedSinceDispatch then picks the action:
5639
+ * • reply already streaming (the live trace: TTFT ~500ms < the ~900ms re-final) → DROP. True
5640
+ * "supersede" would mean aborting audible speech mid-word to re-answer nearly the same text —
5641
+ * worse UX — and needs turn-abort plumbing in every host (the lab bridge has none;
5642
+ * DuplexAgent.send is a non-cancelable queue, so a second send just stacks a SECOND full reply).
5643
+ * Soniox's premature endpoint fires at a prosodic boundary, so the loss is trailing word(s).
5644
+ * • NO reply yet → DISPATCH the fuller text. Live-verified necessity: the reflex Holds on the
5645
+ * truncated fragment ("Hi, please tell me a very short" → Hold), and dropping the re-final then
5646
+ * starves the conversation entirely — the fuller final IS the completion the Hold is waiting
5647
+ * for. A slow-but-answering reflex in this window degrades to today's two-reply behavior (rare
5648
+ * race), never worse. Fillers ("mhm") deliberately don't count as replies — see speakFiller. */
5649
+ refinalizes(next) {
5650
+ const pw = this.lastDispatchWords, nw = next.trim().split(/\s+/);
5651
+ const norm2 = _VoiceEngine.normWord;
5652
+ if (pw.length < 3 || nw.length < pw.length) return false;
5653
+ if (this.clock.now() - this.lastDispatchAt >= _VoiceEngine.DUP_FINAL_MS) return false;
5654
+ for (let i = 0; i < pw.length - 1; i++) if (norm2(pw[i]) !== norm2(nw[i])) return false;
5655
+ const pLast = norm2(pw[pw.length - 1]), nSame = norm2(nw[pw.length - 1]);
5656
+ if (!nSame.startsWith(pLast)) return false;
5657
+ return nw.length > pw.length || nSame.length > pLast.length;
5658
+ }
5659
+ static TRAIL_RE = /(?:^|\s)(?:and|but|or|so|to|the|a|an|of|in|for|with|that|if|uh|um|like|about|from|into|on|is|are|was|were|,)$/i;
5660
+ /** The utterance sounds like the user paused mid-thought (trailing conjunction/filler/comma). */
5661
+ looksIncomplete(text) {
5662
+ return _VoiceEngine.TRAIL_RE.test(text.trim());
5663
+ }
5664
+ handleUtterance(text) {
5665
+ if (this.speaking && (this.ctxOpen || this.pausedAt) && this.overlapCapable) {
5666
+ this.diag("utterance_drop", { reason: "overlap_noise", text: text.slice(0, 60) });
5667
+ this.stt.reset();
5668
+ return;
5829
5669
  }
5830
- };
5831
- }
5832
- function mcpToolsToAgentTools(specs, callTool, prefix = "mcp__", filter) {
5833
- return (filter ? specs.filter(filter) : specs).map((s) => mcpToolToAgentTool(s, callTool, prefix));
5834
- }
5835
- function describeSpec(s) {
5836
- const schema = s.inputSchema ? `
5837
- args: ${JSON.stringify(s.inputSchema)}` : "";
5838
- return `${s.name} \u2014 ${s.description ?? "(no description)"}${schema}`;
5839
- }
5840
- function makeMcpToolSearch(specs, callTool, options = {}) {
5841
- const maxResults = options.maxResults ?? 10;
5842
- const byName = new Map(specs.map((s) => [s.name, s]));
5843
- const catalogLine = `${specs.length} MCP tool(s) available \u2014 search by keyword, then call by exact name.`;
5844
- const searchTool = {
5845
- name: "ToolSearch",
5846
- description: `Search the available MCP tools by keyword (${catalogLine}). Returns matching tool names + their argument schemas; call one with \`McpCall\`.`,
5847
- parameters: { type: "object", required: ["query"], properties: { query: { type: "string", description: "keywords to match against tool name + description" } } },
5848
- async run({ query }) {
5849
- const q = String(query ?? "").trim();
5850
- if (!q) return catalogLine;
5851
- const { kept } = topByRelevance(specs, q, (s) => `${s.name} ${s.description ?? ""}`, maxResults);
5852
- if (!kept.length) return `(no MCP tool matches "${q}" \u2014 try broader keywords)`;
5853
- return kept.map(describeSpec).join("\n");
5670
+ if (this.echoActive() && (!this.options.bargeIn || (this.usingAec ? !this.genuine(text) : this.novelWords(text).length < 2))) {
5671
+ this.diag("echo_swallow", { guard: "echo_gate", text: text.slice(0, 60) });
5672
+ this.stt.reset();
5673
+ return;
5854
5674
  }
5855
- };
5856
- const callMcpTool = {
5857
- name: "McpCall",
5858
- description: "Call an MCP tool discovered via `ToolSearch`, by its exact name. Pass its arguments as `args`.",
5859
- parameters: {
5860
- type: "object",
5861
- required: ["name"],
5862
- properties: {
5863
- name: { type: "string", description: "exact tool name from ToolSearch" },
5864
- args: { type: "object", description: "arguments object for the tool (per its schema)" }
5675
+ const squash = (t) => t.toLowerCase().replace(/[^a-z]/g, "").replace(/(.)\1+/g, "$1");
5676
+ if (this.ackAt && this.lastAck && this.clock.now() - this.ackAt < 6e3 && squash(text) === squash(this.lastAck)) {
5677
+ this.diag("echo_swallow", { guard: "ack_squash", text: text.slice(0, 60) });
5678
+ this.ackAt = 0;
5679
+ return;
5680
+ }
5681
+ if (this.pendingUtt) this.mergePath = "merged";
5682
+ this.pendingUtt = this.mergeUtterance(this.pendingUtt, text);
5683
+ if (this.pendingTimer) this.clock.clearTimeout(this.pendingTimer);
5684
+ if (this.options.incompleteMergeMs && this.looksIncomplete(this.pendingUtt)) {
5685
+ log10.verbose(`hold: incomplete utterance "${this.pendingUtt.slice(-40)}"`);
5686
+ this.diag("hold", { reason: "incomplete", tail: this.pendingUtt.slice(-40) });
5687
+ this.options.onHold();
5688
+ if (this.options.holdFiller && !this.speaking) this.speakFiller(this.options.holdFiller);
5689
+ this.pendingTimer = this.clock.setTimeout(() => this.flushUtterance(), this.options.incompleteMergeMs);
5690
+ return;
5691
+ }
5692
+ if (!this.options.utteranceMergeMs || this.words(this.pendingUtt).length >= 4) return this.flushUtterance();
5693
+ this.pendingTimer = this.clock.setTimeout(() => this.flushUtterance(), this.options.utteranceMergeMs);
5694
+ }
5695
+ flushUtterance() {
5696
+ if (this.pendingTimer) {
5697
+ this.clock.clearTimeout(this.pendingTimer);
5698
+ this.pendingTimer = null;
5699
+ }
5700
+ if (this.specTimer) {
5701
+ this.clock.clearTimeout(this.specTimer);
5702
+ this.specTimer = null;
5703
+ }
5704
+ this.specSpent = false;
5705
+ this.specPartial = "";
5706
+ this.specText = "";
5707
+ if (this.bcTimer) {
5708
+ this.clock.clearTimeout(this.bcTimer);
5709
+ this.bcTimer = null;
5710
+ }
5711
+ this.bcCount = 0;
5712
+ this.bcPartial = "";
5713
+ const text = this.stripBackchannelEcho(this.pendingUtt);
5714
+ this.pendingUtt = "";
5715
+ const path = this.mergePath;
5716
+ this.mergePath = "direct";
5717
+ if (text) {
5718
+ const flat = text.toLowerCase().replace(/[^a-z0-9]/g, "");
5719
+ if (flat && flat === this.lastDispatchFlat && !this.repliedSinceDispatch && this.clock.now() - this.lastDispatchAt < _VoiceEngine.DUP_FINAL_MS) {
5720
+ log10.verbose(`dropped duplicate final "${text.slice(0, 40)}" (identical to the just-dispatched utterance, no reply between)`);
5721
+ this.diag("utterance_drop", { reason: "dup_final", text: text.slice(0, 60) });
5722
+ return;
5865
5723
  }
5866
- },
5867
- async run({ name, args }) {
5868
- const n = String(name ?? "");
5869
- if (!byName.has(n)) return `Error: unknown MCP tool '${n}'. Use ToolSearch to find valid names.`;
5870
- const r = toResult(await callTool(n, args ?? {}));
5871
- return r.images?.length ? r : r.text;
5724
+ const refinal = this.refinalizes(text);
5725
+ if (refinal && this.repliedSinceDispatch) {
5726
+ log10.verbose(`dropped re-finalization "${text.slice(0, 60)}" (superset of the just-dispatched utterance, reply already flowing)`);
5727
+ this.diag("utterance_drop", { reason: "refinalized", text: text.slice(0, 60) });
5728
+ this.lastDispatchFlat = flat;
5729
+ this.lastDispatchWords = text.trim().split(/\s+/);
5730
+ return;
5731
+ }
5732
+ this.lastDispatchFlat = flat;
5733
+ this.lastDispatchWords = text.trim().split(/\s+/);
5734
+ this.lastDispatchAt = this.clock.now();
5735
+ this.repliedSinceDispatch = false;
5736
+ this.lastDispatchWasQuestion = /\?\s*$/.test(text);
5737
+ this.turnStartAt = this.clock.now();
5738
+ this.bargeGraceUntil = this.clock.now() + this.options.bargeGraceMs;
5739
+ this.diag("dispatch", { text, path: refinal ? "refinalized" : path, question: this.lastDispatchWasQuestion });
5740
+ this.options.onUtterance(text);
5741
+ }
5742
+ }
5743
+ get overlapCapable() {
5744
+ return this.usingAec && this.options.overlapPause && !!this.player.pause && !!this.player.resume;
5745
+ }
5746
+ armResume() {
5747
+ if (this.resumeTimer) this.clock.clearTimeout(this.resumeTimer);
5748
+ this.resumeTimer = this.clock.setTimeout(() => {
5749
+ this.resumeTimer = null;
5750
+ if (!this.pausedAt) return;
5751
+ this.diag("overlap_resume", { heldMs: Math.round(this.clock.now() - this.pausedAt) });
5752
+ this.stt.reset();
5753
+ this.resetOverlap(true);
5754
+ }, this.options.overlapResumeMs);
5755
+ }
5756
+ resetOverlap(resume) {
5757
+ if (this.resumeTimer) {
5758
+ this.clock.clearTimeout(this.resumeTimer);
5759
+ this.resumeTimer = null;
5760
+ }
5761
+ if (this.pausedAt && resume) {
5762
+ this.player.resume?.();
5763
+ this.lastResumeAt = this.clock.now();
5764
+ }
5765
+ this.pausedAt = 0;
5766
+ this.lastOverlapPartial = "";
5767
+ this.gatePassTimes = [];
5768
+ }
5769
+ /** energy two-stage barge-in (heuristic tier only): spike over echo baseline → pause + confirm via STT */
5770
+ gatePassTimes = [];
5771
+ // recent gate-PASSING chunks (helper zeroes residue — nonzero = vetted)
5772
+ handleLevel(rms) {
5773
+ if (this.usingAec) {
5774
+ if (!this.options.overlapEnergyHold || !this.speaking || !this.overlapCapable || this.pausedAt || rms < 50) return;
5775
+ const t = this.clock.now();
5776
+ this.gatePassTimes = this.gatePassTimes.filter((x) => t - x < 350);
5777
+ this.gatePassTimes.push(t);
5778
+ if (this.gatePassTimes.length < 2) return;
5779
+ this.gatePassTimes = [];
5780
+ this.pausedAt = t;
5781
+ this.player.pause();
5782
+ this.armResume();
5783
+ return;
5784
+ }
5785
+ if (!this.speaking) {
5786
+ this.baseline = 0;
5787
+ this.hot = 0;
5788
+ return;
5789
+ }
5790
+ if (!this.baseline) {
5791
+ this.baseline = rms;
5792
+ return;
5872
5793
  }
5873
- };
5874
- return [searchTool, callMcpTool];
5875
- }
5876
- function buildMcpCatalog(servers) {
5877
- const specs = [];
5878
- const routes = /* @__PURE__ */ new Map();
5879
- for (const m of servers) {
5880
- for (const s of m.specs) {
5881
- const base = `mcp__${m.name}__${s.name}`.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 128);
5882
- let display = base;
5883
- for (let i = 2; routes.has(display); i++) display = `${base.slice(0, 128 - String(i).length - 1)}_${i}`;
5884
- specs.push({ name: display, description: s.description, inputSchema: s.inputSchema });
5885
- routes.set(display, { server: m.name, rawName: s.name });
5794
+ this.baseline = this.baseline * 0.9 + rms * 0.1;
5795
+ if (rms > Math.max(this.baseline * this.options.bargeRmsMult, this.options.bargeRmsFloor)) this.hot++;
5796
+ else this.hot = 0;
5797
+ if (this.hot >= 2 && !this.suspectUntil) {
5798
+ this.suspectUntil = this.clock.now() + 1300;
5799
+ this.clock.setTimeout(() => {
5800
+ this.suspectUntil = 0;
5801
+ }, 1350);
5886
5802
  }
5887
5803
  }
5888
- return { specs, routes };
5889
- }
5890
- function searchOverCatalog(servers, specs, routes, resolve, options) {
5891
- const tools = specs.length ? makeMcpToolSearch(specs, (name, args) => {
5892
- const r = routes.get(name);
5893
- if (!r) throw new Error(`unknown MCP tool '${name}' \u2014 use ToolSearch to find valid names`);
5894
- return resolve(r.server, r.rawName, args ?? {});
5895
- }, options) : [];
5896
- return { tools, serverNames: servers, toolCount: specs.length };
5897
- }
5898
- function makeMcpToolSearchFromMounted(mounted, options) {
5899
- const { specs, routes } = buildMcpCatalog(mounted);
5900
- const byName = new Map(mounted.map((m) => [m.name, m]));
5901
- return searchOverCatalog(mounted.map((m) => m.name), specs, routes, (server, rawName, args) => byName.get(server).client.callTool(rawName, args), options);
5902
- }
5903
- function makeLazyMcpToolSearch(servers, resolve, options) {
5904
- const { specs, routes } = buildMcpCatalog(servers);
5905
- return searchOverCatalog(servers.map((s) => s.name), specs, routes, resolve, options);
5906
- }
5804
+ };
5907
5805
 
5908
- // src/hooks.ts
5909
- var RecordingHooks = class {
5910
- /** tool name -> reason; a matching preToolUse call is blocked with that reason. */
5911
- constructor(blocks = {}) {
5912
- this.blocks = blocks;
5913
- }
5914
- blocks;
5915
- pre = [];
5916
- post = [];
5917
- outputs = [];
5918
- stops = [];
5919
- preToolUse(call, meta) {
5920
- this.pre.push({ call, meta });
5921
- const reason = this.blocks[call.name];
5922
- if (reason != null) return { block: true, reason };
5923
- }
5924
- postToolUse(call, result, meta) {
5925
- this.post.push({ call, result, meta });
5806
+ // src/voice/spokenSplitter.ts
5807
+ var OPEN = "<spoken>";
5808
+ var CLOSE = "</spoken>";
5809
+ var CLOSERS = `"')]}\xBB\u201D\u2019`;
5810
+ var hasSpeech = (s) => /[\p{L}\p{N}]/u.test(s);
5811
+ var SentenceCoalescer = class _SentenceCoalescer {
5812
+ buf = "";
5813
+ static isEnd(c) {
5814
+ return c === "\n" || c === "." || c === "!" || c === "?" || c === "\u2026";
5926
5815
  }
5927
- onToolOutput(call, chunk, meta) {
5928
- this.outputs.push({ call, chunk, meta });
5816
+ feed(delta) {
5817
+ if (delta) this.buf += delta;
5818
+ let cut = -1;
5819
+ for (let i = 0; i < this.buf.length; i++) if (_SentenceCoalescer.isEnd(this.buf[i])) cut = i;
5820
+ if (cut < 0) return "";
5821
+ if (this.buf[cut] !== "\n") while (cut + 1 < this.buf.length && CLOSERS.includes(this.buf[cut + 1])) cut++;
5822
+ const ready = this.buf.slice(0, cut + 1).trim();
5823
+ this.buf = this.buf.slice(cut + 1);
5824
+ return hasSpeech(ready) ? ready : "";
5929
5825
  }
5930
- onStop(finalText) {
5931
- this.stops.push(finalText);
5826
+ flush() {
5827
+ const s = this.buf.trim();
5828
+ this.buf = "";
5829
+ return hasSpeech(s) ? s : "";
5932
5830
  }
5933
5831
  };
5934
- var RecordingLifecycle = class {
5935
- /** @param startContext injected at session start; @param rewrite maps a submitted prompt to a new one. */
5936
- constructor(startContext, rewrite) {
5937
- this.startContext = startContext;
5938
- this.rewrite = rewrite;
5939
- }
5940
- startContext;
5941
- rewrite;
5942
- starts = 0;
5943
- prompts = [];
5944
- compactions = [];
5945
- subagentStops = [];
5946
- onSessionStart() {
5947
- this.starts++;
5948
- return this.startContext;
5949
- }
5950
- onUserPromptSubmit(text) {
5951
- this.prompts.push(text);
5952
- return this.rewrite?.(text);
5832
+ var SpokenSplitter = class {
5833
+ buf = "";
5834
+ inSpoken = false;
5835
+ /** True once any spoken char has ever been emitted (drives the no-spoken fallback). */
5836
+ spokeAny = false;
5837
+ /** Feed a delta; returns the spoken/detail spans completed by this chunk (either may be ''). */
5838
+ feed(delta) {
5839
+ this.buf += delta;
5840
+ return this.drain(false);
5953
5841
  }
5954
- onPreCompact(messages) {
5955
- this.compactions.push(messages.length);
5842
+ /** Drain any buffered partial. A trailing `<…` that never completed a tag is emitted as detail. */
5843
+ flush() {
5844
+ return this.drain(true);
5956
5845
  }
5957
- onSubagentStop(summary, info) {
5958
- this.subagentStops.push({ summary, label: info?.label });
5846
+ drain(final) {
5847
+ let spoken = "";
5848
+ let detail = "";
5849
+ while (this.buf.length) {
5850
+ const tag = this.inSpoken ? CLOSE : OPEN;
5851
+ const idx = this.buf.indexOf(tag);
5852
+ if (idx >= 0) {
5853
+ const text2 = this.buf.slice(0, idx);
5854
+ if (this.inSpoken) spoken += text2;
5855
+ else detail += text2;
5856
+ this.buf = this.buf.slice(idx + tag.length);
5857
+ this.inSpoken = !this.inSpoken;
5858
+ continue;
5859
+ }
5860
+ const lt = this.buf.lastIndexOf("<");
5861
+ const holdStart = lt >= 0 && tag.startsWith(this.buf.slice(lt)) ? lt : this.buf.length;
5862
+ const text = this.buf.slice(0, holdStart);
5863
+ if (this.inSpoken) spoken += text;
5864
+ else detail += text;
5865
+ this.buf = this.buf.slice(holdStart);
5866
+ break;
5867
+ }
5868
+ if (final && this.buf) {
5869
+ if (this.inSpoken) spoken += this.buf;
5870
+ else detail += this.buf;
5871
+ this.buf = "";
5872
+ }
5873
+ if (spoken.trim()) this.spokeAny = true;
5874
+ return { spoken, detail };
5959
5875
  }
5960
5876
  };
5961
5877
 
5962
- // src/index.ts
5963
- init_logging();
5964
-
5965
- // src/voice/engine.ts
5966
- init_logging();
5967
- var log11 = forComponent("VoiceEngine");
5968
- var now = () => performance.now();
5969
- var forSpeech = (t) => t.replace(/[*_`#]+/g, "").replace(/^[ \t]*[-•]\s+/gm, "").replace(/\s*[\u2013\u2014]\s*/g, ", ").replace(/[\u2010\u2011]/g, "-").replace(/\s*\|\s*/g, ", ").replace(/(\d)\s+%/g, "$1%").replace(/\.{3,}/g, ".");
5970
- var VoiceEngineOptions = class {
5971
- stt;
5972
- tts;
5973
- player;
5974
- /** a final utterance arrived (endpoint) — host dispatches it as a turn */
5975
- onUtterance = () => {
5976
- };
5977
- /** live partial transcript while listening (host renders the 🎤 line) */
5978
- onPartial = () => {
5979
- };
5980
- onState = () => {
5981
- };
5982
- /** user spoke/acted over playback — host aborts the in-flight turn (called AFTER audio is killed).
5983
- * phase: 'speaking' = cut mid-speech (real interruption); 'drain' = in the final audio tail
5984
- * (normal turn-taking — hosts shouldn't alarm). */
5985
- onBargeIn = () => {
5986
- };
5987
- /** spoken micro-ack on utterance endpoint (masks LLM TTFT); '' disables */
5988
- ackPhrase = "";
5989
- /** Endpoint merge window (ms): hold an endpointed utterance briefly — if speech resumes (spelled
5990
- * letters, mid-thought pauses), the next utterance MERGES instead of dispatching a truncated one
5991
- * ("E-L-Y." / "A."). Costs this much latency per turn; 0 disables. */
5992
- utteranceMergeMs = 350;
5993
- /** Extended merge window (ms) for utterances that look incomplete (trailing conjunction/filler).
5994
- * Gives the user time to finish their thought without triggering a model call. */
5995
- incompleteMergeMs = 1500;
5996
- /** Grace window (ms) after an utterance dispatches, during which the user's own trailing audio cannot
5997
- * barge the reply it requested. Soniox keeps finalizing partials past <end>; without this they read
5998
- * as a barge and abort the fresh turn (live: mid-sentence self-interruption + steps=1→steps=0 double
5999
- * abort). Short enough that a genuine immediate barge ("no wait—") still lands right after. */
6000
- bargeGraceMs = 600;
6001
- /** Barge-in (talk over the assistant to interrupt). true = full-duplex (needs echo cancellation, or
6002
- * the assistant's own TTS bleeds back and self-interrupts). false = HALF-DUPLEX: the engine is deaf
6003
- * while audible (speaking + drain tail), so echo can never become a phantom turn — the right mode
6004
- * when there's no AEC (e.g. the non-VPIO mic fallback) and no headphones. Cost: can't interrupt. */
6005
- bargeIn = true;
6006
- /** Filler phrase spoken when holding for an incomplete utterance ('' disables). */
6007
- holdFiller = "";
6008
- /** Called when the engine holds an incomplete utterance (host can render a visual cue). */
6009
- onHold = () => {
6010
- };
6011
- /** heuristic (non-AEC) energy barge-in tuning */
6012
- bargeRmsMult = 2;
6013
- bargeRmsFloor = 500;
6014
- /** Overlap turn-taking (AEC tier, needs player.pause/resume) — human phone-call model, driven by
6015
- * the STT ITSELF (a trained speech classifier) instead of energy thresholds (energy could not
6016
- * separate residue bursts from speech in every room — hiccup whack-a-mole): a GENUINE partial
6017
- * (novel words dominate — echo of our own reply is inert) while speaking → PAUSE (exact-sample
6018
- * hold); partial grows into dominant-novel ≥2 words → cede (interrupt; the LLM re-enters); partial
6019
- * stalls/endpoints without ceding (backchannel by DURATION, not vocabulary) → resume + drop. false disables. */
6020
- overlapPause = true;
6021
- /** no new partial activity for this long while paused → resume, drop the interjection */
6022
- overlapResumeMs = 700;
6023
- /** A genuine barge over a LONG reply is defeated by the dominant-novel gate: Meet echoes our own
6024
- * speech back, so the partial is mostly our words + a few of hers → never "dominant novel" → it
6025
- * resumes (replaying old audio — the audible "completes the buffer" blip) instead of ceding.
6026
- * Mechanism-based discriminator: a re-PAUSE this soon after a resume = a persistent human, not an
6027
- * echo blip (which pauses once and stalls). Cede on the re-pause regardless of the novel gate. */
6028
- overlapRepauseCedeMs = 1500;
6029
- /** Speculative ENERGY pre-pause while speaking (AEC tier): two residue gate-passes within 350ms →
6030
- * pause ~300ms before the STT tokens land. But energy CANNOT separate residue bursts from speech
6031
- * (the documented whack-a-mole) — so a residue spike during loud playback false-pauses with NO user
6032
- * speech at all, an audible hiccup. Default OFF: the genuine-gated STT partial is the
6033
- * mechanism-correct pause trigger; enable only if barge-in onset feels sluggish in a clean-AEC room. */
6034
- overlapEnergyHold = false;
6035
- /** Map inline `[emotion]` tags (emitted by the model, prompt-taught) into Cartesia inline emotion
6036
- * tags in the spoken transcript (sonic-3 stitches the prosody). false = strip them silently. */
6037
- emotions = true;
6038
- /** Show the `[emotion]` tags in the on-screen echo (debug). false = hide (spoken-only). */
6039
- showEmotions = false;
5878
+ // src/duplex.ts
5879
+ var log11 = forComponent("DuplexAgent");
5880
+ function describeCall(call) {
5881
+ const v = call.args && Object.values(call.args).find((x) => typeof x === "string" && x.trim());
5882
+ const hint = v ? ` (${String(v).replace(/\s+/g, " ").trim().slice(0, 48)})` : "";
5883
+ return `${call.name}${hint}`;
5884
+ }
5885
+ var DuplexAgentOptions = class {
5886
+ /** Any ai.libx.js AIClient — shared by all tiers (routed by model). */
5887
+ ai;
5888
+ /** The WORKER's filesystem (act + think). If omitted the worker keeps Agent's jailed-disk-at-cwd default. */
5889
+ fs;
5890
+ // The reflex IS the voice. 120b (not 20b) for channel discipline + instruction-following: the 20b
5891
+ // mislabels gpt-oss harmony channels under load, leaking raw analysis into the spoken `final` channel
5892
+ // (and misfiring Hold). 120b is the same price tier (~$0.15/$0.60) — the quality/cost trade is free.
5893
+ reflexModel = "groq/openai/gpt-oss-120b";
5894
+ actModel = "anthropic/claude-sonnet-4-6";
5895
+ /** Premium reasoning model. Set to `false` to disable the Think tier entirely. */
5896
+ thinkModel = "anthropic/claude-opus-4-8";
5897
+ /** Per-worker providerOptions, derived from the worker's actual model at spawn time (IoC — keeps duplex
5898
+ * provider-agnostic). Workers override the reflex/main model, so provider-specific options (e.g. cursor's
5899
+ * cwd/cursorSession) must be recomputed for the worker's model, never inherited from the main template —
5900
+ * leaking cursor options to an anthropic worker is a hard 400. Returns undefined → no providerOptions. */
5901
+ providerOptionsFor;
5902
+ /** Escape hatches merged over the derived per-agent options. */
5903
+ reflexOptions;
5904
+ actOptions;
5905
+ thinkOptions;
5906
+ /** Fresh-context check on each successful Act task: a NEW agent (no self-confirmation bias) re-reads
5907
+ * the file state against the brief and fixes any gap before the result is re-voiced. Bounded to one
5908
+ * pass; ~2x Act cost so default OFF. The self-verify FOOTER (same context) was measured ineffective —
5909
+ * this is the structural fix (see mind/10). Think tasks are pure reasoning, never checked. */
5910
+ verifyActTasks = false;
5911
+ /** Receives the voice text_delta stream + task lifecycle events. */
5912
+ host;
5913
+ /** How many recent transcript messages are rendered into a worker's brief. */
5914
+ excerptTurns = 6;
5915
+ /** Voice register: 'neutral' = clean spoken style; 'conversational' = human-like — fillers,
5916
+ * backchannels, impulsive first reactions before content (mimics real duplex conversation). */
5917
+ voiceStyle = "neutral";
5918
+ /** Teach the model to emit inline `[emotion]` tags for Cartesia emotion control. Only set when the
5919
+ * TTS actually speaks them — text-duplex (no TTS) would otherwise print literal tags. */
5920
+ emotionTags = false;
5921
+ /** Awaited BEFORE a worker spawns — open a per-task checkpoint frame, audit, etc.
5922
+ * (post-spawn would race the worker's first edits). */
5923
+ onTaskStart;
5924
+ /** Re-voice throttled worker progress asides ('[task t1 progress] …') so long tasks aren't dead
5925
+ * air. Off by default — each update costs a voice turn (LLM call + speech). */
5926
+ progressUpdates = false;
5927
+ /** Min ms between progress re-voices per task. */
5928
+ progressIntervalMs = 25e3;
5929
+ /** Relay worker questions (AskUserQuestion + permission asks via parkQuestion) through the VOICE:
5930
+ * the question re-voices as '[task <id> asks] …', the user answers conversationally, and the
5931
+ * voice model resolves it with the AnswerTask tool. Off → host.ask passthrough (text menus). */
5932
+ askRelay = false;
5933
+ /** Parked questions auto-resolve empty after this long (callers map '' to deny/best-judgment). */
5934
+ askTimeoutMs = 12e4;
5935
+ /** Max retained task records: oldest SETTLED tasks (and their activity tails) are evicted past this,
5936
+ * bounding memory over a long-lived session. Running tasks are never evicted. */
5937
+ maxTaskRecords = 50;
5938
+ /** Host overrides for QuickLook lookups (keyed by `what`). The engine's defaults go through the
5939
+ * (possibly jailed) fs — e.g. `.git/**` is deny-listed, so the CLI supplies 'branch' itself. */
5940
+ quickLook;
5941
+ /** Memory directory/directories on the WORKER fs. If set, the voice agent gets Remember + Recall
5942
+ * tools directly (no delegation needed) and implicit capture guidance. */
5943
+ memoryDir;
5944
+ /** User-scope memory dir for global facts (type=user/feedback). Forwarded to Remember's routing. */
5945
+ memoryUserDir;
6040
5946
  };
6041
- var VoiceEngine = class _VoiceEngine {
5947
+ var RESERVED_EVENT_MARKER = /\[task\b[^\]\n]*\b(?:completed|failed|progress|asks)\b/i;
5948
+ var RESERVED_EVENT_OPENER = /\[\s*task\b/i;
5949
+ var STAGE_DIRECTION_RE = /^\(\s*(?:(?:waiting|checking|searching|thinking|processing|loading|working|fetching|looking)\b[^)]*|[^)]*(?:\.\.\.|…)\s*)\)$/i;
5950
+ var VOICE_SYSTEM_PROMPT = 'You are a spoken voice assistant \u2014 the user HEARS everything you say. Use short sentences. One idea per sentence. No markdown, no bullet lists, no code blocks, no headings, no emoji. Never emit stage directions or parenthetical asides about your own process \u2014 nothing like "(waiting for the result...)" or "(checking)"; while work runs, either say it as plain speech or end your turn.\nThis holds even when asked to "print", "list", "show", or "make a table" \u2014 there is no screen for the spoken channel. Speak it as flowing prose ("Tuesday is half a meter, Wednesday a bit less\u2026"), or if they truly need it on screen, route it to Act to render. Never emit dashes or pipes into speech.\nKeep turns SHORT \u2014 one to three sentences, then stop. Never lecture, enumerate cases, or add caveats unprompted. Conversation is a fast exchange: give the one thing asked, and let the user pull more if they want it.\nYou have three cognitive tiers \u2014 like a human brain:\n\u2022 YOU (reflex) \u2014 instant, lightweight. Handle greetings, simple questions, status checks, QuickLook.\n\u2022 `Act` \u2014 your hands. A background worker with its own configured tools and access to the user\'s environment (files and shell{{WORKER_WEB}}). Use for reading, editing, searching, running tasks, building \u2014 any real work.\n{{THINK_SLOT}}\nWhen you are unsure whether you can do or access something, do NOT assume and do NOT claim a capability you have not confirmed. To check what you can do, QuickLook `capabilities` (instant \u2014 it lists your worker\'s real tools) and answer from that. Never promise an ability that is not in your capabilities; if it is not there, tell the user plainly you can\'t. To actually DO real work, call `Act`. When the user mentions their project, folder, files, or environment ("this project", "the current folder", "my code"), call `Act` IMMEDIATELY \u2014 do not ask for paths or details the worker can discover itself. Never pretend to have done the work or invent results \u2014 the worker\'s report is your only source.\nYou cannot mute the microphone or stop voice capture yourself \u2014 no tool does it. If the user asks you to stop listening or turn the voice off, never claim you did: tell them to say exactly "voice off" (handled by the app directly), or type /voice.\nYou are NOT a knowledge base. For any question whose answer needs SPECIFIC verifiable facts you do not already have in hand \u2014 how to build/configure/implement something, exact API, library, entitlement, command or option names, current events, or particular numbers, dates, or names \u2014 do NOT answer from your own memory: you will confidently make things up (a fake API, a wrong entitlement, an event that did not happen). Route it to `Act`, which can search and verify, and speak only what its report says. DELEGATION RULE \u2014 decide for yourself, the user never has to push: if you cannot answer confidently from the conversation plus trivial well-known knowledge, do NOT refuse and do NOT guess \u2014 dispatch `Act` immediately with a clear brief and say you are checking. Anything needing CURRENT data (weather, news, prices, dates, sky/astronomy, "right now"), real computation, or verification is an automatic dispatch \u2014 never a refusal. The user should never need to say "search the web" or "think harder" to make you act; needing fresh or verified information IS the trigger. Refuse only what your worker genuinely cannot do (check `capabilities`), and say why. Answer inline ONLY for general conversation, chit-chat, and trivia you are sure of, or facts you can see via QuickLook. When elaborating on a completed task ("tell me more", "the gist"), stay strictly within what that result actually said \u2014 if the user asks for something the result did not cover, that is NEW information: dispatch `Act`, do not improvise.\nALWAYS react before you work: the FIRST thing in your turn is a brief spoken acknowledgement of what you heard and what you are about to do ("got it \u2014 opening that now", "sure, let me pull it up", "okay, checking"). NEVER call a tool (Act, Think, QuickLook) silently \u2014 the user must hear you react before you go quiet to work. After dispatching Act or Think, that same one short sentence IS your turn \u2014 end it and do not wait for the result.\nA completed task speaks its OWN result to the user (the worker voices what matters as it finishes) \u2014 you do NOT re-voice clean task results. A FAILED or INCOMPLETE task still arrives as a "[task t1 failed] \u2026" event for you to handle. The completed result stays in YOUR context \u2014 it is yours to draw on. When the user follows up ("tell me more", "what else", "and?"), answer FROM that result first: you already have the detail, so elaborate on what you have. Do NOT spawn a fresh worker to re-search or re-gather what you were just handed. Re-dispatch ONLY when genuinely new information is needed \u2014 e.g. the user wants the full contents of a SPECIFIC source, which is one WebFetch of that URL, not a brand-new search. "[task t1 progress] \u2026" events are interim status, NOT results \u2014 give at most a half-sentence aside ("still on it \u2014 running tests now") and end your turn. Never present progress as a finished result.\nCRITICAL: while a task is still running you have NO answer yet \u2014 never state a specific result of any kind (a number, size, count, name, path, or value). The real answer arrives ONLY in the "[task \u2026 completed]" event; inventing one meanwhile (a made-up disk size, commit count, etc.) is a serious error. Until then, only acknowledge and wait.\nNever read raw file paths, diffs, or code aloud verbatim.\nDo NOT end every turn with the same canned offer ("want a rundown?", "want the steps?"). Offer once at most; if the user pushes back, repeats themselves, or sounds unsatisfied ("you know what I mean?", "think deeper", "are you sure?"), do NOT re-offer the same thing \u2014 change approach: dispatch `Act`/`Think` to actually dig in, or ask one concrete clarifying question. Repeating a non-answer is worse than silence.\n"[task t1 asks] \u2026" events are QUESTIONS from a background task \u2014 relay to the user in your own words, short, then end your turn. When the user answers, call `AnswerTask` with that id and their answer. NEVER answer on the user\'s behalf for permissions or risky operations; if their reply is ambiguous, confirm first.\nIf the user\'s message sounds INCOMPLETE \u2014 trailing off mid-sentence, a fragment that needs more context ("and then we", "but the problem is"), hesitation fillers ("uh", "um") \u2014 call `Hold` instead of answering. This keeps listening for the rest of their thought. Only respond with substance when you have a complete question or request.\nDispatch discipline: send ONE self-contained task per request \u2014 a single worker with the full brief beats several workers with fragments (each worker starts fresh and re-discovers context). NEVER dispatch a worker just to read files or gather information \u2014 workers explore and discover context themselves; pass on what you already know and let one worker do the whole job. Split into parallel tasks only when the user asks for genuinely independent things. When a task completes, report its result and stop \u2014 do NOT dispatch follow-up work (verification, polish, extras) the user did not ask for, unless the report itself signals failure or doubt.\nDo not fire a second Act/Think for work already in flight, and NEVER spawn a second task to re-count, cross-check, or verify a result a worker already gave you \u2014 trust its answer; a single question gets ONE task. Call `TaskStatus` at most ONCE per turn; if a task is still running, just say "still on it" and end the turn \u2014 never poll it again and again in a loop. Use `CancelTask` when the user asks to stop something.\nPRIORITY: when the user says goodbye or wants to end/finish/wrap up the session ("ok bye", "that\'s all", "let\'s finish", "let\'s end", "goodnight", "exit", "wrap up"), call `ExitSession` IMMEDIATELY \u2014 do not act, do not check status, just exit.\nFor TRIVIAL instant lookups only \u2014 current time, git branch, listing a folder, peeking at a small file, or checking your own `capabilities`/tools \u2014 use `QuickLook` (instant, no task). Whenever the user asks what you can do or whether you have some ability, QuickLook `capabilities` and answer from that \u2014 never guess. Anything requiring searching, reasoning, running commands, or editing goes through `Act`.\n{{MEMORY_SLOT}}\nUser messages may arrive via speech-to-text and can carry transcription artifacts \u2014 odd words, cut-offs, homophones ("for you" vs "folder"). Read for INTENT, not surface text. If a message seems garbled, surprising, or only half-parses, do NOT guess an action or improvise content from it \u2014 briefly confirm what they meant ("did you mean\u2026?") and wait. A one-line confirm beats a confident wrong answer or an invented response to a request you did not actually understand.';
5951
+ var THINK_GUIDANCE = "\u2022 `Think` \u2014 your brain. A premium reasoning model, FAR more expensive than Act. Reserve it for open-ended architecture/design questions, or a problem Act already FAILED at. ALL implementation work \u2014 coding, refactoring, debugging, edge cases, tests \u2014 goes to Act; Act is highly capable. Never send the same work to both.";
5952
+ var THINK_DISABLED_GUIDANCE = "(Think tier is not available \u2014 use Act for all escalations.)";
5953
+ var VOICE_STYLE_CONVERSATIONAL = `Speak like a person in a live conversation, not an assistant reading a script. React first, then deliver: a quick impulsive beat ("oh nice", "hmm, hold on", "ah, got it") before the substance. Use contractions always. Vary sentence length \u2014 some very short. Light fillers and backchannels are fine ("mm-hm", "right", "let's see") but at most one per reply \u2014 never stack them. When you escalate to Act or Think, say it like a human would ("hang on, let me actually dig into that \u2014 gimme a minute") instead of announcing a task. When a result comes back, react to it like you just found out ("okay so \u2014 turns out\u2026"). Match the user's energy: a quick question gets a quick answer \u2014 a few words is a perfectly good turn. Prefer a short answer plus an offer ("want the details?") over covering everything. Never narrate your own mechanics (no "I will now act", no task ids out loud).`;
5954
+ var EMOTION_TAGS_GUIDANCE = `EMOTION: your voice is synthesized with emotion control. Prefix a sentence with an inline [emotion] tag, placed directly before the sentence it colors, to shape how it is spoken. Use it ONLY when the emotion genuinely fits the words (it amplifies real feeling, it cannot fake it) \u2014 do not tag every sentence; reserve it for moments that carry feeling, and vary which one you use. You may also drop [laughter] for a natural laugh. Available emotions: ${EMOTIONS.join(", ")}.`;
5955
+ var DuplexAgent = class _DuplexAgent {
6042
5956
  options;
6043
- state = "idle";
6044
- stt;
6045
- tts;
6046
- player;
6047
- speaking = false;
6048
- // audible (deltas flowing OR audio draining)
6049
- ctxOpen = false;
6050
- // the current TTS context still accepts deltas (false once end-frame sent)
6051
- interrupted = false;
6052
- // barge-in latch: drop in-flight deltas until the next legitimate turn
6053
- spokeDeltas = false;
6054
- // a TTS context is open for the current spoken turn
6055
- drainTimer = null;
6056
- // heuristic tier state (inert under AEC) — frozen as validated in the experiment
6057
- echoWords = /* @__PURE__ */ new Set();
6058
- prevReply = "";
6059
- reply = "";
6060
- echoUntil = 0;
6061
- baseline = 0;
6062
- hot = 0;
6063
- suspectUntil = 0;
6064
- ackAt = 0;
6065
- // when the micro-ack was spoken — its echo can leak before the AEC filter converges
6066
- bargeGraceUntil = 0;
6067
- // no barge-in until this time — the user's OWN trailing audio (after the
6068
- // utterance that JUST dispatched this turn) must not immediately re-interrupt the reply it requested.
6069
- pendingUtt = "";
6070
- // endpointed text held for the merge window
6071
- pendingTimer = null;
6072
- lastInterrupted = null;
6073
- // overlap (pause) tier state — AEC + pause-capable sinks only
6074
- pausedAt = 0;
6075
- lastResumeAt = 0;
6076
- // when the overlap last resumed from a false alarm — a quick re-pause cedes
6077
- lastOverlapPartial = "";
6078
- // change-detection: only NEW partial text counts as activity
6079
- resumeTimer = null;
6080
- turnStartAt = 0;
6081
- // timestamp when the current turn began (for TTFT logging)
6082
- // Central speech queue (above the TTS context): complete worker utterances serialize into ONE
6083
- // playback stream, one-at-a-time, never splicing into the live reflex's open utterance.
6084
- uttQueue = [];
6085
- // Per-turn emotion-tag parser (reset on beginSpeech) — converts `[emotion]` → Cartesia inline tags
6086
- // for TTS, tracks tag-free prose for echo discrimination, and surfaces display text for the screen.
6087
- emo = null;
5957
+ voice;
5958
+ tasks = /* @__PURE__ */ new Map();
5959
+ queue = Promise.resolve();
5960
+ seq = 0;
5961
+ pendingEvents = [];
5962
+ /** Out-of-band follow-up attribution for the events coalescing into the next flush turn: TRUE iff ≥1 of
5963
+ * the tasks being integrated was NON-CLEAN (early-stop/failure). Carried out-of-band on the enqueue call
5964
+ * by the caller that KNOWS the outcome — a plain boolean the MODEL CANNOT PERTURB. It is NOT scanned from
5965
+ * worker-authored event text (v1: an "Outcome:" substring over-stamped siblings) and NOT keyed on a brief
5966
+ * string the reflex re-authors (v2: a paraphrased escalation brief missed the Set → followUp:false →
5967
+ * RE-ENABLED unbounded auto-escalation, the dangerous runaway direction). See [[wrong-discriminator]] /
5968
+ * [[drive-real-reflex]] / [[fakeaiclient-blind-to-wire-format]]. */
5969
+ pendingNonClean = false;
5970
+ flushQueued = false;
5971
+ /** Per-voice-turn guards (reset by resetTurn at each turn's start). The reflex is a weak model:
5972
+ * left unguarded it polls TaskStatus after a dispatch and/or dispatches silently (dead air).
5973
+ * Like CC's Task tool, a dispatch is "said my piece, now wait for the push" — these enforce that. */
5974
+ turnDispatched = false;
5975
+ // an Act/Think fired this turn
5976
+ spokeBeforeDispatch = false;
5977
+ // the reflex ALREADY acked before dispatching — the forced post-dispatch text step must stay silent (live: "Got it…" twice)
5978
+ turnBriefs = /* @__PURE__ */ new Set();
5979
+ // briefs dispatched this turn (detect identical re-dispatch)
5980
+ spokeThisTurn = false;
5981
+ // any non-empty text_delta streamed this turn
5982
+ externalSpeech = false;
5983
+ // host spoke on our behalf (adaptive micro-ack) — not reflex output
5984
+ heldThisTurn = false;
5985
+ // Hold called this turn → turn is INTENTIONALLY silent (suppress reflex text + no dead-air ack)
5986
+ nudging = false;
5987
+ // re-ack pass in flight: block ALL tools, prevent recursion
5988
+ reflexBuf = "";
5989
+ // accumulated reflex text this turn (fabricated-event detection)
5990
+ reflexForwarded = 0;
5991
+ // chars of reflexBuf already forwarded to the host/TTS
5992
+ fabricationCut = false;
5993
+ // reflex emitted a reserved [task …] marker → suppress its tail
5994
+ /** TRUE for the duration of a re-voice turn that is integrating ≥1 NON-CLEAN task (turn-eligibility,
5995
+ * carried out-of-band — NOT derived from any worker/brief string). ANY Act/Think dispatched in such a
5996
+ * turn is stamped followUp:true. This GUARANTEES the dangerous direction is impossible: a genuine
5997
+ * escalation (even one with a paraphrased brief) ALWAYS lands in a non-clean integration turn, so it is
5998
+ * ALWAYS recognized as a follow-up and CANNOT re-escalate (one hop). The single-dispatch-per-turn guard
5999
+ * means at most one dispatch happens per flush, so realistically "the one dispatch IS the escalation".
6000
+ * ACCEPTED SAFE-DIRECTION ERROR: if the reflex instead dispatches FRESH unrelated work during a non-clean
6001
+ * flush (rare — and only possible when it batches multiple calls in one step, bypassing the guard), that
6002
+ * fresh task is over-stamped followUp:true and forgoes ONE future auto-escalation. That is SAFE (it only
6003
+ * ever REMOVES a future escalation, never adds one — no runaway) and is the correct side to err on. */
6004
+ turnFollowUp = false;
6005
+ /** Hard absolute backstop against runaway regardless of attribution: total automatic escalations across
6006
+ * the whole conversation. Once it hits MAX_AUTO_ESCALATIONS, no integration turn offers escalate/re-delegate. */
6007
+ autoEscalations = 0;
6008
+ static MAX_AUTO_ESCALATIONS = 8;
6009
+ /** Parked worker questions awaiting a (voice-relayed) user answer, keyed by ask id. */
6010
+ pendingAsks = /* @__PURE__ */ new Map();
6011
+ /** SPECULATIVE REFLEX (VoiceEngine.onSpeculate → speculate()): the reflex turn starts on a stable
6012
+ * PARTIAL transcript, its spoken output buffered — nothing reaches TTS until the endpointed final
6013
+ * confirms via send() (which flushes the buffer and adopts the call as THE turn). A divergent final
6014
+ * aborts it: output dropped, history rolled back, the final dispatches normally. */
6015
+ spec;
6016
+ /** Aborted speculative calls (double-billing visibility — each one paid for tokens never spoken). */
6017
+ speculativeAbortedCalls = 0;
6018
+ /** Tools the reflex may execute on UNCONFIRMED (speculated) input — read-only lookups and the
6019
+ * intentionally-silent Hold. Anything else (Act/Think/AnswerTask/CancelTask/ExitSession/memory
6020
+ * writes/…) is a side effect on input the user may not have said: the guard ABORTS the speculation
6021
+ * instead (the endpointed final then dispatches normally and may use the tool for real). */
6022
+ static SPEC_SAFE_TOOLS = /* @__PURE__ */ new Set(["QuickLook", "TaskStatus", "Hold"]);
6023
+ /** Lazily resolved memory tools (async loadMemory runs in initMemory). */
6024
+ memoryReady;
6088
6025
  constructor(options) {
6089
- this.options = { ...new VoiceEngineOptions(), ...options };
6026
+ this.options = { ...new DuplexAgentOptions(), ...options };
6090
6027
  const o = this.options;
6091
- if (!o.stt || !o.tts || !o.player) throw new Error("VoiceEngine needs stt, tts and player (see cli/voice.ts VoiceIO for platform defaults)");
6092
- this.stt = o.stt;
6093
- this.tts = o.tts;
6094
- this.player = o.player;
6028
+ if (o.memoryDir && o.fs) {
6029
+ this.memoryReady = loadMemory(o.fs, o.memoryDir, { maxWritesPerSession: 10, userDir: o.memoryUserDir });
6030
+ }
6031
+ const memSlot = o.memoryDir && o.fs ? VOICE_MEMORY_PROMPT : "NEVER claim to have stored, saved, or remembered something durably \u2014 you cannot. Anything the user wants persisted (their name, preferences, notes) must go through Act so a worker writes it to memory.";
6032
+ const thinkSlot = o.thinkModel !== false ? THINK_GUIDANCE : THINK_DISABLED_GUIDANCE;
6033
+ const workerToolNames = (o.actOptions?.tools ?? []).map((t) => t.name);
6034
+ const canSearch = workerToolNames.some((n) => /WebSearch/i.test(n));
6035
+ const canFetch = workerToolNames.some((n) => /WebFetch/i.test(n));
6036
+ const workerWeb = canSearch ? `, and it CAN search the web and read web pages \u2014 so when the user gives you something specific to look up ("search for X", "find me\u2026", "what's the latest on\u2026"), route it to Act. But a bare capability QUESTION like "can you search the web?" just gets a short spoken "yes, I can" \u2014 do NOT dispatch and NEVER invent a query the user did not give you` : canFetch ? ", and it can fetch a specific web page URL (but cannot search the web)" : "";
6037
+ const mcpNames = [
6038
+ ...Object.keys(o.actOptions?.providerOptions?.mcpServers ?? {}),
6039
+ ...new Set(workerToolNames.filter((n) => n.startsWith("mcp__")).map((n) => n.slice(5).split("__")[0]))
6040
+ ];
6041
+ const workerMcp = mcpNames.length ? `, and it can use these MCP servers: ${[...new Set(mcpNames)].join(", ")}` + (mcpNames.some((n) => /browser/i.test(n)) ? ' \u2014 including driving a REAL browser (open tabs, navigate, click, screenshot), so answer "yes" if asked whether you can control/drive a browser and route an actual browse to Act' : "") : "";
6042
+ const prompt = VOICE_SYSTEM_PROMPT.replace("{{MEMORY_SLOT}}", memSlot).replace("{{THINK_SLOT}}", thinkSlot).replace("{{WORKER_WEB}}", workerWeb + workerMcp) + (o.voiceStyle === "conversational" ? "\n" + VOICE_STYLE_CONVERSATIONAL : "") + (o.emotionTags ? "\n" + EMOTION_TAGS_GUIDANCE : "") + `
6043
+ Today's date: ${(/* @__PURE__ */ new Date()).toDateString()}.`;
6044
+ const tools = [
6045
+ ...o.reflexOptions?.tools ?? [],
6046
+ this.actTool(),
6047
+ ...o.thinkModel !== false ? [this.thinkTool()] : [],
6048
+ this.taskStatusTool(),
6049
+ this.cancelTaskTool(),
6050
+ this.quickLookTool(),
6051
+ this.answerTaskTool(),
6052
+ this.holdTool()
6053
+ ];
6054
+ const host = o.host;
6055
+ const voiceHost = host && {
6056
+ ask: host.ask ? (q) => host.ask(q) : void 0,
6057
+ confirm: host.confirm ? (p, m) => host.confirm(p, m) : void 0,
6058
+ notify: (ev) => {
6059
+ if (ev?.kind === "text_delta" && typeof ev.message === "string") {
6060
+ if (this.heldThisTurn) return;
6061
+ if (this.fabricationCut) return;
6062
+ if (this.turnDispatched && this.spokeBeforeDispatch) return;
6063
+ const msg = ev.message;
6064
+ this.reflexBuf += msg;
6065
+ this.scrubStageDirections();
6066
+ const m = this.reflexBuf.match(RESERVED_EVENT_MARKER) ?? this.reflexBuf.match(RESERVED_EVENT_OPENER);
6067
+ if (m) {
6068
+ this.fabricationCut = true;
6069
+ log11.warn(`reflex fabricated a [task \u2026] event in its spoken stream \u2014 cutting it (kept ${m.index} chars)`);
6070
+ const safe = this.reflexBuf.slice(this.reflexForwarded, m.index);
6071
+ if (!safe) return;
6072
+ if (safe.trim()) this.spokeThisTurn = true;
6073
+ this.emitHost({ ...ev, message: safe });
6074
+ return;
6075
+ }
6076
+ const held = this.reflexBuf.length - this.reflexForwarded;
6077
+ const partial = held > 0 && /\[\s*t?a?s?k?$/i.test(this.reflexBuf.slice(-Math.min(held, 6)));
6078
+ let upto = partial ? this.reflexBuf.length - this.reflexBuf.slice(-6).match(/\[\s*t?a?s?k?$/i)[0].length : this.reflexBuf.length;
6079
+ const paren = this.reflexBuf.lastIndexOf("(");
6080
+ if (paren >= this.reflexForwarded && !this.reflexBuf.includes(")", paren) && this.reflexBuf.length - paren <= 80)
6081
+ upto = Math.min(upto, paren);
6082
+ const out = this.reflexBuf.slice(this.reflexForwarded, upto);
6083
+ this.reflexForwarded = upto;
6084
+ if (!out) return;
6085
+ if (out.trim()) this.spokeThisTurn = true;
6086
+ this.emitHost({ ...ev, message: out });
6087
+ return;
6088
+ }
6089
+ host.notify?.(ev);
6090
+ }
6091
+ };
6092
+ this.voice = new Agent({
6093
+ ai: o.ai,
6094
+ fs: new MemFilesystem2(),
6095
+ model: o.reflexModel,
6096
+ stream: true,
6097
+ host: voiceHost,
6098
+ // The reflex IS the conversational channel — it confirms ambiguity inline ("did you mean…?"),
6099
+ // never via the blocking AskUserQuestion tool (Agent auto-adds it whenever a host is set). Left in,
6100
+ // it stalls a voice turn until the kill-switch. Worker questions still reach the user via parkQuestion.
6101
+ askUserQuestion: false,
6102
+ systemPrompt: prompt,
6103
+ instructionFiles: false,
6104
+ maxSteps: 8,
6105
+ timeoutMs: 3e4,
6106
+ ...o.reflexOptions,
6107
+ tools,
6108
+ // Composed AFTER the spread so the dispatch guard can't be dropped by reflexOptions.
6109
+ hooks: composeHooks(this.dispatchGuard(), o.reflexOptions?.hooks)
6110
+ });
6095
6111
  }
6096
- async start() {
6097
- this.tts.onAudio = (c) => {
6098
- if (this.speaking) this.player.write(c);
6112
+ /** Resolve memory tools + inject index into voice system prompt (once). */
6113
+ async initMemory() {
6114
+ if (!this.memoryReady) return;
6115
+ const mem = await this.memoryReady;
6116
+ this.memoryReady = void 0;
6117
+ this.voice.options.tools.push(...mem.tools);
6118
+ if (mem.index) this.voice.options.systemPrompt += "\n\n" + mem.index;
6119
+ }
6120
+ /** Flush any held-back trailing fragment (a possible `[task` opener that never completed) once the
6121
+ * turn's stream is done — so a legit message ending in "[t" isn't silently dropped. */
6122
+ flushHeldReflexTail() {
6123
+ if (this.fabricationCut) return;
6124
+ this.scrubStageDirections();
6125
+ const tail = this.reflexBuf.slice(this.reflexForwarded);
6126
+ this.reflexForwarded = this.reflexBuf.length;
6127
+ if (!tail) return;
6128
+ if (tail.trim()) this.spokeThisTurn = true;
6129
+ this.emitHost({ kind: "text_delta", message: tail });
6130
+ }
6131
+ /** Remove complete stage-direction parentheticals from the UNFORWARDED reflex text (STAGE_DIRECTION_RE).
6132
+ * Only the unforwarded region is touched — already-spoken audio can't be unsent, and splicing before
6133
+ * reflexForwarded would corrupt the forward offset. */
6134
+ scrubStageDirections() {
6135
+ if (this.reflexForwarded >= this.reflexBuf.length) return;
6136
+ const region = this.reflexBuf.slice(this.reflexForwarded);
6137
+ const scrubbed = region.replace(/\([^()]*\)/g, (s) => STAGE_DIRECTION_RE.test(s) ? "" : s);
6138
+ if (scrubbed !== region) this.reflexBuf = this.reflexBuf.slice(0, this.reflexForwarded) + scrubbed;
6139
+ }
6140
+ /** Clear the per-turn guards. Called at the head of every voice turn (user send + re-voice flush). */
6141
+ resetTurn() {
6142
+ this.turnDispatched = false;
6143
+ this.spokeBeforeDispatch = false;
6144
+ this.turnBriefs.clear();
6145
+ this.spokeThisTurn = false;
6146
+ this.externalSpeech = false;
6147
+ this.heldThisTurn = false;
6148
+ this.reflexBuf = "";
6149
+ this.reflexForwarded = 0;
6150
+ this.fabricationCut = false;
6151
+ this.turnFollowUp = false;
6152
+ this.voice.options.toolChoice = void 0;
6153
+ }
6154
+ /** preToolUse guard on the reflex: once it has dispatched this turn, a dispatch is "said my piece,
6155
+ * now wait for the push" (CC's Task model). Block the temptations — TaskStatus polling and identical
6156
+ * re-dispatch — so the only remaining move is to voice a short ack and end. A genuinely NEW Act is
6157
+ * still allowed (parallel independent work). During a re-ack pass, block every tool. */
6158
+ dispatchGuard() {
6159
+ return {
6160
+ preToolUse: (call) => {
6161
+ if (this.spec?.state === "pending" && !_DuplexAgent.SPEC_SAFE_TOOLS.has(call.name)) {
6162
+ log11.verbose(`speculation aborted: reflex called ${call.name} on unconfirmed input`);
6163
+ this.abortSpeculation();
6164
+ return { block: true, reason: "Speculative turn aborted." };
6165
+ }
6166
+ if (this.nudging) return { block: true, reason: "Just say one short spoken acknowledgement \u2014 no tools this turn." };
6167
+ if (!this.turnDispatched) return;
6168
+ if (call.name === "TaskStatus")
6169
+ return { block: true, reason: "You just dispatched a task this turn \u2014 do NOT poll. Give one short spoken acknowledgement and end your turn; the result arrives later as a [task \u2026] event." };
6170
+ if ((call.name === "Act" || call.name === "Think") && this.turnBriefs.has(String(call.args?.brief ?? "")))
6171
+ return { block: true, reason: "You already dispatched this exact task \u2014 acknowledge briefly and end your turn." };
6172
+ }
6099
6173
  };
6100
- this.stt.onPartial = (text) => this.handlePartial(text);
6101
- this.stt.onUtterance = (text) => this.handleUtterance(text);
6102
- this.stt.onLevel = (rms) => this.handleLevel(rms);
6103
- await Promise.all([this.tts.connect(), this.stt.start()]);
6104
- this.setState("listening");
6105
- log11.debug(`voice I/O up (${this.stt.usingAec ? "AEC" : "heuristic echo"} capture)`);
6106
6174
  }
6107
- get usingAec() {
6108
- return this.stt.usingAec;
6175
+ /** The host spoke on this turn's behalf OUTSIDE the reflex stream (e.g. the voice engine's adaptive
6176
+ * micro-ack). For a DISPATCHED turn that's a sufficient ack (don't stack a second one); for an
6177
+ * inline turn whose reply came back empty it is NOT — "One sec." followed by permanent silence is
6178
+ * still dead air, so silentTurn ignores external speech unless work was dispatched. */
6179
+ noteExternalSpeech() {
6180
+ this.externalSpeech = true;
6109
6181
  }
6110
- /** Flip barge-in at runtime (e.g. the mic fell back to non-VPIO → go half-duplex so echo can't leak). */
6111
- setBargeIn(on) {
6112
- this.options.bargeIn = on;
6182
+ /** True when the just-finished turn voiced NOTHING — dead air to repair. Two ways this happens:
6183
+ * (a) a dispatch with no spoken ack, and (b) an INLINE turn whose `final` channel came back empty —
6184
+ * gpt-oss harmony sometimes puts the whole reply in `analysis` (→ thinking_delta, suppressed in
6185
+ * voice) and emits an empty `final`, so no text_delta ever streams. Both ship silence; both repair.
6186
+ * Requires a host: without one there's no stream to detect speech on (and no one to speak to). */
6187
+ get silentTurn() {
6188
+ const ackedByHost = this.externalSpeech && this.turnDispatched;
6189
+ return !!this.options.host && !this.spokeThisTurn && !ackedByHost && !this.heldThisTurn;
6113
6190
  }
6114
- /** Show/hide the `[emotion]` debug tags in the echo (next turn's stream picks it up). */
6115
- setShowEmotions(on) {
6116
- this.options.showEmotions = on;
6191
+ /** A turn that voiced nothing is dead air. Re-prompt the reflex ONCE so the LLM itself voices a short
6192
+ * line (no template). If it STILL says nothing, fall back to a minimal line so silence never ships.
6193
+ * Wording adapts to whether work was dispatched (an ack) or the inline reply was simply lost. */
6194
+ async ackIfSilent(fallback) {
6195
+ const dispatched = this.turnDispatched;
6196
+ this.nudging = true;
6197
+ try {
6198
+ await this.voice.send(fallback ? "[reminder] You said nothing to the user this turn. Tell them, in ONE short spoken sentence, what just happened \u2014 no tools." : dispatched ? "[reminder] You dispatched a task but said nothing to the user. Say ONE short spoken acknowledgement now \u2014 no tools." : "[reminder] You said nothing to the user this turn. Give your ONE short spoken reply now \u2014 no tools.");
6199
+ } catch (e) {
6200
+ log11.warn(`ack nudge failed: ${e instanceof Error ? e.message : e}`);
6201
+ } finally {
6202
+ this.nudging = false;
6203
+ }
6204
+ if (!this.spokeThisTurn) {
6205
+ const pool = fallback ? [fallback] : dispatched ? _DuplexAgent.FALLBACK_ACKS : _DuplexAgent.FALLBACK_RETRY;
6206
+ const fresh = pool.filter((p) => p !== this.lastFallback);
6207
+ const line = (fresh.length ? fresh : pool)[Math.floor(Math.random() * (fresh.length || pool.length))];
6208
+ this.lastFallback = line;
6209
+ this.emitHost({ kind: "text_delta", message: line });
6210
+ }
6117
6211
  }
6118
- idleWaiters = [];
6119
- setState(s) {
6120
- if (this.state === s) return;
6121
- this.state = s;
6122
- this.options.onState(s);
6123
- if (s !== "speaking" && s !== "thinking") {
6124
- for (const r of this.idleWaiters.splice(0)) r();
6212
+ /** Dead-air fallback pools (see ackIfSilent). Both retry lines keep the 'say that again' phrase —
6213
+ * hosts/tests key on it. */
6214
+ static FALLBACK_ACKS = ["Okay, on it.", "On it.", "Alright, working on it."];
6215
+ static FALLBACK_RETRY = ["Sorry, could you say that again?", "Hm, I missed that \u2014 could you say that again?"];
6216
+ lastFallback = "";
6217
+ /** One user turn: the voice agent streams the reply (and may Act/Think). Serialized with re-voice turns.
6218
+ * If a speculative reflex call is pending and this content CONFIRMS it (the final matches the
6219
+ * speculated partial), the in-flight call is adopted as THE turn: its buffered output flushes to the
6220
+ * host NOW (this is the latency win) and streaming continues live. Any other content aborts the
6221
+ * speculation first (rolled back silently) and runs a normal turn behind it. */
6222
+ send(content) {
6223
+ const spec = this.spec;
6224
+ if (spec?.state === "pending") {
6225
+ if (typeof content === "string" && speculationConfirms(spec.text, content)) {
6226
+ spec.state = "confirmed";
6227
+ spec.finalText = content;
6228
+ for (const ev of spec.buf.splice(0)) this.options.host?.notify?.(ev);
6229
+ spec.decide("confirm");
6230
+ this.notify("diag", "speculation_confirmed", { text: spec.text.slice(0, 80) });
6231
+ return spec.done;
6232
+ }
6233
+ this.abortSpeculation();
6125
6234
  }
6235
+ return this.enqueue(async () => {
6236
+ await this.initMemory();
6237
+ this.resetTurn();
6238
+ const res = await this.voice.send(content);
6239
+ this.flushHeldReflexTail();
6240
+ if (this.silentTurn) await this.ackIfSilent();
6241
+ return res;
6242
+ });
6126
6243
  }
6127
- /** Resolve when the engine is no longer speaking (immediate if already idle). */
6128
- awaitIdle() {
6129
- if (this.state !== "speaking" && this.state !== "thinking") return Promise.resolve();
6130
- return new Promise((r) => this.idleWaiters.push(r));
6244
+ /** Start a HELD speculative reflex turn on a stable partial (VoiceEngine.onSpeculate). Its spoken
6245
+ * output buffers in emitHost; send() later confirms (flush + adopt) or aborts it. The turn holds the
6246
+ * voice mutex until that decision (bounded by a safety timeout), so no other turn can interleave
6247
+ * into the transcript between the speculative messages and their rollback. No-op if a speculation
6248
+ * is already in flight. */
6249
+ speculate(text) {
6250
+ if (!text.trim() || this.spec) return;
6251
+ let decide;
6252
+ const decision = new Promise((r) => {
6253
+ decide = r;
6254
+ });
6255
+ const spec = {
6256
+ text,
6257
+ state: "pending",
6258
+ buf: [],
6259
+ ctl: new AbortController(),
6260
+ decide,
6261
+ decision,
6262
+ done: void 0
6263
+ };
6264
+ this.spec = spec;
6265
+ spec.done = this.enqueue(async () => {
6266
+ const empty = { text: "", steps: 0, finishReason: "aborted", messages: this.voice.transcript };
6267
+ if (spec.state === "aborted") {
6268
+ this.speculativeAbortedCalls++;
6269
+ if (this.spec === spec) this.spec = void 0;
6270
+ return empty;
6271
+ }
6272
+ await this.initMemory();
6273
+ this.resetTurn();
6274
+ const base = this.voice.transcript.length;
6275
+ const prevSignal = this.voice.options.signal;
6276
+ this.voice.options.signal = spec.ctl.signal;
6277
+ let res;
6278
+ try {
6279
+ res = await this.voice.send(spec.text);
6280
+ } catch (e) {
6281
+ log11.warn(`speculative turn failed: ${e instanceof Error ? e.message : e}`);
6282
+ } finally {
6283
+ this.voice.options.signal = prevSignal;
6284
+ }
6285
+ const timer = setTimeout(() => {
6286
+ spec.state = spec.state === "pending" ? "aborted" : spec.state;
6287
+ spec.decide("abort");
6288
+ }, 1e4);
6289
+ timer.unref?.();
6290
+ const d = await spec.decision;
6291
+ clearTimeout(timer);
6292
+ if (d === "abort") {
6293
+ if (this.voice.transcript.length > base) this.voice.transcript.length = base;
6294
+ this.speculativeAbortedCalls++;
6295
+ log11.verbose(`speculation aborted (${this.speculativeAbortedCalls} total): "${spec.text.slice(0, 50)}"`);
6296
+ if (this.spec === spec) this.spec = void 0;
6297
+ return res ?? empty;
6298
+ }
6299
+ for (let i = base; i < this.voice.transcript.length; i++) {
6300
+ const m = this.voice.transcript[i];
6301
+ if (m.role === "user" && contentText(m.content) === spec.text) {
6302
+ m.content = spec.finalText;
6303
+ break;
6304
+ }
6305
+ }
6306
+ this.spec = void 0;
6307
+ this.flushHeldReflexTail();
6308
+ if (this.silentTurn) await this.ackIfSilent();
6309
+ return res ?? empty;
6310
+ });
6131
6311
  }
6132
- // --- speaking side (host-driven) ---
6133
- /** open a spoken turn (idempotent — safe from both onUtterance and first-delta paths).
6134
- * `ack` speaks the configured micro-ack as the context opener (utterance path only —
6135
- * masks LLM TTFT; re-voice turns begun by their first delta skip it). */
6136
- beginSpeech(ack = false) {
6137
- if (this.speaking && this.ctxOpen) return;
6138
- if (this.drainTimer) {
6139
- clearTimeout(this.drainTimer);
6140
- this.drainTimer = null;
6141
- }
6142
- this.interrupted = false;
6143
- this.resetOverlap(true);
6144
- if (!this.speaking) this.player.markTurn();
6145
- this.speaking = true;
6146
- this.ctxOpen = true;
6147
- this.spokeDeltas = false;
6148
- this.reply = "";
6149
- this.emo = this.options.emotions ? new EmotionStream(this.options.showEmotions) : null;
6150
- this.echoWords = new Set(this.words(this.prevReply));
6151
- this.tts.newContext();
6152
- if (ack && this.options.ackPhrase) {
6153
- this.tts.speak(this.options.ackPhrase + " ", true);
6154
- this.spokeDeltas = true;
6155
- this.ackAt = now();
6156
- }
6157
- if (!this.turnStartAt) this.turnStartAt = now();
6158
- this.setState("thinking");
6312
+ /** Abort a pending speculation (idempotent): kill the in-flight call, drop its buffered output.
6313
+ * Called on divergence (send), a stale partial (VoiceEngine.onSpeculateAbort), a side-effect tool
6314
+ * attempt (dispatchGuard), or host paths that will never dispatch a final (commands, voice-off). */
6315
+ abortSpeculation() {
6316
+ const spec = this.spec;
6317
+ if (spec?.state !== "pending") return;
6318
+ spec.state = "aborted";
6319
+ spec.buf.length = 0;
6320
+ spec.ctl.abort();
6321
+ spec.decide("abort");
6322
+ this.notify("diag", "speculation_aborted", { text: spec.text.slice(0, 80) });
6159
6323
  }
6160
- /** Feed a spoken delta. Returns the on-screen echo text (emotion tags shown/hidden per config) so the
6161
- * host renders the SAME stream that was parsed for TTS — no second, state-doubling parse. */
6162
- speakDelta(text) {
6163
- if (this.interrupted) return "";
6164
- if (!this.speaking || !this.ctxOpen) this.beginSpeech();
6165
- const { speech, display, prose } = this.emo ? this.emo.feed(text) : { speech: text, display: text, prose: text };
6166
- this.reply += prose;
6167
- for (const w of this.words(this.reply)) this.echoWords.add(w);
6168
- this.tts.speak(forSpeech(speech), true);
6169
- if (!this.spokeDeltas && this.turnStartAt) log11.debug(`ttft: ${Math.round(now() - this.turnStartAt)}ms`);
6170
- this.spokeDeltas = true;
6171
- this.setState("speaking");
6172
- return display;
6324
+ /** Cancel a running background task — shared by the CancelTask tool and the CLI /tasks picker. */
6325
+ cancelTask(id) {
6326
+ const rec = this.tasks.get(id);
6327
+ if (!rec) return `No task '${id}'.`;
6328
+ if (rec.status !== "running") return `Task ${rec.id} is already ${rec.status}.`;
6329
+ rec.status = "cancelled";
6330
+ rec.controller.abort();
6331
+ return `Task ${rec.id} (${rec.label}) cancelled.`;
6173
6332
  }
6174
- /** close the spoken turn (idempotent); stays audible until ALL audio arrived AND playback drains */
6175
- endSpeech() {
6176
- this.interrupted = false;
6177
- if (!this.speaking) return;
6178
- this.ctxOpen = false;
6179
- if (this.emo) {
6180
- const t = this.emo.flush();
6181
- this.emo = null;
6182
- if (t.prose) this.reply += t.prose;
6183
- if (t.speech) this.tts.speak(forSpeech(t.speech), true);
6333
+ /** Barge-in: the user took the floor while task(s) were running. Suppress those tasks' remaining SPOKEN
6334
+ * delivery so a superseded topic never talks over the new one (the debt-after-jokes regression). The
6335
+ * tasks keep running and still fold their result into the transcript — recoverable, just not spoken.
6336
+ * Returns the parked ids (for logging). Does NOT cancel: that's a deliberate reflex/user action. */
6337
+ parkInFlightDeliveries() {
6338
+ const parked = [];
6339
+ for (const rec of this.tasks.values())
6340
+ if (rec.status === "running" && !rec.deliveryParked) {
6341
+ rec.deliveryParked = true;
6342
+ parked.push(rec.id);
6343
+ }
6344
+ return parked;
6345
+ }
6346
+ /** Resolve when all queued voice turns AND all in-flight worker tasks have settled (tests, graceful shutdown). */
6347
+ async idle() {
6348
+ while (true) {
6349
+ const q = this.queue;
6350
+ await q.catch(() => {
6351
+ });
6352
+ await Promise.all([...this.tasks.values()].map((t) => t.promise));
6353
+ if (this.queue === q && ![...this.tasks.values()].some((t) => t.status === "running")) return;
6184
6354
  }
6185
- if (this.reply) this.prevReply = this.reply;
6186
- const settle = () => {
6187
- if (this.ctxOpen) {
6188
- this.drainTimer = null;
6355
+ }
6356
+ /** Promise-chain mutex: turns run strictly one at a time; a failed turn doesn't poison the chain. */
6357
+ enqueue(fn) {
6358
+ const run = this.queue.then(fn, fn);
6359
+ this.queue = run.then(() => {
6360
+ }, () => {
6361
+ });
6362
+ return run;
6363
+ }
6364
+ notify(kind, message, data) {
6365
+ this.emitHost({ kind, message, data });
6366
+ }
6367
+ /** Host-boundary emit for the reflex's spoken channel: during a PENDING speculation, text_delta and
6368
+ * hold_filler are BUFFERED (nothing may reach TTS on unconfirmed input); confirm flushes them in
6369
+ * order, abort drops them silently. Everything else (task_* lifecycle, worker speak_utterance —
6370
+ * which bypasses this via host.notify directly) passes through untouched. */
6371
+ emitHost(ev) {
6372
+ const spec = this.spec;
6373
+ if (spec && (ev.kind === "text_delta" || ev.kind === "hold_filler")) {
6374
+ if (spec.state === "pending") {
6375
+ spec.buf.push(ev);
6189
6376
  return;
6190
6377
  }
6191
- if (this.pausedAt) {
6192
- this.drainTimer = setTimeout(settle, 250);
6193
- return;
6378
+ if (spec.state === "aborted") return;
6379
+ }
6380
+ this.options.host?.notify?.(ev);
6381
+ }
6382
+ /** Queue a `[task …]` event for re-voicing. Events arriving while the voice is busy coalesce into ONE turn.
6383
+ * `nonClean` (out-of-band boolean, set by the caller that KNOWS this event integrates a NON-CLEAN outcome)
6384
+ * marks the coalesced flush as a non-clean integration turn — turn-eligibility, never inferred from event
6385
+ * text and never keyed on a (re-authored) brief string. Any dispatch in such a turn is a follow-up. */
6386
+ queueRevoice(event, nonClean = false) {
6387
+ this.pendingEvents.push(event);
6388
+ if (nonClean) this.pendingNonClean = true;
6389
+ if (this.flushQueued) return;
6390
+ this.flushQueued = true;
6391
+ void this.enqueue(async () => {
6392
+ this.flushQueued = false;
6393
+ const events = this.pendingEvents.splice(0);
6394
+ const nonCleanTurn = this.pendingNonClean;
6395
+ this.pendingNonClean = false;
6396
+ if (!events.length) return;
6397
+ const failed = events.find((e) => /^\[task\b[^\]\n]*\bfailed\b/i.test(e));
6398
+ this.resetTurn();
6399
+ this.turnFollowUp = nonCleanTurn;
6400
+ await this.voice.send(events.join("\n"));
6401
+ this.flushHeldReflexTail();
6402
+ if (this.silentTurn) await this.ackIfSilent(failed ? "Sorry, that didn't work \u2014 the task failed." : void 0);
6403
+ this.notify("revoice_done", "");
6404
+ });
6405
+ }
6406
+ /** The worker's brief: the Act/Think args + a STATIC text snapshot of the recent conversation.
6407
+ * Act briefs get a self-verify footer — the worker's report is trusted without review, so it
6408
+ * must check its own work before reporting (nearly free under prompt caching; measured honest:
6409
+ * it does NOT fix one-shot logic bugs — see mind/10). Think tasks are pure reasoning — no footer. */
6410
+ buildBrief(brief, tier = "act", deliver = true) {
6411
+ const recent = this.voice.transcript.filter((m) => (m.role === "user" || m.role === "assistant") && contentText(m.content).trim()).slice(-this.options.excerptTurns).map((m) => `${m.role}: ${contentText(m.content)}`).join("\n");
6412
+ const verify = tier === "act" ? "\n\nBefore reporting done: re-read what you changed and check it against EVERY requirement above \u2014 fix any gap first. Your report is trusted without review." : "";
6413
+ const deliverContract = deliver ? `
6414
+
6415
+ ## DELIVER (spoken delivery)
6416
+ You are reporting back to a user who is LISTENING. Stream your work normally \u2014 your prose is the written work record and detail, and is NOT spoken. Wrap anything the user should HEAR in <spoken>\u2026</spoken> tags. LEAD WITH the actual content they asked for: if they asked for a specific piece of content \u2014 a value, a name, the actual lines, the writing itself \u2014 that content goes INSIDE the <spoken> tags, not a remark about it. Your FIRST <spoken> segment is substantive \u2014 never a greeting or an acknowledgement (the front-end has already acked; do not double-ack). Keep spoken text concise and natural for the ear: short sentences, no markdown. NEVER enumerate in speech \u2014 no numbered or bulleted lists ("One. \u2026 Two. \u2026" is robotic). Deliver multiple items as flowing conversation with brief connective phrasing ("here's one\u2026", "and another\u2026", "oh, and\u2026"), pausing between items with sentence breaks, not numbers.` + (this.options.emotionTags ? " Inside <spoken>, you may prefix a sentence with an inline [emotion] tag (e.g. [excited], [curious]) to color how it is voiced \u2014 only when it genuinely fits, and vary it; [laughter] gives a natural laugh." : "") : "";
6417
+ return (recent ? `${brief}
6418
+
6419
+ ## Recent conversation (for context)
6420
+ ${recent}` : brief) + verify + deliverContract;
6421
+ }
6422
+ /** Spawn a detached worker for task `id`; its settlement notifies + enqueues the re-voice turn. */
6423
+ spawnWorker(id, label, briefText, tier, brief, followUp) {
6424
+ const o = this.options;
6425
+ const tierOpts = tier === "think" ? o.thinkOptions : o.actOptions;
6426
+ const tierModel = tier === "think" ? o.thinkModel : o.actModel;
6427
+ const controller = new AbortController();
6428
+ const base = tierOpts?.hooks ?? o.actOptions?.hooks;
6429
+ const report = o.progressUpdates ? this.progressReporter(id) : void 0;
6430
+ const tail = [];
6431
+ const pushTail = (line) => {
6432
+ tail.push(line.slice(0, 200));
6433
+ if (tail.length > 120) tail.splice(0, tail.length - 120);
6434
+ };
6435
+ const hooks = {
6436
+ ...base,
6437
+ preToolUse: async (call, meta) => {
6438
+ const d = await base?.preToolUse?.(call, meta);
6439
+ pushTail(`\u2699 ${describeCall(call)}`);
6440
+ report?.pre(call);
6441
+ return d;
6442
+ },
6443
+ postToolUse: async (call, result, meta) => {
6444
+ await base?.postToolUse?.(call, result, meta);
6445
+ const last = result?.trim().split("\n").filter(Boolean).pop();
6446
+ if (last) pushTail(` \u21B3 ${last}`);
6447
+ report?.post(call);
6448
+ },
6449
+ onToolOutput: (call, chunk, meta) => {
6450
+ base?.onToolOutput?.(call, chunk, meta);
6451
+ report?.output(chunk);
6452
+ }
6453
+ };
6454
+ const relayAsk = async (q) => {
6455
+ const opts = q.options?.length ? ` Options: ${q.options.map((x) => x.label).join(", ")}.` : "";
6456
+ const a = await this.parkQuestion(id, `${q.question}${opts}`);
6457
+ return a || "(no answer from the user \u2014 use your best judgment and note the assumption)";
6458
+ };
6459
+ const splitter = new SpokenSplitter();
6460
+ const speak = (seg) => {
6461
+ if (seg && !this.tasks.get(id)?.deliveryParked) o.host?.notify?.({ kind: "speak_utterance", message: seg });
6462
+ };
6463
+ const coalescer = new SentenceCoalescer();
6464
+ const feedSpoken = (s) => {
6465
+ const ready = coalescer.feed(s);
6466
+ if (ready) speak(ready);
6467
+ };
6468
+ const flushSpoken = () => speak(coalescer.flush());
6469
+ const askBridge = o.askRelay ? { ask: relayAsk } : o.host?.ask ? { ask: (q) => o.host.ask(q) } : {};
6470
+ const workerHost = {
6471
+ ...askBridge,
6472
+ notify: (ev) => {
6473
+ if (ev?.kind === "text_delta" && typeof ev.message === "string") {
6474
+ const { spoken, detail } = splitter.feed(ev.message);
6475
+ feedSpoken(spoken);
6476
+ if (detail.trim()) pushTail(detail.trim());
6477
+ return;
6478
+ }
6479
+ }
6480
+ };
6481
+ const agentOpts = {
6482
+ ai: o.ai,
6483
+ fs: o.fs,
6484
+ model: tierModel,
6485
+ ...tier === "think" ? { reasoning: tierOpts?.reasoning ?? "high" } : {},
6486
+ ...tierOpts,
6487
+ // Recompute providerOptions for THIS worker's model (after tierOpts so it wins over any inherited
6488
+ // main-template value) — prevents cursor-only cwd/cursorSession leaking onto an anthropic worker.
6489
+ providerOptions: o.providerOptionsFor?.(tierModel),
6490
+ stream: true,
6491
+ // worker streams text_delta so the splitter can extract <spoken> live (after tierOpts: never overridden off)
6492
+ host: workerHost,
6493
+ // carries BOTH ask AND the <spoken>-splitting notify
6494
+ ...hooks ? { hooks } : {},
6495
+ signal: controller.signal
6496
+ // shared with the checker so a cancel tears down both
6497
+ };
6498
+ const promise = new Agent(agentOpts).run(briefText).then((res) => {
6499
+ const { spoken, detail } = splitter.flush();
6500
+ feedSpoken(spoken);
6501
+ if (detail.trim()) pushTail(detail.trim());
6502
+ flushSpoken();
6503
+ return res;
6504
+ }).then((res) => this.maybeVerify(id, brief, res, tier, agentOpts, askBridge)).then((res) => this.onWorkerSettled(id, res)).catch((err) => this.onWorkerFailed(id, err));
6505
+ this.tasks.set(id, { id, label, status: "running", controller, promise, tail, brief, followUp, splitter });
6506
+ if (this.tasks.size > this.options.maxTaskRecords)
6507
+ for (const [tid, rec] of this.tasks) {
6508
+ if (this.tasks.size <= this.options.maxTaskRecords) break;
6509
+ if (rec.status !== "running") this.tasks.delete(tid);
6194
6510
  }
6195
- this.drainTimer = null;
6196
- this.speaking = false;
6197
- if (this.turnStartAt) log11.debug(`turn: ${Math.round(now() - this.turnStartAt)}ms (incl. playback)`);
6198
- this.echoUntil = now() + 2500;
6199
- if (!this.usingAec) this.stt.reset();
6200
- this.setState("listening");
6201
- if (this.uttQueue.length) this.pumpQueue();
6511
+ }
6512
+ /** Fresh-context check of a successful Act task: a NEW agent (same model/fs/tools, but NO shared
6513
+ * conversation context) re-reads the file state against the brief and fixes any gap. The fix lands
6514
+ * on the shared fs automatically (workers write fs directly, no overlay), so grading sees the
6515
+ * corrected state. Bounded to ONE pass. Off unless `verifyActTasks`; never runs for think/failed/
6516
+ * cancelled tasks. Usage is merged so /cost reflects the real (worker + checker) spend. */
6517
+ async maybeVerify(id, brief, res, tier, agentOpts, askBridge) {
6518
+ if (!this.options.verifyActTasks || tier !== "act" || res.finishReason !== "stop") return res;
6519
+ if (this.tasks.get(id)?.status === "cancelled") return res;
6520
+ const { stream: _stream, host: _host, ...restOpts } = agentOpts;
6521
+ const checkerOpts = {
6522
+ ...restOpts,
6523
+ ...askBridge.ask ? { host: { ask: askBridge.ask } } : {}
6202
6524
  };
6203
- const drainThenSettle = () => {
6204
- if (this.drainTimer) clearTimeout(this.drainTimer);
6205
- this.drainTimer = setTimeout(settle, this.player.drainMs() + 300);
6525
+ const checkBrief = `${this.buildBrief(brief, tier, false)}
6526
+
6527
+ ## VERIFY MODE
6528
+ Another agent just implemented the above. Independently check the CURRENT state of the files against EVERY requirement. Fix any gap you find. If everything is already correct, make NO changes \u2014 do not refactor or improve \u2014 and report "verified".`;
6529
+ this.notify("task_verify", `task ${id}: verifying`, { id });
6530
+ const cres = await new Agent(checkerOpts).run(checkBrief);
6531
+ if (cres.finishReason !== "stop") {
6532
+ log11.warn(`task ${id}: verify inconclusive (${cres.finishReason})`);
6533
+ this.notify("task_verify", `task ${id}: verify inconclusive (${cres.finishReason})`, { id, finishReason: cres.finishReason });
6534
+ }
6535
+ const sum = (a = 0, b = 0) => a + b;
6536
+ return {
6537
+ ...res,
6538
+ steps: res.steps + cres.steps,
6539
+ // Merge the checker's messages so downstream tool-call/step accounting includes BOTH agents
6540
+ // (else a verified task's toolCalls would undercount vs its steps/usage).
6541
+ messages: [...res.messages, ...cres.messages],
6542
+ usageEstimated: res.usageEstimated || cres.usageEstimated,
6543
+ usage: res.usage && cres.usage ? {
6544
+ promptTokens: sum(res.usage.promptTokens, cres.usage.promptTokens),
6545
+ completionTokens: sum(res.usage.completionTokens, cres.usage.completionTokens),
6546
+ totalTokens: sum(res.usage.totalTokens, cres.usage.totalTokens),
6547
+ cacheCreationTokens: sum(res.usage.cacheCreationTokens, cres.usage.cacheCreationTokens),
6548
+ cacheReadTokens: sum(res.usage.cacheReadTokens, cres.usage.cacheReadTokens)
6549
+ } : res.usage ?? cres.usage
6206
6550
  };
6207
- if (this.spokeDeltas) {
6208
- this.tts.onDone = drainThenSettle;
6209
- this.tts.end();
6210
- if (this.drainTimer) clearTimeout(this.drainTimer);
6211
- this.drainTimer = setTimeout(drainThenSettle, 15e3);
6212
- } else drainThenSettle();
6213
6551
  }
6214
- /** text of the reply cut by the last barge-in — consumed by the host to tell the model what
6215
- * the user did NOT hear. Cleared on read. */
6216
- takeInterruptedReply() {
6217
- const r = this.lastInterrupted;
6218
- this.lastInterrupted = null;
6219
- return r;
6552
+ /** Throttled per-task progress: worker tool calls → at most one progress re-voice per interval.
6553
+ * Two sources, one throttle: completed steps (post) and a heartbeat for a SINGLE long tool call
6554
+ * (pre records the in-flight call; a self-cleaning timer narrates "still inside Bash — 70s").
6555
+ * Completion supersedes: nothing is emitted once the task has settled. */
6556
+ progressReporter(id) {
6557
+ let lastAt = Date.now();
6558
+ let steps = 0;
6559
+ let inflight = null;
6560
+ const due = () => {
6561
+ if (this.pendingAsks.size) return void 0;
6562
+ const rec = this.tasks.get(id);
6563
+ return rec && rec.status === "running" && Date.now() - lastAt >= this.options.progressIntervalMs ? rec : void 0;
6564
+ };
6565
+ const emit = (rec, line, call) => {
6566
+ lastAt = Date.now();
6567
+ this.notify("task_progress", `task ${id} (${rec.label}): ${line}`, { id, steps, call: call.name });
6568
+ this.queueRevoice(`[task ${id} progress] ${line}`);
6569
+ };
6570
+ const timer = setInterval(() => {
6571
+ const rec = this.tasks.get(id);
6572
+ if (!rec || rec.status !== "running") return clearInterval(timer);
6573
+ if (!inflight || !due()) return;
6574
+ const last = inflight.tail.trim().split("\n").filter(Boolean).pop()?.slice(-80);
6575
+ emit(rec, `still inside ${describeCall(inflight.call)} \u2014 ${Math.round((Date.now() - inflight.at) / 1e3)}s on this step${last ? `, last output: ${last}` : ""}`, inflight.call);
6576
+ }, Math.max(this.options.progressIntervalMs, 250));
6577
+ timer.unref?.();
6578
+ return {
6579
+ pre: (call) => {
6580
+ inflight = { call, at: Date.now(), tail: "" };
6581
+ },
6582
+ output: (chunk) => {
6583
+ if (inflight) inflight.tail = (inflight.tail + chunk).slice(-500);
6584
+ },
6585
+ // digest only — NEVER re-voices directly
6586
+ post: (call) => {
6587
+ steps++;
6588
+ inflight = null;
6589
+ const rec = due();
6590
+ if (rec) emit(rec, `still running \u2014 ${steps} steps so far, now: ${describeCall(call)}`, call);
6591
+ }
6592
+ };
6220
6593
  }
6221
- /** Speak a short filler phrase without starting a model turn (stays in listening mode after). */
6222
- speakFiller(text) {
6223
- if (!text || this.speaking) return;
6224
- this.beginSpeech();
6225
- this.speakDelta(text);
6226
- this.endSpeech();
6594
+ /** Park a question under `askId` (a task id, or any unique key for permission asks): re-voices
6595
+ * '[task <id> asks] …' and resolves with the user's answer via AnswerTask — or '' on timeout/
6596
+ * task settle (callers map '' to deny / best-judgment). Workers never block forever. */
6597
+ parkQuestion(askId, question) {
6598
+ return new Promise((resolve) => {
6599
+ let settled = false;
6600
+ const finish = (answer) => {
6601
+ if (settled) return;
6602
+ settled = true;
6603
+ clearTimeout(timer);
6604
+ this.pendingAsks.delete(askId);
6605
+ resolve(answer);
6606
+ };
6607
+ const timer = setTimeout(() => {
6608
+ this.notify("task_ask_timeout", `task ${askId}: question timed out \u2014 proceeding without an answer`);
6609
+ finish("");
6610
+ }, this.options.askTimeoutMs);
6611
+ this.pendingAsks.set(askId, { question, resolve: finish });
6612
+ this.notify("task_ask", `task ${askId} asks: ${question}`, { id: askId, question });
6613
+ this.queueRevoice(`[task ${askId} asks] ${question}
6614
+ (Relay this to the user in your own words. When they answer, call AnswerTask with id "${askId}" and their answer.)`);
6615
+ });
6227
6616
  }
6228
- /** Enqueue a COMPLETE worker utterance (already-split spoken text) onto the central speech queue.
6229
- * If nothing is currently speaking it plays immediately; otherwise it queues and plays after the
6230
- * current utterance fully ends (settle → pumpQueue) — never spliced into an open reflex utterance. */
6231
- enqueueUtterance(text) {
6232
- if (!text || !/[\p{L}\p{N}]/u.test(text)) return;
6233
- this.uttQueue.push(text);
6234
- if (!this.speaking) this.pumpQueue();
6617
+ /** Resolve any question a settling/cancelled task left parked (its answer can no longer matter). */
6618
+ dropAsk(id) {
6619
+ this.pendingAsks.get(id)?.resolve("");
6235
6620
  }
6236
- /** Play the next queued worker utterance as its own one-shot turn (begin → delta → end). Drives the
6237
- * next one from the settle completion (endSpeech), so utterances serialize without overlap. */
6238
- pumpQueue() {
6239
- if (this.speaking) return;
6240
- const text = this.uttQueue.shift();
6241
- if (text == null) return;
6242
- this.beginSpeech();
6243
- this.speakDelta(text);
6244
- this.endSpeech();
6621
+ /** Build the INTEGRATION TURN prompt for a NON-CLEAN settled worker (early stop / failure). A clean
6622
+ * success never reaches here — it streams its own `<spoken>` delivery during the run. For a partial
6623
+ * or failed result the outcome re-enters the reflex as a decision (like a tool_result flowing back
6624
+ * into a normal agent loop): the reflex evaluates the outcome against the original intent and chooses
6625
+ * what to do next.
6626
+ *
6627
+ * Decision branches (the reflex acts on them with EXISTING tools — no new surface):
6628
+ * • accept → SPEAK the (partial) result plainly — don't dress a failure up as success.
6629
+ * • escalate → call `Think` with the SAME brief — only when Act failed/stalled AND a Think tier
6630
+ * exists AND this task wasn't already a follow-up (one hop max). Wires the dead
6631
+ * "Reserve Think for a problem Act already FAILED at" promise.
6632
+ * • re-delegate→ call `Act` with a CORRECTED brief — for a recoverable error / partial result.
6633
+ * • ask → ask the user ONE concrete question if genuinely blocked.
6634
+ *
6635
+ * Keeps the `[task <id> completed]` / `[task <id> failed]` opener so existing coalescing + the
6636
+ * failed-revoice fallback still fire, and the per-event transcript markers stay intact. */
6637
+ integrationPrompt(rec, outcome, body, finishReason) {
6638
+ const opener = outcome === "error" ? `[task ${rec.id} failed]` : `[task ${rec.id} completed]`;
6639
+ const underCap = this.autoEscalations < _DuplexAgent.MAX_AUTO_ESCALATIONS;
6640
+ const canEscalate = (outcome === "error" || outcome === "incomplete") && underCap;
6641
+ const hasThink = this.options.thinkModel !== false;
6642
+ const options = [];
6643
+ if (!rec.followUp && canEscalate && hasThink)
6644
+ options.push("ESCALATE to the Think tier (call Think with the same brief) if this is a hard/architectural problem the Act worker stalled or failed on");
6645
+ if (!rec.followUp && canEscalate)
6646
+ options.push("RE-DELEGATE to Act with a corrected brief if the failure looks recoverable (a wrong path, a fixable mistake)");
6647
+ options.push("ASK the user one short, concrete question if you genuinely cannot proceed without their input");
6648
+ options.push("ACCEPT and tell the user plainly what happened (don't dress a failure up as success)");
6649
+ const decision = options.length > 1 ? ` You must decide what to do next \u2014 choose ONE: ${options.map((o, i) => `(${i + 1}) ${o}`).join("; ")}. Pick exactly one and act on it; do not voice this as a finished success.` : ` Tell the user plainly what happened \u2014 do not present this as a finished success.`;
6650
+ const state = outcome === "error" ? `the worker FAILED with: ${body}` : `the worker STOPPED EARLY (${finishReason}) \u2014 its result is PARTIAL, not a finished success: ${body}`;
6651
+ return `${opener} Original request: "${rec.brief}". Outcome: ${state}.${decision}`;
6245
6652
  }
6246
- /** barge-in: stop audio NOW, cancel generation, reset for the user's utterance */
6247
- interrupt() {
6248
- this.uttQueue = [];
6249
- if (!this.speaking && !this.drainTimer) return;
6250
- if (this.drainTimer) {
6251
- clearTimeout(this.drainTimer);
6252
- this.drainTimer = null;
6653
+ onWorkerSettled(id, res) {
6654
+ this.dropAsk(id);
6655
+ const rec = this.tasks.get(id);
6656
+ if (res.finishReason === "aborted" || rec.status === "cancelled") {
6657
+ rec.status = "cancelled";
6658
+ this.notify("task_cancelled", `task ${id} (${rec.label}) cancelled`);
6659
+ return;
6253
6660
  }
6254
- this.resetOverlap(false);
6255
- this.lastResumeAt = 0;
6256
- const heardChars = Math.round(Math.max(0, this.player.playedMs()) / 1e3 * 15);
6257
- if (this.reply) this.lastInterrupted = { full: this.reply, heard: this.reply.slice(0, heardChars) };
6258
- this.speaking = false;
6259
- this.ctxOpen = false;
6260
- this.interrupted = true;
6261
- this.suspectUntil = 0;
6262
- this.echoUntil = now() + Math.max(2500, this.player.drainMs() + 3e3);
6263
- this.tts.cancel();
6264
- this.player.kill();
6265
- if (!this.usingAec) this.stt.reset();
6266
- if (this.reply) this.prevReply = this.reply;
6267
- this.setState("listening");
6661
+ if (res.finishReason === "error") {
6662
+ const msg = res.error instanceof Error ? res.error.message : String(res.error ?? "unknown error");
6663
+ return this.failTask(rec, msg);
6664
+ }
6665
+ rec.status = "done";
6666
+ rec.result = res.text;
6667
+ const incomplete = res.finishReason !== "stop";
6668
+ log11.verbose(`task ${id} done (${res.steps} steps${incomplete ? `, INCOMPLETE: ${res.finishReason}` : ""})`);
6669
+ this.notify("task_done", `task ${id} (${rec.label}) completed`, {
6670
+ id,
6671
+ text: res.text,
6672
+ usage: res.usage,
6673
+ usageEstimated: res.usageEstimated,
6674
+ finishReason: res.finishReason,
6675
+ steps: res.steps,
6676
+ toolCalls: res.messages.filter((m) => m.role === "tool").length
6677
+ });
6678
+ if (incomplete) {
6679
+ return this.queueRevoice(this.integrationPrompt(rec, "incomplete", res.text, res.finishReason), true);
6680
+ }
6681
+ const tail = rec.splitter?.flush();
6682
+ if (tail?.spoken && !rec.deliveryParked) this.options.host?.notify?.({ kind: "speak_utterance", message: tail.spoken });
6683
+ if (res.text.trim()) this.voice.transcript.push({ role: "assistant", content: res.text });
6684
+ if (!rec.splitter?.spokeAny && res.text.trim() && !rec.deliveryParked)
6685
+ this.options.host?.notify?.({ kind: "speak_utterance", message: res.text });
6268
6686
  }
6269
- stop() {
6270
- this.uttQueue = [];
6271
- if (this.resumeTimer) clearTimeout(this.resumeTimer);
6272
- if (this.pendingTimer) clearTimeout(this.pendingTimer);
6273
- if (this.drainTimer) clearTimeout(this.drainTimer);
6274
- this.stt.stop();
6275
- this.player.kill();
6276
- this.tts.close();
6277
- this.setState("idle");
6687
+ onWorkerFailed(id, err) {
6688
+ this.failTask(this.tasks.get(id), err instanceof Error ? err.message : String(err));
6278
6689
  }
6279
- // --- listening side (STT-driven) ---
6280
- words(s) {
6281
- return s.toLowerCase().replace(/[^a-z0-9\s]/g, "").split(/\s+/).filter((w) => w.length >= 2);
6690
+ failTask(rec, msg) {
6691
+ this.dropAsk(rec.id);
6692
+ rec.status = "error";
6693
+ rec.result = msg;
6694
+ log11.warn(`task ${rec.id} failed: ${msg}`);
6695
+ this.notify("task_error", `task ${rec.id} (${rec.label}) failed: ${msg}`);
6696
+ this.queueRevoice(this.integrationPrompt(rec, "error", msg, "error"), true);
6282
6697
  }
6283
- novelWords(text) {
6284
- return this.words(text).filter((w) => !this.echoWords.has(w));
6698
+ // --- voice tools (closures over this instance) ---
6699
+ /** Live-switch the think tier: `false` disables (removes the Think tool from the voice agent),
6700
+ * a model id enables (adds the tool if missing). The system-prompt THINK_SLOT text is frozen at
6701
+ * construction — the tool's own description carries the routing guidance, so a live enable works;
6702
+ * dispatch()'s think→act fallback covers any straggler calls after a live disable. */
6703
+ setThinkModel(model) {
6704
+ this.options.thinkModel = model;
6705
+ const tools = this.voice.options.tools;
6706
+ const i = tools.findIndex((t) => t.name === "Think");
6707
+ if (model === false && i >= 0) tools.splice(i, 1);
6708
+ else if (model !== false && i < 0) tools.push(this.thinkTool());
6285
6709
  }
6286
- echoActive() {
6287
- return this.speaking || now() < this.echoUntil;
6710
+ /** User/programmatic spawn: the CLI's /act and /think commands. Returns the task id.
6711
+ * `followUp` marks an automatic escalation/re-delegation (set by the integration turn) so the new
6712
+ * task's own integration turn won't escalate again — capping auto-follow-ups to one hop. */
6713
+ async dispatch(brief, tier = "act", label, followUp = false) {
6714
+ if (tier === "think" && this.options.thinkModel === false) tier = "act";
6715
+ if (followUp) this.autoEscalations++;
6716
+ const id = `t${++this.seq}`;
6717
+ const lbl = label ?? tier;
6718
+ await this.options.onTaskStart?.(id, lbl);
6719
+ this.spawnWorker(id, lbl, this.buildBrief(brief, tier), tier, brief, followUp);
6720
+ this.notify("task_started", `task ${id} (${lbl}) started`, { id, brief, tier });
6721
+ return id;
6288
6722
  }
6289
- /** Genuine user speech vs our own bleed (AEC tier): novel words must DOMINATE, not merely exist.
6290
- * Degraded AEC + an STT mis-hearing manufactures a single novel word out of pure echo (a name or
6291
- * rare word in our own reply comes back transcribed slightly differently — 1 novel / N words).
6292
- * A real interjection is mostly novel ("stop", "wait what") — short utterances pass on ratio,
6293
- * longer ones on count. */
6294
- genuine(text) {
6295
- const total = this.words(text).length;
6296
- const novel = this.novelWords(text).length;
6297
- return novel > 0 && novel / Math.max(1, total) > 0.5;
6723
+ actTool() {
6724
+ return {
6725
+ name: "Act",
6726
+ description: 'Escalate real work (reading/editing files, searching, running tasks, building) to a standard background worker. Returns immediately with a task id; the result arrives later as a "[task <id> completed]" event. Provide a clear, self-contained `brief` (the worker does not hear the live conversation).',
6727
+ parameters: {
6728
+ type: "object",
6729
+ required: ["brief"],
6730
+ properties: {
6731
+ brief: { type: "string", description: "full, self-contained instructions for the worker" },
6732
+ label: { type: "string", description: "a short (2-4 word) label for the task" }
6733
+ }
6734
+ },
6735
+ run: async ({ brief, label }) => {
6736
+ this.spokeBeforeDispatch = this.spokeThisTurn;
6737
+ this.turnDispatched = true;
6738
+ this.turnBriefs.add(String(brief ?? ""));
6739
+ this.voice.options.toolChoice = "none";
6740
+ const id = await this.dispatch(String(brief ?? ""), "act", label ? String(label) : void 0, this.turnFollowUp);
6741
+ return this.spokeBeforeDispatch ? `Acting on task ${id}. You already acknowledged \u2014 end your turn now with NO further text; the result will arrive as a [task ${id} completed] event.` : `Acting on task ${id}. Acknowledge briefly; the result will arrive as a [task ${id} completed] event.`;
6742
+ }
6743
+ };
6298
6744
  }
6299
- handlePartial(text) {
6300
- if (this.speaking) {
6301
- if (!this.options.bargeIn) return;
6302
- if (now() < this.bargeGraceUntil) {
6303
- if (!this.echoActive() || (this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) this.options.onPartial(text);
6304
- return;
6745
+ thinkTool() {
6746
+ return {
6747
+ name: "Think",
6748
+ description: "Escalate to a premium deep-reasoning agent for complex analysis, architecture decisions, hard debugging, or planning. Same async pattern as Act \u2014 returns a task id. Use when the problem needs careful thought before (or instead of) action. Do not use Think for simple tasks \u2014 Act is cheaper and faster.",
6749
+ parameters: {
6750
+ type: "object",
6751
+ required: ["brief"],
6752
+ properties: {
6753
+ brief: { type: "string", description: "the question or problem to reason about deeply" },
6754
+ label: { type: "string", description: "a short (2-4 word) label for the task" }
6755
+ }
6756
+ },
6757
+ run: async ({ brief, label }) => {
6758
+ this.spokeBeforeDispatch = this.spokeThisTurn;
6759
+ this.turnDispatched = true;
6760
+ this.turnBriefs.add(String(brief ?? ""));
6761
+ this.voice.options.toolChoice = "none";
6762
+ const id = await this.dispatch(String(brief ?? ""), "think", label ? String(label) : void 0, this.turnFollowUp);
6763
+ return this.spokeBeforeDispatch ? `Thinking on task ${id}. You already acknowledged \u2014 end your turn now with NO further text; the result will arrive as a [task ${id} completed] event.` : `Thinking on task ${id}. Acknowledge briefly; the result will arrive as a [task ${id} completed] event.`;
6305
6764
  }
6306
- if (this.overlapCapable) {
6307
- const txt = text.trim();
6308
- if (!txt || txt === this.lastOverlapPartial) return;
6309
- this.lastOverlapPartial = txt;
6310
- if (!this.genuine(txt)) {
6311
- if (this.pausedAt) this.armResume();
6312
- return;
6765
+ };
6766
+ }
6767
+ taskStatusTool() {
6768
+ return {
6769
+ name: "TaskStatus",
6770
+ description: "Status of background tasks. Pass `id` for one task, or omit it to list all.",
6771
+ parameters: { type: "object", properties: { id: { type: "string" } } },
6772
+ run: async ({ id }) => {
6773
+ const list = id ? [this.tasks.get(String(id))].filter(Boolean) : [...this.tasks.values()];
6774
+ if (!list.length) return id ? `No task '${id}'.` : "No background tasks.";
6775
+ return list.map((t) => `${t.id} (${t.label}): ${t.status}`).join("\n");
6776
+ }
6777
+ };
6778
+ }
6779
+ /** Sub-100ms read-only lookups the voice may do itself — everything else stays Act-only.
6780
+ * fs-only (no shell; the engine is VFS-abstracted): time, git branch (.git/HEAD read), ls, file
6781
+ * head. Output is hard-capped so a lookup can never bloat the skinny voice context. */
6782
+ quickLookTool() {
6783
+ const CAP = 2e3;
6784
+ const kinds = [.../* @__PURE__ */ new Set(["time", "branch", "ls", "file", "capabilities", ...Object.keys(this.options.quickLook ?? {})])];
6785
+ return {
6786
+ name: "QuickLook",
6787
+ description: `Instant read-only lookup \u2014 one of: ${kinds.join(", ")}. For trivial facts only; anything needing search, commands, or reasoning goes through Act.`,
6788
+ parameters: {
6789
+ type: "object",
6790
+ required: ["what"],
6791
+ properties: {
6792
+ what: { type: "string", enum: kinds, description: "what to look up" },
6793
+ path: { type: "string", description: "for ls/file: the path to look at" }
6313
6794
  }
6314
- if (!this.pausedAt) {
6315
- this.pausedAt = now();
6316
- this.player.pause();
6317
- if (this.lastResumeAt && now() - this.lastResumeAt < this.options.overlapRepauseCedeMs) {
6318
- this.interrupt();
6319
- this.options.onBargeIn(this.ctxOpen ? "speaking" : "drain");
6320
- return;
6795
+ },
6796
+ run: async ({ what, path }) => {
6797
+ const fs = this.options.fs;
6798
+ try {
6799
+ const over = this.options.quickLook?.[String(what)];
6800
+ if (over) return await over(path ? String(path) : void 0);
6801
+ switch (String(what)) {
6802
+ case "capabilities": {
6803
+ const actTools = this.options.actOptions?.tools ?? [];
6804
+ const names = actTools.map((t) => t.name);
6805
+ const mcpServers = Object.keys(this.options.actOptions?.providerOptions?.mcpServers ?? {});
6806
+ const mcpNote = mcpServers.length ? ` Plus MCP servers your worker can use: ${mcpServers.join(", ")} (e.g. browser-bridge \u2192 drive a real browser: open tabs, navigate, click, screenshot).` : "";
6807
+ if (!names.length)
6808
+ return "Your worker uses Act's default local toolset (reading/editing files, running shell commands). No extra tools (e.g. web/internet) are configured; if a request is not a basic file or shell operation, assume you can't do it and say so." + mcpNote;
6809
+ const hasFetch = names.some((n) => /WebFetch/i.test(n));
6810
+ const hasBrowser = names.some((n) => /browser.*(navigate|click|page|type)/i.test(n));
6811
+ const hasSearch = names.some((n) => /(^|_)WebSearch$|search/i.test(n) && !/WebFetch|browser/i.test(n));
6812
+ const notes = [];
6813
+ if (hasFetch) notes.push("WebFetch retrieves ONE specific URL you are given \u2014 it is not a search engine.");
6814
+ if (hasBrowser) notes.push("The browser tools drive a real browser: you CAN open a site and, if needed, navigate to a search engine and search there \u2014 but it is manual and takes a moment, not an instant lookup.");
6815
+ else if (!hasSearch && hasFetch) notes.push('You have no general web-search tool, so for an instant "search the web" you can only fetch a URL they provide.');
6816
+ const webNote = notes.length ? " NOTE: " + notes.join(" ") : "";
6817
+ return `Tools your background worker (Act) can actually use: ${names.join(", ")}. Read each name literally and match the request to a SPECIFIC tool; if none fits, you do NOT have that ability \u2014 say so honestly.` + webNote + mcpNote;
6818
+ }
6819
+ case "time":
6820
+ return (/* @__PURE__ */ new Date()).toString();
6821
+ case "branch": {
6822
+ if (!fs) return "unavailable (no filesystem)";
6823
+ const head = (await fs.readFile(".git/HEAD")).trim();
6824
+ return head.startsWith("ref: refs/heads/") ? `branch: ${head.slice("ref: refs/heads/".length)}` : `detached HEAD at ${head.slice(0, 12)}`;
6825
+ }
6826
+ case "ls": {
6827
+ if (!fs) return "unavailable (no filesystem)";
6828
+ const p = String(path ?? ".");
6829
+ try {
6830
+ const names = await fs.readDir(p);
6831
+ return names.slice(0, 50).join("\n") + (names.length > 50 ? `
6832
+ \u2026 (+${names.length - 50} more)` : "");
6833
+ } catch {
6834
+ const names = await fs.readDir(".").catch(() => []);
6835
+ return `'${p}' not found here \u2014 you are likely already inside it. Current directory listing:
6836
+ ` + names.slice(0, 50).join("\n") + (names.length > 50 ? `
6837
+ \u2026 (+${names.length - 50} more)` : "");
6838
+ }
6839
+ }
6840
+ case "file": {
6841
+ if (!fs) return "unavailable (no filesystem)";
6842
+ if (!path) return "file lookup needs a path";
6843
+ try {
6844
+ const text = await fs.readFile(String(path));
6845
+ return text.length > CAP ? text.slice(0, CAP) + `
6846
+ \u2026 (truncated \u2014 ${text.length} chars total; Act for the full file)` : text;
6847
+ } catch {
6848
+ const names = await fs.readDir(".").catch(() => []);
6849
+ return `'${path}' not found. Current directory contains:
6850
+ ` + names.slice(0, 50).join("\n");
6851
+ }
6852
+ }
6853
+ default:
6854
+ return `unknown lookup '${what}'`;
6321
6855
  }
6856
+ } catch (e) {
6857
+ return `lookup failed: ${e?.message ?? e}`;
6322
6858
  }
6323
- if (this.words(txt).length >= 2) {
6324
- const phase = this.ctxOpen ? "speaking" : "drain";
6325
- this.interrupt();
6326
- this.options.onBargeIn(phase);
6327
- return;
6328
- }
6329
- this.armResume();
6330
- return;
6331
6859
  }
6332
- const barge = this.usingAec ? this.genuine(text) : this.novelWords(text).length >= (this.suspectUntil ? 1 : 2);
6333
- if (barge) {
6334
- const phase = this.ctxOpen ? "speaking" : "drain";
6335
- this.interrupt();
6336
- this.options.onBargeIn(phase);
6860
+ };
6861
+ }
6862
+ answerTaskTool() {
6863
+ return {
6864
+ name: "AnswerTask",
6865
+ description: `Relay the user's answer to a pending question from a background task (the "[task <id> asks]" events). Pass the id from the event and the user's answer.`,
6866
+ parameters: {
6867
+ type: "object",
6868
+ required: ["id", "answer"],
6869
+ properties: { id: { type: "string" }, answer: { type: "string", description: "the user's answer, verbatim or faithfully summarized" } }
6870
+ },
6871
+ run: async ({ id, answer }) => {
6872
+ const ask = this.pendingAsks.get(String(id));
6873
+ if (!ask) return `No pending question for '${id}' \u2014 it may have been answered already or timed out.`;
6874
+ ask.resolve(String(answer ?? ""));
6875
+ return `Answer relayed \u2014 task ${id} resumes.`;
6337
6876
  }
6338
- return;
6339
- }
6340
- if (this.pendingUtt && text.trim()) {
6341
- if (this.pendingTimer) clearTimeout(this.pendingTimer);
6342
- this.pendingTimer = setTimeout(() => this.flushUtterance(), Math.max(800, this.options.utteranceMergeMs));
6343
- }
6344
- if (!this.echoActive() || (this.usingAec ? this.genuine(text) : this.novelWords(text).length >= 1)) this.options.onPartial(text);
6877
+ };
6345
6878
  }
6346
- /** Merge a resumed utterance into the pending one, deduping any word-overlap. Soniox re-finalizes
6347
- * overlapping audio when the silence-timer and the semantic `<end>` both endpoint a growing
6348
- * utterance (or after a reconnect): the next "utterance" repeats the tail of the previous one, and
6349
- * a naive `${prev} ${next}` produced the live duplication ("Um, I want to check if Um, I want to
6350
- * check if…"). Find the longest suffix of `prev`'s words that prefixes `next` and drop it. */
6351
- mergeUtterance(prev, next) {
6352
- if (!prev) return next;
6353
- if (!next) return prev;
6354
- const pw = prev.split(/\s+/), nw = next.split(/\s+/);
6355
- const norm2 = (w) => w.toLowerCase().replace(/[^a-z0-9]/g, "");
6356
- const max = Math.min(pw.length, nw.length);
6357
- for (let k = max; k > 0; k--) {
6358
- let match = true;
6359
- for (let i = 0; i < k; i++) if (norm2(pw[pw.length - k + i]) !== norm2(nw[i])) {
6360
- match = false;
6361
- break;
6879
+ holdTool() {
6880
+ return {
6881
+ name: "Hold",
6882
+ description: 'The user seems mid-thought \u2014 hold the turn (stay listening) instead of answering. Optionally pass a short filler ("mhm", "go on") to speak while waiting. Use when the message sounds incomplete, trailing off, or like they paused to think.',
6883
+ parameters: {
6884
+ type: "object",
6885
+ properties: {
6886
+ filler: { type: "string", description: 'optional short filler to speak ("mhm", "go on", "mm-hm")' }
6887
+ }
6888
+ },
6889
+ run: async ({ filler }) => {
6890
+ this.heldThisTurn = true;
6891
+ this.notify("hold_filler", filler ? String(filler) : "");
6892
+ return "Holding \u2014 listening for the rest of the user's thought. Do not respond further this turn.";
6362
6893
  }
6363
- if (match) return [...pw, ...nw.slice(k)].join(" ");
6364
- }
6365
- return `${prev} ${next}`;
6894
+ };
6366
6895
  }
6367
- static TRAIL_RE = /(?:^|\s)(?:and|but|or|so|to|the|a|an|of|in|for|with|that|if|uh|um|like|about|from|into|on|is|are|was|were|,)$/i;
6368
- /** The utterance sounds like the user paused mid-thought (trailing conjunction/filler/comma). */
6369
- looksIncomplete(text) {
6370
- return _VoiceEngine.TRAIL_RE.test(text.trim());
6896
+ cancelTaskTool() {
6897
+ return {
6898
+ name: "CancelTask",
6899
+ description: "Cancel a running background task by id.",
6900
+ parameters: { type: "object", required: ["id"], properties: { id: { type: "string" } } },
6901
+ run: async ({ id }) => this.cancelTask(String(id))
6902
+ };
6371
6903
  }
6372
- handleUtterance(text) {
6373
- if (this.speaking && (this.ctxOpen || this.pausedAt) && this.overlapCapable) {
6374
- this.stt.reset();
6375
- return;
6904
+ };
6905
+
6906
+ // src/mcp.ts
6907
+ function toResult(result) {
6908
+ if (result == null) return { text: "" };
6909
+ if (typeof result === "string") return { text: result };
6910
+ const content = result.content;
6911
+ if (Array.isArray(content)) {
6912
+ const texts = [];
6913
+ const images = [];
6914
+ for (const c of content) {
6915
+ if (c?.type === "image" && typeof c.data === "string" && c.mimeType) {
6916
+ images.push({ mimeType: c.mimeType, data: c.data });
6917
+ } else if (typeof c?.text === "string") {
6918
+ texts.push(c.text);
6919
+ } else {
6920
+ texts.push(JSON.stringify(c));
6921
+ }
6376
6922
  }
6377
- if (this.echoActive() && (!this.options.bargeIn || (this.usingAec ? !this.genuine(text) : this.novelWords(text).length < 2))) {
6378
- this.stt.reset();
6379
- return;
6923
+ const text = texts.join("\n");
6924
+ if (text || images.length) return { text, ...images.length ? { images } : {} };
6925
+ }
6926
+ return { text: JSON.stringify(result) };
6927
+ }
6928
+ function mcpToolToAgentTool(spec, callTool, prefix = "mcp__") {
6929
+ return {
6930
+ name: `${prefix}${spec.name}`,
6931
+ description: spec.description ?? `MCP tool ${spec.name}`,
6932
+ parameters: spec.inputSchema ?? { type: "object", properties: {} },
6933
+ async run(args, _ctx) {
6934
+ const r = toResult(await callTool(spec.name, args ?? {}));
6935
+ return r.images?.length ? r : r.text;
6380
6936
  }
6381
- const squash = (t) => t.toLowerCase().replace(/[^a-z]/g, "").replace(/(.)\1+/g, "$1");
6382
- if (this.ackAt && now() - this.ackAt < 6e3 && squash(text) === squash(this.options.ackPhrase)) {
6383
- this.ackAt = 0;
6384
- return;
6937
+ };
6938
+ }
6939
+ function mcpToolsToAgentTools(specs, callTool, prefix = "mcp__", filter) {
6940
+ return (filter ? specs.filter(filter) : specs).map((s) => mcpToolToAgentTool(s, callTool, prefix));
6941
+ }
6942
+ function describeSpec(s) {
6943
+ const schema = s.inputSchema ? `
6944
+ args: ${JSON.stringify(s.inputSchema)}` : "";
6945
+ return `${s.name} \u2014 ${s.description ?? "(no description)"}${schema}`;
6946
+ }
6947
+ function makeMcpToolSearch(specs, callTool, options = {}) {
6948
+ const maxResults = options.maxResults ?? 10;
6949
+ const byName = new Map(specs.map((s) => [s.name, s]));
6950
+ const catalogLine = `${specs.length} MCP tool(s) available \u2014 search by keyword, then call by exact name.`;
6951
+ const searchTool = {
6952
+ name: "ToolSearch",
6953
+ description: `Search the available MCP tools by keyword (${catalogLine}). Returns matching tool names + their argument schemas; call one with \`McpCall\`.`,
6954
+ parameters: { type: "object", required: ["query"], properties: { query: { type: "string", description: "keywords to match against tool name + description" } } },
6955
+ async run({ query }) {
6956
+ const q = String(query ?? "").trim();
6957
+ if (!q) return catalogLine;
6958
+ const { kept } = topByRelevance(specs, q, (s) => `${s.name} ${s.description ?? ""}`, maxResults);
6959
+ if (!kept.length) return `(no MCP tool matches "${q}" \u2014 try broader keywords)`;
6960
+ return kept.map(describeSpec).join("\n");
6385
6961
  }
6386
- this.pendingUtt = this.mergeUtterance(this.pendingUtt, text);
6387
- if (this.pendingTimer) clearTimeout(this.pendingTimer);
6388
- if (this.options.incompleteMergeMs && this.looksIncomplete(this.pendingUtt)) {
6389
- log11.verbose(`hold: incomplete utterance "${this.pendingUtt.slice(-40)}"`);
6390
- this.options.onHold();
6391
- if (this.options.holdFiller && !this.speaking) {
6392
- this.beginSpeech();
6393
- this.speakDelta(this.options.holdFiller);
6394
- this.endSpeech();
6962
+ };
6963
+ const callMcpTool = {
6964
+ name: "McpCall",
6965
+ description: "Call an MCP tool discovered via `ToolSearch`, by its exact name. Pass its arguments as `args`.",
6966
+ parameters: {
6967
+ type: "object",
6968
+ required: ["name"],
6969
+ properties: {
6970
+ name: { type: "string", description: "exact tool name from ToolSearch" },
6971
+ args: { type: "object", description: "arguments object for the tool (per its schema)" }
6395
6972
  }
6396
- this.pendingTimer = setTimeout(() => this.flushUtterance(), this.options.incompleteMergeMs);
6397
- return;
6398
- }
6399
- if (!this.options.utteranceMergeMs || this.words(this.pendingUtt).length >= 4) return this.flushUtterance();
6400
- this.pendingTimer = setTimeout(() => this.flushUtterance(), this.options.utteranceMergeMs);
6401
- }
6402
- flushUtterance() {
6403
- if (this.pendingTimer) {
6404
- clearTimeout(this.pendingTimer);
6405
- this.pendingTimer = null;
6973
+ },
6974
+ async run({ name, args }) {
6975
+ const n = String(name ?? "");
6976
+ if (!byName.has(n)) return `Error: unknown MCP tool '${n}'. Use ToolSearch to find valid names.`;
6977
+ const r = toResult(await callTool(n, args ?? {}));
6978
+ return r.images?.length ? r : r.text;
6406
6979
  }
6407
- const text = this.pendingUtt;
6408
- this.pendingUtt = "";
6409
- if (text) {
6410
- this.turnStartAt = now();
6411
- this.bargeGraceUntil = now() + this.options.bargeGraceMs;
6412
- this.options.onUtterance(text);
6980
+ };
6981
+ return [searchTool, callMcpTool];
6982
+ }
6983
+ function buildMcpCatalog(servers) {
6984
+ const specs = [];
6985
+ const routes = /* @__PURE__ */ new Map();
6986
+ for (const m of servers) {
6987
+ for (const s of m.specs) {
6988
+ const base = `mcp__${m.name}__${s.name}`.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 128);
6989
+ let display = base;
6990
+ for (let i = 2; routes.has(display); i++) display = `${base.slice(0, 128 - String(i).length - 1)}_${i}`;
6991
+ specs.push({ name: display, description: s.description, inputSchema: s.inputSchema });
6992
+ routes.set(display, { server: m.name, rawName: s.name });
6413
6993
  }
6414
6994
  }
6415
- get overlapCapable() {
6416
- return this.usingAec && this.options.overlapPause && !!this.player.pause && !!this.player.resume;
6995
+ return { specs, routes };
6996
+ }
6997
+ function searchOverCatalog(servers, specs, routes, resolve, options) {
6998
+ const tools = specs.length ? makeMcpToolSearch(specs, (name, args) => {
6999
+ const r = routes.get(name);
7000
+ if (!r) throw new Error(`unknown MCP tool '${name}' \u2014 use ToolSearch to find valid names`);
7001
+ return resolve(r.server, r.rawName, args ?? {});
7002
+ }, options) : [];
7003
+ return { tools, serverNames: servers, toolCount: specs.length };
7004
+ }
7005
+ function makeMcpToolSearchFromMounted(mounted, options) {
7006
+ const { specs, routes } = buildMcpCatalog(mounted);
7007
+ const byName = new Map(mounted.map((m) => [m.name, m]));
7008
+ return searchOverCatalog(mounted.map((m) => m.name), specs, routes, (server, rawName, args) => byName.get(server).client.callTool(rawName, args), options);
7009
+ }
7010
+ function makeLazyMcpToolSearch(servers, resolve, options) {
7011
+ const { specs, routes } = buildMcpCatalog(servers);
7012
+ return searchOverCatalog(servers.map((s) => s.name), specs, routes, resolve, options);
7013
+ }
7014
+
7015
+ // src/hooks.ts
7016
+ var RecordingHooks = class {
7017
+ /** tool name -> reason; a matching preToolUse call is blocked with that reason. */
7018
+ constructor(blocks = {}) {
7019
+ this.blocks = blocks;
6417
7020
  }
6418
- armResume() {
6419
- if (this.resumeTimer) clearTimeout(this.resumeTimer);
6420
- this.resumeTimer = setTimeout(() => {
6421
- this.resumeTimer = null;
6422
- if (!this.pausedAt) return;
6423
- this.stt.reset();
6424
- this.resetOverlap(true);
6425
- }, this.options.overlapResumeMs);
7021
+ blocks;
7022
+ pre = [];
7023
+ post = [];
7024
+ outputs = [];
7025
+ stops = [];
7026
+ preToolUse(call, meta) {
7027
+ this.pre.push({ call, meta });
7028
+ const reason = this.blocks[call.name];
7029
+ if (reason != null) return { block: true, reason };
6426
7030
  }
6427
- resetOverlap(resume) {
6428
- if (this.resumeTimer) {
6429
- clearTimeout(this.resumeTimer);
6430
- this.resumeTimer = null;
6431
- }
6432
- if (this.pausedAt && resume) {
6433
- this.player.resume?.();
6434
- this.lastResumeAt = now();
6435
- }
6436
- this.pausedAt = 0;
6437
- this.lastOverlapPartial = "";
6438
- this.gatePassTimes = [];
7031
+ postToolUse(call, result, meta) {
7032
+ this.post.push({ call, result, meta });
6439
7033
  }
6440
- /** energy two-stage barge-in (heuristic tier only): spike over echo baseline → pause + confirm via STT */
6441
- gatePassTimes = [];
6442
- // recent gate-PASSING chunks (helper zeroes residue — nonzero = vetted)
6443
- handleLevel(rms) {
6444
- if (this.usingAec) {
6445
- if (!this.options.overlapEnergyHold || !this.speaking || !this.overlapCapable || this.pausedAt || rms < 50) return;
6446
- const t = now();
6447
- this.gatePassTimes = this.gatePassTimes.filter((x) => t - x < 350);
6448
- this.gatePassTimes.push(t);
6449
- if (this.gatePassTimes.length < 2) return;
6450
- this.gatePassTimes = [];
6451
- this.pausedAt = t;
6452
- this.player.pause();
6453
- this.armResume();
6454
- return;
6455
- }
6456
- if (!this.speaking) {
6457
- this.baseline = 0;
6458
- this.hot = 0;
6459
- return;
6460
- }
6461
- if (!this.baseline) {
6462
- this.baseline = rms;
6463
- return;
6464
- }
6465
- this.baseline = this.baseline * 0.9 + rms * 0.1;
6466
- if (rms > Math.max(this.baseline * this.options.bargeRmsMult, this.options.bargeRmsFloor)) this.hot++;
6467
- else this.hot = 0;
6468
- if (this.hot >= 2 && !this.suspectUntil) {
6469
- this.suspectUntil = now() + 1300;
6470
- setTimeout(() => {
6471
- this.suspectUntil = 0;
6472
- }, 1350);
6473
- }
7034
+ onToolOutput(call, chunk, meta) {
7035
+ this.outputs.push({ call, chunk, meta });
7036
+ }
7037
+ onStop(finalText) {
7038
+ this.stops.push(finalText);
7039
+ }
7040
+ };
7041
+ var RecordingLifecycle = class {
7042
+ /** @param startContext injected at session start; @param rewrite maps a submitted prompt to a new one. */
7043
+ constructor(startContext, rewrite) {
7044
+ this.startContext = startContext;
7045
+ this.rewrite = rewrite;
7046
+ }
7047
+ startContext;
7048
+ rewrite;
7049
+ starts = 0;
7050
+ prompts = [];
7051
+ compactions = [];
7052
+ subagentStops = [];
7053
+ onSessionStart() {
7054
+ this.starts++;
7055
+ return this.startContext;
7056
+ }
7057
+ onUserPromptSubmit(text) {
7058
+ this.prompts.push(text);
7059
+ return this.rewrite?.(text);
7060
+ }
7061
+ onPreCompact(messages) {
7062
+ this.compactions.push(messages.length);
7063
+ }
7064
+ onSubagentStop(summary, info) {
7065
+ this.subagentStops.push({ summary, label: info?.label });
6474
7066
  }
6475
7067
  };
6476
7068
 
7069
+ // src/index.ts
7070
+ init_logging();
7071
+
6477
7072
  // src/voice/soniox.ts
6478
7073
  init_logging();
6479
7074
 
@@ -6486,7 +7081,7 @@ async function resolveAuth(auth) {
6486
7081
 
6487
7082
  // src/voice/soniox.ts
6488
7083
  var log12 = forComponent("SonioxSTT");
6489
- var now2 = () => performance.now();
7084
+ var now = () => performance.now();
6490
7085
  var SonioxSTTOptions = class {
6491
7086
  auth = "";
6492
7087
  source;
@@ -6515,6 +7110,20 @@ var SonioxSTT = class {
6515
7110
  * loop). The host tears voice down instead of spinning forever. */
6516
7111
  onFatal = () => {
6517
7112
  };
7113
+ /** Diagnostic tap (provider errors, ws reconnects, no-audio watchdog). Fire-and-forget: a throwing
7114
+ * handler disables itself — never affects transcription. Wired by VoiceIO / the lab bridge. */
7115
+ onDiag = () => {
7116
+ };
7117
+ diagOn = true;
7118
+ diag(kind, fields) {
7119
+ if (!this.diagOn) return;
7120
+ try {
7121
+ this.onDiag({ t: now(), kind, ...fields });
7122
+ } catch (e) {
7123
+ this.diagOn = false;
7124
+ log12.debug(`onDiag threw \u2014 STT diagnostics disabled: ${e instanceof Error ? e.message : e}`);
7125
+ }
7126
+ }
6518
7127
  lastChunkAt = 0;
6519
7128
  // timestamp of the most recent mic chunk (0 = none yet)
6520
7129
  startedChunksAt = 0;
@@ -6555,8 +7164,12 @@ var SonioxSTT = class {
6555
7164
  this.ws.onclose = (ev) => {
6556
7165
  if (this.stopped) return;
6557
7166
  log12.warn(`soniox ws closed (${ev.code} ${ev.reason || ""}) \u2014 reconnecting`);
7167
+ this.diag("stt_ws_closed", { code: ev.code, reason: String(ev.reason || ""), reconnecting: true });
6558
7168
  this.reset();
6559
- this.connectWs().catch((e) => log12.error(`soniox reconnect failed: ${e.message}`));
7169
+ this.connectWs().catch((e) => {
7170
+ log12.error(`soniox reconnect failed: ${e.message}`);
7171
+ this.diag("stt_reconnect_failed", { message: e.message });
7172
+ });
6560
7173
  };
6561
7174
  }
6562
7175
  async start() {
@@ -6565,20 +7178,21 @@ var SonioxSTT = class {
6565
7178
  this.sourceStarted = true;
6566
7179
  this.endpointTimer = setInterval(() => {
6567
7180
  const combined = (this.finalText + this.partialText).trim();
6568
- if (!combined || now2() - this.lastChangeAt < this.options.silenceEndpointMs) return;
6569
- if (this.firstTokenAt) log12.debug(`stt: ${Math.round(now2() - this.firstTokenAt)}ms first-token\u2192silence-endpoint, "${combined.slice(0, 60)}"`);
7181
+ if (!combined || now() - this.lastChangeAt < this.options.silenceEndpointMs) return;
7182
+ if (this.firstTokenAt) log12.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192silence-endpoint, "${combined.slice(0, 60)}"`);
6570
7183
  this.reset();
6571
- this.onUtterance(combined, now2());
7184
+ this.onUtterance(combined, now());
6572
7185
  }, 120);
6573
7186
  this.endpointTimer.unref?.();
6574
- this.startedChunksAt = now2();
7187
+ this.startedChunksAt = now();
6575
7188
  const noAudioMs = this.options.noAudioTimeoutMs;
6576
7189
  if (noAudioMs > 0) {
6577
7190
  this.noAudioTimer = setInterval(() => {
6578
7191
  if (this.stopped) return;
6579
7192
  const ref = this.lastChunkAt || this.startedChunksAt;
6580
- if (now2() - ref > noAudioMs) {
7193
+ if (now() - ref > noAudioMs) {
6581
7194
  log12.error(`stt: no mic audio for >${Math.round(noAudioMs / 1e3)}s \u2014 capture device stopped delivering`);
7195
+ this.diag("stt_watchdog_fatal", { noAudioMs });
6582
7196
  this.onFatal("microphone stopped delivering audio (try a different input device, e.g. AirPods, or check System Settings \u2192 Sound \u2192 Input)");
6583
7197
  this.stop();
6584
7198
  }
@@ -6586,7 +7200,7 @@ var SonioxSTT = class {
6586
7200
  this.noAudioTimer.unref?.();
6587
7201
  }
6588
7202
  await this.options.source.start((chunk) => {
6589
- this.lastChunkAt = now2();
7203
+ this.lastChunkAt = now();
6590
7204
  let sum = 0;
6591
7205
  const view = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
6592
7206
  for (let i = 0; i + 1 < chunk.byteLength; i += 2) {
@@ -6598,7 +7212,10 @@ var SonioxSTT = class {
6598
7212
  });
6599
7213
  }
6600
7214
  handle(m) {
6601
- if (m.error_message) return log12.error(`soniox: ${m.error_message}`);
7215
+ if (m.error_message) {
7216
+ this.diag("stt_error", { message: String(m.error_message), code: m.error_code });
7217
+ return log12.error(`soniox: ${m.error_message}`);
7218
+ }
6602
7219
  let endpoint = false;
6603
7220
  for (const t of m.tokens ?? []) {
6604
7221
  if (t.text === "<end>") endpoint = true;
@@ -6608,15 +7225,15 @@ var SonioxSTT = class {
6608
7225
  const combined = this.finalText + this.partialText;
6609
7226
  if (combined !== this.lastCombined) {
6610
7227
  this.lastCombined = combined;
6611
- this.lastChangeAt = now2();
6612
- if (!this.firstTokenAt && combined.trim()) this.firstTokenAt = now2();
7228
+ this.lastChangeAt = now();
7229
+ if (!this.firstTokenAt && combined.trim()) this.firstTokenAt = now();
6613
7230
  }
6614
7231
  this.onPartial(combined);
6615
7232
  if (endpoint && this.finalText.trim()) {
6616
7233
  const utterance = this.finalText.trim();
6617
- if (this.firstTokenAt) log12.debug(`stt: ${Math.round(now2() - this.firstTokenAt)}ms first-token\u2192endpoint, "${utterance.slice(0, 60)}"`);
7234
+ if (this.firstTokenAt) log12.debug(`stt: ${Math.round(now() - this.firstTokenAt)}ms first-token\u2192endpoint, "${utterance.slice(0, 60)}"`);
6618
7235
  this.reset();
6619
- this.onUtterance(utterance, now2());
7236
+ this.onUtterance(utterance, now());
6620
7237
  }
6621
7238
  }
6622
7239
  reset() {
@@ -6638,7 +7255,7 @@ var SonioxSTT = class {
6638
7255
  // src/voice/cartesia.ts
6639
7256
  init_logging();
6640
7257
  var log13 = forComponent("CartesiaTTS");
6641
- var now3 = () => performance.now();
7258
+ var now2 = () => performance.now();
6642
7259
  var CartesiaTTSOptions = class {
6643
7260
  auth = "";
6644
7261
  voiceId = "";
@@ -6655,6 +7272,20 @@ var CartesiaTTS = class _CartesiaTTS {
6655
7272
  };
6656
7273
  onDone = () => {
6657
7274
  };
7275
+ /** Diagnostic tap (provider errors, circuit-breaker open/close, ws reconnects). Fire-and-forget:
7276
+ * a throwing handler disables itself — never affects synthesis. Wired by VoiceIO / the lab bridge. */
7277
+ onDiag = () => {
7278
+ };
7279
+ diagOn = true;
7280
+ diag(kind, fields) {
7281
+ if (!this.diagOn) return;
7282
+ try {
7283
+ this.onDiag({ t: now2(), kind, ...fields });
7284
+ } catch (e) {
7285
+ this.diagOn = false;
7286
+ log13.debug(`onDiag threw \u2014 TTS diagnostics disabled: ${e instanceof Error ? e.message : e}`);
7287
+ }
7288
+ }
6658
7289
  firstAudioAt = 0;
6659
7290
  /** Circuit breaker: consecutive error count + down flag. */
6660
7291
  consecutiveErrors = 0;
@@ -6688,8 +7319,12 @@ var CartesiaTTS = class _CartesiaTTS {
6688
7319
  });
6689
7320
  this.ws.onclose = (ev) => {
6690
7321
  log13.warn(`cartesia ws closed (${ev.code} ${ev.reason || ""})`);
7322
+ this.diag("tts_ws_closed", { code: ev.code, reason: String(ev.reason || ""), reconnecting: !this.closed });
6691
7323
  if (!this.closed) {
6692
- this.connecting = this.doConnect().catch((e) => log13.error(`cartesia reconnect failed: ${e.message}`));
7324
+ this.connecting = this.doConnect().catch((e) => {
7325
+ log13.error(`cartesia reconnect failed: ${e.message}`);
7326
+ this.diag("tts_reconnect_failed", { message: e.message });
7327
+ });
6693
7328
  }
6694
7329
  };
6695
7330
  this.ws.onmessage = (ev) => {
@@ -6698,7 +7333,7 @@ var CartesiaTTS = class _CartesiaTTS {
6698
7333
  if (m.type === "chunk" && m.data) {
6699
7334
  this.consecutiveErrors = 0;
6700
7335
  this.markRecovered();
6701
- if (!this.firstAudioAt) this.firstAudioAt = now3();
7336
+ if (!this.firstAudioAt) this.firstAudioAt = now2();
6702
7337
  this.onAudio(base64ToBytes(m.data));
6703
7338
  } else if (m.type === "done") {
6704
7339
  this.consecutiveErrors = 0;
@@ -6707,15 +7342,17 @@ var CartesiaTTS = class _CartesiaTTS {
6707
7342
  } else if (m.type === "error") {
6708
7343
  if (/already been cancelled|does not exist/.test(m.message || "")) return;
6709
7344
  this.consecutiveErrors++;
7345
+ this.diag("tts_error", { message: String(m.message || ""), code: m.status_code, contextId: m.context_id, consecutive: this.consecutiveErrors });
6710
7346
  if (!this.down && this.consecutiveErrors >= _CartesiaTTS.CB_THRESHOLD) {
6711
7347
  this.down = true;
6712
- this.downAt = now3();
7348
+ this.downAt = now2();
6713
7349
  this.consecutiveOk = 0;
6714
7350
  log13.warn(`TTS circuit breaker open \u2014 ${this.consecutiveErrors} consecutive errors, switching to text-only`);
7351
+ this.diag("tts_breaker_open", { errors: this.consecutiveErrors });
6715
7352
  this.onDone();
6716
7353
  this.startProbe();
6717
7354
  } else if (!this.down) {
6718
- log13.warn(`cartesia: ${JSON.stringify(m)}`);
7355
+ (/No valid transcripts/i.test(m.message || "") ? log13.debug : log13.warn)(`cartesia: ${JSON.stringify(m)}`);
6719
7356
  }
6720
7357
  }
6721
7358
  };
@@ -6728,7 +7365,8 @@ var CartesiaTTS = class _CartesiaTTS {
6728
7365
  this.down = false;
6729
7366
  this.consecutiveOk = 0;
6730
7367
  this.stopProbe();
6731
- const downMs = this.downAt ? now3() - this.downAt : 0;
7368
+ const downMs = this.downAt ? now2() - this.downAt : 0;
7369
+ this.diag("tts_breaker_close", { downMs: Math.round(downMs) });
6732
7370
  (downMs < 2e3 ? log13.debug : log13.info)(`TTS recovered${downMs ? ` (down ${downMs}ms)` : ""}`);
6733
7371
  }
6734
7372
  /** Ensure the WS is open before sending — reconnects if idle-closed. */
@@ -6736,6 +7374,15 @@ var CartesiaTTS = class _CartesiaTTS {
6736
7374
  if (this.connecting) await this.connecting;
6737
7375
  if (this.ws?.readyState !== WebSocket.OPEN) await this.connect();
6738
7376
  }
7377
+ /** Prime the synthesis pipeline on a throwaway context right after connect: the FIRST synthesis on
7378
+ * a fresh WS pays Cartesia's cold spin-up (~1s+ live) — absorb it at mic-on instead of turn one.
7379
+ * The returned audio is discarded (engine drops onAudio while not speaking; once a real turn calls
7380
+ * newContext() any stragglers are stale-context-filtered). Respects the circuit breaker. */
7381
+ warmup() {
7382
+ if (this.down) return;
7383
+ this.newContext();
7384
+ if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame("Ok.", false));
7385
+ }
6739
7386
  newContext() {
6740
7387
  this.ctxId = `ctx-${++this.ctxSeq}`;
6741
7388
  this.firstAudioAt = 0;
@@ -6776,7 +7423,7 @@ var CartesiaTTS = class _CartesiaTTS {
6776
7423
  }
6777
7424
  this.consecutiveErrors = 0;
6778
7425
  this.newContext();
6779
- if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(".", false));
7426
+ if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame("Ok.", false));
6780
7427
  }, _CartesiaTTS.CB_PROBE_MS);
6781
7428
  this.probeTimer.unref?.();
6782
7429
  }