@saturn-universe/projections 0.1.1 → 0.1.2-dev.10
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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/project.d.ts +1 -0
- package/dist/project.js +23 -2
- package/dist/team.d.ts +1 -0
- package/dist/team.js +44 -9
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { projectBatch, projectBatchWithChanges, createCoreProjections } from './project.js';
|
|
1
|
+
export { projectBatch, projectBatchWithChanges, createCoreProjections, sessionHasActiveRun, } from './project.js';
|
|
2
2
|
export type { ProjectionChanges } from './project.js';
|
|
3
3
|
export { coreProjectionName, coreProjectionVersion, initialProjection } from './state.js';
|
|
4
4
|
export type { ExecutionFact, ModelFact, ProjectionCollection, ProjectionIndexes, ProjectionState, } from './state.js';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { projectBatch, projectBatchWithChanges, createCoreProjections } from './project.js';
|
|
1
|
+
export { projectBatch, projectBatchWithChanges, createCoreProjections, sessionHasActiveRun, } from './project.js';
|
|
2
2
|
export { coreProjectionName, coreProjectionVersion, initialProjection } from './state.js';
|
|
3
3
|
export { initialSurface, reduceSurface } from './surface.js';
|
package/dist/project.d.ts
CHANGED
|
@@ -18,3 +18,4 @@ export declare function createCoreProjections(hash: Hash): {
|
|
|
18
18
|
initial: typeof initialProjection;
|
|
19
19
|
reduce: (state: ProjectionState, events: readonly RuntimeEvent[]) => ProjectionState;
|
|
20
20
|
};
|
|
21
|
+
export declare function sessionHasActiveRun(s: ProjectionState, sessionId: string): boolean;
|
package/dist/project.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { acceptsModelResponse, canonicalJson, earlierDeadline, isLimited, isTerminalRunEventType, isTerminalRunStatus, ownerAllowsRoute, resolveSubagentWallTimeMs, settledModelTokens, streamKey, RuntimeError, transitionRun, } from '@saturn-universe/core';
|
|
1
|
+
import { acceptsModelResponse, canonicalJson, contextExtensionRefPattern, earlierDeadline, isLimited, isTerminalRunEventType, isTerminalRunStatus, ownerAllowsRoute, resolveSubagentWallTimeMs, settledModelTokens, streamKey, RuntimeError, transitionRun, } from '@saturn-universe/core';
|
|
2
2
|
import { initialSurface, reduceSurface } from './surface.js';
|
|
3
3
|
import { coreProjectionName, coreProjectionVersion, emptyIndexes, initialProjection, projectionCollections, } from './state.js';
|
|
4
|
-
import { attachTeamMember, finishTeam, isDegradedWorkerTurn, planTeam, projectTeamMessage, projectTeamTurn, } from './team.js';
|
|
4
|
+
import { attachTeamMember, finishTeam, isDegradedWorkerTurn, planTeam, projectTeamMessage, projectTeamStopRequest, projectTeamTurn, } from './team.js';
|
|
5
5
|
import { reduceSearchEvent } from './search.js';
|
|
6
6
|
export function projectBatch(previous, events, hash) {
|
|
7
7
|
return projectBatchWithChanges(previous, events, hash).state;
|
|
@@ -297,6 +297,7 @@ function applyEvent(s, e, hash) {
|
|
|
297
297
|
case 'agent.created':
|
|
298
298
|
case 'session.created':
|
|
299
299
|
case 'session.agent.bound':
|
|
300
|
+
case 'session.mode.selected':
|
|
300
301
|
case 'session.archived':
|
|
301
302
|
case 'agent_session.created':
|
|
302
303
|
case 'agent_session.archived':
|
|
@@ -323,6 +324,9 @@ function applyEvent(s, e, hash) {
|
|
|
323
324
|
case 'run.team.planned':
|
|
324
325
|
planTeam(s, e, r);
|
|
325
326
|
break;
|
|
327
|
+
case 'run.team.member.stop_requested':
|
|
328
|
+
projectTeamStopRequest(s, e, r);
|
|
329
|
+
break;
|
|
326
330
|
case 'run.team.message.sent':
|
|
327
331
|
case 'run.team.message.read':
|
|
328
332
|
projectTeamMessage(s, e, r);
|
|
@@ -406,6 +410,9 @@ function reduceWorkspaceEvent(s, e, r) {
|
|
|
406
410
|
}
|
|
407
411
|
return r;
|
|
408
412
|
}
|
|
413
|
+
export function sessionHasActiveRun(s, sessionId) {
|
|
414
|
+
return someRuns(s, s.indexes.runIdsBySession[sessionId], (run) => !isTerminalRunStatus(run.status));
|
|
415
|
+
}
|
|
409
416
|
function reduceSessionEvent(s, e, r) {
|
|
410
417
|
switch (e.type) {
|
|
411
418
|
case 'agent.created':
|
|
@@ -424,6 +431,16 @@ function reduceSessionEvent(s, e, r) {
|
|
|
424
431
|
add(s.bindings, `${b.sessionId}:${b.agentSessionId}`, b);
|
|
425
432
|
break;
|
|
426
433
|
}
|
|
434
|
+
case 'session.mode.selected': {
|
|
435
|
+
const { sessionId } = e.scope;
|
|
436
|
+
const session = s.sessions[sessionId];
|
|
437
|
+
if (session.status !== 'open' || !contextExtensionRefPattern.test(e.payload.modeRef))
|
|
438
|
+
invalid('invalid session mode selection');
|
|
439
|
+
if (sessionHasActiveRun(s, sessionId))
|
|
440
|
+
invalid('session has active work');
|
|
441
|
+
s.sessions[sessionId] = { ...session, modeRef: e.payload.modeRef };
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
427
444
|
case 'session.archived': {
|
|
428
445
|
const id = e.scope.sessionId;
|
|
429
446
|
if (s.sessions[id].status !== 'open')
|
|
@@ -505,6 +522,9 @@ function reduceRunEvent(s, e, r) {
|
|
|
505
522
|
invalid('stream has unresolved calls');
|
|
506
523
|
if (m.role === 'main' && s.indexes.mainMembershipByRun[m.runId] !== undefined)
|
|
507
524
|
invalid('duplicate run main');
|
|
525
|
+
const selectedModeRef = s.sessions[e.scope.sessionId]?.modeRef;
|
|
526
|
+
if (m.role === 'main' && selectedModeRef && m.config.modeRef !== selectedModeRef)
|
|
527
|
+
invalid('Run Mode does not match the session selection');
|
|
508
528
|
if (m.config.checkpoint) {
|
|
509
529
|
const { sourceRunId, originalInputId } = m.config.checkpoint;
|
|
510
530
|
const source = s.memberships[`${sourceRunId}:${m.agentSessionId}`];
|
|
@@ -539,6 +559,7 @@ function reduceRunEvent(s, e, r) {
|
|
|
539
559
|
invalid('run has pending calls');
|
|
540
560
|
if (e.type === 'run.completed') {
|
|
541
561
|
if (team?.members.some((member) => member.status !== 'completed' &&
|
|
562
|
+
member.status !== 'stopped' &&
|
|
542
563
|
!(member.parent !== null && isDegradedWorkerTurn(s, member.turnId))))
|
|
543
564
|
invalid('team has unfinished members');
|
|
544
565
|
const main = s.memberships[`${id}:${s.indexes.mainMembershipByRun[id]}`];
|
package/dist/team.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ type TurnEvent = RuntimeEvent<'turn.queued' | 'turn.started' | 'turn.paused' | '
|
|
|
5
5
|
export declare function planTeam(s: ProjectionState, e: RuntimeEvent<'run.team.planned'>, run: RunState | undefined): void;
|
|
6
6
|
export declare function attachTeamMember(s: ProjectionState, m: RunAgent): void;
|
|
7
7
|
export declare function projectTeamTurn(s: ProjectionState, turn: Turn, e: TurnEvent): void;
|
|
8
|
+
export declare function projectTeamStopRequest(s: ProjectionState, e: RuntimeEvent<'run.team.member.stop_requested'>, run: RunState | undefined): void;
|
|
8
9
|
export declare function projectTeamMessage(s: ProjectionState, e: MessageEvent, run: RunState | undefined): void;
|
|
9
10
|
export declare function finishTeam(team: TeamState): TeamState;
|
|
10
11
|
export declare function isDegradedWorkerTurn(s: ProjectionState, turnId: string | undefined): boolean;
|
package/dist/team.js
CHANGED
|
@@ -103,9 +103,7 @@ export function planTeam(s, e, run) {
|
|
|
103
103
|
? teamConcurrency(team)
|
|
104
104
|
: (current?.concurrency ?? teamConcurrency(team));
|
|
105
105
|
const mergedConcurrency = asyncCoordinator && current ? Math.max(current.concurrency, incomingConcurrency) : incomingConcurrency;
|
|
106
|
-
const cappedConcurrency = mainMembership.config.
|
|
107
|
-
? Math.min(mergedConcurrency, mainMembership.config.teamMaxConcurrency)
|
|
108
|
-
: mergedConcurrency;
|
|
106
|
+
const cappedConcurrency = resolveEffectiveTeamConcurrency(mainMembership.config.teamDefaultConcurrency !== undefined ? team.concurrency : mergedConcurrency, mainMembership.config);
|
|
109
107
|
s.teams[runId] = {
|
|
110
108
|
runId,
|
|
111
109
|
concurrency: Math.max(1, cappedConcurrency),
|
|
@@ -141,7 +139,9 @@ export function attachTeamMember(s, m) {
|
|
|
141
139
|
invalid('invalid team root');
|
|
142
140
|
s.teams[m.runId] = {
|
|
143
141
|
runId: m.runId,
|
|
144
|
-
concurrency:
|
|
142
|
+
concurrency: m.config.teamDefaultConcurrency === undefined
|
|
143
|
+
? teamConcurrency(config)
|
|
144
|
+
: resolveEffectiveTeamConcurrency(config.concurrency, m.config),
|
|
145
145
|
members: [{ id: teamRootId, parent: null, agentSessionId: m.agentSessionId, status: 'waiting' }],
|
|
146
146
|
tasks: config.members.map(({ id, task }) => ({ memberId: id, task, status: 'waiting' })),
|
|
147
147
|
inbox: [],
|
|
@@ -197,9 +197,12 @@ export function attachTeamMember(s, m) {
|
|
|
197
197
|
const tasksMatch = asyncCoordinator
|
|
198
198
|
? expectedTasks.every((expected) => currentTasks.some((cur) => cur.memberId === expected.memberId && cur.task === expected.task))
|
|
199
199
|
: canonicalJson(expectedTasks) === canonicalJson(currentTasks);
|
|
200
|
+
const concurrency = rootConfig.teamDefaultConcurrency === undefined
|
|
201
|
+
? teamConcurrency(config)
|
|
202
|
+
: resolveEffectiveTeamConcurrency(config.concurrency, rootConfig);
|
|
200
203
|
const concurrencyValid = asyncCoordinator
|
|
201
|
-
?
|
|
202
|
-
:
|
|
204
|
+
? concurrency <= current.concurrency
|
|
205
|
+
: concurrency === current.concurrency;
|
|
203
206
|
if (!worker ||
|
|
204
207
|
!concurrencyValid ||
|
|
205
208
|
!tasksMatch ||
|
|
@@ -255,7 +258,10 @@ export function projectTeamTurn(s, turn, e) {
|
|
|
255
258
|
? s.memberships[`${turn.runId}:${mainAgentSessionId}`]?.config
|
|
256
259
|
: undefined;
|
|
257
260
|
const asyncCoordinator = mainConfig ? isAsyncCoordinatorConfig(mainConfig) : false;
|
|
258
|
-
|
|
261
|
+
// Older event logs may contain starts above the recorded team concurrency.
|
|
262
|
+
const effectiveConcurrency = asyncCoordinator && mainConfig?.teamDefaultConcurrency === undefined
|
|
263
|
+
? (mainConfig?.teamMaxConcurrency ?? team.concurrency)
|
|
264
|
+
: resolveEffectiveTeamConcurrency(team.concurrency, mainConfig);
|
|
259
265
|
if (e.type === 'turn.started' || e.type === 'turn.resumed') {
|
|
260
266
|
if (asyncCoordinator) {
|
|
261
267
|
if (member.id !== teamRootId &&
|
|
@@ -269,7 +275,7 @@ export function projectTeamTurn(s, turn, e) {
|
|
|
269
275
|
}
|
|
270
276
|
if (e.type === 'turn.started' && !(member.id === teamRootId && asyncCoordinator)) {
|
|
271
277
|
for (const child of team.members.filter(({ parent }) => parent === member.id)) {
|
|
272
|
-
const childFinished = child.status === 'completed' || isDegradedWorkerTurn(s, child.turnId);
|
|
278
|
+
const childFinished = child.status === 'completed' || child.status === 'stopped' || isDegradedWorkerTurn(s, child.turnId);
|
|
273
279
|
if (!childFinished ||
|
|
274
280
|
!team.inbox.some(({ messageId, from, to, readAt }) => from === child.id &&
|
|
275
281
|
to === member.id &&
|
|
@@ -278,7 +284,11 @@ export function projectTeamTurn(s, turn, e) {
|
|
|
278
284
|
invalid('team member is waiting for child reports');
|
|
279
285
|
}
|
|
280
286
|
}
|
|
281
|
-
const
|
|
287
|
+
const stoppedByMain = e.type === 'turn.interrupted' && e.payload.stoppedByMain;
|
|
288
|
+
if (stoppedByMain && (!member.stopRequest || e.payload.reason !== 'cancelled'))
|
|
289
|
+
invalid('member stop requires a coordinator request');
|
|
290
|
+
const status = stoppedByMain && s.runStates[turn.runId]?.status !== 'cancelling' ? 'stopped' : turn.status;
|
|
291
|
+
const progress = { turnId: turn.id, status };
|
|
282
292
|
s.teams[turn.runId] = {
|
|
283
293
|
...team,
|
|
284
294
|
members: team.members.map((item) => (item === member ? { ...item, ...progress } : item)),
|
|
@@ -291,6 +301,31 @@ export function projectTeamTurn(s, turn, e) {
|
|
|
291
301
|
: task),
|
|
292
302
|
};
|
|
293
303
|
}
|
|
304
|
+
export function projectTeamStopRequest(s, e, run) {
|
|
305
|
+
const { runId } = e.scope;
|
|
306
|
+
const team = s.teams[runId];
|
|
307
|
+
const { memberId, turnId, requesterAgentSessionId, requestedBy, reason } = e.payload;
|
|
308
|
+
const main = team?.members.find(({ id }) => id === teamRootId);
|
|
309
|
+
const config = main && s.memberships[`${runId}:${main.agentSessionId}`]?.config;
|
|
310
|
+
const worker = team?.members.find(({ id }) => id === memberId);
|
|
311
|
+
if (!team ||
|
|
312
|
+
run?.status !== 'running' ||
|
|
313
|
+
main?.agentSessionId !== requesterAgentSessionId ||
|
|
314
|
+
main.status !== 'running' ||
|
|
315
|
+
!config ||
|
|
316
|
+
!isAsyncCoordinatorConfig(config) ||
|
|
317
|
+
!worker?.parent ||
|
|
318
|
+
worker.status !== 'running' ||
|
|
319
|
+
worker.turnId !== turnId ||
|
|
320
|
+
worker.stopRequest ||
|
|
321
|
+
!reason.trim() ||
|
|
322
|
+
reason.length > 4000)
|
|
323
|
+
invalid('member stop requires a running worker and the main coordinator');
|
|
324
|
+
s.teams[runId] = {
|
|
325
|
+
...team,
|
|
326
|
+
members: team.members.map((member) => member === worker ? { ...member, stopRequest: { requestedBy, reason } } : member),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
294
329
|
export function projectTeamMessage(s, e, run) {
|
|
295
330
|
const team = s.teams[e.scope.runId];
|
|
296
331
|
if (!team || run?.status !== 'running')
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saturn-universe/projections",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2-dev.10",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
8
8
|
],
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@saturn-universe/core": "0.1.
|
|
10
|
+
"@saturn-universe/core": "0.1.2-dev.10"
|
|
11
11
|
},
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|