@a5c-ai/babysitter-sdk 0.0.16
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/cli/main.d.ts +5 -0
- package/dist/cli/main.d.ts.map +1 -0
- package/dist/cli/main.js +1343 -0
- package/dist/cli/nodeTaskRunner.d.ts +16 -0
- package/dist/cli/nodeTaskRunner.d.ts.map +1 -0
- package/dist/cli/nodeTaskRunner.js +46 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/runner/env.d.ts +58 -0
- package/dist/runner/env.d.ts.map +1 -0
- package/dist/runner/env.js +113 -0
- package/dist/runner/index.d.ts +3 -0
- package/dist/runner/index.d.ts.map +1 -0
- package/dist/runner/index.js +18 -0
- package/dist/runner/nodeRunner.d.ts +60 -0
- package/dist/runner/nodeRunner.d.ts.map +1 -0
- package/dist/runner/nodeRunner.js +354 -0
- package/dist/runtime/commitEffectResult.d.ts +3 -0
- package/dist/runtime/commitEffectResult.d.ts.map +1 -0
- package/dist/runtime/commitEffectResult.js +172 -0
- package/dist/runtime/constants.d.ts +2 -0
- package/dist/runtime/constants.d.ts.map +1 -0
- package/dist/runtime/constants.js +5 -0
- package/dist/runtime/createRun.d.ts +3 -0
- package/dist/runtime/createRun.d.ts.map +1 -0
- package/dist/runtime/createRun.js +81 -0
- package/dist/runtime/errorUtils.d.ts +10 -0
- package/dist/runtime/errorUtils.d.ts.map +1 -0
- package/dist/runtime/errorUtils.js +42 -0
- package/dist/runtime/exceptions.d.ts +45 -0
- package/dist/runtime/exceptions.d.ts.map +1 -0
- package/dist/runtime/exceptions.js +99 -0
- package/dist/runtime/index.d.ts +12 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +34 -0
- package/dist/runtime/instrumentation.d.ts +6 -0
- package/dist/runtime/instrumentation.d.ts.map +1 -0
- package/dist/runtime/instrumentation.js +14 -0
- package/dist/runtime/intrinsics/breakpoint.d.ts +4 -0
- package/dist/runtime/intrinsics/breakpoint.d.ts.map +1 -0
- package/dist/runtime/intrinsics/breakpoint.js +42 -0
- package/dist/runtime/intrinsics/index.d.ts +7 -0
- package/dist/runtime/intrinsics/index.d.ts.map +1 -0
- package/dist/runtime/intrinsics/index.js +15 -0
- package/dist/runtime/intrinsics/orchestratorTask.d.ts +4 -0
- package/dist/runtime/intrinsics/orchestratorTask.d.ts.map +1 -0
- package/dist/runtime/intrinsics/orchestratorTask.js +28 -0
- package/dist/runtime/intrinsics/parallel.d.ts +5 -0
- package/dist/runtime/intrinsics/parallel.d.ts.map +1 -0
- package/dist/runtime/intrinsics/parallel.js +45 -0
- package/dist/runtime/intrinsics/sleep.d.ts +4 -0
- package/dist/runtime/intrinsics/sleep.d.ts.map +1 -0
- package/dist/runtime/intrinsics/sleep.js +70 -0
- package/dist/runtime/intrinsics/task.d.ts +20 -0
- package/dist/runtime/intrinsics/task.d.ts.map +1 -0
- package/dist/runtime/intrinsics/task.js +237 -0
- package/dist/runtime/invocation/hashInvocationKey.d.ts +12 -0
- package/dist/runtime/invocation/hashInvocationKey.d.ts.map +1 -0
- package/dist/runtime/invocation/hashInvocationKey.js +12 -0
- package/dist/runtime/invocation/index.d.ts +3 -0
- package/dist/runtime/invocation/index.d.ts.map +1 -0
- package/dist/runtime/invocation/index.js +5 -0
- package/dist/runtime/orchestrateIteration.d.ts +3 -0
- package/dist/runtime/orchestrateIteration.d.ts.map +1 -0
- package/dist/runtime/orchestrateIteration.js +195 -0
- package/dist/runtime/processContext.d.ts +19 -0
- package/dist/runtime/processContext.d.ts.map +1 -0
- package/dist/runtime/processContext.js +55 -0
- package/dist/runtime/replay/createReplayEngine.d.ts +31 -0
- package/dist/runtime/replay/createReplayEngine.d.ts.map +1 -0
- package/dist/runtime/replay/createReplayEngine.js +82 -0
- package/dist/runtime/replay/effectIndex.d.ts +34 -0
- package/dist/runtime/replay/effectIndex.d.ts.map +1 -0
- package/dist/runtime/replay/effectIndex.js +241 -0
- package/dist/runtime/replay/index.d.ts +7 -0
- package/dist/runtime/replay/index.d.ts.map +1 -0
- package/dist/runtime/replay/index.js +19 -0
- package/dist/runtime/replay/replayCursor.d.ts +7 -0
- package/dist/runtime/replay/replayCursor.d.ts.map +1 -0
- package/dist/runtime/replay/replayCursor.js +22 -0
- package/dist/runtime/replay/stateCache.d.ts +48 -0
- package/dist/runtime/replay/stateCache.d.ts.map +1 -0
- package/dist/runtime/replay/stateCache.js +211 -0
- package/dist/runtime/types.d.ts +147 -0
- package/dist/runtime/types.d.ts.map +1 -0
- package/dist/runtime/types.js +2 -0
- package/dist/storage/atomic.d.ts +2 -0
- package/dist/storage/atomic.d.ts.map +1 -0
- package/dist/storage/atomic.js +54 -0
- package/dist/storage/cleanup.d.ts +4 -0
- package/dist/storage/cleanup.d.ts.map +1 -0
- package/dist/storage/cleanup.js +96 -0
- package/dist/storage/clock.d.ts +6 -0
- package/dist/storage/clock.d.ts.map +1 -0
- package/dist/storage/clock.js +29 -0
- package/dist/storage/createRunDir.d.ts +6 -0
- package/dist/storage/createRunDir.d.ts.map +1 -0
- package/dist/storage/createRunDir.js +59 -0
- package/dist/storage/index.d.ts +9 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +28 -0
- package/dist/storage/journal.d.ts +4 -0
- package/dist/storage/journal.d.ts.map +1 -0
- package/dist/storage/journal.js +103 -0
- package/dist/storage/lock.d.ts +5 -0
- package/dist/storage/lock.d.ts.map +1 -0
- package/dist/storage/lock.js +41 -0
- package/dist/storage/paths.d.ts +19 -0
- package/dist/storage/paths.d.ts.map +1 -0
- package/dist/storage/paths.js +46 -0
- package/dist/storage/runFiles.d.ts +5 -0
- package/dist/storage/runFiles.d.ts.map +1 -0
- package/dist/storage/runFiles.js +39 -0
- package/dist/storage/snapshotState.d.ts +10 -0
- package/dist/storage/snapshotState.d.ts.map +1 -0
- package/dist/storage/snapshotState.js +15 -0
- package/dist/storage/storeTaskArtifacts.d.ts +6 -0
- package/dist/storage/storeTaskArtifacts.d.ts.map +1 -0
- package/dist/storage/storeTaskArtifacts.js +58 -0
- package/dist/storage/tasks.d.ts +17 -0
- package/dist/storage/tasks.d.ts.map +1 -0
- package/dist/storage/tasks.js +82 -0
- package/dist/storage/types.d.ts +112 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +2 -0
- package/dist/storage/ulids.d.ts +11 -0
- package/dist/storage/ulids.d.ts.map +1 -0
- package/dist/storage/ulids.js +25 -0
- package/dist/tasks/batching.d.ts +29 -0
- package/dist/tasks/batching.d.ts.map +1 -0
- package/dist/tasks/batching.js +66 -0
- package/dist/tasks/context.d.ts +11 -0
- package/dist/tasks/context.d.ts.map +1 -0
- package/dist/tasks/context.js +181 -0
- package/dist/tasks/defineTask.d.ts +9 -0
- package/dist/tasks/defineTask.d.ts.map +1 -0
- package/dist/tasks/defineTask.js +58 -0
- package/dist/tasks/index.d.ts +8 -0
- package/dist/tasks/index.d.ts.map +1 -0
- package/dist/tasks/index.js +23 -0
- package/dist/tasks/kinds/index.d.ts +7 -0
- package/dist/tasks/kinds/index.d.ts.map +1 -0
- package/dist/tasks/kinds/index.js +333 -0
- package/dist/tasks/registry.d.ts +53 -0
- package/dist/tasks/registry.d.ts.map +1 -0
- package/dist/tasks/registry.js +145 -0
- package/dist/tasks/serializer.d.ts +60 -0
- package/dist/tasks/serializer.d.ts.map +1 -0
- package/dist/tasks/serializer.js +193 -0
- package/dist/tasks/types.d.ts +148 -0
- package/dist/tasks/types.d.ts.map +1 -0
- package/dist/tasks/types.js +2 -0
- package/dist/test-fixtures/kinds/index.d.ts +56 -0
- package/dist/test-fixtures/kinds/index.d.ts.map +1 -0
- package/dist/test-fixtures/kinds/index.js +44 -0
- package/dist/testing/deterministic.d.ts +90 -0
- package/dist/testing/deterministic.d.ts.map +1 -0
- package/dist/testing/deterministic.js +449 -0
- package/dist/testing/index.d.ts +4 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +23 -0
- package/dist/testing/runHarness.d.ts +64 -0
- package/dist/testing/runHarness.d.ts.map +1 -0
- package/dist/testing/runHarness.js +161 -0
- package/dist/testing/snapshots.d.ts +17 -0
- package/dist/testing/snapshots.d.ts.map +1 -0
- package/dist/testing/snapshots.js +24 -0
- package/package.json +35 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runToCompletionWithFakeRunner = runToCompletionWithFakeRunner;
|
|
4
|
+
const orchestrateIteration_1 = require("../runtime/orchestrateIteration");
|
|
5
|
+
const commitEffectResult_1 = require("../runtime/commitEffectResult");
|
|
6
|
+
const DEFAULT_MAX_ITERATIONS = 100;
|
|
7
|
+
async function runToCompletionWithFakeRunner(options) {
|
|
8
|
+
const { runDir, resolve, logger, onIteration } = options;
|
|
9
|
+
const maxIterations = options.maxIterations ?? DEFAULT_MAX_ITERATIONS;
|
|
10
|
+
if (maxIterations <= 0 || !Number.isFinite(maxIterations)) {
|
|
11
|
+
throw new Error("maxIterations must be a positive finite number");
|
|
12
|
+
}
|
|
13
|
+
const cleanupProviders = applyDeterministicProviders(options.clock, options.ulids);
|
|
14
|
+
const executed = [];
|
|
15
|
+
const executionLog = [];
|
|
16
|
+
let iterations = 0;
|
|
17
|
+
try {
|
|
18
|
+
while (iterations < maxIterations) {
|
|
19
|
+
iterations += 1;
|
|
20
|
+
const clockHandle = options.clock;
|
|
21
|
+
const deterministicNow = clockHandle ? () => clockHandle.now() : undefined;
|
|
22
|
+
const iteration = await (0, orchestrateIteration_1.orchestrateIteration)({
|
|
23
|
+
runDir,
|
|
24
|
+
logger,
|
|
25
|
+
now: resolveNowOption(options.now ?? deterministicNow),
|
|
26
|
+
});
|
|
27
|
+
await onIteration?.(iteration);
|
|
28
|
+
const iterationLog = {
|
|
29
|
+
iteration: iterations,
|
|
30
|
+
status: iteration.status,
|
|
31
|
+
metadata: iteration.metadata ?? null,
|
|
32
|
+
pending: iteration.status === "waiting" ? iteration.nextActions.map(snapshotAction) : [],
|
|
33
|
+
executed: [],
|
|
34
|
+
};
|
|
35
|
+
if (iteration.status === "completed") {
|
|
36
|
+
executionLog.push(iterationLog);
|
|
37
|
+
return {
|
|
38
|
+
status: "completed",
|
|
39
|
+
output: iteration.output,
|
|
40
|
+
metadata: iteration.metadata ?? null,
|
|
41
|
+
executed,
|
|
42
|
+
iterations,
|
|
43
|
+
executionLog,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (iteration.status === "failed") {
|
|
47
|
+
executionLog.push(iterationLog);
|
|
48
|
+
return {
|
|
49
|
+
status: "failed",
|
|
50
|
+
error: iteration.error,
|
|
51
|
+
metadata: iteration.metadata ?? null,
|
|
52
|
+
executed,
|
|
53
|
+
iterations,
|
|
54
|
+
executionLog,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (iteration.status !== "waiting") {
|
|
58
|
+
executionLog.push(iterationLog);
|
|
59
|
+
throw new Error("Unexpected iteration status");
|
|
60
|
+
}
|
|
61
|
+
const pendingActions = iteration.nextActions;
|
|
62
|
+
const executedThisIteration = [];
|
|
63
|
+
let handled = false;
|
|
64
|
+
for (const action of pendingActions) {
|
|
65
|
+
const resolution = await resolve(action);
|
|
66
|
+
if (!resolution) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
handled = true;
|
|
70
|
+
await (0, commitEffectResult_1.commitEffectResult)({
|
|
71
|
+
runDir,
|
|
72
|
+
effectId: action.effectId,
|
|
73
|
+
invocationKey: action.invocationKey,
|
|
74
|
+
result: toCommitResult(resolution),
|
|
75
|
+
});
|
|
76
|
+
const entry = { action, resolution };
|
|
77
|
+
executedThisIteration.push(entry);
|
|
78
|
+
executed.push(entry);
|
|
79
|
+
}
|
|
80
|
+
iterationLog.executed = executedThisIteration.map(({ action }) => snapshotAction(action));
|
|
81
|
+
executionLog.push(iterationLog);
|
|
82
|
+
if (!handled) {
|
|
83
|
+
return {
|
|
84
|
+
status: "waiting",
|
|
85
|
+
pending: pendingActions,
|
|
86
|
+
metadata: iteration.metadata ?? null,
|
|
87
|
+
executed,
|
|
88
|
+
iterations,
|
|
89
|
+
executionLog,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
throw new Error(`runToCompletionWithFakeRunner exceeded maxIterations=${maxIterations}`);
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
cleanupProviders();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function toCommitResult(resolution) {
|
|
100
|
+
const base = {
|
|
101
|
+
stdout: resolution.stdout,
|
|
102
|
+
stderr: resolution.stderr,
|
|
103
|
+
startedAt: resolution.startedAt,
|
|
104
|
+
finishedAt: resolution.finishedAt,
|
|
105
|
+
metadata: resolution.metadata,
|
|
106
|
+
};
|
|
107
|
+
if (resolution.status === "ok") {
|
|
108
|
+
return {
|
|
109
|
+
status: "ok",
|
|
110
|
+
value: resolution.value,
|
|
111
|
+
...base,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (resolution.error === undefined) {
|
|
115
|
+
throw new Error("FakeActionResolution.status='error' requires an error payload");
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
status: "error",
|
|
119
|
+
error: resolution.error,
|
|
120
|
+
...base,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function snapshotAction(action) {
|
|
124
|
+
return {
|
|
125
|
+
effectId: action.effectId,
|
|
126
|
+
invocationKey: action.invocationKey,
|
|
127
|
+
taskId: action.taskId,
|
|
128
|
+
stepId: action.stepId,
|
|
129
|
+
kind: action.kind,
|
|
130
|
+
schedulerHints: action.schedulerHints,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function resolveNowOption(now) {
|
|
134
|
+
if (!now)
|
|
135
|
+
return () => new Date();
|
|
136
|
+
if (typeof now === "function") {
|
|
137
|
+
return now;
|
|
138
|
+
}
|
|
139
|
+
const fixed = now;
|
|
140
|
+
return () => fixed;
|
|
141
|
+
}
|
|
142
|
+
function applyDeterministicProviders(clock, ulids) {
|
|
143
|
+
const restoreFns = [];
|
|
144
|
+
if (clock) {
|
|
145
|
+
restoreFns.push(clock.apply());
|
|
146
|
+
}
|
|
147
|
+
if (ulids) {
|
|
148
|
+
restoreFns.push(ulids.apply());
|
|
149
|
+
}
|
|
150
|
+
return () => {
|
|
151
|
+
while (restoreFns.length > 0) {
|
|
152
|
+
const restore = restoreFns.pop();
|
|
153
|
+
try {
|
|
154
|
+
restore?.();
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// ignore cleanup errors
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { JsonRecord } from "../storage/types";
|
|
2
|
+
import { type StateCacheSnapshot } from "../runtime/replay/stateCache";
|
|
3
|
+
export interface JournalSnapshotEntry {
|
|
4
|
+
seq: number;
|
|
5
|
+
ulid: string;
|
|
6
|
+
type: string;
|
|
7
|
+
recordedAt: string;
|
|
8
|
+
data: JsonRecord;
|
|
9
|
+
}
|
|
10
|
+
export interface RunSnapshot {
|
|
11
|
+
journal: JournalSnapshotEntry[];
|
|
12
|
+
state: StateCacheSnapshot | null;
|
|
13
|
+
}
|
|
14
|
+
export declare function readJournalSnapshot(runDir: string): Promise<JournalSnapshotEntry[]>;
|
|
15
|
+
export declare function readStateSnapshot(runDir: string): Promise<StateCacheSnapshot | null>;
|
|
16
|
+
export declare function captureRunSnapshot(runDir: string): Promise<RunSnapshot>;
|
|
17
|
+
//# sourceMappingURL=snapshots.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshots.d.ts","sourceRoot":"","sources":["../../src/testing/snapshots.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAkB,KAAK,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAEvF,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,UAAU,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,oBAAoB,EAAE,CAAC;IAChC,KAAK,EAAE,kBAAkB,GAAG,IAAI,CAAC;CAClC;AAED,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,EAAE,CAAC,CASzF;AAED,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAE1F;AAED,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAG7E"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readJournalSnapshot = readJournalSnapshot;
|
|
4
|
+
exports.readStateSnapshot = readStateSnapshot;
|
|
5
|
+
exports.captureRunSnapshot = captureRunSnapshot;
|
|
6
|
+
const journal_1 = require("../storage/journal");
|
|
7
|
+
const stateCache_1 = require("../runtime/replay/stateCache");
|
|
8
|
+
async function readJournalSnapshot(runDir) {
|
|
9
|
+
const events = await (0, journal_1.loadJournal)(runDir);
|
|
10
|
+
return events.map((event) => ({
|
|
11
|
+
seq: event.seq,
|
|
12
|
+
ulid: event.ulid,
|
|
13
|
+
type: event.type,
|
|
14
|
+
recordedAt: event.recordedAt,
|
|
15
|
+
data: event.data,
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
async function readStateSnapshot(runDir) {
|
|
19
|
+
return (0, stateCache_1.readStateCache)(runDir);
|
|
20
|
+
}
|
|
21
|
+
async function captureRunSnapshot(runDir) {
|
|
22
|
+
const [journal, state] = await Promise.all([readJournalSnapshot(runDir), readStateSnapshot(runDir)]);
|
|
23
|
+
return { journal, state };
|
|
24
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@a5c-ai/babysitter-sdk",
|
|
3
|
+
"version": "0.0.16",
|
|
4
|
+
"description": "Storage and run-registry primitives for event-sourced babysitter workflows.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"babysitter": "dist/cli/main.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p tsconfig.json",
|
|
17
|
+
"clean": "rimraf dist",
|
|
18
|
+
"lint": "eslint \"src/**/*.ts\" --max-warnings=0",
|
|
19
|
+
"test": "vitest run",
|
|
20
|
+
"test:watch": "vitest",
|
|
21
|
+
"smoke:cli": "node scripts/smoke-cli.js --runs-dir packages/sdk/test-fixtures/cli/runs/smoke"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"fs-extra": "^11.2.0",
|
|
25
|
+
"ulid": "^2.3.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/fs-extra": "^11.0.4",
|
|
29
|
+
"@types/node": "^20.11.30",
|
|
30
|
+
"eslint": "^8.57.0",
|
|
31
|
+
"rimraf": "^5.0.5",
|
|
32
|
+
"typescript": "^5.4.2",
|
|
33
|
+
"vitest": "^1.2.2"
|
|
34
|
+
}
|
|
35
|
+
}
|