@qvac/harness 0.7.1 → 1.0.0-rc.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.
Files changed (201) hide show
  1. package/README.md +224 -0
  2. package/dist-lib/index.d.ts +55 -3
  3. package/dist-lib/index.js +721 -236
  4. package/dist-lib/lib/capabilities/completion-engine/abort-polyfill.d.ts +1 -0
  5. package/dist-lib/lib/capabilities/completion-engine/abort-polyfill.js +10 -0
  6. package/dist-lib/lib/capabilities/completion-engine/actors/answering.d.ts +3 -0
  7. package/dist-lib/lib/capabilities/completion-engine/actors/answering.js +17 -0
  8. package/dist-lib/lib/capabilities/completion-engine/actors/approval.d.ts +4 -0
  9. package/dist-lib/lib/capabilities/completion-engine/actors/approval.js +29 -0
  10. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.d.ts +4 -0
  11. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.js +73 -0
  12. package/dist-lib/lib/capabilities/completion-engine/actors/execute.d.ts +41 -0
  13. package/dist-lib/lib/capabilities/completion-engine/actors/execute.js +49 -0
  14. package/dist-lib/lib/capabilities/completion-engine/actors/executing.d.ts +3 -0
  15. package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +102 -0
  16. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.d.ts +47 -0
  17. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +87 -0
  18. package/dist-lib/lib/capabilities/completion-engine/actors/finishing.d.ts +2 -0
  19. package/dist-lib/lib/capabilities/completion-engine/actors/finishing.js +32 -0
  20. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-preload.d.ts +3 -0
  21. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-preload.js +75 -0
  22. package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.d.ts +3 -0
  23. package/dist-lib/lib/capabilities/completion-engine/actors/inspecting.js +152 -0
  24. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.d.ts +3 -0
  25. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.js +94 -0
  26. package/dist-lib/lib/capabilities/completion-engine/actors/preload-failure.d.ts +9 -0
  27. package/dist-lib/lib/capabilities/completion-engine/actors/preload-failure.js +11 -0
  28. package/dist-lib/lib/capabilities/completion-engine/actors/preloading.d.ts +3 -0
  29. package/dist-lib/lib/capabilities/completion-engine/actors/preloading.js +114 -0
  30. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.d.ts +3 -0
  31. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +198 -0
  32. package/dist-lib/lib/capabilities/completion-engine/actors/rejecting.d.ts +3 -0
  33. package/dist-lib/lib/capabilities/completion-engine/actors/rejecting.js +71 -0
  34. package/dist-lib/lib/capabilities/completion-engine/actors/rejection.d.ts +18 -0
  35. package/dist-lib/lib/capabilities/completion-engine/actors/rejection.js +70 -0
  36. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.d.ts +3 -0
  37. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.js +60 -0
  38. package/dist-lib/lib/capabilities/completion-engine/actors/stream.d.ts +21 -0
  39. package/dist-lib/lib/capabilities/completion-engine/actors/stream.js +48 -0
  40. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.d.ts +3 -0
  41. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +156 -0
  42. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.d.ts +7 -0
  43. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +61 -0
  44. package/dist-lib/lib/capabilities/completion-engine/driver.d.ts +25 -0
  45. package/dist-lib/lib/capabilities/completion-engine/driver.js +553 -0
  46. package/dist-lib/lib/capabilities/completion-engine/durable.d.ts +61 -0
  47. package/dist-lib/lib/capabilities/completion-engine/durable.js +150 -0
  48. package/dist-lib/lib/capabilities/completion-engine/operations.d.ts +6 -0
  49. package/dist-lib/lib/capabilities/completion-engine/operations.js +239 -0
  50. package/dist-lib/lib/capabilities/completion-engine/ports.d.ts +3 -0
  51. package/dist-lib/lib/capabilities/completion-engine/ports.js +267 -0
  52. package/dist-lib/lib/capabilities/completion-engine/session.d.ts +76 -0
  53. package/dist-lib/lib/capabilities/completion-engine/session.js +70 -0
  54. package/dist-lib/lib/capabilities/completion-loop/index.d.ts +80 -0
  55. package/dist-lib/lib/capabilities/completion-loop/index.js +158 -0
  56. package/dist-lib/lib/capabilities/completion-loop/metrics.d.ts +5 -0
  57. package/dist-lib/lib/capabilities/completion-loop/metrics.js +29 -0
  58. package/dist-lib/lib/capabilities/completion-loop/round.d.ts +33 -0
  59. package/dist-lib/lib/capabilities/completion-loop/round.js +353 -0
  60. package/dist-lib/lib/capabilities/completion-loop/tool-execution.d.ts +60 -0
  61. package/dist-lib/lib/capabilities/completion-loop/tool-execution.js +270 -0
  62. package/dist-lib/lib/capabilities/completion-loop/turns.d.ts +23 -0
  63. package/dist-lib/lib/capabilities/completion-loop/turns.js +112 -0
  64. package/dist-lib/lib/capabilities/completion-runtime.d.ts +46 -0
  65. package/dist-lib/lib/capabilities/completion-runtime.js +74 -0
  66. package/dist-lib/lib/capabilities/completion.d.ts +18 -335
  67. package/dist-lib/lib/capabilities/completion.js +20 -1301
  68. package/dist-lib/lib/capabilities/download.d.ts +1 -1
  69. package/dist-lib/lib/capabilities/download.js +7 -2
  70. package/dist-lib/lib/capabilities/invoke.js +5 -3
  71. package/dist-lib/lib/capabilities/tool-plan.d.ts +35 -0
  72. package/dist-lib/lib/capabilities/tool-plan.js +66 -0
  73. package/dist-lib/lib/capabilities/tool-turns.d.ts +11 -2
  74. package/dist-lib/lib/capabilities/tool-turns.js +30 -2
  75. package/dist-lib/lib/capabilities/types.d.ts +287 -0
  76. package/dist-lib/lib/capabilities/types.js +1 -0
  77. package/dist-lib/lib/completion-lifecycle.d.ts +14 -0
  78. package/dist-lib/lib/completion-lifecycle.js +126 -0
  79. package/dist-lib/lib/connect.d.ts +10 -3
  80. package/dist-lib/lib/connect.js +329 -23
  81. package/dist-lib/lib/contract-types.d.ts +19 -2
  82. package/dist-lib/lib/harness-lifecycle.d.ts +66 -0
  83. package/dist-lib/lib/harness-lifecycle.js +129 -0
  84. package/dist-lib/lib/helpers/detach-json.d.ts +1 -0
  85. package/dist-lib/lib/helpers/detach-json.js +146 -0
  86. package/dist-lib/lib/helpers/markup-scanner.d.ts +26 -0
  87. package/dist-lib/lib/helpers/markup-scanner.js +156 -0
  88. package/dist-lib/lib/helpers/markup-stream-filter.js +138 -35
  89. package/dist-lib/lib/helpers/race-abort.d.ts +9 -4
  90. package/dist-lib/lib/helpers/race-abort.js +2 -7
  91. package/dist-lib/lib/helpers/recovery-required.d.ts +1 -0
  92. package/dist-lib/lib/helpers/recovery-required.js +1 -0
  93. package/dist-lib/lib/helpers/run-outcome.d.ts +10 -2
  94. package/dist-lib/lib/helpers/run-outcome.js +8 -5
  95. package/dist-lib/lib/helpers/tool-call-markup.d.ts +2 -0
  96. package/dist-lib/lib/helpers/tool-call-markup.js +197 -82
  97. package/dist-lib/lib/hooks/built-in/arg-repair.d.ts +1 -0
  98. package/dist-lib/lib/hooks/built-in/arg-repair.js +1 -0
  99. package/dist-lib/lib/hooks/built-in/egress.d.ts +1 -0
  100. package/dist-lib/lib/hooks/built-in/egress.js +2 -1
  101. package/dist-lib/lib/hooks/built-in/sandbox.d.ts +1 -0
  102. package/dist-lib/lib/hooks/built-in/sandbox.js +6 -1
  103. package/dist-lib/lib/hooks/built-in/skill-catalog.d.ts +1 -0
  104. package/dist-lib/lib/hooks/built-in/skill-catalog.js +2 -1
  105. package/dist-lib/lib/hooks/pipeline.d.ts +4 -1
  106. package/dist-lib/lib/hooks/pipeline.js +24 -2
  107. package/dist-lib/lib/hooks/run.d.ts +2 -1
  108. package/dist-lib/lib/hooks/run.js +19 -7
  109. package/dist-lib/lib/hooks/types.d.ts +17 -6
  110. package/dist-lib/lib/machine/completion/actions/approval.d.ts +3 -0
  111. package/dist-lib/lib/machine/completion/actions/approval.js +16 -0
  112. package/dist-lib/lib/machine/completion/actions/completion.d.ts +23 -0
  113. package/dist-lib/lib/machine/completion/actions/completion.js +142 -0
  114. package/dist-lib/lib/machine/completion/actions/skills.d.ts +9 -0
  115. package/dist-lib/lib/machine/completion/actions/skills.js +83 -0
  116. package/dist-lib/lib/machine/completion/actions/tools.d.ts +16 -0
  117. package/dist-lib/lib/machine/completion/actions/tools.js +173 -0
  118. package/dist-lib/lib/machine/completion/context.d.ts +9 -0
  119. package/dist-lib/lib/machine/completion/context.js +44 -0
  120. package/dist-lib/lib/machine/completion/contract.d.ts +6 -0
  121. package/dist-lib/lib/machine/completion/contract.js +1 -0
  122. package/dist-lib/lib/machine/completion/guards/retry.d.ts +9 -0
  123. package/dist-lib/lib/machine/completion/guards/retry.js +25 -0
  124. package/dist-lib/lib/machine/completion/implementations.d.ts +81 -0
  125. package/dist-lib/lib/machine/completion/implementations.js +152 -0
  126. package/dist-lib/lib/machine/completion/machine.d.ts +375 -0
  127. package/dist-lib/lib/machine/completion/machine.js +511 -0
  128. package/dist-lib/lib/machine/completion/ports.d.ts +65 -0
  129. package/dist-lib/lib/machine/completion/ports.js +224 -0
  130. package/dist-lib/lib/machine/completion/rules.d.ts +39 -0
  131. package/dist-lib/lib/machine/completion/rules.js +115 -0
  132. package/dist-lib/lib/machine/completion/types.d.ts +493 -0
  133. package/dist-lib/lib/machine/completion/types.js +1 -0
  134. package/dist-lib/lib/machine/engine.d.ts +93 -0
  135. package/dist-lib/lib/machine/engine.js +483 -0
  136. package/dist-lib/lib/machine/errors.d.ts +44 -0
  137. package/dist-lib/lib/machine/errors.js +373 -0
  138. package/dist-lib/lib/machine/guards.d.ts +2 -0
  139. package/dist-lib/lib/machine/guards.js +6 -0
  140. package/dist-lib/lib/machine/index.d.ts +20 -0
  141. package/dist-lib/lib/machine/index.js +11 -0
  142. package/dist-lib/lib/machine/replay-output.d.ts +24 -0
  143. package/dist-lib/lib/machine/replay-output.js +50 -0
  144. package/dist-lib/lib/machine/stable-stringify.d.ts +1 -0
  145. package/dist-lib/lib/machine/stable-stringify.js +11 -0
  146. package/dist-lib/lib/models/model-pool.d.ts +14 -6
  147. package/dist-lib/lib/models/model-pool.js +95 -30
  148. package/dist-lib/lib/run-log/history-codec.d.ts +23 -0
  149. package/dist-lib/lib/run-log/history-codec.js +973 -0
  150. package/dist-lib/lib/run-log/patch.d.ts +6 -0
  151. package/dist-lib/lib/run-log/patch.js +93 -0
  152. package/dist-lib/lib/run-log/persisted-actor.d.ts +18 -0
  153. package/dist-lib/lib/run-log/persisted-actor.js +218 -0
  154. package/dist-lib/lib/run-log/protocol-types.d.ts +153 -0
  155. package/dist-lib/lib/run-log/protocol-types.js +1 -0
  156. package/dist-lib/lib/run-log/protocol.d.ts +27 -0
  157. package/dist-lib/lib/run-log/protocol.js +1056 -0
  158. package/dist-lib/lib/run-log/replay.d.ts +25 -0
  159. package/dist-lib/lib/run-log/replay.js +129 -0
  160. package/dist-lib/lib/run-log/run-journal.d.ts +125 -0
  161. package/dist-lib/lib/run-log/run-journal.js +783 -0
  162. package/dist-lib/lib/run-log-inspect.d.ts +25 -0
  163. package/dist-lib/lib/run-log-inspect.js +627 -0
  164. package/dist-lib/lib/run-log-protocol.d.ts +2 -0
  165. package/dist-lib/lib/run-log-protocol.js +1 -0
  166. package/dist-lib/lib/run-log-redact.d.ts +27 -0
  167. package/dist-lib/lib/run-log-redact.js +1107 -0
  168. package/dist-lib/lib/serve.js +307 -11
  169. package/dist-lib/lib/spawn.js +3 -2
  170. package/dist-lib/lib/tool-call-display.d.ts +1 -21
  171. package/dist-lib/lib/tools/exec/adapters/python/wheels.js +2 -1
  172. package/dist-lib/lib/tools/exec.d.ts +2 -1
  173. package/dist-lib/lib/tools/exec.js +57 -2
  174. package/dist-lib/lib/tools/generate-image.js +1 -1
  175. package/dist-lib/lib/tools/generate-music.js +1 -1
  176. package/dist-lib/lib/tools/http-request.d.ts +2 -1
  177. package/dist-lib/lib/tools/http-request.js +2 -1
  178. package/dist-lib/lib/tools/mcp-call.js +8 -1
  179. package/dist-lib/lib/tools/operations/builders.d.ts +1 -0
  180. package/dist-lib/lib/tools/operations/builders.js +1 -0
  181. package/dist-lib/lib/tools/operations/compile.js +11 -1
  182. package/dist-lib/lib/tools/operations/manifest.js +1 -0
  183. package/dist-lib/lib/tools/outbound-guard.d.ts +8 -6
  184. package/dist-lib/lib/tools/outbound-guard.js +25 -8
  185. package/dist-lib/lib/tools/skills/catalog.d.ts +1 -0
  186. package/dist-lib/lib/tools/skills/catalog.js +35 -0
  187. package/dist-lib/lib/tools/skills/cli-schema-types.d.ts +1 -0
  188. package/dist-lib/lib/tools/skills/cli-schema.js +25 -0
  189. package/dist-lib/lib/wire.d.ts +5 -0
  190. package/dist-lib/lib/wire.js +128 -5
  191. package/dist-lib/schema/harness.js +20 -5
  192. package/dist-lib/spec/hrpc/messages.js +95 -15
  193. package/dist-lib/spec/hyperschema/index.js +95 -15
  194. package/dist-lib/spec/hyperschema/schema.json +69 -1
  195. package/dist-lib/spec/hyperschema/types.d.ts +10 -3
  196. package/package.json +20 -2
  197. package/patches/@qvac%2Finference@0.20.0.patch +67 -0
  198. package/patches/bare-stow@0.1.5.patch +68 -0
  199. package/patches/hypercore-blob-server@1.15.0.patch +15 -0
  200. package/patches/localwatch@1.4.3.patch +13 -0
  201. package/patches/pyodide@314.0.2.patch +144 -0
