@lornatang/dsh-codex-security 0.1.11 → 0.1.12
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 +8 -0
- package/README.md +1 -1
- package/README.zh.md +1 -1
- package/lib/constants.js +1 -1
- package/lib/deep-coordinator.js +67 -3
- package/lib/deep-worker.js +4 -1
- package/lib/service.js +28 -25
- package/lib/store.js +36 -14
- package/lib/target.js +48 -6
- package/lib/tools.js +47 -1
- package/lib/types/constants.d.ts +1 -1
- package/lib/types/deep-coordinator.d.ts +10 -0
- package/lib/types/index.d.ts +1 -1
- package/lib/types/store.d.ts +5 -1
- package/lib/types/target.d.ts +10 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.1.12] - 2026-11-19
|
|
4
|
+
|
|
5
|
+
- Read chunked evidence from the already verified worker text cache instead of re-reading and hashing the whole file per chunk.
|
|
6
|
+
- Serve scan listings from a cached index invalidated by index.json metadata instead of rebuilding and locking on every read.
|
|
7
|
+
- Read scan triage, findings and artifacts from the resolved scan directory in one pass instead of resolving the index repeatedly.
|
|
8
|
+
- Offload Deep worker payloads to per-worker sidecar files so journal transitions no longer rewrite every worker findings on each save, and detect merge collisions with a set.
|
|
9
|
+
- Group draft evidence reads by source file behind one bounded reader, and normalize tool results without a JSON round-trip.
|
|
10
|
+
|
|
3
11
|
## [0.1.11] - 2026-11-19
|
|
4
12
|
|
|
5
13
|
- Remove the unused scan-draft JSON schema and the three reference links that pointed at it; draft validation is unchanged and still enforced by the host.
|
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ dsh plugin --profile <name> add @lornatang/dsh-codex-security
|
|
|
34
34
|
dsh plugin --profile <name> remove @lornatang/dsh-codex-security
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
For the GitHub 0.1.
|
|
37
|
+
For the GitHub 0.1.12 release, use `dsh plugin --profile <name> add https://github.com/Lornatang/dsh-codex-security/releases/download/v0.1.12/lornatang-dsh-codex-security-0.1.12.tgz`. The npm registry version may be older.
|
|
38
38
|
|
|
39
39
|
The `add` command composes the layer into the profile: the profile's `cordis.patch.yml` gains an insert entry, row id `codex-security`, whose `config` starts from the bundle defaults `maxFiles: 5000`, `maxFileBytes: 1048576`, and `defaultMode: standard`; every other field takes its default from `src/config.ts`. The package manifest points `dsh.bundle.patch` at that document. The `remove` command deletes the row and leaves scans already written under the profile data directory untouched.
|
|
40
40
|
|
package/README.zh.md
CHANGED
|
@@ -34,7 +34,7 @@ dsh plugin --profile <name> add @lornatang/dsh-codex-security
|
|
|
34
34
|
dsh plugin --profile <name> remove @lornatang/dsh-codex-security
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
安装 GitHub 上的 0.1.
|
|
37
|
+
安装 GitHub 上的 0.1.12 版本,请运行 `dsh plugin --profile <name> add https://github.com/Lornatang/dsh-codex-security/releases/download/v0.1.12/lornatang-dsh-codex-security-0.1.12.tgz`。npm 仓库中的版本可能较旧。
|
|
38
38
|
|
|
39
39
|
`add` 命令把这一层组合进 profile:profile 的 `cordis.patch.yml` 中新增一条 insert 条目,row id 为 `codex-security`,其 `config` 从本包默认值 `maxFiles: 5000`、`maxFileBytes: 1048576`、`defaultMode: standard` 开始;其余字段取 `src/config.ts` 中的默认值。包清单把 `dsh.bundle.patch` 指向该文档。`remove` 命令删除该条目,已经写入 profile 数据目录的扫描保持不变。
|
|
40
40
|
|
package/lib/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const PACKAGE_NAME = '@lornatang/dsh-codex-security';
|
|
2
2
|
export const PLUGIN_NAME = 'codex-security';
|
|
3
|
-
export const VERSION = '0.1.
|
|
3
|
+
export const VERSION = '0.1.12';
|
|
4
4
|
export const ROUTE_PREFIX = '/plugins/codex-security';
|
|
5
5
|
export const PRODUCER = { name: '@lornatang/dsh-codex-security', version: VERSION };
|
|
6
6
|
export const SKILL_NAMES = ['security-scan', 'security-diff-scan', 'deep-security-scan', 'fix-finding', 'verify-fix', 'triage-finding', 'threat-model', 'track-findings', 'resolve-dependency-alert', 'vulnerability-writeup', 'assess-patch-risk', 'attack-path-analysis', 'define-security-policy', 'finding-discovery', 'propose-security-hardening', 'validation'];
|
package/lib/deep-coordinator.js
CHANGED
|
@@ -6,7 +6,7 @@ import { assertWorkerSnapshotMatches, captureWorkerSnapshot } from './deep-worke
|
|
|
6
6
|
import { acquireStateLock } from './state-lock.js';
|
|
7
7
|
import { resolveTarget } from './target.js';
|
|
8
8
|
import { sourceRootIdentity } from './secure-read.js';
|
|
9
|
-
import { newId, nowIso, readJson, sha256, writeJsonAtomic } from './util.js';
|
|
9
|
+
import { ensureDir, newId, nowIso, readJson, sha256, writeJsonAtomic } from './util.js';
|
|
10
10
|
const OUTPUT_SCHEMA = {
|
|
11
11
|
type: 'object', properties: { draftJson: { type: 'string', description: 'JSON object with findings and reviewState.' } },
|
|
12
12
|
required: ['draftJson'], additionalProperties: false,
|
|
@@ -67,13 +67,73 @@ export class DeepCoordinator {
|
|
|
67
67
|
lockPath(scanId) { return join(this.service.store.stateDir, '.locks', 'deep-' + scanId + '.sqlite'); }
|
|
68
68
|
transitionPath(scanId) { return join(this.service.store.stateDir, '.locks', 'deep-transition-' + scanId + '.sqlite'); }
|
|
69
69
|
cancelPath(scanId) { return join(this.service.store.stateDir, 'deep', scanId + '.cancel.json'); }
|
|
70
|
-
|
|
70
|
+
runSidecarDir(scanId) { return join(this.service.store.stateDir, 'deep', scanId + '.runs'); }
|
|
71
|
+
runSidecarPath(scanId, childId) { return join(this.runSidecarDir(scanId), childId + '.json'); }
|
|
72
|
+
sidecarHashes = new Map();
|
|
73
|
+
/** Heavy per-worker results that no longer belong in the transition journal. */
|
|
74
|
+
runPayload(run) {
|
|
75
|
+
const payload = {};
|
|
76
|
+
if (run.draft !== undefined)
|
|
77
|
+
payload['draft'] = run.draft;
|
|
78
|
+
if (run.receipts !== undefined)
|
|
79
|
+
payload['receipts'] = run.receipts;
|
|
80
|
+
if (run.findings !== undefined)
|
|
81
|
+
payload['findings'] = run.findings;
|
|
82
|
+
if (run.coverage !== undefined)
|
|
83
|
+
payload['coverage'] = run.coverage;
|
|
84
|
+
if (run.reviewState !== undefined)
|
|
85
|
+
payload['reviewState'] = run.reviewState;
|
|
86
|
+
return payload;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Journal transitions are written frequently. Keep only light run metadata in
|
|
90
|
+
* the journal and offload each worker's findings/coverage/receipts to a
|
|
91
|
+
* per-worker sidecar, written only when its payload actually changed.
|
|
92
|
+
*/
|
|
93
|
+
async save(journal) {
|
|
94
|
+
if (journal.runs.some((run) => Object.keys(this.runPayload(run)).length > 0))
|
|
95
|
+
await ensureDir(this.runSidecarDir(journal.scanId));
|
|
96
|
+
for (const run of journal.runs) {
|
|
97
|
+
if (!/^dcs_[a-f0-9]{24}$/.test(run.childId))
|
|
98
|
+
throw new Error('codex-security: invalid Deep worker id');
|
|
99
|
+
const payload = this.runPayload(run);
|
|
100
|
+
if (Object.keys(payload).length === 0)
|
|
101
|
+
continue;
|
|
102
|
+
const text = JSON.stringify(payload);
|
|
103
|
+
const digest = sha256(text);
|
|
104
|
+
const cacheKey = journal.scanId + '\u0000' + run.childId;
|
|
105
|
+
if (this.sidecarHashes.get(cacheKey) === digest)
|
|
106
|
+
continue;
|
|
107
|
+
await writeJsonAtomic(this.runSidecarPath(journal.scanId, run.childId), payload);
|
|
108
|
+
this.sidecarHashes.set(cacheKey, digest);
|
|
109
|
+
}
|
|
110
|
+
const light = { ...journal, runs: journal.runs.map((run) => ({
|
|
111
|
+
number: run.number, childId: run.childId, scanId: run.scanId, state: run.state,
|
|
112
|
+
...(run.complete !== undefined ? { complete: run.complete } : {}),
|
|
113
|
+
...(run.error ? { error: run.error } : {}),
|
|
114
|
+
...(run.newCount !== undefined ? { newCount: run.newCount } : {}),
|
|
115
|
+
})) };
|
|
116
|
+
await writeJsonAtomic(this.journalPath(journal.scanId), light);
|
|
117
|
+
}
|
|
71
118
|
async read(scanId) {
|
|
72
119
|
if (!/^cs_[a-f0-9]{24}$/.test(scanId))
|
|
73
120
|
throw new Error('codex-security: invalid Deep scan id');
|
|
74
121
|
const journal = await readJson(this.journalPath(scanId));
|
|
75
122
|
if (!journal || journal.version !== 1 || journal.scanId !== scanId)
|
|
76
123
|
throw new Error('codex-security: Deep scan journal not found');
|
|
124
|
+
for (const run of journal.runs) {
|
|
125
|
+
if (!run || !/^dcs_[a-f0-9]{24}$/.test(run.childId))
|
|
126
|
+
continue;
|
|
127
|
+
if (run.draft !== undefined && run.receipts !== undefined && run.findings !== undefined)
|
|
128
|
+
continue;
|
|
129
|
+
const payload = await readJson(this.runSidecarPath(scanId, run.childId)).catch(() => undefined);
|
|
130
|
+
if (!payload || typeof payload !== 'object')
|
|
131
|
+
continue;
|
|
132
|
+
for (const key of ['draft', 'receipts', 'findings', 'coverage', 'reviewState']) {
|
|
133
|
+
if (run[key] === undefined && payload[key] !== undefined)
|
|
134
|
+
run[key] = payload[key];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
77
137
|
return journal;
|
|
78
138
|
}
|
|
79
139
|
authorize(journal, actor, claim) {
|
|
@@ -405,6 +465,7 @@ export class DeepCoordinator {
|
|
|
405
465
|
}
|
|
406
466
|
merge(journal) {
|
|
407
467
|
const merged = new Map();
|
|
468
|
+
const fingerprints = new Set();
|
|
408
469
|
let collisions = 0;
|
|
409
470
|
for (const run of journal.runs) {
|
|
410
471
|
if (run.state !== 'processed' || !run.findings)
|
|
@@ -420,8 +481,11 @@ export class DeepCoordinator {
|
|
|
420
481
|
prior.runs.push(run.number);
|
|
421
482
|
continue;
|
|
422
483
|
}
|
|
423
|
-
|
|
484
|
+
// A previous key with the same fingerprint but a different semantic
|
|
485
|
+
// signature means the workers disagreed about one identity.
|
|
486
|
+
if (fingerprints.has(fingerprint))
|
|
424
487
|
collisions++;
|
|
488
|
+
fingerprints.add(fingerprint);
|
|
425
489
|
merged.set(key, { finding, signature, runs: [run.number] });
|
|
426
490
|
newCount++;
|
|
427
491
|
}
|
package/lib/deep-worker.js
CHANGED
|
@@ -166,7 +166,10 @@ export class WorkerRegistry {
|
|
|
166
166
|
async readChunk(childId, path, offset) {
|
|
167
167
|
if (!Number.isSafeInteger(offset) || offset < 0)
|
|
168
168
|
throw new Error('codex-security: invalid worker source offset');
|
|
169
|
-
|
|
169
|
+
// Chunks after the first were re-opening and re-hashing the whole file. The
|
|
170
|
+
// cache only ever holds bytes verified against the bound snapshot hash, so
|
|
171
|
+
// serving them cannot smuggle unverified content to the worker.
|
|
172
|
+
const content = this.cachedText(childId, path);
|
|
170
173
|
if (offset > content.length)
|
|
171
174
|
throw new Error('codex-security: worker source offset exceeds file length');
|
|
172
175
|
const end = Math.min(offset + 4096, content.length);
|
package/lib/service.js
CHANGED
|
@@ -6,7 +6,7 @@ import { buildCoverage, buildFindingsDocument, fingerprintFor, normalizeAnchor,
|
|
|
6
6
|
import { gitString } from './git.js';
|
|
7
7
|
import { FINDING_STATUSES, SEVERITIES } from './types.js';
|
|
8
8
|
import { ScanStore, defaultStateDir } from './store.js';
|
|
9
|
-
import { readEvidenceLine, resolveTarget } from './target.js';
|
|
9
|
+
import { readEvidenceLine, readEvidenceLines, resolveTarget } from './target.js';
|
|
10
10
|
import { assertArtifactContract } from './schema.js';
|
|
11
11
|
import { canonicalProspectivePath, ensureDir, isWithin, joinWithin, newId, normalizeLine, nowIso, readFullText, readJson, sanitizeRelative, sha256, truncate, writeFileAtomic, writeJsonAtomic } from './util.js';
|
|
12
12
|
import { createExportValidator, writeSecureExport } from './secure-export.js';
|
|
@@ -574,18 +574,17 @@ export class SecurityService {
|
|
|
574
574
|
if (keys.size >= 500)
|
|
575
575
|
break;
|
|
576
576
|
}
|
|
577
|
+
if (keys.size === 0)
|
|
578
|
+
return new Map();
|
|
579
|
+
// Group by source path first so a draft citing several lines of one file
|
|
580
|
+
// pays one hardened read instead of one per line.
|
|
581
|
+
const requests = Array.from(keys.values());
|
|
582
|
+
const lines = await readEvidenceLines(record.target.rootPath, requests, this.config.maxFileBytes);
|
|
577
583
|
const evidence = new Map();
|
|
578
|
-
const
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
const lines = await Promise.all(batch.map(async ([key, value]) => {
|
|
583
|
-
const line = await readEvidenceLine(record.target.rootPath, value.path, value.line, this.config.maxFileBytes);
|
|
584
|
-
return { key, line };
|
|
585
|
-
}));
|
|
586
|
-
for (const entry of lines)
|
|
587
|
-
if (entry.line !== undefined)
|
|
588
|
-
evidence.set(entry.key, entry.line);
|
|
584
|
+
for (const [key, value] of keys) {
|
|
585
|
+
const line = lines.get(value.path + ':' + value.line);
|
|
586
|
+
if (line !== undefined)
|
|
587
|
+
evidence.set(key, line);
|
|
589
588
|
}
|
|
590
589
|
return evidence;
|
|
591
590
|
}
|
|
@@ -595,20 +594,19 @@ export class SecurityService {
|
|
|
595
594
|
const files = new Map();
|
|
596
595
|
const evidence = new Map();
|
|
597
596
|
const read = async (path) => {
|
|
598
|
-
let
|
|
599
|
-
if (
|
|
600
|
-
|
|
601
|
-
files.set(path,
|
|
597
|
+
let lines = files.get(path);
|
|
598
|
+
if (lines === undefined) {
|
|
599
|
+
lines = (await registry.read('verified-draft', path)).split('\n');
|
|
600
|
+
files.set(path, lines);
|
|
602
601
|
}
|
|
603
|
-
return
|
|
602
|
+
return lines;
|
|
604
603
|
};
|
|
605
604
|
try {
|
|
606
605
|
for (const finding of findings) {
|
|
607
606
|
if (!Array.isArray(finding.locations))
|
|
608
607
|
throw new Error('codex-security: finding locations are required');
|
|
609
608
|
for (const location of finding.locations) {
|
|
610
|
-
const
|
|
611
|
-
const lines = content.split('\n');
|
|
609
|
+
const lines = await read(location.path);
|
|
612
610
|
if (!Number.isInteger(location.startLine) || location.startLine < 1 || location.startLine > lines.length || (location.endLine !== undefined && location.endLine > lines.length))
|
|
613
611
|
throw new Error('codex-security: finding location is outside the bound source');
|
|
614
612
|
evidence.set(location.path + ':' + location.startLine, truncate(normalizeLine(lines[location.startLine - 1] ?? ''), 400));
|
|
@@ -617,12 +615,11 @@ export class SecurityService {
|
|
|
617
615
|
const item = asObject(raw);
|
|
618
616
|
if (!item || typeof item['path'] !== 'string' || typeof item['code'] !== 'string' || !item['code'])
|
|
619
617
|
throw new Error('codex-security: worker code evidence requires a bound source path and code');
|
|
620
|
-
const
|
|
618
|
+
const lines = await read(item['path']);
|
|
621
619
|
const start = item['startLine'] ?? item['start_line'];
|
|
622
620
|
const end = item['endLine'] ?? item['end_line'] ?? start;
|
|
623
|
-
let excerpt =
|
|
621
|
+
let excerpt = lines.join('\n');
|
|
624
622
|
if (start !== undefined) {
|
|
625
|
-
const lines = content.split('\n');
|
|
626
623
|
if (typeof start !== 'number' || typeof end !== 'number' || !Number.isInteger(start) || !Number.isInteger(end) || start < 1 || end < start || end > lines.length)
|
|
627
624
|
throw new Error('codex-security: code evidence location is outside the bound source');
|
|
628
625
|
excerpt = lines.slice(start - 1, end).join('\n');
|
|
@@ -1138,11 +1135,17 @@ export class SecurityService {
|
|
|
1138
1135
|
async getScan(args) {
|
|
1139
1136
|
const scanId = requiredStr(args, 'scanId');
|
|
1140
1137
|
const { record, dir } = await this.requireScan(scanId);
|
|
1141
|
-
|
|
1138
|
+
// Read the scan files that all belong to this one response from the resolved
|
|
1139
|
+
// directory instead of re-resolving the index (and re-reading scan.json)
|
|
1140
|
+
// through the store once per document.
|
|
1141
|
+
const triage = (await readJson(join(dir, 'triage.json'))) ?? {};
|
|
1142
|
+
const findings = ((await readJson(join(dir, 'findings.json')))?.findings ?? []).map((finding) => {
|
|
1143
|
+
const entry = triage[finding.findingId];
|
|
1144
|
+
return { ...finding, status: entry?.status ?? 'open', ...(entry ? { triage: entry } : {}) };
|
|
1145
|
+
});
|
|
1142
1146
|
const coverage = await readJson(join(dir, 'coverage.json'));
|
|
1143
1147
|
const manifest = await readJson(join(dir, 'scan-manifest.json'));
|
|
1144
|
-
const artifacts = await this.store.listArtifacts(scanId);
|
|
1145
|
-
const triage = await this.store.readTriage(scanId);
|
|
1148
|
+
const artifacts = await this.store.listArtifacts(scanId, dir);
|
|
1146
1149
|
const reviewItems = (await readJson(join(dir, 'review-items.json')))?.items ?? [];
|
|
1147
1150
|
return {
|
|
1148
1151
|
ok: true,
|
package/lib/store.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readdir } from 'node:fs/promises';
|
|
1
|
+
import { readdir, 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';
|
|
@@ -57,6 +57,8 @@ export function summarize(record) {
|
|
|
57
57
|
export class ScanStore {
|
|
58
58
|
stateDir;
|
|
59
59
|
queue = Promise.resolve();
|
|
60
|
+
indexCache;
|
|
61
|
+
indexStamp;
|
|
60
62
|
constructor(stateDir) {
|
|
61
63
|
this.stateDir = stateDir;
|
|
62
64
|
}
|
|
@@ -84,9 +86,30 @@ export class ScanStore {
|
|
|
84
86
|
this.queue = next.catch(() => undefined);
|
|
85
87
|
return next;
|
|
86
88
|
}
|
|
89
|
+
/** Cheap identity of index.json so a changed on-disk index invalidates the cache. */
|
|
90
|
+
async indexFileStamp() {
|
|
91
|
+
const info = await stat(this.indexPath).catch(() => undefined);
|
|
92
|
+
return info ? info.mtimeMs + ':' + info.size + ':' + info.ino : undefined;
|
|
93
|
+
}
|
|
87
94
|
async loadIndex() {
|
|
88
|
-
const
|
|
89
|
-
|
|
95
|
+
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) {
|
|
107
|
+
return this.indexCache;
|
|
108
|
+
}
|
|
109
|
+
const rebuilt = await this.rebuildIndex();
|
|
110
|
+
this.indexCache = rebuilt;
|
|
111
|
+
this.indexStamp = undefined;
|
|
112
|
+
return rebuilt;
|
|
90
113
|
}
|
|
91
114
|
async rebuildIndex() {
|
|
92
115
|
const index = { version: 1, scans: {} };
|
|
@@ -106,6 +129,8 @@ export class ScanStore {
|
|
|
106
129
|
}
|
|
107
130
|
async saveIndex(index) {
|
|
108
131
|
await writeJsonAtomic(this.indexPath, index);
|
|
132
|
+
this.indexCache = index;
|
|
133
|
+
this.indexStamp = await this.indexFileStamp();
|
|
109
134
|
}
|
|
110
135
|
async create(input, options = {}) {
|
|
111
136
|
return this.serialize(async () => {
|
|
@@ -211,11 +236,10 @@ export class ScanStore {
|
|
|
211
236
|
return readJson(joinWithin(found.dir, name));
|
|
212
237
|
}
|
|
213
238
|
async list() {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
});
|
|
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();
|
|
242
|
+
return Object.values(index.scans).sort((left, right) => right.createdAt.localeCompare(left.createdAt));
|
|
219
243
|
}
|
|
220
244
|
async saveArtifact(scanId, relativePath, content, mediaType) {
|
|
221
245
|
const found = await this.get(scanId);
|
|
@@ -243,13 +267,12 @@ export class ScanStore {
|
|
|
243
267
|
const content = await readText(target, limit);
|
|
244
268
|
if (content === undefined)
|
|
245
269
|
throw new Error('codex-security: artifact not found: ' + relativePath);
|
|
246
|
-
const { stat } = await import('node:fs/promises');
|
|
247
270
|
const info = await stat(target);
|
|
248
271
|
return { path: clean, content, bytes: info.size, truncated: info.size > limit };
|
|
249
272
|
}
|
|
250
|
-
async listArtifacts(scanId) {
|
|
251
|
-
const
|
|
252
|
-
if (!
|
|
273
|
+
async listArtifacts(scanId, knownDir) {
|
|
274
|
+
const scanDir = knownDir ?? (await this.get(scanId))?.dir;
|
|
275
|
+
if (!scanDir)
|
|
253
276
|
return [];
|
|
254
277
|
const results = [];
|
|
255
278
|
const walk = async (dir, relBase) => {
|
|
@@ -274,7 +297,6 @@ export class ScanStore {
|
|
|
274
297
|
continue;
|
|
275
298
|
if (relBase.length === 0 && SEALED_FILES.has(guardName(entry.name)))
|
|
276
299
|
continue;
|
|
277
|
-
const { stat } = await import('node:fs/promises');
|
|
278
300
|
const info = await stat(join(dir, entry.name)).catch((error) => {
|
|
279
301
|
if (error.code === 'ENOENT')
|
|
280
302
|
return undefined;
|
|
@@ -284,7 +306,7 @@ export class ScanStore {
|
|
|
284
306
|
results.push({ path: rel, bytes: info.size });
|
|
285
307
|
}
|
|
286
308
|
};
|
|
287
|
-
await walk(
|
|
309
|
+
await walk(scanDir, '');
|
|
288
310
|
return results.sort((left, right) => left.path.localeCompare(right.path));
|
|
289
311
|
}
|
|
290
312
|
async readTriage(scanId) {
|
package/lib/target.js
CHANGED
|
@@ -178,10 +178,55 @@ export async function resolveDiff(rootPath, options) {
|
|
|
178
178
|
}
|
|
179
179
|
/** Read one source line for anchor derivation without trusting it as instructions. */
|
|
180
180
|
export async function readEvidenceLine(rootPath, path, line, maxBytes) {
|
|
181
|
-
const { open } = await import('node:fs/promises');
|
|
182
|
-
const { constants } = await import('node:fs');
|
|
183
181
|
if (!Number.isFinite(line) || line < 1)
|
|
184
182
|
return undefined;
|
|
183
|
+
const lines = await readEvidenceFileLines(rootPath, path, maxBytes);
|
|
184
|
+
if (!lines)
|
|
185
|
+
return undefined;
|
|
186
|
+
const value = lines[Math.trunc(line) - 1];
|
|
187
|
+
return value === undefined ? undefined : truncate(normalizeLine(value), 400);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Read many evidence lines with one hardened open per source file. The old
|
|
191
|
+
* per-line reader repeated the lstat/open/realpath verification and re-read the
|
|
192
|
+
* file prefix for every requested line, so a draft citing several lines of one
|
|
193
|
+
* file paid that cost repeatedly.
|
|
194
|
+
*/
|
|
195
|
+
export async function readEvidenceLines(rootPath, requests, maxBytes) {
|
|
196
|
+
const byPath = new Map();
|
|
197
|
+
for (const request of requests) {
|
|
198
|
+
if (!Number.isFinite(request.line) || request.line < 1)
|
|
199
|
+
continue;
|
|
200
|
+
const line = Math.trunc(request.line);
|
|
201
|
+
const bucket = byPath.get(request.path);
|
|
202
|
+
if (bucket) {
|
|
203
|
+
if (!bucket.includes(line))
|
|
204
|
+
bucket.push(line);
|
|
205
|
+
}
|
|
206
|
+
else
|
|
207
|
+
byPath.set(request.path, [line]);
|
|
208
|
+
}
|
|
209
|
+
const result = new Map();
|
|
210
|
+
const groups = [...byPath.entries()];
|
|
211
|
+
const concurrency = 8;
|
|
212
|
+
for (let index = 0; index < groups.length; index += concurrency) {
|
|
213
|
+
await Promise.all(groups.slice(index, index + concurrency).map(async ([path, lineNumbers]) => {
|
|
214
|
+
const lines = await readEvidenceFileLines(rootPath, path, maxBytes);
|
|
215
|
+
if (!lines)
|
|
216
|
+
return;
|
|
217
|
+
for (const line of lineNumbers) {
|
|
218
|
+
const value = lines[line - 1];
|
|
219
|
+
if (value !== undefined)
|
|
220
|
+
result.set(path + ':' + line, truncate(normalizeLine(value), 400));
|
|
221
|
+
}
|
|
222
|
+
}));
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
/** Hardened single-file read shared by the line readers. */
|
|
227
|
+
async function readEvidenceFileLines(rootPath, path, maxBytes) {
|
|
228
|
+
const { open } = await import('node:fs/promises');
|
|
229
|
+
const { constants } = await import('node:fs');
|
|
185
230
|
if (typeof path !== 'string' || path.length === 0)
|
|
186
231
|
return undefined;
|
|
187
232
|
let absolute;
|
|
@@ -216,10 +261,7 @@ export async function readEvidenceLine(rootPath, path, line, maxBytes) {
|
|
|
216
261
|
return undefined;
|
|
217
262
|
const buffer = Buffer.alloc(Math.min(maxBytes, 256 * 1024));
|
|
218
263
|
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
219
|
-
|
|
220
|
-
const lines = text.split('\n');
|
|
221
|
-
const value = lines[line - 1];
|
|
222
|
-
return value === undefined ? undefined : truncate(normalizeLine(value), 400);
|
|
264
|
+
return buffer.subarray(0, bytesRead).toString('utf8').split('\n');
|
|
223
265
|
}
|
|
224
266
|
catch {
|
|
225
267
|
return undefined;
|
package/lib/tools.js
CHANGED
|
@@ -13,6 +13,52 @@ const FORMAT_ENUM = ENUM('sarif', 'json', 'csv', 'markdown');
|
|
|
13
13
|
function render(_args, value) {
|
|
14
14
|
return [{ type: 'text', text: JSON.stringify(value, null, 2) }];
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* DSH rejects values containing undefined even though JSON.stringify drops them.
|
|
18
|
+
* A full JSON round-trip on every tool result allocates a second copy of large
|
|
19
|
+
* responses; walk the value instead and keep it when nothing needs normalizing.
|
|
20
|
+
*/
|
|
21
|
+
function normalizeToolResult(value) {
|
|
22
|
+
return normalizeValue(value, new Set());
|
|
23
|
+
}
|
|
24
|
+
function normalizeValue(value, seen) {
|
|
25
|
+
if (value === undefined || typeof value === 'function' || typeof value === 'symbol')
|
|
26
|
+
return undefined;
|
|
27
|
+
if (typeof value === 'number')
|
|
28
|
+
return Number.isFinite(value) ? value : null;
|
|
29
|
+
if (typeof value !== 'object' || value === null)
|
|
30
|
+
return value;
|
|
31
|
+
if (seen.has(value))
|
|
32
|
+
throw new TypeError('codex-security: tool result contains a circular structure');
|
|
33
|
+
if (Array.isArray(value)) {
|
|
34
|
+
seen.add(value);
|
|
35
|
+
let changed = false;
|
|
36
|
+
const normalized = value.map((item) => {
|
|
37
|
+
const next = normalizeValue(item, seen);
|
|
38
|
+
const result = next === undefined ? null : next;
|
|
39
|
+
if (result !== item)
|
|
40
|
+
changed = true;
|
|
41
|
+
return result;
|
|
42
|
+
});
|
|
43
|
+
seen.delete(value);
|
|
44
|
+
return changed ? normalized : value;
|
|
45
|
+
}
|
|
46
|
+
seen.add(value);
|
|
47
|
+
let changed = false;
|
|
48
|
+
const normalized = {};
|
|
49
|
+
for (const [key, item] of Object.entries(value)) {
|
|
50
|
+
const next = normalizeValue(item, seen);
|
|
51
|
+
if (next === undefined) {
|
|
52
|
+
changed = true;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (next !== item)
|
|
56
|
+
changed = true;
|
|
57
|
+
normalized[key] = next;
|
|
58
|
+
}
|
|
59
|
+
seen.delete(value);
|
|
60
|
+
return changed ? normalized : value;
|
|
61
|
+
}
|
|
16
62
|
function tool(name, description, properties, required, run, timeoutMs = 180000) {
|
|
17
63
|
const parameters = { type: 'object', properties, ...(required.length > 0 ? { required } : {}), additionalProperties: false };
|
|
18
64
|
const validate = compileToolArguments(parameters);
|
|
@@ -24,7 +70,7 @@ function tool(name, description, properties, required, run, timeoutMs = 180000)
|
|
|
24
70
|
timeoutMs,
|
|
25
71
|
// DSH rejects values containing undefined even though JSON.stringify drops them.
|
|
26
72
|
// Normalize the whole response, including nested optional fields, at the boundary.
|
|
27
|
-
execute: async (args, exec) => { validate(args); return
|
|
73
|
+
execute: async (args, exec) => { validate(args); return normalizeToolResult(await run(args, exec)); },
|
|
28
74
|
};
|
|
29
75
|
}
|
|
30
76
|
const SCAN_ID = { type: 'string', description: 'Scan id returned by a codex_security_start_*_scan tool, e.g. cs_<24 hex chars>.' };
|
package/lib/types/constants.d.ts
CHANGED
|
@@ -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.
|
|
3
|
+
export declare const VERSION = "0.1.12";
|
|
4
4
|
export declare const ROUTE_PREFIX = "/plugins/codex-security";
|
|
5
5
|
export declare const PRODUCER: {
|
|
6
6
|
name: string;
|
|
@@ -42,6 +42,16 @@ export declare class DeepCoordinator {
|
|
|
42
42
|
private lockPath;
|
|
43
43
|
private transitionPath;
|
|
44
44
|
private cancelPath;
|
|
45
|
+
private runSidecarDir;
|
|
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
|
+
*/
|
|
45
55
|
private save;
|
|
46
56
|
private read;
|
|
47
57
|
private authorize;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -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.
|
|
9
|
+
export declare const version = "0.1.12";
|
|
10
10
|
export { SecurityService };
|
|
11
11
|
export type { ConfigType };
|
|
12
12
|
export declare function loadBundledSkills(packageRoot: string): SkillRegistration[];
|
package/lib/types/store.d.ts
CHANGED
|
@@ -27,11 +27,15 @@ export declare function summarize(record: ScanRecord): ScanSummary;
|
|
|
27
27
|
export declare class ScanStore {
|
|
28
28
|
readonly stateDir: string;
|
|
29
29
|
private queue;
|
|
30
|
+
private indexCache?;
|
|
31
|
+
private indexStamp?;
|
|
30
32
|
constructor(stateDir: string);
|
|
31
33
|
private get indexPath();
|
|
32
34
|
scanDir(record: Pick<ScanRecord, 'repoKey' | 'scanId'>): string;
|
|
33
35
|
init(): Promise<void>;
|
|
34
36
|
private serialize;
|
|
37
|
+
/** Cheap identity of index.json so a changed on-disk index invalidates the cache. */
|
|
38
|
+
private indexFileStamp;
|
|
35
39
|
loadIndex(): Promise<ScanIndex>;
|
|
36
40
|
private rebuildIndex;
|
|
37
41
|
saveIndex(index: ScanIndex): Promise<void>;
|
|
@@ -59,7 +63,7 @@ export declare class ScanStore {
|
|
|
59
63
|
bytes: number;
|
|
60
64
|
truncated: boolean;
|
|
61
65
|
}>;
|
|
62
|
-
listArtifacts(scanId: string): Promise<{
|
|
66
|
+
listArtifacts(scanId: string, knownDir?: string): Promise<{
|
|
63
67
|
path: string;
|
|
64
68
|
bytes: number;
|
|
65
69
|
}[]>;
|
package/lib/types/target.d.ts
CHANGED
|
@@ -35,3 +35,13 @@ export declare function resolveDiff(rootPath: string, options: {
|
|
|
35
35
|
}): Promise<DiffSnapshot>;
|
|
36
36
|
/** Read one source line for anchor derivation without trusting it as instructions. */
|
|
37
37
|
export declare function readEvidenceLine(rootPath: string, path: string, line: number, maxBytes: number): Promise<string | undefined>;
|
|
38
|
+
/**
|
|
39
|
+
* Read many evidence lines with one hardened open per source file. The old
|
|
40
|
+
* per-line reader repeated the lstat/open/realpath verification and re-read the
|
|
41
|
+
* file prefix for every requested line, so a draft citing several lines of one
|
|
42
|
+
* file paid that cost repeatedly.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readEvidenceLines(rootPath: string, requests: {
|
|
45
|
+
path: string;
|
|
46
|
+
line: number;
|
|
47
|
+
}[], maxBytes: number): Promise<Map<string, string>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lornatang/dsh-codex-security",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "Agent-native security review workbench for DeepSeek Harness, modeled on OpenAI codex-security: sealed scans, findings contract, deep-scan orchestration and SARIF/JSON export.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|