@primitive.ai/prim 0.1.0-alpha.113 → 0.1.0-alpha.115
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 +34 -0
- package/assets/primitive-gameplan/SKILL.md +134 -0
- package/assets/primitive-gameplan/claude-hooks.json +28 -0
- package/assets/primitive-gameplan/scripts/activate.mjs +37 -0
- package/dist/{chunk-IW5HCCOW.js → chunk-IMYELJIV.js} +1 -1
- package/dist/{chunk-XV6C5ZV2.js → chunk-RUUSUF43.js} +245 -2
- package/dist/hook-runtime/hooks/post-tool-use.js +4866 -131
- package/dist/hook-runtime/hooks/pre-tool-use.js +4893 -142
- package/dist/hook-runtime/hooks/prim-hook.js +4867 -132
- package/dist/hook-runtime/index.js +621 -265
- package/dist/hooks/post-tool-use.js +2 -2
- package/dist/hooks/pre-tool-use.js +22 -6
- package/dist/hooks/prim-hook.js +2 -2
- package/dist/index.js +498 -142
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -203,6 +203,25 @@ prim codex install # Install OpenAI Codex hooks (user scope; --s
|
|
|
203
203
|
prim hermes install # Install Hermes Agent hooks (global ~/.hermes/config.yaml)
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
+
For `$write-gameplan` or `$write-workstream` authoring in Codex, install the
|
|
207
|
+
separate Primitive planning companion from the target repository after the
|
|
208
|
+
public `$write-gameplan` skill is available:
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
prim setup --agent codex
|
|
212
|
+
prim gameplan install --agent codex
|
|
213
|
+
prim gameplan status
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
`gameplan install` adds the companion to this repository and a prompt dispatcher
|
|
217
|
+
to the user's Codex hooks, including linked worktrees. It does not replace the
|
|
218
|
+
public planning skill or install Conflict Gates; `setup` handles the latter.
|
|
219
|
+
Review `/hooks` in Codex and start a fresh session. `prim gameplan uninstall`
|
|
220
|
+
removes this repository's companion and leaves the shared user hook in place;
|
|
221
|
+
pass `--remove-user-hook` only when no other repository needs it. A Decision
|
|
222
|
+
scoped only to a code path named inside a gameplan is surfaced during planning,
|
|
223
|
+
but the edit gate sees the changed gameplan path.
|
|
224
|
+
|
|
206
225
|
Passive capture is repo-scoped even when these integrations are installed at user
|
|
207
226
|
scope. `prim enable` marks the current Git repo active; `prim disable` makes it
|
|
208
227
|
inactive. A project-scoped install activates its repo automatically. In an inactive
|
|
@@ -362,6 +381,21 @@ creating it; do not guess.
|
|
|
362
381
|
own — `<child>` depends on `<parent>`. Both are idempotent and refuse any link that
|
|
363
382
|
would create a cycle (exit 2); an unresolved id exits 4.
|
|
364
383
|
|
|
384
|
+
### Bash script coverage
|
|
385
|
+
|
|
386
|
+
Conflict Gates also inspect Bash writes expressed as Python `-c`, Node.js
|
|
387
|
+
`-e`/`--eval`, or quoted stdin heredocs. Static paths passed to Python `Path`
|
|
388
|
+
writes or writable `open`, and Node.js `fs`/`fs.promises` writeFile/appendFile
|
|
389
|
+
calls, participate in the existing preflight check. Simple path variables and
|
|
390
|
+
direct import aliases are supported. Analysis never executes the script.
|
|
391
|
+
|
|
392
|
+
Script coverage remains unverified: dynamic paths, reassignment, nested scopes,
|
|
393
|
+
directory changes, wrappers, external scripts, and unsupported syntax can hide
|
|
394
|
+
other writes. Unknown commands display a “not verified” warning in active repos;
|
|
395
|
+
recognized read-only commands stay quiet. Known conflicts still prompt or block
|
|
396
|
+
according to the existing policy. Embedded parsing stops at 64 KiB, targets are
|
|
397
|
+
bounded to 32, and shell proposal truncation at 6,144 bytes is disclosed.
|
|
398
|
+
|
|
365
399
|
### Reconcile
|
|
366
400
|
|
|
367
401
|
```bash
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: primitive-gameplan
|
|
3
|
+
description: Use alongside write-gameplan and write-workstream to bring applicable Primitive Decisions into architectural discussion, review plans for unsupported architectural additions, and preserve engineer-approved resolutions. Does not replace either public skill or authorize implementation.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Primitive companion for gameplanning
|
|
7
|
+
|
|
8
|
+
Use the public planning skill unchanged. This companion adds decision context to
|
|
9
|
+
its existing architecture discussion and final review; it adds no schema fields
|
|
10
|
+
or human checkpoints between execution patches. Codex is the primary host:
|
|
11
|
+
discover this companion under `.agents/skills`, make the public skills available
|
|
12
|
+
to Codex, and use `$write-gameplan` or `$write-workstream`.
|
|
13
|
+
`prim gameplan install --agent codex` places a dispatcher
|
|
14
|
+
in the user's Codex hooks to load this companion for explicit invocations;
|
|
15
|
+
`AGENTS.md` also binds implicit planning-skill use.
|
|
16
|
+
Claude retains `/write-gameplan` and `/write-workstream` through
|
|
17
|
+
[the Claude hook configuration](claude-hooks.json), merged into
|
|
18
|
+
`.claude/settings.json` without replacing existing hooks.
|
|
19
|
+
|
|
20
|
+
## Before choosing architecture
|
|
21
|
+
|
|
22
|
+
Inspect the requested behavior and its existing code path. Identify both the
|
|
23
|
+
intended code files and the plan/workstream path, even if the plan does not exist
|
|
24
|
+
yet. From the target repository root, use the installed `prim` CLI, or
|
|
25
|
+
`npx --yes @primitive.ai/prim` when there is no `prim` executable:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
prim auth status
|
|
29
|
+
prim decisions check --files "src/billing/export.ts,gameplans/billing-export.yaml"
|
|
30
|
+
prim decisions show dec_123
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Replace the example paths and ID with real ones. `check` accepts comma-separated
|
|
34
|
+
repo-relative paths and emits JSON without a `--json` flag. Check intended code
|
|
35
|
+
paths as well as the artifact: a decision scoped to billing may not govern the
|
|
36
|
+
gameplans directory. Repeat the lookup if the planned footprint expands. Use
|
|
37
|
+
`show` on decisions you will rely on to inspect their full constraints, rationale,
|
|
38
|
+
source/attribution, status, and scope. A path match alone does not settle a choice.
|
|
39
|
+
|
|
40
|
+
Give a short brief before patch decomposition: relevant settled choices,
|
|
41
|
+
explicitly rejected approaches, and remaining architectural questions. Cite the
|
|
42
|
+
Decision IDs and explain their bearing on this change. Do not invent a blanket
|
|
43
|
+
ban on validators, workers, or persistence from an example of overengineering.
|
|
44
|
+
|
|
45
|
+
An empty successful lookup means no matching decisions were returned, not that
|
|
46
|
+
the proposed architecture is approved. Nonzero exits, `unavailable`, partial or
|
|
47
|
+
truncated responses, and missing provenance mean coverage is unknown or limited.
|
|
48
|
+
Report that limitation and use available engineer-supplied constraints. Do not
|
|
49
|
+
claim Primitive clearance, start an authentication flow, enable capture, or change
|
|
50
|
+
enforcement settings merely to finish planning.
|
|
51
|
+
|
|
52
|
+
## Resolve choices in the existing format
|
|
53
|
+
|
|
54
|
+
Use `architectureDesign.decisions[].resolution` in the public gameplan schema:
|
|
55
|
+
|
|
56
|
+
- `engineer_approved`: an actual engineer answer or identifiable engineer-approved
|
|
57
|
+
source covers this exact choice and scope. Evidence includes the Primitive ID,
|
|
58
|
+
substantive constraint, source of approval, and why it applies here.
|
|
59
|
+
- `constrained`: binding requirements eliminate meaningful alternatives; explain
|
|
60
|
+
that reasoning, including the applicable Decision when one supplies a constraint.
|
|
61
|
+
- `delegated`: the engineer explicitly delegated this choice within a stated
|
|
62
|
+
scope; retain that delegation. The agent's resulting selection is not a new
|
|
63
|
+
user-originated decision without ratification.
|
|
64
|
+
- `unresolved`: the remaining choice needs an answer. Keep it in `openQuestions`
|
|
65
|
+
too, and leave dependent patches/specs provisional until it is resolved.
|
|
66
|
+
|
|
67
|
+
Keep rationale in `resolution.rationale`. A captured agent suggestion, an author
|
|
68
|
+
name, adoption status alone, an existing implementation, or a broad milestone is
|
|
69
|
+
not evidence that an engineer approved a new mechanism. For example, approval of
|
|
70
|
+
PostgreSQL does not approve another durable ledger. Conflicting or ambiguous
|
|
71
|
+
Decisions require clarification; do not silently prefer the newest one.
|
|
72
|
+
|
|
73
|
+
When alternatives remain, present the minimal approach, credible alternatives,
|
|
74
|
+
and relevant prior constraints before choosing. Ask one question at a time when
|
|
75
|
+
answers depend on each other. Do not repeat a question already answered within
|
|
76
|
+
the same scope. For a workstream, use its existing decisions/open-questions prose
|
|
77
|
+
rather than introducing gameplan fields.
|
|
78
|
+
|
|
79
|
+
## Review the complete artifact before handoff
|
|
80
|
+
|
|
81
|
+
Read the solution, operational considerations, every patch, and every formal spec
|
|
82
|
+
against the resolved architecture. Look for consequential choices introduced as
|
|
83
|
+
implementation details: another source of truth, validation boundary, state
|
|
84
|
+
lifetime, worker, scheduler, retry protocol, or cross-runtime data flow. Check
|
|
85
|
+
each against its actual evidence and scope, not merely whether an ID is present.
|
|
86
|
+
This is a semantic review by the planning agent: `prim decisions check` is a
|
|
87
|
+
lookup, not that review.
|
|
88
|
+
|
|
89
|
+
For an unsupported addition, identify the patch and concrete choice. Remove an
|
|
90
|
+
unnecessary agent-added mechanism consistently from the artifact, or present it
|
|
91
|
+
as an unresolved exception with alternatives before finalizing dependent work.
|
|
92
|
+
Discussion of a rejected option is allowed; adopting it without resolution is not.
|
|
93
|
+
Re-run the public skill's schema/spec validation after changes. A validator PASS
|
|
94
|
+
does not verify that a human approved the design.
|
|
95
|
+
|
|
96
|
+
## Preserve the answers
|
|
97
|
+
|
|
98
|
+
At a natural planning boundary, retain durable engineer resolutions in Primitive
|
|
99
|
+
when recording is authorized. Check the applicable records and
|
|
100
|
+
`prim decisions recent --limit 20` to avoid duplicates. Use the installed Prim
|
|
101
|
+
recording policy when available; a request to draft a plan alone does not grant
|
|
102
|
+
permission to publish decisions into an inactive repository.
|
|
103
|
+
|
|
104
|
+
Before a create, check `prim decisions create --help`. Record the exact approved
|
|
105
|
+
choice with `--intent`, `--attribution user`, source-grounded `--rationale`, one
|
|
106
|
+
`--decided` per binding constraint, and `--alternatives` only for options actually
|
|
107
|
+
rejected. An agent selection under delegation uses `--attribution agent` unless
|
|
108
|
+
the engineer ratifies that selection. Missing rationale is a question, not a
|
|
109
|
+
license to invent one. Apply only the approved scope using `--files`,
|
|
110
|
+
`--scope-dir`, or `--scope-repo`; include the plan path when that scope governs it.
|
|
111
|
+
Do not silently broaden a code-only constraint to all plans or the whole repo.
|
|
112
|
+
|
|
113
|
+
Honor existing recording authorization without asking again. For an inactive repo,
|
|
114
|
+
Prim's global `--yes` is appropriate only for a specifically authorized create;
|
|
115
|
+
npm's `npx --yes` is merely package-install confirmation. Preserve returned IDs
|
|
116
|
+
in the existing evidence fields. Report failed writes as unsaved, retaining the
|
|
117
|
+
approved wording in the plan for recovery; after an ambiguous failure, check for
|
|
118
|
+
an existing record before retrying. Never invent an ID or a successful save.
|
|
119
|
+
|
|
120
|
+
For parent workstreams, follow the public skill's write-back procedure: present
|
|
121
|
+
the concrete edits and obtain its required approval before applying them. Saving
|
|
122
|
+
a Primitive Decision does not authorize a workstream edit.
|
|
123
|
+
|
|
124
|
+
## Enforcement boundary
|
|
125
|
+
|
|
126
|
+
Existing Conflict Gates may inspect saved plans when enabled and in scope. This
|
|
127
|
+
companion neither installs nor enables gates. It does not guarantee detection of
|
|
128
|
+
every architectural implication or cryptographically verify approval. Chat-only
|
|
129
|
+
plans bypass file-write gates, and infrastructure failures may fail open.
|
|
130
|
+
|
|
131
|
+
If a gate flags a conflict, surface the governing Decision and resolve the
|
|
132
|
+
architectural choice with the engineer. Do not run `prim reconcile`, weaken gate
|
|
133
|
+
mode, or change/supersede the Decision merely to make the plan pass. A bypass or
|
|
134
|
+
change of constraint needs explicit engineer authorization for that action.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"hooks": {
|
|
3
|
+
"UserPromptExpansion": [
|
|
4
|
+
{
|
|
5
|
+
"matcher": "(^|:)write-(gameplan|workstream)$",
|
|
6
|
+
"hooks": [
|
|
7
|
+
{
|
|
8
|
+
"type": "command",
|
|
9
|
+
"command": "node \"$CLAUDE_PROJECT_DIR/.agents/skills/primitive-gameplan/scripts/activate.mjs\"",
|
|
10
|
+
"timeout": 5
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"PreToolUse": [
|
|
16
|
+
{
|
|
17
|
+
"matcher": "Skill",
|
|
18
|
+
"hooks": [
|
|
19
|
+
{
|
|
20
|
+
"type": "command",
|
|
21
|
+
"command": "node \"$CLAUDE_PROJECT_DIR/.agents/skills/primitive-gameplan/scripts/activate.mjs\"",
|
|
22
|
+
"timeout": 5
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
const event = JSON.parse(readFileSync(0, "utf8"));
|
|
4
|
+
let name;
|
|
5
|
+
if (event.hook_event_name === "UserPromptSubmit") {
|
|
6
|
+
name = event.prompt?.match(
|
|
7
|
+
/^\s*[$/](write-(?:gameplan|workstream))(?=\s|$)/
|
|
8
|
+
)?.[1];
|
|
9
|
+
} else if (
|
|
10
|
+
event.hook_event_name === "UserPromptExpansion" &&
|
|
11
|
+
event.expansion_type === "slash_command"
|
|
12
|
+
) {
|
|
13
|
+
name = event.command_name;
|
|
14
|
+
} else if (
|
|
15
|
+
event.hook_event_name === "PreToolUse" &&
|
|
16
|
+
event.tool_name === "Skill"
|
|
17
|
+
) {
|
|
18
|
+
name = event.tool_input?.skill;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (
|
|
22
|
+
typeof name === "string" &&
|
|
23
|
+
/^(?:[^:]+:)*write-(?:gameplan|workstream)$/.test(name)
|
|
24
|
+
) {
|
|
25
|
+
const companion = readFileSync(
|
|
26
|
+
new URL("../SKILL.md", import.meta.url),
|
|
27
|
+
"utf8"
|
|
28
|
+
);
|
|
29
|
+
process.stdout.write(
|
|
30
|
+
`${JSON.stringify({
|
|
31
|
+
hookSpecificOutput: {
|
|
32
|
+
hookEventName: event.hook_event_name,
|
|
33
|
+
additionalContext: `This repository uses the Primitive planning companion alongside ${name}. Its local skill follows:\n\n${companion}`,
|
|
34
|
+
},
|
|
35
|
+
})}\n`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
@@ -350,6 +350,213 @@ function demoteForMode(verdict, mode) {
|
|
|
350
350
|
|
|
351
351
|
// src/hooks/shell-targets.ts
|
|
352
352
|
import { parse } from "unbash";
|
|
353
|
+
|
|
354
|
+
// src/hooks/script-targets.ts
|
|
355
|
+
import { parser as javascript } from "@lezer/javascript";
|
|
356
|
+
import { parser as python } from "@lezer/python";
|
|
357
|
+
var MAX_SCRIPT_BYTES = 65536;
|
|
358
|
+
var WRITERS = /* @__PURE__ */ new Set(["writeFile", "writeFileSync", "appendFile", "appendFileSync"]);
|
|
359
|
+
var FS_MODULES = /* @__PURE__ */ new Set(["fs", "node:fs", "fs/promises", "node:fs/promises"]);
|
|
360
|
+
var IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
361
|
+
function children(node) {
|
|
362
|
+
const nodes = [];
|
|
363
|
+
for (let child = node?.firstChild; child; child = child.nextSibling) nodes.push(child);
|
|
364
|
+
return nodes;
|
|
365
|
+
}
|
|
366
|
+
function literal(text) {
|
|
367
|
+
const quote = text[0];
|
|
368
|
+
if (quote !== '"' && quote !== "'" || text.at(-1) !== quote) return;
|
|
369
|
+
let value = "";
|
|
370
|
+
for (let i = 1; i < text.length - 1; i++) {
|
|
371
|
+
const char = text[i];
|
|
372
|
+
if (char === quote || char === "\n" || char === "\r") return;
|
|
373
|
+
if (char !== "\\") {
|
|
374
|
+
value += char;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
const escaped = text[++i];
|
|
378
|
+
const simple = {
|
|
379
|
+
"\\": "\\",
|
|
380
|
+
"'": "'",
|
|
381
|
+
'"': '"',
|
|
382
|
+
n: "\n",
|
|
383
|
+
r: "\r",
|
|
384
|
+
t: " "
|
|
385
|
+
};
|
|
386
|
+
if (Object.hasOwn(simple, escaped)) value += simple[escaped];
|
|
387
|
+
else if (escaped === "u" || escaped === "x") {
|
|
388
|
+
const length = escaped === "u" ? 4 : 2;
|
|
389
|
+
const digits = text.slice(i + 1, i + 1 + length);
|
|
390
|
+
if (digits.length !== length || !/^[\da-f]+$/i.test(digits)) return;
|
|
391
|
+
value += String.fromCharCode(Number.parseInt(digits, 16));
|
|
392
|
+
i += length;
|
|
393
|
+
} else return;
|
|
394
|
+
}
|
|
395
|
+
return value;
|
|
396
|
+
}
|
|
397
|
+
function scriptWriteTargets(source, language) {
|
|
398
|
+
if (Buffer.byteLength(source) > MAX_SCRIPT_BYTES) return [];
|
|
399
|
+
try {
|
|
400
|
+
const tree = (language === "python" ? python : javascript).parse(source);
|
|
401
|
+
const cursor = tree.cursor();
|
|
402
|
+
let nodes = 0;
|
|
403
|
+
do {
|
|
404
|
+
if (++nodes > 2e4 || cursor.type.isError) return [];
|
|
405
|
+
if (language === "javascript" && cursor.name === "VariableDefinition" && source.slice(cursor.from, cursor.to) === "require")
|
|
406
|
+
return [];
|
|
407
|
+
if (["PropertyName", "VariableName", "VariableDefinition"].includes(cursor.name) && ["chdir", "exec", "eval"].includes(source.slice(cursor.from, cursor.to)))
|
|
408
|
+
return [];
|
|
409
|
+
} while (cursor.next());
|
|
410
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
411
|
+
if (language === "python") bindings.set("open", { kind: "symbol", value: "open" });
|
|
412
|
+
else bindings.set("require", { kind: "symbol", value: "require" });
|
|
413
|
+
if (language === "javascript") {
|
|
414
|
+
for (const statement of children(tree.topNode)) {
|
|
415
|
+
if (statement.name !== "FunctionDeclaration") continue;
|
|
416
|
+
const name = statement.getChild("VariableDefinition");
|
|
417
|
+
if (name) bindings.set(source.slice(name.from, name.to), void 0);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
const paths = /* @__PURE__ */ new Set();
|
|
421
|
+
const text = (node) => source.slice(node.from, node.to);
|
|
422
|
+
const symbol = (value) => ({ kind: "symbol", value });
|
|
423
|
+
const bind = (name, value) => {
|
|
424
|
+
bindings.set(name, bindings.has(name) ? void 0 : value);
|
|
425
|
+
};
|
|
426
|
+
const forget = () => {
|
|
427
|
+
for (const name of bindings.keys()) bindings.set(name, void 0);
|
|
428
|
+
};
|
|
429
|
+
const args = (node) => children(node.getChild("ArgList")).filter((child) => !["(", ")", ","].includes(child.name));
|
|
430
|
+
const resolveValue = (node) => {
|
|
431
|
+
if (!node) return;
|
|
432
|
+
if (node.name === "String") {
|
|
433
|
+
const value = literal(text(node));
|
|
434
|
+
return value === void 0 ? void 0 : { kind: "string", value };
|
|
435
|
+
}
|
|
436
|
+
if (node.name === "VariableName") return bindings.get(text(node));
|
|
437
|
+
if (node.name === "MemberExpression") {
|
|
438
|
+
const [base, dot, property] = children(node);
|
|
439
|
+
if (dot?.name !== "." || property?.name !== "PropertyName") return;
|
|
440
|
+
const owner = resolveValue(base);
|
|
441
|
+
const name = text(property);
|
|
442
|
+
if (owner?.kind === "symbol") {
|
|
443
|
+
if (owner.value === "pathlib" && name === "Path") return symbol("Path");
|
|
444
|
+
if (owner.value === "fs" && name === "promises") return symbol("fs");
|
|
445
|
+
if (owner.value === "fs" && WRITERS.has(name)) return symbol("write");
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (node.name === "CallExpression") {
|
|
449
|
+
const callee = resolveValue(node.firstChild ?? void 0);
|
|
450
|
+
const parameters = args(node);
|
|
451
|
+
const first = resolveValue(parameters[0]);
|
|
452
|
+
if (callee?.kind === "symbol" && first?.kind === "string" && parameters.length === 1) {
|
|
453
|
+
if (callee.value === "Path") return { kind: "path", value: first.value };
|
|
454
|
+
if (callee.value === "require" && FS_MODULES.has(first.value)) return symbol("fs");
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
const inspect = (node) => {
|
|
459
|
+
if (/Function|Class|Lambda|ArrowFunction/.test(node.name)) return;
|
|
460
|
+
if (["AssignmentExpression", "UpdateExpression", "NamedExpression"].includes(node.name) || node.name.includes("Comprehension")) {
|
|
461
|
+
forget();
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (node.name === "CallExpression") {
|
|
465
|
+
const calleeNode = node.firstChild;
|
|
466
|
+
if (!calleeNode) return;
|
|
467
|
+
const callee = resolveValue(calleeNode);
|
|
468
|
+
const parameters = args(node);
|
|
469
|
+
const first = resolveValue(parameters[0]);
|
|
470
|
+
if (callee?.kind === "symbol" && callee.value === "write" && first?.kind === "string")
|
|
471
|
+
paths.add(first.value);
|
|
472
|
+
if (callee?.kind === "symbol" && callee.value === "open") {
|
|
473
|
+
const modeAt = parameters.findIndex(
|
|
474
|
+
(p) => p.name === "VariableName" && text(p) === "mode"
|
|
475
|
+
);
|
|
476
|
+
const mode = resolveValue(modeAt < 0 ? parameters[1] : parameters[modeAt + 2]);
|
|
477
|
+
if (first && ["string", "path"].includes(first.kind) && mode?.kind === "string" && /^[rwax][bt+]*$/.test(mode.value) && /[wax+]/.test(mode.value))
|
|
478
|
+
paths.add(first.value);
|
|
479
|
+
}
|
|
480
|
+
if (calleeNode.name === "MemberExpression") {
|
|
481
|
+
const [base, dot, property] = children(calleeNode);
|
|
482
|
+
const owner = resolveValue(base);
|
|
483
|
+
if (dot?.name === "." && property && owner?.kind === "path" && ["write_text", "write_bytes"].includes(text(property)))
|
|
484
|
+
paths.add(owner.value);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
for (const child of children(node)) inspect(child);
|
|
488
|
+
};
|
|
489
|
+
const importStatement = (node) => {
|
|
490
|
+
const statement = text(node).trim().replace(/;$/, "");
|
|
491
|
+
if (language === "python") {
|
|
492
|
+
const module = /^import pathlib(?: as (\w+))?$/.exec(statement);
|
|
493
|
+
const path = /^from pathlib import Path(?: as (\w+))?$/.exec(statement);
|
|
494
|
+
if (module) bind(module[1] ?? "pathlib", symbol("pathlib"));
|
|
495
|
+
else if (path) bind(path[1] ?? "Path", symbol("Path"));
|
|
496
|
+
else forget();
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
const sourceNode = node.getChild("String");
|
|
500
|
+
if (!sourceNode || !FS_MODULES.has(literal(text(sourceNode)) ?? "")) {
|
|
501
|
+
forget();
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const group = node.getChild("ImportGroup");
|
|
505
|
+
if (group) {
|
|
506
|
+
for (const entry of text(group).slice(1, -1).split(",")) {
|
|
507
|
+
const match = /^\s*(\w+)(?:\s+as\s+(\w+))?\s*$/.exec(entry);
|
|
508
|
+
if (match)
|
|
509
|
+
bind(match[2] ?? match[1], WRITERS.has(match[1]) ? symbol("write") : void 0);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
const name = node.getChild("VariableDefinition");
|
|
513
|
+
if (name) bind(text(name), symbol("fs"));
|
|
514
|
+
};
|
|
515
|
+
const statements = children(tree.topNode).flatMap(
|
|
516
|
+
(statement) => statement.name === "StatementGroup" ? children(statement) : [statement]
|
|
517
|
+
);
|
|
518
|
+
for (const statement of statements) {
|
|
519
|
+
if (["Comment", "LineComment", "BlockComment", ";"].includes(statement.name)) continue;
|
|
520
|
+
if (["ImportStatement", "ImportDeclaration"].includes(statement.name)) {
|
|
521
|
+
importStatement(statement);
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
if (statement.name === "AssignStatement" || statement.name === "VariableDeclaration") {
|
|
525
|
+
const parts = children(statement).filter(
|
|
526
|
+
(p) => !["const", "let", "var", ";"].includes(p.name)
|
|
527
|
+
);
|
|
528
|
+
const [target, operator, expression] = parts;
|
|
529
|
+
if (parts.length !== 3 || !target || !operator || text(operator) !== "=") {
|
|
530
|
+
forget();
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
inspect(expression);
|
|
534
|
+
if (["VariableName", "VariableDefinition"].includes(target.name) && IDENTIFIER.test(text(target)))
|
|
535
|
+
bind(text(target), resolveValue(expression));
|
|
536
|
+
else if (target.name === "ObjectPattern" && resolveValue(expression)?.kind === "symbol" && resolveValue(expression)?.value === "fs") {
|
|
537
|
+
for (const entry of text(target).slice(1, -1).split(",")) {
|
|
538
|
+
const match = /^\s*(\w+)(?:\s*:\s*(\w+))?\s*$/.exec(entry);
|
|
539
|
+
if (match)
|
|
540
|
+
bind(match[2] ?? match[1], WRITERS.has(match[1]) ? symbol("write") : void 0);
|
|
541
|
+
else forget();
|
|
542
|
+
}
|
|
543
|
+
} else forget();
|
|
544
|
+
} else if (statement.name === "ExpressionStatement") inspect(statement);
|
|
545
|
+
else if (statement.name === "WithStatement") {
|
|
546
|
+
for (const child of children(statement)) {
|
|
547
|
+
if (child.name === "as") forget();
|
|
548
|
+
if (child.name === "CallExpression") inspect(child);
|
|
549
|
+
}
|
|
550
|
+
forget();
|
|
551
|
+
} else forget();
|
|
552
|
+
}
|
|
553
|
+
return [...paths];
|
|
554
|
+
} catch {
|
|
555
|
+
return [];
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// src/hooks/shell-targets.ts
|
|
353
560
|
var OUTPUT_REDIRECTS = /* @__PURE__ */ new Set([">", ">>", ">|", "&>", "&>>"]);
|
|
354
561
|
var READ_ONLY_COMMANDS = new Set(": cat echo false printf pwd test true [".split(" "));
|
|
355
562
|
var SINKS = /* @__PURE__ */ new Set(["/dev/null", "/dev/stdout", "/dev/stderr"]);
|
|
@@ -865,6 +1072,39 @@ function operands(words, short = "", long = /* @__PURE__ */ new Set()) {
|
|
|
865
1072
|
}
|
|
866
1073
|
return result;
|
|
867
1074
|
}
|
|
1075
|
+
function embeddedScript(command, name, state) {
|
|
1076
|
+
unknown(state);
|
|
1077
|
+
if (state.cwdUncertain || command.prefix.length) return;
|
|
1078
|
+
const words = command.suffix.map(
|
|
1079
|
+
(word) => (word.parts ? word.parts.every(staticPart) : staticWord(word) !== void 0) && !word.value.includes("\0") ? word.value : void 0
|
|
1080
|
+
);
|
|
1081
|
+
if (words.some((word) => word === void 0)) return;
|
|
1082
|
+
const args = words;
|
|
1083
|
+
const language = name === "node" ? "javascript" : "python";
|
|
1084
|
+
if (language === "javascript" && /^--input-type=(module|commonjs)$/.test(args[0] ?? ""))
|
|
1085
|
+
args.shift();
|
|
1086
|
+
const inlineFlag = language === "python" ? ["-c"] : ["-e", "--eval"];
|
|
1087
|
+
let source;
|
|
1088
|
+
if (args.length === 2 && inlineFlag.includes(args[0])) source = args[1];
|
|
1089
|
+
else if (args.length === 0 || args.length === 1 && args[0] === "-") {
|
|
1090
|
+
const input = command.redirects.filter((redirect) => redirect.operator.startsWith("<"));
|
|
1091
|
+
const heredoc = input[0];
|
|
1092
|
+
if (input.length === 1 && heredoc.operator === "<<" && heredoc.heredocQuoted === true)
|
|
1093
|
+
source = heredoc.content;
|
|
1094
|
+
}
|
|
1095
|
+
if (source !== void 0)
|
|
1096
|
+
for (const path of scriptWriteTargets(source, language)) state.paths.add(path);
|
|
1097
|
+
}
|
|
1098
|
+
function changesDirectory(value) {
|
|
1099
|
+
if (Array.isArray(value)) return value.some(changesDirectory);
|
|
1100
|
+
if (!value || typeof value !== "object") return false;
|
|
1101
|
+
const node = value;
|
|
1102
|
+
if (node.type === "Command" && ["cd", "pushd", "popd", "eval", "source", "."].includes(
|
|
1103
|
+
staticWord(value.name) ?? ""
|
|
1104
|
+
))
|
|
1105
|
+
return true;
|
|
1106
|
+
return Object.values(node).some(changesDirectory);
|
|
1107
|
+
}
|
|
868
1108
|
function visitCommand(command, state) {
|
|
869
1109
|
const name = staticWord(command.name);
|
|
870
1110
|
const literalApplyPatch = name === "apply_patch" && !command.redirects.some((redirect) => redirect.operator === "<<-");
|
|
@@ -873,7 +1113,9 @@ function visitCommand(command, state) {
|
|
|
873
1113
|
} else {
|
|
874
1114
|
for (const redirect of command.redirects) visitRedirect(redirect, state);
|
|
875
1115
|
}
|
|
876
|
-
if (
|
|
1116
|
+
if (name && (name === "node" || /^python(?:3(?:\.\d+)?)?$/.test(name))) {
|
|
1117
|
+
embeddedScript(command, name, state);
|
|
1118
|
+
} else if (!name || name.includes("/") || !literalApplyPatch && command.suffix.some((word) => !staticWord(word))) {
|
|
877
1119
|
unknown(state);
|
|
878
1120
|
} else if (literalApplyPatch) {
|
|
879
1121
|
} else if (!READ_ONLY_COMMANDS.has(name) && !knownNoWorktreeMutation(name, staticArgs(command.suffix))) {
|
|
@@ -932,7 +1174,8 @@ function analyzeShellTargets(source) {
|
|
|
932
1174
|
paths: /* @__PURE__ */ new Set(),
|
|
933
1175
|
complete: !script.errors?.length,
|
|
934
1176
|
mutation: false,
|
|
935
|
-
definiteEdit: false
|
|
1177
|
+
definiteEdit: false,
|
|
1178
|
+
cwdUncertain: changesDirectory(script)
|
|
936
1179
|
};
|
|
937
1180
|
for (const statement of script.commands) visitStatement(statement, state);
|
|
938
1181
|
if (script.errors?.length) state.mutation = true;
|