@caupulican/pi-adaptative 0.90.7 → 0.90.12

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 (224) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/dist/bundled-resources/skills/tool-call-repair/SKILL.md +5 -4
  3. package/dist/bundled-resources/skills/tool-call-repair/references/failure-grammar.md +6 -3
  4. package/dist/bundled-resources/skills/tool-call-repair/references/repair-catalogue.md +1 -0
  5. package/dist/core/agent-session-contracts.d.ts +2 -0
  6. package/dist/core/agent-session-contracts.d.ts.map +1 -1
  7. package/dist/core/agent-session-contracts.js.map +1 -1
  8. package/dist/core/agent-session.d.ts +8 -7
  9. package/dist/core/agent-session.d.ts.map +1 -1
  10. package/dist/core/agent-session.js +84 -197
  11. package/dist/core/agent-session.js.map +1 -1
  12. package/dist/core/autonomy/bounded-completion.d.ts +7 -1
  13. package/dist/core/autonomy/bounded-completion.d.ts.map +1 -1
  14. package/dist/core/autonomy/bounded-completion.js +8 -2
  15. package/dist/core/autonomy/bounded-completion.js.map +1 -1
  16. package/dist/core/autonomy/contracts.d.ts +1 -1
  17. package/dist/core/autonomy/contracts.d.ts.map +1 -1
  18. package/dist/core/autonomy/contracts.js.map +1 -1
  19. package/dist/core/background-lane-controller.d.ts +14 -0
  20. package/dist/core/background-lane-controller.d.ts.map +1 -1
  21. package/dist/core/background-lane-controller.js +32 -0
  22. package/dist/core/background-lane-controller.js.map +1 -1
  23. package/dist/core/billing-failover-controller.js +1 -1
  24. package/dist/core/billing-failover-controller.js.map +1 -1
  25. package/dist/core/context/current-work-memory.d.ts +1 -1
  26. package/dist/core/context/current-work-memory.d.ts.map +1 -1
  27. package/dist/core/context/current-work-memory.js +2 -1
  28. package/dist/core/context/current-work-memory.js.map +1 -1
  29. package/dist/core/context/long-term-memory-trigger.d.ts +1 -1
  30. package/dist/core/context/long-term-memory-trigger.d.ts.map +1 -1
  31. package/dist/core/context/long-term-memory-trigger.js +2 -1
  32. package/dist/core/context/long-term-memory-trigger.js.map +1 -1
  33. package/dist/core/delegation/managed-lane-controller.js +2 -2
  34. package/dist/core/delegation/managed-lane-controller.js.map +1 -1
  35. package/dist/core/delegation/worker-agent-control-coordinator.d.ts +13 -0
  36. package/dist/core/delegation/worker-agent-control-coordinator.d.ts.map +1 -1
  37. package/dist/core/delegation/worker-agent-control-coordinator.js +71 -8
  38. package/dist/core/delegation/worker-agent-control-coordinator.js.map +1 -1
  39. package/dist/core/delegation/worker-attempt-executor.d.ts.map +1 -1
  40. package/dist/core/delegation/worker-attempt-executor.js +13 -8
  41. package/dist/core/delegation/worker-attempt-executor.js.map +1 -1
  42. package/dist/core/delegation/worker-claim.d.ts.map +1 -1
  43. package/dist/core/delegation/worker-claim.js +14 -1
  44. package/dist/core/delegation/worker-claim.js.map +1 -1
  45. package/dist/core/delegation/worker-delegation-controller.d.ts +3 -2
  46. package/dist/core/delegation/worker-delegation-controller.d.ts.map +1 -1
  47. package/dist/core/delegation/worker-delegation-controller.js +95 -42
  48. package/dist/core/delegation/worker-delegation-controller.js.map +1 -1
  49. package/dist/core/delegation/worker-fleet-limits.d.ts.map +1 -1
  50. package/dist/core/delegation/worker-fleet-limits.js +5 -1
  51. package/dist/core/delegation/worker-fleet-limits.js.map +1 -1
  52. package/dist/core/delegation/worker-lane-projection.d.ts.map +1 -1
  53. package/dist/core/delegation/worker-lane-projection.js +7 -4
  54. package/dist/core/delegation/worker-lane-projection.js.map +1 -1
  55. package/dist/core/delegation/worker-lifecycle.d.ts +8 -0
  56. package/dist/core/delegation/worker-lifecycle.d.ts.map +1 -1
  57. package/dist/core/delegation/worker-lifecycle.js.map +1 -1
  58. package/dist/core/delegation/worker-notification-coordinator.d.ts +21 -0
  59. package/dist/core/delegation/worker-notification-coordinator.d.ts.map +1 -1
  60. package/dist/core/delegation/worker-notification-coordinator.js +65 -2
  61. package/dist/core/delegation/worker-notification-coordinator.js.map +1 -1
  62. package/dist/core/delegation/worker-runner.d.ts +7 -0
  63. package/dist/core/delegation/worker-runner.d.ts.map +1 -1
  64. package/dist/core/delegation/worker-runner.js +24 -7
  65. package/dist/core/delegation/worker-runner.js.map +1 -1
  66. package/dist/core/delegation/worker-system-prompt.d.ts +54 -0
  67. package/dist/core/delegation/worker-system-prompt.d.ts.map +1 -0
  68. package/dist/core/delegation/worker-system-prompt.js +85 -0
  69. package/dist/core/delegation/worker-system-prompt.js.map +1 -0
  70. package/dist/core/delegation/worker-tree-budget-coordinator.d.ts.map +1 -1
  71. package/dist/core/delegation/worker-tree-budget-coordinator.js +4 -0
  72. package/dist/core/delegation/worker-tree-budget-coordinator.js.map +1 -1
  73. package/dist/core/delegation/worker-write-reservation-coordinator.d.ts +4 -0
  74. package/dist/core/delegation/worker-write-reservation-coordinator.d.ts.map +1 -1
  75. package/dist/core/delegation/worker-write-reservation-coordinator.js +28 -1
  76. package/dist/core/delegation/worker-write-reservation-coordinator.js.map +1 -1
  77. package/dist/core/durable-custom-message-turn-controller.d.ts.map +1 -1
  78. package/dist/core/durable-custom-message-turn-controller.js +14 -3
  79. package/dist/core/durable-custom-message-turn-controller.js.map +1 -1
  80. package/dist/core/extension-binding-controller.d.ts +107 -0
  81. package/dist/core/extension-binding-controller.d.ts.map +1 -0
  82. package/dist/core/extension-binding-controller.js +224 -0
  83. package/dist/core/extension-binding-controller.js.map +1 -0
  84. package/dist/core/extensions/extension-session-scope.d.ts +17 -0
  85. package/dist/core/extensions/extension-session-scope.d.ts.map +1 -0
  86. package/dist/core/extensions/extension-session-scope.js +135 -0
  87. package/dist/core/extensions/extension-session-scope.js.map +1 -0
  88. package/dist/core/extensions/index.d.ts +1 -0
  89. package/dist/core/extensions/index.d.ts.map +1 -1
  90. package/dist/core/extensions/index.js +1 -0
  91. package/dist/core/extensions/index.js.map +1 -1
  92. package/dist/core/extensions/types.d.ts +2 -0
  93. package/dist/core/extensions/types.d.ts.map +1 -1
  94. package/dist/core/extensions/types.js.map +1 -1
  95. package/dist/core/extensions/wrapper.d.ts +2 -1
  96. package/dist/core/extensions/wrapper.d.ts.map +1 -1
  97. package/dist/core/extensions/wrapper.js +10 -4
  98. package/dist/core/extensions/wrapper.js.map +1 -1
  99. package/dist/core/foreground-terminal-handoff-controller.d.ts +9 -0
  100. package/dist/core/foreground-terminal-handoff-controller.d.ts.map +1 -1
  101. package/dist/core/foreground-terminal-handoff-controller.js +35 -9
  102. package/dist/core/foreground-terminal-handoff-controller.js.map +1 -1
  103. package/dist/core/goal-loop-controller.d.ts.map +1 -1
  104. package/dist/core/goal-loop-controller.js +10 -1
  105. package/dist/core/goal-loop-controller.js.map +1 -1
  106. package/dist/core/goals/compact-goal-context.d.ts +1 -1
  107. package/dist/core/goals/compact-goal-context.d.ts.map +1 -1
  108. package/dist/core/goals/compact-goal-context.js +2 -1
  109. package/dist/core/goals/compact-goal-context.js.map +1 -1
  110. package/dist/core/goals/goal-execution-errors.d.ts +18 -0
  111. package/dist/core/goals/goal-execution-errors.d.ts.map +1 -0
  112. package/dist/core/goals/goal-execution-errors.js +21 -0
  113. package/dist/core/goals/goal-execution-errors.js.map +1 -0
  114. package/dist/core/goals/goal-lifecycle.d.ts.map +1 -1
  115. package/dist/core/goals/goal-lifecycle.js +3 -3
  116. package/dist/core/goals/goal-lifecycle.js.map +1 -1
  117. package/dist/core/goals/goal-session-controller.d.ts +11 -0
  118. package/dist/core/goals/goal-session-controller.d.ts.map +1 -1
  119. package/dist/core/goals/goal-session-controller.js +74 -14
  120. package/dist/core/goals/goal-session-controller.js.map +1 -1
  121. package/dist/core/goals/goal-state.js +1 -1
  122. package/dist/core/goals/goal-state.js.map +1 -1
  123. package/dist/core/goals/goal-tool-core.d.ts.map +1 -1
  124. package/dist/core/goals/goal-tool-core.js +2 -2
  125. package/dist/core/goals/goal-tool-core.js.map +1 -1
  126. package/dist/core/goals/natural-language-goal.d.ts +18 -0
  127. package/dist/core/goals/natural-language-goal.d.ts.map +1 -1
  128. package/dist/core/goals/natural-language-goal.js +99 -16
  129. package/dist/core/goals/natural-language-goal.js.map +1 -1
  130. package/dist/core/model-registry.d.ts +5 -0
  131. package/dist/core/model-registry.d.ts.map +1 -1
  132. package/dist/core/model-registry.js +10 -0
  133. package/dist/core/model-registry.js.map +1 -1
  134. package/dist/core/model-resolver.d.ts.map +1 -1
  135. package/dist/core/model-resolver.js +3 -1
  136. package/dist/core/model-resolver.js.map +1 -1
  137. package/dist/core/orchestration/capability-gateway.d.ts +1 -1
  138. package/dist/core/orchestration/capability-gateway.d.ts.map +1 -1
  139. package/dist/core/orchestration/capability-gateway.js +4 -4
  140. package/dist/core/orchestration/capability-gateway.js.map +1 -1
  141. package/dist/core/orchestration/event-store.d.ts +8 -0
  142. package/dist/core/orchestration/event-store.d.ts.map +1 -1
  143. package/dist/core/orchestration/event-store.js +9 -7
  144. package/dist/core/orchestration/event-store.js.map +1 -1
  145. package/dist/core/orchestration/task-runtime-reducer.js +1 -1
  146. package/dist/core/orchestration/task-runtime-reducer.js.map +1 -1
  147. package/dist/core/orchestration/worker-model-pins.d.ts +7 -0
  148. package/dist/core/orchestration/worker-model-pins.d.ts.map +1 -1
  149. package/dist/core/orchestration/worker-model-pins.js +17 -1
  150. package/dist/core/orchestration/worker-model-pins.js.map +1 -1
  151. package/dist/core/provider-request-runtime-controller.d.ts +9 -0
  152. package/dist/core/provider-request-runtime-controller.d.ts.map +1 -1
  153. package/dist/core/provider-request-runtime-controller.js +17 -3
  154. package/dist/core/provider-request-runtime-controller.js.map +1 -1
  155. package/dist/core/provider-tool-text.d.ts +15 -1
  156. package/dist/core/provider-tool-text.d.ts.map +1 -1
  157. package/dist/core/provider-tool-text.js +42 -10
  158. package/dist/core/provider-tool-text.js.map +1 -1
  159. package/dist/core/research/research-lane-controller.d.ts +1 -1
  160. package/dist/core/research/research-lane-controller.d.ts.map +1 -1
  161. package/dist/core/research/research-lane-controller.js +2 -1
  162. package/dist/core/research/research-lane-controller.js.map +1 -1
  163. package/dist/core/resource-loader.d.ts +3 -0
  164. package/dist/core/resource-loader.d.ts.map +1 -1
  165. package/dist/core/resource-loader.js +8 -0
  166. package/dist/core/resource-loader.js.map +1 -1
  167. package/dist/core/runtime-builder.d.ts +8 -0
  168. package/dist/core/runtime-builder.d.ts.map +1 -1
  169. package/dist/core/runtime-builder.js +6 -0
  170. package/dist/core/runtime-builder.js.map +1 -1
  171. package/dist/core/session-supervision-runtime.d.ts.map +1 -1
  172. package/dist/core/session-supervision-runtime.js +2 -1
  173. package/dist/core/session-supervision-runtime.js.map +1 -1
  174. package/dist/core/settings-manager.d.ts +2 -2
  175. package/dist/core/settings-manager.d.ts.map +1 -1
  176. package/dist/core/settings-manager.js +7 -2
  177. package/dist/core/settings-manager.js.map +1 -1
  178. package/dist/core/tasks/task-state.d.ts +1 -1
  179. package/dist/core/tasks/task-state.d.ts.map +1 -1
  180. package/dist/core/tasks/task-state.js +4 -2
  181. package/dist/core/tasks/task-state.js.map +1 -1
  182. package/dist/core/tools/delegate.d.ts +13 -0
  183. package/dist/core/tools/delegate.d.ts.map +1 -1
  184. package/dist/core/tools/delegate.js +20 -18
  185. package/dist/core/tools/delegate.js.map +1 -1
  186. package/dist/core/util/atomic-file.d.ts +7 -0
  187. package/dist/core/util/atomic-file.d.ts.map +1 -1
  188. package/dist/core/util/atomic-file.js +8 -6
  189. package/dist/core/util/atomic-file.js.map +1 -1
  190. package/dist/core/util/faultable-fs.d.ts +30 -0
  191. package/dist/core/util/faultable-fs.d.ts.map +1 -0
  192. package/dist/core/util/faultable-fs.js +30 -0
  193. package/dist/core/util/faultable-fs.js.map +1 -0
  194. package/dist/modes/interactive/auth-dialogs-controller.d.ts.map +1 -1
  195. package/dist/modes/interactive/auth-dialogs-controller.js +1 -0
  196. package/dist/modes/interactive/auth-dialogs-controller.js.map +1 -1
  197. package/dist/modes/interactive/components/activity-lane.d.ts.map +1 -1
  198. package/dist/modes/interactive/components/activity-lane.js +4 -3
  199. package/dist/modes/interactive/components/activity-lane.js.map +1 -1
  200. package/dist/modes/interactive/components/footer.js +1 -1
  201. package/dist/modes/interactive/components/footer.js.map +1 -1
  202. package/dist/modes/interactive/components/oauth-selector.d.ts +2 -0
  203. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
  204. package/dist/modes/interactive/components/oauth-selector.js +3 -2
  205. package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
  206. package/dist/modes/interactive/report-commands.js +1 -1
  207. package/dist/modes/interactive/report-commands.js.map +1 -1
  208. package/dist/modes/interactive/session-flow-commands.d.ts +1 -1
  209. package/dist/modes/interactive/session-flow-commands.d.ts.map +1 -1
  210. package/dist/modes/interactive/session-flow-commands.js +3 -2
  211. package/dist/modes/interactive/session-flow-commands.js.map +1 -1
  212. package/docs/extensions.md +1 -0
  213. package/docs/providers.md +3 -0
  214. package/docs/settings.md +4 -2
  215. package/docs/tool-repair.md +1 -0
  216. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  217. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  218. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  219. package/examples/extensions/sandbox/package-lock.json +2 -2
  220. package/examples/extensions/sandbox/package.json +1 -1
  221. package/examples/extensions/with-deps/package-lock.json +2 -2
  222. package/examples/extensions/with-deps/package.json +1 -1
  223. package/npm-shrinkwrap.json +12 -12
  224. package/package.json +9 -4
