@omacode/micromanager 0.1.1 → 1.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/README.md +7 -4
- package/THIRD_PARTY.md +100 -0
- package/decider/.python-version +1 -0
- package/decider/README.md +35 -0
- package/decider/decider/__init__.py +1 -0
- package/decider/decider/server.py +327 -0
- package/decider/pyproject.toml +34 -0
- package/decider/uv.lock +1255 -0
- package/dist/agenda.js +51 -0
- package/dist/agents.js +58 -0
- package/dist/artifact/handoff.js +281 -0
- package/dist/ask.js +200 -0
- package/dist/backends.js +219 -0
- package/dist/backlog.js +414 -0
- package/dist/bench.js +427 -0
- package/dist/board.js +1 -0
- package/dist/brief.js +202 -0
- package/dist/browser.js +679 -0
- package/dist/budget.js +61 -0
- package/dist/catalog.js +187 -0
- package/dist/checkchange.js +145 -0
- package/dist/checkpoint.js +94 -0
- package/dist/cli.js +764 -0
- package/dist/comfy-gate.js +258 -0
- package/dist/compact.js +142 -0
- package/dist/config.js +344 -0
- package/dist/context/capture.js +1 -0
- package/dist/context/digest.js +1 -0
- package/dist/context/embed.js +1 -0
- package/dist/context/index.js +2 -0
- package/dist/context/ledger.js +1 -0
- package/dist/context/manager.js +1 -0
- package/dist/context/memory.js +1 -0
- package/dist/context/policy.js +1 -0
- package/dist/context/prices.js +1 -0
- package/dist/context/recall.js +1 -0
- package/dist/context/rules.js +1 -0
- package/dist/context/search.js +1 -0
- package/dist/context/status.js +1 -0
- package/dist/context/store.js +1 -0
- package/dist/context/summary.js +1 -0
- package/dist/context/tokens.js +1 -0
- package/dist/context/tools.js +1 -0
- package/dist/continuation.js +184 -0
- package/dist/contract.js +294 -0
- package/dist/contractround.js +178 -0
- package/dist/control.js +1 -0
- package/dist/creative-tools.js +237 -0
- package/dist/dataset.js +175 -0
- package/dist/decide.js +676 -0
- package/dist/decider.js +171 -0
- package/dist/design-shell.js +448 -0
- package/dist/design.js +1410 -0
- package/dist/desktop.js +44 -0
- package/dist/egress.js +156 -0
- package/dist/eval.js +60 -0
- package/dist/evolver.js +302 -0
- package/dist/features.js +193 -0
- package/dist/followups.js +32 -0
- package/dist/forms.js +195 -0
- package/dist/gate.js +108 -0
- package/dist/gatebox.js +46 -0
- package/dist/grants.js +189 -0
- package/dist/graph.js +65 -0
- package/dist/guard/index.js +1 -0
- package/dist/guard/wire.js +1 -0
- package/dist/helpers.js +85 -0
- package/dist/hooks.js +158 -0
- package/dist/image-models.js +117 -0
- package/{src/integration.ts → dist/integration.js} +1 -2
- package/dist/journal.js +1 -0
- package/dist/look.js +363 -0
- package/dist/memory.js +1 -0
- package/dist/mode.js +97 -0
- package/dist/native.js +3348 -0
- package/dist/nogit.js +259 -0
- package/dist/outcomes.js +148 -0
- package/dist/permission.js +35 -0
- package/dist/pick.js +89 -0
- package/dist/pipelines.js +262 -0
- package/dist/plan.js +641 -0
- package/dist/plandoc.js +211 -0
- package/dist/plugin.js +4736 -0
- package/dist/policy.js +58 -0
- package/dist/pools.js +1 -0
- package/dist/preview.js +274 -0
- package/dist/profile.js +165 -0
- package/dist/progress.js +1 -0
- package/dist/promote.js +313 -0
- package/dist/redact.js +1 -0
- package/dist/registry.js +179 -0
- package/dist/riskscore.js +126 -0
- package/dist/roles.js +1 -0
- package/dist/routing.js +1 -0
- package/dist/runner.js +1892 -0
- package/{src/sandbox.ts → dist/sandbox.js} +1 -2
- package/dist/sandboxgate.js +90 -0
- package/dist/scheduler.js +170 -0
- package/dist/services-main.js +131 -0
- package/dist/services.js +265 -0
- package/dist/session.js +241 -0
- package/dist/setup.js +423 -0
- package/dist/shell/decide.js +1 -0
- package/dist/shell/index.js +2 -0
- package/dist/shell/sandbox.js +1 -0
- package/dist/shell/wire.js +1 -0
- package/dist/skills.js +103 -0
- package/dist/state.js +1 -0
- package/dist/store.js +1 -0
- package/dist/switcher.js +1 -0
- package/dist/synth.js +418 -0
- package/dist/tools.js +477 -0
- package/dist/trials.js +223 -0
- package/dist/usage.js +1 -0
- package/dist/wait.js +167 -0
- package/dist/watchdog.js +476 -0
- package/dist/web-guidance.js +43 -0
- package/dist/which.js +20 -0
- package/dist/worktree/git.js +545 -0
- package/dist/writeguard.js +311 -0
- package/package.json +36 -17
- package/skills/deck/SKILL.md +98 -0
- package/skills/design/SKILL.md +150 -0
- package/skills/image/SKILL.md +66 -0
- package/skills/orchestrator/SKILL.md +107 -0
- package/skills/video/SKILL.md +97 -0
- package/skills/web/SKILL.md +60 -0
- package/src/agenda.ts +0 -56
- package/src/agents.ts +0 -74
- package/src/ask.ts +0 -243
- package/src/backends.ts +0 -144
- package/src/backlog.ts +0 -436
- package/src/bench.ts +0 -163
- package/src/board.ts +0 -1
- package/src/brief.ts +0 -151
- package/src/browser.ts +0 -624
- package/src/budget.ts +0 -73
- package/src/catalog.ts +0 -214
- package/src/checkchange.ts +0 -180
- package/src/checkpoint.ts +0 -99
- package/src/cli.ts +0 -117
- package/src/compact.ts +0 -162
- package/src/config.ts +0 -340
- package/src/context/capture.ts +0 -1
- package/src/context/digest.ts +0 -1
- package/src/context/embed.ts +0 -1
- package/src/context/index.ts +0 -2
- package/src/context/ledger.ts +0 -1
- package/src/context/manager.ts +0 -1
- package/src/context/memory.ts +0 -1
- package/src/context/policy.ts +0 -1
- package/src/context/prices.ts +0 -1
- package/src/context/recall.ts +0 -1
- package/src/context/rules.ts +0 -1
- package/src/context/search.ts +0 -1
- package/src/context/status.ts +0 -1
- package/src/context/store.ts +0 -1
- package/src/context/summary.ts +0 -1
- package/src/context/tokens.ts +0 -1
- package/src/context/tools.ts +0 -1
- package/src/continuation.ts +0 -122
- package/src/contract.ts +0 -342
- package/src/contractround.ts +0 -225
- package/src/control.ts +0 -1
- package/src/dataset.ts +0 -133
- package/src/decide.ts +0 -690
- package/src/eval.ts +0 -66
- package/src/evolver.ts +0 -369
- package/src/followups.ts +0 -50
- package/src/forms.ts +0 -191
- package/src/gate.ts +0 -90
- package/src/grants.ts +0 -239
- package/src/graph.ts +0 -74
- package/src/guard/index.ts +0 -1
- package/src/guard/wire.ts +0 -1
- package/src/helpers.ts +0 -105
- package/src/hooks.ts +0 -199
- package/src/jev.ts +0 -141
- package/src/journal.ts +0 -1
- package/src/look.ts +0 -390
- package/src/memory.ts +0 -1
- package/src/native.ts +0 -2455
- package/src/nogit.ts +0 -112
- package/src/outcomes.ts +0 -210
- package/src/pick.ts +0 -112
- package/src/pipelines.ts +0 -252
- package/src/plan.ts +0 -449
- package/src/plugin.ts +0 -3187
- package/src/policy.ts +0 -84
- package/src/pools.ts +0 -1
- package/src/profile.ts +0 -186
- package/src/progress.ts +0 -1
- package/src/redact.ts +0 -1
- package/src/registry.ts +0 -207
- package/src/roles.ts +0 -1
- package/src/routing.ts +0 -1
- package/src/runner.ts +0 -1506
- package/src/scheduler.ts +0 -200
- package/src/session.ts +0 -269
- package/src/shell/decide.ts +0 -1
- package/src/shell/index.ts +0 -2
- package/src/shell/sandbox.ts +0 -1
- package/src/shell/wire.ts +0 -1
- package/src/skills.ts +0 -111
- package/src/state.ts +0 -1
- package/src/store.ts +0 -1
- package/src/switcher.ts +0 -1
- package/src/tools.ts +0 -480
- package/src/trials.ts +0 -247
- package/src/usage.ts +0 -1
- package/src/wait.ts +0 -180
- package/src/watchdog.ts +0 -498
- package/src/worktree/git.ts +0 -378
package/dist/promote.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// What a plan worker hands back, checked path by path before a single byte is copied. Plan mode is a hard boundary
|
|
2
|
+
// (docs/roadmap/omacode-roadmap.md §5, A1): a planner drafts in a throwaway worktree under the state directory, and the
|
|
3
|
+
// only thing that leaves it is what its report names on an `Artifacts:` line. That line is worker-written text, so it is
|
|
4
|
+
// treated as an attack until it proves otherwise: no symlinks anywhere on the way (a link is how a harmless-looking name
|
|
5
|
+
// turns into /etc/shadow), no FIFOs, sockets or devices, no absolute paths, no ".." that climbs out, no path that
|
|
6
|
+
// resolves outside the worktree, a size cap and a secret scan. The copy itself opens each source with O_NOFOLLOW, so a
|
|
7
|
+
// link created between the check and the copy is not followed either.
|
|
8
|
+
//
|
|
9
|
+
// A rejection is per path and does not stop the others, but `ok` is false for the whole report: the caller must not mark
|
|
10
|
+
// the task passed while a path it named was refused.
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { closeSync, constants, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
14
|
+
import { MAX_FILE_BYTES, SECRET } from "./nogit.js";
|
|
15
|
+
import { findSecrets } from "./redact.js";
|
|
16
|
+
import { planArea } from "./worktree/git.js";
|
|
17
|
+
// The whole report may not carry more than this, however many files it names: a plan is a draft, not a data transfer.
|
|
18
|
+
export const MAX_PROMOTED_BYTES = 64 * 1024 * 1024;
|
|
19
|
+
// How much of a file is scanned for secrets. A secret past the first MiB is not found, which is stated, not fixed here:
|
|
20
|
+
// a bigger window only moves the line, and no plan artifact is a secret store.
|
|
21
|
+
const SCAN_BYTES = 1024 * 1024;
|
|
22
|
+
const why = (err) => (err instanceof Error ? err.message : String(err));
|
|
23
|
+
// A path as the report wrote it: the surrounding quotes or backticks are formatting, not part of the name.
|
|
24
|
+
function strip(raw) {
|
|
25
|
+
const quoted = /^([`"'])([\s\S]*)\1$/.exec(raw.trim());
|
|
26
|
+
return (quoted ? quoted[2] : raw).trim();
|
|
27
|
+
}
|
|
28
|
+
// The kind of entry that is never an artifact. Symlinks are reported on their own, because they are the interesting one.
|
|
29
|
+
function special(st) {
|
|
30
|
+
if (st.isFIFO())
|
|
31
|
+
return "fifo";
|
|
32
|
+
if (st.isSocket())
|
|
33
|
+
return "socket";
|
|
34
|
+
if (st.isBlockDevice() || st.isCharacterDevice())
|
|
35
|
+
return "device";
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
// Equals or lies under a resolved root, without a sibling whose name merely starts with the same characters.
|
|
39
|
+
function inside(path, root) {
|
|
40
|
+
return path === root || path.startsWith(root.endsWith(sep) ? root : root + sep);
|
|
41
|
+
}
|
|
42
|
+
// Walks to the target one segment at a time from the worktree root, so a link in the middle of the path is seen as what
|
|
43
|
+
// it is rather than silently followed by the next lstat.
|
|
44
|
+
function walkTo(root, segments) {
|
|
45
|
+
let cur = root;
|
|
46
|
+
for (const segment of segments) {
|
|
47
|
+
cur = join(cur, segment);
|
|
48
|
+
let st;
|
|
49
|
+
try {
|
|
50
|
+
st = lstatSync(cur);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
return { reason: `cannot be read: ${why(err)}` };
|
|
54
|
+
}
|
|
55
|
+
if (st.isSymbolicLink())
|
|
56
|
+
return { reason: "a symlink in the artifact path" };
|
|
57
|
+
}
|
|
58
|
+
return { target: cur };
|
|
59
|
+
}
|
|
60
|
+
// The regular files of an artifact, or why it may not be one: a directory is walked whole, and one symlink, FIFO,
|
|
61
|
+
// socket or device inside it refuses the directory rather than being skipped.
|
|
62
|
+
function collect(target, rel) {
|
|
63
|
+
let st;
|
|
64
|
+
try {
|
|
65
|
+
st = lstatSync(target);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
return { reason: `cannot be read: ${why(err)}` };
|
|
69
|
+
}
|
|
70
|
+
if (st.isSymbolicLink())
|
|
71
|
+
return { reason: "a symlink in the artifact path" };
|
|
72
|
+
const kind = special(st);
|
|
73
|
+
if (kind)
|
|
74
|
+
return { reason: `the artifact is a ${kind}, not a regular file or directory` };
|
|
75
|
+
if (st.isFile())
|
|
76
|
+
return { files: [{ rel, abs: target, bytes: st.size, dev: st.dev, ino: st.ino }] };
|
|
77
|
+
if (!st.isDirectory())
|
|
78
|
+
return { reason: "the artifact is not a regular file or directory" };
|
|
79
|
+
const files = [];
|
|
80
|
+
const walk = (abs, relDir) => {
|
|
81
|
+
let names;
|
|
82
|
+
try {
|
|
83
|
+
names = readdirSync(abs).sort();
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
return `cannot be read: ${why(err)}`;
|
|
87
|
+
}
|
|
88
|
+
for (const name of names) {
|
|
89
|
+
const child = join(abs, name);
|
|
90
|
+
const childRel = `${relDir}/${name}`;
|
|
91
|
+
let cst;
|
|
92
|
+
try {
|
|
93
|
+
cst = lstatSync(child);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
return `cannot be read: ${why(err)}`;
|
|
97
|
+
}
|
|
98
|
+
if (cst.isSymbolicLink())
|
|
99
|
+
return `nested symlink in ${childRel}`;
|
|
100
|
+
const childKind = special(cst);
|
|
101
|
+
if (childKind)
|
|
102
|
+
return `nested ${childKind} in ${childRel}`;
|
|
103
|
+
if (cst.isDirectory()) {
|
|
104
|
+
const nested = walk(child, childRel);
|
|
105
|
+
if (nested)
|
|
106
|
+
return nested;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (!cst.isFile())
|
|
110
|
+
return `nested special file in ${childRel}`;
|
|
111
|
+
files.push({ rel: childRel, abs: child, bytes: cst.size, dev: cst.dev, ino: cst.ino });
|
|
112
|
+
}
|
|
113
|
+
return undefined;
|
|
114
|
+
};
|
|
115
|
+
const nested = walk(target, rel);
|
|
116
|
+
return nested ? { reason: nested } : { files };
|
|
117
|
+
}
|
|
118
|
+
// One file: opened without following a link, then checked against the inode `lstat` saw. O_NOFOLLOW covers the last
|
|
119
|
+
// component only, so a parent directory swapped for a symlink between the check and the copy would otherwise be
|
|
120
|
+
// followed; the descriptor's own dev/ino is what proves the bytes come from the file that was inspected.
|
|
121
|
+
// The same buffer is scanned for a secret, hashed and written 0600 under a 0700 parent, so the record describes exactly
|
|
122
|
+
// the bytes that landed. Exported for its own test: the race it closes cannot be reproduced from outside.
|
|
123
|
+
export function copyOne(src, dest, expect) {
|
|
124
|
+
let fd;
|
|
125
|
+
try {
|
|
126
|
+
fd = openSync(src, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
127
|
+
const opened = fstatSync(fd);
|
|
128
|
+
if (opened.dev !== expect.dev || opened.ino !== expect.ino)
|
|
129
|
+
return { reason: "changed while it was checked" };
|
|
130
|
+
const bytes = readFileSync(fd);
|
|
131
|
+
// The lstat before the copy said its size; a file that grew since is refused on the bytes actually read.
|
|
132
|
+
if (bytes.length > MAX_FILE_BYTES)
|
|
133
|
+
return { reason: "over the size cap" };
|
|
134
|
+
const sample = bytes.subarray(0, SCAN_BYTES);
|
|
135
|
+
// Binary content is not scanned: a NUL in the sample means the file is not text, and a token format cannot be read
|
|
136
|
+
// out of one anyway. A secret in a binary artifact is a limitation of this check, not a promise.
|
|
137
|
+
if (!sample.includes(0)) {
|
|
138
|
+
const found = findSecrets(sample.toString("utf8")).filter((f) => f.kind === "secret");
|
|
139
|
+
if (found.length)
|
|
140
|
+
return { reason: "contains a secret" };
|
|
141
|
+
}
|
|
142
|
+
mkdirSync(dirname(dest), { recursive: true, mode: 0o700 });
|
|
143
|
+
writeFileSync(dest, bytes, { mode: 0o600 });
|
|
144
|
+
return { bytes: bytes.length, sha256: createHash("sha256").update(bytes).digest("hex") };
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
return { reason: `cannot be copied: ${why(err)}` };
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
if (fd !== undefined)
|
|
151
|
+
closeSync(fd);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
export function promoteArtifacts(input) {
|
|
155
|
+
const promoted = [];
|
|
156
|
+
const rejected = [];
|
|
157
|
+
let root;
|
|
158
|
+
try {
|
|
159
|
+
root = realpathSync(input.root);
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
// No worktree, nothing to promote from: every path is refused rather than silently dropped.
|
|
163
|
+
for (const path of input.paths)
|
|
164
|
+
rejected.push({ path, reason: `the plan worktree cannot be read: ${why(err)}` });
|
|
165
|
+
return { ok: false, promoted, rejected };
|
|
166
|
+
}
|
|
167
|
+
const base = resolve(input.root);
|
|
168
|
+
// Where the project sits inside the worktree: the walk starts at the root, the paths resolve against the project.
|
|
169
|
+
const offset = relative(base, resolve(input.dir)).split(sep).join("/");
|
|
170
|
+
const offsetSegments = offset === "" || offset === "." ? [] : offset.split("/").filter(Boolean);
|
|
171
|
+
const outsideDir = offset === ".." || offset.startsWith("../") || isAbsolute(offset);
|
|
172
|
+
const destRoot = join(planArea(input.project, input.jobId), "artifacts", input.taskId);
|
|
173
|
+
const seen = new Set();
|
|
174
|
+
for (const raw of input.paths) {
|
|
175
|
+
// (a) and (b): formatting off, then a posix path. A benign internal "a/../b" normalises to "b" and is accepted;
|
|
176
|
+
// only a ".." that is still there after normalisation -- which can only be a leading one -- climbs out.
|
|
177
|
+
const given = strip(raw);
|
|
178
|
+
const norm = posix.normalize(given);
|
|
179
|
+
if (isAbsolute(given) || isAbsolute(norm) || given.startsWith("~")) {
|
|
180
|
+
rejected.push({ path: raw, reason: "an absolute path" });
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (norm === ".." || norm.startsWith("../") || norm.split("/").includes("..")) {
|
|
184
|
+
rejected.push({ path: raw, reason: "escapes the worktree with `..`" });
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
// (c) a name for a file, not the directory itself and not a directory by its trailing slash.
|
|
188
|
+
if (given === "" || norm === "." || given.endsWith("/")) {
|
|
189
|
+
rejected.push({ path: raw, reason: "names no file" });
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (outsideDir) {
|
|
193
|
+
rejected.push({ path: raw, reason: "resolves outside the plan worktree" });
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
// (d) every segment from the root, the project's own offset included.
|
|
197
|
+
const reached = walkTo(base, [...offsetSegments, ...norm.split("/")]);
|
|
198
|
+
if ("reason" in reached) {
|
|
199
|
+
rejected.push({ path: raw, reason: reached.reason });
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const target = reached.target;
|
|
203
|
+
// (e) the target itself, and a directory in full.
|
|
204
|
+
const found = collect(target, norm);
|
|
205
|
+
if ("reason" in found) {
|
|
206
|
+
rejected.push({ path: raw, reason: found.reason });
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
const files = found.files;
|
|
210
|
+
// (f) the filesystem's own answer, resolving what the walk could not see (a link in a parent the walk did see would
|
|
211
|
+
// already have stopped it; this is the check that no future refactor can skip).
|
|
212
|
+
try {
|
|
213
|
+
if (!inside(realpathSync(target), root)) {
|
|
214
|
+
rejected.push({ path: raw, reason: "resolves outside the plan worktree" });
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
rejected.push({ path: raw, reason: `cannot be read: ${why(err)}` });
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
// (g) the size caps, one file and the artifact whole.
|
|
223
|
+
const total = files.reduce((sum, f) => sum + f.bytes, 0);
|
|
224
|
+
if (files.some((f) => f.bytes > MAX_FILE_BYTES) || total > MAX_PROMOTED_BYTES) {
|
|
225
|
+
rejected.push({ path: raw, reason: "over the size cap" });
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
// (h) a name that says it holds a secret is refused whatever it holds.
|
|
229
|
+
const named = files.find((f) => SECRET.some((re) => re.test(basename(f.abs))));
|
|
230
|
+
if (named) {
|
|
231
|
+
rejected.push({ path: raw, reason: `a secret file name: ${named.rel}` });
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (seen.has(norm))
|
|
235
|
+
continue; // the same path named twice is one artifact
|
|
236
|
+
seen.add(norm);
|
|
237
|
+
// (i) copy; the content scan happens on the bytes that are copied. A rejection part-way through a directory removes
|
|
238
|
+
// what that artifact already wrote, so a refused artifact leaves nothing of itself behind.
|
|
239
|
+
const written = [];
|
|
240
|
+
let failure;
|
|
241
|
+
for (const file of files) {
|
|
242
|
+
const copied = copyOne(file.abs, join(destRoot, ...file.rel.split("/")), file);
|
|
243
|
+
if ("reason" in copied) {
|
|
244
|
+
failure = file.rel === norm ? copied.reason : `${copied.reason} in ${file.rel}`;
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
written.push({ path: file.rel, bytes: copied.bytes, sha256: copied.sha256 });
|
|
248
|
+
}
|
|
249
|
+
if (failure) {
|
|
250
|
+
try {
|
|
251
|
+
rmSync(join(destRoot, ...norm.split("/")), { recursive: true, force: true });
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
// a leftover under the state directory is not an escape, so failing to clean it is not fatal
|
|
255
|
+
}
|
|
256
|
+
rejected.push({ path: raw, reason: failure });
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
promoted.push(...written);
|
|
260
|
+
}
|
|
261
|
+
return { ok: rejected.length === 0, promoted, rejected };
|
|
262
|
+
}
|
|
263
|
+
// The durable half of a plan task: the worktree it drafted in is thrown away with its branch, so the patch, the
|
|
264
|
+
// worker's findings and a manifest of what was handed back are what is left of it. The manifest is the index of the
|
|
265
|
+
// record: it names every file and repeats the hash of each promoted artifact, so a reader does not have to trust the
|
|
266
|
+
// copies' names. Like the copies, everything is written 0600 under a 0700 directory.
|
|
267
|
+
export function writePromotionRecord(input) {
|
|
268
|
+
const area = planArea(input.project, input.jobId);
|
|
269
|
+
const at = new Date().toISOString();
|
|
270
|
+
const manifestPath = join(area, "artifacts", input.taskId, "manifest.json");
|
|
271
|
+
const findingsPath = join(area, "findings", `${input.taskId}.md`);
|
|
272
|
+
const patchPath = input.git ? join(area, "patches", `${input.taskId}.patch`) : undefined;
|
|
273
|
+
const body = `${input.report}${input.report.endsWith("\n") ? "" : "\n"}`;
|
|
274
|
+
const findings = `# ${input.taskId} — ${input.title}\n\nRecorded ${at} for plan job ${input.jobId}.\n\n## Findings\n\n${body}`;
|
|
275
|
+
mkdirSync(dirname(findingsPath), { recursive: true, mode: 0o700 });
|
|
276
|
+
writeFileSync(findingsPath, findings, { mode: 0o600 });
|
|
277
|
+
if (patchPath && input.git) {
|
|
278
|
+
mkdirSync(dirname(patchPath), { recursive: true, mode: 0o700 });
|
|
279
|
+
// Written even when empty: the record says the task changed nothing, which is a fact about the plan.
|
|
280
|
+
writeFileSync(patchPath, input.git.patch, { mode: 0o600 });
|
|
281
|
+
}
|
|
282
|
+
const manifest = {
|
|
283
|
+
version: 1,
|
|
284
|
+
job: input.jobId,
|
|
285
|
+
task: input.taskId,
|
|
286
|
+
title: input.title,
|
|
287
|
+
at,
|
|
288
|
+
checks: [...input.checks],
|
|
289
|
+
git: input.git ? { base: input.git.base, head: input.git.head } : null,
|
|
290
|
+
patch: input.git ? `patches/${input.taskId}.patch` : null,
|
|
291
|
+
...(input.git ? { patchBytes: Buffer.byteLength(input.git.patch) } : {}),
|
|
292
|
+
findings: `findings/${input.taskId}.md`,
|
|
293
|
+
artifacts: input.promotion.promoted.map((a) => ({ path: a.path, bytes: a.bytes, sha256: a.sha256 })),
|
|
294
|
+
rejected: input.promotion.rejected.map((r) => ({ path: r.path, reason: r.reason })),
|
|
295
|
+
...(input.git ? {} : { note: "no git in this project: there is no patch" }),
|
|
296
|
+
};
|
|
297
|
+
mkdirSync(dirname(manifestPath), { recursive: true, mode: 0o700 });
|
|
298
|
+
writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, { mode: 0o600 });
|
|
299
|
+
return { manifest: manifestPath, findings: findingsPath, ...(patchPath ? { patch: patchPath } : {}) };
|
|
300
|
+
}
|
|
301
|
+
// The `Artifacts:` lines of a report, comma-separated, with the backticks a model likes around paths. Several lines add
|
|
302
|
+
// up; "none" is the report saying it has none, not a file of that name.
|
|
303
|
+
export function parseArtifacts(text) {
|
|
304
|
+
const paths = [];
|
|
305
|
+
for (const line of text.matchAll(/^[ \t]*Artifacts[ \t]*:[ \t]*(.*)$/gim)) {
|
|
306
|
+
for (const part of line[1].split(",")) {
|
|
307
|
+
const path = strip(part.replace(/^[ \t`]+|[ \t`]+$/g, ""));
|
|
308
|
+
if (path && !/^(?:none|\(none\)|-+)$/i.test(path))
|
|
309
|
+
paths.push(path);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return paths;
|
|
313
|
+
}
|
package/dist/redact.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@omacode/guard/redact";
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// The model registry and the router over it (design.md §19): which models exist, where they run, what they can
|
|
2
|
+
// do, and which one a task gets. Local when capable; cloud for hard tasks, long contexts, or busy hardware.
|
|
3
|
+
import { chain } from "./routing.js";
|
|
4
|
+
import { MODELS, family, roleById } from "./roles.js";
|
|
5
|
+
const TIER_RANK = { quick: 0, agent: 1, deep: 2 };
|
|
6
|
+
// Cloud models (OpenCode Go) and the local fleet of ~/Models/bench/ROSTER.md (2026-10-04). Local models run with
|
|
7
|
+
// a 16k context. The NPU is reserved for Whisper plus one small quick model (Phase A).
|
|
8
|
+
export const DEFAULT_REGISTRY = [
|
|
9
|
+
// Vision and context as the host lists them (GET /api/model), budgets from opencode.ai/docs/go (2026-10-19).
|
|
10
|
+
{ id: MODELS.glm, location: "cloud", device: "cloud", censored: true, family: "glm", tier: "agent", vision: true, ctx: 1000000, budget: 60 },
|
|
11
|
+
{ id: MODELS.deepseek, location: "cloud", device: "cloud", censored: true, family: "deepseek", tier: "agent", vision: true, ctx: 1000000, budget: 80 },
|
|
12
|
+
{ id: MODELS.deepseekPro, location: "cloud", device: "cloud", censored: true, family: "deepseek", tier: "agent", vision: true, ctx: 1000000, budget: 60 },
|
|
13
|
+
{ id: MODELS.mimo, location: "cloud", device: "cloud", censored: true, family: "mimo", tier: "agent", vision: true, ctx: 1048576, budget: 15 },
|
|
14
|
+
{ id: MODELS.mimoFlash, location: "cloud", device: "cloud", censored: true, family: "mimo", tier: "agent", vision: true, ctx: 1048576, budget: 60 },
|
|
15
|
+
{ id: MODELS.qwen, location: "cloud", device: "cloud", censored: true, family: "qwen", tier: "agent", vision: true, ctx: 1000000, budget: 30 },
|
|
16
|
+
{ id: MODELS.qwenPlus, location: "cloud", device: "cloud", censored: true, family: "qwen", tier: "agent", vision: true, ctx: 1000000, budget: 30 },
|
|
17
|
+
{ id: MODELS.haiku, location: "cloud", device: "cloud", censored: true, family: "claude", tier: "agent", vision: true, ctx: 1000000, budget: 15 },
|
|
18
|
+
{ id: MODELS.minimax, location: "cloud", device: "cloud", censored: true, family: "minimax", tier: "agent", vision: true, ctx: 1000000 },
|
|
19
|
+
// Go Plus and the subscription line-up (2026-12): known so the planner and the runner can route a pool entry the
|
|
20
|
+
// built-ins do not name. Whether a provider is authenticated is still the host catalog's answer (plugin.ts
|
|
21
|
+
// availability()), so a model here that the host does not have is skipped, not called.
|
|
22
|
+
{ id: "opencode-go/hy4-flash", location: "cloud", device: "cloud", censored: true, family: "hy4", tier: "quick", vision: true, ctx: 1000000, budget: 60 },
|
|
23
|
+
{ id: "opencode-go/iceman-contributor-free", location: "cloud", device: "cloud", censored: true, family: "iceman", tier: "quick", vision: true, ctx: 262144 },
|
|
24
|
+
{ id: "google/gemini-4-carbon", location: "cloud", device: "cloud", censored: true, family: "gemini", tier: "deep", vision: true, ctx: 1048576 },
|
|
25
|
+
{ id: "google/gemini-4-neon", location: "cloud", device: "cloud", censored: true, family: "gemini", tier: "agent", vision: true, ctx: 1048576 },
|
|
26
|
+
{ id: "google/gemini-4-hydrogen", location: "cloud", device: "cloud", censored: true, family: "gemini", tier: "quick", vision: true, ctx: 1048576 },
|
|
27
|
+
{ id: "kimi-code-plan-global/k3.1-pro", location: "cloud", device: "cloud", censored: true, family: "k3", tier: "deep", vision: true, ctx: 1048576 },
|
|
28
|
+
{ id: "kimi-code-plan-global/k3.1-pro-highspeed", location: "cloud", device: "cloud", censored: true, family: "k3", tier: "agent", vision: true, ctx: 1048576 },
|
|
29
|
+
{ id: "llamaswap/oss-u", location: "local", device: "cuda", censored: false, family: "gpt-oss", tier: "deep", vision: false, ctx: 16384, tokPerSec: 53, swapGroup: "cuda0" },
|
|
30
|
+
{ id: "llamaswap/ornith-u", location: "local", device: "cuda", censored: false, family: "ornith", tier: "agent", vision: true, ctx: 16384, tokPerSec: 40, swapGroup: "cuda0" },
|
|
31
|
+
{ id: "llamaswap/ornith-u-s", location: "local", device: "cuda", censored: false, family: "ornith", tier: "agent", vision: true, ctx: 16384, tokPerSec: 45, swapGroup: "cuda0" },
|
|
32
|
+
{ id: "llamaswap/ornith", location: "local", device: "cuda", censored: true, family: "ornith", tier: "agent", vision: true, ctx: 16384, tokPerSec: 35, swapGroup: "cuda0" },
|
|
33
|
+
{ id: "llamaswap/mimo", location: "local", device: "cuda", censored: true, family: "mimo", tier: "agent", vision: true, ctx: 16384, tokPerSec: 39, swapGroup: "cuda0" },
|
|
34
|
+
{ id: "llamaigpu/qwen3.8-u", location: "local", device: "igpu", censored: false, family: "qwen", tier: "agent", vision: true, ctx: 16384, tokPerSec: 6.5, resident: true },
|
|
35
|
+
{ id: "flmnpu/qwen3:4b", location: "local", device: "npu", censored: true, family: "qwen", tier: "quick", vision: false, ctx: 16384, tokPerSec: 19, note: "quick tier only; skipped a required tool in Phase A" },
|
|
36
|
+
{ id: "flmnpu/qwen3vl-it:4b", location: "local", device: "npu", censored: true, family: "qwen", tier: "quick", vision: true, ctx: 16384, tokPerSec: 19 },
|
|
37
|
+
{ id: "flmnpu/gpt-oss:20b", location: "local", device: "npu", censored: true, family: "gpt-oss", tier: "deep", vision: false, ctx: 16384, disabled: true, note: "the NPU stays free for Whisper" },
|
|
38
|
+
];
|
|
39
|
+
// Entries from the config replace or add models by id.
|
|
40
|
+
export function mergeRegistry(base, extra) {
|
|
41
|
+
const out = new Map(base.map((m) => [m.id, m]));
|
|
42
|
+
for (const e of extra) {
|
|
43
|
+
if (!e.id)
|
|
44
|
+
continue;
|
|
45
|
+
const known = out.get(e.id);
|
|
46
|
+
out.set(e.id, { ...(known ?? defaultsFor(e.id)), ...e });
|
|
47
|
+
}
|
|
48
|
+
return [...out.values()];
|
|
49
|
+
}
|
|
50
|
+
// A model the registry does not know: cautious defaults (local, censored, agent tier, small context).
|
|
51
|
+
function defaultsFor(id) {
|
|
52
|
+
const cloud = id.startsWith("opencode-go/") || id.startsWith("opencode/");
|
|
53
|
+
return { id, location: cloud ? "cloud" : "local", device: cloud ? "cloud" : "cuda", censored: true, family: family(id), tier: "agent", vision: false, ctx: cloud ? 128000 : 16384 };
|
|
54
|
+
}
|
|
55
|
+
export const DEFAULT_ENDPOINTS = {
|
|
56
|
+
llamaswap: "http://127.0.0.1:8000",
|
|
57
|
+
llamaigpu: "http://127.0.0.1:8092",
|
|
58
|
+
flmnpu: "http://127.0.0.1:52625",
|
|
59
|
+
};
|
|
60
|
+
async function getJson(fetcher, url, timeoutMs) {
|
|
61
|
+
const res = await fetcher(url, { signal: AbortSignal.timeout(timeoutMs) });
|
|
62
|
+
if (!res.ok)
|
|
63
|
+
throw new Error(`${url}: HTTP ${res.status}`);
|
|
64
|
+
return res.json();
|
|
65
|
+
}
|
|
66
|
+
// Reads what the local servers report (Phase A, A8): llama-swap's loaded status, llama-vulkan's slots, FLM's
|
|
67
|
+
// model list. Cloud models count as reachable; their limits are the budget's job.
|
|
68
|
+
export async function probe(registry, endpoints = DEFAULT_ENDPOINTS, fetcher = fetch, timeoutMs = 1500) {
|
|
69
|
+
const out = new Map();
|
|
70
|
+
const name = (id) => id.slice(id.indexOf("/") + 1);
|
|
71
|
+
const [swap, igpuHealth, igpuSlots, npu] = await Promise.all([
|
|
72
|
+
endpoints.llamaswap ? getJson(fetcher, `${endpoints.llamaswap}/v1/models`, timeoutMs).catch(() => undefined) : undefined,
|
|
73
|
+
endpoints.llamaigpu ? getJson(fetcher, `${endpoints.llamaigpu}/health`, timeoutMs).catch(() => undefined) : undefined,
|
|
74
|
+
endpoints.llamaigpu ? getJson(fetcher, `${endpoints.llamaigpu}/slots`, timeoutMs).catch(() => undefined) : undefined,
|
|
75
|
+
endpoints.flmnpu ? getJson(fetcher, `${endpoints.flmnpu}/v1/models`, timeoutMs).catch(() => undefined) : undefined,
|
|
76
|
+
]);
|
|
77
|
+
const swapModels = new Map((swap?.data ?? []).map((m) => [m.id, m.status?.value]));
|
|
78
|
+
const slots = Array.isArray(igpuSlots) ? igpuSlots : [];
|
|
79
|
+
const npuIds = new Set((npu?.data ?? []).map((m) => m.id));
|
|
80
|
+
for (const m of registry) {
|
|
81
|
+
if (m.location === "cloud")
|
|
82
|
+
out.set(m.id, { reachable: true });
|
|
83
|
+
else if (m.id.startsWith("llamaswap/")) {
|
|
84
|
+
const status = swapModels.get(name(m.id));
|
|
85
|
+
out.set(m.id, { reachable: status !== undefined, loaded: status === "loaded" });
|
|
86
|
+
}
|
|
87
|
+
else if (m.id.startsWith("llamaigpu/")) {
|
|
88
|
+
const ok = igpuHealth?.status === "ok";
|
|
89
|
+
out.set(m.id, { reachable: ok, loaded: ok, busy: slots.length > 0 && slots.every((s) => s.is_processing) });
|
|
90
|
+
}
|
|
91
|
+
else if (m.id.startsWith("flmnpu/")) {
|
|
92
|
+
out.set(m.id, { reachable: npuIds.has(name(m.id)) });
|
|
93
|
+
}
|
|
94
|
+
else
|
|
95
|
+
out.set(m.id, { reachable: false });
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
export function tierFor(difficulty = 0.5) {
|
|
100
|
+
return difficulty <= 0.33 ? "quick" : difficulty <= 0.66 ? "agent" : "deep";
|
|
101
|
+
}
|
|
102
|
+
// Roles that do agentic work never go to the NPU's quick model.
|
|
103
|
+
const QUICK_ROLES = new Set(["explore", "write"]);
|
|
104
|
+
export function route(registry, needs, availability) {
|
|
105
|
+
const tier = tierFor(needs.difficulty);
|
|
106
|
+
const avail = (id) => availability?.get(id) ?? { reachable: true };
|
|
107
|
+
const local = needs.sensitive || needs.uncensored;
|
|
108
|
+
const allow = needs.allow && needs.allow.length > 0 ? new Set(needs.allow) : undefined;
|
|
109
|
+
const fits = (m) => !m.disabled &&
|
|
110
|
+
avail(m.id).reachable &&
|
|
111
|
+
(!allow || allow.has(m.id)) &&
|
|
112
|
+
(!needs.vision || m.vision) &&
|
|
113
|
+
m.ctx >= (needs.ctx ?? 0) &&
|
|
114
|
+
(!local || m.location === "local") &&
|
|
115
|
+
(!needs.uncensored || !m.censored) &&
|
|
116
|
+
(m.device !== "npu" || (tier === "quick" && QUICK_ROLES.has(needs.role)));
|
|
117
|
+
const families = needs.avoidFamilies ?? [];
|
|
118
|
+
let candidates = registry.filter((m) => fits(m) && !families.includes(m.family));
|
|
119
|
+
// A reviewer never checks its own family, but a one-family pool would otherwise have no reviewer at all: keep
|
|
120
|
+
// the pool and say the rule could not be met (the pool is authoritative; the family rule is not).
|
|
121
|
+
const familyDropped = candidates.length === 0 && families.length > 0;
|
|
122
|
+
if (familyDropped)
|
|
123
|
+
candidates = registry.filter(fits);
|
|
124
|
+
if (candidates.length === 0) {
|
|
125
|
+
const why = [needs.uncensored && "uncensored", local && "local", needs.vision && "vision", needs.ctx && `ctx ≥ ${needs.ctx}`].filter(Boolean).join(", ");
|
|
126
|
+
const pool = allow
|
|
127
|
+
? `; ${needs.role} pool: ${(needs.allow ?? []).slice(0, 4).join(", ")}${(needs.allow?.length ?? 0) > 4 ? ", …" : ""}`
|
|
128
|
+
: "";
|
|
129
|
+
return { reason: `no reachable model fits (${why || "any"}${pool})`, ranked: [] };
|
|
130
|
+
}
|
|
131
|
+
// Local when capable: a local model covers the tier, and nothing pins the cloud.
|
|
132
|
+
const localCovers = candidates.some((m) => m.location === "local" && TIER_RANK[m.tier] >= TIER_RANK[tier] && !avail(m.id).busy);
|
|
133
|
+
const wantLocal = local || (localCovers && !needs.preferCloud);
|
|
134
|
+
// The role's pool in its order (the user's picks, else the role matrix in src/roles.ts) breaks ties, so roles
|
|
135
|
+
// spread over the families instead of all landing on the alphabetically first one (live test: deepseek got every role).
|
|
136
|
+
const preferred = needs.allow ?? roleById(needs.role)?.models ?? [];
|
|
137
|
+
const rank = (id) => (preferred.includes(id) ? preferred.indexOf(id) : preferred.length);
|
|
138
|
+
const score = (m) => {
|
|
139
|
+
const a = avail(m.id);
|
|
140
|
+
return [
|
|
141
|
+
m.location === (wantLocal ? "local" : "cloud") ? 0 : 1,
|
|
142
|
+
TIER_RANK[m.tier] >= TIER_RANK[tier] ? 0 : 1, // covers the tier
|
|
143
|
+
a.busy ? 1 : 0,
|
|
144
|
+
m.resident || a.loaded || m.id === needs.loadedModel || m.location === "cloud" ? 0 : 1, // no swap
|
|
145
|
+
Math.abs(TIER_RANK[m.tier] - TIER_RANK[tier]), // no bigger than needed
|
|
146
|
+
-(m.tokPerSec ?? 0),
|
|
147
|
+
rank(m.id),
|
|
148
|
+
];
|
|
149
|
+
};
|
|
150
|
+
const ranked = [...candidates].sort((a, b) => {
|
|
151
|
+
const x = score(a), y = score(b);
|
|
152
|
+
for (let i = 0; i < x.length; i++)
|
|
153
|
+
if (x[i] !== y[i])
|
|
154
|
+
return x[i] - y[i];
|
|
155
|
+
return a.id.localeCompare(b.id);
|
|
156
|
+
});
|
|
157
|
+
const best = ranked[0];
|
|
158
|
+
const reason = [
|
|
159
|
+
best.location,
|
|
160
|
+
`${tier} task`,
|
|
161
|
+
needs.uncensored ? "uncensored" : needs.sensitive ? "sensitive" : undefined,
|
|
162
|
+
avail(best.id).loaded || best.resident ? "already loaded" : best.location === "local" ? "needs a swap" : undefined,
|
|
163
|
+
familyDropped ? "the pool has no other family" : undefined,
|
|
164
|
+
].filter(Boolean).join(", ");
|
|
165
|
+
return { model: best.id, reason, ranked: ranked.map((m) => m.id) };
|
|
166
|
+
}
|
|
167
|
+
// The local model for a sensitive task: only a model the user put in the role's pool may serve it. `outside` names
|
|
168
|
+
// the local model the old, pool-blind route would have taken, so the consent question can say why it is asking.
|
|
169
|
+
export function sensitiveLocalModel(registry, role, difficulty, routing, availability) {
|
|
170
|
+
const def = roleById(role);
|
|
171
|
+
const pool = def ? chain(def, routing) : [];
|
|
172
|
+
if (pool.length === 0)
|
|
173
|
+
return {};
|
|
174
|
+
const inside = route(registry, { role, difficulty, sensitive: true, allow: pool }, availability);
|
|
175
|
+
if (inside.model)
|
|
176
|
+
return { model: inside.model };
|
|
177
|
+
const outside = route(registry, { role, difficulty, sensitive: true }, availability);
|
|
178
|
+
return outside.model && !pool.includes(outside.model) ? { outside: outside.model } : {};
|
|
179
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// A deterministic risk score for the commands of a job (D23, A1-T14). When no sandbox can run on the machine, the user
|
|
2
|
+
// is asked before a job's commands and checks run with their own rights; the form shows this score, its findings and the
|
|
3
|
+
// check lines first (src/sandboxgate.ts). The score is arithmetic over the command text and nothing else: no model call,
|
|
4
|
+
// no state, so two runs of the same text give the same number and the number can be shown before anything starts.
|
|
5
|
+
//
|
|
6
|
+
// It is a help and not a guarantee, in the same words the form uses: it reads what a command says, not what the program
|
|
7
|
+
// it starts ends up doing. A check that is one word long can do anything, and a `make` that looks plain can delete a
|
|
8
|
+
// directory, which is why the user's answer, not this number, is what opens a job.
|
|
9
|
+
import { homedir, tmpdir } from "node:os";
|
|
10
|
+
import { join, resolve } from "node:path";
|
|
11
|
+
const has = (pattern) => (command) => pattern.test(command);
|
|
12
|
+
// The weights are D23's: one rule that is bad on its own sets the score, the others add a third of their weight, so a
|
|
13
|
+
// command that is destructive and privileged is worse than one that is only destructive. Plain builds and tests weigh
|
|
14
|
+
// nothing: they are what a check usually is, and naming them keeps the list complete instead of silent.
|
|
15
|
+
const RULES = [
|
|
16
|
+
{
|
|
17
|
+
rule: "destructive",
|
|
18
|
+
weight: 40,
|
|
19
|
+
what: "destructive: deletes or overwrites without asking (rm -rf, rm -fr, dd, mkfs, chmod -R, chown -R, a disk device, a fork bomb)",
|
|
20
|
+
test: has(/\brm\s+-[a-zA-Z]*r[a-zA-Z]*f[a-zA-Z]*\b|\brm\s+-[a-zA-Z]*f[a-zA-Z]*r[a-zA-Z]*\b|\bdd\s+(?:if|of|bs|count)=|\bmkfs(?:\.[a-z0-9]+)?\b|\bchmod\s+-[rR]\b|\bchown\s+-[rR]\b|>\s*\/dev\/[sh]d|:\s*\(\s*\)\s*\{[^}]*:\s*\|\s*:/),
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
rule: "privilege",
|
|
24
|
+
weight: 35,
|
|
25
|
+
what: "privilege: runs as root or as another user (sudo, doas, pkexec, su -)",
|
|
26
|
+
test: has(/\b(?:sudo|doas|pkexec)\b|\bsu\s+-/),
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
rule: "keys-secrets",
|
|
30
|
+
weight: 30,
|
|
31
|
+
what: "keys/secrets: reads key material, credentials or an env file (~/.ssh, ~/.aws, ~/.gnupg, ~/.config/gh, id_rsa, .env)",
|
|
32
|
+
test: has(/~\/\.(?:ssh|aws|gnupg)\b|~\/\.config\/gh\b|(?:^|[\s"'`=/])\.env\b|\bid_(?:rsa|ed25519)\b/),
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
rule: "network-execute",
|
|
36
|
+
weight: 20,
|
|
37
|
+
what: "network-execute: downloads and runs code (curl | sh, wget | sh, npx, pip install, npm i -g)",
|
|
38
|
+
test: has(/\b(?:curl|wget)\b[^|;\n]*\|\s*(?:sudo\s+)?(?:sh|bash|zsh|dash)\b|\bnpx\b|\bpip[0-9.]*\s+install\b|\bnpm\s+(?:i|install)\s+(?:-g|--global)\b/),
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
rule: "obfuscation",
|
|
42
|
+
weight: 20,
|
|
43
|
+
what: "obfuscation: hides what it runs (base64 -d, eval, node -e with child_process)",
|
|
44
|
+
test: has(/\bbase64\s+(?:-d|--decode)\b|\beval\b|\bnode\s+(?:-e|--eval)\b[^\n]*child_process/),
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
rule: "writes-outside",
|
|
48
|
+
weight: 15,
|
|
49
|
+
what: "writes outside the project: absolute paths away from it, or .. climbing out",
|
|
50
|
+
test: outside,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
rule: "git-refs",
|
|
54
|
+
weight: 15,
|
|
55
|
+
what: "git ref moves: push, reset --hard, clean -fdx, checkout -- .",
|
|
56
|
+
test: has(/\bgit\s+(?:push|reset\s+--hard|clean\s+-[a-zA-Z]*f[a-zA-Z]*d[a-zA-Z]*x|clean\s+-[a-zA-Z]*d[a-zA-Z]*f[a-zA-Z]*x|checkout\s+--\s+\.)\b/),
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
rule: "plain-build",
|
|
60
|
+
weight: 0,
|
|
61
|
+
what: "plain build or test: the usual command of a check (npm test, npm run, pytest, tsc, cargo test, go test)",
|
|
62
|
+
test: has(/(?:^|[;&|]\s*)(?:npm\s+(?:test|run|t)\b|pytest\b|cargo\s+test\b|go\s+test\b)|\btsc\b/),
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
// A token that names a path outside the project: an absolute path (or one under ~) that is not the project, its
|
|
66
|
+
// subdirectories or the temp directory, or a token that climbs with "..". The project is the process's directory: the
|
|
67
|
+
// score takes no project argument on purpose (a pure function of the command text and where the plugin runs), and a
|
|
68
|
+
// check that names another checkout is exactly what this rule is about.
|
|
69
|
+
function outside(command) {
|
|
70
|
+
const root = resolve(process.cwd());
|
|
71
|
+
const temp = resolve(tmpdir());
|
|
72
|
+
for (const raw of command.split(/\s+/)) {
|
|
73
|
+
const token = raw.replace(/^["'`([{,]+/, "").replace(/["'`)\]}.,;:]+$/, "");
|
|
74
|
+
if (!token)
|
|
75
|
+
continue;
|
|
76
|
+
if (/(^|\/)\.\.(\/|$)/.test(token))
|
|
77
|
+
return true;
|
|
78
|
+
if (token.includes("://"))
|
|
79
|
+
continue; // a URL is network-execute's business, not a write here
|
|
80
|
+
const path = token.startsWith("~/") ? join(homedir(), token.slice(2)) : token;
|
|
81
|
+
if (!path.startsWith("/"))
|
|
82
|
+
continue;
|
|
83
|
+
const abs = resolve(path);
|
|
84
|
+
if (abs === root || abs.startsWith(`${root}/`))
|
|
85
|
+
continue;
|
|
86
|
+
if (abs === temp || abs.startsWith(`${temp}/`))
|
|
87
|
+
continue;
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
// The band follows the worst rule before the number: a destructive, privileged or key-reading command is high whatever
|
|
93
|
+
// the arithmetic says, because the score is there to rank a list, not to reassure. The threshold only catches
|
|
94
|
+
// combinations of lesser rules (a build plus a network call, say).
|
|
95
|
+
function bandOf(score, findings) {
|
|
96
|
+
const worst = findings.reduce((w, f) => Math.max(w, f.weight), 0);
|
|
97
|
+
if (worst >= 30)
|
|
98
|
+
return "high";
|
|
99
|
+
if (worst >= 15 || score >= 20)
|
|
100
|
+
return "medium";
|
|
101
|
+
return "low";
|
|
102
|
+
}
|
|
103
|
+
// No commands (or none with text): nothing to weigh, so nothing is claimed about it.
|
|
104
|
+
const NOTHING = { score: 0, band: "low", findings: [] };
|
|
105
|
+
export function scoreCommands(commands, opts) {
|
|
106
|
+
const text = commands.filter((c) => c.trim());
|
|
107
|
+
if (text.length === 0)
|
|
108
|
+
return NOTHING;
|
|
109
|
+
const matched = RULES.map((rule, at) => ({ rule, at })).filter(({ rule }) => text.some((c) => rule.test(c)));
|
|
110
|
+
// One finding per rule, in weight order: the worst first, which is the one the score is built around.
|
|
111
|
+
const findings = matched
|
|
112
|
+
.sort((a, b) => b.rule.weight - a.rule.weight || a.at - b.at)
|
|
113
|
+
.map(({ rule }) => ({ rule: rule.rule, weight: rule.weight, what: rule.what }));
|
|
114
|
+
const weights = findings.map((f) => f.weight);
|
|
115
|
+
const max = weights.length ? Math.max(...weights) : 0;
|
|
116
|
+
const total = weights.reduce((sum, w) => sum + w, 0);
|
|
117
|
+
const plan = opts?.plan === true;
|
|
118
|
+
// A plan job's checks are written by an investigator and promoted from a worktree that nobody has inspected yet, so
|
|
119
|
+
// the same text is worth more there. The bonus is flat: it says why the job starts above the command itself.
|
|
120
|
+
const score = Math.min(100, max + Math.floor((total - max) / 3) + (plan ? 15 : 0));
|
|
121
|
+
if (plan)
|
|
122
|
+
findings.push({ rule: "plan", weight: 15, what: "a plan job's checks come from an untrusted worktree (+15)" });
|
|
123
|
+
// The plan finding is named where its weight belongs, so the list stays in weight order after the addition.
|
|
124
|
+
const ordered = [...findings].sort((a, b) => b.weight - a.weight);
|
|
125
|
+
return { score, band: bandOf(score, ordered), findings: ordered };
|
|
126
|
+
}
|
package/dist/roles.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@omacode/state/roles";
|
package/dist/routing.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@omacode/state/routing";
|