@linghun/tui 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/background-control-runtime.js +27 -26
  2. package/dist/capability-runtime.js +16 -15
  3. package/dist/{chunk-N7G2X2LK.js → chunk-33E6RDCY.js} +10 -10
  4. package/dist/{chunk-FTAEPAVY.js → chunk-3BPKJMIV.js} +2 -2
  5. package/dist/{chunk-WQMSRO4D.js → chunk-4E56DWEZ.js} +2 -2
  6. package/dist/chunk-4UER2AKI.js +636 -0
  7. package/dist/{chunk-WHFDDZHY.js → chunk-5CXN4JLE.js} +2 -2
  8. package/dist/{chunk-JDWXC2OQ.js → chunk-624A7BBJ.js} +3 -3
  9. package/dist/{chunk-3GCIXKCI.js → chunk-7TYK55JT.js} +4 -0
  10. package/dist/{chunk-JO5ANTMR.js → chunk-AZU7HXIT.js} +76 -29
  11. package/dist/{chunk-B4GQTZMT.js → chunk-CD54BQJ5.js} +1 -1
  12. package/dist/{chunk-ZZ2CNCXJ.js → chunk-CDVYF7RU.js} +1 -1
  13. package/dist/{chunk-ECFLCY5V.js → chunk-DYDTLRHB.js} +1 -1
  14. package/dist/{chunk-AKPXMCVF.js → chunk-HIIYRATQ.js} +1 -1
  15. package/dist/{chunk-JHGWGH2C.js → chunk-J6CYFWSW.js} +89 -7
  16. package/dist/{chunk-66BNE332.js → chunk-JJQETFCR.js} +1 -1
  17. package/dist/{chunk-RVY34HK4.js → chunk-KOULRCYJ.js} +4 -1
  18. package/dist/{chunk-QB4VWH7W.js → chunk-LIPZJNXS.js} +2 -2
  19. package/dist/{chunk-OF3K3CU5.js → chunk-MXPBYB54.js} +1 -1
  20. package/dist/{chunk-DNQN74CC.js → chunk-NL4M3V5D.js} +33 -0
  21. package/dist/{chunk-GKZYTPN2.js → chunk-O6MKVGVK.js} +1 -1
  22. package/dist/{chunk-LVIM2PA7.js → chunk-QG2FJ3NR.js} +1 -1
  23. package/dist/{chunk-2FVEKTKN.js → chunk-QJTET7EZ.js} +1 -1
  24. package/dist/{chunk-4WHZ6JKD.js → chunk-RFSSKGCE.js} +178 -45
  25. package/dist/{chunk-OEYW3JE3.js → chunk-SILJJAK2.js} +1 -1
  26. package/dist/{chunk-THZBFYAH.js → chunk-UN53MBAR.js} +1 -1
  27. package/dist/{chunk-TYF4GJR3.js → chunk-VAGL7CNR.js} +1 -1
  28. package/dist/{chunk-D7TCSOAZ.js → chunk-WOBNOBB5.js} +1 -1
  29. package/dist/{chunk-JQCRCDOC.js → chunk-XWRHWM4H.js} +9 -9
  30. package/dist/{chunk-GMUCSNUD.js → chunk-Y5ZV47XC.js} +2 -2
  31. package/dist/command-panel-runtime.js +15 -14
  32. package/dist/compact-cache-command-runtime.js +27 -26
  33. package/dist/compact-preflight-runtime.js +5 -5
  34. package/dist/connector-runtime.js +17 -16
  35. package/dist/details-status-runtime.js +15 -14
  36. package/dist/evidence-runtime.js +2 -2
  37. package/dist/extension-command-runtime.js +3 -3
  38. package/dist/extension-slash-runtime.js +16 -15
  39. package/dist/failure-learning-command-runtime.js +16 -15
  40. package/dist/git-command-runtime.js +16 -15
  41. package/dist/headless-bench-runtime.d.ts +108 -0
  42. package/dist/headless-bench-runtime.d.ts.map +1 -0
  43. package/dist/headless-bench-runtime.js +30 -0
  44. package/dist/index.d.ts +31 -0
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +492 -45
  47. package/dist/job-agent-command-runtime.d.ts +2 -1
  48. package/dist/job-agent-command-runtime.d.ts.map +1 -1
  49. package/dist/job-agent-command-runtime.js +15 -14
  50. package/dist/job-runtime.js +4 -4
  51. package/dist/mcp-index-command-runtime.js +3 -3
  52. package/dist/mcp-index-runtime.js +18 -17
  53. package/dist/mcp-stdio-runtime.js +2 -2
  54. package/dist/memory-command-runtime.js +16 -15
  55. package/dist/meta-scheduler-runtime.d.ts +10 -0
  56. package/dist/meta-scheduler-runtime.d.ts.map +1 -1
  57. package/dist/meta-scheduler-runtime.js +2 -1
  58. package/dist/model-command-runtime.js +16 -15
  59. package/dist/model-prompt-runtime.d.ts.map +1 -1
  60. package/dist/model-prompt-runtime.js +1 -1
  61. package/dist/model-stream-runtime.d.ts +4 -0
  62. package/dist/model-stream-runtime.d.ts.map +1 -1
  63. package/dist/model-stream-runtime.js +27 -26
  64. package/dist/model-tool-runtime.d.ts +2 -2
  65. package/dist/model-tool-runtime.js +27 -26
  66. package/dist/permission-approval-runtime.js +27 -26
  67. package/dist/process-command-runtime.d.ts.map +1 -1
  68. package/dist/process-command-runtime.js +2 -2
  69. package/dist/process-guard.d.ts +3 -0
  70. package/dist/process-guard.d.ts.map +1 -1
  71. package/dist/process-guard.js +1 -1
  72. package/dist/remote-command-runtime.js +16 -15
  73. package/dist/runner-runtime.js +3 -3
  74. package/dist/shell/components/ProductBlock.js +2 -2
  75. package/dist/shell/components/ShellApp.js +6 -6
  76. package/dist/shell/ink-renderer.js +6 -6
  77. package/dist/shell/view-model.js +2 -2
  78. package/dist/slash-command-runtime.js +27 -26
  79. package/dist/tui-agent-job-runtime.js +5 -5
  80. package/dist/tui-data-types.d.ts +10 -0
  81. package/dist/tui-data-types.d.ts.map +1 -1
  82. package/dist/tui-details-runtime.js +5 -5
  83. package/dist/tui-output-surface.js +3 -3
  84. package/dist/verification-command-runtime.js +6 -6
  85. package/dist/workflow-command-runtime.d.ts.map +1 -1
  86. package/dist/workflow-command-runtime.js +15 -14
  87. package/package.json +5 -5
  88. package/dist/{chunk-M4AIK2SP.js → chunk-KJVHLTQR.js} +3 -3
  89. package/dist/{chunk-64MJP6FK.js → chunk-L2GU3PWU.js} +3 -3
