@unbrained/pm-lint 0.0.0-stage → 2026.10.28
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 +32 -0
- package/LICENSE +21 -0
- package/README.md +446 -2
- package/dist/index.d.ts +51 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/src/baseline-file.d.ts +7 -0
- package/dist/src/baseline-file.d.ts.map +1 -0
- package/dist/src/baseline-file.js +79 -0
- package/dist/src/baseline-file.js.map +1 -0
- package/dist/src/baseline.d.ts +39 -0
- package/dist/src/baseline.d.ts.map +1 -0
- package/dist/src/baseline.js +68 -0
- package/dist/src/baseline.js.map +1 -0
- package/dist/src/commands.d.ts +33 -0
- package/dist/src/commands.d.ts.map +1 -0
- package/dist/src/commands.js +94 -0
- package/dist/src/commands.js.map +1 -0
- package/dist/src/engine.d.ts +48 -0
- package/dist/src/engine.d.ts.map +1 -0
- package/dist/src/engine.js +124 -0
- package/dist/src/engine.js.map +1 -0
- package/dist/src/incremental-command.d.ts +6 -0
- package/dist/src/incremental-command.d.ts.map +1 -0
- package/dist/src/incremental-command.js +27 -0
- package/dist/src/incremental-command.js.map +1 -0
- package/dist/src/incremental-contract.d.ts +72 -0
- package/dist/src/incremental-contract.d.ts.map +1 -0
- package/dist/src/incremental-contract.js +142 -0
- package/dist/src/incremental-contract.js.map +1 -0
- package/dist/src/incremental-file.d.ts +13 -0
- package/dist/src/incremental-file.d.ts.map +1 -0
- package/dist/src/incremental-file.js +119 -0
- package/dist/src/incremental-file.js.map +1 -0
- package/dist/src/incremental.d.ts +41 -0
- package/dist/src/incremental.d.ts.map +1 -0
- package/dist/src/incremental.js +116 -0
- package/dist/src/incremental.js.map +1 -0
- package/dist/src/repair-command.d.ts +6 -0
- package/dist/src/repair-command.d.ts.map +1 -0
- package/dist/src/repair-command.js +26 -0
- package/dist/src/repair-command.js.map +1 -0
- package/dist/src/repair-snapshot.d.ts +5 -0
- package/dist/src/repair-snapshot.d.ts.map +1 -0
- package/dist/src/repair-snapshot.js +66 -0
- package/dist/src/repair-snapshot.js.map +1 -0
- package/dist/src/repairs.d.ts +64 -0
- package/dist/src/repairs.d.ts.map +1 -0
- package/dist/src/repairs.js +129 -0
- package/dist/src/repairs.js.map +1 -0
- package/dist/src/rule-pack-command.d.ts +8 -0
- package/dist/src/rule-pack-command.d.ts.map +1 -0
- package/dist/src/rule-pack-command.js +30 -0
- package/dist/src/rule-pack-command.js.map +1 -0
- package/dist/src/rule-pack.d.ts +79 -0
- package/dist/src/rule-pack.d.ts.map +1 -0
- package/dist/src/rule-pack.js +174 -0
- package/dist/src/rule-pack.js.map +1 -0
- package/dist/src/rules.d.ts +28 -0
- package/dist/src/rules.d.ts.map +1 -0
- package/dist/src/rules.js +153 -0
- package/dist/src/rules.js.map +1 -0
- package/dist/src/settings.d.ts +31 -0
- package/dist/src/settings.d.ts.map +1 -0
- package/dist/src/settings.js +109 -0
- package/dist/src/settings.js.map +1 -0
- package/dist/src/sidecar-publication.d.ts +39 -0
- package/dist/src/sidecar-publication.d.ts.map +1 -0
- package/dist/src/sidecar-publication.js +105 -0
- package/dist/src/sidecar-publication.js.map +1 -0
- package/dist/src/statuses.d.ts +8 -0
- package/dist/src/statuses.d.ts.map +1 -0
- package/dist/src/statuses.js +10 -0
- package/dist/src/statuses.js.map +1 -0
- package/dist/src/types.d.ts +44 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/witness-command.d.ts +6 -0
- package/dist/src/witness-command.d.ts.map +1 -0
- package/dist/src/witness-command.js +27 -0
- package/dist/src/witness-command.js.map +1 -0
- package/dist/src/witnesses.d.ts +74 -0
- package/dist/src/witnesses.d.ts.map +1 -0
- package/dist/src/witnesses.js +171 -0
- package/dist/src/witnesses.js.map +1 -0
- package/docs/incremental-example.md +64 -0
- package/docs/incremental-validation.md +99 -0
- package/docs/portable-rule-packs-validation.md +86 -0
- package/docs/repair-preview-example.md +43 -0
- package/docs/repair-preview-validation.md +42 -0
- package/examples/authoring-rules.json +30 -0
- package/manifest.json +13 -0
- package/package.json +150 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repair-command.js","sourceRoot":"","sources":["../../src/repair-command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAEtE,iGAAiG;AACjG,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,GAA0B;IAC/D,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAW,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QACjG,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,MAAM,uBAAuB,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,CAAC;IAC9E,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,CAAC,KAAK,YAAY,aAAa,CAAC;YAAE,MAAM,KAAK,CAAC;QACnD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;IAC7H,CAAC;AACH,CAAC;AAED,4GAA4G;AAC5G,MAAM,UAAU,qBAAqB,CAAC,GAAiB;IACrD,GAAG,CAAC,eAAe,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,WAAW,EAAE,wFAAwF,EAAE,KAAK,EAAE;YACrK,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,iDAAiD,EAAE;YACxH,EAAE,IAAI,EAAE,YAAY,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,+DAA+D,EAAE;SACjJ,EAAE,QAAQ,EAAE,CAAC,4CAA4C,EAAE,yDAAyD,CAAC,EAAE,GAAG,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC;AACpJ,CAAC;AAED,uGAAuG;AACvG,SAAS,MAAM,CAAC,KAAc;IAC5B,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAuB,CAAC;AAC/F,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Operational state and executable extensions are foreign to an observational authoring snapshot. */
|
|
2
|
+
export declare const REPAIR_EXCLUDED_STATE: Readonly<Record<string, string>>;
|
|
3
|
+
/** Isolate SDK read-side caches, validate copy stability and discard only this invocation's own scratch directory. */
|
|
4
|
+
export declare function withRepairSnapshot<T>(source: string, read: (snapshot: string) => Promise<T>, onSnapshotRead?: () => Promise<void>): Promise<T>;
|
|
5
|
+
//# sourceMappingURL=repair-snapshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repair-snapshot.d.ts","sourceRoot":"","sources":["../../src/repair-snapshot.ts"],"names":[],"mappings":"AAMA,sGAAsG;AACtG,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAyM,CAAC;AAsB7Q,sHAAsH;AACtH,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,cAAc,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAapJ"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { chmod, cp, mkdtemp, readFile, readdir, rm } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join, relative } from 'node:path';
|
|
5
|
+
import { RepairRefusal } from './repairs.js';
|
|
6
|
+
/** Operational state and executable extensions are foreign to an observational authoring snapshot. */
|
|
7
|
+
export const REPAIR_EXCLUDED_STATE = { runtime: 'foreign operational state', locks: 'peer ownership', extensions: 'executable state', transactions: 'foreign journals', checkpoints: 'foreign recovery state', search: 'derived indexes' };
|
|
8
|
+
/** Resolve excluded root entries once for snapshot copying and fingerprints. */
|
|
9
|
+
const SKIPPED = new Set(Object.keys(REPAIR_EXCLUDED_STATE));
|
|
10
|
+
/** Fingerprint regular source files and names without creating SDK caches in the source tracker. */
|
|
11
|
+
async function fingerprint(root) {
|
|
12
|
+
const hash = createHash('sha256');
|
|
13
|
+
/** Recurse only inside the selected tracker and refuse links or special files. */
|
|
14
|
+
async function visit(directory) {
|
|
15
|
+
for (const entry of (await readdir(directory, { withFileTypes: true })).sort(/** Stable source ordering makes copy checks deterministic. */ (a, b) => a.name.localeCompare(b.name))) {
|
|
16
|
+
const path = join(directory, entry.name);
|
|
17
|
+
if (SKIPPED.has(relative(root, path).split(/[\\/]/)[0]))
|
|
18
|
+
continue;
|
|
19
|
+
if (entry.isDirectory())
|
|
20
|
+
await visit(path);
|
|
21
|
+
else if (entry.isFile()) {
|
|
22
|
+
const bytes = await readFile(path);
|
|
23
|
+
hash.update(JSON.stringify([relative(root, path), bytes.length]));
|
|
24
|
+
hash.update(bytes);
|
|
25
|
+
}
|
|
26
|
+
else
|
|
27
|
+
throw new RepairRefusal('unsafe_repair', 'Repair context requires regular tracker files.', 'Replace symbolic links or special files with ordinary tracker files before requesting a preview.');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
await visit(root);
|
|
31
|
+
return hash.digest('hex');
|
|
32
|
+
}
|
|
33
|
+
/** Isolate SDK read-side caches, validate copy stability and discard only this invocation's own scratch directory. */
|
|
34
|
+
export async function withRepairSnapshot(source, read, onSnapshotRead) {
|
|
35
|
+
const before = await fingerprint(source);
|
|
36
|
+
const scratch = await mkdtemp(join(tmpdir(), 'pm-lint-repair-'));
|
|
37
|
+
try {
|
|
38
|
+
const snapshot = join(scratch, 'tracker');
|
|
39
|
+
await onSnapshotRead?.();
|
|
40
|
+
await cp(source, snapshot, { recursive: true, dereference: false, filter: /** Foreign state is never followed or copied. */ /** Foreign state is never followed or copied. */ path => !SKIPPED.has(relative(source, path).split(/[\\/]/)[0]) });
|
|
41
|
+
if (await fingerprint(snapshot) !== before)
|
|
42
|
+
throw changed();
|
|
43
|
+
await writableDirectories(snapshot);
|
|
44
|
+
const result = await read(snapshot);
|
|
45
|
+
if (await fingerprint(source) !== before)
|
|
46
|
+
throw changed();
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
await writableDirectories(scratch);
|
|
51
|
+
await rm(scratch, { recursive: true, force: true });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** A changed source invalidates the whole single-target receipt, including its substantive questions. */
|
|
55
|
+
function changed() {
|
|
56
|
+
return new RepairRefusal('stale_revision', 'Tracker bytes changed during the repair preview.', 'Request a fresh preview; review its current revision before using any patch.');
|
|
57
|
+
}
|
|
58
|
+
/** Source permissions stay untouched; owned scratch directories must allow SDK writes and reliable cleanup. */
|
|
59
|
+
async function writableDirectories(root) {
|
|
60
|
+
await chmod(root, 0o700);
|
|
61
|
+
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
62
|
+
if (entry.isDirectory())
|
|
63
|
+
await writableDirectories(join(root, entry.name));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=repair-snapshot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repair-snapshot.js","sourceRoot":"","sources":["../../src/repair-snapshot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC7E,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,sGAAsG;AACtG,MAAM,CAAC,MAAM,qBAAqB,GAAqC,EAAE,OAAO,EAAE,2BAA2B,EAAE,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,kBAAkB,EAAE,YAAY,EAAE,kBAAkB,EAAE,WAAW,EAAE,wBAAwB,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC;AAE7Q,gFAAgF;AAChF,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC,CAAC;AAE5D,oGAAoG;AACpG,KAAK,UAAU,WAAW,CAAC,IAAY;IACrC,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IAClC,kFAAkF;IAClF,KAAK,UAAU,KAAK,CAAC,SAAiB;QACpC,KAAK,MAAM,KAAK,IAAI,CAAC,MAAM,OAAO,CAAC,SAAS,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,8DAA8D,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,C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AAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAC7E,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ItemMetadata, ItemMutationPreviewValidation } from '@unbrained/pm-cli/sdk';
|
|
2
|
+
import type { RuleCode } from './types.ts';
|
|
3
|
+
/** Actionable refusal categories; a refusal never contains a usable patch. */
|
|
4
|
+
export type RepairRefusalCode = 'invalid_request' | 'unknown_item' | 'stale_revision' | 'limit_exceeded' | 'unsafe_repair';
|
|
5
|
+
/** Typed refusal shared by embedded callers and the host renderer. */
|
|
6
|
+
export declare class RepairRefusal extends Error {
|
|
7
|
+
/** Stable machine-readable category. */
|
|
8
|
+
readonly code: RepairRefusalCode;
|
|
9
|
+
/** Concrete recovery action for the caller. */
|
|
10
|
+
readonly remedy: string;
|
|
11
|
+
/** Preserve a stable machine code and a concrete next action without exposing source paths. */
|
|
12
|
+
constructor(code: RepairRefusalCode, message: string, remedy: string);
|
|
13
|
+
}
|
|
14
|
+
/** Single-target request; resubmitting observed evidence requires the exact source revision. */
|
|
15
|
+
export interface RepairRequest {
|
|
16
|
+
id: string;
|
|
17
|
+
revision?: string;
|
|
18
|
+
}
|
|
19
|
+
/** Mechanical edits only; substantive metadata and relationships cannot be authored here. */
|
|
20
|
+
export interface RepairPatch {
|
|
21
|
+
title?: string;
|
|
22
|
+
tags?: string[];
|
|
23
|
+
}
|
|
24
|
+
/** A concrete surface defect explained without copying unrelated tracker text. */
|
|
25
|
+
export interface MechanicalFinding {
|
|
26
|
+
code: 'title_whitespace' | 'tag_normalization';
|
|
27
|
+
field: 'title' | 'tags';
|
|
28
|
+
}
|
|
29
|
+
/** Explicit substantive question, never a proposed answer or acceptance criterion. */
|
|
30
|
+
export interface RepairQuestion {
|
|
31
|
+
code: RuleCode | 'missing_expected_behavior';
|
|
32
|
+
field: string;
|
|
33
|
+
question: string;
|
|
34
|
+
}
|
|
35
|
+
/** Compact observational receipt; patches require fresh application-time revision checks by the caller. */
|
|
36
|
+
export interface RepairPreview {
|
|
37
|
+
version: 1;
|
|
38
|
+
id: string;
|
|
39
|
+
revision: string;
|
|
40
|
+
plan_key: string;
|
|
41
|
+
corpus_count: number;
|
|
42
|
+
writes: 0;
|
|
43
|
+
findings: MechanicalFinding[];
|
|
44
|
+
patch: RepairPatch;
|
|
45
|
+
questions: RepairQuestion[];
|
|
46
|
+
validation: ItemMutationPreviewValidation | null;
|
|
47
|
+
remaining_mechanical: number;
|
|
48
|
+
/** Foreign operational state is excluded without reading, replaying or deleting it. */
|
|
49
|
+
excluded_state: Readonly<Record<string, string>>;
|
|
50
|
+
}
|
|
51
|
+
/** Reuse core import normalization for the two explicitly supported mechanical surfaces. */
|
|
52
|
+
export declare function mechanicalRepairs(item: ItemMetadata, originalTags?: unknown): {
|
|
53
|
+
findings: MechanicalFinding[];
|
|
54
|
+
patch: RepairPatch;
|
|
55
|
+
};
|
|
56
|
+
/** Read boundaries available to deterministic interleaving tests and observational instrumentation. */
|
|
57
|
+
export type RepairBoundary = 'snapshot_read' | 'settings_read' | 'source_read' | 'planned' | 'validated' | 'source_rechecked';
|
|
58
|
+
/** Optional observational instrumentation; it cannot supply or replace tracker state. */
|
|
59
|
+
export interface RepairPreviewOptions {
|
|
60
|
+
onBoundary?: (boundary: RepairBoundary) => Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
/** Assemble fresh single-item repair context and validate through core without writing source state. */
|
|
63
|
+
export declare function previewAuthoringRepairs(pmRoot: string, request: RepairRequest, options?: RepairPreviewOptions): Promise<RepairPreview>;
|
|
64
|
+
//# sourceMappingURL=repairs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repairs.d.ts","sourceRoot":"","sources":["../../src/repairs.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,6BAA6B,EAAoC,MAAM,uBAAuB,CAAC;AAK3H,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM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E,cAAc,EAAE,CAAC;IAC5B,UAAU,EAAE,6BAA6B,GAAG,IAAI,CAAC;IACjD,oBAAoB,EAAE,MAAM,CAAC;IAC7B,uFAAuF;IACvF,cAAc,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAClD;AAgBD,4FAA4F;AAC5F,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,YAAY,EAAE,YAAY,GAAE,OAAmB,GAAG;IAAE,QAAQ,EAAE,iBAAiB,EAAE,CAAC;IAAC,KAAK,EAAE,WAAW,CAAA;CAAE,CAc9I;AAeD,uGAAuG;AACvG,MAAM,MAAM,cAAc,GAAG,eAAe,GAAG,eAAe,GAAG,aAAa,GAAG,SAAS,GAAG,WAAW,GAAG,kBAAkB,CAAC;AAE9H,yFAAyF;AACzF,MAAM,WAAW,oBAAoB;IACnC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D;AAED,wGAAwG;AACxG,wBAAsB,uBAAuB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,GAAE,oBAAyB,GAAG,OAAO,CAAC,aAAa,CAAC,CAGhJ"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { decode } from '@toon-format/toon';
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
3
|
+
import { relative, resolve, sep } from 'node:path';
|
|
4
|
+
import { PmClient, certifyCompleteListResult, getSettingsPath, loadGuardedRuntimeSchema, locateItem, readSettings, resolveItemTypeRegistry, splitFrontMatter, planItemMutationDocument, previewItemMutations, readFileIfExists, toImportTags, toNonEmptyImportString } from '@unbrained/pm-cli/sdk';
|
|
5
|
+
import { REPAIR_EXCLUDED_STATE, withRepairSnapshot } from './repair-snapshot.js';
|
|
6
|
+
import { sourceDigest } from './baseline.js';
|
|
7
|
+
import { lintCorpus } from './engine.js';
|
|
8
|
+
import { lintSettingsFromText, resolveConfig } from './settings.js';
|
|
9
|
+
/** Typed refusal shared by embedded callers and the host renderer. */
|
|
10
|
+
export class RepairRefusal extends Error {
|
|
11
|
+
/** Stable machine-readable category. */
|
|
12
|
+
code;
|
|
13
|
+
/** Concrete recovery action for the caller. */
|
|
14
|
+
remedy;
|
|
15
|
+
/** Preserve a stable machine code and a concrete next action without exposing source paths. */
|
|
16
|
+
constructor(code, message, remedy) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = 'RepairRefusal';
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.remedy = remedy;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Stable author questions for existing lint diagnostics; no text is invented for the item. */
|
|
24
|
+
const QUESTIONS = {
|
|
25
|
+
PML001: 'What concrete action and scope should the title name?',
|
|
26
|
+
PML002: 'What observable expected behavior proves this work is complete?',
|
|
27
|
+
PML003: 'What measurable outcome should replace the vague criterion?',
|
|
28
|
+
PML004: 'What observed result or evidence supports closure?',
|
|
29
|
+
PML005: 'What is the current work state, and should the claim remain active?',
|
|
30
|
+
PML006: 'Which implementation files or reproducible tests support this feature?',
|
|
31
|
+
PML007: 'Which concrete child item belongs to this epic?',
|
|
32
|
+
PML008: 'What context, constraints or evidence should supplement the title?',
|
|
33
|
+
PML009: 'What blocks this work, and which current item identifies the blocker?',
|
|
34
|
+
PML010: 'How does this work differ from the duplicate candidates, or which item is canonical?',
|
|
35
|
+
};
|
|
36
|
+
/** Reuse core import normalization for the two explicitly supported mechanical surfaces. */
|
|
37
|
+
export function mechanicalRepairs(item, originalTags = item.tags) {
|
|
38
|
+
const findings = [];
|
|
39
|
+
const patch = {};
|
|
40
|
+
const title = toNonEmptyImportString(item.title);
|
|
41
|
+
if (title !== undefined && title !== item.title) {
|
|
42
|
+
findings.push({ code: 'title_whitespace', field: 'title' });
|
|
43
|
+
patch.title = title;
|
|
44
|
+
}
|
|
45
|
+
const tags = toImportTags(item.tags);
|
|
46
|
+
if (JSON.stringify(tags) !== JSON.stringify(originalTags)) {
|
|
47
|
+
findings.push({ code: 'tag_normalization', field: 'tags' });
|
|
48
|
+
patch.tags = tags;
|
|
49
|
+
}
|
|
50
|
+
return { findings, patch };
|
|
51
|
+
}
|
|
52
|
+
/** Refuse malformed JavaScript requests as strictly as CLI flags. */
|
|
53
|
+
function assertRequest(request) {
|
|
54
|
+
if (!request || typeof request.id !== 'string' || !/^[a-z0-9]+(?:-[a-z0-9]+)+$/i.test(request.id) || Object.keys(request).some(/** Unknown controls cannot silently enable application. */ /** Unknown controls cannot silently enable application. */ key => !['id', 'revision'].includes(key)) || (request.revision !== undefined && (typeof request.revision !== 'string' || !/^[a-f0-9]{64}$/.test(request.revision)))) {
|
|
55
|
+
throw new RepairRefusal('invalid_request', 'One canonical item id and an optional SHA-256 revision are required.', 'Supply an item id; reuse the revision from a fresh repair preview when checking saved context.');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** Hash all certified context so concurrent peer edits cannot silently change relationship-dependent questions. */
|
|
59
|
+
function contextRevision(source, settings) {
|
|
60
|
+
const items = certifyCompleteListResult(source).items.map(/** Bind all native fields, not just the proposed patch content. */ /** Bind all native fields, not just the proposed patch content. */ item => [item.id, sourceDigest(item)]).sort(/** Canonical ids make scan ordering irrelevant. */ (left, right) => left[0].localeCompare(right[0]));
|
|
61
|
+
return createHash('sha256').update(JSON.stringify([items, settings])).digest('hex');
|
|
62
|
+
}
|
|
63
|
+
/** Assemble fresh single-item repair context and validate through core without writing source state. */
|
|
64
|
+
export async function previewAuthoringRepairs(pmRoot, request, options = {}) {
|
|
65
|
+
assertRequest(request);
|
|
66
|
+
return withRepairSnapshot(pmRoot, /** No SDK read or disposable validation can write into the source tracker. */ /** No SDK read or disposable validation can write into the source tracker. */ snapshot => buildRepairPreview(snapshot, request, options), options.onBoundary === undefined ? undefined : /** Surface the copy boundary without exposing scratch paths. */ () => options.onBoundary('snapshot_read'));
|
|
67
|
+
}
|
|
68
|
+
/** Read and validate exclusively in an owned disposable snapshot. */
|
|
69
|
+
async function buildRepairPreview(pmRoot, request, options) {
|
|
70
|
+
const client = new PmClient({ pmRoot, noExtensions: true });
|
|
71
|
+
const settings = await readFileIfExists(getSettingsPath(pmRoot));
|
|
72
|
+
await options.onBoundary?.('settings_read');
|
|
73
|
+
const config = resolveConfig(lintSettingsFromText(settings));
|
|
74
|
+
// Guard references before readSettings can bootstrap schema files outside the disposable root.
|
|
75
|
+
const schema = JSON.parse(settings).schema;
|
|
76
|
+
const schemaFiles = Object.values(schema?.files ?? {});
|
|
77
|
+
if (schemaFiles.some(/** Excluded operational state cannot silently replace referenced schema with defaults. */ /** Excluded operational state cannot silently replace referenced schema with defaults. */ file => typeof file !== 'string' || Object.hasOwn(REPAIR_EXCLUDED_STATE, relative(pmRoot, resolve(pmRoot, file)).split(sep)[0]))) {
|
|
78
|
+
throw new RepairRefusal('unsafe_repair', 'Schema configuration must reference retained tracker files.', 'Move referenced schema files under the tracker schema directory before requesting a preview.');
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
await loadGuardedRuntimeSchema(pmRoot, schema);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
throw new RepairRefusal('unsafe_repair', 'Schema references must stay inside the repair snapshot.', 'Use regular tracker-relative schema files before requesting a preview.');
|
|
85
|
+
}
|
|
86
|
+
const nativeSettings = await readSettings(pmRoot);
|
|
87
|
+
const registry = resolveItemTypeRegistry(nativeSettings);
|
|
88
|
+
// Preserve pre-migration bytes: the pinned SDK may canonicalize legacy Markdown while reading.
|
|
89
|
+
const located = await locateItem(pmRoot, request.id, nativeSettings.id_prefix, nativeSettings.item_format, registry.type_to_folder);
|
|
90
|
+
const raw = located === null ? null : await readFileIfExists(located.itemPath);
|
|
91
|
+
const source = await client.listAllComplete();
|
|
92
|
+
const corpus = certifyCompleteListResult(source);
|
|
93
|
+
const item = corpus.items.find(/** Only canonical identities are accepted. */ /** Only canonical identities are accepted. */ row => row.id === request.id);
|
|
94
|
+
if (item === undefined)
|
|
95
|
+
throw new RepairRefusal('unknown_item', 'The requested item is absent from the certified tracker.', 'Select a current item id from pm list and request a new preview.');
|
|
96
|
+
await options.onBoundary?.('source_read');
|
|
97
|
+
const revision = createHash('sha256').update(JSON.stringify([sourceDigest(item), raw])).digest('hex');
|
|
98
|
+
if (request.revision !== undefined && request.revision !== revision)
|
|
99
|
+
throw staleRevision();
|
|
100
|
+
const context = contextRevision(source, settings);
|
|
101
|
+
const original = located.item_format === 'toon' ? decode(raw) : JSON.parse(splitFrontMatter(raw).frontMatter);
|
|
102
|
+
const { findings, patch } = mechanicalRepairs(item, original.tags);
|
|
103
|
+
if (Buffer.byteLength(JSON.stringify(patch)) > 16384)
|
|
104
|
+
throw new RepairRefusal('limit_exceeded', 'The mechanical patch exceeds 16 KiB.', 'Review this item directly with pm get --full; repair the oversized field explicitly.');
|
|
105
|
+
const questions = lintCorpus(source, config, { id: [item.id] }).findings.map(/** Existing diagnostics become questions without generating answers. */ /** Existing diagnostics become questions without generating answers. */ finding => ({ code: finding.rule, field: finding.field, question: QUESTIONS[finding.rule] }));
|
|
106
|
+
if (item.type === 'Issue' && toNonEmptyImportString(item.expected_result) === undefined)
|
|
107
|
+
questions.push({ code: 'missing_expected_behavior', field: 'expected_result', question: 'What should happen for the reported reproduction steps?' });
|
|
108
|
+
const planKey = createHash('sha256').update(JSON.stringify([1, revision, context, patch])).digest('hex');
|
|
109
|
+
let validation = null;
|
|
110
|
+
if (findings.length > 0) {
|
|
111
|
+
try {
|
|
112
|
+
const plan = await planItemMutationDocument({ pmRoot, transactionId: `lint-repair-${planKey}`, input: JSON.stringify([{ op: 'update', id: item.id, options: { ...patch, ...(patch.tags === undefined ? {} : { tags: JSON.stringify(patch.tags) }) } }]) });
|
|
113
|
+
await options.onBoundary?.('planned');
|
|
114
|
+
// A fresh disposable validation identity avoids replaying a source journal, including foreign records.
|
|
115
|
+
validation = await previewItemMutations({ pmRoot, author: 'lint-preview', transactionId: `lint-preview-${randomUUID()}`, mutations: plan.mutations });
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
throw new RepairRefusal('unsafe_repair', 'Core could not freshly validate the proposed repair.', 'Run pm validate and inspect ownership/schema constraints; refresh the preview after resolving them.');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
await options.onBoundary?.('validated');
|
|
122
|
+
await options.onBoundary?.('source_rechecked');
|
|
123
|
+
return { version: 1, id: item.id, revision, plan_key: planKey, corpus_count: corpus.items.length, writes: 0, findings, patch, questions, validation, excluded_state: { ...REPAIR_EXCLUDED_STATE }, remaining_mechanical: mechanicalRepairs({ ...item, ...patch }).findings.length };
|
|
124
|
+
}
|
|
125
|
+
/** Explain stale evidence without ever returning a frozen patch for reuse. */
|
|
126
|
+
function staleRevision() {
|
|
127
|
+
return new RepairRefusal('stale_revision', 'The source revision or preview context changed.', 'Read a new preview and review its revision; never apply the saved patch against a changed target.');
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=repairs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repairs.js","sourceRoot":"","sources":["../../src/repairs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,yBAAyB,EAAE,eAAe,EAAE,wBAAwB,EAAE,UAAU,EAAE,YAAY,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAEpS,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACjF,OAAO,EAAE,YAAY,EAAE,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AE,iDAAiD,EAAE,mGAAmG,CAAC,CAAC;AACrM,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CommandHandlerContext, ExtensionApi } from '@unbrained/pm-cli/sdk/authoring';
|
|
2
|
+
/** Render bounded diagnostics and typed refusals through the host's TOON/JSON transport. */
|
|
3
|
+
export declare function runRulePackCommand(ctx: CommandHandlerContext): Promise<Record<string, unknown>>;
|
|
4
|
+
/** Preserve infrastructure failures while rendering actionable pack refusals for agents. */
|
|
5
|
+
export declare function rulePackRefusalReceipt(error: unknown): Record<string, unknown>;
|
|
6
|
+
/** Register an observational command with host budgeting and no git dependency. */
|
|
7
|
+
export declare function registerRulePackCommand(api: ExtensionApi): void;
|
|
8
|
+
//# sourceMappingURL=rule-pack-command.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-pack-command.d.ts","sourceRoot":"","sources":["../../src/rule-pack-command.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAG3F,4FAA4F;AAC5F,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,qBAAqB,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAWrG;AAED,4FAA4F;AAC5F,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAI9E;AAED,mFAAmF;AACnF,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,YAAY,GAAG,IAAI,CAE/D"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { PmClient, defineCommand } from '@unbrained/pm-cli/sdk';
|
|
3
|
+
import { lintRulePack, readRulePack, RulePackRefusal } from './rule-pack.js';
|
|
4
|
+
/** Render bounded diagnostics and typed refusals through the host's TOON/JSON transport. */
|
|
5
|
+
export async function runRulePackCommand(ctx) {
|
|
6
|
+
try {
|
|
7
|
+
if (typeof ctx.options.pack !== 'string' || ctx.options.pack.trim() === '')
|
|
8
|
+
throw new RulePackRefusal('invalid_pack', 'One --pack JSON file is required.', 'Supply --pack authoring-rules.json using the documented version 1 contract.');
|
|
9
|
+
const root = ctx.source_workspace_root ?? ctx.repo_root;
|
|
10
|
+
const pack = await readRulePack(resolve(root ?? '.', ctx.options.pack));
|
|
11
|
+
const client = new PmClient({ cwd: root, pmRoot: ctx.pm_root, author: ctx.global.author, noExtensions: true });
|
|
12
|
+
const { exit_code, ...result } = await lintRulePack(client, pack);
|
|
13
|
+
return { ...result, ...(exit_code === 0 ? {} : { exit_code }) };
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return rulePackRefusalReceipt(error);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** Preserve infrastructure failures while rendering actionable pack refusals for agents. */
|
|
20
|
+
export function rulePackRefusalReceipt(error) {
|
|
21
|
+
if (!(error instanceof RulePackRefusal))
|
|
22
|
+
throw error;
|
|
23
|
+
const refusal = { code: error.code, message: error.message, remedy: error.remedy };
|
|
24
|
+
return { ok: false, refusal, count: 0, findings: [], exit_code: 2 };
|
|
25
|
+
}
|
|
26
|
+
/** Register an observational command with host budgeting and no git dependency. */
|
|
27
|
+
export function registerRulePackCommand(api) {
|
|
28
|
+
api.registerCommand(defineCommand({ name: 'lint pack', description: 'Check organization authoring rules from a bounded version 1 JSON pack', intent: 'Apply portable semantic authoring checks without executing plugins', read_output: { row_keys: ['findings'] }, flags: [{ long: '--pack', value_name: 'file', value_type: 'string', description: 'Required version 1 declarative authoring rule pack' }], examples: ['pm lint pack --pack authoring-rules.json --json'], run: runRulePackCommand }));
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=rule-pack-command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-pack-command.js","sourceRoot":"","sources":["../../src/rule-pack-command.ts"],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AACH,CAAC;AAED,4FAA4F;AAC5F,MAAM,UAAU,sBAAsB,CAAC,KAAc;IACnD,IAAI,CAAC,CAAC,KAAK,YAAY,eAAe,CAAC;QAAE,MAAM,KAAK,CAAC;IACrD,MAAM,OAAO,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;IACnF,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;AACtE,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,uBAAuB,CAAC,GAAiB;IACvD,GAAG,CAAC,eAAe,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,WAAW,EAAE,uEAAuE,EAAE,MAAM,EAAE,oEAAoE,EAAE,WAAW,EAAE,EAAE,QAAQ,EAAE,CAAC,UAAU,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,oDAAoD,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,iDAAiD,CAAC,EAAE,GAAG,EAAE,kBAAkB,EAAE,CAAC,CAAC,CAAC;AAC3e,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { PmClient, PmCompleteListCertificate } from '@unbrained/pm-cli/sdk';
|
|
2
|
+
import type { Severity } from './types.ts';
|
|
3
|
+
/** Version 1 text fields retain core's canonical metadata spelling and types. */
|
|
4
|
+
export declare const RULE_PACK_FIELDS: readonly ['title', 'description', 'acceptance_criteria', 'definition_of_ready', 'resolution', 'blocked_reason', 'outcome', 'value', 'why_now'];
|
|
5
|
+
/** Literal semantic checks deliberately exclude regex, glob, recursion and executable code. */
|
|
6
|
+
export type AuthoringPredicate = {
|
|
7
|
+
op: 'present';
|
|
8
|
+
} | {
|
|
9
|
+
op: 'contains' | 'not_contains' | 'starts_with';
|
|
10
|
+
value: string;
|
|
11
|
+
} | {
|
|
12
|
+
op: 'min_length' | 'max_length';
|
|
13
|
+
value: number;
|
|
14
|
+
};
|
|
15
|
+
/** Organization-owned assertion; all selectors are exact, case-sensitive OR lists, ANDed together. */
|
|
16
|
+
export interface AuthoringRule {
|
|
17
|
+
id: string;
|
|
18
|
+
field: typeof RULE_PACK_FIELDS[number];
|
|
19
|
+
predicate: AuthoringPredicate;
|
|
20
|
+
severity: Severity;
|
|
21
|
+
explanation: string;
|
|
22
|
+
remedy: string;
|
|
23
|
+
when?: {
|
|
24
|
+
statuses?: string[];
|
|
25
|
+
types?: string[];
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Portable, strictly parsed pack with no implicit settings or execution hooks. */
|
|
29
|
+
export interface RulePack {
|
|
30
|
+
version: 1;
|
|
31
|
+
rules: AuthoringRule[];
|
|
32
|
+
}
|
|
33
|
+
/** Stable refusal categories distinguish contract, resource and I/O remedies. */
|
|
34
|
+
export type RulePackRefusalCode = 'invalid_pack' | 'unsafe_pattern' | 'limit_exceeded' | 'pack_io';
|
|
35
|
+
/** Fail closed with a bounded explanation that does not echo source content or paths. */
|
|
36
|
+
export declare class RulePackRefusal extends Error {
|
|
37
|
+
/** Stable category shared by CLI and SDK consumers. */
|
|
38
|
+
readonly code: RulePackRefusalCode;
|
|
39
|
+
/** Concrete next action for recovering from the refusal. */
|
|
40
|
+
readonly remedy: string;
|
|
41
|
+
/** Preserve a machine-readable category and next action for every transport. */
|
|
42
|
+
constructor(code: RulePackRefusalCode, message: string, remedy: string);
|
|
43
|
+
}
|
|
44
|
+
/** Hard ceilings apply to SDK input, file reads and evaluation; no silent truncation. */
|
|
45
|
+
export declare const RULE_PACK_LIMITS: {
|
|
46
|
+
readonly bytes: 65536;
|
|
47
|
+
readonly rules: 64;
|
|
48
|
+
readonly literal: 512;
|
|
49
|
+
readonly text: 65536;
|
|
50
|
+
readonly checks: 100000;
|
|
51
|
+
readonly findings: 1000;
|
|
52
|
+
};
|
|
53
|
+
/** Validate and detach untrusted SDK input before any tracker read. */
|
|
54
|
+
export declare function parseRulePack(value: unknown): RulePack;
|
|
55
|
+
/** Read at most one bounded regular JSON file; special files never become blocking streams. */
|
|
56
|
+
export declare function readRulePack(path: string): Promise<RulePack>;
|
|
57
|
+
/** One compact diagnostic carries the organization's explanation and concrete correction. */
|
|
58
|
+
export interface RulePackFinding {
|
|
59
|
+
item_id: string;
|
|
60
|
+
rule: string;
|
|
61
|
+
field: AuthoringRule['field'];
|
|
62
|
+
severity: Severity;
|
|
63
|
+
explanation: string;
|
|
64
|
+
remedy: string;
|
|
65
|
+
}
|
|
66
|
+
/** Complete receipt retains counts and certification without copying tracker text. */
|
|
67
|
+
export interface RulePackResult {
|
|
68
|
+
version: 1;
|
|
69
|
+
findings: RulePackFinding[];
|
|
70
|
+
count: number;
|
|
71
|
+
corpus_count: number;
|
|
72
|
+
checks: number;
|
|
73
|
+
summary: Record<Severity, number>;
|
|
74
|
+
complete_list: PmCompleteListCertificate;
|
|
75
|
+
exit_code: number;
|
|
76
|
+
}
|
|
77
|
+
/** Use core's certified full reader so filesystem and remote SDK adapters share semantics. */
|
|
78
|
+
export declare function lintRulePack(client: Pick<PmClient, 'listAllComplete'>, input: RulePack): Promise<RulePackResult>;
|
|
79
|
+
//# sourceMappingURL=rule-pack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-pack.d.ts","sourceRoot":"","sources":["../../src/rule-pack.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AACjF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,iFAAiF;AACjF,eAAO,MAAM,gBAAgB,YAAI,OAAO,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certifyCompleteListResult } from '@unbrained/pm-cli/sdk';
|
|
4
|
+
/** Version 1 text fields retain core's canonical metadata spelling and types. */
|
|
5
|
+
export const RULE_PACK_FIELDS = ['title', 'description', 'acceptance_criteria', 'definition_of_ready', 'resolution', 'blocked_reason', 'outcome', 'value', 'why_now'];
|
|
6
|
+
/** Fail closed with a bounded explanation that does not echo source content or paths. */
|
|
7
|
+
export class RulePackRefusal extends Error {
|
|
8
|
+
/** Stable category shared by CLI and SDK consumers. */
|
|
9
|
+
code;
|
|
10
|
+
/** Concrete next action for recovering from the refusal. */
|
|
11
|
+
remedy;
|
|
12
|
+
/** Preserve a machine-readable category and next action for every transport. */
|
|
13
|
+
constructor(code, message, remedy) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = 'RulePackRefusal';
|
|
16
|
+
this.code = code;
|
|
17
|
+
this.remedy = remedy;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Hard ceilings apply to SDK input, file reads and evaluation; no silent truncation. */
|
|
21
|
+
export const RULE_PACK_LIMITS = { bytes: 65536, rules: 64, literal: 512, text: 65536, checks: 100000, findings: 1000 };
|
|
22
|
+
/** Reject malformed values at a contract coordinate without disclosing their contents. */
|
|
23
|
+
function invalid(path) {
|
|
24
|
+
throw new RulePackRefusal('invalid_pack', `Invalid version 1 rule pack at ${path}; check required fields, unknown fields and value types.`, 'Use the documented version 1 keys, supported text fields, unique rule ids and literal predicates.');
|
|
25
|
+
}
|
|
26
|
+
/** Bound resources independently of well-formedness, preserving the failed dimension. */
|
|
27
|
+
function bound(ok, dimension) {
|
|
28
|
+
if (!ok)
|
|
29
|
+
throw new RulePackRefusal('limit_exceeded', `Rule pack ${dimension} limit exceeded.`, 'Reduce the pack or applicable rule scope to the documented ceilings; never truncate a certified corpus.');
|
|
30
|
+
}
|
|
31
|
+
/** Validate exact own keys before reading a JSON object's values. */
|
|
32
|
+
function object(value, required, optional, path) {
|
|
33
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value) || !required.every(/** Required keys must be explicit. */ /** Required keys must be explicit. */ key => Object.hasOwn(value, key)))
|
|
34
|
+
invalid(path);
|
|
35
|
+
if (Object.keys(value).some(/** Refuse all undeclared options. */ /** Refuse all undeclared options. */ key => !required.includes(key) && !optional.includes(key)))
|
|
36
|
+
throw new RulePackRefusal('invalid_pack', `Unknown field in ${path}.`, 'Remove undeclared fields at this contract coordinate; use only documented version 1 keys.');
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
/** Validate bounded nonblank strings without changing literal semantics. */
|
|
40
|
+
function text(value, limit, path) {
|
|
41
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
42
|
+
invalid(path);
|
|
43
|
+
bound(value.length <= limit, path);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
/** Parse one non-executable predicate, refusing regex and wildcard modes explicitly. */
|
|
47
|
+
function predicate(value, path) {
|
|
48
|
+
const input = object(value, ['op'], ['value'], path);
|
|
49
|
+
if (['regex', 'matches', 'glob', 'expression'].includes(input.op))
|
|
50
|
+
throw new RulePackRefusal('unsafe_pattern', `Executable or unbounded pattern at ${path}.`, 'Use contains, not_contains or starts_with with a literal string; regex, globs and expressions are unsupported.');
|
|
51
|
+
if (input.op === 'present') {
|
|
52
|
+
if (Object.hasOwn(input, 'value'))
|
|
53
|
+
invalid(path);
|
|
54
|
+
return { op: 'present' };
|
|
55
|
+
}
|
|
56
|
+
if (input.op === 'contains' || input.op === 'not_contains' || input.op === 'starts_with')
|
|
57
|
+
return { op: input.op, value: text(input.value, RULE_PACK_LIMITS.literal, path) };
|
|
58
|
+
if (input.op !== 'min_length' && input.op !== 'max_length')
|
|
59
|
+
invalid(path);
|
|
60
|
+
if (typeof input.value !== 'number' || !Number.isInteger(input.value) || input.value < 0)
|
|
61
|
+
invalid(path);
|
|
62
|
+
bound(input.value <= RULE_PACK_LIMITS.text, path);
|
|
63
|
+
return { op: input.op, value: input.value };
|
|
64
|
+
}
|
|
65
|
+
/** Parse bounded exact selectors, including custom statuses and item types. */
|
|
66
|
+
function selectors(value, path) {
|
|
67
|
+
const input = object(value, [], ['statuses', 'types'], path);
|
|
68
|
+
const result = {};
|
|
69
|
+
for (const key of ['statuses', 'types']) {
|
|
70
|
+
if (!Object.hasOwn(input, key))
|
|
71
|
+
continue;
|
|
72
|
+
const entries = input[key];
|
|
73
|
+
if (!Array.isArray(entries) || entries.length === 0)
|
|
74
|
+
invalid(`${path}.${key}`);
|
|
75
|
+
bound(entries.length <= 32, `${path}.${key}`);
|
|
76
|
+
result[key] = entries.map(/** Selector strings are literal, bounded identifiers. */ /** Selector strings are literal, bounded identifiers. */ entry => text(entry, 64, `${path}.${key}`));
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
/** Validate and detach untrusted SDK input before any tracker read. */
|
|
81
|
+
export function parseRulePack(value) {
|
|
82
|
+
const input = object(value, ['version', 'rules'], [], 'pack');
|
|
83
|
+
if (input.version !== 1 || !Array.isArray(input.rules) || input.rules.length === 0)
|
|
84
|
+
invalid('pack');
|
|
85
|
+
bound(input.rules.length <= RULE_PACK_LIMITS.rules, 'rules');
|
|
86
|
+
const seen = new Set();
|
|
87
|
+
const rules = input.rules.map(/** Each rule is rebuilt from validated scalar values. */ (value, index) => {
|
|
88
|
+
const path = `rules[${index}]`;
|
|
89
|
+
const rule = object(value, ['id', 'field', 'predicate', 'severity', 'explanation', 'remedy'], ['when'], path);
|
|
90
|
+
const id = text(rule.id, 64, `${path}.id`);
|
|
91
|
+
if (id !== id.trim() || !/^[A-Za-z][A-Za-z0-9_.-]*$/.test(id) || seen.has(id))
|
|
92
|
+
invalid(`${path}.id`);
|
|
93
|
+
seen.add(id);
|
|
94
|
+
if (!RULE_PACK_FIELDS.includes(rule.field))
|
|
95
|
+
invalid(`${path}.field`);
|
|
96
|
+
if (!['info', 'warn', 'error'].includes(rule.severity))
|
|
97
|
+
invalid(`${path}.severity`);
|
|
98
|
+
return { id, field: rule.field, predicate: predicate(rule.predicate, `${path}.predicate`), severity: rule.severity, explanation: text(rule.explanation, 256, `${path}.explanation`), remedy: text(rule.remedy, 256, `${path}.remedy`), ...(Object.hasOwn(rule, 'when') ? { when: selectors(rule.when, `${path}.when`) } : {}) };
|
|
99
|
+
});
|
|
100
|
+
const pack = { version: 1, rules };
|
|
101
|
+
bound(Buffer.byteLength(JSON.stringify(pack)) <= RULE_PACK_LIMITS.bytes, 'bytes');
|
|
102
|
+
return pack;
|
|
103
|
+
}
|
|
104
|
+
/** Read at most one bounded regular JSON file; special files never become blocking streams. */
|
|
105
|
+
export async function readRulePack(path) {
|
|
106
|
+
let value;
|
|
107
|
+
try {
|
|
108
|
+
// O_NONBLOCK prevents FIFO opens from waiting for a writer; stat rejects special files.
|
|
109
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
110
|
+
try {
|
|
111
|
+
const stat = await handle.stat();
|
|
112
|
+
if (!stat.isFile())
|
|
113
|
+
throw new Error('regular file required');
|
|
114
|
+
const buffer = Buffer.alloc(RULE_PACK_LIMITS.bytes + 1);
|
|
115
|
+
let used = 0;
|
|
116
|
+
while (used < buffer.length) {
|
|
117
|
+
const { bytesRead } = await handle.read(buffer, used, buffer.length - used, null);
|
|
118
|
+
if (bytesRead === 0)
|
|
119
|
+
break;
|
|
120
|
+
used += bytesRead;
|
|
121
|
+
}
|
|
122
|
+
bound(used <= RULE_PACK_LIMITS.bytes, 'bytes');
|
|
123
|
+
value = JSON.parse(buffer.subarray(0, used).toString('utf8'));
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
await handle.close();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
if (error instanceof RulePackRefusal)
|
|
131
|
+
throw error;
|
|
132
|
+
throw new RulePackRefusal('pack_io', 'Cannot read a regular rule-pack JSON file.', 'Supply one readable UTF-8 JSON file within the documented byte limit.');
|
|
133
|
+
}
|
|
134
|
+
return parseRulePack(value);
|
|
135
|
+
}
|
|
136
|
+
/** Evaluate an assertion in bounded linear literal operations; length counts UTF-16 code units. */
|
|
137
|
+
function satisfies(value, check) {
|
|
138
|
+
switch (check.op) {
|
|
139
|
+
case 'present': return value.trim().length > 0;
|
|
140
|
+
case 'contains': return value.includes(check.value);
|
|
141
|
+
case 'not_contains': return !value.includes(check.value);
|
|
142
|
+
case 'starts_with': return value.startsWith(check.value);
|
|
143
|
+
case 'min_length': return value.length >= check.value;
|
|
144
|
+
case 'max_length': return value.length <= check.value;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/** Use core's certified full reader so filesystem and remote SDK adapters share semantics. */
|
|
148
|
+
export async function lintRulePack(client, input) {
|
|
149
|
+
const pack = parseRulePack(input);
|
|
150
|
+
const corpus = certifyCompleteListResult(await client.listAllComplete());
|
|
151
|
+
bound(corpus.items.length * pack.rules.length <= RULE_PACK_LIMITS.checks, 'checks');
|
|
152
|
+
const findings = [];
|
|
153
|
+
const summary = { info: 0, warn: 0, error: 0 };
|
|
154
|
+
let checks = 0;
|
|
155
|
+
for (const item of corpus.items) {
|
|
156
|
+
for (const rule of pack.rules) {
|
|
157
|
+
if (rule.when?.statuses !== undefined && !rule.when.statuses.includes(item.status))
|
|
158
|
+
continue;
|
|
159
|
+
if (rule.when?.types !== undefined && !rule.when.types.includes(item.type))
|
|
160
|
+
continue;
|
|
161
|
+
checks += 1;
|
|
162
|
+
const value = item[rule.field] ?? '';
|
|
163
|
+
bound(value.length <= RULE_PACK_LIMITS.text, 'item text');
|
|
164
|
+
if (satisfies(value, rule.predicate))
|
|
165
|
+
continue;
|
|
166
|
+
bound(findings.length < RULE_PACK_LIMITS.findings, 'findings');
|
|
167
|
+
findings.push({ item_id: item.id, rule: rule.id, field: rule.field, severity: rule.severity, explanation: rule.explanation, remedy: rule.remedy });
|
|
168
|
+
summary[rule.severity] += 1;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
findings.sort(/** Certified item ids and unique rule ids make each pair distinct; use locale-free ordering. */ (a, b) => (a.item_id === b.item_id ? a.rule < b.rule : a.item_id < b.item_id) ? -1 : 1);
|
|
172
|
+
return { version: 1, findings, count: findings.length, corpus_count: corpus.items.length, checks, summary, complete_list: corpus.complete_list, exit_code: summary.error > 0 ? 1 : 0 };
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=rule-pack.js.map
|