@dench.com/cli 3.12.9-staging.1239 → 3.12.11-staging.1243
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/fs-daemon-sync-tracker.ts +29 -7
- package/fs-daemon-workspace.ts +46 -26
- package/home-sync-lib.ts +131 -0
- package/home-sync.ts +59 -40
- package/lib/machines.ts +80 -0
- package/lib/references.generated.json +6 -6
- package/lib/state-machine.ts +205 -0
- package/package.json +1 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Explicit state machines.
|
|
3
|
+
*
|
|
4
|
+
* A status/state/phase field that many writers move by hand is an implicit
|
|
5
|
+
* state machine: nothing says which moves are legal, so a late webhook, a
|
|
6
|
+
* retry or a sweeper can resurrect a terminal row (canceled → starting,
|
|
7
|
+
* stopped → active, lost → won). Nov 2026 inventory and history:
|
|
8
|
+
* docs/agent-notes/state-machines.md.
|
|
9
|
+
*
|
|
10
|
+
* `defineMachine` writes the legal moves down once. Writers ask the machine
|
|
11
|
+
* before they write (`decide`); `stateMachineDb.ts` does that inside the
|
|
12
|
+
* mutation that patches the row, so the check and the write are one
|
|
13
|
+
* transaction.
|
|
14
|
+
*
|
|
15
|
+
* Pure module: no Convex imports, so the Next app, the gateway and React
|
|
16
|
+
* reducers can share the same tables.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export type TransitionTable<S extends string> = {
|
|
20
|
+
readonly [K in S]: readonly S[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** What a writer should do with a requested move. */
|
|
24
|
+
export type TransitionDecision<S extends string> =
|
|
25
|
+
/** Legal move to a different state: write it. */
|
|
26
|
+
| { kind: "apply"; from: S | undefined; to: S }
|
|
27
|
+
/** Already in the target state. The caller decides whether the rest of its
|
|
28
|
+
* patch still applies (for a terminal state it usually must not). */
|
|
29
|
+
| { kind: "same"; from: S; to: S }
|
|
30
|
+
/** Not a legal move from the current state. */
|
|
31
|
+
| { kind: "illegal"; from: S | undefined; to: S }
|
|
32
|
+
/** The caller said which states it expected to move from and the row is
|
|
33
|
+
* elsewhere (compare-and-set miss). */
|
|
34
|
+
| { kind: "unexpected"; from: S | undefined; to: S; expected: readonly S[] };
|
|
35
|
+
|
|
36
|
+
export class IllegalTransitionError extends Error {
|
|
37
|
+
readonly machine: string;
|
|
38
|
+
readonly from: string | undefined;
|
|
39
|
+
readonly to: string;
|
|
40
|
+
readonly reason: "illegal" | "unexpected";
|
|
41
|
+
constructor(
|
|
42
|
+
machine: string,
|
|
43
|
+
from: string | undefined,
|
|
44
|
+
to: string,
|
|
45
|
+
reason: "illegal" | "unexpected",
|
|
46
|
+
) {
|
|
47
|
+
super(
|
|
48
|
+
`${machine}: ${reason === "illegal" ? "illegal transition" : "unexpected state"} ${from ?? "(unset)"} → ${to}`,
|
|
49
|
+
);
|
|
50
|
+
this.name = "IllegalTransitionError";
|
|
51
|
+
this.machine = machine;
|
|
52
|
+
this.from = from;
|
|
53
|
+
this.to = to;
|
|
54
|
+
this.reason = reason;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type ExpectOption<S extends string> = { expect?: S | readonly S[] };
|
|
59
|
+
|
|
60
|
+
export interface Machine<S extends string> {
|
|
61
|
+
readonly name: string;
|
|
62
|
+
readonly states: readonly S[];
|
|
63
|
+
/** States with no outgoing moves. */
|
|
64
|
+
readonly terminal: readonly S[];
|
|
65
|
+
/** States a new row may start in. */
|
|
66
|
+
readonly initial: readonly S[];
|
|
67
|
+
readonly transitions: TransitionTable<S>;
|
|
68
|
+
isState(value: unknown): value is S;
|
|
69
|
+
isTerminal(state: S | undefined): boolean;
|
|
70
|
+
/** `from === undefined` is a row with no value yet (insert, or a legacy row
|
|
71
|
+
* written before the field existed): `initial` and `legacyFrom` states are
|
|
72
|
+
* allowed. */
|
|
73
|
+
can(from: S | undefined, to: S): boolean;
|
|
74
|
+
decide(
|
|
75
|
+
from: S | undefined,
|
|
76
|
+
to: S,
|
|
77
|
+
options?: ExpectOption<S>,
|
|
78
|
+
): TransitionDecision<S>;
|
|
79
|
+
/** Throws IllegalTransitionError unless the decision is `apply` or `same`. */
|
|
80
|
+
assert(from: S | undefined, to: S, options?: ExpectOption<S>): void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function defineMachine<const S extends string>(
|
|
84
|
+
name: string,
|
|
85
|
+
transitions: TransitionTable<S>,
|
|
86
|
+
options: {
|
|
87
|
+
/** Defaults to every state. */
|
|
88
|
+
initial?: readonly S[];
|
|
89
|
+
/** Extra states reachable from an unset field (legacy rows). */
|
|
90
|
+
legacyFrom?: readonly S[];
|
|
91
|
+
} = {},
|
|
92
|
+
): Machine<S> {
|
|
93
|
+
const states = Object.keys(transitions) as S[];
|
|
94
|
+
const known = new Set<string>(states);
|
|
95
|
+
for (const from of states) {
|
|
96
|
+
for (const to of transitions[from]) {
|
|
97
|
+
if (!known.has(to)) {
|
|
98
|
+
throw new Error(`${name}: ${from} → ${to} targets an unknown state`);
|
|
99
|
+
}
|
|
100
|
+
if (to === from) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`${name}: ${from} → ${from} is implicit ("same"); do not list it`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const initial = options.initial ?? states;
|
|
108
|
+
for (const s of [...initial, ...(options.legacyFrom ?? [])]) {
|
|
109
|
+
if (!known.has(s)) throw new Error(`${name}: unknown initial state ${s}`);
|
|
110
|
+
}
|
|
111
|
+
const fromUnset = new Set<string>([
|
|
112
|
+
...initial,
|
|
113
|
+
...(options.legacyFrom ?? []),
|
|
114
|
+
]);
|
|
115
|
+
const terminal = states.filter((s) => transitions[s].length === 0);
|
|
116
|
+
const edges = new Map<string, Set<string>>(
|
|
117
|
+
states.map((s) => [s, new Set<string>(transitions[s])]),
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const can = (from: S | undefined, to: S): boolean =>
|
|
121
|
+
from === undefined
|
|
122
|
+
? fromUnset.has(to)
|
|
123
|
+
: (edges.get(from)?.has(to) ?? false);
|
|
124
|
+
|
|
125
|
+
const decide = (
|
|
126
|
+
from: S | undefined,
|
|
127
|
+
to: S,
|
|
128
|
+
opts?: ExpectOption<S>,
|
|
129
|
+
): TransitionDecision<S> => {
|
|
130
|
+
if (from !== undefined && from === to) return { kind: "same", from, to };
|
|
131
|
+
if (opts?.expect !== undefined) {
|
|
132
|
+
const expected: readonly S[] =
|
|
133
|
+
typeof opts.expect === "string"
|
|
134
|
+
? [opts.expect as S]
|
|
135
|
+
: (opts.expect as readonly S[]);
|
|
136
|
+
if (from === undefined || !expected.includes(from)) {
|
|
137
|
+
return { kind: "unexpected", from, to, expected };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return can(from, to)
|
|
141
|
+
? { kind: "apply", from, to }
|
|
142
|
+
: { kind: "illegal", from, to };
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
name,
|
|
147
|
+
states,
|
|
148
|
+
terminal,
|
|
149
|
+
initial,
|
|
150
|
+
transitions,
|
|
151
|
+
isState: (value: unknown): value is S =>
|
|
152
|
+
typeof value === "string" && known.has(value),
|
|
153
|
+
isTerminal: (state) => state !== undefined && terminal.includes(state),
|
|
154
|
+
can,
|
|
155
|
+
decide,
|
|
156
|
+
assert(from, to, opts) {
|
|
157
|
+
const d = decide(from, to, opts);
|
|
158
|
+
if (d.kind === "illegal" || d.kind === "unexpected") {
|
|
159
|
+
throw new IllegalTransitionError(name, from, to, d.kind);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Every state reachable from `from` in one or more moves. */
|
|
166
|
+
export function reachableFrom<S extends string>(
|
|
167
|
+
machine: Machine<S>,
|
|
168
|
+
from: S,
|
|
169
|
+
): Set<S> {
|
|
170
|
+
const seen = new Set<S>();
|
|
171
|
+
const stack: S[] = [...machine.transitions[from]];
|
|
172
|
+
while (stack.length > 0) {
|
|
173
|
+
const s = stack.pop() as S;
|
|
174
|
+
if (seen.has(s)) continue;
|
|
175
|
+
seen.add(s);
|
|
176
|
+
stack.push(...machine.transitions[s]);
|
|
177
|
+
}
|
|
178
|
+
return seen;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* React-side helper: a reducer that only accepts legal moves. Illegal moves
|
|
183
|
+
* keep the current state (and report through `onIllegal`, default
|
|
184
|
+
* console.warn in development) instead of producing an impossible UI mode.
|
|
185
|
+
*/
|
|
186
|
+
export function machineReducer<S extends string>(
|
|
187
|
+
machine: Machine<S>,
|
|
188
|
+
onIllegal: (from: S, to: S) => void = (from, to) => {
|
|
189
|
+
if (
|
|
190
|
+
typeof process !== "undefined" &&
|
|
191
|
+
process.env?.NODE_ENV !== "production"
|
|
192
|
+
) {
|
|
193
|
+
console.warn(
|
|
194
|
+
`${machine.name}: ignored illegal transition ${from} → ${to}`,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
) {
|
|
199
|
+
return (state: S, to: S): S => {
|
|
200
|
+
const d = machine.decide(state, to);
|
|
201
|
+
if (d.kind === "apply") return to;
|
|
202
|
+
if (d.kind !== "same") onIllegal(state, to);
|
|
203
|
+
return state;
|
|
204
|
+
};
|
|
205
|
+
}
|