package/dist/index.js CHANGED
@@ -13,10 +13,10 @@ import {
13
13
  } from "./chunk-O7EFPSMC.js";
14
14
  import {
15
15
  handleAppsCommand
16
- } from "./chunk-JDWXC2OQ.js";
16
+ } from "./chunk-624A7BBJ.js";
17
17
  import {
18
18
  handleCapabilitiesCommand
19
- } from "./chunk-QB4VWH7W.js";
19
+ } from "./chunk-LIPZJNXS.js";
20
20
  import {
21
21
  __testFormatStartAgentDidNotStartMessage,
22
22
  __testParseRunWorkflowToolInput,
@@ -28,9 +28,11 @@ import {
28
28
  __testUpdateCommandPanelSelection,
29
29
  addAllowRuleForTest,
30
30
  cancelPendingInteraction,
31
+ clearRequestActivity,
31
32
  configureSlashCommandRuntime,
32
33
  confirmPendingInteraction,
33
34
  cycleMode,
35
+ evaluateAggregatedFinalAnswerGate,
34
36
  executePermissionApprove,
35
37
  executePermissionDeny,
36
38
  getWorkspaceTrustCommandGuard,
@@ -79,7 +81,7 @@ import {
79
81
  toggleCommandPanelSelection,
80
82
  updateCommandPanelSelection,
81
83
  writeWorkspaceTrustStartupNotice
82
- } from "./chunk-4WHZ6JKD.js";
84
+ } from "./chunk-RFSSKGCE.js";
83
85
  import {
84
86
  createCommandBlock,
85
87
  createUserTextBlock
@@ -108,7 +110,7 @@ import {
108
110
  sendRemoteEventReal,
109
111
  validateRemoteInboundEnvelope,
110
112
  validateRemotePairingEnvelope
111
- } from "./chunk-66BNE332.js";
113
+ } from "./chunk-JJQETFCR.js";
112
114
  import "./chunk-UT5BOJKU.js";
113
115
  import "./chunk-RM5JDRCW.js";
114
116
  import {
@@ -118,15 +120,15 @@ import {
118
120
  handleMemoryCommand,
119
121
  resumeSessionWithHandoff,
120
122
  runAutoLearningOnTurnEnd
121
- } from "./chunk-GMUCSNUD.js";
123
+ } from "./chunk-Y5ZV47XC.js";
122
124
  import "./chunk-VTQL23UM.js";
123
- import "./chunk-DNQN74CC.js";
125
+ import "./chunk-NL4M3V5D.js";
124
126
  import "./chunk-KQJHVJZB.js";
125
127
  import {
126
128
  handleModelCommand,
127
129
  handleModelSetupInput,
128
130
  startModelSetup
129
- } from "./chunk-D7TCSOAZ.js";
131
+ } from "./chunk-WOBNOBB5.js";
130
132
  import "./chunk-NMNOAFFT.js";
131
133
  import {
132
134
  executeExtraTool,
@@ -137,13 +139,13 @@ import {
137
139
  handleIndexCommand,
138
140
  handleMcpCommand,
139
141
  refreshIndexStatus
140
- } from "./chunk-JQCRCDOC.js";
142
+ } from "./chunk-XWRHWM4H.js";
141
143
  import "./chunk-W6NU7ZAV.js";
142
- import "./chunk-OF3K3CU5.js";
143
- import "./chunk-SRZIA6B4.js";
144
144
  import {
145
145
  isPotentiallyMutatingMcpTool
146
- } from "./chunk-OEYW3JE3.js";
146
+ } from "./chunk-SILJJAK2.js";
147
+ import "./chunk-MXPBYB54.js";
148
+ import "./chunk-SRZIA6B4.js";
147
149
  import {
148
150
  formatProviderGuardSummary,
149
151
  formatRunnerGuardSummary,
@@ -159,16 +161,16 @@ import {
159
161
  handleCheckpointCommand,
160
162
  handleGitCommand,
161
163
  handleWorktreeCommand
162
- } from "./chunk-GKZYTPN2.js";
164
+ } from "./chunk-O6MKVGVK.js";
163
165
  import "./chunk-GTP2KPLY.js";
164
166
  import "./chunk-7ZE5JFAZ.js";
165
167
  import {
166
168
  handlePluginsCommand,
167
169
  handleSkillsCommand
168
- } from "./chunk-FTAEPAVY.js";
170
+ } from "./chunk-3BPKJMIV.js";
169
171
  import {
170
172
  handleFailuresCommand
171
- } from "./chunk-2FVEKTKN.js";
173
+ } from "./chunk-QJTET7EZ.js";
172
174
  import "./chunk-X7A22JHP.js";
