@llman-sdd/core 0.11.0 → 0.12.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.
Files changed (112) hide show
  1. package/package.json +6 -3
  2. package/src/archive/freeze.ts +0 -13
  3. package/src/archive/frozenCard.ts +1 -10
  4. package/src/archive/ignoreChain.ts +2 -6
  5. package/src/archive/sevenzip.ts +0 -1
  6. package/src/change/closeOutHarness.ts +17 -0
  7. package/src/change/collect.ts +0 -5
  8. package/src/change/frontmatter.ts +15 -7
  9. package/src/change/id.ts +0 -2
  10. package/src/change/lifecycle.ts +1 -38
  11. package/src/config/changeId.ts +0 -2
  12. package/src/config/load.ts +0 -4
  13. package/src/config/schema.ts +24 -0
  14. package/src/git/spawnGit.ts +0 -3
  15. package/src/hints/jitHints.ts +82 -0
  16. package/src/index.ts +455 -3
  17. package/src/init/defaultConfig.ts +2 -0
  18. package/src/init/init.ts +61 -20
  19. package/src/init/uncommentConfig.ts +909 -0
  20. package/src/locale.ts +0 -2
  21. package/src/project/migrateNotes.ts +0 -9
  22. package/src/project/migrations.ts +70 -0
  23. package/src/report/graph/analysis.ts +0 -4
  24. package/src/report/graph/nodes.ts +0 -6
  25. package/src/report/show.ts +7 -23
  26. package/src/report/specHelpers.ts +10 -14
  27. package/src/review/review.ts +0 -12
  28. package/src/spec/assignSpecIds.ts +0 -3
  29. package/src/spec/authoring.ts +14 -26
  30. package/src/spec/ir.ts +14 -5
  31. package/src/spec/keywords.ts +0 -2
  32. package/src/spec/machineKey.ts +49 -0
  33. package/src/spec/migrateNative.ts +4 -18
  34. package/src/spec/parser.ts +10 -19
  35. package/src/spec/reqRef.ts +135 -0
  36. package/src/studio/changeScope.ts +50 -0
  37. package/src/studio/changeScopeDiff.ts +195 -0
  38. package/src/studio/contracts.ts +493 -0
  39. package/src/studio/decisionTree.ts +206 -0
  40. package/src/studio/deeplink.ts +116 -0
  41. package/src/studio/demos.ts +167 -0
  42. package/src/studio/diff.ts +494 -0
  43. package/src/studio/drift.ts +738 -0
  44. package/src/studio/extract.ts +360 -0
  45. package/src/studio/featureSync.ts +592 -0
  46. package/src/studio/forest.ts +570 -0
  47. package/src/studio/index.ts +32 -0
  48. package/src/studio/model.ts +254 -0
  49. package/src/studio/revisions.ts +476 -0
  50. package/src/studio/search.ts +337 -0
  51. package/src/studio/store.ts +811 -0
  52. package/src/studio/sync.ts +912 -0
  53. package/src/studio/textfold.ts +72 -0
  54. package/src/studio/time.ts +107 -0
  55. package/src/studio/validate.ts +363 -0
  56. package/src/templates/embedded.ts +4 -6
  57. package/src/templates/engine.ts +1 -1
  58. package/src/templates/skills.ts +71 -49
  59. package/src/theorems/guard.ts +52 -0
  60. package/src/theorems/index.ts +56 -0
  61. package/src/theorems/migrate.ts +133 -0
  62. package/src/theorems/schema.ts +67 -0
  63. package/src/theorems/scope.ts +68 -0
  64. package/src/theorems/serialize.ts +60 -0
  65. package/src/theorems/transactionMachine.ts +247 -0
  66. package/src/theorems/validate.ts +141 -0
  67. package/src/util/nextNumber.ts +0 -2
  68. package/src/validation/changeCheck.ts +0 -24
  69. package/src/validation/flowAndNesting.ts +434 -0
  70. package/src/validation/harness.ts +108 -65
  71. package/src/validation/validate.ts +26 -12
  72. package/src/validation/xstateTransitions.ts +483 -184
  73. package/templates/zh-Hans/llmanspec-agents-stub.md +17 -2
  74. package/templates/zh-Hans/skills/llman-sdd-autopilot/SKILL.md +34 -0
  75. package/templates/zh-Hans/skills/llman-sdd-change/SKILL.md +44 -0
  76. package/templates/zh-Hans/skills/llman-sdd-change/playbooks/archive.md +15 -0
  77. package/templates/zh-Hans/skills/llman-sdd-change/playbooks/draft.md +9 -0
  78. package/templates/zh-Hans/skills/llman-sdd-review/SKILL.md +71 -0
  79. package/templates/zh-Hans/skills/llman-sdd-specs/SKILL.md +70 -0
  80. package/templates/zh-Hans/skills/llman-sdd-specs/playbooks/compact.md +12 -0
  81. package/templates/zh-Hans/skills/llman-sdd-specs/playbooks/doctor.md +26 -0
  82. package/templates/zh-Hans/skills/llman-sdd-specs/references/graph-dsl.md +33 -0
  83. package/templates/zh-Hans/skills/llman-sdd-studio/SKILL.md +33 -0
  84. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/collab.md +20 -0
  85. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/drift.md +52 -0
  86. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/onboard.md +33 -0
  87. package/templates/zh-Hans/units/skills/cli-footer.md +2 -2
  88. package/templates/zh-Hans/agents-root-stub.md +0 -9
  89. package/templates/zh-Hans/skills/llman-sdd-apply-cycle.md +0 -69
  90. package/templates/zh-Hans/skills/llman-sdd-apply.md +0 -116
  91. package/templates/zh-Hans/skills/llman-sdd-arch-review.md +0 -58
  92. package/templates/zh-Hans/skills/llman-sdd-archive.md +0 -78
  93. package/templates/zh-Hans/skills/llman-sdd-continue.md +0 -34
  94. package/templates/zh-Hans/skills/llman-sdd-draft.md +0 -53
  95. package/templates/zh-Hans/skills/llman-sdd-explore.md +0 -63
  96. package/templates/zh-Hans/skills/llman-sdd-ff.md +0 -34
  97. package/templates/zh-Hans/skills/llman-sdd-graph.md +0 -56
  98. package/templates/zh-Hans/skills/llman-sdd-propose.md +0 -106
  99. package/templates/zh-Hans/skills/llman-sdd-quick.md +0 -40
  100. package/templates/zh-Hans/skills/llman-sdd-research.md +0 -35
  101. package/templates/zh-Hans/skills/llman-sdd-specs-compact.md +0 -42
  102. package/templates/zh-Hans/skills/llman-sdd-validate.md +0 -28
  103. package/templates/zh-Hans/skills/llman-sdd-verify.md +0 -77
  104. package/templates/zh-Hans/skills/llman-sdd-wayfinder.md +0 -83
  105. package/templates/zh-Hans/units/skills/ethics-governance.md +0 -6
  106. package/templates/zh-Hans/units/skills/git-native-flow.md +0 -56
  107. package/templates/zh-Hans/units/skills/human-readable-summary.md +0 -9
  108. package/templates/zh-Hans/units/skills/stage-guard.md +0 -17
  109. package/templates/zh-Hans/units/skills/structured-protocol.md +0 -19
  110. package/templates/zh-Hans/units/skills/validation-hints.md +0 -20
  111. package/templates/zh-Hans/units/skills/version-drift.md +0 -3
  112. package/templates/zh-Hans/units/spec/feature-contract.md +0 -8
