opencode-agent-harness 0.0.0-stage → 0.3.14
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 +217 -0
- package/LICENSE +21 -0
- package/README.md +636 -2
- package/bin/agent-harness.js +3182 -0
- package/contrib/global-plugins/README.md +49 -0
- package/contrib/global-plugins/harness-freshness.ts +355 -0
- package/package.json +86 -4
- package/templates/.agents/AGENTS.md +148 -0
- package/templates/.agents/features/INDEX.md +10 -0
- package/templates/.agents/handoffs/README.md +87 -0
- package/templates/.agents/memory/dev-loop.md +59 -0
- package/templates/.agents/memory/domain-map.md +29 -0
- package/templates/.agents/memory/environments.md +35 -0
- package/templates/.agents/memory/flow-map.md +23 -0
- package/templates/.agents/memory/history.md +7 -0
- package/templates/.agents/memory/locations.md +14 -0
- package/templates/.agents/memory/model-routing.md +261 -0
- package/templates/.agents/memory/orchestration.md +183 -0
- package/templates/.agents/memory/owned-files.md +19 -0
- package/templates/.agents/memory/product-principles.md +37 -0
- package/templates/.agents/memory/quality-parameters.md +37 -0
- package/templates/.agents/memory/stack-versions.md +35 -0
- package/templates/.agents/memory/test-design-parameters.md +36 -0
- package/templates/.agents/nightly.json +17 -0
- package/templates/.agents/qa-plans/README.md +70 -0
- package/templates/.agents/qa-runs/README.md +21 -0
- package/templates/.agents/reviews/README.md +47 -0
- package/templates/.agents/rules/00-operating.md +199 -0
- package/templates/.agents/skills/data-engineer/SKILL.md +31 -0
- package/templates/.agents/skills/devops-engineer/SKILL.md +50 -0
- package/templates/.agents/skills/diagnostics-expert/SKILL.md +44 -0
- package/templates/.agents/skills/docs/SKILL.md +140 -0
- package/templates/.agents/skills/features/SKILL.md +187 -0
- package/templates/.agents/skills/frontend-engineer/SKILL.md +81 -0
- package/templates/.agents/skills/handoff/SKILL.md +85 -0
- package/templates/.agents/skills/mobile-engineer/SKILL.md +101 -0
- package/templates/.agents/skills/planner/SKILL.md +112 -0
- package/templates/.agents/skills/product-manager/SKILL.md +115 -0
- package/templates/.agents/skills/qa-architect/SKILL.md +224 -0
- package/templates/.agents/skills/qa-runner/SKILL.md +64 -0
- package/templates/.agents/skills/security-engineer/SKILL.md +53 -0
- package/templates/.agents/skills/system-architect/SKILL.md +42 -0
- package/templates/.agents/skills/ui-designer/SKILL.md +71 -0
- package/templates/.github/workflows/ci.yml +148 -0
- package/templates/.github/workflows/deploy-uat.yml +95 -0
- package/templates/.github/workflows/nightly.yml +83 -0
- package/templates/.github/workflows/uat-health.yml +41 -0
- package/templates/.husky/pre-push +23 -0
- package/templates/.ignore +28 -0
- package/templates/.opencode/agents/data-engineer.md +37 -0
- package/templates/.opencode/agents/devops-engineer.md +37 -0
- package/templates/.opencode/agents/diagnostics-expert.md +37 -0
- package/templates/.opencode/agents/docs.md +37 -0
- package/templates/.opencode/agents/features.md +21 -0
- package/templates/.opencode/agents/frontend-engineer.md +37 -0
- package/templates/.opencode/agents/handoff.md +21 -0
- package/templates/.opencode/agents/mobile-engineer.md +37 -0
- package/templates/.opencode/agents/planner.md +34 -0
- package/templates/.opencode/agents/product-manager.md +29 -0
- package/templates/.opencode/agents/qa-architect.md +27 -0
- package/templates/.opencode/agents/qa-runner.md +17 -0
- package/templates/.opencode/agents/security-engineer.md +37 -0
- package/templates/.opencode/agents/system-architect.md +37 -0
- package/templates/.opencode/agents/ui-designer.md +37 -0
- package/templates/.opencode/commands/board.md +15 -0
- package/templates/.opencode/commands/checkpoint.md +25 -0
- package/templates/.opencode/commands/feature.md +56 -0
- package/templates/.opencode/commands/finish.md +36 -0
- package/templates/.opencode/commands/next.md +49 -0
- package/templates/.opencode/commands/report.md +55 -0
- package/templates/.opencode/commands/resume.md +39 -0
- package/templates/.opencode/commands/switch-model.md +34 -0
- package/templates/.opencode/commands/tidy.md +49 -0
- package/templates/.opencode/plugins/backlog-notify.ts +57 -0
- package/templates/.opencode/plugins/backlog.ts +272 -0
- package/templates/.opencode/plugins/checkpoint.ts +160 -0
- package/templates/.opencode/plugins/context-guard.ts +1164 -0
- package/templates/.opencode/plugins/diff.ts +85 -0
- package/templates/.opencode/plugins/gates.ts +71 -0
- package/templates/.opencode/plugins/job-notify.ts +241 -0
- package/templates/.opencode/plugins/orient.ts +67 -0
- package/templates/.opencode/plugins/session.ts +89 -0
- package/templates/.opencode/plugins/wait.ts +101 -0
- package/templates/.opencode/plugins/where.ts +70 -0
- package/templates/.opencode/skills/bounded-explore/SKILL.md +38 -0
- package/templates/.opencode/skills/dev-loop/SKILL.md +87 -0
- package/templates/.vercelignore +16 -0
- package/templates/AGENTS.md +27 -0
- package/templates/BACKLOG.md +95 -0
- package/templates/docs/ci-cd-pipeline.md +81 -0
- package/templates/docs/gates.md +128 -0
- package/templates/docs/mcp-integrations.md +97 -0
- package/templates/docs/mobile-testing.md +555 -0
- package/templates/docs/nightly-testing.md +106 -0
- package/templates/docs/wsl-optimizations.md +290 -0
- package/templates/opencode.json +188 -0
- package/templates/scripts/agent-report.js +541 -0
- package/templates/scripts/agent-search.sh +117 -0
- package/templates/scripts/backlog.js +1924 -0
- package/templates/scripts/board-service.sh +214 -0
- package/templates/scripts/checkpoint.js +290 -0
- package/templates/scripts/finalize.js +416 -0
- package/templates/scripts/nightly.js +913 -0
- package/templates/scripts/orient.js +514 -0
- package/templates/scripts/qa/board-client.js +323 -0
- package/templates/scripts/qa/board-lib.js +96 -0
- package/templates/scripts/qa/board-sync.js +499 -0
- package/templates/scripts/qa/capacity-lib.js +916 -0
- package/templates/scripts/qa/check-backlog.js +434 -0
- package/templates/scripts/qa/check-capacity.js +85 -0
- package/templates/scripts/qa/check-ci-policy.js +164 -0
- package/templates/scripts/qa/check-concurrency.js +344 -0
- package/templates/scripts/qa/check-dispatch-config.js +296 -0
- package/templates/scripts/qa/check-docs.js +288 -0
- package/templates/scripts/qa/check-integrate.js +98 -0
- package/templates/scripts/qa/check-linger.js +160 -0
- package/templates/scripts/qa/check-prefix-stability.js +187 -0
- package/templates/scripts/qa/check-product.js +177 -0
- package/templates/scripts/qa/check-qa-plan.js +378 -0
- package/templates/scripts/qa/check-qa-scripts.js +85 -0
- package/templates/scripts/qa/check-quality.js +249 -0
- package/templates/scripts/qa/check-test-wiring.js +253 -0
- package/templates/scripts/qa/check-worktrees.js +93 -0
- package/templates/scripts/qa/ci-triage.js +346 -0
- package/templates/scripts/qa/ci-usage-lib.js +185 -0
- package/templates/scripts/qa/ci-wait-lib.js +294 -0
- package/templates/scripts/qa/command-classifier.js +552 -0
- package/templates/scripts/qa/context-guard-core.js +1618 -0
- package/templates/scripts/qa/diff.js +174 -0
- package/templates/scripts/qa/feature-scaffold.js +224 -0
- package/templates/scripts/qa/fixtures/backlog.md +104 -0
- package/templates/scripts/qa/governance.js +174 -0
- package/templates/scripts/qa/hash-file.js +32 -0
- package/templates/scripts/qa/lease-lib.js +263 -0
- package/templates/scripts/qa/merge-backlog.js +324 -0
- package/templates/scripts/qa/mobile-e2e/analyze-timings.js +300 -0
- package/templates/scripts/qa/mobile-e2e/apk-cache.js +420 -0
- package/templates/scripts/qa/mobile-e2e/backend-build-cache.js +203 -0
- package/templates/scripts/qa/mobile-e2e/boot-emulator.sh +432 -0
- package/templates/scripts/qa/mobile-e2e/build-apk.sh +210 -0
- package/templates/scripts/qa/mobile-e2e/check-flows.js +568 -0
- package/templates/scripts/qa/mobile-e2e/config.yaml +18 -0
- package/templates/scripts/qa/mobile-e2e/device-down.sh +88 -0
- package/templates/scripts/qa/mobile-e2e/device-up.sh +258 -0
- package/templates/scripts/qa/mobile-e2e/ios-simulator-down.sh +72 -0
- package/templates/scripts/qa/mobile-e2e/ios-simulator-up.sh +153 -0
- package/templates/scripts/qa/mobile-e2e/job-events.js +181 -0
- package/templates/scripts/qa/mobile-e2e/prewarm.sh +56 -0
- package/templates/scripts/qa/mobile-e2e/run-detached.sh +222 -0
- package/templates/scripts/qa/mobile-e2e/run-maestro.js +1543 -0
- package/templates/scripts/qa/mobile-e2e/ui-dump.js +327 -0
- package/templates/scripts/qa/models.allowlist.txt +32 -0
- package/templates/scripts/qa/provider-stats.js +878 -0
- package/templates/scripts/qa/run-clean.js +83 -0
- package/templates/scripts/qa/run-gates.js +213 -0
- package/templates/scripts/qa/run-loop.js +220 -0
- package/templates/scripts/qa/session-recon.js +512 -0
- package/templates/scripts/qa/store-lib.js +324 -0
- package/templates/scripts/qa/tool-lib.js +134 -0
- package/templates/scripts/qa/where.js +214 -0
- package/templates/scripts/qa/worktree-hygiene.js +217 -0
- package/templates/scripts/qa/wslconfig-merge.js +157 -0
- package/templates/scripts/serve.js +1328 -0
- package/templates/scripts/setup-nightly.sh +185 -0
- package/templates/scripts/setup-tmpfs.sh +374 -0
- package/templates/scripts/ship.js +586 -0
- package/templates/scripts/switch-reasoning-model.ps1 +101 -0
- package/templates/scripts/switch-reasoning-model.sh +104 -0
- package/templates/scripts/wait-for.js +342 -0
- package/templates/scripts/wsl-gpu.sh +180 -0
- package/templates/scripts/wsl-tune.sh +319 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
/*
|
|
4
|
+
* command-classifier — decide whether a shell command will launch a
|
|
5
|
+
* memory-heavy process, and whether that process is LONG-LIVED (holds RAM for
|
|
6
|
+
* its whole lifetime) or a ONE-SHOT (spikes and releases).
|
|
7
|
+
*
|
|
8
|
+
* Why a real analyzer instead of a regex over the raw string: a substring match
|
|
9
|
+
* reads arguments and paths as if they were the program. `adb -s emulator-5554
|
|
10
|
+
* shell uiautomator dump` and `python3 - <<EOF … "~/.maestro/…"` are read-only
|
|
11
|
+
* diagnostics, but `/emulator\b/` and `/maestro\b/i` labelled them as launching
|
|
12
|
+
* an emulator and Maestro — which blocked them whenever the device tier was up
|
|
13
|
+
* (the F-311 dead end). So this module:
|
|
14
|
+
* 1. tokenizes the shell (quotes, escapes, operators, redirections, heredocs,
|
|
15
|
+
* command substitution),
|
|
16
|
+
* 2. resolves each simple command's PROGRAM — peeling `VAR=x` assignments and
|
|
17
|
+
* wrappers (`env`, `sudo`, `nohup`, `timeout`, `systemd-run`, …) and
|
|
18
|
+
* resolving interpreters (`bash|sh|node|python … <script>`) and
|
|
19
|
+
* multiplexers (`npm/yarn/pnpm … run <script>`, `npx <pkg>`),
|
|
20
|
+
* 3. matches ONLY the program / subcommand / script basename against the rule
|
|
21
|
+
* table, and
|
|
22
|
+
* 4. recurses into embedded payloads (`--cmd "…"`, `sh -c "…"`).
|
|
23
|
+
*
|
|
24
|
+
* Paths, device serials, and heredoc bodies can never be mistaken for a heavy
|
|
25
|
+
* program, and `echo "npm run dev"` / a commit message mentioning "emulator"
|
|
26
|
+
* stay quiet.
|
|
27
|
+
*
|
|
28
|
+
* Pure: string in, findings out, never throws.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/* Heavy labels that are LONG-LIVED tiers: they hold RAM for their whole
|
|
32
|
+
* lifetime and must not run two at once (emulator + `next dev` + a third dev
|
|
33
|
+
* server). Everything else heavy (a build, a test tier, `maestro`) is a
|
|
34
|
+
* ONE-SHOT: it spikes RAM but releases it, so it is legitimate to run while a
|
|
35
|
+
* live tier is up (the live-tier test pattern) and must NOT be counted as an
|
|
36
|
+
* extra concurrent tier. */
|
|
37
|
+
const LONG_LIVED = new Set([
|
|
38
|
+
"emulator",
|
|
39
|
+
"next dev",
|
|
40
|
+
"metro",
|
|
41
|
+
"vite",
|
|
42
|
+
"bundler",
|
|
43
|
+
"dev server",
|
|
44
|
+
"docker",
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
/* Programs that prepend another command; the real program is resolved past
|
|
48
|
+
* them. `systemd-run`/`timeout` need bespoke skipping (see skipWrapper). */
|
|
49
|
+
const WRAPPERS = new Set([
|
|
50
|
+
"env",
|
|
51
|
+
"sudo",
|
|
52
|
+
"nohup",
|
|
53
|
+
"time",
|
|
54
|
+
"command",
|
|
55
|
+
"exec",
|
|
56
|
+
"stdbuf",
|
|
57
|
+
"nice",
|
|
58
|
+
"ionice",
|
|
59
|
+
"setsid",
|
|
60
|
+
"timeout",
|
|
61
|
+
"xargs",
|
|
62
|
+
"systemd-run",
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
/* Wrapper options that consume the following token (so it is not mistaken for
|
|
66
|
+
* the program). */
|
|
67
|
+
const WRAPPER_OPTS_WITH_ARG = {
|
|
68
|
+
sudo: [
|
|
69
|
+
"-u", "--user", "-g", "--group", "-h", "--host", "-p", "--prompt",
|
|
70
|
+
"-C", "--close-from", "-r", "--role", "-t", "--type", "-T",
|
|
71
|
+
"--command-timeout", "-U", "--other-user", "-D", "--chdir",
|
|
72
|
+
],
|
|
73
|
+
env: ["-u", "--unset", "-S", "--split-string", "-C", "--chdir"],
|
|
74
|
+
stdbuf: ["-i", "--input", "-o", "--output", "-e", "--error"],
|
|
75
|
+
ionice: ["-c", "--class", "-n", "--classdata", "-p", "--pid", "-P", "--pgid", "-u", "--uid"],
|
|
76
|
+
timeout: ["-k", "--kill-after", "-s", "--signal"],
|
|
77
|
+
nice: ["-n", "--adjustment"],
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const SHELLS = new Set(["bash", "sh", "zsh", "dash", "ksh", "ash", "fish", "csh", "tcsh"]);
|
|
81
|
+
const NODEISH = new Set(["node", "nodejs", "deno"]);
|
|
82
|
+
const PYISH = new Set(["python", "python3", "python2", "ruby", "perl", "php"]);
|
|
83
|
+
|
|
84
|
+
/* Script basenames and npm/yarn/pnpm script names -> [label, longLived]. These
|
|
85
|
+
* are the harness-owned mobile-e2e wrappers and the project test/dev scripts. */
|
|
86
|
+
const SCRIPT_RULES = {
|
|
87
|
+
dev: ["dev server", true],
|
|
88
|
+
develop: ["dev server", true],
|
|
89
|
+
"start:dev": ["dev server", true],
|
|
90
|
+
"test:quick": ["heavy test tier", false],
|
|
91
|
+
"test:api": ["heavy test tier", false],
|
|
92
|
+
"test:verify": ["heavy test tier", false],
|
|
93
|
+
"test:e2e": ["heavy test tier", false],
|
|
94
|
+
"test:mobile": ["heavy test tier", false],
|
|
95
|
+
"test:data": ["heavy test tier", false],
|
|
96
|
+
"test:integration": ["heavy test tier", false],
|
|
97
|
+
"build-dev-apk.sh": ["apk build", false],
|
|
98
|
+
"build-apk.sh": ["apk build", false],
|
|
99
|
+
"prewarm.sh": ["apk build", false],
|
|
100
|
+
"boot-emulator.sh": ["emulator", true],
|
|
101
|
+
"device-up.sh": ["emulator", true],
|
|
102
|
+
"run-maestro.js": ["maestro", false],
|
|
103
|
+
"run-maestro.sh": ["maestro", false],
|
|
104
|
+
"run-detached.sh": ["maestro", false],
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
function finding(label, longLived, evidence) {
|
|
108
|
+
return { label, longLived, evidence };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function baseName(token) {
|
|
112
|
+
let t = String(token == null ? "" : token).replace(/["'`]/g, "");
|
|
113
|
+
t = t.replace(/\\/g, "/");
|
|
114
|
+
const parts = t.split("/");
|
|
115
|
+
let b = parts[parts.length - 1] || t;
|
|
116
|
+
return b.replace(/\.exe$/i, "");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* ---------------------------------------------------------------- tokenizer
|
|
120
|
+
* Split a shell command into simple commands (`;`, `&&`, `||`, `|`, `&`,
|
|
121
|
+
* newlines, subshell parens separate). Quoting, escaping, and redirections are
|
|
122
|
+
* honoured; heredoc bodies and command substitutions are NOT scanned as shell
|
|
123
|
+
* (their content can never label the command). Returns an array of segments,
|
|
124
|
+
* each an array of word strings. */
|
|
125
|
+
function tokenize(input) {
|
|
126
|
+
const s = String(input == null ? "" : input);
|
|
127
|
+
const segments = [];
|
|
128
|
+
let words = [];
|
|
129
|
+
let cur = "";
|
|
130
|
+
let has = false;
|
|
131
|
+
let i = 0;
|
|
132
|
+
const n = s.length;
|
|
133
|
+
const pending = []; // heredoc delimiters awaiting their bodies
|
|
134
|
+
|
|
135
|
+
const flushWord = () => {
|
|
136
|
+
if (has) {
|
|
137
|
+
words.push(cur);
|
|
138
|
+
cur = "";
|
|
139
|
+
has = false;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const flushSegment = () => {
|
|
143
|
+
flushWord();
|
|
144
|
+
if (words.length) segments.push(words);
|
|
145
|
+
words = [];
|
|
146
|
+
};
|
|
147
|
+
const skipHeredoc = (h) => {
|
|
148
|
+
let done = false;
|
|
149
|
+
while (i < n) {
|
|
150
|
+
let eol = s.indexOf("\n", i);
|
|
151
|
+
if (eol === -1) eol = n;
|
|
152
|
+
const line = s.slice(i, eol);
|
|
153
|
+
const test = h.stripTabs ? line.replace(/^\t+/, "") : line;
|
|
154
|
+
i = eol < n ? eol + 1 : n;
|
|
155
|
+
if (test === h.delim) {
|
|
156
|
+
done = true;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return done;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
while (i < n) {
|
|
164
|
+
const c = s[i];
|
|
165
|
+
|
|
166
|
+
if (c === "\n") {
|
|
167
|
+
i += 1;
|
|
168
|
+
while (pending.length) {
|
|
169
|
+
const h = pending.shift();
|
|
170
|
+
if (!skipHeredoc(h)) break;
|
|
171
|
+
}
|
|
172
|
+
flushSegment();
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (c === "#" && !has) {
|
|
176
|
+
while (i < n && s[i] !== "\n") i += 1;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (c === " " || c === "\t" || c === "\r") {
|
|
180
|
+
flushWord();
|
|
181
|
+
i += 1;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (c === ";" || c === "&" || c === "|" || c === "(" || c === ")") {
|
|
185
|
+
flushWord();
|
|
186
|
+
flushSegment();
|
|
187
|
+
i += 1;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (c === "<" || c === ">") {
|
|
191
|
+
if (has && /^\d+$/.test(cur)) {
|
|
192
|
+
cur = "";
|
|
193
|
+
has = false;
|
|
194
|
+
}
|
|
195
|
+
flushWord();
|
|
196
|
+
let op = c;
|
|
197
|
+
i += 1;
|
|
198
|
+
if (s[i] === c) {
|
|
199
|
+
op += c;
|
|
200
|
+
i += 1;
|
|
201
|
+
}
|
|
202
|
+
if (s[i] === "&") {
|
|
203
|
+
op += "&";
|
|
204
|
+
i += 1;
|
|
205
|
+
}
|
|
206
|
+
if (op === "<<" || op === "<<-") {
|
|
207
|
+
const stripTabs = op === "<<-";
|
|
208
|
+
while (i < n && (s[i] === " " || s[i] === "\t")) i += 1;
|
|
209
|
+
let delim = "";
|
|
210
|
+
if (s[i] === '"' || s[i] === "'") {
|
|
211
|
+
const q = s[i];
|
|
212
|
+
i += 1;
|
|
213
|
+
while (i < n && s[i] !== q) {
|
|
214
|
+
delim += s[i];
|
|
215
|
+
i += 1;
|
|
216
|
+
}
|
|
217
|
+
if (s[i] === q) i += 1;
|
|
218
|
+
} else {
|
|
219
|
+
while (i < n && !/[\s;&|<>()]/.test(s[i])) {
|
|
220
|
+
delim += s[i];
|
|
221
|
+
i += 1;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
pending.push({ delim, stripTabs });
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
// Ordinary redirection: drop the target word.
|
|
228
|
+
while (i < n && (s[i] === " " || s[i] === "\t")) i += 1;
|
|
229
|
+
while (i < n && !/[\s;&|<>()]/.test(s[i])) {
|
|
230
|
+
if (s[i] === '"' || s[i] === "'") {
|
|
231
|
+
const q = s[i];
|
|
232
|
+
i += 1;
|
|
233
|
+
while (i < n && s[i] !== q) i += 1;
|
|
234
|
+
if (s[i] === q) i += 1;
|
|
235
|
+
} else {
|
|
236
|
+
i += 1;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (c === "$" && s[i + 1] === "(") {
|
|
242
|
+
let depth = 1;
|
|
243
|
+
i += 2;
|
|
244
|
+
while (i < n && depth > 0) {
|
|
245
|
+
if (s[i] === "(") depth += 1;
|
|
246
|
+
else if (s[i] === ")") depth -= 1;
|
|
247
|
+
i += 1;
|
|
248
|
+
}
|
|
249
|
+
cur += " ";
|
|
250
|
+
has = true;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (c === "`") {
|
|
254
|
+
i += 1;
|
|
255
|
+
while (i < n && s[i] !== "`") i += 1;
|
|
256
|
+
if (s[i] === "`") i += 1;
|
|
257
|
+
cur += " ";
|
|
258
|
+
has = true;
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (c === "'") {
|
|
262
|
+
i += 1;
|
|
263
|
+
has = true;
|
|
264
|
+
while (i < n && s[i] !== "'") {
|
|
265
|
+
cur += s[i];
|
|
266
|
+
i += 1;
|
|
267
|
+
}
|
|
268
|
+
if (s[i] === "'") i += 1;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (c === '"') {
|
|
272
|
+
i += 1;
|
|
273
|
+
has = true;
|
|
274
|
+
while (i < n && s[i] !== '"') {
|
|
275
|
+
if (s[i] === "\\" && i + 1 < n && /["\\$`]/.test(s[i + 1])) {
|
|
276
|
+
cur += s[i + 1];
|
|
277
|
+
i += 2;
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
if (s[i] === "$" && s[i + 1] === "(") {
|
|
281
|
+
let depth = 1;
|
|
282
|
+
i += 2;
|
|
283
|
+
while (i < n && depth > 0) {
|
|
284
|
+
if (s[i] === "(") depth += 1;
|
|
285
|
+
else if (s[i] === ")") depth -= 1;
|
|
286
|
+
i += 1;
|
|
287
|
+
}
|
|
288
|
+
cur += " ";
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
cur += s[i];
|
|
292
|
+
i += 1;
|
|
293
|
+
}
|
|
294
|
+
if (s[i] === '"') i += 1;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (c === "\\" && i + 1 < n) {
|
|
298
|
+
cur += s[i + 1];
|
|
299
|
+
has = true;
|
|
300
|
+
i += 2;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
cur += c;
|
|
305
|
+
has = true;
|
|
306
|
+
i += 1;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
flushSegment();
|
|
310
|
+
return segments;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/* ------------------------------------------------------------- resolvers */
|
|
314
|
+
function firstNonFlag(args, from) {
|
|
315
|
+
for (let i = from || 0; i < args.length; i += 1) {
|
|
316
|
+
if (args[i] === "--") continue;
|
|
317
|
+
if (!args[i].startsWith("-")) return { value: args[i], index: i };
|
|
318
|
+
}
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function skipWrapper(program, words, idx) {
|
|
323
|
+
let i = idx + 1;
|
|
324
|
+
const argOpts = WRAPPER_OPTS_WITH_ARG[program] || [];
|
|
325
|
+
if (program === "systemd-run") {
|
|
326
|
+
while (i < words.length && words[i] !== "--") {
|
|
327
|
+
if (!words[i].startsWith("-")) break;
|
|
328
|
+
i += 1;
|
|
329
|
+
}
|
|
330
|
+
if (words[i] === "--") i += 1;
|
|
331
|
+
return i;
|
|
332
|
+
}
|
|
333
|
+
if (program === "timeout") {
|
|
334
|
+
while (i < words.length && words[i].startsWith("-")) {
|
|
335
|
+
i += argOpts.includes(words[i]) ? 2 : 1;
|
|
336
|
+
}
|
|
337
|
+
if (i < words.length) i += 1; // the duration
|
|
338
|
+
return i;
|
|
339
|
+
}
|
|
340
|
+
while (i < words.length) {
|
|
341
|
+
const w = words[i];
|
|
342
|
+
if (argOpts.includes(w)) {
|
|
343
|
+
i += 2;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
if (w.startsWith("-") || /^[A-Za-z_][A-Za-z0-9_]*=/.test(w)) {
|
|
347
|
+
i += 1;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
return i;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/* The real program token of a simple command: assignments stripped, wrappers
|
|
356
|
+
* peeled. Returns `{ program, args }` or null. */
|
|
357
|
+
function resolveProgram(words) {
|
|
358
|
+
let i = 0;
|
|
359
|
+
while (i < words.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(words[i])) i += 1;
|
|
360
|
+
while (i < words.length) {
|
|
361
|
+
const p = baseName(words[i]).toLowerCase();
|
|
362
|
+
if (WRAPPERS.has(p)) {
|
|
363
|
+
i = skipWrapper(p, words, i);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
if (i >= words.length) return null;
|
|
369
|
+
const program = baseName(words[i]).toLowerCase();
|
|
370
|
+
if (!program) return null;
|
|
371
|
+
return { program, args: words.slice(i + 1) };
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function classifyRunner(program, args) {
|
|
375
|
+
const sub = firstNonFlag(args);
|
|
376
|
+
if (!sub) return null;
|
|
377
|
+
if (program === "npx" || program === "bunx") {
|
|
378
|
+
return classifyProgram(baseName(sub.value).toLowerCase(), args.slice(sub.index + 1));
|
|
379
|
+
}
|
|
380
|
+
if (sub.value === "run" || sub.value === "run-script") {
|
|
381
|
+
const script = firstNonFlag(args, sub.index + 1);
|
|
382
|
+
if (!script) return null;
|
|
383
|
+
const r = SCRIPT_RULES[script.value];
|
|
384
|
+
return r ? finding(r[0], r[1], `npm script ${script.value}`) : null;
|
|
385
|
+
}
|
|
386
|
+
if (sub.value === "exec" || sub.value === "x" || sub.value === "dlx") {
|
|
387
|
+
const pkg = firstNonFlag(args, sub.index + 1);
|
|
388
|
+
if (!pkg) return null;
|
|
389
|
+
return classifyProgram(baseName(pkg.value).toLowerCase(), args.slice(pkg.index + 1));
|
|
390
|
+
}
|
|
391
|
+
const r = SCRIPT_RULES[sub.value];
|
|
392
|
+
return r ? finding(r[0], r[1], `script ${sub.value}`) : null;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function classifyInterpreter(program, args) {
|
|
396
|
+
if ((NODEISH.has(program) || PYISH.has(program)) && args.some((a) => ["-e", "--eval", "-p", "--print", "-c"].includes(a))) {
|
|
397
|
+
return null; // inline code is opaque
|
|
398
|
+
}
|
|
399
|
+
const script = firstNonFlag(args);
|
|
400
|
+
if (!script) return null;
|
|
401
|
+
const r = SCRIPT_RULES[baseName(script.value).toLowerCase()];
|
|
402
|
+
return r ? finding(r[0], r[1], `script ${baseName(script.value)}`) : null;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function classifyProgram(program, args) {
|
|
406
|
+
const ev = (detail) => `program ${program}${detail ? ` ${detail}` : ""}`;
|
|
407
|
+
if (/^qemu-system/.test(program)) return finding("emulator", true, ev());
|
|
408
|
+
switch (program) {
|
|
409
|
+
case "emulator":
|
|
410
|
+
return finding("emulator", true, ev());
|
|
411
|
+
case "maestro":
|
|
412
|
+
return finding("maestro", false, ev());
|
|
413
|
+
case "metro":
|
|
414
|
+
return finding("metro", true, ev());
|
|
415
|
+
case "webpack":
|
|
416
|
+
case "turbopack":
|
|
417
|
+
return finding("bundler", true, ev());
|
|
418
|
+
case "xcodebuild":
|
|
419
|
+
return finding("native build", false, ev());
|
|
420
|
+
case "gradlew":
|
|
421
|
+
case "gradle": {
|
|
422
|
+
const task = args.find((a) => /assemble/i.test(a));
|
|
423
|
+
return finding(task ? "apk build" : "native build", false, ev(task || ""));
|
|
424
|
+
}
|
|
425
|
+
case "next": {
|
|
426
|
+
const s = firstNonFlag(args);
|
|
427
|
+
return s && (s.value === "dev" || s.value === "start") ? finding("next dev", true, ev(s.value)) : null;
|
|
428
|
+
}
|
|
429
|
+
case "expo": {
|
|
430
|
+
const s = firstNonFlag(args);
|
|
431
|
+
if (!s) return null;
|
|
432
|
+
if (s.value === "start") return finding("metro", true, ev(s.value));
|
|
433
|
+
if (s.value === "prebuild" || s.value === "export") return finding("apk build", false, ev(s.value));
|
|
434
|
+
return null;
|
|
435
|
+
}
|
|
436
|
+
case "react-native": {
|
|
437
|
+
const s = firstNonFlag(args);
|
|
438
|
+
if (!s) return null;
|
|
439
|
+
if (s.value === "start") return finding("metro", true, ev(s.value));
|
|
440
|
+
if (s.value === "build" || s.value === "run-android") return finding("apk build", false, ev(s.value));
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
case "vite": {
|
|
444
|
+
const s = firstNonFlag(args);
|
|
445
|
+
return s && ["dev", "serve", "preview"].includes(s.value) ? finding("vite", true, ev(s.value)) : null;
|
|
446
|
+
}
|
|
447
|
+
case "docker": {
|
|
448
|
+
const s = firstNonFlag(args);
|
|
449
|
+
return s && ["build", "run", "compose"].includes(s.value) ? finding("docker", true, ev(s.value)) : null;
|
|
450
|
+
}
|
|
451
|
+
case "prisma": {
|
|
452
|
+
const s = firstNonFlag(args);
|
|
453
|
+
return s && (s.value === "migrate" || s.value === "generate") ? finding("build", false, ev(s.value)) : null;
|
|
454
|
+
}
|
|
455
|
+
case "turbo":
|
|
456
|
+
case "nx": {
|
|
457
|
+
const s = firstNonFlag(args);
|
|
458
|
+
return s && (s.value === "run" || s.value === "build") ? finding("build", false, ev(s.value)) : null;
|
|
459
|
+
}
|
|
460
|
+
case "tsc":
|
|
461
|
+
return args.includes("--build") || args.includes("-b") ? finding("build", false, ev("--build")) : null;
|
|
462
|
+
case "npm":
|
|
463
|
+
case "yarn":
|
|
464
|
+
case "pnpm":
|
|
465
|
+
case "bun":
|
|
466
|
+
case "npx":
|
|
467
|
+
case "bunx":
|
|
468
|
+
return classifyRunner(program, args);
|
|
469
|
+
default:
|
|
470
|
+
if (SHELLS.has(program) || NODEISH.has(program) || PYISH.has(program)) return classifyInterpreter(program, args);
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/* Analyze ONE simple command (already tokenized words) into findings, including
|
|
476
|
+
* any `--cmd` payload and `sh -c` body it embeds. */
|
|
477
|
+
function analyzeSegment(words) {
|
|
478
|
+
const out = [];
|
|
479
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
480
|
+
const w = words[i];
|
|
481
|
+
if (w === "--cmd" && i + 1 < words.length) {
|
|
482
|
+
for (const f of analyzeAll(words[i + 1])) out.push(f);
|
|
483
|
+
i += 1;
|
|
484
|
+
} else if (w.startsWith("--cmd=")) {
|
|
485
|
+
for (const f of analyzeAll(w.slice("--cmd=".length))) out.push(f);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
const cmd = resolveProgram(words);
|
|
489
|
+
if (cmd) {
|
|
490
|
+
const f = classifyProgram(cmd.program, cmd.args);
|
|
491
|
+
if (f) out.push(f);
|
|
492
|
+
if (SHELLS.has(cmd.program)) {
|
|
493
|
+
const ci = cmd.args.indexOf("-c");
|
|
494
|
+
if (ci !== -1 && cmd.args[ci + 1]) for (const g of analyzeAll(cmd.args[ci + 1])) out.push(g);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return out;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function dedupe(findings) {
|
|
501
|
+
const seen = new Set();
|
|
502
|
+
const out = [];
|
|
503
|
+
for (const f of findings) {
|
|
504
|
+
if (!f || !f.label || seen.has(f.label)) continue;
|
|
505
|
+
seen.add(f.label);
|
|
506
|
+
out.push(f);
|
|
507
|
+
}
|
|
508
|
+
return out;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/* Every heavy finding in a command (may contain several segments/payloads). */
|
|
512
|
+
function analyzeAll(cmd) {
|
|
513
|
+
const s = String(cmd == null ? "" : cmd);
|
|
514
|
+
if (!s.trim()) return [];
|
|
515
|
+
const out = [];
|
|
516
|
+
for (const seg of tokenize(s)) {
|
|
517
|
+
for (const f of analyzeSegment(seg)) out.push(f);
|
|
518
|
+
}
|
|
519
|
+
return dedupe(out);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/* The single most important finding: a long-lived tier dominates a one-shot. */
|
|
523
|
+
function analyzeCommand(cmd) {
|
|
524
|
+
const all = analyzeAll(cmd);
|
|
525
|
+
if (all.length === 0) return null;
|
|
526
|
+
return all.find((f) => f.longLived) || all[0];
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function classifyHeavy(cmd) {
|
|
530
|
+
const f = analyzeCommand(cmd);
|
|
531
|
+
return f ? f.label : null;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function isHeavyCommand(cmd) {
|
|
535
|
+
return analyzeCommand(cmd) !== null;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function isLongLivedHeavy(cmd) {
|
|
539
|
+
const f = analyzeCommand(cmd);
|
|
540
|
+
return !!(f && f.longLived);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
module.exports = {
|
|
544
|
+
LONG_LIVED,
|
|
545
|
+
SCRIPT_RULES,
|
|
546
|
+
tokenize,
|
|
547
|
+
analyzeAll,
|
|
548
|
+
analyzeCommand,
|
|
549
|
+
classifyHeavy,
|
|
550
|
+
isHeavyCommand,
|
|
551
|
+
isLongLivedHeavy,
|
|
552
|
+
};
|