@re-cinq/floor-assembly-lines 0.0.0-stage → 0.6.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.
- package/LICENSE +201 -0
- package/README.md +146 -2
- package/dist/agent-verdict.d.ts +14 -0
- package/dist/agent-verdict.js +55 -0
- package/dist/definition-hash.d.ts +1 -0
- package/dist/definition-hash.js +21 -0
- package/dist/diamond.fixtures.d.ts +6 -0
- package/dist/diamond.fixtures.js +33 -0
- package/dist/event-names.d.ts +3 -0
- package/dist/event-names.js +4 -0
- package/dist/fanout.d.ts +26 -0
- package/dist/fanout.fixtures.d.ts +4 -0
- package/dist/fanout.fixtures.js +32 -0
- package/dist/fanout.js +120 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +16 -0
- package/dist/line-args.d.ts +2 -0
- package/dist/line-args.js +5 -0
- package/dist/line-fanout.d.ts +4 -0
- package/dist/line-fanout.js +91 -0
- package/dist/line-graph.d.ts +10 -0
- package/dist/line-graph.js +52 -0
- package/dist/line-needs.d.ts +40 -0
- package/dist/line-needs.js +100 -0
- package/dist/line-stations.d.ts +14 -0
- package/dist/line-stations.js +15 -0
- package/dist/line-validation.d.ts +6 -0
- package/dist/line-validation.js +169 -0
- package/dist/node-events.d.ts +41 -0
- package/dist/node-events.js +57 -0
- package/dist/placeholder-pattern.d.ts +2 -0
- package/dist/placeholder-pattern.js +7 -0
- package/dist/prompt-parameters.d.ts +20 -0
- package/dist/prompt-parameters.js +51 -0
- package/dist/prompt-placeholders.d.ts +28 -0
- package/dist/prompt-placeholders.js +47 -0
- package/dist/refusal.d.ts +3 -0
- package/dist/refusal.js +8 -0
- package/dist/start-events.d.ts +4 -0
- package/dist/start-events.js +13 -0
- package/dist/transition.d.ts +71 -0
- package/dist/transition.js +162 -0
- package/dist/walk-budget.d.ts +8 -0
- package/dist/walk-budget.js +27 -0
- package/package.json +58 -4
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { foldFans, launchOf, relaunchMissing } from "./fanout.js";
|
|
2
|
+
import { budgetOutcome, isTransition, isUnbudgetedForwardHop } from "./walk-budget.js";
|
|
3
|
+
export function endsRun(transition) {
|
|
4
|
+
return transition.kind === "finish" || transition.kind === "halt";
|
|
5
|
+
}
|
|
6
|
+
const DEFAULT_MAX_NODES = 200;
|
|
7
|
+
/** The exit and the fail node are both terminal: a walk that arrives at either stops there. */
|
|
8
|
+
export function isTerminalNode(assemblyLine, nodeId) {
|
|
9
|
+
return nodeId === assemblyLine.exit || nodeId === assemblyLine.fail;
|
|
10
|
+
}
|
|
11
|
+
// The executor's edge rule: exact-outcome match preferred over `always`; null when nothing matches.
|
|
12
|
+
export function selectEdge(assemblyLine, from, outcome) {
|
|
13
|
+
const candidates = assemblyLine.edges.filter((edge) => edge.from === from && (edge.on === outcome || edge.on === "always"));
|
|
14
|
+
return candidates.find((edge) => edge.on === outcome) ?? candidates.at(0) ?? null;
|
|
15
|
+
}
|
|
16
|
+
// Replay the visit history for what happens next (the sole routing definition): a revisited node bumps the iteration, and a budgeted edge additionally halts past its iterationMax.
|
|
17
|
+
export function getNextTransition(assemblyLine, visits, maxNodes = DEFAULT_MAX_NODES) {
|
|
18
|
+
const early = relaunchMissing(assemblyLine, visits) ?? replayBlocked(assemblyLine, visits, maxNodes);
|
|
19
|
+
if (early) {
|
|
20
|
+
return early;
|
|
21
|
+
}
|
|
22
|
+
const folded = foldFans(assemblyLine, visits);
|
|
23
|
+
const { state, failure } = replayFromLastHandRun(assemblyLine, folded);
|
|
24
|
+
return failure ?? stepFrom(assemblyLine, { visits, folded, state });
|
|
25
|
+
}
|
|
26
|
+
function stepFrom(assemblyLine, { visits, folded, state }) {
|
|
27
|
+
// A visit is always on record here: only a visit's outcome can carry the walk onto fail, which validation refuses as the entry.
|
|
28
|
+
if (state.currentId === assemblyLine.fail) {
|
|
29
|
+
return deliberateFailure(assemblyLine, folded.at(-1));
|
|
30
|
+
}
|
|
31
|
+
return state.currentId === assemblyLine.exit ? { kind: "finish" } : launchOf(assemblyLine, visits, state);
|
|
32
|
+
}
|
|
33
|
+
// Why the replay cannot produce a next step. An unfinished visit means the answer is not knowable yet rather than wrong; the node ceiling means the definition is cycling and the walk would never reach exit.
|
|
34
|
+
function replayBlocked(assemblyLine, visits, maxNodes) {
|
|
35
|
+
if (visits.some((visit) => visit.outcome === null)) {
|
|
36
|
+
return { kind: "await" };
|
|
37
|
+
}
|
|
38
|
+
if (unattendedVisits(assemblyLine, visits) >= maxNodes) {
|
|
39
|
+
return {
|
|
40
|
+
kind: "halt",
|
|
41
|
+
outcome: "error",
|
|
42
|
+
reason: `AssemblyLine ${assemblyLine.name}: maxNodes (${maxNodes}) reached without hitting exit or a person acting`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
// The ceiling guards a MACHINE loop that never ends, so it counts only the visits since a person last acted — the same exemption a human-gated back-edge gets from iterationMax.
|
|
48
|
+
function unattendedVisits(assemblyLine, visits) {
|
|
49
|
+
const human = new Set((assemblyLine.nodes ?? [])
|
|
50
|
+
.filter((node) => node.kind === "human")
|
|
51
|
+
.map((node) => node.id));
|
|
52
|
+
return (visits.length -
|
|
53
|
+
1 -
|
|
54
|
+
visits.findLastIndex((visit) => human.has(visit.nodeId) || Boolean(visit.requestedBy)));
|
|
55
|
+
}
|
|
56
|
+
/** The visits the walk routes on: from the last one a person ran by hand, or all of them when nobody did. */
|
|
57
|
+
export function visitsSinceHandRun(visits) {
|
|
58
|
+
return visits.slice(Math.max(0, lastHandRun(visits)));
|
|
59
|
+
}
|
|
60
|
+
// Where the walk got to, or the Transition that ended it — replayed from the last hand-run visit on.
|
|
61
|
+
function replayFromLastHandRun(assemblyLine, visits) {
|
|
62
|
+
const { state, accounting } = walkFromLastHandRun(assemblyLine, visits);
|
|
63
|
+
const since = visitsSinceHandRun(visits);
|
|
64
|
+
return {
|
|
65
|
+
state,
|
|
66
|
+
failure: replayVisits(assemblyLine, since, state, accounting),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// Starts where the person put the walk. The visits before still number the iterations after, or a revisit would collide with a row already recorded; their back-edges are not carried, because a person acting grants a fresh budget.
|
|
70
|
+
function walkFromLastHandRun(assemblyLine, visits) {
|
|
71
|
+
const walk = freshWalk(assemblyLine, visits);
|
|
72
|
+
const index = lastHandRun(visits);
|
|
73
|
+
if (index < 0) {
|
|
74
|
+
return walk;
|
|
75
|
+
}
|
|
76
|
+
visits.slice(0, index).forEach((visit) => recordVisit(visit, walk.accounting));
|
|
77
|
+
walk.state.currentId = visits[index].nodeId;
|
|
78
|
+
walk.state.iteration = visits[index].iteration;
|
|
79
|
+
return walk;
|
|
80
|
+
}
|
|
81
|
+
function lastHandRun(visits) {
|
|
82
|
+
return visits.findLastIndex((visit) => visit.requestedBy);
|
|
83
|
+
}
|
|
84
|
+
// A walk that has not moved: on its first iteration, having spent nothing, at the node the run was started at. That is the node of its first visit — a start may name a node other than the line's entry — and the line's entry for a run with no visit yet. Every replay begins here — the history is what moves it, so nothing is carried over between calls.
|
|
85
|
+
function freshWalk(assemblyLine, visits) {
|
|
86
|
+
return {
|
|
87
|
+
state: { currentId: visits[0]?.nodeId ?? assemblyLine.entry, iteration: 1 },
|
|
88
|
+
accounting: {
|
|
89
|
+
backEdgeCounts: new Map(),
|
|
90
|
+
highestIteration: new Map(),
|
|
91
|
+
visited: new Set(),
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function replayVisits(assemblyLine, visits, state, accounting) {
|
|
96
|
+
for (const visit of visits) {
|
|
97
|
+
const failure = applyVisit(assemblyLine, visit, state, accounting);
|
|
98
|
+
if (failure) {
|
|
99
|
+
return failure;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
// One visit's contribution to the walk: mutates state/accounting toward the next node, or returns the Transition that ends the replay.
|
|
105
|
+
function applyVisit(assemblyLine, visit, state, accounting) {
|
|
106
|
+
recordVisit(visit, accounting);
|
|
107
|
+
const diverged = divergenceFailure(assemblyLine, visit, state);
|
|
108
|
+
if (diverged) {
|
|
109
|
+
return diverged;
|
|
110
|
+
}
|
|
111
|
+
const chosen = selectEdge(assemblyLine, visit.nodeId, visit.outcome);
|
|
112
|
+
if (!chosen) {
|
|
113
|
+
return noEdgeFailure(assemblyLine, visit);
|
|
114
|
+
}
|
|
115
|
+
return followEdge(assemblyLine, visit, chosen, { state, accounting });
|
|
116
|
+
}
|
|
117
|
+
function recordVisit(visit, accounting) {
|
|
118
|
+
accounting.visited.add(visit.nodeId);
|
|
119
|
+
accounting.highestIteration.set(visit.nodeId, Math.max(accounting.highestIteration.get(visit.nodeId) ?? 0, visit.iteration));
|
|
120
|
+
}
|
|
121
|
+
// Node id AND iteration must both match the recomputed walk, or a wrong-iteration row replays cleanly while a compare-and-set hits a different iteration's row (silent split-brain).
|
|
122
|
+
function divergenceFailure(assemblyLine, visit, state) {
|
|
123
|
+
if (visit.nodeId === state.currentId &&
|
|
124
|
+
visit.iteration === state.iteration) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
kind: "halt",
|
|
129
|
+
outcome: "error",
|
|
130
|
+
reason: `AssemblyLine ${assemblyLine.name}: node rows diverge from the definition (recorded "${visit.nodeId}" iter ${visit.iteration}, expected "${state.currentId}" iter ${state.iteration})`,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function noEdgeFailure(assemblyLine, visit) {
|
|
134
|
+
return {
|
|
135
|
+
kind: "halt",
|
|
136
|
+
outcome: "error",
|
|
137
|
+
reason: `AssemblyLine ${assemblyLine.name}: no edge from "${visit.nodeId}" for outcome "${visit.outcome}"`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
// The line chose to end here: the reason names the visit whose outcome led to the fail node.
|
|
141
|
+
function deliberateFailure(assemblyLine, visit) {
|
|
142
|
+
const why = visit
|
|
143
|
+
? `node "${visit.nodeId}" reported "${visit.outcome}"`
|
|
144
|
+
: `the walk began at the fail node "${assemblyLine.fail}"`;
|
|
145
|
+
return { kind: "halt", outcome: "failed", reason: `AssemblyLine ${assemblyLine.name}: ${why}` };
|
|
146
|
+
}
|
|
147
|
+
// Moves the walk along `chosen`, or returns the Transition that ends it. A plain forward hop only sets the node; a revisit additionally bumps the iteration past the highest already recorded, which is what makes a second attempt distinguishable from the first.
|
|
148
|
+
function followEdge(assemblyLine, visit, chosen, walk) {
|
|
149
|
+
const { state, accounting } = walk;
|
|
150
|
+
if (isUnbudgetedForwardHop(chosen, accounting)) {
|
|
151
|
+
state.currentId = chosen.to;
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
const outcome = budgetOutcome(assemblyLine, visit, chosen, accounting);
|
|
155
|
+
if (isTransition(outcome)) {
|
|
156
|
+
return outcome;
|
|
157
|
+
}
|
|
158
|
+
state.iteration = (accounting.highestIteration.get(outcome.nextId) ?? 0) + 1;
|
|
159
|
+
state.currentId = outcome.nextId;
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=transition.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { NodeVisit, Transition, WalkAccounting, WalkEdge, WalkGraph } from "./transition.js";
|
|
2
|
+
export declare function isUnbudgetedForwardHop(chosen: WalkEdge, accounting: WalkAccounting): boolean;
|
|
3
|
+
export declare function budgetOutcome(assemblyLine: WalkGraph, visit: NodeVisit, chosen: WalkEdge, accounting: WalkAccounting): Transition | {
|
|
4
|
+
nextId: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function isTransition(outcome: Transition | {
|
|
7
|
+
nextId: string;
|
|
8
|
+
}): outcome is Transition;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// The retry budget of a walk: which edge hops are free, and when a revisited or budgeted edge has spent what the definition allowed it.
|
|
2
|
+
// A fresh forward hop with no budget just moves on — only a revisit or a budgeted edge needs the accounting below.
|
|
3
|
+
export function isUnbudgetedForwardHop(chosen, accounting) {
|
|
4
|
+
return (chosen.iterationMax === undefined && !accounting.visited.has(chosen.to));
|
|
5
|
+
}
|
|
6
|
+
// The budget decision for a revisited/budgeted edge: an exhausted budget halts with its own reason, otherwise the edge is spent and the walk advances to its target.
|
|
7
|
+
export function budgetOutcome(assemblyLine, visit, chosen, accounting) {
|
|
8
|
+
const key = `${chosen.from}->${chosen.to}`;
|
|
9
|
+
const count = (accounting.backEdgeCounts.get(key) ?? 0) + 1;
|
|
10
|
+
if (chosen.iterationMax !== undefined && count > chosen.iterationMax) {
|
|
11
|
+
return budgetSpent(assemblyLine, visit, chosen, key);
|
|
12
|
+
}
|
|
13
|
+
accounting.backEdgeCounts.set(key, count);
|
|
14
|
+
return { nextId: chosen.to };
|
|
15
|
+
}
|
|
16
|
+
export function isTransition(outcome) {
|
|
17
|
+
return "kind" in outcome;
|
|
18
|
+
}
|
|
19
|
+
// The retry budget for this edge is gone. The budget is HOW the run ended; the visit's own error (if any) is left for the caller to attach, since this kernel carries no failure detail of its own.
|
|
20
|
+
function budgetSpent(assemblyLine, visit, chosen, key) {
|
|
21
|
+
return {
|
|
22
|
+
kind: "halt",
|
|
23
|
+
outcome: "iteration_max",
|
|
24
|
+
reason: `AssemblyLine ${assemblyLine.name}: node "${visit.nodeId}" failed — the ${key} retry budget (${chosen.iterationMax}) is spent`,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=walk-budget.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@re-cinq/floor-assembly-lines",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.1",
|
|
4
|
+
"description": "The rules of a floor's assembly line, as pure functions: which edge a walk takes next and when it ends, the checks a line must pass before it is stored, and the parameters a prompt is given. No I/O and no database.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/re-cinq/floor.git",
|
|
9
|
+
"directory": "packages/assembly-lines"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/re-cinq/floor/tree/main/packages/assembly-lines#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/re-cinq/floor/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"floor",
|
|
17
|
+
"assembly-line",
|
|
18
|
+
"pipeline",
|
|
19
|
+
"validation"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"types": "dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./placeholder-pattern": {
|
|
30
|
+
"types": "./dist/placeholder-pattern.d.ts",
|
|
31
|
+
"default": "./dist/placeholder-pattern.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"!dist/**/*.map"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=22"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc -p tsconfig.build.json",
|
|
46
|
+
"dev": "tsc --watch -p tsconfig.build.json",
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"test:watch": "vitest",
|
|
49
|
+
"typecheck": "tsc --noEmit"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@re-cinq/floor-contracts": "0.6.1"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/node": "^22.20.1",
|
|
56
|
+
"@vitest/coverage-v8": "^5.0.0",
|
|
57
|
+
"typescript": "^7.0.2",
|
|
58
|
+
"vitest": "^5.0.0"
|
|
59
|
+
}
|
|
60
|
+
}
|