@lornatang/dsh-codex-security 0.1.12 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +1 -1
  3. package/README.zh.md +1 -1
  4. package/docs/ARCHITECTURE.md +59 -59
  5. package/docs/ARTIFACTS.md +49 -49
  6. package/docs/CODEX-COMPATIBILITY.md +17 -17
  7. package/docs/SPEC.md +43 -43
  8. package/docs/TOOLS.md +364 -364
  9. package/docs/VALIDATION.md +33 -33
  10. package/docs/superpowers/plans/2026-11-16-deep-scan.md +71 -71
  11. package/docs/superpowers/specs/2026-11-16-deep-scan-design.md +31 -31
  12. package/lib/client.js +6 -1
  13. package/lib/constants.js +1 -1
  14. package/lib/deep-coordinator.js +80 -55
  15. package/lib/git.js +44 -8
  16. package/lib/service.js +65 -29
  17. package/lib/store.js +78 -39
  18. package/lib/target.js +27 -11
  19. package/lib/types/constants.d.ts +1 -1
  20. package/lib/types/deep-coordinator.d.ts +2 -8
  21. package/lib/types/git.d.ts +4 -0
  22. package/lib/types/index.d.ts +1 -1
  23. package/lib/types/service.d.ts +5 -3
  24. package/lib/types/store.d.ts +6 -1
  25. package/lib/types/target.d.ts +6 -2
  26. package/lib/types/util.d.ts +1 -0
  27. package/lib/util.js +18 -15
  28. package/package.json +1 -1
  29. package/schemas/coverage.schema.json +136 -136
  30. package/skills/README.md +122 -122
  31. package/skills/assess-patch-risk/SKILL.md +10 -10
  32. package/skills/attack-path-analysis/SKILL.md +10 -10
  33. package/skills/deep-security-scan/SKILL.md +15 -15
  34. package/skills/define-security-policy/SKILL.md +10 -10
  35. package/skills/finding-discovery/SKILL.md +10 -10
  36. package/skills/fix-finding/SKILL.md +14 -14
  37. package/skills/propose-security-hardening/SKILL.md +10 -10
  38. package/skills/track-findings/SKILL.md +12 -12
  39. package/skills/validation/SKILL.md +10 -10
  40. package/skills/verify-fix/SKILL.md +14 -14
package/lib/git.js CHANGED
@@ -1,4 +1,4 @@
1
- import { execFile } from 'node:child_process';
1
+ import { execFile, spawn } from 'node:child_process';
2
2
  import { promisify } from 'node:util';
3
3
  import { createHash } from 'node:crypto';
4
4
  import { createReadStream } from 'node:fs';
@@ -62,27 +62,27 @@ export async function trackedFiles(root, signal) {
62
62
  return result.stdout.split('\u0000').filter(Boolean);
63
63
  }