173
175
  import {
174
176
  startFeishuLongConnection
@@ -186,7 +188,7 @@ import {
186
188
  processRemoteBindCommand,
187
189
  wecomBridgeAdapter
188
190
  } from "./chunk-CFXB5SE5.js";
189
- import "./chunk-WHFDDZHY.js";
191
+ import "./chunk-5CXN4JLE.js";
190
192
  import {
191
193
  breakCacheTestHooks
192
194
  } from "./chunk-WVAQYKGL.js";
@@ -219,19 +221,23 @@ import {
219
221
  showCommandPanel,
220
222
  t,
221
223
  writeStatus
222
- } from "./chunk-JO5ANTMR.js";
223
- import "./chunk-WQMSRO4D.js";
224
+ } from "./chunk-AZU7HXIT.js";
224
225
  import "./chunk-V7GDHHBL.js";
225
226
  import "./chunk-GCMH5P4W.js";
226
- import "./chunk-THZBFYAH.js";
227
- import {
228
- ShellBlockOutput
229
- } from "./chunk-ECFLCY5V.js";
230
227
  import {
231
228
  addAllowRule,
232
229
  decidePermission,
233
230
  loadPermissionState
234
231
  } from "./chunk-DDXS2RDZ.js";
232
+ import "./chunk-4E56DWEZ.js";
233
+ import {
234
+ cleanupCompletedBackgroundTasks,
235
+ getDurableJobPaths
236
+ } from "./chunk-CDVYF7RU.js";
237
+ import "./chunk-UN53MBAR.js";
238
+ import {
239
+ ShellBlockOutput
240
+ } from "./chunk-DYDTLRHB.js";
235
241
  import {
236
242
  formatCatalogHelp,
237
243
  formatSlashDiscovery,
@@ -242,14 +248,10 @@ import {
242
248
  import {
243
249
  createTerminalReadinessView
244
250
  } from "./chunk-PBIPV4LD.js";
245
- import {
246
- cleanupCompletedBackgroundTasks,
247
- getDurableJobPaths
248
- } from "./chunk-ZZ2CNCXJ.js";
249
251
  import {
250
252
  getDurableJobsRoot
251
- } from "./chunk-TYF4GJR3.js";
252
- import "./chunk-B4GQTZMT.js";
253
+ } from "./chunk-VAGL7CNR.js";
254
+ import "./chunk-CD54BQJ5.js";
253
255
  import "./chunk-L4XXW6Y5.js";
254
256
  import {
255
257
  classifyProviderVerificationLevel,
@@ -269,16 +271,26 @@ import {
269
271
  formatRuntimePathMarker,
270
272
  formatStartupPathMarker
271
273
  } from "./chunk-VIBENDE3.js";
274
+ import {
275
+ collectHeadlessArtifactChecklist,
276
+ createHeadlessBenchInitialPrompt,
277
+ createHeadlessBenchRepairPrompt,
278
+ resolveHeadlessBenchConfig,
279
+ runHeadlessEnvironmentPreflight,
280
+ validateHeadlessBenchCompletion
281
+ } from "./chunk-4UER2AKI.js";
272
282
  import {
273
283
  formatConfigOverview,
274
284
  formatFeaturePolicy,
275
285
  validateExtensionContributionExecution
276
- } from "./chunk-LVIM2PA7.js";
277
- import "./chunk-RVY34HK4.js";
286
+ } from "./chunk-QG2FJ3NR.js";
287
+ import "./chunk-KOULRCYJ.js";
278
288
  import {
289
+ getTrackedProcessSnapshot,
279
290
  installProcessGuardExitHandlers,
280
- requestTrackedProcessStop
281
- } from "./chunk-JHGWGH2C.js";
291
+ requestTrackedProcessStop,
292
+ trackChildProcess
293
+ } from "./chunk-J6CYFWSW.js";
282
294
  import "./chunk-E72U7WW7.js";
283
295
  import {
284
296
  buildConfigPanelActions,
@@ -286,7 +298,7 @@ import {
286
298
  createShellViewModel,
287
299
  mapPendingApprovalToPermission,
288
300
  mapRequestActivityToView
289
- } from "./chunk-AKPXMCVF.js";
301
+ } from "./chunk-HIIYRATQ.js";
290
302
  import {
291
303
  buildTranscriptScreenBuffer,
292
304
  isSelectionStale,
@@ -306,7 +318,7 @@ import "./chunk-KI7C6ZOD.js";
306
318
  import {
307
319
  createModelSystemPrompt,
308
320
  sanitizeMainScreenLeakage
309
- } from "./chunk-3GCIXKCI.js";
321
+ } from "./chunk-7TYK55JT.js";
310
322
  import "./chunk-J7ZE2JKP.js";
311
323
  import "./chunk-BOV4K7FP.js";
312
324
  import "./chunk-KTWV5JC5.js";
@@ -331,9 +343,10 @@ import {
331
343
  validateCodebaseMemoryToolExecution
332
344
  } from "./chunk-I3NTAIMF.js";
