larkin 0.4.9 → 0.4.11

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.
@@ -82,11 +82,10 @@ Options:
82
82
  --base-url <url> custom 端点(provider=custom 时必填;也可覆盖 preset 默认端点)
83
83
  --model <id> 模型 ID;默认取 provider 预设。不同 runtime 的模型可用
84
84
  \`larkin model\` 查看 / 切换
85
- --tenant feishu|lark 授权二维码之前选择品牌。默认 feishu(accounts.feishu.cn /
86
- https://open.feishu.cn)。Lark 用 --tenant lark
87
- (accounts.larksuite.com / https://open.larksuite.com)。
88
- 交互式未指定时会先询问。飞书与 Lark 是不同平台,
89
- Lark 租户不得使用 feishu.cn 主机。
85
+ --from-cli-profile <name> 可选:复用已有官方 lark-cli profile(需 LARKIN_SETUP_APP_SECRET)
86
+ --tenant feishu|lark 授权二维码之前选择品牌。默认 feishu(扫码 /page/launcher)。
87
+ Lark 用 --tenant lark:扫码 /page/cli,完成后凭证回传,
88
+ 不要打开 /page/launcher。交互式未指定时会先询问。
90
89
 
91
90
  setup handles browser authorization, permission grants, credential storage, Agent configuration,
92
91
  and target-only hot attach. Each Agent is identified by its bot App ID: selecting the same bot reuses
@@ -163,12 +162,13 @@ export async function main() {
163
162
  die("--tenant 只支持 feishu 或 lark");
164
163
  registerArgs.push("--tenant", tenant === "lark" ? "lark" : "feishu");
165
164
  }
166
- for (const name of ["--provider", "--api-key", "--base-url", "--model"]) {
165
+ for (const name of ["--provider", "--api-key", "--base-url", "--model", "--from-cli-profile"]) {
167
166
  const value = flag(name);
168
167
  if (value)
169
168
  registerArgs.push(name, value);
170
169
  }
171
170
  const result = await runForeground("bot-register", registerArgs);
171
+ delete process.env.LARKIN_SETUP_APP_SECRET;
172
172
  if (result.code !== 0)
173
173
  die("机器人授权或 Agent 配置未完成");
174
174
  let agentId;
@@ -0,0 +1,161 @@
1
+ function escapeXml(value) {
2
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
3
+ }
4
+ function extractTag(content, tagName) {
5
+ const match = new RegExp(`<${tagName}>([\\s\\S]*?)</${tagName}>`).exec(content);
6
+ return match ? match[1].trim() : null;
7
+ }
8
+ /**
9
+ * Pi subagents emit terminal <status> as Done or a display label
10
+ * (Error: ..., Aborted (...), Stopped, Wrapped up (turn limit)).
11
+ * Raw tokens completed/error/aborted/stopped/steered/turn-limit are also accepted.
12
+ */
13
+ const TERMINAL_STATUS_TOKENS = new Set([
14
+ "done",
15
+ "completed",
16
+ "error",
17
+ "aborted",
18
+ "stopped",
19
+ "steered",
20
+ "turn-limit",
21
+ "turn_limit",
22
+ ]);
23
+ export function isCanonicalTerminalPiSubagentStatus(status) {
24
+ const normalized = status.trim().toLowerCase();
25
+ if (!normalized)
26
+ return false;
27
+ if (TERMINAL_STATUS_TOKENS.has(normalized))
28
+ return true;
29
+ if (normalized.startsWith("error:") || normalized.startsWith("error "))
30
+ return true;
31
+ if (normalized.startsWith("aborted"))
32
+ return true;
33
+ if (normalized.startsWith("stopped"))
34
+ return true;
35
+ if (normalized.includes("turn limit") || normalized.includes("turn-limit") || normalized.includes("turn_limit")) {
36
+ return true;
37
+ }
38
+ if (normalized.includes("steered"))
39
+ return true;
40
+ return false;
41
+ }
42
+ function parseTaskNotificationBlock(block) {
43
+ const taskId = extractTag(block, "task-id");
44
+ const status = extractTag(block, "status");
45
+ const summary = extractTag(block, "summary");
46
+ const result = extractTag(block, "result");
47
+ if (!taskId || !status || !summary || !result)
48
+ return null;
49
+ if (!isCanonicalTerminalPiSubagentStatus(status))
50
+ return null;
51
+ return {
52
+ taskId,
53
+ toolUseId: extractTag(block, "tool-use-id") ?? undefined,
54
+ outputFile: extractTag(block, "output-file") ?? undefined,
55
+ status,
56
+ summary,
57
+ result,
58
+ };
59
+ }
60
+ function parseCanonicalNotificationContent(content) {
61
+ const blocks = [...content.matchAll(/<task-notification>([\s\S]*?)<\/task-notification>/g)]
62
+ .map((match) => match[1]);
63
+ if (blocks.length === 0)
64
+ return null;
65
+ const seen = new Set();
66
+ const notifications = [];
67
+ for (const block of blocks) {
68
+ const parsed = parseTaskNotificationBlock(block);
69
+ if (!parsed || seen.has(parsed.taskId))
70
+ continue;
71
+ seen.add(parsed.taskId);
72
+ notifications.push(parsed);
73
+ }
74
+ if (notifications.length === 0)
75
+ return null;
76
+ const taskIds = notifications.map((notification) => notification.taskId);
77
+ return { taskIds, notifications, content, key: taskIds.join("|") };
78
+ }
79
+ function collectCandidateContents(node, found = new Set()) {
80
+ const candidates = [];
81
+ const visit = (value) => {
82
+ if (value === null || value === undefined)
83
+ return;
84
+ if (Array.isArray(value)) {
85
+ for (const item of value)
86
+ visit(item);
87
+ return;
88
+ }
89
+ if (typeof value !== "object")
90
+ return;
91
+ const record = value;
92
+ if (record.customType === "subagent-notification" && typeof record.content === "string") {
93
+ if (!found.has(record.content)) {
94
+ found.add(record.content);
95
+ candidates.push(record.content);
96
+ }
97
+ }
98
+ else if (Array.isArray(record.content)) {
99
+ visit(record.content);
100
+ }
101
+ if (Array.isArray(record.messages))
102
+ visit(record.messages);
103
+ if (Array.isArray(record.parts))
104
+ visit(record.parts);
105
+ };
106
+ visit(node);
107
+ return candidates;
108
+ }
109
+ export function buildCanonicalPiSubagentNotificationContent(options) {
110
+ const status = options.status ?? "Done";
111
+ const summary = options.summary ?? `Agent ${JSON.stringify("Fixture")} completed`;
112
+ const result = options.result ?? "Fixture result.";
113
+ const totalTokens = options.totalTokens ?? 12;
114
+ const toolUses = options.toolUses ?? 1;
115
+ const durationMs = options.durationMs ?? 34;
116
+ return [
117
+ "<task-notification>",
118
+ `<task-id>${escapeXml(options.taskId)}</task-id>`,
119
+ options.toolUseId ? `<tool-use-id>${escapeXml(options.toolUseId)}</tool-use-id>` : null,
120
+ options.outputFile ? `<output-file>${escapeXml(options.outputFile)}</output-file>` : null,
121
+ `<status>${escapeXml(status)}</status>`,
122
+ `<summary>${escapeXml(summary)}</summary>`,
123
+ `<result>${escapeXml(result)}</result>`,
124
+ `<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${toolUses}</tool_uses><duration_ms>${durationMs}</duration_ms></usage>`,
125
+ "</task-notification>",
126
+ ].filter(Boolean).join("\n");
127
+ }
128
+ export function buildCanonicalPiSubagentAssistantMessage(options) {
129
+ return {
130
+ role: "assistant",
131
+ content: [{
132
+ type: "custom",
133
+ customType: "subagent-notification",
134
+ content: buildCanonicalPiSubagentNotificationContent(options),
135
+ }],
136
+ };
137
+ }
138
+ export function extractCanonicalPiSubagentNotification(messages) {
139
+ const seen = new Set();
140
+ const notifications = [];
141
+ const contents = [];
142
+ for (const content of collectCandidateContents(messages)) {
143
+ const parsed = parseCanonicalNotificationContent(content);
144
+ if (!parsed)
145
+ continue;
146
+ contents.push(parsed.content);
147
+ for (const notification of parsed.notifications) {
148
+ if (seen.has(notification.taskId))
149
+ continue;
150
+ seen.add(notification.taskId);
151
+ notifications.push(notification);
152
+ }
153
+ }
154
+ if (notifications.length === 0)
155
+ return null;
156
+ const taskIds = notifications.map((notification) => notification.taskId);
157
+ return { taskIds, notifications, content: contents.join("\n"), key: taskIds.join("|") };
158
+ }
159
+ export function extractCanonicalPiSubagentCompletionKeyFromMessages(messages) {
160
+ return extractCanonicalPiSubagentNotification(messages)?.key ?? null;
161
+ }
@@ -10,6 +10,7 @@ import { BUNDLED_PI_VERSION, piAgentDirectory } from "./pi-provider-config.mjs";
10
10
  import { recordPiRuntimeArtifactProvenance } from "./pi-artifact-provenance.mjs";
11
11
  import { traceProcessBoundary } from "../platform/process-boundary-trace.mjs";
12
12
  import { resolvePiSubagentExtensionArg } from "./pi-subagent-injection.mjs";
13
+ import { extractCanonicalPiSubagentCompletionKeyFromMessages } from "./pi-subagents-notification.mjs";
13
14
  import { resolvePiBashTimeoutExtensionArg } from "./pi-bash-timeout-injection.mjs";
14
15
  import { classifyRuntimePrerequisite, probeNativeRuntimeReadiness, providerAuthenticationFailureReadiness, RuntimePrerequisiteError, } from "./runtime-readiness.mjs";
15
16
  import { buildStrictProviderErrorInput, classifyStrictProviderError, INTERNAL_CONTEXT_WINDOW_PROJECTION_REASON, } from "./provider-error-classifier.mjs";
@@ -451,6 +452,8 @@ class PiSession extends EventSession {
451
452
  observedSubmitEpochs = new Set();
452
453
  observedAcceptedEpochs = new Set();
453
454
  observedCompletedEpochs = new Set();
455
+ observedBackgroundCompletionKeys = new Set();
456
+ pendingUnownedCompletionKeys = new Set();
454
457
  observedAgentEndEpochs = new Set();
455
458
  firstOutputObserved = false;
456
459
  toolCallOpen = false;
@@ -551,6 +554,8 @@ class PiSession extends EventSession {
551
554
  this.observedSubmitEpochs.clear();
552
555
  this.observedAcceptedEpochs.clear();
553
556
  this.observedCompletedEpochs.clear();
557
+ this.observedBackgroundCompletionKeys.clear();
558
+ this.pendingUnownedCompletionKeys.clear();
554
559
  this.observedAgentEndEpochs.clear();
555
560
  this.activeEpoch = null;
556
561
  this.settleArmedEpoch = null;
@@ -600,11 +605,25 @@ class PiSession extends EventSession {
600
605
  this.observedCompletedEpochs.add(this.activeEpoch);
601
606
  this.emitObservation("completed");
602
607
  }
608
+ const completionNotificationKey = extractCanonicalPiSubagentCompletionKeyFromMessages(event.messages);
609
+ if (completionNotificationKey
610
+ && this.activeEpoch === null
611
+ && !this.observedBackgroundCompletionKeys.has(completionNotificationKey)) {
612
+ // agent_end can still have an active Pi session. Prompting before
613
+ // unowned agent_settled is rejected as "Agent is already processing".
614
+ this.pendingUnownedCompletionKeys.add(completionNotificationKey);
615
+ }
603
616
  }
604
617
  else if (event?.type === "agent_settled") {
605
618
  const epoch = this.activeEpoch;
606
- if (epoch === null || this.settleArmedEpoch !== epoch)
619
+ if (epoch === null) {
620
+ this.flushPendingUnownedCompletions();
621
+ return;
622
+ }
623
+ if (this.settleArmedEpoch !== epoch) {
624
+ this.flushPendingUnownedCompletions();
607
625
  return;
626
+ }
608
627
  this.activeEpoch = null;
609
628
  this.settleArmedEpoch = null;
610
629
  const error = this.finalAssistantError;
@@ -645,6 +664,7 @@ class PiSession extends EventSession {
645
664
  this.observedAcceptedEpochs.delete(epoch);
646
665
  this.observedCompletedEpochs.delete(epoch);
647
666
  this.observedAgentEndEpochs.delete(epoch);
667
+ this.flushPendingUnownedCompletions();
648
668
  }
649
669
  else if (event?.type === "tool_execution_start") {
650
670
  if (!this.firstOutputObserved) {
@@ -672,6 +692,15 @@ class PiSession extends EventSession {
672
692
  this.emit({ type: "activity", activity: kind, text: String(event.assistantMessageEvent.delta) });
673
693
  }
674
694
  }
695
+ flushPendingUnownedCompletions() {
696
+ for (const completionKey of this.pendingUnownedCompletionKeys) {
697
+ if (this.observedBackgroundCompletionKeys.has(completionKey))
698
+ continue;
699
+ this.observedBackgroundCompletionKeys.add(completionKey);
700
+ this.emitObservation("completed", { completionKey });
701
+ }
702
+ this.pendingUnownedCompletionKeys.clear();
703
+ }
675
704
  emitObservation(phase, fields = {}) {
676
705
  const inputId = this.oldestOwnedInput();
677
706
  const observation = { type: "runtime-observation", runtime: "pi",
@@ -681,6 +710,8 @@ class PiSession extends EventSession {
681
710
  Object.defineProperty(observation, "sessionId", { value: this.sessionId, enumerable: false });
682
711
  if (inputId)
683
712
  Object.defineProperty(observation, "inputId", { value: inputId, enumerable: false });
713
+ if (fields.completionKey)
714
+ Object.defineProperty(observation, "completionKey", { value: fields.completionKey, enumerable: false });
684
715
  this.emit(observation);
685
716
  }
686
717
  oldestOwnedInput() {
@@ -39,6 +39,7 @@ class RuntimeSessionRecoveryError extends Error {
39
39
  }
40
40
  exports.RuntimeSessionRecoveryError = RuntimeSessionRecoveryError;
41
41
  const MAX_DELIVERIES = 2048;
42
+ const BACKGROUND_COMPLETION_IMMEDIATE_RETRY_LIMIT = 5;
42
43
  const now = () => new Date().toISOString();
43
44
  const isActiveDelivery = (status) => ["pending", "submitting", "accepted"].includes(status);
44
45
  const REPLAY_FAILURE_CODES = new Set([
@@ -521,6 +522,12 @@ function createRuntimeHost(options) {
521
522
  queueMicrotask(() => { void retryPending(agent); });
522
523
  }
523
524
  queueMicrotask(() => { void scanAndPromoteAcceptedInboxUpdates(agent); });
525
+ // A completion queued during this submit has no turn-end if prompt
526
+ // rejected/threw before turn-start. Drain now that submitting is clear.
527
+ if (!agent.busy && !agent.turnInProgress && agent.backgroundCompletionQueue.length > 0
528
+ && !agent.backgroundCompletionRetryTimer) {
529
+ scheduleBackgroundCompletionDrain(agent);
530
+ }
524
531
  }
525
532
  };
526
533
  const retryPending = async (agent) => {
@@ -599,6 +606,210 @@ function createRuntimeHost(options) {
599
606
  agent.retryAfterSubmit = true;
600
607
  await retryPending(agent);
601
608
  };
609
+ const clearBackgroundCompletionRetryTimer = (agent) => {
610
+ if (!agent.backgroundCompletionRetryTimer)
611
+ return;
612
+ clearTimeout(agent.backgroundCompletionRetryTimer);
613
+ agent.backgroundCompletionRetryTimer = null;
614
+ };
615
+ const scheduleBackgroundCompletionDrain = (agent, delayMs = 0) => {
616
+ if (agent.stopped)
617
+ return;
618
+ if (delayMs > 0) {
619
+ if (agent.backgroundCompletionRetryTimer)
620
+ return;
621
+ agent.backgroundCompletionRetryTimer = setTimeout(() => {
622
+ agent.backgroundCompletionRetryTimer = null;
623
+ if (agent.stopped)
624
+ return;
625
+ void drainBackgroundCompletionQueue(agent);
626
+ }, delayMs);
627
+ agent.backgroundCompletionRetryTimer.unref?.();
628
+ return;
629
+ }
630
+ clearBackgroundCompletionRetryTimer(agent);
631
+ queueMicrotask(() => { void drainBackgroundCompletionQueue(agent); });
632
+ };
633
+ const dropBackgroundCompletion = (agent, completionKey) => {
634
+ if (!completionKey)
635
+ return;
636
+ if (agent.backgroundCompletionQueue[0] === completionKey)
637
+ agent.backgroundCompletionQueue.shift();
638
+ else {
639
+ const index = agent.backgroundCompletionQueue.indexOf(completionKey);
640
+ if (index >= 0)
641
+ agent.backgroundCompletionQueue.splice(index, 1);
642
+ }
643
+ agent.backgroundCompletionKeys.add(completionKey);
644
+ if (agent.backgroundCompletionInFlight === completionKey) {
645
+ agent.backgroundCompletionInFlight = null;
646
+ agent.backgroundCompletionWakeInputId = null;
647
+ }
648
+ agent.backgroundCompletionRejectStreak = 0;
649
+ clearBackgroundCompletionRetryTimer(agent);
650
+ };
651
+ const scheduleBackgroundCompletionRetry = (agent) => {
652
+ const streak = agent.backgroundCompletionRejectStreak;
653
+ if (streak < BACKGROUND_COMPLETION_IMMEDIATE_RETRY_LIMIT) {
654
+ scheduleBackgroundCompletionDrain(agent);
655
+ return;
656
+ }
657
+ const delayedAttempt = streak - BACKGROUND_COMPLETION_IMMEDIATE_RETRY_LIMIT;
658
+ if (delayedAttempt >= retryPolicy.maxAttempts) {
659
+ const exhausted = agent.backgroundCompletionQueue[0];
660
+ if (exhausted) {
661
+ log("background completion wake retries exhausted", exhausted);
662
+ dropBackgroundCompletion(agent, exhausted);
663
+ }
664
+ if (agent.backgroundCompletionQueue.length > 0)
665
+ scheduleBackgroundCompletionDrain(agent);
666
+ return;
667
+ }
668
+ const delay = Math.min(retryPolicy.maxDelayMs, retryPolicy.baseDelayMs * 2 ** delayedAttempt);
669
+ scheduleBackgroundCompletionDrain(agent, delay);
670
+ };
671
+ const isPersistentBackgroundWakeFailure = (event) => {
672
+ if (!event)
673
+ return false;
674
+ const category = event.errorCategory;
675
+ if (category === "auth" || category === "billing" || category === "quota")
676
+ return true;
677
+ return category === "provider" && event.retryable === false;
678
+ };
679
+ const failBackgroundCompletionWake = (agent, event) => {
680
+ const completionKey = agent.backgroundCompletionInFlight ?? agent.backgroundCompletionQueue[0];
681
+ if (!completionKey)
682
+ return;
683
+ rearmBackgroundCompletion(agent, completionKey);
684
+ if (isPersistentBackgroundWakeFailure(event)) {
685
+ log("background completion wake dropped", event?.errorCategory ?? "persistent");
686
+ dropBackgroundCompletion(agent, completionKey);
687
+ if (agent.backgroundCompletionQueue.length > 0)
688
+ scheduleBackgroundCompletionDrain(agent);
689
+ return;
690
+ }
691
+ agent.backgroundCompletionRejectStreak += 1;
692
+ scheduleBackgroundCompletionRetry(agent);
693
+ };
694
+ const rearmBackgroundCompletion = (agent, completionKey = agent.backgroundCompletionInFlight) => {
695
+ if (!completionKey)
696
+ return;
697
+ agent.backgroundCompletionKeys.delete(completionKey);
698
+ if (agent.backgroundCompletionInFlight === completionKey) {
699
+ agent.backgroundCompletionInFlight = null;
700
+ agent.backgroundCompletionWakeInputId = null;
701
+ }
702
+ if (!agent.backgroundCompletionQueue.includes(completionKey)) {
703
+ agent.backgroundCompletionQueue.unshift(completionKey);
704
+ }
705
+ };
706
+ const commitBackgroundCompletion = (agent) => {
707
+ const completionKey = agent.backgroundCompletionInFlight;
708
+ if (!completionKey)
709
+ return;
710
+ if (agent.backgroundCompletionQueue[0] === completionKey)
711
+ agent.backgroundCompletionQueue.shift();
712
+ agent.backgroundCompletionInFlight = null;
713
+ agent.backgroundCompletionWakeInputId = null;
714
+ agent.backgroundCompletionRejectStreak = 0;
715
+ clearBackgroundCompletionRetryTimer(agent);
716
+ };
717
+ const wakeOnBackgroundCompletion = async (agent) => {
718
+ // Caller already reserved submitting/busy so a concurrent idle drain cannot
719
+ // dequeue another head while ensureSession/prompt yields.
720
+ try {
721
+ const session = await ensureSession(agent);
722
+ if (agent.session !== session || agent.stopped)
723
+ return { status: "dropped" };
724
+ const input = options.promptBuilder.buildRuntimeInput("wake", node_crypto_1.default.randomUUID(), { wakeReason: "background subagent completed" });
725
+ const result = await session.prompt(input);
726
+ if (agent.session !== session || agent.stopped)
727
+ return { status: "dropped" };
728
+ if (result.status === "accepted") {
729
+ markSessionStable(agent, session);
730
+ return { status: "accepted", inputId: input.inputId };
731
+ }
732
+ agent.busy = false;
733
+ return { status: "retry" };
734
+ }
735
+ catch (error) {
736
+ agent.busy = false;
737
+ log("background completion wake failed", error instanceof Error ? error.message : String(error));
738
+ return { status: "retry" };
739
+ }
740
+ };
741
+ const drainBackgroundCompletionQueue = async (agent) => {
742
+ if (agent.stopped || agent.busy || agent.turnInProgress || agent.submitting)
743
+ return;
744
+ if (agent.backgroundCompletionInFlight || !agent.backgroundCompletionQueue.length)
745
+ return;
746
+ clearBackgroundCompletionRetryTimer(agent);
747
+ const proactive = agent.piProactiveCompaction
748
+ && agent.piProactiveCompactionGeneration === agent.generation
749
+ && agent.piProactiveCompactionSession === agent.session
750
+ ? agent.piProactiveCompaction : null;
751
+ if (proactive && await proactive === "failed")
752
+ return;
753
+ if (agent.adapter.id === "pi" && agent.piProactiveCompactionFailedGeneration === agent.generation)
754
+ return;
755
+ if (agent.adapter.id === "pi" && !agent.busy && !agent.turnInProgress && agent.session) {
756
+ const idleGate = proactivelyCompactPiAtIdle(agent, agent.session);
757
+ if (idleGate && await idleGate === "failed")
758
+ return;
759
+ }
760
+ if (agent.stopped || agent.busy || agent.turnInProgress || agent.submitting)
761
+ return;
762
+ if (agent.backgroundCompletionInFlight || !agent.backgroundCompletionQueue.length)
763
+ return;
764
+ agent.submitting = true;
765
+ agent.busy = true;
766
+ const completionKey = agent.backgroundCompletionQueue[0];
767
+ try {
768
+ const outcome = await wakeOnBackgroundCompletion(agent);
769
+ if (outcome.status === "accepted") {
770
+ agent.backgroundCompletionKeys.add(completionKey);
771
+ agent.backgroundCompletionInFlight = completionKey;
772
+ agent.backgroundCompletionWakeInputId = outcome.inputId;
773
+ // Keep the reject streak until the wake turn actually succeeds. An accepted
774
+ // prompt that later terminal-fails must not reset the bounded retry budget.
775
+ clearBackgroundCompletionRetryTimer(agent);
776
+ }
777
+ else if (outcome.status === "dropped") {
778
+ if (agent.stopped) {
779
+ if (agent.backgroundCompletionQueue[0] === completionKey)
780
+ agent.backgroundCompletionQueue.shift();
781
+ agent.backgroundCompletionKeys.delete(completionKey);
782
+ clearBackgroundCompletionRetryTimer(agent);
783
+ }
784
+ else {
785
+ rearmBackgroundCompletion(agent, completionKey);
786
+ agent.busy = false;
787
+ }
788
+ }
789
+ else {
790
+ agent.busy = false;
791
+ agent.backgroundCompletionRejectStreak += 1;
792
+ // Leave the item queued+deduped. Immediate microtask retries cover a
793
+ // brief reject; after that a bounded timer still drains because Pi will
794
+ // not re-emit the same completion.
795
+ scheduleBackgroundCompletionRetry(agent);
796
+ }
797
+ }
798
+ finally {
799
+ agent.submitting = false;
800
+ }
801
+ };
802
+ const noteBackgroundCompletion = (agent, completionKey) => {
803
+ if (agent.backgroundCompletionKeys.has(completionKey))
804
+ return;
805
+ agent.backgroundCompletionKeys.add(completionKey);
806
+ if (!agent.backgroundCompletionQueue.includes(completionKey)) {
807
+ agent.backgroundCompletionQueue.push(completionKey);
808
+ }
809
+ if (!agent.busy && !agent.turnInProgress && !agent.submitting && !agent.backgroundCompletionInFlight) {
810
+ scheduleBackgroundCompletionDrain(agent);
811
+ }
812
+ };
602
813
  const scheduleRecreate = (agent, reason) => {
603
814
  if (agent.stopped || agent.retryTimer || agent.starting || agent.session)
604
815
  return;
@@ -621,6 +832,8 @@ function createRuntimeHost(options) {
621
832
  if (proactive && await proactive === "failed")
622
833
  return;
623
834
  await retryPending(agent);
835
+ if (agent.backgroundCompletionQueue.length > 0)
836
+ scheduleBackgroundCompletionDrain(agent);
624
837
  }).catch((error) => {
625
838
  const readiness = error instanceof runtime_readiness_js_1.RuntimePrerequisiteError ? error.readiness
626
839
  : (0, runtime_readiness_js_1.classifyRuntimePrerequisite)(agent.adapter.id, error);
@@ -643,6 +856,7 @@ function createRuntimeHost(options) {
643
856
  agent.busy = false;
644
857
  agent.submitting = false;
645
858
  agent.generation += 1;
859
+ rearmBackgroundCompletion(agent);
646
860
  agent.recreateReason = reason;
647
861
  if (agent.stabilityTimer)
648
862
  clearTimeout(agent.stabilityTimer);
@@ -663,6 +877,7 @@ function createRuntimeHost(options) {
663
877
  agent.busy = false;
664
878
  agent.submitting = false;
665
879
  agent.generation += 1;
880
+ rearmBackgroundCompletion(agent);
666
881
  if (agent.stabilityTimer)
667
882
  clearTimeout(agent.stabilityTimer);
668
883
  agent.stabilityTimer = null;
@@ -919,6 +1134,15 @@ function createRuntimeHost(options) {
919
1134
  agent.busy = false;
920
1135
  emit({ type: "activity", agentId: agent.config.agentId, activity: "idle", activityKind: "idle", detailKind: "turn_ended" });
921
1136
  reconcileAcceptedAtTurnEnd(agent);
1137
+ if (agent.backgroundCompletionInFlight) {
1138
+ if (agent.turnHadFailure)
1139
+ failBackgroundCompletionWake(agent);
1140
+ else
1141
+ commitBackgroundCompletion(agent);
1142
+ }
1143
+ if (agent.backgroundCompletionQueue.length > 0 && !agent.backgroundCompletionRetryTimer) {
1144
+ scheduleBackgroundCompletionDrain(agent);
1145
+ }
922
1146
  telemetry?.runtimeEvent(agent.config.agentId, event);
923
1147
  if (recoveredAuthentication) {
924
1148
  agent.authFailureActive = false;
@@ -938,12 +1162,24 @@ function createRuntimeHost(options) {
938
1162
  });
939
1163
  queueMicrotask(() => { void scanAndPromoteAcceptedInboxUpdates(agent); });
940
1164
  }
1165
+ else if (event.type === "runtime-observation") {
1166
+ if (event.phase === "completed" && typeof event.completionKey === "string") {
1167
+ noteBackgroundCompletion(agent, event.completionKey);
1168
+ }
1169
+ }
941
1170
  else if (event.type === "activity") {
942
1171
  if (agent.turnInProgress && event.activity !== "internal")
943
1172
  agent.turnHadAuthenticatedOutput = true;
944
1173
  emit({ type: "activity", agentId: agent.config.agentId, activity: event.activity, activityKind: event.activity });
945
1174
  }
946
1175
  else if (event.type === "input-error") {
1176
+ if (agent.backgroundCompletionInFlight
1177
+ && (!event.inputId || !agent.backgroundCompletionWakeInputId
1178
+ || event.inputId === agent.backgroundCompletionWakeInputId)
1179
+ && event.willRetry !== true) {
1180
+ agent.turnHadFailure = true;
1181
+ failBackgroundCompletionWake(agent, event);
1182
+ }
947
1183
  const record = event.inputId
948
1184
  ? agent.records.get(event.inputId) ?? [...agent.records.values()].find((candidate) => candidate.input.inputId === event.inputId)
949
1185
  : undefined;
@@ -1270,10 +1506,13 @@ function createRuntimeHost(options) {
1270
1506
  clearTimeout(previous.retryTimer);
1271
1507
  if (previous.stabilityTimer)
1272
1508
  clearTimeout(previous.stabilityTimer);
1509
+ if (previous.backgroundCompletionRetryTimer)
1510
+ clearTimeout(previous.backgroundCompletionRetryTimer);
1273
1511
  const candidate = {
1274
1512
  ...previous, config, adapter, session, launchId: node_crypto_1.default.randomUUID(), busy: false, submitting: false,
1275
1513
  starting: null, retryAfterSubmit: false, generation: 0, poller: null, retryTimer: null,
1276
1514
  recreateAttempts: 0, stabilityTimer: null, recreateReason: null, stopped: false,
1515
+ backgroundCompletionRetryTimer: null,
1277
1516
  disabledReason: null, configurationRecovery: null,
1278
1517
  readiness: previous.authFailureActive ? previous.readiness : readiness,
1279
1518
  };
@@ -1290,6 +1529,11 @@ function createRuntimeHost(options) {
1290
1529
  if (!candidate.authFailureActive) {
1291
1530
  emit({ type: "agent-status", agentId: config.agentId, status: "active", readiness: candidate.readiness ?? readiness });
1292
1531
  }
1532
+ // Commit clears any inherited backoff timer; reschedule so a queued
1533
+ // completion still drains on the new session.
1534
+ if (candidate.backgroundCompletionQueue.length > 0) {
1535
+ scheduleBackgroundCompletionDrain(candidate);
1536
+ }
1293
1537
  await previous.session?.close("runtime candidate committed")
1294
1538
  .catch((error) => log("previous runtime close after candidate commit failed", String(error)));
1295
1539
  },
@@ -1622,7 +1866,10 @@ function createRuntimeHost(options) {
1622
1866
  piProactiveCompaction: null, piProactiveCompactionGeneration: null, piProactiveCompactionSession: null,
1623
1867
  piProactiveCompactionFailedGeneration: null, readiness: null,
1624
1868
  turnInProgress: false, turnHadFailure: false, turnHadAuthenticatedOutput: false, authFailureActive: false,
1625
- promotedInboxUpdateIds: new Set() };
1869
+ promotedInboxUpdateIds: new Set(),
1870
+ backgroundCompletionQueue: [], backgroundCompletionKeys: new Set(),
1871
+ backgroundCompletionInFlight: null, backgroundCompletionWakeInputId: null,
1872
+ backgroundCompletionRejectStreak: 0, backgroundCompletionRetryTimer: null };
1626
1873
  const startupConsumed = [];
1627
1874
  const startupQuarantined = [];
1628
1875
  for (const record of agent.records.values()) {
@@ -1773,6 +2020,8 @@ function createRuntimeHost(options) {
1773
2020
  clearTimeout(agent.retryTimer);
1774
2021
  if (agent.stabilityTimer)
1775
2022
  clearTimeout(agent.stabilityTimer);
2023
+ if (agent.backgroundCompletionRetryTimer)
2024
+ clearTimeout(agent.backgroundCompletionRetryTimer);
1776
2025
  if (agent.busy)
1777
2026
  await agent.session?.cancel(reason);
1778
2027
  await agent.session?.close(reason);