@@ -0,0 +1,6 @@
1
+ import { isRecord } from '../guards.ts';
2
+ import type { Patch } from './protocol-types.ts';
3
+ export { isRecord };
4
+ export type { Patch } from './protocol-types.ts';
5
+ export declare function diff(prev: unknown, next: unknown): Patch | undefined;
6
+ export declare function apply(base: unknown, patch: Patch): unknown;
@@ -0,0 +1,93 @@
1
+ import { isRecord } from '../guards.js';
2
+ export { isRecord };
3
+ export function diff(prev, next) {
4
+ if (prev === next)
5
+ return undefined;
6
+ if (Array.isArray(prev) && Array.isArray(next)) {
7
+ let kept = 0;
8
+ while (kept < prev.length && kept < next.length && same(prev[kept], next[kept])) {
9
+ kept++;
10
+ }
11
+ if (kept === prev.length && kept === next.length) {
12
+ return undefined;
13
+ }
14
+ return { t: kept, a: next.slice(kept) };
15
+ }
16
+ if (isRecord(prev) && !Array.isArray(prev) && isRecord(next) && !Array.isArray(next)) {
17
+ const o = Object.create(null);
18
+ const d = [];
19
+ let changed = false;
20
+ for (const key of Object.keys(next)) {
21
+ if (next[key] === undefined)
22
+ continue;
23
+ const patch = Object.hasOwn(prev, key) && prev[key] !== undefined
24
+ ? diff(prev[key], next[key])
25
+ : { $: next[key] };
26
+ if (patch) {
27
+ o[key] = patch;
28
+ changed = true;
29
+ }
30
+ }
31
+ for (const key of Object.keys(prev)) {
32
+ if (prev[key] !== undefined && (!Object.hasOwn(next, key) || next[key] === undefined)) {
33
+ d.push(key);
34
+ changed = true;
35
+ }
36
+ }
37
+ if (!changed) {
38
+ return undefined;
39
+ }
40
+ return d.length ? { o, d } : { o };
41
+ }
42
+ return { $: next };
43
+ }
44
+ function same(prev, next) {
45
+ return prev === next || diff(prev, next) === undefined;
46
+ }
47
+ function isReplacementPatch(patch) {
48
+ return Object.hasOwn(patch, '$');
49
+ }
50
+ function isArrayPatch(patch) {
51
+ return Object.hasOwn(patch, 't');
52
+ }
53
+ function isObjectPatch(patch) {
54
+ return Object.hasOwn(patch, 'o');
55
+ }
56
+ export function apply(base, patch) {
57
+ if (isReplacementPatch(patch)) {
58
+ return patch.$;
59
+ }
60
+ if (isArrayPatch(patch)) {
61
+ const arrayPatch = patch;
62
+ const array = Array.isArray(base) ? base : [];
63
+ array.length = Math.min(array.length, arrayPatch.t);
64
+ for (const value of arrayPatch.a) {
65
+ array.push(value);
66
+ }
67
+ return array;
68
+ }
69
+ if (!isObjectPatch(patch)) {
70
+ throw new TypeError('Invalid JSON patch');
71
+ }
72
+ const objectPatch = patch;
73
+ const record = isRecord(base) && !Array.isArray(base) ? base : {};
74
+ for (const key of objectPatch.d ?? []) {
75
+ if (Object.hasOwn(record, key)) {
76
+ delete record[key];
77
+ }
78
+ }
79
+ for (const key of Object.keys(objectPatch.o)) {
80
+ const child = objectPatch.o[key];
81
+ if (child === undefined) {
82
+ continue;
83
+ }
84
+ const value = apply(Object.hasOwn(record, key) ? record[key] : undefined, child);
85
+ Object.defineProperty(record, key, {
86
+ value,
87
+ writable: true,
88
+ enumerable: true,
89
+ configurable: true
90
+ });
91
+ }
92
+ return record;
93
+ }
@@ -0,0 +1,18 @@
1
+ import { type AnyStateMachine, type Actor } from 'xstate';
2
+ import { RunJournal } from './run-journal.ts';
3
+ export interface MachineLog {
4
+ ready(): Promise<void>;
5
+ readonly length: number;
6
+ get(index: number): Promise<unknown>;
7
+ append(value: unknown): Promise<unknown>;
8
+ }
9
+ export interface PersistedActor<Machine extends AnyStateMachine> {
10
+ actor: Actor<Machine>;
11
+ persistence: {
12
+ journal: RunJournal;
13
+ checkpoint(): Promise<void>;
14
+ flush(): Promise<void>;
15
+ dispose(): Promise<void>;
16
+ };
17
+ }
18
+ export declare function createPersistedActor<Machine extends AnyStateMachine>(core: MachineLog | RunJournal, machine: Machine, onFailure?: (error: Error) => void, now?: () => number): Promise<PersistedActor<Machine>>;
@@ -0,0 +1,218 @@
1
+ import { createActor } from 'xstate';
2
+ import { EventCodec, RUN_LOG_VERSION, RunJournal } from './run-journal.js';
3
+ import { diff, isRecord } from './patch.js';
4
+ import { brandCapturedRecord, isCapturedJson, transitionName } from './protocol.js';
5
+ const CHECKPOINT_EVERY = 64;
6
+ const ROOT_ACTOR_ID = 'qvac-completion-machine';
7
+ const UNRECORDED = new Set(['OUTPUT', 'xstate.stop']);
8
+ const noClock = () => 0;
9
+ export async function createPersistedActor(core, machine, onFailure, now = noClock) {
10
+ const journal = core instanceof RunJournal ? core : new RunJournal(core);
11
+ if (!journal.restored) {
12
+ await journal.restore();
13
+ }
14
+ let observeChild;
15
+ const codec = new EventCodec(journal.requestHistoryBase);
16
+ const events = [];
17
+ const trace = [];
18
+ let fresh = true;
19
+ const recordedBase = Object.create(machine);
20
+ const recorded = Object.assign(recordedBase, {
21
+ transition(...args) {
22
+ if (!UNRECORDED.has(args[1].type)) {
23
+ events.push(codec.encode(journal.redactValue(args[1])));
24
+ }
25
+ return machine.transition(...args);
26
+ }
27
+ });
28
+ let rootActor;
29
+ const isRootActor = (ref) => rootActor !== undefined && ref === rootActor;
30
+ const actorRefId = (ref) => {
31
+ if (typeof ref !== 'object' || ref === null || !('id' in ref)) {
32
+ return undefined;
33
+ }
34
+ return typeof ref.id === 'string' ? ref.id : undefined;
35
+ };
36
+ const isInitialRootMicrostep = (event) => actorRefId(event.actorRef) === ROOT_ACTOR_ID &&
37
+ event._transitions.some((transition) => transition.source.id === machine.id || transition.source.id.startsWith(`${machine.id}.`));
38
+ const observeTrace = (event) => {
39
+ for (const step of traceFromMicrostep(event)) {
40
+ trace.push(step);
41
+ }
42
+ };
43
+ const actor = createActor(recorded, {
44
+ id: ROOT_ACTOR_ID,
45
+ snapshot: journal.snapshot,
46
+ inspect: (event) => {
47
+ if (event.type === '@xstate.microstep' &&
48
+ (isRootActor(event.actorRef) || isInitialRootMicrostep(event))) {
49
+ observeTrace(event);
50
+ }
51
+ if (event.type === '@xstate.snapshot' &&
52
+ !isRootActor(event.actorRef) &&
53
+ event.snapshot.status === 'active') {
54
+ observeChild?.();
55
+ }
56
+ }
57
+ });
58
+ rootActor = actor;
59
+ let base = journal.exportSnapshot();
60
+ let deltas = journal.deltasSinceCheckpoint;
61
+ let seenContext;
62
+ let seenStatus;
63
+ let seenValue;
64
+ let seenChildren;
65
+ let seenChildCount = 0;
66
+ let seenChildSnapshots = new WeakMap();
67
+ let disposed = false;
68
+ const previousOnFailure = journal.onFailure;
69
+ const fail = (error) => {
70
+ journal.failure ??= error;
71
+ actor.stop();
72
+ onFailure?.(error);
73
+ };
74
+ journal.onFailure = (error) => {
75
+ previousOnFailure?.(error);
76
+ fail(error);
77
+ };
78
+ const checkpoint = () => {
79
+ if (disposed || journal.failure) {
80
+ return journal.flush();
81
+ }
82
+ if (isDoneSnapshot(base)) {
83
+ return journal.flush();
84
+ }
85
+ try {
86
+ const live = actor.getSnapshot();
87
+ const children = live.children;
88
+ let childCount = 0;
89
+ let childrenUnchanged = true;
90
+ for (const id in children) {
91
+ if (!Object.hasOwn(children, id))
92
+ continue;
93
+ childCount++;
94
+ const child = children[id];
95
+ if (seenChildSnapshots.get(child) !== child.getSnapshot()) {
96
+ childrenUnchanged = false;
97
+ }
98
+ }
99
+ if (live.context === seenContext &&
100
+ live.status === seenStatus &&
101
+ live.value === seenValue &&
102
+ live.children === seenChildren &&
103
+ childCount === seenChildCount &&
104
+ childrenUnchanged &&
105
+ events.length === 0 &&
106
+ trace.length === 0) {
107
+ return journal.flush();
108
+ }
109
+ seenContext = live.context;
110
+ seenStatus = live.status;
111
+ seenValue = live.value;
112
+ seenChildren = live.children;
113
+ seenChildCount = childCount;
114
+ seenChildSnapshots = new WeakMap();
115
+ for (const id in children) {
116
+ if (!Object.hasOwn(children, id))
117
+ continue;
118
+ const child = children[id];
119
+ seenChildSnapshots.set(child, child.getSnapshot());
120
+ }
121
+ const snapshot = actor.getPersistedSnapshot();
122
+ const recordedSnapshot = journal.compactSnapshot(journal.redactValue(snapshot));
123
+ const savedAt = now();
124
+ const redactedPendingArgs = journal.pendingArgumentsWereRedacted(snapshot);
125
+ const redactedIds = journal.redactedPendingCallIds(snapshot);
126
+ const reset = fresh ? true : undefined;
127
+ if (base === undefined || deltas >= CHECKPOINT_EVERY) {
128
+ base = recordedSnapshot;
129
+ deltas = 0;
130
+ fresh = false;
131
+ const appended = journal.append({
132
+ version: RUN_LOG_VERSION,
133
+ type: 'checkpoint',
134
+ snapshot: recordedSnapshot,
135
+ savedAt,
136
+ redactedPendingArgs: redactedPendingArgs ?? undefined,
137
+ redactedPendingIds: redactedIds,
138
+ events: events.splice(0),
139
+ trace: trace.length ? trace.splice(0) : undefined,
140
+ reset
141
+ });
142
+ codec.reset();
143
+ return appended;
144
+ }
145
+ const patch = diff(base, recordedSnapshot);
146
+ if (!patch && events.length === 0 && trace.length === 0) {
147
+ return journal.flush();
148
+ }
149
+ base = recordedSnapshot;
150
+ deltas++;
151
+ fresh = false;
152
+ return journal.append({
153
+ version: RUN_LOG_VERSION,
154
+ type: 'delta',
155
+ patch: patch && isCapturedJson(recordedSnapshot)
156
+ ? brandCapturedRecord(patch)
157
+ : (patch ?? undefined),
158
+ savedAt,
159
+ redactedPendingArgs: redactedPendingArgs ?? undefined,
160
+ redactedPendingIds: redactedIds,
161
+ events: events.splice(0),
162
+ trace: trace.length ? trace.splice(0) : undefined,
163
+ reset
164
+ });
165
+ }
166
+ catch (error) {
167
+ const failure = error instanceof Error ? error : new Error(String(error));
168
+ fail(failure);
169
+ return Promise.reject(failure);
170
+ }
171
+ };
172
+ observeChild = () => {
173
+ void checkpoint().catch(noop);
174
+ };
175
+ const subscription = actor.subscribe({
176
+ next: () => {
177
+ void checkpoint().catch(noop);
178
+ },
179
+ error: (error) => fail(error instanceof Error ? error : new Error(String(error)))
180
+ });
181
+ return {
182
+ actor,
183
+ persistence: {
184
+ journal,
185
+ checkpoint,
186
+ flush: () => journal.flush(),
187
+ async dispose() {
188
+ if (!disposed) {
189
+ disposed = true;
190
+ subscription.unsubscribe();
191
+ actor.stop();
192
+ }
193
+ await journal.flush();
194
+ }
195
+ }
196
+ };
197
+ }
198
+ function traceFromMicrostep(event) {
199
+ if (UNRECORDED.has(event.event.type))
200
+ return [];
201
+ const trace = [];
202
+ for (const transition of event._transitions) {
203
+ if (UNRECORDED.has(transition.eventType))
204
+ continue;
205
+ trace.push({
206
+ eventType: transition.eventType,
207
+ sourceIds: [transition.source.id],
208
+ targetIds: transition.target?.map((state) => state.id) ?? [],
209
+ guardNames: transition.guard ? [transitionName(transition.guard)] : [],
210
+ actionNames: transition.actions.map(transitionName)
211
+ });
212
+ }
213
+ return trace;
214
+ }
215
+ function isDoneSnapshot(value) {
216
+ return isRecord(value) && value.status === 'done';
217
+ }
218
+ function noop() { }
@@ -0,0 +1,153 @@
1
+ export interface Operation {
2
+ id: string;
3
+ name: string;
4
+ args: unknown;
5
+ effect: 'read' | 'write';
6
+ reason?: string;
7
+ }
8
+ export interface RunBudget {
9
+ readonly deadlineMs: number;
10
+ outputBytesUsed: number;
11
+ }
12
+ export type Patch = {
13
+ $: unknown;
14
+ } | {
15
+ o: Record<string, Patch>;
16
+ d?: string[];
17
+ } | {
18
+ t: number;
19
+ a: unknown[];
20
+ };
21
+ export interface HistoryReference {
22
+ $qvacHistory: {
23
+ version: 1;
24
+ base: 'request.messages';
25
+ items: ({
26
+ index: number;
27
+ } | {
28
+ inline: unknown;
29
+ })[];
30
+ };
31
+ }
32
+ export type RecoveryResolution = {
33
+ operationId: string;
34
+ decision: 'retry';
35
+ } | {
36
+ operationId: string;
37
+ decision: 'result';
38
+ result: unknown;
39
+ } | {
40
+ operationId: string;
41
+ decision: 'skip';
42
+ };
43
+ export interface ForkOrigin {
44
+ runId: string;
45
+ length: number;
46
+ key?: string;
47
+ }
48
+ export interface ForkSegment {
49
+ key: string;
50
+ length: number;
51
+ }
52
+ export interface MachineTransitionTrace {
53
+ eventType: string;
54
+ sourceIds: string[];
55
+ targetIds: string[];
56
+ guardNames: string[];
57
+ actionNames: string[];
58
+ }
59
+ export type Receipt = (Operation & {
60
+ facts?: unknown;
61
+ status: 'intent' | 'retry';
62
+ }) | {
63
+ id: string;
64
+ name: string;
65
+ effect: Operation['effect'];
66
+ facts?: unknown;
67
+ status: 'result';
68
+ result: unknown;
69
+ resultUndefined?: true;
70
+ abandoned?: true;
71
+ };
72
+ export type RunRecord = {
73
+ version: 5;
74
+ type: 'metadata';
75
+ runId: string;
76
+ request: unknown;
77
+ requestDigest?: string;
78
+ digestRunId?: string;
79
+ budget: RunBudget;
80
+ machine: number;
81
+ definition?: unknown;
82
+ xstate?: string;
83
+ forkedFrom?: ForkOrigin;
84
+ startedAt?: number;
85
+ } | {
86
+ version: 5;
87
+ type: 'fork';
88
+ runId: string;
89
+ forkedFrom: ForkOrigin;
90
+ request?: Record<string, unknown>;
91
+ segments?: ForkSegment[];
92
+ } | {
93
+ version: 5;
94
+ type: 'checkpoint';
95
+ snapshot: unknown;
96
+ savedAt: number;
97
+ redactedPendingArgs?: boolean;
98
+ redactedPendingIds?: string[];
99
+ events?: Patch[];
100
+ trace?: MachineTransitionTrace[];
101
+ reset?: true;
102
+ } | {
103
+ version: 5;
104
+ type: 'delta';
105
+ patch?: Patch;
106
+ savedAt: number;
107
+ redactedPendingArgs?: boolean;
108
+ redactedPendingIds?: string[];
109
+ events: Patch[];
110
+ trace?: MachineTransitionTrace[];
111
+ reset?: true;
112
+ } | {
113
+ version: 5;
114
+ type: 'operation';
115
+ receipt: Receipt;
116
+ };
117
+ export interface EngineStep {
118
+ seq: number;
119
+ expectedLength: number;
120
+ records: readonly RunRecord[];
121
+ }
122
+ export interface EngineBoot {
123
+ protocolVersion: 1;
124
+ length: number;
125
+ nextSeq: number;
126
+ machineVersion: number | null;
127
+ xstateVersion: string | null;
128
+ metadata: Extract<RunRecord, {
129
+ type: 'metadata';
130
+ }> | null;
131
+ snapshot?: unknown;
132
+ eventState: unknown | null;
133
+ snapshotSavedAt?: number;
134
+ firstSavedAt?: number;
135
+ deltasSinceCheckpoint: number;
136
+ redactedPendingArgs: boolean | null;
137
+ redactedPendingIds: string[];
138
+ unredactedLength: number;
139
+ restoredRedacted: boolean;
140
+ forkBeforeWrite: Array<[string, number]>;
141
+ receipts: Receipt[];
142
+ intentArgs: Array<[string, unknown]>;
143
+ }
144
+ export interface HostRunLogLease {
145
+ generation: string;
146
+ boot?: EngineBoot;
147
+ digestKey: Uint8Array;
148
+ }
149
+ export interface HostRunLog {
150
+ begin(runId: string): Promise<HostRunLogLease>;
151
+ append(runId: string, generation: string, step: EngineStep): Promise<void>;
152
+ release(runId: string, generation: string): Promise<void>;
153
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import type { EngineBoot, EngineStep, Receipt, RunRecord } from './protocol-types.ts';
2
+ export declare const RUN_LOG_PROTOCOL_VERSION: 1;
3
+ export declare const RUN_LOG_VERSION: 5;
4
+ export type { EngineBoot, EngineStep, ForkOrigin, ForkSegment, HostRunLog, HostRunLogLease, HistoryReference, MachineTransitionTrace, Operation, Patch, Receipt, RecoveryResolution, RunBudget, RunRecord } from './protocol-types.ts';
5
+ export declare function transitionName(named: unknown): string;
6
+ export declare class InvalidEngineStep extends Error {
7
+ constructor(message: string);
8
+ }
9
+ export declare function pendingToolArgs(snapshot: unknown, settled?: (receiptId: string) => boolean): unknown[];
10
+ export declare function pendingCallNames(snapshot: unknown): Map<string, string>;
11
+ export declare function pendingCallArgs(snapshot: unknown, settled?: (receiptId: string) => boolean): Map<string, unknown>;
12
+ export declare function unsettledRedactedIds(redactedPendingIds: readonly string[], receipts: Iterable<Receipt>): string[];
13
+ export declare function createEngineBoot(): EngineBoot;
14
+ export declare function prepareEngineBoot(value?: EngineBoot): EngineBoot;
15
+ export declare function captureRunRecord(record: unknown): RunRecord;
16
+ export declare function brandCapturedRecord<Value>(value: Value): Value;
17
+ export declare function isCapturedJson(value: unknown): boolean;
18
+ export declare function createEngineStep(seq: number, expectedLength: number, records: readonly RunRecord[]): EngineStep;
19
+ export declare function captureEngineStep(step: EngineStep): EngineStep;
20
+ export declare function requestFromBoot(boot: EngineBoot): unknown;
21
+ export declare function contextFromBoot(boot: EngineBoot): unknown;
22
+ export interface BuildEngineBootOptions {
23
+ nextSeq: number;
24
+ }
25
+ export declare function buildEngineBoot(records: readonly RunRecord[], opts: BuildEngineBootOptions): EngineBoot;
26
+ export declare function applyEngineStep(boot: EngineBoot, step: EngineStep): EngineBoot;
27
+ export declare function validateEngineBoot(boot: EngineBoot): void;