@llman-sdd/core 0.10.1 → 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 (119) hide show
  1. package/package.json +11 -8
  2. package/src/archive/freeze.ts +41 -15
  3. package/src/archive/frozenCard.ts +57 -9
  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 +53 -94
  11. package/src/config/changeId.ts +0 -2
  12. package/src/config/load.ts +0 -4
  13. package/src/config/schema.ts +24 -13
  14. package/src/config/surface.ts +2 -12
  15. package/src/context/sddState.ts +23 -18
  16. package/src/git/spawnGit.ts +0 -3
  17. package/src/hints/jitHints.ts +82 -0
  18. package/src/index.ts +462 -39
  19. package/src/init/defaultConfig.ts +4 -13
  20. package/src/init/init.ts +108 -36
  21. package/src/init/skillCheck.ts +60 -0
  22. package/src/init/uncommentConfig.ts +909 -0
  23. package/src/locale.ts +0 -2
  24. package/src/project/migrateNotes.ts +0 -9
  25. package/src/project/migrations.ts +151 -9
  26. package/src/report/graph/analysis.ts +0 -4
  27. package/src/report/graph/nodes.ts +0 -6
  28. package/src/report/show.ts +7 -23
  29. package/src/report/specHelpers.ts +10 -14
  30. package/src/review/review.ts +0 -12
  31. package/src/spec/assignSpecIds.ts +0 -3
  32. package/src/spec/authoring.ts +16 -24
  33. package/src/spec/ir.ts +14 -5
  34. package/src/spec/keywords.ts +0 -2
  35. package/src/spec/machineKey.ts +49 -0
  36. package/src/spec/migrateNative.ts +4 -18
  37. package/src/spec/parser.ts +10 -19
  38. package/src/spec/reqRef.ts +135 -0
  39. package/src/studio/changeScope.ts +50 -0
  40. package/src/studio/changeScopeDiff.ts +195 -0
  41. package/src/studio/contracts.ts +493 -0
  42. package/src/studio/decisionTree.ts +206 -0
  43. package/src/studio/deeplink.ts +116 -0
  44. package/src/studio/demos.ts +167 -0
  45. package/src/studio/diff.ts +494 -0
  46. package/src/studio/drift.ts +738 -0
  47. package/src/studio/extract.ts +360 -0
  48. package/src/studio/featureSync.ts +592 -0
  49. package/src/studio/forest.ts +570 -0
  50. package/src/studio/index.ts +32 -0
  51. package/src/studio/model.ts +254 -0
  52. package/src/studio/revisions.ts +476 -0
  53. package/src/studio/search.ts +337 -0
  54. package/src/studio/store.ts +811 -0
  55. package/src/studio/sync.ts +912 -0
  56. package/src/studio/textfold.ts +72 -0
  57. package/src/studio/time.ts +107 -0
  58. package/src/studio/validate.ts +363 -0
  59. package/src/templates/embedded.ts +4 -6
  60. package/src/templates/engine.ts +1 -1
  61. package/src/templates/skills.ts +76 -51
  62. package/src/theorems/guard.ts +52 -0
  63. package/src/theorems/index.ts +56 -0
  64. package/src/theorems/migrate.ts +133 -0
  65. package/src/theorems/schema.ts +67 -0
  66. package/src/theorems/scope.ts +68 -0
  67. package/src/theorems/serialize.ts +60 -0
  68. package/src/theorems/transactionMachine.ts +247 -0
  69. package/src/theorems/validate.ts +141 -0
  70. package/src/util/nextNumber.ts +0 -2
  71. package/src/validation/changeCheck.ts +0 -24
  72. package/src/validation/flowAndNesting.ts +434 -0
  73. package/src/validation/harness.ts +108 -65
  74. package/src/validation/validate.ts +26 -12
  75. package/src/validation/xstateTransitions.ts +483 -184
  76. package/templates/zh-Hans/llmanspec-agents-stub.md +17 -2
  77. package/templates/zh-Hans/skills/llman-sdd-autopilot/SKILL.md +34 -0
  78. package/templates/zh-Hans/skills/llman-sdd-change/SKILL.md +44 -0
  79. package/templates/zh-Hans/skills/llman-sdd-change/playbooks/archive.md +15 -0
  80. package/templates/zh-Hans/skills/llman-sdd-change/playbooks/draft.md +9 -0
  81. package/templates/zh-Hans/skills/llman-sdd-review/SKILL.md +71 -0
  82. package/templates/zh-Hans/skills/llman-sdd-specs/SKILL.md +70 -0
  83. package/templates/zh-Hans/skills/llman-sdd-specs/playbooks/compact.md +12 -0
  84. package/templates/zh-Hans/skills/llman-sdd-specs/playbooks/doctor.md +26 -0
  85. package/templates/zh-Hans/skills/llman-sdd-specs/references/graph-dsl.md +33 -0
  86. package/templates/zh-Hans/skills/llman-sdd-studio/SKILL.md +33 -0
  87. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/collab.md +20 -0
  88. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/drift.md +52 -0
  89. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/onboard.md +33 -0
  90. package/templates/zh-Hans/units/skills/cli-footer.md +2 -2
  91. package/src/context/indexStore.ts +0 -171
  92. package/src/context/retrieve.ts +0 -332
  93. package/src/context/tree.ts +0 -112
  94. package/templates/zh-Hans/agents-root-stub.md +0 -9
  95. package/templates/zh-Hans/skills/llman-sdd-apply-cycle.md +0 -69
  96. package/templates/zh-Hans/skills/llman-sdd-apply.md +0 -117
  97. package/templates/zh-Hans/skills/llman-sdd-arch-review.md +0 -58
  98. package/templates/zh-Hans/skills/llman-sdd-archive.md +0 -79
  99. package/templates/zh-Hans/skills/llman-sdd-continue.md +0 -34
  100. package/templates/zh-Hans/skills/llman-sdd-draft.md +0 -53
  101. package/templates/zh-Hans/skills/llman-sdd-explore.md +0 -64
  102. package/templates/zh-Hans/skills/llman-sdd-ff.md +0 -33
  103. package/templates/zh-Hans/skills/llman-sdd-graph.md +0 -56
  104. package/templates/zh-Hans/skills/llman-sdd-propose.md +0 -107
  105. package/templates/zh-Hans/skills/llman-sdd-quick.md +0 -41
  106. package/templates/zh-Hans/skills/llman-sdd-research.md +0 -35
  107. package/templates/zh-Hans/skills/llman-sdd-specs-compact.md +0 -41
  108. package/templates/zh-Hans/skills/llman-sdd-validate.md +0 -28
  109. package/templates/zh-Hans/skills/llman-sdd-verify.md +0 -77
  110. package/templates/zh-Hans/skills/llman-sdd-wayfinder.md +0 -83
  111. package/templates/zh-Hans/units/skills/ethics-governance.md +0 -6
  112. package/templates/zh-Hans/units/skills/git-native-flow.md +0 -56
  113. package/templates/zh-Hans/units/skills/human-readable-summary.md +0 -9
  114. package/templates/zh-Hans/units/skills/index-fallback.md +0 -1
  115. package/templates/zh-Hans/units/skills/stage-guard.md +0 -17
  116. package/templates/zh-Hans/units/skills/structured-protocol.md +0 -19
  117. package/templates/zh-Hans/units/skills/validation-hints.md +0 -20
  118. package/templates/zh-Hans/units/skills/version-drift.md +0 -3
  119. package/templates/zh-Hans/units/spec/feature-contract.md +0 -8
