@llman-sdd/core 0.11.0 → 0.12.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/package.json +6 -3
- package/src/archive/freeze.ts +0 -13
- package/src/archive/frozenCard.ts +1 -10
- package/src/archive/ignoreChain.ts +2 -6
- package/src/archive/sevenzip.ts +0 -1
- package/src/change/closeOutHarness.ts +17 -0
- package/src/change/collect.ts +0 -5
- package/src/change/frontmatter.ts +15 -7
- package/src/change/id.ts +0 -2
- package/src/change/lifecycle.ts +1 -38
- package/src/config/changeId.ts +0 -2
- package/src/config/load.ts +0 -4
- package/src/config/schema.ts +24 -0
- package/src/git/spawnGit.ts +0 -3
- package/src/hints/jitHints.ts +82 -0
- package/src/index.ts +455 -3
- package/src/init/defaultConfig.ts +2 -0
- package/src/init/init.ts +61 -20
- package/src/init/uncommentConfig.ts +909 -0
- package/src/locale.ts +0 -2
- package/src/project/migrateNotes.ts +0 -9
- package/src/project/migrations.ts +70 -0
- package/src/report/graph/analysis.ts +0 -4
- package/src/report/graph/nodes.ts +0 -6
- package/src/report/show.ts +7 -23
- package/src/report/specHelpers.ts +10 -14
- package/src/review/review.ts +0 -12
- package/src/spec/assignSpecIds.ts +0 -3
- package/src/spec/authoring.ts +14 -26
- package/src/spec/ir.ts +14 -5
- package/src/spec/keywords.ts +0 -2
- package/src/spec/machineKey.ts +49 -0
- package/src/spec/migrateNative.ts +4 -18
- package/src/spec/parser.ts +10 -19
- package/src/spec/reqRef.ts +135 -0
- package/src/studio/changeScope.ts +50 -0
- package/src/studio/changeScopeDiff.ts +195 -0
- package/src/studio/contracts.ts +493 -0
- package/src/studio/decisionTree.ts +206 -0
- package/src/studio/deeplink.ts +116 -0
- package/src/studio/demos.ts +167 -0
- package/src/studio/diff.ts +494 -0
- package/src/studio/drift.ts +738 -0
- package/src/studio/extract.ts +360 -0
- package/src/studio/featureSync.ts +592 -0
- package/src/studio/forest.ts +570 -0
- package/src/studio/index.ts +32 -0
- package/src/studio/model.ts +254 -0
- package/src/studio/revisions.ts +476 -0
- package/src/studio/search.ts +337 -0
- package/src/studio/store.ts +811 -0
- package/src/studio/sync.ts +912 -0
- package/src/studio/textfold.ts +72 -0
- package/src/studio/time.ts +107 -0
- package/src/studio/validate.ts +363 -0
- package/src/templates/embedded.ts +4 -6
- package/src/templates/engine.ts +1 -1
- package/src/templates/skills.ts +71 -49
- package/src/theorems/guard.ts +52 -0
- package/src/theorems/index.ts +56 -0
- package/src/theorems/migrate.ts +133 -0
- package/src/theorems/schema.ts +67 -0
- package/src/theorems/scope.ts +68 -0
- package/src/theorems/serialize.ts +60 -0
- package/src/theorems/transactionMachine.ts +247 -0
- package/src/theorems/validate.ts +141 -0
- package/src/util/nextNumber.ts +0 -2
- package/src/validation/changeCheck.ts +0 -24
- package/src/validation/flowAndNesting.ts +434 -0
- package/src/validation/harness.ts +108 -65
- package/src/validation/validate.ts +26 -12
- package/src/validation/xstateTransitions.ts +483 -184
- package/templates/zh-Hans/llmanspec-agents-stub.md +17 -2
- package/templates/zh-Hans/skills/llman-sdd-autopilot/SKILL.md +34 -0
- package/templates/zh-Hans/skills/llman-sdd-change/SKILL.md +44 -0
- package/templates/zh-Hans/skills/llman-sdd-change/playbooks/archive.md +15 -0
- package/templates/zh-Hans/skills/llman-sdd-change/playbooks/draft.md +9 -0
- package/templates/zh-Hans/skills/llman-sdd-review/SKILL.md +71 -0
- package/templates/zh-Hans/skills/llman-sdd-specs/SKILL.md +70 -0
- package/templates/zh-Hans/skills/llman-sdd-specs/playbooks/compact.md +12 -0
- package/templates/zh-Hans/skills/llman-sdd-specs/playbooks/doctor.md +26 -0
- package/templates/zh-Hans/skills/llman-sdd-specs/references/graph-dsl.md +33 -0
- package/templates/zh-Hans/skills/llman-sdd-studio/SKILL.md +33 -0
- package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/collab.md +20 -0
- package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/drift.md +52 -0
- package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/onboard.md +33 -0
- package/templates/zh-Hans/units/skills/cli-footer.md +2 -2
- package/templates/zh-Hans/agents-root-stub.md +0 -9
- package/templates/zh-Hans/skills/llman-sdd-apply-cycle.md +0 -69
- package/templates/zh-Hans/skills/llman-sdd-apply.md +0 -116
- package/templates/zh-Hans/skills/llman-sdd-arch-review.md +0 -58
- package/templates/zh-Hans/skills/llman-sdd-archive.md +0 -78
- package/templates/zh-Hans/skills/llman-sdd-continue.md +0 -34
- package/templates/zh-Hans/skills/llman-sdd-draft.md +0 -53
- package/templates/zh-Hans/skills/llman-sdd-explore.md +0 -63
- package/templates/zh-Hans/skills/llman-sdd-ff.md +0 -34
- package/templates/zh-Hans/skills/llman-sdd-graph.md +0 -56
- package/templates/zh-Hans/skills/llman-sdd-propose.md +0 -106
- package/templates/zh-Hans/skills/llman-sdd-quick.md +0 -40
- package/templates/zh-Hans/skills/llman-sdd-research.md +0 -35
- package/templates/zh-Hans/skills/llman-sdd-specs-compact.md +0 -42
- package/templates/zh-Hans/skills/llman-sdd-validate.md +0 -28
- package/templates/zh-Hans/skills/llman-sdd-verify.md +0 -77
- package/templates/zh-Hans/skills/llman-sdd-wayfinder.md +0 -83
- package/templates/zh-Hans/units/skills/ethics-governance.md +0 -6
- package/templates/zh-Hans/units/skills/git-native-flow.md +0 -56
- package/templates/zh-Hans/units/skills/human-readable-summary.md +0 -9
- package/templates/zh-Hans/units/skills/stage-guard.md +0 -17
- package/templates/zh-Hans/units/skills/structured-protocol.md +0 -19
- package/templates/zh-Hans/units/skills/validation-hints.md +0 -20
- package/templates/zh-Hans/units/skills/version-drift.md +0 -3
- package/templates/zh-Hans/units/spec/feature-contract.md +0 -8
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anchored comments and non-destructive Agent revision proposals (studio-core r131).
|
|
3
|
+
* Agent proposals never mutate human drafts directly; human acceptance applies ops atomically.
|
|
4
|
+
*/
|
|
5
|
+
import { computeMachineDiff, type MachineSemanticDiff } from './diff.ts';
|
|
6
|
+
import {
|
|
7
|
+
cloneMachineDraft,
|
|
8
|
+
cloneMachineIR,
|
|
9
|
+
nextPrefixedUid,
|
|
10
|
+
type MachineDraft,
|
|
11
|
+
type RevisionOp,
|
|
12
|
+
type SemanticAnchor,
|
|
13
|
+
type StudioComment,
|
|
14
|
+
type StudioMachineIR,
|
|
15
|
+
type StudioRevisionProposal,
|
|
16
|
+
type StudioStateDoc,
|
|
17
|
+
} from './model.ts';
|
|
18
|
+
import { validateMachineGraph, type MachineDiagnostic } from './validate.ts';
|
|
19
|
+
|
|
20
|
+
export class StudioRevisionError extends Error {
|
|
21
|
+
constructor(message: string) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = 'StudioRevisionError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AddCommentInput {
|
|
28
|
+
anchor: SemanticAnchor;
|
|
29
|
+
author: 'human' | 'agent';
|
|
30
|
+
body: string;
|
|
31
|
+
createdAt: string;
|
|
32
|
+
replyTo?: string;
|
|
33
|
+
watchTag?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function addStudioComment(doc: StudioStateDoc, input: AddCommentInput): StudioStateDoc {
|
|
37
|
+
const body = input.body.trim();
|
|
38
|
+
if (body === '') throw new StudioRevisionError('comment body must not be empty');
|
|
39
|
+
const id = nextPrefixedUid(
|
|
40
|
+
'c',
|
|
41
|
+
doc.comments.map((c) => c.id),
|
|
42
|
+
);
|
|
43
|
+
const comment: StudioComment = {
|
|
44
|
+
id,
|
|
45
|
+
anchor: input.anchor,
|
|
46
|
+
author: input.author,
|
|
47
|
+
body,
|
|
48
|
+
createdAt: input.createdAt,
|
|
49
|
+
status: 'open',
|
|
50
|
+
...(input.replyTo !== undefined ? { replyTo: input.replyTo } : {}),
|
|
51
|
+
...(input.watchTag !== undefined && input.watchTag.trim() !== ''
|
|
52
|
+
? { watchTag: input.watchTag.trim() }
|
|
53
|
+
: {}),
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
...doc,
|
|
57
|
+
updatedAt: input.createdAt,
|
|
58
|
+
comments: [...doc.comments, comment],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function replyStudioComment(
|
|
63
|
+
doc: StudioStateDoc,
|
|
64
|
+
commentId: string,
|
|
65
|
+
input: { author: 'human' | 'agent'; body: string; createdAt: string; resolve?: boolean },
|
|
66
|
+
): StudioStateDoc {
|
|
67
|
+
const target = doc.comments.find((c) => c.id === commentId);
|
|
68
|
+
if (!target) throw new StudioRevisionError(`comment not found: ${commentId}`);
|
|
69
|
+
const added = addStudioComment(doc, {
|
|
70
|
+
anchor: target.anchor,
|
|
71
|
+
author: input.author,
|
|
72
|
+
body: input.body,
|
|
73
|
+
createdAt: input.createdAt,
|
|
74
|
+
replyTo: target.id,
|
|
75
|
+
});
|
|
76
|
+
if (!input.resolve) return added;
|
|
77
|
+
return {
|
|
78
|
+
...added,
|
|
79
|
+
comments: added.comments.map((c) => (c.id === target.id ? { ...c, status: 'resolved' } : c)),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function resolveStudioComment(
|
|
84
|
+
doc: StudioStateDoc,
|
|
85
|
+
commentId: string,
|
|
86
|
+
updatedAt: string,
|
|
87
|
+
): StudioStateDoc {
|
|
88
|
+
const exists = doc.comments.some((c) => c.id === commentId);
|
|
89
|
+
if (!exists) throw new StudioRevisionError(`comment not found: ${commentId}`);
|
|
90
|
+
return {
|
|
91
|
+
...doc,
|
|
92
|
+
updatedAt,
|
|
93
|
+
revision: (doc.revision ?? 0) + 1,
|
|
94
|
+
comments: doc.comments.map((c) => (c.id === commentId ? { ...c, status: 'resolved' } : c)),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function editStudioComment(
|
|
99
|
+
doc: StudioStateDoc,
|
|
100
|
+
commentId: string,
|
|
101
|
+
input: { body: string; updatedAt: string },
|
|
102
|
+
): StudioStateDoc {
|
|
103
|
+
const body = input.body.trim();
|
|
104
|
+
if (body === '') throw new StudioRevisionError('comment body must not be empty');
|
|
105
|
+
const exists = doc.comments.some((c) => c.id === commentId);
|
|
106
|
+
if (!exists) throw new StudioRevisionError(`comment not found: ${commentId}`);
|
|
107
|
+
return {
|
|
108
|
+
...doc,
|
|
109
|
+
updatedAt: input.updatedAt,
|
|
110
|
+
revision: (doc.revision ?? 0) + 1,
|
|
111
|
+
comments: doc.comments.map((c) => (c.id === commentId ? { ...c, body } : c)),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function commentWatchTag(comment: StudioComment): string {
|
|
116
|
+
const tag = comment.watchTag?.trim();
|
|
117
|
+
return tag !== undefined && tag !== '' ? tag : comment.author;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function setStudioCommentWatchTag(
|
|
121
|
+
doc: StudioStateDoc,
|
|
122
|
+
commentId: string,
|
|
123
|
+
watchTag: string,
|
|
124
|
+
updatedAt: string,
|
|
125
|
+
): StudioStateDoc {
|
|
126
|
+
const exists = doc.comments.some((c) => c.id === commentId);
|
|
127
|
+
if (!exists) throw new StudioRevisionError(`comment not found: ${commentId}`);
|
|
128
|
+
const trimmed = watchTag.trim();
|
|
129
|
+
return {
|
|
130
|
+
...doc,
|
|
131
|
+
updatedAt,
|
|
132
|
+
revision: (doc.revision ?? 0) + 1,
|
|
133
|
+
comments: doc.comments.map((c) => {
|
|
134
|
+
if (c.id !== commentId) return c;
|
|
135
|
+
if (trimmed === '') {
|
|
136
|
+
const { watchTag: _unused, ...rest } = c;
|
|
137
|
+
void _unused;
|
|
138
|
+
return rest;
|
|
139
|
+
}
|
|
140
|
+
return { ...c, watchTag: trimmed };
|
|
141
|
+
}),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function reopenStudioComment(
|
|
146
|
+
doc: StudioStateDoc,
|
|
147
|
+
commentId: string,
|
|
148
|
+
updatedAt: string,
|
|
149
|
+
): StudioStateDoc {
|
|
150
|
+
const exists = doc.comments.some((c) => c.id === commentId);
|
|
151
|
+
if (!exists) throw new StudioRevisionError(`comment not found: ${commentId}`);
|
|
152
|
+
return {
|
|
153
|
+
...doc,
|
|
154
|
+
updatedAt,
|
|
155
|
+
revision: (doc.revision ?? 0) + 1,
|
|
156
|
+
comments: doc.comments.map((c) => (c.id === commentId ? { ...c, status: 'open' } : c)),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function deleteStudioComment(
|
|
161
|
+
doc: StudioStateDoc,
|
|
162
|
+
commentId: string,
|
|
163
|
+
updatedAt: string,
|
|
164
|
+
): StudioStateDoc {
|
|
165
|
+
const exists = doc.comments.some((c) => c.id === commentId);
|
|
166
|
+
if (!exists) throw new StudioRevisionError(`comment not found: ${commentId}`);
|
|
167
|
+
return {
|
|
168
|
+
...doc,
|
|
169
|
+
updatedAt,
|
|
170
|
+
revision: (doc.revision ?? 0) + 1,
|
|
171
|
+
comments: doc.comments.filter((c) => c.id !== commentId),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface ProposeRevisionInput {
|
|
176
|
+
machineKey: string;
|
|
177
|
+
rationale: string;
|
|
178
|
+
ops: RevisionOp[];
|
|
179
|
+
createdAt: string;
|
|
180
|
+
source?: 'agent' | 'drift-trace';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function proposeRevision(doc: StudioStateDoc, input: ProposeRevisionInput): StudioStateDoc {
|
|
184
|
+
if (!doc.machines[input.machineKey]) {
|
|
185
|
+
throw new StudioRevisionError(`machine not found in state doc: ${input.machineKey}`);
|
|
186
|
+
}
|
|
187
|
+
if (input.ops.length === 0) {
|
|
188
|
+
throw new StudioRevisionError('revision proposal must contain at least one op');
|
|
189
|
+
}
|
|
190
|
+
const id = nextPrefixedUid(
|
|
191
|
+
'rev',
|
|
192
|
+
doc.revisions.map((r) => r.id),
|
|
193
|
+
);
|
|
194
|
+
const proposal: StudioRevisionProposal = {
|
|
195
|
+
id,
|
|
196
|
+
machineKey: input.machineKey,
|
|
197
|
+
author: 'agent',
|
|
198
|
+
...(input.source !== undefined ? { source: input.source } : {}),
|
|
199
|
+
rationale: input.rationale.trim(),
|
|
200
|
+
ops: input.ops.map((op) => ({ ...op })),
|
|
201
|
+
createdAt: input.createdAt,
|
|
202
|
+
status: 'pending',
|
|
203
|
+
};
|
|
204
|
+
return {
|
|
205
|
+
...doc,
|
|
206
|
+
updatedAt: input.createdAt,
|
|
207
|
+
revisions: [...doc.revisions, proposal],
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function findStateByName(machine: StudioMachineIR, name: string) {
|
|
212
|
+
const trimmed = name.trim();
|
|
213
|
+
return machine.states.find((s) => s.name === trimmed);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Purely apply structured RevisionOp[] to a cloned StudioMachineIR.
|
|
218
|
+
* `rename_state` updates the state's name in place while preserving its `uid` and all connected edges.
|
|
219
|
+
*/
|
|
220
|
+
export function applyRevisionOpsToMachine(
|
|
221
|
+
machine: StudioMachineIR,
|
|
222
|
+
ops: readonly RevisionOp[],
|
|
223
|
+
): StudioMachineIR {
|
|
224
|
+
const next = cloneMachineIR(machine);
|
|
225
|
+
|
|
226
|
+
for (const op of ops) {
|
|
227
|
+
if (op.op === 'add_state') {
|
|
228
|
+
const name = op.name.trim();
|
|
229
|
+
if (name === '') throw new StudioRevisionError('add_state requires a non-empty name');
|
|
230
|
+
if (!findStateByName(next, name)) {
|
|
231
|
+
const uid = nextPrefixedUid(
|
|
232
|
+
's',
|
|
233
|
+
next.states.map((s) => s.uid),
|
|
234
|
+
);
|
|
235
|
+
next.states.push({ uid, name });
|
|
236
|
+
if (next.initialStateUid === null) next.initialStateUid = uid;
|
|
237
|
+
}
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (op.op === 'rename_state') {
|
|
242
|
+
const target = findStateByName(next, op.from);
|
|
243
|
+
if (!target) {
|
|
244
|
+
throw new StudioRevisionError(`rename_state: state not found: ${op.from}`);
|
|
245
|
+
}
|
|
246
|
+
const newName = op.to.trim();
|
|
247
|
+
if (newName === '') throw new StudioRevisionError('rename_state requires a non-empty `to`');
|
|
248
|
+
target.name = newName;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (op.op === 'remove_state') {
|
|
253
|
+
const target = findStateByName(next, op.stateName);
|
|
254
|
+
if (!target) {
|
|
255
|
+
throw new StudioRevisionError(`remove_state: state not found: ${op.stateName}`);
|
|
256
|
+
}
|
|
257
|
+
next.states = next.states.filter((s) => s.uid !== target.uid);
|
|
258
|
+
next.transitions = next.transitions.filter(
|
|
259
|
+
(t) => t.fromUid !== target.uid && t.toUid !== target.uid,
|
|
260
|
+
);
|
|
261
|
+
if (next.initialStateUid === target.uid) {
|
|
262
|
+
next.initialStateUid = next.states[0]?.uid ?? null;
|
|
263
|
+
}
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (op.op === 'add_transition') {
|
|
268
|
+
const ensureByName = (rawName: string): string => {
|
|
269
|
+
const trimmed = rawName.trim();
|
|
270
|
+
const found = findStateByName(next, trimmed);
|
|
271
|
+
if (found) return found.uid;
|
|
272
|
+
const uid = nextPrefixedUid(
|
|
273
|
+
's',
|
|
274
|
+
next.states.map((s) => s.uid),
|
|
275
|
+
);
|
|
276
|
+
next.states.push({ uid, name: trimmed });
|
|
277
|
+
return uid;
|
|
278
|
+
};
|
|
279
|
+
const fromUid = ensureByName(op.from);
|
|
280
|
+
const toUid = ensureByName(op.to);
|
|
281
|
+
const uid = nextPrefixedUid(
|
|
282
|
+
'e',
|
|
283
|
+
next.transitions.map((t) => t.uid),
|
|
284
|
+
);
|
|
285
|
+
const condition = op.condition?.trim();
|
|
286
|
+
next.transitions.push({
|
|
287
|
+
uid,
|
|
288
|
+
fromUid,
|
|
289
|
+
event: op.event.trim(),
|
|
290
|
+
...(condition !== undefined && condition !== '' ? { condition } : {}),
|
|
291
|
+
toUid,
|
|
292
|
+
});
|
|
293
|
+
if (condition !== undefined && condition !== '' && next.columns.condition === undefined) {
|
|
294
|
+
next.columns.condition = '条件';
|
|
295
|
+
}
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (op.op === 'remove_transition') {
|
|
300
|
+
const fromSt = findStateByName(next, op.from);
|
|
301
|
+
const toSt = findStateByName(next, op.to);
|
|
302
|
+
if (!fromSt || !toSt) {
|
|
303
|
+
throw new StudioRevisionError(
|
|
304
|
+
`remove_transition: endpoint not found (${op.from} -> ${op.to})`,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
const matchCondition = (op.condition ?? '').trim();
|
|
308
|
+
const idx = next.transitions.findIndex(
|
|
309
|
+
(t) =>
|
|
310
|
+
t.fromUid === fromSt.uid &&
|
|
311
|
+
t.toUid === toSt.uid &&
|
|
312
|
+
t.event === op.event.trim() &&
|
|
313
|
+
(t.condition ?? '').trim() === matchCondition,
|
|
314
|
+
);
|
|
315
|
+
if (idx === -1) {
|
|
316
|
+
throw new StudioRevisionError(
|
|
317
|
+
`remove_transition: transition not found (${op.from} --[${op.event}]--> ${op.to})`,
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
next.transitions.splice(idx, 1);
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (op.op === 'update_transition') {
|
|
325
|
+
const fromSt = findStateByName(next, op.match.from);
|
|
326
|
+
const toSt = findStateByName(next, op.match.to);
|
|
327
|
+
if (!fromSt || !toSt) {
|
|
328
|
+
throw new StudioRevisionError(
|
|
329
|
+
`update_transition: match endpoint not found (${op.match.from} -> ${op.match.to})`,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
const matchCondition = (op.match.condition ?? '').trim();
|
|
333
|
+
const edge = next.transitions.find(
|
|
334
|
+
(t) =>
|
|
335
|
+
t.fromUid === fromSt.uid &&
|
|
336
|
+
t.toUid === toSt.uid &&
|
|
337
|
+
t.event === op.match.event.trim() &&
|
|
338
|
+
(t.condition ?? '').trim() === matchCondition,
|
|
339
|
+
);
|
|
340
|
+
if (!edge) {
|
|
341
|
+
throw new StudioRevisionError(
|
|
342
|
+
`update_transition: transition not found (${op.match.from} --[${op.match.event}]--> ${op.match.to})`,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
if (op.next.from !== undefined) {
|
|
346
|
+
const nf = findStateByName(next, op.next.from);
|
|
347
|
+
if (!nf)
|
|
348
|
+
throw new StudioRevisionError(`update_transition: state not found: ${op.next.from}`);
|
|
349
|
+
edge.fromUid = nf.uid;
|
|
350
|
+
}
|
|
351
|
+
if (op.next.to !== undefined) {
|
|
352
|
+
const nt = findStateByName(next, op.next.to);
|
|
353
|
+
if (!nt) throw new StudioRevisionError(`update_transition: state not found: ${op.next.to}`);
|
|
354
|
+
edge.toUid = nt.uid;
|
|
355
|
+
}
|
|
356
|
+
if (op.next.event !== undefined) edge.event = op.next.event.trim();
|
|
357
|
+
if (op.next.condition !== undefined) {
|
|
358
|
+
const ng = op.next.condition.trim();
|
|
359
|
+
if (ng === '') delete edge.condition;
|
|
360
|
+
else {
|
|
361
|
+
edge.condition = ng;
|
|
362
|
+
if (next.columns.condition === undefined) {
|
|
363
|
+
next.columns.condition = '条件';
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return next;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export interface RevisionPreviewResult {
|
|
374
|
+
machine: StudioMachineIR;
|
|
375
|
+
diffFromCurrent: MachineSemanticDiff;
|
|
376
|
+
diffFromBaseline: MachineSemanticDiff;
|
|
377
|
+
diagnostics: MachineDiagnostic[];
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Preview a revision proposal without modifying the draft. */
|
|
381
|
+
export function previewRevisionProposal(
|
|
382
|
+
draft: MachineDraft,
|
|
383
|
+
proposal: StudioRevisionProposal,
|
|
384
|
+
): RevisionPreviewResult {
|
|
385
|
+
const baseSource = draft.current ?? draft.baseline;
|
|
386
|
+
if (!baseSource) {
|
|
387
|
+
throw new StudioRevisionError(`cannot preview revision on empty draft: ${proposal.machineKey}`);
|
|
388
|
+
}
|
|
389
|
+
const previewMachine = applyRevisionOpsToMachine(baseSource, proposal.ops);
|
|
390
|
+
const diffFromCurrent = computeMachineDiff({
|
|
391
|
+
mode: 'modify',
|
|
392
|
+
baseline: cloneMachineIR(baseSource),
|
|
393
|
+
current: previewMachine,
|
|
394
|
+
sourceFile: draft.sourceFile,
|
|
395
|
+
});
|
|
396
|
+
const diffFromBaseline = computeMachineDiff({
|
|
397
|
+
...cloneMachineDraft(draft),
|
|
398
|
+
current: previewMachine,
|
|
399
|
+
});
|
|
400
|
+
const diagnostics = validateMachineGraph(previewMachine);
|
|
401
|
+
return { machine: previewMachine, diffFromCurrent, diffFromBaseline, diagnostics };
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Atomically accept a pending revision proposal into the machine draft and mark it accepted. */
|
|
405
|
+
export function acceptRevisionProposal(
|
|
406
|
+
doc: StudioStateDoc,
|
|
407
|
+
revisionId: string,
|
|
408
|
+
acceptedAt: string,
|
|
409
|
+
): StudioStateDoc {
|
|
410
|
+
const proposal = doc.revisions.find((r) => r.id === revisionId);
|
|
411
|
+
if (!proposal) throw new StudioRevisionError(`revision not found: ${revisionId}`);
|
|
412
|
+
if (proposal.status !== 'pending') {
|
|
413
|
+
throw new StudioRevisionError(`revision ${revisionId} is already ${proposal.status}`);
|
|
414
|
+
}
|
|
415
|
+
const draft = doc.machines[proposal.machineKey];
|
|
416
|
+
if (!draft || !draft.current) {
|
|
417
|
+
throw new StudioRevisionError(`active machine draft not found: ${proposal.machineKey}`);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const nextMachine = applyRevisionOpsToMachine(draft.current, proposal.ops);
|
|
421
|
+
const nextDraft: MachineDraft = {
|
|
422
|
+
...cloneMachineDraft(draft),
|
|
423
|
+
current: nextMachine,
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const existingTl = doc.timelines[proposal.machineKey];
|
|
427
|
+
const nextTimelines = { ...doc.timelines };
|
|
428
|
+
if (existingTl) {
|
|
429
|
+
const entries = [...existingTl.entries];
|
|
430
|
+
const parentEntry = entries[existingTl.headIdx];
|
|
431
|
+
const seq = entries.length;
|
|
432
|
+
entries.push({
|
|
433
|
+
seq,
|
|
434
|
+
id: `#${String(seq)}`,
|
|
435
|
+
...(parentEntry ? { parentId: parentEntry.id } : {}),
|
|
436
|
+
actor: 'agent',
|
|
437
|
+
label: `accept ${proposal.id}: ${proposal.rationale}`,
|
|
438
|
+
createdAt: acceptedAt,
|
|
439
|
+
isBase: false,
|
|
440
|
+
mode: nextDraft.mode,
|
|
441
|
+
snapshot: cloneMachineIR(nextMachine),
|
|
442
|
+
});
|
|
443
|
+
nextTimelines[proposal.machineKey] = {
|
|
444
|
+
entries,
|
|
445
|
+
headIdx: entries.length - 1,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return {
|
|
450
|
+
...doc,
|
|
451
|
+
updatedAt: acceptedAt,
|
|
452
|
+
machines: {
|
|
453
|
+
...doc.machines,
|
|
454
|
+
[proposal.machineKey]: nextDraft,
|
|
455
|
+
},
|
|
456
|
+
timelines: nextTimelines,
|
|
457
|
+
revisions: doc.revisions.map((r) => (r.id === revisionId ? { ...r, status: 'accepted' } : r)),
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export function rejectRevisionProposal(
|
|
462
|
+
doc: StudioStateDoc,
|
|
463
|
+
revisionId: string,
|
|
464
|
+
rejectedAt: string,
|
|
465
|
+
): StudioStateDoc {
|
|
466
|
+
const proposal = doc.revisions.find((r) => r.id === revisionId);
|
|
467
|
+
if (!proposal) throw new StudioRevisionError(`revision not found: ${revisionId}`);
|
|
468
|
+
if (proposal.status !== 'pending') {
|
|
469
|
+
throw new StudioRevisionError(`revision ${revisionId} is already ${proposal.status}`);
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
...doc,
|
|
473
|
+
updatedAt: rejectedAt,
|
|
474
|
+
revisions: doc.revisions.map((r) => (r.id === revisionId ? { ...r, status: 'rejected' } : r)),
|
|
475
|
+
};
|
|
476
|
+
}
|