pi-harness-runtime 1.1.506 → 1.1.508

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 (88) hide show
  1. package/CHANGELOG.md +166 -0
  2. package/harness/auto-resume-marker.ts +177 -0
  3. package/harness/provider-quota-reset.ts +53 -19
  4. package/harness/provider-recovery-runtime.ts +20 -0
  5. package/harness/quota-capture.ts +150 -0
  6. package/harness/quota-low-remaining.ts +75 -0
  7. package/harness/quota-reset-duration.ts +87 -0
  8. package/harness/quota-threshold.ts +117 -0
  9. package/harness/resume-scope.ts +85 -0
  10. package/harness/scope-expansion-runtime.ts +337 -0
  11. package/harness/scope-expansion.ts +512 -0
  12. package/index.ts +608 -14
  13. package/mirror.ts +4 -0
  14. package/package.json +1 -1
  15. package/packages/agent-policy/package.json +1 -1
  16. package/packages/auth/package.json +1 -1
  17. package/packages/autonomous-runtime/package.json +1 -1
  18. package/packages/checkpoint/package.json +1 -1
  19. package/packages/clipboard/package.json +1 -1
  20. package/packages/codex-watcher/package.json +1 -1
  21. package/packages/config-capture/package.json +1 -1
  22. package/packages/contracts/package.json +1 -1
  23. package/packages/cookie-sanitizer/package.json +1 -1
  24. package/packages/evaluation-engine/package.json +1 -1
  25. package/packages/event-bus/package.json +1 -1
  26. package/packages/event-store/package.json +1 -1
  27. package/packages/experience-replay/package.json +1 -1
  28. package/packages/file-copy-helper/package.json +1 -1
  29. package/packages/intent-analyzer/package.json +1 -1
  30. package/packages/jev-judge/package.json +1 -1
  31. package/packages/knowledge-retrieval/package.json +1 -1
  32. package/packages/learning-engine/package.json +1 -1
  33. package/packages/logging/package.json +1 -1
  34. package/packages/memory-engine/package.json +1 -1
  35. package/packages/notification/dist/adapters/detail-format.d.ts +5 -2
  36. package/packages/notification/dist/adapters/detail-format.d.ts.map +1 -1
  37. package/packages/notification/dist/adapters/detail-format.js +29 -0
  38. package/packages/notification/dist/adapters/detail-format.js.map +1 -1
  39. package/packages/notification/dist/adapters/email-adapter.d.ts.map +1 -1
  40. package/packages/notification/dist/adapters/email-adapter.js +3 -0
  41. package/packages/notification/dist/adapters/email-adapter.js.map +1 -1
  42. package/packages/notification/dist/adapters/line-adapter.d.ts.map +1 -1
  43. package/packages/notification/dist/adapters/line-adapter.js +3 -0
  44. package/packages/notification/dist/adapters/line-adapter.js.map +1 -1
  45. package/packages/notification/dist/adapters/ntfy-adapter.d.ts.map +1 -1
  46. package/packages/notification/dist/adapters/ntfy-adapter.js +3 -0
  47. package/packages/notification/dist/adapters/ntfy-adapter.js.map +1 -1
  48. package/packages/notification/dist/adapters/telegram-adapter.d.ts +0 -1
  49. package/packages/notification/dist/adapters/telegram-adapter.d.ts.map +1 -1
  50. package/packages/notification/dist/adapters/telegram-adapter.js +3 -3
  51. package/packages/notification/dist/adapters/telegram-adapter.js.map +1 -1
  52. package/packages/notification/dist/notification-center.d.ts.map +1 -1
  53. package/packages/notification/dist/notification-center.js +24 -0
  54. package/packages/notification/dist/notification-center.js.map +1 -1
  55. package/packages/notification/dist/notification-policy.d.ts.map +1 -1
  56. package/packages/notification/dist/notification-policy.js +3 -0
  57. package/packages/notification/dist/notification-policy.js.map +1 -1
  58. package/packages/notification/dist/types.d.ts +23 -1
  59. package/packages/notification/dist/types.d.ts.map +1 -1
  60. package/packages/notification/package.json +1 -1
  61. package/packages/notification/src/adapters/detail-format.ts +45 -2
  62. package/packages/notification/src/adapters/email-adapter.ts +3 -0
  63. package/packages/notification/src/adapters/line-adapter.ts +3 -0
  64. package/packages/notification/src/adapters/ntfy-adapter.ts +3 -0
  65. package/packages/notification/src/adapters/telegram-adapter.ts +3 -4
  66. package/packages/notification/src/notification-center.ts +28 -0
  67. package/packages/notification/src/notification-policy.ts +3 -0
  68. package/packages/notification/src/types.ts +25 -0
  69. package/packages/paperclip-integration/package.json +1 -1
  70. package/packages/performance-optimizer/package.json +1 -1
  71. package/packages/privilege-broker/package.json +1 -1
  72. package/packages/prompt-compiler/package.json +1 -1
  73. package/packages/providers/package.json +1 -1
  74. package/packages/qdrant-skills/package.json +1 -1
  75. package/packages/quota-manager/package.json +1 -1
  76. package/packages/relay/package.json +1 -1
  77. package/packages/skill-mcp-client/package.json +1 -1
  78. package/packages/skills/package.json +1 -1
  79. package/packages/todo-bd-sync/package.json +1 -1
  80. package/packages/token-estimation/package.json +1 -1
  81. package/packages/tooling-preference/package.json +1 -1
  82. package/packages/trajectory/package.json +1 -1
  83. package/packages/tui/package.json +1 -1
  84. package/packages/two-phase-review/package.json +1 -1
  85. package/packages/types/package.json +1 -1
  86. package/packages/webmcp-tester/package.json +1 -1
  87. package/packages/wiki/package.json +1 -1
  88. package/packages/write-review/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -2,6 +2,172 @@
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.508](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.508) (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
+ * **test:** the quota capture integration needs the runtime of bun ([86fae7d](https://github.com/ManotLuijiu/pi-harness-runtime/commit/86fae7de31e76414aa31bb75eb136155f62d7a40))
85
+ * **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))
86
+ * **workspace:** keep a client name out of the comment about the search depth ([b572e0a](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b572e0af16ff562203bda2b5c47b41807cff5a95))
87
+ * **workspace:** the repository names the mode before the mode stays unknown ([188a32c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/188a32c7a6af1465dd24714278fccc73d06f6477))
88
+
89
+ ### [1.1.507](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.507) (2026-11-11)
90
+
91
+
92
+ ### Features
93
+
94
+ * **actions:** register the job resume, the GLM countdown and the queued continuations ([177c964](https://github.com/ManotLuijiu/pi-harness-runtime/commit/177c964555b0057e6bc7d293dafab10c95eabbf9))
95
+ * **delegation:** a child is only a child when it really ran ([ac4e678](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ac4e6782d7d4bc4941e458425c8a9f38a51fc9d4))
96
+ * **delegation:** the scope of a request is measured before it is judged ([684b6a5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/684b6a55184f63d3be24c48dcecfb0b13410dc15))
97
+ * **harness:** the startup tells the user that the session keeps its issues with bd ([81d63c5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/81d63c5dc2452b9f08001756866db80046a2532a))
98
+ * **honcho:** the memory service speaks the native MCP of Pi ([f9f740c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f9f740c12869bae4c4380afbe8c76e12388bb744))
99
+ * **jev:** one decision per question, bound to the question it was evaluated for ([03448aa](https://github.com/ManotLuijiu/pi-harness-runtime/commit/03448aaa17f2d0c3bcf644b0b1d5949727dfff8c))
100
+ * **jev:** the evaluation of a choice sees what the options mean ([f0c207e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f0c207e074701e6b7dab0965170ec39453d9d502))
101
+ * **jev:** the pending question carries the ask and the answer ([a792133](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a792133e517fb8c12d3363e98942f3af30270f27))
102
+ * **jev:** the pending question counts down to the decision ([d0cc171](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d0cc171223986b2d32c670b6c1c74712fe0a4ad8))
103
+ * **keys:** tell the user what to do about an exposed key file ([7031003](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7031003574307351b7a8f28ee14159b58e17b793))
104
+ * **keys:** the key files are repaired when their permissions are unsafe ([e9c2c26](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e9c2c26b287665b56ed749de269737e8403bb8af))
105
+ * **mcp:** the harness connects the registered servers when nobody else does ([7fef951](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7fef95137ad4664df57f045cd91b7d233315b0bc))
106
+ * **merge:** a manual run can leave the merge to the pull request event ([b3d1950](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b3d1950b5941c78ccd56951acdafd1d4afbe1bff))
107
+ * **phase:** the agent owns the todo list and the harness says so ([c489370](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c489370bd7926aece8cb1a77aa20ca2d2672f1fb))
108
+ * **providers:** a shared registry of the pending automatic actions and a bounded recovery ([c5f955b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c5f955bc82adcb6999a89d5bc278fa87fccee022))
109
+ * **providers:** one failure has one executor, and the retry bridge is checked ([e73c4c7](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e73c4c76ff20a9708083659bdcefe74870a84837))
110
+ * **quota:** a fresh capture and a low allowance decide the MiniMax recovery ([06d7b8e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/06d7b8e971e0da58a344f75778c98edb442562bd))
111
+ * **recovery:** a MiniMax quota exhaustion waits for the captured reset plus ten minutes ([5af08a0](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5af08a0e86c8c93fa5b7ff64a7b08e4514bc68f3))
112
+ * **review:** the review starts on its own when the implementation is done ([04f51f5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/04f51f599cd3222b6c74dd8fadb1373bd72d9bd7))
113
+ * **status:** the status line names the phase, the progress and the children ([0167bf3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/0167bf396a0b987c1d44f3b869351976a951dd65))
114
+ * **test:** the task waits for the final tier of the validation plan ([fa147db](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fa147dbf7d50d56e6ea48c6d8fbb4fcb190caf8a))
115
+ * **validation:** a large job starts with a measured pilot ([425964b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/425964b04a3d4fa02adfabed47ff62c5ec2b9c28))
116
+ * **validation:** the size of a job is a fact, not a phrase ([8aa6c11](https://github.com/ManotLuijiu/pi-harness-runtime/commit/8aa6c119565d0f8412dc8aeafa6b295d47d3f093))
117
+ * **workspace:** a Frappe bench names the mode of the workspace ([9d58517](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9d585172063a330992fee933316e268ae9985e3c))
118
+
119
+
120
+ ### Bug Fixes
121
+
122
+ * **ci:** every run of a required check counts, and the worst one decides ([da9e0da](https://github.com/ManotLuijiu/pi-harness-runtime/commit/da9e0da6b0ca29cb320764fd5fc1b8a8dee8b1e0))
123
+ * **ci:** the newest attempt of a run decides, not the attempt that failed ([9d7adcc](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9d7adcc31cbacc517c3c1a9bcd3956be94c908ef))
124
+ * **harness:** a raw control byte hid the request scope from the review ([852b1ff](https://github.com/ManotLuijiu/pi-harness-runtime/commit/852b1ff3afc21adb71e92b85d21ff1c88e14816a))
125
+ * **honcho:** a handshake that never finishes is named instead of hanging the startup ([d66005f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d66005f928a4f8df5de5b8a0579cbd4f8df37ff9))
126
+ * **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))
127
+ * **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))
128
+ * **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))
129
+ * **honcho:** the report names the state of the MCP server, not the registration ([762f4d5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/762f4d59e4eabcab9a1c21a61aa522c3267621b3))
130
+ * **hooks:** a tag is checked through the commit behind it ([c823e8e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c823e8e1f6973c62b5449a97c101759a4d5cbba5))
131
+ * **jev:** a completion question, a stale check and a pending validation ([827120b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/827120b4138b6f5ae253e69154de190085703de8))
132
+ * **jev:** a new request does not inherit the completion of the old one ([0f863c7](https://github.com/ManotLuijiu/pi-harness-runtime/commit/0f863c7fcaf35e261beabebef15b8ab634132fdc))
133
+ * **jev:** a prohibition must not hide a destructive action ([78a6cc1](https://github.com/ManotLuijiu/pi-harness-runtime/commit/78a6cc199e647f812e145483f7e94991358d8f34))
134
+ * **jev:** a refusal without punctuation, and only a decline of an action ([ef3dbf8](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ef3dbf87a2a1725373281613a0d48881ed5d2239))
135
+ * **jev:** a rejection during the countdown pauses the work ([9b018ce](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9b018ce1152b6724dee73cdc901b8a360ecc91ab))
136
+ * **jev:** a session switch ends the wait of the old session ([ca03d39](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ca03d399c0d4a1b2dd428ac39836ab53c0e51ab6))
137
+ * **jev:** an open question is not a permission to answer it ([e20eca2](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e20eca24bb88d385198bb12ee3ebf573af9aea86))
138
+ * **jev:** keep a comparative caveat with the option it constrains ([7be306b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7be306b0d0872713ebca9386d613ae5a8da9d5b6))
139
+ * **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))
140
+ * **jev:** the action of the question, and an open question blocks the work ([1f9ff39](https://github.com/ManotLuijiu/pi-harness-runtime/commit/1f9ff39efc4ee17464c54c2ca9b583245093dbac))
141
+ * **jev:** the alternatives of a question are parsed without a quadratic pattern ([63016a3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/63016a3f0488acb801990dd46770c00d382b5858))
142
+ * **jev:** the countdown of an obsolete question does not outlive the work ([fe78b29](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fe78b29e6fc513b11a6993bc68fd9418dc77f79b))
143
+ * **jev:** the facts of an option survive the wording of the agent ([5a6250c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5a6250c07aa8d965bc6ae242cccbf519477598f8))
144
+ * **jev:** the scope decides the question, the identity guards the awaits ([ed348b2](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ed348b2f94460a3b3ac11c01afd4420ee4ec1020))
145
+ * **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))
146
+ * **jev:** the subject of the scope decides, a clarification is preserved ([faf6a0f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/faf6a0fe7d0dce94d4fa48d0d7a57d126556ba6d))
147
+ * **keys:** the key directory is audited as a tree ([081cc75](https://github.com/ManotLuijiu/pi-harness-runtime/commit/081cc75ac8ceff36b0d619c3f76371dc08459d3c))
148
+ * **logging:** the sanitizer of a message writes its control characters as escapes ([f4bbdb9](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f4bbdb94bc1801d4228142bc1a1b31505c42a575))
149
+ * **mcp:** the connector and the watcher stop with the session ([4d1b87d](https://github.com/ManotLuijiu/pi-harness-runtime/commit/4d1b87d14d015749025b62bf54d6d0b6137295f7))
150
+ * **mcp:** the connector survives a runtime that cannot list the servers ([d0f555f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d0f555fa14f51de33591e572a41ded5b59a3e4dd))
151
+ * **merge:** the boolean input is read as a boolean ([971b716](https://github.com/ManotLuijiu/pi-harness-runtime/commit/971b7169a43db382dbdeb6bb9bab02ab56e0351c))
152
+ * **merge:** the manual job needs the right of the merge ([7ba685e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7ba685ec43d815f90aa35b742579e87e76c69209))
153
+ * **merge:** the merge of the pull request is the GraphQL mutation ([972bab4](https://github.com/ManotLuijiu/pi-harness-runtime/commit/972bab4b0f7faf1b0047f3bb767ef6db3de4b2fa))
154
+ * **merge:** the workflow merges the pull request the gates cleared ([d7639ec](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d7639ec9b83723510acb4a5c4e23d5d6167c3af6))
155
+ * **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))
156
+ * **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))
157
+ * **recovery:** a cancelled countdown does not own the next attempt ([7b79f30](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7b79f301389fae48201e696d3086d257ed722fb9))
158
+ * **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))
159
+ * **recovery:** a manual request takes the work back from the countdown ([fa81d8f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fa81d8fd9d6160166a095e07bdf17049bbafa32d))
160
+ * **recovery:** the predicate of Pi decides the retry, the provider identity is normalized ([6c8c17b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/6c8c17b53302cea1602d192251eacd0ebbd3e8c9))
161
+ * **release:** a manual release keeps the version of its manifest ([122e661](https://github.com/ManotLuijiu/pi-harness-runtime/commit/122e661ea48cb8aff14c71dbd0221a4cf0ba4c8e))
162
+ * **release:** the alignment names the reason of a failure ([5814dd0](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5814dd0b780f5839cf8011ed0cbbbb31e7eb0418))
163
+ * **release:** the channels of the package name the release ([a7a273f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a7a273ffe8398e2b5b07ef1c321777de41792c6e))
164
+ * **release:** the release publishes and leaves the channels alone ([5871746](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5871746589c1281d855dbeb591b02419fa43f22e))
165
+ * **release:** the release pull request names the version of its branch ([a846e83](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a846e83e6de6e529f4bca05e869db0b5eebb37c5))
166
+ * **release:** the release scans the tree of the commit it publishes ([7d47a0b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7d47a0b5406480a1e1d3a52623a76aabd1e2d0ba))
167
+ * **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))
168
+ * **workspace:** keep a client name out of the comment about the search depth ([b572e0a](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b572e0af16ff562203bda2b5c47b41807cff5a95))
169
+ * **workspace:** the repository names the mode before the mode stays unknown ([188a32c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/188a32c7a6af1465dd24714278fccc73d06f6477))
170
+
5
171
  ### [1.1.506](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.506) (2026-11-11)
6
172
 
7
173
 
@@ -0,0 +1,177 @@
1
+ /**
2
+ * The marker of an automatic resume.
3
+ *
4
+ * An automatic resume is a message of the harness, not a message of the
5
+ * human. It carries a marker, so the harness can recognize it later: a
6
+ * resume that was queued while the request ran and that is consumed after
7
+ * the request finished must not send the agent to the backlog.
8
+ *
9
+ * The marker is a custom message of Pi. Pi removes the marker before the
10
+ * provider sees the message, so the content of the resume stays the
11
+ * ordinary text of the resume.
12
+ */
13
+
14
+ /**
15
+ * The custom type of the message that the harness queues as a resume.
16
+ *
17
+ * The prefix `harness:` is the convention of the harness for the messages
18
+ * that it generates itself: every path that skips the traffic of the
19
+ * harness recognizes it.
20
+ */
21
+ export const HARNESS_AUTO_RESUME_TYPE = "harness:auto-resume";
22
+
23
+ /** What the marker of a resume carries. */
24
+ export interface AutoResumeDetails {
25
+ /** Why the resume was queued. */
26
+ reason: string;
27
+ /**
28
+ * What the resume continues.
29
+ *
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.
33
+ */
34
+ scope: "request" | "turn";
35
+ /** A nonce that tells this resume from another one. */
36
+ nonce: string;
37
+ }
38
+
39
+ /** The shape of a message of Pi that the harness can recognize. */
40
+ export interface MaybeAutoResumeMessage {
41
+ role?: string;
42
+ customType?: string;
43
+ content?: unknown;
44
+ display?: boolean;
45
+ details?: unknown;
46
+ }
47
+
48
+ /**
49
+ * A message that carries the marker of the harness.
50
+ *
51
+ * The marker is the proof; the details are read from it when they are
52
+ * usable, and a marker without details is still a marker.
53
+ */
54
+ export interface AutoResumeCarrier {
55
+ role: "custom";
56
+ customType: typeof HARNESS_AUTO_RESUME_TYPE;
57
+ content?: unknown;
58
+ display?: boolean;
59
+ details?: unknown;
60
+ }
61
+
62
+ /** The message that the harness queues as a resume. */
63
+ export interface AutoResumeMessage {
64
+ role: "custom";
65
+ customType: typeof HARNESS_AUTO_RESUME_TYPE;
66
+ content: string;
67
+ display: boolean;
68
+ details: AutoResumeDetails;
69
+ }
70
+
71
+ /**
72
+ * Is this message the resume of the harness?
73
+ *
74
+ * Only the explicit marker counts. A message of the human that says
75
+ * "resume" is ordinary text and authorizes nothing.
76
+ */
77
+ export function isAutoResumeMessage(
78
+ message: unknown,
79
+ ): message is AutoResumeCarrier {
80
+ if (message === null || typeof message !== "object") return false;
81
+ const candidate = message as MaybeAutoResumeMessage;
82
+ return (
83
+ candidate.role === "custom" &&
84
+ candidate.customType === HARNESS_AUTO_RESUME_TYPE
85
+ );
86
+ }
87
+
88
+ /**
89
+ * The details of a resume, when the message carries them.
90
+ *
91
+ * A message with the marker but without usable details is still a resume:
92
+ * the details only tell why it was queued.
93
+ */
94
+ export function autoResumeDetails(
95
+ message: unknown,
96
+ ): AutoResumeDetails | undefined {
97
+ if (!isAutoResumeMessage(message)) return undefined;
98
+ const details = message.details;
99
+ if (details === null || typeof details !== "object") return undefined;
100
+ const candidate = details as Partial<AutoResumeDetails>;
101
+ if (typeof candidate.reason !== "string") return undefined;
102
+ return {
103
+ reason: candidate.reason,
104
+ scope: candidate.scope === "turn" ? "turn" : "request",
105
+ nonce: typeof candidate.nonce === "string" ? candidate.nonce : "",
106
+ };
107
+ }
108
+
109
+ /**
110
+ * The index of the resume in the messages, or -1 when there is none.
111
+ *
112
+ * The last resume wins: it is the one that is consumed.
113
+ */
114
+ export function findAutoResumeIndex(
115
+ messages: readonly unknown[] | null | undefined,
116
+ ): number {
117
+ if (!Array.isArray(messages)) return -1;
118
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
119
+ if (isAutoResumeMessage(messages[index])) return index;
120
+ }
121
+ return -1;
122
+ }
123
+
124
+ /** The message that the harness queues as a resume. */
125
+ export function buildAutoResumeMessage(input: {
126
+ content: string;
127
+ reason: string;
128
+ scope: "request" | "turn";
129
+ nonce: string;
130
+ }): AutoResumeMessage {
131
+ return {
132
+ role: "custom",
133
+ customType: HARNESS_AUTO_RESUME_TYPE,
134
+ content: input.content,
135
+ display: false,
136
+ details: {
137
+ reason: input.reason,
138
+ scope: input.scope,
139
+ nonce: input.nonce,
140
+ },
141
+ };
142
+ }
143
+
144
+ /**
145
+ * The text that replaces a resume of a request that is already complete.
146
+ *
147
+ * It states the truth and asks for nothing: the turn ends, and no work is
148
+ * started, continued or expanded.
149
+ */
150
+ export const NEUTRALIZED_RESUME_TEXT = [
151
+ "The request of the user is already complete.",
152
+ "There is no work left to continue.",
153
+ "Do not start, resume or expand any work, and do not look for other tasks.",
154
+ "Answer with one short sentence that says the request is complete.",
155
+ ].join(" ");
156
+
157
+ /**
158
+ * Replace a resume of a completed request with a message that stops the turn.
159
+ *
160
+ * The array keeps a valid ending: the message that replaces the resume is
161
+ * a user message, so the call of the provider stays well formed. The
162
+ * messages that are not the resume are not touched.
163
+ */
164
+ export function neutralizeAutoResume(
165
+ messages: readonly unknown[] | null | undefined,
166
+ index: number,
167
+ ): unknown[] {
168
+ if (!Array.isArray(messages)) return [];
169
+ if (index < 0 || index >= messages.length) return [...messages];
170
+ const replaced = [...messages];
171
+ replaced[index] = {
172
+ role: "user",
173
+ content: [{ type: "text", text: NEUTRALIZED_RESUME_TEXT }],
174
+ timestamp: Date.now(),
175
+ };
176
+ return replaced;
177
+ }
@@ -5,6 +5,7 @@ import {
5
5
  normalizeProviderIdentity,
6
6
  type RecoveryResetEvidence,
7
7
  } from "./provider-recovery.ts";
8
+ import { decideQuotaReset, quotaWindowState } from "./quota-threshold.ts";
8
9
 
9
10
  /** Give the quota service time to settle before probing it again. */
10
11
  export const QUOTA_RESET_BUFFER_MS = 10 * 60_000;
@@ -13,8 +14,18 @@ export const QUOTA_RESET_BUFFER_MS = 10 * 60_000;
13
14
  type QuotaWindowName = "h5" | "weekly";
14
15
 
15
16
  /**
16
- * A failed request plus fresh, fully consumed quota can identify a reset
17
- * even when the provider's generic error does not name its window.
17
+ * A failed request plus fresh quota can identify a reset even when the
18
+ * provider's generic error does not name its window.
19
+ *
20
+ * A window that is empty decides on its own: the account is blocked until
21
+ * it resets, and the bounded cycle cannot change that. A window that is
22
+ * nearly empty - below ten percent of the allowance - decides as well,
23
+ * when its reset is more than an hour away. A reset that arrives sooner is
24
+ * reached by the bounded cycle, and the cycle keeps the failure.
25
+ *
26
+ * The percentages of the mirror are the used percentages; the policy is
27
+ * stated in the remaining percentage, and the conversion is in
28
+ * `quota-threshold.ts`.
18
29
  *
19
30
  * The selector reads the capture that the harness already has. It never
20
31
  * scrapes again and never parses the text of the console: a capture that is
@@ -60,31 +71,54 @@ export function confirmedQuotaReset(
60
71
  resetAt: mirror.weekly_resets_at_epoch,
61
72
  },
62
73
  ];
74
+ // An empty window blocks the account until it resets.
63
75
  const exhausted = windows.filter(
64
- ({ used }) =>
65
- typeof used === "number" && Number.isFinite(used) && used >= 100,
76
+ ({ used }) => quotaWindowState(used) === "exhausted",
66
77
  );
67
- if (exhausted.length === 0) return undefined;
78
+ if (exhausted.length > 0) {
79
+ // If an exhausted window has no usable reset, another window cannot
80
+ // establish when the account becomes usable. Keep bounded recovery.
81
+ let latest: { window: QuotaWindowName; resetAt: number } | undefined;
82
+ for (const { window, resetAt } of exhausted) {
83
+ if (
84
+ typeof resetAt !== "number" ||
85
+ !Number.isFinite(resetAt) ||
86
+ resetAt <= now
87
+ ) {
88
+ return undefined;
89
+ }
90
+ if (!latest || resetAt > latest.resetAt) latest = { window, resetAt };
91
+ }
92
+ if (!latest) return undefined;
93
+ return {
94
+ provider: normalizeProviderIdentity(provider),
95
+ window: latest.window,
96
+ resetAt: latest.resetAt,
97
+ observedAt,
98
+ source: mirror.source,
99
+ };
100
+ }
68
101
 
69
- // If an exhausted window has no usable reset, another window cannot
70
- // establish when the account becomes usable. Keep bounded recovery.
71
- let latest: { window: QuotaWindowName; resetAt: number } | undefined;
72
- for (const { window, resetAt } of exhausted) {
73
- if (
74
- typeof resetAt !== "number" ||
75
- !Number.isFinite(resetAt) ||
76
- resetAt <= now
77
- ) {
78
- return undefined;
102
+ // No window is empty. A window that is nearly empty decides, when its
103
+ // reset is far enough away: a reset that arrives within the hour is
104
+ // reached by the bounded cycle of the failure.
105
+ const low = windows.filter(({ used }) => quotaWindowState(used) === "low");
106
+ let latestLow: { window: QuotaWindowName; resetAt: number } | undefined;
107
+ for (const { window, used, resetAt } of low) {
108
+ if (decideQuotaReset({ usedPct: used, resetAt, now }) !== "reset") {
109
+ continue;
110
+ }
111
+ if (typeof resetAt !== "number" || !Number.isFinite(resetAt)) continue;
112
+ if (!latestLow || resetAt > latestLow.resetAt) {
113
+ latestLow = { window, resetAt };
79
114
  }
80
- if (!latest || resetAt > latest.resetAt) latest = { window, resetAt };
81
115
  }
82
- if (!latest) return undefined;
116
+ if (!latestLow) return undefined;
83
117
 
84
118
  return {
85
119
  provider: normalizeProviderIdentity(provider),
86
- window: latest.window,
87
- resetAt: latest.resetAt,
120
+ window: latestLow.window,
121
+ resetAt: latestLow.resetAt,
88
122
  observedAt,
89
123
  source: mirror.source,
90
124
  };
@@ -226,6 +226,26 @@ export class ProviderRecoveryRuntime {
226
226
  };
227
227
  }
228
228
 
229
+ /**
230
+ * The failure that waits for the settled boundary.
231
+ *
232
+ * The caller reads it to prepare what the recovery needs before it
233
+ * picks a deadline - a fresh quota capture, for example. Reading it
234
+ * changes nothing: the candidate stays where it is until the run
235
+ * settles.
236
+ */
237
+ pendingQuotaFailure(): { provider: string; kind: string } | null {
238
+ const candidate = this.candidate;
239
+ if (!candidate) return null;
240
+ if (
241
+ candidate.failure.kind !== "usage-exhausted" &&
242
+ candidate.failure.kind !== "rate-limit"
243
+ ) {
244
+ return null;
245
+ }
246
+ return { provider: candidate.provider, kind: candidate.failure.kind };
247
+ }
248
+
229
249
  /**
230
250
  * The run settled.
231
251
  *
@@ -0,0 +1,150 @@
1
+ /**
2
+ * The quota capture that a provider recovery needs.
3
+ *
4
+ * The recovery decides when to continue from a capture of the quota, and
5
+ * the row of the mirror can be hours old. A capture that is forced before
6
+ * the decision refreshes that row.
7
+ *
8
+ * The capture is the one of the harness: it refreshes the mirror and
9
+ * nothing else. It never charges an attempt of the recovery and it never
10
+ * schedules anything.
11
+ *
12
+ * A capture that failed is remembered as a failure: the row of the mirror
13
+ * is the old one then, and an old row cannot prove a deadline.
14
+ */
15
+
16
+ /** The time a capture of the same failure may be reused. */
17
+ export const QUOTA_CAPTURE_REUSE_MS = 2 * 60_000;
18
+
19
+ /** The time the recovery waits for the capture. */
20
+ export const QUOTA_CAPTURE_WAIT_MS = 15_000;
21
+
22
+ /** What the last capture did. */
23
+ export interface QuotaCaptureOutcome {
24
+ /** The provider the capture was about. */
25
+ provider: string;
26
+ /** Did the capture work? */
27
+ ok: boolean;
28
+ /** When the capture ended. */
29
+ at: number;
30
+ }
31
+
32
+ /** The dependencies of the controller. */
33
+ export interface QuotaCaptureOptions {
34
+ /**
35
+ * Capture the quota of a provider.
36
+ *
37
+ * The result says whether the capture worked. A capture that throws is
38
+ * a failed capture, not a crash of the harness.
39
+ */
40
+ capture(provider: string): Promise<boolean>;
41
+ /** The clock of the controller. */
42
+ now?(): number;
43
+ /** The log of the controller. */
44
+ log?(message: string): void;
45
+ /** The time the recovery waits for the capture. */
46
+ waitMs?: number;
47
+ /** The time a capture of the same failure may be reused. */
48
+ reuseMs?: number;
49
+ }
50
+
51
+ /**
52
+ * The capture of the quota of a provider failure.
53
+ *
54
+ * One capture at a time: the browser of the harness is not a resource that
55
+ * is spent twice for one failure.
56
+ */
57
+ export class QuotaCaptureController {
58
+ private readonly options: QuotaCaptureOptions;
59
+ private readonly now: () => number;
60
+ private readonly waitMs: number;
61
+ private readonly reuseMs: number;
62
+ private pending: {
63
+ provider: string;
64
+ promise: Promise<boolean>;
65
+ startedAt: number;
66
+ } | null = null;
67
+ private last: QuotaCaptureOutcome | null = null;
68
+
69
+ constructor(options: QuotaCaptureOptions) {
70
+ this.options = options;
71
+ this.now = options.now ?? (() => Date.now());
72
+ this.waitMs = options.waitMs ?? QUOTA_CAPTURE_WAIT_MS;
73
+ this.reuseMs = options.reuseMs ?? QUOTA_CAPTURE_REUSE_MS;
74
+ }
75
+
76
+ /** The outcome of the last capture. */
77
+ get outcome(): QuotaCaptureOutcome | null {
78
+ return this.last;
79
+ }
80
+
81
+ /**
82
+ * Did the last capture of this provider fail?
83
+ *
84
+ * A failed capture means that the row of the mirror is the old one, so
85
+ * it cannot prove a deadline.
86
+ */
87
+ failed(provider: string): boolean {
88
+ const outcome = this.last;
89
+ if (!outcome) return false;
90
+ return outcome.provider === provider.toLowerCase() && !outcome.ok;
91
+ }
92
+
93
+ /**
94
+ * Start the capture of a quota failure.
95
+ *
96
+ * A capture that is running for the same provider is not started again,
97
+ * and a capture that ended recently is not repeated: its row is fresh
98
+ * enough for the decision.
99
+ */
100
+ begin(provider: string): void {
101
+ const key = provider.toLowerCase();
102
+ const current = this.pending;
103
+ if (
104
+ current &&
105
+ current.provider === key &&
106
+ current.startedAt > this.now() - this.reuseMs
107
+ ) {
108
+ return;
109
+ }
110
+ const promise = (async () => {
111
+ try {
112
+ const ok = await this.options.capture(key);
113
+ this.last = { provider: key, ok, at: this.now() };
114
+ return ok;
115
+ } catch (error) {
116
+ this.options.log?.(
117
+ `The forced quota capture of ${key} failed: ${
118
+ error instanceof Error ? error.message : String(error)
119
+ }`,
120
+ );
121
+ this.last = { provider: key, ok: false, at: this.now() };
122
+ return false;
123
+ }
124
+ })();
125
+ this.pending = { provider: key, promise, startedAt: this.now() };
126
+ }
127
+
128
+ /**
129
+ * Wait for the capture, within a bound.
130
+ *
131
+ * A capture that takes too long is given up: the recovery keeps the
132
+ * bounded cycle, and the capture that finishes later only refreshes
133
+ * the mirror.
134
+ */
135
+ async wait(): Promise<void> {
136
+ const current = this.pending;
137
+ if (!current) return;
138
+ let timer: ReturnType<typeof setTimeout> | undefined;
139
+ try {
140
+ await Promise.race([
141
+ current.promise,
142
+ new Promise<void>((resolve) => {
143
+ timer = setTimeout(resolve, this.waitMs);
144
+ }),
145
+ ]);
146
+ } finally {
147
+ if (timer) clearTimeout(timer);
148
+ }
149
+ }
150
+ }