@@ -0,0 +1,337 @@
1
+ /**
2
+ * Pure Tier 0 local CJK-bigram + BM25 search engine for Studio workspaces (studio-core r158).
3
+ * Zero I/O, zero node:*, zero wall-clock calls, zero external dependencies.
4
+ */
5
+ import type { CapabilityDoc } from '../spec/ir.ts';
6
+ import type { TheoremEntry } from '../theorems/schema.ts';
7
+ import { extractDecisionTables } from './decisionTree.ts';
8
+ import { extractStudioMachines } from './extract.ts';
9
+ import type { StudioStateDoc } from './model.ts';
10
+
11
+ export type StudioSearchTargetKind =
12
+ | 'spec'
13
+ | 'rule'
14
+ | 'state'
15
+ | 'edge'
16
+ | 'decision'
17
+ | 'comment'
18
+ | 'timeline'
19
+ | 'theorem';
20
+
21
+ export interface WorkspaceSearchDocument {
22
+ id: string;
23
+ targetKind: StudioSearchTargetKind;
24
+ targetId: string;
25
+ capId: string;
26
+ machineKey?: string;
27
+ title: string;
28
+ body: string;
29
+ }
30
+
31
+ export interface WorkspaceSearchHit {
32
+ id: string;
33
+ targetKind: StudioSearchTargetKind;
34
+ targetId: string;
35
+ capId: string;
36
+ machineKey?: string;
37
+ title: string;
38
+ snippet: string;
39
+ score: number;
40
+ source: 'local-bm25' | 'rag-serve';
41
+ }
42
+
43
+ const ASCII_TOKEN_RE = /@req:[a-z0-9_-]+|[a-z0-9_@.-]{2,}/giu;
44
+ const HAN_RUN_RE = /\p{Script=Han}+/gu;
45
+
46
+ /**
47
+ * Mixed ASCII + CJK unigram/bigram tokenizer.
48
+ * Keeps `@req:rN` & English identifiers intact while generating overlapping CJK bigrams
49
+ * so Chinese natural phrases recall reliably without an external segmenter.
50
+ */
51
+ export function tokenizeSearchText(text: string): string[] {
52
+ const lower = text.toLowerCase();
53
+ const tokens: string[] = [];
54
+
55
+ for (const m of lower.matchAll(ASCII_TOKEN_RE)) {
56
+ const tok = m[0].replaceAll(/^[.-]+|[.-]+$/gu, '');
57
+ if (tok.length >= 2) tokens.push(tok);
58
+ }
59
+
60
+ for (const m of lower.matchAll(HAN_RUN_RE)) {
61
+ const run = m[0];
62
+ const chars = Array.from(run);
63
+ for (let i = 0; i < chars.length; i += 1) {
64
+ tokens.push(chars[i]!);
65
+ if (i + 1 < chars.length) {
66
+ tokens.push(`${chars[i]!}${chars[i + 1]!}`);
67
+ }
68
+ }
69
+ }
70
+ return tokens;
71
+ }
72
+
73
+ export function extractHighlightSnippet(body: string, query: string, maxLen = 140): string {
74
+ const clean = body.replaceAll(/\s+/gu, ' ').trim();
75
+ if (clean.length <= maxLen) return clean;
76
+ const q = query.trim().toLowerCase();
77
+ const lower = clean.toLowerCase();
78
+ let idx = q.length > 0 ? lower.indexOf(q) : -1;
79
+ if (idx < 0) {
80
+ const terms = tokenizeSearchText(query).sort((a, b) => b.length - a.length);
81
+ for (const term of terms) {
82
+ const pos = lower.indexOf(term);
83
+ if (pos >= 0) {
84
+ idx = pos;
85
+ break;
86
+ }
87
+ }
88
+ }
89
+ if (idx < 0) return `${clean.slice(0, maxLen - 1)}…`;
90
+ const start = Math.max(0, idx - Math.floor(maxLen * 0.35));
91
+ const end = Math.min(clean.length, start + maxLen);
92
+ const prefix = start > 0 ? '…' : '';
93
+ const suffix = end < clean.length ? '…' : '';
94
+ return `${prefix}${clean.slice(start, end)}${suffix}`;
95
+ }
96
+
97
+ export interface SearchableSpecInput {
98
+ capId?: string;
99
+ doc: CapabilityDoc;
100
+ }
101
+
102
+ /** Build workspace-isolated search documents from specs, the active stateDoc and theorems. */
103
+ export function buildWorkspaceSearchDocuments(
104
+ specs: readonly SearchableSpecInput[],
105
+ stateDoc?: StudioStateDoc | null,
106
+ theorems?: readonly TheoremEntry[],
107
+ ): WorkspaceSearchDocument[] {
108
+ const docs: WorkspaceSearchDocument[] = [];
109
+
110
+ for (const item of specs) {
111
+ const capId = item.capId ?? item.doc.featureName;
112
+ const allTags = item.doc.rules.flatMap((r) => r.tags);
113
+ docs.push({
114
+ id: `spec:${capId}`,
115
+ targetKind: 'spec',
116
+ targetId: capId,
117
+ capId,
118
+ title: capId,
119
+ body: `${capId} ${item.doc.header.purpose ?? ''} ${allTags.join(' ')}`,
120
+ });
121
+
122
+ for (const rule of item.doc.rules) {
123
+ const scenarioLines = rule.scenarios.map((sc) =>
124
+ [sc.name, ...sc.steps.map((st) => `${st.kind} ${st.text}`)].join(' '),
125
+ );
126
+ const reqTag = rule.reqId ? `@req:${rule.reqId}` : '';
127
+ docs.push({
128
+ id: `rule:${capId}:${rule.reqId || rule.title}`,
129
+ targetKind: 'rule',
130
+ targetId: rule.reqId || rule.title,
131
+ capId,
132
+ title: `${capId} · ${reqTag ? `${reqTag} ` : ''}${rule.title}`,
133
+ body: [reqTag, rule.title, rule.description, ...scenarioLines].filter(Boolean).join('\n'),
134
+ });
135
+ }
136
+
137
+ const extracted = extractStudioMachines([
138
+ { fileName: `${capId}.feature`, specId: capId, doc: item.doc },
139
+ ]);
140
+ const draftKeys = new Set(Object.keys(stateDoc?.machines ?? {}));
141
+ for (const { machine } of extracted) {
142
+ if (draftKeys.has(machine.machineKey)) continue;
143
+ for (const st of machine.states) {
144
+ docs.push({
145
+ id: `state:${machine.machineKey}:${st.uid}`,
146
+ targetKind: 'state',
147
+ targetId: st.uid,
148
+ capId: machine.capability,
149
+ machineKey: machine.machineKey,
150
+ title: `${machine.capability} · 状态 · ${st.name}`,
151
+ body: `${st.name} ${st.nodeKind === '判断' ? '条件判断' : '状态'} ${st.nestKey ?? ''} ${machine.graphKind ?? 'machine'} ${machine.ruleTitle}`,
152
+ });
153
+ }
154
+ }
155
+
156
+ for (const dt of extractDecisionTables(item.doc)) {
157
+ const cells = [dt.headers.join(' | '), ...dt.rows.map((r) => r.join(' | '))].join('\n');
158
+ docs.push({
159
+ id: `decision:${capId}:${dt.id}`,
160
+ targetKind: 'decision',
161
+ targetId: dt.id,
162
+ capId,
163
+ title: `${capId} · 决策表 · ${dt.title}`,
164
+ body: `${dt.title}\n${cells}`,
165
+ });
166
+ }
167
+ }
168
+
169
+ if (stateDoc) {
170
+ for (const [machineKey, draft] of Object.entries(stateDoc.machines)) {
171
+ const machine = draft.current;
172
+ if (!machine) continue;
173
+ for (const st of machine.states) {
174
+ docs.push({
175
+ id: `state:${machineKey}:${st.uid}`,
176
+ targetKind: 'state',
177
+ targetId: st.uid,
178
+ capId: machine.capability,
179
+ machineKey,
180
+ title: `${machine.capability} · 状态 · ${st.name}`,
181
+ body: `${st.name} ${st.nodeKind === '判断' ? '条件判断' : '状态'} ${st.nestKey ?? ''} ${machine.ruleTitle}`,
182
+ });
183
+ }
184
+ for (const tr of machine.transitions) {
185
+ const fromName = machine.states.find((s) => s.uid === tr.fromUid)?.name ?? tr.fromUid;
186
+ const toName = machine.states.find((s) => s.uid === tr.toUid)?.name ?? tr.toUid;
187
+ docs.push({
188
+ id: `edge:${machineKey}:${tr.uid}`,
189
+ targetKind: 'edge',
190
+ targetId: tr.uid,
191
+ capId: machine.capability,
192
+ machineKey,
193
+ title: `${machine.capability} · 转移 · ${fromName} → ${toName}`,
194
+ body: `${tr.event} ${tr.condition ? `[${tr.condition}]` : ''} ${fromName} ${toName}`,
195
+ });
196
+ }
197
+ const tl = stateDoc.timelines[machineKey];
198
+ for (const entry of tl?.entries ?? []) {
199
+ docs.push({
200
+ id: `timeline:${machineKey}:${entry.id}`,
201
+ targetKind: 'timeline',
202
+ targetId: entry.id,
203
+ capId: machine.capability,
204
+ machineKey,
205
+ title: `${machine.capability} · 时间线 · ${entry.label}`,
206
+ body: `${entry.label} ${entry.actor ?? 'human'} ${entry.id}`,
207
+ });
208
+ }
209
+ }
210
+
211
+ for (const comment of stateDoc.comments ?? []) {
212
+ const anchor = comment.anchor;
213
+ const anchoredDraft =
214
+ 'machineKey' in anchor ? stateDoc.machines[anchor.machineKey] : undefined;
215
+ const capId =
216
+ 'capability' in anchor
217
+ ? anchor.capability
218
+ : 'machineKey' in anchor
219
+ ? (anchoredDraft?.current?.capability ??
220
+ anchoredDraft?.baseline?.capability ??
221
+ anchor.machineKey)
222
+ : 'workspace';
223
+ const machineKey = 'machineKey' in comment.anchor ? comment.anchor.machineKey : undefined;
224
+ const targetId =
225
+ 'stateUid' in comment.anchor
226
+ ? comment.anchor.stateUid
227
+ : 'edgeUid' in comment.anchor
228
+ ? comment.anchor.edgeUid
229
+ : comment.id;
230
+ docs.push({
231
+ id: `comment:${comment.id}`,
232
+ targetKind: 'comment',
233
+ targetId,
234
+ capId,
235
+ ...(machineKey ? { machineKey } : {}),
236
+ title: `评论 (${comment.status === 'open' ? '待处理' : '已解决'}) · ${capId}`,
237
+ body: comment.body,
238
+ });
239
+ }
240
+ }
241
+
242
+ if (theorems !== undefined) {
243
+ for (const theorem of theorems) {
244
+ docs.push({
245
+ id: `theorem:${theorem.id}`,
246
+ targetKind: 'theorem',
247
+ targetId: theorem.id,
248
+ capId: theorem.filePath,
249
+ title: `人类定理 · ${theorem.id} · ${theorem.title}`,
250
+ body: [
251
+ theorem.title,
252
+ theorem.statement,
253
+ theorem.rationale,
254
+ theorem.severity,
255
+ theorem.status,
256
+ ...(theorem.scope ?? []),
257
+ ...theorem.tags,
258
+ theorem.verify?.command ?? '',
259
+ ]
260
+ .filter((part) => part !== '')
261
+ .join('\n'),
262
+ });
263
+ }
264
+ }
265
+
266
+ return docs;
267
+ }
268
+
269
+ /** Deterministic BM25 ranking (k1 = 1.2, b = 0.75) with exact-substring bonus. */
270
+ export function searchWorkspaceBm25(
271
+ docs: readonly WorkspaceSearchDocument[],
272
+ query: string,
273
+ topK = 20,
274
+ ): WorkspaceSearchHit[] {
275
+ const trimmed = query.trim();
276
+ if (!trimmed || docs.length === 0) return [];
277
+ const queryTerms = Array.from(new Set(tokenizeSearchText(trimmed)));
278
+ const lowerQuery = trimmed.toLowerCase();
279
+
280
+ const tokenizedDocs = docs.map((d) => {
281
+ const titleTokens = tokenizeSearchText(d.title);
282
+ const bodyTokens = tokenizeSearchText(d.body);
283
+ const tf = new Map<string, number>();
284
+ for (const t of titleTokens) tf.set(t, (tf.get(t) ?? 0) + 2);
285
+ for (const t of bodyTokens) tf.set(t, (tf.get(t) ?? 0) + 1);
286
+ return { doc: d, tf, len: titleTokens.length * 2 + bodyTokens.length };
287
+ });
288
+
289
+ const N = tokenizedDocs.length;
290
+ const avgLen = Math.max(1, tokenizedDocs.reduce((acc, item) => acc + item.len, 0) / N);
291
+ const df = new Map<string, number>();
292
+ for (const term of queryTerms) {
293
+ let count = 0;
294
+ for (const item of tokenizedDocs) {
295
+ if (item.tf.has(term)) count += 1;
296
+ }
297
+ df.set(term, count);
298
+ }
299
+
300
+ const k1 = 1.2;
301
+ const b = 0.75;
302
+ const scored: WorkspaceSearchHit[] = [];
303
+
304
+ for (const item of tokenizedDocs) {
305
+ let score = 0;
306
+ for (const term of queryTerms) {
307
+ const freq = item.tf.get(term) ?? 0;
308
+ if (freq === 0) continue;
309
+ const docFreq = df.get(term) ?? 0;
310
+ const idf = Math.log(1 + (N - docFreq + 0.5) / (docFreq + 0.5));
311
+ const denom = freq + k1 * (1 - b + b * (item.len / avgLen));
312
+ score += idf * ((freq * (k1 + 1)) / denom);
313
+ }
314
+ const titleLower = item.doc.title.toLowerCase();
315
+ const bodyLower = item.doc.body.toLowerCase();
316
+ if (titleLower.includes(lowerQuery)) score += 4;
317
+ else if (bodyLower.includes(lowerQuery)) score += 2;
318
+
319
+ if (score > 0) {
320
+ scored.push({
321
+ id: item.doc.id,
322
+ targetKind: item.doc.targetKind,
323
+ targetId: item.doc.targetId,
324
+ capId: item.doc.capId,
325
+ ...(item.doc.machineKey ? { machineKey: item.doc.machineKey } : {}),
326
+ title: item.doc.title,
327
+ snippet: extractHighlightSnippet(item.doc.body, trimmed),
328
+ score: Number(score.toFixed(4)),
329
+ source: 'local-bm25',
330
+ });
331
+ }
332
+ }
333
+
334
+ return scored
335
+ .sort((a, bHit) => bHit.score - a.score || a.id.localeCompare(bHit.id))
336
+ .slice(0, Math.max(1, topK));
337
+ }