pi-harness-runtime 1.1.513 → 1.1.514
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/AGENTS.md +23 -0
- package/CHANGELOG.md +92 -0
- package/harness/exploration-guard-runtime.ts +316 -0
- package/harness/plan-handoff-bridge.ts +247 -0
- package/harness/plan-handoff-delivery.ts +193 -0
- package/harness/plan-handoff-recipient-selection.ts +205 -0
- package/harness/plan-handoff-registration.ts +169 -0
- package/index.ts +451 -18
- package/package.json +7 -1
- package/packages/agent-policy/package.json +1 -1
- package/packages/auth/package.json +1 -1
- package/packages/autonomous-runtime/package.json +1 -1
- package/packages/checkpoint/package.json +1 -1
- package/packages/clipboard/package.json +1 -1
- package/packages/codex-watcher/package.json +1 -1
- package/packages/config-capture/package.json +1 -1
- package/packages/contracts/package.json +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/evaluation-engine/package.json +1 -1
- package/packages/event-bus/package.json +1 -1
- package/packages/event-store/package.json +1 -1
- package/packages/experience-replay/package.json +1 -1
- package/packages/exploration-guard/dist/budget.d.ts +105 -0
- package/packages/exploration-guard/dist/budget.d.ts.map +1 -0
- package/packages/exploration-guard/dist/budget.js +199 -0
- package/packages/exploration-guard/dist/budget.js.map +1 -0
- package/packages/exploration-guard/dist/checkpoint.d.ts +140 -0
- package/packages/exploration-guard/dist/checkpoint.d.ts.map +1 -0
- package/packages/exploration-guard/dist/checkpoint.js +201 -0
- package/packages/exploration-guard/dist/checkpoint.js.map +1 -0
- package/packages/exploration-guard/dist/index.d.ts +19 -0
- package/packages/exploration-guard/dist/index.d.ts.map +1 -0
- package/packages/exploration-guard/dist/index.js +18 -0
- package/packages/exploration-guard/dist/index.js.map +1 -0
- package/packages/exploration-guard/dist/observation.d.ts +82 -0
- package/packages/exploration-guard/dist/observation.d.ts.map +1 -0
- package/packages/exploration-guard/dist/observation.js +239 -0
- package/packages/exploration-guard/dist/observation.js.map +1 -0
- package/packages/exploration-guard/dist/state.d.ts +107 -0
- package/packages/exploration-guard/dist/state.d.ts.map +1 -0
- package/packages/exploration-guard/dist/state.js +300 -0
- package/packages/exploration-guard/dist/state.js.map +1 -0
- package/packages/exploration-guard/dist/status.d.ts +56 -0
- package/packages/exploration-guard/dist/status.d.ts.map +1 -0
- package/packages/exploration-guard/dist/status.js +79 -0
- package/packages/exploration-guard/dist/status.js.map +1 -0
- package/packages/exploration-guard/dist/tracker.d.ts +115 -0
- package/packages/exploration-guard/dist/tracker.d.ts.map +1 -0
- package/packages/exploration-guard/dist/tracker.js +207 -0
- package/packages/exploration-guard/dist/tracker.js.map +1 -0
- package/packages/exploration-guard/dist/types.d.ts +254 -0
- package/packages/exploration-guard/dist/types.d.ts.map +1 -0
- package/packages/exploration-guard/dist/types.js +14 -0
- package/packages/exploration-guard/dist/types.js.map +1 -0
- package/packages/exploration-guard/package.json +16 -0
- package/packages/exploration-guard/src/budget.ts +275 -0
- package/packages/exploration-guard/src/checkpoint.ts +297 -0
- package/packages/exploration-guard/src/index.ts +97 -0
- package/packages/exploration-guard/src/observation.ts +342 -0
- package/packages/exploration-guard/src/state.ts +357 -0
- package/packages/exploration-guard/src/status.ts +120 -0
- package/packages/exploration-guard/src/tracker.ts +291 -0
- package/packages/exploration-guard/src/types.ts +276 -0
- 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 +27 -2
- package/packages/jev-judge/dist/continuation-controller.d.ts.map +1 -1
- package/packages/jev-judge/dist/continuation-controller.js +87 -11
- package/packages/jev-judge/dist/continuation-controller.js.map +1 -1
- package/packages/jev-judge/dist/index.d.ts +2 -0
- package/packages/jev-judge/dist/index.d.ts.map +1 -1
- package/packages/jev-judge/dist/index.js +2 -0
- package/packages/jev-judge/dist/index.js.map +1 -1
- package/packages/jev-judge/dist/todo-provider.d.ts.map +1 -1
- package/packages/jev-judge/dist/todo-provider.js +93 -18
- package/packages/jev-judge/dist/todo-provider.js.map +1 -1
- package/packages/jev-judge/dist/todo-reconciliation.d.ts +50 -0
- package/packages/jev-judge/dist/todo-reconciliation.d.ts.map +1 -0
- package/packages/jev-judge/dist/todo-reconciliation.js +96 -0
- package/packages/jev-judge/dist/todo-reconciliation.js.map +1 -0
- package/packages/jev-judge/package.json +1 -1
- package/packages/jev-judge/src/continuation-controller.ts +90 -12
- package/packages/jev-judge/src/index.ts +8 -0
- package/packages/jev-judge/src/todo-provider.ts +117 -25
- package/packages/jev-judge/src/todo-reconciliation.ts +121 -0
- package/packages/knowledge-retrieval/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/logging/package.json +1 -1
- package/packages/memory-engine/package.json +1 -1
- package/packages/notification/dist/adapters/email-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/email-adapter.js +1 -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 +1 -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 +1 -0
- package/packages/notification/dist/adapters/ntfy-adapter.js.map +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 +3 -0
- package/packages/notification/dist/notification-policy.js.map +1 -1
- package/packages/notification/dist/telegram-webhook-handler.d.ts +8 -0
- package/packages/notification/dist/telegram-webhook-handler.d.ts.map +1 -1
- package/packages/notification/dist/telegram-webhook-handler.js +8 -0
- package/packages/notification/dist/telegram-webhook-handler.js.map +1 -1
- package/packages/notification/dist/types.d.ts +8 -1
- package/packages/notification/dist/types.d.ts.map +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/notification/src/adapters/email-adapter.ts +1 -0
- package/packages/notification/src/adapters/line-adapter.ts +1 -0
- package/packages/notification/src/adapters/ntfy-adapter.ts +1 -0
- 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 +3 -0
- package/packages/notification/src/telegram-webhook-handler.ts +8 -0
- package/packages/notification/src/types.ts +7 -0
- package/packages/paperclip-integration/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/plan-handoff/dist/index.d.ts +31 -0
- package/packages/plan-handoff/dist/index.d.ts.map +1 -0
- package/packages/plan-handoff/dist/index.js +23 -0
- package/packages/plan-handoff/dist/index.js.map +1 -0
- package/packages/plan-handoff/dist/mailbox.d.ts +134 -0
- package/packages/plan-handoff/dist/mailbox.d.ts.map +1 -0
- package/packages/plan-handoff/dist/mailbox.js +243 -0
- package/packages/plan-handoff/dist/mailbox.js.map +1 -0
- package/packages/plan-handoff/dist/message.d.ts +152 -0
- package/packages/plan-handoff/dist/message.d.ts.map +1 -0
- package/packages/plan-handoff/dist/message.js +157 -0
- package/packages/plan-handoff/dist/message.js.map +1 -0
- package/packages/plan-handoff/dist/pairing.d.ts +51 -0
- package/packages/plan-handoff/dist/pairing.d.ts.map +1 -0
- package/packages/plan-handoff/dist/pairing.js +88 -0
- package/packages/plan-handoff/dist/pairing.js.map +1 -0
- package/packages/plan-handoff/dist/publish.d.ts +96 -0
- package/packages/plan-handoff/dist/publish.d.ts.map +1 -0
- package/packages/plan-handoff/dist/publish.js +97 -0
- package/packages/plan-handoff/dist/publish.js.map +1 -0
- package/packages/plan-handoff/dist/registry.d.ts +95 -0
- package/packages/plan-handoff/dist/registry.d.ts.map +1 -0
- package/packages/plan-handoff/dist/registry.js +122 -0
- package/packages/plan-handoff/dist/registry.js.map +1 -0
- package/packages/plan-handoff/dist/retention.d.ts +101 -0
- package/packages/plan-handoff/dist/retention.d.ts.map +1 -0
- package/packages/plan-handoff/dist/retention.js +178 -0
- package/packages/plan-handoff/dist/retention.js.map +1 -0
- package/packages/plan-handoff/dist/routing.d.ts +69 -0
- package/packages/plan-handoff/dist/routing.d.ts.map +1 -0
- package/packages/plan-handoff/dist/routing.js +75 -0
- package/packages/plan-handoff/dist/routing.js.map +1 -0
- package/packages/plan-handoff/dist/workspace.d.ts +59 -0
- package/packages/plan-handoff/dist/workspace.d.ts.map +1 -0
- package/packages/plan-handoff/dist/workspace.js +78 -0
- package/packages/plan-handoff/dist/workspace.js.map +1 -0
- package/packages/plan-handoff/package.json +16 -0
- package/packages/plan-handoff/src/index.ts +75 -0
- package/packages/plan-handoff/src/mailbox.ts +306 -0
- package/packages/plan-handoff/src/message.ts +286 -0
- package/packages/plan-handoff/src/pairing.ts +120 -0
- package/packages/plan-handoff/src/publish.ts +186 -0
- package/packages/plan-handoff/src/registry.ts +174 -0
- package/packages/plan-handoff/src/retention.ts +237 -0
- package/packages/plan-handoff/src/routing.ts +134 -0
- package/packages/plan-handoff/src/workspace.ts +89 -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
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bridge between the sender and the recipient of a plan.
|
|
3
|
+
*
|
|
4
|
+
* The sender publishes a plan into the mailbox of the instance it selected.
|
|
5
|
+
* The recipient reads its own mailbox and delivers the plans that are
|
|
6
|
+
* addressed to it.
|
|
7
|
+
*
|
|
8
|
+
* The bridge never delivers a plan to an instance of another workspace: the
|
|
9
|
+
* mailbox of an agent belongs to that agent, and the routing decided the
|
|
10
|
+
* recipient before the message was written.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { randomUUID } from "node:crypto";
|
|
14
|
+
import { homedir, hostname } from "node:os";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import {
|
|
17
|
+
Mailbox,
|
|
18
|
+
PairingStore,
|
|
19
|
+
PendingHandoffStore,
|
|
20
|
+
RegistrationStore,
|
|
21
|
+
ReplyStore,
|
|
22
|
+
publishPlan,
|
|
23
|
+
type AgentRegistration,
|
|
24
|
+
type PlanHandoffMessage,
|
|
25
|
+
type PublicationResult,
|
|
26
|
+
type PublishPlanContent,
|
|
27
|
+
type RecipientPairing,
|
|
28
|
+
} from "../packages/plan-handoff/dist/index.js";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The directory of the handoff state of the user.
|
|
32
|
+
*/
|
|
33
|
+
export const PLAN_HANDOFF_HOME = join(
|
|
34
|
+
homedir(),
|
|
35
|
+
".pi-harness-runtime",
|
|
36
|
+
"plan-handoff",
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The directory of the registrations.
|
|
41
|
+
*/
|
|
42
|
+
export const PLAN_HANDOFF_REGISTRY_DIR = join(PLAN_HANDOFF_HOME, "agents");
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The directory of the retained handoffs.
|
|
46
|
+
*/
|
|
47
|
+
export const PLAN_HANDOFF_RETAINED_DIR = join(PLAN_HANDOFF_HOME, "retained");
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The directory of the mailboxes.
|
|
51
|
+
*/
|
|
52
|
+
export const PLAN_HANDOFF_MAILBOX_DIR = join(PLAN_HANDOFF_HOME, "mailboxes");
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The directory of the replies.
|
|
56
|
+
*/
|
|
57
|
+
export const PLAN_HANDOFF_REPLY_DIR = join(PLAN_HANDOFF_HOME, "replies");
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The file that remembers the pairing of a workspace.
|
|
61
|
+
*/
|
|
62
|
+
export const PLAN_HANDOFF_PAIRING_FILE = join(
|
|
63
|
+
PLAN_HANDOFF_HOME,
|
|
64
|
+
"pairings.json",
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The store of the registrations of the user.
|
|
69
|
+
*/
|
|
70
|
+
export function planHandoffRegistry(): RegistrationStore {
|
|
71
|
+
return new RegistrationStore(PLAN_HANDOFF_REGISTRY_DIR);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The store of the pairings of the user.
|
|
76
|
+
*/
|
|
77
|
+
export function planHandoffPairings(): PairingStore {
|
|
78
|
+
return new PairingStore(PLAN_HANDOFF_PAIRING_FILE);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The store of the retained handoffs of the user.
|
|
83
|
+
*/
|
|
84
|
+
export function planHandoffRetained(): PendingHandoffStore {
|
|
85
|
+
return new PendingHandoffStore(PLAN_HANDOFF_RETAINED_DIR);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The store of the replies of the user.
|
|
90
|
+
*/
|
|
91
|
+
export function planHandoffReplies(): ReplyStore {
|
|
92
|
+
return new ReplyStore(PLAN_HANDOFF_REPLY_DIR);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The mailbox of one agent.
|
|
97
|
+
*/
|
|
98
|
+
export function planHandoffMailbox(agentId: string): Mailbox {
|
|
99
|
+
return new Mailbox(join(PLAN_HANDOFF_MAILBOX_DIR, agentId));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The options of a publication through the bridge.
|
|
104
|
+
*/
|
|
105
|
+
export interface BridgePublishOptions {
|
|
106
|
+
/** The plan and the action it asks for. */
|
|
107
|
+
plan: PublishPlanContent;
|
|
108
|
+
/** The identity of the sender. */
|
|
109
|
+
sender: {
|
|
110
|
+
agentId: string;
|
|
111
|
+
sessionId: string;
|
|
112
|
+
workspace?: string;
|
|
113
|
+
label?: string;
|
|
114
|
+
};
|
|
115
|
+
/** The working directory of the sender. */
|
|
116
|
+
cwd?: string;
|
|
117
|
+
/** The host of the sender. */
|
|
118
|
+
host?: string;
|
|
119
|
+
/** The pairing that was selected before. */
|
|
120
|
+
pairing?: RecipientPairing;
|
|
121
|
+
/** The clock of the publication. */
|
|
122
|
+
now?: () => number;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The result of a publication through the bridge.
|
|
127
|
+
*/
|
|
128
|
+
export interface BridgePublishResult {
|
|
129
|
+
/** What the publication decided. */
|
|
130
|
+
outcome: "addressed" | "needs-recipient" | "retained";
|
|
131
|
+
/** The instance the plan was addressed to. */
|
|
132
|
+
recipient?: AgentRegistration;
|
|
133
|
+
/** The instances that matched, for a selection by the user. */
|
|
134
|
+
candidates: AgentRegistration[];
|
|
135
|
+
/** Why the publication decided that, in words. */
|
|
136
|
+
reason: string;
|
|
137
|
+
/** The message, when the plan could be addressed. */
|
|
138
|
+
message?: PlanHandoffMessage;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Publish a plan into the mailbox of the selected instance.
|
|
143
|
+
*
|
|
144
|
+
* A plan without a recipient is retained: it waits for an instance of the
|
|
145
|
+
* workspace, and it is never delivered to an instance of another project.
|
|
146
|
+
*/
|
|
147
|
+
export function publishPlanThroughBridge(
|
|
148
|
+
options: BridgePublishOptions,
|
|
149
|
+
): BridgePublishResult {
|
|
150
|
+
const registry = planHandoffRegistry();
|
|
151
|
+
const result: PublicationResult = publishPlan({
|
|
152
|
+
plan: options.plan,
|
|
153
|
+
sender: options.sender,
|
|
154
|
+
registrations: registry.live(),
|
|
155
|
+
host: options.host ?? hostname(),
|
|
156
|
+
pairing: options.pairing,
|
|
157
|
+
cwd: options.cwd,
|
|
158
|
+
now: options.now,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
if (result.outcome !== "addressed" || !result.message || !result.recipient) {
|
|
162
|
+
if (result.outcome === "retained") {
|
|
163
|
+
// The plan waits for a recipient of its workspace. It keeps its own
|
|
164
|
+
// identity, so a later claim of the retained handoff names the
|
|
165
|
+
// exact plan that was published.
|
|
166
|
+
const now = options.now ? options.now() : Date.now();
|
|
167
|
+
const id = randomUUID();
|
|
168
|
+
planHandoffRetained().retainWithExpiry({
|
|
169
|
+
id,
|
|
170
|
+
correlationId: id,
|
|
171
|
+
workspace:
|
|
172
|
+
options.sender.workspace ??
|
|
173
|
+
options.cwd ??
|
|
174
|
+
process.cwd(),
|
|
175
|
+
state: "awaiting-recipient",
|
|
176
|
+
createdAt: now,
|
|
177
|
+
summary: options.plan.plan.summary,
|
|
178
|
+
...(options.plan.plan.reference
|
|
179
|
+
? { reference: options.plan.plan.reference }
|
|
180
|
+
: {}),
|
|
181
|
+
}, now);
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
outcome: result.outcome,
|
|
185
|
+
candidates: result.candidates,
|
|
186
|
+
reason: result.reason,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
planHandoffMailbox(result.recipient.agentId).publish(result.message);
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
outcome: "addressed",
|
|
194
|
+
recipient: result.recipient,
|
|
195
|
+
candidates: result.candidates,
|
|
196
|
+
reason: result.reason,
|
|
197
|
+
message: result.message,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The options of a receipt.
|
|
203
|
+
*/
|
|
204
|
+
export interface BridgeReceiveOptions {
|
|
205
|
+
/** The registration of this instance. */
|
|
206
|
+
registration: AgentRegistration;
|
|
207
|
+
/** The clock of the receipt. */
|
|
208
|
+
now?: () => number;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The plans that are addressed to this instance and were not delivered yet.
|
|
213
|
+
*
|
|
214
|
+
* The recipient validates the message against its own registration before
|
|
215
|
+
* it queues anything: a plan of a session that ended, of another generation
|
|
216
|
+
* or of another workspace never enters the queue.
|
|
217
|
+
*/
|
|
218
|
+
export function receivePlans(
|
|
219
|
+
options: BridgeReceiveOptions,
|
|
220
|
+
): PlanHandoffMessage[] {
|
|
221
|
+
const mailbox = planHandoffMailbox(options.registration.agentId);
|
|
222
|
+
const received: PlanHandoffMessage[] = [];
|
|
223
|
+
for (const entry of mailbox.pending()) {
|
|
224
|
+
const message = entry.message;
|
|
225
|
+
if (message.recipient.agentId !== options.registration.agentId) continue;
|
|
226
|
+
if (message.recipient.sessionId !== options.registration.sessionId) continue;
|
|
227
|
+
if (message.recipient.generation !== options.registration.generation) continue;
|
|
228
|
+
received.push(message);
|
|
229
|
+
}
|
|
230
|
+
return received;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Acknowledge the delivery of a plan.
|
|
235
|
+
*/
|
|
236
|
+
export function acknowledgePlan(agentId: string, messageId: string): boolean {
|
|
237
|
+
const acknowledged = planHandoffMailbox(agentId).acknowledge(messageId);
|
|
238
|
+
return acknowledged?.state === "delivered";
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Claim a plan for this instance.
|
|
243
|
+
*/
|
|
244
|
+
export function claimPlan(agentId: string, messageId: string): boolean {
|
|
245
|
+
const claimed = planHandoffMailbox(agentId).claim(messageId);
|
|
246
|
+
return claimed?.state === "claimed";
|
|
247
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The delivery of a plan to the Pi instance of its workspace.
|
|
3
|
+
*
|
|
4
|
+
* The Codex watcher observes a plan of a project. The delivery decides which
|
|
5
|
+
* Pi instance receives it:
|
|
6
|
+
*
|
|
7
|
+
* - the instance whose canonical workspace is the workspace of the plan,
|
|
8
|
+
* - chosen automatically when exactly one live instance runs there,
|
|
9
|
+
* - chosen by the user when several of them do,
|
|
10
|
+
* - and nothing is delivered when no instance runs there: the plan waits.
|
|
11
|
+
*
|
|
12
|
+
* The comparison of the workspaces is a comparison of the canonical paths, so
|
|
13
|
+
* a symlink of the same directory matches and two worktrees of one
|
|
14
|
+
* repository do not.
|
|
15
|
+
*
|
|
16
|
+
* The decision belongs to the package of the handoff: this module only names
|
|
17
|
+
* the workspace of the sender, remembers the pairing of the user and writes
|
|
18
|
+
* the plan into the mailbox of the instance that was selected.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { hostname } from "node:os";
|
|
22
|
+
import {
|
|
23
|
+
canonicalizePath,
|
|
24
|
+
type AgentRegistration,
|
|
25
|
+
type Mailbox,
|
|
26
|
+
type PendingHandoffStore,
|
|
27
|
+
type RecipientResolution,
|
|
28
|
+
} from "../packages/plan-handoff/dist/index.js";
|
|
29
|
+
import {
|
|
30
|
+
planHandoffMailbox,
|
|
31
|
+
planHandoffPairings,
|
|
32
|
+
planHandoffRegistry,
|
|
33
|
+
planHandoffRetained,
|
|
34
|
+
} from "./plan-handoff-bridge.ts";
|
|
35
|
+
import { publishPlan } from "../packages/plan-handoff/dist/index.js";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What the delivery decided.
|
|
39
|
+
*/
|
|
40
|
+
export type PlanDeliveryDecision =
|
|
41
|
+
/** The plan is delivered to the instance of this workspace. */
|
|
42
|
+
| "delivered"
|
|
43
|
+
/** The plan is queued in the mailbox of the instance. */
|
|
44
|
+
| "queued"
|
|
45
|
+
/** Several instances run in the workspace: the user chooses. */
|
|
46
|
+
| "needs-recipient"
|
|
47
|
+
/** No instance runs in the workspace: the plan waits. */
|
|
48
|
+
| "retained";
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The result of a delivery.
|
|
52
|
+
*/
|
|
53
|
+
export interface PlanDeliveryResult {
|
|
54
|
+
/** What the delivery decided. */
|
|
55
|
+
decision: PlanDeliveryDecision;
|
|
56
|
+
/** The instance the plan is for. */
|
|
57
|
+
recipient?: AgentRegistration;
|
|
58
|
+
/** The instances that matched the workspace. */
|
|
59
|
+
candidates: AgentRegistration[];
|
|
60
|
+
/** Why the delivery decided that, in words. */
|
|
61
|
+
reason: string;
|
|
62
|
+
/** The routing the delivery used. */
|
|
63
|
+
resolution: RecipientResolution;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The options of a delivery.
|
|
68
|
+
*/
|
|
69
|
+
export interface PlanDeliveryOptions {
|
|
70
|
+
/** The workspace of the plan: the project the plan belongs to. */
|
|
71
|
+
workspace: string;
|
|
72
|
+
/** The identity of the sender. */
|
|
73
|
+
sender: {
|
|
74
|
+
agentId: string;
|
|
75
|
+
sessionId: string;
|
|
76
|
+
label?: string;
|
|
77
|
+
};
|
|
78
|
+
/** The summary of the plan, for the user. */
|
|
79
|
+
summary: string;
|
|
80
|
+
/** The path of the plan, when it is a file. */
|
|
81
|
+
reference?: string;
|
|
82
|
+
/** The live registrations. The registry of the user by default. */
|
|
83
|
+
registrations?: AgentRegistration[];
|
|
84
|
+
/** The host of the sender. */
|
|
85
|
+
host?: string;
|
|
86
|
+
/** The pairing that was selected before. */
|
|
87
|
+
pairing?: { agentId: string; sessionId?: string };
|
|
88
|
+
/** The mailbox of the instances. The mailbox of the user by default. */
|
|
89
|
+
mailbox?: (agentId: string) => Mailbox;
|
|
90
|
+
/** The store of the retained handoffs. The store of the user by default. */
|
|
91
|
+
retained?: PendingHandoffStore;
|
|
92
|
+
/** The clock of the delivery. */
|
|
93
|
+
now?: () => number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Deliver a plan to the Pi instance of its workspace.
|
|
98
|
+
*
|
|
99
|
+
* The plan is written into the mailbox of the instance. The instance reads
|
|
100
|
+
* its own mailbox, so the plan reaches exactly the instance the routing
|
|
101
|
+
* selected, and never an instance of another project.
|
|
102
|
+
*/
|
|
103
|
+
export function deliverPlanToWorkspace(
|
|
104
|
+
options: PlanDeliveryOptions,
|
|
105
|
+
): PlanDeliveryResult {
|
|
106
|
+
const now = options.now ?? (() => Date.now());
|
|
107
|
+
const registry = planHandoffRegistry();
|
|
108
|
+
const registrations = options.registrations ?? registry.live();
|
|
109
|
+
const mailbox = options.mailbox ?? planHandoffMailbox;
|
|
110
|
+
const retained = options.retained ?? planHandoffRetained();
|
|
111
|
+
const host = options.host ?? hostname();
|
|
112
|
+
const workspace = canonicalizePath(options.workspace);
|
|
113
|
+
|
|
114
|
+
// The pairing the user chose before decides, when it still names a live
|
|
115
|
+
// instance of this workspace. A pairing of a session that ended never
|
|
116
|
+
// decides.
|
|
117
|
+
const pairing = options.pairing ?? planHandoffPairings().read(workspace);
|
|
118
|
+
|
|
119
|
+
const publication = publishPlan({
|
|
120
|
+
plan: {
|
|
121
|
+
action: "inform",
|
|
122
|
+
plan: {
|
|
123
|
+
revision: "1",
|
|
124
|
+
summary: options.summary,
|
|
125
|
+
...(options.reference ? { reference: options.reference } : {}),
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
sender: {
|
|
129
|
+
agentId: options.sender.agentId,
|
|
130
|
+
sessionId: options.sender.sessionId,
|
|
131
|
+
workspace,
|
|
132
|
+
label: options.sender.label,
|
|
133
|
+
},
|
|
134
|
+
registrations,
|
|
135
|
+
host,
|
|
136
|
+
pairing,
|
|
137
|
+
now,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if (publication.outcome === "needs-recipient") {
|
|
141
|
+
return {
|
|
142
|
+
decision: "needs-recipient",
|
|
143
|
+
candidates: publication.candidates,
|
|
144
|
+
reason: publication.reason,
|
|
145
|
+
resolution: publication.resolution,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (publication.outcome === "retained") {
|
|
150
|
+
// No Pi instance runs in the workspace of the plan: the plan waits
|
|
151
|
+
// for one. It is never delivered to an instance of another project.
|
|
152
|
+
const id = `plan-${now()}`;
|
|
153
|
+
retained.retainWithExpiry({
|
|
154
|
+
id,
|
|
155
|
+
correlationId: id,
|
|
156
|
+
workspace,
|
|
157
|
+
state: "awaiting-recipient",
|
|
158
|
+
createdAt: now(),
|
|
159
|
+
summary: options.summary,
|
|
160
|
+
...(options.reference ? { reference: options.reference } : {}),
|
|
161
|
+
}, now());
|
|
162
|
+
return {
|
|
163
|
+
decision: "retained",
|
|
164
|
+
candidates: publication.candidates,
|
|
165
|
+
reason: publication.reason,
|
|
166
|
+
resolution: publication.resolution,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const recipient = publication.recipient as AgentRegistration;
|
|
171
|
+
const message = publication.message;
|
|
172
|
+
if (!message) {
|
|
173
|
+
return {
|
|
174
|
+
decision: "retained",
|
|
175
|
+
candidates: publication.candidates,
|
|
176
|
+
reason:
|
|
177
|
+
"the plan could not be addressed to the instance of the workspace",
|
|
178
|
+
resolution: publication.resolution,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
mailbox(recipient.agentId).publish(message);
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
decision: "queued",
|
|
186
|
+
recipient,
|
|
187
|
+
candidates: publication.candidates,
|
|
188
|
+
reason: publication.reason,
|
|
189
|
+
resolution: publication.resolution,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { planHandoffMailbox };
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The selection of the recipient of a plan.
|
|
3
|
+
*
|
|
4
|
+
* Several Pi instances can run in the workspace of a plan. The plan is not
|
|
5
|
+
* broadcast: the user chooses the instance that receives it, and the choice
|
|
6
|
+
* becomes the pairing of that workspace.
|
|
7
|
+
*
|
|
8
|
+
* The selection is pending until the user answers or the offer expires. The
|
|
9
|
+
* first answer wins, and an expired offer is no offer.
|
|
10
|
+
*
|
|
11
|
+
* Every candidate has its own token: the callback data of a button has to
|
|
12
|
+
* stay short enough for the channels, and a token names one candidate of one
|
|
13
|
+
* offer without carrying the identity of the instance in the open.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { randomUUID } from "node:crypto";
|
|
17
|
+
import {
|
|
18
|
+
canonicalizePath,
|
|
19
|
+
type AgentRegistration,
|
|
20
|
+
type PairingStore,
|
|
21
|
+
} from "../packages/plan-handoff/dist/index.js";
|
|
22
|
+
import { planHandoffPairings } from "./plan-handoff-bridge.ts";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One candidate of an offer.
|
|
26
|
+
*/
|
|
27
|
+
export interface RecipientOffer {
|
|
28
|
+
/** The token that names this candidate in the callback data. */
|
|
29
|
+
token: string;
|
|
30
|
+
/** The agent id of the candidate. */
|
|
31
|
+
agentId: string;
|
|
32
|
+
/** The session of the candidate. */
|
|
33
|
+
sessionId: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* One pending selection.
|
|
38
|
+
*/
|
|
39
|
+
export interface PendingRecipientSelection {
|
|
40
|
+
/** The token that names the selection. */
|
|
41
|
+
token: string;
|
|
42
|
+
/** The canonical workspace of the plan. */
|
|
43
|
+
workspace: string;
|
|
44
|
+
/** The path of the plan, when it is a file. */
|
|
45
|
+
planPath?: string;
|
|
46
|
+
/** The summary of the plan, for the user. */
|
|
47
|
+
summary?: string;
|
|
48
|
+
/** The session of the sender, for the reply. */
|
|
49
|
+
senderSessionId?: string;
|
|
50
|
+
/** The instances the user can choose from. */
|
|
51
|
+
candidates: AgentRegistration[];
|
|
52
|
+
/** The token of every candidate. */
|
|
53
|
+
offers: RecipientOffer[];
|
|
54
|
+
/** The store that keeps the pairing of the workspace. */
|
|
55
|
+
pairings: PairingStore;
|
|
56
|
+
/** When the offer expires. */
|
|
57
|
+
expiresAt: number;
|
|
58
|
+
/** True when the selection was answered. */
|
|
59
|
+
used: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* How long an offer of the recipients stays open.
|
|
64
|
+
*/
|
|
65
|
+
export const RECIPIENT_SELECTION_TTL_MS = 30 * 60 * 1_000;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The pending selections of the runtime.
|
|
69
|
+
*
|
|
70
|
+
* The state is in memory: an offer that the runtime does not remember is no
|
|
71
|
+
* offer, and the user is asked again.
|
|
72
|
+
*/
|
|
73
|
+
const pendingSelections = new Map<string, PendingRecipientSelection>();
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The token of every candidate, so that a click names one candidate.
|
|
77
|
+
*/
|
|
78
|
+
const offerIndex = new Map<string, { selectionToken: string; agentId: string }>();
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Offer the candidates of a plan to the user.
|
|
82
|
+
*
|
|
83
|
+
* The offer names the identity of every candidate: the agent id and the
|
|
84
|
+
* session. The model label is a hint, never an identity.
|
|
85
|
+
*/
|
|
86
|
+
export function offerRecipientSelection(options: {
|
|
87
|
+
workspace: string;
|
|
88
|
+
planPath?: string;
|
|
89
|
+
summary?: string;
|
|
90
|
+
senderSessionId?: string;
|
|
91
|
+
candidates: AgentRegistration[];
|
|
92
|
+
/** The store that keeps the pairing of the workspace. */
|
|
93
|
+
pairings?: PairingStore;
|
|
94
|
+
now?: number;
|
|
95
|
+
ttlMs?: number;
|
|
96
|
+
}): PendingRecipientSelection {
|
|
97
|
+
const now = options.now ?? Date.now();
|
|
98
|
+
const token = randomUUID().replace(/-/g, "").slice(0, 12);
|
|
99
|
+
const offers: RecipientOffer[] = options.candidates.map((candidate) => {
|
|
100
|
+
const offerToken = randomUUID().replace(/-/g, "").slice(0, 12);
|
|
101
|
+
offerIndex.set(offerToken, { selectionToken: token, agentId: candidate.agentId });
|
|
102
|
+
return {
|
|
103
|
+
token: offerToken,
|
|
104
|
+
agentId: candidate.agentId,
|
|
105
|
+
sessionId: candidate.sessionId,
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const selection: PendingRecipientSelection = {
|
|
110
|
+
token,
|
|
111
|
+
workspace: canonicalizePath(options.workspace),
|
|
112
|
+
candidates: options.candidates,
|
|
113
|
+
offers,
|
|
114
|
+
pairings: options.pairings ?? planHandoffPairings(),
|
|
115
|
+
expiresAt: now + (options.ttlMs ?? RECIPIENT_SELECTION_TTL_MS),
|
|
116
|
+
used: false,
|
|
117
|
+
};
|
|
118
|
+
if (options.planPath) selection.planPath = options.planPath;
|
|
119
|
+
if (options.summary) selection.summary = options.summary;
|
|
120
|
+
if (options.senderSessionId) selection.senderSessionId = options.senderSessionId;
|
|
121
|
+
pendingSelections.set(token, selection);
|
|
122
|
+
return selection;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The pending selection of a token.
|
|
127
|
+
*/
|
|
128
|
+
export function readRecipientSelection(
|
|
129
|
+
token: string,
|
|
130
|
+
): PendingRecipientSelection | undefined {
|
|
131
|
+
return pendingSelections.get(token);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The pending selections that are still open.
|
|
136
|
+
*/
|
|
137
|
+
export function openRecipientSelections(
|
|
138
|
+
now: number = Date.now(),
|
|
139
|
+
): PendingRecipientSelection[] {
|
|
140
|
+
return Array.from(pendingSelections.values()).filter(
|
|
141
|
+
(selection) => !selection.used && selection.expiresAt > now,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Apply the choice of the user.
|
|
147
|
+
*
|
|
148
|
+
* The choice names one candidate of the offer. The first answer wins, an
|
|
149
|
+
* expired offer is refused, and a choice of an unknown candidate is refused.
|
|
150
|
+
* The pairing of the workspace is remembered, so the next plan of the same
|
|
151
|
+
* workspace goes to the instance the user chose.
|
|
152
|
+
*/
|
|
153
|
+
export function resolveRecipientSelection(
|
|
154
|
+
offerToken: string,
|
|
155
|
+
now: number = Date.now(),
|
|
156
|
+
): { ok: boolean; reason: string; selection?: PendingRecipientSelection; recipient?: AgentRegistration } {
|
|
157
|
+
const index = offerIndex.get(offerToken);
|
|
158
|
+
if (!index) {
|
|
159
|
+
return {
|
|
160
|
+
ok: false,
|
|
161
|
+
reason: "the offer of the recipients is unknown or already gone",
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
const selection = pendingSelections.get(index.selectionToken);
|
|
165
|
+
if (!selection) {
|
|
166
|
+
offerIndex.delete(offerToken);
|
|
167
|
+
return {
|
|
168
|
+
ok: false,
|
|
169
|
+
reason: "the offer of the recipients is unknown or already gone",
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (selection.used) {
|
|
173
|
+
return {
|
|
174
|
+
ok: false,
|
|
175
|
+
reason: "the offer of the recipients was already answered",
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
if (selection.expiresAt <= now) {
|
|
179
|
+
pendingSelections.delete(selection.token);
|
|
180
|
+
return { ok: false, reason: "the offer of the recipients expired" };
|
|
181
|
+
}
|
|
182
|
+
const candidate = selection.candidates.find(
|
|
183
|
+
(entry) => entry.agentId === index.agentId,
|
|
184
|
+
);
|
|
185
|
+
if (!candidate) {
|
|
186
|
+
return {
|
|
187
|
+
ok: false,
|
|
188
|
+
reason: "the chosen instance is not one of the offered instances",
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
selection.used = true;
|
|
193
|
+
selection.pairings.write(selection.workspace, {
|
|
194
|
+
agentId: candidate.agentId,
|
|
195
|
+
sessionId: candidate.sessionId,
|
|
196
|
+
pairedAt: now,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
ok: true,
|
|
201
|
+
reason: "the recipient is chosen",
|
|
202
|
+
selection,
|
|
203
|
+
recipient: candidate,
|
|
204
|
+
};
|
|
205
|
+
}
|