@@ -1 +1 @@
1
- {"version":3,"file":"wrapper.js","sourceRoot":"","sources":["../../../src/core/extensions/wrapper.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAI9F;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,cAA8B,EAAE,MAAuB;IACzF,OAAO,kBAAkB,CAAC,cAAc,CAAC,UAAU,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC,CAAC;AACpF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,eAAiC,EAAE,MAAuB;IAC7F,OAAO,mBAAmB,CACzB,eAAe,CAAC,GAAG,CAAC,CAAC,cAAc,EAAE,EAAE,CAAC,cAAc,CAAC,UAAU,CAAC,EAClE,GAAG,EAAE,CAAC,MAAM,CAAC,aAAa,EAAE,CAC5B,CAAC;AACH,CAAC","sourcesContent":["/**\n * Tool wrappers for extension-registered tools.\n *\n * These wrappers only adapt tool execution so extension tools receive the runner context.\n * Tool call and tool result interception is handled by AgentSession via agent-core hooks.\n */\n\nimport type { AgentTool } from \"@caupulican/pi-agent-core\";\nimport { wrapToolDefinition, wrapToolDefinitions } from \"../tools/tool-definition-wrapper.ts\";\nimport type { ExtensionRunner } from \"./runner.ts\";\nimport type { RegisteredTool } from \"./types.ts\";\n\n/**\n * Wrap a RegisteredTool into an AgentTool.\n * Uses the runner's createContext() for consistent context across tools and event handlers.\n */\nexport function wrapRegisteredTool(registeredTool: RegisteredTool, runner: ExtensionRunner): AgentTool {\n\treturn wrapToolDefinition(registeredTool.definition, () => runner.createContext());\n}\n\n/**\n * Wrap all registered tools into AgentTools.\n * Uses the runner's createContext() for consistent context across tools and event handlers.\n */\nexport function wrapRegisteredTools(registeredTools: RegisteredTool[], runner: ExtensionRunner): AgentTool[] {\n\treturn wrapToolDefinitions(\n\t\tregisteredTools.map((registeredTool) => registeredTool.definition),\n\t\t() => runner.createContext(),\n\t);\n}\n"]}
1
+ {"version":3,"file":"wrapper.js","sourceRoot":"","sources":["../../../src/core/extensions/wrapper.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EACN,yBAAyB,EACzB,sBAAsB,EACtB,wBAAwB,GACxB,MAAM,8BAA8B,CAAC;AAItC;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,cAA8B,EAAE,MAAuB;IACzF,MAAM,QAAQ,GAAG,sBAAsB,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;IACnE,MAAM,UAAU,GACf,QAAQ,KAAK,SAAS;QACrB,CAAC,CAAC,cAAc,CAAC,UAAU;QAC3B,CAAC,CAAC,yBAAyB,CAAC,cAAc,CAAC,UAAU,EAAE,QAAQ,EAAE,wBAAwB,CAAC,MAAM,CAAC,CAAC,CAAC;IACrG,OAAO,kBAAkB,CAAC,UAAU,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC,CAAC;AACrE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,eAAiC,EAAE,MAAuB;IAC7F,OAAO,eAAe,CAAC,GAAG,CAAC,CAAC,cAAc,EAAE,EAAE,CAAC,kBAAkB,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC,CAAC;AAC5F,CAAC","sourcesContent":["/**\n * Tool wrappers for extension-registered tools.\n *\n * These wrappers adapt tool execution so extension tools receive the runner context\n * and reuse session-scoped identity from prior successful results of the same extension.\n * Tool call and tool result interception is handled by AgentSession via agent-core hooks.\n */\n\nimport type { AgentTool } from \"@caupulican/pi-agent-core\";\nimport { wrapToolDefinition } from \"../tools/tool-definition-wrapper.ts\";\nimport {\n\tapplyExtensionSessionHeal,\n\textensionScopeOwnerKey,\n\textensionSessionScopeFor,\n} from \"./extension-session-scope.ts\";\nimport type { ExtensionRunner } from \"./runner.ts\";\nimport type { RegisteredTool } from \"./types.ts\";\n\n/**\n * Wrap a RegisteredTool into an AgentTool.\n * Uses the runner's createContext() for consistent context across tools and event handlers.\n */\nexport function wrapRegisteredTool(registeredTool: RegisteredTool, runner: ExtensionRunner): AgentTool {\n\tconst ownerKey = extensionScopeOwnerKey(registeredTool.sourceInfo);\n\tconst definition =\n\t\townerKey === undefined\n\t\t\t? registeredTool.definition\n\t\t\t: applyExtensionSessionHeal(registeredTool.definition, ownerKey, extensionSessionScopeFor(runner));\n\treturn wrapToolDefinition(definition, () => runner.createContext());\n}\n\n/**\n * Wrap all registered tools into AgentTools.\n * Uses the runner's createContext() for consistent context across tools and event handlers.\n */\nexport function wrapRegisteredTools(registeredTools: RegisteredTool[], runner: ExtensionRunner): AgentTool[] {\n\treturn registeredTools.map((registeredTool) => wrapRegisteredTool(registeredTool, runner));\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import type { CustomMessage } from "@caupulican/pi-agent-core";
2
2
  import type { LaneTerminalStatus } from "./autonomy/lane-tracker.ts";
3
3
  import { type BackgroundToolTaskRecord } from "./background-tool-task-controller.ts";
4
+ import type { WorkerClaimSnapshotPayload } from "./delegation/session-worker-claim.ts";
4
5
  import type { WorkerTerminalHandoffRecord } from "./delegation/worker-notification-coordinator.ts";
5
6
  import type { ForegroundRecoveryController, ForegroundSubmissionLease } from "./foreground-recovery-controller.ts";
6
7
  import { type GoalState } from "./goals/goal-state.ts";
@@ -9,6 +10,7 @@ interface ForegroundTerminalHandoffControllerDeps {
9
10
  isDisposed(): boolean;
10
11
  getGoalStateSnapshot(): Pick<GoalState, "goalId" | "status"> | undefined;
11
12
  workerInputsWillWakeParent(workerRequestIds: readonly string[]): boolean;
13
+ getWorkerClaimSnapshot?(laneId: string): WorkerClaimSnapshotPayload | undefined;
12
14
  startCustomMessageTurn(message: Pick<CustomMessage<unknown>, "customType" | "content" | "display" | "details">, lease: ForegroundSubmissionLease, goalId?: string): Promise<{
13
15
  completion: Promise<void>;
14
16
  }>;
@@ -22,6 +24,13 @@ export declare function buildForegroundWorkerTerminalHandoffContent(records: rea
22
24
  laneId: string;
23
25
  status: LaneTerminalStatus;
24
26
  reasonCode?: string;
27
+ claim?: {
28
+ summary?: string;
29
+ status?: string;
30
+ changedFiles?: readonly string[];
31
+ blockers?: readonly string[];
32
+ parentReviewRequired?: boolean;
33
+ };
25
34
  }[], options?: {
26
35
  wakeParent?: boolean;
27
36
  }): string;
@@ -1 +1 @@
1
- {"version":3,"file":"foreground-terminal-handoff-controller.d.ts","sourceRoot":"","sources":["../../src/core/foreground-terminal-handoff-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EA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S;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,kBAAkB,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,EAAE,EACvF,OAAO,CAAC,EAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GAChC,MAAM,CAmCR;AAED,+FAA+F;AAC/F,qBAAa,mCAAmC;IAC/C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA0C;IAE/D,YAAY,IAAI,EAAE,uCAAuC,EAExD;IAEK,aAAa,CAAC,OAAO,EAAE,SAAS,2BAA2B,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAoClF;IAEK,WAAW,CAAC,OAAO,EAAE,SAAS,wBAAwB,EAAE,EAAE,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAalG;IAED,OAAO,CAAC,gBAAgB;IAYxB,OAAO,CAAC,UAAU;CAGlB"}
1
+ {"version":3,"file":"foreground-terminal-handoff-controller.d.ts","sourceRoot":"","sources":["../../src/core/foreground-terminal-handoff-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AACrE,OAAO,EACN,KAAK,wBAAwB,EAE7B,MAAM,sCAAsC,CAAC;AAC9C,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,sCAAsC,CAAC;AACvF,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,iDAAiD,CAAC;AAEnG,OAAO,KAAK,EAAE,4BAA4B,EAAE,yBAAyB,EAAE,MAAM,qCAAqC,CAAC;AACnH,OAAO,EAAE,KAAK,SAAS,EAAyB,MAAM,uBAAuB,CAAC;AAE9E,UAAU,uCAAuC;IAChD,UAAU,EAAE,4BAA4B,CAAC;IACzC,UAAU,IAAI,OAAO,CAAC;IACtB,oBAAoB,IAAI,IAAI,CAAC,SAAS,EAA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,EAAE,OAAO,CAAA;CAAE,GAChC,MAAM,CA8CR;AAED,+FAA+F;AAC/F,qBAAa,mCAAmC;IAC/C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA0C;IAE/D,YAAY,IAAI,EAAE,uCAAuC,EAExD;IAEK,aAAa,CAAC,OAAO,EAAE,SAAS,2BAA2B,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAmDlF;IAEK,WAAW,CAAC,OAAO,EAAE,SAAS,wBAAwB,EAAE,EAAE,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAalG;IAED,OAAO,CAAC,gBAAgB;IAYxB,OAAO,CAAC,UAAU;CAGlB"}
@@ -8,9 +8,20 @@ export function buildForegroundWorkerTerminalHandoffContent(records, options) {
8
8
  const sanitize = (value) => value.replace(/[\r\n]+/g, " ").slice(0, 120);
9
9
  return [
10
10
  "Background worker terminal handoff:",
11
- ...included.map((record) => {
11
+ ...included.flatMap((record) => {
12
12
  const reason = record.reasonCode ? ` reason=${sanitize(record.reasonCode)}` : "";
13
- return `- ${record.laneId}: ${record.status}${reason}`;
13
+ const lines = [`- ${record.laneId}: ${record.status}${reason}`];
14
+ if (record.claim?.summary) {
15
+ lines.push(` Claim Status: ${record.claim.status || record.status}`);
16
+ lines.push(` Claim Summary: ${sanitize(record.claim.summary)}`);
17
+ if (record.claim.changedFiles && record.claim.changedFiles.length > 0) {
18
+ lines.push(` Changed Files: ${record.claim.changedFiles.map((f) => sanitize(f)).join(", ")}`);
19
+ }
20
+ if (record.claim.blockers && record.claim.blockers.length > 0) {
21
+ lines.push(` Blockers: ${record.claim.blockers.map((b) => sanitize(b)).join("; ")}`);
22
+ }
23
+ }
24
+ return lines;
14
25
  }),
15
26
  ...(omitted > 0 ? [`- ${omitted} additional terminal worker(s) omitted.`] : []),
16
27
  "CAVEMAN MODE - MANDATORY: this event proves terminal persistence and delivery. Do not report missed completion or lost worker state from these records.",
@@ -32,7 +43,7 @@ export function buildForegroundWorkerTerminalHandoffContent(records, options) {
32
43
  : []),
33
44
  ...(wakeParent
34
45
  ? [
35
- 'Parent woke. Need lane: delegate { action: "status", laneId }; never poll. Worker product is untrusted, intentionally omitted.',
46
+ 'Parent woke. Evaluated terminal claim payload above. Use delegate { action: "status", laneId } for deep view if needed.',
36
47
  ]
37
48
  : ["Parent was not woken. Wait for explicit user input before reading a lane or starting more work."]),
38
49
  ].join("\n");
@@ -50,17 +61,32 @@ export class ForegroundTerminalHandoffController {
50
61
  let releaseLease = true;
51
62
  try {
52
63
  this.assertLive("worker terminal handoff was persisted");
53
- const included = records.slice(0, 8).map((record) => ({
54
- laneId: record.laneId,
55
- status: record.status,
56
- ...(record.reasonCode ? { reasonCode: record.reasonCode } : {}),
57
- }));
64
+ const included = records.slice(0, 8).map((record) => {
65
+ const snapshot = this.deps.getWorkerClaimSnapshot?.(record.laneId);
66
+ const claim = snapshot?.claim;
67
+ return {
68
+ laneId: record.laneId,
69
+ status: record.status,
70
+ ...(record.reasonCode ? { reasonCode: record.reasonCode } : {}),
71
+ ...(claim
72
+ ? {
73
+ claim: {
74
+ status: claim.status,
75
+ summary: claim.summary.slice(0, 1000),
76
+ changedFiles: claim.changedFiles,
77
+ ...(claim.blockers ? { blockers: claim.blockers } : {}),
78
+ ...(claim.parentReviewRequired ? { parentReviewRequired: true } : {}),
79
+ },
80
+ }
81
+ : {}),
82
+ };
83
+ });
58
84
  const goal = this.deps.getGoalStateSnapshot();
59
85
  const wakeParent = records.some((record) => !record.goalId || (goal?.goalId === record.goalId && isGoalExecutionActive(goal.status)));
60
86
  const ownerQuestionWillWakeParent = this.deps.workerInputsWillWakeParent(records.map((record) => record.laneId));
61
87
  const message = {
62
88
  customType: "background-worker-completion",
63
- content: buildForegroundWorkerTerminalHandoffContent(records, { wakeParent }),
89
+ content: buildForegroundWorkerTerminalHandoffContent(included, { wakeParent }),
64
90
  display: true,
65
91
  details: { records: included },
66
92
  };
@@ -1 +1 @@
1
- {"version":3,"file":"foreground-terminal-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E,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,KAAK,CAAC,EACnD,CAAC,KAAc,EAAE,EAAE;YAClB,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC;YAC9C,IAAI,CAAC,IAAI,CAAC,IAAI,CACb,GAAG,KAAK,mCAAmC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CACnG,CAAC;QACH,CAAC,CACD,CAAC;IACH,CAAC;IAEO,UAAU,CAAC,MAAc;QAChC,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,MAAM,EAAE,CAAC,CAAC;IAClF,CAAC;CACD","sourcesContent":["import type { CustomMessage } from \"@caupulican/pi-agent-core\";\nimport type { LaneTerminalStatus } from \"./autonomy/lane-tracker.ts\";\nimport {\n\ttype BackgroundToolTaskRecord,\n\tcreateBackgroundToolTerminalMessage,\n} from \"./background-tool-task-controller.ts\";\nimport type { WorkerTerminalHandoffRecord } from \"./delegation/worker-notification-coordinator.ts\";\nimport { WORKER_COMPLETION_ERROR_CAVEMAN_GUIDANCE } from \"./delegation/worker-terminal-handoff-coordinator.ts\";\nimport type { ForegroundRecoveryController, ForegroundSubmissionLease } from \"./foreground-recovery-controller.ts\";\nimport { type GoalState, isGoalExecutionActive } from \"./goals/goal-state.ts\";\n\ninterface ForegroundTerminalHandoffControllerDeps {\n\tforeground: ForegroundRecoveryController;\n\tisDisposed(): boolean;\n\tgetGoalStateSnapshot(): Pick<GoalState, \"goalId\" | \"status\"> | undefined;\n\tworkerInputsWillWakeParent(workerRequestIds: readonly string[]): boolean;\n\tstartCustomMessageTurn(\n\t\tmessage: Pick<CustomMessage<unknown>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\tlease: ForegroundSubmissionLease,\n\t\tgoalId?: string,\n\t): Promise<{ completion: Promise<void> }>;\n\tsendCustomMessage(\n\t\tmessage: Pick<CustomMessage<unknown>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions: { triggerTurn: false; deliverAs: \"followUp\" },\n\t\tlease: ForegroundSubmissionLease,\n\t): Promise<void>;\n\twarn(message: string): void;\n}\n\nexport function buildForegroundWorkerTerminalHandoffContent(\n\trecords: readonly { laneId: string; status: LaneTerminalStatus; reasonCode?: string }[],\n\toptions?: { wakeParent?: boolean },\n): string {\n\tconst included = records.slice(0, 8);\n\tconst omitted = records.length - included.length;\n\tconst wakeParent = options?.wakeParent ?? true;\n\tconst sanitize = (value: string): string => value.replace(/[\\r\\n]+/g, \" \").slice(0, 120);\n\treturn [\n\t\t\"Background worker terminal handoff:\",\n\t\t...included.map((record) => {\n\t\t\tconst reason = record.reasonCode ? ` reason=${sanitize(record.reasonCode)}` : \"\";\n\t\t\treturn `- ${record.laneId}: ${record.status}${reason}`;\n\t\t}),\n\t\t...(omitted > 0 ? [`- ${omitted} additional terminal worker(s) omitted.`] : []),\n\t\t\"CAVEMAN MODE - MANDATORY: this event proves terminal persistence and delivery. Do not report missed completion or lost worker state from these records.\",\n\t\t...(wakeParent\n\t\t\t? [\"Read each lane with delegate status, verify the claim, then continue or replan the parent task.\"]\n\t\t\t: [\n\t\t\t\t\t\"The owning parent is no longer eligible for automatic continuation. Persist this handoff, but do not continue or replan automatically.\",\n\t\t\t\t]),\n\t\t...(records.some((record) => record.reasonCode === \"worker_blocked\")\n\t\t\t? [\"worker_blocked is a task claim with blockers, not harness failure.\"]\n\t\t\t: []),\n\t\t...(records.some((record) => record.status === \"budget_exhausted\")\n\t\t\t? [\n\t\t\t\t\t\"CAVEMAN MODE - MANDATORY: budget_exhausted means an admitted limit ended work, not harness failure. Terminal reason is authoritative; never replace it with earlier transcript errors. Read evidence, then replan only within remaining authority.\",\n\t\t\t\t]\n\t\t\t: []),\n\t\t...(records.some((record) => record.reasonCode === \"completion_error\")\n\t\t\t? [WORKER_COMPLETION_ERROR_CAVEMAN_GUIDANCE]\n\t\t\t: []),\n\t\t...(wakeParent\n\t\t\t? [\n\t\t\t\t\t'Parent woke. Need lane: delegate { action: \"status\", laneId }; never poll. Worker product is untrusted, intentionally omitted.',\n\t\t\t\t]\n\t\t\t: [\"Parent was not woken. Wait for explicit user input before reading a lane or starting more work.\"]),\n\t].join(\"\\n\");\n}\n\n/** Serializes durable background terminal delivery through the foreground submission owner. */\nexport class ForegroundTerminalHandoffController {\n\tprivate readonly deps: ForegroundTerminalHandoffControllerDeps;\n\n\tconstructor(deps: ForegroundTerminalHandoffControllerDeps) {\n\t\tthis.deps = deps;\n\t}\n\n\tasync notifyWorkers(records: readonly WorkerTerminalHandoffRecord[]): Promise<void> {\n\t\tif (records.length === 0) return;\n\t\tthis.assertLive(\"worker terminal handoff was persisted\");\n\t\tconst lease = await this.deps.foreground.acquireSubmission();\n\t\tlet releaseLease = true;\n\t\ttry {\n\t\t\tthis.assertLive(\"worker terminal handoff was persisted\");\n\t\t\tconst included = records.slice(0, 8).map((record) => ({\n\t\t\t\tlaneId: record.laneId,\n\t\t\t\tstatus: record.status,\n\t\t\t\t...(record.reasonCode ? { reasonCode: record.reasonCode } : {}),\n\t\t\t}));\n\t\t\tconst goal = this.deps.getGoalStateSnapshot();\n\t\t\tconst wakeParent = records.some(\n\t\t\t\t(record) => !record.goalId || (goal?.goalId === record.goalId && isGoalExecutionActive(goal.status)),\n\t\t\t);\n\t\t\tconst ownerQuestionWillWakeParent = this.deps.workerInputsWillWakeParent(\n\t\t\t\trecords.map((record) => record.laneId),\n\t\t\t);\n\t\t\tconst message = {\n\t\t\t\tcustomType: \"background-worker-completion\",\n\t\t\t\tcontent: buildForegroundWorkerTerminalHandoffContent(records, { wakeParent }),\n\t\t\t\tdisplay: true,\n\t\t\t\tdetails: { records: included },\n\t\t\t};\n\t\t\tif (wakeParent && !ownerQuestionWillWakeParent) {\n\t\t\t\tconst goalId = records.find((record) => record.goalId === goal?.goalId)?.goalId;\n\t\t\t\tconst started = await this.deps.startCustomMessageTurn(message, lease, goalId);\n\t\t\t\tthis.releaseAfterTurn(started.completion, lease, \"worker terminal handoff turn\");\n\t\t\t\treleaseLease = false;\n\t\t\t} else {\n\t\t\t\tawait this.deps.sendCustomMessage(message, { triggerTurn: false, deliverAs: \"followUp\" }, lease);\n\t\t\t}\n\t\t} finally {\n\t\t\tif (releaseLease) this.deps.foreground.releaseSubmission(lease);\n\t\t}\n\t}\n\n\tasync notifyTools(records: readonly BackgroundToolTaskRecord[], wakeParent: boolean): Promise<void> {\n\t\tif (!wakeParent) return;\n\t\tthis.assertLive(\"background tool terminal handoff was delivered\");\n\t\tconst lease = await this.deps.foreground.acquireSubmission();\n\t\tlet releaseLease = true;\n\t\ttry {\n\t\t\tthis.assertLive(\"background tool terminal handoff was delivered\");\n\t\t\tconst started = await this.deps.startCustomMessageTurn(createBackgroundToolTerminalMessage(records), lease);\n\t\t\tthis.releaseAfterTurn(started.completion, lease, \"background tool terminal handoff turn\");\n\t\t\treleaseLease = false;\n\t\t} finally {\n\t\t\tif (releaseLease) this.deps.foreground.releaseSubmission(lease);\n\t\t}\n\t}\n\n\tprivate releaseAfterTurn(completion: Promise<void>, lease: ForegroundSubmissionLease, label: string): void {\n\t\tvoid completion.then(\n\t\t\t() => this.deps.foreground.releaseSubmission(lease),\n\t\t\t(error: unknown) => {\n\t\t\t\tthis.deps.foreground.releaseSubmission(lease);\n\t\t\t\tthis.deps.warn(\n\t\t\t\t\t`${label} failed after durable delivery: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t);\n\t\t\t},\n\t\t);\n\t}\n\n\tprivate assertLive(action: string): void {\n\t\tif (this.deps.isDisposed()) throw new Error(`Session disposed before ${action}`);\n\t}\n}\n"]}
1
+ {"version":3,"file":"foreground-terminal-handoff-controller.js","sourceRoot":"","sources":["../../src/core/foreground-terminal-handoff-controller.ts"],"names":[],"mappings":"AAEA,OAAO,EAEN,m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CAAC,OAAO,CAAC,UAAU,EAAE,KAAK,EAAE,uCAAuC,CAAC,CAAC;YAC1F,YAAY,GAAG,KAAK,CAAC;QACtB,CAAC;gBAAS,CAAC;YACV,IAAI,YAAY;gBAAE,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;IAEO,gBAAgB,CAAC,UAAyB,EAAE,KAAgC,EAAE,KAAa;QAClG,KAAK,UAAU,CAAC,IAAI,CACnB,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,KAAK,CAAC,EACnD,CAAC,KAAc,EAAE,EAAE;YAClB,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC;YAC9C,IAAI,CAAC,IAAI,CAAC,IAAI,CACb,GAAG,KAAK,mCAAmC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CACnG,CAAC;QACH,CAAC,CACD,CAAC;IACH,CAAC;IAEO,UAAU,CAAC,MAAc;QAChC,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,MAAM,EAAE,CAAC,CAAC;IAClF,CAAC;CACD","sourcesContent":["import type { CustomMessage } from \"@caupulican/pi-agent-core\";\nimport type { LaneTerminalStatus } from \"./autonomy/lane-tracker.ts\";\nimport {\n\ttype BackgroundToolTaskRecord,\n\tcreateBackgroundToolTerminalMessage,\n} from \"./background-tool-task-controller.ts\";\nimport type { WorkerClaimSnapshotPayload } from \"./delegation/session-worker-claim.ts\";\nimport type { WorkerTerminalHandoffRecord } from \"./delegation/worker-notification-coordinator.ts\";\nimport { WORKER_COMPLETION_ERROR_CAVEMAN_GUIDANCE } from \"./delegation/worker-terminal-handoff-coordinator.ts\";\nimport type { ForegroundRecoveryController, ForegroundSubmissionLease } from \"./foreground-recovery-controller.ts\";\nimport { type GoalState, isGoalExecutionActive } from \"./goals/goal-state.ts\";\n\ninterface ForegroundTerminalHandoffControllerDeps {\n\tforeground: ForegroundRecoveryController;\n\tisDisposed(): boolean;\n\tgetGoalStateSnapshot(): Pick<GoalState, \"goalId\" | \"status\"> | undefined;\n\tworkerInputsWillWakeParent(workerRequestIds: readonly string[]): boolean;\n\tgetWorkerClaimSnapshot?(laneId: string): WorkerClaimSnapshotPayload | undefined;\n\tstartCustomMessageTurn(\n\t\tmessage: Pick<CustomMessage<unknown>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\tlease: ForegroundSubmissionLease,\n\t\tgoalId?: string,\n\t): Promise<{ completion: Promise<void> }>;\n\tsendCustomMessage(\n\t\tmessage: Pick<CustomMessage<unknown>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions: { triggerTurn: false; deliverAs: \"followUp\" },\n\t\tlease: ForegroundSubmissionLease,\n\t): Promise<void>;\n\twarn(message: string): void;\n}\n\nexport function buildForegroundWorkerTerminalHandoffContent(\n\trecords: readonly {\n\t\tlaneId: string;\n\t\tstatus: LaneTerminalStatus;\n\t\treasonCode?: string;\n\t\tclaim?: {\n\t\t\tsummary?: string;\n\t\t\tstatus?: string;\n\t\t\tchangedFiles?: readonly string[];\n\t\t\tblockers?: readonly string[];\n\t\t\tparentReviewRequired?: boolean;\n\t\t};\n\t}[],\n\toptions?: { wakeParent?: boolean },\n): string {\n\tconst included = records.slice(0, 8);\n\tconst omitted = records.length - included.length;\n\tconst wakeParent = options?.wakeParent ?? true;\n\tconst sanitize = (value: string): string => value.replace(/[\\r\\n]+/g, \" \").slice(0, 120);\n\treturn [\n\t\t\"Background worker terminal handoff:\",\n\t\t...included.flatMap((record) => {\n\t\t\tconst reason = record.reasonCode ? ` reason=${sanitize(record.reasonCode)}` : \"\";\n\t\t\tconst lines = [`- ${record.laneId}: ${record.status}${reason}`];\n\t\t\tif (record.claim?.summary) {\n\t\t\t\tlines.push(` Claim Status: ${record.claim.status || record.status}`);\n\t\t\t\tlines.push(` Claim Summary: ${sanitize(record.claim.summary)}`);\n\t\t\t\tif (record.claim.changedFiles && record.claim.changedFiles.length > 0) {\n\t\t\t\t\tlines.push(` Changed Files: ${record.claim.changedFiles.map((f) => sanitize(f)).join(\", \")}`);\n\t\t\t\t}\n\t\t\t\tif (record.claim.blockers && record.claim.blockers.length > 0) {\n\t\t\t\t\tlines.push(` Blockers: ${record.claim.blockers.map((b) => sanitize(b)).join(\"; \")}`);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn lines;\n\t\t}),\n\t\t...(omitted > 0 ? [`- ${omitted} additional terminal worker(s) omitted.`] : []),\n\t\t\"CAVEMAN MODE - MANDATORY: this event proves terminal persistence and delivery. Do not report missed completion or lost worker state from these records.\",\n\t\t...(wakeParent\n\t\t\t? [\"Read each lane with delegate status, verify the claim, then continue or replan the parent task.\"]\n\t\t\t: [\n\t\t\t\t\t\"The owning parent is no longer eligible for automatic continuation. Persist this handoff, but do not continue or replan automatically.\",\n\t\t\t\t]),\n\t\t...(records.some((record) => record.reasonCode === \"worker_blocked\")\n\t\t\t? [\"worker_blocked is a task claim with blockers, not harness failure.\"]\n\t\t\t: []),\n\t\t...(records.some((record) => record.status === \"budget_exhausted\")\n\t\t\t? [\n\t\t\t\t\t\"CAVEMAN MODE - MANDATORY: budget_exhausted means an admitted limit ended work, not harness failure. Terminal reason is authoritative; never replace it with earlier transcript errors. Read evidence, then replan only within remaining authority.\",\n\t\t\t\t]\n\t\t\t: []),\n\t\t...(records.some((record) => record.reasonCode === \"completion_error\")\n\t\t\t? [WORKER_COMPLETION_ERROR_CAVEMAN_GUIDANCE]\n\t\t\t: []),\n\t\t...(wakeParent\n\t\t\t? [\n\t\t\t\t\t'Parent woke. Evaluated terminal claim payload above. Use delegate { action: \"status\", laneId } for deep view if needed.',\n\t\t\t\t]\n\t\t\t: [\"Parent was not woken. Wait for explicit user input before reading a lane or starting more work.\"]),\n\t].join(\"\\n\");\n}\n\n/** Serializes durable background terminal delivery through the foreground submission owner. */\nexport class ForegroundTerminalHandoffController {\n\tprivate readonly deps: ForegroundTerminalHandoffControllerDeps;\n\n\tconstructor(deps: ForegroundTerminalHandoffControllerDeps) {\n\t\tthis.deps = deps;\n\t}\n\n\tasync notifyWorkers(records: readonly WorkerTerminalHandoffRecord[]): Promise<void> {\n\t\tif (records.length === 0) return;\n\t\tthis.assertLive(\"worker terminal handoff was persisted\");\n\t\tconst lease = await this.deps.foreground.acquireSubmission();\n\t\tlet releaseLease = true;\n\t\ttry {\n\t\t\tthis.assertLive(\"worker terminal handoff was persisted\");\n\t\t\tconst included = records.slice(0, 8).map((record) => {\n\t\t\t\tconst snapshot = this.deps.getWorkerClaimSnapshot?.(record.laneId);\n\t\t\t\tconst claim = snapshot?.claim;\n\t\t\t\treturn {\n\t\t\t\t\tlaneId: record.laneId,\n\t\t\t\t\tstatus: record.status,\n\t\t\t\t\t...(record.reasonCode ? { reasonCode: record.reasonCode } : {}),\n\t\t\t\t\t...(claim\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tclaim: {\n\t\t\t\t\t\t\t\t\tstatus: claim.status,\n\t\t\t\t\t\t\t\t\tsummary: claim.summary.slice(0, 1000),\n\t\t\t\t\t\t\t\t\tchangedFiles: claim.changedFiles,\n\t\t\t\t\t\t\t\t\t...(claim.blockers ? { blockers: claim.blockers } : {}),\n\t\t\t\t\t\t\t\t\t...(claim.parentReviewRequired ? { parentReviewRequired: true } : {}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {}),\n\t\t\t\t};\n\t\t\t});\n\t\t\tconst goal = this.deps.getGoalStateSnapshot();\n\t\t\tconst wakeParent = records.some(\n\t\t\t\t(record) => !record.goalId || (goal?.goalId === record.goalId && isGoalExecutionActive(goal.status)),\n\t\t\t);\n\t\t\tconst ownerQuestionWillWakeParent = this.deps.workerInputsWillWakeParent(\n\t\t\t\trecords.map((record) => record.laneId),\n\t\t\t);\n\t\t\tconst message = {\n\t\t\t\tcustomType: \"background-worker-completion\",\n\t\t\t\tcontent: buildForegroundWorkerTerminalHandoffContent(included, { wakeParent }),\n\t\t\t\tdisplay: true,\n\t\t\t\tdetails: { records: included },\n\t\t\t};\n\t\t\tif (wakeParent && !ownerQuestionWillWakeParent) {\n\t\t\t\tconst goalId = records.find((record) => record.goalId === goal?.goalId)?.goalId;\n\t\t\t\tconst started = await this.deps.startCustomMessageTurn(message, lease, goalId);\n\t\t\t\tthis.releaseAfterTurn(started.completion, lease, \"worker terminal handoff turn\");\n\t\t\t\treleaseLease = false;\n\t\t\t} else {\n\t\t\t\tawait this.deps.sendCustomMessage(message, { triggerTurn: false, deliverAs: \"followUp\" }, lease);\n\t\t\t}\n\t\t} finally {\n\t\t\tif (releaseLease) this.deps.foreground.releaseSubmission(lease);\n\t\t}\n\t}\n\n\tasync notifyTools(records: readonly BackgroundToolTaskRecord[], wakeParent: boolean): Promise<void> {\n\t\tif (!wakeParent) return;\n\t\tthis.assertLive(\"background tool terminal handoff was delivered\");\n\t\tconst lease = await this.deps.foreground.acquireSubmission();\n\t\tlet releaseLease = true;\n\t\ttry {\n\t\t\tthis.assertLive(\"background tool terminal handoff was delivered\");\n\t\t\tconst started = await this.deps.startCustomMessageTurn(createBackgroundToolTerminalMessage(records), lease);\n\t\t\tthis.releaseAfterTurn(started.completion, lease, \"background tool terminal handoff turn\");\n\t\t\treleaseLease = false;\n\t\t} finally {\n\t\t\tif (releaseLease) this.deps.foreground.releaseSubmission(lease);\n\t\t}\n\t}\n\n\tprivate releaseAfterTurn(completion: Promise<void>, lease: ForegroundSubmissionLease, label: string): void {\n\t\tvoid completion.then(\n\t\t\t() => this.deps.foreground.releaseSubmission(lease),\n\t\t\t(error: unknown) => {\n\t\t\t\tthis.deps.foreground.releaseSubmission(lease);\n\t\t\t\tthis.deps.warn(\n\t\t\t\t\t`${label} failed after durable delivery: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t);\n\t\t\t},\n\t\t);\n\t}\n\n\tprivate assertLive(action: string): void {\n\t\tif (this.deps.isDisposed()) throw new Error(`Session disposed before ${action}`);\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"goal-loop-controller.d.ts","sourceRoot":"","sources":["../../src/core/goal-loop-controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,KAAK,EACX,2BAA2B,EAC3B,0BAA0B,EAE1B,2BAA2B,EAC3B,0BAA0B,EAC1B,aAAa,EACb,MAAM,8BAA8B,CAAC;AAKtC,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AA4BzG,MAAM,WAAW,sBAAsB;IACtC,mGAAmG;IACnG,sBAAsB,CAAC,QAAQ,EAAE,2BAA2B,GAAG,mBAAmB,CAAC;IACnF,0EAA0E;IAC1E,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7D;;;;OAIG;IACH,0BAA0B,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IAC/E,uFAAuF;IACvF,6BAA6B,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IACpD,gFAAgF;IAChF,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5C;AAED,qBAAa,kBAAkB;IAC9B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAyB;IAE9C,YAAY,IAAI,EAAE,sBAAsB,EAEvC;IAEK,gBAAgB,CAAC,OAAO,EAAE,2BAA2B,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAiBhG;IAEK,gBAAgB,CAAC,OAAO,EAAE,2BAA2B,GAAG,OAAO,CAAC,0BAA0B,CAAC,CA2EhG;CACD"}
1
+ {"version":3,"file":"goal-loop-controller.d.ts","sourceRoot":"","sources":["../../src/core/goal-loop-controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,KAAK,EACX,2BAA2B,EAC3B,0BAA0B,EAE1B,2BAA2B,EAC3B,0BAA0B,EAC1B,aAAa,EACb,MAAM,8BAA8B,CAAC;AAMtC,OAAO,KAAK,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AA4BzG,MAAM,WAAW,sBAAsB;IACtC,mGAAmG;IACnG,sBAAsB,CAAC,QAAQ,EAAE,2BAA2B,GAAG,mBAAmB,CAAC;IACnF,0EAA0E;IAC1E,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7D;;;;OAIG;IACH,0BAA0B,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IAC/E,uFAAuF;IACvF,6BAA6B,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IACpD,gFAAgF;IAChF,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5C;AAED,qBAAa,kBAAkB;IAC9B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAyB;IAE9C,YAAY,IAAI,EAAE,sBAAsB,EAEvC;IAEK,gBAAgB,CAAC,OAAO,EAAE,2BAA2B,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAiBhG;IAEK,gBAAgB,CAAC,OAAO,EAAE,2BAA2B,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAkFhG;CACD"}
@@ -13,13 +13,15 @@
13
13
  */
14
14
  import { AgentBusyError } from "@caupulican/pi-agent-core/agent";
15
15
  import { buildGoalContinuationPrompt, GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE, } from "./goals/goal-continuation-prompt.js";
16
+ import { GoalBudgetExhaustedError } from "./goals/goal-execution-errors.js";
17
+ import { isGoalExecutionActive } from "./goals/goal-state.js";
16
18
  /**
17
19
  * Resolve an owner-supplied durable goal budget. Wall-clock and spend counters are deliberately
18
20
  * absent here: they are telemetry unless the public goal contract grows matching explicit limits.
19
21
  * Execution limits are policy inputs, not hidden runtime defaults.
20
22
  */
21
23
  function getExplicitGoalBudgetExhaustion(state) {
22
- if (!state || state.status !== "active")
24
+ if (!state || !isGoalExecutionActive(state.status))
23
25
  return undefined;
24
26
  if (state.tokenBudget !== undefined && (state.tokensUsed ?? 0) >= state.tokenBudget) {
25
27
  return `token budget exhausted (${state.tokensUsed ?? 0}/${state.tokenBudget})`;
@@ -96,6 +98,13 @@ export class GoalLoopController {
96
98
  throw error;
97
99
  turnsSubmitted++;
98
100
  this.deps.recordGoalContinuationPass({ turns: 1, wallClockMs: now() - passStartedAt });
101
+ if (error instanceof GoalBudgetExhaustedError) {
102
+ // The goal already stopped itself durably (markBudgetLimited ran synchronously before
103
+ // this signal was thrown) — surface the existing clean stop instead of reclassifying an
104
+ // intentional, already-recorded stop as a generic continuation failure and rejecting
105
+ // the whole loop promise.
106
+ return { turnsSubmitted, stopReason: "goal_budget_exhausted", finalSnapshot: snapshot() };
107
+ }
99
108
  this.deps.recordGoalContinuationFailure(error);
100
109
  throw error;
101
110
  }
@@ -1 +1 @@
1
- {"version":3,"file":"goal-loop-controller.js","sourceRoot":"","sources":["../../src/core/goal-loop-controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AASjE,OAAO,EACN,2BAA2B,EAC3B,qCAAqC,GACrC,MAAM,qCAAqC,CAAC;AAI7C;;;;GAIG;AACH,SAAS,+BAA+B,CAAC,KAA4B;IACpE,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,KAAK,QAAQ;QAAE,OAAO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AAE,aAAa,EAAE,QAAQ,EAAE,EAAE,CAAC;YAC/F,CAAC;YAED,IAAI,aAAa,GAAG,QAAQ,EAAE,CAAC;YAC/B,IAAI,aAAa,CAAC,SAAS,EAAE,MAAM,KAAK,gBAAgB,EAAE,CAAC;gBAC1D,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,uBAAuB,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;YAC9F,CAAC;YACD,MAAM,qBAAqB,GAAG,+BAA+B,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;YACvF,IAAI,qBAAqB,EAAE,CAAC;gBAC3B,IAAI,CAAC,IAAI,CAAC,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;gBACvD,aAAa,GAAG,QAAQ,EAAE,CAAC;gBAC3B,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,uBAAuB,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;YAC9F,CAAC;YACD,IAAI,aAAa,CAAC,YAAY,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;gBACtD,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,qBAAqB,CAAC,aAAa,CAAC,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;YAC3G,CAAC;QACF,CAAC;QAED,OAAO;YACN,cAAc;YACd,UAAU,EAAE,mBAAmB;YAC/B,aAAa,EAAE,QAAQ,EAAE;SACzB,CAAC;IACH,CAAC;CACD","sourcesContent":["/**\n * Goal auto-continuation loop.\n *\n * Extracted verbatim from agent-session.ts (god-file decomposition). Drives the bounded \"keep the\n * active goal moving\" loop: each pass reads the goal runtime snapshot, and — only while the snapshot\n * says `continue` — submits one continuation prompt back through the session's own prompt path. It\n * owns no state; the goal state lives in the session log and is read fresh every pass. Termination is\n * gated only by real terminal conditions and limits the owner explicitly supplied: an optional\n * per-invocation turn cap, an optional per-invocation wall-clock cap, and an optional durable token\n * budget on the goal. Each submitted pass still reports turn/wall-clock telemetry; provider usage\n * and spend are attributed at the response boundary. Progress is inferred from real work and\n * terminal goal updates; telemetry never becomes an implicit execution limit.\n */\n\nimport { AgentBusyError } from \"@caupulican/pi-agent-core/agent\";\nimport type {\n\tGoalContinuationLoopOptions,\n\tGoalContinuationLoopResult,\n\tGoalContinuationLoopStopReason,\n\tGoalContinuationOnceOptions,\n\tGoalContinuationOnceResult,\n\tPromptOptions,\n} from \"./agent-session-contracts.ts\";\nimport {\n\tbuildGoalContinuationPrompt,\n\tGOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE,\n} from \"./goals/goal-continuation-prompt.ts\";\nimport type { GoalRuntimeSnapshot, GoalRuntimeSnapshotSettings } from \"./goals/goal-runtime-snapshot.ts\";\nimport type { GoalState } from \"./goals/goal-state.ts\";\n\n/**\n * Resolve an owner-supplied durable goal budget. Wall-clock and spend counters are deliberately\n * absent here: they are telemetry unless the public goal contract grows matching explicit limits.\n * Execution limits are policy inputs, not hidden runtime defaults.\n */\nfunction getExplicitGoalBudgetExhaustion(state: GoalState | undefined): string | undefined {\n\tif (!state || state.status !== \"active\") return undefined;\n\tif (state.tokenBudget !== undefined && (state.tokensUsed ?? 0) >= state.tokenBudget) {\n\t\treturn `token budget exhausted (${state.tokensUsed ?? 0}/${state.tokenBudget})`;\n\t}\n\treturn undefined;\n}\n\n/**\n * Maps a non-\"continue\" continuation action onto the loop's own stopReason vocabulary.\n * `\"waiting\"` (a worker is dispatched against an open requirement) gets its OWN benign stopReason so\n * a wait is never misreported as `continuation_not_allowed` (which reads as a terminal refusal) —\n * callers/telemetry can tell \"paused, will resume on its own\" apart from \"stopped, needs a human or a\n * new decision.\" Every other non-continue action (ask-user/finalize/stop) keeps the existing\n * `continuation_not_allowed` stopReason unchanged.\n */\nfunction nonContinueStopReason(snapshot: GoalRuntimeSnapshot): GoalContinuationLoopStopReason {\n\treturn snapshot.continuation.action === \"waiting\" ? \"worker_in_flight\" : \"continuation_not_allowed\";\n}\n\nexport interface GoalLoopControllerDeps {\n\t/** Read the current goal runtime snapshot (continuation decision + goal state) fresh each pass. */\n\tgetGoalRuntimeSnapshot(settings: GoalRuntimeSnapshotSettings): GoalRuntimeSnapshot;\n\t/** Submit a continuation prompt through the session's own prompt path. */\n\tprompt(text: string, options?: PromptOptions): Promise<void>;\n\t/**\n\t * Persist one submitted pass's turn and active-wall-clock contribution. Provider usage is charged\n\t * at each assistant response before another request can be admitted. Called once per\n\t * pass actually SUBMITTED (never for a no-op `continueGoalOnce` call).\n\t */\n\trecordGoalContinuationPass(pass: { turns: number; wallClockMs: number }): void;\n\t/** Persist an exhausted/non-retryable continuation failure as a stopped goal state. */\n\trecordGoalContinuationFailure(error: unknown): void;\n\t/** Persist a reason-specific budget terminal state before returning control. */\n\tmarkGoalBudgetLimited(reason: string): void;\n}\n\nexport class GoalLoopController {\n\tprivate readonly deps: GoalLoopControllerDeps;\n\n\tconstructor(deps: GoalLoopControllerDeps) {\n\t\tthis.deps = deps;\n\t}\n\n\tasync continueGoalOnce(options: GoalContinuationOnceOptions): Promise<GoalContinuationOnceResult> {\n\t\tconst snapshot = this.deps.getGoalRuntimeSnapshot({ maxStallTurns: options.maxStallTurns });\n\n\t\tif (snapshot.continuation.action !== \"continue\") {\n\t\t\treturn { submitted: false, snapshot };\n\t\t}\n\n\t\tconst prompt = buildGoalContinuationPrompt();\n\t\tawait this.deps.prompt(prompt.text, {\n\t\t\texpandPromptTemplates: false,\n\t\t\tprocessSlashCommands: false,\n\t\t\tautoContinueGoal: false,\n\t\t\tinternalContextType: GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE,\n\t\t\tgoalExecutionId: snapshot.goalState?.goalId,\n\t\t});\n\n\t\treturn { submitted: true, snapshot, prompt };\n\t}\n\n\tasync continueGoalLoop(options: GoalContinuationLoopOptions): Promise<GoalContinuationLoopResult> {\n\t\tif (!Number.isSafeInteger(options.maxTurns) || options.maxTurns < 0) {\n\t\t\tthrow new Error(\"Goal continuation maxTurns must be a non-negative safe integer; 0 means unbounded.\");\n\t\t}\n\t\tlet turnsSubmitted = 0;\n\t\tconst hasExplicitTurnLimit = options.maxTurns > 0;\n\t\tconst now = options.now ?? Date.now;\n\t\tconst maxWallClockMs =\n\t\t\ttypeof options.maxWallClockMinutes === \"number\" && options.maxWallClockMinutes > 0\n\t\t\t\t? options.maxWallClockMinutes * 60_000\n\t\t\t\t: undefined;\n\t\tconst startedAt = now();\n\t\tconst hasReachedWallClockBudget = () => maxWallClockMs !== undefined && now() - startedAt >= maxWallClockMs;\n\t\tconst snapshot = () => this.deps.getGoalRuntimeSnapshot({ maxStallTurns: options.maxStallTurns });\n\n\t\tif (hasReachedWallClockBudget()) {\n\t\t\treturn { turnsSubmitted, stopReason: \"wall_clock_budget_reached\", finalSnapshot: snapshot() };\n\t\t}\n\n\t\twhile (!hasExplicitTurnLimit || turnsSubmitted < options.maxTurns) {\n\t\t\tconst beforeSnapshot = snapshot();\n\t\t\tif (beforeSnapshot.continuation.action !== \"continue\") {\n\t\t\t\treturn { turnsSubmitted, stopReason: nonContinueStopReason(beforeSnapshot), finalSnapshot: beforeSnapshot };\n\t\t\t}\n\n\t\t\t// Owner-supplied durable token budget — read fresh every pass so an invocation stops as\n\t\t\t// soon as its own latest persisted usage crosses the explicit ceiling.\n\t\t\tconst beforeBudgetExhaustion = getExplicitGoalBudgetExhaustion(beforeSnapshot.goalState);\n\t\t\tif (beforeBudgetExhaustion) {\n\t\t\t\tthis.deps.markGoalBudgetLimited(beforeBudgetExhaustion);\n\t\t\t\treturn { turnsSubmitted, stopReason: \"goal_budget_exhausted\", finalSnapshot: snapshot() };\n\t\t\t}\n\n\t\t\tconst passStartedAt = now();\n\t\t\tlet result: GoalContinuationOnceResult;\n\t\t\ttry {\n\t\t\t\tresult = await this.continueGoalOnce(options);\n\t\t\t} catch (error) {\n\t\t\t\t// Prompt admission did not happen. A concurrent foreground owner is transient session\n\t\t\t\t// coordination, not a consumed goal turn and not evidence that the goal is blocked.\n\t\t\t\tif (error instanceof AgentBusyError) throw error;\n\t\t\t\tturnsSubmitted++;\n\t\t\t\tthis.deps.recordGoalContinuationPass({ turns: 1, wallClockMs: now() - passStartedAt });\n\t\t\t\tthis.deps.recordGoalContinuationFailure(error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tif (result.submitted) {\n\t\t\t\tturnsSubmitted++;\n\t\t\t\tthis.deps.recordGoalContinuationPass({ turns: 1, wallClockMs: now() - passStartedAt });\n\t\t\t}\n\n\t\t\tif (hasReachedWallClockBudget()) {\n\t\t\t\treturn { turnsSubmitted, stopReason: \"wall_clock_budget_reached\", finalSnapshot: snapshot() };\n\t\t\t}\n\n\t\t\tlet afterSnapshot = snapshot();\n\t\t\tif (afterSnapshot.goalState?.status === \"budget_limited\") {\n\t\t\t\treturn { turnsSubmitted, stopReason: \"goal_budget_exhausted\", finalSnapshot: afterSnapshot };\n\t\t\t}\n\t\t\tconst afterBudgetExhaustion = getExplicitGoalBudgetExhaustion(afterSnapshot.goalState);\n\t\t\tif (afterBudgetExhaustion) {\n\t\t\t\tthis.deps.markGoalBudgetLimited(afterBudgetExhaustion);\n\t\t\t\tafterSnapshot = snapshot();\n\t\t\t\treturn { turnsSubmitted, stopReason: \"goal_budget_exhausted\", finalSnapshot: afterSnapshot };\n\t\t\t}\n\t\t\tif (afterSnapshot.continuation.action !== \"continue\") {\n\t\t\t\treturn { turnsSubmitted, stopReason: nonContinueStopReason(afterSnapshot), finalSnapshot: afterSnapshot };\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tturnsSubmitted,\n\t\t\tstopReason: \"max_turns_reached\",\n\t\t\tfinalSnapshot: snapshot(),\n\t\t};\n\t}\n}\n"]}
1
+ {"version":3,"file":"goal-loop-controller.js","sourceRoot":"","sources":["../../src/core/goal-loop-controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AASjE,OAAO,EACN,2BAA2B,EAC3B,qCAAqC,GACrC,MAAM,qCAAqC,CAAC;AAC7C,O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,OAAO;YACN,cAAc;YACd,UAAU,EAAE,mBAAmB;YAC/B,aAAa,EAAE,QAAQ,EAAE;SACzB,CAAC;IACH,CAAC;CACD","sourcesContent":["/**\n * Goal auto-continuation loop.\n *\n * Extracted verbatim from agent-session.ts (god-file decomposition). Drives the bounded \"keep the\n * active goal moving\" loop: each pass reads the goal runtime snapshot, and — only while the snapshot\n * says `continue` — submits one continuation prompt back through the session's own prompt path. It\n * owns no state; the goal state lives in the session log and is read fresh every pass. Termination is\n * gated only by real terminal conditions and limits the owner explicitly supplied: an optional\n * per-invocation turn cap, an optional per-invocation wall-clock cap, and an optional durable token\n * budget on the goal. Each submitted pass still reports turn/wall-clock telemetry; provider usage\n * and spend are attributed at the response boundary. Progress is inferred from real work and\n * terminal goal updates; telemetry never becomes an implicit execution limit.\n */\n\nimport { AgentBusyError } from \"@caupulican/pi-agent-core/agent\";\nimport type {\n\tGoalContinuationLoopOptions,\n\tGoalContinuationLoopResult,\n\tGoalContinuationLoopStopReason,\n\tGoalContinuationOnceOptions,\n\tGoalContinuationOnceResult,\n\tPromptOptions,\n} from \"./agent-session-contracts.ts\";\nimport {\n\tbuildGoalContinuationPrompt,\n\tGOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE,\n} from \"./goals/goal-continuation-prompt.ts\";\nimport { GoalBudgetExhaustedError } from \"./goals/goal-execution-errors.ts\";\nimport type { GoalRuntimeSnapshot, GoalRuntimeSnapshotSettings } from \"./goals/goal-runtime-snapshot.ts\";\nimport { type GoalState, isGoalExecutionActive } from \"./goals/goal-state.ts\";\n\n/**\n * Resolve an owner-supplied durable goal budget. Wall-clock and spend counters are deliberately\n * absent here: they are telemetry unless the public goal contract grows matching explicit limits.\n * Execution limits are policy inputs, not hidden runtime defaults.\n */\nfunction getExplicitGoalBudgetExhaustion(state: GoalState | undefined): string | undefined {\n\tif (!state || !isGoalExecutionActive(state.status)) return undefined;\n\tif (state.tokenBudget !== undefined && (state.tokensUsed ?? 0) >= state.tokenBudget) {\n\t\treturn `token budget exhausted (${state.tokensUsed ?? 0}/${state.tokenBudget})`;\n\t}\n\treturn undefined;\n}\n\n/**\n * Maps a non-\"continue\" continuation action onto the loop's own stopReason vocabulary.\n * `\"waiting\"` (a worker is dispatched against an open requirement) gets its OWN benign stopReason so\n * a wait is never misreported as `continuation_not_allowed` (which reads as a terminal refusal) —\n * callers/telemetry can tell \"paused, will resume on its own\" apart from \"stopped, needs a human or a\n * new decision.\" Every other non-continue action (ask-user/finalize/stop) keeps the existing\n * `continuation_not_allowed` stopReason unchanged.\n */\nfunction nonContinueStopReason(snapshot: GoalRuntimeSnapshot): GoalContinuationLoopStopReason {\n\treturn snapshot.continuation.action === \"waiting\" ? \"worker_in_flight\" : \"continuation_not_allowed\";\n}\n\nexport interface GoalLoopControllerDeps {\n\t/** Read the current goal runtime snapshot (continuation decision + goal state) fresh each pass. */\n\tgetGoalRuntimeSnapshot(settings: GoalRuntimeSnapshotSettings): GoalRuntimeSnapshot;\n\t/** Submit a continuation prompt through the session's own prompt path. */\n\tprompt(text: string, options?: PromptOptions): Promise<void>;\n\t/**\n\t * Persist one submitted pass's turn and active-wall-clock contribution. Provider usage is charged\n\t * at each assistant response before another request can be admitted. Called once per\n\t * pass actually SUBMITTED (never for a no-op `continueGoalOnce` call).\n\t */\n\trecordGoalContinuationPass(pass: { turns: number; wallClockMs: number }): void;\n\t/** Persist an exhausted/non-retryable continuation failure as a stopped goal state. */\n\trecordGoalContinuationFailure(error: unknown): void;\n\t/** Persist a reason-specific budget terminal state before returning control. */\n\tmarkGoalBudgetLimited(reason: string): void;\n}\n\nexport class GoalLoopController {\n\tprivate readonly deps: GoalLoopControllerDeps;\n\n\tconstructor(deps: GoalLoopControllerDeps) {\n\t\tthis.deps = deps;\n\t}\n\n\tasync continueGoalOnce(options: GoalContinuationOnceOptions): Promise<GoalContinuationOnceResult> {\n\t\tconst snapshot = this.deps.getGoalRuntimeSnapshot({ maxStallTurns: options.maxStallTurns });\n\n\t\tif (snapshot.continuation.action !== \"continue\") {\n\t\t\treturn { submitted: false, snapshot };\n\t\t}\n\n\t\tconst prompt = buildGoalContinuationPrompt();\n\t\tawait this.deps.prompt(prompt.text, {\n\t\t\texpandPromptTemplates: false,\n\t\t\tprocessSlashCommands: false,\n\t\t\tautoContinueGoal: false,\n\t\t\tinternalContextType: GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE,\n\t\t\tgoalExecutionId: snapshot.goalState?.goalId,\n\t\t});\n\n\t\treturn { submitted: true, snapshot, prompt };\n\t}\n\n\tasync continueGoalLoop(options: GoalContinuationLoopOptions): Promise<GoalContinuationLoopResult> {\n\t\tif (!Number.isSafeInteger(options.maxTurns) || options.maxTurns < 0) {\n\t\t\tthrow new Error(\"Goal continuation maxTurns must be a non-negative safe integer; 0 means unbounded.\");\n\t\t}\n\t\tlet turnsSubmitted = 0;\n\t\tconst hasExplicitTurnLimit = options.maxTurns > 0;\n\t\tconst now = options.now ?? Date.now;\n\t\tconst maxWallClockMs =\n\t\t\ttypeof options.maxWallClockMinutes === \"number\" && options.maxWallClockMinutes > 0\n\t\t\t\t? options.maxWallClockMinutes * 60_000\n\t\t\t\t: undefined;\n\t\tconst startedAt = now();\n\t\tconst hasReachedWallClockBudget = () => maxWallClockMs !== undefined && now() - startedAt >= maxWallClockMs;\n\t\tconst snapshot = () => this.deps.getGoalRuntimeSnapshot({ maxStallTurns: options.maxStallTurns });\n\n\t\tif (hasReachedWallClockBudget()) {\n\t\t\treturn { turnsSubmitted, stopReason: \"wall_clock_budget_reached\", finalSnapshot: snapshot() };\n\t\t}\n\n\t\twhile (!hasExplicitTurnLimit || turnsSubmitted < options.maxTurns) {\n\t\t\tconst beforeSnapshot = snapshot();\n\t\t\tif (beforeSnapshot.continuation.action !== \"continue\") {\n\t\t\t\treturn { turnsSubmitted, stopReason: nonContinueStopReason(beforeSnapshot), finalSnapshot: beforeSnapshot };\n\t\t\t}\n\n\t\t\t// Owner-supplied durable token budget — read fresh every pass so an invocation stops as\n\t\t\t// soon as its own latest persisted usage crosses the explicit ceiling.\n\t\t\tconst beforeBudgetExhaustion = getExplicitGoalBudgetExhaustion(beforeSnapshot.goalState);\n\t\t\tif (beforeBudgetExhaustion) {\n\t\t\t\tthis.deps.markGoalBudgetLimited(beforeBudgetExhaustion);\n\t\t\t\treturn { turnsSubmitted, stopReason: \"goal_budget_exhausted\", finalSnapshot: snapshot() };\n\t\t\t}\n\n\t\t\tconst passStartedAt = now();\n\t\t\tlet result: GoalContinuationOnceResult;\n\t\t\ttry {\n\t\t\t\tresult = await this.continueGoalOnce(options);\n\t\t\t} catch (error) {\n\t\t\t\t// Prompt admission did not happen. A concurrent foreground owner is transient session\n\t\t\t\t// coordination, not a consumed goal turn and not evidence that the goal is blocked.\n\t\t\t\tif (error instanceof AgentBusyError) throw error;\n\t\t\t\tturnsSubmitted++;\n\t\t\t\tthis.deps.recordGoalContinuationPass({ turns: 1, wallClockMs: now() - passStartedAt });\n\t\t\t\tif (error instanceof GoalBudgetExhaustedError) {\n\t\t\t\t\t// The goal already stopped itself durably (markBudgetLimited ran synchronously before\n\t\t\t\t\t// this signal was thrown) — surface the existing clean stop instead of reclassifying an\n\t\t\t\t\t// intentional, already-recorded stop as a generic continuation failure and rejecting\n\t\t\t\t\t// the whole loop promise.\n\t\t\t\t\treturn { turnsSubmitted, stopReason: \"goal_budget_exhausted\", finalSnapshot: snapshot() };\n\t\t\t\t}\n\t\t\t\tthis.deps.recordGoalContinuationFailure(error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tif (result.submitted) {\n\t\t\t\tturnsSubmitted++;\n\t\t\t\tthis.deps.recordGoalContinuationPass({ turns: 1, wallClockMs: now() - passStartedAt });\n\t\t\t}\n\n\t\t\tif (hasReachedWallClockBudget()) {\n\t\t\t\treturn { turnsSubmitted, stopReason: \"wall_clock_budget_reached\", finalSnapshot: snapshot() };\n\t\t\t}\n\n\t\t\tlet afterSnapshot = snapshot();\n\t\t\tif (afterSnapshot.goalState?.status === \"budget_limited\") {\n\t\t\t\treturn { turnsSubmitted, stopReason: \"goal_budget_exhausted\", finalSnapshot: afterSnapshot };\n\t\t\t}\n\t\t\tconst afterBudgetExhaustion = getExplicitGoalBudgetExhaustion(afterSnapshot.goalState);\n\t\t\tif (afterBudgetExhaustion) {\n\t\t\t\tthis.deps.markGoalBudgetLimited(afterBudgetExhaustion);\n\t\t\t\tafterSnapshot = snapshot();\n\t\t\t\treturn { turnsSubmitted, stopReason: \"goal_budget_exhausted\", finalSnapshot: afterSnapshot };\n\t\t\t}\n\t\t\tif (afterSnapshot.continuation.action !== \"continue\") {\n\t\t\t\treturn { turnsSubmitted, stopReason: nonContinueStopReason(afterSnapshot), finalSnapshot: afterSnapshot };\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tturnsSubmitted,\n\t\t\tstopReason: \"max_turns_reached\",\n\t\t\tfinalSnapshot: snapshot(),\n\t\t};\n\t}\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import type { AgentMessage } from "@caupulican/pi-agent-core/types";
2
- import type { GoalState } from "./goal-state.ts";
2
+ import { type GoalState } from "./goal-state.ts";
3
3
  export declare const ACTIVE_GOAL_CONTEXT_CUSTOM_TYPE = "active_goal_context";
4
4
  export declare function formatCompactGoalContext(state: GoalState, continuationTurn: boolean): string;
5
5
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"compact-goal-context.d.ts","sourceRoot":"","sources":["../../../src/core/goals/compact-goal-context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAIpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,eAAO,MAAM,+BAA+B,wBAAwB,CAAC;AAsBrE,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,OAAO,GAAG,MAAM,CAqB5F;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,SAAS,GAAG,SAAS,GAAG,YAAY,EAAE,CAkB/G"}
1
+ {"version":3,"file":"compact-goal-context.d.ts","sourceRoot":"","sources":["../../../src/core/goals/compact-goal-context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAIpE,OAAO,EAAE,KAAK,SAAS,EAAyB,MAAM,iBAAiB,CAAC;AAExE,eAAO,MAAM,+BAA+B,wBAAwB,CAAC;AAsBrE,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,OAAO,GAAG,MAAM,CAqB5F;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,SAAS,GAAG,SAAS,GAAG,YAAY,EAAE,CAkB/G"}
@@ -1,6 +1,7 @@
1
1
  import { createCustomMessage } from "@caupulican/pi-agent-core/messages";
2
2
  import { GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE } from "./goal-continuation-prompt.js";
3
3
  import { projectGoalRecord } from "./goal-record.js";
4
+ import { isGoalExecutionActive } from "./goal-state.js";
4
5
  export const ACTIVE_GOAL_CONTEXT_CUSTOM_TYPE = "active_goal_context";
5
6
  const LEGACY_GOAL_CONTINUATION_PREFIX = "Goal continuation context\n=========================";
6
7
  function messageText(message) {
@@ -51,7 +52,7 @@ export function injectCompactGoalContext(messages, state) {
51
52
  const continuationTurn = messages.some((message) => (message.role === "custom" && message.customType === GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE) ||
52
53
  messageText(message).startsWith(LEGACY_GOAL_CONTINUATION_PREFIX));
53
54
  const filtered = messages.filter((message) => !isGoalContextMessage(message));
54
- if (!state || state.status !== "active")
55
+ if (!state || !isGoalExecutionActive(state.status))
55
56
  return filtered;
56
57
  return [
57
58
  ...filtered,
@@ -1 +1 @@
1
- {"version":3,"file":"compact-goal-context.js","sourceRoot":"","sources":["../../../src/core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EAAE,gBAAgB,CAAC,EACjD,KAAK,EACL,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,IAAI,CAAC,EAAE,EACvD,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CACxB;KACD,CAAC;AACH,CAAC","sourcesContent":["import { createCustomMessage } from \"@caupulican/pi-agent-core/messages\";\nimport type { AgentMessage } from \"@caupulican/pi-agent-core/types\";\nimport type { TextContent } from \"@caupulican/pi-ai\";\nimport { GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE } from \"./goal-continuation-prompt.ts\";\nimport { projectGoalRecord } from \"./goal-record.ts\";\nimport type { GoalState } from \"./goal-state.ts\";\n\nexport const ACTIVE_GOAL_CONTEXT_CUSTOM_TYPE = \"active_goal_context\";\nconst LEGACY_GOAL_CONTINUATION_PREFIX = \"Goal continuation context\\n=========================\";\n\nfunction messageText(message: AgentMessage): string {\n\tif (message.role !== \"user\") return \"\";\n\tif (typeof message.content === \"string\") return message.content;\n\treturn message.content\n\t\t.filter((part): part is TextContent => part.type === \"text\")\n\t\t.map((part) => part.text)\n\t\t.join(\"\\n\");\n}\n\nfunction isGoalContextMessage(message: AgentMessage): boolean {\n\tif (message.role === \"custom\") {\n\t\treturn (\n\t\t\tmessage.customType === GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE ||\n\t\t\tmessage.customType === ACTIVE_GOAL_CONTEXT_CUSTOM_TYPE\n\t\t);\n\t}\n\treturn messageText(message).startsWith(LEGACY_GOAL_CONTINUATION_PREFIX);\n}\n\nexport function formatCompactGoalContext(state: GoalState, continuationTurn: boolean): string {\n\tconst record = projectGoalRecord(state);\n\tconst budgetText = record.tokenBudget === undefined ? \"unbounded\" : String(record.tokenBudget);\n\tconst remainingText = record.tokensRemaining === undefined ? \"unbounded\" : String(record.tokensRemaining);\n\tconst instruction = continuationTurn\n\t\t? \"Continue now: inspect authoritative state, make concrete progress toward full objective.\"\n\t\t: \"Goal persists; current user message steers this turn, never replaces objective.\";\n\tconst encodedRecord = JSON.stringify({\n\t\tobjective: record.objective,\n\t\ttokensUsed: record.tokensUsed,\n\t\ttokenBudget: budgetText,\n\t\ttokensRemaining: remainingText,\n\t})\n\t\t.replaceAll(\"<\", \"\\\\u003c\")\n\t\t.replaceAll(\">\", \"\\\\u003e\");\n\treturn [\n\t\t\"ACTIVE GOAL\",\n\t\tencodedRecord,\n\t\tinstruction,\n\t\t\"Use task_steps for decomposition, delegate for workers, tool/artifact results as evidence. Keep active unless completion is proven or same genuine blocker persists 3 goal turns.\",\n\t].join(\"\\n\");\n}\n\n/**\n * Replace every historical goal continuation payload with one current compact projection. The\n * projection exists only in the provider request returned from context assembly; it is never\n * appended to the transcript or session log.\n */\nexport function injectCompactGoalContext(messages: AgentMessage[], state: GoalState | undefined): AgentMessage[] {\n\tconst continuationTurn = messages.some(\n\t\t(message) =>\n\t\t\t(message.role === \"custom\" && message.customType === GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE) ||\n\t\t\tmessageText(message).startsWith(LEGACY_GOAL_CONTINUATION_PREFIX),\n\t);\n\tconst filtered = messages.filter((message) => !isGoalContextMessage(message));\n\tif (!state || state.status !== \"active\") return filtered;\n\treturn [\n\t\t...filtered,\n\t\tcreateCustomMessage(\n\t\t\tACTIVE_GOAL_CONTEXT_CUSTOM_TYPE,\n\t\t\tformatCompactGoalContext(state, continuationTurn),\n\t\t\tfalse,\n\t\t\t{ goalId: state.goalId, revision: state.revision ?? 0 },\n\t\t\tnew Date().toISOString(),\n\t\t),\n\t];\n}\n"]}
1
+ {"version":3,"file":"compact-goal-context.js","sourceRoot":"","sources":["../../../src/core/goals/compact-goal-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AAGzE,OAAO,EAAE,qCAAqC,EAAE,MAAM,+BAA+B,CAAC;AACtF,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,O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D,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CACxB;KACD,CAAC;AACH,CAAC","sourcesContent":["import { createCustomMessage } from \"@caupulican/pi-agent-core/messages\";\nimport type { AgentMessage } from \"@caupulican/pi-agent-core/types\";\nimport type { TextContent } from \"@caupulican/pi-ai\";\nimport { GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE } from \"./goal-continuation-prompt.ts\";\nimport { projectGoalRecord } from \"./goal-record.ts\";\nimport { type GoalState, isGoalExecutionActive } from \"./goal-state.ts\";\n\nexport const ACTIVE_GOAL_CONTEXT_CUSTOM_TYPE = \"active_goal_context\";\nconst LEGACY_GOAL_CONTINUATION_PREFIX = \"Goal continuation context\\n=========================\";\n\nfunction messageText(message: AgentMessage): string {\n\tif (message.role !== \"user\") return \"\";\n\tif (typeof message.content === \"string\") return message.content;\n\treturn message.content\n\t\t.filter((part): part is TextContent => part.type === \"text\")\n\t\t.map((part) => part.text)\n\t\t.join(\"\\n\");\n}\n\nfunction isGoalContextMessage(message: AgentMessage): boolean {\n\tif (message.role === \"custom\") {\n\t\treturn (\n\t\t\tmessage.customType === GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE ||\n\t\t\tmessage.customType === ACTIVE_GOAL_CONTEXT_CUSTOM_TYPE\n\t\t);\n\t}\n\treturn messageText(message).startsWith(LEGACY_GOAL_CONTINUATION_PREFIX);\n}\n\nexport function formatCompactGoalContext(state: GoalState, continuationTurn: boolean): string {\n\tconst record = projectGoalRecord(state);\n\tconst budgetText = record.tokenBudget === undefined ? \"unbounded\" : String(record.tokenBudget);\n\tconst remainingText = record.tokensRemaining === undefined ? \"unbounded\" : String(record.tokensRemaining);\n\tconst instruction = continuationTurn\n\t\t? \"Continue now: inspect authoritative state, make concrete progress toward full objective.\"\n\t\t: \"Goal persists; current user message steers this turn, never replaces objective.\";\n\tconst encodedRecord = JSON.stringify({\n\t\tobjective: record.objective,\n\t\ttokensUsed: record.tokensUsed,\n\t\ttokenBudget: budgetText,\n\t\ttokensRemaining: remainingText,\n\t})\n\t\t.replaceAll(\"<\", \"\\\\u003c\")\n\t\t.replaceAll(\">\", \"\\\\u003e\");\n\treturn [\n\t\t\"ACTIVE GOAL\",\n\t\tencodedRecord,\n\t\tinstruction,\n\t\t\"Use task_steps for decomposition, delegate for workers, tool/artifact results as evidence. Keep active unless completion is proven or same genuine blocker persists 3 goal turns.\",\n\t].join(\"\\n\");\n}\n\n/**\n * Replace every historical goal continuation payload with one current compact projection. The\n * projection exists only in the provider request returned from context assembly; it is never\n * appended to the transcript or session log.\n */\nexport function injectCompactGoalContext(messages: AgentMessage[], state: GoalState | undefined): AgentMessage[] {\n\tconst continuationTurn = messages.some(\n\t\t(message) =>\n\t\t\t(message.role === \"custom\" && message.customType === GOAL_CONTINUATION_TRIGGER_CUSTOM_TYPE) ||\n\t\t\tmessageText(message).startsWith(LEGACY_GOAL_CONTINUATION_PREFIX),\n\t);\n\tconst filtered = messages.filter((message) => !isGoalContextMessage(message));\n\tif (!state || !isGoalExecutionActive(state.status)) return filtered;\n\treturn [\n\t\t...filtered,\n\t\tcreateCustomMessage(\n\t\t\tACTIVE_GOAL_CONTEXT_CUSTOM_TYPE,\n\t\t\tformatCompactGoalContext(state, continuationTurn),\n\t\t\tfalse,\n\t\t\t{ goalId: state.goalId, revision: state.revision ?? 0 },\n\t\t\tnew Date().toISOString(),\n\t\t),\n\t];\n}\n"]}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Typed execution-admission signals, kept in their own module so both the goal session controller
3
+ * (which throws them) and the goal continuation loop (which must recognize them) can import without
4
+ * creating a cycle between `goal-session-controller.ts` and `../goal-loop-controller.ts`.
5
+ */
6
+ /**
7
+ * Thrown by `GoalSessionController#admitProviderRequest` when a goal's token budget is exhausted —
8
+ * either already recorded (`status: "budget_limited"`) or just crossed by this very admission. The
9
+ * durable stop (`markBudgetLimited`) is always applied synchronously BEFORE this is thrown, so by the
10
+ * time a caller observes it the goal state already reflects the clean stop. Callers that own a
11
+ * bounded "keep going until told to stop" contract (the goal continuation loop) must recognize this
12
+ * distinctly from a genuine unexpected failure and report their own clean stop reason instead of
13
+ * reclassifying an intentional, already-recorded stop as a continuation failure.
14
+ */
15
+ export declare class GoalBudgetExhaustedError extends Error {
16
+ constructor(message: string);
17
+ }
18
+ //# sourceMappingURL=goal-execution-errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"goal-execution-errors.d.ts","sourceRoot":"","sources":["../../../src/core/goals/goal-execution-errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;GAQG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IAClD,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Typed execution-admission signals, kept in their own module so both the goal session controller
3
+ * (which throws them) and the goal continuation loop (which must recognize them) can import without
4
+ * creating a cycle between `goal-session-controller.ts` and `../goal-loop-controller.ts`.
5
+ */
6
+ /**
7
+ * Thrown by `GoalSessionController#admitProviderRequest` when a goal's token budget is exhausted —
8
+ * either already recorded (`status: "budget_limited"`) or just crossed by this very admission. The
9
+ * durable stop (`markBudgetLimited`) is always applied synchronously BEFORE this is thrown, so by the
10
+ * time a caller observes it the goal state already reflects the clean stop. Callers that own a
11
+ * bounded "keep going until told to stop" contract (the goal continuation loop) must recognize this
12
+ * distinctly from a genuine unexpected failure and report their own clean stop reason instead of
13
+ * reclassifying an intentional, already-recorded stop as a continuation failure.
14
+ */
15
+ export class GoalBudgetExhaustedError extends Error {
16
+ constructor(message) {
17
+ super(message);
18
+ this.name = "GoalBudgetExhaustedError";
19
+ }
20
+ }
21
+ //# sourceMappingURL=goal-execution-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"goal-execution-errors.js","sourceRoot":"","sources":["../../../src/core/goals/goal-execution-errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;GAQG;AACH,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IAClD,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACxC,CAAC;CACD","sourcesContent":["/**\n * Typed execution-admission signals, kept in their own module so both the goal session controller\n * (which throws them) and the goal continuation loop (which must recognize them) can import without\n * creating a cycle between `goal-session-controller.ts` and `../goal-loop-controller.ts`.\n */\n\n/**\n * Thrown by `GoalSessionController#admitProviderRequest` when a goal's token budget is exhausted —\n * either already recorded (`status: \"budget_limited\"`) or just crossed by this very admission. The\n * durable stop (`markBudgetLimited`) is always applied synchronously BEFORE this is thrown, so by the\n * time a caller observes it the goal state already reflects the clean stop. Callers that own a\n * bounded \"keep going until told to stop\" contract (the goal continuation loop) must recognize this\n * distinctly from a genuine unexpected failure and report their own clean stop reason instead of\n * reclassifying an intentional, already-recorded stop as a continuation failure.\n */\nexport class GoalBudgetExhaustedError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"GoalBudgetExhaustedError\";\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"goal-lifecycle.d.ts","sourceRoot":"","sources":["../../../src/core/goals/goal-lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,SAAS,EAId,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAmB,KAAK,iBAAiB,EAAE,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAErG,MAAM,WAAW,iBAAiB;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,SAAS,GAAG,iBAAiB,CAExE;AAED,qFAAqF;AACrF,MAAM,WAAW,sBAAsB;IACtC,oBAAoB,IAAI,SAAS,GAAG,SAAS,CAAC;IAC9C,qBAAqB,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAAC;IAC9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M,MAAM,wBAAwB,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAAG,iBAAiB,CAAC;AAE1F,wBAAgB,kBAAkB,CACjC,IAAI,EAAE,sBAAsB,EAC5B,GAAG,SAA2B,GAC5B,wBAAwB,CAM1B"}
@@ -1,4 +1,4 @@
1
- import { applyGoalEvent, isGoalResumableStatus, isGoalUnfinishedStatus, MAX_GOAL_OBJECTIVE_LENGTH, } from "./goal-state.js";
1
+ import { applyGoalEvent, isGoalExecutionActive, isGoalResumableStatus, isGoalUnfinishedStatus, MAX_GOAL_OBJECTIVE_LENGTH, } from "./goal-state.js";
2
2
  import { applyGoalAction } from "./goal-tool-core.js";
3
3
  export function getGoalStateRevision(state) {
4
4
  return { goalId: state.goalId, revision: state.revision ?? 0 };
@@ -15,7 +15,7 @@ function persistResult(host, current, result) {
15
15
  export function pauseGoal(current, now) {
16
16
  if (!current)
17
17
  return missingGoal("pause");
18
- if (current.status !== "active") {
18
+ if (!isGoalExecutionActive(current.status)) {
19
19
  return { ok: false, error: `Goal '${current.goalId}' is ${current.status}; only active goals can be paused.` };
20
20
  }
21
21
  return { ok: true, state: applyGoalEvent(current, { type: "pause_goal", now }) };
@@ -66,7 +66,7 @@ export function replaceGoal(args, now) {
66
66
  export function stopGoalFromSystem(current, args, now) {
67
67
  if (!current)
68
68
  return missingGoal("stop");
69
- if (current.status !== "active") {
69
+ if (!isGoalExecutionActive(current.status)) {
70
70
  return { ok: false, error: `Goal '${current.goalId}' is ${current.status}; no system stop is needed.` };
71
71
  }
72
72
  const reason = args.reason.trim();
@@ -1 +1 @@
1
- {"version":3,"file":"goal-lifecycle.js","sourceRoot":"","sources":["../../../src/core/goals/goal-lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EAEd,qBAAqB,EACrB,sBAAsB,EACtB,yBAAyB,GACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,eAAe,EAAiD,M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EAAE,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;AAC/D,CAAC;AAID,MAAM,UAAU,kBAAkB,CACjC,IAA4B,EAC5B,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;IAE9B,MAAM,OAAO,GAAG,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAC5C,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAClD,IAAI,CAAC,IAAI,CAAC,sBAAsB;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,+CAA+C,EAAE,CAAC;IAC/G,IAAI,CAAC,sBAAsB,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;IAC1C,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AACpC,CAAC","sourcesContent":["import {\n\tapplyGoalEvent,\n\ttype GoalState,\n\tisGoalResumableStatus,\n\tisGoalUnfinishedStatus,\n\tMAX_GOAL_OBJECTIVE_LENGTH,\n} from \"./goal-state.ts\";\nimport { applyGoalAction, type GoalActionFailure, type GoalActionResult } from \"./goal-tool-core.ts\";\n\nexport interface GoalStateRevision {\n\tgoalId: string;\n\trevision: number;\n}\n\nexport function getGoalStateRevision(state: GoalState): GoalStateRevision {\n\treturn { goalId: state.goalId, revision: state.revision ?? 0 };\n}\n\n/** Minimal durable seam shared by CLI, tools, and in-process session restoration. */\nexport interface PersistedGoalStateHost {\n\tgetGoalStateSnapshot(): GoalState | undefined;\n\tsaveGoalStateSnapshot(state: GoalState, expected?: GoalStateRevision): string;\n\tclearGoalStateSnapshot?(state: GoalState, now: string): string;\n}\n\nfunction missingGoal(action: string): GoalActionFailure {\n\treturn { ok: false, error: `No goal exists to ${action}.` };\n}\n\nfunction persistResult(\n\thost: PersistedGoalStateHost,\n\tcurrent: GoalState | undefined,\n\tresult: GoalActionResult,\n): GoalActionResult {\n\tif (!result.ok) return result;\n\thost.saveGoalStateSnapshot(result.state, current ? getGoalStateRevision(current) : undefined);\n\treturn result;\n}\n\nexport function pauseGoal(current: GoalState | undefined, now: string): GoalActionResult {\n\tif (!current) return missingGoal(\"pause\");\n\tif (current.status !== \"active\") {\n\t\treturn { ok: false, error: `Goal '${current.goalId}' is ${current.status}; only active goals can be paused.` };\n\t}\n\treturn { ok: true, state: applyGoalEvent(current, { type: \"pause_goal\", now }) };\n}\n\nexport function resumeGoal(current: GoalState | undefined, now: string): GoalActionResult {\n\tif (!current) return missingGoal(\"resume\");\n\tif (!isGoalResumableStatus(current.status)) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `Goal '${current.goalId}' is ${current.status}; only paused, blocked, or usage-limited goals can be resumed.`,\n\t\t};\n\t}\n\treturn { ok: true, state: applyGoalEvent(current, { type: \"resume_goal\", now }) };\n}\n\nexport function editGoal(\n\tcurrent: GoalState | undefined,\n\targs: { userGoal: string; tokenBudget?: number },\n\tnow: string,\n): GoalActionResult {\n\tif (!current) return missingGoal(\"edit\");\n\tif (current.status === \"cancelled\") {\n\t\treturn { ok: false, error: `Goal '${current.goalId}' is cancelled; start a new goal instead.` };\n\t}\n\tconst userGoal = args.userGoal.trim();\n\tif (!userGoal) return { ok: false, error: \"Goal objective must not be empty.\" };\n\tif (userGoal.length > MAX_GOAL_OBJECTIVE_LENGTH) {\n\t\treturn { ok: false, error: `Goal objective must be at most ${MAX_GOAL_OBJECTIVE_LENGTH} characters.` };\n\t}\n\tif (args.tokenBudget !== undefined && (!Number.isSafeInteger(args.tokenBudget) || args.tokenBudget <= 0)) {\n\t\treturn { ok: false, error: \"Goal token budget must be a positive integer when provided.\" };\n\t}\n\treturn {\n\t\tok: true,\n\t\tstate: applyGoalEvent(current, { type: \"edit_goal\", userGoal, tokenBudget: args.tokenBudget, now }),\n\t};\n}\n\nexport function cancelGoal(current: GoalState | undefined, now: string): GoalActionResult {\n\tif (!current) return missingGoal(\"cancel\");\n\tif (!isGoalUnfinishedStatus(current.status)) {\n\t\treturn { ok: false, error: `Goal '${current.goalId}' is already ${current.status}.` };\n\t}\n\treturn { ok: true, state: applyGoalEvent(current, { type: \"cancel_goal\", now }) };\n}\n\n/** Explicit owner replacement. The old and new states cross one persistence boundary. */\nexport function replaceGoal(\n\targs: { goalId: string; userGoal: string; tokenBudget?: number },\n\tnow: string,\n): GoalActionResult {\n\treturn applyGoalAction(undefined, { action: \"start\", ...args }, now);\n}\n\nexport function stopGoalFromSystem(\n\tcurrent: GoalState | undefined,\n\targs: { status: \"blocked\" | \"usage_limited\" | \"budget_limited\"; reason: string },\n\tnow: string,\n): GoalActionResult {\n\tif (!current) return missingGoal(\"stop\");\n\tif (current.status !== \"active\") {\n\t\treturn { ok: false, error: `Goal '${current.goalId}' is ${current.status}; no system stop is needed.` };\n\t}\n\tconst reason = args.reason.trim();\n\tif (!reason) return { ok: false, error: \"System goal stop requires a reason.\" };\n\treturn { ok: true, state: applyGoalEvent(current, { type: \"system_stop_goal\", ...args, reason, now }) };\n}\n\nexport function pausePersistedGoal(host: PersistedGoalStateHost, now = new Date().toISOString()): GoalActionResult {\n\tconst current = host.getGoalStateSnapshot();\n\treturn persistResult(host, current, pauseGoal(current, now));\n}\n\nexport function resumePersistedGoal(host: PersistedGoalStateHost, now = new Date().toISOString()): GoalActionResult {\n\tconst current = host.getGoalStateSnapshot();\n\treturn persistResult(host, current, resumeGoal(current, now));\n}\n\nexport function editPersistedGoal(\n\thost: PersistedGoalStateHost,\n\targs: { userGoal: string; tokenBudget?: number },\n\tnow = new Date().toISOString(),\n): GoalActionResult {\n\tconst current = host.getGoalStateSnapshot();\n\treturn persistResult(host, current, editGoal(current, args, now));\n}\n\nexport function cancelPersistedGoal(host: PersistedGoalStateHost, now = new Date().toISOString()): GoalActionResult {\n\tconst current = host.getGoalStateSnapshot();\n\treturn persistResult(host, current, cancelGoal(current, now));\n}\n\nexport type ClearPersistedGoalResult = { ok: true; cleared: boolean } | GoalActionFailure;\n\nexport function clearPersistedGoal(\n\thost: PersistedGoalStateHost,\n\tnow = new Date().toISOString(),\n): ClearPersistedGoalResult {\n\tconst current = host.getGoalStateSnapshot();\n\tif (!current) return { ok: true, cleared: false };\n\tif (!host.clearGoalStateSnapshot) return { ok: false, error: \"This session does not support clearing goals.\" };\n\thost.clearGoalStateSnapshot(current, now);\n\treturn { ok: true, cleared: true };\n}\n"]}
1
+ {"version":3,"file":"goal-lifecycle.js","sourceRoot":"","sources":["../../../src/core/goals/goal-lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EAEd,qBAAqB,EACrB,qBAAqB,EACrB,sBAAsB,EACtB,yBAAyB,GACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,eAAe,EAAiD,MAAM,qBAAqB,CAAC;AAOrG,MAAM,UAAU,oBAAoB,CAAC,KAAgB;IACpD,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,IAAI,CAAC,EAAE,CAAC;AAChE,CAAC;AASD,SAAS,WAAW,CAAC,MAAc;IAClC,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,qBAAqB,MAAM,GAAG,EAAE,CAAC;AAC7D,CAAC;AAED,SAAS,aAAa,CACrB,IAA4B,EAC5B,OAA8B,EAC9B,MAAwB;IAExB,IAAI,CAAC,MAAM,CAAC,EAAE;QAAE,OAAO,MAAM,CAAC;IAC9B,IAAI,CAAC,qBAAqB,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IAC9F,OAAO,MAAM,CAAC;AACf,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,OAA8B,EAAE,GAAW;IACpE,IAAI,CAAC,OAAO;QAAE,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC;IAC1C,IAAI,CA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\n} from \"./goal-state.ts\";\nimport { applyGoalAction, type GoalActionFailure, type GoalActionResult } from \"./goal-tool-core.ts\";\n\nexport interface GoalStateRevision {\n\tgoalId: string;\n\trevision: number;\n}\n\nexport function getGoalStateRevision(state: GoalState): GoalStateRevision {\n\treturn { goalId: state.goalId, revision: state.revision ?? 0 };\n}\n\n/** Minimal durable seam shared by CLI, tools, and in-process session restoration. */\nexport interface PersistedGoalStateHost {\n\tgetGoalStateSnapshot(): GoalState | undefined;\n\tsaveGoalStateSnapshot(state: GoalState, expected?: GoalStateRevision): string;\n\tclearGoalStateSnapshot?(state: GoalState, now: string): string;\n}\n\nfunction missingGoal(action: string): GoalActionFailure {\n\treturn { ok: false, error: `No goal exists to ${action}.` };\n}\n\nfunction persistResult(\n\thost: PersistedGoalStateHost,\n\tcurrent: GoalState | undefined,\n\tresult: GoalActionResult,\n): GoalActionResult {\n\tif (!result.ok) return result;\n\thost.saveGoalStateSnapshot(result.state, current ? getGoalStateRevision(current) : undefined);\n\treturn result;\n}\n\nexport function pauseGoal(current: GoalState | undefined, now: string): GoalActionResult {\n\tif (!current) return missingGoal(\"pause\");\n\tif (!isGoalExecutionActive(current.status)) {\n\t\treturn { ok: false, error: `Goal '${current.goalId}' is ${current.status}; only active goals can be paused.` };\n\t}\n\treturn { ok: true, state: applyGoalEvent(current, { type: \"pause_goal\", now }) };\n}\n\nexport function resumeGoal(current: GoalState | undefined, now: string): GoalActionResult {\n\tif (!current) return missingGoal(\"resume\");\n\tif (!isGoalResumableStatus(current.status)) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `Goal '${current.goalId}' is ${current.status}; only paused, blocked, or usage-limited goals can be resumed.`,\n\t\t};\n\t}\n\treturn { ok: true, state: applyGoalEvent(current, { type: \"resume_goal\", now }) };\n}\n\nexport function editGoal(\n\tcurrent: GoalState | undefined,\n\targs: { userGoal: string; tokenBudget?: number },\n\tnow: string,\n): GoalActionResult {\n\tif (!current) return missingGoal(\"edit\");\n\tif (current.status === \"cancelled\") {\n\t\treturn { ok: false, error: `Goal '${current.goalId}' is cancelled; start a new goal instead.` };\n\t}\n\tconst userGoal = args.userGoal.trim();\n\tif (!userGoal) return { ok: false, error: \"Goal objective must not be empty.\" };\n\tif (userGoal.length > MAX_GOAL_OBJECTIVE_LENGTH) {\n\t\treturn { ok: false, error: `Goal objective must be at most ${MAX_GOAL_OBJECTIVE_LENGTH} characters.` };\n\t}\n\tif (args.tokenBudget !== undefined && (!Number.isSafeInteger(args.tokenBudget) || args.tokenBudget <= 0)) {\n\t\treturn { ok: false, error: \"Goal token budget must be a positive integer when provided.\" };\n\t}\n\treturn {\n\t\tok: true,\n\t\tstate: applyGoalEvent(current, { type: \"edit_goal\", userGoal, tokenBudget: args.tokenBudget, now }),\n\t};\n}\n\nexport function cancelGoal(current: GoalState | undefined, now: string): GoalActionResult {\n\tif (!current) return missingGoal(\"cancel\");\n\tif (!isGoalUnfinishedStatus(current.status)) {\n\t\treturn { ok: false, error: `Goal '${current.goalId}' is already ${current.status}.` };\n\t}\n\treturn { ok: true, state: applyGoalEvent(current, { type: \"cancel_goal\", now }) };\n}\n\n/** Explicit owner replacement. The old and new states cross one persistence boundary. */\nexport function replaceGoal(\n\targs: { goalId: string; userGoal: string; tokenBudget?: number },\n\tnow: string,\n): GoalActionResult {\n\treturn applyGoalAction(undefined, { action: \"start\", ...args }, now);\n}\n\nexport function stopGoalFromSystem(\n\tcurrent: GoalState | undefined,\n\targs: { status: \"blocked\" | \"usage_limited\" | \"budget_limited\"; reason: string },\n\tnow: string,\n): GoalActionResult {\n\tif (!current) return missingGoal(\"stop\");\n\tif (!isGoalExecutionActive(current.status)) {\n\t\treturn { ok: false, error: `Goal '${current.goalId}' is ${current.status}; no system stop is needed.` };\n\t}\n\tconst reason = args.reason.trim();\n\tif (!reason) return { ok: false, error: \"System goal stop requires a reason.\" };\n\treturn { ok: true, state: applyGoalEvent(current, { type: \"system_stop_goal\", ...args, reason, now }) };\n}\n\nexport function pausePersistedGoal(host: PersistedGoalStateHost, now = new Date().toISOString()): GoalActionResult {\n\tconst current = host.getGoalStateSnapshot();\n\treturn persistResult(host, current, pauseGoal(current, now));\n}\n\nexport function resumePersistedGoal(host: PersistedGoalStateHost, now = new Date().toISOString()): GoalActionResult {\n\tconst current = host.getGoalStateSnapshot();\n\treturn persistResult(host, current, resumeGoal(current, now));\n}\n\nexport function editPersistedGoal(\n\thost: PersistedGoalStateHost,\n\targs: { userGoal: string; tokenBudget?: number },\n\tnow = new Date().toISOString(),\n): GoalActionResult {\n\tconst current = host.getGoalStateSnapshot();\n\treturn persistResult(host, current, editGoal(current, args, now));\n}\n\nexport function cancelPersistedGoal(host: PersistedGoalStateHost, now = new Date().toISOString()): GoalActionResult {\n\tconst current = host.getGoalStateSnapshot();\n\treturn persistResult(host, current, cancelGoal(current, now));\n}\n\nexport type ClearPersistedGoalResult = { ok: true; cleared: boolean } | GoalActionFailure;\n\nexport function clearPersistedGoal(\n\thost: PersistedGoalStateHost,\n\tnow = new Date().toISOString(),\n): ClearPersistedGoalResult {\n\tconst current = host.getGoalStateSnapshot();\n\tif (!current) return { ok: true, cleared: false };\n\tif (!host.clearGoalStateSnapshot) return { ok: false, error: \"This session does not support clearing goals.\" };\n\thost.clearGoalStateSnapshot(current, now);\n\treturn { ok: true, cleared: true };\n}\n"]}
@@ -70,6 +70,17 @@ export declare class GoalSessionController {
70
70
  authorizeStartFromTool(input: {
71
71
  tokenBudget?: number;
72
72
  }): string | undefined;
73
+ /**
74
+ * True once the currently held execution lease's goal has crossed into `budget_limited` — set
75
+ * synchronously by `chargeExecutionUsage` as soon as a just-charged response crosses the ceiling.
76
+ * Wired to the agent's `shouldStopAfterTurn` hook so a mid-turn budget stop ends the loop
77
+ * gracefully BEFORE the next provider request is even planned, instead of only being caught by
78
+ * `admitProviderRequest`'s throw — which still exists as the backstop for a fresh turn or an
79
+ * external admission starting against an already budget_limited goal, but would otherwise also
80
+ * fire for a request this same lease was already mid-flight for, surfacing a synthetic error
81
+ * message where a clean turn end belongs.
82
+ */
83
+ hasExecutionLeaseCrossedBudgetLimit(): boolean;
73
84
  /** Reserve only remaining output capacity; actual provider usage is charged on response. */
74
85
  admitProviderRequest(): number | undefined;
75
86
  private resolveExecutionState;
@@ -1 +1 @@
1
- {"version":3,"file":"goal-session-controller.d.ts","sourceRoot":"","sources":["../../../src/core/goals/goal-session-controller.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,KAAK,EACX,2BAA2B,EAC3B,0BAA0B,EAC1B,2BAA2B,EAC3B,0BAA0B,EAC1B,aAAa,EACb,MAAM,+BAA+B,CAAC;AACvC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAG9D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAC9E,OAAO,EAAE,KAAK,iBAAiB,EAA4C,MAAM,qBAAqB,CAAC;AACvG,OAAO,EAEN,KAAK,mBAAmB,EACxB,KAAK,2BAA2B,EAChC,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAkB,KAAK,SAAS,EAA0B,MAAM,iBAAiB,CAAC;AAEzF,OAAO,EAAE,KAAK,0BAA0B,EAAyB,MAAM,4BAA4B,CAAC;AAOpG,MAAM,WAAW,yBAAyB;IACzC,iBAAiB,IAAI,cAAc,CAAC;IACpC,gBAAgB,IAAI,MAAM,GAAG,SAAS,CAAC;IACvC,cAAc,IAAI,SAAS,UAAU,EAAE,CAAC;IACxC,sBAAsB,IAAI,qBAAqB,GAAG,SAAS,CAAC;IAC5D,oBAAoB,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IAC7C,gCAAgC,IAAI,IAAI,CAAC;IACzC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC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