@kriya-ai/harness 0.2.5 → 2.0.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/RELEASE_NOTES.md +121 -0
- package/SAFETY.md +181 -0
- package/THIRD_PARTY_NOTICES.md +113 -0
- package/bin/kriya.js +188 -2
- package/bundle/cordis.patch.yml +49 -0
- package/contract/SOURCE.md +3 -3
- package/contract/contract.json +498 -3
- package/contract/schemas/api.json +64 -0
- package/contract/schemas/auth_matrix.json +44 -0
- package/contract/schemas/component_states.json +33 -0
- package/contract/schemas/design_tokens.json +34 -0
- package/contract/schemas/scene_direction.json +82 -0
- package/contract/schemas/threat_model.json +44 -0
- package/package.json +2 -1
- package/plugins/changes/client.js +5 -3
- package/plugins/deploy/index.js +215 -0
- package/plugins/deploy/package.json +13 -0
- package/plugins/grounding/index.js +81 -0
- package/plugins/grounding/lookup.js +373 -0
- package/plugins/grounding/package.json +13 -0
- package/plugins/kriya-tools/index.js +2 -1
- package/plugins/loop/index.js +376 -10
- package/plugins/mcp/index.js +79 -0
- package/plugins/mcp/package.json +13 -0
- package/plugins/mirror/index.js +17 -0
- package/plugins/overview/client.js +5 -2
- package/plugins/preview/client.js +12 -6
- package/plugins/progress/client.js +7 -4
- package/plugins/repo/index.js +230 -0
- package/plugins/repo/package.json +13 -0
- package/plugins/repo/repo.js +348 -0
- package/plugins/runstrip/client.js +426 -0
- package/plugins/runstrip/index.js +301 -0
- package/plugins/runstrip/package.json +23 -0
- package/src/cartridges.js +133 -0
- package/src/contracts.js +130 -0
- package/src/critic.js +78 -0
- package/src/daemon.js +7 -1
- package/src/doctor.js +67 -0
- package/src/edit/core/effects.js +185 -0
- package/src/edit/core/ffmpeg.js +65 -6
- package/src/edit/core/layout.js +46 -1
- package/src/edit/core/ops.js +169 -2
- package/src/edit/core/plan.js +65 -9
- package/src/edit/core/project.d.ts +24 -0
- package/src/edit/core/project.js +64 -0
- package/src/edit/core/solver.js +34 -6
- package/src/edit/core/style-brief.js +211 -0
- package/src/edit/core/style-packs.js +57 -0
- package/src/edit/core/type-kit.js +173 -0
- package/src/edit/director/ask.js +37 -3
- package/src/edit/director/assembly.js +829 -0
- package/src/edit/director/continuity.js +9 -1
- package/src/edit/director/custom-motion.js +88 -0
- package/src/edit/director/graphics.js +44 -10
- package/src/edit/director/intent.js +125 -0
- package/src/edit/director/mentions.js +52 -0
- package/src/edit/director/plan.js +43 -11
- package/src/edit/director/revise.js +132 -9
- package/src/edit/director/viewer.js +136 -0
- package/src/edit/director/watch-fix.js +119 -0
- package/src/edit/engine/captions.js +110 -5
- package/src/edit/engine/confine.js +81 -4
- package/src/edit/engine/custom.js +243 -0
- package/src/edit/engine/dna2.js +893 -0
- package/src/edit/engine/fonts.js +218 -0
- package/src/edit/engine/graphics.js +271 -10
- package/src/edit/engine/index.js +14 -5
- package/src/edit/engine/motion.js +148 -13
- package/src/edit/engine/music-bed.js +59 -1
- package/src/edit/engine/packs.js +11 -0
- package/src/edit/engine/reference2.js +26 -0
- package/src/edit/engine/sfx.js +56 -1
- package/src/edit/engine/tool.js +21 -5
- package/src/edit/gates/assembly.js +194 -0
- package/src/edit/gates/copy.js +7 -4
- package/src/edit/gates/index.js +72 -7
- package/src/edit/gates/reference.js +115 -0
- package/src/edit/gates/tile.js +96 -0
- package/src/edit/host/client.js +4 -3
- package/src/edit/host/clips.js +33 -0
- package/src/edit/host/jobs.js +9 -3
- package/src/edit/host/layers.js +134 -0
- package/src/edit/host/main.js +10 -3
- package/src/edit/host/paths.js +21 -3
- package/src/edit/host/preview.js +108 -0
- package/src/edit/host/projects.js +443 -34
- package/src/edit/host/server.js +235 -20
- package/src/edit/host/tavi.js +77 -0
- package/src/edit/loop/make.js +28 -3
- package/src/edit/recipes/assembly.js +352 -0
- package/src/edit/recipes/graphic-clips.js +4 -2
- package/src/edit/recipes/openings.js +36 -0
- package/src/edit/recipes/reel.js +104 -14
- package/src/edit/recipes/reference-look.js +29 -0
- package/src/edit/recipes/short-from-long.js +3 -3
- package/src/edit/recipes/sound.js +71 -0
- package/src/edit/runtime/cli.js +26 -5
- package/src/edit/runtime/fetch.js +4 -4
- package/src/edit/runtime/licences.js +7 -1
- package/src/explore.js +103 -0
- package/src/failure.js +1 -1
- package/src/gate-commands.js +114 -0
- package/src/gates/api.js +127 -0
- package/src/gates/commands.js +111 -0
- package/src/gates/detectors.js +370 -0
- package/src/gates/http.js +62 -0
- package/src/gates/index.js +196 -0
- package/src/gates/page.js +264 -0
- package/src/gates/sast.js +64 -0
- package/src/gates/stack.js +110 -0
- package/src/gates/tenant.js +162 -0
- package/src/gates/three.js +407 -0
- package/src/gates/ui.js +111 -0
- package/src/launch.js +15 -3
- package/src/loop.js +199 -7
- package/src/mcp.js +126 -0
- package/src/milestones.js +188 -0
- package/src/modes.js +32 -0
- package/src/orchestra.js +43 -0
- package/src/outcomes.js +88 -0
- package/src/profiles.js +5 -0
- package/src/receipts.js +238 -0
- package/src/release.js +356 -0
- package/src/runstrip.js +122 -0
- package/src/stack.js +120 -0
- package/src/verify.js +67 -20
- package/studio/dist/fonts/fraunces.woff2 +0 -0
- package/studio/dist/fonts/geist.woff2 +0 -0
- package/studio/dist/fonts/jetbrains-mono.woff2 +0 -0
- package/studio/dist/studio.css +449 -1
- package/studio/dist/studio.js +3 -3
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createHash } from "node:crypto"
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs"
|
|
3
|
+
import { join, relative } from "node:path"
|
|
4
|
+
|
|
5
|
+
import { gateCommands, readBaseline, writeCandidate } from "../gate-commands.js"
|
|
6
|
+
|
|
7
|
+
// Harness 2.0 W6: the gates that run a command the person confirmed (`kriya
|
|
8
|
+
// gates use`): G-DATA, G-EVAL, G-SAN, G-IAC, G-SIM, G-SIM-HEADLESS
|
|
9
|
+
// (Kriya-Tavi docs/kriya-harness/upgrade_2.0/GATES.md §3). Commands run
|
|
10
|
+
// through the same sandboxed runner as the project's check; none is ever
|
|
11
|
+
// written by a model. Without a confirmed command: not_checked, never pass.
|
|
12
|
+
|
|
13
|
+
const tail = (s, n = 1500) => String(s ?? "").slice(-n)
|
|
14
|
+
const nc = (id, gate) => ({ id, status: "not_checked", reason: `no command is confirmed for ${id} (kriya gates use ${gate} "<command>")` })
|
|
15
|
+
const ran = (r) => r.exitCode === 0 && !r.timedOut
|
|
16
|
+
|
|
17
|
+
/** A content hash of a file or a folder (paths and bytes, sorted), and its line count; null if missing */
|
|
18
|
+
export function outputDigest(root, rel) {
|
|
19
|
+
const abs = join(root, rel)
|
|
20
|
+
if (!existsSync(abs)) return null
|
|
21
|
+
const files = []
|
|
22
|
+
const walk = (p) => {
|
|
23
|
+
if (statSync(p).isDirectory()) for (const n of readdirSync(p).sort()) walk(join(p, n))
|
|
24
|
+
else files.push(p)
|
|
25
|
+
}
|
|
26
|
+
walk(abs)
|
|
27
|
+
const h = createHash("sha256")
|
|
28
|
+
let lines = 0
|
|
29
|
+
for (const f of files.slice(0, 2000)) {
|
|
30
|
+
const bytes = readFileSync(f)
|
|
31
|
+
h.update(relative(abs, f)).update("\0").update(bytes)
|
|
32
|
+
lines += bytes.toString("utf8").split("\n").length - 1
|
|
33
|
+
}
|
|
34
|
+
return { sha: h.digest("hex").slice(0, 16), files: files.length, lines }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** G-DATA: the pipeline twice on its fixture: both succeed, and the output is identical */
|
|
38
|
+
export async function dataGate({ root, command }) {
|
|
39
|
+
const cfg = gateCommands(root).data
|
|
40
|
+
if (!cfg) return nc("G-DATA", "data")
|
|
41
|
+
const first = await command(cfg.command)
|
|
42
|
+
if (!ran(first)) return { id: "G-DATA", status: "fail", kind: "gate", excerpt: `G-DATA: the pipeline failed (${first.timedOut ? "timed out" : `exit ${first.exitCode}`}):\n${tail(first.output)}` }
|
|
43
|
+
const a = outputDigest(root, cfg.output)
|
|
44
|
+
const second = await command(cfg.command)
|
|
45
|
+
if (!ran(second)) return { id: "G-DATA", status: "fail", kind: "gate", excerpt: `G-DATA: the pipeline failed when run again (${second.timedOut ? "timed out" : `exit ${second.exitCode}`}):\n${tail(second.output)}` }
|
|
46
|
+
const b = outputDigest(root, cfg.output)
|
|
47
|
+
if (!a || !b) return { id: "G-DATA", status: "fail", kind: "gate", excerpt: `G-DATA: the pipeline wrote nothing at ${cfg.output}` }
|
|
48
|
+
if (a.sha !== b.sha) return { id: "G-DATA", status: "fail", kind: "gate", excerpt: `G-DATA: running the pipeline twice changed its output at ${cfg.output} (${a.lines} → ${b.lines} lines): it isn't idempotent. Upsert or merge on a key, and replace partitions atomically.` }
|
|
49
|
+
return { id: "G-DATA", status: "pass", note: `two runs, identical output (${b.files} file(s), ${b.lines} lines)` }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The evaluation itself: a work that changes it can't be judged by it
|
|
53
|
+
export const EVAL_FILES = /(^|\/)(evals?|evaluation|benchmarks?)\/|(^|\/)(eval|evaluate|evaluation)[\w-]*\.(py|js|mjs|ts|ipynb|ya?ml|json)$/
|
|
54
|
+
|
|
55
|
+
/** G-EVAL: the confirmed evaluation; its metric must not regress past the confirmed baseline */
|
|
56
|
+
export async function evalGate({ root, command, changed = [], now = Date.now }) {
|
|
57
|
+
const cfg = gateCommands(root).eval
|
|
58
|
+
if (!cfg) return nc("G-EVAL", "eval")
|
|
59
|
+
const touched = changed.filter((rel) => EVAL_FILES.test(rel) && rel !== cfg.metrics)
|
|
60
|
+
if (touched.length) return { id: "G-EVAL", status: "not_checked", reason: `this work changed the evaluation itself (${touched.slice(0, 3).join(", ")}), so it can't judge the work; a person reviews evaluation changes` }
|
|
61
|
+
const started = now()
|
|
62
|
+
const out = await command(cfg.command)
|
|
63
|
+
if (!ran(out)) return { id: "G-EVAL", status: "fail", kind: "gate", excerpt: `G-EVAL: the evaluation failed (${out.timedOut ? "timed out" : `exit ${out.exitCode}`}):\n${tail(out.output)}` }
|
|
64
|
+
const file = join(root, cfg.metrics)
|
|
65
|
+
if (!existsSync(file) || statSync(file).mtimeMs < started - 1000) return { id: "G-EVAL", status: "fail", kind: "gate", excerpt: `G-EVAL: the evaluation didn't write ${cfg.metrics} in this run` }
|
|
66
|
+
let value
|
|
67
|
+
try {
|
|
68
|
+
value = JSON.parse(readFileSync(file, "utf8"))?.[cfg.metric]
|
|
69
|
+
} catch {
|
|
70
|
+
// Reported below
|
|
71
|
+
}
|
|
72
|
+
if (!Number.isFinite(value)) return { id: "G-EVAL", status: "fail", kind: "gate", excerpt: `G-EVAL: ${cfg.metrics} has no number "${cfg.metric}"` }
|
|
73
|
+
const baseline = readBaseline(root)
|
|
74
|
+
if (!baseline || baseline.metric !== cfg.metric) {
|
|
75
|
+
writeCandidate(root, { metric: cfg.metric, value, direction: cfg.direction, at: new Date().toISOString() })
|
|
76
|
+
return { id: "G-EVAL", status: "not_checked", reason: `no confirmed baseline yet: ${cfg.metric} = ${value} recorded; kriya eval baseline confirm makes it the baseline` }
|
|
77
|
+
}
|
|
78
|
+
const worse = cfg.direction === "lower" ? value > baseline.value + cfg.margin : value < baseline.value - cfg.margin
|
|
79
|
+
if (worse) return { id: "G-EVAL", status: "fail", kind: "gate", excerpt: `G-EVAL: ${cfg.metric} is ${value}, worse than the confirmed baseline ${baseline.value} (${cfg.direction} is better, margin ${cfg.margin}). A change ships only if it holds or beats the baseline.` }
|
|
80
|
+
return { id: "G-EVAL", status: "pass", note: `${cfg.metric} ${value} vs baseline ${baseline.value}` }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const SANITIZER = /==\d+==ERROR: (AddressSanitizer|LeakSanitizer)|WARNING: ThreadSanitizer|runtime error: |ERROR: AddressSanitizer|SUMMARY: (Address|Thread|Undefined|Leak)Sanitizer/
|
|
84
|
+
|
|
85
|
+
/** G-SAN, G-IAC, G-SIM: the confirmed command exits 0 (G-SAN also fails on any sanitizer report) */
|
|
86
|
+
export async function commandGate({ root, command, gate, id }) {
|
|
87
|
+
const cfg = gateCommands(root)[gate]
|
|
88
|
+
if (!cfg) return nc(id, gate)
|
|
89
|
+
const out = await command(cfg.command)
|
|
90
|
+
if (id === "G-SAN" && SANITIZER.test(out.output ?? "")) return { id, status: "fail", kind: "gate", excerpt: `G-SAN: a sanitizer reported a problem:\n${tail(String(out.output).split("\n").filter((l) => SANITIZER.test(l) || /^\s+#\d+ /.test(l)).slice(0, 30).join("\n"))}` }
|
|
91
|
+
if (!ran(out)) return { id, status: "fail", kind: "gate", excerpt: `${id}: \`${cfg.command}\` ${out.timedOut ? "timed out" : `exited ${out.exitCode}`}:\n${tail(out.output)}` }
|
|
92
|
+
return { id, status: "pass" }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const HASH = /^STATE-HASH:\s*([0-9a-fA-F]{8,128})\s*$/m
|
|
96
|
+
const FRAME = /^FRAME-MS-MEDIAN:\s*([\d.]+)\s*$/m
|
|
97
|
+
|
|
98
|
+
/** G-SIM-HEADLESS: the seeded simulation twice; the same STATE-HASH both times; the median frame time within budget if one is set */
|
|
99
|
+
export async function simHeadlessGate({ root, command }) {
|
|
100
|
+
const cfg = gateCommands(root)["sim-headless"]
|
|
101
|
+
if (!cfg) return nc("G-SIM-HEADLESS", "sim-headless")
|
|
102
|
+
const runs = [await command(cfg.command), await command(cfg.command)]
|
|
103
|
+
const bad = runs.find((r) => !ran(r))
|
|
104
|
+
if (bad) return { id: "G-SIM-HEADLESS", status: "fail", kind: "gate", excerpt: `G-SIM-HEADLESS: the simulation ${bad.timedOut ? "timed out" : `exited ${bad.exitCode}`}:\n${tail(bad.output)}` }
|
|
105
|
+
const hashes = runs.map((r) => HASH.exec(r.output ?? "")?.[1])
|
|
106
|
+
if (hashes.some((h) => !h)) return { id: "G-SIM-HEADLESS", status: "fail", kind: "gate", excerpt: "G-SIM-HEADLESS: the simulation must print STATE-HASH: <hex> of its final state" }
|
|
107
|
+
if (hashes[0] !== hashes[1]) return { id: "G-SIM-HEADLESS", status: "fail", kind: "gate", excerpt: `G-SIM-HEADLESS: the same seed gave two different final states (${hashes[0]} vs ${hashes[1]}): the simulation isn't deterministic (wall clock, unseeded random, frame-rate-dependent steps)` }
|
|
108
|
+
const frame = Number(FRAME.exec(runs[1].output ?? "")?.[1])
|
|
109
|
+
if (cfg.frameBudgetMs && Number.isFinite(frame) && frame > cfg.frameBudgetMs) return { id: "G-SIM-HEADLESS", status: "fail", kind: "gate", excerpt: `G-SIM-HEADLESS: median frame time ${frame} ms is over the ${cfg.frameBudgetMs} ms budget` }
|
|
110
|
+
return { id: "G-SIM-HEADLESS", status: "pass", note: `deterministic (${hashes[0].slice(0, 12)})${Number.isFinite(frame) ? `, median frame ${frame} ms` : ""}` }
|
|
111
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// Harness 2.0 W3: the fixed static detectors (Kriya-Tavi docs/kriya-harness/
|
|
2
|
+
// upgrade_2.0/CARTRIDGES.md §3–4, GATES.md G-DETECT). Each is plain code over
|
|
3
|
+
// one changed file: its path, its full text and the lines this work added.
|
|
4
|
+
// Findings point at added lines only, so code the project already had is
|
|
5
|
+
// never blamed. No detector runs anything, reads the network or takes a
|
|
6
|
+
// pattern from data: cartridges only name them by id, with a severity.
|
|
7
|
+
// Heuristic ones are warnings (feedback to the builder), never failures,
|
|
8
|
+
// unless a cartridge gives them severity "error".
|
|
9
|
+
|
|
10
|
+
const EXT = (re) => (path) => re.test(path)
|
|
11
|
+
const css = EXT(/\.(css|scss|sass|less)$/)
|
|
12
|
+
const ui = EXT(/\.(tsx|jsx|vue|svelte)$/)
|
|
13
|
+
const js = EXT(/\.(m?[jt]sx?|cjs)$/)
|
|
14
|
+
const py = EXT(/\.py$/)
|
|
15
|
+
const sql = EXT(/\.sql$/)
|
|
16
|
+
const tests = (path) => /(^|\/)(tests?|__tests__|spec|e2e)\//.test(path) || /\.(test|spec)\.[^/]+$/.test(path) || /(^|\/)test_[^/]+\.py$/.test(path)
|
|
17
|
+
const mobile = EXT(/\.(swift|kt|java|dart)$/)
|
|
18
|
+
const iac = EXT(/\.(tf|tfvars|json|ya?ml)$/)
|
|
19
|
+
const tokensFile = (path) => /(^|\/)(tokens|design-tokens|theme|globals|variables|colors?)\.[^/]+$/i.test(path)
|
|
20
|
+
const commentLine = (text) => /^\s*(\/\/|#|\*|\/\*|--|<!--)/.test(text)
|
|
21
|
+
|
|
22
|
+
/** Lines matching `re` among the added ones, as findings with `why`. */
|
|
23
|
+
const onAdded = (file, re, why, skip = commentLine) =>
|
|
24
|
+
file.added.filter(({ text }) => !skip(text) && re.test(text)).map(({ line }) => ({ line, why }))
|
|
25
|
+
|
|
26
|
+
/** The 1-based line of a character offset. */
|
|
27
|
+
const lineAt = (content, index) => content.slice(0, index).split("\n").length
|
|
28
|
+
|
|
29
|
+
/** Matches of a multi-line pattern whose first line was added. */
|
|
30
|
+
function onAddedSpans(file, re, why, keep = () => true) {
|
|
31
|
+
const added = new Set(file.added.map((a) => a.line))
|
|
32
|
+
const out = []
|
|
33
|
+
for (const m of file.content.matchAll(re)) {
|
|
34
|
+
const line = lineAt(file.content, m.index)
|
|
35
|
+
if (added.has(line) && keep(m)) out.push({ line, why })
|
|
36
|
+
}
|
|
37
|
+
return out
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const shannon = (s) => {
|
|
41
|
+
const counts = new Map()
|
|
42
|
+
for (const c of s) counts.set(c, (counts.get(c) ?? 0) + 1)
|
|
43
|
+
let h = 0
|
|
44
|
+
for (const n of counts.values()) h -= (n / s.length) * Math.log2(n / s.length)
|
|
45
|
+
return h
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 1-based line numbers inside the blocks whose opening line matches `header` (the callback's body). */
|
|
49
|
+
function blockLines(content, header) {
|
|
50
|
+
const lines = content.split("\n")
|
|
51
|
+
const hot = new Set()
|
|
52
|
+
for (let i = 0; i < lines.length; i++) {
|
|
53
|
+
if (!header.test(lines[i]) || !lines[i].includes("{")) continue
|
|
54
|
+
let depth = 0
|
|
55
|
+
for (let j = i; j < lines.length; j++) {
|
|
56
|
+
depth += (lines[j].match(/\{/g) ?? []).length - (lines[j].match(/\}/g) ?? []).length
|
|
57
|
+
if (j > i) hot.add(j + 1)
|
|
58
|
+
if (depth <= 0 && j > i) break
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return hot
|
|
62
|
+
}
|
|
63
|
+
const ANIMATION = /\b(function\s+(animate|render|tick|loop|frame)\b|(animate|render|tick|loop|frame)\s*=\s*(async\s*)?(\([^)]*\)|\w+)\s*=>|setAnimationLoop\s*\(|useFrame\s*\(|requestAnimationFrame\s*\()/
|
|
64
|
+
const GPU_RESOURCE = /\bnew\s+(THREE\.)?(\w*Geometry|\w*Material|\w*Texture|WebGLRenderTarget|WebGLRenderer)\s*\(|\bTextureLoader\s*\(\s*\)\s*\.load\s*\(/
|
|
65
|
+
const ASSET = /(^|\/)(assets|public|static|models|textures|hdri|media)\/.*\.(glb|gltf|hdr|exr|ktx2|png|jpe?g)$/i
|
|
66
|
+
|
|
67
|
+
/** Detector id → { files, find(file) }; `file` = { path, content, added: [{ line, text }] }. */
|
|
68
|
+
export const DETECTORS = {
|
|
69
|
+
"FE-HEX": {
|
|
70
|
+
files: (p) => (ui(p) || css(p)) && !tokensFile(p),
|
|
71
|
+
find: (f) => onAdded(f, /(?<![\w&/])#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b(?![\w-])/, "a raw colour value; use a design token"),
|
|
72
|
+
},
|
|
73
|
+
"FE-PX": {
|
|
74
|
+
files: (p) => (ui(p) || css(p)) && !tokensFile(p),
|
|
75
|
+
find: (f) =>
|
|
76
|
+
f.added
|
|
77
|
+
.filter(({ text }) => !commentLine(text))
|
|
78
|
+
.flatMap(({ line, text }) =>
|
|
79
|
+
[...text.matchAll(/\b(margin|padding|gap|top|left|right|bottom|inset)[\w-]*\s*:\s*['"]?([^;'"}]*)/gi)].flatMap((m) =>
|
|
80
|
+
[...m[2].matchAll(/(\d+)px/g)].filter((n) => Number(n[1]) > 2 && Number(n[1]) % 4 !== 0).map(() => ({ line, why: "spacing off the 4 px grid" }))
|
|
81
|
+
)
|
|
82
|
+
),
|
|
83
|
+
},
|
|
84
|
+
"FE-IMG-ALT": {
|
|
85
|
+
files: ui,
|
|
86
|
+
find: (f) => onAddedSpans(f, /<img\b[^>]*>/g, "an image without alt", (m) => !/\balt\s*=/.test(m[0])),
|
|
87
|
+
},
|
|
88
|
+
"FE-CLICK-DIV": {
|
|
89
|
+
files: ui,
|
|
90
|
+
find: (f) => onAddedSpans(f, /<(div|span|li|td)\b[^>]*\bonClick\s*=[^>]*>/g, "a click handler on a non-interactive element without a role and a key handler", (m) => !(/\brole\s*=/.test(m[0]) && /\bonKey(Down|Up|Press)\s*=/.test(m[0]))),
|
|
91
|
+
},
|
|
92
|
+
"FE-FETCH-EFFECT": {
|
|
93
|
+
files: (p) => ui(p) || js(p),
|
|
94
|
+
find: (f) => (/useEffect\s*\(/.test(f.content) && !/AbortController|signal\s*:/.test(f.content) ? onAdded(f, /\bfetch\s*\(/, "a fetch in an effect that can't be cancelled") : []),
|
|
95
|
+
},
|
|
96
|
+
"FE-GLASS-CONTENT": {
|
|
97
|
+
files: css,
|
|
98
|
+
find: (f) => onAddedSpans(f, /([^{}]*)\{[^{}]*backdrop-filter[^{}]*\}/g, "glass behind content; keep it for controls and navigation", (m) => /\b(article|prose|content|post|paragraph|body-text)\b/i.test(m[1])),
|
|
99
|
+
},
|
|
100
|
+
"FE-GLASS-FALLBACK": {
|
|
101
|
+
files: (p) => css(p) || ui(p),
|
|
102
|
+
find: (f) => {
|
|
103
|
+
if (!f.added.some(({ text }) => /backdrop-filter\s*:/.test(text))) return []
|
|
104
|
+
const missing = [
|
|
105
|
+
[/prefers-reduced-transparency/, "prefers-reduced-transparency"],
|
|
106
|
+
[/prefers-contrast/, "prefers-contrast"],
|
|
107
|
+
[/@supports\s+not\s*\(\s*(-webkit-)?backdrop-filter/, "@supports not (backdrop-filter)"],
|
|
108
|
+
].filter(([re]) => !re.test(f.content))
|
|
109
|
+
if (!missing.length) return []
|
|
110
|
+
const line = f.added.find(({ text }) => /backdrop-filter\s*:/.test(text)).line
|
|
111
|
+
return [{ line, why: `glass without its solid fallbacks: ${missing.map(([, n]) => n).join(", ")}` }]
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
"FE-TROPE": {
|
|
115
|
+
files: (p) => ui(p) || /\.(html|md|mdx)$/.test(p),
|
|
116
|
+
find: (f) => [
|
|
117
|
+
...onAdded(f, /\blorem ipsum\b/i, "placeholder text", () => false),
|
|
118
|
+
...onAdded(f, />\s*\p{Extended_Pictographic}\s*</u, "an emoji used as an icon", () => false),
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
"FE-FONT-LICENCE": {
|
|
122
|
+
files: EXT(/\.(woff2?|ttf|otf)$/),
|
|
123
|
+
find: (f) => (f.isNew && !f.siblings.some((n) => /^(OFL|LICEN[CS]E|COPYING)/i.test(n)) ? [{ line: 1, why: "a font file without its licence beside it" }] : []),
|
|
124
|
+
},
|
|
125
|
+
// Harness 2.0 W13 (WD-17; THREE_D_AND_CREATIVE.md §2): graphics
|
|
126
|
+
"GX-COLOR": {
|
|
127
|
+
files: js,
|
|
128
|
+
find: (f) =>
|
|
129
|
+
/\boutputColorSpace\b|\boutputEncoding\b/.test(f.content) && /\btoneMapping\s*=/.test(f.content)
|
|
130
|
+
? []
|
|
131
|
+
: onAdded(f, /\bnew\s+(THREE\.)?WebGL(GPU)?Renderer\s*\(/, "a renderer with no output colour space or tone mapping set; set renderer.outputColorSpace and one renderer.toneMapping"),
|
|
132
|
+
},
|
|
133
|
+
"GX-ALLOC-LOOP": {
|
|
134
|
+
files: js,
|
|
135
|
+
find: (f) => {
|
|
136
|
+
const hot = blockLines(f.content, ANIMATION)
|
|
137
|
+
return f.added
|
|
138
|
+
.filter(({ line, text }) => hot.has(line) && !commentLine(text) && /\bnew\s+(THREE\.\w+|Vector[234]|Matrix[34]|Quaternion|Color|Euler|Box3|Sphere|Ray|Raycaster)\s*\(/.test(text))
|
|
139
|
+
.map(({ line }) => ({ line, why: "an allocation in the animation callback; create it once outside and reuse it" }))
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
"GX-NO-DISPOSE": {
|
|
143
|
+
files: js,
|
|
144
|
+
find: (f) => {
|
|
145
|
+
if (/\.dispose\s*\(/.test(f.content)) return []
|
|
146
|
+
const first = f.added.find(({ text }) => !commentLine(text) && GPU_RESOURCE.test(text))
|
|
147
|
+
return first ? [{ line: first.line, why: "GPU resources created with no dispose path; dispose geometries, materials, textures and targets on unmount" }] : []
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
"GX-DPR": {
|
|
151
|
+
files: js,
|
|
152
|
+
find: (f) => onAdded(f, /setPixelRatio\s*\((?![^)]*\b(Math\.min|min|clamp)\s*\()[^)]*\bdevicePixelRatio\b/, "the pixel ratio follows devicePixelRatio uncapped; use Math.min(devicePixelRatio, the tier's cap)"),
|
|
153
|
+
},
|
|
154
|
+
"FE-ASSET-LICENCE": {
|
|
155
|
+
files: (p) => ASSET.test(p),
|
|
156
|
+
find: (f) => (f.isNew && !f.assetLicensed(f.path) ? [{ line: 1, why: "a new asset with no entry in scene_direction.json assets (path, licence, source)" }] : []),
|
|
157
|
+
},
|
|
158
|
+
"BE-SQL-INTERP": {
|
|
159
|
+
files: (p) => js(p) || py(p),
|
|
160
|
+
find: (f) => [
|
|
161
|
+
...onAddedSpans(f, /(\w*\.?)`[^`]*\b(SELECT|INSERT|UPDATE|DELETE)\b[^`]*\$\{[^`]*`/gi, "SQL built by string interpolation; use parameters", (m) => !/sql$/i.test(m[1])),
|
|
162
|
+
...onAdded(f, /\bf["'][^"']*\b(SELECT|INSERT|UPDATE|DELETE)\b[^"']*\{/i, "SQL built with an f-string; use parameters"),
|
|
163
|
+
...onAdded(f, /["'][^"']*\b(SELECT|INSERT|UPDATE|DELETE)\b[^"']*["']\s*\+\s*\w/i, "SQL built by concatenation; use parameters"),
|
|
164
|
+
],
|
|
165
|
+
},
|
|
166
|
+
"BE-EMPTY-CATCH": {
|
|
167
|
+
files: (p) => js(p) || py(p) || /\.(java|kt|cs)$/.test(p),
|
|
168
|
+
find: (f) => {
|
|
169
|
+
const out = onAdded(f, /\bcatch\s*(\([^)]*\))?\s*\{\s*\}/, "an empty catch: the error disappears")
|
|
170
|
+
const lines = f.content.split("\n")
|
|
171
|
+
for (const { line, text } of f.added)
|
|
172
|
+
if (/^\s*except\b[^:]*:\s*(pass)?\s*$/.test(text) && (/:\s*pass\s*$/.test(text) || /^\s*pass\s*$/.test(lines[line] ?? ""))) out.push({ line, why: "an except that only passes: the error disappears" })
|
|
173
|
+
return out
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
"BE-NO-TIMEOUT": {
|
|
177
|
+
files: js,
|
|
178
|
+
find: (f) => (/AbortSignal\.timeout|signal\s*:|timeout\s*:/.test(f.content) ? [] : onAdded(f, /\b(fetch|axios\.(get|post|put|patch|delete))\s*\(/, "an outbound call without a timeout or signal")),
|
|
179
|
+
},
|
|
180
|
+
"BE-LOG-SECRET": {
|
|
181
|
+
files: (p) => js(p) || py(p),
|
|
182
|
+
find: (f) => onAdded(f, /\b(console\.(log|info|warn|error|debug)|log(ger)?\.(info|warn|error|debug)|print)\s*\([^)]*\b(password|passwd|secret|api[_-]?key|apiKey|private[_-]?key|access[_-]?token|accessToken|refresh[_-]?token|refreshToken|auth[_-]?token|authToken)\b/i, "a log line that may print a secret"),
|
|
183
|
+
},
|
|
184
|
+
"FS-DTO-DUP": {
|
|
185
|
+
files: EXT(/\.tsx?$/),
|
|
186
|
+
find: (f) => {
|
|
187
|
+
const mine = new Set([...f.content.matchAll(/export\s+(?:interface|type)\s+(\w+)/g)].map((m) => m[1]))
|
|
188
|
+
const dup = [...mine].filter((name) => f.others.some((o) => o.path !== f.path && new RegExp(`export\\s+(?:interface|type)\\s+${name}\\b`).test(o.content)))
|
|
189
|
+
return dup.flatMap((name) => onAdded(f, new RegExp(`export\\s+(?:interface|type)\\s+${name}\\b`), `${name} is declared in two files; generate one from the contract`))
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
"MB-SYNC-IO": {
|
|
193
|
+
files: mobile,
|
|
194
|
+
find: (f) => onAdded(f, /\b(Data|String)\(contentsOf:|readAsStringSync\(|readAsBytesSync\(|\.execute\(\)\s*;?\s*$/, "synchronous I/O that can block the main thread"),
|
|
195
|
+
},
|
|
196
|
+
"MB-HARDCODE-DIM": {
|
|
197
|
+
files: mobile,
|
|
198
|
+
find: (f) => onAdded(f, /\b(width|height)\s*[:=(]\s*(320|360|375|390|393|412|414|428|430|768|812|844|852|896|926|932|1024)\b/, "a hard-coded screen dimension"),
|
|
199
|
+
},
|
|
200
|
+
"GM-ALLOC-TICK": {
|
|
201
|
+
files: (p) => js(p) || /\.(cs|cpp|cc|rs)$/.test(p),
|
|
202
|
+
find: (f) => {
|
|
203
|
+
const lines = f.content.split("\n")
|
|
204
|
+
const hot = new Set()
|
|
205
|
+
for (let i = 0; i < lines.length; i++) {
|
|
206
|
+
if (!/\b(update|tick|fixedUpdate|onFrame|step)\s*\([^)]*\)\s*(:\s*[\w<>[\]]+\s*)?\{/.test(lines[i])) continue
|
|
207
|
+
let depth = 0
|
|
208
|
+
for (let j = i; j < lines.length; j++) {
|
|
209
|
+
depth += (lines[j].match(/\{/g) ?? []).length - (lines[j].match(/\}/g) ?? []).length
|
|
210
|
+
if (j > i) hot.add(j + 1)
|
|
211
|
+
if (depth <= 0 && j > i) break
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return f.added.filter(({ line, text }) => hot.has(line) && !commentLine(text) && /\bnew\s+[A-Z]\w*\s*\(|=\s*\[\s*\]|=\s*\{\s*\}/.test(text)).map(({ line }) => ({ line, why: "an allocation in the per-frame loop; pool it" }))
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
"GM-DT-MISSING": {
|
|
218
|
+
files: (p) => js(p) || /\.(cs|gd|cpp|rs)$/.test(p),
|
|
219
|
+
find: (f) => onAdded(f, /\.(x|y|z)\s*[+-]=\s*[^;\n]*\b(speed|velocity|vel)\b(?![^;\n]*\b(dt|delta|deltaTime|elapsed|frameTime)\b)/, "movement without the timestep"),
|
|
220
|
+
},
|
|
221
|
+
"OPS-LATEST": {
|
|
222
|
+
files: (p) => /(^|\/)(Dockerfile|[^/]*\.dockerfile)$/i.test(p) || /(^|\/)docker-compose[^/]*\.ya?ml$/.test(p),
|
|
223
|
+
find: (f) => {
|
|
224
|
+
const stages = new Set([...f.content.matchAll(/^\s*FROM\s+\S+\s+AS\s+(\S+)/gim)].map((m) => m[1].toLowerCase()))
|
|
225
|
+
return f.added.flatMap(({ line, text }) => {
|
|
226
|
+
const from = /^\s*FROM\s+(?:--\S+\s+)*(\S+)/i.exec(text)?.[1]
|
|
227
|
+
const image = from ?? /^\s*image:\s*["']?([^"'\s]+)/.exec(text)?.[1]
|
|
228
|
+
if (!image || image === "scratch" || stages.has(image.toLowerCase()) || image.startsWith("$")) return []
|
|
229
|
+
if (image.includes("@sha256:")) return []
|
|
230
|
+
const tag = /:([^/]+)$/.exec(image)?.[1]
|
|
231
|
+
return !tag || tag === "latest" ? [{ line, why: `${image} isn't pinned to a version` }] : []
|
|
232
|
+
})
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
"OPS-SECRET-ENV": {
|
|
236
|
+
files: (p) => /(^|\/)(Dockerfile|[^/]*\.dockerfile)$/i.test(p) || /(^|\/)\.github\/workflows\/|\.gitlab-ci\.yml$|(^|\/)render\.yaml$|docker-compose[^/]*\.ya?ml$/.test(p),
|
|
237
|
+
find: (f) => [
|
|
238
|
+
...onAdded(f, /^\s*(ENV|ARG)\s+\w*(SECRET|TOKEN|PASSWORD|PASSWD|API_?KEY|PRIVATE_?KEY)\w*[= ]\s*\S+/i, "a secret given a value in the image"),
|
|
239
|
+
...onAdded(f, /\b\w*(secret|token|password|api_?key)\w*\s*:\s*["']?(?!\$\{\{)[A-Za-z0-9_\-/+=]{12,}["']?\s*$/i, "a secret written in a CI or deploy file"),
|
|
240
|
+
],
|
|
241
|
+
},
|
|
242
|
+
"OPS-MIGRATE-LOCK": {
|
|
243
|
+
files: sql,
|
|
244
|
+
find: (f) => [
|
|
245
|
+
...onAdded(f, /\badd\s+column\b(?![^;]*\bdefault\b)[^;]*\bnot\s+null\b/i, "a NOT NULL column without a default locks and fails on a filled table"),
|
|
246
|
+
...onAdded(f, /\bcreate\s+(unique\s+)?index\b(?!\s+concurrently)/i, "an index built without CONCURRENTLY locks writes"),
|
|
247
|
+
],
|
|
248
|
+
},
|
|
249
|
+
"CL-IAM-STAR": {
|
|
250
|
+
files: iac,
|
|
251
|
+
find: (f) => onAdded(f, /["']?(Action|Resource|actions|resources)["']?\s*[:=]\s*(\[\s*)?["']\*["']/, "a wildcard permission"),
|
|
252
|
+
},
|
|
253
|
+
"CL-PUBLIC": {
|
|
254
|
+
files: iac,
|
|
255
|
+
find: (f) => {
|
|
256
|
+
const out = onAdded(f, /\bpublicly_accessible\s*=\s*true\b|\bacl\s*=\s*["']public-read(-write)?["']|\bblock_public_(acls|policy)\s*=\s*false\b/, "a publicly reachable data store")
|
|
257
|
+
const lines = f.content.split("\n")
|
|
258
|
+
for (const { line, text } of f.added) {
|
|
259
|
+
if (!/0\.0\.0\.0\/0/.test(text)) continue
|
|
260
|
+
const block = lines.slice(Math.max(0, line - 8), line + 8).join("\n")
|
|
261
|
+
const ports = [...block.matchAll(/\b(from_port|to_port|port)\s*=\s*(\d+)/g)].map((m) => Number(m[2]))
|
|
262
|
+
if (/\bingress\b/.test(block) && ports.some((p) => p !== 80 && p !== 443)) out.push({ line, why: "ingress from anywhere on a non-web port" })
|
|
263
|
+
}
|
|
264
|
+
return out
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
"SY-UNSAFE-NOCOMMENT": {
|
|
268
|
+
files: EXT(/\.rs$/),
|
|
269
|
+
find: (f) => {
|
|
270
|
+
const lines = f.content.split("\n")
|
|
271
|
+
return f.added.filter(({ line, text }) => /\bunsafe\s*\{/.test(text) && !lines.slice(Math.max(0, line - 4), line - 1).some((l) => /SAFETY:/.test(l))).map(({ line }) => ({ line, why: "an unsafe block without a SAFETY: comment" }))
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
"SY-SPIN": {
|
|
275
|
+
files: EXT(/\.(rs|c|cc|cpp|h|hpp|go|java|m?[jt]s)$/),
|
|
276
|
+
find: (f) => onAdded(f, /\bwhile\s*\(.*\)\s*(\{\s*\}|;)\s*$|\bloop\s*\{\s*\}|\bfor\s*\(\s*;\s*;\s*\)\s*\{\s*\}/, "a busy wait without backoff"),
|
|
277
|
+
},
|
|
278
|
+
"DT-INSERT-NOKEY": {
|
|
279
|
+
files: (p) => /(^|\/)(dags|pipelines?|etl|jobs|models)\//.test(p) && (sql(p) || py(p)),
|
|
280
|
+
find: (f) => onAddedSpans(f, /\binsert\s+into\b[^;]*;?/gi, "an insert a retry would repeat; upsert or merge on a key", (m) => !/\bon\s+conflict\b|\bmerge\b/i.test(m[0])),
|
|
281
|
+
},
|
|
282
|
+
"ML-FIT-TEST": {
|
|
283
|
+
files: (p) => py(p) || /\.ipynb$/.test(p),
|
|
284
|
+
find: (f) => onAdded(f, /\.(fit|fit_transform|partial_fit)\s*\([^)]*\b(X_test|x_test|test_x|test_X|test_data|test_df|test_set|y_test)\b/, "fitting on the test split"),
|
|
285
|
+
},
|
|
286
|
+
"ML-SEED": {
|
|
287
|
+
files: (p) => /(^|\/)(train|training|eval|evaluate)[^/]*\.py$/.test(p),
|
|
288
|
+
find: (f) => (f.isNew && !/random_state|manual_seed|set_seed|np\.random\.seed|random\.seed|seed\s*=/.test(f.content) ? [{ line: 1, why: "a training or evaluation entry point without a fixed seed" }] : []),
|
|
289
|
+
},
|
|
290
|
+
"SEC-BODY-ID": {
|
|
291
|
+
files: (p) => js(p) || py(p),
|
|
292
|
+
find: (f) => onAdded(f, /\b(req\.body|req\.query|req\.params|request\.json\(\)|request\.args|request\.form|searchParams\.get\(|body)\W{1,3}["']?(user_?id|tenant_?id|userId|tenantId|org_?id|orgId|owner_?id|ownerId)\b/, "an owner or tenant id taken from the request; take it from the verified session"),
|
|
293
|
+
},
|
|
294
|
+
"SEC-EVAL": {
|
|
295
|
+
files: (p) => js(p) || py(p),
|
|
296
|
+
find: (f) => [
|
|
297
|
+
...onAdded(f, /(?<![\w.])(eval|exec)\s*\(|\bnew\s+Function\s*\(/, "code built from strings at run time"),
|
|
298
|
+
...onAdded(f, /\b(child_process\.)?(exec|execSync)\s*\(\s*`[^`]*\$\{|\bos\.system\s*\(\s*f["']|\bsubprocess\.\w+\([^)]*shell\s*=\s*True/, "a shell command built from input"),
|
|
299
|
+
],
|
|
300
|
+
},
|
|
301
|
+
"QA-ONLY": {
|
|
302
|
+
files: tests,
|
|
303
|
+
find: (f) => onAdded(f, /\b(it|test|describe|context)\.only\s*\(|\bf(it|describe)\s*\(/, "a focused test that silences the rest"),
|
|
304
|
+
},
|
|
305
|
+
"QA-SKIP": {
|
|
306
|
+
files: tests,
|
|
307
|
+
find: (f) => onAdded(f, /\b(it|test|describe)\.skip\s*\(|\bx(it|describe)\s*\(|@pytest\.mark\.skip|\bt\.Skip\(/, "a newly skipped test"),
|
|
308
|
+
},
|
|
309
|
+
"SD-SLEEP": {
|
|
310
|
+
files: tests,
|
|
311
|
+
find: (f) => onAdded(f, /\bwaitForTimeout\s*\(|\btime\.sleep\s*\(|\bThread\.sleep\s*\(|\bawait\s+sleep\s*\(|new\s+Promise\s*\(\s*\(?\s*\w+\s*\)?\s*=>\s*setTimeout\s*\(/, "a fixed sleep in a test; wait for a condition"),
|
|
312
|
+
},
|
|
313
|
+
"SD-XPATH": {
|
|
314
|
+
files: tests,
|
|
315
|
+
find: (f) => onAdded(f, /xpath=|["']\/\/\w+\[|:nth-child\(|["']\/html\/body/, "a brittle selector; use role, label or test id"),
|
|
316
|
+
},
|
|
317
|
+
"DB-NO-RLS": {
|
|
318
|
+
files: sql,
|
|
319
|
+
find: (f) =>
|
|
320
|
+
onAddedSpans(f, /\bcreate\s+table\s+(?:if\s+not\s+exists\s+)?([\w."]+)/gi, "a table without row-level security in the same migration", (m) => {
|
|
321
|
+
const name = m[1].replace(/"/g, "")
|
|
322
|
+
if (!f.tenantTables(name)) return false
|
|
323
|
+
const esc = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
324
|
+
return !new RegExp(`alter\\s+table\\s+(only\\s+)?"?${esc.replace(/\\\./g, '"?\\."?')}"?\\s+enable\\s+row\\s+level\\s+security`, "i").test(f.content)
|
|
325
|
+
}),
|
|
326
|
+
},
|
|
327
|
+
"DB-FK-NOINDEX": {
|
|
328
|
+
files: sql,
|
|
329
|
+
find: (f) =>
|
|
330
|
+
f.added.flatMap(({ line, text }) => {
|
|
331
|
+
const col = /^\s*"?(\w+)"?\s+[\w ()]+\breferences\b/i.exec(text)?.[1]
|
|
332
|
+
if (!col) return []
|
|
333
|
+
return new RegExp(`create\\s+(unique\\s+)?index[^;]*\\(\\s*"?${col}"?`, "i").test(f.content) ? [] : [{ line, why: `the foreign key ${col} has no index` }]
|
|
334
|
+
}),
|
|
335
|
+
},
|
|
336
|
+
"DB-DROP": {
|
|
337
|
+
files: sql,
|
|
338
|
+
find: (f) => onAdded(f, /\bdrop\s+(table|column)\b|\balter\s+column\s+\w+\s+(set\s+data\s+)?type\b/i, "a destructive change; plan it in two releases"),
|
|
339
|
+
},
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** High-entropy secrets and known key shapes on added lines (G-SECRETS); the value itself is never returned. */
|
|
343
|
+
export const SECRET_RULES = [
|
|
344
|
+
["an AWS access key", /\bAKIA[0-9A-Z]{16}\b/],
|
|
345
|
+
["a GitHub token", /\b(gh[pousr]_[A-Za-z0-9]{36,}|github_pat_[A-Za-z0-9_]{60,})\b/],
|
|
346
|
+
["a Slack token", /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/],
|
|
347
|
+
["a Stripe live key", /\b(sk|rk)_live_[A-Za-z0-9]{20,}\b/],
|
|
348
|
+
["a Google API key", /\bAIza[0-9A-Za-z_-]{35}\b/],
|
|
349
|
+
["an Anthropic or OpenAI key", /\bsk-(ant-)?[A-Za-z0-9_-]{32,}\b/],
|
|
350
|
+
["a private key", /-----BEGIN ([A-Z]+ )?PRIVATE KEY-----/],
|
|
351
|
+
["a JSON web token", /\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/],
|
|
352
|
+
]
|
|
353
|
+
const SECRET_ASSIGN = /\b\w*(secret|token|password|passwd|api[_-]?key|private[_-]?key|access[_-]?key)\w*["']?\s*[:=]\s*["']([^"'\s]{32,})["']/i
|
|
354
|
+
const NOT_SECRETS = /(^|\/)(package-lock\.json|pnpm-lock\.yaml|yarn\.lock|Cargo\.lock|poetry\.lock|go\.sum)$|\.min\.(js|css)$|\.(png|jpe?g|gif|webp|woff2?|ttf|otf|ico|pdf|zip|gz)$/
|
|
355
|
+
|
|
356
|
+
/** @param {{ path: string, added: Array<{ line: number, text: string }> }} file */
|
|
357
|
+
export function findSecrets(file) {
|
|
358
|
+
if (NOT_SECRETS.test(file.path)) return []
|
|
359
|
+
const out = []
|
|
360
|
+
if (/(^|\/)\.env(\.[\w-]+)?$/.test(file.path) && !/\.(example|sample|template)$/.test(file.path)) out.push({ line: 1, why: "an environment file is in the changes; keep it out of git" })
|
|
361
|
+
for (const { line, text } of file.added) {
|
|
362
|
+
const rule = SECRET_RULES.find(([, re]) => re.test(text))
|
|
363
|
+
if (rule) out.push({ line, why: rule[0] })
|
|
364
|
+
else {
|
|
365
|
+
const m = SECRET_ASSIGN.exec(text)
|
|
366
|
+
if (m && shannon(m[2]) >= 4.5 && !/^\$\{|^process\.env|^os\.environ|^<|^your[_-]|^xxx|^changeme/i.test(m[2])) out.push({ line, why: "a high-entropy secret value" })
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return out
|
|
370
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { localUrl } from "../browser.js"
|
|
2
|
+
|
|
3
|
+
// Harness 2.0 W5: the only way the API and tenant gates reach an app
|
|
4
|
+
// (Kriya-Tavi docs/kriya-harness/upgrade_2.0/GATES.md rule 4). Every URL must
|
|
5
|
+
// pass the agent browser's loopback rule; redirects are never followed (a 3xx
|
|
6
|
+
// is just the answer); each request is time-boxed and each gate run has a
|
|
7
|
+
// request budget, so a gate can't flood even the person's own app.
|
|
8
|
+
|
|
9
|
+
export const REQUEST_MS = 10_000
|
|
10
|
+
export const BUDGET = 80
|
|
11
|
+
|
|
12
|
+
export class Budget {
|
|
13
|
+
constructor(limit = BUDGET) {
|
|
14
|
+
this.left = limit
|
|
15
|
+
}
|
|
16
|
+
take() {
|
|
17
|
+
if (this.left <= 0) throw new Error(`the gate's request budget (${BUDGET}) ran out`)
|
|
18
|
+
this.left--
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** One request to the confirmed local app; never anything else. */
|
|
23
|
+
export async function local(base, path, { method = "GET", body, headers = {}, budget, request = fetch } = {}) {
|
|
24
|
+
const target = localUrl(new URL(path, base).href)
|
|
25
|
+
if (target.error) throw new Error(`refused ${path}: ${target.error}`)
|
|
26
|
+
budget?.take()
|
|
27
|
+
const started = performance.now()
|
|
28
|
+
let response
|
|
29
|
+
try {
|
|
30
|
+
response = await request(target.url, {
|
|
31
|
+
method,
|
|
32
|
+
redirect: "manual",
|
|
33
|
+
headers: { accept: "application/json", ...(body !== undefined && typeof body !== "string" && { "content-type": "application/json" }), ...headers },
|
|
34
|
+
...(body !== undefined && { body: typeof body === "string" ? body : JSON.stringify(body) }),
|
|
35
|
+
signal: AbortSignal.timeout(REQUEST_MS),
|
|
36
|
+
})
|
|
37
|
+
} catch (error) {
|
|
38
|
+
return { status: 0, error: String(error?.cause?.code ?? error?.name ?? error).slice(0, 80), ms: performance.now() - started }
|
|
39
|
+
}
|
|
40
|
+
const text = await response.text().catch(() => "")
|
|
41
|
+
let json
|
|
42
|
+
try {
|
|
43
|
+
json = text ? JSON.parse(text) : undefined
|
|
44
|
+
} catch {
|
|
45
|
+
// Not JSON
|
|
46
|
+
}
|
|
47
|
+
return { status: response.status, text: text.slice(0, 20_000), json, headers: response.headers, ms: performance.now() - started }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** "/orders/{id}" with { id: 7 } → "/orders/7" (values URI-encoded) */
|
|
51
|
+
export const fill = (path, values = {}) => String(path).replace(/\{(\w+)\}/g, (m, k) => (values[k] === undefined ? m : encodeURIComponent(String(values[k]))))
|
|
52
|
+
|
|
53
|
+
export const median = (xs) => {
|
|
54
|
+
if (!xs.length) return undefined
|
|
55
|
+
const s = [...xs].sort((a, b) => a - b)
|
|
56
|
+
return s[Math.floor(s.length / 2)]
|
|
57
|
+
}
|
|
58
|
+
export const p95 = (xs) => {
|
|
59
|
+
if (!xs.length) return undefined
|
|
60
|
+
const s = [...xs].sort((a, b) => a - b)
|
|
61
|
+
return s[Math.min(s.length - 1, Math.ceil(s.length * 0.95) - 1)]
|
|
62
|
+
}
|