@archboard/codex-realtime-voice 0.0.0-stage → 0.2.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.
@@ -0,0 +1,289 @@
1
+ import { S as readWorkbenchInstructionDocument, U as ADDITIONAL_CONTEXT_POLICY, W as createTextUserInput, b as EMPTY_SPOKEN_APPROVAL_SNAPSHOT, g as emptyManifest, r as composeWorkbenchRoleInstructions, x as createCodexRealtimeAdapter } from "./instructions-CmBJHqpa.mjs";
2
+ // src/runtime/workhorses/wait-graph/state/wait-graph.ts
3
+ /**
4
+ * Order two strings by code unit so every listing the graph produces is deterministic.
5
+ * @param left - The first string.
6
+ * @param right - The second string.
7
+ * @returns A negative, zero or positive comparison result.
8
+ */
9
+ function compareText(left, right) {
10
+ return left < right ? -1 : left > right ? 1 : 0;
11
+ }
12
+ /**
13
+ * Build the map key that identifies one dynamic operation's edge set.
14
+ * @param owner - The child, calling thread, turn and call that own the edges.
15
+ * @returns A stable string key unique to that owner.
16
+ */
17
+ function ownerKey(owner) {
18
+ return JSON.stringify([
19
+ owner.child,
20
+ owner.caller,
21
+ owner.turn,
22
+ owner.call
23
+ ]);
24
+ }
25
+ /**
26
+ * Build the graph vertex key for one thread as seen from one child; threads of different children
27
+ * never share a vertex, so waits cannot cycle across children.
28
+ * @param child - The child whose thread this is.
29
+ * @param thread - The thread.
30
+ * @returns A stable string key unique to that vertex.
31
+ */
32
+ function vertexKey(child, thread) {
33
+ return JSON.stringify([child, thread]);
34
+ }
35
+ /**
36
+ * Copy an owner into a frozen record so the graph never keeps a reference the caller can mutate.
37
+ * @param owner - The owner to copy.
38
+ * @returns A frozen copy carrying the same identity.
39
+ */
40
+ function copyOwner(owner) {
41
+ return Object.freeze({
42
+ child: owner.child,
43
+ caller: owner.caller,
44
+ turn: owner.turn,
45
+ call: owner.call
46
+ });
47
+ }
48
+ /**
49
+ * Deduplicate and sort target threads so an edge set has one canonical form.
50
+ * @param targets - The threads the owner waits on, in any order and possibly repeated.
51
+ * @returns The unique targets in sorted order.
52
+ */
53
+ function canonicalTargets(targets) {
54
+ return Object.freeze([...new Set(targets)].toSorted(compareText));
55
+ }
56
+ /**
57
+ * Turn caller input into the frozen registration the graph stores.
58
+ * @param input - The owner and the targets it waits on.
59
+ * @returns The registration keyed by owner with canonical targets.
60
+ */
61
+ function registrationFor(input) {
62
+ const owner = copyOwner(input.owner);
63
+ return Object.freeze({
64
+ key: ownerKey(owner),
65
+ owner,
66
+ targets: canonicalTargets(input.targets)
67
+ });
68
+ }
69
+ /**
70
+ * Build one edge from a registration to a target thread.
71
+ * @param registration - The owner's registration.
72
+ * @param target - The thread waited on.
73
+ * @returns A frozen edge.
74
+ */
75
+ function edgeFor(registration, target) {
76
+ return Object.freeze({
77
+ owner: registration.owner,
78
+ target
79
+ });
80
+ }
81
+ /**
82
+ * Expand a registration into one edge per target.
83
+ * @param registration - The owner's registration.
84
+ * @returns The frozen edges in canonical target order.
85
+ */
86
+ function edgesFor(registration) {
87
+ return Object.freeze(registration.targets.map((target) => edgeFor(registration, target)));
88
+ }
89
+ /**
90
+ * Order edges by owner and then by target.
91
+ * @param left - The first edge.
92
+ * @param right - The second edge.
93
+ * @returns A negative, zero or positive comparison result.
94
+ */
95
+ function compareEdges(left, right) {
96
+ const ownerComparison = compareText(ownerKey(left.owner), ownerKey(right.owner));
97
+ return ownerComparison !== 0 ? ownerComparison : compareText(left.target, right.target);
98
+ }
99
+ /**
100
+ * Flatten registrations into one sorted, frozen edge list.
101
+ * @param registrations - The registrations to list.
102
+ * @returns Every edge of every registration in deterministic order.
103
+ */
104
+ function sortedEdges(registrations) {
105
+ return Object.freeze([...registrations].flatMap((registration) => edgesFor(registration)).toSorted(compareEdges));
106
+ }
107
+ /**
108
+ * Ensure a vertex exists for a thread in a child's graph and return its key.
109
+ * @param child - The child whose thread this is.
110
+ * @param thread - The thread.
111
+ * @param nodes - The vertex map being built.
112
+ * @param adjacency - The adjacency map being built.
113
+ * @returns The vertex key.
114
+ */
115
+ function addNode(child, thread, nodes, adjacency) {
116
+ const key = vertexKey(child, thread);
117
+ if (!nodes.has(key)) {
118
+ nodes.set(key, {
119
+ child,
120
+ thread
121
+ });
122
+ adjacency.set(key, /* @__PURE__ */ new Set());
123
+ }
124
+ return key;
125
+ }
126
+ /**
127
+ * Build the directed graph of "caller waits on target" edges from a set of registrations.
128
+ * @param registrations - The registrations to graph.
129
+ * @returns The vertex map and adjacency sets.
130
+ */
131
+ function graphOf(registrations) {
132
+ const nodes = /* @__PURE__ */ new Map();
133
+ const adjacency = /* @__PURE__ */ new Map();
134
+ for (const registration of registrations) {
135
+ const source = addNode(registration.owner.child, registration.owner.caller, nodes, adjacency);
136
+ for (const target of registration.targets) {
137
+ const destination = addNode(registration.owner.child, target, nodes, adjacency);
138
+ adjacency.get(source).add(destination);
139
+ }
140
+ }
141
+ return {
142
+ nodes,
143
+ adjacency
144
+ };
145
+ }
146
+ /**
147
+ * Close the cycle that a back edge to a vertex still on the traversal stack has just revealed.
148
+ * @param target - The vertex the back edge points at.
149
+ * @param stack - The vertices on the current traversal path, in order.
150
+ * @param stackIndex - Each stacked vertex's position in that path.
151
+ * @param nodes - The vertex map.
152
+ * @returns The closed path from the target back to itself.
153
+ */
154
+ function cycleClosedAt(target, stack, stackIndex, nodes) {
155
+ const start = stackIndex.get(target);
156
+ if (start === void 0) throw new Error("Wait graph traversal lost its stack index.");
157
+ return [...stack.slice(start), target].map((nodeKey) => nodes.get(nodeKey));
158
+ }
159
+ /**
160
+ * Search a set of registrations for a wait cycle by depth-first traversal in deterministic order,
161
+ * so a refusal always names the same cycle for the same graph.
162
+ * @param registrations - The registrations to search, including the one being proposed.
163
+ * @returns The first cycle found as a closed path, or null when the graph is acyclic.
164
+ */
165
+ function findCycle(registrations) {
166
+ const { nodes, adjacency } = graphOf(registrations);
167
+ const state = /* @__PURE__ */ new Map();
168
+ const stack = [];
169
+ const stackIndex = /* @__PURE__ */ new Map();
170
+ /**
171
+ * Visit one vertex and everything reachable from it that has not been visited yet.
172
+ * @param key - The vertex to visit.
173
+ * @returns The cycle found below this vertex, or null.
174
+ */
175
+ const visit = (key) => {
176
+ state.set(key, "visiting");
177
+ stackIndex.set(key, stack.length);
178
+ stack.push(key);
179
+ const targets = [...adjacency.get(key) ?? []].toSorted(compareText);
180
+ for (const target of targets) {
181
+ const targetState = state.get(target);
182
+ if (targetState === "visiting") return cycleClosedAt(target, stack, stackIndex, nodes);
183
+ const cycle = targetState === void 0 ? visit(target) : null;
184
+ if (cycle) return cycle;
185
+ }
186
+ stack.pop();
187
+ stackIndex.delete(key);
188
+ state.set(key, "visited");
189
+ return null;
190
+ };
191
+ for (const key of [...nodes.keys()].toSorted(compareText)) {
192
+ if (state.has(key)) continue;
193
+ const cycle = visit(key);
194
+ if (cycle) return cycle;
195
+ }
196
+ return null;
197
+ }
198
+ /**
199
+ * Sort and freeze a list of edges for return to a caller.
200
+ * @param edges - The edges to freeze.
201
+ * @returns The edges in deterministic order.
202
+ */
203
+ function freezeEdges(edges) {
204
+ return Object.freeze([...edges].toSorted(compareEdges));
205
+ }
206
+ /**
207
+ * Convert a cycle of graph vertices into the child and thread path a refusal reports.
208
+ * @param nodes - The closed path found by traversal.
209
+ * @returns The child the cycle belongs to and its thread path.
210
+ */
211
+ function freezeCycle(nodes) {
212
+ const child = nodes[0]?.child;
213
+ if (child === void 0) throw new Error("A wait cycle must contain at least one node.");
214
+ return {
215
+ child,
216
+ cycle: Object.freeze(nodes.map((node) => node.thread))
217
+ };
218
+ }
219
+ /**
220
+ * Pick the registration keys a cleanup removes: exactly one owner, or every owner of an exited
221
+ * child.
222
+ * @param cleanup - The cleanup being applied.
223
+ * @param registrations - The live registrations.
224
+ * @returns The keys to remove, whether or not each is currently registered.
225
+ */
226
+ function keysReleasedBy(cleanup, registrations) {
227
+ if (cleanup.cause !== "child-exit") return /* @__PURE__ */ new Set([ownerKey(cleanup.owner)]);
228
+ const keys = /* @__PURE__ */ new Set();
229
+ for (const [key, registration] of registrations) if (registration.owner.child === cleanup.child) keys.add(key);
230
+ return keys;
231
+ }
232
+ /**
233
+ * Create the wait graph that refuses a dynamic wait which would deadlock: an edge set is accepted
234
+ * only when the graph with it stays acyclic.
235
+ * @returns A fresh, empty graph.
236
+ */
237
+ function createCodexWaitGraph() {
238
+ const registrations = /* @__PURE__ */ new Map();
239
+ /**
240
+ * Add or replace one owner's edge set, refusing it when it would close a cycle.
241
+ * @param input - The owner and the targets it waits on.
242
+ * @returns The accepted edges, or the cycle that refused them.
243
+ */
244
+ const addEdgeSet = (input) => {
245
+ const registration = registrationFor(input);
246
+ const proposed = new Map(registrations);
247
+ proposed.set(registration.key, registration);
248
+ const cycle = findCycle(proposed.values());
249
+ if (cycle) {
250
+ const refusal = freezeCycle(cycle);
251
+ return Object.freeze({
252
+ ok: false,
253
+ reason: "cycle",
254
+ ...refusal
255
+ });
256
+ }
257
+ registrations.set(registration.key, registration);
258
+ return Object.freeze({
259
+ ok: true,
260
+ edges: edgesFor(registration)
261
+ });
262
+ };
263
+ /**
264
+ * Remove the edge sets a cleanup names and report which edges went away.
265
+ * @param cleanup - The owner, or the exited child, whose waits end.
266
+ * @returns The removed edges in deterministic order.
267
+ */
268
+ const release = (cleanup) => {
269
+ const keys = keysReleasedBy(cleanup, registrations);
270
+ const removed = [];
271
+ for (const key of keys) {
272
+ const registration = registrations.get(key);
273
+ if (registration) removed.push(...edgesFor(registration));
274
+ }
275
+ for (const key of keys) registrations.delete(key);
276
+ return freezeEdges(removed);
277
+ };
278
+ /**
279
+ * List every active edge in deterministic order.
280
+ * @returns The active edges.
281
+ */
282
+ const inspect = () => sortedEdges(registrations.values());
283
+ return Object.freeze({
284
+ addEdgeSet,
285
+ release,
286
+ inspect
287
+ });
288
+ }
289
+ export { ADDITIONAL_CONTEXT_POLICY, EMPTY_SPOKEN_APPROVAL_SNAPSHOT, composeWorkbenchRoleInstructions, createCodexRealtimeAdapter, createCodexWaitGraph, createTextUserInput, emptyManifest, readWorkbenchInstructionDocument };