@staix/agent-hub 0.12.2 → 0.12.4
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 +16 -0
- package/docs/cooperbench.md +109 -0
- package/docs/events.md +14 -3
- package/docs/operations.md +158 -5
- package/docs/smoke.md +16 -0
- package/docs/specs/2026-09-19-agent-hub-design.md +66 -0
- package/docs/verification/2026-10-02-0.12.3.md +44 -0
- package/docs/verification/2026-10-02-0.12.4.md +82 -0
- package/package.json +1 -1
- package/plugins/agent-hub/.claude-plugin/plugin.json +1 -1
- package/plugins/agent-hub/server.js +23 -8
- package/src/adapters/claude-channel.ts +19 -5
- package/src/adapters/codex-appserver.ts +38 -0
- package/src/adapters/local-worker.ts +109 -16
- package/src/adapters/pi.ts +106 -21
- package/src/cli/facts-hook.ts +40 -0
- package/src/cli/launch.ts +45 -4
- package/src/cli/main.ts +44 -2
- package/src/cli/status-lines.ts +2 -1
- package/src/cli/upgrade-runtime.ts +1 -1
- package/src/hub/bus.ts +92 -6
- package/src/hub/cohorts.ts +308 -0
- package/src/hub/control-client.ts +3 -3
- package/src/hub/daemon.ts +425 -21
- package/src/hub/events.ts +12 -1
- package/src/hub/execution-budget.ts +138 -0
- package/src/hub/facts.ts +754 -0
- package/src/hub/report.ts +88 -1
- package/src/hub/routing.ts +85 -0
- package/src/hub/tasks.ts +402 -19
- package/src/hub/usage.ts +55 -0
- package/src/local/sandbox.ts +6 -2
- package/src/local/tools.ts +7 -2
- package/src/models/relay.ts +12 -4
- package/src/omniroute/client.ts +14 -3
- package/src/omniroute/usage.ts +38 -0
- package/src/pi/extension.ts +44 -8
- package/templates/AGENTS.block.md +1 -1
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import type { Task } from "./board.ts";
|
|
2
|
+
import type { PeerId } from "./envelope.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Turn-free cohorts (issue #107): the owners of overlapping tasks, frozen into one group from the moment the overlap is
|
|
6
|
+
* found until each of them has stopped after its task closed. Whether a cohort is silent is decided when it is formed
|
|
7
|
+
* (turn-free on, no PII, every owner's context path verified) and never switched on later; it is lifted when an owner
|
|
8
|
+
* that cannot receive facts joins, a path is lost, or a PII task opens. Membership does not end when the board closes a
|
|
9
|
+
* task: a member stays in until its native turn has ended after that, so a late final answer is still the cohort's.
|
|
10
|
+
* That settlement is recorded when it happens and never undone: a settled member's later turns are new work.
|
|
11
|
+
*
|
|
12
|
+
* Completion is two-step. Each member's done is an intent. The member whose intent completes the set is selected, in
|
|
13
|
+
* one synchronous step, to integrate: it is asked to check its work against the others before its done is recorded,
|
|
14
|
+
* and its next done is accepted only for the same target (owner generation, cohort revision, files) once every other
|
|
15
|
+
* member has settled. Anything that changes the target asks again, a bounded number of times; past that the outcome is
|
|
16
|
+
* recorded as unresolved, never as integrated.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** Integration requests for one cohort revision before the outcome is recorded as unresolved. */
|
|
20
|
+
export const MAX_REQUESTS = 3;
|
|
21
|
+
/** A done this soon after an integration request is a retry of a lost answer, not a check of the work. */
|
|
22
|
+
export const RETRY_MS = 2000;
|
|
23
|
+
|
|
24
|
+
export interface Member {
|
|
25
|
+
task: number;
|
|
26
|
+
owner: PeerId;
|
|
27
|
+
/** Changes whenever the task changes hands. */
|
|
28
|
+
gen: number;
|
|
29
|
+
/** When its owner was handed the task: its writes count for the integration target from here until it settles. */
|
|
30
|
+
since: number;
|
|
31
|
+
/** When the task left the open states for its owner. */
|
|
32
|
+
closedAt?: number;
|
|
33
|
+
/** When its owner's native turn first ended after that (or the task closed while the owner was idle). */
|
|
34
|
+
settledAt?: number;
|
|
35
|
+
/** When its owner's native turn first ended after its current intent: its writes for the task have stopped. */
|
|
36
|
+
stoppedAt?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Integration {
|
|
40
|
+
task: number;
|
|
41
|
+
owner: PeerId;
|
|
42
|
+
gen: number;
|
|
43
|
+
revision: number;
|
|
44
|
+
/** The files as they were at the last request: the next done is accepted only for this target. */
|
|
45
|
+
tree: string;
|
|
46
|
+
requests: number;
|
|
47
|
+
/** When the last request went out: a done right after it is a retry, not a confirmation. */
|
|
48
|
+
at: number;
|
|
49
|
+
/** The fact offer that went out with the last request: the next done acknowledges it. */
|
|
50
|
+
offer?: string;
|
|
51
|
+
confirmed?: boolean;
|
|
52
|
+
/** The outcome was recorded as unresolved: this revision asks nothing more, and the member's check counts as usual. */
|
|
53
|
+
closed?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface Cohort {
|
|
57
|
+
id: number;
|
|
58
|
+
/** Bumps on every change of membership or owner, when an intent is withdrawn, and when the cohort is lifted. */
|
|
59
|
+
revision: number;
|
|
60
|
+
members: Map<number, Member>;
|
|
61
|
+
silent: boolean;
|
|
62
|
+
intents: Map<number, { gen: number; at: number }>;
|
|
63
|
+
integration?: Integration;
|
|
64
|
+
/** Members whose completed-change notice the silence withheld: what replaces it if no integration runs. */
|
|
65
|
+
held: Set<number>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface CohortDeps {
|
|
69
|
+
/** Whether these owners may work without messages: turn-free on, no PII, every owner's context path verified. */
|
|
70
|
+
silence: (owners: PeerId[]) => boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Whether `peer` is between native turns now (Codex not busy; Claude's last tool call started before its last Stop).
|
|
73
|
+
* Evidence is a native turn end, never a task approval or a delivery acknowledgement.
|
|
74
|
+
*/
|
|
75
|
+
idle: (peer: PeerId) => boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type Completion =
|
|
79
|
+
| { action: "proceed"; integrated?: boolean }
|
|
80
|
+
| { action: "request"; why: string; requests: number; cohort: Cohort }
|
|
81
|
+
| { action: "unresolved"; why: string; cohort: Cohort };
|
|
82
|
+
|
|
83
|
+
export class Cohorts {
|
|
84
|
+
private readonly live: Cohort[] = [];
|
|
85
|
+
private next = 1;
|
|
86
|
+
|
|
87
|
+
constructor(private readonly d: CohortDeps) {}
|
|
88
|
+
|
|
89
|
+
/** The live cohort `task` is in. A cohort is over the moment its members have all finished, whoever asks. */
|
|
90
|
+
of(task: number): Cohort | undefined {
|
|
91
|
+
this.gc();
|
|
92
|
+
return this.live.find((c) => c.members.has(task));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
list(): Cohort[] {
|
|
96
|
+
this.gc();
|
|
97
|
+
return [...this.live];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* `task` overlaps `others` (open tasks of other owners): from now on they are one cohort. Merges cohorts the tasks were
|
|
102
|
+
* in, records owner changes (each bumps the revision and voids that member's intent), and lifts a silent cohort that
|
|
103
|
+
* an owner without verified facts joins. Returns what happened, for the notices.
|
|
104
|
+
*/
|
|
105
|
+
join(task: Task, others: Task[], gen: (t: Task) => number, handed: (t: Task) => number = () => Date.now()): { cohort: Cohort; formed: boolean; lifted: boolean } | undefined {
|
|
106
|
+
this.gc();
|
|
107
|
+
const all = [task, ...others].filter((t) => t.owner);
|
|
108
|
+
const found = [...new Set(all.map((t) => this.of(t.id)).filter((c): c is Cohort => !!c))];
|
|
109
|
+
if (!found.length && all.length < 2) return undefined;
|
|
110
|
+
const wasSilent = found.some((c) => c.silent);
|
|
111
|
+
let cohort = found[0];
|
|
112
|
+
const formed = !cohort;
|
|
113
|
+
if (!cohort) {
|
|
114
|
+
cohort = { id: this.next++, revision: 0, members: new Map(), silent: false, intents: new Map(), held: new Set() };
|
|
115
|
+
this.live.push(cohort);
|
|
116
|
+
}
|
|
117
|
+
let changed = formed;
|
|
118
|
+
for (const other of found.slice(1)) {
|
|
119
|
+
for (const [id, m] of other.members) cohort.members.set(id, m);
|
|
120
|
+
for (const [id, i] of other.intents) cohort.intents.set(id, i);
|
|
121
|
+
for (const id of other.held) cohort.held.add(id);
|
|
122
|
+
cohort.silent &&= other.silent;
|
|
123
|
+
this.live.splice(this.live.indexOf(other), 1);
|
|
124
|
+
changed = true;
|
|
125
|
+
}
|
|
126
|
+
for (const t of all) {
|
|
127
|
+
const m = cohort.members.get(t.id);
|
|
128
|
+
const g = gen(t);
|
|
129
|
+
if (m && m.owner === t.owner && m.gen === g) continue;
|
|
130
|
+
cohort.members.set(t.id, { task: t.id, owner: t.owner!, gen: g, since: handed(t) });
|
|
131
|
+
cohort.intents.delete(t.id);
|
|
132
|
+
changed = true;
|
|
133
|
+
}
|
|
134
|
+
if (changed) cohort.revision++;
|
|
135
|
+
const owners = [...new Set([...cohort.members.values()].map((m) => m.owner))];
|
|
136
|
+
if (formed) cohort.silent = this.d.silence(owners);
|
|
137
|
+
else if (cohort.silent && changed && !this.d.silence(owners)) cohort.silent = false;
|
|
138
|
+
// Lifted: a silent cohort (or a silent one merged into this) is no longer silent, and its members must hear it.
|
|
139
|
+
return { cohort, formed, lifted: wasSilent && !cohort.silent };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** An owner lost its context path: every silent cohort it is in speaks again. */
|
|
143
|
+
lift(peer: PeerId): Cohort[] {
|
|
144
|
+
return this.liftWhere((c) => [...c.members.values()].some((m) => m.owner === peer));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A PII task opened: every silent cohort speaks again, at once (issue #108). */
|
|
148
|
+
liftAll(): Cohort[] {
|
|
149
|
+
return this.liftWhere(() => true);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private liftWhere(pick: (c: Cohort) => boolean): Cohort[] {
|
|
153
|
+
this.gc(); // a finished cohort is not lifted: a peer leaving after the work (a benchmark's teardown) changes nothing
|
|
154
|
+
const out = this.live.filter((c) => c.silent && pick(c));
|
|
155
|
+
for (const c of out) {
|
|
156
|
+
c.silent = false;
|
|
157
|
+
c.revision++;
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The silent cohort that makes a message from `from` to `to` cohort coordination: `from` is a member that has not
|
|
164
|
+
* settled since its task closed, and `to` is a member too. A settled member works on something else now.
|
|
165
|
+
*/
|
|
166
|
+
silenced(from: PeerId, to: PeerId): Cohort | undefined {
|
|
167
|
+
this.gc();
|
|
168
|
+
return this.live.find((c) => {
|
|
169
|
+
if (!c.silent) return false;
|
|
170
|
+
const members = [...c.members.values()];
|
|
171
|
+
return members.some((m) => m.owner === from && m.settledAt === undefined) && members.some((m) => m.owner === to);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The task left the open states for its owner (done, approved, in review); an idle owner settles at once. */
|
|
176
|
+
closed(task: number, at = Date.now()): void {
|
|
177
|
+
const m = this.of(task)?.members.get(task);
|
|
178
|
+
if (!m) return;
|
|
179
|
+
m.closedAt ??= at;
|
|
180
|
+
if (m.settledAt === undefined && this.d.idle(m.owner)) m.settledAt = at;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** `peer`'s native turn ended: its members whose tasks closed before settle, and those with an intent stop, for good. */
|
|
184
|
+
turnEnded(peer: PeerId, at = Date.now()): void {
|
|
185
|
+
for (const c of this.live) {
|
|
186
|
+
for (const m of c.members.values()) {
|
|
187
|
+
if (m.owner !== peer) continue;
|
|
188
|
+
if (m.closedAt !== undefined && m.closedAt <= at && m.settledAt === undefined) m.settledAt = at;
|
|
189
|
+
const intent = c.intents.get(m.task);
|
|
190
|
+
if (intent && intent.gen === m.gen && intent.at <= at && m.stoppedAt === undefined) m.stoppedAt = at;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The task is open again for its owner (a failed check, changes requested, a reopen): its intent is void, and while
|
|
197
|
+
* its cohort is live it is that cohort's work again. A cohort that is over stays over.
|
|
198
|
+
*/
|
|
199
|
+
withdraw(task: number): void {
|
|
200
|
+
const c = this.of(task);
|
|
201
|
+
if (!c) return;
|
|
202
|
+
const m = c.members.get(task);
|
|
203
|
+
if (m) {
|
|
204
|
+
delete m.closedAt;
|
|
205
|
+
delete m.settledAt;
|
|
206
|
+
delete m.stoppedAt;
|
|
207
|
+
}
|
|
208
|
+
c.intents.delete(task);
|
|
209
|
+
c.revision++;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A done of `task` that selects nobody and asks nothing: the console finishing a member (issue #107). It still counts
|
|
214
|
+
* as that member's intent, so the member whose done completes the set integrates.
|
|
215
|
+
*/
|
|
216
|
+
intent(c: Cohort, task: Task, gen: number): void {
|
|
217
|
+
if (c.intents.has(task.id) && c.intents.get(task.id)!.gen === gen) return;
|
|
218
|
+
c.intents.set(task.id, { gen, at: Date.now() });
|
|
219
|
+
const m = c.members.get(task.id);
|
|
220
|
+
if (!m) return;
|
|
221
|
+
// An owner between turns (the console finished the task, say) has stopped already; one in a turn stops when it ends.
|
|
222
|
+
if (this.d.idle(m.owner)) m.stoppedAt = Date.now();
|
|
223
|
+
else delete m.stoppedAt;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Whether this done of the integrating member is a retry of a lost answer: then nothing counts or is acknowledged. */
|
|
227
|
+
isRetry(c: Cohort, task: Task, gen: number): boolean {
|
|
228
|
+
const ig = c.integration;
|
|
229
|
+
return !!ig && ig.task === task.id && ig.gen === gen && ig.owner === task.owner && ig.revision === c.revision && !ig.confirmed && Date.now() - ig.at < RETRY_MS;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* `task`'s owner called done in silent cohort `c`. Records its intent; then either it proceeds (others are still at
|
|
234
|
+
* work, another member integrates this revision, or its integration is confirmed), or it is asked to integrate (first
|
|
235
|
+
* request, or the target moved), or, past MAX_REQUESTS, the outcome is unresolved.
|
|
236
|
+
*/
|
|
237
|
+
completion(c: Cohort, task: Task, now: { gen: number; tree: string; factsCurrent: boolean; handed?: number }): Completion {
|
|
238
|
+
const m = c.members.get(task.id);
|
|
239
|
+
if (!m || m.owner !== task.owner || m.gen !== now.gen) {
|
|
240
|
+
c.members.set(task.id, { task: task.id, owner: task.owner!, gen: now.gen, since: now.handed ?? Date.now() });
|
|
241
|
+
c.revision++;
|
|
242
|
+
}
|
|
243
|
+
this.intent(c, task, now.gen);
|
|
244
|
+
const missing = [...c.members.values()].filter((x) => c.intents.get(x.task)?.gen !== x.gen);
|
|
245
|
+
if (missing.length) return { action: "proceed" };
|
|
246
|
+
const ig = c.integration;
|
|
247
|
+
if (ig && ig.revision === c.revision && ig.task !== task.id) return { action: "proceed" }; // another member integrates
|
|
248
|
+
if (!ig || ig.revision !== c.revision || ig.task !== task.id || ig.gen !== now.gen || ig.owner !== task.owner) {
|
|
249
|
+
c.integration = { task: task.id, owner: task.owner!, gen: now.gen, revision: c.revision, tree: now.tree, requests: 1, at: Date.now() };
|
|
250
|
+
return { action: "request", why: "", requests: 1, cohort: c };
|
|
251
|
+
}
|
|
252
|
+
if (ig.closed) return { action: "proceed" }; // recorded as unresolved: nothing more is asked of this revision
|
|
253
|
+
// Its next done: accepted only for the same target, once every other member has settled. A later turn of a settled
|
|
254
|
+
// member that touches these files moves the target, which the file hash catches.
|
|
255
|
+
// Other owners only: the integrating owner's own other tasks in the cohort stop with this very turn.
|
|
256
|
+
const running = [...c.members.values()].filter((x) => x.owner !== task.owner && x.stoppedAt === undefined).map((x) => x.owner);
|
|
257
|
+
let why = "";
|
|
258
|
+
if (running.length) why = `${[...new Set(running)].join(", ")} has not stopped since its done, so its changes may not be final`;
|
|
259
|
+
else if (now.tree !== ig.tree) why = "the files changed since the last request";
|
|
260
|
+
else if (!now.factsCurrent) why = "there are changes you have not been shown yet";
|
|
261
|
+
if (!why) {
|
|
262
|
+
ig.confirmed = true;
|
|
263
|
+
return { action: "proceed", integrated: true };
|
|
264
|
+
}
|
|
265
|
+
if (ig.requests >= MAX_REQUESTS) {
|
|
266
|
+
ig.closed = true;
|
|
267
|
+
return { action: "unresolved", why, cohort: c };
|
|
268
|
+
}
|
|
269
|
+
ig.requests++;
|
|
270
|
+
ig.tree = now.tree;
|
|
271
|
+
ig.at = Date.now();
|
|
272
|
+
delete ig.offer;
|
|
273
|
+
delete ig.confirmed; // asked again: a done right after this is a retry, and only a later one can confirm
|
|
274
|
+
return { action: "request", why, requests: ig.requests, cohort: c };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Whether a check that passed for `task` counts: always, unless `task` is the confirmed integrating member of its
|
|
279
|
+
* cohort's current revision, whose check counts only for the target it confirmed. A member that stopped being the
|
|
280
|
+
* last to finish (the revision moved) is an earlier finisher again, and its own check counts.
|
|
281
|
+
*/
|
|
282
|
+
holds(task: number, gen: number, tree: string): boolean {
|
|
283
|
+
const c = this.of(task);
|
|
284
|
+
const ig = c?.integration;
|
|
285
|
+
if (!c || !c.silent || !ig || ig.task !== task || ig.revision !== c.revision || ig.closed) return true;
|
|
286
|
+
return !!ig.confirmed && ig.gen === gen && ig.tree === tree;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** The task lost its owner (released, unassigned): it leaves the cohort, and the revision moves on. */
|
|
290
|
+
leave(task: number): void {
|
|
291
|
+
const c = this.of(task);
|
|
292
|
+
if (!c) return;
|
|
293
|
+
c.members.delete(task);
|
|
294
|
+
c.intents.delete(task);
|
|
295
|
+
c.revision++;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* A cohort is over once every member closed its task and settled (silent), or closed it (not silent: nothing is held
|
|
300
|
+
* back, so nothing waits for a turn end).
|
|
301
|
+
*/
|
|
302
|
+
private gc(): void {
|
|
303
|
+
for (const c of [...this.live]) {
|
|
304
|
+
const members = [...c.members.values()];
|
|
305
|
+
if (members.every((m) => (c.silent ? m.settledAt !== undefined : m.closedAt !== undefined))) this.live.splice(this.live.indexOf(c), 1);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -6,10 +6,10 @@ export function stateDirFor(cwd: string): string {
|
|
|
6
6
|
return projectContext(cwd).stateDir;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
/** Control WS wire version. 2 = `deliver` carries `envs` (digests); 3 = `tools` role and task messages; 4 = budget messages and `hub_checkpoint`; 5 = `ask`; 6 = console-only `ui` session bootstrap; 8 = controlled recovery; 9 = Pi bridge metadata; 10 = durable delivery receipts; 11 = queue hold diagnostics. The plugin is installed apart from the daemon, so they can drift. */
|
|
10
|
-
export const PROTOCOL =
|
|
9
|
+
/** Control WS wire version. 2 = `deliver` carries `envs` (digests); 3 = `tools` role and task messages; 4 = budget messages and `hub_checkpoint`; 5 = `ask`; 6 = console-only `ui` session bootstrap; 8 = controlled recovery; 9 = Pi bridge metadata; 10 = durable delivery receipts; 11 = queue hold diagnostics; 12 = generation-bound channel settlement and execution budgets; 13 = turn-free facts (`facts` with tool, session and transcript binding, `silenced`) and per-recipient send results (issue #108). The plugin is installed apart from the daemon, so they can drift. */
|
|
10
|
+
export const PROTOCOL = 13;
|
|
11
11
|
/** Protocols a current coordinator may authenticate while upgrading a running source. */
|
|
12
|
-
export const RECOVERY_SOURCE_PROTOCOLS = [9, 10, PROTOCOL] as const;
|
|
12
|
+
export const RECOVERY_SOURCE_PROTOCOLS = [9, 10, 11, 12, PROTOCOL] as const;
|
|
13
13
|
|
|
14
14
|
export interface Hello {
|
|
15
15
|
/** `tools`: acts for `peer` (task tools, hub_send) without being a delivery target: the MCP server Kimi and Codex run. */
|