pi-harness-runtime 1.1.514 → 1.1.516
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 +187 -0
- package/harness/approval-decision-store.ts +94 -0
- package/harness/delegated-approval-runtime.ts +375 -0
- package/harness/delegated-approval-types.ts +179 -0
- package/harness/delegation-store.ts +108 -0
- package/harness/exploration-checkpoint-tool.ts +216 -0
- package/harness/exploration-guard-runtime.ts +23 -0
- package/harness/jev-rejection-escalation.ts +248 -0
- package/harness/plan-handoff-bridge.ts +27 -6
- package/harness/plan-handoff-receiver.ts +258 -0
- package/harness/plan-handoff-registration.ts +28 -13
- package/index.ts +516 -23
- package/package.json +1 -4
- 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/dist/atomic-write.d.ts +16 -0
- package/packages/cookie-sanitizer/dist/atomic-write.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/atomic-write.js +28 -0
- package/packages/cookie-sanitizer/dist/atomic-write.js.map +1 -1
- package/packages/cookie-sanitizer/dist/index.d.ts +1 -1
- package/packages/cookie-sanitizer/dist/index.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/index.js +1 -1
- package/packages/cookie-sanitizer/dist/index.js.map +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/cookie-sanitizer/src/atomic-write.ts +29 -0
- package/packages/cookie-sanitizer/src/index.ts +1 -0
- 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/checkpoint.d.ts +12 -0
- package/packages/exploration-guard/dist/checkpoint.d.ts.map +1 -1
- package/packages/exploration-guard/dist/checkpoint.js +33 -1
- package/packages/exploration-guard/dist/checkpoint.js.map +1 -1
- package/packages/exploration-guard/dist/observation.d.ts +7 -0
- package/packages/exploration-guard/dist/observation.d.ts.map +1 -1
- package/packages/exploration-guard/dist/observation.js +90 -27
- package/packages/exploration-guard/dist/observation.js.map +1 -1
- package/packages/exploration-guard/dist/tracker.d.ts.map +1 -1
- package/packages/exploration-guard/dist/tracker.js +3 -1
- package/packages/exploration-guard/dist/tracker.js.map +1 -1
- package/packages/exploration-guard/dist/types.d.ts +1 -1
- package/packages/exploration-guard/dist/types.d.ts.map +1 -1
- package/packages/exploration-guard/package.json +1 -1
- package/packages/exploration-guard/src/checkpoint.ts +44 -1
- package/packages/exploration-guard/src/observation.ts +120 -26
- package/packages/exploration-guard/src/tracker.ts +3 -1
- package/packages/exploration-guard/src/types.ts +1 -1
- package/packages/file-copy-helper/package.json +1 -1
- package/packages/intent-analyzer/package.json +1 -1
- package/packages/jev-judge/dist/continuation-controller.d.ts +43 -0
- package/packages/jev-judge/dist/continuation-controller.d.ts.map +1 -1
- package/packages/jev-judge/dist/continuation-controller.js +54 -7
- package/packages/jev-judge/dist/continuation-controller.js.map +1 -1
- package/packages/jev-judge/package.json +1 -1
- package/packages/jev-judge/src/continuation-controller.ts +89 -7
- 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/telegram-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/telegram-adapter.js +1 -0
- 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 +7 -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 +6 -0
- package/packages/notification/dist/notification-policy.js.map +1 -1
- package/packages/notification/dist/types.d.ts +9 -1
- package/packages/notification/dist/types.d.ts.map +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/notification/src/adapters/telegram-adapter.ts +1 -0
- package/packages/notification/src/notification-center.ts +7 -0
- package/packages/notification/src/notification-policy.ts +6 -0
- package/packages/notification/src/types.ts +8 -0
- package/packages/paperclip-integration/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/plan-handoff/dist/mailbox.d.ts +7 -0
- package/packages/plan-handoff/dist/mailbox.d.ts.map +1 -1
- package/packages/plan-handoff/dist/mailbox.js +9 -0
- package/packages/plan-handoff/dist/mailbox.js.map +1 -1
- package/packages/plan-handoff/package.json +1 -1
- package/packages/plan-handoff/src/mailbox.ts +12 -0
- package/packages/privilege-broker/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/providers/package.json +1 -1
- package/packages/qdrant-skills/package.json +1 -1
- package/packages/quota-manager/package.json +1 -1
- package/packages/relay/package.json +1 -1
- package/packages/skill-mcp-client/package.json +1 -1
- package/packages/skills/package.json +1 -1
- package/packages/todo-bd-sync/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/tooling-preference/package.json +1 -1
- package/packages/trajectory/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/two-phase-review/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/webmcp-tester/package.json +1 -1
- package/packages/wiki/package.json +1 -1
- package/packages/write-review/package.json +1 -1
- package/prompts/slash-commands.md +0 -2
- package/packages/knowledge-retrieval/dist/index.d.ts +0 -8
- package/packages/knowledge-retrieval/dist/index.d.ts.map +0 -1
- package/packages/knowledge-retrieval/dist/index.js +0 -7
- package/packages/knowledge-retrieval/dist/index.js.map +0 -1
- package/packages/knowledge-retrieval/dist/mcp-client.d.ts +0 -61
- package/packages/knowledge-retrieval/dist/mcp-client.d.ts.map +0 -1
- package/packages/knowledge-retrieval/dist/mcp-client.js +0 -131
- package/packages/knowledge-retrieval/dist/mcp-client.js.map +0 -1
- package/packages/knowledge-retrieval/dist/types.d.ts +0 -105
- package/packages/knowledge-retrieval/dist/types.d.ts.map +0 -1
- package/packages/knowledge-retrieval/dist/types.js +0 -7
- package/packages/knowledge-retrieval/dist/types.js.map +0 -1
- package/packages/knowledge-retrieval/package.json +0 -22
- package/packages/knowledge-retrieval/src/index.ts +0 -18
- package/packages/knowledge-retrieval/src/mcp-client.ts +0 -211
- package/packages/knowledge-retrieval/src/types.ts +0 -112
- package/prompts/tencentdb-connect.md +0 -35
- package/prompts/tencentdb-setup.md +0 -101
- package/prompts/tencentdb.md +0 -88
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The runtime of the delegated approval.
|
|
3
|
+
*
|
|
4
|
+
* An operation that the human has to approve is open as a request. The
|
|
5
|
+
* request is delivered to the channels, and two things can end it:
|
|
6
|
+
*
|
|
7
|
+
* - the human answers. The first answer wins, and it is recorded with the
|
|
8
|
+
* source `"human"`.
|
|
9
|
+
* - the deadline passes. The deadline is not an answer: the operation
|
|
10
|
+
* continues only when the user delegated that outcome in advance, and
|
|
11
|
+
* only for the kinds of operation the delegation names. The decision is
|
|
12
|
+
* recorded with the source `"delegated-timeout"`, never as a click of
|
|
13
|
+
* the human.
|
|
14
|
+
*
|
|
15
|
+
* The claim of the decision is atomic and durable, so exactly one resume
|
|
16
|
+
* is queued: a duplicate click, a late inference and a restart cannot
|
|
17
|
+
* queue a second one.
|
|
18
|
+
*
|
|
19
|
+
* The runtime never forces a turn and never decides for the human. It
|
|
20
|
+
* records what happened and hands the decision to the caller.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { ApprovalDecisionStore } from "./approval-decision-store.ts";
|
|
24
|
+
import type { DelegationStore } from "./delegation-store.ts";
|
|
25
|
+
import type {
|
|
26
|
+
ApprovalDecision,
|
|
27
|
+
ApprovalOutcome,
|
|
28
|
+
ApprovalRequest,
|
|
29
|
+
DelegatedOperationKind,
|
|
30
|
+
Delegation,
|
|
31
|
+
} from "./delegated-approval-types.ts";
|
|
32
|
+
import { delegationCovers } from "./delegated-approval-types.ts";
|
|
33
|
+
|
|
34
|
+
/** What the caller wants to ask the human. */
|
|
35
|
+
export interface OpenApprovalInput {
|
|
36
|
+
/** The canonical id of the request. It is the id of the pending question. */
|
|
37
|
+
requestId: string;
|
|
38
|
+
/** The workspace of the operation. */
|
|
39
|
+
workspace: string;
|
|
40
|
+
/** The session of the request. */
|
|
41
|
+
sessionId: string;
|
|
42
|
+
/** The generation of the request. */
|
|
43
|
+
generation: number;
|
|
44
|
+
/** The kind of the operation. */
|
|
45
|
+
kind: DelegatedOperationKind;
|
|
46
|
+
/** The exact operation, in the words of the human. */
|
|
47
|
+
operation: string;
|
|
48
|
+
/** What stays blocked while the request is open. */
|
|
49
|
+
blocked?: string[];
|
|
50
|
+
/** Why the operation is not simply running. */
|
|
51
|
+
reason?: string;
|
|
52
|
+
/** The deadline of the answer. It is absolute. */
|
|
53
|
+
deadlineAt: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The result of opening a request. */
|
|
57
|
+
export interface OpenApprovalOutcome {
|
|
58
|
+
/** The request. It is expired when the delivery failed. */
|
|
59
|
+
request: ApprovalRequest;
|
|
60
|
+
/** Did at least one route accept the message? */
|
|
61
|
+
delivered: boolean;
|
|
62
|
+
/** The routes that refused the message. */
|
|
63
|
+
failures: string[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface DelegatedApprovalDeps {
|
|
67
|
+
/** The clock of the runtime. */
|
|
68
|
+
now(): number;
|
|
69
|
+
/** The delegations the user recorded in advance. */
|
|
70
|
+
delegations: DelegationStore;
|
|
71
|
+
/** The decisions that were made. */
|
|
72
|
+
decisions: ApprovalDecisionStore;
|
|
73
|
+
/**
|
|
74
|
+
* Deliver the request to the human.
|
|
75
|
+
*
|
|
76
|
+
* The result says whether at least one route accepted the message.
|
|
77
|
+
*/
|
|
78
|
+
deliver(
|
|
79
|
+
request: ApprovalRequest,
|
|
80
|
+
): Promise<{ delivered: boolean; failures: string[] }>;
|
|
81
|
+
/**
|
|
82
|
+
* The resume that continues the work.
|
|
83
|
+
*
|
|
84
|
+
* It is queued at most once per request: the claim of the decision is
|
|
85
|
+
* what guarantees it.
|
|
86
|
+
*/
|
|
87
|
+
resume(request: ApprovalRequest, decision: ApprovalDecision): void;
|
|
88
|
+
/**
|
|
89
|
+
* Is the work of the request still active?
|
|
90
|
+
*
|
|
91
|
+
* A request that was cancelled, replaced or completed must not be
|
|
92
|
+
* continued by a timeout.
|
|
93
|
+
*/
|
|
94
|
+
active?(request: ApprovalRequest): boolean;
|
|
95
|
+
/** The work is paused. */
|
|
96
|
+
paused?(): boolean;
|
|
97
|
+
/** The log of the runtime. */
|
|
98
|
+
log?(message: string): void;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The runtime of the delegated approval.
|
|
103
|
+
*/
|
|
104
|
+
export class DelegatedApprovalRuntime {
|
|
105
|
+
private readonly deps: DelegatedApprovalDeps;
|
|
106
|
+
/** The requests that are open. */
|
|
107
|
+
private readonly requests = new Map<string, ApprovalRequest>();
|
|
108
|
+
/** The timer of the earliest deadline. */
|
|
109
|
+
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
110
|
+
|
|
111
|
+
constructor(deps: DelegatedApprovalDeps) {
|
|
112
|
+
this.deps = deps;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The request of an id, when there is one. */
|
|
116
|
+
get(requestId: string): ApprovalRequest | undefined {
|
|
117
|
+
return this.requests.get(requestId);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Stop the timer.
|
|
122
|
+
*
|
|
123
|
+
* The session stops it when it ends: a deadline that fires after the
|
|
124
|
+
* session is gone must not continue anything.
|
|
125
|
+
*/
|
|
126
|
+
stop(): void {
|
|
127
|
+
if (this.timer) {
|
|
128
|
+
clearTimeout(this.timer);
|
|
129
|
+
this.timer = null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Arm the timer of the earliest deadline.
|
|
135
|
+
*
|
|
136
|
+
* One timer serves every request: the deadline that comes first is the
|
|
137
|
+
* one that is watched, and the timer is armed again for the next one
|
|
138
|
+
* when it ran.
|
|
139
|
+
*/
|
|
140
|
+
private armTimer(): void {
|
|
141
|
+
this.stop();
|
|
142
|
+
const now = this.deps.now();
|
|
143
|
+
const next = this.pending()
|
|
144
|
+
.filter((request) => request.deadlineAt > now)
|
|
145
|
+
.sort((a, b) => a.deadlineAt - b.deadlineAt)[0];
|
|
146
|
+
if (!next) return;
|
|
147
|
+
const delay = Math.max(0, next.deadlineAt - now);
|
|
148
|
+
const timer = setTimeout(() => {
|
|
149
|
+
this.timer = null;
|
|
150
|
+
this.expire(next.requestId);
|
|
151
|
+
this.armTimer();
|
|
152
|
+
}, delay);
|
|
153
|
+
// The timer must not hold the process of the harness open.
|
|
154
|
+
(timer as { unref?: () => void }).unref?.();
|
|
155
|
+
this.timer = timer;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The requests that are open. */
|
|
159
|
+
pending(): ApprovalRequest[] {
|
|
160
|
+
return [...this.requests.values()].filter(
|
|
161
|
+
(request) => request.state === "pending",
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Open a request and deliver it to the human.
|
|
167
|
+
*
|
|
168
|
+
* The window starts only when a route accepted the message: a question
|
|
169
|
+
* that nobody was told about must not consume the opportunity of the
|
|
170
|
+
* human to answer.
|
|
171
|
+
*
|
|
172
|
+
* The caller may pass the result of a delivery it made itself. The
|
|
173
|
+
* delivery of the question and the window are then one decision, and
|
|
174
|
+
* the message is not sent twice.
|
|
175
|
+
*/
|
|
176
|
+
async open(
|
|
177
|
+
input: OpenApprovalInput,
|
|
178
|
+
delivery?: { delivered: boolean; failures: string[] },
|
|
179
|
+
): Promise<OpenApprovalOutcome> {
|
|
180
|
+
const existing = this.requests.get(input.requestId);
|
|
181
|
+
if (existing && existing.state === "pending") {
|
|
182
|
+
// The same question is not asked twice: a repeated delivery
|
|
183
|
+
// would only wear the human down.
|
|
184
|
+
this.deps.log?.(
|
|
185
|
+
`The approval request ${input.requestId} is already open - it is not asked again.`,
|
|
186
|
+
);
|
|
187
|
+
return { request: existing, delivered: true, failures: [] };
|
|
188
|
+
}
|
|
189
|
+
const request: ApprovalRequest = {
|
|
190
|
+
requestId: input.requestId,
|
|
191
|
+
workspace: input.workspace,
|
|
192
|
+
sessionId: input.sessionId,
|
|
193
|
+
generation: input.generation,
|
|
194
|
+
kind: input.kind,
|
|
195
|
+
operation: input.operation,
|
|
196
|
+
blocked: input.blocked ?? [],
|
|
197
|
+
reason: input.reason,
|
|
198
|
+
deadlineAt: input.deadlineAt,
|
|
199
|
+
state: "pending",
|
|
200
|
+
};
|
|
201
|
+
this.requests.set(request.requestId, request);
|
|
202
|
+
const result = delivery ?? (await this.deps.deliver(request));
|
|
203
|
+
if (!result.delivered) {
|
|
204
|
+
// The operation stays blocked: nobody was asked, so nobody
|
|
205
|
+
// authorized anything.
|
|
206
|
+
request.state = "expired";
|
|
207
|
+
this.deps.log?.(
|
|
208
|
+
`The approval request ${request.requestId} was not delivered: ${result.failures.join(", ")}. The operation stays blocked.`,
|
|
209
|
+
);
|
|
210
|
+
return { request, delivered: false, failures: result.failures };
|
|
211
|
+
}
|
|
212
|
+
// The window starts with the delivery: the deadline is watched
|
|
213
|
+
// from here on.
|
|
214
|
+
this.armTimer();
|
|
215
|
+
return { request, delivered: true, failures: [] };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The answer of the human.
|
|
220
|
+
*
|
|
221
|
+
* The first answer wins: a second click, a click of another channel
|
|
222
|
+
* and a late answer of a stale button do nothing.
|
|
223
|
+
*/
|
|
224
|
+
answer(
|
|
225
|
+
requestId: string,
|
|
226
|
+
outcome: ApprovalOutcome,
|
|
227
|
+
): ApprovalDecision | null {
|
|
228
|
+
const request = this.requests.get(requestId);
|
|
229
|
+
if (!request) return null;
|
|
230
|
+
if (request.state !== "pending") {
|
|
231
|
+
this.deps.log?.(
|
|
232
|
+
`The approval request ${requestId} was answered already - the answer is stale.`,
|
|
233
|
+
);
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
const decision: ApprovalDecision = {
|
|
237
|
+
requestId,
|
|
238
|
+
outcome,
|
|
239
|
+
source: "human",
|
|
240
|
+
reason: "the human answered",
|
|
241
|
+
at: this.deps.now(),
|
|
242
|
+
};
|
|
243
|
+
// The claim is atomic: two answers that arrive at the same moment
|
|
244
|
+
// still produce one decision.
|
|
245
|
+
if (!this.deps.decisions.claim(decision)) {
|
|
246
|
+
this.deps.log?.(
|
|
247
|
+
`The approval request ${requestId} was answered already - the answer is stale.`,
|
|
248
|
+
);
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
request.state = "answered";
|
|
252
|
+
request.answer = { outcome, source: "human", at: decision.at };
|
|
253
|
+
// The deadline of an answered request is not watched any more.
|
|
254
|
+
this.armTimer();
|
|
255
|
+
return decision;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The deadline of a request passed.
|
|
260
|
+
*
|
|
261
|
+
* The state of the work is checked before the claim, the claim is
|
|
262
|
+
* atomic, and the resume is queued at most once.
|
|
263
|
+
*/
|
|
264
|
+
expire(requestId: string): ApprovalDecision | null {
|
|
265
|
+
const request = this.requests.get(requestId);
|
|
266
|
+
if (!request) return null;
|
|
267
|
+
// The human wins: an answer that arrived before the claim is kept,
|
|
268
|
+
// and the deadline never overrides it.
|
|
269
|
+
if (request.state === "answered") {
|
|
270
|
+
this.deps.log?.(
|
|
271
|
+
`The approval request ${requestId} was answered by the human - the deadline does not override it.`,
|
|
272
|
+
);
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
if (request.state !== "pending") return null;
|
|
276
|
+
// A request that was cancelled, replaced or completed must not be
|
|
277
|
+
// continued by a deadline.
|
|
278
|
+
if (this.deps.active && !this.deps.active(request)) {
|
|
279
|
+
this.deps.log?.(
|
|
280
|
+
`The approval request ${requestId} is not active - the deadline does not continue it.`,
|
|
281
|
+
);
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
if (this.deps.paused?.() === true) {
|
|
285
|
+
this.deps.log?.(
|
|
286
|
+
`The approval request ${requestId} is paused - the deadline does not continue it.`,
|
|
287
|
+
);
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
// The scope of the delegation decides: an operation that no
|
|
291
|
+
// delegation covers stays blocked.
|
|
292
|
+
const delegation = this.delegationForRequest(request);
|
|
293
|
+
const outcome: ApprovalOutcome =
|
|
294
|
+
delegation?.defaultOutcome === "continue" ? "yes" : "no";
|
|
295
|
+
const decision: ApprovalDecision = {
|
|
296
|
+
requestId,
|
|
297
|
+
outcome,
|
|
298
|
+
source: "delegated-timeout",
|
|
299
|
+
...(delegation ? { delegationId: delegation.id } : {}),
|
|
300
|
+
reason: delegation
|
|
301
|
+
? `the deadline passed under the delegation ${delegation.id}`
|
|
302
|
+
: "no delegation covers the operation",
|
|
303
|
+
at: this.deps.now(),
|
|
304
|
+
};
|
|
305
|
+
// The claim is atomic and durable: the decision is handed out once,
|
|
306
|
+
// and a restart does not hand it out again.
|
|
307
|
+
if (!this.deps.decisions.claim(decision)) {
|
|
308
|
+
this.deps.log?.(
|
|
309
|
+
`The decision of the approval request ${requestId} was claimed already.`,
|
|
310
|
+
);
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
request.state = "claimed";
|
|
314
|
+
request.answer = { outcome, source: "delegated-timeout", at: decision.at };
|
|
315
|
+
if (outcome === "yes") {
|
|
316
|
+
// Exactly one resume: the claim is what guarantees it.
|
|
317
|
+
this.queueResume(request, decision);
|
|
318
|
+
} else {
|
|
319
|
+
this.deps.log?.(
|
|
320
|
+
`The operation of the approval request ${requestId} stays blocked: ${decision.reason}.`,
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
return decision;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* The delegation that covers an operation of a workspace.
|
|
328
|
+
*
|
|
329
|
+
* The most recent delegation wins: a delegation that was recorded
|
|
330
|
+
* later replaces the one before it.
|
|
331
|
+
*/
|
|
332
|
+
delegationFor(input: {
|
|
333
|
+
kind: DelegatedOperationKind;
|
|
334
|
+
workspace: string;
|
|
335
|
+
}): Delegation | undefined {
|
|
336
|
+
const now = this.deps.now();
|
|
337
|
+
return this.deps.delegations
|
|
338
|
+
.list(input.workspace)
|
|
339
|
+
.filter((delegation) =>
|
|
340
|
+
delegationCovers(
|
|
341
|
+
delegation,
|
|
342
|
+
input.kind,
|
|
343
|
+
input.workspace,
|
|
344
|
+
now,
|
|
345
|
+
),
|
|
346
|
+
)
|
|
347
|
+
.sort((a, b) => b.recordedAt - a.recordedAt)[0];
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* The delegation that covers the operation of a request.
|
|
352
|
+
*/
|
|
353
|
+
private delegationForRequest(request: ApprovalRequest): Delegation | undefined {
|
|
354
|
+
return this.delegationFor({
|
|
355
|
+
kind: request.kind,
|
|
356
|
+
workspace: request.workspace,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Queue the resume of the work. It never throws. */
|
|
361
|
+
private queueResume(
|
|
362
|
+
request: ApprovalRequest,
|
|
363
|
+
decision: ApprovalDecision,
|
|
364
|
+
): void {
|
|
365
|
+
try {
|
|
366
|
+
this.deps.resume(request, decision);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
this.deps.log?.(
|
|
369
|
+
`The resume of the approval request ${request.requestId} failed: ${
|
|
370
|
+
error instanceof Error ? error.message : String(error)
|
|
371
|
+
}`,
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The approval of an operation that the human has to give.
|
|
3
|
+
*
|
|
4
|
+
* The harness asks the human before an operation it may not run on its own:
|
|
5
|
+
* a commit, a push, a deployment, an external write. Two things can end
|
|
6
|
+
* that question: the human answers, or the deadline passes.
|
|
7
|
+
*
|
|
8
|
+
* The deadline is not an answer. When it passes, the operation continues
|
|
9
|
+
* only when the user delegated that outcome in advance, and only for the
|
|
10
|
+
* kinds of operation the delegation names. A delegation is never extended
|
|
11
|
+
* silently to an external or a destructive action.
|
|
12
|
+
*
|
|
13
|
+
* The decision is recorded with the source it really has: a click of the
|
|
14
|
+
* human is `"human"`, a decision of the judge is `"jev"`, and the outcome
|
|
15
|
+
* of a deadline that ran under a delegation is `"delegated-timeout"`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The kinds of operation a delegation can cover.
|
|
20
|
+
*
|
|
21
|
+
* The vocabulary is small and closed. A delegation names the kinds it
|
|
22
|
+
* covers, and a delegation for one kind never covers another: a
|
|
23
|
+
* delegation for a commit does not cover a push.
|
|
24
|
+
*/
|
|
25
|
+
export const DELEGATED_OPERATION_KINDS = [
|
|
26
|
+
"commit",
|
|
27
|
+
"push",
|
|
28
|
+
"commit-and-push",
|
|
29
|
+
"deploy",
|
|
30
|
+
"external-write",
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
/** A kind of operation a delegation can cover. */
|
|
34
|
+
export type DelegatedOperationKind = (typeof DELEGATED_OPERATION_KINDS)[number];
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Is this a kind of operation a delegation can cover?
|
|
38
|
+
*
|
|
39
|
+
* A kind that is not in the vocabulary authorizes nothing: an unknown
|
|
40
|
+
* operation is never covered by a delegation.
|
|
41
|
+
*/
|
|
42
|
+
export function isDelegatedOperationKind(
|
|
43
|
+
value: unknown,
|
|
44
|
+
): value is DelegatedOperationKind {
|
|
45
|
+
return (
|
|
46
|
+
typeof value === "string" &&
|
|
47
|
+
(DELEGATED_OPERATION_KINDS as readonly string[]).includes(value)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* An advance delegation of the timeout decision.
|
|
53
|
+
*
|
|
54
|
+
* The user records it before the question: "if I do not answer within five
|
|
55
|
+
* minutes, continue with a commit". The delegation is durable, and it
|
|
56
|
+
* belongs to a workspace: a delegation of one workspace never covers the
|
|
57
|
+
* operation of another one.
|
|
58
|
+
*/
|
|
59
|
+
export interface Delegation {
|
|
60
|
+
/** The id of the delegation. */
|
|
61
|
+
id: string;
|
|
62
|
+
/** The workspace the delegation applies to. */
|
|
63
|
+
workspace: string;
|
|
64
|
+
/** The kinds of operation the delegation covers. */
|
|
65
|
+
kinds: DelegatedOperationKind[];
|
|
66
|
+
/** What happens at the deadline. */
|
|
67
|
+
defaultOutcome: "continue" | "block";
|
|
68
|
+
/** When the delegation was recorded. */
|
|
69
|
+
recordedAt: number;
|
|
70
|
+
/** When the delegation stops applying. */
|
|
71
|
+
expiresAt?: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Where a decision came from.
|
|
76
|
+
*
|
|
77
|
+
* The source is part of the decision: a timeout that ran under a
|
|
78
|
+
* delegation is never recorded as a click of the human.
|
|
79
|
+
*/
|
|
80
|
+
export type ApprovalSource = "human" | "jev" | "delegated-timeout";
|
|
81
|
+
|
|
82
|
+
/** The answer of a human to a request. */
|
|
83
|
+
export type ApprovalOutcome = "yes" | "no";
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A request for the approval of an operation.
|
|
87
|
+
*/
|
|
88
|
+
export interface ApprovalRequest {
|
|
89
|
+
/** The canonical id of the request. It is the id of the pending question. */
|
|
90
|
+
requestId: string;
|
|
91
|
+
/** The workspace of the operation. */
|
|
92
|
+
workspace: string;
|
|
93
|
+
/** The session of the request. */
|
|
94
|
+
sessionId: string;
|
|
95
|
+
/** The generation of the request. */
|
|
96
|
+
generation: number;
|
|
97
|
+
/** The kind of the operation. */
|
|
98
|
+
kind: DelegatedOperationKind;
|
|
99
|
+
/** The exact operation, in the words of the human. */
|
|
100
|
+
operation: string;
|
|
101
|
+
/** What stays blocked while the request is open. */
|
|
102
|
+
blocked: string[];
|
|
103
|
+
/** Why the operation is not simply running. */
|
|
104
|
+
reason?: string;
|
|
105
|
+
/** The deadline of the answer. It is absolute. */
|
|
106
|
+
deadlineAt: number;
|
|
107
|
+
/** The state of the request. */
|
|
108
|
+
state: "pending" | "answered" | "claimed" | "expired";
|
|
109
|
+
/** The answer, when there is one. */
|
|
110
|
+
answer?: { outcome: ApprovalOutcome; source: ApprovalSource; at: number };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The recorded decision of a request.
|
|
115
|
+
*
|
|
116
|
+
* The record is what makes the decision auditable: it names the source,
|
|
117
|
+
* the delegation it rests on and the moment it was made.
|
|
118
|
+
*/
|
|
119
|
+
export interface ApprovalDecision {
|
|
120
|
+
/** The request the decision belongs to. */
|
|
121
|
+
requestId: string;
|
|
122
|
+
/** The outcome of the decision. */
|
|
123
|
+
outcome: ApprovalOutcome;
|
|
124
|
+
/** Where the decision came from. */
|
|
125
|
+
source: ApprovalSource;
|
|
126
|
+
/** The delegation the decision rests on. */
|
|
127
|
+
delegationId?: string;
|
|
128
|
+
/** The reason of the decision. */
|
|
129
|
+
reason?: string;
|
|
130
|
+
/** When the decision was made. */
|
|
131
|
+
at: number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The kind of operation of a proposal.
|
|
136
|
+
*
|
|
137
|
+
* The classification is narrow on purpose. A proposal whose kind is not
|
|
138
|
+
* known is not covered by a delegation, so an unknown operation never
|
|
139
|
+
* continues by silence.
|
|
140
|
+
*/
|
|
141
|
+
export function operationKindFor(
|
|
142
|
+
text: string | undefined,
|
|
143
|
+
): DelegatedOperationKind | undefined {
|
|
144
|
+
const value = (text ?? "").toLowerCase();
|
|
145
|
+
if (value.length === 0) return undefined;
|
|
146
|
+
const commit = /\bcommits?\b/.test(value);
|
|
147
|
+
const push = /\bpush(?:ed|es|ing)?\b/.test(value);
|
|
148
|
+
const deploy = /\bdeploy(?:ment|ed|ing)?\b|\bship(?:ped|s|ping)? to production\b/.test(value);
|
|
149
|
+
const external = /\bpublish(?:ed|es|ing)?\b|\bupload(?:ed|s|ing)?\b|\bpost(?:ed|s|ing)? to\b/.test(
|
|
150
|
+
value,
|
|
151
|
+
);
|
|
152
|
+
if (deploy) return "deploy";
|
|
153
|
+
if (external) return "external-write";
|
|
154
|
+
if (commit && push) return "commit-and-push";
|
|
155
|
+
if (commit) return "commit";
|
|
156
|
+
if (push) return "push";
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Does a delegation cover an operation?
|
|
162
|
+
*
|
|
163
|
+
* The check is exact: the workspace must be the same, the delegation must
|
|
164
|
+
* not have expired, and the kind of the operation must be one the
|
|
165
|
+
* delegation names. A delegation for a commit does not cover a push, and
|
|
166
|
+
* a delegation of one workspace does not cover the operation of another.
|
|
167
|
+
*/
|
|
168
|
+
export function delegationCovers(
|
|
169
|
+
delegation: Delegation,
|
|
170
|
+
kind: DelegatedOperationKind,
|
|
171
|
+
workspace: string,
|
|
172
|
+
now: number,
|
|
173
|
+
): boolean {
|
|
174
|
+
if (delegation.workspace !== workspace) return false;
|
|
175
|
+
if (delegation.expiresAt !== undefined && delegation.expiresAt <= now) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return delegation.kinds.includes(kind);
|
|
179
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The delegations the user recorded in advance.
|
|
3
|
+
*
|
|
4
|
+
* A delegation says: "if I do not answer within the window, continue with
|
|
5
|
+
* a commit". It is durable, and it belongs to a workspace.
|
|
6
|
+
*
|
|
7
|
+
* The delegations live in one directory per workspace, and every
|
|
8
|
+
* delegation is one file. Two Pi instances therefore never overwrite each
|
|
9
|
+
* other, and a delegation that is recorded twice replaces only itself.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { atomicWriteFileSync } from "../packages/cookie-sanitizer/dist/index.js";
|
|
15
|
+
import type { DelegatedOperationKind, Delegation } from "./delegated-approval-types.ts";
|
|
16
|
+
import { isDelegatedOperationKind } from "./delegated-approval-types.ts";
|
|
17
|
+
|
|
18
|
+
/** Make a part of a path safe for the file system. */
|
|
19
|
+
function safePart(value: string): string {
|
|
20
|
+
return value.replace(/[^a-zA-Z0-9_.-]/g, "_").slice(-120);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The delegations of the machine.
|
|
25
|
+
*/
|
|
26
|
+
export class DelegationStore {
|
|
27
|
+
/** The root of the store. */
|
|
28
|
+
private readonly root: string;
|
|
29
|
+
|
|
30
|
+
constructor(root: string) {
|
|
31
|
+
this.root = root;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The directory of the delegations of a workspace. */
|
|
35
|
+
private workspaceDir(workspace: string): string {
|
|
36
|
+
return join(this.root, "delegations", safePart(workspace));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Record a delegation.
|
|
41
|
+
*
|
|
42
|
+
* The write is atomic: the file is renamed over its target, so a
|
|
43
|
+
* reader never sees a delegation without its kinds.
|
|
44
|
+
*/
|
|
45
|
+
record(delegation: Delegation): void {
|
|
46
|
+
const dir = this.workspaceDir(delegation.workspace);
|
|
47
|
+
mkdirSync(dir, { recursive: true });
|
|
48
|
+
atomicWriteFileSync(
|
|
49
|
+
join(dir, `${safePart(delegation.id)}.json`),
|
|
50
|
+
JSON.stringify(delegation, null, 2),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The delegations of a workspace.
|
|
56
|
+
*
|
|
57
|
+
* A delegation that cannot be read is skipped: a broken record never
|
|
58
|
+
* authorizes anything.
|
|
59
|
+
*/
|
|
60
|
+
list(workspace: string): Delegation[] {
|
|
61
|
+
const dir = this.workspaceDir(workspace);
|
|
62
|
+
if (!existsSync(dir)) return [];
|
|
63
|
+
const found: Delegation[] = [];
|
|
64
|
+
for (const name of readdirSync(dir)) {
|
|
65
|
+
if (!name.endsWith(".json")) continue;
|
|
66
|
+
const delegation = readJson<Delegation>(join(dir, name));
|
|
67
|
+
if (!delegation) continue;
|
|
68
|
+
if (delegation.workspace !== workspace) continue;
|
|
69
|
+
if (!Array.isArray(delegation.kinds)) continue;
|
|
70
|
+
if (!delegation.kinds.every(isDelegatedOperationKind)) continue;
|
|
71
|
+
found.push({
|
|
72
|
+
...delegation,
|
|
73
|
+
kinds: delegation.kinds as DelegatedOperationKind[],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return found;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Remove a delegation.
|
|
81
|
+
*
|
|
82
|
+
* The result is true when a delegation was removed.
|
|
83
|
+
*/
|
|
84
|
+
remove(id: string): boolean {
|
|
85
|
+
const dir = join(this.root, "delegations");
|
|
86
|
+
if (!existsSync(dir)) return false;
|
|
87
|
+
for (const workspace of readdirSync(dir)) {
|
|
88
|
+
const path = join(dir, workspace, `${safePart(id)}.json`);
|
|
89
|
+
if (!existsSync(path)) continue;
|
|
90
|
+
try {
|
|
91
|
+
unlinkSync(path);
|
|
92
|
+
return true;
|
|
93
|
+
} catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Read a JSON file, or nothing when it cannot be read. */
|
|
102
|
+
function readJson<T>(path: string): T | undefined {
|
|
103
|
+
try {
|
|
104
|
+
return JSON.parse(readFileSync(path, "utf8")) as T;
|
|
105
|
+
} catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
}
|