specdojo 0.3.0 → 0.3.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/CHANGELOG.md +18 -0
- package/dist/catalog-build-references.js +144 -0
- package/dist/catalog-build-render.js +144 -0
- package/dist/catalog-build.js +13 -290
- package/dist/catalog-generate.js +6 -9
- package/dist/catalog-plan-generate.js +2 -2
- package/dist/catalog-plan.js +6 -6
- package/dist/catalog-scaffold.js +4 -4
- package/dist/catalog.js +2 -2
- package/dist/dashboard-exec-records.js +105 -0
- package/dist/dashboard-routines.js +255 -0
- package/dist/dashboard-shared.js +14 -0
- package/dist/dashboard-timeline.js +175 -0
- package/dist/dashboard.js +10 -544
- package/dist/deliverable-trash.js +3 -3
- package/dist/doc-index.js +3 -3
- package/dist/exec-commands.js +557 -0
- package/dist/exec-core.js +457 -0
- package/dist/exec-plans-core.js +693 -0
- package/dist/exec-plans-generation.js +206 -0
- package/dist/exec-plans-storage.js +131 -0
- package/dist/exec-plans.js +3 -1018
- package/dist/exec-repo-integration-git.js +23 -0
- package/dist/exec-repo-integration-sync.js +264 -0
- package/dist/exec-repo-integration.js +4 -281
- package/dist/exec-reporter-output.js +206 -0
- package/dist/exec-reporter.js +2 -206
- package/dist/exec-results.js +22 -15
- package/dist/exec-run-agent-pipeline.js +256 -0
- package/dist/exec-run-agent.js +531 -0
- package/dist/exec-run-in-place.js +541 -0
- package/dist/exec-run-job.js +717 -0
- package/dist/exec-run-lifecycle-commands.js +522 -0
- package/dist/exec-run-mode-shared.js +140 -0
- package/dist/exec-run-register-command.js +244 -0
- package/dist/exec-run-register-core.js +386 -0
- package/dist/exec-run-register-mode.js +206 -0
- package/dist/exec-run-register-resume.js +393 -0
- package/dist/exec-run-register-state.js +322 -0
- package/dist/exec-run-register-worktree.js +509 -0
- package/dist/exec-run-schedule-mode.js +294 -0
- package/dist/exec-run-schedule-prepare.js +353 -0
- package/dist/exec-run-schedule-task.js +572 -0
- package/dist/exec-run-shared.js +399 -0
- package/dist/exec-run.js +11 -6182
- package/dist/exec-shared.js +2 -2
- package/dist/exec-trial-commands.js +558 -0
- package/dist/exec-trial-core.js +371 -0
- package/dist/exec-trial.js +2 -921
- package/dist/exec-worktree-ops-commit.js +707 -0
- package/dist/exec-worktree-ops-lifecycle.js +292 -0
- package/dist/exec-worktree-ops-merge.js +303 -0
- package/dist/exec-worktree-ops.js +3 -1292
- package/dist/exec-worktree.js +32 -4
- package/dist/exec.js +2 -1006
- package/dist/frontmatter-namespace.js +2 -2
- package/dist/grade-apply.js +734 -0
- package/dist/grade-command-apply.js +250 -0
- package/dist/grade-command-inspect.js +138 -0
- package/dist/grade-command-plan.js +101 -0
- package/dist/grade-command-shared.js +68 -0
- package/dist/grade-core.js +307 -0
- package/dist/grade-markdown.js +274 -0
- package/dist/grade-pipeline-state.js +184 -0
- package/dist/grade-plan.js +507 -0
- package/dist/grade-result.js +5 -5
- package/dist/grade-target-discovery.js +265 -0
- package/dist/grade-targets.js +432 -0
- package/dist/grade.js +14 -3188
- package/dist/init-command.js +7 -7
- package/dist/job-definition.js +670 -0
- package/dist/job.js +9 -673
- package/dist/kata-command.js +2 -2
- package/dist/register-command-types.js +1 -0
- package/dist/register-core.js +363 -0
- package/dist/register-create-commands.js +245 -0
- package/dist/register-events.js +4 -4
- package/dist/register-item.js +6 -6
- package/dist/register-maintenance-commands.js +187 -0
- package/dist/register-migration.js +268 -0
- package/dist/register-renumber.js +314 -0
- package/dist/register-transition-commands.js +248 -0
- package/dist/register-update.js +230 -0
- package/dist/register-views.js +256 -0
- package/dist/register.js +61 -2069
- package/dist/review-plan.js +3 -3
- package/dist/routine-definition.js +401 -0
- package/dist/routine.js +6 -402
- package/dist/schedule-approach.js +2 -2
- package/dist/schedule-strategy-generate-document.js +273 -0
- package/dist/schedule-strategy-generate-scope.js +199 -0
- package/dist/schedule-strategy-generate.js +9 -473
- package/dist/schedule.js +4 -4
- package/dist/specdojo-config.js +3 -4
- package/dist/viewpoint-document-kinds-check.js +3 -3
- package/dist/yaml-pages.js +4 -4
- package/dist/yaml.js +28 -0
- package/docs/ja/specdojo/guides/exec-config-guide.md +6 -6
- package/docs/ja/specdojo/guides/orchestrator-operation-guide.md +20 -18
- package/docs/ja/specdojo/guides/routine-operation-guide.md +1 -1
- package/package.json +4 -5
- package/templates/antigravity/README.md +3 -3
- package/templates/antigravity/exec-defaults-snippet.yaml +6 -4
- package/templates/init-exec-defaults.yaml +3 -1
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { extname, isAbsolute, join, relative, resolve } from "node:path";
|
|
4
|
+
import { load } from "./yaml.js";
|
|
5
|
+
import { collectResolvedDeliverables, loadCatalogDocs } from "./catalog-build.js";
|
|
6
|
+
import { isTrashedPath, resolveBasePath } from "./catalog-paths.js";
|
|
7
|
+
import { listFilesRecursive } from "./exec-shared.js";
|
|
8
|
+
import { gradeContentHash } from "./grade-result.js";
|
|
9
|
+
import { GRADE_TARGETS, KATA_DIRS, KATA_REFERENCE_EXTENSIONS, KATA_REFERENCE_FIELDS, gradedViewpoints, isRecord, parseMarkdown, } from "./grade-core.js";
|
|
10
|
+
import { resolveViewpointsDoc } from "./review-plan.js";
|
|
11
|
+
import { assertValidActor, getProjectCatalogPath, getProjectMembersPath, getProjectRolesPath, getProjectSchedulePath, getProjectViewpointsPath, loadConfig, specdojoRootDir, } from "./specdojo-config.js";
|
|
12
|
+
export function resolveProject(projectOption) {
|
|
13
|
+
const { config } = loadConfig();
|
|
14
|
+
if (!config)
|
|
15
|
+
throw new Error("grade commands require .specdojo/specdojo.config.json");
|
|
16
|
+
const id = projectOption?.trim() || config.current_project || Object.keys(config.projects)[0] || "";
|
|
17
|
+
const project = config.projects[id];
|
|
18
|
+
if (!project)
|
|
19
|
+
throw new Error(`Unknown project: ${id}`);
|
|
20
|
+
return { id, project };
|
|
21
|
+
}
|
|
22
|
+
export function loadViewpoints(projectOption) {
|
|
23
|
+
const { project } = resolveProject(projectOption);
|
|
24
|
+
const path = getProjectViewpointsPath(project);
|
|
25
|
+
if (!path)
|
|
26
|
+
throw new Error("viewpoints_path is required for grade");
|
|
27
|
+
return resolveViewpointsDoc(resolve(specdojoRootDir(), path));
|
|
28
|
+
}
|
|
29
|
+
// 対象探索は観点定義が無いプロジェクトでも動かす。その場合は突き合わせ先を持たない扱いにする。
|
|
30
|
+
export function loadViewpointsIfConfigured(projectOption, rootDir) {
|
|
31
|
+
const path = getProjectViewpointsPath(resolveProject(projectOption).project);
|
|
32
|
+
if (!path || !existsSync(resolve(rootDir, path)))
|
|
33
|
+
return undefined;
|
|
34
|
+
return resolveViewpointsDoc(resolve(rootDir, path));
|
|
35
|
+
}
|
|
36
|
+
export function resolveGradeActor(actor, roster) {
|
|
37
|
+
const normalized = actor.trim();
|
|
38
|
+
if (!normalized)
|
|
39
|
+
throw new Error("--by must be a non-empty agent-members.yaml nickname");
|
|
40
|
+
if (!roster)
|
|
41
|
+
throw new Error("members_path is required for grade apply --by");
|
|
42
|
+
assertValidActor(normalized, roster);
|
|
43
|
+
if (roster.members.filter((member) => member.nickname === normalized).length !== 1) {
|
|
44
|
+
throw new Error(`Duplicate actor nickname in members_path: ${normalized}`);
|
|
45
|
+
}
|
|
46
|
+
return normalized;
|
|
47
|
+
}
|
|
48
|
+
export function assertRubric(doc) {
|
|
49
|
+
const rubric = doc.grade_rubric;
|
|
50
|
+
if (!rubric)
|
|
51
|
+
throw new Error("Resolved viewpoints do not define grade_rubric");
|
|
52
|
+
// すべての観点が grade の対象なので、重みのない category の観点は score へ黙って入らなくなる。
|
|
53
|
+
for (const viewpoint of doc.viewpoints ?? []) {
|
|
54
|
+
for (const target of viewpoint.grade_targets ?? GRADE_TARGETS) {
|
|
55
|
+
const weight = rubric.weights[target]?.[viewpoint.category];
|
|
56
|
+
if (typeof weight !== "number" || weight <= 0) {
|
|
57
|
+
throw new Error(`grade_rubric ${rubric.id} has no ${target} weight for category '${viewpoint.category}' used by ${viewpoint.id}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return rubric;
|
|
62
|
+
}
|
|
63
|
+
export function relativePathFromRoot(path, rootDir) {
|
|
64
|
+
return relative(rootDir, path).replace(/\\/g, "/");
|
|
65
|
+
}
|
|
66
|
+
export function repoRelativePath(path) {
|
|
67
|
+
return relativePathFromRoot(path, specdojoRootDir());
|
|
68
|
+
}
|
|
69
|
+
function referenceIds(metadata) {
|
|
70
|
+
return KATA_REFERENCE_FIELDS.flatMap((field) => {
|
|
71
|
+
const value = metadata[field];
|
|
72
|
+
if (typeof value === "string")
|
|
73
|
+
return [value];
|
|
74
|
+
if (Array.isArray(value))
|
|
75
|
+
return value.filter((item) => typeof item === "string");
|
|
76
|
+
return [];
|
|
77
|
+
}).filter((id) => id !== "none" && id !== "undecided");
|
|
78
|
+
}
|
|
79
|
+
function parseKataReference(path, kind) {
|
|
80
|
+
try {
|
|
81
|
+
const extension = extname(path).toLowerCase();
|
|
82
|
+
let metadata;
|
|
83
|
+
if (extension === ".md") {
|
|
84
|
+
metadata = parseMarkdown(readFileSync(path, "utf8"), repoRelativePath(path)).data
|
|
85
|
+
.specdojo;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const parsed = load(readFileSync(path, "utf8"));
|
|
89
|
+
if (!isRecord(parsed))
|
|
90
|
+
return null;
|
|
91
|
+
metadata = isRecord(parsed.specdojo) ? parsed.specdojo : parsed;
|
|
92
|
+
}
|
|
93
|
+
const id = typeof metadata.id === "string" ? metadata.id.trim() : "";
|
|
94
|
+
if (!id)
|
|
95
|
+
return null;
|
|
96
|
+
return { id, path, kind, references: referenceIds(metadata) };
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// Some reusable snippets in Kata directories are Markdown fragments rather than documents.
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// grade の評価対象は利用リポジトリが所有する kata に限る。package 側の kata は SpecDojo が
|
|
104
|
+
// 管理する成果物であり、利用プロジェクトが評価・改善する対象ではない。フォールバックは
|
|
105
|
+
// 個別ファイルの解決にのみ用い、対象の列挙には用いない(解決と列挙を区別する)。
|
|
106
|
+
export function loadKataReferences() {
|
|
107
|
+
const references = new Map();
|
|
108
|
+
const root = specdojoRootDir();
|
|
109
|
+
for (const kind of KATA_DIRS) {
|
|
110
|
+
const paths = listFilesRecursive(join(root, "docs/ja/specdojo", kind))
|
|
111
|
+
.filter((path) => KATA_REFERENCE_EXTENSIONS.has(extname(path).toLowerCase()))
|
|
112
|
+
.flatMap((path) => {
|
|
113
|
+
const reference = parseKataReference(path, kind);
|
|
114
|
+
return reference ? [reference] : [];
|
|
115
|
+
});
|
|
116
|
+
for (const reference of paths) {
|
|
117
|
+
if (!references.has(reference.id))
|
|
118
|
+
references.set(reference.id, reference);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return [...references.values()];
|
|
122
|
+
}
|
|
123
|
+
export function resolveGradeReferencePathsFromCatalog(path, catalog) {
|
|
124
|
+
const target = resolveSafeMarkdownPath(path);
|
|
125
|
+
const targetDocument = parseMarkdown(readFileSync(target, "utf8"), repoRelativePath(target));
|
|
126
|
+
const targetMetadata = targetDocument.data.specdojo;
|
|
127
|
+
const targetId = typeof targetMetadata.id === "string" ? targetMetadata.id.trim() : "";
|
|
128
|
+
if (!targetId)
|
|
129
|
+
return [];
|
|
130
|
+
const byId = new Map(catalog.map((item) => [item.id, item]));
|
|
131
|
+
const selected = new Map();
|
|
132
|
+
const add = (item) => {
|
|
133
|
+
if (item && item.path !== target)
|
|
134
|
+
selected.set(item.id, item);
|
|
135
|
+
};
|
|
136
|
+
for (const id of referenceIds(targetMetadata))
|
|
137
|
+
add(byId.get(id));
|
|
138
|
+
for (const item of catalog)
|
|
139
|
+
if (item.references.includes(targetId))
|
|
140
|
+
add(item);
|
|
141
|
+
// A document and the directly linked rulebook/recipe form the anchor of a Kata set.
|
|
142
|
+
// Follow their declared links, but do not recursively traverse sibling back-links.
|
|
143
|
+
for (let depth = 0; depth < 2; depth += 1) {
|
|
144
|
+
for (const item of [...selected.values()]) {
|
|
145
|
+
if (item.kind !== "rulebooks" && item.kind !== "recipes")
|
|
146
|
+
continue;
|
|
147
|
+
for (const id of item.references)
|
|
148
|
+
add(byId.get(id));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return [...selected.values()].map((item) => item.path).sort();
|
|
152
|
+
}
|
|
153
|
+
export function resolveGradeReferencePaths(path) {
|
|
154
|
+
return resolveGradeReferencePathsFromCatalog(path, loadKataReferences());
|
|
155
|
+
}
|
|
156
|
+
function gradeReferenceExampleFamily(target, path, metadata) {
|
|
157
|
+
if (target === "kata") {
|
|
158
|
+
const rel = repoRelativePath(path);
|
|
159
|
+
const kind = KATA_DIRS.find((candidate) => {
|
|
160
|
+
const marker = `docs/ja/specdojo/${candidate}/`;
|
|
161
|
+
return rel.startsWith(marker) || rel.includes(`/${marker}`);
|
|
162
|
+
});
|
|
163
|
+
if (kind)
|
|
164
|
+
return kind;
|
|
165
|
+
}
|
|
166
|
+
const type = typeof metadata.type === "string" ? metadata.type.trim() : "";
|
|
167
|
+
return type || null;
|
|
168
|
+
}
|
|
169
|
+
export function parseGradeReferenceExample(path, target) {
|
|
170
|
+
try {
|
|
171
|
+
const absolute = resolveSafeMarkdownPath(path);
|
|
172
|
+
const document = parseMarkdown(readFileSync(absolute, "utf8"), repoRelativePath(absolute));
|
|
173
|
+
const metadata = document.data.specdojo;
|
|
174
|
+
return {
|
|
175
|
+
path: absolute,
|
|
176
|
+
family: gradeReferenceExampleFamily(target, absolute, metadata),
|
|
177
|
+
status: typeof metadata.status === "string" ? metadata.status.trim() : "",
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
export function selectGradeReferenceExampleFromCatalog(opts) {
|
|
185
|
+
const target = resolveSafeMarkdownPath(opts.path);
|
|
186
|
+
const targetDocument = parseMarkdown(readFileSync(target, "utf8"), repoRelativePath(target));
|
|
187
|
+
const targetMetadata = targetDocument.data.specdojo;
|
|
188
|
+
const family = gradeReferenceExampleFamily(opts.target, target, targetMetadata);
|
|
189
|
+
if (!family)
|
|
190
|
+
return undefined;
|
|
191
|
+
// 同種別だけを候補にする。種別が違うと構造も目的も異なり、記載水準の基準として
|
|
192
|
+
// 誤りを招く。template はプレースホルダを正解と誤解させ、rulebook は recipe の
|
|
193
|
+
// 基準にならない。該当がなければリファレンスなしで評価する。
|
|
194
|
+
const candidates = opts.candidates
|
|
195
|
+
.filter((candidate) => candidate.path !== target && candidate.family === family && candidate.status === "ready")
|
|
196
|
+
.map((candidate) => candidate.path)
|
|
197
|
+
.sort();
|
|
198
|
+
if (candidates.length === 0)
|
|
199
|
+
return undefined;
|
|
200
|
+
const random = opts.random ?? Math.random;
|
|
201
|
+
const sample = random();
|
|
202
|
+
const normalizedSample = Number.isFinite(sample) ? Math.max(0, sample) : 0;
|
|
203
|
+
const index = Math.min(Math.floor(normalizedSample * candidates.length), candidates.length - 1);
|
|
204
|
+
return candidates[index];
|
|
205
|
+
}
|
|
206
|
+
export function selectGradeReferenceExample(opts) {
|
|
207
|
+
return selectGradeReferenceExampleFromCatalog({
|
|
208
|
+
...opts,
|
|
209
|
+
candidates: opts.candidates.flatMap((candidate) => {
|
|
210
|
+
const parsed = parseGradeReferenceExample(candidate, opts.target);
|
|
211
|
+
return parsed ? [parsed] : [];
|
|
212
|
+
}),
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
export function resolveSafeMarkdownPath(input, root = specdojoRootDir()) {
|
|
216
|
+
const absolute = resolve(root, input);
|
|
217
|
+
const rel = relativePathFromRoot(absolute, root);
|
|
218
|
+
if (rel.startsWith("../") || isAbsolute(rel))
|
|
219
|
+
throw new Error(`Path is outside repository: ${input}`);
|
|
220
|
+
if (extname(absolute).toLowerCase() !== ".md") {
|
|
221
|
+
throw new Error(`${input}: grade currently writes inline findings only for Markdown`);
|
|
222
|
+
}
|
|
223
|
+
if (!existsSync(absolute))
|
|
224
|
+
throw new Error(`Document not found: ${input}`);
|
|
225
|
+
return absolute;
|
|
226
|
+
}
|
|
227
|
+
export function resolveSafeRepositoryPath(input, option) {
|
|
228
|
+
const root = specdojoRootDir();
|
|
229
|
+
const absolute = resolve(root, input);
|
|
230
|
+
const rel = relative(root, absolute);
|
|
231
|
+
if (rel.startsWith("../") || isAbsolute(rel)) {
|
|
232
|
+
throw new Error(`${option} must be inside the repository`);
|
|
233
|
+
}
|
|
234
|
+
return absolute;
|
|
235
|
+
}
|
|
236
|
+
export function isGeneratedGradeTarget(path, rootDir = specdojoRootDir()) {
|
|
237
|
+
return relativePathFromRoot(path, rootDir).split("/").includes("generated");
|
|
238
|
+
}
|
|
239
|
+
export function isTrashedGradeTarget(path, rootDir = specdojoRootDir()) {
|
|
240
|
+
return isTrashedPath(relativePathFromRoot(path, rootDir));
|
|
241
|
+
}
|
|
242
|
+
function criterionId(index) {
|
|
243
|
+
return `DC-${String(index + 1).padStart(3, "0")}`;
|
|
244
|
+
}
|
|
245
|
+
// カタログの Markdown 成果物を絶対パスで引ける形にする。grade の対象探索と
|
|
246
|
+
// done_criteria の解決が同じ集合を見るよう、両者はこの関数を共有する。
|
|
247
|
+
export function loadDeliverableCatalog(projectOption, root = specdojoRootDir()) {
|
|
248
|
+
const { project } = resolveProject(projectOption);
|
|
249
|
+
const catalog = getProjectCatalogPath(project);
|
|
250
|
+
if (!catalog)
|
|
251
|
+
throw new Error("catalog_path is required for deliverable grading");
|
|
252
|
+
const entries = new Map();
|
|
253
|
+
for (const loaded of loadCatalogDocs(resolve(root, catalog))) {
|
|
254
|
+
const resolved = [];
|
|
255
|
+
collectResolvedDeliverables(loaded.doc.groups, resolveBasePath("", loaded.doc.base_path), resolved);
|
|
256
|
+
for (const item of resolved) {
|
|
257
|
+
if (item.item.kind === "generated" || !item.item.path)
|
|
258
|
+
continue;
|
|
259
|
+
if (!item.resolvedPath.endsWith(".md"))
|
|
260
|
+
continue;
|
|
261
|
+
if (isTrashedPath(item.resolvedPath))
|
|
262
|
+
continue;
|
|
263
|
+
const path = resolve(root, item.resolvedPath);
|
|
264
|
+
if (!existsSync(path) || entries.has(path))
|
|
265
|
+
continue;
|
|
266
|
+
entries.set(path, {
|
|
267
|
+
path,
|
|
268
|
+
localId: item.item.local_id,
|
|
269
|
+
dependsOn: item.item.depends_on,
|
|
270
|
+
entryHash: gradeContentHash(stableJson(item.item)),
|
|
271
|
+
doneCriteria: (item.item.done_criteria ?? []).map((criterion, index) => ({
|
|
272
|
+
id: criterionId(index),
|
|
273
|
+
text: criterion.text,
|
|
274
|
+
roles: [...criterion.roles],
|
|
275
|
+
viewpoint: criterion.viewpoint,
|
|
276
|
+
})),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return entries;
|
|
281
|
+
}
|
|
282
|
+
// 成果物ごとの done_criteria 定義を、submission が使うリポジトリ相対パスで引ける形にする。
|
|
283
|
+
// カタログに無い文書(--path で明示した非カタログ文書)は含めず、plan と apply は
|
|
284
|
+
// 定義が無い文書を「完了条件なし」として扱う。
|
|
285
|
+
export function resolveDeliverableDoneCriteria(paths, projectOption) {
|
|
286
|
+
const catalog = loadDeliverableCatalog(projectOption);
|
|
287
|
+
const result = new Map();
|
|
288
|
+
for (const path of paths) {
|
|
289
|
+
const entry = catalog.get(resolveSafeMarkdownPath(path));
|
|
290
|
+
if (entry && entry.doneCriteria.length > 0) {
|
|
291
|
+
result.set(repoRelativePath(entry.path), entry.doneCriteria);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return result;
|
|
295
|
+
}
|
|
296
|
+
// キー順に依存しない直列化。カタログ項目の hash が YAML 上のキー順の入れ替えで変わらないようにする。
|
|
297
|
+
function stableJson(value) {
|
|
298
|
+
if (Array.isArray(value))
|
|
299
|
+
return `[${value.map((item) => stableJson(item)).join(",")}]`;
|
|
300
|
+
if (isRecord(value)) {
|
|
301
|
+
const keys = Object.keys(value)
|
|
302
|
+
.filter((key) => value[key] !== undefined)
|
|
303
|
+
.sort();
|
|
304
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`).join(",")}}`;
|
|
305
|
+
}
|
|
306
|
+
return JSON.stringify(value) ?? "null";
|
|
307
|
+
}
|
|
308
|
+
// 突き合わせ先の変更検出(PJR-Z47X)。観点が comparison_sources で宣言した突き合わせ先と、
|
|
309
|
+
// 成果物の depends_on 先の hash を grade result の source_hashes へ記録し、
|
|
310
|
+
// --changed-only と --dependency-changed が現在の hash と比較する。
|
|
311
|
+
const COMPARISON_SOURCE_RE = /^(catalog-entry|dependencies|schedule|members|roles|deliverable:[a-z0-9][a-z0-9-]*)$/;
|
|
312
|
+
// 突き合わせ先が存在しないことも状態として記録し、後から作られた場合に変更として検出する。
|
|
313
|
+
const ABSENT_SOURCE_HASH = gradeContentHash("specdojo:grade-comparison-source-absent");
|
|
314
|
+
export function createGradeSourceContext(projectOption, rootDir) {
|
|
315
|
+
return {
|
|
316
|
+
rootDir,
|
|
317
|
+
project: resolveProject(projectOption).project,
|
|
318
|
+
projectOption,
|
|
319
|
+
hashes: new Map(),
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
function sourceCatalog(context) {
|
|
323
|
+
if (!context.catalog || !context.localIdToPath) {
|
|
324
|
+
context.catalog = getProjectCatalogPath(context.project)
|
|
325
|
+
? loadDeliverableCatalog(context.projectOption, context.rootDir)
|
|
326
|
+
: new Map();
|
|
327
|
+
context.localIdToPath = new Map([...context.catalog.entries()].map(([path, entry]) => [entry.localId, path]));
|
|
328
|
+
}
|
|
329
|
+
return { catalog: context.catalog, localIdToPath: context.localIdToPath };
|
|
330
|
+
}
|
|
331
|
+
function hashSourceFile(path) {
|
|
332
|
+
if (!path || !existsSync(path))
|
|
333
|
+
return ABSENT_SOURCE_HASH;
|
|
334
|
+
return gradeContentHash(readFileSync(path, "utf8"));
|
|
335
|
+
}
|
|
336
|
+
// ディレクトリはファイルの相対パスと内容の組で hash する。生成物は生成元の変更で検出する。
|
|
337
|
+
function hashSourceDirectory(path) {
|
|
338
|
+
const files = listFilesRecursive(path)
|
|
339
|
+
.filter((file) => !relativePathFromRoot(file, path).split("/").includes("generated"))
|
|
340
|
+
.sort();
|
|
341
|
+
if (files.length === 0)
|
|
342
|
+
return ABSENT_SOURCE_HASH;
|
|
343
|
+
const hash = createHash("sha256");
|
|
344
|
+
for (const file of files) {
|
|
345
|
+
hash.update(`${relativePathFromRoot(file, path)}\0${hashSourceFile(file)}\n`);
|
|
346
|
+
}
|
|
347
|
+
return hash.digest("hex");
|
|
348
|
+
}
|
|
349
|
+
function sharedSourceHash(key, context) {
|
|
350
|
+
const cached = context.hashes.get(key);
|
|
351
|
+
if (cached !== undefined)
|
|
352
|
+
return cached;
|
|
353
|
+
let hash;
|
|
354
|
+
if (key === "schedule") {
|
|
355
|
+
hash = hashSourceDirectory(resolve(context.rootDir, getProjectSchedulePath(context.project)));
|
|
356
|
+
}
|
|
357
|
+
else if (key === "members" || key === "roles") {
|
|
358
|
+
const path = key === "members"
|
|
359
|
+
? getProjectMembersPath(context.project)
|
|
360
|
+
: getProjectRolesPath(context.project);
|
|
361
|
+
hash = hashSourceFile(path ? resolve(context.rootDir, path) : undefined);
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
const localId = key.slice(key.indexOf(":") + 1);
|
|
365
|
+
hash = hashSourceFile(sourceCatalog(context).localIdToPath.get(localId));
|
|
366
|
+
}
|
|
367
|
+
context.hashes.set(key, hash);
|
|
368
|
+
return hash;
|
|
369
|
+
}
|
|
370
|
+
export function gradedComparisonSources(viewpoints, target, metadata) {
|
|
371
|
+
if (!viewpoints)
|
|
372
|
+
return [];
|
|
373
|
+
const sources = new Set();
|
|
374
|
+
for (const viewpoint of gradedViewpoints(viewpoints, target, metadata)) {
|
|
375
|
+
for (const source of viewpoint.comparison_sources ?? []) {
|
|
376
|
+
if (!COMPARISON_SOURCE_RE.test(source)) {
|
|
377
|
+
throw new Error(`${viewpoint.id}: unknown comparison source ${source}`);
|
|
378
|
+
}
|
|
379
|
+
sources.add(source);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return [...sources].sort();
|
|
383
|
+
}
|
|
384
|
+
// depends_on のうちカタログで Markdown 成果物に解決できるものだけを依存先として扱う。
|
|
385
|
+
export function dependencySourceKeys(documentPath, context) {
|
|
386
|
+
const { catalog, localIdToPath } = sourceCatalog(context);
|
|
387
|
+
return (catalog.get(documentPath)?.dependsOn ?? [])
|
|
388
|
+
.filter((localId) => localIdToPath.has(localId))
|
|
389
|
+
.map((localId) => `dependency:${localId}`)
|
|
390
|
+
.sort();
|
|
391
|
+
}
|
|
392
|
+
export function comparisonSourceKeys(sources, documentPath, context) {
|
|
393
|
+
return sources.flatMap((source) => source === "dependencies" ? dependencySourceKeys(documentPath, context) : [source]);
|
|
394
|
+
}
|
|
395
|
+
export function currentSourceHashes(keys, documentPath, context) {
|
|
396
|
+
const hashes = {};
|
|
397
|
+
for (const key of [...new Set(keys)].sort()) {
|
|
398
|
+
if (key === "catalog-entry") {
|
|
399
|
+
hashes[key] =
|
|
400
|
+
sourceCatalog(context).catalog.get(documentPath)?.entryHash ?? ABSENT_SOURCE_HASH;
|
|
401
|
+
}
|
|
402
|
+
else if (key.startsWith("dependency:")) {
|
|
403
|
+
const localId = key.slice("dependency:".length);
|
|
404
|
+
hashes[key] = hashSourceFile(sourceCatalog(context).localIdToPath.get(localId));
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
hashes[key] = sharedSourceHash(key, context);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return hashes;
|
|
411
|
+
}
|
|
412
|
+
// grade result へ記録する突き合わせ先の hash。宣言された突き合わせ先に加え、成果物では
|
|
413
|
+
// --dependency-changed が使う depends_on 先を、観点の宣言に関係なく記録する。
|
|
414
|
+
export function gradeSourceHashes(documentPath, document, viewpoints, target, context) {
|
|
415
|
+
const metadata = isRecord(document.data.specdojo) ? document.data.specdojo : {};
|
|
416
|
+
const keys = [
|
|
417
|
+
...comparisonSourceKeys(gradedComparisonSources(viewpoints, target, metadata), documentPath, context),
|
|
418
|
+
...(target === "deliverable" ? dependencySourceKeys(documentPath, context) : []),
|
|
419
|
+
];
|
|
420
|
+
return currentSourceHashes(keys, documentPath, context);
|
|
421
|
+
}
|
|
422
|
+
/** Current hashes of the comparison sources and dependencies that grade apply records for a document. */
|
|
423
|
+
export function resolveGradeSourceHashes(opts, rootDir = specdojoRootDir()) {
|
|
424
|
+
const absolute = resolveSafeMarkdownPath(opts.path, rootDir);
|
|
425
|
+
const document = parseMarkdown(readFileSync(absolute, "utf8"), relativePathFromRoot(absolute, rootDir));
|
|
426
|
+
const viewpoints = opts.viewpoints ?? loadViewpointsIfConfigured(opts.project, rootDir);
|
|
427
|
+
return gradeSourceHashes(absolute, document, viewpoints, opts.target, createGradeSourceContext(opts.project, rootDir));
|
|
428
|
+
}
|
|
429
|
+
// 記録の無い突き合わせ先(source tracking 導入前の結果を含む)は、鮮度を保証できないため変更として扱う。
|
|
430
|
+
export function sourceHashesChanged(stored, current) {
|
|
431
|
+
return Object.entries(current).some(([key, hash]) => stored?.[key] !== hash);
|
|
432
|
+
}
|