@modootoday/pmcp 0.22.7 → 0.22.9
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/.agents/plugins/marketplace.json +1 -1
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.mcp.json +1 -1
- package/dist/{apply-UYCLP5AL.js → apply-W5ZX5D5X.js} +10 -10
- package/dist/{chunk-6FFGGATI.js → chunk-2M3ECRPV.js} +5 -5
- package/dist/{chunk-XERIEIQ3.js → chunk-37Q7OYQX.js} +12 -12
- package/dist/{chunk-667C72QC.js → chunk-3TXTIBGH.js} +3 -3
- package/dist/{chunk-2R3FVI6M.js → chunk-4K6DXQOQ.js} +2 -2
- package/dist/{chunk-5NPDVKX5.js → chunk-65TTHA3K.js} +5 -5
- package/dist/{chunk-DYAOLBWZ.js → chunk-7GD2RXEX.js} +1081 -328
- package/dist/{chunk-GTZ5R2ER.js → chunk-ANFQ3A24.js} +2 -2
- package/dist/{chunk-7DYWCQVB.js → chunk-ARXCYFFZ.js} +1 -1
- package/dist/{chunk-NK5CAYET.js → chunk-D2P5GPVS.js} +2 -2
- package/dist/{chunk-565QGUAV.js → chunk-GHLTACBM.js} +2 -2
- package/dist/{chunk-4GBK5M5C.js → chunk-IRS5ZBET.js} +2 -2
- package/dist/{chunk-NVNC3JY5.js → chunk-JFXYSTPA.js} +3 -3
- package/dist/{chunk-CILQCERV.js → chunk-KPKMEUUH.js} +2 -2
- package/dist/{chunk-CBGY3ELI.js → chunk-L5S342S2.js} +2 -2
- package/dist/{chunk-QRTA5NGD.js → chunk-MDNLT3YR.js} +3 -3
- package/dist/chunk-NKR54YAW.js +7 -0
- package/dist/{chunk-JX5G2USM.js → chunk-SXOTPXCE.js} +3 -3
- package/dist/{chunk-CFSYEB4U.js → chunk-UYBWUOLR.js} +15 -2
- package/dist/{chunk-EKW2TQA3.js → chunk-YADCUUD4.js} +2 -2
- package/dist/{chunk-AOWWLVFL.js → chunk-ZPG5UPKH.js} +5 -5
- package/dist/{chunk-5EZKGPWY.js → chunk-ZRDQHCXB.js} +2 -2
- package/dist/{chunk-HABVHASY.js → chunk-ZUD2Q4SN.js} +2 -2
- package/dist/{cli-DKVUBOMX.js → cli-ZTYST4U5.js} +4 -4
- package/dist/cli.js +3 -3
- package/dist/{completion-5WIKJUXU.js → completion-P4Q5WEGQ.js} +2 -2
- package/dist/{diagnostics-6EKKP4FS.js → diagnostics-EENJZXVK.js} +5 -5
- package/dist/{doctor-XOUT6HJU.js → doctor-Y6N7JLTY.js} +11 -11
- package/dist/{entry-7THS4LUN.js → entry-3QQHN3DS.js} +9 -9
- package/dist/{exec-mcp-V4VMYZBN.js → exec-mcp-MSKMOFFR.js} +7 -7
- package/dist/{harness-3DFGRL57.js → harness-QMFQR45M.js} +3 -3
- package/dist/harness-launch-gate.js +3 -3
- package/dist/harness-worker.js +5 -5
- package/dist/hook-body.js +1265 -475
- package/dist/{hooks-QGH3RRZH.js → hooks-OJX3NOA5.js} +3 -3
- package/dist/{host-64MTQRJZ.js → host-I5LBMDRG.js} +6 -6
- package/dist/{host-AWCM6O5R.js → host-XKEUZRJK.js} +9 -9
- package/dist/{host-run-NCHMUCWE.js → host-run-NID7HYEA.js} +4 -4
- package/dist/http.js +1 -1
- package/dist/{import-mcp-WFDAOCTO.js → import-mcp-BEVPZARX.js} +6 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +10 -10
- package/dist/{init-GXSUWZC7.js → init-RRF5WOZO.js} +2 -2
- package/dist/{invoke-5FOYWZKY.js → invoke-FOEHGZCL.js} +7 -7
- package/dist/{list-FOVH6DDM.js → list-HPICQV6B.js} +8 -8
- package/dist/{logout-BV4Z36EL.js → logout-CLP7DUYF.js} +7 -7
- package/dist/{mainline-VOEDBDZE.js → mainline-SQSAUOMQ.js} +5 -3
- package/dist/{mcp-VQGNYSEO.js → mcp-AKFNEMLA.js} +2 -2
- package/dist/{project-4YGFV3OU.js → project-NKQKVT7K.js} +4 -4
- package/dist/{run-JP4CIL5Z.js → run-AV2IEFAH.js} +4 -4
- package/dist/{run-JLZ5SCX6.js → run-TCQ7QY63.js} +6 -6
- package/dist/{serve-RYHXWUTT.js → serve-TBB3GQHM.js} +3 -3
- package/dist/{smoke-UA7VVDKD.js → smoke-2FI5D74B.js} +3 -3
- package/dist/{start-login-LXP4FVGX.js → start-login-ZIIZUDN2.js} +7 -7
- package/dist/{status-6TXNV2GA.js → status-AVMKB63P.js} +7 -7
- package/dist/{status-EBD7PGZZ.js → status-K5RQYXPU.js} +8 -8
- package/dist/stdio.js +2 -2
- package/dist/{test-56CNS2LD.js → test-NDE4VQ4N.js} +30 -22
- package/dist/{tui-6JJOARKG.js → tui-FFZEB25B.js} +4 -4
- package/dist/{worker-A3ZT5TTF.js → worker-EEOWWPAM.js} +7 -7
- package/dist/{worktree-sync-JS7ZSCKX.js → worktree-sync-DGNHSQ64.js} +2 -2
- package/docs/hooks.md +13 -3
- package/docs/upgrading.md +8 -0
- package/gemini-extension.json +2 -2
- package/mcp.json +1 -1
- package/package.json +5 -5
- package/plugin.json +1 -1
- package/plugins/pmcp/.claude-plugin/plugin.json +1 -1
- package/plugins/pmcp/.mcp.json +1 -1
- package/plugins/pmcp/gemini-extension.json +2 -2
- package/plugins/pmcp/mcp.json +1 -1
- package/plugins/pmcp/plugin.json +1 -1
- package/dist/chunk-7GL2BXSP.js +0 -7
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
} from "./chunk-UKR64U7J.js";
|
|
4
4
|
import {
|
|
5
5
|
syncWorktree
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-UYBWUOLR.js";
|
|
7
7
|
import {
|
|
8
8
|
localCli,
|
|
9
9
|
pmcpCommand,
|
|
@@ -58,7 +58,7 @@ import {
|
|
|
58
58
|
RUNTIME_IDS,
|
|
59
59
|
climbCommand,
|
|
60
60
|
readTestTable
|
|
61
|
-
} from "./chunk-
|
|
61
|
+
} from "./chunk-ARXCYFFZ.js";
|
|
62
62
|
import {
|
|
63
63
|
TOOLS
|
|
64
64
|
} from "./chunk-V4NPDK6X.js";
|
|
@@ -68,7 +68,7 @@ import {
|
|
|
68
68
|
readHost,
|
|
69
69
|
readLock,
|
|
70
70
|
readLockFile
|
|
71
|
-
} from "./chunk-
|
|
71
|
+
} from "./chunk-4K6DXQOQ.js";
|
|
72
72
|
import {
|
|
73
73
|
ProjectError,
|
|
74
74
|
RULES_DIR,
|
|
@@ -1108,22 +1108,22 @@ function deleteKey(object4, key) {
|
|
|
1108
1108
|
delete object4[head];
|
|
1109
1109
|
return;
|
|
1110
1110
|
}
|
|
1111
|
-
const
|
|
1112
|
-
if (
|
|
1113
|
-
deleteKey(
|
|
1114
|
-
if (Object.keys(
|
|
1111
|
+
const child2 = object4[head];
|
|
1112
|
+
if (child2 === null || typeof child2 !== "object") return;
|
|
1113
|
+
deleteKey(child2, rest);
|
|
1114
|
+
if (Object.keys(child2).length === 0) delete object4[head];
|
|
1115
1115
|
}
|
|
1116
1116
|
var same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
1117
1117
|
var block = (content) => `${BEGIN}
|
|
1118
1118
|
${content}${END}
|
|
1119
1119
|
`;
|
|
1120
1120
|
function withoutBlock(text) {
|
|
1121
|
-
const
|
|
1122
|
-
if (
|
|
1123
|
-
const end = text.indexOf(END,
|
|
1121
|
+
const start2 = text.indexOf(BEGIN);
|
|
1122
|
+
if (start2 === -1) return text;
|
|
1123
|
+
const end = text.indexOf(END, start2);
|
|
1124
1124
|
if (end === -1) return text;
|
|
1125
1125
|
const after = text.slice(end + END.length).replace(/^\n/u, "");
|
|
1126
|
-
return `${text.slice(0,
|
|
1126
|
+
return `${text.slice(0, start2).replace(/\n+$/u, "\n")}${after}`;
|
|
1127
1127
|
}
|
|
1128
1128
|
|
|
1129
1129
|
// src/project/permissions/types.ts
|
|
@@ -2023,151 +2023,845 @@ function readMcp(path, value) {
|
|
|
2023
2023
|
// src/hooks/policies/git/forced-branch-move.ts
|
|
2024
2024
|
import { resolve as resolve3 } from "path";
|
|
2025
2025
|
|
|
2026
|
-
// src/hooks/shell/
|
|
2027
|
-
|
|
2026
|
+
// src/hooks/shell/commands.ts
|
|
2027
|
+
import { homedir } from "os";
|
|
2028
|
+
import { isAbsolute as isAbsolute4, join as join8 } from "path";
|
|
2029
|
+
|
|
2030
|
+
// src/hooks/shell/parse/scanner.ts
|
|
2031
|
+
var ShellSyntaxError = class extends Error {
|
|
2032
|
+
constructor(message, at) {
|
|
2033
|
+
super(`${message} at offset ${at}`);
|
|
2034
|
+
this.at = at;
|
|
2035
|
+
}
|
|
2036
|
+
at;
|
|
2037
|
+
};
|
|
2038
|
+
var OPERATORS = [
|
|
2039
|
+
";;&",
|
|
2028
2040
|
"&&",
|
|
2029
2041
|
"||",
|
|
2042
|
+
";;",
|
|
2043
|
+
";&",
|
|
2044
|
+
"|&",
|
|
2030
2045
|
";",
|
|
2031
|
-
"|",
|
|
2032
2046
|
"&",
|
|
2033
|
-
"
|
|
2047
|
+
"|",
|
|
2034
2048
|
"(",
|
|
2035
2049
|
")",
|
|
2036
|
-
"
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
}
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2050
|
+
"\n"
|
|
2051
|
+
];
|
|
2052
|
+
var REDIRECTS = [
|
|
2053
|
+
"&>>",
|
|
2054
|
+
"<<<",
|
|
2055
|
+
"<<-",
|
|
2056
|
+
"&>",
|
|
2057
|
+
">>",
|
|
2058
|
+
">|",
|
|
2059
|
+
"<>",
|
|
2060
|
+
"<<",
|
|
2061
|
+
">&",
|
|
2062
|
+
"<&",
|
|
2063
|
+
">",
|
|
2064
|
+
"<"
|
|
2065
|
+
];
|
|
2066
|
+
var BREAKS = /* @__PURE__ */ new Set([" ", " ", "\n", ";", "&", "|", "<", ">", "(", ")"]);
|
|
2067
|
+
var TEST_OPERATORS = ["&&", "||", "(", ")", "<", ">"];
|
|
2068
|
+
var ARRAY_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*(?:\[[^\]]*\])?\+?=$/u;
|
|
2069
|
+
var PARAM = /\$(?:([A-Za-z_][A-Za-z0-9_]*)|\{([A-Za-z_][A-Za-z0-9_]*)\})/uy;
|
|
2070
|
+
var ANSI_ESCAPES = {
|
|
2071
|
+
n: "\n",
|
|
2072
|
+
t: " ",
|
|
2073
|
+
r: "\r",
|
|
2074
|
+
a: "\x07",
|
|
2075
|
+
b: "\b",
|
|
2076
|
+
e: "\x1B",
|
|
2077
|
+
E: "\x1B",
|
|
2078
|
+
f: "\f",
|
|
2079
|
+
v: "\v"
|
|
2080
|
+
};
|
|
2081
|
+
var Scanner = class _Scanner {
|
|
2082
|
+
constructor(text, pos, nested) {
|
|
2083
|
+
this.text = text;
|
|
2084
|
+
this.pos = pos;
|
|
2085
|
+
this.nested = nested;
|
|
2086
|
+
}
|
|
2087
|
+
text;
|
|
2088
|
+
pos;
|
|
2089
|
+
nested;
|
|
2090
|
+
pending = [];
|
|
2091
|
+
token() {
|
|
2092
|
+
this.blank();
|
|
2093
|
+
const { text } = this;
|
|
2094
|
+
const start2 = this.pos;
|
|
2095
|
+
if (start2 >= text.length) return { kind: "eof", start: start2 };
|
|
2096
|
+
if (text[start2] === "\n") {
|
|
2097
|
+
this.pos += 1;
|
|
2098
|
+
this.heredocs();
|
|
2099
|
+
return { kind: "op", op: "\n", start: start2 };
|
|
2100
|
+
}
|
|
2101
|
+
if (!text.startsWith("<(", start2) && !text.startsWith(">(", start2)) {
|
|
2102
|
+
const fd = /^\d+(?=[<>])/u.exec(text.slice(start2, start2 + 12))?.[0] ?? "";
|
|
2103
|
+
const at = start2 + fd.length;
|
|
2104
|
+
const redirect = REDIRECTS.find((op2) => text.startsWith(op2, at));
|
|
2105
|
+
if (redirect) {
|
|
2106
|
+
this.pos = at + redirect.length;
|
|
2107
|
+
return { kind: "redirect", op: redirect, fd, start: start2 };
|
|
2108
|
+
}
|
|
2109
|
+
const op = OPERATORS.find(
|
|
2110
|
+
(candidate) => text.startsWith(candidate, start2)
|
|
2111
|
+
);
|
|
2112
|
+
if (op) {
|
|
2113
|
+
this.pos += op.length;
|
|
2114
|
+
return { kind: "op", op, start: start2 };
|
|
2115
|
+
}
|
|
2069
2116
|
}
|
|
2070
|
-
|
|
2071
|
-
if (
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
if (
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
};
|
|
2086
|
-
for (let index = 0; index < script.length; index += 1) {
|
|
2087
|
-
const char = script[index];
|
|
2088
|
-
const pair = script.slice(index, index + 2);
|
|
2089
|
-
if (char === "'") {
|
|
2090
|
-
const end = script.indexOf("'", index + 1);
|
|
2091
|
-
if (end < 0) return null;
|
|
2092
|
-
word += script.slice(index + 1, end);
|
|
2093
|
-
started = true;
|
|
2094
|
-
index = end;
|
|
2095
|
-
continue;
|
|
2117
|
+
const word = this.word();
|
|
2118
|
+
if (this.pos === start2)
|
|
2119
|
+
throw new ShellSyntaxError(`unexpected ${text[start2]}`, start2);
|
|
2120
|
+
return { kind: "word", word, raw: text.slice(start2, this.pos), start: start2 };
|
|
2121
|
+
}
|
|
2122
|
+
// A word inside [[ ]], where < > ( ) && || belong to the test; null at the closing ]].
|
|
2123
|
+
testWord(regex) {
|
|
2124
|
+
const { text } = this;
|
|
2125
|
+
while (" \n".includes(text[this.pos] ?? "x") || text.startsWith("\\\n", this.pos))
|
|
2126
|
+
this.pos += text[this.pos] === "\\" ? 2 : 1;
|
|
2127
|
+
const start2 = this.pos;
|
|
2128
|
+
if (start2 >= text.length) throw new ShellSyntaxError("unclosed [[", start2);
|
|
2129
|
+
if (text.startsWith("]]", start2) && this.ends(start2 + 2)) {
|
|
2130
|
+
this.pos += 2;
|
|
2131
|
+
return null;
|
|
2096
2132
|
}
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2133
|
+
const op = TEST_OPERATORS.find(
|
|
2134
|
+
(candidate) => text.startsWith(candidate, start2)
|
|
2135
|
+
);
|
|
2136
|
+
if (op && !regex) {
|
|
2137
|
+
this.pos += op.length;
|
|
2138
|
+
return { word: { text: op, nested: [], params: [] }, raw: op };
|
|
2139
|
+
}
|
|
2140
|
+
const word = this.word(regex);
|
|
2141
|
+
if (this.pos === start2)
|
|
2142
|
+
throw new ShellSyntaxError(`unexpected ${text[start2]}`, start2);
|
|
2143
|
+
return { word, raw: text.slice(start2, this.pos) };
|
|
2144
|
+
}
|
|
2145
|
+
// The (( ... )) starting at pos as one word, or null when the parentheses close as a subshell.
|
|
2146
|
+
arithmetic(pos) {
|
|
2147
|
+
const nested = [];
|
|
2148
|
+
const end = this.arithmeticEnd(pos, nested);
|
|
2149
|
+
if (end < 0) return null;
|
|
2150
|
+
this.pos = end;
|
|
2151
|
+
return { text: this.text.slice(pos + 2, end - 2), nested, params: [] };
|
|
2152
|
+
}
|
|
2153
|
+
heredoc(redirect, delimiter, raw, strip) {
|
|
2154
|
+
this.pending.push({
|
|
2155
|
+
redirect,
|
|
2156
|
+
delimiter: delimiter.text,
|
|
2157
|
+
strip,
|
|
2158
|
+
quoted: /['"\\]/u.test(raw)
|
|
2159
|
+
});
|
|
2160
|
+
}
|
|
2161
|
+
ends(at) {
|
|
2162
|
+
const char = this.text[at];
|
|
2163
|
+
return char === void 0 || BREAKS.has(char);
|
|
2164
|
+
}
|
|
2165
|
+
blank() {
|
|
2166
|
+
const { text } = this;
|
|
2167
|
+
for (; ; ) {
|
|
2168
|
+
const char = text[this.pos];
|
|
2169
|
+
if (char === " " || char === " ") this.pos += 1;
|
|
2170
|
+
else if (text.startsWith("\\\n", this.pos)) this.pos += 2;
|
|
2171
|
+
else if (char === "#") {
|
|
2172
|
+
const end = text.indexOf("\n", this.pos);
|
|
2173
|
+
this.pos = end < 0 ? text.length : end;
|
|
2174
|
+
} else return;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
word(regex = false) {
|
|
2178
|
+
const { text } = this;
|
|
2179
|
+
const start2 = this.pos;
|
|
2180
|
+
const nested = [];
|
|
2181
|
+
const params = [];
|
|
2182
|
+
let out = "";
|
|
2183
|
+
let depth = 0;
|
|
2184
|
+
while (this.pos < text.length) {
|
|
2185
|
+
const char = text[this.pos];
|
|
2186
|
+
const next = text[this.pos + 1];
|
|
2187
|
+
if ((char === "<" || char === ">") && next === "(") {
|
|
2188
|
+
const inner = this.nested.at(text, this.pos + 2);
|
|
2189
|
+
nested.push(inner.list);
|
|
2190
|
+
this.pos = inner.end;
|
|
2191
|
+
out += "$()";
|
|
2192
|
+
continue;
|
|
2122
2193
|
}
|
|
2123
|
-
if (
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2194
|
+
if (char === "(" && !regex && ARRAY_ASSIGNMENT.test(text.slice(start2, this.pos))) {
|
|
2195
|
+
out += this.array(nested);
|
|
2196
|
+
continue;
|
|
2197
|
+
}
|
|
2198
|
+
if (regex && char === "(") depth += 1;
|
|
2199
|
+
if (regex && char === ")" && depth > 0) depth -= 1;
|
|
2200
|
+
const inRegex = regex && ("()|".includes(char) || depth > 0 && char === " ");
|
|
2201
|
+
if (BREAKS.has(char) && !inRegex) break;
|
|
2202
|
+
if (char === "\\") {
|
|
2203
|
+
if (next !== "\n") out += next ?? "";
|
|
2204
|
+
this.pos += 2;
|
|
2205
|
+
continue;
|
|
2206
|
+
}
|
|
2207
|
+
if (char === "'") {
|
|
2208
|
+
const end = text.indexOf("'", this.pos + 1);
|
|
2209
|
+
if (end < 0) throw new ShellSyntaxError("unclosed '", this.pos);
|
|
2210
|
+
out += text.slice(this.pos + 1, end);
|
|
2211
|
+
this.pos = end + 1;
|
|
2212
|
+
continue;
|
|
2213
|
+
}
|
|
2214
|
+
if (char === "$" && next === "'") {
|
|
2215
|
+
out += this.ansi();
|
|
2216
|
+
continue;
|
|
2217
|
+
}
|
|
2218
|
+
if (char === "$" && next === '"') {
|
|
2219
|
+
this.pos += 1;
|
|
2220
|
+
continue;
|
|
2221
|
+
}
|
|
2222
|
+
if (char === '"') {
|
|
2223
|
+
this.pos += 1;
|
|
2224
|
+
out += this.quoted('"', nested, { params, base: out.length });
|
|
2225
|
+
continue;
|
|
2226
|
+
}
|
|
2227
|
+
const expansion = this.expansion(nested, { params, base: out.length });
|
|
2228
|
+
if (expansion !== null) {
|
|
2229
|
+
out += expansion;
|
|
2230
|
+
continue;
|
|
2231
|
+
}
|
|
2232
|
+
out += char;
|
|
2233
|
+
this.pos += 1;
|
|
2234
|
+
}
|
|
2235
|
+
return { text: out, nested, params };
|
|
2236
|
+
}
|
|
2237
|
+
// Double-quoted text up to close, or to the end for a here-document body (close null).
|
|
2238
|
+
quoted(close, nested, sink) {
|
|
2239
|
+
const { text } = this;
|
|
2240
|
+
let out = "";
|
|
2241
|
+
while (this.pos < text.length) {
|
|
2242
|
+
const char = text[this.pos];
|
|
2243
|
+
if (char === close) {
|
|
2244
|
+
this.pos += 1;
|
|
2245
|
+
return out;
|
|
2246
|
+
}
|
|
2247
|
+
if (char === "\\") {
|
|
2248
|
+
const next = text[this.pos + 1] ?? "";
|
|
2249
|
+
if (next !== "\n") out += '$`"\\'.includes(next) ? next : `\\${next}`;
|
|
2250
|
+
this.pos += 2;
|
|
2251
|
+
continue;
|
|
2252
|
+
}
|
|
2253
|
+
const expansion = this.expansion(
|
|
2254
|
+
nested,
|
|
2255
|
+
sink && { params: sink.params, base: sink.base + out.length }
|
|
2256
|
+
);
|
|
2257
|
+
if (expansion !== null) {
|
|
2258
|
+
out += expansion;
|
|
2259
|
+
continue;
|
|
2260
|
+
}
|
|
2261
|
+
out += char;
|
|
2262
|
+
this.pos += 1;
|
|
2263
|
+
}
|
|
2264
|
+
if (close !== null)
|
|
2265
|
+
throw new ShellSyntaxError(`unclosed ${close}`, this.pos);
|
|
2266
|
+
return out;
|
|
2267
|
+
}
|
|
2268
|
+
expansion(nested, sink) {
|
|
2269
|
+
const { text } = this;
|
|
2270
|
+
const at = this.pos;
|
|
2271
|
+
PARAM.lastIndex = at;
|
|
2272
|
+
const param = PARAM.exec(text);
|
|
2273
|
+
if (param) {
|
|
2274
|
+
this.pos = PARAM.lastIndex;
|
|
2275
|
+
sink?.params.push({
|
|
2276
|
+
name: param[1] ?? param[2],
|
|
2277
|
+
start: sink.base,
|
|
2278
|
+
end: sink.base + param[0].length
|
|
2279
|
+
});
|
|
2280
|
+
return param[0];
|
|
2129
2281
|
}
|
|
2130
|
-
if (
|
|
2131
|
-
const
|
|
2132
|
-
if (
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
index = close;
|
|
2137
|
-
continue;
|
|
2282
|
+
if (text.startsWith("$((", at)) {
|
|
2283
|
+
const end = this.arithmeticEnd(at + 1, nested);
|
|
2284
|
+
if (end >= 0) {
|
|
2285
|
+
this.pos = end;
|
|
2286
|
+
return text.slice(at, end);
|
|
2287
|
+
}
|
|
2138
2288
|
}
|
|
2139
|
-
if (
|
|
2140
|
-
|
|
2141
|
-
|
|
2289
|
+
if (text.startsWith("$(", at)) {
|
|
2290
|
+
const inner = this.nested.at(text, at + 2);
|
|
2291
|
+
nested.push(inner.list);
|
|
2292
|
+
this.pos = inner.end;
|
|
2293
|
+
return "$()";
|
|
2294
|
+
}
|
|
2295
|
+
if (text.startsWith("${", at)) {
|
|
2296
|
+
this.pos = this.braceEnd(at + 2, nested);
|
|
2297
|
+
return text.slice(at, this.pos);
|
|
2298
|
+
}
|
|
2299
|
+
if (text[at] === "`") {
|
|
2300
|
+
nested.push(this.backtick());
|
|
2301
|
+
return "$()";
|
|
2302
|
+
}
|
|
2303
|
+
return null;
|
|
2304
|
+
}
|
|
2305
|
+
// Index just past the )) closing the (( at open, or -1 when a lone ) closes it first.
|
|
2306
|
+
arithmeticEnd(open, nested) {
|
|
2307
|
+
const { text } = this;
|
|
2308
|
+
const found = [];
|
|
2309
|
+
let depth = 0;
|
|
2310
|
+
let index = open + 2;
|
|
2311
|
+
while (index < text.length) {
|
|
2312
|
+
const char = text[index];
|
|
2313
|
+
if (char === "\\") {
|
|
2314
|
+
index += 2;
|
|
2315
|
+
continue;
|
|
2316
|
+
}
|
|
2317
|
+
if (text.startsWith("$(", index) && !text.startsWith("$((", index)) {
|
|
2318
|
+
const inner = this.nested.at(text, index + 2);
|
|
2319
|
+
found.push(inner.list);
|
|
2320
|
+
index = inner.end;
|
|
2321
|
+
continue;
|
|
2322
|
+
}
|
|
2323
|
+
if (char === "(") depth += 1;
|
|
2324
|
+
if (char === ")") {
|
|
2325
|
+
if (depth === 0) {
|
|
2326
|
+
if (text[index + 1] !== ")") return -1;
|
|
2327
|
+
nested.push(...found);
|
|
2328
|
+
return index + 2;
|
|
2329
|
+
}
|
|
2330
|
+
depth -= 1;
|
|
2331
|
+
}
|
|
2142
2332
|
index += 1;
|
|
2143
|
-
continue;
|
|
2144
2333
|
}
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2334
|
+
return -1;
|
|
2335
|
+
}
|
|
2336
|
+
// Index just past the } closing a ${ whose body starts at from.
|
|
2337
|
+
braceEnd(from, nested) {
|
|
2338
|
+
const { text } = this;
|
|
2339
|
+
let depth = 1;
|
|
2340
|
+
this.pos = from;
|
|
2341
|
+
while (this.pos < text.length) {
|
|
2342
|
+
const char = text[this.pos];
|
|
2343
|
+
if (char === "\\") {
|
|
2344
|
+
this.pos += 2;
|
|
2345
|
+
continue;
|
|
2346
|
+
}
|
|
2347
|
+
if (char === "'") {
|
|
2348
|
+
const end = text.indexOf("'", this.pos + 1);
|
|
2349
|
+
if (end < 0) throw new ShellSyntaxError("unclosed '", this.pos);
|
|
2350
|
+
this.pos = end + 1;
|
|
2351
|
+
continue;
|
|
2352
|
+
}
|
|
2353
|
+
if (char === '"') {
|
|
2354
|
+
this.pos += 1;
|
|
2355
|
+
this.quoted('"', nested);
|
|
2356
|
+
continue;
|
|
2357
|
+
}
|
|
2358
|
+
if (text.startsWith("${", this.pos)) {
|
|
2359
|
+
depth += 1;
|
|
2360
|
+
this.pos += 2;
|
|
2361
|
+
continue;
|
|
2362
|
+
}
|
|
2363
|
+
if (char === "}") {
|
|
2364
|
+
depth -= 1;
|
|
2365
|
+
this.pos += 1;
|
|
2366
|
+
if (depth === 0) return this.pos;
|
|
2367
|
+
continue;
|
|
2368
|
+
}
|
|
2369
|
+
if (this.expansion(nested) === null) this.pos += 1;
|
|
2370
|
+
}
|
|
2371
|
+
throw new ShellSyntaxError("unclosed ${", from);
|
|
2372
|
+
}
|
|
2373
|
+
backtick() {
|
|
2374
|
+
const { text } = this;
|
|
2375
|
+
let inner = "";
|
|
2376
|
+
let index = this.pos + 1;
|
|
2377
|
+
while (index < text.length && text[index] !== "`") {
|
|
2378
|
+
const next = text[index + 1] ?? "";
|
|
2379
|
+
if (text[index] === "\\" && "$`\\".includes(next)) {
|
|
2380
|
+
inner += next;
|
|
2381
|
+
index += 2;
|
|
2382
|
+
continue;
|
|
2383
|
+
}
|
|
2384
|
+
inner += text[index];
|
|
2385
|
+
index += 1;
|
|
2149
2386
|
}
|
|
2150
|
-
if (
|
|
2151
|
-
|
|
2152
|
-
|
|
2387
|
+
if (index >= text.length)
|
|
2388
|
+
throw new ShellSyntaxError("unclosed `", this.pos);
|
|
2389
|
+
this.pos = index + 1;
|
|
2390
|
+
return this.nested.whole(inner);
|
|
2391
|
+
}
|
|
2392
|
+
ansi() {
|
|
2393
|
+
const { text } = this;
|
|
2394
|
+
let out = "";
|
|
2395
|
+
let index = this.pos + 2;
|
|
2396
|
+
while (index < text.length && text[index] !== "'") {
|
|
2397
|
+
if (text[index] === "\\") {
|
|
2398
|
+
const next = text[index + 1] ?? "";
|
|
2399
|
+
out += ANSI_ESCAPES[next] ?? next;
|
|
2400
|
+
index += 2;
|
|
2401
|
+
continue;
|
|
2402
|
+
}
|
|
2403
|
+
out += text[index];
|
|
2153
2404
|
index += 1;
|
|
2154
|
-
continue;
|
|
2155
2405
|
}
|
|
2156
|
-
if (
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2406
|
+
if (index >= text.length)
|
|
2407
|
+
throw new ShellSyntaxError("unclosed $'", this.pos);
|
|
2408
|
+
this.pos = index + 1;
|
|
2409
|
+
return out;
|
|
2410
|
+
}
|
|
2411
|
+
// The (...) of name=(...), its elements as words.
|
|
2412
|
+
array(nested) {
|
|
2413
|
+
const { text } = this;
|
|
2414
|
+
const elements = [];
|
|
2415
|
+
this.pos += 1;
|
|
2416
|
+
for (; ; ) {
|
|
2417
|
+
while (" \n".includes(text[this.pos] ?? "x")) this.pos += 1;
|
|
2418
|
+
if (text[this.pos] === "#") this.blank();
|
|
2419
|
+
if (this.pos >= text.length)
|
|
2420
|
+
throw new ShellSyntaxError("unclosed (", this.pos);
|
|
2421
|
+
if (text[this.pos] === ")") {
|
|
2422
|
+
this.pos += 1;
|
|
2423
|
+
return `(${elements.join(" ")})`;
|
|
2424
|
+
}
|
|
2425
|
+
const start2 = this.pos;
|
|
2426
|
+
const element = this.word();
|
|
2427
|
+
if (this.pos === start2)
|
|
2428
|
+
throw new ShellSyntaxError(`unexpected ${text[start2]}`, start2);
|
|
2429
|
+
nested.push(...element.nested);
|
|
2430
|
+
elements.push(element.text);
|
|
2431
|
+
}
|
|
2432
|
+
}
|
|
2433
|
+
heredocs() {
|
|
2434
|
+
const { text } = this;
|
|
2435
|
+
const pending = this.pending;
|
|
2436
|
+
this.pending = [];
|
|
2437
|
+
for (const doc of pending) {
|
|
2438
|
+
let body = "";
|
|
2439
|
+
while (this.pos < text.length) {
|
|
2440
|
+
const end = text.indexOf("\n", this.pos);
|
|
2441
|
+
const stop = end < 0 ? text.length : end;
|
|
2442
|
+
const line = text.slice(this.pos, stop);
|
|
2443
|
+
this.pos = end < 0 ? stop : end + 1;
|
|
2444
|
+
if ((doc.strip ? line.replace(/^\t+/u, "") : line) === doc.delimiter)
|
|
2445
|
+
break;
|
|
2446
|
+
body += `${line}
|
|
2447
|
+
`;
|
|
2448
|
+
}
|
|
2449
|
+
doc.redirect.body = doc.quoted ? { text: body, nested: [], params: [] } : this.expanded(body);
|
|
2160
2450
|
}
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2451
|
+
}
|
|
2452
|
+
expanded(body) {
|
|
2453
|
+
const scanner = new _Scanner(body, 0, this.nested);
|
|
2454
|
+
const nested = [];
|
|
2455
|
+
return { text: scanner.quoted(null, nested), nested, params: [] };
|
|
2456
|
+
}
|
|
2457
|
+
};
|
|
2458
|
+
|
|
2459
|
+
// src/hooks/shell/parse/parser.ts
|
|
2460
|
+
var LIST_END_OPERATORS = /* @__PURE__ */ new Set([")", ";;", ";&", ";;&"]);
|
|
2461
|
+
var CASE_ENDS = /* @__PURE__ */ new Set([";;", ";&", ";;&"]);
|
|
2462
|
+
var NO_KEYWORDS = /* @__PURE__ */ new Set();
|
|
2463
|
+
var ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*(?:\[[^\]]*\])?\+?=/u;
|
|
2464
|
+
var ARRAY = /^[A-Za-z_][A-Za-z0-9_]*(?:\[[^\]]*\])?\+?=\(/u;
|
|
2465
|
+
var DECLARES = /* @__PURE__ */ new Set(["declare", "local", "export", "readonly", "typeset"]);
|
|
2466
|
+
var CONTINUATIONS = /* @__PURE__ */ new Set([
|
|
2467
|
+
"then",
|
|
2468
|
+
"elif",
|
|
2469
|
+
"else",
|
|
2470
|
+
"fi",
|
|
2471
|
+
"do",
|
|
2472
|
+
"done",
|
|
2473
|
+
"esac",
|
|
2474
|
+
"}",
|
|
2475
|
+
// ! negates a whole pipeline, so it cannot follow a |.
|
|
2476
|
+
"!"
|
|
2477
|
+
]);
|
|
2478
|
+
var NESTED = {
|
|
2479
|
+
at(text, pos) {
|
|
2480
|
+
const parser = new Parser(text, pos);
|
|
2481
|
+
const list2 = parser.list(NO_KEYWORDS);
|
|
2482
|
+
parser.op(")");
|
|
2483
|
+
return { list: list2, end: parser.position() };
|
|
2484
|
+
},
|
|
2485
|
+
whole: runnable
|
|
2486
|
+
};
|
|
2487
|
+
function runnable(text) {
|
|
2488
|
+
try {
|
|
2489
|
+
return parseScript(text);
|
|
2490
|
+
} catch (error) {
|
|
2491
|
+
if (!(error instanceof ShellSyntaxError)) throw error;
|
|
2492
|
+
const line = lineStart(text, error.at);
|
|
2493
|
+
return line === 0 ? [] : runnable(text.slice(0, line));
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
function lineStart(text, at) {
|
|
2497
|
+
let newline = text.lastIndexOf("\n", Math.min(at, text.length - 1) - 1);
|
|
2498
|
+
while (newline >= 0 && continued(text, newline)) {
|
|
2499
|
+
newline = text.lastIndexOf("\n", newline - 1);
|
|
2500
|
+
}
|
|
2501
|
+
return newline + 1;
|
|
2502
|
+
}
|
|
2503
|
+
function continued(text, newline) {
|
|
2504
|
+
let slashes = 0;
|
|
2505
|
+
while (text[newline - 1 - slashes] === "\\") slashes += 1;
|
|
2506
|
+
return slashes % 2 === 1;
|
|
2507
|
+
}
|
|
2508
|
+
function parseScript(text) {
|
|
2509
|
+
const parser = new Parser(text, 0);
|
|
2510
|
+
const list2 = parser.list(NO_KEYWORDS);
|
|
2511
|
+
parser.end();
|
|
2512
|
+
return list2;
|
|
2513
|
+
}
|
|
2514
|
+
function describe(token) {
|
|
2515
|
+
if (token.kind === "eof") return "end of input";
|
|
2516
|
+
if (token.kind === "word") return token.raw;
|
|
2517
|
+
return token.op === "\n" ? "newline" : token.op;
|
|
2518
|
+
}
|
|
2519
|
+
var Parser = class {
|
|
2520
|
+
scan;
|
|
2521
|
+
ahead;
|
|
2522
|
+
constructor(text, pos) {
|
|
2523
|
+
this.scan = new Scanner(text, pos, NESTED);
|
|
2524
|
+
}
|
|
2525
|
+
position() {
|
|
2526
|
+
return this.scan.pos;
|
|
2527
|
+
}
|
|
2528
|
+
end() {
|
|
2529
|
+
const token = this.peek();
|
|
2530
|
+
if (token.kind !== "eof") this.fail(token);
|
|
2531
|
+
}
|
|
2532
|
+
op(op) {
|
|
2533
|
+
const token = this.next();
|
|
2534
|
+
if (token.kind !== "op" || token.op !== op) this.fail(token, op);
|
|
2535
|
+
}
|
|
2536
|
+
// Items until a token that closes the enclosing construct: ), ;;, the end, or a keyword in ends.
|
|
2537
|
+
list(ends) {
|
|
2538
|
+
const items = [];
|
|
2539
|
+
for (; ; ) {
|
|
2540
|
+
this.newlines();
|
|
2541
|
+
const token = this.peek();
|
|
2542
|
+
if (token.kind === "eof") return items;
|
|
2543
|
+
if (token.kind === "op" && LIST_END_OPERATORS.has(token.op)) return items;
|
|
2544
|
+
if (token.kind === "word" && ends.has(token.raw)) return items;
|
|
2545
|
+
const { pipelines, operators } = this.andOr();
|
|
2546
|
+
const after = this.peek();
|
|
2547
|
+
const separator = after.kind === "op" && [";", "&", "\n"].includes(after.op) ? after.op : "";
|
|
2548
|
+
items.push({ pipelines, operators, background: separator === "&" });
|
|
2549
|
+
if (!separator) return items;
|
|
2550
|
+
this.next();
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
peek() {
|
|
2554
|
+
this.ahead ??= this.scan.token();
|
|
2555
|
+
return this.ahead;
|
|
2556
|
+
}
|
|
2557
|
+
next() {
|
|
2558
|
+
const token = this.peek();
|
|
2559
|
+
this.ahead = void 0;
|
|
2560
|
+
return token;
|
|
2561
|
+
}
|
|
2562
|
+
fail(token, expected) {
|
|
2563
|
+
const found = describe(token);
|
|
2564
|
+
throw new ShellSyntaxError(
|
|
2565
|
+
expected ? `expected ${expected}, found ${found}` : `unexpected ${found}`,
|
|
2566
|
+
token.start
|
|
2567
|
+
);
|
|
2568
|
+
}
|
|
2569
|
+
isWord(token, raw) {
|
|
2570
|
+
return token.kind === "word" && token.raw === raw;
|
|
2571
|
+
}
|
|
2572
|
+
keyword(raw) {
|
|
2573
|
+
const token = this.next();
|
|
2574
|
+
if (!this.isWord(token, raw)) this.fail(token, raw);
|
|
2575
|
+
}
|
|
2576
|
+
word() {
|
|
2577
|
+
const token = this.next();
|
|
2578
|
+
if (token.kind !== "word") this.fail(token, "a word");
|
|
2579
|
+
return { word: token.word, raw: token.raw };
|
|
2580
|
+
}
|
|
2581
|
+
newlines() {
|
|
2582
|
+
for (; ; ) {
|
|
2583
|
+
const token = this.peek();
|
|
2584
|
+
if (token.kind !== "op" || token.op !== "\n") return;
|
|
2585
|
+
this.next();
|
|
2586
|
+
}
|
|
2587
|
+
}
|
|
2588
|
+
andOr() {
|
|
2589
|
+
const pipelines = [this.pipeline()];
|
|
2590
|
+
const operators = [];
|
|
2591
|
+
for (; ; ) {
|
|
2592
|
+
const token = this.peek();
|
|
2593
|
+
if (token.kind !== "op" || token.op !== "&&" && token.op !== "||")
|
|
2594
|
+
return { pipelines, operators };
|
|
2595
|
+
this.next();
|
|
2596
|
+
operators.push(token.op);
|
|
2597
|
+
this.newlines();
|
|
2598
|
+
pipelines.push(this.pipeline());
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
pipeline() {
|
|
2602
|
+
let negated = false;
|
|
2603
|
+
let prefixed = false;
|
|
2604
|
+
for (; ; ) {
|
|
2605
|
+
const token = this.peek();
|
|
2606
|
+
if (this.isWord(token, "time")) {
|
|
2607
|
+
this.next();
|
|
2608
|
+
if (this.isWord(this.peek(), "-p")) this.next();
|
|
2609
|
+
} else if (this.isWord(token, "!")) {
|
|
2610
|
+
this.next();
|
|
2611
|
+
negated = !negated;
|
|
2612
|
+
} else break;
|
|
2613
|
+
prefixed = true;
|
|
2614
|
+
}
|
|
2615
|
+
const next = this.peek();
|
|
2616
|
+
const ends = next.kind === "eof" || next.kind === "op" && (next.op === ";" || next.op === "\n");
|
|
2617
|
+
if (prefixed && ends) return { negated, commands: [] };
|
|
2618
|
+
const commands = [this.command()];
|
|
2619
|
+
for (; ; ) {
|
|
2620
|
+
const token = this.peek();
|
|
2621
|
+
if (token.kind !== "op" || token.op !== "|" && token.op !== "|&")
|
|
2622
|
+
return { negated, commands };
|
|
2623
|
+
this.next();
|
|
2624
|
+
this.newlines();
|
|
2625
|
+
commands.push(this.command());
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
command() {
|
|
2629
|
+
const token = this.peek();
|
|
2630
|
+
if (token.kind === "op" && token.op === "(")
|
|
2631
|
+
return this.parenthesized(token);
|
|
2632
|
+
if (token.kind !== "word") return this.simple();
|
|
2633
|
+
switch (token.raw) {
|
|
2634
|
+
case "{": {
|
|
2635
|
+
this.next();
|
|
2636
|
+
const body = this.filled(/* @__PURE__ */ new Set(["}"]));
|
|
2637
|
+
this.keyword("}");
|
|
2638
|
+
return { kind: "group", body, redirects: this.redirects() };
|
|
2639
|
+
}
|
|
2640
|
+
case "if":
|
|
2641
|
+
return this.ifCommand();
|
|
2642
|
+
case "while":
|
|
2643
|
+
case "until":
|
|
2644
|
+
return this.loop(token.raw);
|
|
2645
|
+
case "for":
|
|
2646
|
+
case "select":
|
|
2647
|
+
return this.forCommand(token.raw);
|
|
2648
|
+
case "case":
|
|
2649
|
+
return this.caseCommand();
|
|
2650
|
+
case "[[":
|
|
2651
|
+
return this.test();
|
|
2652
|
+
case "function":
|
|
2653
|
+
return this.functionKeyword();
|
|
2654
|
+
default:
|
|
2655
|
+
if (CONTINUATIONS.has(token.raw)) this.fail(token);
|
|
2656
|
+
return this.simple();
|
|
2657
|
+
}
|
|
2658
|
+
}
|
|
2659
|
+
parenthesized(token) {
|
|
2660
|
+
this.ahead = void 0;
|
|
2661
|
+
if (this.scan.text[token.start + 1] === "(") {
|
|
2662
|
+
const word = this.scan.arithmetic(token.start);
|
|
2663
|
+
if (word)
|
|
2664
|
+
return this.closed({
|
|
2665
|
+
kind: "compound",
|
|
2666
|
+
keyword: "((",
|
|
2667
|
+
parts: [],
|
|
2668
|
+
words: [word],
|
|
2669
|
+
redirects: this.redirects()
|
|
2670
|
+
});
|
|
2671
|
+
this.scan.pos = token.start + 1;
|
|
2672
|
+
}
|
|
2673
|
+
const body = this.filled(NO_KEYWORDS);
|
|
2674
|
+
this.op(")");
|
|
2675
|
+
return { kind: "subshell", body, redirects: this.redirects() };
|
|
2676
|
+
}
|
|
2677
|
+
// The list of a construct the shell refuses to leave empty.
|
|
2678
|
+
filled(ends) {
|
|
2679
|
+
const list2 = this.list(ends);
|
|
2680
|
+
if (list2.length === 0) this.fail(this.peek());
|
|
2681
|
+
return list2;
|
|
2682
|
+
}
|
|
2683
|
+
ifCommand() {
|
|
2684
|
+
const parts = [];
|
|
2685
|
+
this.keyword("if");
|
|
2686
|
+
for (; ; ) {
|
|
2687
|
+
parts.push(this.filled(/* @__PURE__ */ new Set(["then"])));
|
|
2688
|
+
this.keyword("then");
|
|
2689
|
+
parts.push(this.filled(/* @__PURE__ */ new Set(["elif", "else", "fi"])));
|
|
2690
|
+
const token = this.next();
|
|
2691
|
+
if (this.isWord(token, "elif")) continue;
|
|
2692
|
+
if (this.isWord(token, "else")) {
|
|
2693
|
+
parts.push(this.filled(/* @__PURE__ */ new Set(["fi"])));
|
|
2694
|
+
this.keyword("fi");
|
|
2695
|
+
} else if (!this.isWord(token, "fi")) this.fail(token, "fi");
|
|
2696
|
+
return this.compound("if", parts, []);
|
|
2697
|
+
}
|
|
2698
|
+
}
|
|
2699
|
+
loop(keyword) {
|
|
2700
|
+
this.next();
|
|
2701
|
+
const condition = this.filled(/* @__PURE__ */ new Set(["do"]));
|
|
2702
|
+
return this.compound(keyword, [condition, this.doBody()], []);
|
|
2703
|
+
}
|
|
2704
|
+
doBody() {
|
|
2705
|
+
this.newlines();
|
|
2706
|
+
this.keyword("do");
|
|
2707
|
+
const body = this.filled(/* @__PURE__ */ new Set(["done"]));
|
|
2708
|
+
this.keyword("done");
|
|
2709
|
+
return body;
|
|
2710
|
+
}
|
|
2711
|
+
forCommand(keyword) {
|
|
2712
|
+
const start2 = this.next();
|
|
2713
|
+
const words = [];
|
|
2714
|
+
let variable;
|
|
2715
|
+
const token = this.peek();
|
|
2716
|
+
if (token.kind === "op" && token.op === "(" && this.scan.text[token.start + 1] === "(") {
|
|
2717
|
+
this.ahead = void 0;
|
|
2718
|
+
const header = this.scan.arithmetic(token.start);
|
|
2719
|
+
if (!header) this.fail(start2, "((");
|
|
2720
|
+
words.push(header);
|
|
2721
|
+
} else {
|
|
2722
|
+
variable = this.word().raw;
|
|
2723
|
+
this.newlines();
|
|
2724
|
+
if (this.isWord(this.peek(), "in")) {
|
|
2725
|
+
this.next();
|
|
2726
|
+
for (; ; ) {
|
|
2727
|
+
const item = this.peek();
|
|
2728
|
+
if (item.kind !== "word") break;
|
|
2729
|
+
words.push(this.word().word);
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
const separator = this.peek();
|
|
2734
|
+
if (separator.kind === "op" && (separator.op === ";" || separator.op === "\n"))
|
|
2735
|
+
this.next();
|
|
2736
|
+
return this.compound(keyword, [this.doBody()], words, variable);
|
|
2737
|
+
}
|
|
2738
|
+
caseCommand() {
|
|
2739
|
+
this.next();
|
|
2740
|
+
const words = [this.word().word];
|
|
2741
|
+
this.newlines();
|
|
2742
|
+
this.keyword("in");
|
|
2743
|
+
const parts = [];
|
|
2744
|
+
for (; ; ) {
|
|
2745
|
+
this.newlines();
|
|
2746
|
+
if (this.isWord(this.peek(), "esac")) {
|
|
2747
|
+
this.next();
|
|
2748
|
+
return this.compound("case", parts, words);
|
|
2749
|
+
}
|
|
2750
|
+
const open = this.peek();
|
|
2751
|
+
if (open.kind === "op" && open.op === "(") this.next();
|
|
2752
|
+
words.push(this.word().word);
|
|
2753
|
+
for (; ; ) {
|
|
2754
|
+
const token = this.next();
|
|
2755
|
+
if (token.kind === "op" && token.op === ")") break;
|
|
2756
|
+
if (token.kind !== "op" || token.op !== "|") this.fail(token, ")");
|
|
2757
|
+
words.push(this.word().word);
|
|
2758
|
+
}
|
|
2759
|
+
parts.push(this.list(/* @__PURE__ */ new Set(["esac"])));
|
|
2760
|
+
const end = this.peek();
|
|
2761
|
+
if (end.kind === "op" && CASE_ENDS.has(end.op)) this.next();
|
|
2762
|
+
else if (!this.isWord(end, "esac")) this.fail(end, ";;");
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
test() {
|
|
2766
|
+
this.next();
|
|
2767
|
+
const words = [];
|
|
2768
|
+
let previous = "";
|
|
2769
|
+
for (; ; ) {
|
|
2770
|
+
const item = this.scan.testWord(previous === "=~");
|
|
2771
|
+
if (!item) return this.closed(this.compound("[[", [], words));
|
|
2772
|
+
words.push(item.word);
|
|
2773
|
+
previous = item.raw;
|
|
2774
|
+
}
|
|
2775
|
+
}
|
|
2776
|
+
// The shell reads (( )) and [[ ]] like a simple command, so a word right after one is no
|
|
2777
|
+
// reserved word: "{ (( n )) }" leaves the brace group open.
|
|
2778
|
+
closed(command) {
|
|
2779
|
+
const token = this.peek();
|
|
2780
|
+
if (token.kind === "word") this.fail(token);
|
|
2781
|
+
return command;
|
|
2782
|
+
}
|
|
2783
|
+
functionKeyword() {
|
|
2784
|
+
this.next();
|
|
2785
|
+
const { raw } = this.word();
|
|
2786
|
+
const token = this.peek();
|
|
2787
|
+
if (token.kind === "op" && token.op === "(" && this.scan.text[token.start + 1] !== "(") {
|
|
2788
|
+
this.next();
|
|
2789
|
+
this.op(")");
|
|
2790
|
+
}
|
|
2791
|
+
return { kind: "function", name: raw, body: this.functionBody() };
|
|
2792
|
+
}
|
|
2793
|
+
// A function body is a compound command: { }, ( ), if, while, until, for, case, [[ ]] or (( )).
|
|
2794
|
+
functionBody() {
|
|
2795
|
+
this.newlines();
|
|
2796
|
+
const start2 = this.peek();
|
|
2797
|
+
const body = this.command();
|
|
2798
|
+
if (body.kind === "simple" || body.kind === "function") this.fail(start2);
|
|
2799
|
+
return body;
|
|
2800
|
+
}
|
|
2801
|
+
compound(keyword, parts, words, variable) {
|
|
2802
|
+
return {
|
|
2803
|
+
kind: "compound",
|
|
2804
|
+
keyword,
|
|
2805
|
+
...variable ? { variable } : {},
|
|
2806
|
+
parts,
|
|
2807
|
+
words,
|
|
2808
|
+
redirects: this.redirects()
|
|
2809
|
+
};
|
|
2810
|
+
}
|
|
2811
|
+
redirects() {
|
|
2812
|
+
const redirects = [];
|
|
2813
|
+
while (this.peek().kind === "redirect") redirects.push(this.redirect());
|
|
2814
|
+
return redirects;
|
|
2815
|
+
}
|
|
2816
|
+
redirect() {
|
|
2817
|
+
const token = this.next();
|
|
2818
|
+
if (token.kind !== "redirect") this.fail(token, "a redirection");
|
|
2819
|
+
const { word, raw } = this.word();
|
|
2820
|
+
const redirect = {
|
|
2821
|
+
op: token.op,
|
|
2822
|
+
fd: token.fd,
|
|
2823
|
+
target: word,
|
|
2824
|
+
body: void 0
|
|
2825
|
+
};
|
|
2826
|
+
if (token.op === "<<" || token.op === "<<-")
|
|
2827
|
+
this.scan.heredoc(redirect, word, raw, token.op === "<<-");
|
|
2828
|
+
return redirect;
|
|
2829
|
+
}
|
|
2830
|
+
simple() {
|
|
2831
|
+
const assigns = [];
|
|
2832
|
+
const words = [];
|
|
2833
|
+
const redirects = [];
|
|
2834
|
+
let first = "";
|
|
2835
|
+
let redirectedAfterAssign = false;
|
|
2836
|
+
for (; ; ) {
|
|
2837
|
+
const token = this.peek();
|
|
2838
|
+
if (token.kind === "redirect") {
|
|
2839
|
+
redirects.push(this.redirect());
|
|
2840
|
+
if (assigns.length > 0) redirectedAfterAssign = true;
|
|
2841
|
+
continue;
|
|
2842
|
+
}
|
|
2843
|
+
if (token.kind !== "word") break;
|
|
2844
|
+
this.next();
|
|
2845
|
+
if (words.length === 0 && ASSIGNMENT.test(token.raw)) {
|
|
2846
|
+
if (redirectedAfterAssign && ARRAY.test(token.raw)) this.fail(token);
|
|
2847
|
+
assigns.push(token.word);
|
|
2848
|
+
continue;
|
|
2849
|
+
}
|
|
2850
|
+
if (ARRAY.test(token.raw) && !DECLARES.has(first)) this.fail(token);
|
|
2851
|
+
if (words.length === 0) first = token.raw;
|
|
2852
|
+
words.push(token.word);
|
|
2853
|
+
const after = this.peek();
|
|
2854
|
+
if (words.length === 1 && assigns.length === 0 && redirects.length === 0 && after.kind === "op" && after.op === "(") {
|
|
2855
|
+
this.next();
|
|
2856
|
+
this.op(")");
|
|
2857
|
+
return { kind: "function", name: token.raw, body: this.functionBody() };
|
|
2858
|
+
}
|
|
2164
2859
|
}
|
|
2165
|
-
|
|
2166
|
-
|
|
2860
|
+
if (assigns.length + words.length + redirects.length === 0)
|
|
2861
|
+
this.fail(this.peek());
|
|
2862
|
+
return { kind: "simple", assigns, words, redirects };
|
|
2167
2863
|
}
|
|
2168
|
-
|
|
2169
|
-
return { tokens, nested };
|
|
2170
|
-
}
|
|
2864
|
+
};
|
|
2171
2865
|
|
|
2172
2866
|
// src/hooks/shell/vocabulary.ts
|
|
2173
2867
|
var WRAPPERS = /* @__PURE__ */ new Set([
|
|
@@ -2247,12 +2941,13 @@ var WRANGLER_MUTATING = /* @__PURE__ */ new Set([
|
|
|
2247
2941
|
]);
|
|
2248
2942
|
|
|
2249
2943
|
// src/hooks/shell/commands.ts
|
|
2250
|
-
var
|
|
2944
|
+
var ASSIGNMENT2 = /^[A-Za-z_][A-Za-z0-9_]*=/u;
|
|
2945
|
+
var MAX_DEPTH = 4;
|
|
2251
2946
|
function unwrap(words) {
|
|
2252
2947
|
const assigned = [];
|
|
2253
2948
|
let rest = [...words];
|
|
2254
2949
|
for (; ; ) {
|
|
2255
|
-
if (rest.length > 0 &&
|
|
2950
|
+
if (rest.length > 0 && ASSIGNMENT2.test(rest[0])) {
|
|
2256
2951
|
assigned.push(rest[0].split("=")[0]);
|
|
2257
2952
|
rest = rest.slice(1);
|
|
2258
2953
|
continue;
|
|
@@ -2267,56 +2962,196 @@ function unwrap(words) {
|
|
|
2267
2962
|
return { assigned, words: rest };
|
|
2268
2963
|
}
|
|
2269
2964
|
}
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2965
|
+
var DECLARES2 = /* @__PURE__ */ new Set(["export", "declare", "local", "readonly", "typeset"]);
|
|
2966
|
+
function start() {
|
|
2967
|
+
return {
|
|
2968
|
+
dir: "",
|
|
2969
|
+
previous: "",
|
|
2970
|
+
stack: [],
|
|
2971
|
+
vars: /* @__PURE__ */ new Map([["HOME", homedir()]]),
|
|
2972
|
+
exported: /* @__PURE__ */ new Set(["HOME"])
|
|
2973
|
+
};
|
|
2974
|
+
}
|
|
2975
|
+
function fork(place) {
|
|
2976
|
+
return {
|
|
2977
|
+
...place,
|
|
2978
|
+
stack: [...place.stack],
|
|
2979
|
+
vars: new Map(place.vars),
|
|
2980
|
+
exported: new Set(place.exported)
|
|
2981
|
+
};
|
|
2982
|
+
}
|
|
2983
|
+
function child(place, prefix) {
|
|
2984
|
+
const next = fork(place);
|
|
2985
|
+
next.stack = [];
|
|
2986
|
+
for (const name of next.vars.keys())
|
|
2987
|
+
if (!next.exported.has(name)) next.vars.delete(name);
|
|
2988
|
+
assign(next, prefix, true);
|
|
2989
|
+
return next;
|
|
2990
|
+
}
|
|
2991
|
+
function expanded(word, vars) {
|
|
2992
|
+
let text = word.text;
|
|
2993
|
+
for (const param of [...word.params].reverse()) {
|
|
2994
|
+
const value = vars.get(param.name);
|
|
2995
|
+
if (value !== void 0)
|
|
2996
|
+
text = text.slice(0, param.start) + value + text.slice(param.end);
|
|
2997
|
+
}
|
|
2998
|
+
return text;
|
|
2999
|
+
}
|
|
3000
|
+
function changeDir(place, target) {
|
|
3001
|
+
if (target === void 0 || target === "~") return homedir();
|
|
3002
|
+
if (target.startsWith("~/")) return join8(homedir(), target.slice(2));
|
|
3003
|
+
if (isAbsolute4(target) || place.dir === "") return target;
|
|
3004
|
+
return join8(place.dir, target);
|
|
3005
|
+
}
|
|
3006
|
+
function assign(place, assigns, exporting) {
|
|
3007
|
+
for (const word of assigns) {
|
|
3008
|
+
const name = /^([A-Za-z_][A-Za-z0-9_]*)=/u.exec(word.text)?.[1];
|
|
3009
|
+
if (!name) continue;
|
|
3010
|
+
if (exporting) place.exported.add(name);
|
|
3011
|
+
const value = expanded(word, place.vars).slice(name.length + 1);
|
|
3012
|
+
const known = word.nested.length === 0 && word.params.every((param) => place.vars.has(param.name)) && !value.includes("$");
|
|
3013
|
+
if (known) place.vars.set(name, value);
|
|
3014
|
+
else place.vars.delete(name);
|
|
3015
|
+
}
|
|
3016
|
+
}
|
|
3017
|
+
function go(place, dir) {
|
|
3018
|
+
place.previous = place.dir;
|
|
3019
|
+
place.dir = dir;
|
|
3020
|
+
}
|
|
3021
|
+
function move(place, argv) {
|
|
3022
|
+
const args = argv.slice(1).filter((word) => !/^-[LPe@]+$/u.test(word));
|
|
3023
|
+
const target = args[0] === "--" ? args[1] : args[0];
|
|
3024
|
+
if (argv[0] === "popd") {
|
|
3025
|
+
const back = place.stack.pop();
|
|
3026
|
+
if (back !== void 0) go(place, back);
|
|
3027
|
+
return;
|
|
3028
|
+
}
|
|
3029
|
+
if (argv[0] === "pushd") {
|
|
3030
|
+
if (target === void 0) return;
|
|
3031
|
+
place.stack.push(place.dir);
|
|
3032
|
+
}
|
|
3033
|
+
go(place, target === "-" ? place.previous : changeDir(place, target));
|
|
3034
|
+
}
|
|
3035
|
+
var Walk = class _Walk {
|
|
3036
|
+
found = [];
|
|
3037
|
+
redirects = [];
|
|
3038
|
+
list(items, place, depth) {
|
|
3039
|
+
for (const item of items) {
|
|
3040
|
+
const here = item.background ? fork(place) : place;
|
|
3041
|
+
for (const pipeline of item.pipelines) {
|
|
3042
|
+
const alone = pipeline.commands.length === 1;
|
|
3043
|
+
for (const command of pipeline.commands)
|
|
3044
|
+
this.command(command, alone ? here : fork(here), depth);
|
|
3045
|
+
}
|
|
3046
|
+
}
|
|
3047
|
+
}
|
|
3048
|
+
command(command, place, depth) {
|
|
3049
|
+
switch (command.kind) {
|
|
3050
|
+
case "simple":
|
|
3051
|
+
this.simple(command, place, depth);
|
|
3052
|
+
return;
|
|
3053
|
+
case "group":
|
|
3054
|
+
this.list(command.body, place, depth);
|
|
3055
|
+
this.redirected(command.redirects, place, depth);
|
|
3056
|
+
return;
|
|
3057
|
+
case "subshell":
|
|
3058
|
+
this.list(command.body, fork(place), depth);
|
|
3059
|
+
this.redirected(command.redirects, place, depth);
|
|
3060
|
+
return;
|
|
3061
|
+
case "compound":
|
|
3062
|
+
this.expansions(command.words, place, depth);
|
|
3063
|
+
if (command.variable) place.vars.delete(command.variable);
|
|
3064
|
+
for (const part of command.parts) this.list(part, place, depth);
|
|
3065
|
+
this.redirected(command.redirects, place, depth);
|
|
3066
|
+
return;
|
|
3067
|
+
case "function":
|
|
3068
|
+
this.command(command.body, fork(place), depth);
|
|
3069
|
+
}
|
|
3070
|
+
}
|
|
3071
|
+
redirected(redirects, place, depth) {
|
|
3072
|
+
this.expansions(redirectWords(redirects), place, depth);
|
|
3073
|
+
const plain = redirects.map(({ op, fd, target }) => ({
|
|
3074
|
+
op,
|
|
3075
|
+
fd,
|
|
3076
|
+
target: expanded(target, place.vars)
|
|
3077
|
+
}));
|
|
3078
|
+
this.redirects.push(...plain);
|
|
3079
|
+
return plain;
|
|
3080
|
+
}
|
|
3081
|
+
// What a simple command does to this shell's variables.
|
|
3082
|
+
settle(command, place) {
|
|
3083
|
+
if (command.words.length === 0) {
|
|
3084
|
+
assign(place, command.assigns, false);
|
|
3085
|
+
return;
|
|
3086
|
+
}
|
|
3087
|
+
const [name, ...args] = command.words.map((word) => word.text);
|
|
3088
|
+
if (DECLARES2.has(name)) {
|
|
3089
|
+
const exporting = name === "export";
|
|
3090
|
+
assign(place, command.words.slice(1), exporting);
|
|
3091
|
+
if (exporting) {
|
|
3092
|
+
for (const arg of args)
|
|
3093
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*$/u.test(arg)) place.exported.add(arg);
|
|
3094
|
+
}
|
|
3095
|
+
}
|
|
3096
|
+
if (name === "read") {
|
|
3097
|
+
for (const arg of args)
|
|
3098
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*$/u.test(arg)) place.vars.delete(arg);
|
|
3099
|
+
}
|
|
3100
|
+
}
|
|
3101
|
+
// Substitutions run in subshells that start where the command is.
|
|
3102
|
+
expansions(words, place, depth) {
|
|
3103
|
+
for (const word of words)
|
|
3104
|
+
for (const nested of word.nested) this.list(nested, fork(place), depth);
|
|
3105
|
+
}
|
|
3106
|
+
simple(command, place, depth) {
|
|
3107
|
+
const written = [...command.assigns, ...command.words];
|
|
3108
|
+
this.expansions(written, place, depth);
|
|
3109
|
+
const redirects = this.redirected(command.redirects, place, depth);
|
|
3110
|
+
if (written.length === 0) return;
|
|
3111
|
+
const raw = written.map((word) => expanded(word, place.vars));
|
|
3112
|
+
this.settle(command, place);
|
|
3113
|
+
const { assigned, words } = unwrap(raw);
|
|
2282
3114
|
if (words.length === 0) {
|
|
2283
|
-
found.push({ argv: [], raw, assigned, dir });
|
|
2284
|
-
return
|
|
3115
|
+
this.found.push({ argv: [], raw, assigned, dir: place.dir, redirects });
|
|
3116
|
+
return;
|
|
2285
3117
|
}
|
|
2286
|
-
if (
|
|
2287
|
-
|
|
2288
|
-
return
|
|
3118
|
+
if (["cd", "pushd", "popd"].includes(words[0])) {
|
|
3119
|
+
move(place, words);
|
|
3120
|
+
return;
|
|
2289
3121
|
}
|
|
2290
3122
|
const scriptAt = SHELLS.has(words[0]) ? words.findIndex((word) => /^-[a-z]*c$/u.test(word)) : -1;
|
|
2291
3123
|
if (scriptAt > 0 || words[0] === "eval") {
|
|
3124
|
+
if (depth >= MAX_DEPTH)
|
|
3125
|
+
throw new ShellSyntaxError("shell nested too deep", 0);
|
|
2292
3126
|
const source = words[0] === "eval" ? words.slice(1).join(" ") : words[scriptAt + 1] ?? "";
|
|
2293
|
-
const inner =
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
3127
|
+
const inner = new _Walk();
|
|
3128
|
+
inner.list(parseScript(source), child(place, command.assigns), depth + 1);
|
|
3129
|
+
this.redirects.push(...inner.redirects);
|
|
3130
|
+
this.found.push(
|
|
3131
|
+
...inner.found.map((step) => ({
|
|
2297
3132
|
...step,
|
|
2298
3133
|
assigned: [...assigned, ...step.assigned]
|
|
2299
3134
|
}))
|
|
2300
3135
|
);
|
|
2301
|
-
return
|
|
2302
|
-
}
|
|
2303
|
-
found.push({ argv: words, raw, assigned, dir });
|
|
2304
|
-
return true;
|
|
2305
|
-
};
|
|
2306
|
-
for (const token of parsed.tokens) {
|
|
2307
|
-
if (!SEPARATORS.has(token)) {
|
|
2308
|
-
current.push(token);
|
|
2309
|
-
continue;
|
|
3136
|
+
return;
|
|
2310
3137
|
}
|
|
2311
|
-
|
|
3138
|
+
this.found.push({ argv: words, raw, assigned, dir: place.dir, redirects });
|
|
2312
3139
|
}
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
3140
|
+
};
|
|
3141
|
+
function redirectWords(redirects) {
|
|
3142
|
+
return redirects.flatMap(
|
|
3143
|
+
(redirect) => redirect.body ? [redirect.target, redirect.body] : [redirect.target]
|
|
3144
|
+
);
|
|
3145
|
+
}
|
|
3146
|
+
function readScript(script) {
|
|
3147
|
+
const walk = new Walk();
|
|
3148
|
+
try {
|
|
3149
|
+
walk.list(parseScript(script), start(), 0);
|
|
3150
|
+
} catch (error) {
|
|
3151
|
+
if (error instanceof ShellSyntaxError) return { error: error.message };
|
|
3152
|
+
throw error;
|
|
2318
3153
|
}
|
|
2319
|
-
return found;
|
|
3154
|
+
return { steps: walk.found, redirects: walk.redirects };
|
|
2320
3155
|
}
|
|
2321
3156
|
function gitCall(words) {
|
|
2322
3157
|
let index = 1;
|
|
@@ -2382,11 +3217,20 @@ function mutates(words, rules2 = DEFAULT_MUTATING_RULES) {
|
|
|
2382
3217
|
}
|
|
2383
3218
|
|
|
2384
3219
|
// src/hooks/policies/_shared.ts
|
|
3220
|
+
var UnreadableCommand = class extends Error {
|
|
3221
|
+
constructor(reason2) {
|
|
3222
|
+
super(`not shell syntax: ${reason2}`);
|
|
3223
|
+
}
|
|
3224
|
+
};
|
|
2385
3225
|
function shellCall(input) {
|
|
2386
3226
|
if (input.tool?.kind !== "shell") return null;
|
|
2387
3227
|
const command = input.tool.command ?? "";
|
|
2388
3228
|
if (command.trim() === "") return null;
|
|
2389
|
-
|
|
3229
|
+
const { steps, redirects, syntaxError } = input.tool;
|
|
3230
|
+
if (steps) return { command, steps, redirects: redirects ?? [] };
|
|
3231
|
+
const read = readScript(command);
|
|
3232
|
+
if ("error" in read) throw new UnreadableCommand(syntaxError ?? read.error);
|
|
3233
|
+
return { command, steps: read.steps, redirects: read.redirects };
|
|
2390
3234
|
}
|
|
2391
3235
|
function program(word) {
|
|
2392
3236
|
if (!word) return "";
|
|
@@ -2419,7 +3263,6 @@ function onlyKeys(raw, keys) {
|
|
|
2419
3263
|
|
|
2420
3264
|
// src/hooks/policies/git/forced-branch-move.ts
|
|
2421
3265
|
var FORCE = /* @__PURE__ */ new Set(["-f", "-M", "--force"]);
|
|
2422
|
-
var FALLBACK = /\bgit\b[^|;&\n]*\sbranch\b[^|;&\n]*\s(?:-f|-M|--force)(?:\s|$)/u;
|
|
2423
3266
|
function forcedTarget(argv) {
|
|
2424
3267
|
if (program(argv[0]) !== "git") return null;
|
|
2425
3268
|
const call = gitCall(argv);
|
|
@@ -2444,13 +3287,6 @@ var forcedBranchMove = {
|
|
|
2444
3287
|
evaluate(input, ctx) {
|
|
2445
3288
|
const call = shellCall(input);
|
|
2446
3289
|
if (!call) return pass;
|
|
2447
|
-
if (!call.steps) {
|
|
2448
|
-
if (!FALLBACK.test(call.command)) return pass;
|
|
2449
|
-
return {
|
|
2450
|
-
verdict: "deny",
|
|
2451
|
-
reason: "A forced branch move could not be read."
|
|
2452
|
-
};
|
|
2453
|
-
}
|
|
2454
3290
|
for (const step of realSteps(call.steps)) {
|
|
2455
3291
|
const target = forcedTarget(step.argv);
|
|
2456
3292
|
if (!target) continue;
|
|
@@ -2467,9 +3303,6 @@ var forcedBranchMove = {
|
|
|
2467
3303
|
|
|
2468
3304
|
// src/hooks/policies/git/gate-bypass-env.ts
|
|
2469
3305
|
var REASON = "This takes a way around the gate that only the operator may take. Fix what the gate reports, or ask the operator.";
|
|
2470
|
-
function escape(text) {
|
|
2471
|
-
return text.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
2472
|
-
}
|
|
2473
3306
|
function bypasses(step, options2) {
|
|
2474
3307
|
if (step.assigned.some((name2) => options2.variables.includes(name2)))
|
|
2475
3308
|
return true;
|
|
@@ -2481,14 +3314,6 @@ function bypasses(step, options2) {
|
|
|
2481
3314
|
}
|
|
2482
3315
|
return options2.noVerify && name === "git" && step.argv.includes("--no-verify");
|
|
2483
3316
|
}
|
|
2484
|
-
function fallback(command, options2) {
|
|
2485
|
-
const names3 = options2.variables.map(escape);
|
|
2486
|
-
if (options2.noVerify) names3.push("--no-verify");
|
|
2487
|
-
if (names3.length === 0) return false;
|
|
2488
|
-
return new RegExp(`(?:^|[^\\w-])(?:${names3.join("|")})\\b`, "u").test(
|
|
2489
|
-
command
|
|
2490
|
-
);
|
|
2491
|
-
}
|
|
2492
3317
|
var gateBypassEnv = {
|
|
2493
3318
|
id: "git.gate_bypass_env",
|
|
2494
3319
|
events: ["before_tool"],
|
|
@@ -2505,13 +3330,12 @@ var gateBypassEnv = {
|
|
|
2505
3330
|
evaluate(input, ctx) {
|
|
2506
3331
|
const call = shellCall(input);
|
|
2507
3332
|
if (!call) return pass;
|
|
2508
|
-
const hit2 = call.steps
|
|
3333
|
+
const hit2 = call.steps.some((step) => bypasses(step, ctx.options));
|
|
2509
3334
|
return hit2 ? { verdict: "deny", reason: REASON } : pass;
|
|
2510
3335
|
}
|
|
2511
3336
|
};
|
|
2512
3337
|
|
|
2513
3338
|
// src/hooks/policies/git/push-without-verify.ts
|
|
2514
|
-
var FALLBACK2 = /\bgit\b[^|;&\n]*\spush\b[^|;&\n]*\s--no-verify(?:\s|$)/u;
|
|
2515
3339
|
var REASON2 = "Pushing with verification disabled skips every pre-push check at once, including the ones that catch what a person cannot review by eye. Fix the failing check instead of waiving all of them.";
|
|
2516
3340
|
function skipsVerify(argv) {
|
|
2517
3341
|
if (program(argv[0]) !== "git") return false;
|
|
@@ -2529,7 +3353,7 @@ var pushWithoutVerify = {
|
|
|
2529
3353
|
evaluate(input) {
|
|
2530
3354
|
const call = shellCall(input);
|
|
2531
3355
|
if (!call) return pass;
|
|
2532
|
-
const hit2 =
|
|
3356
|
+
const hit2 = realSteps(call.steps).some((step) => skipsVerify(step.argv));
|
|
2533
3357
|
return hit2 ? { verdict: "deny", reason: REASON2 } : pass;
|
|
2534
3358
|
}
|
|
2535
3359
|
};
|
|
@@ -2552,7 +3376,6 @@ var COMMIT_VALUES = /* @__PURE__ */ new Set([
|
|
|
2552
3376
|
]);
|
|
2553
3377
|
var SHORT_VALUES = /* @__PURE__ */ new Set(["m", "F", "C", "c", "t"]);
|
|
2554
3378
|
var ROOT_PATHS = /* @__PURE__ */ new Set([".", "./", ":/", ":(top)", ":(top)."]);
|
|
2555
|
-
var NAMES_GIT = /(?:^|[^\w.-])git(?![\w.-])/u;
|
|
2556
3379
|
function stagesAll(args, commit) {
|
|
2557
3380
|
let paths = false;
|
|
2558
3381
|
for (let index = 0; index < args.length; index += 1) {
|
|
@@ -2603,8 +3426,7 @@ var stageAll = {
|
|
|
2603
3426
|
evaluate(input) {
|
|
2604
3427
|
const call = shellCall(input);
|
|
2605
3428
|
if (!call) return pass;
|
|
2606
|
-
if (!call.steps
|
|
2607
|
-
if (call.steps && !realSteps(call.steps).some((step) => stagesEverything(step.argv)))
|
|
3429
|
+
if (!realSteps(call.steps).some((step) => stagesEverything(step.argv)))
|
|
2608
3430
|
return pass;
|
|
2609
3431
|
return {
|
|
2610
3432
|
verdict: "deny",
|
|
@@ -2649,7 +3471,7 @@ function runCheck(command, root, timeout) {
|
|
|
2649
3471
|
}
|
|
2650
3472
|
|
|
2651
3473
|
// src/hooks/checks/options.ts
|
|
2652
|
-
import { isAbsolute as
|
|
3474
|
+
import { isAbsolute as isAbsolute5 } from "path";
|
|
2653
3475
|
function report(raw) {
|
|
2654
3476
|
if (raw["report"] !== void 0 && raw["report"] !== "context")
|
|
2655
3477
|
throw new Error("report must be context");
|
|
@@ -2660,7 +3482,7 @@ function afterEditOptions(raw) {
|
|
|
2660
3482
|
const glob = raw["validator_glob"];
|
|
2661
3483
|
const command = raw["command"];
|
|
2662
3484
|
const seconds = raw["debounce_seconds"] ?? 60;
|
|
2663
|
-
if (typeof glob !== "string" || !glob ||
|
|
3485
|
+
if (typeof glob !== "string" || !glob || isAbsolute5(glob) || glob.split("/").includes(".."))
|
|
2664
3486
|
throw new Error("validator_glob must stay inside the project");
|
|
2665
3487
|
if (!Array.isArray(command) || command.length === 0 || !command.every(
|
|
2666
3488
|
(part) => typeof part === "string" && part.length > 0 && !part.includes("\0")
|
|
@@ -2685,10 +3507,10 @@ function stopOptions(raw) {
|
|
|
2685
3507
|
|
|
2686
3508
|
// src/hooks/checks/validators.ts
|
|
2687
3509
|
import { globSync, realpathSync as realpathSync2, statSync as statSync2 } from "fs";
|
|
2688
|
-
import { dirname as dirname2, isAbsolute as
|
|
3510
|
+
import { dirname as dirname2, isAbsolute as isAbsolute6, relative as relative4, resolve as resolve4, sep } from "path";
|
|
2689
3511
|
function contains(root, target) {
|
|
2690
3512
|
const rest = relative4(root, target);
|
|
2691
|
-
return !
|
|
3513
|
+
return !isAbsolute6(rest) && rest !== ".." && !rest.startsWith(`..${sep}`);
|
|
2692
3514
|
}
|
|
2693
3515
|
function owner(validator) {
|
|
2694
3516
|
const parts = validator.split(sep);
|
|
@@ -2767,7 +3589,7 @@ var afterEdit = {
|
|
|
2767
3589
|
// src/hooks/checks/projection.ts
|
|
2768
3590
|
import { spawnSync as spawnSync3 } from "child_process";
|
|
2769
3591
|
import { existsSync as existsSync8, readFileSync as readFileSync8 } from "fs";
|
|
2770
|
-
import { join as
|
|
3592
|
+
import { join as join9 } from "path";
|
|
2771
3593
|
var LOCK_VERSION_KEY = "pmcp_version";
|
|
2772
3594
|
var PROBLEM = /^x (?:drift|stale|undeclared|dead glob|conflict): /mu;
|
|
2773
3595
|
function run(command, root, deadline) {
|
|
@@ -2791,7 +3613,7 @@ function findProjector(root, stateDir, deadline) {
|
|
|
2791
3613
|
const cli = localCli(root) ?? recordedCli(root, stateDir);
|
|
2792
3614
|
if (!cli || !existsSync8(cli))
|
|
2793
3615
|
return { missing: cli ?? "no local or recorded pmcp" };
|
|
2794
|
-
const expected = readJson2(
|
|
3616
|
+
const expected = readJson2(join9(root, "pmcp.lock"))?.[LOCK_VERSION_KEY];
|
|
2795
3617
|
const answer = run([process.execPath, cli, "--version"], root, deadline);
|
|
2796
3618
|
if (answer.error || answer.stdout.trim() !== expected)
|
|
2797
3619
|
return { missing: cli };
|
|
@@ -2937,9 +3759,7 @@ function startsWith(argv, prefix) {
|
|
|
2937
3759
|
(word, at) => at === 0 ? program(argv[0]) === word : argv[at] === word
|
|
2938
3760
|
);
|
|
2939
3761
|
}
|
|
2940
|
-
function blocked(steps,
|
|
2941
|
-
if (!steps)
|
|
2942
|
-
return rule.blocks.some((prefix) => command.includes(prefix.join(" ")));
|
|
3762
|
+
function blocked(steps, rule) {
|
|
2943
3763
|
return realSteps(steps).some(
|
|
2944
3764
|
(step) => rule.blocks.some((prefix) => startsWith(step.argv, prefix))
|
|
2945
3765
|
);
|
|
@@ -2967,7 +3787,7 @@ var leases = {
|
|
|
2967
3787
|
const call = shellCall(input);
|
|
2968
3788
|
if (!call) return pass;
|
|
2969
3789
|
for (const rule of ctx.options) {
|
|
2970
|
-
if (!blocked(call.steps,
|
|
3790
|
+
if (!blocked(call.steps, rule)) continue;
|
|
2971
3791
|
const lease = ctx.store.leases.read(rule.name);
|
|
2972
3792
|
if (!lease) continue;
|
|
2973
3793
|
return {
|
|
@@ -3059,21 +3879,11 @@ var mailbox = {
|
|
|
3059
3879
|
};
|
|
3060
3880
|
|
|
3061
3881
|
// src/hooks/policies/sessions/pause.ts
|
|
3062
|
-
var BUILT_IN = /\b(git\s+(commit|push|merge|rebase|reset|checkout|switch|cherry-pick|revert|am|stash|tag|clean|restore|rm|mv|worktree\s+(add|remove|move))|bun\s+(install|i|add|remove|rm|update|link)\b|(npm|pnpm|yarn)\s+(install|i|add|remove|publish|ci)\b)\b/u;
|
|
3063
|
-
var escape2 = (word) => word.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
3064
3882
|
function changes(input, options2) {
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
if (
|
|
3068
|
-
|
|
3069
|
-
const command = tool.command ?? "";
|
|
3070
|
-
return BUILT_IN.test(command) || options2.commands.some(
|
|
3071
|
-
(prefix) => new RegExp(`\\b${prefix.map(escape2).join("\\s+")}\\b`, "u").test(
|
|
3072
|
-
command
|
|
3073
|
-
)
|
|
3074
|
-
) || options2.scripts.some((s) => s.test(command));
|
|
3075
|
-
}
|
|
3076
|
-
return realSteps(tool.steps).some((step) => mutates(step.argv, options2));
|
|
3883
|
+
if (input.tool?.kind === "edit") return true;
|
|
3884
|
+
const call = shellCall(input);
|
|
3885
|
+
if (!call) return false;
|
|
3886
|
+
return realSteps(call.steps).some((step) => mutates(step.argv, options2));
|
|
3077
3887
|
}
|
|
3078
3888
|
function patterns(raw, key) {
|
|
3079
3889
|
const list2 = stringList(raw, key);
|
|
@@ -3314,7 +4124,6 @@ var VERB_VALUE_FLAGS = {
|
|
|
3314
4124
|
down: /* @__PURE__ */ new Set(["-t", "--timeout", "--rmi"])
|
|
3315
4125
|
};
|
|
3316
4126
|
var SCOPED_VERBS = /* @__PURE__ */ new Set(["stop", "rm", "kill"]);
|
|
3317
|
-
var FALLBACK3 = /\b(?:docker[ \t]+compose|docker-compose)\b[^|;&]*\b(?:down|stop|rm|kill)\b/u;
|
|
3318
4127
|
function firstArgument(words, valueFlags) {
|
|
3319
4128
|
for (let index = 0; index < words.length; index += 1) {
|
|
3320
4129
|
const word = words[index];
|
|
@@ -3351,13 +4160,6 @@ var composeTeardown = {
|
|
|
3351
4160
|
evaluate(input) {
|
|
3352
4161
|
const call = shellCall(input);
|
|
3353
4162
|
if (!call) return pass;
|
|
3354
|
-
if (!call.steps) {
|
|
3355
|
-
if (!FALLBACK3.test(call.command)) return pass;
|
|
3356
|
-
return {
|
|
3357
|
-
verdict: "deny",
|
|
3358
|
-
reason: "A docker compose teardown could not be read."
|
|
3359
|
-
};
|
|
3360
|
-
}
|
|
3361
4163
|
for (const step of realSteps(call.steps)) {
|
|
3362
4164
|
const reason2 = teardownReason(step.argv);
|
|
3363
4165
|
if (reason2) return { verdict: "deny", reason: reason2 };
|
|
@@ -3367,32 +4169,8 @@ var composeTeardown = {
|
|
|
3367
4169
|
};
|
|
3368
4170
|
|
|
3369
4171
|
// src/hooks/policies/shell/heredoc.ts
|
|
3370
|
-
var
|
|
3371
|
-
var HERESTRING = /<<</u;
|
|
4172
|
+
var HERE = /* @__PURE__ */ new Set(["<<", "<<-", "<<<"]);
|
|
3372
4173
|
var REASON3 = "Heredocs and here-strings are refused: the shell expands and truncates their body silently. Write the file with the editor tool, then run it, or pass the content as an argument.";
|
|
3373
|
-
function maskQuotes(command) {
|
|
3374
|
-
let out = "";
|
|
3375
|
-
for (let index = 0; index < command.length; index += 1) {
|
|
3376
|
-
const char = command[index];
|
|
3377
|
-
if (char === "\\") {
|
|
3378
|
-
out += command.slice(index, index + 2);
|
|
3379
|
-
index += 1;
|
|
3380
|
-
continue;
|
|
3381
|
-
}
|
|
3382
|
-
if (char !== "'" && char !== '"') {
|
|
3383
|
-
out += char;
|
|
3384
|
-
continue;
|
|
3385
|
-
}
|
|
3386
|
-
let end = index + 1;
|
|
3387
|
-
while (end < command.length && command[end] !== char) {
|
|
3388
|
-
end += char === '"' && command[end] === "\\" ? 2 : 1;
|
|
3389
|
-
}
|
|
3390
|
-
if (end >= command.length) return null;
|
|
3391
|
-
out += "Q";
|
|
3392
|
-
index = end;
|
|
3393
|
-
}
|
|
3394
|
-
return out;
|
|
3395
|
-
}
|
|
3396
4174
|
var heredoc = {
|
|
3397
4175
|
id: "shell.heredoc",
|
|
3398
4176
|
events: ["before_tool"],
|
|
@@ -3403,18 +4181,13 @@ var heredoc = {
|
|
|
3403
4181
|
evaluate(input) {
|
|
3404
4182
|
const call = shellCall(input);
|
|
3405
4183
|
if (!call) return pass;
|
|
3406
|
-
|
|
3407
|
-
if (HEREDOC.test(text) || HERESTRING.test(text)) {
|
|
3408
|
-
return { verdict: "deny", reason: REASON3 };
|
|
3409
|
-
}
|
|
3410
|
-
return pass;
|
|
4184
|
+
return call.redirects.some((redirect) => HERE.has(redirect.op)) ? { verdict: "deny", reason: REASON3 } : pass;
|
|
3411
4185
|
}
|
|
3412
4186
|
};
|
|
3413
4187
|
|
|
3414
4188
|
// src/hooks/policies/shell/host-approval.ts
|
|
3415
4189
|
var APPROVALS = "host-approvals";
|
|
3416
4190
|
var VERB = ["host", "approve"];
|
|
3417
|
-
var FALLBACK4 = /\bhost\s+approve\b|host-approvals/u;
|
|
3418
4191
|
var REASON4 = "Approving the pmcp-pty host policy is the operator's step: run pmcp harness host approve in your own terminal, not from an agent.";
|
|
3419
4192
|
function approves(argv) {
|
|
3420
4193
|
const at = argv.indexOf(VERB[0]);
|
|
@@ -3426,7 +4199,8 @@ function hit(input) {
|
|
|
3426
4199
|
return tool.paths.some((path) => path.includes(`/${APPROVALS}`));
|
|
3427
4200
|
const call = shellCall(input);
|
|
3428
4201
|
if (!call) return false;
|
|
3429
|
-
if (
|
|
4202
|
+
if (call.redirects.some((redirect) => redirect.target.includes(APPROVALS)))
|
|
4203
|
+
return true;
|
|
3430
4204
|
return realSteps(call.steps).some(
|
|
3431
4205
|
(step) => approves(step.argv) || step.argv.some((word) => word.includes(APPROVALS))
|
|
3432
4206
|
);
|
|
@@ -3445,7 +4219,6 @@ var hostApproval = {
|
|
|
3445
4219
|
|
|
3446
4220
|
// src/hooks/policies/shell/in-place-edit.ts
|
|
3447
4221
|
var EDITORS = /* @__PURE__ */ new Set(["sed", "perl", "awk", "gawk"]);
|
|
3448
|
-
var FALLBACK5 = /(^|[;&|\n]|&&)[ \t]*(?:[A-Za-z_][A-Za-z0-9_]*=\S*[ \t]+)*(sed|perl|gawk|awk)\b[^\n]*?\s(-i\b|--in-place\b|-i\S+)/u;
|
|
3449
4222
|
var REASON5 = "In-place stream edits (sed -i, perl -i, awk -i) rewrite files without showing a diff and succeed on the first match rather than the intended one. Use the editor tool instead.";
|
|
3450
4223
|
function inPlace(argv) {
|
|
3451
4224
|
const name = program(argv[0]);
|
|
@@ -3463,7 +4236,7 @@ var inPlaceEdit = {
|
|
|
3463
4236
|
evaluate(input) {
|
|
3464
4237
|
const call = shellCall(input);
|
|
3465
4238
|
if (!call) return pass;
|
|
3466
|
-
const hit2 =
|
|
4239
|
+
const hit2 = realSteps(call.steps).some((step) => inPlace(step.argv));
|
|
3467
4240
|
return hit2 ? { verdict: "deny", reason: REASON5 } : pass;
|
|
3468
4241
|
}
|
|
3469
4242
|
};
|
|
@@ -3522,8 +4295,8 @@ function repeats(pattern, at) {
|
|
|
3522
4295
|
if (char === "*" || char === "+") return true;
|
|
3523
4296
|
return char === "{" && /^\{\d*,\}/u.test(pattern.slice(at));
|
|
3524
4297
|
}
|
|
3525
|
-
function classEnd(pattern,
|
|
3526
|
-
let at =
|
|
4298
|
+
function classEnd(pattern, start2) {
|
|
4299
|
+
let at = start2 + 1;
|
|
3527
4300
|
if (pattern[at] === "^") at += 1;
|
|
3528
4301
|
if (pattern[at] === "]") at += 1;
|
|
3529
4302
|
for (; at < pattern.length; at += 1) {
|
|
@@ -3581,9 +4354,9 @@ var FLAVOR_LONG = {
|
|
|
3581
4354
|
"fixed-strings": "fixed",
|
|
3582
4355
|
"basic-regexp": "basic"
|
|
3583
4356
|
};
|
|
3584
|
-
function readGrep(args,
|
|
4357
|
+
function readGrep(args, start2) {
|
|
3585
4358
|
const patterns2 = [];
|
|
3586
|
-
let flavor =
|
|
4359
|
+
let flavor = start2;
|
|
3587
4360
|
let onlyMatching = false;
|
|
3588
4361
|
let operand;
|
|
3589
4362
|
let fromFile = false;
|
|
@@ -3643,14 +4416,14 @@ function costOf(step, options2) {
|
|
|
3643
4416
|
return null;
|
|
3644
4417
|
}
|
|
3645
4418
|
var reason = (why, threshold) => `This grep can keep a CPU core busy for minutes: ${why}. Find the line with grep -n and no -o or large count, then print a window around it with sed -n 'N,+K p' FILE; or use rg, which matches in linear time; or bring the count to ${threshold} or below; or bound the run with timeout 20 grep \u2026.`;
|
|
3646
|
-
function integer(raw, key,
|
|
3647
|
-
const value = raw[key] ??
|
|
4419
|
+
function integer(raw, key, fallback) {
|
|
4420
|
+
const value = raw[key] ?? fallback;
|
|
3648
4421
|
if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 32767)
|
|
3649
4422
|
throw new Error(`${key} must be an integer from 1 to 32767`);
|
|
3650
4423
|
return value;
|
|
3651
4424
|
}
|
|
3652
|
-
function bool(raw, key,
|
|
3653
|
-
const value = raw[key] ??
|
|
4425
|
+
function bool(raw, key, fallback) {
|
|
4426
|
+
const value = raw[key] ?? fallback;
|
|
3654
4427
|
if (typeof value !== "boolean")
|
|
3655
4428
|
throw new Error(`${key} must be true or false`);
|
|
3656
4429
|
return value;
|
|
@@ -3671,8 +4444,9 @@ var runawayRegex = {
|
|
|
3671
4444
|
},
|
|
3672
4445
|
// Unreadable shell syntax passes: a wrong refusal here costs more than a slow grep.
|
|
3673
4446
|
evaluate(input, ctx) {
|
|
4447
|
+
if (input.tool?.syntaxError) return pass;
|
|
3674
4448
|
const call = shellCall(input);
|
|
3675
|
-
if (!call
|
|
4449
|
+
if (!call) return pass;
|
|
3676
4450
|
for (const step of realSteps(call.steps)) {
|
|
3677
4451
|
const why = costOf(step, ctx.options);
|
|
3678
4452
|
if (why)
|
|
@@ -3815,8 +4589,6 @@ var ENV_DUMPS = [
|
|
|
3815
4589
|
["export", "-p"],
|
|
3816
4590
|
["declare", "-x"]
|
|
3817
4591
|
];
|
|
3818
|
-
var FALLBACK_PATH = /(?:^|[^\w.-])\.env(?![.\w-])|\.dev\.vars(?:\.json)?\b/u;
|
|
3819
|
-
var FALLBACK_USE = /(?:^|[;&|(]|&&)\s*(?:sudo\s+)?(?:cat|head|tail|less|grep|rg|jq|base64|cp|curl|wget|nc|scp|rsync)\b/u;
|
|
3820
4592
|
var NO_VALUES = "Use metadata verbs instead (ls, stat, wc, find), or a command that names keys without resolving values.";
|
|
3821
4593
|
function names(word, options2) {
|
|
3822
4594
|
if (PATTERN.test(word)) return false;
|
|
@@ -3867,6 +4639,9 @@ function operandsOf(argv) {
|
|
|
3867
4639
|
}
|
|
3868
4640
|
return operands;
|
|
3869
4641
|
}
|
|
4642
|
+
function inputFiles(step) {
|
|
4643
|
+
return step.redirects.filter((redirect) => redirect.op === "<" || redirect.op === "<>").map((redirect) => redirect.target);
|
|
4644
|
+
}
|
|
3870
4645
|
function severity(steps, options2) {
|
|
3871
4646
|
const wire = steps.some((step) => step.raw.some(isWire));
|
|
3872
4647
|
const real = realSteps(steps);
|
|
@@ -3876,16 +4651,14 @@ function severity(steps, options2) {
|
|
|
3876
4651
|
return wire ? "exfil" : "read";
|
|
3877
4652
|
if (steps.some(dumpsEnvironment)) return wire ? "exfil" : "read";
|
|
3878
4653
|
const touched = real.filter(
|
|
3879
|
-
(step) => operandsOf(step.argv).some(
|
|
4654
|
+
(step) => [...operandsOf(step.argv), ...inputFiles(step)].some(
|
|
4655
|
+
(word) => names(word, options2)
|
|
4656
|
+
)
|
|
3880
4657
|
);
|
|
3881
4658
|
if (touched.some((step) => isWire(step.argv[0]))) return "exfil";
|
|
3882
4659
|
if (!touched.some((step) => READERS.has(program(step.argv[0])))) return null;
|
|
3883
4660
|
return wire ? "exfil" : "read";
|
|
3884
4661
|
}
|
|
3885
|
-
function fallbackSeverity(command) {
|
|
3886
|
-
if (!FALLBACK_PATH.test(command) || !FALLBACK_USE.test(command)) return null;
|
|
3887
|
-
return /\b(?:curl|wget|nc|scp|rsync)\b|https?:\/\//u.test(command) ? "exfil" : "read";
|
|
3888
|
-
}
|
|
3889
4662
|
function verbsOf(table3, key) {
|
|
3890
4663
|
return (stringList(table3, key) ?? []).map((verb) => verb.split(/\s+/u));
|
|
3891
4664
|
}
|
|
@@ -3927,7 +4700,7 @@ var secretExfiltration = {
|
|
|
3927
4700
|
evaluate(input, ctx) {
|
|
3928
4701
|
const call = shellCall(input);
|
|
3929
4702
|
if (!call) return pass;
|
|
3930
|
-
const found =
|
|
4703
|
+
const found = severity(call.steps, ctx.options);
|
|
3931
4704
|
if (found === "exfil") {
|
|
3932
4705
|
return {
|
|
3933
4706
|
verdict: "deny",
|
|
@@ -3946,34 +4719,15 @@ var secretExfiltration = {
|
|
|
3946
4719
|
|
|
3947
4720
|
// src/hooks/policies/shell/stdin-interpreter.ts
|
|
3948
4721
|
var PYTHON = /^i?python[23]?(\.\d+)?$/u;
|
|
3949
|
-
var FALLBACK6 = /(?:^|[;&|(]|&&|\|\|)[ \t]*(?:sudo[ \t]+)?i?python[23]?(?:\.\d+)?\b([^;&|)]*)/gu;
|
|
3950
4722
|
var REASON6 = "This interpreter reads its program from stdin, so the command text does not contain it and whatever fed the pipe already expanded it. Write the script with the editor tool, then run it.";
|
|
3951
4723
|
var INFORMATIONAL = /^(?:-V+|--version|-h|-\?|--help(?:-[a-z]+)?)$/u;
|
|
3952
|
-
|
|
3953
|
-
function withoutRedirects(args) {
|
|
3954
|
-
const kept = [];
|
|
3955
|
-
for (let at = 0; at < args.length; at += 1) {
|
|
3956
|
-
const match = REDIRECT.exec(args[at]);
|
|
3957
|
-
if (!match) kept.push(args[at]);
|
|
3958
|
-
else if (match[1] === "") at += 1;
|
|
3959
|
-
}
|
|
3960
|
-
return kept;
|
|
3961
|
-
}
|
|
3962
|
-
function programFromStdin(words) {
|
|
3963
|
-
const args = withoutRedirects(words);
|
|
4724
|
+
function programFromStdin(args) {
|
|
3964
4725
|
if (args.includes("-")) return true;
|
|
3965
4726
|
if (args.some((arg) => INFORMATIONAL.test(arg))) return false;
|
|
3966
4727
|
return !args.some(
|
|
3967
4728
|
(arg) => arg === "-c" || arg === "-m" || !arg.startsWith("-")
|
|
3968
4729
|
);
|
|
3969
4730
|
}
|
|
3970
|
-
function fallback2(command) {
|
|
3971
|
-
for (const match of command.matchAll(FALLBACK6)) {
|
|
3972
|
-
const args = (match[1] ?? "").trim().split(/\s+/u).filter(Boolean);
|
|
3973
|
-
if (programFromStdin(args)) return true;
|
|
3974
|
-
}
|
|
3975
|
-
return false;
|
|
3976
|
-
}
|
|
3977
4731
|
var stdinInterpreter = {
|
|
3978
4732
|
id: "shell.stdin_interpreter",
|
|
3979
4733
|
events: ["before_tool"],
|
|
@@ -3984,9 +4738,9 @@ var stdinInterpreter = {
|
|
|
3984
4738
|
evaluate(input) {
|
|
3985
4739
|
const call = shellCall(input);
|
|
3986
4740
|
if (!call) return pass;
|
|
3987
|
-
const hit2 =
|
|
4741
|
+
const hit2 = realSteps(call.steps).some(
|
|
3988
4742
|
(step) => PYTHON.test(program(step.argv[0])) && programFromStdin(step.argv.slice(1))
|
|
3989
|
-
)
|
|
4743
|
+
);
|
|
3990
4744
|
return hit2 ? { verdict: "deny", reason: REASON6 } : pass;
|
|
3991
4745
|
}
|
|
3992
4746
|
};
|
|
@@ -4006,12 +4760,12 @@ function claimantNotice(root, names3) {
|
|
|
4006
4760
|
|
|
4007
4761
|
// src/hooks/policies/workspace/leftovers.ts
|
|
4008
4762
|
import { spawnSync as spawnSync4 } from "child_process";
|
|
4009
|
-
import { isAbsolute as
|
|
4763
|
+
import { isAbsolute as isAbsolute7, relative as relative6, resolve as resolve5 } from "path";
|
|
4010
4764
|
var TOUCHED_KEPT = 50;
|
|
4011
4765
|
function addTouched(touched, root, target) {
|
|
4012
4766
|
const path = relative6(
|
|
4013
4767
|
root,
|
|
4014
|
-
|
|
4768
|
+
isAbsolute7(target) ? target : resolve5(root, target)
|
|
4015
4769
|
);
|
|
4016
4770
|
if (!path || path.startsWith("..") || touched.some((entry) => entry.path === path))
|
|
4017
4771
|
return [...touched];
|
|
@@ -4152,7 +4906,6 @@ var sharedCheckout = {
|
|
|
4152
4906
|
// src/hooks/policies/workspace/mainline-deliver.ts
|
|
4153
4907
|
import { spawnSync as spawnSync5 } from "child_process";
|
|
4154
4908
|
import { resolve as resolve6 } from "path";
|
|
4155
|
-
var PUSH = /\bgit\b[^|;&\n]*\spush\b/u;
|
|
4156
4909
|
var REASON7 = "This repository's mainline moves only through pmcp git deliver, which replays, gates and pushes one delivery at a time. Commit, then run pmcp git deliver; other branches, other remotes and other repositories push as usual.";
|
|
4157
4910
|
var VALUE_OPTIONS = /* @__PURE__ */ new Set([
|
|
4158
4911
|
"--repo",
|
|
@@ -4249,10 +5002,6 @@ var mainlineDeliver = {
|
|
|
4249
5002
|
const call = shellCall(input);
|
|
4250
5003
|
const project = checkoutOf(input.projectRoot);
|
|
4251
5004
|
if (!call || !project) return pass;
|
|
4252
|
-
if (!call.steps) {
|
|
4253
|
-
const hit2 = PUSH.test(call.command) && sameRepository(input.cwd, project.commonDir);
|
|
4254
|
-
return hit2 ? { verdict: "deny", reason: REASON7 } : pass;
|
|
4255
|
-
}
|
|
4256
5005
|
for (const step of realSteps(call.steps)) {
|
|
4257
5006
|
if (program(step.argv[0]) !== "git") continue;
|
|
4258
5007
|
const git = gitCall(step.argv);
|
|
@@ -4300,7 +5049,7 @@ function changed(root, from, paths, to = "HEAD") {
|
|
|
4300
5049
|
return gitText(root, ["diff", "--name-only", from, to, "--", ...paths]).split("\n").filter(Boolean);
|
|
4301
5050
|
}
|
|
4302
5051
|
function backgroundInstall(root, command, umask) {
|
|
4303
|
-
const
|
|
5052
|
+
const child2 = withUmask(
|
|
4304
5053
|
umask,
|
|
4305
5054
|
() => spawn(command[0], command.slice(1), {
|
|
4306
5055
|
cwd: root,
|
|
@@ -4308,10 +5057,10 @@ function backgroundInstall(root, command, umask) {
|
|
|
4308
5057
|
stdio: "ignore"
|
|
4309
5058
|
})
|
|
4310
5059
|
);
|
|
4311
|
-
|
|
4312
|
-
|
|
5060
|
+
child2.on("error", () => void 0);
|
|
5061
|
+
child2.unref();
|
|
4313
5062
|
}
|
|
4314
|
-
function
|
|
5063
|
+
function move2(input, ctx, git) {
|
|
4315
5064
|
const opts = ctx.options;
|
|
4316
5065
|
const lines = [];
|
|
4317
5066
|
const report2 = {
|
|
@@ -4370,7 +5119,7 @@ var worktreeSync = {
|
|
|
4370
5119
|
const reason2 = blocked2(input, ctx);
|
|
4371
5120
|
if (reason2)
|
|
4372
5121
|
return input.event === "session_start" ? { context: [`Worktree ${git.root} was not moved: ${reason2}.`] } : pass;
|
|
4373
|
-
const lines =
|
|
5122
|
+
const lines = move2(input, ctx, git);
|
|
4374
5123
|
return lines.length > 0 ? { context: lines } : pass;
|
|
4375
5124
|
}
|
|
4376
5125
|
};
|
|
@@ -4379,7 +5128,7 @@ var worktreeSync = {
|
|
|
4379
5128
|
import { spawnSync as spawnSync6 } from "child_process";
|
|
4380
5129
|
import { createHash as createHash5 } from "crypto";
|
|
4381
5130
|
import { existsSync as existsSync9, readFileSync as readFileSync9, realpathSync as realpathSync3 } from "fs";
|
|
4382
|
-
import { isAbsolute as
|
|
5131
|
+
import { isAbsolute as isAbsolute8, relative as relative8, resolve as resolve8 } from "path";
|
|
4383
5132
|
var FAILED = "Guard failed validation or execution";
|
|
4384
5133
|
var GUARD_PROTOCOL = "pmcp.guard.v1";
|
|
4385
5134
|
var MAX_FINDINGS = 20;
|
|
@@ -4400,7 +5149,7 @@ function customMode(entry) {
|
|
|
4400
5149
|
}
|
|
4401
5150
|
function inside(base, path) {
|
|
4402
5151
|
const rest = relative8(base, path);
|
|
4403
|
-
return rest !== "" && !rest.startsWith("..") && !
|
|
5152
|
+
return rest !== "" && !rest.startsWith("..") && !isAbsolute8(rest);
|
|
4404
5153
|
}
|
|
4405
5154
|
function namesScript(command) {
|
|
4406
5155
|
return [command[0], command[1]].some(
|
|
@@ -4487,6 +5236,9 @@ function request(entry, input) {
|
|
|
4487
5236
|
...entry.event === "prompt" ? { prompt: input.prompt ?? "" } : {}
|
|
4488
5237
|
});
|
|
4489
5238
|
}
|
|
5239
|
+
function failedToRun(error) {
|
|
5240
|
+
return error !== void 0 && error.code !== "EPIPE";
|
|
5241
|
+
}
|
|
4490
5242
|
function run2(entry, input, log) {
|
|
4491
5243
|
const root = input.projectRoot;
|
|
4492
5244
|
if (entry.guardSha256 !== void 0 && guardHash(root, entry.command) !== entry.guardSha256) {
|
|
@@ -4507,7 +5259,7 @@ function run2(entry, input, log) {
|
|
|
4507
5259
|
killSignal: "SIGKILL",
|
|
4508
5260
|
maxBuffer: 65536
|
|
4509
5261
|
});
|
|
4510
|
-
if (result.status !== 0 || result.error)
|
|
5262
|
+
if (result.status !== 0 || failedToRun(result.error))
|
|
4511
5263
|
return { verdict: "deny", reason: FAILED };
|
|
4512
5264
|
const response = JSON.parse(result.stdout);
|
|
4513
5265
|
const extra = Object.keys(response).some(
|
|
@@ -4863,7 +5615,7 @@ function readChecks(raw) {
|
|
|
4863
5615
|
}
|
|
4864
5616
|
|
|
4865
5617
|
// src/hooks/dispatch/failure.ts
|
|
4866
|
-
import { isAbsolute as
|
|
5618
|
+
import { isAbsolute as isAbsolute9, relative as relative9, resolve as resolve9 } from "path";
|
|
4867
5619
|
var PASS_KINDS = ["read", "edit-own-worktree"];
|
|
4868
5620
|
function readFailure(value) {
|
|
4869
5621
|
if (value === void 0) return void 0;
|
|
@@ -4873,8 +5625,8 @@ function readFailure(value) {
|
|
|
4873
5625
|
for (const key of Object.keys(table3))
|
|
4874
5626
|
if (!["timeout_ms", "retry", "pass"].includes(key))
|
|
4875
5627
|
throw new Error(`[hooks.failure] unknown key ${key}`);
|
|
4876
|
-
const integer2 = (key,
|
|
4877
|
-
const v = table3[key] ??
|
|
5628
|
+
const integer2 = (key, fallback, min, max) => {
|
|
5629
|
+
const v = table3[key] ?? fallback;
|
|
4878
5630
|
if (typeof v !== "number" || !Number.isInteger(v) || v < min || v > max)
|
|
4879
5631
|
throw new Error(
|
|
4880
5632
|
`[hooks.failure] ${key} must be an integer from ${min} to ${max}`
|
|
@@ -4898,7 +5650,7 @@ function ownWorktree(input) {
|
|
|
4898
5650
|
const paths = input.tool?.paths ?? [];
|
|
4899
5651
|
return paths.length > 0 && paths.every((path) => {
|
|
4900
5652
|
const rest = relative9(checkout.root, resolve9(input.cwd, path));
|
|
4901
|
-
return rest !== "" && !rest.startsWith("..") && !
|
|
5653
|
+
return rest !== "" && !rest.startsWith("..") && !isAbsolute9(rest);
|
|
4902
5654
|
});
|
|
4903
5655
|
}
|
|
4904
5656
|
function passes(input, pass2) {
|
|
@@ -5469,7 +6221,8 @@ function readSpec(config) {
|
|
|
5469
6221
|
export {
|
|
5470
6222
|
pluginBinaryDoctor,
|
|
5471
6223
|
readMcp,
|
|
5472
|
-
|
|
6224
|
+
readScript,
|
|
6225
|
+
UnreadableCommand,
|
|
5473
6226
|
customEvent,
|
|
5474
6227
|
customMode,
|
|
5475
6228
|
programVersion,
|
|
@@ -5513,4 +6266,4 @@ export {
|
|
|
5513
6266
|
checkBudget,
|
|
5514
6267
|
readSpec
|
|
5515
6268
|
};
|
|
5516
|
-
//# sourceMappingURL=chunk-
|
|
6269
|
+
//# sourceMappingURL=chunk-7GD2RXEX.js.map
|