operon-managed-agents 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 +103 -0
- package/dist/client/client.d.ts +80 -0
- package/dist/client/client.js +232 -0
- package/dist/client/errors.d.ts +7 -0
- package/dist/client/errors.js +21 -0
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.js +4 -0
- package/dist/client/run.d.ts +71 -0
- package/dist/client/run.js +144 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/protocol/index.d.ts +3 -0
- package/dist/protocol/index.js +3 -0
- package/dist/protocol/sse.d.ts +7 -0
- package/dist/protocol/sse.js +53 -0
- package/dist/protocol/types.d.ts +102 -0
- package/dist/protocol/types.js +2 -0
- package/dist/server/broadcast.d.ts +105 -0
- package/dist/server/broadcast.js +130 -0
- package/dist/server/errors.d.ts +23 -0
- package/dist/server/errors.js +43 -0
- package/dist/server/http-server.d.ts +54 -0
- package/dist/server/http-server.js +325 -0
- package/dist/server/idempotency.d.ts +24 -0
- package/dist/server/idempotency.js +52 -0
- package/dist/server/inbox.d.ts +64 -0
- package/dist/server/inbox.js +71 -0
- package/dist/server/index.d.ts +14 -0
- package/dist/server/index.js +14 -0
- package/dist/server/metadata-pg.d.ts +31 -0
- package/dist/server/metadata-pg.js +64 -0
- package/dist/server/metadata.d.ts +38 -0
- package/dist/server/metadata.js +107 -0
- package/dist/server/registries.d.ts +40 -0
- package/dist/server/registries.js +26 -0
- package/dist/server/session-service.d.ts +181 -0
- package/dist/server/session-service.js +658 -0
- package/dist/server/session-worker.d.ts +136 -0
- package/dist/server/session-worker.js +428 -0
- package/dist/server/work-memory.d.ts +27 -0
- package/dist/server/work-memory.js +85 -0
- package/dist/server/work-pg.d.ts +46 -0
- package/dist/server/work-pg.js +122 -0
- package/dist/server/work.d.ts +49 -0
- package/dist/server/work.js +2 -0
- package/package.json +56 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export function managedSessionMetadataTableDDL(table = "managed_session_metadata") {
|
|
2
|
+
return `
|
|
3
|
+
CREATE TABLE IF NOT EXISTS ${table} (
|
|
4
|
+
session_id TEXT PRIMARY KEY,
|
|
5
|
+
document JSONB NOT NULL
|
|
6
|
+
);
|
|
7
|
+
`;
|
|
8
|
+
}
|
|
9
|
+
export class PgManagedSessionMetadataStore {
|
|
10
|
+
pool;
|
|
11
|
+
table;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.pool = options.pool;
|
|
14
|
+
this.table = options.table ?? "managed_session_metadata";
|
|
15
|
+
}
|
|
16
|
+
async get(sessionId) {
|
|
17
|
+
const { rows } = await this.pool.query(`SELECT document FROM ${this.table} WHERE session_id = $1`, [sessionId]);
|
|
18
|
+
return rows[0] === undefined ? undefined : parse(rows[0].document);
|
|
19
|
+
}
|
|
20
|
+
async getMany(sessionIds) {
|
|
21
|
+
if (sessionIds.length === 0)
|
|
22
|
+
return new Map();
|
|
23
|
+
// One statement rather than N: listing is the hot read, and a per-row round trip would make
|
|
24
|
+
// its cost scale with page size on top of the catalog query that produced the ids.
|
|
25
|
+
const { rows } = await this.pool.query(`SELECT session_id, document FROM ${this.table} WHERE session_id = ANY($1)`, [[...sessionIds]]);
|
|
26
|
+
const out = new Map();
|
|
27
|
+
for (const row of rows) {
|
|
28
|
+
const parsed = parse(row.document);
|
|
29
|
+
if (parsed !== undefined)
|
|
30
|
+
out.set(String(row.session_id), parsed);
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/** Atomic claim: false means another creator already owns this id. */
|
|
35
|
+
async create(metadata) {
|
|
36
|
+
const { rows } = await this.pool.query(`INSERT INTO ${this.table} (session_id, document) VALUES ($1, $2)
|
|
37
|
+
ON CONFLICT (session_id) DO NOTHING
|
|
38
|
+
RETURNING session_id`, [metadata.sessionId, JSON.stringify(metadata)]);
|
|
39
|
+
return rows.length > 0;
|
|
40
|
+
}
|
|
41
|
+
async delete(sessionId) {
|
|
42
|
+
await this.pool.query(`DELETE FROM ${this.table} WHERE session_id = $1`, [sessionId]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function parse(document) {
|
|
46
|
+
// `pg` returns jsonb already parsed; a driver that hands back text is still accepted.
|
|
47
|
+
const value = typeof document === "string" ? safeParse(document) : document;
|
|
48
|
+
if (value === null || typeof value !== "object")
|
|
49
|
+
return undefined;
|
|
50
|
+
const record = value;
|
|
51
|
+
if (typeof record.sessionId !== "string" || record.agent === undefined || record.environment === undefined) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
return record;
|
|
55
|
+
}
|
|
56
|
+
function safeParse(text) {
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(text);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=metadata-pg.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { AgentRef, EnvironmentRef } from "../protocol/types.ts";
|
|
2
|
+
export interface ManagedSessionMetadata {
|
|
3
|
+
readonly version: 1;
|
|
4
|
+
readonly sessionId: string;
|
|
5
|
+
readonly agent: AgentRef;
|
|
6
|
+
readonly environment: EnvironmentRef;
|
|
7
|
+
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
8
|
+
readonly createdAt: number;
|
|
9
|
+
readonly updatedAt: number;
|
|
10
|
+
}
|
|
11
|
+
export interface ManagedSessionMetadataStore {
|
|
12
|
+
get(sessionId: string): Promise<ManagedSessionMetadata | undefined>;
|
|
13
|
+
getMany(sessionIds: readonly string[]): Promise<ReadonlyMap<string, ManagedSessionMetadata>>;
|
|
14
|
+
/** Atomically claim a new managed id. False means another creator already owns it. */
|
|
15
|
+
create(metadata: ManagedSessionMetadata): Promise<boolean>;
|
|
16
|
+
delete(sessionId: string): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export declare class MemoryManagedSessionMetadataStore implements ManagedSessionMetadataStore {
|
|
19
|
+
private readonly values;
|
|
20
|
+
get(sessionId: string): Promise<ManagedSessionMetadata | undefined>;
|
|
21
|
+
getMany(sessionIds: readonly string[]): Promise<ReadonlyMap<string, ManagedSessionMetadata>>;
|
|
22
|
+
create(metadata: ManagedSessionMetadata): Promise<boolean>;
|
|
23
|
+
delete(sessionId: string): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
/** Durable default for one-process deployments. Distributed hosts can implement the same
|
|
26
|
+
* interface on their database without changing SessionHost. */
|
|
27
|
+
export declare class DiskManagedSessionMetadataStore implements ManagedSessionMetadataStore {
|
|
28
|
+
private readonly directory;
|
|
29
|
+
private all?;
|
|
30
|
+
constructor(directory: string);
|
|
31
|
+
get(sessionId: string): Promise<ManagedSessionMetadata | undefined>;
|
|
32
|
+
getMany(sessionIds: readonly string[]): Promise<ReadonlyMap<string, ManagedSessionMetadata>>;
|
|
33
|
+
create(metadata: ManagedSessionMetadata): Promise<boolean>;
|
|
34
|
+
delete(sessionId: string): Promise<void>;
|
|
35
|
+
private loadAll;
|
|
36
|
+
private path;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=metadata.d.ts.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
export class MemoryManagedSessionMetadataStore {
|
|
4
|
+
values = new Map();
|
|
5
|
+
async get(sessionId) {
|
|
6
|
+
return this.values.get(sessionId);
|
|
7
|
+
}
|
|
8
|
+
async getMany(sessionIds) {
|
|
9
|
+
return new Map(sessionIds.flatMap((id) => {
|
|
10
|
+
const value = this.values.get(id);
|
|
11
|
+
return value === undefined ? [] : [[id, value]];
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
async create(metadata) {
|
|
15
|
+
if (this.values.has(metadata.sessionId))
|
|
16
|
+
return false;
|
|
17
|
+
this.values.set(metadata.sessionId, metadata);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
async delete(sessionId) {
|
|
21
|
+
this.values.delete(sessionId);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const SAFE_SESSION_ID = /^[A-Za-z0-9_-]+$/;
|
|
25
|
+
/** Durable default for one-process deployments. Distributed hosts can implement the same
|
|
26
|
+
* interface on their database without changing SessionHost. */
|
|
27
|
+
export class DiskManagedSessionMetadataStore {
|
|
28
|
+
directory;
|
|
29
|
+
all;
|
|
30
|
+
constructor(directory) {
|
|
31
|
+
this.directory = directory;
|
|
32
|
+
}
|
|
33
|
+
async get(sessionId) {
|
|
34
|
+
if (this.all !== undefined)
|
|
35
|
+
return (await this.all).get(sessionId);
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(await readFile(this.path(sessionId), "utf8"));
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (isNodeError(error) && error.code === "ENOENT")
|
|
41
|
+
return undefined;
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async getMany(sessionIds) {
|
|
46
|
+
const all = await (this.all ??= this.loadAll());
|
|
47
|
+
return new Map(sessionIds.flatMap((id) => {
|
|
48
|
+
const value = all.get(id);
|
|
49
|
+
return value === undefined ? [] : [[id, value]];
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
async create(metadata) {
|
|
53
|
+
await mkdir(this.directory, { recursive: true });
|
|
54
|
+
try {
|
|
55
|
+
await writeFile(this.path(metadata.sessionId), `${JSON.stringify(metadata)}\n`, {
|
|
56
|
+
encoding: "utf8",
|
|
57
|
+
mode: 0o600,
|
|
58
|
+
flag: "wx",
|
|
59
|
+
});
|
|
60
|
+
if (this.all !== undefined)
|
|
61
|
+
(await this.all).set(metadata.sessionId, metadata);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if (isNodeError(error) && error.code === "EEXIST")
|
|
66
|
+
return false;
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async delete(sessionId) {
|
|
71
|
+
await rm(this.path(sessionId), { force: true });
|
|
72
|
+
if (this.all !== undefined)
|
|
73
|
+
(await this.all).delete(sessionId);
|
|
74
|
+
}
|
|
75
|
+
async loadAll() {
|
|
76
|
+
let names;
|
|
77
|
+
try {
|
|
78
|
+
names = await readdir(this.directory);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (isNodeError(error) && error.code === "ENOENT")
|
|
82
|
+
return new Map();
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
const entries = await Promise.all(names
|
|
86
|
+
.filter((name) => name.endsWith(".json"))
|
|
87
|
+
.map(async (name) => {
|
|
88
|
+
try {
|
|
89
|
+
const value = JSON.parse(await readFile(join(this.directory, name), "utf8"));
|
|
90
|
+
return value.sessionId === name.slice(0, -5) ? [value.sessionId, value] : undefined;
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
}));
|
|
96
|
+
return new Map(entries.filter((entry) => entry !== undefined));
|
|
97
|
+
}
|
|
98
|
+
path(sessionId) {
|
|
99
|
+
if (!SAFE_SESSION_ID.test(sessionId))
|
|
100
|
+
throw new Error(`invalid session id "${sessionId}"`);
|
|
101
|
+
return join(this.directory, `${sessionId}.json`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function isNodeError(error) {
|
|
105
|
+
return error instanceof Error && "code" in error;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=metadata.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Agent, CreateSessionOptions, Harness, Machine, MachineFactory, ResumeSessionOptions } from "operon-agents";
|
|
2
|
+
import type { AgentRef, EnvironmentRef } from "../protocol/types.ts";
|
|
3
|
+
export interface AgentResolution<TContext = unknown> {
|
|
4
|
+
readonly agent?: Agent<TContext>;
|
|
5
|
+
readonly createOptions?: Omit<CreateSessionOptions<TContext>, "id" | "title" | "workDir" | "agent">;
|
|
6
|
+
readonly resumeOptions?: Omit<ResumeSessionOptions<TContext>, "agent">;
|
|
7
|
+
}
|
|
8
|
+
export interface ManagedAgentRegistry<TContext = unknown> {
|
|
9
|
+
resolve(ref: AgentRef): AgentResolution<TContext> | Promise<AgentResolution<TContext>>;
|
|
10
|
+
}
|
|
11
|
+
/** Let managed sessions use the Harness's configured default Agent. */
|
|
12
|
+
export declare class DefaultAgentRegistry<TContext = unknown> implements ManagedAgentRegistry<TContext> {
|
|
13
|
+
private readonly id;
|
|
14
|
+
constructor(id?: string);
|
|
15
|
+
resolve(ref: AgentRef): AgentResolution<TContext>;
|
|
16
|
+
}
|
|
17
|
+
export interface EnvironmentResolution {
|
|
18
|
+
/** Durable working directory recorded by the SessionRepository. */
|
|
19
|
+
readonly workDir: string;
|
|
20
|
+
/** Optional per-session execution backend. Lifecycle remains provider-owned. */
|
|
21
|
+
readonly machine?: Machine | MachineFactory;
|
|
22
|
+
}
|
|
23
|
+
export interface ManagedEnvironmentRegistry {
|
|
24
|
+
resolve(ref: EnvironmentRef): EnvironmentResolution | Promise<EnvironmentResolution>;
|
|
25
|
+
}
|
|
26
|
+
export declare class StaticEnvironmentRegistry implements ManagedEnvironmentRegistry {
|
|
27
|
+
private readonly environments;
|
|
28
|
+
constructor(environments: Readonly<Record<string, EnvironmentResolution>>);
|
|
29
|
+
resolve(ref: EnvironmentRef): EnvironmentResolution;
|
|
30
|
+
}
|
|
31
|
+
/** Convenience composition for a server that owns the Harness. Machine/sandbox lifecycle
|
|
32
|
+
* remains with the host; this merely makes that explicit at the managed-server boundary. */
|
|
33
|
+
export interface ManagedHarnessOptions<TContext = unknown> {
|
|
34
|
+
readonly harness: Harness<TContext>;
|
|
35
|
+
readonly agents?: ManagedAgentRegistry<TContext>;
|
|
36
|
+
readonly environments: ManagedEnvironmentRegistry;
|
|
37
|
+
readonly defaultAgentId?: string;
|
|
38
|
+
readonly machine?: Machine | MachineFactory;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=registries.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ManagedInvalidRequestError } from "./errors.js";
|
|
2
|
+
/** Let managed sessions use the Harness's configured default Agent. */
|
|
3
|
+
export class DefaultAgentRegistry {
|
|
4
|
+
id;
|
|
5
|
+
constructor(id = "default") {
|
|
6
|
+
this.id = id;
|
|
7
|
+
}
|
|
8
|
+
resolve(ref) {
|
|
9
|
+
if (ref.id !== this.id)
|
|
10
|
+
throw new ManagedInvalidRequestError(`unknown agent "${ref.id}"`);
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class StaticEnvironmentRegistry {
|
|
15
|
+
environments;
|
|
16
|
+
constructor(environments) {
|
|
17
|
+
this.environments = environments;
|
|
18
|
+
}
|
|
19
|
+
resolve(ref) {
|
|
20
|
+
const resolution = this.environments[ref.id];
|
|
21
|
+
if (resolution === undefined)
|
|
22
|
+
throw new ManagedInvalidRequestError(`unknown environment "${ref.id}"`);
|
|
23
|
+
return resolution;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=registries.js.map
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The managed API surface: reads and writes of session state.
|
|
3
|
+
*
|
|
4
|
+
* Every method here is a store operation. Nothing opens a session, assembles capabilities,
|
|
5
|
+
* connects an MCP server or boots a sandbox — that is what running a turn does, and running is
|
|
6
|
+
* the worker's job (`SessionWorker`), reached by writing to the log rather than by calling in.
|
|
7
|
+
*
|
|
8
|
+
* The practical consequence is that this object is stateless and horizontally scalable: any
|
|
9
|
+
* replica can serve any request for any session, because the session is not "somewhere" — it is
|
|
10
|
+
* in the store. A gateway in front of it needs no lease lookup and no owner affinity; it is a
|
|
11
|
+
* plain reverse proxy.
|
|
12
|
+
*
|
|
13
|
+
* What this deliberately does NOT have is an `open()`. A resource API that can only be reached
|
|
14
|
+
* by first reviving a runtime makes every read a potential sandbox wake-up, which is how a
|
|
15
|
+
* simple event-stream subscription ends up billing for compute.
|
|
16
|
+
*/
|
|
17
|
+
import { type AgentEvent, type DeleteSessionOptions, type InterruptAnswer, type PendingRunInterrupt, type SessionRepository } from "operon-agents";
|
|
18
|
+
import type { ControlReceiptResource, CreateManagedMessageRequest, CreateManagedSessionRequest, DeliveryReceiptResource, ManagedSession } from "../protocol/types.ts";
|
|
19
|
+
import type { SessionWork } from "./work.ts";
|
|
20
|
+
import { type ManagedSessionMetadataStore } from "./metadata.ts";
|
|
21
|
+
import type { ManagedEnvironmentRegistry } from "./registries.ts";
|
|
22
|
+
import type { EventBroadcaster } from "./broadcast.ts";
|
|
23
|
+
import { type ManagedDeliveryIdempotencyStore } from "./idempotency.ts";
|
|
24
|
+
export interface SessionServiceOptions {
|
|
25
|
+
readonly repository: SessionRepository;
|
|
26
|
+
/**
|
|
27
|
+
* Where inputs and commands are written. Every accepted input goes through `work.append`,
|
|
28
|
+
* which puts it in the session's log and wakes the session for a worker as one step — so
|
|
29
|
+
* "accepted" means "will be claimed", not "is on disk somewhere a worker might look". MUST be
|
|
30
|
+
* the same table the workers claim from.
|
|
31
|
+
*/
|
|
32
|
+
readonly work: SessionWork;
|
|
33
|
+
/** Resolves an environment to the durable `workDir` a session is created under. The machine
|
|
34
|
+
* half of a resolution belongs to the worker; nothing here executes. */
|
|
35
|
+
readonly environments: ManagedEnvironmentRegistry;
|
|
36
|
+
readonly metadataStore?: ManagedSessionMetadataStore;
|
|
37
|
+
/**
|
|
38
|
+
* Partition every session this service creates, and every session `list()` returns, under one
|
|
39
|
+
* owner. A multi-tenant deployment builds one service per tenant so `list()` cannot reach
|
|
40
|
+
* across tenants.
|
|
41
|
+
*
|
|
42
|
+
* Partition, not permission: `get`/`appendEvent` still resolve any id the repository holds.
|
|
43
|
+
* Enforcing who may address a session stays with the authorization hook.
|
|
44
|
+
*/
|
|
45
|
+
readonly ownerKey?: string;
|
|
46
|
+
/** Collapses retried deliveries carrying the same key. Defaults to an in-process store, which
|
|
47
|
+
* is only correct for a single node — a multi-node deployment must supply a shared one. */
|
|
48
|
+
readonly idempotencyStore?: ManagedDeliveryIdempotencyStore;
|
|
49
|
+
/**
|
|
50
|
+
* Low-latency delivery for `watchEvents`. Without one, the stream polls the store and carries
|
|
51
|
+
* only what was persisted — correct, but a poll interval behind and missing live-only events
|
|
52
|
+
* such as token deltas. With one, the store is used to backfill and the broadcast carries the
|
|
53
|
+
* live tail.
|
|
54
|
+
*/
|
|
55
|
+
readonly broadcaster?: EventBroadcaster;
|
|
56
|
+
/**
|
|
57
|
+
* How long a stream may be silent before the stall check runs.
|
|
58
|
+
*
|
|
59
|
+
* With the live channel, a worker dying mid-turn is invisible to a subscriber: the stream is
|
|
60
|
+
* attached to this service, not to the worker, so it does not break — it simply goes quiet,
|
|
61
|
+
* which is indistinguishable from a model thinking. The work table's lease (`work.peek`) is
|
|
62
|
+
* what tells the two apart.
|
|
63
|
+
*/
|
|
64
|
+
readonly stallCheckMs?: number;
|
|
65
|
+
}
|
|
66
|
+
export interface ListPersistedEventsOptions {
|
|
67
|
+
readonly limit: number;
|
|
68
|
+
readonly after?: string;
|
|
69
|
+
readonly order?: "asc" | "desc";
|
|
70
|
+
readonly address?: string;
|
|
71
|
+
}
|
|
72
|
+
export interface PersistedEventPage {
|
|
73
|
+
readonly data: readonly AgentEvent[];
|
|
74
|
+
readonly next?: string;
|
|
75
|
+
}
|
|
76
|
+
export interface WatchEventsOptions {
|
|
77
|
+
/**
|
|
78
|
+
* Resume after the event with this id — the public cursor, the one an SSE `id:` line carries.
|
|
79
|
+
* Only durable events are valid here (see `isDurableAgentEvent`); an id the log does not
|
|
80
|
+
* contain replays from the beginning, because guessing a position is worse than a duplicate
|
|
81
|
+
* the client can dedupe.
|
|
82
|
+
*/
|
|
83
|
+
readonly after?: string;
|
|
84
|
+
readonly address?: string;
|
|
85
|
+
readonly signal?: AbortSignal;
|
|
86
|
+
}
|
|
87
|
+
export declare class SessionService {
|
|
88
|
+
private readonly repository;
|
|
89
|
+
private readonly environments;
|
|
90
|
+
private readonly metadata;
|
|
91
|
+
private readonly ownerKey;
|
|
92
|
+
private readonly work;
|
|
93
|
+
private readonly idempotency;
|
|
94
|
+
private readonly broadcaster;
|
|
95
|
+
private readonly stallCheckMs;
|
|
96
|
+
constructor(options: SessionServiceOptions);
|
|
97
|
+
/**
|
|
98
|
+
* Is this session waiting on work nobody is doing?
|
|
99
|
+
*
|
|
100
|
+
* BOTH conditions are required, and each alone is a false positive waiting to happen: an idle
|
|
101
|
+
* session has no holder and that is fine, and a session mid-turn has unprocessed input right
|
|
102
|
+
* up until the cursor advances. Only "there is work AND nobody holds it" is unambiguous.
|
|
103
|
+
*
|
|
104
|
+
* Undefined when it cannot be determined (the table cannot be asked), because reporting a
|
|
105
|
+
* problem on missing evidence is worse than reporting nothing.
|
|
106
|
+
*/
|
|
107
|
+
private isStranded;
|
|
108
|
+
/**
|
|
109
|
+
* Register a session. Creating it starts no work and provisions nothing: the record exists in
|
|
110
|
+
* the repository and a worker picks it up the first time there is something to process.
|
|
111
|
+
*
|
|
112
|
+
* The agent reference is recorded, not resolved — turning it into an `Agent` needs the model
|
|
113
|
+
* and tool configuration a worker holds, and doing it here would make creation depend on
|
|
114
|
+
* execution being available.
|
|
115
|
+
*/
|
|
116
|
+
create(input: CreateManagedSessionRequest): Promise<ManagedSession>;
|
|
117
|
+
get(id: string): Promise<ManagedSession>;
|
|
118
|
+
list(): Promise<readonly ManagedSession[]>;
|
|
119
|
+
listEvents(id: string, options: ListPersistedEventsOptions): Promise<PersistedEventPage>;
|
|
120
|
+
/**
|
|
121
|
+
* Follow a session's events from a cursor.
|
|
122
|
+
*
|
|
123
|
+
* Order of operations matters and is the whole reason this is not two loops glued together:
|
|
124
|
+
* the broadcast subscription is opened FIRST and buffered, the store is read second, and only
|
|
125
|
+
* then does the buffer drain. Reading first and subscribing after leaves a window in which
|
|
126
|
+
* events land in neither — the exact race a client cannot detect, because the stream simply
|
|
127
|
+
* appears to skip.
|
|
128
|
+
*
|
|
129
|
+
* Duplicates across the seam are removed by event id: an event may legitimately arrive from
|
|
130
|
+
* both sides when it was persisted just as the backfill was catching up.
|
|
131
|
+
*
|
|
132
|
+
* Without a broadcaster this degrades to polling the store, which carries only persisted
|
|
133
|
+
* events. That is correct, just later and thinner.
|
|
134
|
+
*/
|
|
135
|
+
watchEvents(id: string, options?: WatchEventsOptions): AsyncIterable<AgentEvent>;
|
|
136
|
+
/** Read the log, either to the current head (`untilCaughtUp`) or indefinitely. */
|
|
137
|
+
private watchStore;
|
|
138
|
+
interruptions(id: string): Promise<readonly PendingRunInterrupt[]>;
|
|
139
|
+
/**
|
|
140
|
+
* Accept an input and return a receipt. The write is awaited, so the receipt means the input
|
|
141
|
+
* survives a crash — not merely that it reached a process.
|
|
142
|
+
*
|
|
143
|
+
* Acceptance is all this does. Whether the input steers a turn already in flight or starts a
|
|
144
|
+
* fresh one is decided when a worker processes it, which can be an arbitrary moment later.
|
|
145
|
+
*/
|
|
146
|
+
appendEvent(id: string, request: CreateManagedMessageRequest, idempotencyKey?: string): Promise<DeliveryReceiptResource>;
|
|
147
|
+
private acceptEvent;
|
|
148
|
+
/**
|
|
149
|
+
* Ask whoever is running this session to stop, or — if nobody is — discard what is waiting.
|
|
150
|
+
*
|
|
151
|
+
* Written to the log like an input, and for the same reason: the process running the turn
|
|
152
|
+
* may be anywhere, and the only address every node shares is the store. The holder learns of
|
|
153
|
+
* it from its next heartbeat and reads it; with no holder the next worker to claim the
|
|
154
|
+
* session reads it first and skips the inputs it precedes.
|
|
155
|
+
*/
|
|
156
|
+
requestCancel(id: string, options?: {
|
|
157
|
+
readonly actor?: string;
|
|
158
|
+
}): Promise<ControlReceiptResource>;
|
|
159
|
+
/**
|
|
160
|
+
* Answer a durable interruption. The answers are journaled with the command, so a worker
|
|
161
|
+
* that dies mid-continuation leaves nothing for the client to resend: the next holder reads
|
|
162
|
+
* the same record and continues from the last durable pause.
|
|
163
|
+
*/
|
|
164
|
+
answerInterruption(id: string, answers: Readonly<Record<string, InterruptAnswer>>, options?: {
|
|
165
|
+
readonly actor?: string;
|
|
166
|
+
}): Promise<ControlReceiptResource>;
|
|
167
|
+
private appendControl;
|
|
168
|
+
/**
|
|
169
|
+
* Soft by default: the durable record and its managed metadata are retained, and every route
|
|
170
|
+
* that resolves a session starts returning 404. `{ purge: true }` destroys both — deliberately
|
|
171
|
+
* not reachable over HTTP, so retention stays a decision the host makes on its own schedule.
|
|
172
|
+
*/
|
|
173
|
+
delete(id: string, options?: DeleteSessionOptions): Promise<void>;
|
|
174
|
+
/** Undo a soft delete. Throws when the session was purged (its metadata is gone). */
|
|
175
|
+
restore(id: string): Promise<void>;
|
|
176
|
+
private openStore;
|
|
177
|
+
private requireMetadata;
|
|
178
|
+
private summary;
|
|
179
|
+
private toManagedSession;
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=session-service.d.ts.map
|