pi-harness-runtime 1.1.512 → 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 +183 -0
- package/footer-status.ts +112 -43
- package/harness/cookie-import-refresh.ts +60 -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/harness/quota-credential-choice.ts +67 -0
- package/harness/quota-refresh-coordinator.ts +129 -0
- package/index.ts +633 -115
- 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/dist/detect-format.d.ts +26 -12
- package/packages/cookie-sanitizer/dist/detect-format.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/detect-format.js +54 -39
- package/packages/cookie-sanitizer/dist/detect-format.js.map +1 -1
- package/packages/cookie-sanitizer/dist/index.d.ts +9 -3
- package/packages/cookie-sanitizer/dist/index.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/index.js +5 -3
- package/packages/cookie-sanitizer/dist/index.js.map +1 -1
- package/packages/cookie-sanitizer/dist/parse-netscape-space.d.ts +73 -0
- package/packages/cookie-sanitizer/dist/parse-netscape-space.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/parse-netscape-space.js +154 -0
- package/packages/cookie-sanitizer/dist/parse-netscape-space.js.map +1 -0
- package/packages/cookie-sanitizer/dist/parse-netscape.d.ts +28 -1
- package/packages/cookie-sanitizer/dist/parse-netscape.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/parse-netscape.js +50 -13
- package/packages/cookie-sanitizer/dist/parse-netscape.js.map +1 -1
- package/packages/cookie-sanitizer/dist/source-state.d.ts +56 -0
- package/packages/cookie-sanitizer/dist/source-state.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/source-state.js +61 -0
- package/packages/cookie-sanitizer/dist/source-state.js.map +1 -0
- package/packages/cookie-sanitizer/dist/sync.d.ts +9 -0
- package/packages/cookie-sanitizer/dist/sync.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/sync.js +47 -16
- package/packages/cookie-sanitizer/dist/sync.js.map +1 -1
- package/packages/cookie-sanitizer/dist/types.d.ts +1 -1
- package/packages/cookie-sanitizer/dist/types.d.ts.map +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/cookie-sanitizer/src/detect-format.ts +78 -41
- package/packages/cookie-sanitizer/src/index.ts +34 -1
- package/packages/cookie-sanitizer/src/parse-netscape-space.ts +188 -0
- package/packages/cookie-sanitizer/src/parse-netscape.ts +81 -13
- package/packages/cookie-sanitizer/src/source-state.ts +95 -0
- package/packages/cookie-sanitizer/src/sync.ts +59 -16
- package/packages/cookie-sanitizer/src/types.ts +4 -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,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
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The registration of the running Pi instance.
|
|
3
|
+
*
|
|
4
|
+
* The extension registers the instance it runs in, so a sender can find the
|
|
5
|
+
* recipient of a plan in the same workspace. The registration proves that
|
|
6
|
+
* the instance is alive and where it works. It grants no permission: the
|
|
7
|
+
* recipient still validates the message before it queues anything.
|
|
8
|
+
*
|
|
9
|
+
* The model label of the registration is descriptive. The identity is the
|
|
10
|
+
* agent id, and the liveness is the heartbeat.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { randomUUID } from "node:crypto";
|
|
14
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { homedir, hostname } from "node:os";
|
|
16
|
+
import { dirname, join } from "node:path";
|
|
17
|
+
import {
|
|
18
|
+
RegistrationStore,
|
|
19
|
+
detectWorkspace,
|
|
20
|
+
type AgentRegistration,
|
|
21
|
+
} from "../packages/plan-handoff/dist/index.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The directory of the registrations.
|
|
25
|
+
*/
|
|
26
|
+
export const PLAN_HANDOFF_STORE_DIR = join(
|
|
27
|
+
homedir(),
|
|
28
|
+
".pi-harness-runtime",
|
|
29
|
+
"plan-handoff",
|
|
30
|
+
"agents",
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The file that counts the generations of the instances.
|
|
35
|
+
*/
|
|
36
|
+
export const PLAN_HANDOFF_GENERATION_FILE = join(
|
|
37
|
+
homedir(),
|
|
38
|
+
".pi-harness-runtime",
|
|
39
|
+
"plan-handoff",
|
|
40
|
+
"generation.json",
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* How often the instance writes its heartbeat.
|
|
45
|
+
*/
|
|
46
|
+
export const PLAN_HANDOFF_HEARTBEAT_MS = 20_000;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The registration of a running instance.
|
|
50
|
+
*/
|
|
51
|
+
export interface PlanHandoffAgent {
|
|
52
|
+
/** The runtime-generated identity of the instance. */
|
|
53
|
+
agentId: string;
|
|
54
|
+
/** The session of the instance. */
|
|
55
|
+
sessionId: string;
|
|
56
|
+
/** The generation of the instance: a restart raises it. */
|
|
57
|
+
generation: number;
|
|
58
|
+
/** The store the registration lives in. */
|
|
59
|
+
store: RegistrationStore;
|
|
60
|
+
/** Remove the registration and stop the heartbeat. */
|
|
61
|
+
stop: () => void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The options of the registration.
|
|
66
|
+
*/
|
|
67
|
+
export interface StartPlanHandoffOptions {
|
|
68
|
+
/** The session of the instance. */
|
|
69
|
+
sessionId: string;
|
|
70
|
+
/** The workspace of the instance. The working directory by default. */
|
|
71
|
+
workspace?: string;
|
|
72
|
+
/** The host of the instance. */
|
|
73
|
+
host?: string;
|
|
74
|
+
/** The process of the instance. */
|
|
75
|
+
pid?: number;
|
|
76
|
+
/** The model of the instance. Descriptive, never an identity. */
|
|
77
|
+
modelLabel?: string;
|
|
78
|
+
/** The clock of the heartbeat. */
|
|
79
|
+
now?: () => number;
|
|
80
|
+
/** The store of the registrations. */
|
|
81
|
+
store?: RegistrationStore;
|
|
82
|
+
/** The file that counts the generations. */
|
|
83
|
+
generationFile?: string;
|
|
84
|
+
/** How often the heartbeat is written. */
|
|
85
|
+
heartbeatMs?: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The next generation of the instance.
|
|
90
|
+
*
|
|
91
|
+
* The counter survives the end of a session, so a restart of the same
|
|
92
|
+
* session raises the generation instead of reusing it.
|
|
93
|
+
*/
|
|
94
|
+
function nextGeneration(file: string): number {
|
|
95
|
+
let current = 0;
|
|
96
|
+
try {
|
|
97
|
+
const parsed = JSON.parse(readFileSync(file, "utf8")) as {
|
|
98
|
+
generation?: unknown;
|
|
99
|
+
};
|
|
100
|
+
if (typeof parsed?.generation === "number") current = parsed.generation;
|
|
101
|
+
} catch {
|
|
102
|
+
// The counter does not exist yet: the first instance starts at one.
|
|
103
|
+
current = 0;
|
|
104
|
+
}
|
|
105
|
+
const next = current + 1;
|
|
106
|
+
try {
|
|
107
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
108
|
+
writeFileSync(file, `${JSON.stringify({ generation: next }, null, 2)}\n`, {
|
|
109
|
+
mode: 0o600,
|
|
110
|
+
});
|
|
111
|
+
} catch {
|
|
112
|
+
// A counter that cannot be written still yields a generation: the
|
|
113
|
+
// identity of the instance is the agent id, and the generation only
|
|
114
|
+
// refuses a message of a previous instance.
|
|
115
|
+
}
|
|
116
|
+
return next;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Register the running instance and keep its heartbeat alive.
|
|
121
|
+
*
|
|
122
|
+
* The registration is removed when the instance stops. A sender that finds
|
|
123
|
+
* no live registration keeps the plan and waits for an instance to appear.
|
|
124
|
+
*/
|
|
125
|
+
export function startPlanHandoffRegistration(
|
|
126
|
+
options: StartPlanHandoffOptions,
|
|
127
|
+
): PlanHandoffAgent {
|
|
128
|
+
const now = options.now ?? (() => Date.now());
|
|
129
|
+
const store = options.store ?? new RegistrationStore(PLAN_HANDOFF_STORE_DIR, { now });
|
|
130
|
+
const agentId = randomUUID();
|
|
131
|
+
const generation = nextGeneration(
|
|
132
|
+
options.generationFile ?? PLAN_HANDOFF_GENERATION_FILE,
|
|
133
|
+
);
|
|
134
|
+
const workspace = detectWorkspace(options.workspace ?? process.cwd());
|
|
135
|
+
const host = options.host ?? hostname();
|
|
136
|
+
const pid = options.pid ?? process.pid;
|
|
137
|
+
|
|
138
|
+
const write = (): void => {
|
|
139
|
+
const registration: AgentRegistration = {
|
|
140
|
+
agentId,
|
|
141
|
+
sessionId: options.sessionId,
|
|
142
|
+
generation,
|
|
143
|
+
workspace: workspace.path,
|
|
144
|
+
host,
|
|
145
|
+
pid,
|
|
146
|
+
modelLabel: options.modelLabel,
|
|
147
|
+
busy: false,
|
|
148
|
+
paused: false,
|
|
149
|
+
startedAt: now(),
|
|
150
|
+
heartbeatAt: now(),
|
|
151
|
+
};
|
|
152
|
+
store.upsert(registration);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
write();
|
|
156
|
+
const heartbeat = setInterval(write, options.heartbeatMs ?? PLAN_HANDOFF_HEARTBEAT_MS);
|
|
157
|
+
if (typeof heartbeat.unref === "function") heartbeat.unref();
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
agentId,
|
|
161
|
+
sessionId: options.sessionId,
|
|
162
|
+
generation,
|
|
163
|
+
store,
|
|
164
|
+
stop: () => {
|
|
165
|
+
clearInterval(heartbeat);
|
|
166
|
+
store.remove(agentId);
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The choice of the credential of a quota refresh.
|
|
3
|
+
*
|
|
4
|
+
* Two credentials can answer a quota request:
|
|
5
|
+
*
|
|
6
|
+
* - the browser profile: a logged-in session that needs no import,
|
|
7
|
+
* - the canonical cookie cache: the normalized output of the sanitizer.
|
|
8
|
+
*
|
|
9
|
+
* The browser profile comes first, and its success does not depend on the
|
|
10
|
+
* drop folder at all: a machine whose drop file is malformed can still
|
|
11
|
+
* read its quota through the profile.
|
|
12
|
+
*
|
|
13
|
+
* The cookie cache comes second, and only when an import published it. A
|
|
14
|
+
* file that was found but not imported gives a scraper nothing to read,
|
|
15
|
+
* and asking the provider with nothing is a failure with a name.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { CookieSourceStage } from "../packages/cookie-sanitizer/src/source-state.ts";
|
|
19
|
+
|
|
20
|
+
/** The credential that answers a quota refresh. */
|
|
21
|
+
export type QuotaCredentialChoice = "browser-profile" | "cookie-cache" | "none";
|
|
22
|
+
|
|
23
|
+
/** The facts the choice is made from. */
|
|
24
|
+
export interface QuotaCredentialFacts {
|
|
25
|
+
/**
|
|
26
|
+
* The outcome of the browser profile, when it ran.
|
|
27
|
+
*
|
|
28
|
+
* `ok` means the profile held a quota reading.
|
|
29
|
+
*/
|
|
30
|
+
browserProfile?: { ok: boolean; reason?: string } | null;
|
|
31
|
+
/** The stage of the cookie source. */
|
|
32
|
+
cookieStage: CookieSourceStage;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Choose the credential of a quota refresh.
|
|
37
|
+
*
|
|
38
|
+
* The reason of a "none" names the failure, never a credential.
|
|
39
|
+
*/
|
|
40
|
+
export function chooseQuotaCredential(facts: QuotaCredentialFacts): {
|
|
41
|
+
choice: QuotaCredentialChoice;
|
|
42
|
+
reason?: string;
|
|
43
|
+
} {
|
|
44
|
+
const profile = facts.browserProfile;
|
|
45
|
+
if (profile?.ok) {
|
|
46
|
+
return { choice: "browser-profile" };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (facts.cookieStage === "imported") {
|
|
50
|
+
return { choice: "cookie-cache" };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (facts.cookieStage === "discovered") {
|
|
54
|
+
return {
|
|
55
|
+
choice: "none",
|
|
56
|
+
reason:
|
|
57
|
+
"the cookie file was found but not imported into the canonical cache",
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
choice: "none",
|
|
63
|
+
reason: profile?.reason
|
|
64
|
+
? `no imported cookie cache is available (${profile.reason})`
|
|
65
|
+
: "no imported cookie cache is available",
|
|
66
|
+
};
|
|
67
|
+
}
|