@@ -0,0 +1,360 @@
1
+ /**
2
+ * Extract StudioMachineIR drafts from parsed SpecEntry[] (studio-core r128).
3
+ * Reuses xstateTransitions.ts whitelist discovery — zero regex duplication.
4
+ */
5
+ import { specIdOf } from '../spec/ir.ts';
6
+ import type { SpecEntry } from '../validation/validate.ts';
7
+ import {
8
+ extractTransitions,
9
+ FLOW_TAG,
10
+ isJudgmentKind,
11
+ NODES_TAG,
12
+ parseNestRef,
13
+ resolveFlowEdgeColumns,
14
+ resolveGraphScope,
15
+ resolveNodesColumns,
16
+ resolveStateEdgeColumns,
17
+ STATE_TAG,
18
+ } from '../validation/xstateTransitions.ts';
19
+ import {
20
+ cloneMachineIR,
21
+ makeMachineKey,
22
+ type MachineDraft,
23
+ type StudioMachineColumns,
24
+ type StudioMachineIR,
25
+ type StudioStateDoc,
26
+ type StudioStateNode,
27
+ type StudioTransitionEdge,
28
+ } from './model.ts';
29
+
30
+ export interface ExtractedStudioMachine {
31
+ machine: StudioMachineIR;
32
+ draft: MachineDraft;
33
+ }
34
+
35
+ function edgeColumnsFromScope(
36
+ isFlow: boolean,
37
+ cols: NonNullable<ReturnType<typeof resolveStateEdgeColumns>>,
38
+ ): StudioMachineColumns {
39
+ return {
40
+ source: cols.from,
41
+ label: cols.event,
42
+ target: cols.to,
43
+ ...(cols.condition !== undefined ? { condition: cols.condition } : {}),
44
+ ...(cols.action !== undefined ? { action: cols.action } : {}),
45
+ ...(cols.actor !== undefined ? { actor: cols.actor } : {}),
46
+ ...(cols.edgeKind !== undefined ? { edgeKind: cols.edgeKind } : {}),
47
+ ...(cols.meta !== undefined ? { meta: cols.meta } : {}),
48
+ ...(cols.verify !== undefined ? { verify: cols.verify } : {}),
49
+ };
50
+ }
51
+
52
+ /**
53
+ * Extract all @flow/@state transition-table scenarios from SpecEntry[] into StudioMachineIRs.
54
+ */
55
+ export function extractStudioMachines(entries: readonly SpecEntry[]): ExtractedStudioMachine[] {
56
+ const results: ExtractedStudioMachine[] = [];
57
+
58
+ for (const entry of entries) {
59
+ const capability = specIdOf(entry);
60
+ for (const rule of entry.doc.rules) {
61
+ for (const sc of rule.scenarios) {
62
+ const scope = resolveGraphScope(sc);
63
+ if (scope !== FLOW_TAG && scope !== STATE_TAG) continue;
64
+
65
+ const isFlow = scope === FLOW_TAG;
66
+ const cols =
67
+ sc.examples
68
+ ?.map((ex) =>
69
+ isFlow ? resolveFlowEdgeColumns(ex.headers) : resolveStateEdgeColumns(ex.headers),
70
+ )
71
+ .find((c) => c !== null) ?? null;
72
+ if (!cols) continue;
73
+
74
+ const rawTransitions = extractTransitions(sc);
75
+ const stateUidByName = new Map<string, string>();
76
+ const states: StudioStateNode[] = [];
77
+
78
+ const ensureState = (rawName: string): string => {
79
+ const name = rawName.trim();
80
+ const existing = stateUidByName.get(name);
81
+ if (existing !== undefined) return existing;
82
+ const uid = `s${String(states.length + 1)}`;
83
+ stateUidByName.set(name, uid);
84
+ states.push({ uid, name });
85
+ return uid;
86
+ };
87
+
88
+ const transitions: StudioTransitionEdge[] = rawTransitions.map((t, idx) => {
89
+ const fromUid = ensureState(t.from);
90
+ const toUid = ensureState(t.to);
91
+ return {
92
+ uid: `e${String(idx + 1)}`,
93
+ fromUid,
94
+ event: t.event.trim(),
95
+ ...(t.condition?.trim() ? { condition: t.condition.trim() } : {}),
96
+ ...(t.action?.trim() ? { action: t.action.trim() } : {}),
97
+ ...(t.actor?.trim() ? { actor: t.actor.trim() } : {}),
98
+ ...(t.edgeKind?.trim() ? { edgeKind: t.edgeKind.trim() } : {}),
99
+ ...(t.meta?.trim() ? { meta: t.meta.trim() } : {}),
100
+ ...(t.verify?.trim() ? { verify: t.verify.trim() } : {}),
101
+ toUid,
102
+ };
103
+ });
104
+
105
+ const initialStateUid = transitions[0]?.fromUid ?? states[0]?.uid ?? null;
106
+ const machineKey = makeMachineKey(capability, rule.reqId || null, sc.name);
107
+ const machine: StudioMachineIR = {
108
+ machineKey,
109
+ capability,
110
+ reqId: rule.reqId || null,
111
+ ruleTitle: rule.title,
112
+ scenarioName: sc.name,
113
+ ...(isFlow ? { graphKind: 'flow' as const } : { graphKind: 'machine' as const }),
114
+ initialStateUid,
115
+ columns: edgeColumnsFromScope(isFlow, cols),
116
+ states,
117
+ transitions,
118
+ };
119
+
120
+ const draft: MachineDraft = {
121
+ mode: 'modify',
122
+ baseline: cloneMachineIR(machine),
123
+ current: cloneMachineIR(machine),
124
+ sourceFile: entry.fileName,
125
+ };
126
+
127
+ results.push({ machine, draft });
128
+ }
129
+ }
130
+ }
131
+
132
+ applyNodeAttributes(results, entries);
133
+ return results;
134
+ }
135
+
136
+ function applyNodeAttributes(
137
+ results: ExtractedStudioMachine[],
138
+ entries: readonly SpecEntry[],
139
+ ): void {
140
+ const catalog = results.map((r) => r.machine);
141
+ const hostsByRule = new Map<string, ExtractedStudioMachine[]>();
142
+ for (const item of results) {
143
+ const key = `${item.machine.capability}\0${item.machine.reqId ?? ''}`;
144
+ const list = hostsByRule.get(key) ?? [];
145
+ list.push(item);
146
+ hostsByRule.set(key, list);
147
+ }
148
+
149
+ for (const entry of entries) {
150
+ const capability = specIdOf(entry);
151
+ for (const rule of entry.doc.rules) {
152
+ const hosts = hostsByRule.get(`${capability}\0${rule.reqId}`) ?? [];
153
+ if (hosts.length === 0) continue;
154
+ for (const sc of rule.scenarios) {
155
+ if (!sc.tags.includes(NODES_TAG)) continue;
156
+ for (const block of sc.examples ?? []) {
157
+ const cols = resolveNodesColumns(block.headers);
158
+ if (!cols) continue;
159
+ const nodeIdx = block.headers.indexOf(cols.node);
160
+ const kindIdx = cols.nodeKind === undefined ? -1 : block.headers.indexOf(cols.nodeKind);
161
+ const nestIdx = cols.nestKey === undefined ? -1 : block.headers.indexOf(cols.nestKey);
162
+ const sumIdx = cols.summary === undefined ? -1 : block.headers.indexOf(cols.summary);
163
+ const doneIdx = cols.done === undefined ? -1 : block.headers.indexOf(cols.done);
164
+ const actorIdx = cols.actor === undefined ? -1 : block.headers.indexOf(cols.actor);
165
+ for (const row of block.rows) {
166
+ const stateName = (row[nodeIdx] ?? '').trim();
167
+ if (stateName === '') continue;
168
+ const hostsWithState = hosts.filter((h) =>
169
+ h.machine.states.some((s) => s.name === stateName),
170
+ );
171
+ if (hostsWithState.length !== 1) continue;
172
+ const host = hostsWithState[0];
173
+ if (!host) continue;
174
+ const state = host.machine.states.find((s) => s.name === stateName);
175
+ if (!state) continue;
176
+ if (kindIdx !== -1) {
177
+ const kind = (row[kindIdx] ?? '').trim();
178
+ if (kind !== '') state.nodeKind = kind;
179
+ if (isJudgmentKind(kind)) state.nodeKind = '判断';
180
+ }
181
+ if (sumIdx !== -1) {
182
+ const v = (row[sumIdx] ?? '').trim();
183
+ if (v !== '') state.summary = v;
184
+ }
185
+ if (doneIdx !== -1) {
186
+ const v = (row[doneIdx] ?? '').trim();
187
+ if (v !== '') state.done = v;
188
+ }
189
+ if (actorIdx !== -1) {
190
+ const v = (row[actorIdx] ?? '').trim();
191
+ if (v !== '') state.actor = v;
192
+ }
193
+ const ref = parseNestRef(nestIdx === -1 ? '' : (row[nestIdx] ?? ''));
194
+ if (ref === null || ref.reqId === '') continue;
195
+ const targets = catalog.filter((m) => {
196
+ if ((m.reqId ?? '') !== ref.reqId) return false;
197
+ if (ref.scenarioName === null) return true;
198
+ return m.scenarioName === ref.scenarioName;
199
+ });
200
+ if (targets.length !== 1) continue;
201
+ const targetKey = targets[0]?.machineKey;
202
+ if (targetKey) state.nestKey = targetKey;
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+
209
+ for (const item of results) {
210
+ item.draft.baseline = cloneMachineIR(item.machine);
211
+ item.draft.current = cloneMachineIR(item.machine);
212
+ }
213
+ }
214
+
215
+ export interface CreateMachineDraftInput {
216
+ capability: string;
217
+ ruleTitle: string;
218
+ scenarioName: string;
219
+ sourceFile?: string;
220
+ reqId?: string | null;
221
+ graphKind?: StudioMachineIR['graphKind'];
222
+ columns?: StudioMachineColumns;
223
+ states: Array<{
224
+ name: string;
225
+ initial?: boolean;
226
+ nodeKind?: string;
227
+ nestKey?: string;
228
+ summary?: string;
229
+ done?: string;
230
+ actor?: string;
231
+ }>;
232
+ transitions: Array<{ from: string; event: string; condition?: string; to: string }>;
233
+ }
234
+
235
+ /** Build a 0 -> 1 `mode: 'create'` draft with `baseline: null`. */
236
+ export function createMachineDraft(input: CreateMachineDraftInput): MachineDraft {
237
+ const stateUidByName = new Map<string, string>();
238
+ const states: StudioStateNode[] = [];
239
+ let initialStateUid: string | null = null;
240
+
241
+ for (const s of input.states) {
242
+ const name = s.name.trim();
243
+ if (stateUidByName.has(name)) continue;
244
+ const uid = `s${String(states.length + 1)}`;
245
+ stateUidByName.set(name, uid);
246
+ states.push({
247
+ uid,
248
+ name,
249
+ ...(s.nodeKind !== undefined && s.nodeKind !== '' ? { nodeKind: s.nodeKind } : {}),
250
+ ...(s.nestKey !== undefined && s.nestKey !== '' ? { nestKey: s.nestKey } : {}),
251
+ ...(s.summary !== undefined && s.summary !== '' ? { summary: s.summary } : {}),
252
+ ...(s.done !== undefined && s.done !== '' ? { done: s.done } : {}),
253
+ ...(s.actor !== undefined && s.actor !== '' ? { actor: s.actor } : {}),
254
+ });
255
+ if (s.initial && initialStateUid === null) initialStateUid = uid;
256
+ }
257
+
258
+ const ensureState = (rawName: string): string => {
259
+ const name = rawName.trim();
260
+ const existing = stateUidByName.get(name);
261
+ if (existing !== undefined) return existing;
262
+ const uid = `s${String(states.length + 1)}`;
263
+ stateUidByName.set(name, uid);
264
+ states.push({ uid, name });
265
+ return uid;
266
+ };
267
+
268
+ const transitions: StudioTransitionEdge[] = input.transitions.map((t, idx) => {
269
+ const fromUid = ensureState(t.from);
270
+ const toUid = ensureState(t.to);
271
+ const cond = t.condition?.trim();
272
+ return {
273
+ uid: `e${String(idx + 1)}`,
274
+ fromUid,
275
+ event: t.event.trim(),
276
+ ...(cond !== undefined && cond !== '' ? { condition: cond } : {}),
277
+ toUid,
278
+ };
279
+ });
280
+
281
+ if (initialStateUid === null) {
282
+ initialStateUid = transitions[0]?.fromUid ?? states[0]?.uid ?? null;
283
+ }
284
+
285
+ const isFlow = input.graphKind === 'flow';
286
+ const hasCondition = transitions.some(
287
+ (t) => t.condition !== undefined && t.condition.trim() !== '',
288
+ );
289
+ const columns: StudioMachineColumns = input.columns ?? {
290
+ source: isFlow ? '当前步骤' : '当前',
291
+ label: isFlow ? '分支' : '触发',
292
+ target: isFlow ? '下一步' : '目标',
293
+ ...(hasCondition ? { condition: '条件' } : {}),
294
+ };
295
+
296
+ const machine: StudioMachineIR = {
297
+ machineKey: makeMachineKey(input.capability, input.reqId ?? null, input.scenarioName),
298
+ capability: input.capability,
299
+ reqId: input.reqId ?? null,
300
+ ruleTitle: input.ruleTitle,
301
+ scenarioName: input.scenarioName,
302
+ ...(input.graphKind !== undefined ? { graphKind: input.graphKind } : {}),
303
+ initialStateUid,
304
+ columns,
305
+ states,
306
+ transitions,
307
+ };
308
+
309
+ return {
310
+ mode: 'create',
311
+ baseline: null,
312
+ current: machine,
313
+ sourceFile: input.sourceFile ?? `llmanspec/specs/${input.capability}.feature`,
314
+ };
315
+ }
316
+
317
+ /**
318
+ * Leftover workspace-draft may predate newly extracted graphs.
319
+ * Fill missing keys from disk extract and restore `graphKind` when a leftover current omitted it.
320
+ * Never overwrite a draft that already has states.
321
+ */
322
+ export function fillMissingExtractedMachines(
323
+ doc: StudioStateDoc,
324
+ extracted: readonly ExtractedStudioMachine[],
325
+ ): StudioStateDoc {
326
+ let changed = false;
327
+ const machines = { ...doc.machines };
328
+ for (const { draft, machine } of extracted) {
329
+ const key = machine.machineKey;
330
+ const existing = machines[key];
331
+ if (!existing || existing.current === null) {
332
+ machines[key] = draft;
333
+ changed = true;
334
+ continue;
335
+ }
336
+ const fillKind = machine.graphKind;
337
+ if (fillKind !== undefined && existing.current.graphKind === undefined) {
338
+ machines[key] = {
339
+ ...existing,
340
+ current: { ...existing.current, graphKind: fillKind },
341
+ baseline:
342
+ existing.baseline !== null && existing.baseline.graphKind === undefined
343
+ ? { ...existing.baseline, graphKind: fillKind }
344
+ : existing.baseline,
345
+ };
346
+ changed = true;
347
+ }
348
+ }
349
+ return changed ? { ...doc, machines } : doc;
350
+ }
351
+
352
+ /** Mark an existing machine draft as scheduled for removal (`mode: 'remove'`). */
353
+ export function markMachineRemoved(draft: MachineDraft): MachineDraft {
354
+ return {
355
+ mode: 'remove',
356
+ baseline: draft.baseline ? cloneMachineIR(draft.baseline) : null,
357
+ current: null,
358
+ sourceFile: draft.sourceFile,
359
+ };
360
+ }