operon-managed-agents 0.1.0-alpha.2 → 0.1.0-alpha.20
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/README.md +57 -1
- package/dist/client/client.d.ts +4 -2
- package/dist/client/client.js +4 -0
- package/dist/client/run.d.ts +1 -1
- package/dist/client/run.js +14 -5
- package/dist/protocol/types.d.ts +34 -6
- package/dist/server/http-server.d.ts +7 -1
- package/dist/server/http-server.js +12 -5
- package/dist/server/idempotency.d.ts +5 -5
- package/dist/server/inbox.d.ts +14 -16
- package/dist/server/inbox.js +26 -5
- package/dist/server/registries.d.ts +22 -6
- package/dist/server/request-schemas.d.ts +164 -0
- package/dist/server/request-schemas.js +86 -0
- package/dist/server/session-service.d.ts +17 -7
- package/dist/server/session-service.js +60 -99
- package/dist/server/session-worker.d.ts +19 -8
- package/dist/server/session-worker.js +116 -54
- package/dist/server/work.d.ts +2 -2
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -58,10 +58,13 @@ for await (const event of stream) {
|
|
|
58
58
|
// stream.lastEventId is the cursor to continue from elsewhere.
|
|
59
59
|
```
|
|
60
60
|
|
|
61
|
+
`sessions.update(id, { title })` renames a session (`PATCH /v1/sessions/{id}`); the title is the
|
|
62
|
+
only field a client can change after creation, and it is durable before the call resolves.
|
|
63
|
+
|
|
61
64
|
`run()` sends one input and resolves when the turn that took **that** delivery ends — not the
|
|
62
65
|
first `turn.ended` in the replay, which on a session with history belongs to an older turn.
|
|
63
66
|
|
|
64
|
-
The server accepts an `authorize(request, { action, sessionId })` hook. Configure it for any
|
|
67
|
+
The server accepts an `authorize(request, { action, sessionId, origin })` hook. Configure it for any
|
|
65
68
|
networked deployment; it can verify credentials and enforce per-session ownership. The
|
|
66
69
|
bundled in-memory idempotency store is atomic within one process and bounded by TTL/capacity;
|
|
67
70
|
distributed deployments should provide a database-backed `ManagedDeliveryIdempotencyStore`.
|
|
@@ -101,3 +104,56 @@ await client.sessions.messages.create(
|
|
|
101
104
|
{ idempotencyKey: requestId },
|
|
102
105
|
);
|
|
103
106
|
```
|
|
107
|
+
|
|
108
|
+
Whose words a delivery is, the caller says. The party holding a session's credential is its
|
|
109
|
+
user — the way a local session's stdin is — so by default `messages.create` delivers the
|
|
110
|
+
caller's own words and the model sees them bare, as a prompt. A caller relaying someone else's
|
|
111
|
+
words declares it:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
await client.sessions.messages.create(session.id, {
|
|
115
|
+
input: "deploy finished: 3 warnings",
|
|
116
|
+
origin: "external",
|
|
117
|
+
source: "ci",
|
|
118
|
+
actor: "deploy-bot",
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The model then sees the text inside an `<external-message>` envelope stamped as not from the
|
|
123
|
+
user — not an approval, not an answer to anything it asked. `source`, `actor` and `metadata`
|
|
124
|
+
describe the relay and are refused without the declaration. Declaring `external` lowers trust,
|
|
125
|
+
so it needs no permission of its own; the `authorize` hook receives `origin` on
|
|
126
|
+
`messages.create`, and a deployment that hands a credential to something that only relays (a
|
|
127
|
+
webhook bridge, a teammate agent) can refuse it anything else.
|
|
128
|
+
|
|
129
|
+
A worker serves many tenants by default, and refuses what would leak between them or run outside
|
|
130
|
+
the sandbox: a session whose environment does not come from the environment registry, a harness
|
|
131
|
+
holding process-wide MCP connections or an OAuth service, MCP over stdio, and a token read from
|
|
132
|
+
the process environment. A self-hosted, one-owner deployment says so —
|
|
133
|
+
`new SessionWorker({ profile: "single-tenant", … })` — and the worker logs that it did.
|
|
134
|
+
|
|
135
|
+
What a host decides per tenant goes through the registries, which are told whose session they
|
|
136
|
+
resolve for: `resolve(ref, { sessionId, ownerKey, metadata })`. `ownerKey` is stamped by the
|
|
137
|
+
service and trustworthy; `metadata` came from the client. Per-user MCP credentials — a vault —
|
|
138
|
+
are a provider the agent registry hands out, asked on every connect:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const agents: ManagedAgentRegistry = {
|
|
142
|
+
resolve: (_ref, { ownerKey }) => ({
|
|
143
|
+
resumeOptions: {
|
|
144
|
+
mcpServers: { linear: { transport: "http", url: "https://mcp.linear.app/mcp" } },
|
|
145
|
+
mcpCredentials: {
|
|
146
|
+
credentialsFor: async ({ url }) => {
|
|
147
|
+
const token = await vault.tokenFor(ownerKey!, url); // the host's store, refresh, lock
|
|
148
|
+
return token === undefined ? undefined : { headers: { Authorization: `Bearer ${token}` } };
|
|
149
|
+
},
|
|
150
|
+
invalidate: ({ url }) => vault.refresh(ownerKey!, url), // on a 401; the connect retries once
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
}),
|
|
154
|
+
};
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
The engine never stores, refreshes or logs a credential, and since the MCP client runs in the
|
|
158
|
+
worker, a credential never enters the sandbox. The rules and their reasons are in
|
|
159
|
+
`docs/server-profile.md`.
|
package/dist/client/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AgentEvent, InterruptAnswer } from "operon-agents";
|
|
2
2
|
import { type SseFrame } from "../protocol/sse.ts";
|
|
3
|
-
import type { CancelManagedSessionResponse, CreateManagedMessageRequest, CreateManagedSessionRequest,
|
|
3
|
+
import type { CancelManagedSessionResponse, CreateManagedMessageRequest, CreateManagedSessionRequest, MessageReceiptResource, InterruptionsResponse, ListSessionEventsOptions, ListSessionEventsResponse, ListManagedSessionsResponse, ManagedSession, ResumeManagedSessionResponse, StreamSessionEventsOptions, UpdateManagedSessionRequest } from "../protocol/types.ts";
|
|
4
4
|
export interface ManagedAgentsClientOptions {
|
|
5
5
|
readonly baseUrl: string;
|
|
6
6
|
readonly apiKey?: string;
|
|
@@ -48,8 +48,10 @@ export declare class ManagedSessionsClient {
|
|
|
48
48
|
create(request: CreateManagedSessionRequest): Promise<ManagedSession>;
|
|
49
49
|
list(): Promise<ListManagedSessionsResponse>;
|
|
50
50
|
retrieve(sessionId: string): Promise<ManagedSession>;
|
|
51
|
+
/** Rename a session; the new title is durable before this resolves. */
|
|
52
|
+
update(sessionId: string, request: UpdateManagedSessionRequest): Promise<ManagedSession>;
|
|
51
53
|
messages: {
|
|
52
|
-
create: (sessionId: string, request: CreateManagedMessageRequest, options?: CreateManagedMessageOptions) => Promise<
|
|
54
|
+
create: (sessionId: string, request: CreateManagedMessageRequest, options?: CreateManagedMessageOptions) => Promise<MessageReceiptResource>;
|
|
53
55
|
};
|
|
54
56
|
events: {
|
|
55
57
|
list: (sessionId: string, options?: ListSessionEventsOptions) => Promise<ListSessionEventsResponse>;
|
package/dist/client/client.js
CHANGED
|
@@ -129,6 +129,10 @@ export class ManagedSessionsClient {
|
|
|
129
129
|
retrieve(sessionId) {
|
|
130
130
|
return this.root.request(`/sessions/${encodeURIComponent(sessionId)}`);
|
|
131
131
|
}
|
|
132
|
+
/** Rename a session; the new title is durable before this resolves. */
|
|
133
|
+
update(sessionId, request) {
|
|
134
|
+
return this.root.request(`/sessions/${encodeURIComponent(sessionId)}`, { method: "PATCH", body: request });
|
|
135
|
+
}
|
|
132
136
|
messages = {
|
|
133
137
|
create: (sessionId, request, options = {}) => this.root.request(`/sessions/${encodeURIComponent(sessionId)}/messages`, {
|
|
134
138
|
method: "POST",
|
package/dist/client/run.d.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* the replay — and the ordering is still the only way to be sure of seeing it live.
|
|
11
11
|
* 2. The stream begins with history. On a session that has run before, the first `turn.ended`
|
|
12
12
|
* to arrive is an OLD one; ending on it returns the previous answer for the new question.
|
|
13
|
-
* Nothing counts until the turn that took our
|
|
13
|
+
* Nothing counts until the turn that took our message is known, and only that turn's end
|
|
14
14
|
* is an ending.
|
|
15
15
|
* 3. Idle is transient. A session goes idle between parallel tool executions and while waiting
|
|
16
16
|
* for an answer from you; breaking on the first idle ends the loop mid-conversation.
|
package/dist/client/run.js
CHANGED
|
@@ -32,9 +32,10 @@ export async function run(client, sessionId, input, options = {}) {
|
|
|
32
32
|
// Trap 1: open first, then send. Reversing these can drop everything in between.
|
|
33
33
|
const stream = await client.sessions.events.stream(sessionId, { signal: controller.signal });
|
|
34
34
|
iterator = stream[Symbol.asyncIterator]();
|
|
35
|
-
const {
|
|
36
|
-
// Trap 2: which turn is ours. Unknown until
|
|
37
|
-
//
|
|
35
|
+
const { steerId } = await client.sessions.messages.create(sessionId, request);
|
|
36
|
+
// Trap 2: which turn is ours. Unknown until our message shows up — as a `message.appended`
|
|
37
|
+
// carrying our steer id, either inside a turn already running (a steer) or just ahead of the
|
|
38
|
+
// turn that was woken to take it.
|
|
38
39
|
let currentTurn;
|
|
39
40
|
let ourTurn;
|
|
40
41
|
// After answering a pause, the continuation is ours whatever id it runs under.
|
|
@@ -60,14 +61,14 @@ export async function run(client, sessionId, input, options = {}) {
|
|
|
60
61
|
continue;
|
|
61
62
|
if (event.type === "turn.started") {
|
|
62
63
|
currentTurn = event.turnId;
|
|
63
|
-
if (claimNextTurn || (event.origin
|
|
64
|
+
if (claimNextTurn || steerOf(event.origin) === steerId) {
|
|
64
65
|
ourTurn = event.turnId;
|
|
65
66
|
claimNextTurn = false;
|
|
66
67
|
}
|
|
67
68
|
continue;
|
|
68
69
|
}
|
|
69
70
|
if (event.type === "message.appended") {
|
|
70
|
-
if (ourTurn === undefined && event.origin
|
|
71
|
+
if (ourTurn === undefined && steerOf(event.origin) === steerId) {
|
|
71
72
|
// Inside a turn: a steer, and that turn is ours. Between turns: the prompt was
|
|
72
73
|
// journaled ahead of its turn, so the next one to start is ours.
|
|
73
74
|
if (currentTurn !== undefined)
|
|
@@ -141,4 +142,12 @@ function textOf(content) {
|
|
|
141
142
|
.map((part) => part.text);
|
|
142
143
|
return parts.length === 0 ? undefined : parts.join("");
|
|
143
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* The steer a journaled prompt — or the turn it started — came in as: `origin.steerId`, whatever
|
|
147
|
+
* kind the origin is. Undefined for a message that never went through the bus (a reminder, a
|
|
148
|
+
* compaction summary): those answer no message of ours.
|
|
149
|
+
*/
|
|
150
|
+
function steerOf(origin) {
|
|
151
|
+
return origin !== undefined && "steerId" in origin ? origin.steerId : undefined;
|
|
152
|
+
}
|
|
144
153
|
//# sourceMappingURL=run.js.map
|
package/dist/protocol/types.d.ts
CHANGED
|
@@ -27,23 +27,51 @@ export interface CreateManagedSessionRequest {
|
|
|
27
27
|
readonly environment: EnvironmentRef | string;
|
|
28
28
|
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
29
29
|
}
|
|
30
|
+
/** `PATCH /sessions/{id}`: rename a session. Only the title is client-writable after creation. */
|
|
31
|
+
export interface UpdateManagedSessionRequest {
|
|
32
|
+
readonly title: string;
|
|
33
|
+
}
|
|
30
34
|
export interface ListManagedSessionsResponse {
|
|
31
35
|
readonly data: readonly ManagedSession[];
|
|
32
36
|
}
|
|
33
37
|
export interface CreateManagedMessageRequest {
|
|
34
38
|
readonly input: string;
|
|
39
|
+
/**
|
|
40
|
+
* Whose words these are.
|
|
41
|
+
*
|
|
42
|
+
* `user` (default): the caller's own. The party holding a session's credential is its user —
|
|
43
|
+
* the same way a local session's stdin is — so the model sees the text bare, as a prompt.
|
|
44
|
+
*
|
|
45
|
+
* `external`: another party's words the caller is relaying (a peer, a webhook, another
|
|
46
|
+
* agent). The model sees them inside an `<external-message>` envelope stamped as NOT from the
|
|
47
|
+
* user. Declaring this lowers trust, so it needs no extra permission; the server's `authorize`
|
|
48
|
+
* hook receives the value and can require it for a credential that only ever relays.
|
|
49
|
+
*/
|
|
50
|
+
readonly origin?: "user" | "external";
|
|
51
|
+
/** `external` only: what relayed the words (default `"managed-api"`). */
|
|
35
52
|
readonly source?: string;
|
|
53
|
+
/** `external` only: who said them. */
|
|
36
54
|
readonly actor?: string;
|
|
55
|
+
/** `external` only: attributes rendered onto the envelope. */
|
|
37
56
|
readonly metadata?: Readonly<Record<string, string | number | boolean | null>>;
|
|
38
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Which queue the message is filed into (default `steer`). `steer`: taken at the running
|
|
59
|
+
* turn's next step boundary, or — with nothing running — as the next turn's prompt.
|
|
60
|
+
* `follow_up`: a prompt of its own, queued behind the turn in flight.
|
|
61
|
+
*/
|
|
62
|
+
readonly mode?: "steer" | "follow_up";
|
|
39
63
|
}
|
|
40
|
-
|
|
41
|
-
|
|
64
|
+
/**
|
|
65
|
+
* The receipt for an accepted message. It means the message's `steer.queued` record is durable
|
|
66
|
+
* — a worker will take it — not that a turn has started: whoever holds the session decides that
|
|
67
|
+
* later. `steerId` is the message's identity from here on: on the `steer.queued` event, and as
|
|
68
|
+
* `origin.steerId` of the `message.appended` that shows the model saw it.
|
|
69
|
+
*/
|
|
70
|
+
export interface MessageReceiptResource {
|
|
71
|
+
readonly steerId: string;
|
|
42
72
|
readonly sessionId: string;
|
|
43
73
|
readonly acceptedAt: number;
|
|
44
|
-
readonly
|
|
45
|
-
readonly channel: "turn" | "steering" | "follow_up";
|
|
46
|
-
readonly steerId?: string;
|
|
74
|
+
readonly channel: "steering" | "follow_up";
|
|
47
75
|
}
|
|
48
76
|
export interface ListSessionEventsResponse {
|
|
49
77
|
/** Durable AgentEvents in store order. Store sequence remains inside the opaque page cursor;
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { type IncomingMessage, type Server } from "node:http";
|
|
2
2
|
import type { SessionService } from "./session-service.ts";
|
|
3
3
|
import type { SessionWorker } from "./session-worker.ts";
|
|
4
|
-
export type ManagedAuthorizationAction = "sessions.create" | "sessions.list" | "sessions.read" | "sessions.delete" | "messages.create" | "events.list" | "events.stream" | "sessions.cancel" | "interruptions.list" | "sessions.resume";
|
|
4
|
+
export type ManagedAuthorizationAction = "sessions.create" | "sessions.list" | "sessions.read" | "sessions.update" | "sessions.delete" | "messages.create" | "events.list" | "events.stream" | "sessions.cancel" | "interruptions.list" | "sessions.resume";
|
|
5
5
|
export interface ManagedAuthorizationContext {
|
|
6
6
|
readonly action: ManagedAuthorizationAction;
|
|
7
7
|
readonly sessionId?: string;
|
|
8
|
+
/**
|
|
9
|
+
* `messages.create` only: whose words the caller says these are. `user` is the default and
|
|
10
|
+
* makes the caller this session's user; a deployment that hands a credential to something
|
|
11
|
+
* that only relays (a webhook bridge, a teammate agent) can refuse it anything but `external`.
|
|
12
|
+
*/
|
|
13
|
+
readonly origin?: "user" | "external";
|
|
8
14
|
}
|
|
9
15
|
export type ManagedAuthorize = (request: IncomingMessage, context: ManagedAuthorizationContext) => boolean | void | Promise<boolean | void>;
|
|
10
16
|
/**
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createServer } from "node:http";
|
|
2
2
|
import { isDurableAgentEvent } from "operon-agents";
|
|
3
|
+
import { ResumeSessionRequestSchema, parseRequest } from "./request-schemas.js";
|
|
3
4
|
import { ManagedForbiddenError, ManagedInvalidRequestError, ManagedServerError, } from "./errors.js";
|
|
4
5
|
/**
|
|
5
6
|
* Authorize every request. For local demos and tests only.
|
|
@@ -54,6 +55,11 @@ export function createManagedHttpServer(options) {
|
|
|
54
55
|
await authorize(request, "sessions.read", sessionId);
|
|
55
56
|
return sendJson(response, 200, await options.service.get(sessionId));
|
|
56
57
|
}
|
|
58
|
+
if (method === "PATCH" && parts.length === 2) {
|
|
59
|
+
await authorize(request, "sessions.update", sessionId);
|
|
60
|
+
const body = await readJson(request, maxBodyBytes);
|
|
61
|
+
return sendJson(response, 200, await options.service.update(sessionId, body));
|
|
62
|
+
}
|
|
57
63
|
if (method === "DELETE" && parts.length === 2) {
|
|
58
64
|
await authorize(request, "sessions.delete", sessionId);
|
|
59
65
|
await options.service.delete(sessionId);
|
|
@@ -62,8 +68,10 @@ export function createManagedHttpServer(options) {
|
|
|
62
68
|
return;
|
|
63
69
|
}
|
|
64
70
|
if (method === "POST" && parts[2] === "messages" && parts.length === 3) {
|
|
65
|
-
|
|
71
|
+
// Body before authorization: the hook is told whose words the caller claims these are.
|
|
72
|
+
// Reading first costs an unauthenticated caller at most `maxBodyBytes` of parsing.
|
|
66
73
|
const body = await readJson(request, maxBodyBytes);
|
|
74
|
+
await authorize(request, "messages.create", sessionId, body.origin === "external" ? "external" : "user");
|
|
67
75
|
if (typeof body.input !== "string" || !body.input.trim()) {
|
|
68
76
|
return sendJson(response, 400, apiError("invalid_request", "input must not be empty"));
|
|
69
77
|
}
|
|
@@ -134,9 +142,7 @@ export function createManagedHttpServer(options) {
|
|
|
134
142
|
}
|
|
135
143
|
if (method === "POST" && parts[2] === "resume" && parts.length === 3) {
|
|
136
144
|
await authorize(request, "sessions.resume", sessionId);
|
|
137
|
-
const body = await readJson(request, maxBodyBytes);
|
|
138
|
-
if (!isRecord(body.answers))
|
|
139
|
-
return sendJson(response, 400, apiError("invalid_request", "answers must be an object"));
|
|
145
|
+
const body = parseRequest(ResumeSessionRequestSchema, await readJson(request, maxBodyBytes));
|
|
140
146
|
const receipt = await options.service.answerInterruption(sessionId, body.answers);
|
|
141
147
|
void options.worker?.drain(sessionId).catch(() => undefined);
|
|
142
148
|
return sendJson(response, 202, receipt);
|
|
@@ -186,10 +192,11 @@ export function createManagedHttpServer(options) {
|
|
|
186
192
|
response.write(": connected\n\n");
|
|
187
193
|
manageStream(request, response, () => controller.abort());
|
|
188
194
|
}
|
|
189
|
-
async function authorize(request, action, sessionId) {
|
|
195
|
+
async function authorize(request, action, sessionId, origin) {
|
|
190
196
|
const allowed = await options.authorize(request, {
|
|
191
197
|
action,
|
|
192
198
|
...(sessionId !== undefined ? { sessionId } : {}),
|
|
199
|
+
...(origin !== undefined ? { origin } : {}),
|
|
193
200
|
});
|
|
194
201
|
if (allowed === false)
|
|
195
202
|
throw new ManagedForbiddenError();
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Message idempotency.
|
|
3
3
|
*
|
|
4
|
-
* A client that retries a message after a timeout must not have it
|
|
4
|
+
* A client that retries a message after a timeout must not have it accepted twice, and it
|
|
5
5
|
* cannot tell from a failed request whether the first attempt landed. Keyed by
|
|
6
6
|
* `(sessionId, key)`: the same key on a different session is a different delivery.
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { MessageReceiptResource } from "../protocol/types.ts";
|
|
9
9
|
export interface ManagedDeliveryIdempotencyStore {
|
|
10
|
-
run(sessionId: string, key: string, operation: () => Promise<
|
|
10
|
+
run(sessionId: string, key: string, operation: () => Promise<MessageReceiptResource>): Promise<MessageReceiptResource>;
|
|
11
11
|
}
|
|
12
12
|
export declare class MemoryManagedDeliveryIdempotencyStore implements ManagedDeliveryIdempotencyStore {
|
|
13
13
|
private readonly entries;
|
|
@@ -17,7 +17,7 @@ export declare class MemoryManagedDeliveryIdempotencyStore implements ManagedDel
|
|
|
17
17
|
readonly ttlMs?: number;
|
|
18
18
|
readonly maxEntries?: number;
|
|
19
19
|
});
|
|
20
|
-
run(sessionId: string, key: string, operation: () => Promise<
|
|
20
|
+
run(sessionId: string, key: string, operation: () => Promise<MessageReceiptResource>): Promise<MessageReceiptResource>;
|
|
21
21
|
private pruneExpired;
|
|
22
22
|
private makeRoom;
|
|
23
23
|
}
|
package/dist/server/inbox.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The inbox: what a session has been asked to do that no turn has dealt with yet.
|
|
3
3
|
*
|
|
4
|
-
* It is not a table. It is a READ of the session log — every `
|
|
4
|
+
* It is not a table. It is a READ of the session log — every `steer.queued` record and every
|
|
5
5
|
* control record after the worker's cursor — which is what makes it impossible to lose: the
|
|
6
|
-
* log is the only thing
|
|
7
|
-
* Queues, pending marks and wake-up signals are indexes over this read, never the
|
|
6
|
+
* log is the only thing an accepted message writes, so there is no second place for the two to
|
|
7
|
+
* disagree. Queues, pending marks and wake-up signals are indexes over this read, never the
|
|
8
|
+
* truth of it.
|
|
9
|
+
*
|
|
10
|
+
* The input records are the framework's own: a message accepted over the API is journaled
|
|
11
|
+
* exactly as one steered into a local session is (`steerQueuedRecord`), and a worker puts it
|
|
12
|
+
* back on a session's bus unchanged (`HarnessSession.requeue`). Only the control commands are
|
|
13
|
+
* this layer's vocabulary, and they ride the log as `custom` records.
|
|
8
14
|
*
|
|
9
15
|
* Shared by the two things that need the same answer to "is there work here?": the worker (to
|
|
10
16
|
* do it) and the service (to tell a watching client nobody is doing it). One definition, so
|
|
@@ -12,8 +18,7 @@
|
|
|
12
18
|
* read — set by the same write that creates an item, cleared by a holder about to read — never
|
|
13
19
|
* the truth of it.
|
|
14
20
|
*/
|
|
15
|
-
import type
|
|
16
|
-
import type { ExternalOriginMetadataValue } from "operon-agents";
|
|
21
|
+
import { type InterruptAnswer, type SessionStore, type SteerMessage } from "operon-agents";
|
|
17
22
|
/** Where processing stopped. Everything at or before it has been dealt with. */
|
|
18
23
|
export declare const INBOX_CURSOR_KEY = "inbox:cursor";
|
|
19
24
|
/**
|
|
@@ -24,7 +29,7 @@ export declare const INBOX_CURSOR_KEY = "inbox:cursor";
|
|
|
24
29
|
export declare const CONTROL_RECORD_NAME = "managed.control";
|
|
25
30
|
export type ControlCommand = {
|
|
26
31
|
readonly kind: "cancel";
|
|
27
|
-
/** Idempotency identity, the way `
|
|
32
|
+
/** Idempotency identity, the way `steerId` is for an input. */
|
|
28
33
|
readonly commandId: string;
|
|
29
34
|
readonly requestedAt: number;
|
|
30
35
|
readonly actor?: string;
|
|
@@ -35,18 +40,11 @@ export type ControlCommand = {
|
|
|
35
40
|
readonly answers: Readonly<Record<string, InterruptAnswer>>;
|
|
36
41
|
readonly actor?: string;
|
|
37
42
|
};
|
|
43
|
+
/** An accepted message: its `steer.queued` record, ready to be put back on a bus. */
|
|
38
44
|
export interface InboxInput {
|
|
39
45
|
readonly kind: "input";
|
|
40
46
|
readonly sequence: string;
|
|
41
|
-
readonly
|
|
42
|
-
readonly origin: {
|
|
43
|
-
readonly kind: "external";
|
|
44
|
-
readonly source: string;
|
|
45
|
-
readonly deliveryId: string;
|
|
46
|
-
readonly actor?: string;
|
|
47
|
-
readonly metadata?: Readonly<Record<string, ExternalOriginMetadataValue>>;
|
|
48
|
-
};
|
|
49
|
-
readonly mode: "auto" | "steer" | "follow_up";
|
|
47
|
+
readonly item: SteerMessage;
|
|
50
48
|
}
|
|
51
49
|
export interface InboxControl {
|
|
52
50
|
readonly kind: "control";
|
|
@@ -54,7 +52,7 @@ export interface InboxControl {
|
|
|
54
52
|
readonly command: ControlCommand;
|
|
55
53
|
}
|
|
56
54
|
export type InboxItem = InboxInput | InboxControl;
|
|
57
|
-
/** The identity an item is tracked by once dispatched: `
|
|
55
|
+
/** The identity an item is tracked by once dispatched: `steerId` or `commandId`. */
|
|
58
56
|
export declare function inboxItemId(item: InboxItem): string;
|
|
59
57
|
export declare function readInboxCursor(store: SessionStore): Promise<string | undefined>;
|
|
60
58
|
/** Inbox records after `cursor`, in log order, excluding sequences in `exclude`. */
|
package/dist/server/inbox.js
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The inbox: what a session has been asked to do that no turn has dealt with yet.
|
|
3
|
+
*
|
|
4
|
+
* It is not a table. It is a READ of the session log — every `steer.queued` record and every
|
|
5
|
+
* control record after the worker's cursor — which is what makes it impossible to lose: the
|
|
6
|
+
* log is the only thing an accepted message writes, so there is no second place for the two to
|
|
7
|
+
* disagree. Queues, pending marks and wake-up signals are indexes over this read, never the
|
|
8
|
+
* truth of it.
|
|
9
|
+
*
|
|
10
|
+
* The input records are the framework's own: a message accepted over the API is journaled
|
|
11
|
+
* exactly as one steered into a local session is (`steerQueuedRecord`), and a worker puts it
|
|
12
|
+
* back on a session's bus unchanged (`HarnessSession.requeue`). Only the control commands are
|
|
13
|
+
* this layer's vocabulary, and they ride the log as `custom` records.
|
|
14
|
+
*
|
|
15
|
+
* Shared by the two things that need the same answer to "is there work here?": the worker (to
|
|
16
|
+
* do it) and the service (to tell a watching client nobody is doing it). One definition, so
|
|
17
|
+
* they can never disagree on what counts. The work table's `woken` flag is an index over this
|
|
18
|
+
* read — set by the same write that creates an item, cleared by a holder about to read — never
|
|
19
|
+
* the truth of it.
|
|
20
|
+
*/
|
|
21
|
+
import { steerMessageFromRecord } from "operon-agents";
|
|
1
22
|
/** Where processing stopped. Everything at or before it has been dealt with. */
|
|
2
23
|
export const INBOX_CURSOR_KEY = "inbox:cursor";
|
|
3
24
|
/**
|
|
@@ -6,9 +27,9 @@ export const INBOX_CURSOR_KEY = "inbox:cursor";
|
|
|
6
27
|
* extension point the framework left for exactly that.
|
|
7
28
|
*/
|
|
8
29
|
export const CONTROL_RECORD_NAME = "managed.control";
|
|
9
|
-
/** The identity an item is tracked by once dispatched: `
|
|
30
|
+
/** The identity an item is tracked by once dispatched: `steerId` or `commandId`. */
|
|
10
31
|
export function inboxItemId(item) {
|
|
11
|
-
return item.kind === "input" ? item.
|
|
32
|
+
return item.kind === "input" ? item.item.id : item.command.commandId;
|
|
12
33
|
}
|
|
13
34
|
export async function readInboxCursor(store) {
|
|
14
35
|
const raw = await store.getState(INBOX_CURSOR_KEY);
|
|
@@ -36,9 +57,9 @@ export async function hasUnprocessedInbox(store) {
|
|
|
36
57
|
return (await readInbox(store, await readInboxCursor(store))).length > 0;
|
|
37
58
|
}
|
|
38
59
|
function inboxItemFromRecord(sequence, record) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
60
|
+
const steer = steerMessageFromRecord(record);
|
|
61
|
+
if (steer !== undefined)
|
|
62
|
+
return { kind: "input", sequence, item: steer };
|
|
42
63
|
if (record.type === "custom" && record.name === CONTROL_RECORD_NAME) {
|
|
43
64
|
const command = parseControlCommand(record.data);
|
|
44
65
|
return command === undefined ? undefined : { kind: "control", sequence, command };
|
|
@@ -1,12 +1,28 @@
|
|
|
1
|
-
import type { Agent, CreateSessionOptions, Harness,
|
|
1
|
+
import type { Agent, CreateSessionOptions, Harness, Environment, EnvironmentFactory, ResumeSessionOptions } from "operon-agents";
|
|
2
2
|
import type { AgentRef, EnvironmentRef } from "../protocol/types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Which session a registry is resolving for. Handed to both registries by the service at create
|
|
5
|
+
* and by the worker on every open, so a host can choose per session — per tenant — the agent,
|
|
6
|
+
* the environment and the credentials (`resumeOptions.mcpCredentials`) it runs with.
|
|
7
|
+
*/
|
|
8
|
+
export interface ManagedSessionContext {
|
|
9
|
+
readonly sessionId: string;
|
|
10
|
+
/** Stamped by `SessionService({ ownerKey })` at create. Trusted: no client can set it. */
|
|
11
|
+
readonly ownerKey?: string;
|
|
12
|
+
/**
|
|
13
|
+
* The create request's `metadata`. Untrusted: a client wrote it. A host that acts on it (a
|
|
14
|
+
* vault id, a repository) must check it against `ownerKey`, or a client names another
|
|
15
|
+
* tenant's.
|
|
16
|
+
*/
|
|
17
|
+
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
18
|
+
}
|
|
3
19
|
export interface AgentResolution<TContext = unknown> {
|
|
4
20
|
readonly agent?: Agent<TContext>;
|
|
5
21
|
readonly createOptions?: Omit<CreateSessionOptions<TContext>, "id" | "title" | "workDir" | "agent">;
|
|
6
22
|
readonly resumeOptions?: Omit<ResumeSessionOptions<TContext>, "agent">;
|
|
7
23
|
}
|
|
8
24
|
export interface ManagedAgentRegistry<TContext = unknown> {
|
|
9
|
-
resolve(ref: AgentRef): AgentResolution<TContext> | Promise<AgentResolution<TContext>>;
|
|
25
|
+
resolve(ref: AgentRef, session: ManagedSessionContext): AgentResolution<TContext> | Promise<AgentResolution<TContext>>;
|
|
10
26
|
}
|
|
11
27
|
/** Let managed sessions use the Harness's configured default Agent. */
|
|
12
28
|
export declare class DefaultAgentRegistry<TContext = unknown> implements ManagedAgentRegistry<TContext> {
|
|
@@ -18,23 +34,23 @@ export interface EnvironmentResolution {
|
|
|
18
34
|
/** Durable working directory recorded by the SessionRepository. */
|
|
19
35
|
readonly workDir: string;
|
|
20
36
|
/** Optional per-session execution backend. Lifecycle remains provider-owned. */
|
|
21
|
-
readonly
|
|
37
|
+
readonly environment?: Environment | EnvironmentFactory;
|
|
22
38
|
}
|
|
23
39
|
export interface ManagedEnvironmentRegistry {
|
|
24
|
-
resolve(ref: EnvironmentRef): EnvironmentResolution | Promise<EnvironmentResolution>;
|
|
40
|
+
resolve(ref: EnvironmentRef, session: ManagedSessionContext): EnvironmentResolution | Promise<EnvironmentResolution>;
|
|
25
41
|
}
|
|
26
42
|
export declare class StaticEnvironmentRegistry implements ManagedEnvironmentRegistry {
|
|
27
43
|
private readonly environments;
|
|
28
44
|
constructor(environments: Readonly<Record<string, EnvironmentResolution>>);
|
|
29
45
|
resolve(ref: EnvironmentRef): EnvironmentResolution;
|
|
30
46
|
}
|
|
31
|
-
/** Convenience composition for a server that owns the Harness.
|
|
47
|
+
/** Convenience composition for a server that owns the Harness. Environment/sandbox lifecycle
|
|
32
48
|
* remains with the host; this merely makes that explicit at the managed-server boundary. */
|
|
33
49
|
export interface ManagedHarnessOptions<TContext = unknown> {
|
|
34
50
|
readonly harness: Harness<TContext>;
|
|
35
51
|
readonly agents?: ManagedAgentRegistry<TContext>;
|
|
36
52
|
readonly environments: ManagedEnvironmentRegistry;
|
|
37
53
|
readonly defaultAgentId?: string;
|
|
38
|
-
readonly
|
|
54
|
+
readonly environment?: Environment | EnvironmentFactory;
|
|
39
55
|
}
|
|
40
56
|
//# sourceMappingURL=registries.d.ts.map
|