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,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool that records the exploration checkpoint of the agent.
|
|
3
|
+
*
|
|
4
|
+
* The guard watches the reads of the agent, but it does not know what the
|
|
5
|
+
* agent learned from them. Only the agent can say that: the findings, the
|
|
6
|
+
* questions that are still open, the next concrete action and the hash of
|
|
7
|
+
* the files that were read.
|
|
8
|
+
*
|
|
9
|
+
* The tool records that statement beside the coverage of the request, so a
|
|
10
|
+
* compaction does not make the agent find it again.
|
|
11
|
+
*
|
|
12
|
+
* The tool records evidence and authorises nothing. It never writes a file,
|
|
13
|
+
* never changes a branch and never starts another task.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { Type } from "typebox";
|
|
17
|
+
import {
|
|
18
|
+
defineTool,
|
|
19
|
+
type ExtensionAPI,
|
|
20
|
+
type ToolDefinition,
|
|
21
|
+
} from "@earendil-works/pi-coding-agent";
|
|
22
|
+
import type { CheckpointInput } from "../packages/exploration-guard/dist/index.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The recorder of the checkpoint.
|
|
26
|
+
*
|
|
27
|
+
* The runtime of the running request records the checkpoint. The recorder
|
|
28
|
+
* is read when the tool is called, because the request changes while the
|
|
29
|
+
* tool stays registered.
|
|
30
|
+
*/
|
|
31
|
+
export interface ExplorationCheckpointRecorder {
|
|
32
|
+
/**
|
|
33
|
+
* Record the checkpoint of the request.
|
|
34
|
+
*
|
|
35
|
+
* The result is true when the checkpoint was written.
|
|
36
|
+
*/
|
|
37
|
+
recordCheckpoint(input: CheckpointInput): boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The options of the tool.
|
|
42
|
+
*/
|
|
43
|
+
export interface ExplorationCheckpointToolOptions {
|
|
44
|
+
/**
|
|
45
|
+
* The recorder of the running request.
|
|
46
|
+
*
|
|
47
|
+
* There is no recorder when no request is running, and the tool says
|
|
48
|
+
* so instead of pretending that it recorded something.
|
|
49
|
+
*/
|
|
50
|
+
current: () => ExplorationCheckpointRecorder | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The name of the tool.
|
|
55
|
+
*/
|
|
56
|
+
export const EXPLORATION_CHECKPOINT_TOOL = "exploration_checkpoint";
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The parameters of the tool.
|
|
60
|
+
*/
|
|
61
|
+
const parameters = Type.Object({
|
|
62
|
+
findings: Type.Optional(
|
|
63
|
+
Type.Array(
|
|
64
|
+
Type.Object({
|
|
65
|
+
statement: Type.String({
|
|
66
|
+
description:
|
|
67
|
+
"What was found, in one sentence. A finding names a location when it has one.",
|
|
68
|
+
}),
|
|
69
|
+
locations: Type.Optional(
|
|
70
|
+
Type.Array(
|
|
71
|
+
Type.String({
|
|
72
|
+
description: "Where the finding was found.",
|
|
73
|
+
}),
|
|
74
|
+
),
|
|
75
|
+
),
|
|
76
|
+
}),
|
|
77
|
+
{ description: "The findings of the investigation so far." },
|
|
78
|
+
),
|
|
79
|
+
),
|
|
80
|
+
hypotheses: Type.Optional(
|
|
81
|
+
Type.Array(
|
|
82
|
+
Type.Object({
|
|
83
|
+
statement: Type.String({
|
|
84
|
+
description: "A hypothesis about the cause of the problem.",
|
|
85
|
+
}),
|
|
86
|
+
rejected: Type.Optional(
|
|
87
|
+
Type.Boolean({
|
|
88
|
+
description: "True when the hypothesis was ruled out.",
|
|
89
|
+
}),
|
|
90
|
+
),
|
|
91
|
+
}),
|
|
92
|
+
{ description: "The hypotheses that were narrowed or ruled out." },
|
|
93
|
+
),
|
|
94
|
+
),
|
|
95
|
+
openQuestions: Type.Optional(
|
|
96
|
+
Type.Array(
|
|
97
|
+
Type.String({ description: "A question that is still open." }),
|
|
98
|
+
{ description: "The questions that are still open." },
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
nextAction: Type.Optional(
|
|
102
|
+
Type.String({
|
|
103
|
+
description:
|
|
104
|
+
"The next concrete action, in one sentence. It names what to do next, not what to conclude.",
|
|
105
|
+
}),
|
|
106
|
+
),
|
|
107
|
+
references: Type.Optional(
|
|
108
|
+
Type.Array(
|
|
109
|
+
Type.Object({
|
|
110
|
+
path: Type.String({
|
|
111
|
+
description: "The file the findings refer to.",
|
|
112
|
+
}),
|
|
113
|
+
contentHash: Type.Optional(
|
|
114
|
+
Type.String({
|
|
115
|
+
description:
|
|
116
|
+
"The hash of the content that was observed. A file whose hash changes is read again.",
|
|
117
|
+
}),
|
|
118
|
+
),
|
|
119
|
+
}),
|
|
120
|
+
{ description: "The files the findings refer to." },
|
|
121
|
+
),
|
|
122
|
+
),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The tool that records the exploration checkpoint.
|
|
127
|
+
*
|
|
128
|
+
* The tool never throws: a checkpoint that cannot be written is reported
|
|
129
|
+
* as a failure of the record, and the agent continues.
|
|
130
|
+
*/
|
|
131
|
+
export function explorationCheckpointTool(
|
|
132
|
+
options: ExplorationCheckpointToolOptions,
|
|
133
|
+
): ToolDefinition<typeof parameters> {
|
|
134
|
+
return defineTool({
|
|
135
|
+
name: EXPLORATION_CHECKPOINT_TOOL,
|
|
136
|
+
label: "Exploration checkpoint",
|
|
137
|
+
description:
|
|
138
|
+
"Record the exploration checkpoint of the current request: the findings, the hypotheses that were ruled out, the questions that are still open, the next concrete action and the hash of the files that were read. " +
|
|
139
|
+
"Use it when the investigation has produced an answer that a compaction or a later turn would otherwise lose, and when the guard asks for a checkpoint. " +
|
|
140
|
+
"It records evidence: it does not authorise a change, it does not write anything and it does not start another task.",
|
|
141
|
+
parameters,
|
|
142
|
+
async execute(_toolCallId, params) {
|
|
143
|
+
const recorder = options.current();
|
|
144
|
+
if (!recorder) {
|
|
145
|
+
return {
|
|
146
|
+
content: [
|
|
147
|
+
{
|
|
148
|
+
type: "text",
|
|
149
|
+
text: "There is no running request, so there is no exploration to record.",
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
details: {},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const recorded = recorder.recordCheckpoint({
|
|
156
|
+
findings: params.findings,
|
|
157
|
+
hypotheses: params.hypotheses,
|
|
158
|
+
openQuestions: params.openQuestions,
|
|
159
|
+
nextAction: params.nextAction,
|
|
160
|
+
references: params.references,
|
|
161
|
+
});
|
|
162
|
+
if (!recorded) {
|
|
163
|
+
return {
|
|
164
|
+
content: [
|
|
165
|
+
{
|
|
166
|
+
type: "text",
|
|
167
|
+
text: "The checkpoint of the request could not be recorded.",
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
details: {},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
// The answer names what was recorded: the agent must be able to
|
|
174
|
+
// see that the record holds its findings, and a record of
|
|
175
|
+
// nothing is a record that says nothing.
|
|
176
|
+
const parts: string[] = [];
|
|
177
|
+
if (params.findings?.length) {
|
|
178
|
+
parts.push(`${params.findings.length} finding(s)`);
|
|
179
|
+
}
|
|
180
|
+
if (params.hypotheses?.length) {
|
|
181
|
+
parts.push(`${params.hypotheses.length} hypothesis(es)`);
|
|
182
|
+
}
|
|
183
|
+
if (params.openQuestions?.length) {
|
|
184
|
+
parts.push(`${params.openQuestions.length} open question(s)`);
|
|
185
|
+
}
|
|
186
|
+
if (params.references?.length) {
|
|
187
|
+
parts.push(`${params.references.length} file reference(s)`);
|
|
188
|
+
}
|
|
189
|
+
if (params.nextAction) {
|
|
190
|
+
parts.push("a next action");
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
content: [
|
|
194
|
+
{
|
|
195
|
+
type: "text",
|
|
196
|
+
text:
|
|
197
|
+
parts.length > 0
|
|
198
|
+
? `The checkpoint of the request was recorded: ${parts.join(", ")}.`
|
|
199
|
+
: "The checkpoint of the request was recorded, but it was empty.",
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
details: {},
|
|
203
|
+
};
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Register the tool of the exploration checkpoint.
|
|
210
|
+
*/
|
|
211
|
+
export function registerExplorationCheckpointTool(
|
|
212
|
+
pi: ExtensionAPI,
|
|
213
|
+
options: ExplorationCheckpointToolOptions,
|
|
214
|
+
): void {
|
|
215
|
+
pi.registerTool(explorationCheckpointTool(options));
|
|
216
|
+
}
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import type {
|
|
15
|
+
CheckpointInput,
|
|
15
16
|
ExplorationKey,
|
|
16
17
|
ExplorationPhase,
|
|
17
18
|
ExplorationState,
|
|
@@ -313,4 +314,26 @@ export class ExplorationGuardRuntime {
|
|
|
313
314
|
return "investigating | unknown";
|
|
314
315
|
}
|
|
315
316
|
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Record the checkpoint that the agent reports.
|
|
320
|
+
*
|
|
321
|
+
* The findings, the questions, the next action and the hashes of the
|
|
322
|
+
* files that were read are written to the state of the request, so a
|
|
323
|
+
* compaction does not make the agent find them again.
|
|
324
|
+
*
|
|
325
|
+
* A checkpoint of a finished request is ignored: the request is over
|
|
326
|
+
* and its record is closed.
|
|
327
|
+
*/
|
|
328
|
+
recordCheckpoint(input: CheckpointInput): boolean {
|
|
329
|
+
if (this.inactive) return false;
|
|
330
|
+
try {
|
|
331
|
+
this.tracker.recordCheckpoint(this.key(), input);
|
|
332
|
+
return true;
|
|
333
|
+
} catch {
|
|
334
|
+
// The checkpoint could not be written: the agent continues
|
|
335
|
+
// without it, and the record of the request stays as it was.
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
316
339
|
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The escalation of a refusal of the judge.
|
|
3
|
+
*
|
|
4
|
+
* The judge said "no". That is a recommendation, and the human is asked
|
|
5
|
+
* about it: the question of the judge is closed, so a linked question with
|
|
6
|
+
* a fresh id carries the buttons of the human.
|
|
7
|
+
*
|
|
8
|
+
* The rules of the escalation:
|
|
9
|
+
*
|
|
10
|
+
* - The question of the judge is not reused. Live buttons on a resolved
|
|
11
|
+
* request are worse than no buttons, so the escalation gets its own id.
|
|
12
|
+
* - The message names the workspace, the task, the exact action, the
|
|
13
|
+
* reason of the judge and what stays blocked. A human may only answer
|
|
14
|
+
* what it can see.
|
|
15
|
+
* - The buttons are unambiguous: the human answers "do you approve this
|
|
16
|
+
* action", not "do you agree with the judge".
|
|
17
|
+
* - The escalation happens once per rejected proposal. A poll or a turn
|
|
18
|
+
* does not ask again.
|
|
19
|
+
* - When no channel accepts the message, the work stays visibly blocked.
|
|
20
|
+
* The harness never claims that the human was notified.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { NotificationResult } from "../packages/notification/src/types.ts";
|
|
24
|
+
|
|
25
|
+
/** The context of a refusal of the judge. */
|
|
26
|
+
export interface JevRejectionContext {
|
|
27
|
+
/** The request id of the question that the judge closed. */
|
|
28
|
+
originalRequestId: string;
|
|
29
|
+
/** The session of the request. */
|
|
30
|
+
sessionId: string;
|
|
31
|
+
/** The generation of the request. */
|
|
32
|
+
generation: number;
|
|
33
|
+
/** The workspace of the work. */
|
|
34
|
+
workspace: string;
|
|
35
|
+
/** The current task. */
|
|
36
|
+
task?: string;
|
|
37
|
+
/** The exact proposed action. */
|
|
38
|
+
action: string;
|
|
39
|
+
/** The reason of the judge. */
|
|
40
|
+
reason: string;
|
|
41
|
+
/** What stays blocked while the question is open. */
|
|
42
|
+
blocked?: string[];
|
|
43
|
+
/** The work that may continue on its own. */
|
|
44
|
+
independentWork?: string;
|
|
45
|
+
/** The deadline of the answer, in words. */
|
|
46
|
+
deadlineText?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The authorization of the user for the deadline.
|
|
49
|
+
*
|
|
50
|
+
* It is named when the human is told what happens without an answer.
|
|
51
|
+
*/
|
|
52
|
+
delegatedNotice?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The state of an escalation. */
|
|
56
|
+
export interface EscalationState {
|
|
57
|
+
/** The id of the escalation question. */
|
|
58
|
+
questionId: string;
|
|
59
|
+
/** The context of the refusal. */
|
|
60
|
+
context: JevRejectionContext;
|
|
61
|
+
/** The channels that accepted the message. */
|
|
62
|
+
delivered: string[];
|
|
63
|
+
/** The channels that refused the message, with the reason. */
|
|
64
|
+
failures: { channel: string; error: string }[];
|
|
65
|
+
/** The state of the escalation. */
|
|
66
|
+
status: "open" | "answered" | "blocked";
|
|
67
|
+
/** The answer of the human, when there is one. */
|
|
68
|
+
answer?: "yes" | "no";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The result of an escalation. */
|
|
72
|
+
export interface EscalationResult {
|
|
73
|
+
/** The state of the escalation. */
|
|
74
|
+
state: EscalationState;
|
|
75
|
+
/** Did at least one channel accept the message? */
|
|
76
|
+
deliveredAtLeastOne: boolean;
|
|
77
|
+
/** The escalation that was already open, when there was one. */
|
|
78
|
+
reused: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The parts of the runtime that the harness provides. */
|
|
82
|
+
export interface JevRejectionEscalationDeps {
|
|
83
|
+
/**
|
|
84
|
+
* Register the escalation question.
|
|
85
|
+
*
|
|
86
|
+
* It returns the fresh id of the question: the buttons of the human
|
|
87
|
+
* are bound to it.
|
|
88
|
+
*/
|
|
89
|
+
registerQuestion(context: JevRejectionContext, text: string): string;
|
|
90
|
+
/**
|
|
91
|
+
* Deliver the question with the buttons.
|
|
92
|
+
*/
|
|
93
|
+
deliver(
|
|
94
|
+
questionId: string,
|
|
95
|
+
text: string,
|
|
96
|
+
): Promise<Pick<NotificationResult, "success" | "channel" | "error">[]>;
|
|
97
|
+
/**
|
|
98
|
+
* The human approved the action.
|
|
99
|
+
*
|
|
100
|
+
* It is called at most once.
|
|
101
|
+
*/
|
|
102
|
+
onApproved(state: EscalationState): void;
|
|
103
|
+
/**
|
|
104
|
+
* The human rejected the action.
|
|
105
|
+
*/
|
|
106
|
+
onRejected(state: EscalationState): void;
|
|
107
|
+
/** The log of the runtime. */
|
|
108
|
+
log?(message: string): void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The message of the escalation.
|
|
113
|
+
*
|
|
114
|
+
* It names everything the human needs to answer: the workspace, the task,
|
|
115
|
+
* the exact action, the reason of the judge and the consequence of the
|
|
116
|
+
* block.
|
|
117
|
+
*/
|
|
118
|
+
export function buildJevRejectionMessage(context: JevRejectionContext): string {
|
|
119
|
+
const lines: string[] = [];
|
|
120
|
+
lines.push(`Jev recommends No: ${context.reason}.`);
|
|
121
|
+
lines.push(
|
|
122
|
+
`Workspace: ${context.workspace}.${context.task ? ` Task: ${context.task}.` : ""}`,
|
|
123
|
+
);
|
|
124
|
+
lines.push(`Proposed action: ${context.action}.`);
|
|
125
|
+
const blocked = context.blocked?.length
|
|
126
|
+
? context.blocked.join(", ")
|
|
127
|
+
: "the proposed action";
|
|
128
|
+
lines.push(
|
|
129
|
+
`The ${blocked} stays blocked while you decide${
|
|
130
|
+
context.independentWork
|
|
131
|
+
? `; ${context.independentWork} may continue.`
|
|
132
|
+
: "."
|
|
133
|
+
}`,
|
|
134
|
+
);
|
|
135
|
+
if (context.deadlineText) {
|
|
136
|
+
lines.push(`Answer within ${context.deadlineText}.`);
|
|
137
|
+
}
|
|
138
|
+
if (context.delegatedNotice) {
|
|
139
|
+
lines.push(context.delegatedNotice);
|
|
140
|
+
}
|
|
141
|
+
lines.push("Do you approve this action? [Yes] [No]");
|
|
142
|
+
return lines.join("\n");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The runtime of the escalation.
|
|
147
|
+
*/
|
|
148
|
+
export class JevRejectionEscalationRuntime {
|
|
149
|
+
private readonly deps: JevRejectionEscalationDeps;
|
|
150
|
+
/** The escalations, by the request that the judge closed. */
|
|
151
|
+
private readonly byOriginal = new Map<string, EscalationState>();
|
|
152
|
+
/** The escalations, by the id of their question. */
|
|
153
|
+
private readonly byQuestion = new Map<string, EscalationState>();
|
|
154
|
+
|
|
155
|
+
constructor(deps: JevRejectionEscalationDeps) {
|
|
156
|
+
this.deps = deps;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The escalation of a question id. */
|
|
160
|
+
forQuestion(questionId: string): EscalationState | undefined {
|
|
161
|
+
return this.byQuestion.get(questionId);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The escalation of the request that the judge closed. */
|
|
165
|
+
forOriginal(originalRequestId: string): EscalationState | undefined {
|
|
166
|
+
return this.byOriginal.get(originalRequestId);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Ask the human about the refusal of the judge.
|
|
171
|
+
*
|
|
172
|
+
* The escalation happens once per refused proposal: a second call for
|
|
173
|
+
* the same request returns the open escalation without asking again.
|
|
174
|
+
*/
|
|
175
|
+
async escalate(context: JevRejectionContext): Promise<EscalationResult> {
|
|
176
|
+
const existing = this.byOriginal.get(context.originalRequestId);
|
|
177
|
+
if (existing && existing.status === "open") {
|
|
178
|
+
this.deps.log?.(
|
|
179
|
+
`The escalation of ${context.originalRequestId} is already open - it is not asked again.`,
|
|
180
|
+
);
|
|
181
|
+
return {
|
|
182
|
+
state: existing,
|
|
183
|
+
deliveredAtLeastOne: existing.delivered.length > 0,
|
|
184
|
+
reused: true,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const text = buildJevRejectionMessage(context);
|
|
188
|
+
const questionId = this.deps.registerQuestion(context, text);
|
|
189
|
+
const state: EscalationState = {
|
|
190
|
+
questionId,
|
|
191
|
+
context,
|
|
192
|
+
delivered: [],
|
|
193
|
+
failures: [],
|
|
194
|
+
status: "open",
|
|
195
|
+
};
|
|
196
|
+
this.byOriginal.set(context.originalRequestId, state);
|
|
197
|
+
this.byQuestion.set(questionId, state);
|
|
198
|
+
|
|
199
|
+
const results = await this.deps.deliver(questionId, text);
|
|
200
|
+
for (const result of results) {
|
|
201
|
+
if (result.success) {
|
|
202
|
+
state.delivered.push(result.channel);
|
|
203
|
+
} else {
|
|
204
|
+
state.failures.push({
|
|
205
|
+
channel: result.channel,
|
|
206
|
+
error: result.error ?? "the channel refused the message",
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const deliveredAtLeastOne = state.delivered.length > 0;
|
|
211
|
+
if (!deliveredAtLeastOne) {
|
|
212
|
+
// Nobody was asked: the work stays visibly blocked and the
|
|
213
|
+
// harness does not claim that the human was notified.
|
|
214
|
+
state.status = "blocked";
|
|
215
|
+
this.deps.log?.(
|
|
216
|
+
`The escalation of ${context.originalRequestId} was not delivered: ${state.failures
|
|
217
|
+
.map((failure) => failure.channel)
|
|
218
|
+
.join(", ")}. The work stays blocked.`,
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
return { state, deliveredAtLeastOne, reused: false };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The answer of the human.
|
|
226
|
+
*
|
|
227
|
+
* The first answer wins: a second click and a click of another channel
|
|
228
|
+
* do nothing.
|
|
229
|
+
*/
|
|
230
|
+
answer(questionId: string, answer: "yes" | "no"): boolean {
|
|
231
|
+
const state = this.byQuestion.get(questionId);
|
|
232
|
+
if (!state) return false;
|
|
233
|
+
if (state.status !== "open") {
|
|
234
|
+
this.deps.log?.(
|
|
235
|
+
`The escalation ${questionId} was answered already - the answer is stale.`,
|
|
236
|
+
);
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
state.status = "answered";
|
|
240
|
+
state.answer = answer;
|
|
241
|
+
if (answer === "yes") {
|
|
242
|
+
this.deps.onApproved(state);
|
|
243
|
+
} else {
|
|
244
|
+
this.deps.onRejected(state);
|
|
245
|
+
}
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -206,6 +206,13 @@ export interface BridgeReceiveOptions {
|
|
|
206
206
|
registration: AgentRegistration;
|
|
207
207
|
/** The clock of the receipt. */
|
|
208
208
|
now?: () => number;
|
|
209
|
+
/**
|
|
210
|
+
* The mailbox to read.
|
|
211
|
+
*
|
|
212
|
+
* The mailbox of the agent by default. A test passes its own mailbox
|
|
213
|
+
* and still exercises the real bridge.
|
|
214
|
+
*/
|
|
215
|
+
mailbox?: Mailbox;
|
|
209
216
|
}
|
|
210
217
|
|
|
211
218
|
/**
|
|
@@ -214,13 +221,17 @@ export interface BridgeReceiveOptions {
|
|
|
214
221
|
* The recipient validates the message against its own registration before
|
|
215
222
|
* it queues anything: a plan of a session that ended, of another generation
|
|
216
223
|
* or of another workspace never enters the queue.
|
|
224
|
+
*
|
|
225
|
+
* A plan that was claimed but not acknowledged is here again: a crash
|
|
226
|
+
* between the claim and the acknowledgement must not lose the plan.
|
|
217
227
|
*/
|
|
218
228
|
export function receivePlans(
|
|
219
229
|
options: BridgeReceiveOptions,
|
|
220
230
|
): PlanHandoffMessage[] {
|
|
221
|
-
const mailbox =
|
|
231
|
+
const mailbox =
|
|
232
|
+
options.mailbox ?? planHandoffMailbox(options.registration.agentId);
|
|
222
233
|
const received: PlanHandoffMessage[] = [];
|
|
223
|
-
for (const entry of mailbox.
|
|
234
|
+
for (const entry of mailbox.claimable()) {
|
|
224
235
|
const message = entry.message;
|
|
225
236
|
if (message.recipient.agentId !== options.registration.agentId) continue;
|
|
226
237
|
if (message.recipient.sessionId !== options.registration.sessionId) continue;
|
|
@@ -233,15 +244,25 @@ export function receivePlans(
|
|
|
233
244
|
/**
|
|
234
245
|
* Acknowledge the delivery of a plan.
|
|
235
246
|
*/
|
|
236
|
-
export function acknowledgePlan(
|
|
237
|
-
|
|
247
|
+
export function acknowledgePlan(
|
|
248
|
+
agentId: string,
|
|
249
|
+
messageId: string,
|
|
250
|
+
mailbox?: Mailbox,
|
|
251
|
+
): boolean {
|
|
252
|
+
const acknowledged = (mailbox ?? planHandoffMailbox(agentId)).acknowledge(
|
|
253
|
+
messageId,
|
|
254
|
+
);
|
|
238
255
|
return acknowledged?.state === "delivered";
|
|
239
256
|
}
|
|
240
257
|
|
|
241
258
|
/**
|
|
242
259
|
* Claim a plan for this instance.
|
|
243
260
|
*/
|
|
244
|
-
export function claimPlan(
|
|
245
|
-
|
|
261
|
+
export function claimPlan(
|
|
262
|
+
agentId: string,
|
|
263
|
+
messageId: string,
|
|
264
|
+
mailbox?: Mailbox,
|
|
265
|
+
): boolean {
|
|
266
|
+
const claimed = (mailbox ?? planHandoffMailbox(agentId)).claim(messageId);
|
|
246
267
|
return claimed?.state === "claimed";
|
|
247
268
|
}
|