@dogfood-lab/findings 1.4.0 → 1.5.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/cli.js +16 -0
- package/derive/ids.js +31 -3
- package/derive/write-findings.js +28 -0
- package/package.json +1 -1
- package/reader.js +28 -3
- package/review/event-log.js +60 -76
- package/review/review-artifacts.js +45 -7
- package/synthesis/apply-recommendation.js +17 -0
- package/synthesis/pattern-derivation.js +11 -2
- package/synthesis/write-artifacts.js +18 -0
package/cli.js
CHANGED
|
@@ -279,6 +279,14 @@ Synthesis artifacts (patterns / recommendations / doctrine):
|
|
|
279
279
|
doctrine <accept|reject> <id> [--actor X] [--reason Y]
|
|
280
280
|
doctrine queue
|
|
281
281
|
|
|
282
|
+
Advice (read-only, derived from accepted artifacts):
|
|
283
|
+
advise --surface <surface> [--execution-mode <mode>] [--repo <org/repo>] [--json]
|
|
284
|
+
Bootstrap guidance for a new repo/surface.
|
|
285
|
+
--json emits the structured advice bundle
|
|
286
|
+
as pure JSON (pipeable; no human text on
|
|
287
|
+
stdout) for shipcheck / repo-knowledge.
|
|
288
|
+
sync-export [--json] Export accepted artifacts for downstream sync.
|
|
289
|
+
|
|
282
290
|
Note: patterns carry a literal "invalidated" status; recommendations and
|
|
283
291
|
doctrine do not, so invalidate is supported for patterns only. The review /
|
|
284
292
|
reopen verbs target the intermediate "reviewed" state, which the artifact
|
|
@@ -1095,6 +1103,14 @@ Filters (for list):
|
|
|
1095
1103
|
const bundle = generateAdviceBundle(ROOT, { surface, executionMode, repo });
|
|
1096
1104
|
const a = bundle.advice;
|
|
1097
1105
|
|
|
1106
|
+
// --json keeps stdout pure JSON so downstream tools (shipcheck,
|
|
1107
|
+
// repo-knowledge) can pipe the structured bundle without scraping the
|
|
1108
|
+
// human formatter. Matches the sync-export verb's flags.json idiom below.
|
|
1109
|
+
if (flags.json) {
|
|
1110
|
+
console.log(JSON.stringify(bundle, null, 2));
|
|
1111
|
+
process.exit(0);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1098
1114
|
console.log(`Advice for: ${[surface, executionMode, repo].filter(Boolean).join(', ') || 'general'}\n`);
|
|
1099
1115
|
|
|
1100
1116
|
if (a.starter_checks.length > 0) {
|
package/derive/ids.js
CHANGED
|
@@ -5,17 +5,33 @@
|
|
|
5
5
|
* No timestamp noise in IDs.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { createHash } from 'node:crypto';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Delimiter for the boundary hash: a NUL code unit cannot appear in a repo
|
|
12
|
+
* slug (`[a-zA-Z0-9_.-]`) nor in a derived lesson slug, so joining components
|
|
13
|
+
* with it means no component value can forge a false boundary between them.
|
|
14
|
+
*/
|
|
15
|
+
const BOUNDARY_DELIM = '\u0000';
|
|
16
|
+
|
|
8
17
|
/**
|
|
9
18
|
* Generate a stable finding ID from derivation context.
|
|
10
|
-
* Format: dfind-<repo-slug>-<lesson-slug>
|
|
19
|
+
* Format: dfind-<repo-slug>-<lesson-slug>-<boundary-hash>
|
|
20
|
+
*
|
|
21
|
+
* The trailing boundary hash disambiguates inputs whose component boundary is
|
|
22
|
+
* itself an underscore (findings-A-002): `sanitize` folds `_` to `-`, so
|
|
23
|
+
* (repoSlug='a_b', lessonSlug='c') and (repoSlug='a', lessonSlug='b_c') both
|
|
24
|
+
* flatten the human-readable middle to `a-b-c` and previously produced the
|
|
25
|
+
* same id. The hash is computed over the components joined by BOUNDARY_DELIM,
|
|
26
|
+
* so the two tuples hash differently and never collapse to one finding_id.
|
|
11
27
|
*
|
|
12
28
|
* @param {string} repoSlug - e.g. "repo-crawler-mcp"
|
|
13
29
|
* @param {string} lessonSlug - e.g. "surface-misclassification"
|
|
14
30
|
* @returns {string}
|
|
15
31
|
*/
|
|
16
32
|
export function generateFindingId(repoSlug, lessonSlug) {
|
|
17
|
-
const
|
|
18
|
-
return
|
|
33
|
+
const boundary = boundaryHash([repoSlug, lessonSlug]);
|
|
34
|
+
return `dfind-${sanitize(repoSlug)}-${sanitize(lessonSlug)}-${boundary}`;
|
|
19
35
|
}
|
|
20
36
|
|
|
21
37
|
/**
|
|
@@ -46,3 +62,15 @@ function sanitize(s) {
|
|
|
46
62
|
.replace(/-+/g, '-')
|
|
47
63
|
.replace(/^-|-$/g, '');
|
|
48
64
|
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Short stable hash of an ordered component tuple, joined by BOUNDARY_DELIM so
|
|
68
|
+
* no component value can forge a false boundary. 8 lowercase-hex chars are
|
|
69
|
+
* ample for the finding/pattern id space (a per-repo, per-lesson namespace).
|
|
70
|
+
*
|
|
71
|
+
* @param {string[]} components
|
|
72
|
+
* @returns {string}
|
|
73
|
+
*/
|
|
74
|
+
export function boundaryHash(components) {
|
|
75
|
+
return createHash('sha256').update(components.join(BOUNDARY_DELIM)).digest('hex').slice(0, 8);
|
|
76
|
+
}
|
package/derive/write-findings.js
CHANGED
|
@@ -34,6 +34,7 @@ import { mkdirSync, existsSync } from 'node:fs';
|
|
|
34
34
|
import { resolve, dirname } from 'node:path';
|
|
35
35
|
import yaml from 'js-yaml';
|
|
36
36
|
|
|
37
|
+
import { isUnsafeSegment } from '@dogfood-lab/ingest/lib/unsafe-segment.js';
|
|
37
38
|
import { atomicWriteFileSync } from '../lib/atomic-write.js';
|
|
38
39
|
import { validateFinding } from '../validate.js';
|
|
39
40
|
|
|
@@ -75,6 +76,26 @@ export class FindingIdCollisionError extends Error {
|
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Structured error thrown when a finding's `repo` splits into an org/repo
|
|
81
|
+
* segment that is a path-traversal vector (`..` or a path separator).
|
|
82
|
+
*
|
|
83
|
+
* findings-A-001 — write-side traversal guard. The READ side
|
|
84
|
+
* (`loadRecordsForRepoWithSkips`) already rejects such segments via
|
|
85
|
+
* `isUnsafeSegment`; the WRITE side did not, so a schema-valid
|
|
86
|
+
* `repo: '../policies'` (the dogfood-finding `repo` pattern admits `.`/`..`)
|
|
87
|
+
* resolved one level under rootDir and wrote outside `findings/`.
|
|
88
|
+
*/
|
|
89
|
+
export class FindingUnsafeRepoError extends Error {
|
|
90
|
+
constructor(repo, findingId) {
|
|
91
|
+
super(`unsafe repo path segment in finding${findingId ? ` (${findingId})` : ''}: '${repo}' contains a path-traversal or separator and was refused before any write (findings-A-001).`);
|
|
92
|
+
this.name = 'FindingUnsafeRepoError';
|
|
93
|
+
this.code = 'FINDING_UNSAFE_REPO';
|
|
94
|
+
this.repo = repo;
|
|
95
|
+
this.findingId = findingId;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
78
99
|
/**
|
|
79
100
|
* Process-level memory of ids that have already been written via
|
|
80
101
|
* `writeFinding` (singleton) OR the singleton-call inside `writeFindings`
|
|
@@ -127,6 +148,13 @@ export function writeFinding(rootDir, finding) {
|
|
|
127
148
|
const [org, repo] = (finding.repo || '').split('/');
|
|
128
149
|
if (!org || !repo) throw new Error(`Invalid repo in finding: ${finding.repo}`);
|
|
129
150
|
|
|
151
|
+
// findings-A-001 — write-side path-traversal guard. Mirrors the READ side
|
|
152
|
+
// (load-records.js loadRecordsForRepoWithSkips) so a schema-valid
|
|
153
|
+
// `repo: '../policies'` cannot escape `findings/` into a sibling data dir.
|
|
154
|
+
if (isUnsafeSegment(org) || isUnsafeSegment(repo)) {
|
|
155
|
+
throw new FindingUnsafeRepoError(finding.repo, finding.finding_id);
|
|
156
|
+
}
|
|
157
|
+
|
|
130
158
|
// L3-001 (Wave A2 amend2): same-process same-id refusal. Programmatic
|
|
131
159
|
// callers that loop over assembleFinding output now fail-closed at the
|
|
132
160
|
// singleton path, not silently at the atomicWriteFileSync.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dogfood-lab/findings",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Finding contract spine for testing-os. Validates, reads, lists, and queries evidence-bound findings — the fourth contract alongside record, scenario, and policy.",
|
|
6
6
|
"main": "index.js",
|
package/reader.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { readdirSync, existsSync, statSync } from 'node:fs';
|
|
7
|
-
import { resolve, join, basename, extname } from 'node:path';
|
|
7
|
+
import { resolve, join, basename, extname, relative } from 'node:path';
|
|
8
8
|
import { parseFinding, validateFinding } from './validate.js';
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -129,9 +129,21 @@ export function loadFindings(rootDir, opts = {}) {
|
|
|
129
129
|
* @returns {{ path: string, data: object, valid: boolean, errors: Array } | null}
|
|
130
130
|
*/
|
|
131
131
|
export function findById(rootDir, findingId) {
|
|
132
|
+
// B-002 — collect torn finding files encountered during the scan. The
|
|
133
|
+
// finding_id lives INSIDE the file, so a torn file cannot be matched to the
|
|
134
|
+
// requested id; if the id turns out absent from every clean file, any torn
|
|
135
|
+
// file may be the one hiding it. Surfacing them on stderr (below) mirrors the
|
|
136
|
+
// derive CLI's "N finding(s) skipped (torn/unreadable)" honesty so a torn
|
|
137
|
+
// YAML never silently masquerades as a not-found id.
|
|
138
|
+
const torn = [];
|
|
139
|
+
|
|
132
140
|
// Search real findings
|
|
133
141
|
for (const filePath of discoverFindings(rootDir)) {
|
|
134
|
-
const { data } = parseFinding(filePath);
|
|
142
|
+
const { data, error } = parseFinding(filePath);
|
|
143
|
+
if (error) {
|
|
144
|
+
torn.push({ path: filePath, error });
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
135
147
|
if (data && data.finding_id === findingId) {
|
|
136
148
|
const result = validateFinding(data);
|
|
137
149
|
return { path: filePath, data, ...result };
|
|
@@ -140,13 +152,26 @@ export function findById(rootDir, findingId) {
|
|
|
140
152
|
|
|
141
153
|
// Search valid fixtures
|
|
142
154
|
for (const filePath of discoverFixtures(rootDir, 'valid')) {
|
|
143
|
-
const { data } = parseFinding(filePath);
|
|
155
|
+
const { data, error } = parseFinding(filePath);
|
|
156
|
+
if (error) {
|
|
157
|
+
torn.push({ path: filePath, error });
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
144
160
|
if (data && data.finding_id === findingId) {
|
|
145
161
|
const result = validateFinding(data);
|
|
146
162
|
return { path: filePath, data, ...result };
|
|
147
163
|
}
|
|
148
164
|
}
|
|
149
165
|
|
|
166
|
+
if (torn.length > 0) {
|
|
167
|
+
// eslint-disable-next-line no-console
|
|
168
|
+
console.error(`${torn.length} finding(s) skipped (torn/unreadable):`);
|
|
169
|
+
for (const t of torn) {
|
|
170
|
+
// eslint-disable-next-line no-console
|
|
171
|
+
console.error(` ${relative(rootDir, t.path)} — ${t.error}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
150
175
|
return null;
|
|
151
176
|
}
|
|
152
177
|
|
package/review/event-log.js
CHANGED
|
@@ -1,27 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Append-only review event log.
|
|
3
3
|
*
|
|
4
|
-
* Events are stored
|
|
4
|
+
* Events are stored ONE PER FILE under reviews/<YYYY>/<YYYY-MM-DD>/<event-id>.yaml
|
|
5
|
+
* (the same sharded-immutable-file pattern the records/ store uses). This
|
|
6
|
+
* replaced an earlier "daily YAML array, read-modify-rename under a file lock"
|
|
7
|
+
* design: a Phase-9 50-fork race detector proved that NO lock FILE is reliably
|
|
8
|
+
* exclusive on NTFS under heavy concurrent create churn — two appenders could
|
|
9
|
+
* both win the lock, both read N events, both rename, and one clobber the
|
|
10
|
+
* other's event (~1/3 of saturated runs, every child still exiting 0). A
|
|
11
|
+
* shared mutable array file cannot be made safe under concurrency on that fs.
|
|
12
|
+
* One immutable file per event removes the shared mutable state entirely: each
|
|
13
|
+
* append writes a uniquely-named file, so concurrent appends CANNOT collide or
|
|
14
|
+
* lose an event, and no lock is needed for correctness. `getAllEventsWithSkips`
|
|
15
|
+
* already globs reviews/ recursively and merges per-file events.
|
|
5
16
|
*/
|
|
6
17
|
|
|
7
|
-
import {
|
|
8
|
-
import { resolve
|
|
18
|
+
import { mkdirSync, existsSync, openSync, writeSync, closeSync } from 'node:fs';
|
|
19
|
+
import { resolve } from 'node:path';
|
|
9
20
|
import { randomBytes } from 'node:crypto';
|
|
10
21
|
import yaml from 'js-yaml';
|
|
11
22
|
|
|
12
|
-
import { withFileLock } from '../lib/file-lock.js';
|
|
13
|
-
import { renameWithRetry } from '../lib/rename-with-retry.js';
|
|
14
23
|
import { loadYamlDir } from '../lib/safe-yaml-load.js';
|
|
15
24
|
|
|
16
25
|
let _eventCounter = 0;
|
|
17
26
|
|
|
18
27
|
/**
|
|
19
|
-
* Generate a unique event ID.
|
|
28
|
+
* Generate a unique event ID. Includes a random suffix so the id is unique
|
|
29
|
+
* ACROSS processes (the `_eventCounter` is per-process, so two forks would
|
|
30
|
+
* otherwise mint the same `rev-<ts>-<seq>`); this id also names the per-event
|
|
31
|
+
* file, so cross-process uniqueness keeps two concurrent appends from picking
|
|
32
|
+
* the same filename.
|
|
20
33
|
*/
|
|
21
34
|
export function generateEventId() {
|
|
22
35
|
const ts = Date.now().toString(36);
|
|
23
36
|
const seq = (++_eventCounter).toString(36).padStart(4, '0');
|
|
24
|
-
|
|
37
|
+
const rand = randomBytes(4).toString('hex');
|
|
38
|
+
return `rev-${ts}-${seq}-${rand}`;
|
|
25
39
|
}
|
|
26
40
|
|
|
27
41
|
/**
|
|
@@ -71,82 +85,52 @@ export function getLogPath(rootDir, date = new Date()) {
|
|
|
71
85
|
}
|
|
72
86
|
|
|
73
87
|
/**
|
|
74
|
-
*
|
|
88
|
+
* Directory holding one date-sharded set of per-event files:
|
|
89
|
+
* reviews/<YYYY>/<YYYY-MM-DD>/. Each appended event is its own file inside.
|
|
90
|
+
*/
|
|
91
|
+
export function getEventDir(rootDir, date = new Date()) {
|
|
92
|
+
const year = String(date.getFullYear());
|
|
93
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
94
|
+
const day = String(date.getDate()).padStart(2, '0');
|
|
95
|
+
return resolve(rootDir, 'reviews', year, `${year}-${month}-${day}`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Append a review event by writing it to its OWN immutable file
|
|
100
|
+
* (reviews/<YYYY>/<YYYY-MM-DD>/<event-id>.yaml). See the module header for why
|
|
101
|
+
* this replaced the daily-array-under-a-lock design: no shared mutable file
|
|
102
|
+
* means concurrent appends physically cannot collide or lose an event, so no
|
|
103
|
+
* lock is needed for correctness — the Phase-9 50-fork race detector that lost
|
|
104
|
+
* an event ~1/3 of saturated runs is structurally impossible here.
|
|
75
105
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* between read and write leaves the original log intact.
|
|
106
|
+
* Crash-safety: a single `openSync(path, 'wx')` + write of one event is atomic
|
|
107
|
+
* at the file granularity — a crash mid-write leaves a complete event file or
|
|
108
|
+
* none, never a torn shared array. The filename is the (cross-process-unique)
|
|
109
|
+
* event id, so two concurrent appends never target the same path.
|
|
81
110
|
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* across the read → push → rename sequence. Two concurrent `appendEvent` calls
|
|
86
|
-
* to the SAME daily log serialize against each other; calls to DIFFERENT daily
|
|
87
|
-
* logs (e.g. across a midnight boundary) do not contend. The lock is reclaimed
|
|
88
|
-
* if the holder process dies — see `lib/file-lock.js` for the full design
|
|
89
|
-
* rationale (why a lock dir, why not `O_APPEND`, stale recovery semantics,
|
|
90
|
-
* single-machine scope).
|
|
111
|
+
* @param {string} rootDir
|
|
112
|
+
* @param {object} event - a `createEvent()` result.
|
|
113
|
+
* @returns {string} the path of the event file written.
|
|
91
114
|
*/
|
|
92
115
|
export function appendEvent(rootDir, event) {
|
|
93
|
-
const
|
|
94
|
-
const dir = dirname(logPath);
|
|
116
|
+
const dir = getEventDir(rootDir);
|
|
95
117
|
mkdirSync(dir, { recursive: true });
|
|
96
118
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
//
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
events.push(event);
|
|
112
|
-
const tmpSuffix = randomBytes(4).toString('hex');
|
|
113
|
-
const tmpPath = `${logPath}.${tmpSuffix}.tmp`;
|
|
114
|
-
writeFileSync(tmpPath, yaml.dump(events, { lineWidth: 120, noRefs: true }), 'utf-8');
|
|
115
|
-
// Windows EPERM/EBUSY on rename can fire transiently when AV or
|
|
116
|
-
// Search Indexer holds a handle to the freshly written temp. Retry.
|
|
117
|
-
renameWithRetry(tmpPath, logPath);
|
|
118
|
-
return logPath;
|
|
119
|
+
const eventId = event.review_event_id || generateEventId();
|
|
120
|
+
const eventPath = resolve(dir, `${eventId}.yaml`);
|
|
121
|
+
const body = yaml.dump(event, { lineWidth: 120, noRefs: true });
|
|
122
|
+
|
|
123
|
+
// 'wx' = O_EXCL exclusive-create: asserts the unique id has not collided
|
|
124
|
+
// (it carries a random suffix, so it won't) rather than relying on it. No
|
|
125
|
+
// temp+rename is needed — there is no shared file to atomically replace; one
|
|
126
|
+
// event per file is already all-or-nothing.
|
|
127
|
+
const fd = openSync(eventPath, 'wx');
|
|
128
|
+
try {
|
|
129
|
+
writeSync(fd, body);
|
|
130
|
+
} finally {
|
|
131
|
+
closeSync(fd);
|
|
119
132
|
}
|
|
120
|
-
|
|
121
|
-
return withFileLock(logPath, () => {
|
|
122
|
-
let events = [];
|
|
123
|
-
// Read-or-empty without an `existsSync` precheck: the readFileSync call
|
|
124
|
-
// either returns the bytes or throws ENOENT. Avoiding `existsSync` here
|
|
125
|
-
// closes a Windows-specific TOCTOU window where the dirent cache could
|
|
126
|
-
// report `existsSync(logPath) === false` immediately after a sibling
|
|
127
|
-
// process renamed a fresh file into place — which would cause us to
|
|
128
|
-
// start with `events = []` and silently OVERWRITE the sibling's events.
|
|
129
|
-
// The lock alone wasn't enough; the existsSync gate was the bug.
|
|
130
|
-
try {
|
|
131
|
-
const raw = readFileSync(logPath, 'utf-8');
|
|
132
|
-
const parsed = yaml.load(raw);
|
|
133
|
-
if (parsed) events = Array.isArray(parsed) ? parsed : [parsed];
|
|
134
|
-
} catch (err) {
|
|
135
|
-
if (!err || err.code !== 'ENOENT') throw err;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
events.push(event);
|
|
139
|
-
|
|
140
|
-
// Atomic write: temp file → rename. Same pattern persist.js + rebuild-indexes.js use.
|
|
141
|
-
const tmpSuffix = randomBytes(4).toString('hex');
|
|
142
|
-
const tmpPath = `${logPath}.${tmpSuffix}.tmp`;
|
|
143
|
-
writeFileSync(tmpPath, yaml.dump(events, { lineWidth: 120, noRefs: true }), 'utf-8');
|
|
144
|
-
// renameWithRetry: tolerate the Windows EPERM/EBUSY transient handle race
|
|
145
|
-
// even though we hold the per-file lock — antivirus/Search Indexer can
|
|
146
|
-
// still grab a handle on the temp during the rename window.
|
|
147
|
-
renameWithRetry(tmpPath, logPath);
|
|
148
|
-
return logPath;
|
|
149
|
-
});
|
|
133
|
+
return eventPath;
|
|
150
134
|
}
|
|
151
135
|
|
|
152
136
|
/**
|
|
@@ -32,10 +32,12 @@
|
|
|
32
32
|
* partial coverage, not a silent no-op.
|
|
33
33
|
*/
|
|
34
34
|
|
|
35
|
+
import { relative } from 'node:path';
|
|
36
|
+
|
|
35
37
|
import { validateTransition, ACTION_TARGET_STATUS, REASON_REQUIRED, REQUIRES_ACCEPTED, REQUIRES_CLOSED } from './transitions.js';
|
|
36
38
|
import { createEvent, appendEvent } from './event-log.js';
|
|
37
39
|
import {
|
|
38
|
-
|
|
40
|
+
resetSeenArtifactWrite,
|
|
39
41
|
writePattern,
|
|
40
42
|
writeRecommendation,
|
|
41
43
|
writeDoctrine,
|
|
@@ -44,6 +46,34 @@ import {
|
|
|
44
46
|
loadDoctrinesWithSkips
|
|
45
47
|
} from '../synthesis/write-artifacts.js';
|
|
46
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Surface torn/unreadable artifact YAML on stderr — the lookup-path analogue of
|
|
51
|
+
* the derive CLI's "N pattern(s) skipped (torn/unreadable)" reporting
|
|
52
|
+
* (cli.js) and `findRecordFile`'s stderr note (derive/load-records.js).
|
|
53
|
+
*
|
|
54
|
+
* B-001 — `findArtifactById` / `getArtifactReviewQueue` consumed only the
|
|
55
|
+
* loader's `entries`, discarding `skipped[]`. A torn artifact YAML for the very
|
|
56
|
+
* id under accept/show/queue therefore vanished behind a bare "<type> not
|
|
57
|
+
* found", giving the operator no signal that a file was unreadable. The id
|
|
58
|
+
* lives INSIDE the file, so a torn file can't be matched to a requested id;
|
|
59
|
+
* the honest signal is to name the torn files whenever any are present so the
|
|
60
|
+
* operator can distinguish "absent" from "present but unparseable". Returns the
|
|
61
|
+
* skipped list so callers can decide whether the not-found was hint-worthy.
|
|
62
|
+
*
|
|
63
|
+
* @param {string} rootDir
|
|
64
|
+
* @param {string} type - artifact kind, for the message prefix
|
|
65
|
+
* @param {Array<{ path: string, error: string }>} skipped
|
|
66
|
+
*/
|
|
67
|
+
function reportArtifactSkips(rootDir, type, skipped) {
|
|
68
|
+
if (!skipped || skipped.length === 0) return;
|
|
69
|
+
// eslint-disable-next-line no-console
|
|
70
|
+
console.error(`${skipped.length} ${type}(s) skipped (torn/unreadable):`);
|
|
71
|
+
for (const s of skipped) {
|
|
72
|
+
// eslint-disable-next-line no-console
|
|
73
|
+
console.error(` ${relative(rootDir, s.path)} — ${s.error}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
47
77
|
/**
|
|
48
78
|
* Per-artifact-type configuration: the on-disk directory, the id field name,
|
|
49
79
|
* the loader (with-skips) and the writer. Mirrors how the finding engine pairs
|
|
@@ -87,12 +117,15 @@ const ARTIFACT_TYPES = {
|
|
|
87
117
|
export function findArtifactById(rootDir, type, id) {
|
|
88
118
|
const cfg = ARTIFACT_TYPES[type];
|
|
89
119
|
if (!cfg) return null;
|
|
90
|
-
const { entries } = cfg.loadWithSkips(rootDir);
|
|
120
|
+
const { entries, skipped } = cfg.loadWithSkips(rootDir);
|
|
91
121
|
for (const entry of entries) {
|
|
92
122
|
if (entry.data && entry.data[cfg.idKey] === id) {
|
|
93
123
|
return { data: entry.data, path: entry.path, type };
|
|
94
124
|
}
|
|
95
125
|
}
|
|
126
|
+
// B-001 — id absent from the clean entries. A torn file may be hiding it;
|
|
127
|
+
// name the torn files rather than letting a bare not-found mislead.
|
|
128
|
+
reportArtifactSkips(rootDir, type, skipped);
|
|
96
129
|
return null;
|
|
97
130
|
}
|
|
98
131
|
|
|
@@ -217,11 +250,13 @@ export function reviewArtifact(rootDir, params) {
|
|
|
217
250
|
});
|
|
218
251
|
|
|
219
252
|
// Persist via the synthesis writer — which RE-VALIDATES the promoted artifact
|
|
220
|
-
// against its JSON Schema (fail-closed).
|
|
221
|
-
//
|
|
222
|
-
//
|
|
253
|
+
// against its JSON Schema (fail-closed). The reset is the documented opt-in
|
|
254
|
+
// for a legitimate re-write of an id already touched in this process (the
|
|
255
|
+
// synthesis collision guard otherwise refuses the second write). B-003 —
|
|
256
|
+
// scope it to THIS id so a future batch reviewer doesn't disarm the guard for
|
|
257
|
+
// the other ids it has written this process.
|
|
223
258
|
try {
|
|
224
|
-
|
|
259
|
+
resetSeenArtifactWrite(rootDir, type, id);
|
|
225
260
|
cfg.write(rootDir, artifact);
|
|
226
261
|
} catch (err) {
|
|
227
262
|
return { success: false, error: err.message, code: err.code };
|
|
@@ -249,7 +284,10 @@ export function getArtifactReviewQueue(rootDir, type) {
|
|
|
249
284
|
for (const t of types) {
|
|
250
285
|
const cfg = ARTIFACT_TYPES[t];
|
|
251
286
|
if (!cfg) continue;
|
|
252
|
-
const { entries } = cfg.loadWithSkips(rootDir);
|
|
287
|
+
const { entries, skipped } = cfg.loadWithSkips(rootDir);
|
|
288
|
+
// B-001 — a torn artifact silently shrinks the review queue; name it so the
|
|
289
|
+
// operator knows the queue is partial rather than complete.
|
|
290
|
+
reportArtifactSkips(rootDir, t, skipped);
|
|
253
291
|
for (const entry of entries) {
|
|
254
292
|
const data = entry.data;
|
|
255
293
|
if (!data) continue;
|
|
@@ -34,6 +34,7 @@ import { resolve } from 'node:path';
|
|
|
34
34
|
import { existsSync, readFileSync } from 'node:fs';
|
|
35
35
|
import yaml from 'js-yaml';
|
|
36
36
|
|
|
37
|
+
import { isUnsafeSegment } from '@dogfood-lab/ingest/lib/unsafe-segment.js';
|
|
37
38
|
import { atomicWriteFileSync } from '../lib/atomic-write.js';
|
|
38
39
|
import { findArtifactById } from '../review/review-artifacts.js';
|
|
39
40
|
import { createEvent, appendEvent } from '../review/event-log.js';
|
|
@@ -89,6 +90,22 @@ export function applyRecommendation(rootDir, params) {
|
|
|
89
90
|
const action = rec.action || {};
|
|
90
91
|
const surfaces = rec.applies_to?.product_surfaces || [];
|
|
91
92
|
|
|
93
|
+
// findings-A-001 — path-traversal guard on the operator-supplied --policy
|
|
94
|
+
// <org/repo>. `policyPathFor` resolves `policies/repos/<org>/<repo>.yaml`; an
|
|
95
|
+
// org/repo carrying `..` or a separator would escape the policies tree on
|
|
96
|
+
// BOTH the dry-run (path leaked in preview) and write (file touched) paths,
|
|
97
|
+
// so reject here before either branch resolves a path.
|
|
98
|
+
if (params.policyRepo) {
|
|
99
|
+
const [pOrg, pRepo] = String(params.policyRepo).split('/');
|
|
100
|
+
if (!pOrg || !pRepo || isUnsafeSegment(pOrg) || isUnsafeSegment(pRepo)) {
|
|
101
|
+
return structuredError(
|
|
102
|
+
'RECOMMENDATION_UNSAFE_POLICY',
|
|
103
|
+
`policy repo "${params.policyRepo}" is not a safe org/repo path segment`,
|
|
104
|
+
'Pass --policy <org/repo> with no ".." or path separators inside the org or repo name.'
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
92
109
|
// Build the resolution context shared by dry-run and write.
|
|
93
110
|
const isStructured = STRUCTURED_LIST_ACTIONS.has(action.type);
|
|
94
111
|
const surface = surfaces.length === 1 ? surfaces[0] : null;
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { loadFindings } from '../reader.js';
|
|
11
|
+
import { boundaryHash } from '../derive/ids.js';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Derive candidate patterns from accepted findings.
|
|
@@ -120,7 +121,7 @@ function isFalseRecurrence(findings) {
|
|
|
120
121
|
/**
|
|
121
122
|
* Build a pattern candidate from a cluster.
|
|
122
123
|
*/
|
|
123
|
-
function buildPatternCandidate(cluster) {
|
|
124
|
+
export function buildPatternCandidate(cluster) {
|
|
124
125
|
const { findings, issue_kind, root_cause_kind, remediation_kind } = cluster;
|
|
125
126
|
const now = new Date().toISOString();
|
|
126
127
|
|
|
@@ -141,8 +142,16 @@ function buildPatternCandidate(cluster) {
|
|
|
141
142
|
// otherwise two clusters that differ only by root_cause_kind collide on pattern_id and the
|
|
142
143
|
// second writePattern() silently overwrites the first on disk. Surface is added for readability,
|
|
143
144
|
// not for uniqueness.
|
|
145
|
+
//
|
|
146
|
+
// The human-readable slug runs `.replace(/_/g, '-')` AFTER concatenation, which folds the
|
|
147
|
+
// underscore inside a component into the same `-` that delimits components: issue_kind='a_b' +
|
|
148
|
+
// root_cause='c' and issue_kind='a' + root_cause='b_c' both flatten to `...-a-b-c`
|
|
149
|
+
// (findings-A-002). A trailing boundary hash over the UN-flattened cluster key keeps the two
|
|
150
|
+
// distinct, so legitimately different clusters no longer trip the L3-001 *_ID_COLLISION guard.
|
|
144
151
|
const surfaceStr = surfaces.size === 1 ? [...surfaces][0] : 'multi-surface';
|
|
145
|
-
const
|
|
152
|
+
const readable = `${surfaceStr}-${issue_kind}-${root_cause_kind}`.replace(/_/g, '-');
|
|
153
|
+
const boundary = boundaryHash([surfaceStr, issue_kind, root_cause_kind]);
|
|
154
|
+
const slug = `${readable}-${boundary}`;
|
|
146
155
|
|
|
147
156
|
// Determine strength
|
|
148
157
|
const strength = repos.size >= 3 ? 'strong' : repos.size >= 2 ? 'emerging' : 'emerging';
|
|
@@ -59,6 +59,24 @@ export function resetSeenArtifactWrites(rootDir) {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Reset the collision memory for a SINGLE artifact id only.
|
|
64
|
+
*
|
|
65
|
+
* B-003 — `reviewArtifact` re-writes the one id it is promoting, which
|
|
66
|
+
* legitimately needs that id's guard cleared. Clearing the whole root (via
|
|
67
|
+
* `resetSeenArtifactWrites(rootDir)`) also disarms the silent-clobber guard for
|
|
68
|
+
* every OTHER kind/id — latent for a future batch caller reviewing several ids
|
|
69
|
+
* in one process. Scope the reset to `${rootDir}:${kind}:${id}` so only the id
|
|
70
|
+
* under review is cleared.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} rootDir
|
|
73
|
+
* @param {'pattern'|'recommendation'|'doctrine'} kind
|
|
74
|
+
* @param {string} id
|
|
75
|
+
*/
|
|
76
|
+
export function resetSeenArtifactWrite(rootDir, kind, id) {
|
|
77
|
+
seenArtifactWrites.delete(`${rootDir}:${kind}:${id}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
62
80
|
/**
|
|
63
81
|
* Structured collision errors thrown by the singleton writers when called
|
|
64
82
|
* twice in the same process with the same id. Mirror the batch helpers'
|