dsh-score 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +201 -0
- package/README.es.md +182 -0
- package/README.hi.md +182 -0
- package/README.md +216 -0
- package/README.pt.md +182 -0
- package/README.zh.md +190 -0
- package/SECURITY.md +57 -0
- package/THIRD_PARTY_NOTICES.md +15 -0
- package/cordis.patch.yml +40 -0
- package/lib/index.js +1924 -0
- package/lib/types/batch.d.ts +55 -0
- package/lib/types/batch.d.ts.map +1 -0
- package/lib/types/command.d.ts +22 -0
- package/lib/types/command.d.ts.map +1 -0
- package/lib/types/config.d.ts +84 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/dimensions.d.ts +67 -0
- package/lib/types/dimensions.d.ts.map +1 -0
- package/lib/types/domain.d.ts +138 -0
- package/lib/types/domain.d.ts.map +1 -0
- package/lib/types/index.d.ts +50 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/probe.d.ts +137 -0
- package/lib/types/probe.d.ts.map +1 -0
- package/lib/types/report.d.ts +18 -0
- package/lib/types/report.d.ts.map +1 -0
- package/lib/types/result.d.ts +214 -0
- package/lib/types/result.d.ts.map +1 -0
- package/lib/types/sanitize.d.ts +64 -0
- package/lib/types/sanitize.d.ts.map +1 -0
- package/lib/types/score.d.ts +65 -0
- package/lib/types/score.d.ts.map +1 -0
- package/lib/types/tools.d.ts +29 -0
- package/lib/types/tools.d.ts.map +1 -0
- package/lib/types/version.d.ts +8 -0
- package/lib/types/version.d.ts.map +1 -0
- package/package.json +144 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,1924 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import z$1 from "@deepseek-ai/schemastery";
|
|
7
|
+
import { defineDomain, domainTable } from "@deepseek-ai/dsh-storage-domain";
|
|
8
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
9
|
+
//#region src/result.ts
|
|
10
|
+
/**
|
|
11
|
+
* The structured scoring contract `dsh-score/v1`: one score card per target,
|
|
12
|
+
* plus the leaderboard record that aggregates them. The zod schemas are the
|
|
13
|
+
* single source of truth — the same schemas validate records at the durable
|
|
14
|
+
* boundary of the `score` storage domain, and the compiled types type the
|
|
15
|
+
* runtime.
|
|
16
|
+
*
|
|
17
|
+
* Every conclusion a score card carries is backed by an {@link EvidenceLink}
|
|
18
|
+
* (source, sanitized detail, and an audit timestamp). A dimension that has no
|
|
19
|
+
* evidence reports `no-evidence` — it never fabricates a number.
|
|
20
|
+
*
|
|
21
|
+
* @module dsh-score/result
|
|
22
|
+
*/
|
|
23
|
+
/** Schema discriminator of every record this plugin writes (version 1). */
|
|
24
|
+
const RESULT_SCHEMA = "dsh-score/v1";
|
|
25
|
+
/** The five scored dimensions, in canonical order. */
|
|
26
|
+
const DIMENSIONS = [
|
|
27
|
+
"install",
|
|
28
|
+
"maintenance",
|
|
29
|
+
"documentation",
|
|
30
|
+
"security",
|
|
31
|
+
"compliance"
|
|
32
|
+
];
|
|
33
|
+
/** Outcome of one dimension: passing, concerning, failing, or no evidence gathered. */
|
|
34
|
+
const DimensionStatusSchema = z.union([
|
|
35
|
+
z.literal("pass"),
|
|
36
|
+
z.literal("warn"),
|
|
37
|
+
z.literal("fail"),
|
|
38
|
+
z.literal("no-evidence")
|
|
39
|
+
]);
|
|
40
|
+
/** How the target was addressed. */
|
|
41
|
+
const TargetKindSchema = z.union([z.literal("repo"), z.literal("npm")]);
|
|
42
|
+
/** One audit link: where a fact came from, what was observed, and when. */
|
|
43
|
+
const EvidenceLinkSchema = z.object({
|
|
44
|
+
/** Provenance class: `gh-api`, `npm-cli`, `git`, `test-drive`, or `none`. */
|
|
45
|
+
source: z.string().min(1),
|
|
46
|
+
/** Sanitized one-line description of the exact command or record. */
|
|
47
|
+
detail: z.string(),
|
|
48
|
+
/** ISO-8601 timestamp of the observation. */
|
|
49
|
+
observedAt: z.string().min(1)
|
|
50
|
+
});
|
|
51
|
+
/** One dimension's verdict: status, 0..100 score, weight, summary, and evidence. */
|
|
52
|
+
const DimensionScoreSchema = z.object({
|
|
53
|
+
/** Dimension id, see {@link Dimension}. */
|
|
54
|
+
dimension: z.enum(DIMENSIONS),
|
|
55
|
+
/** Verdict, see {@link DimensionStatus}. */
|
|
56
|
+
status: DimensionStatusSchema,
|
|
57
|
+
/** Dimension score in 0..100 (0 when there is no evidence). */
|
|
58
|
+
score: z.number().min(0).max(100),
|
|
59
|
+
/** Weight used when summing into the total (0..100). */
|
|
60
|
+
weight: z.number().min(0).max(100),
|
|
61
|
+
/** Sanitized one-line verdict. */
|
|
62
|
+
summary: z.string(),
|
|
63
|
+
/** Audit links backing the score. */
|
|
64
|
+
evidence: z.array(EvidenceLinkSchema)
|
|
65
|
+
});
|
|
66
|
+
/** One complete score card for a target. */
|
|
67
|
+
const ScoreResultSchema = z.object({
|
|
68
|
+
/** Discriminator {@link RESULT_SCHEMA}. */
|
|
69
|
+
schema: z.literal(RESULT_SCHEMA),
|
|
70
|
+
/** Unique score id (also the `scores` table key). */
|
|
71
|
+
scoreId: z.string().min(1),
|
|
72
|
+
/** Scored target. */
|
|
73
|
+
target: z.object({
|
|
74
|
+
kind: TargetKindSchema,
|
|
75
|
+
/** Sanitized target spec exactly as supplied. */
|
|
76
|
+
spec: z.string().min(1)
|
|
77
|
+
}),
|
|
78
|
+
/** ISO-8601 audit timestamp of the whole score. */
|
|
79
|
+
scoredAt: z.string().min(1),
|
|
80
|
+
/** Wall-clock duration of the scoring run in milliseconds. */
|
|
81
|
+
durationMs: z.number().finite(),
|
|
82
|
+
/** Producer version. */
|
|
83
|
+
pluginVersion: z.string().min(1),
|
|
84
|
+
/** Five dimension scores keyed by dimension id. */
|
|
85
|
+
dimensions: z.object({
|
|
86
|
+
install: DimensionScoreSchema,
|
|
87
|
+
maintenance: DimensionScoreSchema,
|
|
88
|
+
documentation: DimensionScoreSchema,
|
|
89
|
+
security: DimensionScoreSchema,
|
|
90
|
+
compliance: DimensionScoreSchema
|
|
91
|
+
}),
|
|
92
|
+
/** Weighted total over dimensions that have evidence, 0..100. */
|
|
93
|
+
total: z.number().min(0).max(100),
|
|
94
|
+
/** Letter grade derived from the total, or `N/A` when nothing had evidence. */
|
|
95
|
+
grade: z.string(),
|
|
96
|
+
/** One-line sanitized summary of the whole card. */
|
|
97
|
+
verdict: z.string()
|
|
98
|
+
});
|
|
99
|
+
/** One leaderboard row: the per-target total plus per-dimension statuses. */
|
|
100
|
+
const LeaderboardRowSchema = z.object({
|
|
101
|
+
/** Sanitized target spec. */
|
|
102
|
+
target: z.string().min(1),
|
|
103
|
+
/** Address kind. */
|
|
104
|
+
kind: TargetKindSchema,
|
|
105
|
+
/** Weighted total, 0..100. */
|
|
106
|
+
total: z.number().min(0).max(100),
|
|
107
|
+
/** Letter grade. */
|
|
108
|
+
grade: z.string(),
|
|
109
|
+
/** Sanitized one-line verdict. */
|
|
110
|
+
verdict: z.string(),
|
|
111
|
+
/** Scoring duration in milliseconds. */
|
|
112
|
+
durationMs: z.number().finite(),
|
|
113
|
+
/** Per-dimension statuses for the leaderboard table. */
|
|
114
|
+
install: DimensionStatusSchema,
|
|
115
|
+
maintenance: DimensionStatusSchema,
|
|
116
|
+
documentation: DimensionStatusSchema,
|
|
117
|
+
security: DimensionStatusSchema,
|
|
118
|
+
compliance: DimensionStatusSchema
|
|
119
|
+
});
|
|
120
|
+
/** Aggregated leaderboard record keyed by its id. */
|
|
121
|
+
const LeaderboardRecordSchema = z.object({
|
|
122
|
+
/** Discriminator {@link RESULT_SCHEMA}. */
|
|
123
|
+
schema: z.literal(RESULT_SCHEMA),
|
|
124
|
+
/** Leaderboard id (also the `leaderboards` table key). */
|
|
125
|
+
id: z.string().min(1),
|
|
126
|
+
/** ISO-8601 creation timestamp. */
|
|
127
|
+
createdAt: z.string().min(1),
|
|
128
|
+
/** Total wall-clock duration of the batch in milliseconds. */
|
|
129
|
+
durationMs: z.number().finite(),
|
|
130
|
+
/** Per-target rows, ordered by descending total. */
|
|
131
|
+
rows: z.array(LeaderboardRowSchema),
|
|
132
|
+
/** Per-target totals. */
|
|
133
|
+
totals: z.object({
|
|
134
|
+
total: z.number().int().min(0),
|
|
135
|
+
pass: z.number().int().min(0),
|
|
136
|
+
warn: z.number().int().min(0),
|
|
137
|
+
fail: z.number().int().min(0),
|
|
138
|
+
noEvidence: z.number().int().min(0)
|
|
139
|
+
})
|
|
140
|
+
});
|
|
141
|
+
/** Weighted average over dimensions that actually gathered evidence. */
|
|
142
|
+
function computeTotal(dimensions) {
|
|
143
|
+
let weightSum = 0;
|
|
144
|
+
let productSum = 0;
|
|
145
|
+
for (const dimension of dimensions) {
|
|
146
|
+
if (dimension.status === "no-evidence") continue;
|
|
147
|
+
weightSum += dimension.weight;
|
|
148
|
+
productSum += dimension.score * dimension.weight;
|
|
149
|
+
}
|
|
150
|
+
if (weightSum === 0) return 0;
|
|
151
|
+
return Math.round(productSum / weightSum);
|
|
152
|
+
}
|
|
153
|
+
/** Whether any dimension gathered evidence (as opposed to a fully inconclusive card). */
|
|
154
|
+
function hasEvidence(dimensions) {
|
|
155
|
+
return dimensions.some((dimension) => dimension.status !== "no-evidence");
|
|
156
|
+
}
|
|
157
|
+
/** Letter grade for a weighted total; `N/A` when no dimension had evidence. */
|
|
158
|
+
function gradeOf(total, evidenced) {
|
|
159
|
+
if (!evidenced) return "N/A";
|
|
160
|
+
if (total >= 90) return "A";
|
|
161
|
+
if (total >= 75) return "B";
|
|
162
|
+
if (total >= 60) return "C";
|
|
163
|
+
if (total >= 40) return "D";
|
|
164
|
+
return "F";
|
|
165
|
+
}
|
|
166
|
+
/** One-line whole-card verdict from the total and evidence coverage. */
|
|
167
|
+
function verdictOf(total, evidenced) {
|
|
168
|
+
if (!evidenced) return "no evidence gathered from any dimension";
|
|
169
|
+
if (total >= 75) return `healthy (weighted total ${total}/100)`;
|
|
170
|
+
if (total >= 60) return `acceptable (weighted total ${total}/100)`;
|
|
171
|
+
if (total >= 40) return `concerning (weighted total ${total}/100)`;
|
|
172
|
+
return `poor (weighted total ${total}/100)`;
|
|
173
|
+
}
|
|
174
|
+
/** Compile the per-target tallies for one leaderboard record. */
|
|
175
|
+
function totalsOf(rows) {
|
|
176
|
+
const totals = {
|
|
177
|
+
total: rows.length,
|
|
178
|
+
pass: 0,
|
|
179
|
+
warn: 0,
|
|
180
|
+
fail: 0,
|
|
181
|
+
noEvidence: 0
|
|
182
|
+
};
|
|
183
|
+
for (const row of rows) {
|
|
184
|
+
if (row.install === "no-evidence" && row.maintenance === "no-evidence" && row.documentation === "no-evidence" && row.security === "no-evidence" && row.compliance === "no-evidence") {
|
|
185
|
+
totals.noEvidence += 1;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (row.total >= 75) totals.pass += 1;
|
|
189
|
+
else if (row.total >= 60) totals.warn += 1;
|
|
190
|
+
else totals.fail += 1;
|
|
191
|
+
}
|
|
192
|
+
return totals;
|
|
193
|
+
}
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/sanitize.ts
|
|
196
|
+
/**
|
|
197
|
+
* Pure sanitizers for every string that reaches a report, a log, or the model.
|
|
198
|
+
* Nothing here reads the environment or the clock: report content is the only
|
|
199
|
+
* caller, and the sanitizers must stay testable with extreme inputs (token
|
|
200
|
+
* literals, credential-bearing URLs, hostile repo specs, machine-local paths).
|
|
201
|
+
*
|
|
202
|
+
* The same secret patterns drive two jobs — redaction for display and
|
|
203
|
+
* detection for the security dimension — so the compiled regexes are exported.
|
|
204
|
+
*
|
|
205
|
+
* @module dsh-score/sanitize
|
|
206
|
+
*/
|
|
207
|
+
/** GitHub personal access tokens (`ghp_` prefix, ≥20 body chars). */
|
|
208
|
+
const GITHUB_TOKEN = /ghp_[A-Za-z0-9]{20,}/gu;
|
|
209
|
+
/** npm access tokens (`npm_` prefix, ≥20 body chars). */
|
|
210
|
+
const NPM_TOKEN = /npm_[A-Za-z0-9]{20,}/gu;
|
|
211
|
+
/** OpenAI-style keys (`sk-` prefix, ≥16 body chars) and common jwt/aws/twilio shapes. */
|
|
212
|
+
const API_KEY = /(?:sk-[A-Za-z0-9_-]{16,}|AKIA[0-9A-Z]{16}|xox[baprs]-[A-Za-z0-9-]{10,})/gu;
|
|
213
|
+
/** `scheme://user:pass@host` — keeps the scheme and host, drops the credentials. */
|
|
214
|
+
const URL_CREDENTIALS = /(\b[a-z][a-z0-9+.-]*:\/\/)[^/@\s:]+(?::[^/@\s]*)?@/giu;
|
|
215
|
+
/** `Authorization: Bearer <token>`-style header lines. */
|
|
216
|
+
const BEARER = /\b(Bearer\s+)[A-Za-z0-9._~+/=-]{8,}/giu;
|
|
217
|
+
/** Control characters that must never survive into a one-line report field. */
|
|
218
|
+
const CONTROL_CHARS = /[\u0000-\u001f\u007f]/gu;
|
|
219
|
+
/** Maximum length of a sanitized target spec recorded in a report. */
|
|
220
|
+
const MAX_TARGET_SPEC_LENGTH = 1e3;
|
|
221
|
+
/** Marker substituted for every redacted secret. */
|
|
222
|
+
const REDACTED = "***REDACTED***";
|
|
223
|
+
/** Secret pattern name → the compiled matcher, in a stable display order. */
|
|
224
|
+
const SECRET_PATTERNS = [
|
|
225
|
+
{
|
|
226
|
+
name: "github-token",
|
|
227
|
+
pattern: GITHUB_TOKEN
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
name: "npm-token",
|
|
231
|
+
pattern: NPM_TOKEN
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
name: "api-key",
|
|
235
|
+
pattern: API_KEY
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
name: "url-credentials",
|
|
239
|
+
pattern: URL_CREDENTIALS
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
name: "bearer-header",
|
|
243
|
+
pattern: BEARER
|
|
244
|
+
}
|
|
245
|
+
];
|
|
246
|
+
/**
|
|
247
|
+
* Replace credential-shaped substrings with {@link REDACTED}.
|
|
248
|
+
*
|
|
249
|
+
* @param text - any output or user-supplied string.
|
|
250
|
+
* @returns the same text with secrets redacted.
|
|
251
|
+
*/
|
|
252
|
+
function redactSecrets(text) {
|
|
253
|
+
return text.replace(GITHUB_TOKEN, REDACTED).replace(NPM_TOKEN, REDACTED).replace(API_KEY, REDACTED).replace(URL_CREDENTIALS, "$1***@").replace(BEARER, `$1${REDACTED}`);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Detect which secret pattern names occur in a text block, de-duplicated in
|
|
257
|
+
* a stable order. Detection (unlike redaction) reports the full pattern name.
|
|
258
|
+
*
|
|
259
|
+
* @param text - any scanned content.
|
|
260
|
+
* @returns the matched pattern names, in stable order.
|
|
261
|
+
*/
|
|
262
|
+
function scanSecretPatterns(text) {
|
|
263
|
+
const found = [];
|
|
264
|
+
for (const { name, pattern } of SECRET_PATTERNS) {
|
|
265
|
+
pattern.lastIndex = 0;
|
|
266
|
+
if (pattern.test(text)) found.push(name);
|
|
267
|
+
}
|
|
268
|
+
return found;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Sanitize one user-supplied target spec for recording and display: strip
|
|
272
|
+
* control characters, redact embedded credentials, cap the length, and keep
|
|
273
|
+
* surrounding whitespace off the recorded value.
|
|
274
|
+
*
|
|
275
|
+
* @param spec - the raw target as the user supplied it.
|
|
276
|
+
* @returns the sanitized spec; never throws.
|
|
277
|
+
*/
|
|
278
|
+
function sanitizeTarget(spec) {
|
|
279
|
+
return redactSecrets(spec.replace(CONTROL_CHARS, " ").trim()).slice(0, MAX_TARGET_SPEC_LENGTH);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Bound a multi-line output stream to a short tail for one-line summaries.
|
|
283
|
+
* The tail keeps the END of the text (errors and final results cluster there).
|
|
284
|
+
*
|
|
285
|
+
* @param text - collected output.
|
|
286
|
+
* @param maxChars - retained character count.
|
|
287
|
+
* @returns the tail, prefixed with a truncation marker when shortened.
|
|
288
|
+
*/
|
|
289
|
+
function tailText(text, maxChars) {
|
|
290
|
+
const clean = text.replace(/\s+$/u, "");
|
|
291
|
+
if (clean.length <= maxChars) return clean;
|
|
292
|
+
return `…<truncated, showing last ${maxChars} chars>…${clean.slice(-maxChars)}`;
|
|
293
|
+
}
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/dimensions.ts
|
|
296
|
+
/** Build one audit link for a derived fact. */
|
|
297
|
+
function link(source, detail, now) {
|
|
298
|
+
return {
|
|
299
|
+
source,
|
|
300
|
+
detail,
|
|
301
|
+
observedAt: now
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
/** Whole days between an ISO timestamp and `now` (negative when in the future). */
|
|
305
|
+
function daysSince(iso, now) {
|
|
306
|
+
const then = Date.parse(iso);
|
|
307
|
+
if (Number.isNaN(then)) return NaN;
|
|
308
|
+
return (now.getTime() - then) / 864e5;
|
|
309
|
+
}
|
|
310
|
+
/** SPDX identifiers treated as acceptable open-source licenses. */
|
|
311
|
+
const LICENSE_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
312
|
+
"MIT",
|
|
313
|
+
"Apache-2.0",
|
|
314
|
+
"BSD-2-Clause",
|
|
315
|
+
"BSD-3-Clause",
|
|
316
|
+
"0BSD",
|
|
317
|
+
"ISC",
|
|
318
|
+
"Zlib",
|
|
319
|
+
"MPL-2.0",
|
|
320
|
+
"LGPL-2.1",
|
|
321
|
+
"LGPL-3.0",
|
|
322
|
+
"GPL-2.0",
|
|
323
|
+
"GPL-3.0",
|
|
324
|
+
"AGPL-3.0",
|
|
325
|
+
"Unlicense",
|
|
326
|
+
"CC0-1.0",
|
|
327
|
+
"BlueOak-1.0.0"
|
|
328
|
+
]);
|
|
329
|
+
/** SPDX values that mean "no usable license signal". */
|
|
330
|
+
const LICENSE_MISSING = /* @__PURE__ */ new Set([
|
|
331
|
+
"MISSING",
|
|
332
|
+
"NOASSERTION",
|
|
333
|
+
"NO-LICENSE",
|
|
334
|
+
""
|
|
335
|
+
]);
|
|
336
|
+
/** Suspicious markers inside package.json lifecycle scripts (install-time code execution). */
|
|
337
|
+
const MALICIOUS_SCRIPT_MARKERS = [
|
|
338
|
+
/\bcurl\b/iu,
|
|
339
|
+
/\bwget\b/iu,
|
|
340
|
+
/\beval\s*\(/iu,
|
|
341
|
+
/child_process/iu,
|
|
342
|
+
/execSync/iu,
|
|
343
|
+
/\bspawn\s*\(/iu,
|
|
344
|
+
/\brequire\s*\(\s*['"]child_process/iu,
|
|
345
|
+
/base64/iu,
|
|
346
|
+
/powershell/iu,
|
|
347
|
+
/Invoke-Expression/iu
|
|
348
|
+
];
|
|
349
|
+
/** Lifecycle script keys whose values are scanned for malicious markers. */
|
|
350
|
+
const LIFECYCLE_SCRIPT_KEYS = [
|
|
351
|
+
"postinstall",
|
|
352
|
+
"preinstall",
|
|
353
|
+
"install",
|
|
354
|
+
"prepare"
|
|
355
|
+
];
|
|
356
|
+
/**
|
|
357
|
+
* Scan a parsed package.json for suspicious install-time script markers.
|
|
358
|
+
*
|
|
359
|
+
* @param packageJson - the parsed manifest, or undefined.
|
|
360
|
+
* @returns the matched marker source text (one entry per matched script key).
|
|
361
|
+
*/
|
|
362
|
+
function scanMaliciousScripts(packageJson) {
|
|
363
|
+
if (typeof packageJson !== "object" || packageJson === null) return [];
|
|
364
|
+
const scripts = packageJson.scripts;
|
|
365
|
+
if (typeof scripts !== "object" || scripts === null) return [];
|
|
366
|
+
const found = [];
|
|
367
|
+
for (const key of LIFECYCLE_SCRIPT_KEYS) {
|
|
368
|
+
const value = scripts[key];
|
|
369
|
+
if (typeof value !== "string") continue;
|
|
370
|
+
for (const marker of MALICIOUS_SCRIPT_MARKERS) if (marker.test(value)) {
|
|
371
|
+
found.push(`${key}: ${value}`);
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return found;
|
|
376
|
+
}
|
|
377
|
+
/** License classification from an SPDX id (or npm license string). */
|
|
378
|
+
function licenseCompliance(spdx) {
|
|
379
|
+
if (spdx === void 0 || LICENSE_MISSING.has(spdx)) return {
|
|
380
|
+
known: false,
|
|
381
|
+
acceptable: false
|
|
382
|
+
};
|
|
383
|
+
const normalized = spdx.replace(/^\(|\)$/gu, "").trim();
|
|
384
|
+
if (normalized === "") return {
|
|
385
|
+
known: false,
|
|
386
|
+
acceptable: false
|
|
387
|
+
};
|
|
388
|
+
return {
|
|
389
|
+
known: true,
|
|
390
|
+
acceptable: LICENSE_ALLOWLIST.has(normalized)
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
/** The install-success dimension, consuming reserved test-drive evidence. */
|
|
394
|
+
function evaluateInstall(input) {
|
|
395
|
+
const base = {
|
|
396
|
+
dimension: "install",
|
|
397
|
+
weight: input.config.weights.install
|
|
398
|
+
};
|
|
399
|
+
const drive = input.testDrive;
|
|
400
|
+
if (drive === void 0 || drive.verdict === void 0) return {
|
|
401
|
+
...base,
|
|
402
|
+
status: "no-evidence",
|
|
403
|
+
score: 0,
|
|
404
|
+
summary: "no dsh-test-drive result recorded for this target (install success unmeasured)",
|
|
405
|
+
evidence: [link("test-drive", "no test-drive record found in the test_drive domain", input.now)]
|
|
406
|
+
};
|
|
407
|
+
const evidence = [link("test-drive", drive.detail ?? `test-drive verdict ${drive.verdict}`, drive.observedAt ?? input.now)];
|
|
408
|
+
switch (drive.verdict) {
|
|
409
|
+
case "pass": return {
|
|
410
|
+
...base,
|
|
411
|
+
status: "pass",
|
|
412
|
+
score: 100,
|
|
413
|
+
summary: "install smoke verified by dsh-test-drive",
|
|
414
|
+
evidence
|
|
415
|
+
};
|
|
416
|
+
case "partial": return {
|
|
417
|
+
...base,
|
|
418
|
+
status: "warn",
|
|
419
|
+
score: 60,
|
|
420
|
+
summary: "installed but a later assurance was missing (test-drive partial)",
|
|
421
|
+
evidence
|
|
422
|
+
};
|
|
423
|
+
case "fail": return {
|
|
424
|
+
...base,
|
|
425
|
+
status: "fail",
|
|
426
|
+
score: 0,
|
|
427
|
+
summary: "install or boot smoke failed (test-drive fail)",
|
|
428
|
+
evidence
|
|
429
|
+
};
|
|
430
|
+
default: return {
|
|
431
|
+
...base,
|
|
432
|
+
status: "no-evidence",
|
|
433
|
+
score: 0,
|
|
434
|
+
summary: "test-drive ran but nothing decisive (unknown)",
|
|
435
|
+
evidence
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
/** The maintenance dimension: commit recency, issue age, and archived status. */
|
|
440
|
+
function evaluateMaintenance(input) {
|
|
441
|
+
const base = {
|
|
442
|
+
dimension: "maintenance",
|
|
443
|
+
weight: input.config.weights.maintenance
|
|
444
|
+
};
|
|
445
|
+
const { config, now } = input;
|
|
446
|
+
const nowDate = new Date(now);
|
|
447
|
+
const evidence = [];
|
|
448
|
+
const lastActivity = input.repo?.lastCommitAt ?? input.npm?.modifiedAt;
|
|
449
|
+
if (input.repo?.archived ?? false) {
|
|
450
|
+
evidence.push(link("gh-api", "repository is archived", now));
|
|
451
|
+
return {
|
|
452
|
+
...base,
|
|
453
|
+
status: "fail",
|
|
454
|
+
score: 10,
|
|
455
|
+
summary: "repository is archived",
|
|
456
|
+
evidence
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
if (lastActivity === void 0 && input.repo?.oldestOpenIssueAt === void 0) return {
|
|
460
|
+
...base,
|
|
461
|
+
status: "no-evidence",
|
|
462
|
+
score: 0,
|
|
463
|
+
summary: "no commit or publish timestamp available",
|
|
464
|
+
evidence: [link("gh-api", "no pushed_at / commit / npm modified timestamp", now)]
|
|
465
|
+
};
|
|
466
|
+
let status = "pass";
|
|
467
|
+
let score = 100;
|
|
468
|
+
if (lastActivity !== void 0) {
|
|
469
|
+
evidence.push(link("gh-api", `last activity ${lastActivity}`, now));
|
|
470
|
+
const days = daysSince(lastActivity, nowDate);
|
|
471
|
+
if (!Number.isNaN(days)) {
|
|
472
|
+
if (days > config.staleCommitFailDays) {
|
|
473
|
+
status = "fail";
|
|
474
|
+
score = 20;
|
|
475
|
+
} else if (days > config.staleCommitWarnDays) {
|
|
476
|
+
status = "warn";
|
|
477
|
+
score = 60;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const oldestIssue = input.repo?.oldestOpenIssueAt;
|
|
482
|
+
if (oldestIssue !== void 0) {
|
|
483
|
+
evidence.push(link("gh-api", `oldest open issue created ${oldestIssue}`, now));
|
|
484
|
+
const days = daysSince(oldestIssue, nowDate);
|
|
485
|
+
if (!Number.isNaN(days)) {
|
|
486
|
+
if (days > config.staleIssueFailDays && status !== "fail") {
|
|
487
|
+
status = "fail";
|
|
488
|
+
score = Math.min(score, 20);
|
|
489
|
+
} else if (days > config.staleIssueWarnDays && status !== "fail") {
|
|
490
|
+
status = "warn";
|
|
491
|
+
score = Math.min(score, 60);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
const openIssues = input.repo?.openIssues;
|
|
496
|
+
const summary = status === "pass" ? `active (${lastActivity ?? "no timestamp"}${openIssues === void 0 ? "" : `; ${openIssues} open issues`})` : status === "warn" ? `slowing (last activity ${lastActivity ?? "unknown"})` : `stale (last activity ${lastActivity ?? "unknown"})`;
|
|
497
|
+
return {
|
|
498
|
+
...base,
|
|
499
|
+
status,
|
|
500
|
+
score,
|
|
501
|
+
summary,
|
|
502
|
+
evidence
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
/** The documentation dimension: multi-language README, CHANGELOG, SECURITY presence. */
|
|
506
|
+
function evaluateDocumentation(input) {
|
|
507
|
+
const base = {
|
|
508
|
+
dimension: "documentation",
|
|
509
|
+
weight: input.config.weights.documentation
|
|
510
|
+
};
|
|
511
|
+
const rawFiles = input.repo?.files;
|
|
512
|
+
if (rawFiles === void 0 || rawFiles.length === 0) return {
|
|
513
|
+
...base,
|
|
514
|
+
status: "no-evidence",
|
|
515
|
+
score: 0,
|
|
516
|
+
summary: "repository file list unavailable (cannot inspect docs)",
|
|
517
|
+
evidence: [link("gh-api", "no contents listing for the target", input.now)]
|
|
518
|
+
};
|
|
519
|
+
const files = rawFiles.map((name) => name.toLowerCase());
|
|
520
|
+
const has = (name) => files.includes(name);
|
|
521
|
+
const readme = has("readme.md");
|
|
522
|
+
const languages = [
|
|
523
|
+
"readme.zh.md",
|
|
524
|
+
"readme.es.md",
|
|
525
|
+
"readme.pt.md",
|
|
526
|
+
"readme.hi.md"
|
|
527
|
+
].filter(has).length;
|
|
528
|
+
const changelog = has("changelog.md");
|
|
529
|
+
const security = has("security.md");
|
|
530
|
+
const evidence = [link("gh-api", `root files: ${files.join(", ")}`, input.now)];
|
|
531
|
+
if (!readme) return {
|
|
532
|
+
...base,
|
|
533
|
+
status: "fail",
|
|
534
|
+
score: 0,
|
|
535
|
+
summary: "README.md missing",
|
|
536
|
+
evidence
|
|
537
|
+
};
|
|
538
|
+
let score = 40 + languages * 10 + (changelog ? 10 : 0) + (security ? 10 : 0);
|
|
539
|
+
let status;
|
|
540
|
+
if (score >= 80) status = "pass";
|
|
541
|
+
else if (score >= 50) status = "warn";
|
|
542
|
+
else status = "fail";
|
|
543
|
+
return {
|
|
544
|
+
...base,
|
|
545
|
+
status,
|
|
546
|
+
score,
|
|
547
|
+
summary: `${languages}/4 language READMEs${changelog ? " + CHANGELOG" : ""}${security ? " + SECURITY" : ""}`,
|
|
548
|
+
evidence
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
/** The security dimension: license compliance, secret leaks, and malicious script markers. */
|
|
552
|
+
function evaluateSecurity(input) {
|
|
553
|
+
const base = {
|
|
554
|
+
dimension: "security",
|
|
555
|
+
weight: input.config.weights.security
|
|
556
|
+
};
|
|
557
|
+
const { now } = input;
|
|
558
|
+
const evidence = [];
|
|
559
|
+
const spdx = input.repo?.licenseSpdx ?? input.npm?.license;
|
|
560
|
+
const license = licenseCompliance(spdx);
|
|
561
|
+
if (spdx !== void 0) evidence.push(link(input.repo?.licenseSpdx !== void 0 ? "gh-api" : "npm-cli", `license ${spdx}`, now));
|
|
562
|
+
const packageJson = input.repo?.packageJson ?? input.npm?.packageJson;
|
|
563
|
+
const scannedText = [
|
|
564
|
+
packageJson === void 0 ? "" : JSON.stringify(packageJson),
|
|
565
|
+
input.repo?.cordisPatch ?? "",
|
|
566
|
+
input.repo?.readmeText ?? ""
|
|
567
|
+
].join("\n");
|
|
568
|
+
const secretPatterns = scanSecretPatterns(scannedText);
|
|
569
|
+
if (secretPatterns.length > 0) evidence.push(link("file-content", `secret patterns found: ${secretPatterns.join(", ")}`, now));
|
|
570
|
+
const malicious = scanMaliciousScripts(packageJson);
|
|
571
|
+
if (malicious.length > 0) evidence.push(link("file-content", `suspicious install scripts: ${malicious.join("; ")}`, now));
|
|
572
|
+
if (!license.known && secretPatterns.length === 0 && malicious.length === 0 && scannedText.trim() === "") return {
|
|
573
|
+
...base,
|
|
574
|
+
status: "no-evidence",
|
|
575
|
+
score: 0,
|
|
576
|
+
summary: "no license or content available to scan",
|
|
577
|
+
evidence: [link("gh-api", "no license, package.json, or patch content", now)]
|
|
578
|
+
};
|
|
579
|
+
let status = "pass";
|
|
580
|
+
let score = license.acceptable ? 100 : license.known ? 40 : 0;
|
|
581
|
+
if (!license.known) status = "fail";
|
|
582
|
+
else if (!license.acceptable) status = "warn";
|
|
583
|
+
if (secretPatterns.length > 0 || malicious.length > 0) {
|
|
584
|
+
status = "fail";
|
|
585
|
+
score = Math.min(score, 10);
|
|
586
|
+
}
|
|
587
|
+
const summary = secretPatterns.length > 0 ? `secrets leaked (${secretPatterns.join(", ")})` : malicious.length > 0 ? `suspicious install scripts detected` : license.acceptable ? `license ${spdx ?? "ok"}` : license.known ? `unrecognized license ${spdx ?? ""}` : "license missing";
|
|
588
|
+
return {
|
|
589
|
+
...base,
|
|
590
|
+
status,
|
|
591
|
+
score,
|
|
592
|
+
summary,
|
|
593
|
+
evidence
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
/** The compliance dimension: `dsh.bundle.patch` manifest and `dsh-plugin` topic/keyword. */
|
|
597
|
+
function evaluateCompliance(input) {
|
|
598
|
+
const base = {
|
|
599
|
+
dimension: "compliance",
|
|
600
|
+
weight: input.config.weights.compliance
|
|
601
|
+
};
|
|
602
|
+
const { now } = input;
|
|
603
|
+
const evidence = [];
|
|
604
|
+
const packageJson = input.repo?.packageJson ?? input.npm?.packageJson;
|
|
605
|
+
let bundlePatch = false;
|
|
606
|
+
let dshManifest = false;
|
|
607
|
+
if (typeof packageJson === "object" && packageJson !== null) {
|
|
608
|
+
const dsh = packageJson.dsh;
|
|
609
|
+
if (typeof dsh === "object" && dsh !== null) {
|
|
610
|
+
dshManifest = true;
|
|
611
|
+
const bundle = dsh.bundle;
|
|
612
|
+
if (typeof bundle === "object" && bundle !== null && typeof bundle.patch === "string") bundlePatch = bundle.patch.length > 0;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
if (dshManifest) evidence.push(link("file-content", `dsh manifest ${bundlePatch ? "with" : "without"} bundle.patch`, now));
|
|
616
|
+
const topics = input.repo?.topics ?? [];
|
|
617
|
+
const keywords = input.npm?.keywords ?? [];
|
|
618
|
+
const topicLabel = topics.includes("dsh-plugin") || keywords.includes("dsh-plugin");
|
|
619
|
+
if (topicLabel) evidence.push(link("gh-api", "topic/keyword 'dsh-plugin' present", now));
|
|
620
|
+
if (!dshManifest && !topicLabel && packageJson === void 0 && topics.length === 0 && keywords.length === 0) return {
|
|
621
|
+
...base,
|
|
622
|
+
status: "no-evidence",
|
|
623
|
+
score: 0,
|
|
624
|
+
summary: "no manifest, topics, or keywords available",
|
|
625
|
+
evidence: [link("gh-api", "no package.json or topic/keyword data", now)]
|
|
626
|
+
};
|
|
627
|
+
let score = 0;
|
|
628
|
+
if (bundlePatch) score += 60;
|
|
629
|
+
else if (dshManifest) score += 20;
|
|
630
|
+
if (topicLabel) score += 20;
|
|
631
|
+
if (topics.length > 0) score += Math.min(topics.length, 10);
|
|
632
|
+
score = Math.min(score, 100);
|
|
633
|
+
const status = score >= 80 ? "pass" : score >= 40 ? "warn" : "fail";
|
|
634
|
+
const summary = bundlePatch ? "dsh.bundle.patch declared" : dshManifest ? "dsh manifest without bundle.patch" : "no dsh manifest";
|
|
635
|
+
return {
|
|
636
|
+
...base,
|
|
637
|
+
status,
|
|
638
|
+
score,
|
|
639
|
+
summary,
|
|
640
|
+
evidence
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
/** Evaluate all five dimensions for one target. */
|
|
644
|
+
function evaluateAll(input) {
|
|
645
|
+
return {
|
|
646
|
+
install: evaluateInstall(input),
|
|
647
|
+
maintenance: evaluateMaintenance(input),
|
|
648
|
+
documentation: evaluateDocumentation(input),
|
|
649
|
+
security: evaluateSecurity(input),
|
|
650
|
+
compliance: evaluateCompliance(input)
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
//#endregion
|
|
654
|
+
//#region src/probe.ts
|
|
655
|
+
/**
|
|
656
|
+
* The evidence-gathering layer: runs `gh` and `npm` as managed subprocesses
|
|
657
|
+
* over `ctx.subprocess` and parses their output into structured facts. Every
|
|
658
|
+
* command records an {@link EvidenceLink} (source, command, audit timestamp);
|
|
659
|
+
* a command that fails or returns unparsable output contributes NO fact — the
|
|
660
|
+
* consuming dimension then reports `no-evidence` honestly, never a number.
|
|
661
|
+
*
|
|
662
|
+
* No shell is ever involved (argv-only), so hostile target specs stay argv
|
|
663
|
+
* entries. `gh` reads its own credential store (the provider scrubs ambient
|
|
664
|
+
* env credentials), and every recorded string is sanitized before it leaves
|
|
665
|
+
* this layer.
|
|
666
|
+
*
|
|
667
|
+
* @module dsh-score/probe
|
|
668
|
+
*/
|
|
669
|
+
/** Cap on collected output per probe stream (in-memory tail). */
|
|
670
|
+
const COLLECT_MAX_BYTES = 65536;
|
|
671
|
+
/** Cap on the complete spill file per stream. */
|
|
672
|
+
const SPILL_MAX_BYTES = 1048576;
|
|
673
|
+
/** Grace between SIGTERM and SIGKILL for a probe process. */
|
|
674
|
+
const PROBE_GRACE_MS = 5e3;
|
|
675
|
+
/** Regex a GitHub owner or repo segment must satisfy (blocks endpoint injection). */
|
|
676
|
+
const SEGMENT_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
677
|
+
/** Parse the JS target out of an npm-generated `.cmd`/`.bat` shim (Windows). */
|
|
678
|
+
function parseNpmShim(shimPath, shimText) {
|
|
679
|
+
const rel = /"%dp0%\\(?<rel>node_modules\\[^"]+\.(?:js|cjs|mjs))"/u.exec(shimText)?.groups?.rel;
|
|
680
|
+
if (rel === void 0) throw new Error(`dsh-score: cannot parse npm shim ${shimPath} (no dp0-relative JS target found)`);
|
|
681
|
+
return join(dirname(shimPath), rel);
|
|
682
|
+
}
|
|
683
|
+
/** Whether a target spec names a git/github source. */
|
|
684
|
+
function isRepoSpec(spec) {
|
|
685
|
+
return /^github:|^git\+|^git@|\.git(?:#|$)|^https?:\/\//u.test(spec) || spec.includes("/") && !spec.startsWith("@");
|
|
686
|
+
}
|
|
687
|
+
/** Classify a sanitized target spec as a repo or an npm package. */
|
|
688
|
+
function classifyTarget(spec) {
|
|
689
|
+
if (isRepoSpec(spec)) return "repo";
|
|
690
|
+
return "npm";
|
|
691
|
+
}
|
|
692
|
+
/** Strip a trailing `.git` suffix from a repo name. */
|
|
693
|
+
function withoutGitSuffix(name) {
|
|
694
|
+
return name.endsWith(".git") ? name.slice(0, -4) : name;
|
|
695
|
+
}
|
|
696
|
+
/** Extract owner/repo from a target spec; undefined when it does not name a GitHub repo. */
|
|
697
|
+
function parseRepoRef(spec) {
|
|
698
|
+
for (const pattern of [
|
|
699
|
+
/^github:([^/#]+)\/([^/#]+)/u,
|
|
700
|
+
/^git\+https?:\/\/github\.com\/([^/]+)\/([^/#]+)/u,
|
|
701
|
+
/^git@github\.com:([^/]+)\/([^/]+)/u,
|
|
702
|
+
/^https?:\/\/github\.com\/([^/]+)\/([^/#]+)/u,
|
|
703
|
+
/^([^/]+)\/([^/]+)$/u
|
|
704
|
+
]) {
|
|
705
|
+
const match = pattern.exec(spec);
|
|
706
|
+
const owner = match?.[1];
|
|
707
|
+
const repo = match?.[2];
|
|
708
|
+
if (owner === void 0 || repo === void 0) continue;
|
|
709
|
+
const clean = withoutGitSuffix(repo);
|
|
710
|
+
if (SEGMENT_RE.test(owner) && SEGMENT_RE.test(clean)) return {
|
|
711
|
+
owner,
|
|
712
|
+
repo: clean
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
/** Extract owner/repo from an npm `repository.url` (github only). */
|
|
717
|
+
function extractRepoFromNpmUrl(url) {
|
|
718
|
+
const match = /github\.com[/:]([^/]+)\/([^/#.]+)/u.exec(url);
|
|
719
|
+
const owner = match?.[1];
|
|
720
|
+
const repo = match?.[2];
|
|
721
|
+
if (owner === void 0 || repo === void 0) return void 0;
|
|
722
|
+
const clean = withoutGitSuffix(repo);
|
|
723
|
+
if (SEGMENT_RE.test(owner) && SEGMENT_RE.test(clean)) return {
|
|
724
|
+
owner,
|
|
725
|
+
repo: clean
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
/** Parse the `gh api repos/<owner>/<repo>` object into repo facts. */
|
|
729
|
+
function parseRepoMeta(json) {
|
|
730
|
+
const facts = {};
|
|
731
|
+
if (typeof json !== "object" || json === null) return facts;
|
|
732
|
+
const record = json;
|
|
733
|
+
if (typeof record.pushed_at === "string") facts.pushedAt = record.pushed_at;
|
|
734
|
+
if (typeof record.open_issues_count === "number") facts.openIssues = record.open_issues_count;
|
|
735
|
+
if (typeof record.default_branch === "string") facts.defaultBranch = record.default_branch;
|
|
736
|
+
if (typeof record.archived === "boolean") facts.archived = record.archived;
|
|
737
|
+
const license = record.license;
|
|
738
|
+
if (typeof license === "object" && license !== null && typeof license.spdx_id === "string") facts.licenseSpdx = license.spdx_id;
|
|
739
|
+
else facts.licenseSpdx = "MISSING";
|
|
740
|
+
if (Array.isArray(record.topics)) facts.topics = record.topics.filter((topic) => typeof topic === "string");
|
|
741
|
+
return facts;
|
|
742
|
+
}
|
|
743
|
+
/** Parse the newest commit date from `gh api .../commits?per_page=1`. */
|
|
744
|
+
function parseLatestCommit(json) {
|
|
745
|
+
if (!Array.isArray(json) || json.length === 0) return void 0;
|
|
746
|
+
const head = json[0];
|
|
747
|
+
if (typeof head !== "object" || head === null) return void 0;
|
|
748
|
+
const commit = head.commit;
|
|
749
|
+
if (typeof commit !== "object" || commit === null) return void 0;
|
|
750
|
+
const date = commit.committer;
|
|
751
|
+
if (typeof date !== "object" || date === null) return void 0;
|
|
752
|
+
const value = date.date;
|
|
753
|
+
return typeof value === "string" ? value : void 0;
|
|
754
|
+
}
|
|
755
|
+
/** Parse the root file-name list from `gh api .../contents`. */
|
|
756
|
+
function parseFileList(json) {
|
|
757
|
+
if (!Array.isArray(json)) return [];
|
|
758
|
+
const names = [];
|
|
759
|
+
for (const entry of json) {
|
|
760
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
761
|
+
const name = entry.name;
|
|
762
|
+
if (typeof name === "string") names.push(name);
|
|
763
|
+
}
|
|
764
|
+
return names;
|
|
765
|
+
}
|
|
766
|
+
/** Decode the `content` field of a `gh api .../contents/<file>` response. */
|
|
767
|
+
function parseGithubContent(json) {
|
|
768
|
+
if (typeof json !== "object" || json === null) return void 0;
|
|
769
|
+
const content = json.content;
|
|
770
|
+
if (typeof content !== "string") return void 0;
|
|
771
|
+
if (!/^[A-Za-z0-9+/=\s]*$/.test(content)) return void 0;
|
|
772
|
+
try {
|
|
773
|
+
return Buffer.from(content, "base64").toString("utf8");
|
|
774
|
+
} catch {
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
/** Parse the oldest open issue's creation date from `gh api .../issues`. */
|
|
779
|
+
function parseOldestOpenIssue(json) {
|
|
780
|
+
if (!Array.isArray(json) || json.length === 0) return void 0;
|
|
781
|
+
const head = json[0];
|
|
782
|
+
if (typeof head !== "object" || head === null) return void 0;
|
|
783
|
+
const created = head.created_at;
|
|
784
|
+
return typeof created === "string" ? created : void 0;
|
|
785
|
+
}
|
|
786
|
+
/** Parse the `npm view <pkg> --json` manifest into npm facts. */
|
|
787
|
+
function parseNpmView(json) {
|
|
788
|
+
const facts = {};
|
|
789
|
+
if (typeof json !== "object" || json === null) return facts;
|
|
790
|
+
const record = json;
|
|
791
|
+
if (typeof record.name === "string") facts.name = record.name;
|
|
792
|
+
if (typeof record.version === "string") facts.version = record.version;
|
|
793
|
+
const license = record.license;
|
|
794
|
+
if (typeof license === "string") facts.license = license;
|
|
795
|
+
else if (typeof license === "object" && license !== null && typeof license.type === "string") facts.license = license.type;
|
|
796
|
+
const repository = record.repository;
|
|
797
|
+
if (typeof repository === "object" && repository !== null && typeof repository.url === "string") facts.repositoryUrl = repository.url;
|
|
798
|
+
if (Array.isArray(record.keywords)) facts.keywords = record.keywords.filter((keyword) => typeof keyword === "string");
|
|
799
|
+
const time = record.time;
|
|
800
|
+
if (typeof time === "object" && time !== null && typeof time.modified === "string") facts.modifiedAt = time.modified;
|
|
801
|
+
facts.packageJson = json;
|
|
802
|
+
return facts;
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Locate and run `gh`/`npm` through the subprocess seam, cache each located
|
|
806
|
+
* argv prefix, and parse responses. `npm` on Windows resolves to a `.cmd` shim
|
|
807
|
+
* that cannot be spawned directly — the shim is parsed into `[node, js]`.
|
|
808
|
+
*/
|
|
809
|
+
var ProbeDriver = class {
|
|
810
|
+
deps;
|
|
811
|
+
located = /* @__PURE__ */ new Map();
|
|
812
|
+
constructor(deps) {
|
|
813
|
+
this.deps = deps;
|
|
814
|
+
}
|
|
815
|
+
/** Resolve the spawn argv prefix `[program, ...args]` for one CLI. */
|
|
816
|
+
locate(program) {
|
|
817
|
+
let pending = this.located.get(program);
|
|
818
|
+
if (pending === void 0) {
|
|
819
|
+
pending = this.resolveLocation(program);
|
|
820
|
+
this.located.set(program, pending);
|
|
821
|
+
}
|
|
822
|
+
return pending;
|
|
823
|
+
}
|
|
824
|
+
async resolveLocation(program) {
|
|
825
|
+
const found = await this.deps.ctx.subprocess.resolveExecutable(program);
|
|
826
|
+
if (process.platform === "win32" && /\.(?:cmd|bat)$/iu.test(found)) {
|
|
827
|
+
const target = parseNpmShim(found, await readFile(found, "utf8"));
|
|
828
|
+
this.deps.log(`score: ${program} resolved to ${target} (via shim ${found})`);
|
|
829
|
+
return [process.execPath, target];
|
|
830
|
+
}
|
|
831
|
+
if (/\.ps1$/iu.test(found)) throw new Error(`dsh-score: ${program} resolved to the PowerShell shim ${found}; install the native CLI or npm shim`);
|
|
832
|
+
return [found];
|
|
833
|
+
}
|
|
834
|
+
/** Run one CLI invocation with collected stdio and a deadline. */
|
|
835
|
+
async run(program, args, signal) {
|
|
836
|
+
const started = Date.now();
|
|
837
|
+
const deadline = AbortSignal.timeout(this.deps.config.probeTimeoutMs);
|
|
838
|
+
const merged = signal === void 0 ? deadline : AbortSignal.any([signal, deadline]);
|
|
839
|
+
let handle;
|
|
840
|
+
try {
|
|
841
|
+
const [resolved, ...prefix] = await this.locate(program);
|
|
842
|
+
if (resolved === void 0) throw new Error(`${program} location resolved to an empty argv`);
|
|
843
|
+
const spec = {
|
|
844
|
+
argv: [
|
|
845
|
+
resolved,
|
|
846
|
+
...prefix,
|
|
847
|
+
...args
|
|
848
|
+
],
|
|
849
|
+
cwd: process.cwd(),
|
|
850
|
+
stdio: {
|
|
851
|
+
stdin: "ignore",
|
|
852
|
+
stdout: {
|
|
853
|
+
maxBytes: COLLECT_MAX_BYTES,
|
|
854
|
+
spill: { maxBytes: SPILL_MAX_BYTES }
|
|
855
|
+
},
|
|
856
|
+
stderr: {
|
|
857
|
+
maxBytes: COLLECT_MAX_BYTES,
|
|
858
|
+
spill: { maxBytes: SPILL_MAX_BYTES }
|
|
859
|
+
}
|
|
860
|
+
},
|
|
861
|
+
graceMs: PROBE_GRACE_MS,
|
|
862
|
+
signal: merged
|
|
863
|
+
};
|
|
864
|
+
handle = this.deps.ctx.subprocess.spawn(spec);
|
|
865
|
+
} catch (error) {
|
|
866
|
+
return {
|
|
867
|
+
exitCode: null,
|
|
868
|
+
stdout: "",
|
|
869
|
+
stderr: `spawn failed: ${String(error)}`,
|
|
870
|
+
truncated: false,
|
|
871
|
+
durationMs: Date.now() - started,
|
|
872
|
+
timedOut: false
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
let outcome;
|
|
876
|
+
try {
|
|
877
|
+
outcome = await handle.done;
|
|
878
|
+
} catch (error) {
|
|
879
|
+
return {
|
|
880
|
+
exitCode: null,
|
|
881
|
+
stdout: "",
|
|
882
|
+
stderr: `spawn failed: ${String(error)}`,
|
|
883
|
+
truncated: false,
|
|
884
|
+
durationMs: Date.now() - started,
|
|
885
|
+
timedOut: false
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
const stdoutRead = handle.collected.stdout?.readFrom(0);
|
|
889
|
+
const stderrRead = handle.collected.stderr?.readFrom(0);
|
|
890
|
+
const stdout = stdoutRead?.text ?? "";
|
|
891
|
+
const stderr = stderrRead?.text ?? "";
|
|
892
|
+
const truncated = (stdoutRead?.lossy ?? false) || (stderrRead?.lossy ?? false);
|
|
893
|
+
return {
|
|
894
|
+
exitCode: outcome.exitCode,
|
|
895
|
+
stdout,
|
|
896
|
+
stderr,
|
|
897
|
+
truncated,
|
|
898
|
+
durationMs: Date.now() - started,
|
|
899
|
+
timedOut: deadline.aborted && (signal === void 0 || !signal.aborted)
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
/** Run one `gh api <endpoint>` and parse JSON; records an audit link either way. */
|
|
903
|
+
async ghJson(endpoint, signal) {
|
|
904
|
+
const evidence = {
|
|
905
|
+
source: "gh-api",
|
|
906
|
+
detail: `gh api ${endpoint}`,
|
|
907
|
+
observedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
908
|
+
};
|
|
909
|
+
const run = await this.run("gh", ["api", endpoint], signal);
|
|
910
|
+
if (run.exitCode !== 0) return { evidence: [evidence] };
|
|
911
|
+
const trimmed = run.stdout.trim();
|
|
912
|
+
if (trimmed.length === 0) return { evidence: [evidence] };
|
|
913
|
+
try {
|
|
914
|
+
return {
|
|
915
|
+
value: JSON.parse(trimmed),
|
|
916
|
+
evidence: [evidence]
|
|
917
|
+
};
|
|
918
|
+
} catch {
|
|
919
|
+
return { evidence: [evidence] };
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
/** Run one `npm view <pkg> --json`; records an audit link either way. */
|
|
923
|
+
async npmJson(pkgSpec, signal) {
|
|
924
|
+
const evidence = {
|
|
925
|
+
source: "npm-cli",
|
|
926
|
+
detail: `npm view ${pkgSpec} --json`,
|
|
927
|
+
observedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
928
|
+
};
|
|
929
|
+
const run = await this.run("npm", [
|
|
930
|
+
"view",
|
|
931
|
+
pkgSpec,
|
|
932
|
+
"--json"
|
|
933
|
+
], signal);
|
|
934
|
+
if (run.exitCode !== 0) return { evidence: [evidence] };
|
|
935
|
+
const trimmed = run.stdout.trim();
|
|
936
|
+
if (trimmed.length === 0) return { evidence: [evidence] };
|
|
937
|
+
try {
|
|
938
|
+
return {
|
|
939
|
+
value: JSON.parse(trimmed),
|
|
940
|
+
evidence: [evidence]
|
|
941
|
+
};
|
|
942
|
+
} catch {
|
|
943
|
+
return { evidence: [evidence] };
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
/** Probe one GitHub repo: metadata, latest commit, file list, and key file contents. */
|
|
947
|
+
async probeRepo(ref, signal) {
|
|
948
|
+
const facts = {};
|
|
949
|
+
const evidence = [];
|
|
950
|
+
const base = `repos/${ref.owner}/${ref.repo}`;
|
|
951
|
+
const meta = await this.ghJson(base, signal);
|
|
952
|
+
evidence.push(...meta.evidence);
|
|
953
|
+
Object.assign(facts, parseRepoMeta(meta.value));
|
|
954
|
+
const commits = await this.ghJson(`${base}/commits?per_page=1`, signal);
|
|
955
|
+
evidence.push(...commits.evidence);
|
|
956
|
+
const lastCommitAt = parseLatestCommit(commits.value);
|
|
957
|
+
if (lastCommitAt !== void 0) facts.lastCommitAt = lastCommitAt;
|
|
958
|
+
const listing = await this.ghJson(`${base}/contents`, signal);
|
|
959
|
+
evidence.push(...listing.evidence);
|
|
960
|
+
const files = parseFileList(listing.value);
|
|
961
|
+
if (files.length > 0) facts.files = files.map((name) => name.toLowerCase());
|
|
962
|
+
const packageJson = await this.ghJson(`${base}/contents/package.json`, signal);
|
|
963
|
+
evidence.push(...packageJson.evidence);
|
|
964
|
+
const packageJsonText = parseGithubContent(packageJson.value);
|
|
965
|
+
if (packageJsonText !== void 0) try {
|
|
966
|
+
facts.packageJson = JSON.parse(packageJsonText);
|
|
967
|
+
} catch {
|
|
968
|
+
facts.packageJson = void 0;
|
|
969
|
+
}
|
|
970
|
+
const patch = await this.ghJson(`${base}/contents/cordis.patch.yml`, signal);
|
|
971
|
+
evidence.push(...patch.evidence);
|
|
972
|
+
const patchText = parseGithubContent(patch.value);
|
|
973
|
+
if (patchText !== void 0) facts.cordisPatch = patchText;
|
|
974
|
+
const readme = await this.ghJson(`${base}/contents/README.md`, signal);
|
|
975
|
+
evidence.push(...readme.evidence);
|
|
976
|
+
const readmeText = parseGithubContent(readme.value);
|
|
977
|
+
if (readmeText !== void 0) facts.readmeText = readmeText;
|
|
978
|
+
const issues = await this.ghJson(`${base}/issues?state=open&per_page=1&sort=created&direction=asc`, signal);
|
|
979
|
+
evidence.push(...issues.evidence);
|
|
980
|
+
const oldestOpenIssueAt = parseOldestOpenIssue(issues.value);
|
|
981
|
+
if (oldestOpenIssueAt !== void 0) facts.oldestOpenIssueAt = oldestOpenIssueAt;
|
|
982
|
+
return {
|
|
983
|
+
facts,
|
|
984
|
+
evidence
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
/** Probe one npm package manifest; a github repository URL is also parsed out. */
|
|
988
|
+
async probeNpm(spec, signal) {
|
|
989
|
+
const outcome = await this.npmJson(spec, signal);
|
|
990
|
+
return {
|
|
991
|
+
facts: parseNpmView(outcome.value),
|
|
992
|
+
evidence: outcome.evidence
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
};
|
|
996
|
+
//#endregion
|
|
997
|
+
//#region src/version.ts
|
|
998
|
+
/**
|
|
999
|
+
* Producer version, kept in lockstep with `package.json` by
|
|
1000
|
+
* `scripts/release.mjs`; `tests/version.spec.ts` trips when they drift.
|
|
1001
|
+
* The structured score records it as `score.pluginVersion`.
|
|
1002
|
+
* @module dsh-score/version
|
|
1003
|
+
*/
|
|
1004
|
+
const VERSION = "0.1.0";
|
|
1005
|
+
//#endregion
|
|
1006
|
+
//#region src/score.ts
|
|
1007
|
+
/**
|
|
1008
|
+
* The single-target scoring pipeline: classify and sanitize the target, gather
|
|
1009
|
+
* evidence through the {@link ProbeDriver}, read reserved test-drive install
|
|
1010
|
+
* evidence from the already-open `test_drive` domain (best-effort — absent
|
|
1011
|
+
* means `no-evidence`), evaluate the five dimensions, and persist the score
|
|
1012
|
+
* card keyed by a deterministic hash of the target spec (the score cache).
|
|
1013
|
+
*
|
|
1014
|
+
* Every domain access is best-effort: a missing `storageDomain` service or a
|
|
1015
|
+
* failing medium is logged and the score is still returned — persistence
|
|
1016
|
+
* degrades, scoring never fabricates.
|
|
1017
|
+
*
|
|
1018
|
+
* @module dsh-score/score
|
|
1019
|
+
*/
|
|
1020
|
+
/** Prefix of a score-card key (deterministic, so it doubles as the cache key). */
|
|
1021
|
+
const SCORE_KEY_PREFIX = "sc_";
|
|
1022
|
+
/** Deterministic table key for one target spec (sanitized, hashed). */
|
|
1023
|
+
function scoreKey(spec) {
|
|
1024
|
+
return `sc_${createHash("sha256").update(spec).digest("hex").slice(0, 16)}`;
|
|
1025
|
+
}
|
|
1026
|
+
/** Narrow an opaque stored record to a drive match, or undefined when unrelated. */
|
|
1027
|
+
function parseDriveRecord(value, spec, packageName) {
|
|
1028
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
1029
|
+
const record = value;
|
|
1030
|
+
if (record.schema !== "dsh-test-drive/v1") return void 0;
|
|
1031
|
+
const target = record.target;
|
|
1032
|
+
if (typeof target !== "object" || target === null) return void 0;
|
|
1033
|
+
const targetSpec = target.spec;
|
|
1034
|
+
const resolved = target.resolved;
|
|
1035
|
+
const resolvedName = typeof resolved === "object" && resolved !== null ? resolved.packageName : void 0;
|
|
1036
|
+
if (targetSpec !== spec && (packageName === void 0 || resolvedName !== packageName)) return void 0;
|
|
1037
|
+
const run = record.run;
|
|
1038
|
+
const verdict = record.verdict;
|
|
1039
|
+
const reason = record.verdictReason;
|
|
1040
|
+
if (typeof run !== "object" || run === null) return void 0;
|
|
1041
|
+
const runId = run.runId;
|
|
1042
|
+
const finishedAt = run.finishedAt;
|
|
1043
|
+
if (typeof runId !== "string" || typeof finishedAt !== "string") return void 0;
|
|
1044
|
+
if (verdict !== "pass" && verdict !== "fail" && verdict !== "partial" && verdict !== "unknown") return void 0;
|
|
1045
|
+
return {
|
|
1046
|
+
runId,
|
|
1047
|
+
finishedAt,
|
|
1048
|
+
verdict,
|
|
1049
|
+
reason: typeof reason === "string" ? reason : ""
|
|
1050
|
+
};
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* Read the reserved install evidence from the already-open `test_drive`
|
|
1054
|
+
* domain. Best-effort and never hard-dependent: when the storage-domain
|
|
1055
|
+
* service, the `test_drive` domain, or a matching record is absent, this
|
|
1056
|
+
* returns undefined and the install dimension reports `no-evidence`.
|
|
1057
|
+
*
|
|
1058
|
+
* @param ctx - context carrying the optional `storageDomain` service.
|
|
1059
|
+
* @param spec - the scored target spec.
|
|
1060
|
+
* @param packageName - npm package name (alternative match key), when known.
|
|
1061
|
+
* @returns the newest matching drive verdict, or undefined.
|
|
1062
|
+
*/
|
|
1063
|
+
function readTestDriveEvidence(ctx, spec, packageName) {
|
|
1064
|
+
const storageDomain = ctx.get("storageDomain");
|
|
1065
|
+
if (storageDomain === void 0) return void 0;
|
|
1066
|
+
const handle = storageDomain.get("test_drive");
|
|
1067
|
+
if (handle === void 0) return void 0;
|
|
1068
|
+
let runs;
|
|
1069
|
+
try {
|
|
1070
|
+
runs = handle.table("runs");
|
|
1071
|
+
} catch {
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
let best;
|
|
1075
|
+
for (const [, value] of runs.entries()) {
|
|
1076
|
+
const match = parseDriveRecord(value, spec, packageName);
|
|
1077
|
+
if (match === void 0) continue;
|
|
1078
|
+
if (best === void 0 || match.finishedAt > best.finishedAt) best = match;
|
|
1079
|
+
}
|
|
1080
|
+
if (best === void 0) return void 0;
|
|
1081
|
+
return {
|
|
1082
|
+
verdict: best.verdict,
|
|
1083
|
+
detail: `test-drive run ${best.runId}: ${best.reason}`,
|
|
1084
|
+
observedAt: best.finishedAt
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
/** Gather repo/npm facts for one sanitized target. */
|
|
1088
|
+
async function gather(deps, spec, kind, signal) {
|
|
1089
|
+
const evidence = [];
|
|
1090
|
+
let repo;
|
|
1091
|
+
let npm;
|
|
1092
|
+
if (kind === "repo") {
|
|
1093
|
+
const ref = parseRepoRef(spec);
|
|
1094
|
+
if (ref !== void 0) {
|
|
1095
|
+
const outcome = await deps.driver.probeRepo(ref, signal);
|
|
1096
|
+
repo = outcome.facts;
|
|
1097
|
+
evidence.push(...outcome.evidence);
|
|
1098
|
+
}
|
|
1099
|
+
} else {
|
|
1100
|
+
const outcome = await deps.driver.probeNpm(spec, signal);
|
|
1101
|
+
npm = outcome.facts;
|
|
1102
|
+
evidence.push(...outcome.evidence);
|
|
1103
|
+
if (npm.repositoryUrl !== void 0) {
|
|
1104
|
+
const ref = extractRepoFromNpmUrl(npm.repositoryUrl);
|
|
1105
|
+
if (ref !== void 0) {
|
|
1106
|
+
const repoOutcome = await deps.driver.probeRepo(ref, signal);
|
|
1107
|
+
repo = repoOutcome.facts;
|
|
1108
|
+
evidence.push(...repoOutcome.evidence);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
return {
|
|
1113
|
+
repo,
|
|
1114
|
+
npm,
|
|
1115
|
+
evidence
|
|
1116
|
+
};
|
|
1117
|
+
}
|
|
1118
|
+
/** The single-target scoring pipeline. */
|
|
1119
|
+
var ScoreRunner = class {
|
|
1120
|
+
deps;
|
|
1121
|
+
constructor(deps) {
|
|
1122
|
+
this.deps = deps;
|
|
1123
|
+
}
|
|
1124
|
+
/** Persist one settled score card; a failing medium is logged, never thrown. */
|
|
1125
|
+
async record(result) {
|
|
1126
|
+
try {
|
|
1127
|
+
await (await this.deps.domain()).table("scores").put(result.scoreId, result);
|
|
1128
|
+
} catch (error) {
|
|
1129
|
+
this.deps.log(`score: failed to persist score ${result.scoreId}: ${String(error)}`);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
/** Return a still-fresh cached card for the target, or undefined. */
|
|
1133
|
+
async cached(key) {
|
|
1134
|
+
try {
|
|
1135
|
+
const existing = (await this.deps.domain()).table("scores").get(key);
|
|
1136
|
+
if (existing === void 0) return void 0;
|
|
1137
|
+
if (Date.parse(existing.scoredAt) + this.deps.config.cacheMaxAgeMs > Date.now()) return existing;
|
|
1138
|
+
} catch {}
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Score one target through the full pipeline.
|
|
1142
|
+
*
|
|
1143
|
+
* @param target - raw target spec (repo or npm package).
|
|
1144
|
+
* @param options - caller cancellation and cache override.
|
|
1145
|
+
* @returns the structured score card; never throws for missing evidence.
|
|
1146
|
+
*/
|
|
1147
|
+
async score(target, options = {}) {
|
|
1148
|
+
const spec = sanitizeTarget(target);
|
|
1149
|
+
if (spec.length === 0) throw new Error("dsh-score: target must be a non-empty string");
|
|
1150
|
+
const kind = classifyTarget(spec);
|
|
1151
|
+
const key = scoreKey(spec);
|
|
1152
|
+
if (options.refresh !== true) {
|
|
1153
|
+
const existing = await this.cached(key);
|
|
1154
|
+
if (existing !== void 0) return existing;
|
|
1155
|
+
}
|
|
1156
|
+
const started = Date.now();
|
|
1157
|
+
const gathered = await gather(this.deps, spec, kind, options.signal);
|
|
1158
|
+
const testDrive = readTestDriveEvidence(this.deps.ctx, spec, gathered.npm?.name);
|
|
1159
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1160
|
+
const dimensions = evaluateAll({
|
|
1161
|
+
config: this.deps.config,
|
|
1162
|
+
now,
|
|
1163
|
+
target: {
|
|
1164
|
+
kind,
|
|
1165
|
+
spec
|
|
1166
|
+
},
|
|
1167
|
+
...gathered.repo === void 0 ? {} : { repo: gathered.repo },
|
|
1168
|
+
...gathered.npm === void 0 ? {} : { npm: gathered.npm },
|
|
1169
|
+
...testDrive === void 0 ? {} : { testDrive }
|
|
1170
|
+
});
|
|
1171
|
+
const dimValues = Object.values(dimensions);
|
|
1172
|
+
const total = computeTotal(dimValues);
|
|
1173
|
+
const evidenced = hasEvidence(dimValues);
|
|
1174
|
+
const result = {
|
|
1175
|
+
schema: RESULT_SCHEMA,
|
|
1176
|
+
scoreId: key,
|
|
1177
|
+
target: {
|
|
1178
|
+
kind,
|
|
1179
|
+
spec
|
|
1180
|
+
},
|
|
1181
|
+
scoredAt: now,
|
|
1182
|
+
durationMs: Date.now() - started,
|
|
1183
|
+
pluginVersion: VERSION,
|
|
1184
|
+
dimensions,
|
|
1185
|
+
total,
|
|
1186
|
+
grade: gradeOf(total, evidenced),
|
|
1187
|
+
verdict: verdictOf(total, evidenced)
|
|
1188
|
+
};
|
|
1189
|
+
await this.record(result);
|
|
1190
|
+
return result;
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
//#endregion
|
|
1194
|
+
//#region src/batch.ts
|
|
1195
|
+
/**
|
|
1196
|
+
* The `score-batch` background-job producer over `ctx.jobs`. One job scores a
|
|
1197
|
+
* list of targets (serially or with bounded concurrency), streams per-target
|
|
1198
|
+
* progress lines through `readOutput`, and on settlement writes the leaderboard
|
|
1199
|
+
* record (JSON) into the storage domain plus the latest-leaderboard pointer, so
|
|
1200
|
+
* `score_report` can fetch the Markdown/JSON pair by id.
|
|
1201
|
+
*
|
|
1202
|
+
* @module dsh-score/batch
|
|
1203
|
+
*/
|
|
1204
|
+
/** The job kind this producer registers (declaration-merged into JobKindMap). */
|
|
1205
|
+
const SCORE_BATCH_KIND = "score-batch";
|
|
1206
|
+
/** id prefix of a leaderboard record. */
|
|
1207
|
+
const LEADERBOARD_ID_PREFIX = "lb_";
|
|
1208
|
+
/** Fresh unique leaderboard id. */
|
|
1209
|
+
function freshLeaderboardId() {
|
|
1210
|
+
return `lb_${randomBytes(12).toString("hex")}`;
|
|
1211
|
+
}
|
|
1212
|
+
/** Project one score card into a leaderboard row. */
|
|
1213
|
+
function rowOf(result) {
|
|
1214
|
+
return {
|
|
1215
|
+
target: result.target.spec,
|
|
1216
|
+
kind: result.target.kind,
|
|
1217
|
+
total: result.total,
|
|
1218
|
+
grade: result.grade,
|
|
1219
|
+
verdict: result.verdict,
|
|
1220
|
+
durationMs: result.durationMs,
|
|
1221
|
+
install: result.dimensions.install.status,
|
|
1222
|
+
maintenance: result.dimensions.maintenance.status,
|
|
1223
|
+
documentation: result.dimensions.documentation.status,
|
|
1224
|
+
security: result.dimensions.security.status,
|
|
1225
|
+
compliance: result.dimensions.compliance.status
|
|
1226
|
+
};
|
|
1227
|
+
}
|
|
1228
|
+
/** One progress line after a target settles. */
|
|
1229
|
+
function progressLine(index, total, result) {
|
|
1230
|
+
return `[${index}/${total}] ${result.target.spec} → ${result.grade} (${result.total}/100, ${result.verdict})`;
|
|
1231
|
+
}
|
|
1232
|
+
/** The batch work function shared by the job producer and direct tests. */
|
|
1233
|
+
async function runBatch(deps, targets, signal, onProgress) {
|
|
1234
|
+
const leaderboardId = freshLeaderboardId();
|
|
1235
|
+
const started = Date.now();
|
|
1236
|
+
const runner = new ScoreRunner(deps);
|
|
1237
|
+
const rows = [];
|
|
1238
|
+
const queue = [...targets];
|
|
1239
|
+
async function worker() {
|
|
1240
|
+
for (;;) {
|
|
1241
|
+
if (signal.aborted) return;
|
|
1242
|
+
const target = queue.shift();
|
|
1243
|
+
if (target === void 0) return;
|
|
1244
|
+
const result = await runner.score(target, { signal });
|
|
1245
|
+
rows.push(rowOf(result));
|
|
1246
|
+
onProgress(progressLine(rows.length, targets.length, result));
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
const workers = Array.from({ length: deps.config.batchConcurrency }, () => worker());
|
|
1250
|
+
await Promise.all(workers);
|
|
1251
|
+
rows.sort((a, b) => b.total - a.total);
|
|
1252
|
+
const leaderboard = {
|
|
1253
|
+
schema: RESULT_SCHEMA,
|
|
1254
|
+
id: leaderboardId,
|
|
1255
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1256
|
+
durationMs: Date.now() - started,
|
|
1257
|
+
rows,
|
|
1258
|
+
totals: totalsOf(rows)
|
|
1259
|
+
};
|
|
1260
|
+
try {
|
|
1261
|
+
const domain = await deps.domain();
|
|
1262
|
+
await domain.table("leaderboards").put(leaderboardId, leaderboard);
|
|
1263
|
+
await domain.global.set({
|
|
1264
|
+
leaderboardId,
|
|
1265
|
+
createdAt: leaderboard.createdAt
|
|
1266
|
+
});
|
|
1267
|
+
} catch (error) {
|
|
1268
|
+
deps.log(`score: failed to persist leaderboard ${leaderboardId}: ${String(error)}`);
|
|
1269
|
+
}
|
|
1270
|
+
return {
|
|
1271
|
+
leaderboardId,
|
|
1272
|
+
leaderboard
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
/** One-line settlement summary for notices and the final flush. */
|
|
1276
|
+
function leaderboardSummary(leaderboard) {
|
|
1277
|
+
const { totals } = leaderboard;
|
|
1278
|
+
return `leaderboard ${leaderboard.id}: ${totals.pass} pass, ${totals.warn} warn, ${totals.fail} fail, ${totals.noEvidence} no-evidence (of ${totals.total})`;
|
|
1279
|
+
}
|
|
1280
|
+
/**
|
|
1281
|
+
* Start one `score-batch` background job. The returned id doubles as the
|
|
1282
|
+
* handle the model uses to read output and, via the final flush line, to find
|
|
1283
|
+
* the leaderboard record for `score_report`.
|
|
1284
|
+
*
|
|
1285
|
+
* @param deps - batch dependencies.
|
|
1286
|
+
* @param targets - raw target specs.
|
|
1287
|
+
* @param owner - the owning agent (from the command invocation).
|
|
1288
|
+
* @param label - one-line model-facing label.
|
|
1289
|
+
* @returns the registry-issued job id.
|
|
1290
|
+
*/
|
|
1291
|
+
function startBatchJob(deps, targets, owner, label) {
|
|
1292
|
+
const sanitized = targets.map(sanitizeTarget).filter((spec) => spec.length > 0);
|
|
1293
|
+
const kind = SCORE_BATCH_KIND;
|
|
1294
|
+
return deps.ctx.jobs.start({
|
|
1295
|
+
kind,
|
|
1296
|
+
label,
|
|
1297
|
+
owner,
|
|
1298
|
+
run: () => {
|
|
1299
|
+
const abort = new AbortController();
|
|
1300
|
+
const progress = [];
|
|
1301
|
+
const done = Promise.withResolvers();
|
|
1302
|
+
let settled = false;
|
|
1303
|
+
const settle = (outcome) => {
|
|
1304
|
+
if (settled) return outcome;
|
|
1305
|
+
settled = true;
|
|
1306
|
+
progress.push(`${outcome.status === "completed" ? "batch finished" : "batch stopped"}: ${outcome.detail ?? ""}`);
|
|
1307
|
+
done.resolve(outcome);
|
|
1308
|
+
return outcome;
|
|
1309
|
+
};
|
|
1310
|
+
runBatch(deps, sanitized, abort.signal, (line) => {
|
|
1311
|
+
progress.push(line);
|
|
1312
|
+
}).then(({ leaderboardId, leaderboard }) => {
|
|
1313
|
+
progress.push(`leaderboard ${leaderboardId} — fetch with score_report("${leaderboardId}")`);
|
|
1314
|
+
progress.push(renderScoreCardForBatch(leaderboard));
|
|
1315
|
+
settle({
|
|
1316
|
+
status: "completed",
|
|
1317
|
+
detail: leaderboardSummary(leaderboard)
|
|
1318
|
+
});
|
|
1319
|
+
}).catch((error) => {
|
|
1320
|
+
deps.log(`score: batch job failed: ${String(error)}`);
|
|
1321
|
+
settle({
|
|
1322
|
+
status: "failed",
|
|
1323
|
+
detail: tailDetail(String(error))
|
|
1324
|
+
});
|
|
1325
|
+
});
|
|
1326
|
+
return {
|
|
1327
|
+
cancel(reason) {
|
|
1328
|
+
abort.abort(reason ?? "cancelled");
|
|
1329
|
+
progress.push(`cancelling (${reason ?? "no reason given"})`);
|
|
1330
|
+
settle({
|
|
1331
|
+
status: "killed",
|
|
1332
|
+
detail: `cancelled: ${reason ?? "no reason given"}`
|
|
1333
|
+
});
|
|
1334
|
+
},
|
|
1335
|
+
done: done.promise,
|
|
1336
|
+
readOutput: () => {
|
|
1337
|
+
if (progress.length === 0) return "";
|
|
1338
|
+
return `${progress.splice(0, progress.length).join("\n")}\n`;
|
|
1339
|
+
}
|
|
1340
|
+
};
|
|
1341
|
+
}
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
/** Compact leaderboard body for the final batch flush (Markdown). */
|
|
1345
|
+
function renderScoreCardForBatch(leaderboard) {
|
|
1346
|
+
const lines = [
|
|
1347
|
+
`# Leaderboard ${leaderboard.id}`,
|
|
1348
|
+
"",
|
|
1349
|
+
`Created ${leaderboard.createdAt}`,
|
|
1350
|
+
"",
|
|
1351
|
+
"| # | Target | Grade | Total |",
|
|
1352
|
+
"|---|---|---|---|"
|
|
1353
|
+
];
|
|
1354
|
+
leaderboard.rows.forEach((row, index) => {
|
|
1355
|
+
lines.push(`| ${index + 1} | \`${row.target}\` | ${row.grade} | ${row.total} |`);
|
|
1356
|
+
});
|
|
1357
|
+
return lines.join("\n");
|
|
1358
|
+
}
|
|
1359
|
+
/** Bound an error message for job detail lines. */
|
|
1360
|
+
function tailDetail(text) {
|
|
1361
|
+
const clean = text.replace(/\s+/gu, " ").trim();
|
|
1362
|
+
return clean.length <= 200 ? clean : `${clean.slice(0, 197)}…`;
|
|
1363
|
+
}
|
|
1364
|
+
//#endregion
|
|
1365
|
+
//#region src/command.ts
|
|
1366
|
+
/** Split raw command input into target specs (whitespace and commas separate). */
|
|
1367
|
+
function parseTargets(rawInput) {
|
|
1368
|
+
return rawInput.split(/[\s,]+/u).map((token) => token.trim()).filter((token) => token.length > 0);
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* Execute `/score <targets...>`: validate the target list against the
|
|
1372
|
+
* configured batch cap and start the batch job for the invoking agent.
|
|
1373
|
+
*
|
|
1374
|
+
* @param deps - batch dependencies.
|
|
1375
|
+
* @param invocation - the command invocation.
|
|
1376
|
+
* @returns the command result naming the started job.
|
|
1377
|
+
*/
|
|
1378
|
+
function handleScore(deps, invocation) {
|
|
1379
|
+
const targets = parseTargets(invocation.rawInput);
|
|
1380
|
+
if (targets.length === 0) return {
|
|
1381
|
+
kind: "error",
|
|
1382
|
+
text: "Usage: /score <target> [<target> ...] — one or more plugin repos or npm packages"
|
|
1383
|
+
};
|
|
1384
|
+
if (targets.length > deps.config.maxBatchTargets) return {
|
|
1385
|
+
kind: "error",
|
|
1386
|
+
text: `Too many targets: ${targets.length} exceeds the configured batch cap of ${deps.config.maxBatchTargets}`
|
|
1387
|
+
};
|
|
1388
|
+
const jobId = startBatchJob(deps, targets, invocation.agent, `/score ${targets.join(" ")}`);
|
|
1389
|
+
return {
|
|
1390
|
+
kind: "success",
|
|
1391
|
+
text: `Started background scoring job ${String(jobId)} for ${targets.length} target(s). Progress streams through the job output; the final line names the leaderboard id for score_report.`
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
//#endregion
|
|
1395
|
+
//#region src/config.ts
|
|
1396
|
+
/**
|
|
1397
|
+
* Plugin configuration and its explicit resolve step. `resolveConfig` re-judges
|
|
1398
|
+
* every default and bound so programmatic construction that bypasses
|
|
1399
|
+
* Schemastery normalization still fails loud instead of running with hidden
|
|
1400
|
+
* defaults (the explicit-resolve contract). Every tunable is a Config field —
|
|
1401
|
+
* no scoring threshold or weight is hardcoded in the evaluators.
|
|
1402
|
+
*
|
|
1403
|
+
* @module dsh-score/config
|
|
1404
|
+
*/
|
|
1405
|
+
/** Default deadline for one git/gh/npm probe command in milliseconds. */
|
|
1406
|
+
const DEFAULT_PROBE_TIMEOUT_MS = 6e4;
|
|
1407
|
+
const MIN_PROBE_TIMEOUT_MS = 1e3;
|
|
1408
|
+
const MAX_PROBE_TIMEOUT_MS = 6e5;
|
|
1409
|
+
/** Default cap on the sanitized output tail recorded per probe, in bytes. */
|
|
1410
|
+
const DEFAULT_OUTPUT_TAIL_BYTES = 8e3;
|
|
1411
|
+
const MAX_OUTPUT_TAIL_BYTES = 256e3;
|
|
1412
|
+
/** Default staleness for a cached score card before it is re-scored. */
|
|
1413
|
+
const DEFAULT_CACHE_MAX_AGE_MS = 864e5;
|
|
1414
|
+
const MAX_CACHE_MAX_AGE_MS = 2592e6;
|
|
1415
|
+
const MAX_DAYS = 3650;
|
|
1416
|
+
/** Default dimension weights (sum 100). Install is the heaviest because it is the only measured signal. */
|
|
1417
|
+
const DEFAULT_WEIGHTS = {
|
|
1418
|
+
install: 25,
|
|
1419
|
+
maintenance: 20,
|
|
1420
|
+
documentation: 20,
|
|
1421
|
+
security: 20,
|
|
1422
|
+
compliance: 15
|
|
1423
|
+
};
|
|
1424
|
+
/** Schemastery schema for loader-validated configuration. */
|
|
1425
|
+
const Config = z$1.object({
|
|
1426
|
+
probeTimeoutMs: z$1.number().min(MIN_PROBE_TIMEOUT_MS).max(MAX_PROBE_TIMEOUT_MS).default(DEFAULT_PROBE_TIMEOUT_MS),
|
|
1427
|
+
outputTailBytes: z$1.number().min(256).max(MAX_OUTPUT_TAIL_BYTES).default(DEFAULT_OUTPUT_TAIL_BYTES),
|
|
1428
|
+
cacheMaxAgeMs: z$1.number().min(0).max(MAX_CACHE_MAX_AGE_MS).default(DEFAULT_CACHE_MAX_AGE_MS),
|
|
1429
|
+
staleCommitWarnDays: z$1.number().min(1).max(MAX_DAYS).default(90),
|
|
1430
|
+
staleCommitFailDays: z$1.number().min(1).max(MAX_DAYS).default(365),
|
|
1431
|
+
staleIssueWarnDays: z$1.number().min(1).max(MAX_DAYS).default(30),
|
|
1432
|
+
staleIssueFailDays: z$1.number().min(1).max(MAX_DAYS).default(180),
|
|
1433
|
+
maxBatchTargets: z$1.number().min(1).max(200).default(20),
|
|
1434
|
+
batchConcurrency: z$1.number().min(1).max(4).default(1),
|
|
1435
|
+
weights: z$1.object({
|
|
1436
|
+
install: z$1.number().min(0).max(100).default(DEFAULT_WEIGHTS.install),
|
|
1437
|
+
maintenance: z$1.number().min(0).max(100).default(DEFAULT_WEIGHTS.maintenance),
|
|
1438
|
+
documentation: z$1.number().min(0).max(100).default(DEFAULT_WEIGHTS.documentation),
|
|
1439
|
+
security: z$1.number().min(0).max(100).default(DEFAULT_WEIGHTS.security),
|
|
1440
|
+
compliance: z$1.number().min(0).max(100).default(DEFAULT_WEIGHTS.compliance)
|
|
1441
|
+
}).default({ ...DEFAULT_WEIGHTS })
|
|
1442
|
+
});
|
|
1443
|
+
/** Throw the standard fail-loud config error for one invalid field. */
|
|
1444
|
+
function invalid(field, detail) {
|
|
1445
|
+
throw new Error(`dsh-score: config.${field} ${detail}`);
|
|
1446
|
+
}
|
|
1447
|
+
/** Validate one integer bound with a numeric range. */
|
|
1448
|
+
function assertInt(value, field, min, max) {
|
|
1449
|
+
if (!Number.isInteger(value) || value < min || value > max) invalid(field, `must be an integer between ${min} and ${max}`);
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* Resolve raw config to the runtime policy, re-validating defaults and bounds.
|
|
1453
|
+
*
|
|
1454
|
+
* @param config - raw loader config; `undefined` for a bare row.
|
|
1455
|
+
* @returns the frozen resolved config.
|
|
1456
|
+
*/
|
|
1457
|
+
function resolveConfig(config) {
|
|
1458
|
+
const probeTimeoutMs = config?.probeTimeoutMs ?? 6e4;
|
|
1459
|
+
assertInt(probeTimeoutMs, "probeTimeoutMs", MIN_PROBE_TIMEOUT_MS, MAX_PROBE_TIMEOUT_MS);
|
|
1460
|
+
const outputTailBytes = config?.outputTailBytes ?? 8e3;
|
|
1461
|
+
assertInt(outputTailBytes, "outputTailBytes", 256, MAX_OUTPUT_TAIL_BYTES);
|
|
1462
|
+
const cacheMaxAgeMs = config?.cacheMaxAgeMs ?? 864e5;
|
|
1463
|
+
assertInt(cacheMaxAgeMs, "cacheMaxAgeMs", 0, MAX_CACHE_MAX_AGE_MS);
|
|
1464
|
+
const staleCommitWarnDays = config?.staleCommitWarnDays ?? 90;
|
|
1465
|
+
const staleCommitFailDays = config?.staleCommitFailDays ?? 365;
|
|
1466
|
+
assertInt(staleCommitWarnDays, "staleCommitWarnDays", 1, MAX_DAYS);
|
|
1467
|
+
assertInt(staleCommitFailDays, "staleCommitFailDays", 1, MAX_DAYS);
|
|
1468
|
+
if (staleCommitFailDays <= staleCommitWarnDays) invalid("staleCommitFailDays", `must be greater than staleCommitWarnDays (${staleCommitWarnDays})`);
|
|
1469
|
+
const staleIssueWarnDays = config?.staleIssueWarnDays ?? 30;
|
|
1470
|
+
const staleIssueFailDays = config?.staleIssueFailDays ?? 180;
|
|
1471
|
+
assertInt(staleIssueWarnDays, "staleIssueWarnDays", 1, MAX_DAYS);
|
|
1472
|
+
assertInt(staleIssueFailDays, "staleIssueFailDays", 1, MAX_DAYS);
|
|
1473
|
+
if (staleIssueFailDays <= staleIssueWarnDays) invalid("staleIssueFailDays", `must be greater than staleIssueWarnDays (${staleIssueWarnDays})`);
|
|
1474
|
+
const maxBatchTargets = config?.maxBatchTargets ?? 20;
|
|
1475
|
+
assertInt(maxBatchTargets, "maxBatchTargets", 1, 200);
|
|
1476
|
+
const batchConcurrency = config?.batchConcurrency ?? 1;
|
|
1477
|
+
assertInt(batchConcurrency, "batchConcurrency", 1, 4);
|
|
1478
|
+
const weights = {
|
|
1479
|
+
install: config?.weights?.install ?? DEFAULT_WEIGHTS.install,
|
|
1480
|
+
maintenance: config?.weights?.maintenance ?? DEFAULT_WEIGHTS.maintenance,
|
|
1481
|
+
documentation: config?.weights?.documentation ?? DEFAULT_WEIGHTS.documentation,
|
|
1482
|
+
security: config?.weights?.security ?? DEFAULT_WEIGHTS.security,
|
|
1483
|
+
compliance: config?.weights?.compliance ?? DEFAULT_WEIGHTS.compliance
|
|
1484
|
+
};
|
|
1485
|
+
let weightSum = 0;
|
|
1486
|
+
for (const key of [
|
|
1487
|
+
"install",
|
|
1488
|
+
"maintenance",
|
|
1489
|
+
"documentation",
|
|
1490
|
+
"security",
|
|
1491
|
+
"compliance"
|
|
1492
|
+
]) {
|
|
1493
|
+
assertInt(weights[key], `weights.${key}`, 0, 100);
|
|
1494
|
+
weightSum += weights[key];
|
|
1495
|
+
}
|
|
1496
|
+
if (weightSum === 0) invalid("weights", "at least one dimension weight must be greater than 0");
|
|
1497
|
+
return Object.freeze({
|
|
1498
|
+
probeTimeoutMs,
|
|
1499
|
+
outputTailBytes,
|
|
1500
|
+
cacheMaxAgeMs,
|
|
1501
|
+
staleCommitWarnDays,
|
|
1502
|
+
staleCommitFailDays,
|
|
1503
|
+
staleIssueWarnDays,
|
|
1504
|
+
staleIssueFailDays,
|
|
1505
|
+
maxBatchTargets,
|
|
1506
|
+
batchConcurrency,
|
|
1507
|
+
weights: Object.freeze(weights)
|
|
1508
|
+
});
|
|
1509
|
+
}
|
|
1510
|
+
//#endregion
|
|
1511
|
+
//#region src/domain.ts
|
|
1512
|
+
/**
|
|
1513
|
+
* The `score` storage domain: the durable home of score cards and leaderboard
|
|
1514
|
+
* records. Opened through `ctx.storageDomain` (a public service), with record
|
|
1515
|
+
* schemas validated at the durable boundary; the domain stays open for the
|
|
1516
|
+
* plugin's lifetime and closes through the registered effect.
|
|
1517
|
+
*
|
|
1518
|
+
* @module dsh-score/domain
|
|
1519
|
+
*/
|
|
1520
|
+
/** Domain name (also the backend unit name; UNIT_NAME_RE forbids hyphens). */
|
|
1521
|
+
const DOMAIN_NAME = "score";
|
|
1522
|
+
/** Domain format version; bump when a record format changes incompatibly. */
|
|
1523
|
+
const DOMAIN_VERSION = 1;
|
|
1524
|
+
/** Latest-leaderboard pointer value; `''` id means "no leaderboard yet". */
|
|
1525
|
+
const LatestLeaderboardSchema = z.object({
|
|
1526
|
+
leaderboardId: z.string(),
|
|
1527
|
+
createdAt: z.string()
|
|
1528
|
+
});
|
|
1529
|
+
/** Storage-domain spec: one table of score cards, one of leaderboards, plus the latest pointer. */
|
|
1530
|
+
const scoreDomainSpec = defineDomain({
|
|
1531
|
+
name: DOMAIN_NAME,
|
|
1532
|
+
version: 1,
|
|
1533
|
+
global: {
|
|
1534
|
+
schema: LatestLeaderboardSchema,
|
|
1535
|
+
initial: {
|
|
1536
|
+
leaderboardId: "",
|
|
1537
|
+
createdAt: ""
|
|
1538
|
+
}
|
|
1539
|
+
},
|
|
1540
|
+
tables: {
|
|
1541
|
+
scores: domainTable(ScoreResultSchema),
|
|
1542
|
+
leaderboards: domainTable(LeaderboardRecordSchema)
|
|
1543
|
+
}
|
|
1544
|
+
});
|
|
1545
|
+
//#endregion
|
|
1546
|
+
//#region src/report.ts
|
|
1547
|
+
/** Emoji/symbol mark for a dimension status. */
|
|
1548
|
+
function statusMark(status) {
|
|
1549
|
+
switch (status) {
|
|
1550
|
+
case "pass": return "✅";
|
|
1551
|
+
case "warn": return "🟠";
|
|
1552
|
+
case "fail": return "❌";
|
|
1553
|
+
default: return "⬜";
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
/** Human-duration rendering for one number of milliseconds. */
|
|
1557
|
+
function formatDuration(ms) {
|
|
1558
|
+
if (ms < 1e3) return `${ms} ms`;
|
|
1559
|
+
return `${(ms / 1e3).toFixed(1)} s`;
|
|
1560
|
+
}
|
|
1561
|
+
/** Dimension display order for the risk card. */
|
|
1562
|
+
const DIMENSION_LABELS = [
|
|
1563
|
+
{
|
|
1564
|
+
key: "install",
|
|
1565
|
+
label: "Install success"
|
|
1566
|
+
},
|
|
1567
|
+
{
|
|
1568
|
+
key: "maintenance",
|
|
1569
|
+
label: "Maintenance"
|
|
1570
|
+
},
|
|
1571
|
+
{
|
|
1572
|
+
key: "documentation",
|
|
1573
|
+
label: "Documentation"
|
|
1574
|
+
},
|
|
1575
|
+
{
|
|
1576
|
+
key: "security",
|
|
1577
|
+
label: "Security"
|
|
1578
|
+
},
|
|
1579
|
+
{
|
|
1580
|
+
key: "compliance",
|
|
1581
|
+
label: "Protocol compliance"
|
|
1582
|
+
}
|
|
1583
|
+
];
|
|
1584
|
+
/** Render one score card as a Markdown risk card. */
|
|
1585
|
+
function renderScoreCard(result) {
|
|
1586
|
+
const lines = [
|
|
1587
|
+
`# Score ${result.scoreId}`,
|
|
1588
|
+
"",
|
|
1589
|
+
`- Target: \`${result.target.spec}\` (${result.target.kind})`,
|
|
1590
|
+
`- Grade: **${result.grade}** — total ${result.total}/100`,
|
|
1591
|
+
`- Verdict: ${result.verdict}`,
|
|
1592
|
+
`- Scored: ${result.scoredAt} (${formatDuration(result.durationMs)})`,
|
|
1593
|
+
"",
|
|
1594
|
+
"| Dimension | Status | Score | Weight | Summary |",
|
|
1595
|
+
"|---|---|---|---|---|"
|
|
1596
|
+
];
|
|
1597
|
+
for (const { key, label } of DIMENSION_LABELS) {
|
|
1598
|
+
const dimension = result.dimensions[key];
|
|
1599
|
+
lines.push(`| ${label} | ${statusMark(dimension.status)} ${dimension.status} | ${dimension.score} | ${dimension.weight} | ${dimension.summary} |`);
|
|
1600
|
+
}
|
|
1601
|
+
lines.push("", "## Evidence", "");
|
|
1602
|
+
for (const { key, label } of DIMENSION_LABELS) {
|
|
1603
|
+
const dimension = result.dimensions[key];
|
|
1604
|
+
for (const link of dimension.evidence) lines.push(`- \`${label}\` [${link.source} @ ${link.observedAt}]: ${link.detail}`);
|
|
1605
|
+
}
|
|
1606
|
+
return lines.join("\n");
|
|
1607
|
+
}
|
|
1608
|
+
/** Render one leaderboard as a Markdown report. */
|
|
1609
|
+
function renderLeaderboard(record) {
|
|
1610
|
+
const lines = [
|
|
1611
|
+
`# Score leaderboard ${record.id}`,
|
|
1612
|
+
"",
|
|
1613
|
+
`- Created: ${record.createdAt}`,
|
|
1614
|
+
`- Duration: ${formatDuration(record.durationMs)}`,
|
|
1615
|
+
`- Totals: ${record.totals.pass} pass, ${record.totals.warn} warn, ${record.totals.fail} fail, ${record.totals.noEvidence} no-evidence (of ${record.totals.total})`,
|
|
1616
|
+
"",
|
|
1617
|
+
"| # | Target | Grade | Total | Install | Maint. | Docs | Security | Compliance |",
|
|
1618
|
+
"|---|---|---|---|---|---|---|---|---|"
|
|
1619
|
+
];
|
|
1620
|
+
record.rows.forEach((row, index) => {
|
|
1621
|
+
lines.push(`| ${index + 1} | \`${row.target}\` | ${row.grade} | ${row.total} | ${statusMark(row.install)} | ${statusMark(row.maintenance)} | ${statusMark(row.documentation)} | ${statusMark(row.security)} | ${statusMark(row.compliance)} |`);
|
|
1622
|
+
});
|
|
1623
|
+
const weak = record.rows.filter((row) => row.total < 60);
|
|
1624
|
+
if (weak.length > 0) {
|
|
1625
|
+
lines.push("", "## Attention", "");
|
|
1626
|
+
for (const row of weak) lines.push(`- ${row.grade} \`${row.target}\`: ${row.verdict}`);
|
|
1627
|
+
}
|
|
1628
|
+
return lines.join("\n");
|
|
1629
|
+
}
|
|
1630
|
+
//#endregion
|
|
1631
|
+
//#region src/tools.ts
|
|
1632
|
+
/** JSON-schema fragment for one dimension score. */
|
|
1633
|
+
const dimensionScoreSchema = {
|
|
1634
|
+
type: "object",
|
|
1635
|
+
additionalProperties: false,
|
|
1636
|
+
properties: {
|
|
1637
|
+
dimension: { type: "string" },
|
|
1638
|
+
status: { type: "string" },
|
|
1639
|
+
score: { type: "number" },
|
|
1640
|
+
weight: { type: "number" },
|
|
1641
|
+
summary: { type: "string" },
|
|
1642
|
+
evidence: {
|
|
1643
|
+
type: "array",
|
|
1644
|
+
items: {
|
|
1645
|
+
type: "object",
|
|
1646
|
+
additionalProperties: false,
|
|
1647
|
+
properties: {
|
|
1648
|
+
source: { type: "string" },
|
|
1649
|
+
detail: { type: "string" },
|
|
1650
|
+
observedAt: { type: "string" }
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
};
|
|
1656
|
+
/**
|
|
1657
|
+
* Full JSON-schema fragment for the score-card branch of both tool outputs.
|
|
1658
|
+
* The compiled type doubles as the compile-time assertion that the canonical
|
|
1659
|
+
* `ScoreResult` value matches the declared tool contract.
|
|
1660
|
+
*/
|
|
1661
|
+
const scoreResultSchema = {
|
|
1662
|
+
type: "object",
|
|
1663
|
+
additionalProperties: false,
|
|
1664
|
+
properties: {
|
|
1665
|
+
schema: {
|
|
1666
|
+
type: "string",
|
|
1667
|
+
const: RESULT_SCHEMA
|
|
1668
|
+
},
|
|
1669
|
+
scoreId: { type: "string" },
|
|
1670
|
+
target: {
|
|
1671
|
+
type: "object",
|
|
1672
|
+
additionalProperties: false,
|
|
1673
|
+
properties: {
|
|
1674
|
+
kind: { type: "string" },
|
|
1675
|
+
spec: { type: "string" }
|
|
1676
|
+
}
|
|
1677
|
+
},
|
|
1678
|
+
scoredAt: { type: "string" },
|
|
1679
|
+
durationMs: { type: "integer" },
|
|
1680
|
+
pluginVersion: { type: "string" },
|
|
1681
|
+
dimensions: {
|
|
1682
|
+
type: "object",
|
|
1683
|
+
additionalProperties: false,
|
|
1684
|
+
properties: {
|
|
1685
|
+
install: dimensionScoreSchema,
|
|
1686
|
+
maintenance: dimensionScoreSchema,
|
|
1687
|
+
documentation: dimensionScoreSchema,
|
|
1688
|
+
security: dimensionScoreSchema,
|
|
1689
|
+
compliance: dimensionScoreSchema
|
|
1690
|
+
}
|
|
1691
|
+
},
|
|
1692
|
+
total: { type: "number" },
|
|
1693
|
+
grade: { type: "string" },
|
|
1694
|
+
verdict: { type: "string" }
|
|
1695
|
+
}
|
|
1696
|
+
};
|
|
1697
|
+
/** Full JSON-schema fragment for the leaderboard branch of `score_report`. */
|
|
1698
|
+
const leaderboardSchema = {
|
|
1699
|
+
type: "object",
|
|
1700
|
+
additionalProperties: false,
|
|
1701
|
+
properties: {
|
|
1702
|
+
schema: {
|
|
1703
|
+
type: "string",
|
|
1704
|
+
const: RESULT_SCHEMA
|
|
1705
|
+
},
|
|
1706
|
+
id: { type: "string" },
|
|
1707
|
+
createdAt: { type: "string" },
|
|
1708
|
+
durationMs: { type: "integer" },
|
|
1709
|
+
rows: {
|
|
1710
|
+
type: "array",
|
|
1711
|
+
items: {
|
|
1712
|
+
type: "object",
|
|
1713
|
+
additionalProperties: false,
|
|
1714
|
+
properties: {
|
|
1715
|
+
target: { type: "string" },
|
|
1716
|
+
kind: { type: "string" },
|
|
1717
|
+
total: { type: "number" },
|
|
1718
|
+
grade: { type: "string" },
|
|
1719
|
+
verdict: { type: "string" },
|
|
1720
|
+
durationMs: { type: "integer" },
|
|
1721
|
+
install: { type: "string" },
|
|
1722
|
+
maintenance: { type: "string" },
|
|
1723
|
+
documentation: { type: "string" },
|
|
1724
|
+
security: { type: "string" },
|
|
1725
|
+
compliance: { type: "string" }
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
},
|
|
1729
|
+
totals: {
|
|
1730
|
+
type: "object",
|
|
1731
|
+
additionalProperties: false,
|
|
1732
|
+
properties: {
|
|
1733
|
+
total: { type: "integer" },
|
|
1734
|
+
pass: { type: "integer" },
|
|
1735
|
+
warn: { type: "integer" },
|
|
1736
|
+
fail: { type: "integer" },
|
|
1737
|
+
noEvidence: { type: "integer" }
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
};
|
|
1742
|
+
/** Render one `score` result (score card or background handle). */
|
|
1743
|
+
function renderScore(value) {
|
|
1744
|
+
if (value.kind === "background") return [{
|
|
1745
|
+
type: "text",
|
|
1746
|
+
text: `Started background scoring job ${value.jobId}. Read its output for progress; the final line names the leaderboard id for score_report.`
|
|
1747
|
+
}];
|
|
1748
|
+
return [{
|
|
1749
|
+
type: "text",
|
|
1750
|
+
text: renderScoreCard(value)
|
|
1751
|
+
}];
|
|
1752
|
+
}
|
|
1753
|
+
/** Render one `score_report` result (leaderboard or score card). */
|
|
1754
|
+
function renderReport(value) {
|
|
1755
|
+
return [{
|
|
1756
|
+
type: "text",
|
|
1757
|
+
text: "rows" in value ? renderLeaderboard(value) : renderScoreCard(value)
|
|
1758
|
+
}];
|
|
1759
|
+
}
|
|
1760
|
+
/** The deadline for one foreground score: every probe deadline plus slack. */
|
|
1761
|
+
function scoreDeadlineMs(config) {
|
|
1762
|
+
return config.probeTimeoutMs * 10 + 6e4;
|
|
1763
|
+
}
|
|
1764
|
+
/** Owner for a background branch; background work needs a live agent to collect it. */
|
|
1765
|
+
function requireOwner(exec) {
|
|
1766
|
+
if (exec.agent === void 0) throw new Error("dsh-score: background scoring requires an owning agent");
|
|
1767
|
+
return exec.agent;
|
|
1768
|
+
}
|
|
1769
|
+
/** Build both tool definitions over the shared services. */
|
|
1770
|
+
function allTools(services) {
|
|
1771
|
+
return [defineTool({
|
|
1772
|
+
name: "score",
|
|
1773
|
+
description: "Score one DeepSeek Harness plugin target (a GitHub repo spec or an npm package name) across five dimensions from REAL CLI evidence: install success (consuming dsh-test-drive results when present), maintenance activity, documentation completeness, security scan (license, secret leaks, malicious install scripts), and protocol compliance (dsh.bundle.patch + dsh-plugin topic). Every conclusion carries an evidence link and audit timestamp; a dimension without evidence is honestly reported as no-evidence, never fabricated. Returns a risk card with a weighted total and letter grade. Use background: true for a slow target.",
|
|
1774
|
+
parameters: {
|
|
1775
|
+
target: {
|
|
1776
|
+
type: "string",
|
|
1777
|
+
required: true,
|
|
1778
|
+
description: "Plugin target: a GitHub repo (github:owner/repo, owner/repo, a git/https URL) or an npm package name."
|
|
1779
|
+
},
|
|
1780
|
+
refresh: {
|
|
1781
|
+
type: "boolean",
|
|
1782
|
+
description: "Bypass the score cache and re-score from fresh evidence (default false)."
|
|
1783
|
+
},
|
|
1784
|
+
background: {
|
|
1785
|
+
type: "boolean",
|
|
1786
|
+
description: "Score as a background job and return its job id instead of waiting."
|
|
1787
|
+
}
|
|
1788
|
+
},
|
|
1789
|
+
output: {
|
|
1790
|
+
schema: { oneOf: [scoreResultSchema, {
|
|
1791
|
+
type: "object",
|
|
1792
|
+
additionalProperties: false,
|
|
1793
|
+
properties: {
|
|
1794
|
+
kind: {
|
|
1795
|
+
type: "string",
|
|
1796
|
+
const: "background"
|
|
1797
|
+
},
|
|
1798
|
+
jobId: { type: "string" }
|
|
1799
|
+
}
|
|
1800
|
+
}] },
|
|
1801
|
+
render: (_args, value) => renderScore(value)
|
|
1802
|
+
},
|
|
1803
|
+
timeoutMs: scoreDeadlineMs(services.config),
|
|
1804
|
+
async execute(args, exec) {
|
|
1805
|
+
const target = sanitizeTarget(args.target);
|
|
1806
|
+
if (target.length === 0) throw new Error("dsh-score: target must be a non-empty string");
|
|
1807
|
+
if (args.background === true) {
|
|
1808
|
+
const owner = requireOwner(exec);
|
|
1809
|
+
const jobId = startBatchJob(services, [target], owner, `score ${target}`);
|
|
1810
|
+
return {
|
|
1811
|
+
kind: "background",
|
|
1812
|
+
jobId: String(jobId)
|
|
1813
|
+
};
|
|
1814
|
+
}
|
|
1815
|
+
return services.runner.score(target, {
|
|
1816
|
+
signal: exec.signal,
|
|
1817
|
+
refresh: args.refresh === true
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
}), defineTool({
|
|
1821
|
+
name: "score_report",
|
|
1822
|
+
description: "Fetch a stored score record by id: a score card (sc_...), a leaderboard (lb_...), or — with no id — the latest leaderboard. Returns the structured record and renders it as Markdown.",
|
|
1823
|
+
parameters: { id: {
|
|
1824
|
+
type: "string",
|
|
1825
|
+
description: "Score id (sc_...), leaderboard id (lb_...); omitted = the latest leaderboard."
|
|
1826
|
+
} },
|
|
1827
|
+
output: {
|
|
1828
|
+
schema: { oneOf: [scoreResultSchema, leaderboardSchema] },
|
|
1829
|
+
render: (_args, value) => renderReport(value)
|
|
1830
|
+
},
|
|
1831
|
+
async execute(args) {
|
|
1832
|
+
const domain = await services.domain();
|
|
1833
|
+
if (args.id === void 0 || args.id === "") {
|
|
1834
|
+
const latest = domain.global.get();
|
|
1835
|
+
if (latest.leaderboardId === "") throw new Error("dsh-score: no leaderboard recorded yet — run /score or score first");
|
|
1836
|
+
const leaderboard = domain.table("leaderboards").get(latest.leaderboardId);
|
|
1837
|
+
if (leaderboard === void 0) throw new Error(`dsh-score: latest leaderboard ${latest.leaderboardId} not found`);
|
|
1838
|
+
return leaderboard;
|
|
1839
|
+
}
|
|
1840
|
+
if (args.id.startsWith("sc_")) {
|
|
1841
|
+
const card = domain.table("scores").get(args.id);
|
|
1842
|
+
if (card === void 0) throw new Error(`dsh-score: no score recorded with id ${args.id}`);
|
|
1843
|
+
return card;
|
|
1844
|
+
}
|
|
1845
|
+
if (args.id.startsWith("lb_")) {
|
|
1846
|
+
const leaderboard = domain.table("leaderboards").get(args.id);
|
|
1847
|
+
if (leaderboard === void 0) throw new Error(`dsh-score: no leaderboard recorded with id ${args.id}`);
|
|
1848
|
+
return leaderboard;
|
|
1849
|
+
}
|
|
1850
|
+
throw new Error(`dsh-score: unknown report id ${args.id} (expected sc_... score or lb_... leaderboard)`);
|
|
1851
|
+
}
|
|
1852
|
+
})];
|
|
1853
|
+
}
|
|
1854
|
+
//#endregion
|
|
1855
|
+
//#region src/index.ts
|
|
1856
|
+
const name = "dsh-score";
|
|
1857
|
+
/**
|
|
1858
|
+
* Public services only. `storageDomain` is deliberately OPTIONAL: the shipped
|
|
1859
|
+
* `dsh-base` bundle (headless profile) does not mount it, and the plugin must
|
|
1860
|
+
* still boot there — score persistence degrades to disabled with a logged
|
|
1861
|
+
* reason, tools keep working. Install evidence reads the already-open
|
|
1862
|
+
* `test_drive` domain via the same optional service, never a hard dependency.
|
|
1863
|
+
*/
|
|
1864
|
+
const inject = [
|
|
1865
|
+
"tools",
|
|
1866
|
+
"commands",
|
|
1867
|
+
"subprocess",
|
|
1868
|
+
"jobs"
|
|
1869
|
+
];
|
|
1870
|
+
/**
|
|
1871
|
+
* Mount the plugin: resolve config, open the score domain (lazily), register
|
|
1872
|
+
* the tools and the `/score` command as effects, and own the domain handle
|
|
1873
|
+
* through an effect disposer (teardown closes it).
|
|
1874
|
+
*
|
|
1875
|
+
* @param ctx - context carrying tools/commands/subprocess/jobs/storageDomain.
|
|
1876
|
+
* @param config - raw loader config; defaults applied through {@link resolveConfig}.
|
|
1877
|
+
*/
|
|
1878
|
+
function apply(ctx, config) {
|
|
1879
|
+
const resolved = resolveConfig(config);
|
|
1880
|
+
const log = (line) => {
|
|
1881
|
+
ctx.logger("score").info(line);
|
|
1882
|
+
};
|
|
1883
|
+
const storageDomain = ctx.get("storageDomain");
|
|
1884
|
+
const domainPromise = storageDomain === void 0 ? Promise.reject(/* @__PURE__ */ new Error("dsh-score: storageDomain service not mounted in this composition; score persistence disabled")) : (async () => storageDomain.open(scoreDomainSpec))();
|
|
1885
|
+
domainPromise.catch((error) => {
|
|
1886
|
+
log(`score domain unavailable: ${String(error)}`);
|
|
1887
|
+
});
|
|
1888
|
+
const domain = () => domainPromise;
|
|
1889
|
+
ctx.effect(() => () => {
|
|
1890
|
+
domainPromise.then((handle) => handle.close()).catch(() => {});
|
|
1891
|
+
}, "dsh-score score domain close");
|
|
1892
|
+
const driver = new ProbeDriver({
|
|
1893
|
+
ctx,
|
|
1894
|
+
config: resolved,
|
|
1895
|
+
log
|
|
1896
|
+
});
|
|
1897
|
+
const deps = {
|
|
1898
|
+
ctx,
|
|
1899
|
+
config: resolved,
|
|
1900
|
+
driver,
|
|
1901
|
+
log,
|
|
1902
|
+
domain
|
|
1903
|
+
};
|
|
1904
|
+
const runner = new ScoreRunner(deps);
|
|
1905
|
+
const services = {
|
|
1906
|
+
...deps,
|
|
1907
|
+
runner
|
|
1908
|
+
};
|
|
1909
|
+
for (const tool of allTools(services)) ctx.effect(() => ctx.tools.register(tool), `dsh-score: ${tool.name} tool`);
|
|
1910
|
+
ctx.effect(() => ctx.commands.register({
|
|
1911
|
+
name: "score",
|
|
1912
|
+
description: "Batch score plugin targets (background job + leaderboard snapshot)",
|
|
1913
|
+
input: { hint: "repo or npm targets, space-separated" },
|
|
1914
|
+
handler: (invocation) => handleScore(services, invocation)
|
|
1915
|
+
}), "dsh-score: /score command");
|
|
1916
|
+
driver.locate("gh").catch((error) => {
|
|
1917
|
+
log(`gh CLI unavailable: ${String(error)}`);
|
|
1918
|
+
});
|
|
1919
|
+
driver.locate("npm").catch((error) => {
|
|
1920
|
+
log(`npm CLI unavailable: ${String(error)}`);
|
|
1921
|
+
});
|
|
1922
|
+
}
|
|
1923
|
+
//#endregion
|
|
1924
|
+
export { Config, DIMENSIONS, DOMAIN_NAME, DOMAIN_VERSION, LEADERBOARD_ID_PREFIX, LICENSE_ALLOWLIST, LeaderboardRecordSchema, MALICIOUS_SCRIPT_MARKERS, ProbeDriver, REDACTED, RESULT_SCHEMA, SCORE_BATCH_KIND, SCORE_KEY_PREFIX, SECRET_PATTERNS, ScoreResultSchema, ScoreRunner, VERSION, apply, classifyTarget, computeTotal, daysSince, evaluateAll, evaluateCompliance, evaluateDocumentation, evaluateInstall, evaluateMaintenance, evaluateSecurity, extractRepoFromNpmUrl, formatDuration, freshLeaderboardId, gradeOf, hasEvidence, inject, leaderboardSummary, licenseCompliance, name, parseFileList, parseGithubContent, parseLatestCommit, parseNpmShim, parseNpmView, parseOldestOpenIssue, parseRepoMeta, parseRepoRef, parseTargets, progressLine, readTestDriveEvidence, redactSecrets, renderLeaderboard, renderScoreCard, resolveConfig, rowOf, runBatch, sanitizeTarget, scanMaliciousScripts, scanSecretPatterns, scoreDomainSpec, scoreKey, startBatchJob, statusMark, tailText, totalsOf, verdictOf };
|