pi-harness-runtime 1.1.512 → 1.1.514

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 (225) hide show
  1. package/AGENTS.md +23 -0
  2. package/CHANGELOG.md +183 -0
  3. package/footer-status.ts +112 -43
  4. package/harness/cookie-import-refresh.ts +60 -0
  5. package/harness/exploration-guard-runtime.ts +316 -0
  6. package/harness/plan-handoff-bridge.ts +247 -0
  7. package/harness/plan-handoff-delivery.ts +193 -0
  8. package/harness/plan-handoff-recipient-selection.ts +205 -0
  9. package/harness/plan-handoff-registration.ts +169 -0
  10. package/harness/quota-credential-choice.ts +67 -0
  11. package/harness/quota-refresh-coordinator.ts +129 -0
  12. package/index.ts +633 -115
  13. package/package.json +7 -1
  14. package/packages/agent-policy/package.json +1 -1
  15. package/packages/auth/package.json +1 -1
  16. package/packages/autonomous-runtime/package.json +1 -1
  17. package/packages/checkpoint/package.json +1 -1
  18. package/packages/clipboard/package.json +1 -1
  19. package/packages/codex-watcher/package.json +1 -1
  20. package/packages/config-capture/package.json +1 -1
  21. package/packages/contracts/package.json +1 -1
  22. package/packages/cookie-sanitizer/dist/detect-format.d.ts +26 -12
  23. package/packages/cookie-sanitizer/dist/detect-format.d.ts.map +1 -1
  24. package/packages/cookie-sanitizer/dist/detect-format.js +54 -39
  25. package/packages/cookie-sanitizer/dist/detect-format.js.map +1 -1
  26. package/packages/cookie-sanitizer/dist/index.d.ts +9 -3
  27. package/packages/cookie-sanitizer/dist/index.d.ts.map +1 -1
  28. package/packages/cookie-sanitizer/dist/index.js +5 -3
  29. package/packages/cookie-sanitizer/dist/index.js.map +1 -1
  30. package/packages/cookie-sanitizer/dist/parse-netscape-space.d.ts +73 -0
  31. package/packages/cookie-sanitizer/dist/parse-netscape-space.d.ts.map +1 -0
  32. package/packages/cookie-sanitizer/dist/parse-netscape-space.js +154 -0
  33. package/packages/cookie-sanitizer/dist/parse-netscape-space.js.map +1 -0
  34. package/packages/cookie-sanitizer/dist/parse-netscape.d.ts +28 -1
  35. package/packages/cookie-sanitizer/dist/parse-netscape.d.ts.map +1 -1
  36. package/packages/cookie-sanitizer/dist/parse-netscape.js +50 -13
  37. package/packages/cookie-sanitizer/dist/parse-netscape.js.map +1 -1
  38. package/packages/cookie-sanitizer/dist/source-state.d.ts +56 -0
  39. package/packages/cookie-sanitizer/dist/source-state.d.ts.map +1 -0
  40. package/packages/cookie-sanitizer/dist/source-state.js +61 -0
  41. package/packages/cookie-sanitizer/dist/source-state.js.map +1 -0
  42. package/packages/cookie-sanitizer/dist/sync.d.ts +9 -0
  43. package/packages/cookie-sanitizer/dist/sync.d.ts.map +1 -1
  44. package/packages/cookie-sanitizer/dist/sync.js +47 -16
  45. package/packages/cookie-sanitizer/dist/sync.js.map +1 -1
  46. package/packages/cookie-sanitizer/dist/types.d.ts +1 -1
  47. package/packages/cookie-sanitizer/dist/types.d.ts.map +1 -1
  48. package/packages/cookie-sanitizer/package.json +1 -1
  49. package/packages/cookie-sanitizer/src/detect-format.ts +78 -41
  50. package/packages/cookie-sanitizer/src/index.ts +34 -1
  51. package/packages/cookie-sanitizer/src/parse-netscape-space.ts +188 -0
  52. package/packages/cookie-sanitizer/src/parse-netscape.ts +81 -13
  53. package/packages/cookie-sanitizer/src/source-state.ts +95 -0
  54. package/packages/cookie-sanitizer/src/sync.ts +59 -16
  55. package/packages/cookie-sanitizer/src/types.ts +4 -1
  56. package/packages/evaluation-engine/package.json +1 -1
  57. package/packages/event-bus/package.json +1 -1
  58. package/packages/event-store/package.json +1 -1
  59. package/packages/experience-replay/package.json +1 -1
  60. package/packages/exploration-guard/dist/budget.d.ts +105 -0
  61. package/packages/exploration-guard/dist/budget.d.ts.map +1 -0
  62. package/packages/exploration-guard/dist/budget.js +199 -0
  63. package/packages/exploration-guard/dist/budget.js.map +1 -0
  64. package/packages/exploration-guard/dist/checkpoint.d.ts +140 -0
  65. package/packages/exploration-guard/dist/checkpoint.d.ts.map +1 -0
  66. package/packages/exploration-guard/dist/checkpoint.js +201 -0
  67. package/packages/exploration-guard/dist/checkpoint.js.map +1 -0
  68. package/packages/exploration-guard/dist/index.d.ts +19 -0
  69. package/packages/exploration-guard/dist/index.d.ts.map +1 -0
  70. package/packages/exploration-guard/dist/index.js +18 -0
  71. package/packages/exploration-guard/dist/index.js.map +1 -0
  72. package/packages/exploration-guard/dist/observation.d.ts +82 -0
  73. package/packages/exploration-guard/dist/observation.d.ts.map +1 -0
  74. package/packages/exploration-guard/dist/observation.js +239 -0
  75. package/packages/exploration-guard/dist/observation.js.map +1 -0
  76. package/packages/exploration-guard/dist/state.d.ts +107 -0
  77. package/packages/exploration-guard/dist/state.d.ts.map +1 -0
  78. package/packages/exploration-guard/dist/state.js +300 -0
  79. package/packages/exploration-guard/dist/state.js.map +1 -0
  80. package/packages/exploration-guard/dist/status.d.ts +56 -0
  81. package/packages/exploration-guard/dist/status.d.ts.map +1 -0
  82. package/packages/exploration-guard/dist/status.js +79 -0
  83. package/packages/exploration-guard/dist/status.js.map +1 -0
  84. package/packages/exploration-guard/dist/tracker.d.ts +115 -0
  85. package/packages/exploration-guard/dist/tracker.d.ts.map +1 -0
  86. package/packages/exploration-guard/dist/tracker.js +207 -0
  87. package/packages/exploration-guard/dist/tracker.js.map +1 -0
  88. package/packages/exploration-guard/dist/types.d.ts +254 -0
  89. package/packages/exploration-guard/dist/types.d.ts.map +1 -0
  90. package/packages/exploration-guard/dist/types.js +14 -0
  91. package/packages/exploration-guard/dist/types.js.map +1 -0
  92. package/packages/exploration-guard/package.json +16 -0
  93. package/packages/exploration-guard/src/budget.ts +275 -0
  94. package/packages/exploration-guard/src/checkpoint.ts +297 -0
  95. package/packages/exploration-guard/src/index.ts +97 -0
  96. package/packages/exploration-guard/src/observation.ts +342 -0
  97. package/packages/exploration-guard/src/state.ts +357 -0
  98. package/packages/exploration-guard/src/status.ts +120 -0
  99. package/packages/exploration-guard/src/tracker.ts +291 -0
  100. package/packages/exploration-guard/src/types.ts +276 -0
  101. package/packages/file-copy-helper/package.json +1 -1
  102. package/packages/intent-analyzer/package.json +1 -1
  103. package/packages/jev-judge/dist/continuation-controller.d.ts +27 -2
  104. package/packages/jev-judge/dist/continuation-controller.d.ts.map +1 -1
  105. package/packages/jev-judge/dist/continuation-controller.js +87 -11
  106. package/packages/jev-judge/dist/continuation-controller.js.map +1 -1
  107. package/packages/jev-judge/dist/index.d.ts +2 -0
  108. package/packages/jev-judge/dist/index.d.ts.map +1 -1
  109. package/packages/jev-judge/dist/index.js +2 -0
  110. package/packages/jev-judge/dist/index.js.map +1 -1
  111. package/packages/jev-judge/dist/todo-provider.d.ts.map +1 -1
  112. package/packages/jev-judge/dist/todo-provider.js +93 -18
  113. package/packages/jev-judge/dist/todo-provider.js.map +1 -1
  114. package/packages/jev-judge/dist/todo-reconciliation.d.ts +50 -0
  115. package/packages/jev-judge/dist/todo-reconciliation.d.ts.map +1 -0
  116. package/packages/jev-judge/dist/todo-reconciliation.js +96 -0
  117. package/packages/jev-judge/dist/todo-reconciliation.js.map +1 -0
  118. package/packages/jev-judge/package.json +1 -1
  119. package/packages/jev-judge/src/continuation-controller.ts +90 -12
  120. package/packages/jev-judge/src/index.ts +8 -0
  121. package/packages/jev-judge/src/todo-provider.ts +117 -25
  122. package/packages/jev-judge/src/todo-reconciliation.ts +121 -0
  123. package/packages/knowledge-retrieval/package.json +1 -1
  124. package/packages/learning-engine/package.json +1 -1
  125. package/packages/logging/package.json +1 -1
  126. package/packages/memory-engine/package.json +1 -1
  127. package/packages/notification/dist/adapters/email-adapter.d.ts.map +1 -1
  128. package/packages/notification/dist/adapters/email-adapter.js +1 -0
  129. package/packages/notification/dist/adapters/email-adapter.js.map +1 -1
  130. package/packages/notification/dist/adapters/line-adapter.d.ts.map +1 -1
  131. package/packages/notification/dist/adapters/line-adapter.js +1 -0
  132. package/packages/notification/dist/adapters/line-adapter.js.map +1 -1
  133. package/packages/notification/dist/adapters/ntfy-adapter.d.ts.map +1 -1
  134. package/packages/notification/dist/adapters/ntfy-adapter.js +1 -0
  135. package/packages/notification/dist/adapters/ntfy-adapter.js.map +1 -1
  136. package/packages/notification/dist/adapters/telegram-adapter.d.ts.map +1 -1
  137. package/packages/notification/dist/adapters/telegram-adapter.js +1 -0
  138. package/packages/notification/dist/adapters/telegram-adapter.js.map +1 -1
  139. package/packages/notification/dist/notification-center.d.ts.map +1 -1
  140. package/packages/notification/dist/notification-center.js +7 -0
  141. package/packages/notification/dist/notification-center.js.map +1 -1
  142. package/packages/notification/dist/notification-policy.d.ts.map +1 -1
  143. package/packages/notification/dist/notification-policy.js +3 -0
  144. package/packages/notification/dist/notification-policy.js.map +1 -1
  145. package/packages/notification/dist/telegram-webhook-handler.d.ts +8 -0
  146. package/packages/notification/dist/telegram-webhook-handler.d.ts.map +1 -1
  147. package/packages/notification/dist/telegram-webhook-handler.js +8 -0
  148. package/packages/notification/dist/telegram-webhook-handler.js.map +1 -1
  149. package/packages/notification/dist/types.d.ts +8 -1
  150. package/packages/notification/dist/types.d.ts.map +1 -1
  151. package/packages/notification/package.json +1 -1
  152. package/packages/notification/src/adapters/email-adapter.ts +1 -0
  153. package/packages/notification/src/adapters/line-adapter.ts +1 -0
  154. package/packages/notification/src/adapters/ntfy-adapter.ts +1 -0
  155. package/packages/notification/src/adapters/telegram-adapter.ts +1 -0
  156. package/packages/notification/src/notification-center.ts +7 -0
  157. package/packages/notification/src/notification-policy.ts +3 -0
  158. package/packages/notification/src/telegram-webhook-handler.ts +8 -0
  159. package/packages/notification/src/types.ts +7 -0
  160. package/packages/paperclip-integration/package.json +1 -1
  161. package/packages/performance-optimizer/package.json +1 -1
  162. package/packages/plan-handoff/dist/index.d.ts +31 -0
  163. package/packages/plan-handoff/dist/index.d.ts.map +1 -0
  164. package/packages/plan-handoff/dist/index.js +23 -0
  165. package/packages/plan-handoff/dist/index.js.map +1 -0
  166. package/packages/plan-handoff/dist/mailbox.d.ts +134 -0
  167. package/packages/plan-handoff/dist/mailbox.d.ts.map +1 -0
  168. package/packages/plan-handoff/dist/mailbox.js +243 -0
  169. package/packages/plan-handoff/dist/mailbox.js.map +1 -0
  170. package/packages/plan-handoff/dist/message.d.ts +152 -0
  171. package/packages/plan-handoff/dist/message.d.ts.map +1 -0
  172. package/packages/plan-handoff/dist/message.js +157 -0
  173. package/packages/plan-handoff/dist/message.js.map +1 -0
  174. package/packages/plan-handoff/dist/pairing.d.ts +51 -0
  175. package/packages/plan-handoff/dist/pairing.d.ts.map +1 -0
  176. package/packages/plan-handoff/dist/pairing.js +88 -0
  177. package/packages/plan-handoff/dist/pairing.js.map +1 -0
  178. package/packages/plan-handoff/dist/publish.d.ts +96 -0
  179. package/packages/plan-handoff/dist/publish.d.ts.map +1 -0
  180. package/packages/plan-handoff/dist/publish.js +97 -0
  181. package/packages/plan-handoff/dist/publish.js.map +1 -0
  182. package/packages/plan-handoff/dist/registry.d.ts +95 -0
  183. package/packages/plan-handoff/dist/registry.d.ts.map +1 -0
  184. package/packages/plan-handoff/dist/registry.js +122 -0
  185. package/packages/plan-handoff/dist/registry.js.map +1 -0
  186. package/packages/plan-handoff/dist/retention.d.ts +101 -0
  187. package/packages/plan-handoff/dist/retention.d.ts.map +1 -0
  188. package/packages/plan-handoff/dist/retention.js +178 -0
  189. package/packages/plan-handoff/dist/retention.js.map +1 -0
  190. package/packages/plan-handoff/dist/routing.d.ts +69 -0
  191. package/packages/plan-handoff/dist/routing.d.ts.map +1 -0
  192. package/packages/plan-handoff/dist/routing.js +75 -0
  193. package/packages/plan-handoff/dist/routing.js.map +1 -0
  194. package/packages/plan-handoff/dist/workspace.d.ts +59 -0
  195. package/packages/plan-handoff/dist/workspace.d.ts.map +1 -0
  196. package/packages/plan-handoff/dist/workspace.js +78 -0
  197. package/packages/plan-handoff/dist/workspace.js.map +1 -0
  198. package/packages/plan-handoff/package.json +16 -0
  199. package/packages/plan-handoff/src/index.ts +75 -0
  200. package/packages/plan-handoff/src/mailbox.ts +306 -0
  201. package/packages/plan-handoff/src/message.ts +286 -0
  202. package/packages/plan-handoff/src/pairing.ts +120 -0
  203. package/packages/plan-handoff/src/publish.ts +186 -0
  204. package/packages/plan-handoff/src/registry.ts +174 -0
  205. package/packages/plan-handoff/src/retention.ts +237 -0
  206. package/packages/plan-handoff/src/routing.ts +134 -0
  207. package/packages/plan-handoff/src/workspace.ts +89 -0
  208. package/packages/privilege-broker/package.json +1 -1
  209. package/packages/prompt-compiler/package.json +1 -1
  210. package/packages/providers/package.json +1 -1
  211. package/packages/qdrant-skills/package.json +1 -1
  212. package/packages/quota-manager/package.json +1 -1
  213. package/packages/relay/package.json +1 -1
  214. package/packages/skill-mcp-client/package.json +1 -1
  215. package/packages/skills/package.json +1 -1
  216. package/packages/todo-bd-sync/package.json +1 -1
  217. package/packages/token-estimation/package.json +1 -1
  218. package/packages/tooling-preference/package.json +1 -1
  219. package/packages/trajectory/package.json +1 -1
  220. package/packages/tui/package.json +1 -1
  221. package/packages/two-phase-review/package.json +1 -1
  222. package/packages/types/package.json +1 -1
  223. package/packages/webmcp-tester/package.json +1 -1
  224. package/packages/wiki/package.json +1 -1
  225. package/packages/write-review/package.json +1 -1
