pi-harness-runtime 1.1.508 → 1.1.509

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 (61) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/harness/auto-resume-marker.ts +55 -13
  3. package/harness/scope-expansion-runtime.ts +142 -15
  4. package/harness/scope-expansion.ts +150 -35
  5. package/index.ts +130 -42
  6. package/package.json +1 -1
  7. package/packages/agent-policy/package.json +1 -1
  8. package/packages/auth/package.json +1 -1
  9. package/packages/autonomous-runtime/package.json +1 -1
  10. package/packages/checkpoint/package.json +1 -1
  11. package/packages/clipboard/package.json +1 -1
  12. package/packages/codex-watcher/package.json +1 -1
  13. package/packages/config-capture/package.json +1 -1
  14. package/packages/contracts/package.json +1 -1
  15. package/packages/cookie-sanitizer/package.json +1 -1
  16. package/packages/evaluation-engine/package.json +1 -1
  17. package/packages/event-bus/package.json +1 -1
  18. package/packages/event-store/package.json +1 -1
  19. package/packages/experience-replay/package.json +1 -1
  20. package/packages/file-copy-helper/package.json +1 -1
  21. package/packages/intent-analyzer/package.json +1 -1
  22. package/packages/jev-judge/package.json +1 -1
  23. package/packages/knowledge-retrieval/package.json +1 -1
  24. package/packages/learning-engine/package.json +1 -1
  25. package/packages/logging/package.json +1 -1
  26. package/packages/memory-engine/package.json +1 -1
  27. package/packages/notification/dist/notification-center.d.ts +8 -4
  28. package/packages/notification/dist/notification-center.d.ts.map +1 -1
  29. package/packages/notification/dist/notification-center.js +18 -9
  30. package/packages/notification/dist/notification-center.js.map +1 -1
  31. package/packages/notification/dist/notification-policy.d.ts.map +1 -1
  32. package/packages/notification/dist/notification-policy.js +3 -0
  33. package/packages/notification/dist/notification-policy.js.map +1 -1
  34. package/packages/notification/dist/pending-question.d.ts +20 -1
  35. package/packages/notification/dist/pending-question.d.ts.map +1 -1
  36. package/packages/notification/dist/pending-question.js +9 -3
  37. package/packages/notification/dist/pending-question.js.map +1 -1
  38. package/packages/notification/package.json +1 -1
  39. package/packages/notification/src/notification-center.ts +18 -9
  40. package/packages/notification/src/notification-policy.ts +3 -0
  41. package/packages/notification/src/pending-question.ts +28 -2
  42. package/packages/paperclip-integration/package.json +1 -1
  43. package/packages/performance-optimizer/package.json +1 -1
  44. package/packages/privilege-broker/package.json +1 -1
  45. package/packages/prompt-compiler/package.json +1 -1
  46. package/packages/providers/package.json +1 -1
  47. package/packages/qdrant-skills/package.json +1 -1
  48. package/packages/quota-manager/package.json +1 -1
  49. package/packages/relay/package.json +1 -1
  50. package/packages/skill-mcp-client/package.json +1 -1
  51. package/packages/skills/package.json +1 -1
  52. package/packages/todo-bd-sync/package.json +1 -1
  53. package/packages/token-estimation/package.json +1 -1
  54. package/packages/tooling-preference/package.json +1 -1
  55. package/packages/trajectory/package.json +1 -1
  56. package/packages/tui/package.json +1 -1
  57. package/packages/two-phase-review/package.json +1 -1
  58. package/packages/types/package.json +1 -1
  59. package/packages/webmcp-tester/package.json +1 -1
  60. package/packages/wiki/package.json +1 -1
  61. package/packages/write-review/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -2,6 +2,93 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.1.509](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.509) (2026-11-11)
