operon-agents-peers 0.1.0-alpha.0
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/LICENSE +55 -0
- package/README.md +191 -0
- package/dist/define-tool.d.ts +16 -0
- package/dist/define-tool.js +17 -0
- package/dist/directory.d.ts +148 -0
- package/dist/directory.js +148 -0
- package/dist/file-repo.d.ts +3 -0
- package/dist/file-repo.js +119 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +33 -0
- package/dist/mailbox.d.ts +64 -0
- package/dist/mailbox.js +32 -0
- package/dist/member-tool.d.ts +4 -0
- package/dist/member-tool.js +97 -0
- package/dist/network.d.ts +320 -0
- package/dist/network.js +526 -0
- package/dist/repo.d.ts +30 -0
- package/dist/repo.js +27 -0
- package/dist/stats.d.ts +80 -0
- package/dist/stats.js +80 -0
- package/dist/team-tool.d.ts +4 -0
- package/dist/team-tool.js +139 -0
- package/package.json +41 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The coordinator: an explicit roster plus routing on top of `steerTo`.
|
|
3
|
+
*
|
|
4
|
+
* Nothing here is in the engine. The whole thing rests on four public seams:
|
|
5
|
+
* - `api.onEvent` — root-frame lifecycle says who is awake, asleep, or gone
|
|
6
|
+
* - `api.registerTool` — the `Team` tool (creators) and the `Hub` tool (members)
|
|
7
|
+
* - `actions.openSession` — reach another session (reopening it if it was closed)
|
|
8
|
+
* - `handle.steerTo(address, …)` — hand a message to one specific frame
|
|
9
|
+
*
|
|
10
|
+
* TWO ROLES, TWO TOOLS, both fixed at birth (a toolset never changes mid-conversation):
|
|
11
|
+
*
|
|
12
|
+
* - An ordinary agent gets the `Team` tool: it can FORM a team and SPAWN teammates, and its
|
|
13
|
+
* `send` reaches only members of teams it created (scope IS the permission — no visibility
|
|
14
|
+
* policy involved). It has no general peer messaging, and it is not on the roster until the
|
|
15
|
+
* moment it creates a team.
|
|
16
|
+
* - A teammate is a SESSION created through the host's `spawnable` factory, born with the
|
|
17
|
+
* member `Hub` tool (send/list/inbox, gated by the `visibility` policy). Members are the only
|
|
18
|
+
* agents with general send.
|
|
19
|
+
*
|
|
20
|
+
* Identity and capability are therefore produced by the same act: a member's roster row is
|
|
21
|
+
* written at spawn, a creator's at `Team create`. Nothing is inferred from event timing, and
|
|
22
|
+
* subagents (delegations) never enter the peer world at all — `observe` only refreshes the
|
|
23
|
+
* status of rows that explicit acts created.
|
|
24
|
+
*
|
|
25
|
+
* This does not belong in core; see the README for the reasoning and for how
|
|
26
|
+
* this shape was reached.
|
|
27
|
+
*/
|
|
28
|
+
import type { ExtensionAPI, ExtensionActions, ExtensionDefinition, ExtensionHostContext } from "operon-agents";
|
|
29
|
+
import type { AgentEvent } from "operon-agents-core";
|
|
30
|
+
import { type AgentDirectory, type AgentListFilter, type AgentRef, type Visibility } from "./directory.ts";
|
|
31
|
+
import type { PeerLimits, PeerMessage } from "./mailbox.ts";
|
|
32
|
+
import { type PeerRepo } from "./repo.ts";
|
|
33
|
+
import { type PeerBudget, type PeerFleetStats } from "./stats.ts";
|
|
34
|
+
/** Marks a peer message on the wire and in the recipient's journal. `deliveryId` is the reconcile
|
|
35
|
+
* anchor — a restart matches it against the recipient's log to tell delivered from lost. */
|
|
36
|
+
export declare const PEER_SOURCE = "peer";
|
|
37
|
+
/** What routing reports back. `send` never blocks, so this describes DELIVERY, not a reply. */
|
|
38
|
+
export interface PeerReceipt {
|
|
39
|
+
readonly messageId: string;
|
|
40
|
+
readonly to: string;
|
|
41
|
+
readonly status: "delivered" | "failed";
|
|
42
|
+
/** Machine-readable cause, so a model can tell "wrong id" from "you sent too much" and stop
|
|
43
|
+
* retrying what cannot succeed. */
|
|
44
|
+
readonly reason?: "unknown_agent" | "not_visible" | "self_send" | "quota_exceeded" | "mailbox_full" | "unreachable";
|
|
45
|
+
readonly detail?: string;
|
|
46
|
+
}
|
|
47
|
+
/** What `Team spawn` hands a teammate factory. The `peers()` extension's own factory creates the
|
|
48
|
+
* session tagged `params: { peers: { member } }`, so the member is born with its Hub and its
|
|
49
|
+
* roster identity — the host never wires this. */
|
|
50
|
+
export interface TeammateSpawnRequest {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly type: string;
|
|
53
|
+
/** Full team label (`team:<creator>:<name>`). */
|
|
54
|
+
readonly team: string;
|
|
55
|
+
readonly creatorId: string;
|
|
56
|
+
}
|
|
57
|
+
export type TeammateFactory = (request: TeammateSpawnRequest) => Promise<{
|
|
58
|
+
readonly id: string;
|
|
59
|
+
}>;
|
|
60
|
+
export interface PeerNetworkOptions {
|
|
61
|
+
/** Required — there is no permissive default. Gates MEMBER messaging (`Hub send`/`list`).
|
|
62
|
+
* `Team send` is not gated by it: a creator's reach is exactly the members it spawned. */
|
|
63
|
+
readonly visibility: Visibility;
|
|
64
|
+
readonly limits?: PeerLimits;
|
|
65
|
+
/**
|
|
66
|
+
* Where peer state lives — the host's choice of backend (memory default, `createFilePeerRepo`,
|
|
67
|
+
* or its own PG/Redis implementation). See `PeerRepo` for what each facet persists and the
|
|
68
|
+
* atomicity contract implementations must honor.
|
|
69
|
+
*/
|
|
70
|
+
readonly repo?: PeerRepo;
|
|
71
|
+
/**
|
|
72
|
+
* Spend ceiling across the whole network. Reaching it stops PEER traffic; it does not (and
|
|
73
|
+
* cannot) stop an agent already running from finishing its own work — `maxTurns` owns that.
|
|
74
|
+
*/
|
|
75
|
+
readonly budget?: PeerBudget;
|
|
76
|
+
/**
|
|
77
|
+
* Teammate types the model may create with `Team spawn` — the parameter boundary: the HOST
|
|
78
|
+
* defines what a teammate of each type is (its session config, permissions, model), the model
|
|
79
|
+
* only picks a type and a name. Absent → the spawn op reports nothing is spawnable.
|
|
80
|
+
*/
|
|
81
|
+
readonly spawnable?: Readonly<Record<string, TeammateFactory>>;
|
|
82
|
+
}
|
|
83
|
+
/** Options for the creator-side extension. No `labels`: an ordinary agent has no peer identity
|
|
84
|
+
* until it forms a team, and membership is never granted by configuration here — a member is
|
|
85
|
+
* made by a session's `params.member`, which the spawn factory sets. */
|
|
86
|
+
export interface PeerExtensionOptions {
|
|
87
|
+
/** What this agent IS if it steps onto the roster by creating a team — `"lead"`, `"orchestrator"`. */
|
|
88
|
+
readonly type?: string;
|
|
89
|
+
/** One line on what this agent is for. Shown on the roster next to `type`. */
|
|
90
|
+
readonly description?: string;
|
|
91
|
+
}
|
|
92
|
+
/** Options for the member-side extension. `name` is the agent's peer identity (what teammates
|
|
93
|
+
* address), `team` the full label it belongs to. A host can also attach this directly to
|
|
94
|
+
* pre-arrange a standing team without any model-driven spawn. */
|
|
95
|
+
export interface PeerMemberOptions {
|
|
96
|
+
readonly name: string;
|
|
97
|
+
/** Full team label. Host-arranged teams pick any stable string (e.g. `team:host:alpha`);
|
|
98
|
+
* spawn-created members receive the creator-stamped label via `TeammateSpawnRequest`. */
|
|
99
|
+
readonly team: string;
|
|
100
|
+
readonly type?: string;
|
|
101
|
+
readonly description?: string;
|
|
102
|
+
}
|
|
103
|
+
/** Who is calling a Team op: resolved per call from the frame address, because the Team tool is
|
|
104
|
+
* shared by every frame of its session — the root agent IS the session for addressing purposes,
|
|
105
|
+
* a subagent frame is its own (memory-only) identity. */
|
|
106
|
+
export interface TeamCaller {
|
|
107
|
+
readonly agentId: string;
|
|
108
|
+
readonly sessionId: string;
|
|
109
|
+
readonly address: string;
|
|
110
|
+
readonly type?: string;
|
|
111
|
+
readonly description?: string;
|
|
112
|
+
}
|
|
113
|
+
export declare class PeerNetwork {
|
|
114
|
+
readonly directory: AgentDirectory;
|
|
115
|
+
private readonly mailbox;
|
|
116
|
+
private readonly visibility;
|
|
117
|
+
private readonly limits;
|
|
118
|
+
private readonly spawnable;
|
|
119
|
+
/** Any session's actions will do — `openSession` reaches every session on the harness. */
|
|
120
|
+
private actions;
|
|
121
|
+
/** Per-turn outbound counters, keyed by "<agentId>:<turnId>" so they self-expire. */
|
|
122
|
+
private readonly outbound;
|
|
123
|
+
private readonly budget;
|
|
124
|
+
private readonly statistics;
|
|
125
|
+
private readonly usageDiff;
|
|
126
|
+
/**
|
|
127
|
+
* Roster seeding from the card store, kicked off at construction. Every public entry point
|
|
128
|
+
* awaits it, so a durable repo's parked teammates are discoverable before the first route —
|
|
129
|
+
* without forcing hosts through a separate init call.
|
|
130
|
+
*/
|
|
131
|
+
private readonly ready;
|
|
132
|
+
/** Deliveries handed to a recipient that was not running — each one starts a turn. Consumed
|
|
133
|
+
* when the message surfaces in the recipient's conversation. */
|
|
134
|
+
private readonly wokenBy;
|
|
135
|
+
constructor(options: PeerNetworkOptions);
|
|
136
|
+
/** What the fleet has spent, per agent and in total. The view a host needs to notice a runaway
|
|
137
|
+
* team before the bill does. */
|
|
138
|
+
stats(): Promise<PeerFleetStats>;
|
|
139
|
+
/** Idempotent: the first session to mount wins — any session's actions reach every session
|
|
140
|
+
* (`openSession` spans the harness). Mounts also re-arm this on every observed event, which
|
|
141
|
+
* is what re-connects a REPLACEMENT instance (whose own `actions` starts empty) without any
|
|
142
|
+
* session being touched. */
|
|
143
|
+
attachActions(actions: ExtensionActions): void;
|
|
144
|
+
/** Roster lookup in method form — field access (`.directory`) does not survive a service
|
|
145
|
+
* handle, which exposes methods only. Host-side code may keep using `.directory`. */
|
|
146
|
+
getAgent(agentId: string): Promise<AgentRef | undefined>;
|
|
147
|
+
/** Mark an agent parked (its session closed / its member session ended). */
|
|
148
|
+
parkAgent(agentId: string): Promise<void>;
|
|
149
|
+
/** Member identity registration at session start — what `mountHub` calls from `session.start`. */
|
|
150
|
+
registerMemberSession(options: PeerMemberOptions, sessionId: string): Promise<void>;
|
|
151
|
+
/**
|
|
152
|
+
* Detach from the harness and drop in-memory coordination state. This is the replace/unload
|
|
153
|
+
* dispose hook: calls already in flight (leased on this instance) finish normally; later
|
|
154
|
+
* deliveries fail with the existing "unreachable" receipt. The repo behind cards/mailbox/stats
|
|
155
|
+
* is HOST-owned and deliberately left open — a successor instance re-seeds from it.
|
|
156
|
+
*/
|
|
157
|
+
close(): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Refresh the STATUS of rows explicit acts created — nothing more. Identity never comes from
|
|
160
|
+
* here: members are registered at spawn (or member session start), creators at `Team create`.
|
|
161
|
+
* A frame the roster does not know (every ordinary delegation) falls through `setStatus`'s
|
|
162
|
+
* no-op, which is exactly the point.
|
|
163
|
+
*/
|
|
164
|
+
observe(event: AgentEvent, rosterId: string | undefined): void;
|
|
165
|
+
list(filter?: AgentListFilter): Promise<readonly AgentRef[]>;
|
|
166
|
+
/** Peers `selfId` may see, excluding itself. Backs the member `Hub list`. */
|
|
167
|
+
visiblePeers(selfId: string): Promise<readonly AgentRef[]>;
|
|
168
|
+
/** Members of teams `selfId` created. Backs `Team list` and `Team send`'s scope. */
|
|
169
|
+
ownedMembers(selfId: string): Promise<readonly AgentRef[]>;
|
|
170
|
+
/**
|
|
171
|
+
* Form a team. The one act that puts a CREATOR on the roster — its identity and its team are
|
|
172
|
+
* produced together, which is why "holding the Team tool but unknown to the roster" is not a
|
|
173
|
+
* reachable state for anything that matters: every op that needs an identity either creates it
|
|
174
|
+
* (here) or requires a team to exist (everywhere else).
|
|
175
|
+
*
|
|
176
|
+
* Membership cannot be extended sideways: the only agents that ever wear the label are ones
|
|
177
|
+
* `Team spawn` creates under it. So a team can never widen what its creator could already
|
|
178
|
+
* reach — no agent can pull a stranger in, or push itself into someone else's team.
|
|
179
|
+
*/
|
|
180
|
+
createTeam(caller: TeamCaller, name: string): Promise<{
|
|
181
|
+
readonly label: string;
|
|
182
|
+
} | {
|
|
183
|
+
readonly error: string;
|
|
184
|
+
}>;
|
|
185
|
+
/**
|
|
186
|
+
* Create one teammate: resolve the host's factory for `type`, let it build the session (which
|
|
187
|
+
* tags it `params: { peers: { member } }`, so identity + Hub arrive with birth), then hand the initial
|
|
188
|
+
* prompt over as the first peer message — the write-ahead ledger covers the crash window
|
|
189
|
+
* between session creation and the prompt entering its conversation.
|
|
190
|
+
*/
|
|
191
|
+
spawnTeammate(creatorId: string, args: {
|
|
192
|
+
readonly type: string;
|
|
193
|
+
readonly name: string;
|
|
194
|
+
readonly prompt: string;
|
|
195
|
+
readonly team?: string;
|
|
196
|
+
}): Promise<{
|
|
197
|
+
readonly name: string;
|
|
198
|
+
readonly sessionId: string;
|
|
199
|
+
readonly team: string;
|
|
200
|
+
readonly receipt: PeerReceipt;
|
|
201
|
+
} | {
|
|
202
|
+
readonly error: string;
|
|
203
|
+
}>;
|
|
204
|
+
/** Charge one outbound message against the sender's per-turn budget. */
|
|
205
|
+
overBudget(selfId: string, turnId: string): boolean;
|
|
206
|
+
/**
|
|
207
|
+
* MEMBER send: the general peer path, gated by the `visibility` policy. The single enforcement
|
|
208
|
+
* point for the boundary — a member may only address a peer its policy admits.
|
|
209
|
+
*/
|
|
210
|
+
route(from: AgentRef, toId: string, content: string, opts?: {
|
|
211
|
+
readonly replyTo?: string;
|
|
212
|
+
readonly interrupt?: boolean;
|
|
213
|
+
}): Promise<PeerReceipt>;
|
|
214
|
+
/**
|
|
215
|
+
* CREATOR send: scope replaces policy — a creator reaches exactly the members wearing one of
|
|
216
|
+
* its own team labels, and nothing else. No visibility function is consulted.
|
|
217
|
+
*/
|
|
218
|
+
sendWithinTeam(fromId: string, toId: string, content: string, opts?: {
|
|
219
|
+
readonly replyTo?: string;
|
|
220
|
+
readonly interrupt?: boolean;
|
|
221
|
+
}): Promise<PeerReceipt>;
|
|
222
|
+
/** The shared tail of every send: budget → write-ahead ledger (capacity enforced atomically
|
|
223
|
+
* inside) → delivery. Callers have already established WHO may talk to WHOM. */
|
|
224
|
+
private dispatch;
|
|
225
|
+
/**
|
|
226
|
+
* Hand an already-ledgered message to its recipient. Split out from the send paths so a
|
|
227
|
+
* redelivery (see `reconcile`) does not re-run the checks or write a second ledger entry.
|
|
228
|
+
*/
|
|
229
|
+
private deliver;
|
|
230
|
+
/**
|
|
231
|
+
* Redeliver everything the ledger still holds. Call once on startup, after at least one session
|
|
232
|
+
* with the extension is attached (that is what connects the network to a harness). Recipients
|
|
233
|
+
* themselves need not be open: a card-seeded parked teammate is found on the roster and delivery
|
|
234
|
+
* reopens its session.
|
|
235
|
+
*
|
|
236
|
+
* The ledger only ever contains messages whose recipient never took them into its conversation
|
|
237
|
+
* (entries clear on `message.appended`), so anything left after a restart was genuinely lost in
|
|
238
|
+
* the crash window — no journal comparison needed to tell that apart.
|
|
239
|
+
*
|
|
240
|
+
* A message whose recipient is no longer on the roster is dropped rather than retried forever;
|
|
241
|
+
* `dropped` is reported so a host can log it instead of discovering the gap later.
|
|
242
|
+
*/
|
|
243
|
+
reconcile(): Promise<{
|
|
244
|
+
readonly redelivered: number;
|
|
245
|
+
readonly dropped: number;
|
|
246
|
+
}>;
|
|
247
|
+
/** Undelivered ledger entries for one agent — what was routed but never reached its
|
|
248
|
+
* conversation (the crash window). */
|
|
249
|
+
drainInbox(agentId: string): Promise<readonly PeerMessage[]>;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* The METHOD-ONLY surface the session-side mounts and tools consume. Both the live instance
|
|
253
|
+
* and a `ServiceRegistry` handle satisfy it — a handle exposes methods only, which is why
|
|
254
|
+
* nothing here is a field (`.directory` stays host-side). Methods must not return objects
|
|
255
|
+
* that carry the instance (the one leak a handle cannot stop).
|
|
256
|
+
*/
|
|
257
|
+
export type PeerNetworkHandle = Pick<PeerNetwork, "attachActions" | "observe" | "parkAgent" | "registerMemberSession" | "getAgent" | "list" | "visiblePeers" | "ownedMembers" | "createTeam" | "spawnTeammate" | "overBudget" | "route" | "sendWithinTeam" | "drainInbox" | "stats" | "reconcile">;
|
|
258
|
+
/** The extension id, and so the service name its `create` half's network is registered under.
|
|
259
|
+
* A session's `setup` reaches the network as `ctx.shared`; the host reaches it with
|
|
260
|
+
* `harness.services.handle<PeerNetworkHandle>(PEERS_SERVICE)`. */
|
|
261
|
+
export declare const PEERS_SERVICE = "peers";
|
|
262
|
+
/** Mount the creator side (the `Team` tool) onto a session — what `peers().setup` does for an
|
|
263
|
+
* ordinary session. */
|
|
264
|
+
export declare function mountTeam(network: PeerNetworkHandle, api: ExtensionAPI, options?: PeerExtensionOptions): void;
|
|
265
|
+
/** Mount the member side (the `Hub` tool + identity registration) onto a session — what
|
|
266
|
+
* `peers().setup` does for a teammate. */
|
|
267
|
+
export declare function mountHub(network: PeerNetworkHandle, api: ExtensionAPI, options: PeerMemberOptions): void;
|
|
268
|
+
export declare function createPeerNetwork(options: PeerNetworkOptions): PeerNetwork;
|
|
269
|
+
/** What a teammate session is born as: the options `harness.createSession` takes. `peers`
|
|
270
|
+
* merges in the member's peer identity as `params.peers`, so the host never wires a mount. */
|
|
271
|
+
export type TeammateSessionOptions = {
|
|
272
|
+
readonly title?: string;
|
|
273
|
+
readonly extensions?: readonly ExtensionDefinition[];
|
|
274
|
+
readonly params?: Record<string, unknown>;
|
|
275
|
+
} & Record<string, unknown>;
|
|
276
|
+
/**
|
|
277
|
+
* The per-session argument to the `peers` extension (`createSession({ params: { peers } })`).
|
|
278
|
+
*
|
|
279
|
+
* - `member` present ⇒ this session is a teammate: it gets the member `Hub` with this identity,
|
|
280
|
+
* and NOT `Team` — a teammate is a member, it does not form teams of its own.
|
|
281
|
+
* - `member` absent ⇒ an ordinary session, which gets the creator `Team`.
|
|
282
|
+
*
|
|
283
|
+
* A spawn factory sets `member` automatically; a host sets it directly to pre-arrange a standing
|
|
284
|
+
* team. Pass `false` as a session's `peers` param (handled by the framework) to skip peers there.
|
|
285
|
+
*/
|
|
286
|
+
export interface PeerParams {
|
|
287
|
+
readonly member?: PeerMemberOptions;
|
|
288
|
+
}
|
|
289
|
+
export interface PeerOptions extends Omit<PeerNetworkOptions, "spawnable" | "repo"> {
|
|
290
|
+
/**
|
|
291
|
+
* Where peer state lives: a repo, or a factory given the `create` host — the file form builds
|
|
292
|
+
* its file repo in `host.dataDir` (outside the bundle, so an update keeps roster and mailbox).
|
|
293
|
+
* Absent → in-memory (wiped by a reload).
|
|
294
|
+
*/
|
|
295
|
+
readonly repo?: PeerRepo | ((host: ExtensionHostContext) => PeerRepo);
|
|
296
|
+
/**
|
|
297
|
+
* Teammate types the model may create with `Team spawn`, each as the session options a
|
|
298
|
+
* teammate of that type is born with (a constant, or computed from the spawn request). The
|
|
299
|
+
* parameter boundary: the HOST says what a `coder` IS, the model only picks a type and a name.
|
|
300
|
+
* Absent → the spawn op reports nothing is spawnable.
|
|
301
|
+
*/
|
|
302
|
+
readonly teammates?: Readonly<Record<string, TeammateSessionOptions | ((request: TeammateSpawnRequest) => TeammateSessionOptions | Promise<TeammateSessionOptions>)>>;
|
|
303
|
+
/** Creator options (`type`, `description`) for the `Team` an ordinary session is born holding. */
|
|
304
|
+
readonly team?: PeerExtensionOptions;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* The peers extension — ONE definition, both halves:
|
|
308
|
+
*
|
|
309
|
+
* - `create` (the process-shared half) runs once per harness and returns the shared
|
|
310
|
+
* `PeerNetwork`, which the framework registers as the `peers` service. Its spawn factory
|
|
311
|
+
* creates each teammate through `host.createSession`, tagging it `params: { peers: { member } }`.
|
|
312
|
+
* - `setup` (the per-session half) runs on every session: a teammate (has `params.member`) gets
|
|
313
|
+
* the member `Hub`; every other session gets the creator `Team`. It reaches the network only
|
|
314
|
+
* through the `shared` handle, so a reload swaps the network under live sessions.
|
|
315
|
+
*
|
|
316
|
+
* Server: `createHarness({ extensions: [peers({...})] })`. Desktop: a bundle in `extensionDir`
|
|
317
|
+
* whose default export is `peers({...})`. Same definition, only the channel differs.
|
|
318
|
+
*/
|
|
319
|
+
export declare function peers(options: PeerOptions): ExtensionDefinition<PeerNetworkHandle, PeerParams>;
|
|
320
|
+
//# sourceMappingURL=network.d.ts.map
|