@@ -0,0 +1,316 @@
1
+ /**
2
+ * The runtime of the exploration guard.
3
+ *
4
+ * The runtime observes the tools of the agent, keeps the state of the
5
+ * request, and hands the decision of the guard to the caller.
6
+ *
7
+ * The runtime never sends anything and never authorises anything. It
8
+ * returns a pending intervention at a safe boundary, and the caller decides
9
+ * what to do with it. The runtime never touches a provider countdown, never
10
+ * answers a question of the human and never starts a second continuation
11
+ * owner.
12
+ */
13
+
14
+ import type {
15
+ ExplorationKey,
16
+ ExplorationPhase,
17
+ ExplorationState,
18
+ Intervention,
19
+ ToolCallEnd,
20
+ ToolCallInput,
21
+ } from "../packages/exploration-guard/dist/index.js";
22
+ import {
23
+ buildContinuationText,
24
+ describeUnreadableCheckpoint,
25
+ ExplorationTracker,
26
+ toCompactCheckpoint,
27
+ } from "../packages/exploration-guard/dist/index.js";
28
+
29
+ /**
30
+ * The identity of the request that is running.
31
+ *
32
+ * Every part of it is read when it is needed, so a change of the session or
33
+ * of the generation moves the exploration to another state.
34
+ */
35
+ export interface ExplorationIdentity {
36
+ /** The canonical workspace of the request. */
37
+ workspace: () => string;
38
+ /** The session of the request. */
39
+ sessionId: () => string;
40
+ /** The generation of the request. */
41
+ generation: () => string;
42
+ /** The phase of the request. */
43
+ phase: () => ExplorationPhase;
44
+ /** The todo of the request, when the request owns one. */
45
+ todoId?: () => number | undefined;
46
+ }
47
+
48
+ /**
49
+ * The options of the runtime.
50
+ */
51
+ export interface ExplorationRuntimeOptions {
52
+ /** The identity of the request. */
53
+ identity: ExplorationIdentity;
54
+ /** The tracker of the exploration. */
55
+ tracker?: ExplorationTracker;
56
+ /** The clock, so a test can pin the time. */
57
+ now?: () => number;
58
+ }
59
+
60
+ /**
61
+ * A pending intervention of the guard.
62
+ *
63
+ * It is pending until a safe boundary: the runtime speaks once, and the
64
+ * caller consumes it when the agent is between turns.
65
+ */
66
+ export interface PendingIntervention {
67
+ /** The key of the request. */
68
+ key: ExplorationKey;
69
+ /** The text the agent receives. */
70
+ message: string;
71
+ /** True when the guard asks for a checkpoint. */
72
+ requiresCheckpoint: boolean;
73
+ /** The kind of the intervention. */
74
+ kind: Intervention["kind"];
75
+ /** The state of the request when the intervention was raised. */
76
+ state: ExplorationState;
77
+ }
78
+
79
+ /**
80
+ * How serious an intervention is.
81
+ *
82
+ * A soft warning is the least serious, a budget that is reached is more
83
+ * serious, and a blocker is the most serious. The guard speaks again when
84
+ * the seriousness grows, even when the checkpoint did not change.
85
+ */
86
+ const SEVERITY: Record<Intervention["kind"], number> = {
87
+ none: 0,
88
+ "repeat-read": 1,
89
+ stall: 1,
90
+ "read-budget": 2,
91
+ "token-budget": 2,
92
+ "time-budget": 2,
93
+ blocked: 3,
94
+ };
95
+
96
+ /**
97
+ * The number of times the guard speaks in one request.
98
+ *
99
+ * The guard asks, it does not nag: a request that ignored three warnings
100
+ * does not get a fourth one. The agent decides, and a decision that is not
101
+ * taken is not the business of the guard.
102
+ */
103
+ const MAX_SPEECHES_PER_REQUEST = 3;
104
+
105
+ /**
106
+ * The runtime of the exploration guard.
107
+ */
108
+ export class ExplorationGuardRuntime {
109
+ private readonly identity: ExplorationIdentity;
110
+ private readonly tracker: ExplorationTracker;
111
+ private readonly now: () => number;
112
+ /** The calls that are running, by their id. */
113
+ private readonly running = new Map<string, ToolCallInput>();
114
+ /** The intervention that was not consumed yet. */
115
+ private pending: PendingIntervention | null = null;
116
+ /** The generation the pending intervention belongs to. */
117
+ private pendingGeneration = "";
118
+ /** True when the request was completed or cancelled. */
119
+ private inactive = false;
120
+ /** How often the guard spoke in this request. */
121
+ private speechCount = 0;
122
+ /** How serious the last intervention was. */
123
+ private lastSeverity = -1;
124
+ /** The checkpoint revision the guard last spoke at. */
125
+ private spokenRevision = -1;
126
+
127
+ constructor(options: ExplorationRuntimeOptions) {
128
+ this.identity = options.identity;
129
+ this.tracker = options.tracker ?? new ExplorationTracker();
130
+ this.now = options.now ?? (() => Date.now());
131
+ }
132
+
133
+ /** The key of the request that is running. */
134
+ key(): ExplorationKey {
135
+ return {
136
+ workspace: this.identity.workspace(),
137
+ sessionId: this.identity.sessionId(),
138
+ generation: this.identity.generation(),
139
+ phase: this.identity.phase(),
140
+ todoId: this.identity.todoId?.(),
141
+ };
142
+ }
143
+
144
+ /**
145
+ * A tool call begins.
146
+ *
147
+ * The call is remembered, so its result can be paired with it. Nothing
148
+ * is observed yet: a call that fails read nothing.
149
+ */
150
+ toolCall(toolCallId: string, call: ToolCallInput): void {
151
+ this.running.set(toolCallId, call);
152
+ }
153
+
154
+ /**
155
+ * A tool call ended.
156
+ *
157
+ * The observation is made here, because only the result says whether
158
+ * the tool read anything.
159
+ */
160
+ toolCallEnd(
161
+ toolCallId: string,
162
+ end: ToolCallEnd,
163
+ ): PendingIntervention | null {
164
+ const call = this.running.get(toolCallId);
165
+ this.running.delete(toolCallId);
166
+ if (!call) return null;
167
+ if (this.inactive) return null;
168
+
169
+ let result: { shouldSpeak: boolean; intervention: Intervention; state: ExplorationState };
170
+ try {
171
+ result = this.tracker.observe(this.key(), call, end);
172
+ } catch {
173
+ // The state could not be read or written: the guard stays quiet
174
+ // instead of guessing what was read.
175
+ return null;
176
+ }
177
+ if (!result.shouldSpeak) return null;
178
+
179
+ const severity = SEVERITY[result.intervention.kind] ?? 0;
180
+ if (!this.maySpeak(severity, result.state)) return null;
181
+
182
+ this.tracker.markSpoken(result.state);
183
+ this.speechCount += 1;
184
+ this.lastSeverity = severity;
185
+ this.spokenRevision = result.state.checkpointRevision;
186
+ const key = this.key();
187
+ this.pending = {
188
+ key,
189
+ message: result.intervention.message,
190
+ requiresCheckpoint: result.intervention.requiresCheckpoint,
191
+ kind: result.intervention.kind,
192
+ state: result.state,
193
+ };
194
+ this.pendingGeneration = key.generation;
195
+ return this.pending;
196
+ }
197
+
198
+ /**
199
+ * True when the guard may speak.
200
+ *
201
+ * The guard speaks once per checkpoint revision, and again when the
202
+ * seriousness of the intervention grew. It speaks at most three times
203
+ * in one request: a request that ignored the warnings is not nagged.
204
+ */
205
+ private maySpeak(severity: number, state: ExplorationState): boolean {
206
+ if (this.speechCount >= MAX_SPEECHES_PER_REQUEST) return false;
207
+ const revisionChanged = this.spokenRevision !== state.checkpointRevision;
208
+ const escalated = severity > this.lastSeverity;
209
+ return revisionChanged || escalated;
210
+ }
211
+
212
+ /**
213
+ * The intervention that was not consumed yet.
214
+ *
215
+ * The intervention is inert when the request changed: a new request of
216
+ * the human does not inherit the warning of the old one.
217
+ */
218
+ takePending(): PendingIntervention | null {
219
+ const pending = this.pending;
220
+ if (!pending) return null;
221
+ if (pending.key.generation !== this.pendingGeneration) {
222
+ this.pending = null;
223
+ return null;
224
+ }
225
+ if (pending.key.generation !== this.identity.generation()) {
226
+ this.pending = null;
227
+ return null;
228
+ }
229
+ this.pending = null;
230
+ return pending;
231
+ }
232
+
233
+ /**
234
+ * The request is finished, cancelled or answered.
235
+ *
236
+ * The pending actions of the guard become inert: a completed request
237
+ * must not be continued by the guard.
238
+ */
239
+ deactivate(): void {
240
+ this.inactive = true;
241
+ this.pending = null;
242
+ this.running.clear();
243
+ }
244
+
245
+ /**
246
+ * A new request of the human begins.
247
+ *
248
+ * The guard is active again, and the state of the new request is its
249
+ * own: the counters of the old request do not carry over.
250
+ */
251
+ activate(): void {
252
+ this.inactive = false;
253
+ this.pending = null;
254
+ this.speechCount = 0;
255
+ this.lastSeverity = -1;
256
+ this.spokenRevision = -1;
257
+ }
258
+
259
+ /**
260
+ * The checkpoint that survives a compaction.
261
+ *
262
+ * The checkpoint is saved before the compaction, so the summary of the
263
+ * context and the record of the exploration are two separate things.
264
+ */
265
+ saveCheckpoint(): void {
266
+ if (this.inactive) return;
267
+ try {
268
+ const state = this.tracker.state(this.key());
269
+ this.tracker.save(this.key(), {
270
+ ...state,
271
+ updatedAt: this.now(),
272
+ });
273
+ } catch {
274
+ // The checkpoint could not be saved: the compaction continues and
275
+ // the uncertainty is reported afterwards.
276
+ }
277
+ }
278
+
279
+ /**
280
+ * The text that the agent receives after a compaction.
281
+ *
282
+ * The text names the checkpoint of the request. When the checkpoint
283
+ * cannot be read, the text says so: an unknown coverage is never a
284
+ * fresh budget and never the knowledge of the whole repository.
285
+ */
286
+ continuationAfterCompaction(): string {
287
+ try {
288
+ const state = this.tracker.state(this.key());
289
+ return buildContinuationText(state);
290
+ } catch (error) {
291
+ return describeUnreadableCheckpoint((error as Error).message);
292
+ }
293
+ }
294
+
295
+ /**
296
+ * The compact checkpoint of the request, for a durable copy.
297
+ */
298
+ compactCheckpoint(): ReturnType<typeof toCompactCheckpoint> | null {
299
+ try {
300
+ return toCompactCheckpoint(this.tracker.state(this.key()), this.now());
301
+ } catch {
302
+ return null;
303
+ }
304
+ }
305
+
306
+ /**
307
+ * The status of the exploration.
308
+ */
309
+ status(): string {
310
+ try {
311
+ return this.tracker.status(this.key());
312
+ } catch {
313
+ return "investigating | unknown";
314
+ }
315
+ }
316
+ }
@@ -0,0 +1,247 @@
1
+ /**
2
+ * The bridge between the sender and the recipient of a plan.
3
+ *
4
+ * The sender publishes a plan into the mailbox of the instance it selected.
5
+ * The recipient reads its own mailbox and delivers the plans that are
6
+ * addressed to it.
7
+ *
8
+ * The bridge never delivers a plan to an instance of another workspace: the
9
+ * mailbox of an agent belongs to that agent, and the routing decided the
10
+ * recipient before the message was written.
11
+ */
12
+
13
+ import { randomUUID } from "node:crypto";
14
+ import { homedir, hostname } from "node:os";
15
+ import { join } from "node:path";
16
+ import {
17
+ Mailbox,
18
+ PairingStore,
19
+ PendingHandoffStore,
20
+ RegistrationStore,
21
+ ReplyStore,
22
+ publishPlan,
23
+ type AgentRegistration,
24
+ type PlanHandoffMessage,
25
+ type PublicationResult,
26
+ type PublishPlanContent,
27
+ type RecipientPairing,
28
+ } from "../packages/plan-handoff/dist/index.js";
29
+
30
+ /**
31
+ * The directory of the handoff state of the user.
32
+ */
33
+ export const PLAN_HANDOFF_HOME = join(
34
+ homedir(),
35
+ ".pi-harness-runtime",
36
+ "plan-handoff",
37
+ );
38
+
39
+ /**
40
+ * The directory of the registrations.
41
+ */
42
+ export const PLAN_HANDOFF_REGISTRY_DIR = join(PLAN_HANDOFF_HOME, "agents");
43
+
44
+ /**
45
+ * The directory of the retained handoffs.
46
+ */
47
+ export const PLAN_HANDOFF_RETAINED_DIR = join(PLAN_HANDOFF_HOME, "retained");
48
+
49
+ /**
50
+ * The directory of the mailboxes.
51
+ */
52
+ export const PLAN_HANDOFF_MAILBOX_DIR = join(PLAN_HANDOFF_HOME, "mailboxes");
53
+
54
+ /**
55
+ * The directory of the replies.
56
+ */
57
+ export const PLAN_HANDOFF_REPLY_DIR = join(PLAN_HANDOFF_HOME, "replies");
58
+
59
+ /**
60
+ * The file that remembers the pairing of a workspace.
61
+ */
62
+ export const PLAN_HANDOFF_PAIRING_FILE = join(
63
+ PLAN_HANDOFF_HOME,
64
+ "pairings.json",
65
+ );
66
+
67
+ /**
68
+ * The store of the registrations of the user.
69
+ */
70
+ export function planHandoffRegistry(): RegistrationStore {
71
+ return new RegistrationStore(PLAN_HANDOFF_REGISTRY_DIR);
72
+ }
73
+
74
+ /**
75
+ * The store of the pairings of the user.
76
+ */
77
+ export function planHandoffPairings(): PairingStore {
78
+ return new PairingStore(PLAN_HANDOFF_PAIRING_FILE);
79
+ }
80
+
81
+ /**
82
+ * The store of the retained handoffs of the user.
83
+ */
84
+ export function planHandoffRetained(): PendingHandoffStore {
85
+ return new PendingHandoffStore(PLAN_HANDOFF_RETAINED_DIR);
86
+ }
87
+
88
+ /**
89
+ * The store of the replies of the user.
90
+ */
91
+ export function planHandoffReplies(): ReplyStore {
92
+ return new ReplyStore(PLAN_HANDOFF_REPLY_DIR);
93
+ }
94
+
95
+ /**
96
+ * The mailbox of one agent.
97
+ */
98
+ export function planHandoffMailbox(agentId: string): Mailbox {
99
+ return new Mailbox(join(PLAN_HANDOFF_MAILBOX_DIR, agentId));
100
+ }
101
+
102
+ /**
103
+ * The options of a publication through the bridge.
104
+ */
105
+ export interface BridgePublishOptions {
106
+ /** The plan and the action it asks for. */
107
+ plan: PublishPlanContent;
108
+ /** The identity of the sender. */
109
+ sender: {
110
+ agentId: string;
111
+ sessionId: string;
112
+ workspace?: string;
113
+ label?: string;
114
+ };
115
+ /** The working directory of the sender. */
116
+ cwd?: string;
117
+ /** The host of the sender. */
118
+ host?: string;
119
+ /** The pairing that was selected before. */
120
+ pairing?: RecipientPairing;
121
+ /** The clock of the publication. */
122
+ now?: () => number;
123
+ }
124
+
125
+ /**
126
+ * The result of a publication through the bridge.
127
+ */
128
+ export interface BridgePublishResult {
129
+ /** What the publication decided. */
130
+ outcome: "addressed" | "needs-recipient" | "retained";
131
+ /** The instance the plan was addressed to. */
132
+ recipient?: AgentRegistration;
133
+ /** The instances that matched, for a selection by the user. */
134
+ candidates: AgentRegistration[];
135
+ /** Why the publication decided that, in words. */
136
+ reason: string;
137
+ /** The message, when the plan could be addressed. */
138
+ message?: PlanHandoffMessage;
139
+ }
140
+
141
+ /**
142
+ * Publish a plan into the mailbox of the selected instance.
143
+ *
144
+ * A plan without a recipient is retained: it waits for an instance of the
145
+ * workspace, and it is never delivered to an instance of another project.
146
+ */
147
+ export function publishPlanThroughBridge(
148
+ options: BridgePublishOptions,
149
+ ): BridgePublishResult {
150
+ const registry = planHandoffRegistry();
151
+ const result: PublicationResult = publishPlan({
152
+ plan: options.plan,
153
+ sender: options.sender,
154
+ registrations: registry.live(),
155
+ host: options.host ?? hostname(),
156
+ pairing: options.pairing,
157
+ cwd: options.cwd,
158
+ now: options.now,
159
+ });
160
+
161
+ if (result.outcome !== "addressed" || !result.message || !result.recipient) {
162
+ if (result.outcome === "retained") {
163
+ // The plan waits for a recipient of its workspace. It keeps its own
164
+ // identity, so a later claim of the retained handoff names the
165
+ // exact plan that was published.
166
+ const now = options.now ? options.now() : Date.now();
167
+ const id = randomUUID();
168
+ planHandoffRetained().retainWithExpiry({
169
+ id,
170
+ correlationId: id,
171
+ workspace:
172
+ options.sender.workspace ??
173
+ options.cwd ??
174
+ process.cwd(),
175
+ state: "awaiting-recipient",
176
+ createdAt: now,
177
+ summary: options.plan.plan.summary,
178
+ ...(options.plan.plan.reference
179
+ ? { reference: options.plan.plan.reference }
180
+ : {}),
181
+ }, now);
182
+ }
183
+ return {
184
+ outcome: result.outcome,
185
+ candidates: result.candidates,
186
+ reason: result.reason,
187
+ };
188
+ }
189
+
190
+ planHandoffMailbox(result.recipient.agentId).publish(result.message);
191
+
192
+ return {
193
+ outcome: "addressed",
194
+ recipient: result.recipient,
195
+ candidates: result.candidates,
196
+ reason: result.reason,
197
+ message: result.message,
198
+ };
199
+ }
200
+
201
+ /**
202
+ * The options of a receipt.
203
+ */
204
+ export interface BridgeReceiveOptions {
205
+ /** The registration of this instance. */
206
+ registration: AgentRegistration;
207
+ /** The clock of the receipt. */
208
+ now?: () => number;
209
+ }
210
+
211
+ /**
212
+ * The plans that are addressed to this instance and were not delivered yet.
213
+ *
214
+ * The recipient validates the message against its own registration before
215
+ * it queues anything: a plan of a session that ended, of another generation
216
+ * or of another workspace never enters the queue.
217
+ */
218
+ export function receivePlans(
219
+ options: BridgeReceiveOptions,
220
+ ): PlanHandoffMessage[] {
221
+ const mailbox = planHandoffMailbox(options.registration.agentId);
222
+ const received: PlanHandoffMessage[] = [];
223
+ for (const entry of mailbox.pending()) {
224
+ const message = entry.message;
225
+ if (message.recipient.agentId !== options.registration.agentId) continue;
226
+ if (message.recipient.sessionId !== options.registration.sessionId) continue;
227
+ if (message.recipient.generation !== options.registration.generation) continue;
228
+ received.push(message);
229
+ }
230
+ return received;
231
+ }
232
+
233
+ /**
234
+ * Acknowledge the delivery of a plan.
235
+ */
236
+ export function acknowledgePlan(agentId: string, messageId: string): boolean {
237
+ const acknowledged = planHandoffMailbox(agentId).acknowledge(messageId);
238
+ return acknowledged?.state === "delivered";
239
+ }
240
+
241
+ /**
242
+ * Claim a plan for this instance.
243
+ */
244
+ export function claimPlan(agentId: string, messageId: string): boolean {
245
+ const claimed = planHandoffMailbox(agentId).claim(messageId);
246
+ return claimed?.state === "claimed";
247
+ }