6
+
7
+
8
+ ### Features
9
+
10
+ * **actions:** register the job resume, the GLM countdown and the queued continuations ([177c964](https://github.com/ManotLuijiu/pi-harness-runtime/commit/177c964555b0057e6bc7d293dafab10c95eabbf9))
11
+ * **delegation:** a child is only a child when it really ran ([ac4e678](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ac4e6782d7d4bc4941e458425c8a9f38a51fc9d4))
12
+ * **delegation:** the scope of a request is measured before it is judged ([684b6a5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/684b6a55184f63d3be24c48dcecfb0b13410dc15))
13
+ * **harness:** the startup tells the user that the session keeps its issues with bd ([81d63c5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/81d63c5dc2452b9f08001756866db80046a2532a))
14
+ * **honcho:** the memory service speaks the native MCP of Pi ([f9f740c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f9f740c12869bae4c4380afbe8c76e12388bb744))
15
+ * **jev:** one decision per question, bound to the question it was evaluated for ([03448aa](https://github.com/ManotLuijiu/pi-harness-runtime/commit/03448aaa17f2d0c3bcf644b0b1d5949727dfff8c))
16
+ * **jev:** the evaluation of a choice sees what the options mean ([f0c207e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f0c207e074701e6b7dab0965170ec39453d9d502))
17
+ * **jev:** the pending question carries the ask and the answer ([a792133](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a792133e517fb8c12d3363e98942f3af30270f27))
18
+ * **jev:** the pending question counts down to the decision ([d0cc171](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d0cc171223986b2d32c670b6c1c74712fe0a4ad8))
19
+ * **keys:** tell the user what to do about an exposed key file ([7031003](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7031003574307351b7a8f28ee14159b58e17b793))
20
+ * **keys:** the key files are repaired when their permissions are unsafe ([e9c2c26](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e9c2c26b287665b56ed749de269737e8403bb8af))
21
+ * **mcp:** the harness connects the registered servers when nobody else does ([7fef951](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7fef95137ad4664df57f045cd91b7d233315b0bc))
22
+ * **merge:** a manual run can leave the merge to the pull request event ([b3d1950](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b3d1950b5941c78ccd56951acdafd1d4afbe1bff))
23
+ * **phase:** the agent owns the todo list and the harness says so ([c489370](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c489370bd7926aece8cb1a77aa20ca2d2672f1fb))
24
+ * **providers:** a shared registry of the pending automatic actions and a bounded recovery ([c5f955b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c5f955bc82adcb6999a89d5bc278fa87fccee022))
25
+ * **providers:** one failure has one executor, and the retry bridge is checked ([e73c4c7](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e73c4c76ff20a9708083659bdcefe74870a84837))
26
+ * **quota:** a fresh capture and a low allowance decide the MiniMax recovery ([06d7b8e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/06d7b8e971e0da58a344f75778c98edb442562bd))
27
+ * **recovery:** a MiniMax quota exhaustion waits for the captured reset plus ten minutes ([5af08a0](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5af08a0e86c8c93fa5b7ff64a7b08e4514bc68f3))
28
+ * **review:** the review starts on its own when the implementation is done ([04f51f5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/04f51f599cd3222b6c74dd8fadb1373bd72d9bd7))
29
+ * **scope:** a marked resume and a backlog that only an approval starts ([19d3320](https://github.com/ManotLuijiu/pi-harness-runtime/commit/19d33204819cf05e678996fddf4c4621f0dd7d2f))
30
+ * **status:** the status line names the phase, the progress and the children ([0167bf3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/0167bf396a0b987c1d44f3b869351976a951dd65))
31
+ * **test:** the task waits for the final tier of the validation plan ([fa147db](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fa147dbf7d50d56e6ea48c6d8fbb4fcb190caf8a))
32
+ * **validation:** a large job starts with a measured pilot ([425964b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/425964b04a3d4fa02adfabed47ff62c5ec2b9c28))
33
+ * **validation:** the size of a job is a fact, not a phrase ([8aa6c11](https://github.com/ManotLuijiu/pi-harness-runtime/commit/8aa6c119565d0f8412dc8aeafa6b295d47d3f093))
34
+ * **workspace:** a Frappe bench names the mode of the workspace ([9d58517](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9d585172063a330992fee933316e268ae9985e3c))
35
+
36
+
37
+ ### Bug Fixes
38
+
39
+ * **ci:** every run of a required check counts, and the worst one decides ([da9e0da](https://github.com/ManotLuijiu/pi-harness-runtime/commit/da9e0da6b0ca29cb320764fd5fc1b8a8dee8b1e0))
40
+ * **ci:** the newest attempt of a run decides, not the attempt that failed ([9d7adcc](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9d7adcc31cbacc517c3c1a9bcd3956be94c908ef))
41
+ * **harness:** a raw control byte hid the request scope from the review ([852b1ff](https://github.com/ManotLuijiu/pi-harness-runtime/commit/852b1ff3afc21adb71e92b85d21ff1c88e14816a))
42
+ * **honcho:** a handshake that never finishes is named instead of hanging the startup ([d66005f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d66005f928a4f8df5de5b8a0579cbd4f8df37ff9))
43
+ * **honcho:** a probe that the end of the session cuts off is not a failure ([493fd93](https://github.com/ManotLuijiu/pi-harness-runtime/commit/493fd938ba09fdcf57bed4aaf259d81c4040909e))
44
+ * **honcho:** a slow connector is not a failed one, the row is corrected when the tools appear ([b62ac5b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b62ac5b16ad93e2bfb10cbec589b80a09b7e20b3))
45
+ * **honcho:** a tools listing that the end of the session cuts off is not a failure ([62cc8c3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/62cc8c376aade01c953ce2a1c6501ed07b74a1e9))
46
+ * **honcho:** the report names the state of the MCP server, not the registration ([762f4d5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/762f4d59e4eabcab9a1c21a61aa522c3267621b3))
47
+ * **hooks:** a tag is checked through the commit behind it ([c823e8e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c823e8e1f6973c62b5449a97c101759a4d5cbba5))
48
+ * **jev:** a completion question, a stale check and a pending validation ([827120b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/827120b4138b6f5ae253e69154de190085703de8))
49
+ * **jev:** a new request does not inherit the completion of the old one ([0f863c7](https://github.com/ManotLuijiu/pi-harness-runtime/commit/0f863c7fcaf35e261beabebef15b8ab634132fdc))
50
+ * **jev:** a prohibition must not hide a destructive action ([78a6cc1](https://github.com/ManotLuijiu/pi-harness-runtime/commit/78a6cc199e647f812e145483f7e94991358d8f34))
51
+ * **jev:** a refusal without punctuation, and only a decline of an action ([ef3dbf8](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ef3dbf87a2a1725373281613a0d48881ed5d2239))
52
+ * **jev:** a rejection during the countdown pauses the work ([9b018ce](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9b018ce1152b6724dee73cdc901b8a360ecc91ab))
53
+ * **jev:** a session switch ends the wait of the old session ([ca03d39](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ca03d399c0d4a1b2dd428ac39836ab53c0e51ab6))
54
+ * **jev:** an open question is not a permission to answer it ([e20eca2](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e20eca24bb88d385198bb12ee3ebf573af9aea86))
55
+ * **jev:** keep a comparative caveat with the option it constrains ([7be306b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7be306b0d0872713ebca9386d613ae5a8da9d5b6))
56
+ * **jev:** the action of the question on its own text, and a reply is not an authorization ([7c3fdd6](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7c3fdd6822b58bd6ccfa3bcdf1c5282cb3db52dd))
57
+ * **jev:** the action of the question, and an open question blocks the work ([1f9ff39](https://github.com/ManotLuijiu/pi-harness-runtime/commit/1f9ff39efc4ee17464c54c2ca9b583245093dbac))
58
+ * **jev:** the alternatives of a question are parsed without a quadratic pattern ([63016a3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/63016a3f0488acb801990dd46770c00d382b5858))
59
+ * **jev:** the countdown of an obsolete question does not outlive the work ([fe78b29](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fe78b29e6fc513b11a6993bc68fd9418dc77f79b))
60
+ * **jev:** the facts of an option survive the wording of the agent ([5a6250c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5a6250c07aa8d965bc6ae242cccbf519477598f8))
61
+ * **jev:** the scope decides the question, the identity guards the awaits ([ed348b2](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ed348b2f94460a3b3ac11c01afd4420ee4ec1020))
62
+ * **jev:** the scope of a request on every human path, and the todos it touched ([cec8f36](https://github.com/ManotLuijiu/pi-harness-runtime/commit/cec8f3644333b1d13a203340df80a30fef5c1ac0))
63
+ * **jev:** the subject of the scope decides, a clarification is preserved ([faf6a0f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/faf6a0fe7d0dce94d4fa48d0d7a57d126556ba6d))
64
+ * **keys:** the key directory is audited as a tree ([081cc75](https://github.com/ManotLuijiu/pi-harness-runtime/commit/081cc75ac8ceff36b0d619c3f76371dc08459d3c))
65
+ * **logging:** the sanitizer of a message writes its control characters as escapes ([f4bbdb9](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f4bbdb94bc1801d4228142bc1a1b31505c42a575))
66
+ * **mcp:** the connector and the watcher stop with the session ([4d1b87d](https://github.com/ManotLuijiu/pi-harness-runtime/commit/4d1b87d14d015749025b62bf54d6d0b6137295f7))
67
+ * **mcp:** the connector survives a runtime that cannot list the servers ([d0f555f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d0f555fa14f51de33591e572a41ded5b59a3e4dd))
68
+ * **merge:** the boolean input is read as a boolean ([971b716](https://github.com/ManotLuijiu/pi-harness-runtime/commit/971b7169a43db382dbdeb6bb9bab02ab56e0351c))
69
+ * **merge:** the manual job needs the right of the merge ([7ba685e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7ba685ec43d815f90aa35b742579e87e76c69209))
70
+ * **merge:** the merge of the pull request is the GraphQL mutation ([972bab4](https://github.com/ManotLuijiu/pi-harness-runtime/commit/972bab4b0f7faf1b0047f3bb767ef6db3de4b2fa))
71
+ * **merge:** the workflow merges the pull request the gates cleared ([d7639ec](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d7639ec9b83723510acb4a5c4e23d5d6167c3af6))
72
+ * **providers:** the escape of the user stops the recovery, and a permanent failure clears it ([cfe5797](https://github.com/ManotLuijiu/pi-harness-runtime/commit/cfe57976bf463c327e45b874fd297c3e942246b8))
73
+ * **providers:** the pause wins over a retry, and the retry starts when the run settles ([e016dd2](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e016dd2a5add5b0f3c848a225a28a59e0f5fc7f3))
74
+ * **recovery:** a cancelled countdown does not own the next attempt ([7b79f30](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7b79f301389fae48201e696d3086d257ed722fb9))
75
+ * **recovery:** a cancelled retry of Pi is a cancelled decision, a spent budget is not ([7e13f15](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7e13f156a279cded2b2d4b0c99358100b3d5e038))
76
+ * **recovery:** a manual request takes the work back from the countdown ([fa81d8f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fa81d8fd9d6160166a095e07bdf17049bbafa32d))
77
+ * **recovery:** the predicate of Pi decides the retry, the provider identity is normalized ([6c8c17b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/6c8c17b53302cea1602d192251eacd0ebbd3e8c9))
78
+ * **release:** a manual release keeps the version of its manifest ([122e661](https://github.com/ManotLuijiu/pi-harness-runtime/commit/122e661ea48cb8aff14c71dbd0221a4cf0ba4c8e))
79
+ * **release:** the alignment names the reason of a failure ([5814dd0](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5814dd0b780f5839cf8011ed0cbbbb31e7eb0418))
80
+ * **release:** the channels of the package name the release ([a7a273f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a7a273ffe8398e2b5b07ef1c321777de41792c6e))
81
+ * **release:** the release publishes and leaves the channels alone ([5871746](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5871746589c1281d855dbeb591b02419fa43f22e))
82
+ * **release:** the release pull request names the version of its branch ([a846e83](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a846e83e6de6e529f4bca05e869db0b5eebb37c5))
83
+ * **release:** the release scans the tree of the commit it publishes ([7d47a0b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7d47a0b5406480a1e1d3a52623a76aabd1e2d0ba))
84
+ * **scope:** a session that ends leaves no proposal behind ([9aad01e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9aad01e1f13446da1d01032e9967cadf66772792))
85
+ * **scope:** the findings of the review of the scope expansion ([92372e5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/92372e51ca31198593025ce8671b226d2ca7182e))
86
+ * **scope:** the question of a proposal is not obsolete and a rejection is named ([d4c58c3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d4c58c3e63e27449ef1de0f8e4e55ebbf342ff7d))
87
+ * **test:** the quota capture integration needs the runtime of bun ([86fae7d](https://github.com/ManotLuijiu/pi-harness-runtime/commit/86fae7de31e76414aa31bb75eb136155f62d7a40))
88
+ * **workspace:** a strong bench signal decides, and the question is not a fact of an option ([303a828](https://github.com/ManotLuijiu/pi-harness-runtime/commit/303a828f55aa7c4e0a611231f482489b530ff5a3))
89
+ * **workspace:** keep a client name out of the comment about the search depth ([b572e0a](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b572e0af16ff562203bda2b5c47b41807cff5a95))
90
+ * **workspace:** the repository names the mode before the mode stays unknown ([188a32c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/188a32c7a6af1465dd24714278fccc73d06f6477))
91
+
5
92
  ### [1.1.508](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.508) (2026-11-11)
6
93
 
7
94
 
@@ -20,20 +20,32 @@
20
20
  */
21
21
  export const HARNESS_AUTO_RESUME_TYPE = "harness:auto-resume";
22
22
 
23
+ /**
24
+ * What a resume continues.
25
+ *
26
+ * A resume of the request continues the request that is running. A resume
27
+ * of a turn continues a turn that was cut off, which is unfinished by
28
+ * definition. A scope that cannot be read is unknown: the harness does not
29
+ * know what the resume continues, and it does not act on it.
30
+ */
31
+ export type AutoResumeScope = "request" | "turn" | "unknown";
32
+
23
33
  /** What the marker of a resume carries. */
24
34
  export interface AutoResumeDetails {
25
35
  /** Why the resume was queued. */
26
36
  reason: string;
37
+ /** What the resume continues. */
38
+ scope: AutoResumeScope;
39
+ /** A nonce that tells this resume from another one. */
40
+ nonce: string;
27
41
  /**
28
- * What the resume continues.
42
+ * The request the resume belongs to.
29
43
  *
30
- * A resume of the request continues the request that is running. A
31
- * resume of a turn continues a turn that was cut off, which is
32
- * unfinished by definition.
44
+ * The generation of the request is its identity. A resume of a request
45
+ * that the human replaced must not continue the old work inside the
46
+ * new request, and only the identity tells the two apart.
33
47
  */
34
- scope: "request" | "turn";
35
- /** A nonce that tells this resume from another one. */
36
- nonce: string;
48
+ requestGeneration?: number;
37
49
  }
38
50
 
39
51
  /** The shape of a message of Pi that the harness can recognize. */
@@ -88,8 +100,9 @@ export function isAutoResumeMessage(
88
100
  /**
89
101
  * The details of a resume, when the message carries them.
90
102
  *
91
- * A message with the marker but without usable details is still a resume:
92
- * the details only tell why it was queued.
103
+ * The reason and the scope are read apart from each other: a marker
104
+ * without a usable reason still says what the resume continues, and a
105
+ * marker without a usable scope is not a resume of the request.
93
106
  */
94
107
  export function autoResumeDetails(
95
108
  message: unknown,
@@ -98,11 +111,24 @@ export function autoResumeDetails(
98
111
  const details = message.details;
99
112
  if (details === null || typeof details !== "object") return undefined;
100
113
  const candidate = details as Partial<AutoResumeDetails>;
101
- if (typeof candidate.reason !== "string") return undefined;
114
+ const scope: AutoResumeScope =
115
+ candidate.scope === "request" ||
116
+ candidate.scope === "turn" ||
117
+ candidate.scope === "unknown"
118
+ ? candidate.scope
119
+ : "unknown";
120
+ const reason =
121
+ typeof candidate.reason === "string" ? candidate.reason : undefined;
122
+ if (reason === undefined && scope === "unknown") return undefined;
102
123
  return {
103
- reason: candidate.reason,
104
- scope: candidate.scope === "turn" ? "turn" : "request",
124
+ reason: reason ?? "auto-resume",
125
+ scope,
105
126
  nonce: typeof candidate.nonce === "string" ? candidate.nonce : "",
127
+ requestGeneration:
128
+ typeof candidate.requestGeneration === "number" &&
129
+ Number.isFinite(candidate.requestGeneration)
130
+ ? candidate.requestGeneration
131
+ : undefined,
106
132
  };
107
133
  }
108
134
 
@@ -127,6 +153,8 @@ export function buildAutoResumeMessage(input: {
127
153
  reason: string;
128
154
  scope: "request" | "turn";
129
155
  nonce: string;
156
+ /** The request the resume belongs to. */
157
+ requestGeneration?: number;
130
158
  }): AutoResumeMessage {
131
159
  return {
132
160
  role: "custom",
@@ -137,6 +165,7 @@ export function buildAutoResumeMessage(input: {
137
165
  reason: input.reason,
138
166
  scope: input.scope,
139
167
  nonce: input.nonce,
168
+ requestGeneration: input.requestGeneration,
140
169
  },
141
170
  };
142
171
  }
@@ -154,6 +183,18 @@ export const NEUTRALIZED_RESUME_TEXT = [
154
183
  "Answer with one short sentence that says the request is complete.",
155
184
  ].join(" ");
156
185
 
186
+ /**
187
+ * The text that replaces a resume of a request that the human replaced.
188
+ *
189
+ * The request of the resume is not the request that is running: the human
190
+ * has moved on, and the resume must not continue the old work.
191
+ */
192
+ export const NEUTRALIZED_STALE_RESUME_TEXT = [
193
+ "The request that this automatic resume belongs to was replaced by a newer request.",
194
+ "Do not continue, resume or expand that older work, and do not look for other tasks.",
195
+ "Answer with one short sentence that says the newer request is the one that counts.",
196
+ ].join(" ");
197
+
157
198
  /**
158
199
  * Replace a resume of a completed request with a message that stops the turn.
159
200
  *
@@ -164,13 +205,14 @@ export const NEUTRALIZED_RESUME_TEXT = [
164
205
  export function neutralizeAutoResume(
165
206
  messages: readonly unknown[] | null | undefined,
166
207
  index: number,
208
+ text: string = NEUTRALIZED_RESUME_TEXT,
167
209
  ): unknown[] {
168
210
  if (!Array.isArray(messages)) return [];
169
211
  if (index < 0 || index >= messages.length) return [...messages];
170
212
  const replaced = [...messages];
171
213
  replaced[index] = {
172
214
  role: "user",
173
- content: [{ type: "text", text: NEUTRALIZED_RESUME_TEXT }],
215
+ content: [{ type: "text", text }],
174
216
  timestamp: Date.now(),
175
217
  };
176
218
  return replaced;
@@ -18,6 +18,7 @@
18
18
  */
19
19
 
20
20
  import {
21
+ MAX_SCOPE_CANDIDATES,
21
22
  SCOPE_EXPANSION_WINDOW_MS,
22
23
  ScopeExpansionController,
23
24
  createScopeProposal,
@@ -112,11 +113,22 @@ export class ScopeExpansionRuntime {
112
113
  private readonly controller: ScopeExpansionController;
113
114
  private timers = new Map<string, ScopeTimerHandle>();
114
115
  /** The proposal of every generation, for the group check. */
115
- private dispatched = new Map<string, ScopeProposal>();
116
+ private dispatched = new Map<string, { proposal: ScopeProposal; ids: string[] }>();
117
+ /**
118
+ * The runtime was disposed.
119
+ *
120
+ * A disposed runtime belongs to a session that ended: it decides
121
+ * nothing, it dispatches nothing and it keeps no timer. Without this
122
+ * flag a timer of the default clock - the one the wiring uses - would
123
+ * fire after the session and start work in a session that is gone.
124
+ */
125
+ private disposed = false;
116
126
  /** The proposal of every registered question. */
117
127
  private questions = new Map<string, string>();
118
128
  /** The question of every proposal. */
119
129
  private questionIds = new Map<string, string>();
130
+ /** The number of the proposals of this runtime. */
131
+ private proposalCounter = 0;
120
132
 
121
133
  constructor(deps: ScopeExpansionRuntimeDeps) {
122
134
  this.deps = deps;
@@ -129,7 +141,21 @@ export class ScopeExpansionRuntime {
129
141
  }),
130
142
  judge: (proposal) => deps.judge(proposal),
131
143
  dispatch: (proposal, ids) => {
132
- this.dispatched.set(proposal.id, proposal);
144
+ // A disposed runtime belongs to a session that ended: it
145
+ // dispatches nothing.
146
+ if (this.disposed) {
147
+ this.deps.log?.(
148
+ `The scope proposal ${proposal.id} is not dispatched: the runtime is disposed.`,
149
+ );
150
+ return;
151
+ }
152
+ // The ids that were dispatched are the ids of the group: the
153
+ // check of the group asks about the work that started, not
154
+ // about the ids a decision named.
155
+ this.dispatched.set(proposal.id, {
156
+ proposal,
157
+ ids: [...ids],
158
+ });
133
159
  deps.dispatch(proposal, ids);
134
160
  },
135
161
  log: (message) => deps.log?.(message),
@@ -156,7 +182,11 @@ export class ScopeExpansionRuntime {
156
182
  * can stop.
157
183
  */
158
184
  async propose(request: ScopeProposalRequest): Promise<ScopeProposal | null> {
159
- const candidates = request.candidates.slice();
185
+ if (this.disposed) return null;
186
+ // The candidates are bounded here as well: a proposal that names an
187
+ // id beyond the bound would find no known candidate and would
188
+ // throw where the caller expects nothing to happen.
189
+ const candidates = request.candidates.slice(0, MAX_SCOPE_CANDIDATES);
160
190
  if (candidates.length === 0) {
161
191
  this.deps.log?.("No scope proposal: the backlog has no candidate.");
162
192
  return null;
@@ -168,7 +198,15 @@ export class ScopeExpansionRuntime {
168
198
  this.deps.log?.("No scope proposal: no candidate is known.");
169
199
  return null;
170
200
  }
171
- const id = `scope-${request.generation}-${proposedIds[0]}`;
201
+ // The identity of a proposal is unique: two proposals of the same
202
+ // generation with the same first id would answer each other, and the
203
+ // second answer of the human would be dropped.
204
+ const base = `scope-${request.generation}-${proposedIds[0]}`;
205
+ let id = base;
206
+ if (this.controller.get(base)) {
207
+ this.proposalCounter += 1;
208
+ id = `scope-${request.generation}-${this.proposalCounter}-${proposedIds[0]}`;
209
+ }
172
210
  const proposal = createScopeProposal({
173
211
  id,
174
212
  sessionId: "",
@@ -193,7 +231,17 @@ export class ScopeExpansionRuntime {
193
231
  this.questions.set(questionId, proposal.id);
194
232
  this.questionIds.set(proposal.id, questionId);
195
233
  }
196
- const delivered = await this.controller.deliverProposal(proposal);
234
+ let delivered = false;
235
+ try {
236
+ delivered = await this.controller.deliverProposal(proposal);
237
+ } catch (error) {
238
+ // A delivery that throws is a delivery that happened nowhere: the
239
+ // proposal must not stay registered with an open question.
240
+ this.deps.log?.(
241
+ `The delivery of the scope proposal ${proposal.id} failed: ${String(error)}`,
242
+ );
243
+ delivered = false;
244
+ }
197
245
  if (!delivered) {
198
246
  // A proposal that reached nobody leaves no open question behind:
199
247
  // an open question blocks the work of the session.
@@ -219,11 +267,18 @@ export class ScopeExpansionRuntime {
219
267
  outcome: HumanScopeOutcome,
220
268
  reasoning: string,
221
269
  ): Promise<{ started: boolean; reason?: string }> {
270
+ if (this.disposed) {
271
+ return { started: false, reason: "the runtime is disposed" };
272
+ }
222
273
  const decision = this.controller.answer(proposalId, outcome, reasoning);
223
274
  if (!decision) {
224
275
  return { started: false, reason: "the proposal has no open answer" };
225
276
  }
226
- return this.controller.applyDecision(proposalId);
277
+ const result = await this.controller.applyDecision(proposalId);
278
+ // The question of a decided proposal is closed: an open question
279
+ // blocks the work of the session.
280
+ this.closeQuestion(proposalId);
281
+ return result;
227
282
  }
228
283
 
229
284
  /**
@@ -237,6 +292,9 @@ export class ScopeExpansionRuntime {
237
292
  outcome: HumanScopeOutcome,
238
293
  reasoning: string,
239
294
  ): Promise<{ started: boolean; reason?: string }> {
295
+ if (this.disposed) {
296
+ return { started: false, reason: "the runtime is disposed" };
297
+ }
240
298
  const proposalId = this.questions.get(questionId);
241
299
  if (!proposalId) {
242
300
  return { started: false, reason: "the question belongs to no proposal" };
@@ -263,10 +321,19 @@ export class ScopeExpansionRuntime {
263
321
  * work starts.
264
322
  */
265
323
  async evaluateDue(proposalId: string): Promise<ScopeDecision | null> {
324
+ if (this.disposed) return null;
266
325
  const decision = await this.controller.evaluateDue(proposalId);
267
326
  if (!decision) return null;
327
+ // The controller turns a failed evaluation into a wait: it
328
+ // authorizes nothing, and the decision is still delivered, so the
329
+ // human learns that the window ended without an answer.
268
330
  await this.controller.applyDecision(proposalId);
269
- return decision;
331
+ // The decision that was applied is the decision that counts: the
332
+ // controller may have replaced the decision of the evaluation.
333
+ // The question of a decided proposal is closed: an open question
334
+ // blocks the work of the session.
335
+ this.closeQuestion(proposalId);
336
+ return this.controller.get(proposalId)?.decision ?? decision;
270
337
  }
271
338
 
272
339
  /**
@@ -276,33 +343,59 @@ export class ScopeExpansionRuntime {
276
343
  * the next group of the backlog.
277
344
  */
278
345
  async checkGroupFinished(): Promise<void> {
279
- for (const proposal of this.dispatched.values()) {
280
- const ids = proposal.decision?.selectedIds ?? [];
346
+ for (const entry of this.dispatched.values()) {
347
+ const { proposal, ids } = entry;
281
348
  if (ids.length === 0) continue;
282
349
  if (!(await this.deps.groupFinished?.(proposal, ids))) continue;
283
350
  this.controller.finishGroup(proposal.id);
284
351
  this.clearTimer(proposal.id);
352
+ this.closeQuestion(proposal.id);
285
353
  this.dispatched.delete(proposal.id);
286
354
  }
287
355
  }
288
356
 
289
- /** Stop the runtime: no timer survives the session. */
357
+ /**
358
+ * Stop the runtime: no timer survives the session.
359
+ *
360
+ * The timer of the default clock is cancelled with the same clock that
361
+ * armed it: a runtime that keeps its timer would decide the backlog of
362
+ * a session that ended, and the decision would start work in a session
363
+ * that does not exist any more.
364
+ */
290
365
  dispose(): void {
366
+ this.disposed = true;
291
367
  for (const handle of this.timers.values()) {
292
- this.deps.clearTimeout?.(handle);
368
+ this.cancelTimer(handle);
293
369
  }
294
370
  this.timers.clear();
371
+ // A question of a proposal that nobody answered must not stay open:
372
+ // the controller of the questions lives in the process, and an open
373
+ // question of a dead session blocks the next one.
374
+ for (const questionId of this.questions.keys()) {
375
+ this.deps.cancelQuestion?.(questionId);
376
+ }
377
+ this.questions.clear();
378
+ this.questionIds.clear();
295
379
  this.dispatched.clear();
296
380
  }
297
381
 
298
382
  private scheduleEvaluation(proposal: ScopeProposal): void {
299
383
  const delay = Math.max(0, proposal.deadlineAt - this.deps.now());
300
- const setTimer = this.deps.setTimeout ?? ((cb: () => void, ms: number) => setTimeout(cb, ms));
301
- const handle = setTimer(() => {
384
+ const handle = this.armTimer(() => {
302
385
  this.timers.delete(proposal.id);
303
386
  return (async () => {
387
+ if (this.disposed) return;
304
388
  try {
305
- await this.evaluateDue(proposal.id);
389
+ const decision = await this.evaluateDue(proposal.id);
390
+ // A timer that fired before the deadline leaves the window
391
+ // open: the timer is armed again for the rest of it, so
392
+ // the proposal always ends in a decision.
393
+ if (decision === null && !this.disposed) {
394
+ const current = this.controller.get(proposal.id);
395
+ if (current?.state === "proposed") {
396
+ this.scheduleEvaluation(proposal);
397
+ }
398
+ }
306
399
  } catch (error) {
307
400
  this.deps.log?.(
308
401
  `The evaluation of the scope proposal ${proposal.id} failed: ${String(error)}`,
@@ -313,13 +406,47 @@ export class ScopeExpansionRuntime {
313
406
  this.timers.set(proposal.id, handle);
314
407
  }
315
408
 
409
+ /** Arm a timer on the clock of the runtime, or on the default clock. */
410
+ private armTimer(
411
+ callback: () => void | Promise<void>,
412
+ ms: number,
413
+ ): ScopeTimerHandle {
414
+ const setTimer =
415
+ this.deps.setTimeout ??
416
+ ((cb: () => void, delay: number) => setTimeout(cb, delay));
417
+ return setTimer(callback, ms);
418
+ }
419
+
420
+ /** Cancel a timer on the clock that armed it. */
421
+ private cancelTimer(handle: ScopeTimerHandle): void {
422
+ const clearTimer =
423
+ this.deps.clearTimeout ??
424
+ ((h: ScopeTimerHandle) =>
425
+ clearTimeout(h as ReturnType<typeof setTimeout>));
426
+ clearTimer(handle);
427
+ }
428
+
316
429
  private clearTimer(id: string): void {
317
430
  const handle = this.timers.get(id);
318
431
  if (handle === undefined) return;
319
- this.deps.clearTimeout?.(handle);
432
+ this.cancelTimer(handle);
320
433
  this.timers.delete(id);
321
434
  }
322
435
 
436
+ /**
437
+ * Close the question of a proposal.
438
+ *
439
+ * A proposal that reached its end does not keep a question open: an
440
+ * open question blocks the work of the session.
441
+ */
442
+ private closeQuestion(proposalId: string): void {
443
+ const questionId = this.questionIds.get(proposalId);
444
+ if (!questionId) return;
445
+ this.deps.cancelQuestion?.(questionId);
446
+ this.questions.delete(questionId);
447
+ this.questionIds.delete(proposalId);
448
+ }
449
+
323
450
  /**
324
451
  * The message of a proposal, for the caller that delivers it itself.
325
452
  */