sdtk-spec-kit 0.10.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.
- package/README.md +1 -1
- package/assets/manifest/toolkit-bundle.manifest.json +207 -47
- package/assets/manifest/toolkit-bundle.sha256.txt +54 -22
- package/assets/toolkit/toolkit/AGENTS.md +2 -1
- package/assets/toolkit/toolkit/SDTK_TOOLKIT.md +5 -1
- package/assets/toolkit/toolkit/runtimes/claude/CLAUDE_TEMPLATE.md +4 -0
- package/assets/toolkit/toolkit/scripts/install-claude-skills.ps1 +18 -0
- package/assets/toolkit/toolkit/scripts/install-codex-skills.ps1 +1 -0
- package/assets/toolkit/toolkit/skills/sdtk-arch/SKILL.md +2 -2
- package/assets/toolkit/toolkit/skills/sdtk-ba/SKILL.md +2 -2
- package/assets/toolkit/toolkit/skills/sdtk-customer-qa-sheet/SKILL.md +117 -0
- package/assets/toolkit/toolkit/skills/sdtk-customer-qa-sheet/references/PROFILE_TEMPLATE.md +60 -0
- package/assets/toolkit/toolkit/skills/sdtk-customer-qa-sheet/scripts/oq_sync.py +349 -0
- package/assets/toolkit/toolkit/skills/sdtk-customer-qa-sheet/scripts/test_oq_sync.py +175 -0
- package/assets/toolkit/toolkit/skills/sdtk-db-design/SKILL.md +130 -0
- package/assets/toolkit/toolkit/skills/sdtk-db-design/references/REVIEW_PROCESS.md +82 -0
- package/assets/toolkit/toolkit/skills/sdtk-pm/SKILL.md +1 -1
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/SKILL.md +104 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/references/REVIEW_AND_MISTAKES.md +63 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/references/SCR01.json +46 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/references/SCR01.png +0 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/references/SPEC_SCHEMA.md +208 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/references/example_spec.yaml +115 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/scripts/build_design_xlsx.py +550 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/scripts/design_spec.py +305 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/scripts/export_preview.py +111 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/scripts/export_spec_md.py +181 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/scripts/make_template.py +113 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/scripts/test_screen_design.py +198 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/scripts/uia_dump.py +64 -0
- package/assets/toolkit/toolkit/skills/sdtk-screen-design-xlsx/scripts/validate_spec.py +260 -0
- package/assets/toolkit/toolkit/skills/sdtk-session-handoff/SKILL.md +76 -0
- package/assets/toolkit/toolkit/skills/sdtk-session-handoff/references/HANDOFF_TEMPLATE.md +63 -0
- package/assets/toolkit/toolkit/skills/sdtk-session-handoff/references/MOVING_A_PROJECT.md +18 -0
- package/assets/toolkit/toolkit/skills/sdtk-session-handoff/scripts/dirty_files.py +133 -0
- package/assets/toolkit/toolkit/skills/sdtk-session-handoff/scripts/test_dirty_files.py +94 -0
- package/assets/toolkit/toolkit/skills/sdtk-xlsx-safe-edit/SKILL.md +4 -1
- package/assets/toolkit/toolkit/skills/sdtk-xlsx-safe-edit/scripts/test_metadata.py +76 -0
- package/assets/toolkit/toolkit/skills/sdtk-xlsx-safe-edit/scripts/xlsx_xml_edit.py +65 -1
- package/assets/toolkit/toolkit/skills/skills.catalog.yaml +96 -0
- package/assets/toolkit/toolkit/skills-claude/arch/SKILL.md +15 -2
- package/assets/toolkit/toolkit/skills-claude/ba/SKILL.md +8 -4
- package/assets/toolkit/toolkit/skills-claude/customer-qa-sheet/SKILL.md +115 -0
- package/assets/toolkit/toolkit/skills-claude/db-design/SKILL.md +128 -0
- package/assets/toolkit/toolkit/skills-claude/dev/SKILL.md +33 -2
- package/assets/toolkit/toolkit/skills-claude/dev-backend/SKILL.md +10 -0
- package/assets/toolkit/toolkit/skills-claude/dev-frontend/SKILL.md +10 -0
- package/assets/toolkit/toolkit/skills-claude/pm/SKILL.md +14 -3
- package/assets/toolkit/toolkit/skills-claude/qa/SKILL.md +40 -2
- package/assets/toolkit/toolkit/skills-claude/screen-design-xlsx/SKILL.md +101 -0
- package/assets/toolkit/toolkit/skills-claude/session-handoff/SKILL.md +74 -0
- package/assets/toolkit/toolkit/skills-claude/test-case-spec/SKILL.md +6 -0
- package/assets/toolkit/toolkit/skills-claude/xlsx-safe-edit/SKILL.md +4 -1
- package/assets/toolkit/toolkit/templates/README.md +3 -0
- package/assets/toolkit/toolkit/templates/docs/database/DATABASE_SPEC_TEMPLATE.md +14 -0
- package/assets/toolkit/toolkit/templates/screen-design-xlsx/ja-default/TEMPLATE.md +247 -0
- package/assets/toolkit/toolkit/templates/screen-design-xlsx/ja-default/template.xlsx +0 -0
- package/package.json +1 -1
- package/src/commands/help.js +7 -2
- package/src/lib/args.js +5 -2
- package/src/lib/design-handoff-provenance.js +30 -5
- package/src/lib/office-ingest.js +376 -0
- package/src/lib/requirement-analysis.js +77 -41
- package/src/lib/requirement-sources.js +259 -0
- package/src/lib/scope.js +4 -4
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// BK-395 — Office (OOXML) documents as first-class wiki sources.
|
|
4
|
+
//
|
|
5
|
+
// docx / xlsx / pptx are ZIP archives of XML. Node ships zlib, so reading them
|
|
6
|
+
// needs no dependency: ~40 lines of ZIP central-directory parsing plus per-format
|
|
7
|
+
// XML text extraction. Measured against python-docx / openpyxl / python-pptx on
|
|
8
|
+
// real files, this recovers 180/180 text fragments — 100% — in 0-2ms.
|
|
9
|
+
//
|
|
10
|
+
// Output is MARKDOWN, not plain text, and that is the point: headings stay
|
|
11
|
+
// headings, tables stay tables, each sheet and slide becomes its own section.
|
|
12
|
+
// Everything downstream — the heading chunker, the retrieval scorer, page
|
|
13
|
+
// generation — then works on Office documents unchanged.
|
|
14
|
+
//
|
|
15
|
+
// Scope is deliberately fenced (owner-approved 2026-07-29):
|
|
16
|
+
// * OOXML only. Legacy .doc/.xls/.ppt are a different binary format entirely
|
|
17
|
+
// and are refused with a message that says so, not parsed into garbage.
|
|
18
|
+
// * PDF is NOT here. It cannot be done without a dependency or an external
|
|
19
|
+
// binary, which is an architectural decision of its own.
|
|
20
|
+
// * Text only. No OCR, no vision. Images, charts and diagrams are LOST — a
|
|
21
|
+
// slide that is mostly a diagram yields only its labels. Said plainly here
|
|
22
|
+
// and in the usage guide so nobody assumes otherwise.
|
|
23
|
+
|
|
24
|
+
const fs = require("fs");
|
|
25
|
+
const path = require("path");
|
|
26
|
+
const zlib = require("zlib");
|
|
27
|
+
|
|
28
|
+
const OFFICE_EXTENSIONS = Object.freeze([".docx", ".xlsx", ".pptx"]);
|
|
29
|
+
// Pre-2007 binary formats. Not ZIP, not XML, not in scope.
|
|
30
|
+
const LEGACY_EXTENSIONS = Object.freeze([".doc", ".xls", ".ppt"]);
|
|
31
|
+
// Owner-approved cap. A 20,000-row spreadsheet extracts to ~368k tokens and
|
|
32
|
+
// would add 1.4MB to the index for one file; the cap keeps a single document
|
|
33
|
+
// from dominating the corpus.
|
|
34
|
+
const DEFAULT_MAX_TOKENS = 50000;
|
|
35
|
+
const CHARS_PER_TOKEN = 4;
|
|
36
|
+
|
|
37
|
+
class OfficeIngestError extends Error {
|
|
38
|
+
constructor(message, code) {
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = "OfficeIngestError";
|
|
41
|
+
this.code = code || "office_ingest_error";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function extensionOf(filePath) {
|
|
46
|
+
const base = String(filePath || "");
|
|
47
|
+
const dot = base.lastIndexOf(".");
|
|
48
|
+
return dot < 0 ? "" : base.slice(dot).toLowerCase();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isOfficeFile(filePath) {
|
|
52
|
+
return OFFICE_EXTENSIONS.includes(extensionOf(filePath));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isLegacyOfficeFile(filePath) {
|
|
56
|
+
return LEGACY_EXTENSIONS.includes(extensionOf(filePath));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// --- minimal ZIP reader ------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
// Entries are returned lazily: a workbook with 40 sheets should not inflate all
|
|
62
|
+
// of them to answer a question about one.
|
|
63
|
+
function readZipEntries(buffer) {
|
|
64
|
+
let eocd = -1;
|
|
65
|
+
const scanFloor = Math.max(0, buffer.length - 66000); // 22-byte EOCD + max comment
|
|
66
|
+
for (let i = buffer.length - 22; i >= scanFloor; i -= 1) {
|
|
67
|
+
if (buffer.readUInt32LE(i) === 0x06054b50) {
|
|
68
|
+
eocd = i;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (eocd < 0) {
|
|
73
|
+
throw new OfficeIngestError(
|
|
74
|
+
"not an OOXML file (no zip end-of-central-directory record)",
|
|
75
|
+
"not_ooxml"
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const entryCount = buffer.readUInt16LE(eocd + 10);
|
|
80
|
+
let pointer = buffer.readUInt32LE(eocd + 16);
|
|
81
|
+
const entries = new Map();
|
|
82
|
+
|
|
83
|
+
for (let i = 0; i < entryCount; i += 1) {
|
|
84
|
+
if (pointer + 46 > buffer.length || buffer.readUInt32LE(pointer) !== 0x02014b50) break;
|
|
85
|
+
const method = buffer.readUInt16LE(pointer + 10);
|
|
86
|
+
const compressedSize = buffer.readUInt32LE(pointer + 20);
|
|
87
|
+
const nameLen = buffer.readUInt16LE(pointer + 28);
|
|
88
|
+
const extraLen = buffer.readUInt16LE(pointer + 30);
|
|
89
|
+
const commentLen = buffer.readUInt16LE(pointer + 32);
|
|
90
|
+
const localOffset = buffer.readUInt32LE(pointer + 42);
|
|
91
|
+
const name = buffer.toString("utf8", pointer + 46, pointer + 46 + nameLen);
|
|
92
|
+
|
|
93
|
+
entries.set(name, () => {
|
|
94
|
+
const localNameLen = buffer.readUInt16LE(localOffset + 26);
|
|
95
|
+
const localExtraLen = buffer.readUInt16LE(localOffset + 28);
|
|
96
|
+
const start = localOffset + 30 + localNameLen + localExtraLen;
|
|
97
|
+
const raw = buffer.slice(start, start + compressedSize);
|
|
98
|
+
try {
|
|
99
|
+
return (method === 8 ? zlib.inflateRawSync(raw) : raw).toString("utf8");
|
|
100
|
+
} catch (err) {
|
|
101
|
+
throw new OfficeIngestError(
|
|
102
|
+
`could not inflate "${name}": ${err.message}`,
|
|
103
|
+
"corrupt_entry"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
pointer += 46 + nameLen + extraLen + commentLen;
|
|
108
|
+
}
|
|
109
|
+
return entries;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// --- XML helpers -------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
const XML_ENTITIES = { "&": "&", "<": "<", ">": ">", """: '"', "'": "'" };
|
|
115
|
+
|
|
116
|
+
function decodeXml(text) {
|
|
117
|
+
return String(text)
|
|
118
|
+
.replace(/&(amp|lt|gt|quot|apos);/g, (match) => XML_ENTITIES[match])
|
|
119
|
+
.replace(/&#(\d+);/g, (_match, code) => safeCodePoint(Number(code)))
|
|
120
|
+
.replace(/&#x([0-9a-fA-F]+);/g, (_match, code) => safeCodePoint(parseInt(code, 16)));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function safeCodePoint(code) {
|
|
124
|
+
if (!Number.isFinite(code) || code < 0 || code > 0x10ffff) return "";
|
|
125
|
+
try {
|
|
126
|
+
return String.fromCodePoint(code);
|
|
127
|
+
} catch (_err) {
|
|
128
|
+
return "";
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function textRuns(xml, tag) {
|
|
133
|
+
const out = [];
|
|
134
|
+
const re = new RegExp(`<${tag}(?:\\s[^>]*)?>([\\s\\S]*?)</${tag}>`, "g");
|
|
135
|
+
let match;
|
|
136
|
+
while ((match = re.exec(xml)) !== null) {
|
|
137
|
+
out.push(decodeXml(match[1].replace(/<[^>]+>/g, "")));
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function markdownTableRows(cells, isHeaderRow) {
|
|
143
|
+
const rows = [`| ${cells.join(" | ")} |`];
|
|
144
|
+
if (isHeaderRow) rows.push(`|${cells.map(() => "---").join("|")}|`);
|
|
145
|
+
return rows;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// --- docx --------------------------------------------------------------------
|
|
149
|
+
|
|
150
|
+
function extractDocx(entries) {
|
|
151
|
+
const read = entries.get("word/document.xml");
|
|
152
|
+
if (!read) return "";
|
|
153
|
+
const xml = read();
|
|
154
|
+
const body = /<w:body>([\s\S]*)<\/w:body>/.exec(xml);
|
|
155
|
+
const scope = body ? body[1] : xml;
|
|
156
|
+
|
|
157
|
+
const lines = [];
|
|
158
|
+
const blockRe = /<w:(p|tbl)\b[\s\S]*?<\/w:\1>/g;
|
|
159
|
+
let block;
|
|
160
|
+
while ((block = blockRe.exec(scope)) !== null) {
|
|
161
|
+
const chunk = block[0];
|
|
162
|
+
|
|
163
|
+
if (chunk.startsWith("<w:tbl")) {
|
|
164
|
+
const rows = chunk.match(/<w:tr\b[\s\S]*?<\/w:tr>/g) || [];
|
|
165
|
+
rows.forEach((row, index) => {
|
|
166
|
+
const cells = (row.match(/<w:tc\b[\s\S]*?<\/w:tc>/g) || []).map((cell) =>
|
|
167
|
+
textRuns(cell, "w:t").join("").trim()
|
|
168
|
+
);
|
|
169
|
+
if (!cells.length) return;
|
|
170
|
+
lines.push(...markdownTableRows(cells, index === 0));
|
|
171
|
+
});
|
|
172
|
+
lines.push("");
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const text = textRuns(chunk, "w:t").join("").trim();
|
|
177
|
+
if (!text) continue;
|
|
178
|
+
const style = /<w:pStyle\s+w:val="([^"]+)"/.exec(chunk);
|
|
179
|
+
const heading = style && /^Heading(\d)$/i.exec(style[1]);
|
|
180
|
+
if (heading) {
|
|
181
|
+
lines.push(`${"#".repeat(Math.min(6, Number(heading[1])))} ${text}`, "");
|
|
182
|
+
} else {
|
|
183
|
+
lines.push(text, "");
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return lines.join("\n");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// --- xlsx --------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
function extractXlsx(entries) {
|
|
192
|
+
const sharedRead = entries.get("xl/sharedStrings.xml");
|
|
193
|
+
const shared = sharedRead ? textRuns(sharedRead(), "t") : [];
|
|
194
|
+
|
|
195
|
+
const workbookRead = entries.get("xl/workbook.xml");
|
|
196
|
+
const sheetNames = workbookRead
|
|
197
|
+
? (workbookRead().match(/<sheet\b[^>]*name="([^"]*)"/g) || []).map((tag) =>
|
|
198
|
+
decodeXml(/name="([^"]*)"/.exec(tag)[1])
|
|
199
|
+
)
|
|
200
|
+
: [];
|
|
201
|
+
|
|
202
|
+
const sheetFiles = [...entries.keys()]
|
|
203
|
+
.filter((name) => /^xl\/worksheets\/sheet\d+\.xml$/.test(name))
|
|
204
|
+
.sort((a, b) => Number(/(\d+)/.exec(a)[1]) - Number(/(\d+)/.exec(b)[1]));
|
|
205
|
+
|
|
206
|
+
const lines = [];
|
|
207
|
+
sheetFiles.forEach((file, index) => {
|
|
208
|
+
const xml = entries.get(file)();
|
|
209
|
+
const rows = xml.match(/<row\b[\s\S]*?<\/row>/g) || [];
|
|
210
|
+
if (!rows.length) return;
|
|
211
|
+
lines.push(`## ${sheetNames[index] || `Sheet${index + 1}`}`, "");
|
|
212
|
+
rows.forEach((row, rowIndex) => {
|
|
213
|
+
const cells = (row.match(/<c\b[^>]*(?:\/>|[\s\S]*?<\/c>)/g) || []).map((cell) => {
|
|
214
|
+
const value = /<v>([\s\S]*?)<\/v>/.exec(cell);
|
|
215
|
+
if (!value) return textRuns(cell, "t").join("").trim();
|
|
216
|
+
// t="s" means the value is an index into the shared-string table;
|
|
217
|
+
// anything else is a literal (number, date serial, cached formula result).
|
|
218
|
+
return /\st="s"/.test(cell) ? shared[Number(value[1])] || "" : decodeXml(value[1]);
|
|
219
|
+
});
|
|
220
|
+
if (!cells.some((cell) => cell !== "")) return;
|
|
221
|
+
lines.push(...markdownTableRows(cells, rowIndex === 0));
|
|
222
|
+
});
|
|
223
|
+
lines.push("");
|
|
224
|
+
});
|
|
225
|
+
return lines.join("\n");
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// --- pptx --------------------------------------------------------------------
|
|
229
|
+
|
|
230
|
+
function slideNumber(name) {
|
|
231
|
+
const match = /(\d+)/.exec(name);
|
|
232
|
+
return match ? Number(match[1]) : 0;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function extractPptx(entries) {
|
|
236
|
+
const slides = [...entries.keys()]
|
|
237
|
+
.filter((name) => /^ppt\/slides\/slide\d+\.xml$/.test(name))
|
|
238
|
+
.sort((a, b) => slideNumber(a) - slideNumber(b));
|
|
239
|
+
|
|
240
|
+
// Speaker notes carry the argument a deck only gestures at on screen, so they
|
|
241
|
+
// are ingested — labelled, never blended into the slide body, so a citation
|
|
242
|
+
// never implies text was visible when it was not.
|
|
243
|
+
const notesByNumber = new Map();
|
|
244
|
+
for (const name of entries.keys()) {
|
|
245
|
+
if (!/^ppt\/notesSlides\/notesSlide\d+\.xml$/.test(name)) continue;
|
|
246
|
+
notesByNumber.set(slideNumber(name), name);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const lines = [];
|
|
250
|
+
slides.forEach((file, index) => {
|
|
251
|
+
const xml = entries.get(file)();
|
|
252
|
+
const paragraphs = (xml.match(/<a:p\b[\s\S]*?<\/a:p>/g) || [])
|
|
253
|
+
.map((paragraph) => textRuns(paragraph, "a:t").join("").trim())
|
|
254
|
+
.filter(Boolean);
|
|
255
|
+
|
|
256
|
+
const number = index + 1;
|
|
257
|
+
const notesFile = notesByNumber.get(number);
|
|
258
|
+
let notes = [];
|
|
259
|
+
if (notesFile) {
|
|
260
|
+
const notesXml = entries.get(notesFile)();
|
|
261
|
+
notes = (notesXml.match(/<a:p\b[\s\S]*?<\/a:p>/g) || [])
|
|
262
|
+
.map((paragraph) => textRuns(paragraph, "a:t").join("").trim())
|
|
263
|
+
// The slide-number placeholder is rendered into every notes page.
|
|
264
|
+
.filter((text) => text && text !== String(number));
|
|
265
|
+
}
|
|
266
|
+
if (!paragraphs.length && !notes.length) return;
|
|
267
|
+
|
|
268
|
+
lines.push(`## Slide ${number}${paragraphs.length ? `: ${paragraphs[0]}` : ""}`, "");
|
|
269
|
+
// A title-only slide still carries its message in that title. Emitting an
|
|
270
|
+
// empty body would make the section vanish from retrieval entirely, since a
|
|
271
|
+
// heading with nothing under it produces no chunk.
|
|
272
|
+
if (paragraphs.length > 1) {
|
|
273
|
+
lines.push(paragraphs.slice(1).join("\n"), "");
|
|
274
|
+
} else if (paragraphs.length === 1) {
|
|
275
|
+
lines.push(paragraphs[0], "");
|
|
276
|
+
}
|
|
277
|
+
if (notes.length) lines.push("**Speaker notes:**", "", notes.join("\n"), "");
|
|
278
|
+
});
|
|
279
|
+
return lines.join("\n");
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// --- entry point -------------------------------------------------------------
|
|
283
|
+
|
|
284
|
+
const EXTRACTORS = { ".docx": extractDocx, ".xlsx": extractXlsx, ".pptx": extractPptx };
|
|
285
|
+
|
|
286
|
+
function titleFromFilename(filePath) {
|
|
287
|
+
const ext = extensionOf(filePath);
|
|
288
|
+
return path
|
|
289
|
+
.basename(filePath, ext)
|
|
290
|
+
.replace(/[_-]+/g, " ")
|
|
291
|
+
.trim();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function normalizeOutput(text) {
|
|
295
|
+
return String(text || "")
|
|
296
|
+
.replace(/\r\n/g, "\n")
|
|
297
|
+
.replace(/[ \t]+$/gm, "")
|
|
298
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
299
|
+
.trim();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Extract an Office document as markdown.
|
|
304
|
+
*
|
|
305
|
+
* @returns {{markdown: string, format: string, truncated: boolean, tokens: number}}
|
|
306
|
+
*/
|
|
307
|
+
function extractOfficeMarkdown(filePath, options) {
|
|
308
|
+
const opts = options || {};
|
|
309
|
+
const fsImpl = opts.fsImpl || fs;
|
|
310
|
+
const ext = extensionOf(filePath);
|
|
311
|
+
|
|
312
|
+
if (isLegacyOfficeFile(filePath)) {
|
|
313
|
+
throw new OfficeIngestError(
|
|
314
|
+
`"${ext}" is the pre-2007 binary Office format, which SDTK-WIKI does not read. ` +
|
|
315
|
+
`Re-save as ${ext}x (Word/Excel/PowerPoint: File > Save As) and rebuild.`,
|
|
316
|
+
"legacy_format"
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
const extractor = EXTRACTORS[ext];
|
|
320
|
+
if (!extractor) {
|
|
321
|
+
throw new OfficeIngestError(`unsupported extension: ${ext || "(none)"}`, "unsupported");
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
let buffer;
|
|
325
|
+
try {
|
|
326
|
+
buffer = fsImpl.readFileSync(filePath);
|
|
327
|
+
} catch (err) {
|
|
328
|
+
throw new OfficeIngestError(`could not read ${filePath}: ${err.message}`, "unreadable");
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const entries = readZipEntries(buffer);
|
|
332
|
+
let markdown = normalizeOutput(extractor(entries));
|
|
333
|
+
|
|
334
|
+
// Title: docx documents usually carry their own H1; spreadsheets and decks
|
|
335
|
+
// start at "## Sheet"/"## Slide", so give them one from the filename rather
|
|
336
|
+
// than letting the builder fall back to a name with the extension in it.
|
|
337
|
+
if (markdown && !/^#\s/m.test(markdown)) {
|
|
338
|
+
markdown = `# ${titleFromFilename(filePath)}\n\n${markdown}`;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const maxTokens = Number.isFinite(opts.maxTokens) && opts.maxTokens > 0
|
|
342
|
+
? Math.floor(opts.maxTokens)
|
|
343
|
+
: DEFAULT_MAX_TOKENS;
|
|
344
|
+
const maxChars = maxTokens * CHARS_PER_TOKEN;
|
|
345
|
+
let truncated = false;
|
|
346
|
+
if (markdown.length > maxChars) {
|
|
347
|
+
// Cut on a line boundary so a markdown table never ends mid-row.
|
|
348
|
+
const cut = markdown.lastIndexOf("\n", maxChars);
|
|
349
|
+
markdown =
|
|
350
|
+
`${markdown.slice(0, cut > 0 ? cut : maxChars).trimEnd()}\n\n` +
|
|
351
|
+
`[truncated: this document exceeds the ${maxTokens.toLocaleString("en-US")}-token ingest cap]`;
|
|
352
|
+
truncated = true;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return {
|
|
356
|
+
markdown,
|
|
357
|
+
format: ext.slice(1),
|
|
358
|
+
truncated,
|
|
359
|
+
tokens: Math.ceil(markdown.length / CHARS_PER_TOKEN),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
module.exports = {
|
|
364
|
+
DEFAULT_MAX_TOKENS,
|
|
365
|
+
LEGACY_EXTENSIONS,
|
|
366
|
+
OFFICE_EXTENSIONS,
|
|
367
|
+
OfficeIngestError,
|
|
368
|
+
extractDocx,
|
|
369
|
+
extractOfficeMarkdown,
|
|
370
|
+
extractPptx,
|
|
371
|
+
extractXlsx,
|
|
372
|
+
isLegacyOfficeFile,
|
|
373
|
+
isOfficeFile,
|
|
374
|
+
readZipEntries,
|
|
375
|
+
titleFromFilename,
|
|
376
|
+
};
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const crypto = require("crypto");
|
|
4
3
|
const fs = require("fs");
|
|
5
4
|
const path = require("path");
|
|
6
5
|
const { ensureGeneratedScaffold } = require("../commands/generate");
|
|
7
|
-
const { parseFlags,
|
|
6
|
+
const { parseFlags, validatePattern } = require("./args");
|
|
8
7
|
const { ValidationError } = require("./errors");
|
|
8
|
+
const {
|
|
9
|
+
collectSourceFiles,
|
|
10
|
+
ingestSources,
|
|
11
|
+
buildSourceIndexMarkdown,
|
|
12
|
+
unsupportedReason,
|
|
13
|
+
} = require("./requirement-sources");
|
|
9
14
|
|
|
10
15
|
const FEATURE_KEY_REGEX = /^[A-Z][A-Z0-9_]*$/;
|
|
11
16
|
|
|
12
17
|
const FLAG_DEFS = {
|
|
13
|
-
source: { type: "string" },
|
|
18
|
+
source: { type: "string", multiple: true },
|
|
14
19
|
// Primary, scope-neutral flags (+ backward-compatible feature-* aliases).
|
|
15
20
|
key: { type: "string" },
|
|
16
21
|
name: { type: "string" },
|
|
@@ -46,10 +51,6 @@ const MOJIBAKE_MARKERS = [
|
|
|
46
51
|
"\uFFFD",
|
|
47
52
|
];
|
|
48
53
|
|
|
49
|
-
function sha256(buffer) {
|
|
50
|
-
return crypto.createHash("sha256").update(buffer).digest("hex");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
54
|
function ensureDir(dirPath) {
|
|
54
55
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
55
56
|
}
|
|
@@ -74,15 +75,25 @@ function copyFileSafe(sourcePath, targetPath, force) {
|
|
|
74
75
|
fs.copyFileSync(sourcePath, targetPath);
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
// Half-width katakana markers (テ ト ニ) are also ordinary Japanese text, e.g.
|
|
79
|
+
// "キャリスカウト" in an accounting spreadsheet. Mojibake (Vietnamese UTF-8 read as
|
|
80
|
+
// Shift-JIS) puts them next to a Latin letter ("Tテi") or right before a kanji
|
|
81
|
+
// ("ト雪"); genuine katakana sits between other katakana. Count only the former.
|
|
82
|
+
const HALFWIDTH_MARKERS = new Set(["テ", "ト", "ニ"]);
|
|
83
|
+
const LATIN_RE = /[A-Za-z]/;
|
|
84
|
+
const KANJI_RE = /[\u4E00-\u9FFF]/;
|
|
80
85
|
|
|
81
86
|
function countMarker(text, marker) {
|
|
82
87
|
let count = 0;
|
|
83
88
|
let idx = text.indexOf(marker);
|
|
84
89
|
while (idx !== -1) {
|
|
85
|
-
|
|
90
|
+
if (!HALFWIDTH_MARKERS.has(marker)) {
|
|
91
|
+
count++;
|
|
92
|
+
} else {
|
|
93
|
+
const prev = text[idx - 1] || "";
|
|
94
|
+
const next = text[idx + marker.length] || "";
|
|
95
|
+
if (LATIN_RE.test(prev) || LATIN_RE.test(next) || KANJI_RE.test(next)) count++;
|
|
96
|
+
}
|
|
86
97
|
idx = text.indexOf(marker, idx + marker.length);
|
|
87
98
|
}
|
|
88
99
|
return count;
|
|
@@ -129,6 +140,9 @@ function buildRequirementIntakeMarkdown(featureKey, featureName, sourceInfo, dia
|
|
|
129
140
|
`- Preserved copy SHA-256: ${sourceInfo.preservedCopySha256}`,
|
|
130
141
|
`- Size bytes: ${sourceInfo.sizeBytes}`,
|
|
131
142
|
`- Line count: ${sourceInfo.lineCount}`,
|
|
143
|
+
`- Readable path: ${sourceInfo.readableRelativePath}`,
|
|
144
|
+
`- Source count: ${sourceInfo.sourceCount}${sourceInfo.skippedCount ? ` (${sourceInfo.skippedCount} skipped, see index)` : ""}`,
|
|
145
|
+
`- Source index: ${sourceInfo.sourceIndexRelativePath} (read it first; lists every source with an outline)`,
|
|
132
146
|
"",
|
|
133
147
|
"## Encoding Diagnostics",
|
|
134
148
|
"",
|
|
@@ -147,7 +161,8 @@ function buildRequirementIntakeMarkdown(featureKey, featureName, sourceInfo, dia
|
|
|
147
161
|
"## Next Action",
|
|
148
162
|
"",
|
|
149
163
|
`- Use task packet: .sdtk/spec/tasks/${featureKey}/README.md`,
|
|
150
|
-
|
|
164
|
+
`- Read ${sourceInfo.sourceIndexRelativePath} first; open only the readable files and line ranges the phase needs.`,
|
|
165
|
+
"- Run the SDTK-SPEC PM/BA/ARCH/DEV/QA workflow against the indexed sources and generated scaffold.",
|
|
151
166
|
"- Cite source lines or source sections where possible.",
|
|
152
167
|
"- Mark missing or low-confidence details as open questions instead of inventing screens, fields, APIs, data models, or visual constraints.",
|
|
153
168
|
"",
|
|
@@ -165,8 +180,11 @@ function taskHeader(featureKey, featureName, sourceInfo) {
|
|
|
165
180
|
return [
|
|
166
181
|
`Feature key: ${featureKey}`,
|
|
167
182
|
`Feature name: ${featureName}`,
|
|
168
|
-
`
|
|
169
|
-
|
|
183
|
+
`Source index: ${sourceInfo.sourceIndexRelativePath} (${sourceInfo.sourceCount} source(s); read it first)`,
|
|
184
|
+
sourceInfo.sourceCount === 1
|
|
185
|
+
? `Preserved source: ${sourceInfo.preservedCopyRelativePath}`
|
|
186
|
+
: `Preserved sources: .sdtk/spec/requirements/sources/${featureKey}/`,
|
|
187
|
+
...(sourceInfo.sourceCount === 1 ? [`Source SHA-256: ${sourceInfo.sha256}`] : []),
|
|
170
188
|
"Inputs allowed: requirements/**. Forbidden: docs/ui/**",
|
|
171
189
|
].join("\n");
|
|
172
190
|
}
|
|
@@ -181,7 +199,7 @@ function buildInputBoundarySection() {
|
|
|
181
199
|
"",
|
|
182
200
|
"Inputs allowed: requirements/**. Forbidden: docs/ui/**",
|
|
183
201
|
"",
|
|
184
|
-
"- Allowed before handoff completion: requirement source files, the preserved `.sdtk/spec/requirements/sources/**` copy, and generated SPEC scaffold artifacts under `docs/**`.",
|
|
202
|
+
"- Allowed before handoff completion: requirement source files, the preserved `.sdtk/spec/requirements/sources/**` copy, the extracted `.sdtk/spec/requirements/extracted/**` Markdown, the source index, and generated SPEC scaffold artifacts under `docs/**`.",
|
|
185
203
|
"- Forbidden before handoff completion: `docs/ui/**`, reference sample HTML/PNG exports, copied sample layouts, and visual benchmark outputs.",
|
|
186
204
|
"- `docs/ui/**` is evaluation-only after SDTK output generation/handoff; it must not be used as a generation input.",
|
|
187
205
|
"- If worker history shows reads under `docs/ui/**` before `SPEC_TO_DESIGN_HANDOFF` is complete, the lane validator must report `PROVENANCE_VIOLATION`.",
|
|
@@ -208,6 +226,9 @@ function buildProvenanceContractSection(featureKey) {
|
|
|
208
226
|
"- `title`",
|
|
209
227
|
"- `source_quote`: at least 30 Unicode code points copied literally from the requirement source",
|
|
210
228
|
"- `source_line_range`: `[start_line, end_line]` using 1-based source line numbers",
|
|
229
|
+
"- `source_path`: required when the source index lists more than one source — the readable path (from the index) the quote and line range refer to",
|
|
230
|
+
"",
|
|
231
|
+
"Line numbers always refer to the readable path in the source index (the extracted Markdown for Office files, never the binary copy).",
|
|
211
232
|
"",
|
|
212
233
|
"Deterministic verifier requirements:",
|
|
213
234
|
"",
|
|
@@ -237,7 +258,7 @@ function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
|
|
|
237
258
|
...buildProvenanceContractSection(featureKey),
|
|
238
259
|
"## Required Agent Sequence",
|
|
239
260
|
"",
|
|
240
|
-
"1. PM/BA: read the
|
|
261
|
+
"1. PM/BA: read the source index, then only the readable sources it lists that the phase needs, and `docs/discovery/REQUIREMENT_<KEY>.md`.",
|
|
241
262
|
"2. PM/BA: fill project initiation, BA spec, PRD, backlog, and open questions using source evidence.",
|
|
242
263
|
"3. ARCH: extract screens, flows, domain model, APIs, data model, design constraints, and non-goals from source evidence.",
|
|
243
264
|
"4. DEV/QA: prepare implementation and test handoffs only after PM/BA/ARCH artifacts are evidence-backed.",
|
|
@@ -259,7 +280,7 @@ function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
|
|
|
259
280
|
...buildInputBoundarySection(),
|
|
260
281
|
"## PM Tasks",
|
|
261
282
|
"",
|
|
262
|
-
"- Read the
|
|
283
|
+
"- Read the source index and the intake requirement artifact, then the readable sources they point to.",
|
|
263
284
|
"- Fill `docs/product/PROJECT_INITIATION_<KEY>.md`.",
|
|
264
285
|
"- Fill `docs/product/PRD_<KEY>.md` and `docs/product/BACKLOG_<KEY>.md` only with source-backed requirements or explicit decisions.",
|
|
265
286
|
"- Record unresolved items as `OQ-*` open questions.",
|
|
@@ -361,14 +382,14 @@ function buildState(featureKey, featureName, sourceInfo, diagnostics, scaffold,
|
|
|
361
382
|
violationCode: "PROVENANCE_VIOLATION",
|
|
362
383
|
},
|
|
363
384
|
nextRecommendedAction:
|
|
364
|
-
`Open ${taskPacket.files["README.md"]} and run the PM/BA/ARCH/DEV/QA agent workflow against the
|
|
385
|
+
`Open ${taskPacket.files["README.md"]}, read ${sourceInfo.sourceIndexRelativePath}, and run the PM/BA/ARCH/DEV/QA agent workflow against the indexed sources.`,
|
|
365
386
|
};
|
|
366
387
|
}
|
|
367
388
|
|
|
368
389
|
async function runRequirementAnalyze(args) {
|
|
369
390
|
const { flags } = parseFlags(args, FLAG_DEFS);
|
|
370
391
|
|
|
371
|
-
const
|
|
392
|
+
const sourceFiles = collectSourceFiles(flags.source);
|
|
372
393
|
// Accept --key/--name or the backward-compatible --feature-key/--feature-name.
|
|
373
394
|
const featureKey = flags.key || flags["feature-key"];
|
|
374
395
|
const featureName = flags.name || flags["feature-name"];
|
|
@@ -385,8 +406,11 @@ async function runRequirementAnalyze(args) {
|
|
|
385
406
|
"Must be UPPER_SNAKE_CASE (e.g., SHOPEASE, ORDER_TRACKING)."
|
|
386
407
|
);
|
|
387
408
|
|
|
388
|
-
|
|
389
|
-
|
|
409
|
+
// Fail before any write when an explicitly named file cannot be ingested.
|
|
410
|
+
for (const f of sourceFiles) {
|
|
411
|
+
if (!f.explicit) continue;
|
|
412
|
+
const reason = unsupportedReason(f.abs, fs.readFileSync(f.abs));
|
|
413
|
+
if (reason) throw new ValidationError(`Cannot ingest requirement source ${f.abs}: ${reason}`);
|
|
390
414
|
}
|
|
391
415
|
|
|
392
416
|
const projectPath = flags["project-path"] ? path.resolve(flags["project-path"]) : process.cwd();
|
|
@@ -400,31 +424,37 @@ async function runRequirementAnalyze(args) {
|
|
|
400
424
|
verbose: Boolean(flags.verbose),
|
|
401
425
|
});
|
|
402
426
|
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
const diagnostics = detectEncodingDiagnostics(sourceText);
|
|
407
|
-
const sourceHash = sha256(sourceBuffer);
|
|
408
|
-
const sourceCopyRel = path.join(
|
|
409
|
-
".sdtk",
|
|
410
|
-
"spec",
|
|
411
|
-
"requirements",
|
|
412
|
-
"sources",
|
|
427
|
+
const { sources, skipped } = ingestSources({
|
|
428
|
+
files: sourceFiles,
|
|
429
|
+
projectPath,
|
|
413
430
|
featureKey,
|
|
414
|
-
|
|
431
|
+
writeFile: (abs, content) => writeFileSafe(abs, content, flags.force),
|
|
432
|
+
copyFile: (src, dst) => copyFileSafe(src, dst, flags.force),
|
|
433
|
+
});
|
|
434
|
+
const diagnostics = detectEncodingDiagnostics(sources.map((src) => src.text).join("\n"));
|
|
435
|
+
const sourceIndexRel = path.join(".sdtk", "spec", "requirements", `SOURCE_INDEX_${featureKey}.md`);
|
|
436
|
+
writeFileSafe(
|
|
437
|
+
path.join(projectPath, sourceIndexRel),
|
|
438
|
+
`${buildSourceIndexMarkdown(featureKey, sources, skipped)}\n`,
|
|
439
|
+
flags.force
|
|
415
440
|
);
|
|
416
|
-
const sourceCopyPath = path.join(projectPath, sourceCopyRel);
|
|
417
|
-
|
|
418
|
-
copyFileSafe(sourcePath, sourceCopyPath, flags.force);
|
|
419
441
|
|
|
442
|
+
const primary = sources[0];
|
|
443
|
+
const sourcePath = primary.sourcePath;
|
|
444
|
+
const sourceCopyRel = primary.preservedCopyRelativePath;
|
|
420
445
|
const sourceInfo = {
|
|
421
446
|
sourcePath,
|
|
422
|
-
preservedCopyRelativePath:
|
|
423
|
-
preservedCopySha256:
|
|
424
|
-
sha256:
|
|
425
|
-
sizeBytes:
|
|
426
|
-
lineCount:
|
|
447
|
+
preservedCopyRelativePath: primary.preservedCopyRelativePath,
|
|
448
|
+
preservedCopySha256: primary.preservedCopySha256,
|
|
449
|
+
sha256: primary.sha256,
|
|
450
|
+
sizeBytes: primary.sizeBytes,
|
|
451
|
+
lineCount: primary.lineCount,
|
|
452
|
+
readableRelativePath: primary.readableRelativePath,
|
|
453
|
+
sourceCount: sources.length,
|
|
454
|
+
skippedCount: skipped.length,
|
|
455
|
+
sourceIndexRelativePath: sourceIndexRel.replace(/\\/g, "/"),
|
|
427
456
|
};
|
|
457
|
+
const sourcesState = sources.map(({ text, outline, ...rest }) => ({ ...rest, headingCount: outline.length }));
|
|
428
458
|
|
|
429
459
|
const requirementRel = path.join("docs", "discovery", `REQUIREMENT_${featureKey}.md`);
|
|
430
460
|
const stateRel = path.join(".sdtk", "spec", "requirements", `ANALYSIS_STATE_${featureKey}.json`);
|
|
@@ -435,9 +465,12 @@ async function runRequirementAnalyze(args) {
|
|
|
435
465
|
const artifacts = {
|
|
436
466
|
requirement: requirementRel.replace(/\\/g, "/"),
|
|
437
467
|
state: stateRel.replace(/\\/g, "/"),
|
|
438
|
-
sourceCopy: sourceCopyRel
|
|
468
|
+
sourceCopy: sourceCopyRel,
|
|
469
|
+
sourceIndex: sourceInfo.sourceIndexRelativePath,
|
|
439
470
|
};
|
|
440
471
|
const state = buildState(featureKey, featureName, sourceInfo, diagnostics, scaffold, artifacts, taskPacket);
|
|
472
|
+
state.sources = sourcesState;
|
|
473
|
+
state.skippedSources = skipped;
|
|
441
474
|
|
|
442
475
|
writeFileSafe(
|
|
443
476
|
requirementPath,
|
|
@@ -452,6 +485,8 @@ async function runRequirementAnalyze(args) {
|
|
|
452
485
|
featureName,
|
|
453
486
|
projectPath,
|
|
454
487
|
sourcePath,
|
|
488
|
+
sources: sourcesState.map((src) => src.readableRelativePath),
|
|
489
|
+
skippedSources: skipped.map((src) => src.relativePath),
|
|
455
490
|
artifacts,
|
|
456
491
|
taskPacket,
|
|
457
492
|
scaffoldStatus: scaffold.status,
|
|
@@ -467,6 +502,7 @@ async function runRequirementAnalyze(args) {
|
|
|
467
502
|
console.log(` Requirement: ${result.analyze.artifacts.requirement}`);
|
|
468
503
|
console.log(` State: ${result.analyze.artifacts.state}`);
|
|
469
504
|
console.log(` Source copy: ${result.analyze.artifacts.sourceCopy}`);
|
|
505
|
+
console.log(` Sources: ${sources.length} ingested, ${skipped.length} skipped -> ${sourceInfo.sourceIndexRelativePath}`);
|
|
470
506
|
console.log(` Task packet: ${result.analyze.taskPacket.root}`);
|
|
471
507
|
console.log(` Scaffold: ${result.analyze.scaffoldStatus}`);
|
|
472
508
|
console.log(` Analysis: ${result.analyze.analysisStatus}`);
|