pi-harness-runtime 1.1.516 → 1.1.518
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 +194 -0
- package/harness/delegated-approval-runtime.ts +142 -19
- package/harness/delegated-approval-types.ts +194 -13
- package/harness/exploration-guard-runtime.ts +41 -30
- package/harness/jev-rejection-escalation.ts +146 -8
- package/harness/pending-countdown.ts +59 -6
- package/harness/plan-handoff-receiver.ts +162 -13
- package/index.ts +323 -24
- 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/exploration-guard/dist/budget.d.ts +8 -0
- package/packages/exploration-guard/dist/budget.d.ts.map +1 -1
- package/packages/exploration-guard/dist/budget.js +25 -0
- package/packages/exploration-guard/dist/budget.js.map +1 -1
- package/packages/exploration-guard/dist/index.d.ts +1 -1
- package/packages/exploration-guard/dist/index.d.ts.map +1 -1
- package/packages/exploration-guard/dist/index.js +1 -1
- package/packages/exploration-guard/dist/index.js.map +1 -1
- package/packages/exploration-guard/dist/state.d.ts.map +1 -1
- package/packages/exploration-guard/dist/state.js +2 -0
- package/packages/exploration-guard/dist/state.js.map +1 -1
- package/packages/exploration-guard/dist/tracker.d.ts +12 -7
- package/packages/exploration-guard/dist/tracker.d.ts.map +1 -1
- package/packages/exploration-guard/dist/tracker.js +47 -10
- package/packages/exploration-guard/dist/tracker.js.map +1 -1
- package/packages/exploration-guard/dist/types.d.ts +12 -0
- package/packages/exploration-guard/dist/types.d.ts.map +1 -1
- package/packages/exploration-guard/package.json +1 -1
- package/packages/exploration-guard/src/budget.ts +27 -0
- package/packages/exploration-guard/src/index.ts +1 -0
- package/packages/exploration-guard/src/state.ts +4 -0
- package/packages/exploration-guard/src/tracker.ts +51 -9
- package/packages/exploration-guard/src/types.ts +12 -0
- package/packages/file-copy-helper/package.json +1 -1
- package/packages/intent-analyzer/package.json +1 -1
- package/packages/jev-judge/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/plan-handoff/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
|
@@ -131,30 +131,211 @@ export interface ApprovalDecision {
|
|
|
131
131
|
at: number;
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
+
/**
|
|
135
|
+
* The words that ask for permission to do something.
|
|
136
|
+
*
|
|
137
|
+
* The classification runs on free prose, so the words of the text are not
|
|
138
|
+
* enough: a text that only mentions an operation does not propose it. The
|
|
139
|
+
* question has to ask for something.
|
|
140
|
+
*/
|
|
141
|
+
const INTENT_STEMS = [
|
|
142
|
+
"may i",
|
|
143
|
+
"may we",
|
|
144
|
+
"can i",
|
|
145
|
+
"can we",
|
|
146
|
+
"should i",
|
|
147
|
+
"should we",
|
|
148
|
+
"shall i",
|
|
149
|
+
"would i",
|
|
150
|
+
"could i",
|
|
151
|
+
"am i allowed to",
|
|
152
|
+
"are i allowed to",
|
|
153
|
+
"is it ok to",
|
|
154
|
+
"is it okay to",
|
|
155
|
+
"is it fine to",
|
|
156
|
+
"is it allowed to",
|
|
157
|
+
"do you want me to",
|
|
158
|
+
"do you want us to",
|
|
159
|
+
"would you like me to",
|
|
160
|
+
"would you like us to",
|
|
161
|
+
"please",
|
|
162
|
+
"go ahead and",
|
|
163
|
+
"go ahead with",
|
|
164
|
+
"go ahead",
|
|
165
|
+
"ok to",
|
|
166
|
+
"okay to",
|
|
167
|
+
"i want to",
|
|
168
|
+
"i need to",
|
|
169
|
+
"i would like to",
|
|
170
|
+
"we want to",
|
|
171
|
+
"we need to",
|
|
172
|
+
"authorize me to",
|
|
173
|
+
"authorise me to",
|
|
174
|
+
"allow me to",
|
|
175
|
+
"let me",
|
|
176
|
+
"let us",
|
|
177
|
+
];
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The pattern of an ask.
|
|
181
|
+
*
|
|
182
|
+
* The word boundary at the end matters: "may issue a ticket" is not an ask
|
|
183
|
+
* of the shape "may i".
|
|
184
|
+
*/
|
|
185
|
+
const INTENT_PATTERN = new RegExp(
|
|
186
|
+
`\\b(?:${INTENT_STEMS.map((stem) => stem.replace(/ /g, "\\s+")).join("|")})\\b`,
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The words that deny an operation.
|
|
191
|
+
*
|
|
192
|
+
* A denial speaks for the rest of the ask: "do not commit and push" denies
|
|
193
|
+
* both.
|
|
194
|
+
*/
|
|
195
|
+
const DENYING_WORDS = new Set([
|
|
196
|
+
"not",
|
|
197
|
+
"never",
|
|
198
|
+
"without",
|
|
199
|
+
"avoid",
|
|
200
|
+
"skip",
|
|
201
|
+
"refuse",
|
|
202
|
+
"deny",
|
|
203
|
+
"denied",
|
|
204
|
+
"neither",
|
|
205
|
+
"nor",
|
|
206
|
+
]);
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The personal subjects.
|
|
210
|
+
*
|
|
211
|
+
* An operation behind one of them is a fact and not a proposal: "the
|
|
212
|
+
* service I deployed yesterday" talks about the past.
|
|
213
|
+
*/
|
|
214
|
+
const PERSONAL_SUBJECTS = new Set([
|
|
215
|
+
"i",
|
|
216
|
+
"we",
|
|
217
|
+
"he",
|
|
218
|
+
"she",
|
|
219
|
+
"they",
|
|
220
|
+
"you",
|
|
221
|
+
"it",
|
|
222
|
+
"my",
|
|
223
|
+
"our",
|
|
224
|
+
"me",
|
|
225
|
+
"us",
|
|
226
|
+
"them",
|
|
227
|
+
]);
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The operations and the words that name them.
|
|
231
|
+
*
|
|
232
|
+
* Only the active forms are named. The past tense describes what happened
|
|
233
|
+
* and never proposes anything, so a delegation must not cover it.
|
|
234
|
+
*/
|
|
235
|
+
const OPERATION_WORDS: { kind: DelegatedOperationKind; words: string[] }[] = [
|
|
236
|
+
{ kind: "commit", words: ["commit", "commits", "committing"] },
|
|
237
|
+
{ kind: "push", words: ["push", "pushes", "pushing"] },
|
|
238
|
+
{
|
|
239
|
+
kind: "deploy",
|
|
240
|
+
words: [
|
|
241
|
+
"deploy",
|
|
242
|
+
"deploys",
|
|
243
|
+
"deploying",
|
|
244
|
+
"deployment",
|
|
245
|
+
"deployments",
|
|
246
|
+
"ship",
|
|
247
|
+
"ships",
|
|
248
|
+
"shipping",
|
|
249
|
+
],
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
kind: "external-write",
|
|
253
|
+
words: [
|
|
254
|
+
"publish",
|
|
255
|
+
"publishes",
|
|
256
|
+
"publishing",
|
|
257
|
+
"publication",
|
|
258
|
+
"upload",
|
|
259
|
+
"uploads",
|
|
260
|
+
"uploading",
|
|
261
|
+
],
|
|
262
|
+
},
|
|
263
|
+
];
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* The kind of operation a word names.
|
|
267
|
+
*/
|
|
268
|
+
function operationKindOfWord(word: string): DelegatedOperationKind | undefined {
|
|
269
|
+
for (const entry of OPERATION_WORDS) {
|
|
270
|
+
if (entry.words.includes(word)) return entry.kind;
|
|
271
|
+
}
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The operations the ask names, in the order they appear.
|
|
277
|
+
*/
|
|
278
|
+
function operationKindsAfter(value: string, from: number): DelegatedOperationKind[] {
|
|
279
|
+
const sentence = value.slice(from).split(/[.!?;\n]/)[0] ?? "";
|
|
280
|
+
const words = sentence
|
|
281
|
+
.split(/[^a-z0-9-]+/)
|
|
282
|
+
.filter((word) => word.length > 0);
|
|
283
|
+
const kinds: DelegatedOperationKind[] = [];
|
|
284
|
+
let personalWindow = 0;
|
|
285
|
+
let denied = false;
|
|
286
|
+
for (const word of words) {
|
|
287
|
+
if (DENYING_WORDS.has(word)) {
|
|
288
|
+
// The denial speaks for the rest of the ask.
|
|
289
|
+
denied = true;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (denied) continue;
|
|
293
|
+
if (PERSONAL_SUBJECTS.has(word)) {
|
|
294
|
+
// The next words belong to the past of the speaker.
|
|
295
|
+
personalWindow = 3;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (personalWindow > 0) {
|
|
299
|
+
personalWindow -= 1;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const kind = operationKindOfWord(word);
|
|
303
|
+
if (kind && !kinds.includes(kind)) kinds.push(kind);
|
|
304
|
+
}
|
|
305
|
+
// A post to somebody else is an external write.
|
|
306
|
+
if (!denied && /\bpost(?:s|ing)?\s+to\b/.test(sentence)) {
|
|
307
|
+
if (!kinds.includes("external-write")) kinds.push("external-write");
|
|
308
|
+
}
|
|
309
|
+
return kinds;
|
|
310
|
+
}
|
|
311
|
+
|
|
134
312
|
/**
|
|
135
313
|
* The kind of operation of a proposal.
|
|
136
314
|
*
|
|
137
315
|
* The classification is narrow on purpose. A proposal whose kind is not
|
|
138
316
|
* known is not covered by a delegation, so an unknown operation never
|
|
139
317
|
* continues by silence.
|
|
318
|
+
*
|
|
319
|
+
* The text is free prose, so a word is not enough: the text has to ask for
|
|
320
|
+
* something, and the operation has to be the active form of the ask. A
|
|
321
|
+
* mention of the past ("the service I deployed yesterday") and a denial
|
|
322
|
+
* ("do not commit") name no operation at all.
|
|
140
323
|
*/
|
|
141
324
|
export function operationKindFor(
|
|
142
325
|
text: string | undefined,
|
|
143
326
|
): DelegatedOperationKind | undefined {
|
|
144
327
|
const value = (text ?? "").toLowerCase();
|
|
145
|
-
if (value.length === 0) return undefined;
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
);
|
|
152
|
-
if (
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (push) return "push";
|
|
157
|
-
return undefined;
|
|
328
|
+
if (value.trim().length === 0) return undefined;
|
|
329
|
+
const ask = INTENT_PATTERN.exec(value);
|
|
330
|
+
if (!ask) return undefined;
|
|
331
|
+
const kinds = operationKindsAfter(value, ask.index + ask[0].length);
|
|
332
|
+
if (kinds.length === 0) return undefined;
|
|
333
|
+
if (kinds.includes("deploy")) return "deploy";
|
|
334
|
+
if (kinds.includes("external-write")) return "external-write";
|
|
335
|
+
if (kinds.includes("commit") && kinds.includes("push")) {
|
|
336
|
+
return "commit-and-push";
|
|
337
|
+
}
|
|
338
|
+
return kinds[0];
|
|
158
339
|
}
|
|
159
340
|
|
|
160
341
|
/**
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
buildContinuationText,
|
|
25
25
|
describeUnreadableCheckpoint,
|
|
26
26
|
ExplorationTracker,
|
|
27
|
+
interventionSeverity,
|
|
27
28
|
toCompactCheckpoint,
|
|
28
29
|
} from "../packages/exploration-guard/dist/index.js";
|
|
29
30
|
|
|
@@ -73,27 +74,18 @@ export interface PendingIntervention {
|
|
|
73
74
|
requiresCheckpoint: boolean;
|
|
74
75
|
/** The kind of the intervention. */
|
|
75
76
|
kind: Intervention["kind"];
|
|
77
|
+
/**
|
|
78
|
+
* How serious the intervention is.
|
|
79
|
+
*
|
|
80
|
+
* The seriousness is part of the identity of the intervention: a
|
|
81
|
+
* warning that was delivered does not silence a budget that is reached
|
|
82
|
+
* later in the same checkpoint.
|
|
83
|
+
*/
|
|
84
|
+
severity: number;
|
|
76
85
|
/** The state of the request when the intervention was raised. */
|
|
77
86
|
state: ExplorationState;
|
|
78
87
|
}
|
|
79
88
|
|
|
80
|
-
/**
|
|
81
|
-
* How serious an intervention is.
|
|
82
|
-
*
|
|
83
|
-
* A soft warning is the least serious, a budget that is reached is more
|
|
84
|
-
* serious, and a blocker is the most serious. The guard speaks again when
|
|
85
|
-
* the seriousness grows, even when the checkpoint did not change.
|
|
86
|
-
*/
|
|
87
|
-
const SEVERITY: Record<Intervention["kind"], number> = {
|
|
88
|
-
none: 0,
|
|
89
|
-
"repeat-read": 1,
|
|
90
|
-
stall: 1,
|
|
91
|
-
"read-budget": 2,
|
|
92
|
-
"token-budget": 2,
|
|
93
|
-
"time-budget": 2,
|
|
94
|
-
blocked: 3,
|
|
95
|
-
};
|
|
96
|
-
|
|
97
89
|
/**
|
|
98
90
|
* The number of times the guard speaks in one request.
|
|
99
91
|
*
|
|
@@ -120,10 +112,6 @@ export class ExplorationGuardRuntime {
|
|
|
120
112
|
private inactive = false;
|
|
121
113
|
/** How often the guard spoke in this request. */
|
|
122
114
|
private speechCount = 0;
|
|
123
|
-
/** How serious the last intervention was. */
|
|
124
|
-
private lastSeverity = -1;
|
|
125
|
-
/** The checkpoint revision the guard last spoke at. */
|
|
126
|
-
private spokenRevision = -1;
|
|
127
115
|
|
|
128
116
|
constructor(options: ExplorationRuntimeOptions) {
|
|
129
117
|
this.identity = options.identity;
|
|
@@ -177,36 +165,61 @@ export class ExplorationGuardRuntime {
|
|
|
177
165
|
}
|
|
178
166
|
if (!result.shouldSpeak) return null;
|
|
179
167
|
|
|
180
|
-
const severity =
|
|
168
|
+
const severity = interventionSeverity(result.intervention.kind);
|
|
181
169
|
if (!this.maySpeak(severity, result.state)) return null;
|
|
182
170
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
171
|
+
// The intervention is queued, not delivered. The budget of the
|
|
172
|
+
// speech and the deduplication of the revision are only spent when
|
|
173
|
+
// the delivery worked: a message that never reached the agent has
|
|
174
|
+
// not been spoken yet, and it may be spoken again.
|
|
187
175
|
const key = this.key();
|
|
188
176
|
this.pending = {
|
|
189
177
|
key,
|
|
190
178
|
message: result.intervention.message,
|
|
191
179
|
requiresCheckpoint: result.intervention.requiresCheckpoint,
|
|
192
180
|
kind: result.intervention.kind,
|
|
181
|
+
severity,
|
|
193
182
|
state: result.state,
|
|
194
183
|
};
|
|
195
184
|
this.pendingGeneration = key.generation;
|
|
196
185
|
return this.pending;
|
|
197
186
|
}
|
|
198
187
|
|
|
188
|
+
/**
|
|
189
|
+
* The intervention reached the agent.
|
|
190
|
+
*
|
|
191
|
+
* The caller reports a delivery that worked. Only then does the guard
|
|
192
|
+
* count the speech, remember the revision and remember the
|
|
193
|
+
* seriousness: a delivery that the guarded path refused has not spoken,
|
|
194
|
+
* and the next observation may speak again.
|
|
195
|
+
*/
|
|
196
|
+
delivered(pending: PendingIntervention): void {
|
|
197
|
+
if (pending.key.generation !== this.pendingGeneration) {
|
|
198
|
+
// The intervention belongs to a request that is gone.
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
this.tracker.markSpoken(pending.state, pending.severity);
|
|
202
|
+
this.speechCount += 1;
|
|
203
|
+
}
|
|
204
|
+
|
|
199
205
|
/**
|
|
200
206
|
* True when the guard may speak.
|
|
201
207
|
*
|
|
202
208
|
* The guard speaks once per checkpoint revision, and again when the
|
|
203
209
|
* seriousness of the intervention grew. It speaks at most three times
|
|
204
210
|
* in one request: a request that ignored the warnings is not nagged.
|
|
211
|
+
*
|
|
212
|
+
* The memory of the guard is the state of the request: it is durable,
|
|
213
|
+
* it belongs to the request, and it is only written when the
|
|
214
|
+
* intervention really reached the agent. A delivery that was refused
|
|
215
|
+
* spends nothing, so the intervention may be spoken again.
|
|
205
216
|
*/
|
|
206
217
|
private maySpeak(severity: number, state: ExplorationState): boolean {
|
|
207
218
|
if (this.speechCount >= MAX_SPEECHES_PER_REQUEST) return false;
|
|
208
|
-
const
|
|
209
|
-
const
|
|
219
|
+
const spokenRevision = state.spokenRevision ?? -1;
|
|
220
|
+
const spokenSeverity = state.spokenSeverity ?? 0;
|
|
221
|
+
const revisionChanged = spokenRevision !== state.checkpointRevision;
|
|
222
|
+
const escalated = severity > spokenSeverity;
|
|
210
223
|
return revisionChanged || escalated;
|
|
211
224
|
}
|
|
212
225
|
|
|
@@ -253,8 +266,6 @@ export class ExplorationGuardRuntime {
|
|
|
253
266
|
this.inactive = false;
|
|
254
267
|
this.pending = null;
|
|
255
268
|
this.speechCount = 0;
|
|
256
|
-
this.lastSeverity = -1;
|
|
257
|
-
this.spokenRevision = -1;
|
|
258
269
|
}
|
|
259
270
|
|
|
260
271
|
/**
|
|
@@ -42,6 +42,14 @@ export interface JevRejectionContext {
|
|
|
42
42
|
blocked?: string[];
|
|
43
43
|
/** The work that may continue on its own. */
|
|
44
44
|
independentWork?: string;
|
|
45
|
+
/**
|
|
46
|
+
* True when the work was already paused before the refusal.
|
|
47
|
+
*
|
|
48
|
+
* A pause of the user is not the pause of the refusal: the approval of
|
|
49
|
+
* the human releases the action, but it does not clear a pause the user
|
|
50
|
+
* set with a "stop".
|
|
51
|
+
*/
|
|
52
|
+
pausedBefore?: boolean;
|
|
45
53
|
/** The deadline of the answer, in words. */
|
|
46
54
|
deadlineText?: string;
|
|
47
55
|
/**
|
|
@@ -104,6 +112,20 @@ export interface JevRejectionEscalationDeps {
|
|
|
104
112
|
* The human rejected the action.
|
|
105
113
|
*/
|
|
106
114
|
onRejected(state: EscalationState): void;
|
|
115
|
+
/**
|
|
116
|
+
* How many times the delivery is attempted.
|
|
117
|
+
*
|
|
118
|
+
* A channel that refused the message may refuse the next one too, but a
|
|
119
|
+
* transient failure must not leave the work blocked without a reason.
|
|
120
|
+
* The default is three attempts.
|
|
121
|
+
*/
|
|
122
|
+
maxDeliveryAttempts?: number;
|
|
123
|
+
/**
|
|
124
|
+
* The pause between two attempts.
|
|
125
|
+
*
|
|
126
|
+
* The default is 250 milliseconds.
|
|
127
|
+
*/
|
|
128
|
+
retryDelayMs?: number;
|
|
107
129
|
/** The log of the runtime. */
|
|
108
130
|
log?(message: string): void;
|
|
109
131
|
}
|
|
@@ -142,6 +164,35 @@ export function buildJevRejectionMessage(context: JevRejectionContext): string {
|
|
|
142
164
|
return lines.join("\n");
|
|
143
165
|
}
|
|
144
166
|
|
|
167
|
+
/**
|
|
168
|
+
* The continuation that carries the approval of the human.
|
|
169
|
+
*
|
|
170
|
+
* The approval releases the block of the action the judge refused, so the
|
|
171
|
+
* agent has to learn about it: a resume alone leaves the session idle and
|
|
172
|
+
* the work blocked. The message names the approved action and the scope
|
|
173
|
+
* that is released.
|
|
174
|
+
*
|
|
175
|
+
* The approval is not a passed validation: the message says that the
|
|
176
|
+
* checks of the repository still run, so the agent does not treat the
|
|
177
|
+
* approval as a licence that skips them.
|
|
178
|
+
*/
|
|
179
|
+
export function buildJevApprovalContinuation(
|
|
180
|
+
context: JevRejectionContext,
|
|
181
|
+
questionId: string,
|
|
182
|
+
): string {
|
|
183
|
+
const lines: string[] = [];
|
|
184
|
+
lines.push(`The human approved the action of ${questionId}.`);
|
|
185
|
+
lines.push(`Approved action: ${context.action}.`);
|
|
186
|
+
const blocked = context.blocked?.filter((entry) => entry.trim() !== "");
|
|
187
|
+
if (blocked?.length) {
|
|
188
|
+
lines.push(`Released scope: ${blocked.join(", ")}.`);
|
|
189
|
+
}
|
|
190
|
+
lines.push(
|
|
191
|
+
"Continue the work of the request. The approval releases the block of this action; it does not replace the checks of the repository, so the validation and the safety checks still run.",
|
|
192
|
+
);
|
|
193
|
+
return lines.join("\n");
|
|
194
|
+
}
|
|
195
|
+
|
|
145
196
|
/**
|
|
146
197
|
* The runtime of the escalation.
|
|
147
198
|
*/
|
|
@@ -174,9 +225,13 @@ export class JevRejectionEscalationRuntime {
|
|
|
174
225
|
*/
|
|
175
226
|
async escalate(context: JevRejectionContext): Promise<EscalationResult> {
|
|
176
227
|
const existing = this.byOriginal.get(context.originalRequestId);
|
|
177
|
-
if (existing
|
|
228
|
+
if (existing) {
|
|
229
|
+
// The question was already asked for this request. The human is
|
|
230
|
+
// asked once: a second question about the same refusal would be
|
|
231
|
+
// a duplicate, and the buttons of an answered question are
|
|
232
|
+
// stale.
|
|
178
233
|
this.deps.log?.(
|
|
179
|
-
`The escalation of ${context.originalRequestId}
|
|
234
|
+
`The escalation of ${context.originalRequestId} was already asked (${existing.status}) - it is not asked again.`,
|
|
180
235
|
);
|
|
181
236
|
return {
|
|
182
237
|
state: existing,
|
|
@@ -196,7 +251,7 @@ export class JevRejectionEscalationRuntime {
|
|
|
196
251
|
this.byOriginal.set(context.originalRequestId, state);
|
|
197
252
|
this.byQuestion.set(questionId, state);
|
|
198
253
|
|
|
199
|
-
const results = await this.
|
|
254
|
+
const results = await this.deliverWithRetry(questionId, text);
|
|
200
255
|
for (const result of results) {
|
|
201
256
|
if (result.success) {
|
|
202
257
|
state.delivered.push(result.channel);
|
|
@@ -221,6 +276,74 @@ export class JevRejectionEscalationRuntime {
|
|
|
221
276
|
return { state, deliveredAtLeastOne, reused: false };
|
|
222
277
|
}
|
|
223
278
|
|
|
279
|
+
/**
|
|
280
|
+
* Deliver the escalation, with a bounded number of attempts.
|
|
281
|
+
*
|
|
282
|
+
* A channel that refused the message may refuse the next one too, but a
|
|
283
|
+
* transient failure must not leave the work blocked without a reason.
|
|
284
|
+
* The question is the same one in every attempt: a retry never asks the
|
|
285
|
+
* human twice, and the buttons of an earlier attempt stay valid.
|
|
286
|
+
*/
|
|
287
|
+
private async deliverWithRetry(
|
|
288
|
+
questionId: string,
|
|
289
|
+
text: string,
|
|
290
|
+
): Promise<Pick<NotificationResult, "success" | "channel" | "error">[]> {
|
|
291
|
+
const maxAttempts = Math.max(1, this.deps.maxDeliveryAttempts ?? 3);
|
|
292
|
+
const delayMs = Math.max(0, this.deps.retryDelayMs ?? 250);
|
|
293
|
+
// The channels of every attempt are collected: a channel that
|
|
294
|
+
// refused the message in the first attempt and is not part of the
|
|
295
|
+
// second one has still refused it, and the report of the failure
|
|
296
|
+
// must name it.
|
|
297
|
+
const channels = new Map<
|
|
298
|
+
string,
|
|
299
|
+
Pick<NotificationResult, "success" | "channel" | "error">
|
|
300
|
+
>();
|
|
301
|
+
let attempt = 1;
|
|
302
|
+
for (;;) {
|
|
303
|
+
const results = await this.attemptDelivery(questionId, text);
|
|
304
|
+
for (const result of results) {
|
|
305
|
+
const known = channels.get(result.channel);
|
|
306
|
+
if (!known || (!known.success && result.success)) {
|
|
307
|
+
channels.set(result.channel, { ...result });
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (results.some((result) => result.success) || attempt >= maxAttempts) {
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
attempt += 1;
|
|
314
|
+
this.deps.log?.(
|
|
315
|
+
`The escalation ${questionId} was not delivered - the attempt ${attempt} of ${maxAttempts} follows.`,
|
|
316
|
+
);
|
|
317
|
+
if (delayMs > 0) {
|
|
318
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return [...channels.values()];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* One attempt of the delivery.
|
|
326
|
+
*
|
|
327
|
+
* A channel that throws is a failed attempt, not a crash: a throw that
|
|
328
|
+
* escaped would leave the escalation in the state "open" while nobody
|
|
329
|
+
* was asked, and the work would wait for a human who never got the
|
|
330
|
+
* question.
|
|
331
|
+
*/
|
|
332
|
+
private async attemptDelivery(
|
|
333
|
+
questionId: string,
|
|
334
|
+
text: string,
|
|
335
|
+
): Promise<Pick<NotificationResult, "success" | "channel" | "error">[]> {
|
|
336
|
+
try {
|
|
337
|
+
return await this.deps.deliver(questionId, text);
|
|
338
|
+
} catch (error) {
|
|
339
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
340
|
+
this.deps.log?.(
|
|
341
|
+
`The delivery of the escalation ${questionId} failed: ${reason}`,
|
|
342
|
+
);
|
|
343
|
+
return [{ success: false, channel: "notification", error: reason }];
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
224
347
|
/**
|
|
225
348
|
* The answer of the human.
|
|
226
349
|
*
|
|
@@ -230,7 +353,11 @@ export class JevRejectionEscalationRuntime {
|
|
|
230
353
|
answer(questionId: string, answer: "yes" | "no"): boolean {
|
|
231
354
|
const state = this.byQuestion.get(questionId);
|
|
232
355
|
if (!state) return false;
|
|
233
|
-
|
|
356
|
+
// A refusal that nobody received may still be answered: the human
|
|
357
|
+
// saw it in the session, or a channel that reported a failure
|
|
358
|
+
// delivered the message anyway. The row of the blocked refusal is
|
|
359
|
+
// removed by the answer, so the refusal must be answerable.
|
|
360
|
+
if (state.status !== "open" && state.status !== "blocked") {
|
|
234
361
|
this.deps.log?.(
|
|
235
362
|
`The escalation ${questionId} was answered already - the answer is stale.`,
|
|
236
363
|
);
|
|
@@ -238,10 +365,21 @@ export class JevRejectionEscalationRuntime {
|
|
|
238
365
|
}
|
|
239
366
|
state.status = "answered";
|
|
240
367
|
state.answer = answer;
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
368
|
+
// The hook of the harness decides what happens with the work. A
|
|
369
|
+
// hook that throws does not undo the answer - the answer is the
|
|
370
|
+
// fact - but it must not leave the harness in a state where
|
|
371
|
+
// nothing happened and every later click is refused as stale.
|
|
372
|
+
try {
|
|
373
|
+
if (answer === "yes") {
|
|
374
|
+
this.deps.onApproved(state);
|
|
375
|
+
} else {
|
|
376
|
+
this.deps.onRejected(state);
|
|
377
|
+
}
|
|
378
|
+
} catch (error) {
|
|
379
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
380
|
+
this.deps.log?.(
|
|
381
|
+
`The escalation ${questionId} was answered, but the harness failed to continue the work: ${reason}`,
|
|
382
|
+
);
|
|
245
383
|
}
|
|
246
384
|
return true;
|
|
247
385
|
}
|
|
@@ -110,7 +110,7 @@ interface CountdownBinding {
|
|
|
110
110
|
export class PendingQuestionCountdown {
|
|
111
111
|
private readonly deps: PendingCountdownDeps;
|
|
112
112
|
private readonly now: () => number;
|
|
113
|
-
private readonly
|
|
113
|
+
private readonly startTimerFn: (callback: () => void, ms: number) => unknown;
|
|
114
114
|
private readonly stopTimer: (handle: unknown) => void;
|
|
115
115
|
private readonly tickMs: number;
|
|
116
116
|
private handle: unknown = null;
|
|
@@ -142,7 +142,7 @@ export class PendingQuestionCountdown {
|
|
|
142
142
|
constructor(deps: PendingCountdownDeps) {
|
|
143
143
|
this.deps = deps;
|
|
144
144
|
this.now = deps.now ?? (() => Date.now());
|
|
145
|
-
this.
|
|
145
|
+
this.startTimerFn =
|
|
146
146
|
deps.setInterval ??
|
|
147
147
|
((callback, ms) => setInterval(callback, ms) as unknown);
|
|
148
148
|
this.stopTimer =
|
|
@@ -177,6 +177,44 @@ export class PendingQuestionCountdown {
|
|
|
177
177
|
this.begin(requestId, decisionDeadlineAt(startedAt, this.deps.waitMinutes));
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Move the deadline of the running countdown.
|
|
182
|
+
*
|
|
183
|
+
* The countdown owns the deadline of the decision, so the displayed
|
|
184
|
+
* time and the time the decision uses cannot drift apart. A question
|
|
185
|
+
* whose window starts with the delivery of the message has a later
|
|
186
|
+
* deadline than the moment the question was asked: the deadline is
|
|
187
|
+
* moved to that moment, so the row of the countdown and the decision
|
|
188
|
+
* show the same time.
|
|
189
|
+
*
|
|
190
|
+
* A countdown of another request, or no countdown at all, is left
|
|
191
|
+
* alone: the deadline of a question that is not running is not moved.
|
|
192
|
+
*/
|
|
193
|
+
rebase(requestId: string, deadlineAt: number): void {
|
|
194
|
+
const binding = this.binding;
|
|
195
|
+
if (!binding || binding.requestId !== requestId) return;
|
|
196
|
+
if (!Number.isFinite(deadlineAt) || deadlineAt <= 0) return;
|
|
197
|
+
if (binding.deadlineAt === deadlineAt) return;
|
|
198
|
+
binding.deadlineAt = deadlineAt;
|
|
199
|
+
// The deadline moved: the countdown did not fire yet.
|
|
200
|
+
this.deadlineFired = false;
|
|
201
|
+
// A countdown that already fired has no timer: the timer was
|
|
202
|
+
// released with the deadline. The new deadline needs a timer again,
|
|
203
|
+
// or the row would show a time that never arrives and the decision
|
|
204
|
+
// would never start.
|
|
205
|
+
if (this.handle === null) {
|
|
206
|
+
this.startTimer(this.generation);
|
|
207
|
+
}
|
|
208
|
+
// The clock is read once: the row shows the time that is left at
|
|
209
|
+
// the moment the row was drawn.
|
|
210
|
+
const remainingMs = Math.max(0, deadlineAt - this.now());
|
|
211
|
+
this.safeRender({
|
|
212
|
+
requestId: binding.requestId,
|
|
213
|
+
remainingMs,
|
|
214
|
+
remaining: formatRemainingTime(remainingMs),
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
180
218
|
/**
|
|
181
219
|
* Stop the countdown and release its timer.
|
|
182
220
|
*
|
|
@@ -202,21 +240,36 @@ export class PendingQuestionCountdown {
|
|
|
202
240
|
this.binding = { requestId, deadlineAt };
|
|
203
241
|
this.engaged = true;
|
|
204
242
|
this.deadlineFired = false;
|
|
205
|
-
|
|
206
|
-
this.handle = this.startTimer(() => this.tick(generation), this.tickMs);
|
|
207
|
-
} catch (error) {
|
|
243
|
+
if (!this.startTimer(generation)) {
|
|
208
244
|
// The timer could not be created: the countdown owns nothing, so
|
|
209
245
|
// it must not claim the wait. The row of the countdown is
|
|
210
246
|
// cleared with it, so no row claims a decision that is not due.
|
|
211
247
|
this.engaged = false;
|
|
212
248
|
this.binding = null;
|
|
213
249
|
this.safeCall(() => this.deps.onStop?.(requestId));
|
|
214
|
-
this.report(error);
|
|
215
250
|
return;
|
|
216
251
|
}
|
|
217
252
|
this.tick(generation);
|
|
218
253
|
}
|
|
219
254
|
|
|
255
|
+
/**
|
|
256
|
+
* Start the timer of the countdown.
|
|
257
|
+
*
|
|
258
|
+
* The timer belongs to the generation of the binding: a tick of an
|
|
259
|
+
* older generation is ignored.
|
|
260
|
+
*/
|
|
261
|
+
private startTimer(generation: number): boolean {
|
|
262
|
+
try {
|
|
263
|
+
this.handle = this.startTimerFn(() => this.tick(generation), this.tickMs);
|
|
264
|
+
return true;
|
|
265
|
+
} catch (error) {
|
|
266
|
+
// The timer could not be created: the failure is reported and
|
|
267
|
+
// the countdown owns nothing.
|
|
268
|
+
this.report(error);
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
220
273
|
/**
|
|
221
274
|
* One tick of the countdown.
|
|
222
275
|
*
|