@praudent/dagma 0.0.0-stage → 0.1.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/dist/admin-ui/assets/index-CHJ8lsZp.js +120 -0
- package/dist/admin-ui/assets/index-XyqtD-IA.css +1 -0
- package/dist/admin-ui/index.html +12 -0
- package/dist/blueprint/agent.d.ts +148 -0
- package/dist/blueprint/agent.js +1 -0
- package/dist/blueprint/declaration-site.d.ts +7 -0
- package/dist/blueprint/declaration-site.js +8 -0
- package/dist/blueprint/declared-structure.d.ts +145 -0
- package/dist/blueprint/declared-structure.js +213 -0
- package/dist/blueprint/define-run-state.d.ts +79 -0
- package/dist/blueprint/define-run-state.js +89 -0
- package/dist/blueprint/load-blueprint.d.ts +28 -0
- package/dist/blueprint/load-blueprint.js +85 -0
- package/dist/blueprint/outcomes.d.ts +76 -0
- package/dist/blueprint/outcomes.js +10 -0
- package/dist/blueprint/phase.d.ts +153 -0
- package/dist/blueprint/phase.js +1 -0
- package/dist/blueprint/places.d.ts +26 -0
- package/dist/blueprint/places.js +37 -0
- package/dist/blueprint/prompt-file.d.ts +81 -0
- package/dist/blueprint/prompt-file.js +131 -0
- package/dist/blueprint/reply-reader.d.ts +36 -0
- package/dist/blueprint/reply-reader.js +38 -0
- package/dist/blueprint/run-settings.d.ts +38 -0
- package/dist/blueprint/run-settings.js +1 -0
- package/dist/blueprint/serializer.d.ts +14 -0
- package/dist/blueprint/serializer.js +4 -0
- package/dist/blueprint/write-scope.d.ts +39 -0
- package/dist/blueprint/write-scope.js +106 -0
- package/dist/command.d.ts +2 -0
- package/dist/command.js +148 -0
- package/dist/commands/calls-answering.d.ts +16 -0
- package/dist/commands/calls-answering.js +63 -0
- package/dist/commands/json-file.d.ts +6 -0
- package/dist/commands/json-file.js +19 -0
- package/dist/commands/run-command.d.ts +23 -0
- package/dist/commands/run-command.js +172 -0
- package/dist/commands/typed-answers.d.ts +10 -0
- package/dist/commands/typed-answers.js +43 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.js +20 -0
- package/dist/devtool-server/app-pages.d.ts +12 -0
- package/dist/devtool-server/app-pages.js +68 -0
- package/dist/devtool-server/blueprint-description.d.ts +21 -0
- package/dist/devtool-server/blueprint-description.js +27 -0
- package/dist/devtool-server/connected-runs.d.ts +36 -0
- package/dist/devtool-server/connected-runs.js +201 -0
- package/dist/devtool-server/devtool-server.d.ts +17 -0
- package/dist/devtool-server/devtool-server.js +294 -0
- package/dist/devtool-server/devtool-shapes.d.ts +312 -0
- package/dist/devtool-server/devtool-shapes.js +24 -0
- package/dist/devtool-server/listed-runs.d.ts +17 -0
- package/dist/devtool-server/listed-runs.js +42 -0
- package/dist/devtool-server/print-blueprint.d.ts +1 -0
- package/dist/devtool-server/print-blueprint.js +36 -0
- package/dist/example/added-species.d.ts +21 -0
- package/dist/example/added-species.js +19 -0
- package/dist/example/cover.d.ts +21 -0
- package/dist/example/cover.js +67 -0
- package/dist/example/cross-references.d.ts +21 -0
- package/dist/example/cross-references.js +77 -0
- package/dist/example/entry-drafts.d.ts +21 -0
- package/dist/example/entry-drafts.js +63 -0
- package/dist/example/entry-merge.d.ts +108 -0
- package/dist/example/entry-merge.js +284 -0
- package/dist/example/field-guide.d.ts +26 -0
- package/dist/example/field-guide.js +40 -0
- package/dist/example/guide-page.d.ts +21 -0
- package/dist/example/guide-page.js +15 -0
- package/dist/example/missing-species.d.ts +21 -0
- package/dist/example/missing-species.js +59 -0
- package/dist/example/run-state.d.ts +803 -0
- package/dist/example/run-state.js +58 -0
- package/dist/example/serializers.d.ts +32 -0
- package/dist/example/serializers.js +42 -0
- package/dist/example/species-list.d.ts +47 -0
- package/dist/example/species-list.js +93 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +25 -0
- package/dist/inference/anthropic-api.d.ts +37 -0
- package/dist/inference/anthropic-api.js +197 -0
- package/dist/inference/anthropic-prices.d.ts +18 -0
- package/dist/inference/anthropic-prices.js +39 -0
- package/dist/inference/anthropic-request.d.ts +51 -0
- package/dist/inference/anthropic-request.js +208 -0
- package/dist/inference/bedrock-api.d.ts +45 -0
- package/dist/inference/bedrock-api.js +242 -0
- package/dist/inference/bedrock-prices.d.ts +22 -0
- package/dist/inference/bedrock-prices.js +79 -0
- package/dist/inference/bedrock-request.d.ts +27 -0
- package/dist/inference/bedrock-request.js +193 -0
- package/dist/inference/call-queue-shapes.d.ts +273 -0
- package/dist/inference/call-queue-shapes.js +82 -0
- package/dist/inference/call-queue.d.ts +109 -0
- package/dist/inference/call-queue.js +408 -0
- package/dist/inference/gemini-api.d.ts +44 -0
- package/dist/inference/gemini-api.js +191 -0
- package/dist/inference/gemini-prices.d.ts +19 -0
- package/dist/inference/gemini-prices.js +33 -0
- package/dist/inference/gemini-request.d.ts +25 -0
- package/dist/inference/gemini-request.js +126 -0
- package/dist/inference/inference.d.ts +84 -0
- package/dist/inference/inference.js +9 -0
- package/dist/inference/openai-api.d.ts +29 -0
- package/dist/inference/openai-api.js +138 -0
- package/dist/inference/openai-prices.d.ts +12 -0
- package/dist/inference/openai-prices.js +25 -0
- package/dist/inference/openai-request.d.ts +50 -0
- package/dist/inference/openai-request.js +100 -0
- package/dist/inference/own-worker.d.ts +6 -0
- package/dist/inference/own-worker.js +16 -0
- package/dist/inference/scripted.d.ts +146 -0
- package/dist/inference/scripted.js +67 -0
- package/dist/run/assignments/agent-call.d.ts +32 -0
- package/dist/run/assignments/agent-call.js +218 -0
- package/dist/run/assignments/agent-phase.d.ts +14 -0
- package/dist/run/assignments/agent-phase.js +243 -0
- package/dist/run/assignments/assignment-memory.d.ts +42 -0
- package/dist/run/assignments/assignment-memory.js +52 -0
- package/dist/run/assignments/assignment.d.ts +17 -0
- package/dist/run/assignments/assignment.js +252 -0
- package/dist/run/assignments/model-settings.d.ts +39 -0
- package/dist/run/assignments/model-settings.js +57 -0
- package/dist/run/assignments/prepared-call.d.ts +28 -0
- package/dist/run/assignments/prepared-call.js +115 -0
- package/dist/run/assignments/tool-call.d.ts +26 -0
- package/dist/run/assignments/tool-call.js +108 -0
- package/dist/run/blueprint-mistake.d.ts +8 -0
- package/dist/run/blueprint-mistake.js +14 -0
- package/dist/run/code-phase.d.ts +11 -0
- package/dist/run/code-phase.js +59 -0
- package/dist/run/create-run.d.ts +36 -0
- package/dist/run/create-run.js +119 -0
- package/dist/run/execute-run.d.ts +7 -0
- package/dist/run/execute-run.js +158 -0
- package/dist/run/record/apply-event.d.ts +13 -0
- package/dist/run/record/apply-event.js +256 -0
- package/dist/run/record/events.d.ts +187 -0
- package/dist/run/record/events.js +1 -0
- package/dist/run/record/postgres-run-store.d.ts +13 -0
- package/dist/run/record/postgres-run-store.js +189 -0
- package/dist/run/record/progress-projection.d.ts +83 -0
- package/dist/run/record/progress-projection.js +167 -0
- package/dist/run/record/progress.d.ts +207 -0
- package/dist/run/record/progress.js +63 -0
- package/dist/run/record/run-store.d.ts +74 -0
- package/dist/run/record/run-store.js +1 -0
- package/dist/run/record/run-summary.d.ts +8 -0
- package/dist/run/record/run-summary.js +19 -0
- package/dist/run/record/run-types.d.ts +95 -0
- package/dist/run/record/run-types.js +1 -0
- package/dist/run/record/state-changes.d.ts +42 -0
- package/dist/run/record/state-changes.js +157 -0
- package/dist/run/resume-run.d.ts +25 -0
- package/dist/run/resume-run.js +99 -0
- package/dist/run/run-endings.d.ts +20 -0
- package/dist/run/run-endings.js +20 -0
- package/dist/run/run-option-shapes.d.ts +176 -0
- package/dist/run/run-option-shapes.js +47 -0
- package/dist/run/run-session.d.ts +144 -0
- package/dist/run/run-session.js +278 -0
- package/dist/worker/answer-queued-call.d.ts +19 -0
- package/dist/worker/answer-queued-call.js +100 -0
- package/dist/worker/launchers/claude-code.d.ts +7 -0
- package/dist/worker/launchers/claude-code.js +156 -0
- package/dist/worker/launchers/codex.d.ts +6 -0
- package/dist/worker/launchers/codex.js +141 -0
- package/dist/worker/launchers/launch.d.ts +31 -0
- package/dist/worker/launchers/launch.js +6 -0
- package/dist/worker/launchers/program-run.d.ts +30 -0
- package/dist/worker/launchers/program-run.js +92 -0
- package/dist/worker/launchers/tool-server.d.ts +26 -0
- package/dist/worker/launchers/tool-server.js +90 -0
- package/dist/worker/worker.d.ts +13 -0
- package/dist/worker/worker.js +170 -0
- package/package.json +43 -4
- package/README.md +0 -3
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { placeName, placeNamed } from '../blueprint/places.js';
|
|
2
|
+
import { runAgentPhase } from './assignments/agent-phase.js';
|
|
3
|
+
import { blueprintCode } from './blueprint-mistake.js';
|
|
4
|
+
import { runCodePhase } from './code-phase.js';
|
|
5
|
+
import { RunFailed, RunStopped, RunWaitsForPerson } from './run-endings.js';
|
|
6
|
+
/** The events line that ends a run which did not finish, from what ended it. */
|
|
7
|
+
function endingOf(error) {
|
|
8
|
+
if (error instanceof RunWaitsForPerson)
|
|
9
|
+
return { kind: 'run-waiting' };
|
|
10
|
+
if (error instanceof RunStopped)
|
|
11
|
+
return {
|
|
12
|
+
kind: 'run-stopped',
|
|
13
|
+
reason: error.message,
|
|
14
|
+
...(error.afterPhase ? { afterPhase: error.afterPhase } : {}),
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
kind: 'run-failed',
|
|
18
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
19
|
+
...(error instanceof RunFailed && error.assignmentId
|
|
20
|
+
? { assignmentId: error.assignmentId }
|
|
21
|
+
: {}),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Executes a run from where it stands until it finishes, waits for the person, is stopped or
|
|
25
|
+
* fails. Returns the status it ended with. */
|
|
26
|
+
export async function executeRun({ session, }) {
|
|
27
|
+
await session.saveSummary();
|
|
28
|
+
let ending = { kind: 'run-finished' };
|
|
29
|
+
try {
|
|
30
|
+
await runTrunk({ session });
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
ending = endingOf(error);
|
|
34
|
+
}
|
|
35
|
+
// When the events store itself cannot be written, the summary still says how the run ended.
|
|
36
|
+
await session.record(ending).catch(() => undefined);
|
|
37
|
+
await session.saveSummary();
|
|
38
|
+
return session.progress.status;
|
|
39
|
+
}
|
|
40
|
+
/** Stops the run after the place it was asked to stop after, the first time that place finishes
|
|
41
|
+
* in this run. */
|
|
42
|
+
function stopIfAskedToAfter({ session, position, }) {
|
|
43
|
+
const { stopAfter, stoppedAfterPhase } = session.progress;
|
|
44
|
+
// A place the run started after, from a checkpoint, did not finish in this run.
|
|
45
|
+
if (stopAfter === position.place && !stoppedAfterPhase && !position.fromCheckpoint)
|
|
46
|
+
throw new RunStopped({
|
|
47
|
+
message: `Stopped after ${position.place}, as asked.`,
|
|
48
|
+
afterPhase: position.place,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/** Runs the trunk's phases in order from where the run stands. */
|
|
52
|
+
async function runTrunk({ session }) {
|
|
53
|
+
const { trunk } = session.blueprint;
|
|
54
|
+
const indexOf = (name) => trunk.findIndex((phase) => phase.name === name);
|
|
55
|
+
for (;;) {
|
|
56
|
+
session.stopSignal.throwIfAborted();
|
|
57
|
+
session.drainSignal.throwIfAborted();
|
|
58
|
+
const { phasePosition } = session.progress;
|
|
59
|
+
if (phasePosition?.stage === 'running') {
|
|
60
|
+
await runTrunkPhase({ session, phase: trunk[indexOf(phasePosition.phase)] });
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (phasePosition)
|
|
64
|
+
stopIfAskedToAfter({ session, position: { ...phasePosition, place: phasePosition.phase } });
|
|
65
|
+
const phase = trunk[phasePosition ? indexOf(phasePosition.phase) + 1 : 0];
|
|
66
|
+
if (!phase)
|
|
67
|
+
return;
|
|
68
|
+
await session.record({ kind: 'phase-started', phase: phase.name });
|
|
69
|
+
await runTrunkPhase({ session, phase });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Runs a trunk phase to its end. Each time it has nothing left that can run, the side lines
|
|
73
|
+
* beside it are checked; after a visit to one the phase goes on with whatever that made possible. */
|
|
74
|
+
async function runTrunkPhase({ session, phase, }) {
|
|
75
|
+
// A visit that was under way when the run stopped is finished before the phase goes on.
|
|
76
|
+
if (session.progress.openVisit)
|
|
77
|
+
await finishOpenVisit({ session });
|
|
78
|
+
const visitSideLine = () => visitSideLineThatIsNeeded({ session, during: phase.name });
|
|
79
|
+
if (phase.kind === 'agents')
|
|
80
|
+
await runAgentPhase({ session, phase, place: phase.name, whenNothingCanRun: visitSideLine });
|
|
81
|
+
else {
|
|
82
|
+
await runCodePhase({ session, phase, place: phase.name });
|
|
83
|
+
while (await visitSideLine())
|
|
84
|
+
;
|
|
85
|
+
}
|
|
86
|
+
await finishPlace({ session, place: phase.name });
|
|
87
|
+
}
|
|
88
|
+
/** Saves the checkpoint of a place whose phase has ended and records that it finished. */
|
|
89
|
+
async function finishPlace({ session, place, }) {
|
|
90
|
+
const checkpoint = await session.store.writeCheckpoint({
|
|
91
|
+
runId: session.runId,
|
|
92
|
+
checkpoint: session.checkpointNow({ phase: place }),
|
|
93
|
+
});
|
|
94
|
+
await session.record({ kind: 'phase-finished', phase: place, checkpoint });
|
|
95
|
+
await session.saveSummary();
|
|
96
|
+
}
|
|
97
|
+
/** Checks the side lines beside a trunk phase that has nothing left it can run, and visits the
|
|
98
|
+
* first whose condition holds. Returns whether the run went onto one. */
|
|
99
|
+
async function visitSideLineThatIsNeeded({ session, during, }) {
|
|
100
|
+
if (session.stopSignal.aborted || session.drainSignal.aborted)
|
|
101
|
+
return false;
|
|
102
|
+
for (const sideLine of session.blueprint.sideLines) {
|
|
103
|
+
const visits = session.progress.sideLineVisits[sideLine.name] ?? 0;
|
|
104
|
+
if (!sideLine.beside.includes(during) || visits >= sideLine.atMost)
|
|
105
|
+
continue;
|
|
106
|
+
const entered = blueprintCode({
|
|
107
|
+
what: `${sideLine.name}'s entersWhen`,
|
|
108
|
+
run: () => sideLine.entersWhen.test({
|
|
109
|
+
state: session.state,
|
|
110
|
+
questionsAnsweredBy: session.questionsAnsweredBy,
|
|
111
|
+
}),
|
|
112
|
+
});
|
|
113
|
+
await session.record({
|
|
114
|
+
kind: 'side-line-checked',
|
|
115
|
+
sideLine: sideLine.name,
|
|
116
|
+
during,
|
|
117
|
+
entered,
|
|
118
|
+
...(entered ? { visit: visits + 1 } : {}),
|
|
119
|
+
});
|
|
120
|
+
if (!entered)
|
|
121
|
+
continue;
|
|
122
|
+
await finishOpenVisit({ session });
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
/** Runs the phases of the side line the run is visiting, in order from where the visit stands,
|
|
128
|
+
* and records that the run came back. */
|
|
129
|
+
async function finishOpenVisit({ session }) {
|
|
130
|
+
const { sideLine: name, visit } = session.progress.openVisit;
|
|
131
|
+
const { phases } = session.blueprint.sideLines.find((each) => each.name === name);
|
|
132
|
+
const indexOf = (place) => phases.findIndex((phase) => phase.name === placeNamed({ name: place }).phase);
|
|
133
|
+
const run = async ({ phase, place }) => {
|
|
134
|
+
if (phase.kind === 'agents')
|
|
135
|
+
await runAgentPhase({ session, phase, place });
|
|
136
|
+
else
|
|
137
|
+
await runCodePhase({ session, phase, place });
|
|
138
|
+
await finishPlace({ session, place });
|
|
139
|
+
};
|
|
140
|
+
for (;;) {
|
|
141
|
+
session.stopSignal.throwIfAborted();
|
|
142
|
+
session.drainSignal.throwIfAborted();
|
|
143
|
+
const { position } = session.progress.openVisit;
|
|
144
|
+
if (position?.stage === 'running') {
|
|
145
|
+
await run({ phase: phases[indexOf(position.place)], place: position.place });
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (position)
|
|
149
|
+
stopIfAskedToAfter({ session, position });
|
|
150
|
+
const phase = phases[position ? indexOf(position.place) + 1 : 0];
|
|
151
|
+
if (!phase)
|
|
152
|
+
break;
|
|
153
|
+
const place = placeName({ phase: phase.name, sideLine: { name, visit } });
|
|
154
|
+
await session.record({ kind: 'phase-started', phase: place });
|
|
155
|
+
await run({ phase, place });
|
|
156
|
+
}
|
|
157
|
+
await session.record({ kind: 'side-line-left', sideLine: name, visit });
|
|
158
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RunEvent } from './events.js';
|
|
2
|
+
import { type Checkpoint, type RunProgress } from './progress.js';
|
|
3
|
+
/** Applies one events line to a run's progress, in place. */
|
|
4
|
+
export declare function applyEvent({ progress, event }: {
|
|
5
|
+
progress: RunProgress;
|
|
6
|
+
event: RunEvent;
|
|
7
|
+
}): void;
|
|
8
|
+
/** A run's progress after the given events. With `upToSeq`, as it stood after that line. */
|
|
9
|
+
export declare function progressFromEvents({ start, events, upToSeq, }: {
|
|
10
|
+
start: Checkpoint;
|
|
11
|
+
events: RunEvent[];
|
|
12
|
+
upToSeq?: number;
|
|
13
|
+
}): RunProgress;
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { allAnswered, assignmentIdentity, progressAtStart, } from './progress.js';
|
|
2
|
+
import { withChangeEntries } from './state-changes.js';
|
|
3
|
+
import { placeNamed } from '../../blueprint/places.js';
|
|
4
|
+
/** Applies one events line to a run's progress, in place. */
|
|
5
|
+
export function applyEvent({ progress, event }) {
|
|
6
|
+
// Time between two lines of one process counts as time the run was being executed.
|
|
7
|
+
if (progress.lastLineAt && event.kind !== 'run-started' && event.kind !== 'run-resumed')
|
|
8
|
+
progress.totals.durationMs += Date.parse(event.at) - Date.parse(progress.lastLineAt);
|
|
9
|
+
progress.seq = event.seq;
|
|
10
|
+
progress.lastLineAt = event.at;
|
|
11
|
+
const assignmentOfCall = (callId) => progress.assignments[progress.calls[callId].assignmentId];
|
|
12
|
+
const applyWrites = ({ writes, assignment, }) => {
|
|
13
|
+
if (writes.change?.length) {
|
|
14
|
+
if (assignment)
|
|
15
|
+
assignment.firstChangeSeq ??= event.seq - 1;
|
|
16
|
+
progress.state = withChangeEntries({ state: progress.state, entries: writes.change });
|
|
17
|
+
}
|
|
18
|
+
for (const artifact of writes.artifacts ?? [])
|
|
19
|
+
progress.artifacts[artifact.name] = artifact;
|
|
20
|
+
};
|
|
21
|
+
const addQuestions = ({ questions, owner, }) => {
|
|
22
|
+
for (const saved of questions) {
|
|
23
|
+
progress.questions[saved.id] = { ...saved, owner };
|
|
24
|
+
progress.questionIds.push(saved.id);
|
|
25
|
+
}
|
|
26
|
+
return questions.map((saved) => saved.id);
|
|
27
|
+
};
|
|
28
|
+
/** The agent at the assignment's place in the chain is done; the next one is up. */
|
|
29
|
+
const finishPlace = ({ assignment, callId, callFailed, }) => {
|
|
30
|
+
assignment.finishedPlaces.push({
|
|
31
|
+
agent: progress.calls[callId].agent,
|
|
32
|
+
callId,
|
|
33
|
+
callFailed,
|
|
34
|
+
stateBeforeSeq: assignment.firstChangeSeq ?? event.seq - 1,
|
|
35
|
+
stateAfterSeq: event.seq,
|
|
36
|
+
});
|
|
37
|
+
assignment.placeInChain += 1;
|
|
38
|
+
assignment.correctionsInARow = 0;
|
|
39
|
+
delete assignment.lastCorrection;
|
|
40
|
+
delete assignment.pendingAsk;
|
|
41
|
+
delete assignment.firstChangeSeq;
|
|
42
|
+
};
|
|
43
|
+
/** The run stands at a place: on the side line it is visiting when the place is there, or
|
|
44
|
+
* else on the trunk. */
|
|
45
|
+
const setPosition = (position) => {
|
|
46
|
+
if (progress.openVisit && placeNamed({ name: position.place }).sideLine)
|
|
47
|
+
progress.openVisit.position = position;
|
|
48
|
+
else
|
|
49
|
+
progress.phasePosition = { phase: position.place, stage: position.stage };
|
|
50
|
+
};
|
|
51
|
+
switch (event.kind) {
|
|
52
|
+
case 'run-started':
|
|
53
|
+
progress.status = 'running';
|
|
54
|
+
progress.createdAt = event.at;
|
|
55
|
+
progress.blueprintHash = event.blueprintHash;
|
|
56
|
+
progress.callsAnsweredBy = event.callsAnsweredBy;
|
|
57
|
+
progress.questionsAnsweredBy = event.questionsAnsweredBy;
|
|
58
|
+
progress.overrides = event.overrides;
|
|
59
|
+
progress.stopAfter = event.stopAfter;
|
|
60
|
+
progress.startedFrom = event.startedFrom;
|
|
61
|
+
break;
|
|
62
|
+
case 'run-resumed':
|
|
63
|
+
progress.status = 'running';
|
|
64
|
+
progress.blueprintHash = event.blueprintHash;
|
|
65
|
+
delete progress.reason;
|
|
66
|
+
applyWrites({ writes: event });
|
|
67
|
+
if (event.overrides)
|
|
68
|
+
progress.overrides = event.overrides;
|
|
69
|
+
// A call that started and never ended was cut off; its agent is called again.
|
|
70
|
+
for (const call of Object.values(progress.calls)) {
|
|
71
|
+
if (call.status !== 'in-progress')
|
|
72
|
+
continue;
|
|
73
|
+
const assignment = progress.assignments[call.assignmentId];
|
|
74
|
+
const { pendingAsk } = assignment;
|
|
75
|
+
const toolAsked = pendingAsk?.by === 'tool' &&
|
|
76
|
+
!allAnswered({ progress, questionIds: pendingAsk.questionIds });
|
|
77
|
+
// A call cut off after one of its tools asked had ended there: its assignment waits.
|
|
78
|
+
call.status = toolAsked ? 'asked' : 'stopped';
|
|
79
|
+
if (toolAsked)
|
|
80
|
+
assignment.status = 'waiting-for-person';
|
|
81
|
+
}
|
|
82
|
+
for (const assignment of Object.values(progress.assignments))
|
|
83
|
+
assignment.correctionsInARow = 0;
|
|
84
|
+
break;
|
|
85
|
+
case 'side-line-checked':
|
|
86
|
+
if (event.entered && event.visit !== undefined) {
|
|
87
|
+
const { sideLine, visit, during } = event;
|
|
88
|
+
progress.sideLineVisits[sideLine] = visit;
|
|
89
|
+
progress.openVisit = { sideLine, visit, during };
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
case 'side-line-left':
|
|
93
|
+
delete progress.openVisit;
|
|
94
|
+
break;
|
|
95
|
+
case 'phase-started':
|
|
96
|
+
setPosition({ place: event.phase, stage: 'running' });
|
|
97
|
+
progress.codePhases[event.phase] = { questionIds: [], accepted: false };
|
|
98
|
+
break;
|
|
99
|
+
case 'assignment-added':
|
|
100
|
+
progress.assignments[event.assignmentId] = {
|
|
101
|
+
id: event.assignmentId,
|
|
102
|
+
phase: event.phase,
|
|
103
|
+
lane: event.lane,
|
|
104
|
+
key: event.key,
|
|
105
|
+
label: event.label,
|
|
106
|
+
repeat: event.repeat,
|
|
107
|
+
status: 'waiting-to-start',
|
|
108
|
+
mayStart: false,
|
|
109
|
+
placeInChain: 0,
|
|
110
|
+
finishedPlaces: [],
|
|
111
|
+
attempts: Object.create(null),
|
|
112
|
+
correctionsInARow: 0,
|
|
113
|
+
questionIds: [],
|
|
114
|
+
};
|
|
115
|
+
progress.assignmentIds.push(event.assignmentId);
|
|
116
|
+
break;
|
|
117
|
+
case 'assignment-may-start':
|
|
118
|
+
progress.assignments[event.assignmentId].mayStart = true;
|
|
119
|
+
break;
|
|
120
|
+
case 'assignment-dropped':
|
|
121
|
+
progress.assignments[event.assignmentId].status = 'dropped';
|
|
122
|
+
break;
|
|
123
|
+
case 'assignment-started': {
|
|
124
|
+
const assignment = progress.assignments[event.assignmentId];
|
|
125
|
+
assignment.status = 'running';
|
|
126
|
+
assignment.subject = event.subject;
|
|
127
|
+
assignment.agents = event.agents;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case 'call-started': {
|
|
131
|
+
const assignment = progress.assignments[event.assignmentId];
|
|
132
|
+
assignment.attempts[event.agent] = event.attempt;
|
|
133
|
+
assignment.status = 'running';
|
|
134
|
+
// A new call means the questions it waited on are answered.
|
|
135
|
+
delete assignment.pendingAsk;
|
|
136
|
+
progress.calls[event.callId] = {
|
|
137
|
+
assignmentId: event.assignmentId,
|
|
138
|
+
agent: event.agent,
|
|
139
|
+
stateSeq: event.stateSeq,
|
|
140
|
+
status: 'in-progress',
|
|
141
|
+
};
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case 'tool-call-finished': {
|
|
145
|
+
const assignment = assignmentOfCall(event.callId);
|
|
146
|
+
applyWrites({ writes: event, assignment });
|
|
147
|
+
if (event.questions?.length) {
|
|
148
|
+
const questionIds = addQuestions({
|
|
149
|
+
questions: event.questions,
|
|
150
|
+
owner: { assignmentId: assignment.id },
|
|
151
|
+
});
|
|
152
|
+
assignment.questionIds.push(...questionIds);
|
|
153
|
+
assignment.pendingAsk = { by: 'tool', callId: event.callId, questionIds };
|
|
154
|
+
}
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
case 'call-ended': {
|
|
158
|
+
const { totals } = progress;
|
|
159
|
+
progress.calls[event.callId].status = event.status;
|
|
160
|
+
totals.calls += 1;
|
|
161
|
+
totals.costUsd += event.costUsd ?? 0;
|
|
162
|
+
totals.programCostUsd += event.programCostUsd ?? 0;
|
|
163
|
+
for (const name of ['input', 'cached', 'output', 'reasoning'])
|
|
164
|
+
if (event.tokens[name] === undefined)
|
|
165
|
+
totals.callsNotReporting[name] += 1;
|
|
166
|
+
else
|
|
167
|
+
totals.tokens[name] += event.tokens[name];
|
|
168
|
+
if (event.status === 'asked')
|
|
169
|
+
assignmentOfCall(event.callId).status = 'waiting-for-person';
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case 'reply-judged': {
|
|
173
|
+
const assignment = progress.assignments[event.assignmentId];
|
|
174
|
+
progress.calls[event.callId].outcome = event.outcome;
|
|
175
|
+
applyWrites({ writes: event, assignment });
|
|
176
|
+
if (event.outcome === 'sent-back') {
|
|
177
|
+
assignment.correctionsInARow += 1;
|
|
178
|
+
assignment.lastCorrection = { callId: event.callId, text: event.correction ?? '' };
|
|
179
|
+
progress.totals.corrections += 1;
|
|
180
|
+
delete assignment.pendingAsk;
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
if (event.outcome === 'asked') {
|
|
184
|
+
const questionIds = addQuestions({
|
|
185
|
+
questions: event.questions ?? [],
|
|
186
|
+
owner: { assignmentId: assignment.id },
|
|
187
|
+
});
|
|
188
|
+
assignment.questionIds.push(...questionIds);
|
|
189
|
+
assignment.pendingAsk = { by: 'outcome', callId: event.callId, questionIds };
|
|
190
|
+
assignment.status = 'waiting-for-person';
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
finishPlace({ assignment, callId: event.callId, callFailed: false });
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
case 'failed-call-accepted': {
|
|
197
|
+
const assignment = progress.assignments[event.assignmentId];
|
|
198
|
+
applyWrites({ writes: event, assignment });
|
|
199
|
+
finishPlace({ assignment, callId: event.callId, callFailed: true });
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
case 'question-answered': {
|
|
203
|
+
const question = progress.questions[event.questionId];
|
|
204
|
+
question.answer = event.answer;
|
|
205
|
+
question.answeredBy = event.answeredBy;
|
|
206
|
+
// An answered question gives the agent its corrections back.
|
|
207
|
+
if ('assignmentId' in question.owner)
|
|
208
|
+
progress.assignments[question.owner.assignmentId].correctionsInARow = 0;
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case 'assignment-finished': {
|
|
212
|
+
const assignment = progress.assignments[event.assignmentId];
|
|
213
|
+
assignment.status = 'finished';
|
|
214
|
+
progress.finishedAssignments[assignment.phase] ??= Object.create(null);
|
|
215
|
+
progress.finishedAssignments[assignment.phase][assignmentIdentity(assignment)] = true;
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
case 'code-phase-ended':
|
|
219
|
+
applyWrites({ writes: event });
|
|
220
|
+
// A run started from a checkpoint may go on with a code phase it did not start itself.
|
|
221
|
+
progress.codePhases[event.phase] ??= { questionIds: [], accepted: false };
|
|
222
|
+
progress.codePhases[event.phase].accepted = event.outcome === 'accepted';
|
|
223
|
+
progress.codePhases[event.phase].questionIds.push(...addQuestions({ questions: event.questions ?? [], owner: { phase: event.phase } }));
|
|
224
|
+
break;
|
|
225
|
+
case 'phase-finished':
|
|
226
|
+
setPosition({ place: event.phase, stage: 'finished' });
|
|
227
|
+
progress.checkpointsSaved += 1;
|
|
228
|
+
break;
|
|
229
|
+
case 'run-waiting':
|
|
230
|
+
progress.status = 'waiting';
|
|
231
|
+
break;
|
|
232
|
+
case 'run-stopped':
|
|
233
|
+
progress.status = 'stopped';
|
|
234
|
+
progress.reason = event.reason;
|
|
235
|
+
if (event.afterPhase)
|
|
236
|
+
progress.stoppedAfterPhase = true;
|
|
237
|
+
break;
|
|
238
|
+
case 'run-failed':
|
|
239
|
+
progress.status = 'failed';
|
|
240
|
+
progress.reason = event.reason;
|
|
241
|
+
break;
|
|
242
|
+
case 'run-finished':
|
|
243
|
+
progress.status = 'finished';
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/** A run's progress after the given events. With `upToSeq`, as it stood after that line. */
|
|
248
|
+
export function progressFromEvents({ start, events, upToSeq, }) {
|
|
249
|
+
const progress = progressAtStart({ start });
|
|
250
|
+
for (const event of events) {
|
|
251
|
+
if (upToSeq !== undefined && event.seq > upToSeq)
|
|
252
|
+
break;
|
|
253
|
+
applyEvent({ progress, event });
|
|
254
|
+
}
|
|
255
|
+
return progress;
|
|
256
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type { CallsAnsweredBy, Effort, ModelSettings, QuestionsAnsweredBy } from '../../blueprint/run-settings.js';
|
|
2
|
+
import type { Question } from '../../blueprint/outcomes.js';
|
|
3
|
+
/** One place in the run state a change touched, with the value before and the new value. */
|
|
4
|
+
export type ChangeEntry = {
|
|
5
|
+
path: (string | number)[];
|
|
6
|
+
kind: 'added' | 'changed' | 'removed';
|
|
7
|
+
before?: unknown;
|
|
8
|
+
after?: unknown;
|
|
9
|
+
};
|
|
10
|
+
/** An artifact as an events line names it. */
|
|
11
|
+
export type SavedArtifact = {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
mediaType: string;
|
|
15
|
+
sizeInBytes: number;
|
|
16
|
+
};
|
|
17
|
+
export type SavedQuestion = {
|
|
18
|
+
id: string;
|
|
19
|
+
question: Question;
|
|
20
|
+
};
|
|
21
|
+
/** Token counts of a call, summed over every provider request it made. A count the provider did
|
|
22
|
+
* not report is left out, never saved as zero. `cached` is part of `input`, `reasoning` part of
|
|
23
|
+
* `output`. */
|
|
24
|
+
export type Tokens = {
|
|
25
|
+
input?: number;
|
|
26
|
+
cached?: number;
|
|
27
|
+
output?: number;
|
|
28
|
+
reasoning?: number;
|
|
29
|
+
};
|
|
30
|
+
/** A run's own model settings for every call, one phase or one agent. */
|
|
31
|
+
export type ModelOverride = {
|
|
32
|
+
phase?: string;
|
|
33
|
+
agent?: string;
|
|
34
|
+
modelSettings: ModelSettings;
|
|
35
|
+
};
|
|
36
|
+
/** What a change-making line carries: the change, the artifacts written and the questions asked. */
|
|
37
|
+
type Writes = {
|
|
38
|
+
change?: ChangeEntry[];
|
|
39
|
+
artifacts?: SavedArtifact[];
|
|
40
|
+
questions?: SavedQuestion[];
|
|
41
|
+
};
|
|
42
|
+
/** One line of a run's events file. The events are the one account of a run; everything else
|
|
43
|
+
* can be rebuilt from them and the starting checkpoint. */
|
|
44
|
+
export type RunEvent = {
|
|
45
|
+
seq: number;
|
|
46
|
+
at: string;
|
|
47
|
+
} & RunHappening;
|
|
48
|
+
export type RunHappening = {
|
|
49
|
+
kind: 'run-started';
|
|
50
|
+
blueprint: string;
|
|
51
|
+
blueprintHash: string;
|
|
52
|
+
structureFile: string;
|
|
53
|
+
callsAnsweredBy: CallsAnsweredBy;
|
|
54
|
+
/** The replies file of a scripted run, so Resume finds it again. */
|
|
55
|
+
scriptedRepliesFile?: string;
|
|
56
|
+
questionsAnsweredBy: QuestionsAnsweredBy;
|
|
57
|
+
overrides: ModelOverride[];
|
|
58
|
+
startedFrom?: {
|
|
59
|
+
runId: string;
|
|
60
|
+
checkpoint: string;
|
|
61
|
+
};
|
|
62
|
+
stopAfter?: string;
|
|
63
|
+
} | {
|
|
64
|
+
kind: 'run-resumed';
|
|
65
|
+
blueprintHash: string;
|
|
66
|
+
structureFile: string;
|
|
67
|
+
/** New overrides for every call made after this resume. */
|
|
68
|
+
overrides?: ModelOverride[];
|
|
69
|
+
/** What the current schema's defaults added to the saved state. */
|
|
70
|
+
change?: ChangeEntry[];
|
|
71
|
+
}
|
|
72
|
+
/** A trunk phase had nothing left that could run, and the condition of a side line beside it
|
|
73
|
+
* was checked. `visit` is set when the run went onto the line. */
|
|
74
|
+
| {
|
|
75
|
+
kind: 'side-line-checked';
|
|
76
|
+
sideLine: string;
|
|
77
|
+
during: string;
|
|
78
|
+
entered: boolean;
|
|
79
|
+
visit?: number;
|
|
80
|
+
}
|
|
81
|
+
/** The run came back from a side line into the trunk phase it left. */
|
|
82
|
+
| {
|
|
83
|
+
kind: 'side-line-left';
|
|
84
|
+
sideLine: string;
|
|
85
|
+
visit: number;
|
|
86
|
+
} | {
|
|
87
|
+
kind: 'phase-started';
|
|
88
|
+
phase: string;
|
|
89
|
+
} | {
|
|
90
|
+
kind: 'assignment-added';
|
|
91
|
+
assignmentId: string;
|
|
92
|
+
phase: string;
|
|
93
|
+
lane: string;
|
|
94
|
+
key: string;
|
|
95
|
+
label: string;
|
|
96
|
+
repeat: number;
|
|
97
|
+
} | {
|
|
98
|
+
kind: 'assignment-may-start';
|
|
99
|
+
assignmentId: string;
|
|
100
|
+
} | {
|
|
101
|
+
kind: 'assignment-dropped';
|
|
102
|
+
assignmentId: string;
|
|
103
|
+
} | {
|
|
104
|
+
kind: 'assignment-started';
|
|
105
|
+
assignmentId: string;
|
|
106
|
+
subject: unknown;
|
|
107
|
+
agents: string[];
|
|
108
|
+
} | {
|
|
109
|
+
kind: 'call-started';
|
|
110
|
+
callId: string;
|
|
111
|
+
assignmentId: string;
|
|
112
|
+
phase: string;
|
|
113
|
+
agent: string;
|
|
114
|
+
/** The agent's place in the chain, from 0. */
|
|
115
|
+
placeInChain: number;
|
|
116
|
+
attempt: number;
|
|
117
|
+
answeredBy: CallsAnsweredBy;
|
|
118
|
+
model?: string;
|
|
119
|
+
effort?: Effort;
|
|
120
|
+
promptSizeInBytes: number;
|
|
121
|
+
/** The last events line the prompt's state includes. */
|
|
122
|
+
stateSeq: number;
|
|
123
|
+
} | ({
|
|
124
|
+
kind: 'tool-call-finished';
|
|
125
|
+
callId: string;
|
|
126
|
+
tool: string;
|
|
127
|
+
durationMs: number;
|
|
128
|
+
status: 'returned' | 'failed' | 'asked';
|
|
129
|
+
} & Writes) | {
|
|
130
|
+
kind: 'call-ended';
|
|
131
|
+
callId: string;
|
|
132
|
+
/** `replied`: the reply arrived. `asked`: a tool asked the person and the call ended. */
|
|
133
|
+
status: 'replied' | 'asked' | 'failed' | 'stopped';
|
|
134
|
+
durationMs: number;
|
|
135
|
+
tokens: Tokens;
|
|
136
|
+
/** Priced from the tokens when the call ended. */
|
|
137
|
+
costUsd?: number;
|
|
138
|
+
/** The cost the model program itself reported for the call, as an estimate. */
|
|
139
|
+
programCostUsd?: number;
|
|
140
|
+
failure?: string;
|
|
141
|
+
/** A connection failure that the next attempt retries. */
|
|
142
|
+
retried?: boolean;
|
|
143
|
+
} | ({
|
|
144
|
+
kind: 'reply-judged';
|
|
145
|
+
callId: string;
|
|
146
|
+
assignmentId: string;
|
|
147
|
+
outcome: 'accepted' | 'sent-back' | 'asked';
|
|
148
|
+
correction?: string;
|
|
149
|
+
/** Names of the declared checks (or `may-change`) that sent this reply back. */
|
|
150
|
+
failedChecks?: string[];
|
|
151
|
+
} & Writes)
|
|
152
|
+
/** A call failed for good and its agent's `whenCallFails` saved this in place of a reply. */
|
|
153
|
+
| ({
|
|
154
|
+
kind: 'failed-call-accepted';
|
|
155
|
+
callId: string;
|
|
156
|
+
assignmentId: string;
|
|
157
|
+
} & Writes) | {
|
|
158
|
+
kind: 'question-answered';
|
|
159
|
+
questionId: string;
|
|
160
|
+
answer: string;
|
|
161
|
+
answeredBy: 'person' | 'run';
|
|
162
|
+
} | {
|
|
163
|
+
kind: 'assignment-finished';
|
|
164
|
+
assignmentId: string;
|
|
165
|
+
} | ({
|
|
166
|
+
kind: 'code-phase-ended';
|
|
167
|
+
phase: string;
|
|
168
|
+
outcome: 'accepted' | 'asked';
|
|
169
|
+
durationMs: number;
|
|
170
|
+
} & Writes) | {
|
|
171
|
+
kind: 'phase-finished';
|
|
172
|
+
phase: string;
|
|
173
|
+
checkpoint: string;
|
|
174
|
+
} | {
|
|
175
|
+
kind: 'run-waiting';
|
|
176
|
+
} | {
|
|
177
|
+
kind: 'run-stopped';
|
|
178
|
+
reason: string;
|
|
179
|
+
afterPhase?: string;
|
|
180
|
+
} | {
|
|
181
|
+
kind: 'run-failed';
|
|
182
|
+
reason: string;
|
|
183
|
+
assignmentId?: string;
|
|
184
|
+
} | {
|
|
185
|
+
kind: 'run-finished';
|
|
186
|
+
};
|
|
187
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Pool, PoolClient } from 'pg';
|
|
2
|
+
import type { RunStore } from './run-store.js';
|
|
3
|
+
type Queryable = Pick<Pool, 'query'> | Pick<PoolClient, 'query'>;
|
|
4
|
+
/** Creates Dagma's self-managed `dagma` schema and tables idempotently. Never touches `public`.
|
|
5
|
+
* Uses a Postgres advisory lock so concurrent process startups never race on `CREATE SCHEMA`. */
|
|
6
|
+
export declare function ensureDagmaSchema({ pool }: {
|
|
7
|
+
pool: Queryable;
|
|
8
|
+
}): Promise<void>;
|
|
9
|
+
/** Postgres-backed `RunStore` operating exclusively in the `dagma` schema on the caller's pool. */
|
|
10
|
+
export declare function postgresRunStore({ pool }: {
|
|
11
|
+
pool: Queryable;
|
|
12
|
+
}): RunStore;
|
|
13
|
+
export {};
|