pi-harness-runtime 1.1.485 → 1.1.486
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.
- package/CHANGELOG.md +63 -0
- package/harness/provider-failure.ts +3 -0
- package/harness/provider-recovery-runtime.ts +480 -0
- package/index.ts +138 -231
- package/package.json +1 -1
- package/packages/agent-policy/package.json +1 -1
- package/packages/auth/package.json +1 -1
- package/packages/autonomous-runtime/package.json +1 -1
- package/packages/checkpoint/package.json +1 -1
- package/packages/clipboard/package.json +1 -1
- package/packages/codex-watcher/package.json +1 -1
- package/packages/config-capture/package.json +1 -1
- package/packages/contracts/package.json +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/evaluation-engine/package.json +1 -1
- package/packages/event-bus/package.json +1 -1
- package/packages/event-store/package.json +1 -1
- package/packages/experience-replay/package.json +1 -1
- package/packages/file-copy-helper/package.json +1 -1
- package/packages/intent-analyzer/package.json +1 -1
- package/packages/jev-judge/dist/continuation-controller.d.ts +8 -0
- package/packages/jev-judge/dist/continuation-controller.d.ts.map +1 -1
- package/packages/jev-judge/dist/continuation-controller.js +10 -0
- package/packages/jev-judge/dist/continuation-controller.js.map +1 -1
- package/packages/jev-judge/package.json +1 -1
- package/packages/jev-judge/src/continuation-controller.ts +11 -0
- package/packages/knowledge-retrieval/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/logging/package.json +1 -1
- package/packages/memory-engine/package.json +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/paperclip-integration/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/privilege-broker/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/providers/package.json +1 -1
- package/packages/qdrant-skills/package.json +1 -1
- package/packages/quota-manager/package.json +1 -1
- package/packages/relay/package.json +1 -1
- package/packages/skill-mcp-client/package.json +1 -1
- package/packages/skills/package.json +1 -1
- package/packages/todo-bd-sync/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/tooling-preference/package.json +1 -1
- package/packages/trajectory/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/two-phase-review/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/webmcp-tester/package.json +1 -1
- package/packages/wiki/package.json +1 -1
- package/packages/write-review/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,69 @@
|
|
|
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.486](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.486) (2026-11-09)
|
|
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
|
+
* **honcho:** the memory service speaks the native MCP of Pi ([f9f740c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f9f740c12869bae4c4380afbe8c76e12388bb744))
|
|
14
|
+
* **jev:** one decision per question, bound to the question it was evaluated for ([03448aa](https://github.com/ManotLuijiu/pi-harness-runtime/commit/03448aaa17f2d0c3bcf644b0b1d5949727dfff8c))
|
|
15
|
+
* **jev:** the evaluation of a choice sees what the options mean ([f0c207e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/f0c207e074701e6b7dab0965170ec39453d9d502))
|
|
16
|
+
* **jev:** the pending question carries the ask and the answer ([a792133](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a792133e517fb8c12d3363e98942f3af30270f27))
|
|
17
|
+
* **jev:** the pending question counts down to the decision ([d0cc171](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d0cc171223986b2d32c670b6c1c74712fe0a4ad8))
|
|
18
|
+
* **keys:** tell the user what to do about an exposed key file ([7031003](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7031003574307351b7a8f28ee14159b58e17b793))
|
|
19
|
+
* **keys:** the key files are repaired when their permissions are unsafe ([e9c2c26](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e9c2c26b287665b56ed749de269737e8403bb8af))
|
|
20
|
+
* **mcp:** the harness connects the registered servers when nobody else does ([7fef951](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7fef95137ad4664df57f045cd91b7d233315b0bc))
|
|
21
|
+
* **merge:** a manual run can leave the merge to the pull request event ([b3d1950](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b3d1950b5941c78ccd56951acdafd1d4afbe1bff))
|
|
22
|
+
* **phase:** the agent owns the todo list and the harness says so ([c489370](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c489370bd7926aece8cb1a77aa20ca2d2672f1fb))
|
|
23
|
+
* **providers:** a shared registry of the pending automatic actions and a bounded recovery ([c5f955b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c5f955bc82adcb6999a89d5bc278fa87fccee022))
|
|
24
|
+
* **providers:** one failure has one executor, and the retry bridge is checked ([e73c4c7](https://github.com/ManotLuijiu/pi-harness-runtime/commit/e73c4c76ff20a9708083659bdcefe74870a84837))
|
|
25
|
+
* **review:** the review starts on its own when the implementation is done ([04f51f5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/04f51f599cd3222b6c74dd8fadb1373bd72d9bd7))
|
|
26
|
+
* **status:** the status line names the phase, the progress and the children ([0167bf3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/0167bf396a0b987c1d44f3b869351976a951dd65))
|
|
27
|
+
* **test:** the task waits for the final tier of the validation plan ([fa147db](https://github.com/ManotLuijiu/pi-harness-runtime/commit/fa147dbf7d50d56e6ea48c6d8fbb4fcb190caf8a))
|
|
28
|
+
* **validation:** a large job starts with a measured pilot ([425964b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/425964b04a3d4fa02adfabed47ff62c5ec2b9c28))
|
|
29
|
+
* **validation:** the size of a job is a fact, not a phrase ([8aa6c11](https://github.com/ManotLuijiu/pi-harness-runtime/commit/8aa6c119565d0f8412dc8aeafa6b295d47d3f093))
|
|
30
|
+
* **workspace:** a Frappe bench names the mode of the workspace ([9d58517](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9d585172063a330992fee933316e268ae9985e3c))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### Bug Fixes
|
|
34
|
+
|
|
35
|
+
* **ci:** every run of a required check counts, and the worst one decides ([da9e0da](https://github.com/ManotLuijiu/pi-harness-runtime/commit/da9e0da6b0ca29cb320764fd5fc1b8a8dee8b1e0))
|
|
36
|
+
* **ci:** the newest attempt of a run decides, not the attempt that failed ([9d7adcc](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9d7adcc31cbacc517c3c1a9bcd3956be94c908ef))
|
|
37
|
+
* **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))
|
|
38
|
+
* **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))
|
|
39
|
+
* **honcho:** the report names the state of the MCP server, not the registration ([762f4d5](https://github.com/ManotLuijiu/pi-harness-runtime/commit/762f4d59e4eabcab9a1c21a61aa522c3267621b3))
|
|
40
|
+
* **hooks:** a tag is checked through the commit behind it ([c823e8e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/c823e8e1f6973c62b5449a97c101759a4d5cbba5))
|
|
41
|
+
* **jev:** a prohibition must not hide a destructive action ([78a6cc1](https://github.com/ManotLuijiu/pi-harness-runtime/commit/78a6cc199e647f812e145483f7e94991358d8f34))
|
|
42
|
+
* **jev:** a refusal without punctuation, and only a decline of an action ([ef3dbf8](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ef3dbf87a2a1725373281613a0d48881ed5d2239))
|
|
43
|
+
* **jev:** a rejection during the countdown pauses the work ([9b018ce](https://github.com/ManotLuijiu/pi-harness-runtime/commit/9b018ce1152b6724dee73cdc901b8a360ecc91ab))
|
|
44
|
+
* **jev:** a session switch ends the wait of the old session ([ca03d39](https://github.com/ManotLuijiu/pi-harness-runtime/commit/ca03d399c0d4a1b2dd428ac39836ab53c0e51ab6))
|
|
45
|
+
* **jev:** keep a comparative caveat with the option it constrains ([7be306b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7be306b0d0872713ebca9386d613ae5a8da9d5b6))
|
|
46
|
+
* **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))
|
|
47
|
+
* **jev:** the action of the question, and an open question blocks the work ([1f9ff39](https://github.com/ManotLuijiu/pi-harness-runtime/commit/1f9ff39efc4ee17464c54c2ca9b583245093dbac))
|
|
48
|
+
* **jev:** the alternatives of a question are parsed without a quadratic pattern ([63016a3](https://github.com/ManotLuijiu/pi-harness-runtime/commit/63016a3f0488acb801990dd46770c00d382b5858))
|
|
49
|
+
* **jev:** the facts of an option survive the wording of the agent ([5a6250c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5a6250c07aa8d965bc6ae242cccbf519477598f8))
|
|
50
|
+
* **keys:** the key directory is audited as a tree ([081cc75](https://github.com/ManotLuijiu/pi-harness-runtime/commit/081cc75ac8ceff36b0d619c3f76371dc08459d3c))
|
|
51
|
+
* **mcp:** the connector and the watcher stop with the session ([4d1b87d](https://github.com/ManotLuijiu/pi-harness-runtime/commit/4d1b87d14d015749025b62bf54d6d0b6137295f7))
|
|
52
|
+
* **mcp:** the connector survives a runtime that cannot list the servers ([d0f555f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d0f555fa14f51de33591e572a41ded5b59a3e4dd))
|
|
53
|
+
* **merge:** the boolean input is read as a boolean ([971b716](https://github.com/ManotLuijiu/pi-harness-runtime/commit/971b7169a43db382dbdeb6bb9bab02ab56e0351c))
|
|
54
|
+
* **merge:** the manual job needs the right of the merge ([7ba685e](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7ba685ec43d815f90aa35b742579e87e76c69209))
|
|
55
|
+
* **merge:** the merge of the pull request is the GraphQL mutation ([972bab4](https://github.com/ManotLuijiu/pi-harness-runtime/commit/972bab4b0f7faf1b0047f3bb767ef6db3de4b2fa))
|
|
56
|
+
* **merge:** the workflow merges the pull request the gates cleared ([d7639ec](https://github.com/ManotLuijiu/pi-harness-runtime/commit/d7639ec9b83723510acb4a5c4e23d5d6167c3af6))
|
|
57
|
+
* **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))
|
|
58
|
+
* **release:** a manual release keeps the version of its manifest ([122e661](https://github.com/ManotLuijiu/pi-harness-runtime/commit/122e661ea48cb8aff14c71dbd0221a4cf0ba4c8e))
|
|
59
|
+
* **release:** the alignment names the reason of a failure ([5814dd0](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5814dd0b780f5839cf8011ed0cbbbb31e7eb0418))
|
|
60
|
+
* **release:** the channels of the package name the release ([a7a273f](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a7a273ffe8398e2b5b07ef1c321777de41792c6e))
|
|
61
|
+
* **release:** the release publishes and leaves the channels alone ([5871746](https://github.com/ManotLuijiu/pi-harness-runtime/commit/5871746589c1281d855dbeb591b02419fa43f22e))
|
|
62
|
+
* **release:** the release pull request names the version of its branch ([a846e83](https://github.com/ManotLuijiu/pi-harness-runtime/commit/a846e83e6de6e529f4bca05e869db0b5eebb37c5))
|
|
63
|
+
* **release:** the release scans the tree of the commit it publishes ([7d47a0b](https://github.com/ManotLuijiu/pi-harness-runtime/commit/7d47a0b5406480a1e1d3a52623a76aabd1e2d0ba))
|
|
64
|
+
* **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))
|
|
65
|
+
* **workspace:** keep a client name out of the comment about the search depth ([b572e0a](https://github.com/ManotLuijiu/pi-harness-runtime/commit/b572e0af16ff562203bda2b5c47b41807cff5a95))
|
|
66
|
+
* **workspace:** the repository names the mode before the mode stays unknown ([188a32c](https://github.com/ManotLuijiu/pi-harness-runtime/commit/188a32c7a6af1465dd24714278fccc73d06f6477))
|
|
67
|
+
|
|
5
68
|
### [1.1.485](https://github.com/ManotLuijiu/pi-harness-runtime/compare/v1.1.448...v1.1.485) (2026-11-09)
|
|
6
69
|
|
|
7
70
|
|
|
@@ -268,6 +268,9 @@ export function classifyProviderFailure(text: string | undefined): ProviderFailu
|
|
|
268
268
|
retryable: true,
|
|
269
269
|
recovery: "bounded-recovery",
|
|
270
270
|
...(window ? { window } : {}),
|
|
271
|
+
// A stated deadline is a lower bound of the next attempt. It is
|
|
272
|
+
// not the deadline of the attempt: the cycle may wait longer.
|
|
273
|
+
...(retryAfterMs !== undefined ? { retryAfterMs } : {}),
|
|
271
274
|
...(code ? { code } : {}),
|
|
272
275
|
...(provider ? { provider } : {}),
|
|
273
276
|
reason: provider
|
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The runtime of the automatic resumes after a provider failure.
|
|
3
|
+
*
|
|
4
|
+
* The runtime owns the sequence of a failure:
|
|
5
|
+
*
|
|
6
|
+
* ```text
|
|
7
|
+
* failure -> candidate -> (the run settles) -> attempt -> dispatch
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* Two boundaries matter, and both were missing before.
|
|
11
|
+
*
|
|
12
|
+
* **The settled boundary.** Pi retries a failed turn internally, and it
|
|
13
|
+
* emits the events of the failed attempt before it decides to retry. A
|
|
14
|
+
* recovery that starts at the message of the failed attempt is therefore a
|
|
15
|
+
* competing continuation: while the internal retries run, the harness
|
|
16
|
+
* injects its own resume. The runtime keeps the failure as a candidate and
|
|
17
|
+
* starts the countdown when the run settles, which is the moment the
|
|
18
|
+
* internal retries are over.
|
|
19
|
+
*
|
|
20
|
+
* **The ownership of the dispatch.** The injected message is the literal
|
|
21
|
+
* `resume`, and that message clears an explicit pause of the user. A timer
|
|
22
|
+
* that does not look at the state of the session therefore overrides the
|
|
23
|
+
* decision of the user. Before every dispatch the runtime checks that the
|
|
24
|
+
* session is the same, that the user did not pause, and that no question
|
|
25
|
+
* waits for an answer.
|
|
26
|
+
*
|
|
27
|
+
* The runtime never owns the execution: it hands the resume to the
|
|
28
|
+
* dispatcher, which is the existing executor of the harness.
|
|
29
|
+
*/
|
|
30
|
+
import type { ProviderFailure } from "./provider-failure.ts";
|
|
31
|
+
import type { PendingAction, PendingActionRegistry } from "./pending-actions.ts";
|
|
32
|
+
import type {
|
|
33
|
+
ProviderRecoveryController,
|
|
34
|
+
RecoveryResetEvidence,
|
|
35
|
+
} from "./provider-recovery.ts";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The state of the session, read when a resume would run.
|
|
39
|
+
*/
|
|
40
|
+
export interface ProviderRecoverySessionState {
|
|
41
|
+
/** The session that is current now. */
|
|
42
|
+
sessionId: string;
|
|
43
|
+
/** The user paused the work explicitly. */
|
|
44
|
+
explicitlyPaused: boolean;
|
|
45
|
+
/** A question waits for the answer of the user. */
|
|
46
|
+
questionPending: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The decision about a dispatch.
|
|
51
|
+
*/
|
|
52
|
+
export type ProviderRecoveryDecision =
|
|
53
|
+
| { allowed: true }
|
|
54
|
+
| { allowed: false; reason: string };
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Whether a scheduled resume may still be dispatched.
|
|
58
|
+
*
|
|
59
|
+
* The resume is the literal `resume` message, and that message clears an
|
|
60
|
+
* explicit pause of the user. A resume that ignores a pause overrides the
|
|
61
|
+
* decision of the user, so the pause stops it. A changed session stops it
|
|
62
|
+
* too: a resume of the previous session belongs to nobody.
|
|
63
|
+
*/
|
|
64
|
+
export function mayDispatchResume(state: {
|
|
65
|
+
/** The session the resume was scheduled in. */
|
|
66
|
+
scheduledSessionId: string;
|
|
67
|
+
/** The state of the session now. */
|
|
68
|
+
current: ProviderRecoverySessionState;
|
|
69
|
+
}): ProviderRecoveryDecision {
|
|
70
|
+
if (state.scheduledSessionId !== state.current.sessionId) {
|
|
71
|
+
return { allowed: false, reason: "the session changed" };
|
|
72
|
+
}
|
|
73
|
+
if (state.current.explicitlyPaused) {
|
|
74
|
+
return { allowed: false, reason: "the user paused the work" };
|
|
75
|
+
}
|
|
76
|
+
if (state.current.questionPending) {
|
|
77
|
+
return { allowed: false, reason: "a question waits for the user" };
|
|
78
|
+
}
|
|
79
|
+
return { allowed: true };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A failure that waits for the settled boundary.
|
|
84
|
+
*/
|
|
85
|
+
export interface ProviderFailureCandidate {
|
|
86
|
+
/** The classified failure. */
|
|
87
|
+
failure: ProviderFailure;
|
|
88
|
+
/** The identity of the failed turn. */
|
|
89
|
+
failureKey: string;
|
|
90
|
+
/** The provider that rejected the request. */
|
|
91
|
+
provider: string;
|
|
92
|
+
/**
|
|
93
|
+
* Whether the overload executor may resume this failure.
|
|
94
|
+
*
|
|
95
|
+
* The overload has its own executor, and it never joins the cycle: one
|
|
96
|
+
* failure has one executor.
|
|
97
|
+
*/
|
|
98
|
+
overloadEligible: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Whether the bounded recovery cycle may resume this failure.
|
|
101
|
+
*
|
|
102
|
+
* The classification decides: a rate limit and an exhausted plan join
|
|
103
|
+
* the cycle, an overload keeps its own executor, and a credential
|
|
104
|
+
* failure joins neither.
|
|
105
|
+
*/
|
|
106
|
+
boundedEligible: boolean;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The options of the runtime.
|
|
111
|
+
*/
|
|
112
|
+
export interface ProviderRecoveryRuntimeOptions {
|
|
113
|
+
/** The controller of the cycle. It decides the deadlines. */
|
|
114
|
+
controller: ProviderRecoveryController;
|
|
115
|
+
/** The registry of the pending actions. */
|
|
116
|
+
actions: PendingActionRegistry;
|
|
117
|
+
/** The identity of the pending action of an attempt of the cycle. */
|
|
118
|
+
recoveryActionId: string;
|
|
119
|
+
/** The identity of the pending action of a blocked provider. */
|
|
120
|
+
blockedActionId: string;
|
|
121
|
+
/** The identity of the pending action of the overload resume. */
|
|
122
|
+
overloadActionId: string;
|
|
123
|
+
/** The state of the session, read when a resume would run. */
|
|
124
|
+
context: () => ProviderRecoverySessionState;
|
|
125
|
+
/** The dispatcher. It sends the resume through the existing executor. */
|
|
126
|
+
dispatch: (reason: string) => void | Promise<void>;
|
|
127
|
+
/** The clock. It is injected, so the tests are exact. */
|
|
128
|
+
now?: () => number;
|
|
129
|
+
/** The timer that starts a resume. */
|
|
130
|
+
setTimeout?: (callback: () => void, delayMs: number) => unknown;
|
|
131
|
+
/** The timer that stops a resume. */
|
|
132
|
+
clearTimeout?: (handle: unknown) => void;
|
|
133
|
+
/** The reset evidence of the provider, when the harness observed one. */
|
|
134
|
+
resetEvidence?: (failure: ProviderFailure) => RecoveryResetEvidence | undefined;
|
|
135
|
+
/** The number of attempts the cycle allows. */
|
|
136
|
+
maxAttempts: number;
|
|
137
|
+
/** The wait of the overload executor, in milliseconds. */
|
|
138
|
+
overloadDelayMs?: () => number;
|
|
139
|
+
/** The number of attempts the overload executor allows. */
|
|
140
|
+
overloadMaxAttempts?: number;
|
|
141
|
+
/** The log of the runtime. */
|
|
142
|
+
log?: (message: string) => void;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The runtime of the automatic resumes after a provider failure.
|
|
147
|
+
*/
|
|
148
|
+
export class ProviderRecoveryRuntime {
|
|
149
|
+
private readonly options: ProviderRecoveryRuntimeOptions;
|
|
150
|
+
private readonly now: () => number;
|
|
151
|
+
private readonly setTimer: (callback: () => void, delayMs: number) => unknown;
|
|
152
|
+
private readonly clearTimer: (handle: unknown) => void;
|
|
153
|
+
/** The failure that waits for the settled boundary. */
|
|
154
|
+
private candidate: ProviderFailureCandidate | null = null;
|
|
155
|
+
/** The timer of the pending attempt of the cycle. */
|
|
156
|
+
private recoveryTimer: unknown = null;
|
|
157
|
+
/** The timer of the pending overload resume. */
|
|
158
|
+
private overloadTimer: unknown = null;
|
|
159
|
+
/** The number of the overload resumes that ran. */
|
|
160
|
+
private overloadAttempts = 0;
|
|
161
|
+
|
|
162
|
+
constructor(options: ProviderRecoveryRuntimeOptions) {
|
|
163
|
+
this.options = options;
|
|
164
|
+
this.now = options.now ?? (() => Date.now());
|
|
165
|
+
this.setTimer =
|
|
166
|
+
options.setTimeout ??
|
|
167
|
+
((callback, delayMs) => setTimeout(callback, delayMs) as unknown);
|
|
168
|
+
this.clearTimer =
|
|
169
|
+
options.clearTimeout ??
|
|
170
|
+
((handle) => clearTimeout(handle as ReturnType<typeof setTimeout>));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Note a failure of a turn.
|
|
175
|
+
*
|
|
176
|
+
* The failure is a candidate: the countdown starts when the run
|
|
177
|
+
* settles, because Pi retries a failed turn internally first.
|
|
178
|
+
*/
|
|
179
|
+
noteFailure(
|
|
180
|
+
failure: ProviderFailure,
|
|
181
|
+
failureKey: string,
|
|
182
|
+
options: {
|
|
183
|
+
provider?: string;
|
|
184
|
+
overloadEligible?: boolean;
|
|
185
|
+
boundedEligible?: boolean;
|
|
186
|
+
} = {},
|
|
187
|
+
): void {
|
|
188
|
+
this.candidate = {
|
|
189
|
+
failure,
|
|
190
|
+
failureKey,
|
|
191
|
+
provider: (options.provider ?? failure.provider ?? "provider").toLowerCase(),
|
|
192
|
+
overloadEligible: options.overloadEligible === true,
|
|
193
|
+
boundedEligible: options.boundedEligible === true,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The run settled.
|
|
199
|
+
*
|
|
200
|
+
* The internal retries of Pi are over, so the failure is final. The
|
|
201
|
+
* recovery starts now, and its deadline is measured from this moment.
|
|
202
|
+
*/
|
|
203
|
+
noteSettled(): void {
|
|
204
|
+
const candidate = this.candidate;
|
|
205
|
+
this.candidate = null;
|
|
206
|
+
if (!candidate) return;
|
|
207
|
+
|
|
208
|
+
if (candidate.overloadEligible) {
|
|
209
|
+
this.startOverloadResume();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (!candidate.boundedEligible) {
|
|
213
|
+
// The failure has no executor: the classification says that no
|
|
214
|
+
// automatic retry can fix it. Nothing is scheduled.
|
|
215
|
+
this.log(
|
|
216
|
+
`Provider failure has no automatic executor: ${candidate.failure.kind}`,
|
|
217
|
+
);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
this.startRecovery(candidate);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* A successful turn ends the cycle and clears the blocked row.
|
|
225
|
+
*/
|
|
226
|
+
noteSuccess(provider: string | undefined): void {
|
|
227
|
+
this.cancelPending();
|
|
228
|
+
// The provider works again: the counter of the overload resumes
|
|
229
|
+
// starts at zero, so the next overload gets the full budget.
|
|
230
|
+
this.overloadAttempts = 0;
|
|
231
|
+
// The cycle is keyed by the provider, so a turn without a known
|
|
232
|
+
// provider ends no cycle. It still ends the pending resumes above.
|
|
233
|
+
const known =
|
|
234
|
+
typeof provider === "string" && provider.trim().length > 0;
|
|
235
|
+
if (known) {
|
|
236
|
+
this.options.controller.noteSuccess({
|
|
237
|
+
sessionId: this.options.context().sessionId,
|
|
238
|
+
provider: provider.toLowerCase(),
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
this.options.actions.removeWhere(
|
|
242
|
+
(action) => action.owner === "provider" && action.state === "blocked",
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* A user intervention cancels the pending resumes.
|
|
248
|
+
*
|
|
249
|
+
* A resume that runs after the user took over is a resume that
|
|
250
|
+
* overrides the decision of the user.
|
|
251
|
+
*/
|
|
252
|
+
cancel(): void {
|
|
253
|
+
this.cancelPending();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Cancel the pending resumes without touching the cycle.
|
|
258
|
+
*
|
|
259
|
+
* The cycle keeps its budget: a cancelled attempt is not an attempt of
|
|
260
|
+
* the budget, but the failure that caused it is still the failure of
|
|
261
|
+
* the cycle.
|
|
262
|
+
*/
|
|
263
|
+
cancelPending(): void {
|
|
264
|
+
this.candidate = null;
|
|
265
|
+
this.cancelRecoveryAttempt();
|
|
266
|
+
this.cancelOverloadResume();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The session ended: every pending resume dies with it.
|
|
271
|
+
*/
|
|
272
|
+
reset(): void {
|
|
273
|
+
this.cancel();
|
|
274
|
+
this.overloadAttempts = 0;
|
|
275
|
+
this.options.controller.clear();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Whether a resume of the cycle is pending.
|
|
280
|
+
*/
|
|
281
|
+
get hasPendingRecovery(): boolean {
|
|
282
|
+
return this.recoveryTimer !== null;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Whether an overload resume is pending.
|
|
287
|
+
*/
|
|
288
|
+
get hasPendingOverload(): boolean {
|
|
289
|
+
return this.overloadTimer !== null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The number of the overload resumes that ran.
|
|
294
|
+
*
|
|
295
|
+
* The executor of the overload asks the runtime before it schedules
|
|
296
|
+
* another one: the limit of the executor and the count of the runtime
|
|
297
|
+
* are the same number.
|
|
298
|
+
*/
|
|
299
|
+
get overloadAttemptCount(): number {
|
|
300
|
+
return this.overloadAttempts;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Start the cycle of a failure.
|
|
305
|
+
*/
|
|
306
|
+
private startRecovery(candidate: ProviderFailureCandidate): void {
|
|
307
|
+
const { failure, failureKey, provider } = candidate;
|
|
308
|
+
const sessionId = this.options.context().sessionId;
|
|
309
|
+
const outcome = this.options.controller.noteFailure(
|
|
310
|
+
{
|
|
311
|
+
failureKey,
|
|
312
|
+
sessionId,
|
|
313
|
+
provider,
|
|
314
|
+
kind: failure.kind,
|
|
315
|
+
...(failure.window ? { window: failure.window } : {}),
|
|
316
|
+
...(failure.retryAfterMs !== undefined
|
|
317
|
+
? { retryAfterAt: this.now() + failure.retryAfterMs }
|
|
318
|
+
: {}),
|
|
319
|
+
},
|
|
320
|
+
this.options.resetEvidence?.(failure),
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
if (outcome.outcome === "scheduled") {
|
|
324
|
+
this.log(
|
|
325
|
+
`Provider recovery attempt ${outcome.attempt}/${this.maxAttempts} at ${new Date(outcome.dueAt).toISOString()} (bound=${outcome.boundBy})`,
|
|
326
|
+
);
|
|
327
|
+
this.scheduleAttempt(outcome.dueAt, outcome.attempt, failure, sessionId);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (outcome.outcome === "blocked") {
|
|
332
|
+
this.log(
|
|
333
|
+
`Provider recovery exhausted after ${outcome.attempt} attempts`,
|
|
334
|
+
);
|
|
335
|
+
this.registerBlocked(failure, outcome.attempt);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
this.log(
|
|
340
|
+
`Provider recovery unchanged (${outcome.outcome}), attempt ${outcome.attempt}`,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* The number of attempts the cycle allows.
|
|
346
|
+
*/
|
|
347
|
+
private get maxAttempts(): number {
|
|
348
|
+
return this.options.maxAttempts;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Register the row of a blocked provider.
|
|
353
|
+
*/
|
|
354
|
+
private registerBlocked(failure: ProviderFailure, attempt: number): void {
|
|
355
|
+
const action: PendingAction = {
|
|
356
|
+
id: this.options.blockedActionId,
|
|
357
|
+
sessionId: this.options.context().sessionId,
|
|
358
|
+
owner: "provider",
|
|
359
|
+
kind: "retry",
|
|
360
|
+
state: "blocked",
|
|
361
|
+
...(failure.provider ? { provider: failure.provider } : {}),
|
|
362
|
+
...(failure.code ? { code: failure.code } : {}),
|
|
363
|
+
reason: `recovery attempts exhausted (${attempt}/${this.maxAttempts})`,
|
|
364
|
+
};
|
|
365
|
+
this.options.actions.register(action);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Start the timer of an attempt of the cycle.
|
|
370
|
+
*/
|
|
371
|
+
private scheduleAttempt(
|
|
372
|
+
dueAt: number,
|
|
373
|
+
attempt: number,
|
|
374
|
+
failure: ProviderFailure,
|
|
375
|
+
sessionId: string,
|
|
376
|
+
): void {
|
|
377
|
+
this.cancelRecoveryAttempt();
|
|
378
|
+
this.options.actions.register({
|
|
379
|
+
id: this.options.recoveryActionId,
|
|
380
|
+
sessionId,
|
|
381
|
+
owner: "provider-recovery",
|
|
382
|
+
kind: "retry",
|
|
383
|
+
state: "scheduled",
|
|
384
|
+
dueAt,
|
|
385
|
+
attempt,
|
|
386
|
+
maxAttempts: this.maxAttempts,
|
|
387
|
+
...(failure.provider ? { provider: failure.provider } : {}),
|
|
388
|
+
...(failure.code ? { code: failure.code } : {}),
|
|
389
|
+
reason: failure.reason,
|
|
390
|
+
});
|
|
391
|
+
const delayMs = Math.max(0, dueAt - this.now());
|
|
392
|
+
this.recoveryTimer = this.setTimer(() => {
|
|
393
|
+
this.recoveryTimer = null;
|
|
394
|
+
// The row leaves when the attempt runs: a countdown must not stay
|
|
395
|
+
// at 00:00.
|
|
396
|
+
this.options.actions.remove(this.options.recoveryActionId);
|
|
397
|
+
this.dispatchIfOwned(sessionId, "provider-recovery");
|
|
398
|
+
}, delayMs);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Start the timer of the overload resume.
|
|
403
|
+
*/
|
|
404
|
+
private startOverloadResume(): void {
|
|
405
|
+
const limit = this.options.overloadMaxAttempts ?? 3;
|
|
406
|
+
if (this.overloadAttempts >= limit) {
|
|
407
|
+
this.log(
|
|
408
|
+
`Provider overload resume not scheduled: ${this.overloadAttempts}/${limit} attempts used`,
|
|
409
|
+
);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
this.cancelOverloadResume();
|
|
413
|
+
this.overloadAttempts += 1;
|
|
414
|
+
const delayMs = this.options.overloadDelayMs?.() ?? 60_000;
|
|
415
|
+
const sessionId = this.options.context().sessionId;
|
|
416
|
+
this.options.actions.register({
|
|
417
|
+
id: this.options.overloadActionId,
|
|
418
|
+
sessionId,
|
|
419
|
+
owner: "provider-overload",
|
|
420
|
+
kind: "retry",
|
|
421
|
+
state: "scheduled",
|
|
422
|
+
dueAt: this.now() + delayMs,
|
|
423
|
+
attempt: this.overloadAttempts,
|
|
424
|
+
reason: "the provider is overloaded",
|
|
425
|
+
});
|
|
426
|
+
this.overloadTimer = this.setTimer(() => {
|
|
427
|
+
this.overloadTimer = null;
|
|
428
|
+
this.options.actions.remove(this.options.overloadActionId);
|
|
429
|
+
this.dispatchIfOwned(sessionId, "provider-overload");
|
|
430
|
+
}, delayMs);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Cancel the pending attempt of the cycle.
|
|
435
|
+
*/
|
|
436
|
+
private cancelRecoveryAttempt(): void {
|
|
437
|
+
if (this.recoveryTimer !== null) {
|
|
438
|
+
this.clearTimer(this.recoveryTimer);
|
|
439
|
+
this.recoveryTimer = null;
|
|
440
|
+
}
|
|
441
|
+
this.options.actions.remove(this.options.recoveryActionId);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Cancel the pending overload resume.
|
|
446
|
+
*/
|
|
447
|
+
private cancelOverloadResume(): void {
|
|
448
|
+
if (this.overloadTimer !== null) {
|
|
449
|
+
this.clearTimer(this.overloadTimer);
|
|
450
|
+
this.overloadTimer = null;
|
|
451
|
+
}
|
|
452
|
+
this.options.actions.remove(this.options.overloadActionId);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Dispatch a resume, but only while it still owns the session.
|
|
457
|
+
*
|
|
458
|
+
* The ownership is checked at the moment of the dispatch, not at the
|
|
459
|
+
* moment of the scheduling: the user may pause the work while the
|
|
460
|
+
* countdown runs.
|
|
461
|
+
*/
|
|
462
|
+
private dispatchIfOwned(sessionId: string, reason: string): void {
|
|
463
|
+
const decision = mayDispatchResume({
|
|
464
|
+
scheduledSessionId: sessionId,
|
|
465
|
+
current: this.options.context(),
|
|
466
|
+
});
|
|
467
|
+
if (decision.allowed === false) {
|
|
468
|
+
this.log(`Resume skipped (${reason}): ${decision.reason}`);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
void this.options.dispatch(reason);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Write to the log of the runtime.
|
|
476
|
+
*/
|
|
477
|
+
private log(message: string): void {
|
|
478
|
+
this.options.log?.(message);
|
|
479
|
+
}
|
|
480
|
+
}
|