64
64
  export async function changedFiles(root, options) {
65
- const args = ['diff', '--name-status', '-z', '--find-renames'];
65
+ const args = ['--no-replace-objects', 'diff', '--name-status', '-z', '--find-renames'];
66
66
  if (options.staged)
67
67
  args.push('--cached');
68
68
  if (options.base) {
69
- const resolved = await resolveRevision(root, options.base);
69
+ const resolved = await resolveRevision(root, options.base, options.signal);
70
70
  if (!resolved)
71
71
  throw new Error('codex-security: invalid diff base: ' + options.base);
72
72
  if (options.workingTree) {
73
- const mergeBase = await gitString(root, ['merge-base', resolved, 'HEAD']);
73
+ const mergeBase = await gitString(root, ['--no-replace-objects', 'merge-base', resolved, options.head ?? 'HEAD'], undefined, options.signal);
74
74
  if (!mergeBase)
75
75
  throw new Error('codex-security: cannot resolve merge base: ' + options.base);
76
76
  args.push(mergeBase);
77
77
  }
78
78
  else {
79
- args.push(resolved + '...HEAD');
79
+ args.push(resolved + '...' + (options.head ?? 'HEAD'));
80
80
  }
81
81
  }
82
82
  else if (!options.staged) {
83
- args.push('HEAD');
83
+ args.push(options.head ?? 'HEAD');
84
84
  }
85
- const result = await git(root, args);
85
+ const result = await git(root, [...args, '--'], undefined, options.signal);
86
86
  if (!result.ok)
87
87
  throw new Error('codex-security: git diff failed: ' + result.stderr.trim());
88
88
  const parts = result.stdout.split('\u0000').filter(Boolean);
@@ -104,13 +104,49 @@ export async function changedFiles(root, options) {
104
104
  }
105
105
  if (options.workingTree && !options.staged) {
106
106
  const seen = new Set(files.map((file) => file.path));
107
- for (const entry of await statusEntries(root)) {
107
+ for (const entry of await statusEntries(root, options.signal)) {
108
108
  if (entry.code === '??' && !seen.has(entry.path))
109
109
  files.push({ status: 'A', path: entry.path });
110
110
  }
111
111
  }
112
112
  return files;
113
113
  }
114
+ /** Read a bounded prefix of a regular file from the exact commit. */
115
+ export async function revisionFilePrefix(root, revision, path, limit) {
116
+ if (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(revision) || path.includes('\0') || !Number.isSafeInteger(limit) || limit <= 0)
117
+ return undefined;
118
+ const entry = await git(root, ['--no-replace-objects', '--literal-pathspecs', 'ls-tree', '-z', '--full-tree', revision, '--', path]);
119
+ const match = /^100(?:644|755) blob ([a-f0-9]{40}|[a-f0-9]{64})\t/.exec(entry.stdout);
120
+ if (!entry.ok || !match || entry.stdout.slice(match[0].length) !== path + '\0')
121
+ return undefined;
122
+ const child = spawn('git', ['--no-replace-objects', 'cat-file', 'blob', match[1]], {
123
+ cwd: root, timeout: 60000, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'],
124
+ });
125
+ const exited = new Promise((resolve) => {
126
+ child.once('error', () => resolve(null));
127
+ child.once('close', resolve);
128
+ });
129
+ const chunks = [];
130
+ let size = 0;
131
+ try {
132
+ for await (const chunk of child.stdout) {
133
+ const prefix = chunk.subarray(0, limit - size);
134
+ chunks.push(prefix);
135
+ size += prefix.length;
136
+ if (size === limit) {
137
+ child.kill();
138
+ break;
139
+ }
140
+ }
141
+ const code = await exited;
142
+ return size === limit || code === 0 ? Buffer.concat(chunks, size) : undefined;
143
+ }
144
+ catch {
145
+ child.kill();
146
+ await exited;
147
+ return undefined;
148
+ }
149
+ }
114
150
  export async function fileDigest(path, signal) {
115
151
  try {
116
152
  const hash = createHash('sha256');
package/lib/service.js CHANGED
@@ -1,4 +1,4 @@
1
- import { lstat, readFile } from 'node:fs/promises';
1
+ import { lstat, mkdtemp, readFile, rm } from 'node:fs/promises';
2
2
  import { dirname, join, resolve as resolvePath } from 'node:path';
3
3
  import { DEFAULT_CONFIG } from './config.js';
4
4
  import { PACKAGE_NAME, PLUGIN_NAME, PRODUCER, SKILL_NAMES, VERSION } from './constants.js';
@@ -8,7 +8,7 @@ import { FINDING_STATUSES, SEVERITIES } from './types.js';
8
8
  import { ScanStore, defaultStateDir } from './store.js';
9
9
  import { readEvidenceLine, readEvidenceLines, resolveTarget } from './target.js';
10
10
  import { assertArtifactContract } from './schema.js';
11
- import { canonicalProspectivePath, ensureDir, isWithin, joinWithin, newId, normalizeLine, nowIso, readFullText, readJson, sanitizeRelative, sha256, truncate, writeFileAtomic, writeJsonAtomic } from './util.js';
11
+ import { canonicalProspectivePath, ensureDir, isWithin, joinWithin, newId, normalizeLine, nowIso, readFullText, readJson, renameFileAtomic, sanitizeRelative, sha256, truncate, writeFileAtomic, writeJsonAtomic } from './util.js';
12
12
  import { createExportValidator, writeSecureExport } from './secure-export.js';
13
13
  import { acquireStateLock } from './state-lock.js';
14
14
  import { WorkerRegistry } from './deep-worker.js';
@@ -511,6 +511,7 @@ export class SecurityService {
511
511
  validateFindingEvidence(deduped);
512
512
  assertArtifactContract('findings', buildFindingsDocument(scanId, deduped));
513
513
  const stored = {
514
+ ...(record.target.kind === 'git_diff' && !record.target.workingTree ? { boundRevisionEvidence: true } : {}),
514
515
  scanId,
515
516
  recordedAt: nowIso(),
516
517
  complete: draft.complete === true,
@@ -579,7 +580,7 @@ export class SecurityService {
579
580
  // Group by source path first so a draft citing several lines of one file
580
581
  // pays one hardened read instead of one per line.
581
582
  const requests = Array.from(keys.values());
582
- const lines = await readEvidenceLines(record.target.rootPath, requests, this.config.maxFileBytes);
583
+ const lines = await readEvidenceLines(record.target, requests, this.config.maxFileBytes);
583
584
  const evidence = new Map();
584
585
  for (const [key, value] of keys) {
585
586
  const line = lines.get(value.path + ':' + value.line);
@@ -691,7 +692,7 @@ export class SecurityService {
691
692
  const severity = this.severityOf(requiredStr(input, 'severity'));
692
693
  const path = sanitizeRelative(requiredStr(input, 'path'));
693
694
  const startLine = Math.max(1, Math.floor(num(input['startLine']) ?? 1));
694
- const anchor = str(input['anchor']) ?? normalizeAnchor(ruleId, path, await readEvidenceLine(record.target.rootPath, path, startLine, this.config.maxFileBytes), str(input['instance']));
695
+ const anchor = str(input['anchor']) ?? normalizeAnchor(ruleId, path, await readEvidenceLine(record.target, path, startLine, this.config.maxFileBytes), str(input['instance']));
695
696
  const instance = str(input['instance']);
696
697
  const key = ruleId + '\u0000' + anchor + '\u0000' + (instance ?? '');
697
698
  const candidateId = 'cand_' + sha256(key).slice(0, 16);
@@ -916,19 +917,25 @@ export class SecurityService {
916
917
  const reviewState = draft.input.reviewState ?? { surfaces: [] };
917
918
  const warnings = [...record.warnings];
918
919
  let migratedDraftIds;
919
- if (findings.some((finding) => finding.fingerprints.primary !== fingerprintFor(record.target.targetId, finding.ruleId, finding.identity.anchor, finding.identity.instance))) {
920
- // Only unsealed checkpoints are migrated. Sealed historical artifacts
921
- // remain byte-for-byte immutable and use the legacy comparison path.
920
+ const legacyIds = findings.some((finding) => finding.fingerprints.primary !== fingerprintFor(record.target.targetId, finding.ruleId, finding.identity.anchor, finding.identity.instance));
921
+ const verifyRevisionEvidence = record.target.kind === 'git_diff' && !record.target.workingTree && !draft.boundRevisionEvidence;
922
+ if (legacyIds || verifyRevisionEvidence) {
923
+ // Older checkpoints may have derived revision-only anchors from disk.
924
+ // Match identities before publishing; candidate and review records may
925
+ // still reference the original findings.
922
926
  const evidenceLines = await this.collectEvidenceLines(record, draft.input.findings);
923
927
  const migrated = draft.input.findings.map((input) => normalizeFinding(scanId, input, { rootPath: record.target.rootPath, targetId: record.target.targetId, evidenceLines }));
924
928
  if (new Set(migrated.map((finding) => finding.fingerprints.primary)).size !== migrated.length)
925
929
  throw new Error('codex-security: legacy draft has duplicate finding identities; record a corrected draft');
926
930
  const byIdentity = new Map(migrated.map((finding) => [JSON.stringify([finding.ruleId, finding.identity.anchor, finding.identity.instance ?? '', finding.locations[0]?.path]), finding]));
927
- migratedDraftIds = findings.map((finding) => ({ previous: finding.findingId, current: byIdentity.get(JSON.stringify([finding.ruleId, finding.identity.anchor, finding.identity.instance ?? '', finding.locations[0]?.path]))?.findingId ?? '' }));
928
- if (migratedDraftIds.some((entry) => !entry.current))
931
+ const matches = findings.map((finding) => ({ previous: finding.findingId, current: byIdentity.get(JSON.stringify([finding.ruleId, finding.identity.anchor, finding.identity.instance ?? '', finding.locations[0]?.path]))?.findingId ?? '' }));
932
+ if (matches.some((entry) => !entry.current))
929
933
  throw new Error('codex-security: legacy draft identities changed; record a corrected draft');
930
- findings = sortedFindings(migrated);
931
- warnings.push('unsealed legacy draft finding IDs were migrated to the Codex-compatible identity contract');
934
+ if (legacyIds) {
935
+ migratedDraftIds = matches;
936
+ findings = sortedFindings(migrated);
937
+ warnings.push('unsealed legacy draft finding IDs were migrated to the Codex-compatible identity contract');
938
+ }
932
939
  }
933
940
  if (reviewState.surfaces.length === 0)
934
941
  throw new Error('codex-security: at least one coverage surface is required before sealing');
@@ -1045,23 +1052,47 @@ export class SecurityService {
1045
1052
  },
1046
1053
  };
1047
1054
  assertArtifactContract('scan-manifest', manifest);
1048
- for (const file of sealedFiles) {
1049
- options?.signal?.throwIfAborted();
1050
- await writeFileAtomic(joinWithin(dir, file.path), file.content);
1051
- }
1052
- await writeJsonAtomic(join(dir, 'scan-manifest.json'), manifest);
1053
1055
  const counts = severityCounts(findings);
1054
1056
  options?.signal?.throwIfAborted();
1055
- const updated = await this.store.update(scanId, (current) => {
1056
- current.status = 'completed';
1057
- current.completedAt = sealedAt;
1058
- current.findingsCount = findings.length;
1059
- current.severityCounts = counts;
1060
- current.artifacts = record.artifacts.map((entry) => ({ ...entry, createdAt: sealedAt }));
1061
- current.policy = policy;
1062
- current.warnings = warnings;
1063
- current.progress.phase = 'completed';
1064
- });
1057
+ const stageDir = await mkdtemp(join(dir, 'checkpoints', 'seal-'));
1058
+ let updated;
1059
+ try {
1060
+ for (const file of sealedFiles) {
1061
+ options?.signal?.throwIfAborted();
1062
+ await writeFileAtomic(join(stageDir, file.path), file.content);
1063
+ }
1064
+ await writeJsonAtomic(join(stageDir, 'scan-manifest.json'), manifest);
1065
+ updated = await this.store.update(scanId, (current) => {
1066
+ current.status = 'completed';
1067
+ current.completedAt = sealedAt;
1068
+ current.findingsCount = findings.length;
1069
+ current.severityCounts = counts;
1070
+ current.artifacts = record.artifacts.map((entry) => ({ ...entry, createdAt: sealedAt }));
1071
+ current.policy = policy;
1072
+ current.warnings = warnings;
1073
+ current.progress.phase = 'completed';
1074
+ }, {
1075
+ signal: options?.signal,
1076
+ publish: async (commit) => {
1077
+ const publish = async () => {
1078
+ options?.signal?.throwIfAborted();
1079
+ // Readers use the scan record as the publication marker. Once
1080
+ // publication starts, finish it before honoring cancellation.
1081
+ for (const file of sealedFiles)
1082
+ await renameFileAtomic(join(stageDir, file.path), join(dir, file.path));
1083
+ await renameFileAtomic(join(stageDir, 'scan-manifest.json'), join(dir, 'scan-manifest.json'));
1084
+ await commit();
1085
+ };
1086
+ if (options?.withPublication)
1087
+ await options.withPublication(publish);
1088
+ else
1089
+ await publish();
1090
+ },
1091
+ });
1092
+ }
1093
+ finally {
1094
+ await rm(stageDir, { recursive: true, force: true }).catch(() => undefined);
1095
+ }
1065
1096
  const absolute = (name) => join(dir, name);
1066
1097
  return {
1067
1098
  ok: true,
@@ -1139,12 +1170,17 @@ export class SecurityService {
1139
1170
  // directory instead of re-resolving the index (and re-reading scan.json)
1140
1171
  // through the store once per document.
1141
1172
  const triage = (await readJson(join(dir, 'triage.json'))) ?? {};
1142
- const findings = ((await readJson(join(dir, 'findings.json')))?.findings ?? []).map((finding) => {
1173
+ const [document, coverage, manifest] = record.status === 'completed'
1174
+ ? await Promise.all([
1175
+ readJson(join(dir, 'findings.json')),
1176
+ readJson(join(dir, 'coverage.json')),
1177
+ readJson(join(dir, 'scan-manifest.json')),
1178
+ ])
1179
+ : [];
1180
+ const findings = (document?.findings ?? []).map((finding) => {
1143
1181
  const entry = triage[finding.findingId];
1144
1182
  return { ...finding, status: entry?.status ?? 'open', ...(entry ? { triage: entry } : {}) };
1145
1183
  });
1146
- const coverage = await readJson(join(dir, 'coverage.json'));
1147
- const manifest = await readJson(join(dir, 'scan-manifest.json'));
1148
1184
  const artifacts = await this.store.listArtifacts(scanId, dir);
1149
1185
  const reviewItems = (await readJson(join(dir, 'review-items.json')))?.items ?? [];
1150
1186
  return {
package/lib/store.js CHANGED
@@ -1,9 +1,10 @@
1
- import { readdir, stat } from 'node:fs/promises';
1
+ import { readdir, rm, stat } from 'node:fs/promises';
2
2
  import { dirname, join } from 'node:path';
3
3
  import { ensureDir, joinWithin, nowIso, readJson, readText, sanitizeRelative, writeFileAtomic, writeJsonAtomic, sha256 } from './util.js';
4
4
  import { acquireStateLock } from './state-lock.js';
5
5
  /** Files owned by the seal step; agents cannot overwrite them through save_artifact. */
6
- const SEALED_FILES = new Set(['scan.json', 'findings.json', 'coverage.json', 'scan-manifest.json', 'sarif.json', 'report.md', 'threat-model.md', 'threatmodel.md']);
6
+ const FINAL_ARTIFACTS = new Set(['findings.json', 'coverage.json', 'scan-manifest.json', 'sarif.json', 'report.md', 'threat-model.md']);
7
+ const SEALED_FILES = new Set([...FINAL_ARTIFACTS, 'scan.json', 'threatmodel.md']);
7
8
  const INTERNAL_FILES = new Set([...SEALED_FILES, 'candidates.json', 'validations.json', 'attack-paths.json', 'review-items.json', 'triage.json']);
8
9
  /**
9
10
  * Fold an artifact path into the form Win32 uses when resolving it. Windows
@@ -63,6 +64,7 @@ export class ScanStore {
63
64
  this.stateDir = stateDir;
64
65
  }
65
66
  get indexPath() { return join(this.stateDir, 'index.json'); }
67
+ get indexLockPath() { return join(this.stateDir, '.locks', 'index.sqlite'); }
66
68
  scanDir(record) {
67
69
  return join(this.stateDir, 'repositories', record.repoKey, 'scans', record.scanId);
68
70
  }
@@ -73,7 +75,7 @@ export class ScanStore {
73
75
  serialize(operation, signal) {
74
76
  const guarded = async () => {
75
77
  signal?.throwIfAborted();
76
- const release = await acquireStateLock(join(this.stateDir, '.locks', 'index.sqlite'), { signal });
78
+ const release = await acquireStateLock(this.indexLockPath, { signal });
77
79
  try {
78
80
  signal?.throwIfAborted();
79
81
  return await operation();
@@ -91,25 +93,21 @@ export class ScanStore {
91
93
  const info = await stat(this.indexPath).catch(() => undefined);
92
94
  return info ? info.mtimeMs + ':' + info.size + ':' + info.ino : undefined;
93
95
  }
94
- async loadIndex() {
96
+ async readIndex() {
95
97
  const stamp = await this.indexFileStamp();
96
- if (stamp !== undefined) {
97
- if (this.indexCache && this.indexStamp === stamp)
98
- return this.indexCache;
99
- const stored = await readJson(this.indexPath);
100
- if (stored && stored.version === 1) {
101
- this.indexCache = stored;
102
- this.indexStamp = stamp;
103
- return stored;
104
- }
105
- }
106
- else if (this.indexCache && this.indexStamp === undefined) {
98
+ if (stamp === undefined)
99
+ return undefined;
100
+ if (this.indexCache && this.indexStamp === stamp)
107
101
  return this.indexCache;
108
- }
109
- const rebuilt = await this.rebuildIndex();
110
- this.indexCache = rebuilt;
111
- this.indexStamp = undefined;
112
- return rebuilt;
102
+ const stored = await readJson(this.indexPath);
103
+ if (stored?.version !== 1)
104
+ return undefined;
105
+ this.indexCache = stored;
106
+ this.indexStamp = stamp;
107
+ return stored;
108
+ }
109
+ async loadIndex() {
110
+ return (await this.readIndex()) ?? this.rebuildIndex();
113
111
  }
114
112
  async rebuildIndex() {
115
113
  const index = { version: 1, scans: {} };
@@ -205,21 +203,40 @@ export class ScanStore {
205
203
  }
206
204
  return undefined;
207
205
  }
208
- async update(scanId, mutate) {
209
- return this.serialize(async () => {
210
- const found = await this.get(scanId);
211
- if (!found)
212
- throw new Error('codex-security: scan not found: ' + scanId);
213
- const record = found.record;
214
- mutate(record);
215
- record.updatedAt = nowIso();
216
- record.progress.updatedAt = record.updatedAt;
217
- await this.writeRecord(record);
218
- const index = await this.loadIndex();
219
- index.scans[record.scanId] = { ...summarize(record), repoKey: record.repoKey };
220
- await this.saveIndex(index);
221
- return record;
222
- });
206
+ async update(scanId, mutate, options = {}) {
207
+ let published;
208
+ try {
209
+ return await this.serialize(async () => {
210
+ const found = await this.get(scanId);
211
+ if (!found)
212
+ throw new Error('codex-security: scan not found: ' + scanId);
213
+ const record = found.record;
214
+ mutate(record);
215
+ record.updatedAt = nowIso();
216
+ record.progress.updatedAt = record.updatedAt;
217
+ const index = await this.loadIndex();
218
+ const nextIndex = { ...index, scans: { ...index.scans, [scanId]: summarize(record) } };
219
+ if (options.publish) {
220
+ await options.publish(async () => {
221
+ // A missing index is rebuilt from scan records. Invalidate it before
222
+ // commit so a crash cannot leave a stale running summary on disk.
223
+ await rm(this.indexPath, { force: true });
224
+ await this.writeRecord(record);
225
+ published = record;
226
+ });
227
+ }
228
+ else {
229
+ await this.writeRecord(record);
230
+ }
231
+ await this.saveIndex(nextIndex);
232
+ return record;
233
+ }, options.signal);
234
+ }
235
+ catch (error) {
236
+ if (published)
237
+ return published;
238
+ throw error;
239
+ }
223
240
  }
224
241
  async writeScanFile(scanId, name, value) {
225
242
  const found = await this.get(scanId);
@@ -233,12 +250,30 @@ export class ScanStore {
233
250
  const found = await this.get(scanId);
234
251
  if (!found)
235
252
  throw new Error('codex-security: scan not found: ' + scanId);
236
- return readJson(joinWithin(found.dir, name));
253
+ const clean = sanitizeRelative(name);
254
+ if (found.record.status !== 'completed' && FINAL_ARTIFACTS.has(guardName(clean)))
255
+ return undefined;
256
+ return readJson(joinWithin(found.dir, clean));
237
257
  }
238
258
  async list() {
239
- // Read-only fast path: list never mutates scan state and must not serialize behind
240
- // the index lock. The cached index is reused until index.json changes on disk.
241
- const index = await this.loadIndex();
259
+ let index = await this.readIndex();
260
+ if (!index) {
261
+ // Rebuild while publication owns the lock without waiting. If the lock
262
+ // is free, persist the repair to restore the cached path for later reads.
263
+ const release = await acquireStateLock(this.indexLockPath, { timeoutMs: 0 }).catch(() => undefined);
264
+ try {
265
+ if (release)
266
+ index = await this.readIndex();
267
+ if (!index) {
268
+ index = await this.rebuildIndex();
269
+ if (release)
270
+ await this.saveIndex(index).catch(() => undefined);
271
+ }
272
+ }
273
+ finally {
274
+ release?.();
275
+ }
276
+ }
242
277
  return Object.values(index.scans).sort((left, right) => right.createdAt.localeCompare(left.createdAt));
243
278
  }
244
279
  async saveArtifact(scanId, relativePath, content, mediaType) {
@@ -263,6 +298,10 @@ export class ScanStore {
263
298
  if (!found)
264
299
  throw new Error('codex-security: scan not found: ' + scanId);
265
300
  const clean = sanitizeRelative(relativePath);
301
+ const guarded = guardName(clean);
302
+ if (guarded.startsWith('checkpoints/seal-') || (found.record.status !== 'completed' && FINAL_ARTIFACTS.has(guarded))) {
303
+ throw new Error('codex-security: artifact not found: ' + relativePath);
304
+ }
266
305
  const target = joinWithin(found.dir, clean);
267
306
  const content = await readText(target, limit);
268
307
  if (content === undefined)
package/lib/target.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { lstat, realpath, stat } from 'node:fs/promises';
2
2
  import { basename, dirname, join, relative, resolve, sep } from 'node:path';
3
3
  import { createHash } from 'node:crypto';
4
- import { isProbablyBinary, isWithin, joinWithin, languageOf, listFiles, matchesAny, matchesScopePath, normalizeLine, stableTargetId, truncate } from './util.js';
5
- import { changedFiles, currentBranch, fileDigest, headRevision, remoteUrl, repoRoot, resolveRevision, statusEntries, trackedFiles } from './git.js';
4
+ import { isProbablyBinary, isWithin, joinWithin, languageOf, listFiles, matchesAny, matchesScopePath, normalizeLine, sanitizeRelative, stableTargetId, truncate } from './util.js';
5
+ import { changedFiles, currentBranch, fileDigest, headRevision, remoteUrl, repoRoot, resolveRevision, revisionFilePrefix, statusEntries, trackedFiles } from './git.js';
6
6
  const DEFAULT_EXCLUDES = ['.git/**', 'node_modules/**', 'dist/**', 'build/**', 'out/**', 'coverage/**', '.next/**', '.nuxt/**', 'vendor/**', 'target/**', '.venv/**', '__pycache__/**', '*.min.js', '*.min.css', '*.map', '*.lock', 'package-lock.json', 'pnpm-lock.yaml', 'yarn.lock', 'Cargo.lock', 'poetry.lock', 'uv.lock', 'composer.lock', 'go.sum'];
7
7
  export async function resolveTarget(options) {
8
8
  options.signal?.throwIfAborted();
@@ -57,7 +57,7 @@ export async function resolveTarget(options) {
57
57
  let kind = 'directory_snapshot';
58
58
  if (gitRepo)
59
59
  kind = options.diffTarget || options.workingTree ? 'git_diff' : 'git_worktree';
60
- const diff = kind === 'git_diff' ? await resolveDiff(rootPath, { diffTarget: baseRevision, workingTree: options.workingTree === true }) : undefined;
60
+ const diff = kind === 'git_diff' ? await resolveDiff(rootPath, { diffTarget: baseRevision, workingTree: options.workingTree === true, head: revision, signal: options.signal }) : undefined;
61
61
  const diffFiles = diff?.files.filter((file) => (matchesScopePath(file.path, scope.includePaths) || (file.oldPath !== undefined && matchesScopePath(file.oldPath, scope.includePaths))) &&
62
62
  !matchesAny(file.path, excludePaths));
63
63
  const changedPaths = diffFiles ? new Set(diffFiles.map((file) => file.path)) : undefined;
@@ -172,15 +172,15 @@ function displayNameFor(rootPath, remote) {
172
172
  return name.length > 0 ? name : rootPath;
173
173
  }
174
174
  export async function resolveDiff(rootPath, options) {
175
- const head = await headRevision(rootPath);
176
- const files = await changedFiles(rootPath, { base: options.diffTarget, staged: options.staged, workingTree: options.workingTree });
175
+ const head = options.head ?? await headRevision(rootPath, options.signal);
176
+ const files = await changedFiles(rootPath, { base: options.diffTarget, head, staged: options.staged, workingTree: options.workingTree, signal: options.signal });
177
177
  return { files, ...(options.diffTarget ? { base: options.diffTarget } : {}), ...(head ? { head } : {}), workingTree: options.workingTree === true || (!options.diffTarget && !options.staged) };
178
178
  }
179
179
  /** Read one source line for anchor derivation without trusting it as instructions. */
180
- export async function readEvidenceLine(rootPath, path, line, maxBytes) {
180
+ export async function readEvidenceLine(source, path, line, maxBytes) {
181
181
  if (!Number.isFinite(line) || line < 1)
182
182
  return undefined;
183
- const lines = await readEvidenceFileLines(rootPath, path, maxBytes);
183
+ const lines = await readEvidenceFileLines(source, path, maxBytes);
184
184
  if (!lines)
185
185
  return undefined;
186
186
  const value = lines[Math.trunc(line) - 1];
@@ -192,7 +192,7 @@ export async function readEvidenceLine(rootPath, path, line, maxBytes) {
192
192
  * file prefix for every requested line, so a draft citing several lines of one
193
193
  * file paid that cost repeatedly.
194
194
  */
195
- export async function readEvidenceLines(rootPath, requests, maxBytes) {
195
+ export async function readEvidenceLines(source, requests, maxBytes) {
196
196
  const byPath = new Map();
197
197
  for (const request of requests) {
198
198
  if (!Number.isFinite(request.line) || request.line < 1)
@@ -211,7 +211,7 @@ export async function readEvidenceLines(rootPath, requests, maxBytes) {
211
211
  const concurrency = 8;
212
212
  for (let index = 0; index < groups.length; index += concurrency) {
213
213
  await Promise.all(groups.slice(index, index + concurrency).map(async ([path, lineNumbers]) => {
214
- const lines = await readEvidenceFileLines(rootPath, path, maxBytes);
214
+ const lines = await readEvidenceFileLines(source, path, maxBytes);
215
215
  if (!lines)
216
216
  return;
217
217
  for (const line of lineNumbers) {
@@ -224,7 +224,23 @@ export async function readEvidenceLines(rootPath, requests, maxBytes) {
224
224
  return result;
225
225
  }
226
226
  /** Hardened single-file read shared by the line readers. */
227
- async function readEvidenceFileLines(rootPath, path, maxBytes) {
227
+ async function readEvidenceFileLines(source, path, maxBytes) {
228
+ const maxPrefixBytes = Math.trunc(Math.min(maxBytes, 256 * 1024));
229
+ if (typeof source !== 'string' && source.kind === 'git_diff' && !source.workingTree) {
230
+ const revision = source.headRevision ?? source.revision;
231
+ if (!revision)
232
+ return undefined;
233
+ let clean;
234
+ try {
235
+ clean = sanitizeRelative(path);
236
+ }
237
+ catch {
238
+ return undefined;
239
+ }
240
+ const prefix = await revisionFilePrefix(source.rootPath, revision, clean, maxPrefixBytes);
241
+ return prefix?.toString('utf8').split('\n');
242
+ }
243
+ const rootPath = typeof source === 'string' ? source : source.rootPath;
228
244
  const { open } = await import('node:fs/promises');
229
245
  const { constants } = await import('node:fs');
230
246
  if (typeof path !== 'string' || path.length === 0)
@@ -259,7 +275,7 @@ async function readEvidenceFileLines(rootPath, path, maxBytes) {
259
275
  const current = await lstat(canonical).catch(() => undefined);
260
276
  if (!current || !current.isFile() || current.dev !== opened.dev || current.ino !== opened.ino || current.size !== opened.size)
261
277
  return undefined;
262
- const buffer = Buffer.alloc(Math.min(maxBytes, 256 * 1024));
278
+ const buffer = Buffer.alloc(maxPrefixBytes);
263
279
  const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
264
280
  return buffer.subarray(0, bytesRead).toString('utf8').split('\n');
265
281
  }
@@ -1,6 +1,6 @@
1
1
  export declare const PACKAGE_NAME = "@lornatang/dsh-codex-security";
2
2
  export declare const PLUGIN_NAME = "codex-security";
3
- export declare const VERSION = "0.1.12";
3
+ export declare const VERSION = "0.1.13";
4
4
  export declare const ROUTE_PREFIX = "/plugins/codex-security";
5
5
  export declare const PRODUCER: {
6
6
  name: string;
@@ -44,16 +44,10 @@ export declare class DeepCoordinator {
44
44
  private cancelPath;
45
45
  private runSidecarDir;
46
46
  private runSidecarPath;
47
- private readonly sidecarHashes;
48
- /** Heavy per-worker results that no longer belong in the transition journal. */
49
- private runPayload;
50
- /**
51
- * Journal transitions are written frequently. Keep only light run metadata in
52
- * the journal and offload each worker's findings/coverage/receipts to a
53
- * per-worker sidecar, written only when its payload actually changed.
54
- */
47
+ private readonly dirtyPayloads;
55
48
  private save;
56
49
  private read;
50
+ private restoreRunPayloads;
57
51
  private authorize;
58
52
  start(args: Json, actor: DeepActor): Promise<Json>;
59
53
  resume(args: Json, actor: DeepActor): Promise<Json>;
@@ -23,7 +23,11 @@ export interface ChangedFile {
23
23
  }
24
24
  export declare function changedFiles(root: string, options: {
25
25
  base?: string;
26
+ head?: string;
26
27
  staged?: boolean;
27
28
  workingTree?: boolean;
29
+ signal?: AbortSignal;
28
30
  }): Promise<ChangedFile[]>;
31
+ /** Read a bounded prefix of a regular file from the exact commit. */
32
+ export declare function revisionFilePrefix(root: string, revision: string, path: string, limit: number): Promise<Buffer | undefined>;
29
33
  export declare function fileDigest(path: string, signal?: AbortSignal): Promise<string | undefined>;
@@ -6,7 +6,7 @@ import type { PluginContext } from './types.js';
6
6
  export declare const name = "codex-security";
7
7
  export declare const inject: string[];
8
8
  export { Config };
9
- export declare const version = "0.1.12";
9
+ export declare const version = "0.1.13";
10
10
  export { SecurityService };
11
11
  export type { ConfigType };
12
12
  export declare function loadBundledSkills(packageRoot: string): SkillRegistration[];
@@ -4,6 +4,10 @@ import { ScanStore } from './store.js';
4
4
  import type { WorkerSnapshot } from './deep-worker.js';
5
5
  export declare const FORMATS: readonly ["sarif", "json", "csv", "markdown"];
6
6
  export type ExportFormat = (typeof FORMATS)[number];
7
+ interface SealOptions {
8
+ signal?: AbortSignal;
9
+ withPublication?: (publish: () => Promise<void>) => Promise<void>;
10
+ }
7
11
  type Json = Record<string, unknown>;
8
12
  export interface SecurityServiceOptions {
9
13
  config?: Partial<Config>;
@@ -60,9 +64,7 @@ export declare class SecurityService {
60
64
  saveArtifact(args: Json): Promise<Json>;
61
65
  private saveArtifactUnlocked;
62
66
  readArtifact(args: Json): Promise<Json>;
63
- completeScan(args: Json, options?: {
64
- signal?: AbortSignal;
65
- }): Promise<Json>;
67
+ completeScan(args: Json, options?: SealOptions): Promise<Json>;
66
68
  private completeScanUnlocked;
67
69
  failScan(args: Json): Promise<Json>;
68
70
  private failScanUnlocked;
@@ -31,11 +31,13 @@ export declare class ScanStore {
31
31
  private indexStamp?;
32
32
  constructor(stateDir: string);
33
33
  private get indexPath();
34
+ private get indexLockPath();
34
35
  scanDir(record: Pick<ScanRecord, 'repoKey' | 'scanId'>): string;
35
36
  init(): Promise<void>;
36
37
  private serialize;
37
38
  /** Cheap identity of index.json so a changed on-disk index invalidates the cache. */
38
39
  private indexFileStamp;
40
+ private readIndex;
39
41
  loadIndex(): Promise<ScanIndex>;
40
42
  private rebuildIndex;
41
43
  saveIndex(index: ScanIndex): Promise<void>;
@@ -47,7 +49,10 @@ export declare class ScanStore {
47
49
  record: ScanRecord;
48
50
  dir: string;
49
51
  } | undefined>;
50
- update(scanId: string, mutate: (record: ScanRecord) => void): Promise<ScanRecord>;
52
+ update(scanId: string, mutate: (record: ScanRecord) => void, options?: {
53
+ signal?: AbortSignal;
54
+ publish?: (commit: () => Promise<void>) => Promise<void>;
55
+ }): Promise<ScanRecord>;
51
56
  writeScanFile(scanId: string, name: string, value: unknown): Promise<string>;
52
57
  readScanFile<T>(scanId: string, name: string): Promise<T | undefined>;
53
58
  list(): Promise<ScanSummary[]>;
@@ -30,18 +30,22 @@ export interface DiffSnapshot {
30
30
  }
31
31
  export declare function resolveDiff(rootPath: string, options: {
32
32
  diffTarget?: string;
33
+ head?: string;
33
34
  staged?: boolean;
34
35
  workingTree?: boolean;
36
+ signal?: AbortSignal;
35
37
  }): Promise<DiffSnapshot>;
38
+ type EvidenceSource = string | Pick<TargetInfo, 'rootPath' | 'kind' | 'workingTree' | 'revision' | 'headRevision'>;
36
39
  /** Read one source line for anchor derivation without trusting it as instructions. */
37
- export declare function readEvidenceLine(rootPath: string, path: string, line: number, maxBytes: number): Promise<string | undefined>;
40
+ export declare function readEvidenceLine(source: EvidenceSource, path: string, line: number, maxBytes: number): Promise<string | undefined>;
38
41
  /**
39
42
  * Read many evidence lines with one hardened open per source file. The old
40
43
  * per-line reader repeated the lstat/open/realpath verification and re-read the
41
44
  * file prefix for every requested line, so a draft citing several lines of one
42
45
  * file paid that cost repeatedly.
43
46
  */
44
- export declare function readEvidenceLines(rootPath: string, requests: {
47
+ export declare function readEvidenceLines(source: EvidenceSource, requests: {
45
48
  path: string;
46
49
  line: number;
47
50
  }[], maxBytes: number): Promise<Map<string, string>>;
51
+ export {};