pi-harness-runtime 1.1.506 → 1.1.508
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +166 -0
- package/harness/auto-resume-marker.ts +177 -0
- package/harness/provider-quota-reset.ts +53 -19
- package/harness/provider-recovery-runtime.ts +20 -0
- package/harness/quota-capture.ts +150 -0
- package/harness/quota-low-remaining.ts +75 -0
- package/harness/quota-reset-duration.ts +87 -0
- package/harness/quota-threshold.ts +117 -0
- package/harness/resume-scope.ts +85 -0
- package/harness/scope-expansion-runtime.ts +337 -0
- package/harness/scope-expansion.ts +512 -0
- package/index.ts +608 -14
- package/mirror.ts +4 -0
- 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/package.json +1 -1
- 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/adapters/detail-format.d.ts +5 -2
- package/packages/notification/dist/adapters/detail-format.d.ts.map +1 -1
- package/packages/notification/dist/adapters/detail-format.js +29 -0
- package/packages/notification/dist/adapters/detail-format.js.map +1 -1
- package/packages/notification/dist/adapters/email-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/email-adapter.js +3 -0
- package/packages/notification/dist/adapters/email-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/line-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/line-adapter.js +3 -0
- package/packages/notification/dist/adapters/line-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/ntfy-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/ntfy-adapter.js +3 -0
- package/packages/notification/dist/adapters/ntfy-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/telegram-adapter.d.ts +0 -1
- package/packages/notification/dist/adapters/telegram-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/telegram-adapter.js +3 -3
- package/packages/notification/dist/adapters/telegram-adapter.js.map +1 -1
- package/packages/notification/dist/notification-center.d.ts.map +1 -1
- package/packages/notification/dist/notification-center.js +24 -0
- 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/types.d.ts +23 -1
- package/packages/notification/dist/types.d.ts.map +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/notification/src/adapters/detail-format.ts +45 -2
- package/packages/notification/src/adapters/email-adapter.ts +3 -0
- package/packages/notification/src/adapters/line-adapter.ts +3 -0
- package/packages/notification/src/adapters/ntfy-adapter.ts +3 -0
- package/packages/notification/src/adapters/telegram-adapter.ts +3 -4
- package/packages/notification/src/notification-center.ts +28 -0
- package/packages/notification/src/notification-policy.ts +3 -0
- package/packages/notification/src/types.ts +25 -0
- 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
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The alert of a low allowance.
|
|
3
|
+
*
|
|
4
|
+
* An allowance that falls below ten percent is worth a message: the human
|
|
5
|
+
* can act before the window is empty. The message is sent once per
|
|
6
|
+
* crossing: a capture of the same state again is not a new crossing, and
|
|
7
|
+
* an allowance that recovers arms the alert again.
|
|
8
|
+
*
|
|
9
|
+
* The alert is not a decision and not a recovery: it needs no approval
|
|
10
|
+
* and it costs no attempt of the recovery cycle.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
LOW_REMAINING_THRESHOLD_PCT,
|
|
15
|
+
quotaWindowState,
|
|
16
|
+
} from "./quota-threshold.ts";
|
|
17
|
+
|
|
18
|
+
/** What the tracker says about an observation. */
|
|
19
|
+
export type QuotaAlertDecision =
|
|
20
|
+
/** The allowance is below the threshold and the alert was not sent yet. */
|
|
21
|
+
| "notify"
|
|
22
|
+
/** The allowance is below the threshold and the alert was sent already. */
|
|
23
|
+
| "silent"
|
|
24
|
+
/** The allowance recovered; the alert is armed again. */
|
|
25
|
+
| "rearmed"
|
|
26
|
+
/** Nothing is known about the allowance. */
|
|
27
|
+
| "none";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The state of the alert, per provider.
|
|
31
|
+
*
|
|
32
|
+
* The tracker remembers the providers whose alert was sent. The state is
|
|
33
|
+
* the state of the allowance, not of the capture: a capture that says
|
|
34
|
+
* nothing does not arm the alert again.
|
|
35
|
+
*/
|
|
36
|
+
export class QuotaLowRemainingTracker {
|
|
37
|
+
private readonly thresholdPct: number;
|
|
38
|
+
private readonly alerted = new Set<string>();
|
|
39
|
+
|
|
40
|
+
constructor(thresholdPct: number = LOW_REMAINING_THRESHOLD_PCT) {
|
|
41
|
+
this.thresholdPct = thresholdPct;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Observe the used percentage of the five-hour window of a provider.
|
|
46
|
+
*
|
|
47
|
+
* The percentage is the used percentage of the mirror: the allowance
|
|
48
|
+
* that is left is its complement.
|
|
49
|
+
*/
|
|
50
|
+
observe(
|
|
51
|
+
provider: string,
|
|
52
|
+
usedPct: number | undefined | null,
|
|
53
|
+
): QuotaAlertDecision {
|
|
54
|
+
const key = provider.toLowerCase();
|
|
55
|
+
const state = quotaWindowState(usedPct);
|
|
56
|
+
if (state === "unknown") return "none";
|
|
57
|
+
if (state === "available") {
|
|
58
|
+
// The allowance recovered: the next crossing alerts again.
|
|
59
|
+
return this.alerted.delete(key) ? "rearmed" : "none";
|
|
60
|
+
}
|
|
61
|
+
if (this.alerted.has(key)) return "silent";
|
|
62
|
+
this.alerted.add(key);
|
|
63
|
+
return "notify";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Was the alert sent for this provider? */
|
|
67
|
+
isAlerted(provider: string): boolean {
|
|
68
|
+
return this.alerted.has(provider.toLowerCase());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The threshold of the alert, in percent of the remaining allowance. */
|
|
72
|
+
get threshold(): number {
|
|
73
|
+
return this.thresholdPct;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The duration of a quota reset, as the console of a provider writes it.
|
|
3
|
+
*
|
|
4
|
+
* The console of MiniMax shows the remaining time of a window as text
|
|
5
|
+
* ("Resets in 1 hr 42 min"). The mirror of the harness needs an absolute
|
|
6
|
+
* moment, because the recovery compares it with the clock when it decides
|
|
7
|
+
* when to continue.
|
|
8
|
+
*
|
|
9
|
+
* The conversion is anchored at the moment of the capture: the text
|
|
10
|
+
* describes the moment of the capture, so the epoch of the reset is the
|
|
11
|
+
* capture plus the duration. A text that is not a duration - a date, an
|
|
12
|
+
* empty field, a number without a unit - proves nothing and yields no
|
|
13
|
+
* epoch.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The milliseconds of every unit the console writes. */
|
|
17
|
+
const UNIT_MS: Record<string, number> = {
|
|
18
|
+
s: 1000,
|
|
19
|
+
sec: 1000,
|
|
20
|
+
secs: 1000,
|
|
21
|
+
second: 1000,
|
|
22
|
+
seconds: 1000,
|
|
23
|
+
m: 60_000,
|
|
24
|
+
min: 60_000,
|
|
25
|
+
mins: 60_000,
|
|
26
|
+
minute: 60_000,
|
|
27
|
+
minutes: 60_000,
|
|
28
|
+
h: 3_600_000,
|
|
29
|
+
hr: 3_600_000,
|
|
30
|
+
hrs: 3_600_000,
|
|
31
|
+
hour: 3_600_000,
|
|
32
|
+
hours: 3_600_000,
|
|
33
|
+
d: 86_400_000,
|
|
34
|
+
day: 86_400_000,
|
|
35
|
+
days: 86_400_000,
|
|
36
|
+
w: 604_800_000,
|
|
37
|
+
week: 604_800_000,
|
|
38
|
+
weeks: 604_800_000,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const DURATION_PATTERN =
|
|
42
|
+
/(\d+(?:\.\d+)?)\s*(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w)\b/gi;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The duration of a reset text, in milliseconds.
|
|
46
|
+
*
|
|
47
|
+
* The text may carry a prefix ("Resets in ...") and several units
|
|
48
|
+
* ("4 days 20 hr 42 min"). The units are summed. A text without a
|
|
49
|
+
* duration yields nothing.
|
|
50
|
+
*/
|
|
51
|
+
export function parseResetDurationMs(
|
|
52
|
+
text: string | undefined | null,
|
|
53
|
+
): number | undefined {
|
|
54
|
+
if (typeof text !== "string") return undefined;
|
|
55
|
+
const trimmed = text.trim();
|
|
56
|
+
if (!trimmed) return undefined;
|
|
57
|
+
let total = 0;
|
|
58
|
+
let found = false;
|
|
59
|
+
for (const match of trimmed.matchAll(DURATION_PATTERN)) {
|
|
60
|
+
const amount = Number.parseFloat(match[1]);
|
|
61
|
+
const unit = match[2].toLowerCase();
|
|
62
|
+
const unitMs = UNIT_MS[unit];
|
|
63
|
+
if (!Number.isFinite(amount) || unitMs === undefined) continue;
|
|
64
|
+
total += amount * unitMs;
|
|
65
|
+
found = true;
|
|
66
|
+
}
|
|
67
|
+
return found ? Math.round(total) : undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The absolute epoch of a reset, read from the text of the console.
|
|
72
|
+
*
|
|
73
|
+
* The observation is the anchor: the text describes the state of the
|
|
74
|
+
* window at the moment of the capture.
|
|
75
|
+
*/
|
|
76
|
+
export function resetEpochFromDuration(
|
|
77
|
+
text: string | undefined | null,
|
|
78
|
+
observedAtMs: number | undefined | null,
|
|
79
|
+
): number | undefined {
|
|
80
|
+
if (typeof observedAtMs !== "number" || !Number.isFinite(observedAtMs)) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const duration = parseResetDurationMs(text);
|
|
84
|
+
if (duration === undefined) return undefined;
|
|
85
|
+
const epoch = observedAtMs + duration;
|
|
86
|
+
return Number.isFinite(epoch) ? epoch : undefined;
|
|
87
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The threshold of the low-allowance policy.
|
|
3
|
+
*
|
|
4
|
+
* The console of a provider shows the percentage that is left ("5h: 0%
|
|
5
|
+
* left"), the mirror of the harness stores the percentage that is used.
|
|
6
|
+
* The two are complements, and the policy is stated in the remaining
|
|
7
|
+
* percentage, so the conversion and the thresholds live here and are used
|
|
8
|
+
* by name.
|
|
9
|
+
*
|
|
10
|
+
* The module is pure: it decides, it does not observe. The capture of the
|
|
11
|
+
* quota, the freshness of the capture and the scheduling of the recovery
|
|
12
|
+
* belong to the modules around it.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** The remaining percentage below which the allowance of a window is low. */
|
|
16
|
+
export const LOW_REMAINING_THRESHOLD_PCT = 10;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A reset that is nearer than this is left to the bounded recovery cycle.
|
|
20
|
+
*
|
|
21
|
+
* The cycle of a failure waits 1, 4, 9 and 16 minutes. A reset that
|
|
22
|
+
* arrives within the hour is reached by that cycle, and spending the
|
|
23
|
+
* budget of the cycle on a reset that is about to happen would leave
|
|
24
|
+
* nothing for the failure that comes next.
|
|
25
|
+
*/
|
|
26
|
+
export const RESET_HORIZON_MS = 60 * 60_000;
|
|
27
|
+
|
|
28
|
+
/** The remaining percentage of a used percentage. */
|
|
29
|
+
export function remainingPctFromUsedPct(usedPct: number): number {
|
|
30
|
+
return 100 - usedPct;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The used percentage of a remaining percentage. */
|
|
34
|
+
export function usedPctFromRemainingPct(remainingPct: number): number {
|
|
35
|
+
return 100 - remainingPct;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The state of one quota window. */
|
|
39
|
+
export type QuotaWindowState =
|
|
40
|
+
/** Nothing is known about the window. */
|
|
41
|
+
| "unknown"
|
|
42
|
+
/** The window still has its allowance. */
|
|
43
|
+
| "available"
|
|
44
|
+
/** The window is below the threshold, but not empty. */
|
|
45
|
+
| "low"
|
|
46
|
+
/** The window is empty. */
|
|
47
|
+
| "exhausted";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The state of a window, read from its used percentage.
|
|
51
|
+
*
|
|
52
|
+
* An unknown percentage is unknown and nothing else: it is not an empty
|
|
53
|
+
* window and it is not a window with an allowance.
|
|
54
|
+
*/
|
|
55
|
+
export function quotaWindowState(
|
|
56
|
+
usedPct: number | undefined | null,
|
|
57
|
+
): QuotaWindowState {
|
|
58
|
+
if (typeof usedPct !== "number" || !Number.isFinite(usedPct)) {
|
|
59
|
+
return "unknown";
|
|
60
|
+
}
|
|
61
|
+
const remaining = remainingPctFromUsedPct(usedPct);
|
|
62
|
+
if (remaining <= 0) return "exhausted";
|
|
63
|
+
if (remaining < LOW_REMAINING_THRESHOLD_PCT) return "low";
|
|
64
|
+
return "available";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Is the window below the threshold (nearly empty or empty)? */
|
|
68
|
+
export function isBelowLowThreshold(
|
|
69
|
+
usedPct: number | undefined | null,
|
|
70
|
+
): boolean {
|
|
71
|
+
const state = quotaWindowState(usedPct);
|
|
72
|
+
return state === "low" || state === "exhausted";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Is the window completely empty? */
|
|
76
|
+
export function isFullyExhausted(
|
|
77
|
+
usedPct: number | undefined | null,
|
|
78
|
+
): boolean {
|
|
79
|
+
return quotaWindowState(usedPct) === "exhausted";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The decision of the policy for one window. */
|
|
83
|
+
export type QuotaResetDecision =
|
|
84
|
+
/** Wait for the reset of the window. */
|
|
85
|
+
| "reset"
|
|
86
|
+
/** Keep the bounded recovery cycle. */
|
|
87
|
+
| "bounded";
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Decide what one window means for a failed request.
|
|
91
|
+
*
|
|
92
|
+
* An empty window blocks the account until it resets, whatever the
|
|
93
|
+
* distance of the reset is: the bounded cycle cannot help, because every
|
|
94
|
+
* attempt of the cycle fails the same way.
|
|
95
|
+
*
|
|
96
|
+
* A window that is nearly empty blocks the account as well, but a reset
|
|
97
|
+
* that arrives within the hour is reached by the cycle, so the cycle
|
|
98
|
+
* keeps the failure.
|
|
99
|
+
*/
|
|
100
|
+
export function decideQuotaReset(input: {
|
|
101
|
+
usedPct: number | undefined | null;
|
|
102
|
+
resetAt: number | undefined | null;
|
|
103
|
+
now: number;
|
|
104
|
+
}): QuotaResetDecision {
|
|
105
|
+
const state = quotaWindowState(input.usedPct);
|
|
106
|
+
if (state !== "exhausted" && state !== "low") return "bounded";
|
|
107
|
+
const resetAt = input.resetAt;
|
|
108
|
+
if (
|
|
109
|
+
typeof resetAt !== "number" ||
|
|
110
|
+
!Number.isFinite(resetAt) ||
|
|
111
|
+
resetAt <= input.now
|
|
112
|
+
) {
|
|
113
|
+
return "bounded";
|
|
114
|
+
}
|
|
115
|
+
if (state === "exhausted") return "reset";
|
|
116
|
+
return resetAt - input.now > RESET_HORIZON_MS ? "reset" : "bounded";
|
|
117
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The scope of an automatic resume.
|
|
3
|
+
*
|
|
4
|
+
* An automatic resume continues the request that is running. It is not an
|
|
5
|
+
* instruction to find more work: a queued `resume` that arrives after the
|
|
6
|
+
* request is complete sends the agent to the backlog, and the backlog is
|
|
7
|
+
* not work of this request.
|
|
8
|
+
*
|
|
9
|
+
* The mechanical question - does authorized work remain? - belongs in code
|
|
10
|
+
* and not in the judge. The judge answers the other question: whether a
|
|
11
|
+
* concrete proposed scope may be taken on.
|
|
12
|
+
*
|
|
13
|
+
* The decision is deliberately conservative. An unknown state is not a
|
|
14
|
+
* completion: the uncertainty is preserved and the resume goes on, because
|
|
15
|
+
* stopping work that is unfinished is worse than asking again.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The state of the work of the request that is running. */
|
|
19
|
+
export type ResumeWorkState = "unfinished" | "complete" | "unknown";
|
|
20
|
+
|
|
21
|
+
/** What the guard does with an automatic resume. */
|
|
22
|
+
export type ResumeScopeAction = "continue" | "suppress";
|
|
23
|
+
|
|
24
|
+
export interface ResumeScopeInput {
|
|
25
|
+
/** The state of the work of the request that is running. */
|
|
26
|
+
work: ResumeWorkState;
|
|
27
|
+
/** The reason of the resume, for the report. */
|
|
28
|
+
reason: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ResumeScopeDecision {
|
|
32
|
+
/** The action of the guard. */
|
|
33
|
+
action: ResumeScopeAction;
|
|
34
|
+
/** The reason of the decision, for the log. */
|
|
35
|
+
reason: string;
|
|
36
|
+
/** The line the user sees when the resume is suppressed. */
|
|
37
|
+
report?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The rule that the agent keeps across a compaction.
|
|
42
|
+
*
|
|
43
|
+
* The rule is reinforcement, not enforcement: the guard in the code owns
|
|
44
|
+
* the decision, and this text tells the agent what the guard decided and
|
|
45
|
+
* why the backlog is not the next task.
|
|
46
|
+
*/
|
|
47
|
+
export const RESUME_SCOPE_RULE = [
|
|
48
|
+
"An automatic resume continues only the current authorized request.",
|
|
49
|
+
"If that request and its required validation are finished, remain stopped.",
|
|
50
|
+
"Pending issues in bd or another backlog are not pending work of this request.",
|
|
51
|
+
"Propose any new scope separately; do not create executable todos for it",
|
|
52
|
+
"until the human or the configured Jev proposal policy authorizes it.",
|
|
53
|
+
].join(" ");
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The decision of the guard.
|
|
57
|
+
*
|
|
58
|
+
* - Work of the request remains: the resume goes on, and it continues that
|
|
59
|
+
* work only.
|
|
60
|
+
* - The work of the request is complete: the resume is suppressed. The
|
|
61
|
+
* agent is not told to find something else, and no backlog issue becomes
|
|
62
|
+
* work of this request.
|
|
63
|
+
* - The state is unknown: the resume goes on. The uncertainty is preserved
|
|
64
|
+
* - the guard does not declare a completion it cannot prove, and it does
|
|
65
|
+
* not substitute unrelated work either.
|
|
66
|
+
*/
|
|
67
|
+
export function decideResumeScope(input: ResumeScopeInput): ResumeScopeDecision {
|
|
68
|
+
if (input.work === "complete") {
|
|
69
|
+
return {
|
|
70
|
+
action: "suppress",
|
|
71
|
+
reason: "the work of the request is complete",
|
|
72
|
+
report: `The work of the request is complete; the automatic resume (${input.reason}) is not sent.`,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (input.work === "unknown") {
|
|
76
|
+
return {
|
|
77
|
+
action: "continue",
|
|
78
|
+
reason: "the state of the work is unknown; the uncertainty is preserved",
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
action: "continue",
|
|
83
|
+
reason: "the work of the request is unfinished",
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The runtime of the scope expansion.
|
|
3
|
+
*
|
|
4
|
+
* The controller (`harness/scope-expansion.ts`) owns the state of a
|
|
5
|
+
* proposal. This module owns the moments around it:
|
|
6
|
+
*
|
|
7
|
+
* - the proposal is created and delivered, and the window of the decision
|
|
8
|
+
* starts with the delivery,
|
|
9
|
+
* - the pending question is registered, so the answer of the human has a
|
|
10
|
+
* canonical identity,
|
|
11
|
+
* - the deadline fires once and lets Jev decide the exact proposal,
|
|
12
|
+
* - the decision of the human is read again after the delivery and before
|
|
13
|
+
* the dispatch: a rejection that arrived late still prevents the work,
|
|
14
|
+
* - the authorized group finishes and the session stops again.
|
|
15
|
+
*
|
|
16
|
+
* Everything the runtime needs from the outside is injected, so the
|
|
17
|
+
* behaviour is deterministic in a test.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
SCOPE_EXPANSION_WINDOW_MS,
|
|
22
|
+
ScopeExpansionController,
|
|
23
|
+
createScopeProposal,
|
|
24
|
+
scopeProposalMessage,
|
|
25
|
+
type ScopeCandidate,
|
|
26
|
+
type ScopeDecision,
|
|
27
|
+
type ScopeProposal,
|
|
28
|
+
} from "./scope-expansion.ts";
|
|
29
|
+
|
|
30
|
+
/** What a caller wants to propose. */
|
|
31
|
+
export interface ScopeProposalRequest {
|
|
32
|
+
/** The identity of the request whose work is finished. */
|
|
33
|
+
requestId: string;
|
|
34
|
+
/** The workspace the work happens in. */
|
|
35
|
+
workspace: string;
|
|
36
|
+
/** The generation of the completion. */
|
|
37
|
+
generation: number;
|
|
38
|
+
/** The candidates of the backlog. */
|
|
39
|
+
candidates: ScopeCandidate[];
|
|
40
|
+
/** What would be done. */
|
|
41
|
+
scope: string;
|
|
42
|
+
/** How the work would be validated. */
|
|
43
|
+
validation: string;
|
|
44
|
+
/** The material risks or costs. */
|
|
45
|
+
risks?: string[];
|
|
46
|
+
/** The choices the human has. */
|
|
47
|
+
choices?: string[];
|
|
48
|
+
/** The ids the proposal names. Defaults to every candidate. */
|
|
49
|
+
proposedIds?: string[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The outcome of a human answer. */
|
|
53
|
+
export type HumanScopeOutcome = "approve" | "reject";
|
|
54
|
+
|
|
55
|
+
/** The handle of a timer of the runtime. */
|
|
56
|
+
export type ScopeTimerHandle = ReturnType<typeof setTimeout> | number;
|
|
57
|
+
|
|
58
|
+
export interface ScopeExpansionRuntimeDeps {
|
|
59
|
+
/** The clock of the runtime. */
|
|
60
|
+
now(): number;
|
|
61
|
+
/**
|
|
62
|
+
* Deliver a message to the channels of the human.
|
|
63
|
+
*
|
|
64
|
+
* The result says whether at least one route accepted the message.
|
|
65
|
+
*/
|
|
66
|
+
deliver(
|
|
67
|
+
text: string,
|
|
68
|
+
meta: { id: string; kind: "proposal" | "decision"; questionId?: string },
|
|
69
|
+
): Promise<{ delivered: boolean; failures: string[] }>;
|
|
70
|
+
/** Jev decides the proposal when the human did not answer. */
|
|
71
|
+
judge(proposal: ScopeProposal): Promise<ScopeDecision>;
|
|
72
|
+
/** Start the authorized work. */
|
|
73
|
+
dispatch(proposal: ScopeProposal, ids: string[]): void;
|
|
74
|
+
/**
|
|
75
|
+
* Register the pending question of a proposal.
|
|
76
|
+
*
|
|
77
|
+
* The returned id is the canonical question id: it correlates the
|
|
78
|
+
* buttons of the notification with the answer of the human.
|
|
79
|
+
*/
|
|
80
|
+
registerQuestion(proposal: ScopeProposal): string | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Cancel a question that nobody was told about.
|
|
83
|
+
*
|
|
84
|
+
* A question of a proposal that was not delivered must not stay open: an
|
|
85
|
+
* open question blocks the work of the session.
|
|
86
|
+
*/
|
|
87
|
+
cancelQuestion?(questionId: string): void;
|
|
88
|
+
/**
|
|
89
|
+
* The latest human decision of a proposal.
|
|
90
|
+
*
|
|
91
|
+
* It is read after the delivery of the decision and before the
|
|
92
|
+
* dispatch, so a rejection that arrived late still prevents the work.
|
|
93
|
+
*/
|
|
94
|
+
latestHumanDecision?(proposal: ScopeProposal): HumanScopeOutcome | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Are the authorized issues of the group finished?
|
|
97
|
+
*
|
|
98
|
+
* The check is mechanical: the work of the group is done when the
|
|
99
|
+
* issues of the group are closed.
|
|
100
|
+
*/
|
|
101
|
+
groupFinished?(proposal: ScopeProposal, ids: string[]): boolean | Promise<boolean>;
|
|
102
|
+
/** The timer of the deadline. */
|
|
103
|
+
setTimeout?(callback: () => void | Promise<void>, ms: number): ScopeTimerHandle;
|
|
104
|
+
/** The cancel of the timer. */
|
|
105
|
+
clearTimeout?(handle: ScopeTimerHandle): void;
|
|
106
|
+
/** The log of the runtime. */
|
|
107
|
+
log?(message: string): void;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class ScopeExpansionRuntime {
|
|
111
|
+
private readonly deps: ScopeExpansionRuntimeDeps;
|
|
112
|
+
private readonly controller: ScopeExpansionController;
|
|
113
|
+
private timers = new Map<string, ScopeTimerHandle>();
|
|
114
|
+
/** The proposal of every generation, for the group check. */
|
|
115
|
+
private dispatched = new Map<string, ScopeProposal>();
|
|
116
|
+
/** The proposal of every registered question. */
|
|
117
|
+
private questions = new Map<string, string>();
|
|
118
|
+
/** The question of every proposal. */
|
|
119
|
+
private questionIds = new Map<string, string>();
|
|
120
|
+
|
|
121
|
+
constructor(deps: ScopeExpansionRuntimeDeps) {
|
|
122
|
+
this.deps = deps;
|
|
123
|
+
this.controller = new ScopeExpansionController({
|
|
124
|
+
now: () => deps.now(),
|
|
125
|
+
deliver: (text, meta) =>
|
|
126
|
+
deps.deliver(text, {
|
|
127
|
+
...meta,
|
|
128
|
+
questionId: meta.questionId ?? this.questionIds.get(meta.id),
|
|
129
|
+
}),
|
|
130
|
+
judge: (proposal) => deps.judge(proposal),
|
|
131
|
+
dispatch: (proposal, ids) => {
|
|
132
|
+
this.dispatched.set(proposal.id, proposal);
|
|
133
|
+
deps.dispatch(proposal, ids);
|
|
134
|
+
},
|
|
135
|
+
log: (message) => deps.log?.(message),
|
|
136
|
+
latestHumanDecision: (proposal) =>
|
|
137
|
+
deps.latestHumanDecision?.(proposal),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The proposals that wait for an answer. */
|
|
142
|
+
get open(): ScopeProposal[] {
|
|
143
|
+
return this.controller.open;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The proposal with this identity. */
|
|
147
|
+
get(id: string): ScopeProposal | undefined {
|
|
148
|
+
return this.controller.get(id);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Propose a new scope.
|
|
153
|
+
*
|
|
154
|
+
* The proposal is delivered before the window of the decision starts: a
|
|
155
|
+
* proposal that reaches no route must not start a countdown that nobody
|
|
156
|
+
* can stop.
|
|
157
|
+
*/
|
|
158
|
+
async propose(request: ScopeProposalRequest): Promise<ScopeProposal | null> {
|
|
159
|
+
const candidates = request.candidates.slice();
|
|
160
|
+
if (candidates.length === 0) {
|
|
161
|
+
this.deps.log?.("No scope proposal: the backlog has no candidate.");
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
const proposedIds =
|
|
165
|
+
request.proposedIds?.filter((id) => candidates.some((c) => c.id === id)) ??
|
|
166
|
+
candidates.map((candidate) => candidate.id);
|
|
167
|
+
if (proposedIds.length === 0) {
|
|
168
|
+
this.deps.log?.("No scope proposal: no candidate is known.");
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const id = `scope-${request.generation}-${proposedIds[0]}`;
|
|
172
|
+
const proposal = createScopeProposal({
|
|
173
|
+
id,
|
|
174
|
+
sessionId: "",
|
|
175
|
+
requestId: request.requestId,
|
|
176
|
+
generation: request.generation,
|
|
177
|
+
workspace: request.workspace,
|
|
178
|
+
candidates,
|
|
179
|
+
proposedIds,
|
|
180
|
+
scope: request.scope,
|
|
181
|
+
validation: request.validation,
|
|
182
|
+
risks: request.risks ?? [],
|
|
183
|
+
now: this.deps.now(),
|
|
184
|
+
});
|
|
185
|
+
if (!this.controller.register(proposal)) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
// The question is registered before the delivery: the buttons of the
|
|
189
|
+
// notification carry the canonical question id, and the id exists
|
|
190
|
+
// only when the question does.
|
|
191
|
+
const questionId = this.deps.registerQuestion(proposal);
|
|
192
|
+
if (questionId) {
|
|
193
|
+
this.questions.set(questionId, proposal.id);
|
|
194
|
+
this.questionIds.set(proposal.id, questionId);
|
|
195
|
+
}
|
|
196
|
+
const delivered = await this.controller.deliverProposal(proposal);
|
|
197
|
+
if (!delivered) {
|
|
198
|
+
// A proposal that reached nobody leaves no open question behind:
|
|
199
|
+
// an open question blocks the work of the session.
|
|
200
|
+
if (questionId) {
|
|
201
|
+
this.deps.cancelQuestion?.(questionId);
|
|
202
|
+
this.questions.delete(questionId);
|
|
203
|
+
this.questionIds.delete(proposal.id);
|
|
204
|
+
}
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
this.scheduleEvaluation(proposal);
|
|
208
|
+
return proposal;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The answer of the human.
|
|
213
|
+
*
|
|
214
|
+
* The first answer wins, and the answer beats an evaluation that is
|
|
215
|
+
* still running.
|
|
216
|
+
*/
|
|
217
|
+
async answer(
|
|
218
|
+
proposalId: string,
|
|
219
|
+
outcome: HumanScopeOutcome,
|
|
220
|
+
reasoning: string,
|
|
221
|
+
): Promise<{ started: boolean; reason?: string }> {
|
|
222
|
+
const decision = this.controller.answer(proposalId, outcome, reasoning);
|
|
223
|
+
if (!decision) {
|
|
224
|
+
return { started: false, reason: "the proposal has no open answer" };
|
|
225
|
+
}
|
|
226
|
+
return this.controller.applyDecision(proposalId);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The answer of the human, by the identity of the question.
|
|
231
|
+
*
|
|
232
|
+
* The channels answer a question, not a proposal: the canonical
|
|
233
|
+
* question id is what the callback carries.
|
|
234
|
+
*/
|
|
235
|
+
async answerByQuestion(
|
|
236
|
+
questionId: string,
|
|
237
|
+
outcome: HumanScopeOutcome,
|
|
238
|
+
reasoning: string,
|
|
239
|
+
): Promise<{ started: boolean; reason?: string }> {
|
|
240
|
+
const proposalId = this.questions.get(questionId);
|
|
241
|
+
if (!proposalId) {
|
|
242
|
+
return { started: false, reason: "the question belongs to no proposal" };
|
|
243
|
+
}
|
|
244
|
+
return this.answer(proposalId, outcome, reasoning);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The proposal of a canonical question id. */
|
|
248
|
+
proposalForQuestion(questionId: string): ScopeProposal | undefined {
|
|
249
|
+
const proposalId = this.questions.get(questionId);
|
|
250
|
+
if (!proposalId) return undefined;
|
|
251
|
+
return this.controller.get(proposalId);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** The canonical question id of a proposal. */
|
|
255
|
+
questionIdFor(proposalId: string): string | undefined {
|
|
256
|
+
return this.questionIds.get(proposalId);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The evaluation of a proposal whose deadline has passed.
|
|
261
|
+
*
|
|
262
|
+
* The evaluation happens once, and the decision is delivered before the
|
|
263
|
+
* work starts.
|
|
264
|
+
*/
|
|
265
|
+
async evaluateDue(proposalId: string): Promise<ScopeDecision | null> {
|
|
266
|
+
const decision = await this.controller.evaluateDue(proposalId);
|
|
267
|
+
if (!decision) return null;
|
|
268
|
+
await this.controller.applyDecision(proposalId);
|
|
269
|
+
return decision;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* The work of the authorized group finished.
|
|
274
|
+
*
|
|
275
|
+
* The session stops again: the approval of that group does not authorize
|
|
276
|
+
* the next group of the backlog.
|
|
277
|
+
*/
|
|
278
|
+
async checkGroupFinished(): Promise<void> {
|
|
279
|
+
for (const proposal of this.dispatched.values()) {
|
|
280
|
+
const ids = proposal.decision?.selectedIds ?? [];
|
|
281
|
+
if (ids.length === 0) continue;
|
|
282
|
+
if (!(await this.deps.groupFinished?.(proposal, ids))) continue;
|
|
283
|
+
this.controller.finishGroup(proposal.id);
|
|
284
|
+
this.clearTimer(proposal.id);
|
|
285
|
+
this.dispatched.delete(proposal.id);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Stop the runtime: no timer survives the session. */
|
|
290
|
+
dispose(): void {
|
|
291
|
+
for (const handle of this.timers.values()) {
|
|
292
|
+
this.deps.clearTimeout?.(handle);
|
|
293
|
+
}
|
|
294
|
+
this.timers.clear();
|
|
295
|
+
this.dispatched.clear();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private scheduleEvaluation(proposal: ScopeProposal): void {
|
|
299
|
+
const delay = Math.max(0, proposal.deadlineAt - this.deps.now());
|
|
300
|
+
const setTimer = this.deps.setTimeout ?? ((cb: () => void, ms: number) => setTimeout(cb, ms));
|
|
301
|
+
const handle = setTimer(() => {
|
|
302
|
+
this.timers.delete(proposal.id);
|
|
303
|
+
return (async () => {
|
|
304
|
+
try {
|
|
305
|
+
await this.evaluateDue(proposal.id);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
this.deps.log?.(
|
|
308
|
+
`The evaluation of the scope proposal ${proposal.id} failed: ${String(error)}`,
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
})();
|
|
312
|
+
}, delay);
|
|
313
|
+
this.timers.set(proposal.id, handle);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private clearTimer(id: string): void {
|
|
317
|
+
const handle = this.timers.get(id);
|
|
318
|
+
if (handle === undefined) return;
|
|
319
|
+
this.deps.clearTimeout?.(handle);
|
|
320
|
+
this.timers.delete(id);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The message of a proposal, for the caller that delivers it itself.
|
|
325
|
+
*/
|
|
326
|
+
message(proposal: ScopeProposal): string {
|
|
327
|
+
return scopeProposalMessage(proposal);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* The window of a decision, in milliseconds.
|
|
333
|
+
*
|
|
334
|
+
* The runtime uses the same constant as the controller: one source of the
|
|
335
|
+
* five minutes.
|
|
336
|
+
*/
|
|
337
|
+
export const SCOPE_WINDOW_MS = SCOPE_EXPANSION_WINDOW_MS;
|