pi-harness-runtime 1.1.508 → 1.1.510
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 +175 -0
- package/harness/auto-resume-marker.ts +55 -13
- package/harness/local-refusal-marker.ts +104 -0
- package/harness/scope-expansion-runtime.ts +142 -15
- package/harness/scope-expansion.ts +150 -35
- package/index.ts +229 -43
- 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/auto-continue.d.ts.map +1 -1
- package/packages/jev-judge/dist/auto-continue.js +11 -1
- package/packages/jev-judge/dist/auto-continue.js.map +1 -1
- package/packages/jev-judge/dist/question-risk.d.ts +8 -0
- package/packages/jev-judge/dist/question-risk.d.ts.map +1 -1
- package/packages/jev-judge/dist/question-risk.js +17 -0
- package/packages/jev-judge/dist/question-risk.js.map +1 -1
- package/packages/jev-judge/package.json +1 -1
- package/packages/jev-judge/src/auto-continue.ts +13 -1
- package/packages/jev-judge/src/question-risk.ts +19 -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/dist/notification-center.d.ts +8 -4
- package/packages/notification/dist/notification-center.d.ts.map +1 -1
- package/packages/notification/dist/notification-center.js +18 -9
- package/packages/notification/dist/notification-center.js.map +1 -1
- package/packages/notification/dist/notification-policy.d.ts.map +1 -1
- package/packages/notification/dist/notification-policy.js +3 -0
- package/packages/notification/dist/notification-policy.js.map +1 -1
- package/packages/notification/dist/pending-question.d.ts +89 -1
- package/packages/notification/dist/pending-question.d.ts.map +1 -1
- package/packages/notification/dist/pending-question.js +220 -4
- package/packages/notification/dist/pending-question.js.map +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/notification/src/notification-center.ts +18 -9
- package/packages/notification/src/notification-policy.ts +3 -0
- package/packages/notification/src/pending-question.ts +287 -3
- 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
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
import {
|
|
21
|
+
MAX_SCOPE_CANDIDATES,
|
|
21
22
|
SCOPE_EXPANSION_WINDOW_MS,
|
|
22
23
|
ScopeExpansionController,
|
|
23
24
|
createScopeProposal,
|
|
@@ -112,11 +113,22 @@ export class ScopeExpansionRuntime {
|
|
|
112
113
|
private readonly controller: ScopeExpansionController;
|
|
113
114
|
private timers = new Map<string, ScopeTimerHandle>();
|
|
114
115
|
/** The proposal of every generation, for the group check. */
|
|
115
|
-
private dispatched = new Map<string, ScopeProposal>();
|
|
116
|
+
private dispatched = new Map<string, { proposal: ScopeProposal; ids: string[] }>();
|
|
117
|
+
/**
|
|
118
|
+
* The runtime was disposed.
|
|
119
|
+
*
|
|
120
|
+
* A disposed runtime belongs to a session that ended: it decides
|
|
121
|
+
* nothing, it dispatches nothing and it keeps no timer. Without this
|
|
122
|
+
* flag a timer of the default clock - the one the wiring uses - would
|
|
123
|
+
* fire after the session and start work in a session that is gone.
|
|
124
|
+
*/
|
|
125
|
+
private disposed = false;
|
|
116
126
|
/** The proposal of every registered question. */
|
|
117
127
|
private questions = new Map<string, string>();
|
|
118
128
|
/** The question of every proposal. */
|
|
119
129
|
private questionIds = new Map<string, string>();
|
|
130
|
+
/** The number of the proposals of this runtime. */
|
|
131
|
+
private proposalCounter = 0;
|
|
120
132
|
|
|
121
133
|
constructor(deps: ScopeExpansionRuntimeDeps) {
|
|
122
134
|
this.deps = deps;
|
|
@@ -129,7 +141,21 @@ export class ScopeExpansionRuntime {
|
|
|
129
141
|
}),
|
|
130
142
|
judge: (proposal) => deps.judge(proposal),
|
|
131
143
|
dispatch: (proposal, ids) => {
|
|
132
|
-
|
|
144
|
+
// A disposed runtime belongs to a session that ended: it
|
|
145
|
+
// dispatches nothing.
|
|
146
|
+
if (this.disposed) {
|
|
147
|
+
this.deps.log?.(
|
|
148
|
+
`The scope proposal ${proposal.id} is not dispatched: the runtime is disposed.`,
|
|
149
|
+
);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
// The ids that were dispatched are the ids of the group: the
|
|
153
|
+
// check of the group asks about the work that started, not
|
|
154
|
+
// about the ids a decision named.
|
|
155
|
+
this.dispatched.set(proposal.id, {
|
|
156
|
+
proposal,
|
|
157
|
+
ids: [...ids],
|
|
158
|
+
});
|
|
133
159
|
deps.dispatch(proposal, ids);
|
|
134
160
|
},
|
|
135
161
|
log: (message) => deps.log?.(message),
|
|
@@ -156,7 +182,11 @@ export class ScopeExpansionRuntime {
|
|
|
156
182
|
* can stop.
|
|
157
183
|
*/
|
|
158
184
|
async propose(request: ScopeProposalRequest): Promise<ScopeProposal | null> {
|
|
159
|
-
|
|
185
|
+
if (this.disposed) return null;
|
|
186
|
+
// The candidates are bounded here as well: a proposal that names an
|
|
187
|
+
// id beyond the bound would find no known candidate and would
|
|
188
|
+
// throw where the caller expects nothing to happen.
|
|
189
|
+
const candidates = request.candidates.slice(0, MAX_SCOPE_CANDIDATES);
|
|
160
190
|
if (candidates.length === 0) {
|
|
161
191
|
this.deps.log?.("No scope proposal: the backlog has no candidate.");
|
|
162
192
|
return null;
|
|
@@ -168,7 +198,15 @@ export class ScopeExpansionRuntime {
|
|
|
168
198
|
this.deps.log?.("No scope proposal: no candidate is known.");
|
|
169
199
|
return null;
|
|
170
200
|
}
|
|
171
|
-
|
|
201
|
+
// The identity of a proposal is unique: two proposals of the same
|
|
202
|
+
// generation with the same first id would answer each other, and the
|
|
203
|
+
// second answer of the human would be dropped.
|
|
204
|
+
const base = `scope-${request.generation}-${proposedIds[0]}`;
|
|
205
|
+
let id = base;
|
|
206
|
+
if (this.controller.get(base)) {
|
|
207
|
+
this.proposalCounter += 1;
|
|
208
|
+
id = `scope-${request.generation}-${this.proposalCounter}-${proposedIds[0]}`;
|
|
209
|
+
}
|
|
172
210
|
const proposal = createScopeProposal({
|
|
173
211
|
id,
|
|
174
212
|
sessionId: "",
|
|
@@ -193,7 +231,17 @@ export class ScopeExpansionRuntime {
|
|
|
193
231
|
this.questions.set(questionId, proposal.id);
|
|
194
232
|
this.questionIds.set(proposal.id, questionId);
|
|
195
233
|
}
|
|
196
|
-
|
|
234
|
+
let delivered = false;
|
|
235
|
+
try {
|
|
236
|
+
delivered = await this.controller.deliverProposal(proposal);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
// A delivery that throws is a delivery that happened nowhere: the
|
|
239
|
+
// proposal must not stay registered with an open question.
|
|
240
|
+
this.deps.log?.(
|
|
241
|
+
`The delivery of the scope proposal ${proposal.id} failed: ${String(error)}`,
|
|
242
|
+
);
|
|
243
|
+
delivered = false;
|
|
244
|
+
}
|
|
197
245
|
if (!delivered) {
|
|
198
246
|
// A proposal that reached nobody leaves no open question behind:
|
|
199
247
|
// an open question blocks the work of the session.
|
|
@@ -219,11 +267,18 @@ export class ScopeExpansionRuntime {
|
|
|
219
267
|
outcome: HumanScopeOutcome,
|
|
220
268
|
reasoning: string,
|
|
221
269
|
): Promise<{ started: boolean; reason?: string }> {
|
|
270
|
+
if (this.disposed) {
|
|
271
|
+
return { started: false, reason: "the runtime is disposed" };
|
|
272
|
+
}
|
|
222
273
|
const decision = this.controller.answer(proposalId, outcome, reasoning);
|
|
223
274
|
if (!decision) {
|
|
224
275
|
return { started: false, reason: "the proposal has no open answer" };
|
|
225
276
|
}
|
|
226
|
-
|
|
277
|
+
const result = await this.controller.applyDecision(proposalId);
|
|
278
|
+
// The question of a decided proposal is closed: an open question
|
|
279
|
+
// blocks the work of the session.
|
|
280
|
+
this.closeQuestion(proposalId);
|
|
281
|
+
return result;
|
|
227
282
|
}
|
|
228
283
|
|
|
229
284
|
/**
|
|
@@ -237,6 +292,9 @@ export class ScopeExpansionRuntime {
|
|
|
237
292
|
outcome: HumanScopeOutcome,
|
|
238
293
|
reasoning: string,
|
|
239
294
|
): Promise<{ started: boolean; reason?: string }> {
|
|
295
|
+
if (this.disposed) {
|
|
296
|
+
return { started: false, reason: "the runtime is disposed" };
|
|
297
|
+
}
|
|
240
298
|
const proposalId = this.questions.get(questionId);
|
|
241
299
|
if (!proposalId) {
|
|
242
300
|
return { started: false, reason: "the question belongs to no proposal" };
|
|
@@ -263,10 +321,19 @@ export class ScopeExpansionRuntime {
|
|
|
263
321
|
* work starts.
|
|
264
322
|
*/
|
|
265
323
|
async evaluateDue(proposalId: string): Promise<ScopeDecision | null> {
|
|
324
|
+
if (this.disposed) return null;
|
|
266
325
|
const decision = await this.controller.evaluateDue(proposalId);
|
|
267
326
|
if (!decision) return null;
|
|
327
|
+
// The controller turns a failed evaluation into a wait: it
|
|
328
|
+
// authorizes nothing, and the decision is still delivered, so the
|
|
329
|
+
// human learns that the window ended without an answer.
|
|
268
330
|
await this.controller.applyDecision(proposalId);
|
|
269
|
-
|
|
331
|
+
// The decision that was applied is the decision that counts: the
|
|
332
|
+
// controller may have replaced the decision of the evaluation.
|
|
333
|
+
// The question of a decided proposal is closed: an open question
|
|
334
|
+
// blocks the work of the session.
|
|
335
|
+
this.closeQuestion(proposalId);
|
|
336
|
+
return this.controller.get(proposalId)?.decision ?? decision;
|
|
270
337
|
}
|
|
271
338
|
|
|
272
339
|
/**
|
|
@@ -276,33 +343,59 @@ export class ScopeExpansionRuntime {
|
|
|
276
343
|
* the next group of the backlog.
|
|
277
344
|
*/
|
|
278
345
|
async checkGroupFinished(): Promise<void> {
|
|
279
|
-
for (const
|
|
280
|
-
const ids =
|
|
346
|
+
for (const entry of this.dispatched.values()) {
|
|
347
|
+
const { proposal, ids } = entry;
|
|
281
348
|
if (ids.length === 0) continue;
|
|
282
349
|
if (!(await this.deps.groupFinished?.(proposal, ids))) continue;
|
|
283
350
|
this.controller.finishGroup(proposal.id);
|
|
284
351
|
this.clearTimer(proposal.id);
|
|
352
|
+
this.closeQuestion(proposal.id);
|
|
285
353
|
this.dispatched.delete(proposal.id);
|
|
286
354
|
}
|
|
287
355
|
}
|
|
288
356
|
|
|
289
|
-
/**
|
|
357
|
+
/**
|
|
358
|
+
* Stop the runtime: no timer survives the session.
|
|
359
|
+
*
|
|
360
|
+
* The timer of the default clock is cancelled with the same clock that
|
|
361
|
+
* armed it: a runtime that keeps its timer would decide the backlog of
|
|
362
|
+
* a session that ended, and the decision would start work in a session
|
|
363
|
+
* that does not exist any more.
|
|
364
|
+
*/
|
|
290
365
|
dispose(): void {
|
|
366
|
+
this.disposed = true;
|
|
291
367
|
for (const handle of this.timers.values()) {
|
|
292
|
-
this.
|
|
368
|
+
this.cancelTimer(handle);
|
|
293
369
|
}
|
|
294
370
|
this.timers.clear();
|
|
371
|
+
// A question of a proposal that nobody answered must not stay open:
|
|
372
|
+
// the controller of the questions lives in the process, and an open
|
|
373
|
+
// question of a dead session blocks the next one.
|
|
374
|
+
for (const questionId of this.questions.keys()) {
|
|
375
|
+
this.deps.cancelQuestion?.(questionId);
|
|
376
|
+
}
|
|
377
|
+
this.questions.clear();
|
|
378
|
+
this.questionIds.clear();
|
|
295
379
|
this.dispatched.clear();
|
|
296
380
|
}
|
|
297
381
|
|
|
298
382
|
private scheduleEvaluation(proposal: ScopeProposal): void {
|
|
299
383
|
const delay = Math.max(0, proposal.deadlineAt - this.deps.now());
|
|
300
|
-
const
|
|
301
|
-
const handle = setTimer(() => {
|
|
384
|
+
const handle = this.armTimer(() => {
|
|
302
385
|
this.timers.delete(proposal.id);
|
|
303
386
|
return (async () => {
|
|
387
|
+
if (this.disposed) return;
|
|
304
388
|
try {
|
|
305
|
-
await this.evaluateDue(proposal.id);
|
|
389
|
+
const decision = await this.evaluateDue(proposal.id);
|
|
390
|
+
// A timer that fired before the deadline leaves the window
|
|
391
|
+
// open: the timer is armed again for the rest of it, so
|
|
392
|
+
// the proposal always ends in a decision.
|
|
393
|
+
if (decision === null && !this.disposed) {
|
|
394
|
+
const current = this.controller.get(proposal.id);
|
|
395
|
+
if (current?.state === "proposed") {
|
|
396
|
+
this.scheduleEvaluation(proposal);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
306
399
|
} catch (error) {
|
|
307
400
|
this.deps.log?.(
|
|
308
401
|
`The evaluation of the scope proposal ${proposal.id} failed: ${String(error)}`,
|
|
@@ -313,13 +406,47 @@ export class ScopeExpansionRuntime {
|
|
|
313
406
|
this.timers.set(proposal.id, handle);
|
|
314
407
|
}
|
|
315
408
|
|
|
409
|
+
/** Arm a timer on the clock of the runtime, or on the default clock. */
|
|
410
|
+
private armTimer(
|
|
411
|
+
callback: () => void | Promise<void>,
|
|
412
|
+
ms: number,
|
|
413
|
+
): ScopeTimerHandle {
|
|
414
|
+
const setTimer =
|
|
415
|
+
this.deps.setTimeout ??
|
|
416
|
+
((cb: () => void, delay: number) => setTimeout(cb, delay));
|
|
417
|
+
return setTimer(callback, ms);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** Cancel a timer on the clock that armed it. */
|
|
421
|
+
private cancelTimer(handle: ScopeTimerHandle): void {
|
|
422
|
+
const clearTimer =
|
|
423
|
+
this.deps.clearTimeout ??
|
|
424
|
+
((h: ScopeTimerHandle) =>
|
|
425
|
+
clearTimeout(h as ReturnType<typeof setTimeout>));
|
|
426
|
+
clearTimer(handle);
|
|
427
|
+
}
|
|
428
|
+
|
|
316
429
|
private clearTimer(id: string): void {
|
|
317
430
|
const handle = this.timers.get(id);
|
|
318
431
|
if (handle === undefined) return;
|
|
319
|
-
this.
|
|
432
|
+
this.cancelTimer(handle);
|
|
320
433
|
this.timers.delete(id);
|
|
321
434
|
}
|
|
322
435
|
|
|
436
|
+
/**
|
|
437
|
+
* Close the question of a proposal.
|
|
438
|
+
*
|
|
439
|
+
* A proposal that reached its end does not keep a question open: an
|
|
440
|
+
* open question blocks the work of the session.
|
|
441
|
+
*/
|
|
442
|
+
private closeQuestion(proposalId: string): void {
|
|
443
|
+
const questionId = this.questionIds.get(proposalId);
|
|
444
|
+
if (!questionId) return;
|
|
445
|
+
this.deps.cancelQuestion?.(questionId);
|
|
446
|
+
this.questions.delete(questionId);
|
|
447
|
+
this.questionIds.delete(proposalId);
|
|
448
|
+
}
|
|
449
|
+
|
|
323
450
|
/**
|
|
324
451
|
* The message of a proposal, for the caller that delivers it itself.
|
|
325
452
|
*/
|
|
@@ -105,6 +105,8 @@ export interface CreateScopeProposalInput {
|
|
|
105
105
|
scope: string;
|
|
106
106
|
validation: string;
|
|
107
107
|
risks?: string[];
|
|
108
|
+
/** The choices the human has. */
|
|
109
|
+
choices?: string[];
|
|
108
110
|
now: number;
|
|
109
111
|
}
|
|
110
112
|
|
|
@@ -113,16 +115,22 @@ export interface CreateScopeProposalInput {
|
|
|
113
115
|
*
|
|
114
116
|
* The proposal names a bounded subset of the candidates: a candidate that
|
|
115
117
|
* is not named is not proposed, and an id that is not a candidate is
|
|
116
|
-
* refused. The
|
|
117
|
-
*
|
|
118
|
+
* refused. The bound applies to the proposed ids, and an id is checked
|
|
119
|
+
* against the whole list of the candidates: a bound that drops a known id
|
|
120
|
+
* would refuse a proposal of ids that the caller knew.
|
|
121
|
+
*
|
|
122
|
+
* The deadline is absolute, so every reader measures the same moment.
|
|
118
123
|
*/
|
|
119
124
|
export function createScopeProposal(input: CreateScopeProposalInput): ScopeProposal {
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
|
|
125
|
+
const known = new Set(input.candidates.map((candidate) => candidate.id));
|
|
126
|
+
const proposedIds = input.proposedIds
|
|
127
|
+
.filter((id) => known.has(id))
|
|
128
|
+
.slice(0, MAX_SCOPE_CANDIDATES);
|
|
123
129
|
if (proposedIds.length === 0) {
|
|
124
130
|
throw new Error("a scope proposal needs at least one candidate id");
|
|
125
131
|
}
|
|
132
|
+
const proposed = new Set(proposedIds);
|
|
133
|
+
const candidates = input.candidates.filter((candidate) => proposed.has(candidate.id));
|
|
126
134
|
return {
|
|
127
135
|
id: input.id,
|
|
128
136
|
sessionId: input.sessionId,
|
|
@@ -134,7 +142,10 @@ export function createScopeProposal(input: CreateScopeProposalInput): ScopePropo
|
|
|
134
142
|
scope: input.scope,
|
|
135
143
|
validation: input.validation,
|
|
136
144
|
risks: input.risks ?? [],
|
|
137
|
-
choices:
|
|
145
|
+
choices:
|
|
146
|
+
input.choices && input.choices.length > 0
|
|
147
|
+
? input.choices
|
|
148
|
+
: ["approve the proposed subset", "decline", "revise the proposal"],
|
|
138
149
|
createdAt: input.now,
|
|
139
150
|
deadlineAt: input.now + SCOPE_EXPANSION_WINDOW_MS,
|
|
140
151
|
state: "proposed",
|
|
@@ -162,7 +173,7 @@ export function scopeProposalMessage(proposal: ScopeProposal): string {
|
|
|
162
173
|
lines.push(
|
|
163
174
|
`Choices: ${proposal.choices.join(" / ")}.`,
|
|
164
175
|
`Deadline: ${new Date(proposal.deadlineAt).toISOString()}.`,
|
|
165
|
-
`If unanswered for
|
|
176
|
+
`If unanswered for ${Math.round(SCOPE_EXPANSION_WINDOW_MS / 60_000)} minutes, Jev may decide this proposal under your policy.`,
|
|
166
177
|
);
|
|
167
178
|
return lines.join("\n");
|
|
168
179
|
}
|
|
@@ -170,18 +181,22 @@ export function scopeProposalMessage(proposal: ScopeProposal): string {
|
|
|
170
181
|
/**
|
|
171
182
|
* The message of the decision.
|
|
172
183
|
*
|
|
173
|
-
* The decision names
|
|
174
|
-
* starts or the session stays
|
|
184
|
+
* The decision names the proposal, what was decided, the scope that is
|
|
185
|
+
* really authorized, and whether the work starts or the session stays
|
|
186
|
+
* stopped.
|
|
175
187
|
*/
|
|
176
188
|
export function scopeDecisionMessage(
|
|
177
189
|
proposal: ScopeProposal,
|
|
178
190
|
decision: ScopeDecision,
|
|
179
191
|
action: "starting the selected work" | "remaining stopped",
|
|
192
|
+
authorizedIds?: readonly string[],
|
|
180
193
|
): string {
|
|
181
|
-
const who = decision.source === "jev" ? "Jev decision after
|
|
194
|
+
const who = decision.source === "jev" ? "Jev decision after the window" : "Your decision";
|
|
195
|
+
const ids = authorizedIds ?? decision.selectedIds;
|
|
182
196
|
return [
|
|
183
197
|
`${who}: ${decision.outcome}.`,
|
|
184
|
-
`
|
|
198
|
+
`Proposal: ${proposal.id}.`,
|
|
199
|
+
`Scope: ${ids.length > 0 ? ids.join(", ") : "none"}.`,
|
|
185
200
|
`Reason: ${decision.reasoning}`,
|
|
186
201
|
`Action: ${action}.`,
|
|
187
202
|
`Proposed: ${proposal.scope}`,
|
|
@@ -256,7 +271,16 @@ export class ScopeExpansionController {
|
|
|
256
271
|
private generationCounter = 0;
|
|
257
272
|
private answered = new Set<string>();
|
|
258
273
|
private evaluated = new Set<string>();
|
|
259
|
-
|
|
274
|
+
/** The proposals whose decision is being applied right now. */
|
|
275
|
+
private applying = new Set<string>();
|
|
276
|
+
/**
|
|
277
|
+
* The generations that are closed.
|
|
278
|
+
*
|
|
279
|
+
* Every closed generation is remembered: the approval of one group is
|
|
280
|
+
* not the approval of the next one, and that holds for every group
|
|
281
|
+
* that finished, not only for the last one.
|
|
282
|
+
*/
|
|
283
|
+
private closedGenerations = new Set<number>();
|
|
260
284
|
|
|
261
285
|
constructor(deps: ScopeExpansionDeps) {
|
|
262
286
|
this.deps = deps;
|
|
@@ -287,7 +311,7 @@ export class ScopeExpansionController {
|
|
|
287
311
|
* approval of one group is not the approval of the next one.
|
|
288
312
|
*/
|
|
289
313
|
register(proposal: ScopeProposal): boolean {
|
|
290
|
-
if (this.
|
|
314
|
+
if (this.closedGenerations.has(proposal.generation)) {
|
|
291
315
|
this.deps.log?.(
|
|
292
316
|
`The scope proposal ${proposal.id} is refused: the generation ${proposal.generation} is closed.`,
|
|
293
317
|
);
|
|
@@ -361,6 +385,20 @@ export class ScopeExpansionController {
|
|
|
361
385
|
failed: true,
|
|
362
386
|
};
|
|
363
387
|
}
|
|
388
|
+
// A failed evaluation is not a decision: it authorizes nothing, and
|
|
389
|
+
// it is a wait that ends the attempts for this proposal.
|
|
390
|
+
if (decision.failed === true) {
|
|
391
|
+
decision = {
|
|
392
|
+
outcome: "wait",
|
|
393
|
+
selectedIds: [],
|
|
394
|
+
reasoning:
|
|
395
|
+
decision.reasoning.length > 0
|
|
396
|
+
? decision.reasoning
|
|
397
|
+
: "the evaluation failed",
|
|
398
|
+
source: "jev",
|
|
399
|
+
failed: true,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
364
402
|
// The human answered while the evaluation was running: the answer
|
|
365
403
|
// wins.
|
|
366
404
|
if (this.answered.has(id)) {
|
|
@@ -381,6 +419,32 @@ export class ScopeExpansionController {
|
|
|
381
419
|
* that the human was informed and start anyway.
|
|
382
420
|
*/
|
|
383
421
|
async applyDecision(id: string): Promise<{ started: boolean; reason?: string }> {
|
|
422
|
+
// The decision of a proposal is applied once. The answer of the
|
|
423
|
+
// human that arrives while the decision is on its way starts a
|
|
424
|
+
// second application of the same proposal, and the group must not
|
|
425
|
+
// start twice.
|
|
426
|
+
if (this.applying.has(id)) {
|
|
427
|
+
return {
|
|
428
|
+
started: false,
|
|
429
|
+
reason: "the decision of the proposal is being applied",
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
this.applying.add(id);
|
|
433
|
+
try {
|
|
434
|
+
return await this.applyDecisionOnce(id);
|
|
435
|
+
} finally {
|
|
436
|
+
this.applying.delete(id);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Apply the decision of a proposal.
|
|
442
|
+
*
|
|
443
|
+
* The decision is applied once: the caller holds the lock.
|
|
444
|
+
*/
|
|
445
|
+
private async applyDecisionOnce(
|
|
446
|
+
id: string,
|
|
447
|
+
): Promise<{ started: boolean; reason?: string }> {
|
|
384
448
|
const proposal = this.proposals.get(id);
|
|
385
449
|
if (!proposal) return { started: false, reason: "unknown proposal" };
|
|
386
450
|
const decision = proposal.decision;
|
|
@@ -391,8 +455,14 @@ export class ScopeExpansionController {
|
|
|
391
455
|
// The human decides. The answer is read before the delivery, so the
|
|
392
456
|
// delivered message and the executed work agree.
|
|
393
457
|
let effective = decision;
|
|
458
|
+
const recorded = this.humanDecision(proposal);
|
|
394
459
|
const latestBefore = this.deps.latestHumanDecision?.(proposal);
|
|
395
|
-
if (
|
|
460
|
+
if (recorded) {
|
|
461
|
+
// The first answer of the human wins: the answer that the
|
|
462
|
+
// controller recorded is stronger than a store that reports the
|
|
463
|
+
// last click.
|
|
464
|
+
effective = recorded;
|
|
465
|
+
} else if (latestBefore === "reject") {
|
|
396
466
|
effective = {
|
|
397
467
|
outcome: "reject",
|
|
398
468
|
selectedIds: [],
|
|
@@ -412,10 +482,13 @@ export class ScopeExpansionController {
|
|
|
412
482
|
const scope = authorizedScope(proposal, effective);
|
|
413
483
|
const action: "starting the selected work" | "remaining stopped" =
|
|
414
484
|
scope.ids.length > 0 ? "starting the selected work" : "remaining stopped";
|
|
415
|
-
const delivery = await this.deps.deliver(
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
485
|
+
const delivery = await this.deps.deliver(
|
|
486
|
+
scopeDecisionMessage(proposal, effective, action, scope.ids),
|
|
487
|
+
{
|
|
488
|
+
id: proposal.id,
|
|
489
|
+
kind: "decision",
|
|
490
|
+
},
|
|
491
|
+
);
|
|
419
492
|
if (!delivery.delivered) {
|
|
420
493
|
proposal.state = "stopped";
|
|
421
494
|
proposal.stopReason = `the decision was not delivered: ${delivery.failures.join(", ")}`;
|
|
@@ -424,32 +497,61 @@ export class ScopeExpansionController {
|
|
|
424
497
|
}
|
|
425
498
|
proposal.state = "decided";
|
|
426
499
|
// The answer is read again after the delivery: a rejection that
|
|
427
|
-
// arrived while the decision was on its way still prevents the work
|
|
500
|
+
// arrived while the decision was on its way still prevents the work,
|
|
501
|
+
// and the answer of the controller is stronger than the store.
|
|
502
|
+
const after = this.humanDecision(proposal);
|
|
428
503
|
const latestAfter = this.deps.latestHumanDecision?.(proposal);
|
|
429
|
-
|
|
504
|
+
let finalDecision = effective;
|
|
505
|
+
if (after) {
|
|
506
|
+
finalDecision = after;
|
|
507
|
+
} else if (latestAfter === "reject") {
|
|
508
|
+
finalDecision = {
|
|
509
|
+
outcome: "reject",
|
|
510
|
+
selectedIds: [],
|
|
511
|
+
reasoning: "the human rejected the proposal",
|
|
512
|
+
source: "human",
|
|
513
|
+
};
|
|
514
|
+
} else if (latestAfter === "approve" && effective.source !== "human") {
|
|
515
|
+
finalDecision = {
|
|
516
|
+
...effective,
|
|
517
|
+
outcome: "approve",
|
|
518
|
+
selectedIds: [...proposal.proposedIds],
|
|
519
|
+
source: "human",
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
if (finalDecision.outcome === "reject" && effective.outcome !== "reject") {
|
|
430
523
|
proposal.state = "stopped";
|
|
431
524
|
proposal.stopReason =
|
|
432
525
|
"the human rejected the proposal after the decision was delivered";
|
|
526
|
+
// The delivered message said something else: the human has to
|
|
527
|
+
// know that the work stays stopped. A correction that reaches
|
|
528
|
+
// nobody does not change the outcome: the work stays stopped
|
|
529
|
+
// either way.
|
|
530
|
+
const correction = await this.deps.deliver(
|
|
531
|
+
scopeDecisionMessage(proposal, finalDecision, "remaining stopped", []),
|
|
532
|
+
{ id: proposal.id, kind: "decision" },
|
|
533
|
+
);
|
|
534
|
+
if (!correction.delivered) {
|
|
535
|
+
proposal.stopReason = `${proposal.stopReason}; the correction was not delivered: ${correction.failures.join(", ")}`;
|
|
536
|
+
}
|
|
433
537
|
this.deps.log?.(
|
|
434
538
|
`The scope proposal ${proposal.id} stays unexecuted: ${proposal.stopReason}`,
|
|
435
539
|
);
|
|
436
540
|
return { started: false, reason: proposal.stopReason };
|
|
437
541
|
}
|
|
438
|
-
let finalScope =
|
|
439
|
-
if (
|
|
440
|
-
|
|
441
|
-
...effective,
|
|
442
|
-
outcome: "approve",
|
|
443
|
-
selectedIds: [...proposal.proposedIds],
|
|
444
|
-
source: "human",
|
|
445
|
-
};
|
|
446
|
-
proposal.decision = approved;
|
|
447
|
-
finalScope = authorizedScope(proposal, approved);
|
|
542
|
+
let finalScope = authorizedScope(proposal, finalDecision);
|
|
543
|
+
if (finalDecision !== effective) {
|
|
544
|
+
proposal.decision = finalDecision;
|
|
448
545
|
// The delivered message said something else: the human has to
|
|
449
|
-
// know what
|
|
546
|
+
// know what starts. A correction that reaches nobody leaves
|
|
450
547
|
// the work stopped.
|
|
451
548
|
const correction = await this.deps.deliver(
|
|
452
|
-
scopeDecisionMessage(
|
|
549
|
+
scopeDecisionMessage(
|
|
550
|
+
proposal,
|
|
551
|
+
finalDecision,
|
|
552
|
+
finalScope.ids.length > 0 ? "starting the selected work" : "remaining stopped",
|
|
553
|
+
finalScope.ids,
|
|
554
|
+
),
|
|
453
555
|
{ id: proposal.id, kind: "decision" },
|
|
454
556
|
);
|
|
455
557
|
if (!correction.delivered) {
|
|
@@ -464,8 +566,8 @@ export class ScopeExpansionController {
|
|
|
464
566
|
if (finalScope.ids.length === 0) {
|
|
465
567
|
proposal.state = "stopped";
|
|
466
568
|
proposal.stopReason =
|
|
467
|
-
|
|
468
|
-
?
|
|
569
|
+
finalDecision.outcome === "reject"
|
|
570
|
+
? finalDecision.reasoning
|
|
469
571
|
: (finalScope.reason ?? "the decision authorizes no work");
|
|
470
572
|
return { started: false, reason: proposal.stopReason };
|
|
471
573
|
}
|
|
@@ -474,6 +576,19 @@ export class ScopeExpansionController {
|
|
|
474
576
|
return { started: true };
|
|
475
577
|
}
|
|
476
578
|
|
|
579
|
+
/**
|
|
580
|
+
* The answer of the human, when the controller recorded one.
|
|
581
|
+
*
|
|
582
|
+
* The first answer wins: a second answer - a stale button of a
|
|
583
|
+
* notification that was already delivered - does not replace it.
|
|
584
|
+
*/
|
|
585
|
+
private humanDecision(proposal: ScopeProposal): ScopeDecision | undefined {
|
|
586
|
+
if (!this.answered.has(proposal.id)) return undefined;
|
|
587
|
+
const decision = proposal.decision;
|
|
588
|
+
if (!decision || decision.source !== "human") return undefined;
|
|
589
|
+
return decision;
|
|
590
|
+
}
|
|
591
|
+
|
|
477
592
|
/**
|
|
478
593
|
* The authorized group finished.
|
|
479
594
|
*
|
|
@@ -484,7 +599,7 @@ export class ScopeExpansionController {
|
|
|
484
599
|
const proposal = this.proposals.get(id);
|
|
485
600
|
if (!proposal) return;
|
|
486
601
|
proposal.state = "finished";
|
|
487
|
-
this.
|
|
602
|
+
this.closedGenerations.add(proposal.generation);
|
|
488
603
|
this.deps.log?.(
|
|
489
604
|
`The scope proposal ${proposal.id} is finished: the session stops again.`,
|
|
490
605
|
);
|