333
345
  import {
334
- appendSystemEvent
335
- } from "./chunk-64MJP6FK.js";
336
- import "./chunk-O7S3HYE6.js";
346
+ appendSystemEvent,
347
+ createEvidenceRecord,
348
+ rememberEvidence
349
+ } from "./chunk-L2GU3PWU.js";
337
350
  import {
338
351
  BACKGROUND_KIND_CAPS,
339
352
  BACKGROUND_RUNNING_GLOBAL_CAP,
@@ -361,6 +374,7 @@ import {
361
374
  createSingleToolCallContinuation,
362
375
  runtimeFromContinuation
363
376
  } from "./chunk-RDGM4RUE.js";
377
+ import "./chunk-O7S3HYE6.js";
364
378
  import {
365
379
  LINGHUN_BASH_MAX_OUTPUT_DEFAULT,
366
380
  LINGHUN_BASH_MAX_OUTPUT_UPPER_LIMIT,
@@ -510,6 +524,11 @@ import {
510
524
  createToolContext
511
525
  } from "@linghun/tools";
512
526
  var tuiStatus = "ready";
527
+ var DEFAULT_HEADLESS_MAX_CONTINUATIONS = 3;
528
+ var MAX_HEADLESS_CONTINUATIONS = 3;
529
+ var HEADLESS_CONTINUATION_BACKOFF_BASE_MS = 250;
530
+ var HEADLESS_CLEANUP_SETTLE_MS = 500;
531
+ var HEADLESS_DEADLINE_CLOSURE_WINDOW_MS = 6e4;
513
532
  async function createAgentRegistryState(projectPath) {
514
533
  const result = await loadAgentRegistry(projectPath);
515
534
  return { agents: result.items, errors: result.errors };
@@ -550,11 +569,7 @@ function toggleCurrentCommandPanelDetails(context) {
550
569
  context.ctrlOExpandState = { active: false };
551
570
  return true;
552
571
  }
553
- async function runTui(options = {}) {
554
- const input = options.stdin ?? defaultStdin;
555
- const output = options.stdout ?? defaultStdout;
556
- const errorOutput = options.stderr ?? defaultStderr;
557
- const projectPath = options.projectPath ?? process.cwd();
572
+ async function createTuiRuntimeContext(projectPath) {
558
573
  const config = await loadConfig(projectPath);
559
574
  const storagePaths = resolveStoragePaths(config, projectPath);
560
575
  const store = new SessionStore({ sessionRootDir: storagePaths.sessions, projectPath });
@@ -600,6 +615,7 @@ async function runTui(options = {}) {
600
615
  backgroundAbortControllers: /* @__PURE__ */ new Map(),
601
616
  discoveredDeferredToolNames: /* @__PURE__ */ new Set()
602
617
  };
618
+ context.tools.trackChildProcess = (child, options) => trackChildProcess(child, options);
603
619
  context.turnContinuity = createInitialContinuityState();
604
620
  context.recentTaskKinds = [];
605
621
  context.workflows.templates = mergeWorkflowTemplates(
@@ -607,8 +623,9 @@ async function runTui(options = {}) {
607
623
  context.agentRegistry.agents.map(registryAgentToWorkflowTemplate),
608
624
  context.workflowRegistry.workflows.map(registryWorkflowToTemplate)
609
625
  );
610
- installProcessGuardExitHandlers();
611
- const startup = await prepareTuiStartup(input, output, context);
626
+ return { context, store };
627
+ }
628
+ async function hydrateRuntimeContext(context) {
612
629
  await refreshIndexStatus(context);
613
630
  await hydrateDurableJobBackgroundTasks(context);
614
631
  await hydratePersistentAgents(context);
@@ -617,8 +634,8 @@ async function runTui(options = {}) {
617
634
  context.store.prune(50).catch(() => {
618
635
  });
619
636
  context.failureLearning.records = await loadFailureRecords(context.failureLearning);
620
- const gateway = createModelGateway(context.config);
621
- context.modelGateway = gateway;
637
+ }
638
+ function attachProviderRuntimeHooks(context) {
622
639
  registerProviderHooks({
623
640
  onRetry: (info) => {
624
641
  if (info.requestContext === "agent") return;
@@ -632,6 +649,19 @@ async function runTui(options = {}) {
632
649
  }
633
650
  });
634
651
  context.pushNotification = (text, tone) => pushTransientNotification(context, text, tone);
652
+ }
653
+ async function runTui(options = {}) {
654
+ const input = options.stdin ?? defaultStdin;
655
+ const output = options.stdout ?? defaultStdout;
656
+ const errorOutput = options.stderr ?? defaultStderr;
657
+ const projectPath = options.projectPath ?? process.cwd();
658
+ const { context, store } = await createTuiRuntimeContext(projectPath);
659
+ installProcessGuardExitHandlers();
660
+ const startup = await prepareTuiStartup(input, output, context);
661
+ await hydrateRuntimeContext(context);
662
+ const gateway = createModelGateway(context.config);
663
+ context.modelGateway = gateway;
664
+ attachProviderRuntimeHooks(context);
635
665
  const sigintHandler = () => {
636
666
  requestTrackedProcessStop(false);
637
667
  void handleInterruptCommand([], context, output).catch((error) => {
@@ -663,6 +693,420 @@ async function runTui(options = {}) {
663
693
  process.removeListener("SIGINT", sigintHandler);
664
694
  }
665
695
  }
696
+ async function runHeadlessTask(options) {
697
+ const output = options.stdout ?? defaultStdout;
698
+ const errorOutput = options.stderr ?? defaultStderr;
699
+ const projectPath = options.projectPath ?? process.cwd();
700
+ const prompt = options.prompt.trim();
701
+ if (!prompt) {
702
+ writeLine(errorOutput, "\u9519\u8BEF\uFF1Aheadless run prompt \u4E0D\u80FD\u4E3A\u7A7A\u3002");
703
+ return 2;
704
+ }
705
+ const runtime = options.__testContext && options.__testStore ? { context: options.__testContext, store: options.__testStore } : await createTuiRuntimeContext(projectPath);
706
+ const { context, store } = runtime;
707
+ context.permissionMode = options.mode ?? "full-access";
708
+ context.planAccepted = false;
709
+ installProcessGuardExitHandlers();
710
+ if (!options.__testSkipHydration) {
711
+ await hydrateRuntimeContext(context);
712
+ }
713
+ const gateway = options.__testGateway ?? createModelGateway(context.config);
714
+ const sendHeadlessMessage = options.__testSendMessage ?? sendMessage;
715
+ context.modelGateway = gateway;
716
+ attachProviderRuntimeHooks(context);
717
+ const maxApprovals = Math.max(0, Math.min(options.maxApprovals ?? 32, 200));
718
+ const autoApprove = options.autoApprove ?? true;
719
+ const maxContinuations = Math.max(
720
+ 0,
721
+ Math.min(options.maxContinuations ?? DEFAULT_HEADLESS_MAX_CONTINUATIONS, MAX_HEADLESS_CONTINUATIONS)
722
+ );
723
+ const deadlineAtMs = resolveHeadlessDeadlineAtMs(options);
724
+ const benchConfig = await resolveHeadlessBenchConfig({
725
+ prompt,
726
+ projectPath,
727
+ ...options.bench ? { options: options.bench } : {}
728
+ });
729
+ const benchPreflight = benchConfig.enabled && benchConfig.preflight ? await runHeadlessEnvironmentPreflight(projectPath) : void 0;
730
+ if (benchConfig.enabled && benchPreflight) {
731
+ writeLine(output, `[headless] bench preflight: ${benchPreflight.summary}`);
732
+ }
733
+ try {
734
+ let approvals = 0;
735
+ let continuations = 0;
736
+ let requestAttempts = 0;
737
+ let benchValidationPassed = !benchConfig.enabled;
738
+ let lastValidation;
739
+ const runOneRequest = async (text) => {
740
+ requestAttempts += 1;
741
+ const previousProviderFailureId = context.lastProviderFailure?.evidenceId;
742
+ const deferredApprovals = [];
743
+ const messagePromise = sendHeadlessMessage(text, context, gateway, output);
744
+ const pump = createHeadlessApprovalPump({
745
+ context,
746
+ gateway,
747
+ output,
748
+ errorOutput,
749
+ autoApprove,
750
+ maxApprovals,
751
+ getApprovals: () => approvals,
752
+ setApprovals: (value) => {
753
+ approvals = value;
754
+ },
755
+ deferredApprovals
756
+ });
757
+ const messageResult = await runWithHeadlessApprovalPump(messagePromise, pump);
758
+ if (messageResult.exitCode !== void 0) {
759
+ return { exitCode: messageResult.exitCode };
760
+ }
761
+ const approvalStatus = await pumpHeadlessApprovals({
762
+ context,
763
+ gateway,
764
+ output,
765
+ errorOutput,
766
+ autoApprove,
767
+ maxApprovals,
768
+ approvals,
769
+ deferredApprovals
770
+ });
771
+ approvals = approvalStatus.approvals;
772
+ if (approvalStatus.exitCode !== void 0) {
773
+ return { exitCode: approvalStatus.exitCode };
774
+ }
775
+ const deferredStatus = await runDeferredHeadlessApprovals({
776
+ context,
777
+ gateway,
778
+ output,
779
+ errorOutput,
780
+ autoApprove,
781
+ maxApprovals,
782
+ approvals,
783
+ deferredApprovals
784
+ });
785
+ approvals = deferredStatus.approvals;
786
+ if (deferredStatus.exitCode !== void 0) {
787
+ return { exitCode: deferredStatus.exitCode };
788
+ }
789
+ const providerFailure = context.lastProviderFailure;
790
+ const hasNewProviderFailure = Boolean(providerFailure) && providerFailure?.evidenceId !== previousProviderFailureId;
791
+ return { providerFailure: hasNewProviderFailure };
792
+ };
793
+ const initialPrompt = createHeadlessBenchInitialPrompt({
794
+ originalPrompt: prompt,
795
+ config: benchConfig,
796
+ ...benchPreflight ? { preflight: benchPreflight } : {}
797
+ });
798
+ let status = await runOneRequest(initialPrompt);
799
+ while (status.exitCode === void 0 && status.providerFailure && continuations < maxContinuations && shouldRunHeadlessProviderContinuation(context)) {
800
+ continuations += 1;
801
+ const sessionId = await ensureSession(context);
802
+ const continuationPrompt = createHeadlessProviderContinuationPrompt(context);
803
+ await appendSystemEvent(
804
+ context,
805
+ sessionId,
806
+ `headless_provider_stream_continuation: attempt=${continuations}; reason=${context.lastProviderFailure?.summary ?? "provider_failure"}`,
807
+ "warning"
808
+ );
809
+ writeLine(output, `[headless] provider stream failed; continuing attempt ${continuations}/${maxContinuations}`);
810
+ context.lastProviderFailure = void 0;
811
+ await sleep(createHeadlessContinuationBackoffMs(continuations));
812
+ status = await runOneRequest(continuationPrompt);
813
+ }
814
+ if (continuations > 0 && !status.providerFailure) {
815
+ context.lastProviderFailure = void 0;
816
+ }
817
+ if (status.exitCode !== void 0) {
818
+ return status.exitCode;
819
+ }
820
+ if (status.providerFailure) {
821
+ writeLine(
822
+ errorOutput,
823
+ createHeadlessProviderFailureDiagnostic(context, {
824
+ requestAttempts,
825
+ continuations,
826
+ maxContinuations
827
+ })
828
+ );
829
+ return 1;
830
+ }
831
+ if (benchConfig.enabled) {
832
+ for (let repairAttempt = 0; repairAttempt <= benchConfig.maxRepairAttempts; repairAttempt += 1) {
833
+ const validation = await validateHeadlessBenchCompletion({ projectPath, config: benchConfig });
834
+ lastValidation = validation;
835
+ if (validation.ok) {
836
+ benchValidationPassed = true;
837
+ writeLine(output, `[headless] bench validation passed: ${validation.summary}`);
838
+ break;
839
+ }
840
+ const failure = validation.failure;
841
+ writeLine(
842
+ errorOutput,
843
+ `[headless] bench validation failed: ${failure.category}; ${failure.summary.split(/\r?\n/u)[0]}`
844
+ );
845
+ if (repairAttempt >= benchConfig.maxRepairAttempts) {
846
+ writeLine(
847
+ errorOutput,
848
+ `\u9519\u8BEF\uFF1Aheadless bench \u4FEE\u8865\u5DF2\u8FBE\u4E0A\u9650 ${benchConfig.maxRepairAttempts}\uFF0C\u6700\u540E\u5931\u8D25\u7C7B\u522B\uFF1A${failure.category}`
849
+ );
850
+ return 5;
851
+ }
852
+ if (isHeadlessDeadlineApproaching(deadlineAtMs)) {
853
+ const remaining = formatHeadlessRemainingTime(deadlineAtMs);
854
+ writeLine(
855
+ errorOutput,
856
+ `[headless] deadline approaching (${remaining}); closure validation ran, skipping repair loop.`
857
+ );
858
+ return 6;
859
+ }
860
+ const repairPrompt = createHeadlessBenchRepairPrompt({
861
+ originalPrompt: prompt,
862
+ failure,
863
+ attempt: repairAttempt + 1,
864
+ maxAttempts: benchConfig.maxRepairAttempts,
865
+ profile: benchConfig.profile,
866
+ ...benchPreflight ? { preflight: benchPreflight } : {}
867
+ });
868
+ const repairStatus = await runOneRequest(repairPrompt);
869
+ if (repairStatus.exitCode !== void 0) {
870
+ return repairStatus.exitCode;
871
+ }
872
+ if (repairStatus.providerFailure) {
873
+ writeLine(errorOutput, "\u9519\u8BEF\uFF1Aheadless bench \u4FEE\u8865\u671F\u95F4 provider stream \u5931\u8D25\u3002");
874
+ return 1;
875
+ }
876
+ }
877
+ }
878
+ if (context.sessionId) {
879
+ await recordHeadlessArtifactChecklist(context, context.sessionId, benchConfig, lastValidation);
880
+ }
881
+ const cleanup = await finishHeadlessRuntime(context);
882
+ if (!cleanup.ok) {
883
+ const cleanupMessage = `\u8B66\u544A\uFF1Aheadless run \u6536\u5C3E\u5931\u8D25\uFF1A${cleanup.reason}`;
884
+ writeLine(errorOutput, cleanupMessage);
885
+ if (context.sessionId) {
886
+ await appendSystemEvent(context, context.sessionId, cleanupMessage, "warning").catch(
887
+ () => void 0
888
+ );
889
+ }
890
+ if (benchValidationPassed) {
891
+ writeLine(errorOutput, "\u8B66\u544A\uFF1A\u4EFB\u52A1\u7ED3\u679C\u5DF2\u5B8C\u6210\u4F46\u6E05\u7406\u5931\u8D25\uFF1B\u8FD9\u4E0D\u662F agent \u89E3\u9898\u5931\u8D25\u3002");
892
+ } else {
893
+ return 4;
894
+ }
895
+ }
896
+ if (context.sessionId && !context.sessionEnded) {
897
+ await store.appendEvent(context.sessionId, createSessionEndEvent(context.sessionId));
898
+ context.sessionEnded = true;
899
+ }
900
+ return 0;
901
+ } catch (error) {
902
+ const message = error instanceof Error ? error.message : "headless run \u6267\u884C\u5931\u8D25\u3002";
903
+ writeLine(errorOutput, `\u9519\u8BEF\uFF1A${message}`);
904
+ return 1;
905
+ } finally {
906
+ await finishHeadlessRuntime(context);
907
+ }
908
+ }
909
+ async function pumpHeadlessApprovals(input) {
910
+ let approvals = input.approvals;
911
+ while (input.context.pendingLocalApproval) {
912
+ if (!input.autoApprove) {
913
+ input.context.activeAbortController?.abort();
914
+ writeLine(
915
+ input.errorOutput,
916
+ `\u9519\u8BEF\uFF1Aheadless run \u505C\u5728\u6743\u9650\u786E\u8BA4\uFF1A${input.context.pendingLocalApproval.kind}\u3002\u8BF7\u5F00\u542F --auto-approve \u6216\u6539\u7528\u4EA4\u4E92\u6A21\u5F0F\u3002`
917
+ );
918
+ return { approvals, exitCode: 3 };
919
+ }
920
+ if (approvals >= input.maxApprovals) {
921
+ input.context.activeAbortController?.abort();
922
+ writeLine(
923
+ input.errorOutput,
924
+ `\u9519\u8BEF\uFF1Aheadless run \u81EA\u52A8\u6279\u51C6\u8D85\u8FC7\u4E0A\u9650 ${input.maxApprovals}\uFF0C\u505C\u5728\u6743\u9650\u786E\u8BA4\uFF1A${input.context.pendingLocalApproval.kind}\u3002`
925
+ );
926
+ return { approvals, exitCode: 3 };
927
+ }
928
+ const approval = input.context.pendingLocalApproval;
929
+ input.context.pendingLocalApproval = void 0;
930
+ approvals += 1;
931
+ writeLine(input.output, `[headless] auto-approved ${approval.kind}`);
932
+ if (input.deferredApprovals && hasModelContinuation(approval)) {
933
+ input.deferredApprovals.push(approval);
934
+ continue;
935
+ }
936
+ await executePermissionApprove(approval, input.context, input.gateway, input.output);
937
+ }
938
+ return { approvals };
939
+ }
940
+ async function runDeferredHeadlessApprovals(input) {
941
+ let approvals = input.approvals;
942
+ while (input.deferredApprovals.length > 0) {
943
+ const approval = input.deferredApprovals.shift();
944
+ if (!approval) continue;
945
+ await executePermissionApprove(approval, input.context, input.gateway, input.output);
946
+ const nested = await pumpHeadlessApprovals({
947
+ context: input.context,
948
+ gateway: input.gateway,
949
+ output: input.output,
950
+ errorOutput: input.errorOutput,
951
+ autoApprove: input.autoApprove,
952
+ maxApprovals: input.maxApprovals,
953
+ approvals
954
+ });
955
+ approvals = nested.approvals;
956
+ if (nested.exitCode !== void 0) {
957
+ return { approvals, exitCode: nested.exitCode };
958
+ }
959
+ }
960
+ return { approvals };
961
+ }
962
+ function hasModelContinuation(approval) {
963
+ return "continuation" in approval && Boolean(approval.continuation);
964
+ }
965
+ function createHeadlessApprovalPump(input) {
966
+ let running;
967
+ return async () => {
968
+ if (running) return running;
969
+ running = pumpHeadlessApprovals({
970
+ context: input.context,
971
+ gateway: input.gateway,
972
+ output: input.output,
973
+ errorOutput: input.errorOutput,
974
+ autoApprove: input.autoApprove,
975
+ maxApprovals: input.maxApprovals,
976
+ approvals: input.getApprovals(),
977
+ deferredApprovals: input.deferredApprovals
978
+ }).then((result) => {
979
+ input.setApprovals(result.approvals);
980
+ return result;
981
+ });
982
+ try {
983
+ return await running;
984
+ } finally {
985
+ running = void 0;
986
+ }
987
+ };
988
+ }
989
+ async function runWithHeadlessApprovalPump(messagePromise, pump) {
990
+ let settled = false;
991
+ let exitCode;
992
+ const loop = (async () => {
993
+ while (!settled && exitCode === void 0) {
994
+ const result = await pump();
995
+ exitCode = result.exitCode;
996
+ if (exitCode !== void 0) break;
997
+ await sleep(50);
998
+ }
999
+ })();
1000
+ try {
1001
+ await messagePromise;
1002
+ } finally {
1003
+ settled = true;
1004
+ await loop;
1005
+ }
1006
+ return exitCode !== void 0 ? { exitCode } : {};
1007
+ }
1008
+ function sleep(ms) {
1009
+ return new Promise((resolveSleep) => setTimeout(resolveSleep, ms));
1010
+ }
1011
+ function createHeadlessContinuationBackoffMs(attempt) {
1012
+ return Math.min(HEADLESS_CONTINUATION_BACKOFF_BASE_MS * 2 ** Math.max(0, attempt - 1), 1500);
1013
+ }
1014
+ function resolveHeadlessDeadlineAtMs(options) {
1015
+ if (typeof options.deadlineAtMs === "number" && Number.isFinite(options.deadlineAtMs)) {
1016
+ return options.deadlineAtMs;
1017
+ }
1018
+ if (typeof options.deadlineMs === "number" && Number.isFinite(options.deadlineMs)) {
1019
+ return Date.now() + Math.max(0, options.deadlineMs);
1020
+ }
1021
+ const envDeadlineAt = Number.parseInt(process.env.LINGHUN_HEADLESS_DEADLINE_AT_MS ?? "", 10);
1022
+ if (Number.isFinite(envDeadlineAt) && envDeadlineAt > 0) {
1023
+ return envDeadlineAt;
1024
+ }
1025
+ return void 0;
1026
+ }
1027
+ function isHeadlessDeadlineApproaching(deadlineAtMs) {
1028
+ return deadlineAtMs !== void 0 && deadlineAtMs - Date.now() <= HEADLESS_DEADLINE_CLOSURE_WINDOW_MS;
1029
+ }
1030
+ function formatHeadlessRemainingTime(deadlineAtMs) {
1031
+ if (deadlineAtMs === void 0) return "unknown";
1032
+ return `${Math.max(0, Math.ceil((deadlineAtMs - Date.now()) / 1e3))}s remaining`;
1033
+ }
1034
+ function shouldRunHeadlessProviderContinuation(context) {
1035
+ return context.evidence.some((record) => !record.supportsClaims.includes("provider_failure")) || context.tools.changedFiles.length > 0;
1036
+ }
1037
+ async function recordHeadlessArtifactChecklist(context, sessionId, config, lastValidation) {
1038
+ const checklist = await collectHeadlessArtifactChecklist({
1039
+ projectPath: context.projectPath,
1040
+ config,
1041
+ changedFiles: context.tools.changedFiles,
1042
+ ...lastValidation ? { lastValidation } : {}
1043
+ });
1044
+ const evidence = createEvidenceRecord(
1045
+ "command_output",
1046
+ `headless artifact checklist: ${checklist.summary}`,
1047
+ "headless:artifact-checklist",
1048
+ [
1049
+ "headless_artifact_checklist",
1050
+ checklist.verificationRan ? "verification_ran" : "verification_not_run",
1051
+ checklist.changedFiles.length > 0 ? "file_written" : "no_file_changes"
1052
+ ]
1053
+ );
1054
+ rememberEvidence(context, evidence);
1055
+ await context.store.appendEvent(sessionId, { type: "evidence_record", ...evidence });
1056
+ await appendSystemEvent(context, sessionId, `headless artifact checklist: ${checklist.summary}`, "info");
1057
+ }
1058
+ var __testHeadlessRuntime = {
1059
+ isDeadlineApproaching: isHeadlessDeadlineApproaching,
1060
+ formatRemainingTime: formatHeadlessRemainingTime
1061
+ };
1062
+ function createHeadlessProviderFailureDiagnostic(context, input) {
1063
+ const failure = context.lastProviderFailure;
1064
+ const lastEvidence = context.evidence.find(
1065
+ (record) => !record.supportsClaims.includes("provider_failure")
1066
+ );
1067
+ const changedFiles = context.tools.changedFiles.length;
1068
+ return [
1069
+ "\u9519\u8BEF\uFF1Aprovider stream \u5931\u8D25\uFF0Cheadless continuation \u5DF2\u8FBE\u4E0A\u9650\u6216\u7F3A\u5C11\u53EF\u6062\u590D\u8BC1\u636E\u3002",
1070
+ `providerKind=${failure?.kind ?? "unknown"}`,
1071
+ `providerCode=${failure?.code ?? "unknown"}`,
1072
+ `attempts=${input.requestAttempts}`,
1073
+ `continuations=${input.continuations}/${input.maxContinuations}`,
1074
+ `lastSuccessfulTool=${lastEvidence?.source ?? "none"}`,
1075
+ `fileChanges=${changedFiles > 0 ? `yes:${changedFiles}` : "no"}`,
1076
+ failure?.summary ? `summary=${failure.summary}` : ""
1077
+ ].filter(Boolean).join(" ");
1078
+ }
1079
+ function createHeadlessProviderContinuationPrompt(context) {
1080
+ return context.language === "en-US" ? "The previous response stream was interrupted. Continue the original task based on the current workspace state, transcript, tool evidence, and any file changes already present. Do not restart from scratch unless needed." : "\u4E0A\u4E00\u8F6E\u54CD\u5E94\u6D41\u4E2D\u65AD\u3002\u8BF7\u57FA\u4E8E\u5F53\u524D\u5DE5\u4F5C\u533A\u72B6\u6001\u3001\u5DF2\u6709 transcript\u3001\u5DE5\u5177\u8BC1\u636E\u548C\u5DF2\u7ECF\u5B58\u5728\u7684\u6587\u4EF6\u6539\u52A8\u7EE7\u7EED\u5B8C\u6210\u539F\u4EFB\u52A1\uFF1B\u9664\u975E\u5FC5\u8981\uFF0C\u4E0D\u8981\u4ECE\u5934\u91CD\u505A\u3002";
1081
+ }
1082
+ async function finishHeadlessRuntime(context) {
1083
+ if (context.pendingLocalApproval) {
1084
+ return { ok: false, reason: `\u4ECD\u6709\u6743\u9650\u786E\u8BA4\u672A\u5904\u7406\uFF1A${context.pendingLocalApproval.kind}` };
1085
+ }
1086
+ if (context.activeAbortController) {
1087
+ context.activeAbortController.abort();
1088
+ }
1089
+ context.tools.abortSignal = void 0;
1090
+ context.interrupt = { type: "idle" };
1091
+ clearRequestActivity(context);
1092
+ context.activeAbortController = void 0;
1093
+ const stopResult = requestTrackedProcessStop(true);
1094
+ await sleep(HEADLESS_CLEANUP_SETTLE_MS);
1095
+ const stillTracked = getTrackedProcessSnapshot();
1096
+ if (stopResult.failures.length > 0) {
1097
+ return {
1098
+ ok: false,
1099
+ reason: `\u6E05\u7406\u672C\u5730\u5B50\u8FDB\u7A0B\u5931\u8D25\uFF1A${stopResult.failures.map((failure) => `${failure.pid}:${failure.message}`).join("; ")}`
1100
+ };
1101
+ }
1102
+ if (stillTracked.length > 0) {
1103
+ return {
1104
+ ok: false,
1105
+ reason: `\u4ECD\u6709\u672C\u5730\u5B50\u8FDB\u7A0B\u672A\u9000\u51FA\uFF1A${stillTracked.map((entry) => `${entry.pid}${entry.label ? `(${entry.label})` : ""}`).join(", ")}`
1106
+ };
1107
+ }
1108
+ return { ok: true };
1109
+ }
666
1110
  var MAX_TOOL_LOG_FILES = 200;
667
1111
  async function cleanStaleToolLogs(context) {
668
1112
  const logsRoot = resolveStoragePaths(context.config, context.projectPath).logs;
@@ -2220,6 +2664,7 @@ export {
2220
2664
  __testCreateVerificationLevelForReadiness,
2221
2665
  __testFormatStartAgentDidNotStartMessage,
2222
2666
  __testGetCurrentWorkflowStepRequest,
2667
+ __testHeadlessRuntime,
2223
2668
  __testParseRunWorkflowToolInput,
2224
2669
  __testRenderInteractiveChoiceLines,
2225
2670
  __testRunWorkflowStepsWithPlan,
@@ -2273,6 +2718,7 @@ export {
2273
2718
  dingtalkBridgeAdapter,
2274
2719
  dingtalkStreamFrameToBridgeEvent,
2275
2720
  estimateFileMetrics,
2721
+ evaluateAggregatedFinalAnswerGate,
2276
2722
  executeExtraTool,
2277
2723
  executeSearchExtraTools,
2278
2724
  feishuBridgeAdapter,
@@ -2320,6 +2766,7 @@ export {
2320
2766
  recordModelUsage,
2321
2767
  runAutoLearningOnTurnEnd,
2322
2768
  runCommandCaptureForTest,
2769
+ runHeadlessTask,
2323
2770
  runTui,
2324
2771
  runVerificationCommandForTest,
2325
2772
  runtimeFromContinuation,