@saturn-universe/runtime 0.0.0-stage → 0.1.1
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 +661 -0
- package/dist/agent-loop.d.ts +48 -0
- package/dist/agent-loop.js +1072 -0
- package/dist/command-dependencies.d.ts +14 -0
- package/dist/command-dependencies.js +1 -0
- package/dist/command-handler.d.ts +10 -0
- package/dist/command-handler.js +269 -0
- package/dist/compaction.d.ts +81 -0
- package/dist/compaction.js +444 -0
- package/dist/completion-policy.d.ts +30 -0
- package/dist/completion-policy.js +502 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +8 -0
- package/dist/journal.d.ts +28 -0
- package/dist/journal.js +473 -0
- package/dist/recovery.d.ts +19 -0
- package/dist/recovery.js +103 -0
- package/dist/run-loop.d.ts +49 -0
- package/dist/run-loop.js +739 -0
- package/dist/search-loop.d.ts +57 -0
- package/dist/search-loop.js +905 -0
- package/dist/team-loop.d.ts +46 -0
- package/dist/team-loop.js +936 -0
- package/package.json +15 -3
- package/README.md +0 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AdmissionSnapshot, CommitReceipt, Hash, RuntimeCommand, RuntimeCommit } from '@saturn-universe/core';
|
|
2
|
+
export interface CommandDependencies {
|
|
3
|
+
commit: RuntimeCommit;
|
|
4
|
+
reader: {
|
|
5
|
+
snapshot(): Promise<AdmissionSnapshot>;
|
|
6
|
+
admission?(command: RuntimeCommand): Promise<AdmissionSnapshot>;
|
|
7
|
+
};
|
|
8
|
+
receipt(commandId: string, commandHash: string): Promise<CommitReceipt | null>;
|
|
9
|
+
hash: Hash;
|
|
10
|
+
now(): string;
|
|
11
|
+
id(): string;
|
|
12
|
+
epoch: number;
|
|
13
|
+
rootUri: string;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RunConfig, RuntimeCommand, CommandId, JsonValue, AgentSessionId } from '@saturn-universe/core';
|
|
2
|
+
import type { CommandDependencies } from './command-dependencies.js';
|
|
3
|
+
type PrepareAgentConfig = (config: RunConfig, role: 'main' | 'worker' | 'planner', id: AgentSessionId) => RunConfig;
|
|
4
|
+
export declare function createCommandHandler(d: CommandDependencies): {
|
|
5
|
+
handle(commandId: CommandId, command: RuntimeCommand, identity?: {
|
|
6
|
+
version: 2;
|
|
7
|
+
hash: string;
|
|
8
|
+
}, prepareAgentConfig?: PrepareAgentConfig): Promise<JsonValue>;
|
|
9
|
+
};
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { RuntimeError, sameWorkspaceRootUri, streamsRequiredForCommit, teamRootId, validatePlannerConfig, validateTeamConfig, } from '@saturn-universe/core';
|
|
2
|
+
export function createCommandHandler(d) {
|
|
3
|
+
return {
|
|
4
|
+
async handle(commandId, command, identity, prepareAgentConfig) {
|
|
5
|
+
if (command.type.endsWith('.query'))
|
|
6
|
+
return query((await (d.reader.admission ? d.reader.admission(command) : d.reader.snapshot())).state, command);
|
|
7
|
+
if (!['workspace.create', 'session.create', 'run.submit'].includes(command.type))
|
|
8
|
+
throw new RuntimeError('UNSUPPORTED', `M1 does not execute ${command.type}`);
|
|
9
|
+
const commandHash = identity?.hash ?? d.hash(command);
|
|
10
|
+
const previous = await d.receipt(commandId, commandHash);
|
|
11
|
+
if (previous)
|
|
12
|
+
return result(previous);
|
|
13
|
+
const { state } = await (d.reader.admission ? d.reader.admission(command) : d.reader.snapshot());
|
|
14
|
+
const events = decide(state, command, d, identity?.version, prepareAgentConfig);
|
|
15
|
+
const streams = streamsRequiredForCommit(events);
|
|
16
|
+
const receipt = await d.commit.commit({
|
|
17
|
+
commandId,
|
|
18
|
+
commandHash,
|
|
19
|
+
correlationId: commandId,
|
|
20
|
+
actor: { kind: 'user', id: 'local' },
|
|
21
|
+
expected: [...streams.entries()].map(([key, stream]) => ({
|
|
22
|
+
stream,
|
|
23
|
+
lastSeq: state.streams[key] ?? 0,
|
|
24
|
+
})),
|
|
25
|
+
events,
|
|
26
|
+
hostEpoch: d.epoch,
|
|
27
|
+
});
|
|
28
|
+
return result(receipt);
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function decide(s, c, d, identityVersion, prepareAgentConfig) {
|
|
33
|
+
const occurredAt = d.now();
|
|
34
|
+
const event = (type, scope, payload) => ({
|
|
35
|
+
id: d.id(),
|
|
36
|
+
type,
|
|
37
|
+
eventVersion: 1,
|
|
38
|
+
scope,
|
|
39
|
+
payload,
|
|
40
|
+
occurredAt,
|
|
41
|
+
causationId: null,
|
|
42
|
+
});
|
|
43
|
+
switch (c.type) {
|
|
44
|
+
case 'workspace.create': {
|
|
45
|
+
if (!sameWorkspaceRootUri(c.rootUri, d.rootUri) || Object.keys(s.workspaces).length)
|
|
46
|
+
throw new RuntimeError('INVALID_TRANSITION', 'workspace already exists or root differs');
|
|
47
|
+
const workspaceId = d.id();
|
|
48
|
+
return [
|
|
49
|
+
event('workspace.created', { kind: 'workspace', workspaceId }, { workspace: { id: workspaceId, rootUri: c.rootUri, status: 'open', createdAt: occurredAt } }),
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
case 'session.create': {
|
|
53
|
+
const w = s.workspaces[c.workspaceId];
|
|
54
|
+
if (!w || w.status !== 'open')
|
|
55
|
+
throw new RuntimeError('NOT_FOUND', 'Workspace not open');
|
|
56
|
+
const workspaceId = c.workspaceId;
|
|
57
|
+
const agentId = d.id();
|
|
58
|
+
const sessionId = d.id();
|
|
59
|
+
const agentSessionId = d.id();
|
|
60
|
+
return [
|
|
61
|
+
event('agent.created', { kind: 'workspace', workspaceId }, {
|
|
62
|
+
agent: {
|
|
63
|
+
id: agentId,
|
|
64
|
+
workspaceId,
|
|
65
|
+
role: 'main',
|
|
66
|
+
defaultScope: 'session',
|
|
67
|
+
modeRef: 'm1',
|
|
68
|
+
createdAt: occurredAt,
|
|
69
|
+
},
|
|
70
|
+
}),
|
|
71
|
+
event('session.created', { kind: 'session', workspaceId, sessionId }, { session: { id: sessionId, workspaceId, title: c.title, status: 'open', createdAt: occurredAt } }),
|
|
72
|
+
event('agent_session.created', { kind: 'agent_session', workspaceId, sessionId, agentId, agentSessionId }, {
|
|
73
|
+
agentSession: {
|
|
74
|
+
id: agentSessionId,
|
|
75
|
+
workspaceId,
|
|
76
|
+
agentId,
|
|
77
|
+
owner: { scope: 'session', sessionId },
|
|
78
|
+
status: 'open',
|
|
79
|
+
createdAt: occurredAt,
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
event('session.agent.bound', { kind: 'session', workspaceId, sessionId }, { binding: { sessionId, agentId, agentSessionId, role: 'main' } }),
|
|
83
|
+
];
|
|
84
|
+
}
|
|
85
|
+
case 'run.submit': {
|
|
86
|
+
const input = c.input;
|
|
87
|
+
const team = input.config.team;
|
|
88
|
+
const planner = input.config.planner;
|
|
89
|
+
if (team !== undefined && planner !== undefined)
|
|
90
|
+
throw new RuntimeError('INVALID_CONFIG', 'team and planner cannot both be set');
|
|
91
|
+
if (team !== undefined)
|
|
92
|
+
validateTeamConfig(team);
|
|
93
|
+
if (planner !== undefined) {
|
|
94
|
+
validatePlannerConfig(planner);
|
|
95
|
+
if (!input.plannerConfig)
|
|
96
|
+
throw new RuntimeError('INVALID_CONFIG', 'plannerConfig is required when planner is set');
|
|
97
|
+
}
|
|
98
|
+
const session = s.sessions[input.sessionId];
|
|
99
|
+
if (!session || session.workspaceId !== input.workspaceId || session.status !== 'open')
|
|
100
|
+
throw new RuntimeError('NOT_FOUND', 'Session not open');
|
|
101
|
+
const binding = Object.values(s.bindings).find((b) => b.sessionId === input.sessionId && b.role === 'main');
|
|
102
|
+
if (!binding)
|
|
103
|
+
throw new RuntimeError('INVALID_TRANSITION', 'Session has no main Agent');
|
|
104
|
+
const { agentId, agentSessionId } = binding;
|
|
105
|
+
const mainConfig = prepareAgentConfig?.(input.config, 'main', agentSessionId) ?? input.config;
|
|
106
|
+
const runId = d.id();
|
|
107
|
+
const turnId = d.id();
|
|
108
|
+
const messageId = d.id();
|
|
109
|
+
const { workspaceId, sessionId } = input;
|
|
110
|
+
const runScope = { kind: 'run', workspaceId, sessionId, runId };
|
|
111
|
+
const agentScope = {
|
|
112
|
+
kind: 'agent_session',
|
|
113
|
+
workspaceId,
|
|
114
|
+
sessionId,
|
|
115
|
+
runId,
|
|
116
|
+
agentId,
|
|
117
|
+
agentSessionId,
|
|
118
|
+
};
|
|
119
|
+
const nextOrdinal = Object.values(s.turns)
|
|
120
|
+
.filter((t) => t.agentSessionId === agentSessionId)
|
|
121
|
+
.reduce((max, t) => Math.max(max, t.ordinal), 0) + 1;
|
|
122
|
+
return [
|
|
123
|
+
event('run.created', runScope, {
|
|
124
|
+
runId,
|
|
125
|
+
inputMessageId: messageId,
|
|
126
|
+
workingDirectoryUri: input.workingDirectoryUri ?? d.rootUri,
|
|
127
|
+
...(identityVersion ? { submissionIdentityVersion: identityVersion } : {}),
|
|
128
|
+
}),
|
|
129
|
+
event('run.agent.attached', runScope, {
|
|
130
|
+
membership: {
|
|
131
|
+
runId,
|
|
132
|
+
agentId,
|
|
133
|
+
agentSessionId,
|
|
134
|
+
role: 'main',
|
|
135
|
+
config: mainConfig,
|
|
136
|
+
...(team ? { teamMemberId: teamRootId } : {}),
|
|
137
|
+
},
|
|
138
|
+
}),
|
|
139
|
+
...(team?.members.flatMap(({ id: teamMemberId }) => {
|
|
140
|
+
const agentId = d.id();
|
|
141
|
+
const agentSessionId = d.id();
|
|
142
|
+
const workerConfig = prepareAgentConfig?.(input.config, 'worker', agentSessionId) ?? input.config;
|
|
143
|
+
return [
|
|
144
|
+
event('agent.created', { kind: 'workspace', workspaceId }, {
|
|
145
|
+
agent: {
|
|
146
|
+
id: agentId,
|
|
147
|
+
workspaceId,
|
|
148
|
+
role: 'worker',
|
|
149
|
+
defaultScope: 'run',
|
|
150
|
+
modeRef: workerConfig.modeRef,
|
|
151
|
+
createdAt: occurredAt,
|
|
152
|
+
},
|
|
153
|
+
}),
|
|
154
|
+
event('agent_session.created', { ...agentScope, agentId, agentSessionId }, {
|
|
155
|
+
agentSession: {
|
|
156
|
+
id: agentSessionId,
|
|
157
|
+
workspaceId,
|
|
158
|
+
agentId,
|
|
159
|
+
owner: { scope: 'run', sessionId, runId },
|
|
160
|
+
status: 'open',
|
|
161
|
+
createdAt: occurredAt,
|
|
162
|
+
},
|
|
163
|
+
}),
|
|
164
|
+
event('run.agent.attached', runScope, {
|
|
165
|
+
membership: {
|
|
166
|
+
runId,
|
|
167
|
+
agentId,
|
|
168
|
+
agentSessionId,
|
|
169
|
+
role: 'worker',
|
|
170
|
+
config: workerConfig,
|
|
171
|
+
teamMemberId,
|
|
172
|
+
},
|
|
173
|
+
}),
|
|
174
|
+
];
|
|
175
|
+
}) ?? []),
|
|
176
|
+
...(planner && input.plannerConfig
|
|
177
|
+
? (() => {
|
|
178
|
+
const plannerAgentId = d.id();
|
|
179
|
+
const plannerAgentSessionId = d.id();
|
|
180
|
+
const plannerConfig = prepareAgentConfig?.(input.plannerConfig, 'planner', plannerAgentSessionId) ??
|
|
181
|
+
input.plannerConfig;
|
|
182
|
+
return [
|
|
183
|
+
event('agent.created', { kind: 'workspace', workspaceId }, {
|
|
184
|
+
agent: {
|
|
185
|
+
id: plannerAgentId,
|
|
186
|
+
workspaceId,
|
|
187
|
+
role: 'specialist',
|
|
188
|
+
defaultScope: 'run',
|
|
189
|
+
modeRef: plannerConfig.modeRef,
|
|
190
|
+
createdAt: occurredAt,
|
|
191
|
+
},
|
|
192
|
+
}),
|
|
193
|
+
event('agent_session.created', { ...agentScope, agentId: plannerAgentId, agentSessionId: plannerAgentSessionId }, {
|
|
194
|
+
agentSession: {
|
|
195
|
+
id: plannerAgentSessionId,
|
|
196
|
+
workspaceId,
|
|
197
|
+
agentId: plannerAgentId,
|
|
198
|
+
owner: { scope: 'run', sessionId, runId },
|
|
199
|
+
status: 'open',
|
|
200
|
+
createdAt: occurredAt,
|
|
201
|
+
},
|
|
202
|
+
}),
|
|
203
|
+
event('run.agent.attached', runScope, {
|
|
204
|
+
membership: {
|
|
205
|
+
runId,
|
|
206
|
+
agentId: plannerAgentId,
|
|
207
|
+
agentSessionId: plannerAgentSessionId,
|
|
208
|
+
role: 'worker',
|
|
209
|
+
config: plannerConfig,
|
|
210
|
+
},
|
|
211
|
+
}),
|
|
212
|
+
];
|
|
213
|
+
})()
|
|
214
|
+
: []),
|
|
215
|
+
event('turn.queued', agentScope, { turnId, ordinal: nextOrdinal, inputMessageId: messageId }),
|
|
216
|
+
event('message.user.added', agentScope, {
|
|
217
|
+
message: { id: messageId, agentSessionId, runId, turnId, role: 'user', content: input.content },
|
|
218
|
+
}),
|
|
219
|
+
];
|
|
220
|
+
}
|
|
221
|
+
default:
|
|
222
|
+
throw new RuntimeError('UNSUPPORTED', 'command is not implemented');
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function result(receipt) {
|
|
226
|
+
const run = receipt.events.find((e) => e.type === 'run.created');
|
|
227
|
+
const turn = receipt.events.find((e) => e.type === 'turn.queued');
|
|
228
|
+
const session = receipt.events.find((e) => e.type === 'session.created');
|
|
229
|
+
const workspace = receipt.events.find((e) => e.type === 'workspace.created');
|
|
230
|
+
return {
|
|
231
|
+
...(workspace ? { workspaceId: workspace.payload.workspace.id } : {}),
|
|
232
|
+
...(session ? { sessionId: session.payload.session.id } : {}),
|
|
233
|
+
...(run ? { runId: run.payload.runId } : {}),
|
|
234
|
+
...(turn ? { turnId: turn.payload.turnId } : {}),
|
|
235
|
+
cursor: receipt.lastPosition,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
function query(s, c) {
|
|
239
|
+
let value;
|
|
240
|
+
switch (c.type) {
|
|
241
|
+
case 'workspace.query':
|
|
242
|
+
value = Object.values(s.workspaces);
|
|
243
|
+
break;
|
|
244
|
+
case 'session.query':
|
|
245
|
+
value = s.sessions[c.sessionId];
|
|
246
|
+
break;
|
|
247
|
+
case 'run.query':
|
|
248
|
+
value = s.runStates[c.runId];
|
|
249
|
+
break;
|
|
250
|
+
case 'surface.query': {
|
|
251
|
+
const surface = s.surfaces[c.agentSessionId];
|
|
252
|
+
value = surface
|
|
253
|
+
? {
|
|
254
|
+
agentSessionId: surface.agentSessionId,
|
|
255
|
+
items: surface.items,
|
|
256
|
+
groups: surface.groups,
|
|
257
|
+
lastStreamSeq: surface.lastStreamSeq,
|
|
258
|
+
throughPosition: surface.throughPosition,
|
|
259
|
+
}
|
|
260
|
+
: undefined;
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
default:
|
|
264
|
+
throw new RuntimeError('UNSUPPORTED', 'query is not implemented');
|
|
265
|
+
}
|
|
266
|
+
if (value === undefined)
|
|
267
|
+
throw new RuntimeError('NOT_FOUND', 'entity not found');
|
|
268
|
+
return JSON.parse(JSON.stringify(value));
|
|
269
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { AgentExecutionScope, CommandId, ExecutionEnvironmentReader, Hash, HostCommand, LoggerPort, ModelGateway, RunConfig, RuntimeJournal, RuntimeStateReader, SurfaceCompactionReceipt, SurfaceItem, SurfaceReader, SurfaceUnit, TurnId } from '@saturn-universe/core';
|
|
2
|
+
import type { CommandDependencies } from './command-dependencies.js';
|
|
3
|
+
export interface AutoCompactionOptions {
|
|
4
|
+
readonly enabled?: boolean;
|
|
5
|
+
readonly thresholdRatio?: number;
|
|
6
|
+
readonly maxHeadroomTokens?: number;
|
|
7
|
+
readonly headroomRatio?: number;
|
|
8
|
+
readonly minRecentSteps?: number;
|
|
9
|
+
readonly tailWindowRatio?: number;
|
|
10
|
+
readonly preflightWindowRatio?: number;
|
|
11
|
+
readonly maxHalvings?: number;
|
|
12
|
+
readonly minPrefixChars?: number;
|
|
13
|
+
readonly summaryTimeoutMs?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface PreflightSummaryPlan {
|
|
16
|
+
readonly units: readonly SurfaceUnit[];
|
|
17
|
+
readonly items: readonly SurfaceItem[];
|
|
18
|
+
readonly transcript: string;
|
|
19
|
+
readonly rawPrefixChars: number;
|
|
20
|
+
readonly estimatedTokens: number;
|
|
21
|
+
readonly halvings: number;
|
|
22
|
+
}
|
|
23
|
+
export type AutoCompactionOutcome = {
|
|
24
|
+
readonly compacted: true;
|
|
25
|
+
readonly compactionId: string;
|
|
26
|
+
readonly replacedCount: number;
|
|
27
|
+
readonly halvings: number;
|
|
28
|
+
readonly boundary: 'turn' | 'step_prefix';
|
|
29
|
+
} | {
|
|
30
|
+
readonly compacted: false;
|
|
31
|
+
readonly reason: string;
|
|
32
|
+
};
|
|
33
|
+
export declare function resolveAutoCompactionOptions(options?: boolean | AutoCompactionOptions): Required<AutoCompactionOptions>;
|
|
34
|
+
export declare function serializeItemsForSummary(items: readonly SurfaceItem[], maxToolChars?: number): string;
|
|
35
|
+
export declare function preparePreflightSummaryPlan(units: readonly SurfaceUnit[], summaryContextWindow: number, options?: Pick<AutoCompactionOptions, 'preflightWindowRatio' | 'maxHalvings'>): PreflightSummaryPlan | undefined;
|
|
36
|
+
export declare function generateCompactionSummary(deps: {
|
|
37
|
+
readonly gateway: ModelGateway;
|
|
38
|
+
readonly hash: Hash;
|
|
39
|
+
readonly id: () => string;
|
|
40
|
+
readonly logger?: LoggerPort;
|
|
41
|
+
}, input: {
|
|
42
|
+
readonly runId: AgentExecutionScope['runId'];
|
|
43
|
+
readonly turnId: TurnId;
|
|
44
|
+
readonly config: RunConfig;
|
|
45
|
+
readonly units: readonly SurfaceUnit[];
|
|
46
|
+
readonly signal: AbortSignal;
|
|
47
|
+
readonly options?: Pick<AutoCompactionOptions, 'preflightWindowRatio' | 'maxHalvings' | 'summaryTimeoutMs'>;
|
|
48
|
+
}): Promise<{
|
|
49
|
+
readonly ok: true;
|
|
50
|
+
readonly summary: string;
|
|
51
|
+
readonly plan: PreflightSummaryPlan;
|
|
52
|
+
readonly modelRef: string;
|
|
53
|
+
} | {
|
|
54
|
+
readonly ok: false;
|
|
55
|
+
readonly reason: string;
|
|
56
|
+
}>;
|
|
57
|
+
export declare function autoCompactSurface(deps: {
|
|
58
|
+
readonly surface: SurfaceReader;
|
|
59
|
+
readonly state: RuntimeStateReader;
|
|
60
|
+
readonly journal: RuntimeJournal;
|
|
61
|
+
readonly gateway: ModelGateway;
|
|
62
|
+
readonly hash: Hash;
|
|
63
|
+
readonly id: () => string;
|
|
64
|
+
readonly logger?: LoggerPort;
|
|
65
|
+
}, input: {
|
|
66
|
+
readonly route: AgentExecutionScope;
|
|
67
|
+
readonly turnId: TurnId;
|
|
68
|
+
readonly config: RunConfig;
|
|
69
|
+
readonly contextWindow: number;
|
|
70
|
+
readonly signal: AbortSignal;
|
|
71
|
+
readonly options?: AutoCompactionOptions;
|
|
72
|
+
}): Promise<AutoCompactionOutcome>;
|
|
73
|
+
export declare function compactSurface(d: Pick<CommandDependencies, 'commit' | 'receipt' | 'hash' | 'id' | 'now' | 'epoch'> & {
|
|
74
|
+
environment: ExecutionEnvironmentReader;
|
|
75
|
+
surfaceReader: SurfaceReader;
|
|
76
|
+
stateReader: RuntimeStateReader;
|
|
77
|
+
gateway?: ModelGateway;
|
|
78
|
+
logger?: LoggerPort;
|
|
79
|
+
}, commandId: CommandId, command: Extract<HostCommand, {
|
|
80
|
+
type: 'surface.compact';
|
|
81
|
+
}>): Promise<SurfaceCompactionReceipt>;
|