@workit-dev/pi 2.29.0 → 2.29.1
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/dist/worker.js +104 -16
- package/dist/workit.js +1418 -1098
- package/package.json +1 -1
package/dist/workit.js
CHANGED
|
@@ -19990,9 +19990,9 @@ var arithmeticEnd = (command, start) => {
|
|
|
19990
19990
|
const end = substitutionEnd(command, start);
|
|
19991
19991
|
return command[end - 1] === ")" && end > start + 2 ? end : 0;
|
|
19992
19992
|
};
|
|
19993
|
-
var segmentsOf = (command, dialect = "posix") => {
|
|
19993
|
+
var segmentsOf = (command, dialect = "posix", options = {}) => {
|
|
19994
19994
|
try {
|
|
19995
|
-
return tokenize(command, dialect);
|
|
19995
|
+
return tokenize(command, dialect, options.spans === true);
|
|
19996
19996
|
} catch (error) {
|
|
19997
19997
|
const why = error instanceof ShellAnalysisError ? error.message : String(error);
|
|
19998
19998
|
return [{ words: [], redirects: [], unanalyzable: why }];
|
|
@@ -20002,12 +20002,14 @@ var afterEnv = (words) => {
|
|
|
20002
20002
|
const at = words.map((word) => word.split(/[\\/]/).at(-1)).lastIndexOf("env");
|
|
20003
20003
|
return at >= 0 && words.slice(at + 1).every((word, index, rest) => /^-|=/.test(word) || /^-[^-]*[uCS]$|^--(?:unset|chdir|split-string)$/.test(rest[index - 1] ?? ""));
|
|
20004
20004
|
};
|
|
20005
|
-
var tokenize = (command, dialect) => {
|
|
20005
|
+
var tokenize = (command, dialect, spans) => {
|
|
20006
20006
|
const posix = dialect === "posix";
|
|
20007
20007
|
const segments = [];
|
|
20008
20008
|
let current = { words: [], redirects: [] };
|
|
20009
20009
|
let word = "";
|
|
20010
20010
|
let inWord = false;
|
|
20011
|
+
let wordStart = -1;
|
|
20012
|
+
let cursor = 0;
|
|
20011
20013
|
let expanding = false;
|
|
20012
20014
|
let afterOperator = false;
|
|
20013
20015
|
let pending = null;
|
|
@@ -20018,9 +20020,13 @@ var tokenize = (command, dialect) => {
|
|
|
20018
20020
|
if (!inWord)
|
|
20019
20021
|
return;
|
|
20020
20022
|
const text = MARKERS.test(word) ? literal2(word) : word;
|
|
20023
|
+
const span = [wordStart, cursor];
|
|
20021
20024
|
if (pending === "redirect") {
|
|
20022
|
-
if (!duplicate || !/^(?:\d+-?|-)$/.test(text))
|
|
20025
|
+
if (!duplicate || !/^(?:\d+-?|-)$/.test(text)) {
|
|
20023
20026
|
current.redirects.push(text);
|
|
20027
|
+
if (spans)
|
|
20028
|
+
(current.redirectSpans ??= []).push(span);
|
|
20029
|
+
}
|
|
20024
20030
|
} else if (pending === "heredoc")
|
|
20025
20031
|
heredocs.push({ delimiter: text.replace(/^-/, ""), owner: current });
|
|
20026
20032
|
else if (pending === "herestring")
|
|
@@ -20028,17 +20034,20 @@ var tokenize = (command, dialect) => {
|
|
|
20028
20034
|
else if (pending === "input")
|
|
20029
20035
|
(current.inputs ??= []).push(text);
|
|
20030
20036
|
else {
|
|
20031
|
-
const
|
|
20037
|
+
const at0 = current.words.length;
|
|
20032
20038
|
if (/^[^=-][^=]*=/.test(word) && afterEnv(current.words))
|
|
20033
20039
|
word = word.replace(/^[^=]*/, (name) => name.replace(/\W/g, "_"));
|
|
20034
20040
|
current.words.push(...MARKERS.test(word) ? expandBraces(word) : [word]);
|
|
20041
|
+
if (spans)
|
|
20042
|
+
(current.spans ??= []).push(...current.words.slice(at0).map(() => span));
|
|
20035
20043
|
if (expanding || !posix && word.startsWith("@"))
|
|
20036
|
-
(current.expands ??= []).push(...current.words.slice(
|
|
20044
|
+
(current.expands ??= []).push(...current.words.slice(at0).map((_, i) => at0 + i));
|
|
20037
20045
|
}
|
|
20038
20046
|
pending = null;
|
|
20039
20047
|
duplicate = false;
|
|
20040
20048
|
word = "";
|
|
20041
20049
|
inWord = false;
|
|
20050
|
+
wordStart = -1;
|
|
20042
20051
|
expanding = false;
|
|
20043
20052
|
};
|
|
20044
20053
|
const flushSegment = () => {
|
|
@@ -20063,9 +20072,12 @@ var tokenize = (command, dialect) => {
|
|
|
20063
20072
|
};
|
|
20064
20073
|
const plain = posix ? PLAIN_POSIX : PLAIN_POWERSHELL;
|
|
20065
20074
|
for (let index = 0;index < command.length; index++) {
|
|
20075
|
+
cursor = index;
|
|
20066
20076
|
plain.lastIndex = index;
|
|
20067
20077
|
const run = plain.exec(command);
|
|
20068
20078
|
if (run) {
|
|
20079
|
+
if (wordStart < 0)
|
|
20080
|
+
wordStart = index;
|
|
20069
20081
|
word += run[0];
|
|
20070
20082
|
inWord = true;
|
|
20071
20083
|
if (EXPANDING_RUN.test(run[0]))
|
|
@@ -20091,9 +20103,11 @@ var tokenize = (command, dialect) => {
|
|
|
20091
20103
|
word += text;
|
|
20092
20104
|
inWord = true;
|
|
20093
20105
|
index = end;
|
|
20094
|
-
} else if (posix && char === "$" && next === '"')
|
|
20106
|
+
} else if (posix && char === "$" && next === '"') {
|
|
20107
|
+
if (wordStart < 0)
|
|
20108
|
+
wordStart = index;
|
|
20095
20109
|
continue;
|
|
20096
|
-
else if (char === "'" || char === '"') {
|
|
20110
|
+
} else if (char === "'" || char === '"') {
|
|
20097
20111
|
const end = closingQuote(command, index, dialect);
|
|
20098
20112
|
const body = command.slice(index + 1, end < 0 ? undefined : end);
|
|
20099
20113
|
if (posix && char === '"' && /\$\(|`/.test(body))
|
|
@@ -20134,6 +20148,7 @@ var tokenize = (command, dialect) => {
|
|
|
20134
20148
|
index = end;
|
|
20135
20149
|
} else if (posix && char === "(" && (pending === "input" || pending === "redirect")) {
|
|
20136
20150
|
const sign = pending === "input" ? "<" : ">";
|
|
20151
|
+
wordStart = index - 1;
|
|
20137
20152
|
pending = null;
|
|
20138
20153
|
const end = substitutionEnd(command, index);
|
|
20139
20154
|
const inner = command.slice(index + 1, end);
|
|
@@ -20146,8 +20161,10 @@ var tokenize = (command, dialect) => {
|
|
|
20146
20161
|
if (char === "(" && inWord && word.endsWith("$")) {
|
|
20147
20162
|
word = word.slice(0, -1);
|
|
20148
20163
|
inWord = word !== "";
|
|
20149
|
-
if (!inWord)
|
|
20164
|
+
if (!inWord) {
|
|
20150
20165
|
expanding = false;
|
|
20166
|
+
wordStart = -1;
|
|
20167
|
+
}
|
|
20151
20168
|
}
|
|
20152
20169
|
group(char === "(" ? "open" : "close");
|
|
20153
20170
|
} else if (!posix && (char === "{" || char === "}")) {
|
|
@@ -20182,6 +20199,7 @@ var tokenize = (command, dialect) => {
|
|
|
20182
20199
|
if (inWord && /^\d$/.test(word)) {
|
|
20183
20200
|
word = "";
|
|
20184
20201
|
inWord = false;
|
|
20202
|
+
wordStart = -1;
|
|
20185
20203
|
} else
|
|
20186
20204
|
flushWord();
|
|
20187
20205
|
if (command[index + 1] === ">")
|
|
@@ -20213,7 +20231,10 @@ var tokenize = (command, dialect) => {
|
|
|
20213
20231
|
word += char;
|
|
20214
20232
|
inWord = true;
|
|
20215
20233
|
}
|
|
20234
|
+
if (inWord && wordStart < 0)
|
|
20235
|
+
wordStart = cursor;
|
|
20216
20236
|
}
|
|
20237
|
+
cursor = command.length;
|
|
20217
20238
|
flushSegment();
|
|
20218
20239
|
return segments;
|
|
20219
20240
|
};
|
|
@@ -30575,8 +30596,8 @@ var writePaths = (input) => {
|
|
|
30575
30596
|
return [...new Set(paths)];
|
|
30576
30597
|
};
|
|
30577
30598
|
// packages/workit-core/src/hooks/standing.ts
|
|
30578
|
-
import
|
|
30579
|
-
import
|
|
30599
|
+
import fs32 from "node:fs";
|
|
30600
|
+
import path52 from "node:path";
|
|
30580
30601
|
|
|
30581
30602
|
// packages/workit-core/src/approvals/store.ts
|
|
30582
30603
|
import { createHash as createHash20, randomBytes as randomBytes4 } from "node:crypto";
|
|
@@ -30795,6 +30816,185 @@ var approveCallDeny = (fix) => `Run \`${fix}\` as its own call: no chain, pipe,
|
|
|
30795
30816
|
// packages/workit-core/src/approvals/inline.ts
|
|
30796
30817
|
import path49 from "node:path";
|
|
30797
30818
|
|
|
30819
|
+
// packages/workit-core/src/hooks/cwd-normalize.ts
|
|
30820
|
+
var DIR = String.raw`(?:[A-Za-z0-9_./@%+:,=-]|(?<=[A-Za-z0-9])~)+|'[^'$${"`"}\n]*'|"(?:[^"$${"`"}\\!\n]|\\(?=[^"$${"`"}\\!\n]))*"`;
|
|
30821
|
+
var COMPOUND2 = new RegExp(String.raw`^\s*cd\s+(${DIR})\s*&&\s*(workit(?:\s[\s\S]*)?)$`, "u");
|
|
30822
|
+
var UNSAFE = /[\p{Cf}\p{Zl}\p{Zp}]|[^\P{Cc}\n]/u;
|
|
30823
|
+
var newlines = (text) => text.split(`
|
|
30824
|
+
`).length - 1;
|
|
30825
|
+
var writtenWord = (command, segment, index) => {
|
|
30826
|
+
const span = segment.spans?.[index];
|
|
30827
|
+
if (!span)
|
|
30828
|
+
return null;
|
|
30829
|
+
const glued = (other) => other !== undefined && other !== span && (other[1] === span[0] || other[0] === span[1]);
|
|
30830
|
+
if (glued(segment.spans?.[index - 1]) || glued(segment.spans?.[index + 1]))
|
|
30831
|
+
return null;
|
|
30832
|
+
const text = command.slice(span[0], span[1]);
|
|
30833
|
+
const value = segment.words[index] ?? "";
|
|
30834
|
+
return UNSAFE.test(text) || newlines(text) !== newlines(value) ? null : text;
|
|
30835
|
+
};
|
|
30836
|
+
var writtenWords = (command, segment, indexes) => {
|
|
30837
|
+
const texts = [];
|
|
30838
|
+
let last;
|
|
30839
|
+
for (const index of indexes) {
|
|
30840
|
+
const span = segment.spans?.[index];
|
|
30841
|
+
if (span && span === last)
|
|
30842
|
+
continue;
|
|
30843
|
+
last = span;
|
|
30844
|
+
const text = writtenWord(command, segment, index);
|
|
30845
|
+
if (text === null)
|
|
30846
|
+
return null;
|
|
30847
|
+
texts.push(text);
|
|
30848
|
+
}
|
|
30849
|
+
return texts;
|
|
30850
|
+
};
|
|
30851
|
+
var cwdIndependent = (text, colons = false) => {
|
|
30852
|
+
let quoted = false;
|
|
30853
|
+
for (let index = 0;index < text.length; index += 1) {
|
|
30854
|
+
const char = text[index] ?? "";
|
|
30855
|
+
if (char === "\\")
|
|
30856
|
+
index += 1;
|
|
30857
|
+
else if (char === "'" && !quoted) {
|
|
30858
|
+
const end = text.indexOf("'", index + 1);
|
|
30859
|
+
if (end < 0)
|
|
30860
|
+
return false;
|
|
30861
|
+
index = end;
|
|
30862
|
+
} else if (char === '"')
|
|
30863
|
+
quoted = !quoted;
|
|
30864
|
+
else if (char === "$" || char === "`")
|
|
30865
|
+
return false;
|
|
30866
|
+
else if (quoted)
|
|
30867
|
+
continue;
|
|
30868
|
+
else if (/[*?[]/u.test(char) || /[<>]/u.test(char) && text[index + 1] === "(")
|
|
30869
|
+
return false;
|
|
30870
|
+
else if (char === "~") {
|
|
30871
|
+
const start = index === 0 || colons && text[index - 1] === ":";
|
|
30872
|
+
if (!start || !/^(?:\/|:|$)/u.test(text.slice(index + 1, index + 2)))
|
|
30873
|
+
return false;
|
|
30874
|
+
}
|
|
30875
|
+
}
|
|
30876
|
+
return !quoted;
|
|
30877
|
+
};
|
|
30878
|
+
var NOT_A_PATH = /^(?:\d*|true|false|yes|no|on|off|-[\w-]*(?:=\d+)?)$/iu;
|
|
30879
|
+
var cwdFreeAssignment = (text, value) => {
|
|
30880
|
+
const written = text.replace(/^[A-Za-z_]\w*=/u, "");
|
|
30881
|
+
if (!cwdIndependent(written, true))
|
|
30882
|
+
return false;
|
|
30883
|
+
const quotes = /['"\\]/u.test(written);
|
|
30884
|
+
const parts = value.replace(/^[A-Za-z_]\w*=/u, "").split(":");
|
|
30885
|
+
return parts.every((part) => part.startsWith("/") || !quotes && /^~(?:\/|$)/u.test(part) || (part !== "" || parts.length === 1) && NOT_A_PATH.test(part));
|
|
30886
|
+
};
|
|
30887
|
+
var plainQuotedExpansions = (text) => {
|
|
30888
|
+
const bare = text.replaceAll(/\\./gu, "").replaceAll(/\$(?:[A-Za-z_]\w*|\{[A-Za-z_]\w*\})/gu, "");
|
|
30889
|
+
return !bare.includes("$") && !bare.includes("`");
|
|
30890
|
+
};
|
|
30891
|
+
var certainDir = (text, value) => value !== "" && !value.startsWith("-") && (/^(?:"(?:[^"\\]|\\.)*"|'[^']*')$/u.test(text) && (text.startsWith("'") || plainQuotedExpansions(text)) || cwdIndependent(text) && !/[{}]/u.test(text.replaceAll(/'[^']*'|"(?:[^"\\]|\\.)*"|\\./gu, "")));
|
|
30892
|
+
var namesSet = (segments) => {
|
|
30893
|
+
const names = new Set;
|
|
30894
|
+
for (const { words } of segments) {
|
|
30895
|
+
const at = /^(?:export|declare|typeset|local|readonly)$/u.test(words[0] ?? "") ? 1 : 0;
|
|
30896
|
+
const assigns = words.slice(at).filter((word) => /^[A-Za-z_]\w*=/u.test(word));
|
|
30897
|
+
if (at === 1 || assigns.length === words.length)
|
|
30898
|
+
for (const word of assigns)
|
|
30899
|
+
names.add(word.slice(0, word.indexOf("=")));
|
|
30900
|
+
if (words[0] === "read" || words[0] === "mapfile") {
|
|
30901
|
+
for (const word of words.slice(1))
|
|
30902
|
+
if (/^[A-Za-z_]\w*$/u.test(word))
|
|
30903
|
+
names.add(word);
|
|
30904
|
+
}
|
|
30905
|
+
}
|
|
30906
|
+
return names;
|
|
30907
|
+
};
|
|
30908
|
+
var NO_PATH = /^(?:\d+|-[A-Za-z0-9-]*)$/u;
|
|
30909
|
+
var cwdFreeTail = (texts, check = false) => {
|
|
30910
|
+
const [command = "", ...args] = texts;
|
|
30911
|
+
if (command.includes("/") && !command.startsWith("/"))
|
|
30912
|
+
return false;
|
|
30913
|
+
return check || args.every((text) => NO_PATH.test(text) || /^(?:\/|~(?:\/|$))/u.test(text));
|
|
30914
|
+
};
|
|
30915
|
+
var GLOBALS_FIRST = new RegExp(String.raw`^(\s*workit)((?:\s+(?:--json|--cwd(?:=|\s+)(?:${DIR})))+)\s+([a-z][a-z-]*)(?=\s|$)`, "u");
|
|
30916
|
+
var VERB_FIRST = /^((?:--json\s+)*)([a-z][a-z-]*)(?=\s|$)/u;
|
|
30917
|
+
var VERBS = new Set(WORKIT_ALLOWED_VERBS);
|
|
30918
|
+
var FILTERS = new Set(["tail", "head", "grep"]);
|
|
30919
|
+
var clean = (s) => !s.unanalyzable && !s.group && !s.subs?.length && !s.stdin?.length && !s.redirects.length;
|
|
30920
|
+
var stdinOnly = (words) => {
|
|
30921
|
+
const [name, ...args] = words;
|
|
30922
|
+
const operands = args.filter((w) => !w.startsWith("-"));
|
|
30923
|
+
if (name === "grep")
|
|
30924
|
+
return operands.length <= 1 && !args.some((w) => /^-[^-]*[rRf]|^--(recursive|file)/u.test(w));
|
|
30925
|
+
return operands.every((w) => /^\d+$/u.test(w)) && !args.some((w) => w.startsWith("--output"));
|
|
30926
|
+
};
|
|
30927
|
+
var cwdRewrite = (command) => {
|
|
30928
|
+
const match = COMPOUND2.exec(command);
|
|
30929
|
+
const dir = match?.[1];
|
|
30930
|
+
const rest = match?.[2];
|
|
30931
|
+
if (!dir || !rest || dir.startsWith("-"))
|
|
30932
|
+
return null;
|
|
30933
|
+
if (/[&;<>`\n]/u.test(rest.replaceAll("2>&1", "").replaceAll(/'[^']*'|"([^"\\]|\\.)*"/gu, "")))
|
|
30934
|
+
return null;
|
|
30935
|
+
const segments = segmentsOf(rest);
|
|
30936
|
+
const [head, ...filters] = segments;
|
|
30937
|
+
if (!head || head.words[0] !== "workit")
|
|
30938
|
+
return null;
|
|
30939
|
+
if (!segments.every(clean))
|
|
30940
|
+
return null;
|
|
30941
|
+
if (!segments.slice(0, -1).every((s) => s.pipe))
|
|
30942
|
+
return null;
|
|
30943
|
+
if (!filters.every((s) => FILTERS.has(s.words[0] ?? "") && stdinOnly(s.words)))
|
|
30944
|
+
return null;
|
|
30945
|
+
const at = head.words.indexOf("--cwd");
|
|
30946
|
+
if (at >= 0) {
|
|
30947
|
+
const bare = dir.replace(/^(['"])(.*)\1$/u, "$2");
|
|
30948
|
+
return bare[0] === "/" && head.words[at + 1] === bare ? rest : null;
|
|
30949
|
+
}
|
|
30950
|
+
if (head.words.some((w) => w.startsWith("--cwd=")))
|
|
30951
|
+
return null;
|
|
30952
|
+
const args = rest.slice("workit".length).trimStart();
|
|
30953
|
+
const verb = VERB_FIRST.exec(args);
|
|
30954
|
+
if (!verb?.[2] || !VERBS.has(verb[2]))
|
|
30955
|
+
return `workit --cwd ${dir} ${args}`.trimEnd();
|
|
30956
|
+
const json = verb[1] ? " --json" : "";
|
|
30957
|
+
return `workit ${verb[2]} --cwd ${dir}${json}${args.slice(verb[0].length)}`.trimEnd();
|
|
30958
|
+
};
|
|
30959
|
+
var verbFirst = (command) => {
|
|
30960
|
+
const match = GLOBALS_FIRST.exec(command);
|
|
30961
|
+
const [whole, lead, globals, verb] = match ?? [];
|
|
30962
|
+
if (!whole || !verb || !VERBS.has(verb) || !globals?.includes("--cwd"))
|
|
30963
|
+
return null;
|
|
30964
|
+
const segments = segmentsOf(command);
|
|
30965
|
+
if (segments.length !== 1 || !segments.every(clean))
|
|
30966
|
+
return null;
|
|
30967
|
+
return `${lead} ${verb}${globals}${command.slice(whole.length)}`;
|
|
30968
|
+
};
|
|
30969
|
+
var NPX = new RegExp(String.raw`^(\s*(?:cd\s+(?:${DIR})\s*&&\s*)?)npx(?:\s+(?:-y|--yes))*\s+@workit-dev\/cli(?:@[\w.-]+)?(?=\s|$)`, "u");
|
|
30970
|
+
var npxWorkit = (command) => {
|
|
30971
|
+
const match = NPX.exec(command);
|
|
30972
|
+
if (!match)
|
|
30973
|
+
return null;
|
|
30974
|
+
const call = `${match[1]}workit${command.slice(match[0].length)}`;
|
|
30975
|
+
const left = segmentsOf(call).some((s) => s.unanalyzable || s.words.some((w) => /^(npx|@workit-dev\/cli(@\S*)?)$/u.test(w)));
|
|
30976
|
+
return left ? null : cwdRewrite(call) ?? verbFirst(call) ?? call;
|
|
30977
|
+
};
|
|
30978
|
+
var MUTATING_GIT2 = /^git( -\S+| -[Cc] \S+)* (commit|push|add|switch|checkout|merge|rebase|reset|cherry-pick|revert|branch .*(-[dDmM]|--delete|--move)\b|tag (?!-l\b|--list)|stash(?! (list|show)\b))/u;
|
|
30979
|
+
var cdWorkitSplit = (command) => {
|
|
30980
|
+
const match = COMPOUND2.exec(command);
|
|
30981
|
+
const dir = match?.[1];
|
|
30982
|
+
const rest = match?.[2];
|
|
30983
|
+
if (!dir || !rest || cwdRewrite(command) !== null)
|
|
30984
|
+
return null;
|
|
30985
|
+
const whole = segmentsOf(rest);
|
|
30986
|
+
const boundary = (head) => segmentsOf(head).every((s, at) => s.words.join("\x00") === whole[at]?.words.join("\x00"));
|
|
30987
|
+
for (const op of rest.matchAll(/\|\||;/gu)) {
|
|
30988
|
+
const head = rest.slice(0, op.index).trimEnd();
|
|
30989
|
+
const call = cwdRewrite(`cd ${dir} && ${head}`);
|
|
30990
|
+
if (!call || !boundary(head))
|
|
30991
|
+
continue;
|
|
30992
|
+
const next = segmentsOf(rest.slice(op.index + op[0].length))[0];
|
|
30993
|
+
return next && MUTATING_GIT2.test(next.words.join(" ")) ? call : null;
|
|
30994
|
+
}
|
|
30995
|
+
return null;
|
|
30996
|
+
};
|
|
30997
|
+
|
|
30798
30998
|
// packages/workit-core/src/approvals/release-notes-ask.ts
|
|
30799
30999
|
import { createHash as createHash21 } from "node:crypto";
|
|
30800
31000
|
import { existsSync as existsSync17, readFileSync as readFileSync14, realpathSync as realpathSync12 } from "node:fs";
|
|
@@ -30967,12 +31167,41 @@ var quote2 = (word, dialect) => {
|
|
|
30967
31167
|
return word;
|
|
30968
31168
|
return `'${word.replaceAll("'", dialect === "powershell" ? "''" : `'\\''`)}'`;
|
|
30969
31169
|
};
|
|
31170
|
+
var producedFile = (command, before, words) => {
|
|
31171
|
+
const named = (target) => words.some((word) => word === target || word.endsWith(`=${target}`));
|
|
31172
|
+
for (const segment of before) {
|
|
31173
|
+
const tee = (segment.words[0] ?? "").split(/[\\/]/u).at(-1) === "tee";
|
|
31174
|
+
const found = segment.redirects.findIndex(named);
|
|
31175
|
+
const span = found >= 0 ? segment.redirectSpans?.[found] : undefined;
|
|
31176
|
+
if (span)
|
|
31177
|
+
return command.slice(span[0], span[1]);
|
|
31178
|
+
const operand = tee ? segment.words.findIndex((word, index) => index > 0 && !word.startsWith("-") && named(word)) : -1;
|
|
31179
|
+
if (operand > 0)
|
|
31180
|
+
return writtenWord(command, segment, operand) ?? "a file the call reads";
|
|
31181
|
+
}
|
|
31182
|
+
return null;
|
|
31183
|
+
};
|
|
30970
31184
|
var routed = (call, globals) => {
|
|
30971
31185
|
if (!globals.includes("--json") || call.slice(1).includes("--json"))
|
|
30972
31186
|
return call;
|
|
30973
31187
|
const end = call.indexOf("--");
|
|
30974
31188
|
return end < 0 ? [...call, "--json"] : [...call.slice(0, end), "--json", ...call.slice(end)];
|
|
30975
31189
|
};
|
|
31190
|
+
var keptIndexes = (words, at) => {
|
|
31191
|
+
const kept = [];
|
|
31192
|
+
for (let index = at;index < words.length; index += 1) {
|
|
31193
|
+
if (words[index] === "--") {
|
|
31194
|
+
for (;index < words.length; index += 1)
|
|
31195
|
+
kept.push(index);
|
|
31196
|
+
break;
|
|
31197
|
+
}
|
|
31198
|
+
if (words[index] === "--cwd")
|
|
31199
|
+
index += 1;
|
|
31200
|
+
else if (!words[index]?.startsWith("--cwd="))
|
|
31201
|
+
kept.push(index);
|
|
31202
|
+
}
|
|
31203
|
+
return kept;
|
|
31204
|
+
};
|
|
30976
31205
|
var cwdValue = (value) => value && !value.startsWith("--") ? value : null;
|
|
30977
31206
|
var cwdOf = (globals) => {
|
|
30978
31207
|
let cwd = null;
|
|
@@ -31013,7 +31242,7 @@ var isReadFilter = (segment) => (segment.words[0] === "tail" || segment.words[0]
|
|
|
31013
31242
|
var onlyFiltersAfter = (segments) => segments[0]?.pipe === true && segments.slice(1).every((segment, index, rest) => isReadFilter(segment) && (index === rest.length - 1 ? !segment.pipe : segment.pipe));
|
|
31014
31243
|
var UNSAFE_TEXT = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
|
|
31015
31244
|
var ODD_SPACE = /[^\S \t\n]/u;
|
|
31016
|
-
var
|
|
31245
|
+
var DIR2 = "<dir>";
|
|
31017
31246
|
var literalDir = (word, expanded) => !expanded && !/^[~=]/u.test(word) && !UNSAFE_TEXT.test(word);
|
|
31018
31247
|
var cwdIndexes = (words, from, verb) => {
|
|
31019
31248
|
const found = [];
|
|
@@ -31037,22 +31266,25 @@ var leadingCd = (before, command) => {
|
|
|
31037
31266
|
return;
|
|
31038
31267
|
const dir = cd.words[1] ?? "";
|
|
31039
31268
|
const clean = !cd.redirects.length && !cd.subs?.length && !dir.startsWith("-");
|
|
31040
|
-
|
|
31269
|
+
const written = writtenWord(command, cd, 1);
|
|
31270
|
+
const certain = written !== null && certainDir(written, dir);
|
|
31271
|
+
return clean && certain && literalDir(dir, Boolean(cd.expands?.length)) ? dir : DIR2;
|
|
31041
31272
|
};
|
|
31042
|
-
var
|
|
31273
|
+
var fixDir = (cwd, literal, cd, dialect) => {
|
|
31043
31274
|
if (cwd === null)
|
|
31044
31275
|
return cd ?? null;
|
|
31045
31276
|
if (!literal)
|
|
31046
|
-
return
|
|
31047
|
-
|
|
31277
|
+
return DIR2;
|
|
31278
|
+
const paths = dialect === "powershell" ? path49.win32 : path49.posix;
|
|
31279
|
+
if (paths.isAbsolute(cwd) || path49.isAbsolute(cwd) || cd === null)
|
|
31048
31280
|
return cwd;
|
|
31049
|
-
return cd === undefined || cd ===
|
|
31281
|
+
return cd === undefined || cd === DIR2 ? DIR2 : paths.join(cd, cwd);
|
|
31050
31282
|
};
|
|
31051
|
-
var OPAQUE_RELEASE_FIX = `workit release --cwd ${
|
|
31283
|
+
var OPAQUE_RELEASE_FIX = `workit release --cwd ${DIR2} notes approve <version>`;
|
|
31052
31284
|
var inlineCallIn = (command, dialect = "posix") => {
|
|
31053
31285
|
if (!/\bworkit\b/u.test(command))
|
|
31054
31286
|
return null;
|
|
31055
|
-
const segments = segmentsOf(command, dialect);
|
|
31287
|
+
const segments = segmentsOf(command, dialect, { spans: true });
|
|
31056
31288
|
for (const [position, segment] of segments.entries()) {
|
|
31057
31289
|
if (segment.unanalyzable)
|
|
31058
31290
|
continue;
|
|
@@ -31060,8 +31292,10 @@ var inlineCallIn = (command, dialect = "posix") => {
|
|
|
31060
31292
|
if ((words[0] ?? "").endsWith("sh"))
|
|
31061
31293
|
for (const word of words.slice(1)) {
|
|
31062
31294
|
const inner = /\s/u.test(word) ? inlineCallIn(word, dialect) : null;
|
|
31295
|
+
if (inner?.plain)
|
|
31296
|
+
return { plain: false, fix: word.trim(), words: inner.words };
|
|
31063
31297
|
if (inner)
|
|
31064
|
-
return
|
|
31298
|
+
return inner;
|
|
31065
31299
|
}
|
|
31066
31300
|
const start = words.findIndex(isWorkitWord2);
|
|
31067
31301
|
if (start < 0)
|
|
@@ -31082,12 +31316,44 @@ var inlineCallIn = (command, dialect = "posix") => {
|
|
|
31082
31316
|
return { plain: true, words: routed(call, words.slice(1, at)), cwd: verb.cwd };
|
|
31083
31317
|
const last = cwds.at(-1);
|
|
31084
31318
|
const lastLiteral = last === undefined || literal(last);
|
|
31085
|
-
const
|
|
31086
|
-
const
|
|
31087
|
-
const
|
|
31088
|
-
const
|
|
31089
|
-
const
|
|
31090
|
-
|
|
31319
|
+
const before = segments.slice(0, position);
|
|
31320
|
+
const cd = leadingCd(before, command);
|
|
31321
|
+
const set = namesSet(before);
|
|
31322
|
+
const expanding = new Set(segment.expands ?? []);
|
|
31323
|
+
const texts = [];
|
|
31324
|
+
let prev;
|
|
31325
|
+
for (const index of keptIndexes(words, at)) {
|
|
31326
|
+
const span = segment.spans?.[index];
|
|
31327
|
+
if (span && span === prev)
|
|
31328
|
+
continue;
|
|
31329
|
+
prev = span;
|
|
31330
|
+
const local = expanding.has(index) ? [...(words[index] ?? "").matchAll(/\$\{?(\w+)/gu)].find((match) => set.has(match[1] ?? "")) : undefined;
|
|
31331
|
+
texts.push(local ? `<path of $${local[1]}>` : writtenWord(command, segment, index));
|
|
31332
|
+
}
|
|
31333
|
+
const file = producedFile(command, before, words) ?? undefined;
|
|
31334
|
+
const done = (fix) => ({
|
|
31335
|
+
plain: false,
|
|
31336
|
+
fix,
|
|
31337
|
+
words: call,
|
|
31338
|
+
...file && { file }
|
|
31339
|
+
});
|
|
31340
|
+
if (texts.includes(null)) {
|
|
31341
|
+
const verbs = call.slice(0, 2).join(" ");
|
|
31342
|
+
return done(release ? OPAQUE_RELEASE_FIX : `workit ${verbs} <its other words as you wrote them>`);
|
|
31343
|
+
}
|
|
31344
|
+
const [head = "", ...rest] = texts;
|
|
31345
|
+
const cwdTexts = cwds.map((index) => writtenWord(command, segment, index));
|
|
31346
|
+
const uncertain = !rest.every((text) => cwdIndependent(text)) || cwdTexts.some((text) => text === null || !cwdIndependent(text.replace(/^--cwd=/u, "")));
|
|
31347
|
+
if (cd && cd !== DIR2 && uncertain) {
|
|
31348
|
+
const dir = before[0] ? writtenWord(command, before[0], 1) : null;
|
|
31349
|
+
const all = writtenWords(command, segment, Array.from({ length: words.length - start - 1 }, (_, n) => start + 1 + n));
|
|
31350
|
+
if (dir !== null && all)
|
|
31351
|
+
return done(`cd ${dir} && workit ${all.join(" ")}`);
|
|
31352
|
+
}
|
|
31353
|
+
const cdLost = cd === undefined && !command.includes("||") && before.some((step) => step.words[0] === "cd");
|
|
31354
|
+
const cwd = cdLost && verb.cwd === null ? DIR2 : fixDir(verb.cwd, lastLiteral, cd, dialect);
|
|
31355
|
+
const where = cwd === null ? [] : ["--cwd", cwd === DIR2 ? DIR2 : quote2(cwd, dialect)];
|
|
31356
|
+
return done(["workit", head, ...where, ...rest].join(" "));
|
|
31091
31357
|
}
|
|
31092
31358
|
return null;
|
|
31093
31359
|
};
|
|
@@ -31096,7 +31362,16 @@ var inlineAskFor = (words, cwd) => {
|
|
|
31096
31362
|
return releaseNotesAskAt(words.slice(3), cwd);
|
|
31097
31363
|
return asked(words) && youTrackWritesMode(cwd) !== "auto" ? youTrackAsk(words.slice(1), cwd) : null;
|
|
31098
31364
|
};
|
|
31099
|
-
var
|
|
31365
|
+
var showable = (text) => !/`|[\p{Cf}\p{Zl}\p{Zp}]|[^\P{Cc}\n]/u.test(text);
|
|
31366
|
+
var inlineCallDeny = (fix, file) => {
|
|
31367
|
+
const call = showable(fix) ? `\`${fix}\`` : "the call, its words as you wrote them,";
|
|
31368
|
+
const run = `${call} as its own call so the host can ask the user.`;
|
|
31369
|
+
const first = file ? `An earlier step in this command writes ${showable(file) && !file.includes(`
|
|
31370
|
+
`) ? `\`${file}\`` : "the file the call reads"}; run that step as its own call first, then run ${run}` : `Run ${run}`;
|
|
31371
|
+
const segments = segmentsOf(fix.replace(/<[^>\n]*>/gu, "x"));
|
|
31372
|
+
const expands = segments.length > 1 || segments.some((segment) => segment.expands?.length) || /(?:^|\s)~[^/\s]/u.test(fix);
|
|
31373
|
+
return expands ? `${first} Spell out each word the shell would expand ($…, a glob, a cd): the host asks only on a call its text fixes.` : first;
|
|
31374
|
+
};
|
|
31100
31375
|
|
|
31101
31376
|
// packages/workit-core/src/hooks/hosts/claude-code.ts
|
|
31102
31377
|
var CLAUDE_CODE_DESCRIPTOR = {
|
|
@@ -31577,159 +31852,710 @@ function neededChecks(lane, configured) {
|
|
|
31577
31852
|
|
|
31578
31853
|
// packages/workit-core/src/hooks/worktree-confine.ts
|
|
31579
31854
|
import { createHash as createHash23 } from "node:crypto";
|
|
31580
|
-
import
|
|
31855
|
+
import fs31 from "node:fs";
|
|
31581
31856
|
import os11 from "node:os";
|
|
31857
|
+
import path51 from "node:path";
|
|
31858
|
+
|
|
31859
|
+
// packages/workit-core/src/hooks/identity-env.ts
|
|
31860
|
+
import fs30 from "node:fs";
|
|
31582
31861
|
import path50 from "node:path";
|
|
31583
|
-
var
|
|
31584
|
-
var
|
|
31585
|
-
var
|
|
31586
|
-
var
|
|
31587
|
-
|
|
31588
|
-
|
|
31862
|
+
var NAMES = "WORKIT_(?:AGENT_ID|HOST|SESSION_ID|SESSION_TOKEN)";
|
|
31863
|
+
var IDENTITY = new RegExp(`^${NAMES}$`, "u");
|
|
31864
|
+
var HOST_NAMES = "(?:OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDECODE|CLAUDE_CODE_ENTRYPOINT)";
|
|
31865
|
+
var HOST_VARIABLE = new RegExp(`^${HOST_NAMES}$`, "u");
|
|
31866
|
+
var IDENTITY_ANY = new RegExp(NAMES, "iu");
|
|
31867
|
+
var STATE_NAMES = "(?:WORKIT_(?:STATE_DIR|CONFIG(?:_DIR)?)|XDG_(?:STATE|CONFIG|DATA)_HOME|HOME|USERPROFILE|(?:LOCAL)?APPDATA)";
|
|
31868
|
+
var STATE2 = new RegExp(`^${STATE_NAMES}$`, "u");
|
|
31869
|
+
var STATE_ANY = new RegExp(STATE_NAMES, "iu");
|
|
31870
|
+
var powershell = (names) => new RegExp([
|
|
31871
|
+
`\\$\\{?env:${names}\\}?\\s*(?:[-+*/]|\\?\\?)?=(?!=)`,
|
|
31872
|
+
`${PS_ITEM}[^;|\\n]*?(?<![$\\w{])env:[\\\\/]?${names}\\b`,
|
|
31873
|
+
`SetEnvironmentVariable\\(\\s*['"]${names}\\b`
|
|
31874
|
+
].join("|"), "iu");
|
|
31875
|
+
var PS_ITEM = "\\b(?:(?:Set|New|Remove|Clear|Rename|Move|Copy)-Item(?:Property)?|(?:Set|Add|Clear)-Content|si|ni|ri|rm|del|erase|rd|rmdir|cli|sc|ac|clc|rni|mi|move|mv|cpi|copy|cp|ren)\\b";
|
|
31876
|
+
var POWERSHELL = powershell(NAMES);
|
|
31877
|
+
var POWERSHELL_STATE = powershell(STATE_NAMES);
|
|
31878
|
+
var POWERSHELL_HOST = powershell(HOST_NAMES);
|
|
31879
|
+
var POWERSHELL_OPAQUE = new RegExp([
|
|
31880
|
+
`SetEnvironmentVariable\\(\\s*(?:[^'"\\s]|"[^"]*\\$)`,
|
|
31881
|
+
`${PS_ITEM}[^;|\\n]*?(?<![$\\w{])env:[\\\\/]?["']?[$(]`,
|
|
31882
|
+
`(?:^|[;&|{(\\n])\\s*\\.\\s+[^\\s.]`,
|
|
31883
|
+
`\\b(?:iex|Invoke-Expression)\\b`
|
|
31884
|
+
].join("|"), "iu");
|
|
31885
|
+
var cmdSet = (names) => new RegExp(`(?:^|[&|(\\s"])set\\s+(?:/[ap]\\s+)?"?(${names})\\s*\\+?=`, "iu");
|
|
31886
|
+
var CMD_IDENTITY = cmdSet(NAMES);
|
|
31887
|
+
var CMD_STATE = cmdSet(STATE_NAMES);
|
|
31888
|
+
var CMD_HOST = cmdSet(HOST_NAMES);
|
|
31889
|
+
var RUNS_TEXT = /\bworkit(?:-cli)?\b|@workit-dev[\\/]cli/iu;
|
|
31890
|
+
var DECLARES = new Set(["export", "unset", "declare", "typeset", "local", "readonly"]);
|
|
31891
|
+
var OPAQUE = new Set([
|
|
31892
|
+
"watch",
|
|
31893
|
+
"strace",
|
|
31894
|
+
"chroot",
|
|
31895
|
+
"flock",
|
|
31896
|
+
"ssh",
|
|
31897
|
+
"su",
|
|
31898
|
+
"runuser",
|
|
31899
|
+
"script",
|
|
31900
|
+
"parallel"
|
|
31901
|
+
]);
|
|
31902
|
+
var ENV_VALUED = new Set(["u", "C", "S", "a", "P"]);
|
|
31903
|
+
var ENV_FLAGS = new Set(["i", "0", "v"]);
|
|
31904
|
+
var ENV_LONG_VALUED = new Set(["--unset", "--chdir", "--split-string", "--argv0"]);
|
|
31905
|
+
var ENV_LONG_FLAGS = /^--(?:ignore-environment|null|debug|(?:block|default|ignore)-signal(?:=.*)?)$/u;
|
|
31906
|
+
var base2 = (word) => (word?.split(/[\\/]/u).at(-1) ?? "").replace(/\.exe$/iu, "");
|
|
31907
|
+
var ASSIGN = /^[A-Za-z_]\w*\+?=/u;
|
|
31908
|
+
var nameOf = (word) => (word?.split("=")[0] ?? "").replace(/\+$/u, "");
|
|
31909
|
+
var named = (word) => IDENTITY.test(nameOf(word));
|
|
31910
|
+
var stateNamed = (word) => STATE2.test(nameOf(word));
|
|
31911
|
+
var isWorkit = (word) => /^workit(?:-cli)?(?:\.[cm]?js)?$/u.test(base2(word)) || /@workit-dev[\\/]cli\b|workit-cli[\\/](?:dist|src)[\\/]/u.test(word ?? "");
|
|
31912
|
+
var linksToWorkit = (word, cwd) => {
|
|
31913
|
+
const places = /[\\/]/u.test(word) ? [path50.resolve(cwd ?? process.cwd(), word)] : (process.env.PATH ?? "").split(path50.delimiter).map((dir) => path50.join(dir, word));
|
|
31914
|
+
for (const place of places) {
|
|
31589
31915
|
try {
|
|
31590
|
-
|
|
31591
|
-
|
|
31592
|
-
|
|
31593
|
-
return file;
|
|
31594
|
-
rest = rest ? path50.join(path50.basename(dir), rest) : path50.basename(dir);
|
|
31595
|
-
}
|
|
31916
|
+
const real = fs30.realpathSync(place);
|
|
31917
|
+
return isWorkit(real);
|
|
31918
|
+
} catch {}
|
|
31596
31919
|
}
|
|
31920
|
+
return false;
|
|
31597
31921
|
};
|
|
31598
|
-
var
|
|
31599
|
-
|
|
31600
|
-
|
|
31601
|
-
|
|
31602
|
-
|
|
31603
|
-
|
|
31604
|
-
if (part === "" || part === ".")
|
|
31605
|
-
continue;
|
|
31606
|
-
if (part === "..") {
|
|
31607
|
-
at = path50.dirname(at);
|
|
31608
|
-
continue;
|
|
31609
|
-
}
|
|
31610
|
-
const next = path50.join(at, part);
|
|
31611
|
-
let link = null;
|
|
31612
|
-
if (!missing)
|
|
31613
|
-
try {
|
|
31614
|
-
link = fs30.readlinkSync(next);
|
|
31615
|
-
} catch (error) {
|
|
31616
|
-
missing = error.code !== "EINVAL";
|
|
31617
|
-
}
|
|
31618
|
-
if (link === null) {
|
|
31619
|
-
at = next;
|
|
31620
|
-
hops.chain = 40;
|
|
31621
|
-
} else if (hops.chain-- > 0 && hops.total-- > 0)
|
|
31622
|
-
at = canonical6(path50.isAbsolute(link) ? link : `${at}${path50.sep}${link}`, hops);
|
|
31922
|
+
var newSeg = () => ({ identity: null, scoped: null, runs: false, words: [] });
|
|
31923
|
+
var UNREADABLE = "env/unset arguments Workit cannot read";
|
|
31924
|
+
var note = (scan, seg, word) => {
|
|
31925
|
+
if (scan.identity && named(word)) {
|
|
31926
|
+
if (seg)
|
|
31927
|
+
seg.identity ??= nameOf(word);
|
|
31623
31928
|
else
|
|
31624
|
-
|
|
31929
|
+
scan.hit ??= nameOf(word);
|
|
31625
31930
|
}
|
|
31626
|
-
|
|
31627
|
-
|
|
31628
|
-
|
|
31629
|
-
|
|
31630
|
-
|
|
31631
|
-
|
|
31931
|
+
if (!stateNamed(word) && !(scan.identity && HOST_VARIABLE.test(nameOf(word))))
|
|
31932
|
+
return;
|
|
31933
|
+
if (seg)
|
|
31934
|
+
seg.scoped ??= nameOf(word);
|
|
31935
|
+
else
|
|
31936
|
+
scan.shell ??= nameOf(word);
|
|
31632
31937
|
};
|
|
31633
|
-
var
|
|
31634
|
-
|
|
31635
|
-
|
|
31636
|
-
|
|
31637
|
-
|
|
31638
|
-
|
|
31938
|
+
var mayBeWorkit = (words, cwd) => words.some(({ word, built }) => built || linksToWorkit(word, cwd));
|
|
31939
|
+
var scanEnv = (words, at, expands, scan, seg, depth) => {
|
|
31940
|
+
const risky = (what) => void (scan.risky ??= what);
|
|
31941
|
+
const unset = (name) => note(scan, seg, name);
|
|
31942
|
+
const split = (line, rest, built) => {
|
|
31943
|
+
if (line === undefined || depth >= 4)
|
|
31944
|
+
return risky(UNREADABLE);
|
|
31945
|
+
if (built || /[$`]/u.test(line))
|
|
31946
|
+
risky(UNREADABLE);
|
|
31947
|
+
const tokens = [];
|
|
31948
|
+
const expanded = new Set;
|
|
31949
|
+
try {
|
|
31950
|
+
for (const segment of segmentsOf(line)) {
|
|
31951
|
+
for (const index of segment.expands ?? [])
|
|
31952
|
+
expanded.add(tokens.length + index);
|
|
31953
|
+
tokens.push(...segment.words);
|
|
31954
|
+
}
|
|
31955
|
+
} catch {
|
|
31956
|
+
return risky(UNREADABLE);
|
|
31639
31957
|
}
|
|
31640
|
-
|
|
31641
|
-
|
|
31958
|
+
scanEnv([...tokens, ...words.slice(rest)], 0, expanded, scan, seg, depth + 1);
|
|
31959
|
+
};
|
|
31960
|
+
let options = true;
|
|
31961
|
+
for (let index = at;index < words.length; index += 1) {
|
|
31962
|
+
const word = words[index] ?? "";
|
|
31963
|
+
if (expands.has(index)) {
|
|
31964
|
+
risky(UNREADABLE);
|
|
31965
|
+
const built = options ? /^(?:--split-string=|-[\w]*S)(.+)$/su.exec(word) : null;
|
|
31966
|
+
if (built)
|
|
31967
|
+
return split(built[1], index + 1, true);
|
|
31968
|
+
continue;
|
|
31969
|
+
}
|
|
31970
|
+
if (options && word === "--") {
|
|
31971
|
+
options = false;
|
|
31972
|
+
continue;
|
|
31973
|
+
}
|
|
31974
|
+
if (options && word === "-") {
|
|
31975
|
+
risky("env -i");
|
|
31976
|
+
continue;
|
|
31977
|
+
}
|
|
31978
|
+
if (options && word.startsWith("--")) {
|
|
31979
|
+
const [name = "", value] = word.split(/=(.*)/su);
|
|
31980
|
+
if (name === "--ignore-environment")
|
|
31981
|
+
risky("env -i");
|
|
31982
|
+
else if (name === "--unset")
|
|
31983
|
+
unset(value ?? words[index += 1]);
|
|
31984
|
+
else if (name === "--split-string")
|
|
31985
|
+
return split(value ?? words[index + 1], index + (value === undefined ? 2 : 1), expands.has(index + (value === undefined ? 1 : 0)));
|
|
31986
|
+
else if (ENV_LONG_VALUED.has(name))
|
|
31987
|
+
index += value === undefined ? 1 : 0;
|
|
31988
|
+
else if (!ENV_LONG_FLAGS.test(word))
|
|
31989
|
+
risky(UNREADABLE);
|
|
31990
|
+
continue;
|
|
31991
|
+
}
|
|
31992
|
+
if (options && word.startsWith("-") && word.length > 1) {
|
|
31993
|
+
for (let at2 = 1;at2 < word.length; at2 += 1) {
|
|
31994
|
+
const letter = word[at2] ?? "";
|
|
31995
|
+
if (letter === "i")
|
|
31996
|
+
risky("env -i");
|
|
31997
|
+
else if (ENV_FLAGS.has(letter))
|
|
31998
|
+
continue;
|
|
31999
|
+
else if (ENV_VALUED.has(letter)) {
|
|
32000
|
+
const glued = word.slice(at2 + 1);
|
|
32001
|
+
if (!glued)
|
|
32002
|
+
index += 1;
|
|
32003
|
+
const value = glued || words[index];
|
|
32004
|
+
if (letter === "u")
|
|
32005
|
+
unset(value);
|
|
32006
|
+
if (letter === "S")
|
|
32007
|
+
return split(value, index + 1, expands.has(index));
|
|
32008
|
+
break;
|
|
32009
|
+
} else {
|
|
32010
|
+
risky(UNREADABLE);
|
|
32011
|
+
break;
|
|
32012
|
+
}
|
|
32013
|
+
}
|
|
32014
|
+
continue;
|
|
32015
|
+
}
|
|
32016
|
+
if (ASSIGN.test(word)) {
|
|
32017
|
+
unset(word);
|
|
32018
|
+
continue;
|
|
32019
|
+
}
|
|
32020
|
+
return commandAt(words.slice(index), scan, seg, new Set([...expands].filter((at2) => at2 >= index).map((at2) => at2 - index)));
|
|
31642
32021
|
}
|
|
31643
32022
|
};
|
|
31644
|
-
var
|
|
31645
|
-
|
|
31646
|
-
|
|
31647
|
-
|
|
31648
|
-
|
|
32023
|
+
var commandAt = (words, scan, seg, expands = new Set, from = 0) => {
|
|
32024
|
+
let index = from;
|
|
32025
|
+
while (index < words.length) {
|
|
32026
|
+
while (ASSIGN.test(words[index] ?? "")) {
|
|
32027
|
+
note(scan, seg, words[index]);
|
|
32028
|
+
index += 1;
|
|
32029
|
+
}
|
|
32030
|
+
const name = base2(words[index]).toLowerCase();
|
|
32031
|
+
if (name === "env")
|
|
32032
|
+
return scanEnv(words, index + 1, expands, scan, seg, 0);
|
|
32033
|
+
if (OPAQUE.has(name))
|
|
32034
|
+
return opaque(words.slice(index + 1), scan, seg, true);
|
|
32035
|
+
if (name === "busybox" || name === "toybox") {
|
|
32036
|
+
index += 1;
|
|
32037
|
+
continue;
|
|
32038
|
+
}
|
|
32039
|
+
const takes = WRAPPERS3[name];
|
|
32040
|
+
if (takes === undefined)
|
|
32041
|
+
break;
|
|
32042
|
+
index = pastOptions(words, index + 1, takes);
|
|
32043
|
+
}
|
|
32044
|
+
const word = words[index];
|
|
32045
|
+
if (word === undefined)
|
|
32046
|
+
return;
|
|
32047
|
+
seg.words.push({ word, built: expands.has(index) });
|
|
32048
|
+
const rest = words.slice(index + 1);
|
|
32049
|
+
if (isWorkit(word) || rest.some(isWorkit))
|
|
32050
|
+
seg.runs = true;
|
|
32051
|
+
if (!isWorkit(word) && rest.some((next) => base2(next).toLowerCase() === "env"))
|
|
32052
|
+
opaque(rest, scan, seg, false);
|
|
32053
|
+
const program = base2(word).toLowerCase();
|
|
32054
|
+
const shell = program === "pwsh" || program === "powershell";
|
|
32055
|
+
if (program === "cmd" || shell) {
|
|
32056
|
+
const at = rest.findIndex((next) => shell ? /^-(?:c|command)$/iu.test(next) : /^\/[ck]$/iu.test(next));
|
|
32057
|
+
if (at >= 0) {
|
|
32058
|
+
const script = rest.slice(at + 1).join(" ");
|
|
32059
|
+
const change = shell ? powershellChange(script, scan.identity) : cmdChange(script, scan);
|
|
32060
|
+
if (change)
|
|
32061
|
+
scan.hit ??= change;
|
|
32062
|
+
}
|
|
32063
|
+
}
|
|
32064
|
+
};
|
|
32065
|
+
var opaque = (rest, scan, seg, assigns) => {
|
|
32066
|
+
if (!rest.some(isWorkit))
|
|
32067
|
+
return;
|
|
32068
|
+
seg.runs = true;
|
|
32069
|
+
const changes = (word) => base2(word).toLowerCase() === "env" || DECLARES.has(word) || assigns && ASSIGN.test(word);
|
|
32070
|
+
if (rest.some(changes))
|
|
32071
|
+
scan.risky ??= UNREADABLE;
|
|
32072
|
+
if (rest.some((word) => base2(word).toLowerCase() === "env"))
|
|
32073
|
+
for (const word of rest)
|
|
32074
|
+
note(scan, seg, word);
|
|
32075
|
+
};
|
|
32076
|
+
var LOGIN_VALUED = {
|
|
32077
|
+
su: /[sgGwc]/u,
|
|
32078
|
+
runuser: /[sgGwuc]/u,
|
|
32079
|
+
script: /[cEIOBTmot]/u
|
|
32080
|
+
};
|
|
32081
|
+
var loginScript = (words) => {
|
|
32082
|
+
const at = words.findIndex((word) => /^(?:su|runuser|script)$/u.test(base2(word)));
|
|
32083
|
+
if (at < 0)
|
|
31649
32084
|
return null;
|
|
32085
|
+
const valued = LOGIN_VALUED[base2(words[at])] ?? /c/u;
|
|
32086
|
+
for (let index = at + 1;index < words.length; index += 1) {
|
|
32087
|
+
const word = words[index] ?? "";
|
|
32088
|
+
if (word === "--command")
|
|
32089
|
+
return words[index + 1] ?? null;
|
|
32090
|
+
if (word.startsWith("--command="))
|
|
32091
|
+
return word.slice("--command=".length);
|
|
32092
|
+
if (!/^-[^-]/u.test(word))
|
|
32093
|
+
continue;
|
|
32094
|
+
const ends = word.slice(1).search(valued) + 1;
|
|
32095
|
+
if (ends > 0 && word[ends] === "c")
|
|
32096
|
+
return word.slice(ends + 1) || (words[index + 1] ?? null);
|
|
31650
32097
|
}
|
|
32098
|
+
return null;
|
|
31651
32099
|
};
|
|
31652
|
-
var
|
|
31653
|
-
|
|
31654
|
-
var readBinding = (file) => {
|
|
32100
|
+
var scanCommand = (command, dialect, depth, scan) => {
|
|
32101
|
+
let segments;
|
|
31655
32102
|
try {
|
|
31656
|
-
|
|
31657
|
-
return typeof value.worktree === "string" ? value : null;
|
|
32103
|
+
segments = segmentsOf(command, dialect);
|
|
31658
32104
|
} catch {
|
|
31659
|
-
return
|
|
32105
|
+
return;
|
|
32106
|
+
}
|
|
32107
|
+
for (const { words, expands = [] } of segments) {
|
|
32108
|
+
const expanded = new Set(expands);
|
|
32109
|
+
const seg = newSeg();
|
|
32110
|
+
let at = 0;
|
|
32111
|
+
while (ASSIGN.test(words[at] ?? ""))
|
|
32112
|
+
at += 1;
|
|
32113
|
+
for (const word of words.slice(0, at))
|
|
32114
|
+
note(scan, at === words.length ? null : seg, word);
|
|
32115
|
+
let declare = at;
|
|
32116
|
+
if (["builtin", "command"].includes(words[declare] ?? ""))
|
|
32117
|
+
declare += 1;
|
|
32118
|
+
if (DECLARES.has(words[declare] ?? "")) {
|
|
32119
|
+
for (let index = declare + 1;index < words.length; index += 1) {
|
|
32120
|
+
note(scan, null, words[index]);
|
|
32121
|
+
if (expanded.has(index))
|
|
32122
|
+
scan.risky ??= UNREADABLE;
|
|
32123
|
+
}
|
|
32124
|
+
continue;
|
|
32125
|
+
}
|
|
32126
|
+
commandAt(words, scan, seg, expanded, at);
|
|
32127
|
+
const script = depth < 4 ? shellScript2(words) ?? loginScript(words) : null;
|
|
32128
|
+
if (script) {
|
|
32129
|
+
const before = scan.runs;
|
|
32130
|
+
scan.runs = false;
|
|
32131
|
+
scanCommand(script, dialect, depth + 1, scan);
|
|
32132
|
+
seg.runs ||= scan.runs;
|
|
32133
|
+
scan.runs ||= before;
|
|
32134
|
+
}
|
|
32135
|
+
scan.words.push(...seg.words);
|
|
32136
|
+
const reaches = () => seg.runs || mayBeWorkit(seg.words, scan.cwd);
|
|
32137
|
+
if (seg.identity && reaches())
|
|
32138
|
+
scan.hit ??= seg.identity;
|
|
32139
|
+
if (seg.scoped && reaches())
|
|
32140
|
+
scan.state ??= seg.scoped;
|
|
32141
|
+
scan.runs ||= seg.runs;
|
|
31660
32142
|
}
|
|
31661
32143
|
};
|
|
31662
|
-
var
|
|
31663
|
-
|
|
31664
|
-
|
|
32144
|
+
var cmdChange = (script, scan) => (scan.identity ? CMD_IDENTITY.exec(script)?.[1] : null) ?? (RUNS_TEXT.test(script) ? CMD_STATE.exec(script)?.[1] ?? (scan.identity ? CMD_HOST.exec(script)?.[1] : null) ?? null : null);
|
|
32145
|
+
var powershellChange = (command, identity) => {
|
|
32146
|
+
const hit = identity ? POWERSHELL.exec(command)?.[0] : null;
|
|
32147
|
+
if (hit)
|
|
32148
|
+
return hit;
|
|
32149
|
+
if (!RUNS_TEXT.test(command) && !/&\s*[$(]/u.test(command))
|
|
31665
32150
|
return null;
|
|
31666
|
-
const
|
|
31667
|
-
|
|
32151
|
+
const state = POWERSHELL_STATE.exec(command)?.[0] ?? (identity ? POWERSHELL_HOST.exec(command)?.[0] : null) ?? cmdChange(command, { identity });
|
|
32152
|
+
if (state)
|
|
32153
|
+
return state;
|
|
32154
|
+
return POWERSHELL_OPAQUE.test(command) ? UNREADABLE : null;
|
|
31668
32155
|
};
|
|
31669
|
-
var
|
|
31670
|
-
const
|
|
31671
|
-
|
|
31672
|
-
|
|
31673
|
-
|
|
31674
|
-
|
|
31675
|
-
|
|
31676
|
-
|
|
32156
|
+
var identityEnvChange = (command, dialect = "posix", options = {}) => {
|
|
32157
|
+
const identity = options.identity ?? true;
|
|
32158
|
+
if (dialect === "powershell")
|
|
32159
|
+
return powershellChange(command, identity);
|
|
32160
|
+
if (!/WORKIT_|XDG_|HOME|USERPROFILE|APPDATA|OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDE|EnvironmentVariable|\b(?:env|unset|export|declare|typeset|local|readonly)\b/iu.test(command))
|
|
32161
|
+
return null;
|
|
32162
|
+
const scan = {
|
|
32163
|
+
identity,
|
|
32164
|
+
cwd: options.cwd,
|
|
32165
|
+
hit: null,
|
|
32166
|
+
state: null,
|
|
32167
|
+
shell: null,
|
|
32168
|
+
risky: null,
|
|
32169
|
+
runs: false,
|
|
32170
|
+
words: []
|
|
31677
32171
|
};
|
|
31678
|
-
|
|
31679
|
-
|
|
31680
|
-
|
|
31681
|
-
|
|
31682
|
-
|
|
31683
|
-
|
|
31684
|
-
|
|
31685
|
-
|
|
31686
|
-
worktree
|
|
31687
|
-
});
|
|
31688
|
-
} catch {}
|
|
31689
|
-
};
|
|
31690
|
-
var hostIsolated = (cwd, agentId) => {
|
|
31691
|
-
try {
|
|
31692
|
-
if (agentId === null)
|
|
31693
|
-
return false;
|
|
31694
|
-
const checkout = checkoutOf3(cwd);
|
|
31695
|
-
if (!checkout)
|
|
31696
|
-
return false;
|
|
31697
|
-
const dotGit = path50.join(checkout.root, ".git");
|
|
31698
|
-
if (!fs30.statSync(dotGit).isFile())
|
|
31699
|
-
return false;
|
|
31700
|
-
const [adminDir, gitDir] = gitDirsAt(checkout.root, null, false) ?? [];
|
|
31701
|
-
const back = fs30.readFileSync(path50.join(adminDir, "gitdir"), "utf8").trim();
|
|
31702
|
-
if (path50.relative(canonical6(path50.resolve(adminDir, back)), canonical6(dotGit)) !== "")
|
|
31703
|
-
return false;
|
|
31704
|
-
const expected = path50.join(path50.dirname(gitDir), ".claude", "worktrees", `agent-${agentId}`);
|
|
31705
|
-
return path50.relative(canonical6(expected), checkout.root) === "";
|
|
31706
|
-
} catch {
|
|
31707
|
-
return false;
|
|
31708
|
-
}
|
|
31709
|
-
};
|
|
31710
|
-
var runnerAt = (target) => {
|
|
31711
|
-
const base = path50.basename(target);
|
|
31712
|
-
return base.startsWith(OPAQUE_TARGET) ? decodeURIComponent(base.slice(OPAQUE_TARGET.length)) : null;
|
|
32172
|
+
scanCommand(command, dialect, 0, scan);
|
|
32173
|
+
if (scan.hit)
|
|
32174
|
+
return scan.hit;
|
|
32175
|
+
if (scan.state)
|
|
32176
|
+
return scan.state;
|
|
32177
|
+
if (scan.shell && (scan.runs || mayBeWorkit(scan.words, scan.cwd)))
|
|
32178
|
+
return scan.shell;
|
|
32179
|
+
return scan.runs ? scan.risky : null;
|
|
31713
32180
|
};
|
|
31714
|
-
var
|
|
31715
|
-
|
|
31716
|
-
|
|
32181
|
+
var identityEnvDeny = (change) => {
|
|
32182
|
+
if (change === UNREADABLE || IDENTITY_ANY.test(change) || !STATE_ANY.test(change))
|
|
32183
|
+
return `workit identity: ${change === UNREADABLE ? `this command runs workit with ${UNREADABLE}` : `this command changes ${change}`}. The host sets WORKIT_HOST, WORKIT_SESSION_ID, WORKIT_AGENT_ID and its own session variables, and Workit acts as who they name; run it without changing them (a verifier or reviewer passes the --session and --token its start text names).`;
|
|
32184
|
+
return `workit identity: this command runs workit with ${change} changed. Workit reads issued sessions, grants and its ledger trust from the state and config dirs the host's environment names, so a workit call may not move them (#980); run it without setting, unsetting or emptying ${change}.`;
|
|
31717
32185
|
};
|
|
31718
|
-
|
|
31719
|
-
|
|
31720
|
-
|
|
31721
|
-
|
|
31722
|
-
|
|
32186
|
+
|
|
32187
|
+
// packages/workit-core/src/hooks/exit-pipe.ts
|
|
32188
|
+
var EXIT_VERBS = [
|
|
32189
|
+
["ci", "wait"],
|
|
32190
|
+
["check"],
|
|
32191
|
+
["pr", "merge"],
|
|
32192
|
+
["release"],
|
|
32193
|
+
["verify-delivery"]
|
|
32194
|
+
];
|
|
32195
|
+
var base3 = (word = "") => word.split(/[\\/]/u).at(-1) ?? "";
|
|
32196
|
+
var verbPath = (words) => {
|
|
32197
|
+
const path = [];
|
|
32198
|
+
for (let at = 0;at < words.length && path.length < 2; at += 1) {
|
|
32199
|
+
const word = words[at];
|
|
32200
|
+
if (word === "--")
|
|
32201
|
+
break;
|
|
32202
|
+
if (word === "--cwd")
|
|
32203
|
+
at += 1;
|
|
32204
|
+
else if (!word.startsWith("-"))
|
|
32205
|
+
path.push(word);
|
|
32206
|
+
}
|
|
32207
|
+
return path;
|
|
32208
|
+
};
|
|
32209
|
+
var identityAssignment = (name) => identityEnvChange(`${name}=x workit`) !== null;
|
|
32210
|
+
var identityWhy = (names) => names.length ? `
|
|
32211
|
+
Why: the retry leaves out ${names.join(", ")}: the identity guard refuses a workit call that sets ${names.length === 1 ? "it" : "them"}.` : "";
|
|
32212
|
+
var exitCall = (segment, first) => {
|
|
32213
|
+
const { words } = segment;
|
|
32214
|
+
let at = first;
|
|
32215
|
+
while (at < words.length && /^[A-Za-z_]\w*=/u.test(words[at]))
|
|
32216
|
+
at += 1;
|
|
32217
|
+
if (!/^workit(?:-cli)?$/u.test(base3(words[at])))
|
|
32218
|
+
return null;
|
|
32219
|
+
const path = verbPath(words.slice(at + 1));
|
|
32220
|
+
return EXIT_VERBS.some((verb) => verb.every((word, index) => path[index] === word)) ? { segment, first, at } : null;
|
|
32221
|
+
};
|
|
32222
|
+
var backgrounded = (command, segment) => {
|
|
32223
|
+
const ends = [...segment.spans ?? [], ...segment.redirectSpans ?? []].map(([, end]) => end);
|
|
32224
|
+
return /^[ \t]*&(?![&>])/u.test(command.slice(Math.max(0, ...ends)));
|
|
32225
|
+
};
|
|
32226
|
+
var setsPipefail = (words) => words[0] === "set" && words.some((word, at) => word === "pipefail" && /^-[a-z]*o$/u.test(words[at - 1] ?? ""));
|
|
32227
|
+
var range = (from, to) => Array.from({ length: to - from }, (_, i) => from + i);
|
|
32228
|
+
var absoluteCwd = (text, value) => cwdIndependent(text) && (/^(?:\/|[A-Za-z]:[\\/])/u.test(value) || /^~(?:\/|$)/u.test(text));
|
|
32229
|
+
var retryOf = (command, found) => {
|
|
32230
|
+
const { segment, first, at, cd, set } = found;
|
|
32231
|
+
const { words } = segment;
|
|
32232
|
+
const dropped = [];
|
|
32233
|
+
const env = range(first, at).filter((index) => {
|
|
32234
|
+
const name = words[index].slice(0, words[index].indexOf("="));
|
|
32235
|
+
if (!identityAssignment(name))
|
|
32236
|
+
return true;
|
|
32237
|
+
dropped.push(name);
|
|
32238
|
+
return false;
|
|
32239
|
+
});
|
|
32240
|
+
const envText = writtenWords(command, segment, env);
|
|
32241
|
+
const callText = writtenWords(command, segment, range(at + 1, words.length));
|
|
32242
|
+
const dir = cd ? writtenWord(command, cd, 1) : "";
|
|
32243
|
+
if (!envText || !callText || cd === undefined || dir === null)
|
|
32244
|
+
return { retry: null, dropped };
|
|
32245
|
+
const reads = [dir, ...envText, ...callText].flatMap((text) => [...text.matchAll(/\$\{?(\w+)/gu)].map((match) => match[1] ?? "")).find((name) => set.has(name));
|
|
32246
|
+
if (reads)
|
|
32247
|
+
return { retry: null, dropped, reads };
|
|
32248
|
+
const call = words.slice(at + 1);
|
|
32249
|
+
const verbAt = call[0]?.startsWith("-") ? 0 : 1;
|
|
32250
|
+
const plain = (body) => [...envText, "workit", ...body].join(" ");
|
|
32251
|
+
if (!cd)
|
|
32252
|
+
return { retry: plain(callText), dropped };
|
|
32253
|
+
const check = verbPath(call)[0] === "check";
|
|
32254
|
+
const cwds = range(at + 1, words.length).filter((index) => words.slice(at + 1, index).every((word) => word !== "--") && (words[index - 1] === "--cwd" || words[index].startsWith("--cwd=")));
|
|
32255
|
+
const moves = certainDir(dir, cd.words[1] ?? "") && env.every((index, n) => cwdFreeAssignment(envText[n] ?? "", words[index])) && callText.every((text) => cwdIndependent(text)) && (!call.includes("--") || callText.includes("--") && cwdFreeTail(callText.slice(callText.indexOf("--") + 1), check)) && cwds.every((index) => {
|
|
32256
|
+
const text = writtenWord(command, segment, index) ?? "";
|
|
32257
|
+
return absoluteCwd(text.replace(/^--cwd=/u, ""), words[index].replace(/^--cwd=/u, ""));
|
|
32258
|
+
});
|
|
32259
|
+
if (!moves)
|
|
32260
|
+
return { retry: `cd ${dir} && ${plain(callText)}`, dropped };
|
|
32261
|
+
if (cwds.length)
|
|
32262
|
+
return { retry: plain(callText), dropped };
|
|
32263
|
+
return {
|
|
32264
|
+
retry: plain([...callText.slice(0, verbAt), "--cwd", dir, ...callText.slice(verbAt)]),
|
|
32265
|
+
dropped
|
|
32266
|
+
};
|
|
32267
|
+
};
|
|
32268
|
+
function exitPipeGate(command, dialect = "posix") {
|
|
32269
|
+
if (!/\bworkit/u.test(command) || !/\b(?:tail|head)\b/u.test(command))
|
|
32270
|
+
return null;
|
|
32271
|
+
const groups = [];
|
|
32272
|
+
let carry = null;
|
|
32273
|
+
let pipefail = false;
|
|
32274
|
+
let cd = null;
|
|
32275
|
+
const seen = [];
|
|
32276
|
+
const chained = !command.includes("||");
|
|
32277
|
+
for (const segment of segmentsOf(command, dialect, { spans: true })) {
|
|
32278
|
+
if (segment.unanalyzable)
|
|
32279
|
+
return null;
|
|
32280
|
+
let words = segment.words;
|
|
32281
|
+
let closes = segment.group === "close";
|
|
32282
|
+
if (segment.group === "open") {
|
|
32283
|
+
groups.push(null);
|
|
32284
|
+
continue;
|
|
32285
|
+
}
|
|
32286
|
+
if (words[0] === "{") {
|
|
32287
|
+
groups.push(null);
|
|
32288
|
+
words = words.slice(1);
|
|
32289
|
+
} else if (words[0] === "}") {
|
|
32290
|
+
closes = true;
|
|
32291
|
+
words = words.slice(1);
|
|
32292
|
+
}
|
|
32293
|
+
if (closes) {
|
|
32294
|
+
const inner = groups.pop() ?? null;
|
|
32295
|
+
if (segment.group === "close" && cd)
|
|
32296
|
+
cd = undefined;
|
|
32297
|
+
if (inner && groups.length)
|
|
32298
|
+
groups[groups.length - 1] = inner;
|
|
32299
|
+
carry = segment.pipe ? inner ?? carry : null;
|
|
32300
|
+
continue;
|
|
32301
|
+
}
|
|
32302
|
+
if (!words.length && !segment.stdin && !segment.subs)
|
|
32303
|
+
continue;
|
|
32304
|
+
if (setsPipefail(words))
|
|
32305
|
+
pipefail = true;
|
|
32306
|
+
const filter = base3(words[0]);
|
|
32307
|
+
if (carry && !pipefail && (filter === "tail" || filter === "head")) {
|
|
32308
|
+
const { retry, dropped, reads } = retryOf(command, carry);
|
|
32309
|
+
const verb = verbPath(carry.segment.words.slice(carry.at + 1)).join(" ");
|
|
32310
|
+
const why = reads ? `Why: an earlier step sets $${reads}, which the call reads: run the same command without the \`| ${filter}\`, that step kept.` : `Why: run the same call without the \`| ${filter}\`, its words as you wrote them.`;
|
|
32311
|
+
return {
|
|
32312
|
+
kind: "deny",
|
|
32313
|
+
reason: `workit exit-pipe: piping \`workit ${verb}\` into ${filter} hides its exit code.
|
|
32314
|
+
${retry ? `Retry as: ${retry}` : why}${identityWhy(dropped)}`,
|
|
32315
|
+
unblock: retry
|
|
32316
|
+
};
|
|
32317
|
+
}
|
|
32318
|
+
const call = exitCall(segment, segment.words.length - words.length);
|
|
32319
|
+
const found = call ? { ...call, cd, set: namesSet(seen) } : null;
|
|
32320
|
+
seen.push(segment);
|
|
32321
|
+
if (found && groups.length)
|
|
32322
|
+
groups[groups.length - 1] = found;
|
|
32323
|
+
carry = segment.pipe ? found ?? carry : null;
|
|
32324
|
+
const clean = chained && words === segment.words && words.length === 2 && !segment.pipe && !backgrounded(command, segment);
|
|
32325
|
+
if (words[0] === "cd")
|
|
32326
|
+
cd = clean && cd === null ? segment : undefined;
|
|
32327
|
+
else if (cd)
|
|
32328
|
+
cd = undefined;
|
|
32329
|
+
}
|
|
32330
|
+
return null;
|
|
32331
|
+
}
|
|
32332
|
+
|
|
32333
|
+
// packages/workit-core/src/hooks/worktree-confine.ts
|
|
32334
|
+
var FANOUT_TOKEN_FILE = "workit-fanout-token";
|
|
32335
|
+
var NONE5 = { kind: "none" };
|
|
32336
|
+
var CASE_INSENSITIVE2 = process.platform === "darwin" || process.platform === "win32";
|
|
32337
|
+
var realOf = (file) => {
|
|
32338
|
+
let rest = "";
|
|
32339
|
+
for (let dir = file;; dir = path51.dirname(dir)) {
|
|
32340
|
+
try {
|
|
32341
|
+
return path51.join(fs31.realpathSync.native(dir), rest);
|
|
32342
|
+
} catch {
|
|
32343
|
+
if (path51.dirname(dir) === dir)
|
|
32344
|
+
return file;
|
|
32345
|
+
rest = rest ? path51.join(path51.basename(dir), rest) : path51.basename(dir);
|
|
32346
|
+
}
|
|
32347
|
+
}
|
|
32348
|
+
};
|
|
32349
|
+
var canonical6 = (file, hops = { chain: 40, total: 1000 }) => {
|
|
32350
|
+
const start = path51.isAbsolute(file) ? file : `${process.cwd()}${path51.sep}${file}`;
|
|
32351
|
+
const { root } = path51.parse(start);
|
|
32352
|
+
let at = root;
|
|
32353
|
+
let missing = false;
|
|
32354
|
+
for (const part of start.slice(root.length).split(/[\\/]+/)) {
|
|
32355
|
+
if (part === "" || part === ".")
|
|
32356
|
+
continue;
|
|
32357
|
+
if (part === "..") {
|
|
32358
|
+
at = path51.dirname(at);
|
|
32359
|
+
continue;
|
|
32360
|
+
}
|
|
32361
|
+
const next = path51.join(at, part);
|
|
32362
|
+
let link = null;
|
|
32363
|
+
if (!missing)
|
|
32364
|
+
try {
|
|
32365
|
+
link = fs31.readlinkSync(next);
|
|
32366
|
+
} catch (error) {
|
|
32367
|
+
missing = error.code !== "EINVAL";
|
|
32368
|
+
}
|
|
32369
|
+
if (link === null) {
|
|
32370
|
+
at = next;
|
|
32371
|
+
hops.chain = 40;
|
|
32372
|
+
} else if (hops.chain-- > 0 && hops.total-- > 0)
|
|
32373
|
+
at = canonical6(path51.isAbsolute(link) ? link : `${at}${path51.sep}${link}`, hops);
|
|
32374
|
+
else
|
|
32375
|
+
return path51.resolve(file);
|
|
32376
|
+
}
|
|
32377
|
+
return realOf(at);
|
|
32378
|
+
};
|
|
32379
|
+
var within = (child, parent) => {
|
|
32380
|
+
const fold = (value) => CASE_INSENSITIVE2 ? value.toLowerCase() : value;
|
|
32381
|
+
const relative = path51.relative(fold(parent), fold(child));
|
|
32382
|
+
return relative === "" || !relative.startsWith("..") && !path51.isAbsolute(relative);
|
|
32383
|
+
};
|
|
32384
|
+
var checkoutOf3 = (file) => {
|
|
32385
|
+
for (let dir = canonical6(file);; dir = path51.dirname(dir)) {
|
|
32386
|
+
const dotGit = path51.join(dir, ".git");
|
|
32387
|
+
if (fs31.existsSync(dotGit)) {
|
|
32388
|
+
const admin = fs31.statSync(dotGit).isFile() ? gitDirsAt(dir, null, false)?.[0] : undefined;
|
|
32389
|
+
return { root: dir, workit: !!admin && fs31.existsSync(path51.join(admin, FANOUT_TOKEN_FILE)) };
|
|
32390
|
+
}
|
|
32391
|
+
if (path51.dirname(dir) === dir)
|
|
32392
|
+
return null;
|
|
32393
|
+
}
|
|
32394
|
+
};
|
|
32395
|
+
var workitWorktreeOf = (file) => {
|
|
32396
|
+
try {
|
|
32397
|
+
const checkout = checkoutOf3(file);
|
|
32398
|
+
return checkout?.workit ? checkout.root : null;
|
|
32399
|
+
} catch {
|
|
32400
|
+
return null;
|
|
32401
|
+
}
|
|
32402
|
+
};
|
|
32403
|
+
var bindingDir = () => path51.join(resolveStateDir(), "worktree-bind");
|
|
32404
|
+
var bindingFile = (session, agentId) => path51.join(bindingDir(), `${createHash23("sha256").update(`${session}\x00${agentId}`).digest("hex").slice(0, 32)}.json`);
|
|
32405
|
+
var readBinding = (file) => {
|
|
32406
|
+
try {
|
|
32407
|
+
const value = JSON.parse(fs31.readFileSync(file, "utf8"));
|
|
32408
|
+
return typeof value.worktree === "string" ? value : null;
|
|
32409
|
+
} catch {
|
|
32410
|
+
return null;
|
|
32411
|
+
}
|
|
32412
|
+
};
|
|
32413
|
+
var boundWorktree = (input) => {
|
|
32414
|
+
const agentId = input.session.agentId;
|
|
32415
|
+
if (!agentId)
|
|
32416
|
+
return null;
|
|
32417
|
+
const binding = readBinding(bindingFile(input.session.id, agentId));
|
|
32418
|
+
return binding && workitWorktreeOf(binding.worktree) === binding.worktree ? binding.worktree : null;
|
|
32419
|
+
};
|
|
32420
|
+
var bind = (input, worktree) => {
|
|
32421
|
+
const agentId = input.session.agentId;
|
|
32422
|
+
const file = bindingFile(input.session.id, agentId);
|
|
32423
|
+
const binding = {
|
|
32424
|
+
worktree,
|
|
32425
|
+
session: input.session.id,
|
|
32426
|
+
agentId,
|
|
32427
|
+
at: new Date().toISOString()
|
|
32428
|
+
};
|
|
32429
|
+
fs31.mkdirSync(path51.dirname(file), { recursive: true });
|
|
32430
|
+
fs31.writeFileSync(file, JSON.stringify(binding));
|
|
32431
|
+
try {
|
|
32432
|
+
if (inWorkitWorkspace(input.cwd))
|
|
32433
|
+
appendHookObserved(input.cwd, {
|
|
32434
|
+
type: "worktree.bound",
|
|
32435
|
+
actor: { host: input.host, session: input.session.id, agentId },
|
|
32436
|
+
agentType: input.session.agentType,
|
|
32437
|
+
worktree
|
|
32438
|
+
});
|
|
32439
|
+
} catch {}
|
|
32440
|
+
};
|
|
32441
|
+
var hostIsolated = (cwd, agentId) => {
|
|
32442
|
+
try {
|
|
32443
|
+
if (agentId === null)
|
|
32444
|
+
return false;
|
|
32445
|
+
const checkout = checkoutOf3(cwd);
|
|
32446
|
+
if (!checkout)
|
|
32447
|
+
return false;
|
|
32448
|
+
const dotGit = path51.join(checkout.root, ".git");
|
|
32449
|
+
if (!fs31.statSync(dotGit).isFile())
|
|
32450
|
+
return false;
|
|
32451
|
+
const [adminDir, gitDir] = gitDirsAt(checkout.root, null, false) ?? [];
|
|
32452
|
+
const back = fs31.readFileSync(path51.join(adminDir, "gitdir"), "utf8").trim();
|
|
32453
|
+
if (path51.relative(canonical6(path51.resolve(adminDir, back)), canonical6(dotGit)) !== "")
|
|
32454
|
+
return false;
|
|
32455
|
+
const expected = path51.join(path51.dirname(gitDir), ".claude", "worktrees", `agent-${agentId}`);
|
|
32456
|
+
return path51.relative(canonical6(expected), checkout.root) === "";
|
|
32457
|
+
} catch {
|
|
32458
|
+
return false;
|
|
32459
|
+
}
|
|
32460
|
+
};
|
|
32461
|
+
var runnerAt = (target) => {
|
|
32462
|
+
const base = path51.basename(target);
|
|
32463
|
+
return base.startsWith(OPAQUE_TARGET) ? decodeURIComponent(base.slice(OPAQUE_TARGET.length)) : null;
|
|
32464
|
+
};
|
|
32465
|
+
var shown = (target) => {
|
|
32466
|
+
const named = /^\.workit-(?:live-target$|runs-)/.test(path51.basename(target)) ? path51.dirname(target) : target;
|
|
32467
|
+
return path51.sep === "\\" ? named.replaceAll("/", "\\") : named;
|
|
32468
|
+
};
|
|
32469
|
+
var plainWorkitCall = (command, dialect) => {
|
|
32470
|
+
if (dialect !== "posix")
|
|
32471
|
+
return null;
|
|
32472
|
+
const segments = segmentsOf(command, dialect, { spans: true });
|
|
32473
|
+
const [only] = segments;
|
|
32474
|
+
if (segments.length !== 1 || !only || only.unanalyzable || only.group || only.pipe)
|
|
32475
|
+
return null;
|
|
32476
|
+
if (only.redirects.length || only.subs?.length || only.stdin?.length || only.inputs?.length)
|
|
32477
|
+
return null;
|
|
32478
|
+
const { words } = only;
|
|
32479
|
+
let at = 0;
|
|
32480
|
+
while (at < words.length && /^[A-Za-z_]\w*=/.test(words[at]))
|
|
32481
|
+
at++;
|
|
32482
|
+
if (words[at] !== "workit" || (only.expands ?? []).some((index) => index >= at))
|
|
32483
|
+
return null;
|
|
32484
|
+
return { segment: only, at, cwd: skipWorkitGlobals(words.slice(at + 1)).cwd };
|
|
32485
|
+
};
|
|
32486
|
+
var workitCallDir = (command, dialect) => {
|
|
32487
|
+
const call = plainWorkitCall(command, dialect);
|
|
32488
|
+
return call?.cwd && path51.isAbsolute(call.cwd) ? call.cwd : null;
|
|
32489
|
+
};
|
|
32490
|
+
var shellWord = (dir) => /^[\w@%+=:,./-]+$/u.test(dir) ? dir : `'${dir.replaceAll("'", `'\\''`)}'`;
|
|
32491
|
+
var cwdAt = (words, index) => words[index] === "--cwd" && words[index + 1] !== undefined ? 2 : words[index]?.startsWith("--cwd=") ? 1 : 0;
|
|
32492
|
+
var plainRetry = (dir, command) => {
|
|
32493
|
+
const call = plainWorkitCall(command, "posix");
|
|
32494
|
+
if (!call)
|
|
32495
|
+
return null;
|
|
32496
|
+
const { segment, at } = call;
|
|
32497
|
+
const { words } = segment;
|
|
32498
|
+
const dropped = [];
|
|
32499
|
+
const env = [];
|
|
32500
|
+
for (let index = 0;index < at; index++) {
|
|
32501
|
+
const name = words[index].slice(0, words[index].indexOf("="));
|
|
32502
|
+
if (identityAssignment(name))
|
|
32503
|
+
dropped.push(name);
|
|
32504
|
+
else
|
|
32505
|
+
env.push(index);
|
|
32506
|
+
}
|
|
32507
|
+
const globals = [];
|
|
32508
|
+
let index = at + 1;
|
|
32509
|
+
for (;index < words.length; index++) {
|
|
32510
|
+
const skip = cwdAt(words, index);
|
|
32511
|
+
if (skip)
|
|
32512
|
+
index += skip - 1;
|
|
32513
|
+
else if (/^--(?:session|token|as)$/u.test(words[index]) && words[index + 1] !== undefined)
|
|
32514
|
+
globals.push(index, ++index);
|
|
32515
|
+
else if (/^--(?:session|token|as)=|^--json$/u.test(words[index]))
|
|
32516
|
+
globals.push(index);
|
|
32517
|
+
else
|
|
32518
|
+
break;
|
|
32519
|
+
}
|
|
32520
|
+
const verb = index;
|
|
32521
|
+
const rest = [];
|
|
32522
|
+
for (index += 1;index < words.length; index++) {
|
|
32523
|
+
if (words[index] === "--") {
|
|
32524
|
+
for (;index < words.length; index++)
|
|
32525
|
+
rest.push(index);
|
|
32526
|
+
break;
|
|
32527
|
+
}
|
|
32528
|
+
const skip = cwdAt(words, index);
|
|
32529
|
+
if (skip)
|
|
32530
|
+
index += skip - 1;
|
|
32531
|
+
else
|
|
32532
|
+
rest.push(index);
|
|
32533
|
+
}
|
|
32534
|
+
const text = (indexes) => writtenWords(command, segment, indexes);
|
|
32535
|
+
const parts = [text(env), text(globals), text(verb < words.length ? [verb] : []), text(rest)];
|
|
32536
|
+
if (parts.includes(null))
|
|
32537
|
+
return null;
|
|
32538
|
+
const [envText, globalText, verbText, restText] = parts;
|
|
32539
|
+
const cwd = ["--cwd", shellWord(dir)];
|
|
32540
|
+
const body = [...globalText, ...verbText, ...cwd, ...restText];
|
|
32541
|
+
return { retry: [...envText, "workit", ...body].join(" "), dropped };
|
|
32542
|
+
};
|
|
32543
|
+
var runFrom = (dir, command) => `cd ${dir} && ${command.replace(/^\s*cd\s+\S+\s*(?:&&|;)\s*/, "")}`;
|
|
32544
|
+
var deny2 = (what, unblock, note = "") => ({
|
|
32545
|
+
kind: "deny",
|
|
32546
|
+
reason: `${what}.
|
|
32547
|
+
${unblock}${note}`,
|
|
31723
32548
|
unblock
|
|
31724
32549
|
});
|
|
31725
32550
|
var unboundDeny = (input, target) => deny2(`workit worktree confinement: implementer ${input.session.agentId} is not working in a Workit worktree yet, and ${shown(target)} is outside every one`, "Why: work only in your brief's worktree (`workit fanout worktree create <slice>`); with none, tell the lead.");
|
|
31726
32551
|
var outsideDeny = (input, worktree, target, command) => {
|
|
31727
32552
|
const runner = runnerAt(target);
|
|
31728
|
-
|
|
32553
|
+
const plain = runner !== null && command ? plainRetry(worktree, command) : null;
|
|
32554
|
+
return deny2(`workit worktree confinement: implementer ${input.session.agentId} is bound to the worktree ${worktree}, and ${runner === null ? `${shown(target)} is` : `\`${runner}\` runs in ${shown(target)},`} outside it`, runner !== null && command ? `Retry as: ${plain?.retry ?? runFrom(worktree, command)}` : `Why: edit only under ${worktree}, git one verb per call (\`workit git --cwd ${worktree} commit -m … -- <paths>\`); the rest is a follow-up.`, identityWhy(plain?.dropped ?? []));
|
|
31729
32555
|
};
|
|
31730
32556
|
var unknownDirDeny = (input, worktree) => deny2(`workit worktree confinement: implementer ${input.session.agentId} writes after a directory change Workit cannot resolve (\`cd -\`, \`cd "$X"\`, \`popd\`)`, `Why: write by absolute path, \`git -C ${worktree ?? "<worktree>"}\` or \`workit <verb> --cwd\`.`);
|
|
31731
32557
|
var refsOf = (targets) => [
|
|
31732
|
-
...new Set(targets.flatMap((target) =>
|
|
32558
|
+
...new Set(targets.flatMap((target) => path51.relative(UNKNOWN_VAR, path51.resolve(target)).split(path51.sep)[0].split("+").filter(Boolean).map(decodeURIComponent)))
|
|
31733
32559
|
];
|
|
31734
32560
|
var literalForm = (command, refs) => {
|
|
31735
32561
|
let text = command;
|
|
@@ -31762,10 +32588,10 @@ function confineImplementer(input, targets, command) {
|
|
|
31762
32588
|
try {
|
|
31763
32589
|
if (!input.session.agentId || hostIsolated(input.cwd, input.session.agentId))
|
|
31764
32590
|
return NONE5;
|
|
31765
|
-
const unresolved = targets.filter((target) => within(
|
|
32591
|
+
const unresolved = targets.filter((target) => within(path51.resolve(target), UNKNOWN_VAR));
|
|
31766
32592
|
if (unresolved.length > 0)
|
|
31767
32593
|
return unknownVarDeny(input, unresolved, command);
|
|
31768
|
-
if (targets.some((target) => within(
|
|
32594
|
+
if (targets.some((target) => within(path51.resolve(target), UNKNOWN_DIR)))
|
|
31769
32595
|
return unknownDirDeny(input, boundWorktree(input));
|
|
31770
32596
|
const bindings = canonical6(bindingDir());
|
|
31771
32597
|
const judged = targets.map((named) => ({ named, real: canonical6(named) })).filter(({ real }) => within(real, bindings) || checkoutOf3(real) !== null);
|
|
@@ -31857,7 +32683,7 @@ function confineImplementerShell(input, command, dialect = "posix") {
|
|
|
31857
32683
|
if (rawInvocations(command, dialect).some((invocation) => invocation.tool === "git" && leadOwnedGit(invocation.args)) || workitInvocations(command, dialect).some(leadOwnedWorkit))
|
|
31858
32684
|
return leadOwnedDeny(input, command);
|
|
31859
32685
|
} catch {}
|
|
31860
|
-
return confineImplementer(input, shellMutationTargets(input.cwd, command, dialect, true), command);
|
|
32686
|
+
return confineImplementer(input, shellMutationTargets(workitCallDir(command, dialect) ?? input.cwd, command, dialect, true), command);
|
|
31861
32687
|
}
|
|
31862
32688
|
var LITERAL_SET = /^\s*([A-Za-z_]\w*)=(['"]?)((?:[\w@%+=:./\\-]|(?<=[^\s:=])~)*)\2[ \t]*(?:;|&&|\n)\s*/;
|
|
31863
32689
|
var SPELLED = /^(?:[A-Za-z]|wt|wd|dir|repo|root|base|sha|ref|branch|file|out|scratch|worktree|WT|WD|DIR|REPO|ROOT|BASE|SHA|REF|BRANCH|FILE|OUT|SCRATCH|WORKTREE)$/;
|
|
@@ -31872,10 +32698,10 @@ var INLINE = /^-[a-zA-Z]*[ceEpm]$|^--(?:eval|print)(?:=|$)|^--(?:test|version|he
|
|
|
31872
32698
|
var VALUED2 = /^-[WXIrC]$|^--(?:require|import|loader|conditions|input-type)$/;
|
|
31873
32699
|
var commonDir = (root) => gitDirsAt(root, null, false)?.[1] ?? "";
|
|
31874
32700
|
var inScratch = (real, repo) => {
|
|
31875
|
-
for (let dir = real;
|
|
31876
|
-
if (
|
|
32701
|
+
for (let dir = real;path51.dirname(dir) !== dir; dir = path51.dirname(dir)) {
|
|
32702
|
+
if (path51.basename(dir) !== SCRATCH)
|
|
31877
32703
|
continue;
|
|
31878
|
-
const root =
|
|
32704
|
+
const root = path51.dirname(dir);
|
|
31879
32705
|
const checkout = checkoutOf3(root);
|
|
31880
32706
|
if (checkout?.root === root && (repo === null || checkout.workit || commonDir(root) === repo))
|
|
31881
32707
|
return true;
|
|
@@ -31885,7 +32711,7 @@ var inScratch = (real, repo) => {
|
|
|
31885
32711
|
var inGitDir = (real) => real.split(/[\\/]/).includes(".git");
|
|
31886
32712
|
var SAFE_KEY = /^(?:core\.(?:repositoryformatversion|filemode|bare|logallrefupdates|ignorecase|precomposeunicode|symlinks)|user\..*|init\.defaultbranch|commit\.gpgsign|extensions\.(?:objectformat|refstorage)|branch\..*\.(?:remote|merge)|remote\..*\.fetch)$/;
|
|
31887
32713
|
var literalIn = (value, repo, checkout = false) => {
|
|
31888
|
-
if (/[$`]/.test(value) || !
|
|
32714
|
+
if (/[$`]/.test(value) || !path51.isAbsolute(value))
|
|
31889
32715
|
return false;
|
|
31890
32716
|
const real = canonical6(value);
|
|
31891
32717
|
const root = checkout ? checkoutOf3(real)?.root : undefined;
|
|
@@ -31906,45 +32732,45 @@ var configEntries = (text) => {
|
|
|
31906
32732
|
return entries;
|
|
31907
32733
|
};
|
|
31908
32734
|
function gitDirsAt(dir, repo, scan = true) {
|
|
31909
|
-
for (let at = dir;; at =
|
|
31910
|
-
const dotGit =
|
|
32735
|
+
for (let at = dir;; at = path51.dirname(at)) {
|
|
32736
|
+
const dotGit = path51.join(at, ".git");
|
|
31911
32737
|
let gitDir = null;
|
|
31912
32738
|
try {
|
|
31913
|
-
if (
|
|
32739
|
+
if (fs31.statSync(dotGit).isDirectory())
|
|
31914
32740
|
gitDir = canonical6(dotGit);
|
|
31915
32741
|
else {
|
|
31916
|
-
const named = /^gitdir:\s*(.+?)\s*$/mu.exec(
|
|
32742
|
+
const named = /^gitdir:\s*(.+?)\s*$/mu.exec(fs31.readFileSync(dotGit, "utf8"))?.[1];
|
|
31917
32743
|
if (!named)
|
|
31918
32744
|
return null;
|
|
31919
|
-
gitDir = canonical6(
|
|
32745
|
+
gitDir = canonical6(path51.resolve(at, named));
|
|
31920
32746
|
}
|
|
31921
32747
|
} catch (error) {
|
|
31922
32748
|
if (error.code !== "ENOENT")
|
|
31923
32749
|
return null;
|
|
31924
|
-
if (["HEAD", "objects", "refs"].every((name) =>
|
|
32750
|
+
if (["HEAD", "objects", "refs"].every((name) => fs31.existsSync(path51.join(at, name))))
|
|
31925
32751
|
gitDir = canonical6(at);
|
|
31926
32752
|
}
|
|
31927
32753
|
if (gitDir !== null) {
|
|
31928
|
-
const common =
|
|
31929
|
-
const shared =
|
|
32754
|
+
const common = path51.join(gitDir, "commondir");
|
|
32755
|
+
const shared = fs31.existsSync(common) ? canonical6(path51.resolve(gitDir, fs31.readFileSync(common, "utf8").trim())) : gitDir;
|
|
31930
32756
|
const dirs = [gitDir, shared];
|
|
31931
|
-
for (const config of scan ? new Set([
|
|
32757
|
+
for (const config of scan ? new Set([path51.join(gitDir, "config"), path51.join(shared, "config")]) : []) {
|
|
31932
32758
|
let text = "";
|
|
31933
32759
|
try {
|
|
31934
|
-
text =
|
|
32760
|
+
text = fs31.readFileSync(config, "utf8");
|
|
31935
32761
|
} catch {
|
|
31936
32762
|
continue;
|
|
31937
32763
|
}
|
|
31938
32764
|
for (const [key, value] of configEntries(text)) {
|
|
31939
32765
|
if (key === "core.worktree")
|
|
31940
|
-
dirs.push(canonical6(
|
|
32766
|
+
dirs.push(canonical6(path51.resolve(gitDir, value)));
|
|
31941
32767
|
else if (/^remote\..*\.url$/.test(key) ? !literalIn(value, repo, true) : !SAFE_KEY.test(key))
|
|
31942
32768
|
return null;
|
|
31943
32769
|
}
|
|
31944
32770
|
}
|
|
31945
32771
|
return dirs;
|
|
31946
32772
|
}
|
|
31947
|
-
if (
|
|
32773
|
+
if (path51.dirname(at) === at)
|
|
31948
32774
|
return [];
|
|
31949
32775
|
}
|
|
31950
32776
|
}
|
|
@@ -32084,10 +32910,10 @@ var gitKind = (args, local = () => true) => {
|
|
|
32084
32910
|
return GIT_VERBS[verb].startsWith("+") ? "write" : "read";
|
|
32085
32911
|
};
|
|
32086
32912
|
var remoteIn = (dir, name, repo) => {
|
|
32087
|
-
const shared =
|
|
32913
|
+
const shared = path51.isAbsolute(dir) ? gitDirsAt(canonical6(dir), repo, false)?.[1] : undefined;
|
|
32088
32914
|
if (!shared)
|
|
32089
32915
|
return false;
|
|
32090
|
-
const url = configEntries(
|
|
32916
|
+
const url = configEntries(fs31.readFileSync(path51.join(shared, "config"), "utf8")).find(([key]) => key === `remote.${name}.url`.toLowerCase())?.[1];
|
|
32091
32917
|
return url !== undefined && literalIn(url, repo, true);
|
|
32092
32918
|
};
|
|
32093
32919
|
var judgeEnvAllowed = (word) => {
|
|
@@ -32116,7 +32942,7 @@ var judgeGit = (command, repo, cwd, depth = 0) => {
|
|
|
32116
32942
|
at = after;
|
|
32117
32943
|
}
|
|
32118
32944
|
}
|
|
32119
|
-
const name =
|
|
32945
|
+
const name = path51.basename(words[at] ?? "");
|
|
32120
32946
|
let start = words.findIndex((word) => /(?:^|[\\/])git(?:\.exe)?(?:=|$)/.test(word));
|
|
32121
32947
|
if (start > at) {
|
|
32122
32948
|
if (/^(?:grep|egrep|rg|echo|printf|ls|wc|head|tail|cat|which|type|test|\[|cd|mkdir)$/.test(name) || name === "command" && /^-[vV]$/.test(words[at + 1] ?? ""))
|
|
@@ -32127,7 +32953,7 @@ var judgeGit = (command, repo, cwd, depth = 0) => {
|
|
|
32127
32953
|
if (start >= 0 && words[start].includes("="))
|
|
32128
32954
|
why = "git is redefined";
|
|
32129
32955
|
const run = words[start] ?? "";
|
|
32130
|
-
if (run.includes("/") && (!
|
|
32956
|
+
if (run.includes("/") && (!path51.isAbsolute(run) || literalIn(run, repo, true)))
|
|
32131
32957
|
why ??= `\`${run}\` is not the system git`;
|
|
32132
32958
|
if (segment.expands?.includes(at) && name !== "[" || /\{[^}]*,/.test(words[at] ?? ""))
|
|
32133
32959
|
why = "the command's name is expanded by the shell";
|
|
@@ -32152,13 +32978,13 @@ var judgeGit = (command, repo, cwd, depth = 0) => {
|
|
|
32152
32978
|
const args = words[index] === "--version" ? ["version"] : words.slice(index);
|
|
32153
32979
|
if (!why && start >= 0) {
|
|
32154
32980
|
const local = (source, remote) => remote && /^[\w-][\w.-]*$/.test(source) ? remoteIn(dir, source, repo) : literalIn(source, repo, true);
|
|
32155
|
-
const runsIn =
|
|
32981
|
+
const runsIn = path51.isAbsolute(dir) ? dir : cwd && path51.resolve(cwd, dir);
|
|
32156
32982
|
const home = runsIn && checkoutOf3(runsIn);
|
|
32157
32983
|
const fetches = args[0] === "fetch" && plainFetch(args.slice(1)) && home && (home.workit || commonDir(home.root) === repo) && !words.some((word) => /^--[gw]/.test(word));
|
|
32158
32984
|
if (!fetches && !gitKind(args, local))
|
|
32159
32985
|
why = `\`git ${args.slice(0, 2).join(" ")}\` is not allowed`;
|
|
32160
32986
|
}
|
|
32161
|
-
const shell = /^(?:grep|egrep|rg|echo|printf|cat)$/.test(name) ? -1 : words.findIndex((word) => /^(?:sh|bash|zsh|dash|ksh|mksh|ash)$/.test(
|
|
32987
|
+
const shell = /^(?:grep|egrep|rg|echo|printf|cat)$/.test(name) ? -1 : words.findIndex((word) => /^(?:sh|bash|zsh|dash|ksh|mksh|ash)$/.test(path51.basename(word)));
|
|
32162
32988
|
const scripts = [
|
|
32163
32989
|
...words.filter((_, after) => shell >= 0 && after > shell && /^-[a-zA-Z]*c[a-zA-Z]*$/.test(words[after - 1])),
|
|
32164
32990
|
...shell < 0 ? [] : segment.stdin ?? []
|
|
@@ -32203,7 +33029,7 @@ var judgeVerbRefused = (args) => {
|
|
|
32203
33029
|
var CLI_RUNNERS = /^(?:npx|bunx|pnpx|npm|pnpm|yarn|bun|node|nodejs|deno|tsx|ts-node)$/;
|
|
32204
33030
|
var CLI_ENTRY = /(?:^|[=\\/])(?:workit(?:\.(?:exe|cmd|[cm]?js))?|@workit-dev\/cli(?:@\S*)?)$|(?:workit-cli|@workit-dev[\\/]cli)[\\/](?:src[\\/](?:index|main)|dist[\\/]index)\.[cm]?[jt]sx?$/;
|
|
32205
33031
|
var workitCalls = (command, depth = 0, aliases = new Map) => depth > 4 ? [] : commandLines(command).flatMap((words) => {
|
|
32206
|
-
const name =
|
|
33032
|
+
const name = path51.basename(words[0] ?? "").toLowerCase();
|
|
32207
33033
|
if (name === "alias")
|
|
32208
33034
|
return words.flatMap((word) => {
|
|
32209
33035
|
const set = /^([\w-]+)=(.*)$/s.exec(word);
|
|
@@ -32232,11 +33058,11 @@ var judgeRefusal = (command, fed, depth = 0) => {
|
|
|
32232
33058
|
return why;
|
|
32233
33059
|
}
|
|
32234
33060
|
const { words } = segment;
|
|
32235
|
-
const runs = (word = "") => RUNS.test(
|
|
33061
|
+
const runs = (word = "") => RUNS.test(path51.basename(word).toLowerCase());
|
|
32236
33062
|
let at = 0;
|
|
32237
33063
|
while (at < words.length && (LEADS.test(words[at]) || /^-\w$/.test(words[at - 1] ?? "") && !runs(words[at])))
|
|
32238
33064
|
at++;
|
|
32239
|
-
const name =
|
|
33065
|
+
const name = path51.basename(words[at] ?? "").toLowerCase();
|
|
32240
33066
|
const args = words.slice(at + 1);
|
|
32241
33067
|
const unseen = `\`${words.join(" ").slice(0, 80)}\` runs a script Workit cannot read (piped, on stdin, a process substitution or eval): write the case to a file in ${SCRATCH} and dispatch it from a test instead`;
|
|
32242
33068
|
if (/^(?:cd|chdir|pushd|popd)$/.test(name))
|
|
@@ -32318,10 +33144,10 @@ var judgeDeny = (input, what, target, retry, why = "write only there (mkdir -p i
|
|
|
32318
33144
|
const role = input.session.agentType?.endsWith("reviewer") ? "reviewer" : "verifier";
|
|
32319
33145
|
const root = checkoutOf3(input.cwd)?.root;
|
|
32320
33146
|
const candidate = target ? checkoutOf3(target)?.root : undefined;
|
|
32321
|
-
const other = root && candidate && candidate !== root && commonDir(candidate) === commonDir(root) ?
|
|
32322
|
-
const scratch = root ? `${
|
|
32323
|
-
const call = root ? retry?.((other ??
|
|
32324
|
-
const unblock = call ? `Retry as: ${call}` : `Why: ${why.replaceAll("<scratch>", other ?? (root ?
|
|
33147
|
+
const other = root && candidate && candidate !== root && commonDir(candidate) === commonDir(root) ? path51.join(candidate, SCRATCH) : undefined;
|
|
33148
|
+
const scratch = root ? `${path51.join(root, SCRATCH)}${other ? ` or ${other}` : ""}` : `${SCRATCH}/ at the root of the checkout you judge`;
|
|
33149
|
+
const call = root ? retry?.((other ?? path51.join(root, SCRATCH)).replaceAll("\\", "/")) : null;
|
|
33150
|
+
const unblock = call ? `Retry as: ${call}` : `Why: ${why.replaceAll("<scratch>", other ?? (root ? path51.join(root, SCRATCH) : SCRATCH))}`;
|
|
32325
33151
|
return {
|
|
32326
33152
|
kind: "deny",
|
|
32327
33153
|
reason: `workit scratch confinement: ${role} ${input.session.agentId} may write only in its scratch dir ${scratch}, and ${what}.
|
|
@@ -32331,9 +33157,9 @@ ${unblock}`,
|
|
|
32331
33157
|
};
|
|
32332
33158
|
var movedInto = (command, cwd, target, dir) => {
|
|
32333
33159
|
for (const { words, redirects } of segmentsOf(command))
|
|
32334
|
-
for (const word of [...redirects, ...
|
|
32335
|
-
if (
|
|
32336
|
-
const moved = command.replace(new RegExp(`((?:>>?\\|?|\\btee(?:\\s+-a)?)\\s*)(['"]?)${word.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&")}\\2(?![^\\s;&|)])`), (_, lead) => `${lead}${dir}/${
|
|
33160
|
+
for (const word of [...redirects, ...path51.basename(words[0] ?? "") === "tee" ? words : []])
|
|
33161
|
+
if (path51.resolve(cwd, word) === path51.resolve(cwd, target)) {
|
|
33162
|
+
const moved = command.replace(new RegExp(`((?:>>?\\|?|\\btee(?:\\s+-a)?)\\s*)(['"]?)${word.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&")}\\2(?![^\\s;&|)])`), (_, lead) => `${lead}${dir}/${path51.basename(word)}`);
|
|
32337
33163
|
return moved === command ? null : moved;
|
|
32338
33164
|
}
|
|
32339
33165
|
return null;
|
|
@@ -32345,19 +33171,19 @@ function confineJudge(input, targets, placeholders = placeholdersOf(targets), co
|
|
|
32345
33171
|
const judged = checkoutOf3(input.cwd);
|
|
32346
33172
|
const repo = judged ? commonDir(judged.root) : null;
|
|
32347
33173
|
const scratch = (real) => inScratch(real, repo) || [os11.tmpdir(), "/tmp"].some((dir) => {
|
|
32348
|
-
const [host, , session] =
|
|
33174
|
+
const [host, , session] = path51.relative(canonical6(dir), real).split(path51.sep);
|
|
32349
33175
|
return /^claude-\d+$/.test(host) && session === input.session.id;
|
|
32350
33176
|
});
|
|
32351
33177
|
for (const target of targets) {
|
|
32352
|
-
const resolved =
|
|
33178
|
+
const resolved = path51.resolve(target);
|
|
32353
33179
|
if (within(resolved, UNKNOWN_VAR) || within(resolved, UNKNOWN_DIR))
|
|
32354
33180
|
return judgeDeny(input, "it writes to a path Workit cannot resolve", undefined, () => {
|
|
32355
33181
|
const literal = command && literalForm(command, refsOf([target]));
|
|
32356
33182
|
return literal === command ? null : literal;
|
|
32357
33183
|
});
|
|
32358
33184
|
const real = canonical6(target);
|
|
32359
|
-
const runsGit = placeholders.has(target) && /^\.workit-(?:runs-git|live-target)$/.test(
|
|
32360
|
-
const gitDirs = runsGit ? gitDirsAt(
|
|
33185
|
+
const runsGit = placeholders.has(target) && /^\.workit-(?:runs-git|live-target)$/.test(path51.basename(target));
|
|
33186
|
+
const gitDirs = runsGit ? gitDirsAt(path51.dirname(real), repo) : [];
|
|
32361
33187
|
if (gitDirs === null)
|
|
32362
33188
|
return judgeDeny(input, `git in ${shown(target)} uses a git dir or config Workit cannot check`);
|
|
32363
33189
|
if (runsGit ? gitDirs.length > 0 ? gitDirs.every(scratch) : scratch(real) : scratch(real) && !inGitDir(real))
|
|
@@ -32425,7 +33251,7 @@ function confineJudgeShell(input, command, dialect = "posix") {
|
|
|
32425
33251
|
let git = [];
|
|
32426
33252
|
try {
|
|
32427
33253
|
git = rawInvocations(command, dialect).flatMap(({ tool, dir, args }) => tool === "git" && (args[0] === "push" || leadOwnedGit(args) || gitKind(args) === "write" && !/^(?:init|clone)$/.test(args[0]) && !(args[0] === "fetch" && plainFetch(args.slice(1)))) ? [
|
|
32428
|
-
|
|
33254
|
+
path51.join(dir === undefined ? UNKNOWN_DIR : path51.resolve(input.cwd, dir ?? "."), `${OPAQUE_TARGET}git`)
|
|
32429
33255
|
] : []);
|
|
32430
33256
|
} catch {}
|
|
32431
33257
|
const lists = [
|
|
@@ -32470,17 +33296,17 @@ var judgeChecksLine = (cwd) => {
|
|
|
32470
33296
|
|
|
32471
33297
|
// packages/workit-core/src/hooks/standing.ts
|
|
32472
33298
|
var readPlans = (store) => {
|
|
32473
|
-
const dir =
|
|
33299
|
+
const dir = path52.join(store, "fanouts");
|
|
32474
33300
|
let names;
|
|
32475
33301
|
try {
|
|
32476
|
-
names =
|
|
33302
|
+
names = fs32.readdirSync(dir).filter((name) => name.endsWith(".json"));
|
|
32477
33303
|
} catch {
|
|
32478
33304
|
return [];
|
|
32479
33305
|
}
|
|
32480
33306
|
const plans = [];
|
|
32481
33307
|
for (const name of names) {
|
|
32482
33308
|
try {
|
|
32483
|
-
const raw = JSON.parse(
|
|
33309
|
+
const raw = JSON.parse(fs32.readFileSync(path52.join(dir, name), "utf8"));
|
|
32484
33310
|
if (typeof raw.name !== "string" || !Array.isArray(raw.slices))
|
|
32485
33311
|
continue;
|
|
32486
33312
|
const texts = (value) => Array.isArray(value) ? value.filter((item) => typeof item === "string" && item !== "") : [];
|
|
@@ -32602,128 +33428,42 @@ var standingDelta = (input) => {
|
|
|
32602
33428
|
}
|
|
32603
33429
|
};
|
|
32604
33430
|
var sinceStart = new Map;
|
|
32605
|
-
// packages/workit-core/src/hooks/
|
|
32606
|
-
|
|
32607
|
-
|
|
32608
|
-
|
|
32609
|
-
|
|
32610
|
-
|
|
32611
|
-
|
|
32612
|
-
|
|
32613
|
-
var
|
|
32614
|
-
|
|
32615
|
-
|
|
32616
|
-
|
|
32617
|
-
|
|
32618
|
-
|
|
32619
|
-
}
|
|
32620
|
-
|
|
32621
|
-
|
|
32622
|
-
|
|
32623
|
-
|
|
32624
|
-
|
|
32625
|
-
|
|
32626
|
-
|
|
32627
|
-
|
|
32628
|
-
|
|
32629
|
-
|
|
32630
|
-
|
|
32631
|
-
|
|
32632
|
-
|
|
32633
|
-
|
|
32634
|
-
|
|
32635
|
-
|
|
32636
|
-
|
|
32637
|
-
|
|
32638
|
-
const
|
|
32639
|
-
|
|
32640
|
-
|
|
32641
|
-
return bare[0] === "/" && head.words[at + 1] === bare ? rest : null;
|
|
32642
|
-
}
|
|
32643
|
-
if (head.words.some((w) => w.startsWith("--cwd=")))
|
|
32644
|
-
return null;
|
|
32645
|
-
const args = rest.slice("workit".length).trimStart();
|
|
32646
|
-
const verb = VERB_FIRST.exec(args);
|
|
32647
|
-
if (!verb?.[2] || !VERBS.has(verb[2]))
|
|
32648
|
-
return `workit --cwd ${dir} ${args}`.trimEnd();
|
|
32649
|
-
const json = verb[1] ? " --json" : "";
|
|
32650
|
-
return `workit ${verb[2]} --cwd ${dir}${json}${args.slice(verb[0].length)}`.trimEnd();
|
|
32651
|
-
};
|
|
32652
|
-
var verbFirst = (command) => {
|
|
32653
|
-
const match = GLOBALS_FIRST.exec(command);
|
|
32654
|
-
const [whole, lead, globals, verb] = match ?? [];
|
|
32655
|
-
if (!whole || !verb || !VERBS.has(verb) || !globals?.includes("--cwd"))
|
|
32656
|
-
return null;
|
|
32657
|
-
const segments = segmentsOf(command);
|
|
32658
|
-
if (segments.length !== 1 || !segments.every(clean))
|
|
32659
|
-
return null;
|
|
32660
|
-
return `${lead} ${verb}${globals}${command.slice(whole.length)}`;
|
|
32661
|
-
};
|
|
32662
|
-
var NPX = new RegExp(String.raw`^(\s*(?:cd\s+(?:${DIR2})\s*&&\s*)?)npx(?:\s+(?:-y|--yes))*\s+@workit-dev\/cli(?:@[\w.-]+)?(?=\s|$)`, "u");
|
|
32663
|
-
var npxWorkit = (command) => {
|
|
32664
|
-
const match = NPX.exec(command);
|
|
32665
|
-
if (!match)
|
|
32666
|
-
return null;
|
|
32667
|
-
const call = `${match[1]}workit${command.slice(match[0].length)}`;
|
|
32668
|
-
const left = segmentsOf(call).some((s) => s.unanalyzable || s.words.some((w) => /^(npx|@workit-dev\/cli(@\S*)?)$/u.test(w)));
|
|
32669
|
-
return left ? null : cwdRewrite(call) ?? verbFirst(call) ?? call;
|
|
32670
|
-
};
|
|
32671
|
-
var MUTATING_GIT2 = /^git( -\S+| -[Cc] \S+)* (commit|push|add|switch|checkout|merge|rebase|reset|cherry-pick|revert|branch .*(-[dDmM]|--delete|--move)\b|tag (?!-l\b|--list)|stash(?! (list|show)\b))/u;
|
|
32672
|
-
var cdWorkitSplit = (command) => {
|
|
32673
|
-
const match = COMPOUND2.exec(command);
|
|
32674
|
-
const dir = match?.[1];
|
|
32675
|
-
const rest = match?.[2];
|
|
32676
|
-
if (!dir || !rest || cwdRewrite(command) !== null)
|
|
32677
|
-
return null;
|
|
32678
|
-
const whole = segmentsOf(rest);
|
|
32679
|
-
const boundary = (head) => segmentsOf(head).every((s, at) => s.words.join("\x00") === whole[at]?.words.join("\x00"));
|
|
32680
|
-
for (const op of rest.matchAll(/\|\||;/gu)) {
|
|
32681
|
-
const head = rest.slice(0, op.index).trimEnd();
|
|
32682
|
-
const call = cwdRewrite(`cd ${dir} && ${head}`);
|
|
32683
|
-
if (!call || !boundary(head))
|
|
32684
|
-
continue;
|
|
32685
|
-
const next = segmentsOf(rest.slice(op.index + op[0].length))[0];
|
|
32686
|
-
return next && MUTATING_GIT2.test(next.words.join(" ")) ? call : null;
|
|
32687
|
-
}
|
|
32688
|
-
return null;
|
|
32689
|
-
};
|
|
32690
|
-
|
|
32691
|
-
// packages/workit-core/src/hooks/spawn-gate.ts
|
|
32692
|
-
import fs32 from "node:fs";
|
|
32693
|
-
import path53 from "node:path";
|
|
32694
|
-
|
|
32695
|
-
// packages/workit-core/src/hooks/model-limit.ts
|
|
32696
|
-
import { closeSync as closeSync4, openSync as openSync4, readdirSync as readdirSync8, readSync, statSync as statSync5 } from "node:fs";
|
|
32697
|
-
import os12 from "node:os";
|
|
32698
|
-
import path52 from "node:path";
|
|
32699
|
-
var MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
|
|
32700
|
-
var WEEKDAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
32701
|
-
var TIME_WORD = String.raw`(?:\d|(?:in|at|on|later|tomorrow|soon)\b|(?:${[...WEEKDAYS, ...MONTHS].join("|")})[a-z]*\b)`;
|
|
32702
|
-
var LIMIT_END = String.raw`(?=\s*(?:\|\d{10}\b|(?:[∙·•|,:\-–—.]\s*)?(?:$|\(?(?:resets?|try again)\s+${TIME_WORD})))`;
|
|
32703
|
-
var LIMIT_PATTERNS = [
|
|
32704
|
-
/\byou(?:'|\u2019)?ve hit your\b[^.\n]{0,40}\blimit\b/iu,
|
|
32705
|
-
new RegExp(String.raw`\bclaude(?: ai)? usage limit reached${LIMIT_END}`, "imu"),
|
|
32706
|
-
new RegExp(String.raw`\b(?:5-hour|five-hour|weekly|daily|session|opus|sonnet|fable|haiku) limit reached${LIMIT_END}`, "imu"),
|
|
32707
|
-
/\busage limit\b[^\n]{0,200}\b(?:try again|resets?) (?:in|at)\b/iu,
|
|
32708
|
-
/\byou exceeded your current quota\b|\binsufficient_quota\b/iu
|
|
32709
|
-
];
|
|
32710
|
-
var RATE_LIMIT = /\brate_limit(?:_error)?\b|"type":\s*"rate_limit|\bstatus(?:code)?["':\s]+429\b|^\s*(?:\w+Error:\s*)?429\b/imu;
|
|
32711
|
-
var LONG_WAIT_MS = 15 * 60000;
|
|
32712
|
-
var AT_TIME = new RegExp(String.raw`\b(?:resets?|reset at|try again at|available again at|retry at)\s+(?:at\s+|on\s+)?` + String.raw`(?:(?:${WEEKDAYS.join("|")})[a-z]*,?\s+)?` + String.raw`(?:(${MONTHS.join("|")})[a-z]*\.?\s+(\d{1,2})(?:st|nd|rd|th)?,?\s+(?:at\s+)?)?` + String.raw`(\d{1,2})(?::(\d{2}))?\s*(am|pm)?(?:\s*\(([A-Za-z_]+(?:/[A-Za-z_+-]+)*)\))?`, "iu");
|
|
32713
|
-
var IN_DURATION = /\b(?:try again|resets?|retry|available again)\s+in\s+((?:\d+(?:\.\d+)?\s*(?:days?|d|hours?|hrs?|h|minutes?|mins?|m|seconds?|secs?|s)\b[\s,]*(?:and\s+)?)+)/iu;
|
|
32714
|
-
var RETRY_AFTER = /\bretry[- ]after\b["':\s]+(\d+)/iu;
|
|
32715
|
-
var EPOCH = /\|(\d{10})\b/u;
|
|
32716
|
-
var UNIT_MS = [
|
|
32717
|
-
[/^d/u, 86400000],
|
|
32718
|
-
[/^h/u, 3600000],
|
|
32719
|
-
[/^m/u, 60000],
|
|
32720
|
-
[/^s/u, 1000]
|
|
32721
|
-
];
|
|
32722
|
-
var durationMs = (text) => {
|
|
32723
|
-
let total = 0;
|
|
32724
|
-
for (const [, amount, unit] of text.matchAll(/(\d+(?:\.\d+)?)\s*([a-z]+)/giu))
|
|
32725
|
-
total += Number(amount) * (UNIT_MS.find(([re]) => re.test(unit.toLowerCase()))?.[1] ?? 0);
|
|
32726
|
-
return total;
|
|
33431
|
+
// packages/workit-core/src/hooks/spawn-gate.ts
|
|
33432
|
+
import fs33 from "node:fs";
|
|
33433
|
+
import path54 from "node:path";
|
|
33434
|
+
|
|
33435
|
+
// packages/workit-core/src/hooks/model-limit.ts
|
|
33436
|
+
import { closeSync as closeSync4, openSync as openSync4, readdirSync as readdirSync8, readSync, statSync as statSync5 } from "node:fs";
|
|
33437
|
+
import os12 from "node:os";
|
|
33438
|
+
import path53 from "node:path";
|
|
33439
|
+
var MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
|
|
33440
|
+
var WEEKDAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
33441
|
+
var TIME_WORD = String.raw`(?:\d|(?:in|at|on|later|tomorrow|soon)\b|(?:${[...WEEKDAYS, ...MONTHS].join("|")})[a-z]*\b)`;
|
|
33442
|
+
var LIMIT_END = String.raw`(?=\s*(?:\|\d{10}\b|(?:[∙·•|,:\-–—.]\s*)?(?:$|\(?(?:resets?|try again)\s+${TIME_WORD})))`;
|
|
33443
|
+
var LIMIT_PATTERNS = [
|
|
33444
|
+
/\byou(?:'|\u2019)?ve hit your\b[^.\n]{0,40}\blimit\b/iu,
|
|
33445
|
+
new RegExp(String.raw`\bclaude(?: ai)? usage limit reached${LIMIT_END}`, "imu"),
|
|
33446
|
+
new RegExp(String.raw`\b(?:5-hour|five-hour|weekly|daily|session|opus|sonnet|fable|haiku) limit reached${LIMIT_END}`, "imu"),
|
|
33447
|
+
/\busage limit\b[^\n]{0,200}\b(?:try again|resets?) (?:in|at)\b/iu,
|
|
33448
|
+
/\byou exceeded your current quota\b|\binsufficient_quota\b/iu
|
|
33449
|
+
];
|
|
33450
|
+
var RATE_LIMIT = /\brate_limit(?:_error)?\b|"type":\s*"rate_limit|\bstatus(?:code)?["':\s]+429\b|^\s*(?:\w+Error:\s*)?429\b/imu;
|
|
33451
|
+
var LONG_WAIT_MS = 15 * 60000;
|
|
33452
|
+
var AT_TIME = new RegExp(String.raw`\b(?:resets?|reset at|try again at|available again at|retry at)\s+(?:at\s+|on\s+)?` + String.raw`(?:(?:${WEEKDAYS.join("|")})[a-z]*,?\s+)?` + String.raw`(?:(${MONTHS.join("|")})[a-z]*\.?\s+(\d{1,2})(?:st|nd|rd|th)?,?\s+(?:at\s+)?)?` + String.raw`(\d{1,2})(?::(\d{2}))?\s*(am|pm)?(?:\s*\(([A-Za-z_]+(?:/[A-Za-z_+-]+)*)\))?`, "iu");
|
|
33453
|
+
var IN_DURATION = /\b(?:try again|resets?|retry|available again)\s+in\s+((?:\d+(?:\.\d+)?\s*(?:days?|d|hours?|hrs?|h|minutes?|mins?|m|seconds?|secs?|s)\b[\s,]*(?:and\s+)?)+)/iu;
|
|
33454
|
+
var RETRY_AFTER = /\bretry[- ]after\b["':\s]+(\d+)/iu;
|
|
33455
|
+
var EPOCH = /\|(\d{10})\b/u;
|
|
33456
|
+
var UNIT_MS = [
|
|
33457
|
+
[/^d/u, 86400000],
|
|
33458
|
+
[/^h/u, 3600000],
|
|
33459
|
+
[/^m/u, 60000],
|
|
33460
|
+
[/^s/u, 1000]
|
|
33461
|
+
];
|
|
33462
|
+
var durationMs = (text) => {
|
|
33463
|
+
let total = 0;
|
|
33464
|
+
for (const [, amount, unit] of text.matchAll(/(\d+(?:\.\d+)?)\s*([a-z]+)/giu))
|
|
33465
|
+
total += Number(amount) * (UNIT_MS.find(([re]) => re.test(unit.toLowerCase()))?.[1] ?? 0);
|
|
33466
|
+
return total;
|
|
32727
33467
|
};
|
|
32728
33468
|
var zoneOffset = (zone, at) => {
|
|
32729
33469
|
const parts = new Intl.DateTimeFormat("en-US", {
|
|
@@ -32893,17 +33633,17 @@ var firstPrompt = (file) => {
|
|
|
32893
33633
|
var claudeProjectSlug = (cwd) => cwd.replace(/[^A-Za-z0-9]/gu, "-");
|
|
32894
33634
|
var listDir = (dir) => {
|
|
32895
33635
|
try {
|
|
32896
|
-
return readdirSync8(dir).map((name) =>
|
|
33636
|
+
return readdirSync8(dir).map((name) => path53.join(dir, name));
|
|
32897
33637
|
} catch {
|
|
32898
33638
|
return [];
|
|
32899
33639
|
}
|
|
32900
33640
|
};
|
|
32901
33641
|
var SCAN_MS = 30 * 60000;
|
|
32902
33642
|
var scanClaudeTranscripts = (cwd, env, now = new Date) => {
|
|
32903
|
-
const projects =
|
|
32904
|
-
const project =
|
|
33643
|
+
const projects = path53.join(env.CLAUDE_CONFIG_DIR || path53.join(env.HOME || os12.homedir(), ".claude"), "projects");
|
|
33644
|
+
const project = path53.join(projects, claudeProjectSlug(cwd));
|
|
32905
33645
|
const notes = [];
|
|
32906
|
-
const files = listDir(project).flatMap((entry) => entry.endsWith(".jsonl") ? [{ file: entry, worker: false }] : listDir(
|
|
33646
|
+
const files = listDir(project).flatMap((entry) => entry.endsWith(".jsonl") ? [{ file: entry, worker: false }] : listDir(path53.join(entry, "subagents")).filter((file) => file.endsWith(".jsonl")).map((file) => ({ file, worker: true })));
|
|
32907
33647
|
for (const { file, worker } of files)
|
|
32908
33648
|
try {
|
|
32909
33649
|
const stat = statSync5(file);
|
|
@@ -32918,7 +33658,7 @@ var scanClaudeTranscripts = (cwd, env, now = new Date) => {
|
|
|
32918
33658
|
if (!seen)
|
|
32919
33659
|
continue;
|
|
32920
33660
|
notes.push(worker ? redispatchNote(seen, {
|
|
32921
|
-
label: `worker ${
|
|
33661
|
+
label: `worker ${path53.basename(file, ".jsonl")}`,
|
|
32922
33662
|
prompt: firstPrompt(file)
|
|
32923
33663
|
}, now) : `Workit: this session hit a usage limit (${seen.reason}); ${seen.key} is marked exhausted until ${shortTime(seen.until)}. Claude Code runs only Anthropic models: Workit cannot switch this session, so continue on another host or after the reset.`);
|
|
32924
33664
|
} catch {}
|
|
@@ -33017,13 +33757,13 @@ var agentModelDecision = (cwd, tool, options = {}) => {
|
|
|
33017
33757
|
var MADE_WORKTREE = /^WORKTREE:[ \t]*(?!not made yet)\S(?![^\n]*not made yet)/mu;
|
|
33018
33758
|
var namesMade = (word) => {
|
|
33019
33759
|
try {
|
|
33020
|
-
const stat =
|
|
33760
|
+
const stat = fs33.statSync(word);
|
|
33021
33761
|
if (stat.isFile())
|
|
33022
|
-
return stat.size < 1e6 && MADE_WORKTREE.test(
|
|
33023
|
-
const dotGit =
|
|
33024
|
-
const link =
|
|
33025
|
-
const admin = link.isFile() && link.size <= 4096 && /^gitdir: *(.+)/u.exec(
|
|
33026
|
-
return !!admin &&
|
|
33762
|
+
return stat.size < 1e6 && MADE_WORKTREE.test(fs33.readFileSync(word, "utf8"));
|
|
33763
|
+
const dotGit = path54.join(word, ".git");
|
|
33764
|
+
const link = fs33.statSync(dotGit);
|
|
33765
|
+
const admin = link.isFile() && link.size <= 4096 && /^gitdir: *(.+)/u.exec(fs33.readFileSync(dotGit, "utf8"));
|
|
33766
|
+
return !!admin && fs33.existsSync(path54.resolve(word, admin[1].trim(), FANOUT_TOKEN_FILE));
|
|
33027
33767
|
} catch {
|
|
33028
33768
|
return false;
|
|
33029
33769
|
}
|
|
@@ -33068,9 +33808,9 @@ var spawnDecision = (descriptor, cwd, tool, permissionMode = null) => {
|
|
|
33068
33808
|
};
|
|
33069
33809
|
|
|
33070
33810
|
// packages/workit-core/src/hooks/server-gate.ts
|
|
33071
|
-
import
|
|
33072
|
-
import
|
|
33073
|
-
var procNoticeFile = (env, session) =>
|
|
33811
|
+
import fs34 from "node:fs";
|
|
33812
|
+
import path55 from "node:path";
|
|
33813
|
+
var procNoticeFile = (env, session) => path55.join(stateDir(env), "procs", "notices", `${session.replace(/[^A-Za-z0-9_.@+-]/gu, "-").slice(0, 120) || "none"}.json`);
|
|
33074
33814
|
var mb = (bytes) => `${Math.round(Number(bytes) / 1048576)} MB`;
|
|
33075
33815
|
function takeProcNotice(session, env = process.env) {
|
|
33076
33816
|
if (!session)
|
|
@@ -33078,12 +33818,12 @@ function takeProcNotice(session, env = process.env) {
|
|
|
33078
33818
|
const file = procNoticeFile(env, session);
|
|
33079
33819
|
const taken = `${file}.${process.pid}.taken`;
|
|
33080
33820
|
try {
|
|
33081
|
-
|
|
33821
|
+
fs34.renameSync(file, taken);
|
|
33082
33822
|
} catch {
|
|
33083
33823
|
return null;
|
|
33084
33824
|
}
|
|
33085
33825
|
try {
|
|
33086
|
-
const { notices } = JSON.parse(
|
|
33826
|
+
const { notices } = JSON.parse(fs34.readFileSync(taken, "utf8"));
|
|
33087
33827
|
const lines = (Array.isArray(notices) ? notices : []).flatMap((notice) => {
|
|
33088
33828
|
if (!notice || typeof notice !== "object")
|
|
33089
33829
|
return [];
|
|
@@ -33100,29 +33840,29 @@ function takeProcNotice(session, env = process.env) {
|
|
|
33100
33840
|
return null;
|
|
33101
33841
|
} finally {
|
|
33102
33842
|
try {
|
|
33103
|
-
|
|
33843
|
+
fs34.rmSync(taken, { force: true });
|
|
33104
33844
|
} catch {}
|
|
33105
33845
|
}
|
|
33106
33846
|
}
|
|
33107
33847
|
|
|
33108
33848
|
// packages/workit-core/src/hooks/teardown.ts
|
|
33109
33849
|
import { spawn } from "node:child_process";
|
|
33110
|
-
import
|
|
33111
|
-
import
|
|
33850
|
+
import fs35 from "node:fs";
|
|
33851
|
+
import path57 from "node:path";
|
|
33112
33852
|
|
|
33113
33853
|
// packages/workit-core/src/core/cli-locate.ts
|
|
33114
33854
|
import { closeSync as closeSync5, existsSync as existsSync18, openSync as openSync5, readSync as readSync2 } from "node:fs";
|
|
33115
|
-
import
|
|
33855
|
+
import path56 from "node:path";
|
|
33116
33856
|
var LAUNCHER_MARKER = "workit-owned-launcher";
|
|
33117
33857
|
var CLI_PACKAGE_PATH = ["node_modules", "@workit-dev", "cli"];
|
|
33118
33858
|
var installRoot = (env, platform = process.platform) => {
|
|
33119
33859
|
if (env.WORKIT_INSTALL_ROOT)
|
|
33120
33860
|
return env.WORKIT_INSTALL_ROOT;
|
|
33121
33861
|
const data = dataDir(env, platform);
|
|
33122
|
-
return platform === "win32" ?
|
|
33862
|
+
return platform === "win32" ? path56.join(data, "install") : data;
|
|
33123
33863
|
};
|
|
33124
|
-
var ownedLauncherPath = (env, platform = process.platform) =>
|
|
33125
|
-
var ownedCurrentEntry = (env, platform = process.platform) =>
|
|
33864
|
+
var ownedLauncherPath = (env, platform = process.platform) => path56.join(installRoot(env, platform), "bin", platform === "win32" ? "workit.cmd" : "workit");
|
|
33865
|
+
var ownedCurrentEntry = (env, platform = process.platform) => path56.join(installRoot(env, platform), "current", ...CLI_PACKAGE_PATH, "dist", "index.js");
|
|
33126
33866
|
var head = (file) => {
|
|
33127
33867
|
let fd;
|
|
33128
33868
|
try {
|
|
@@ -33149,7 +33889,7 @@ var locateWorkitCli = (options = {}) => {
|
|
|
33149
33889
|
source: "owned-launcher",
|
|
33150
33890
|
command: launcher,
|
|
33151
33891
|
args: [],
|
|
33152
|
-
binDir:
|
|
33892
|
+
binDir: path56.dirname(launcher)
|
|
33153
33893
|
};
|
|
33154
33894
|
const current = ownedCurrentEntry(env, platform);
|
|
33155
33895
|
if (exists(current))
|
|
@@ -33178,7 +33918,7 @@ var workitCliEnv = (located, env = process.env, platform = process.platform) =>
|
|
|
33178
33918
|
if (located.binDir) {
|
|
33179
33919
|
const key = pathKey(env);
|
|
33180
33920
|
const delimiter = platform === "win32" ? ";" : ":";
|
|
33181
|
-
const same = (dir) => platform === "win32" ?
|
|
33921
|
+
const same = (dir) => platform === "win32" ? path56.win32.normalize(dir).toLowerCase() === path56.win32.normalize(located.binDir).toLowerCase() : path56.posix.normalize(dir) === path56.posix.normalize(located.binDir);
|
|
33182
33922
|
const rest = (env[key] ?? "").split(delimiter).filter((dir) => dir && !same(dir));
|
|
33183
33923
|
patch[key] = [located.binDir, ...rest].join(delimiter);
|
|
33184
33924
|
}
|
|
@@ -33202,9 +33942,9 @@ var ENDS = {
|
|
|
33202
33942
|
};
|
|
33203
33943
|
var SAFE_SESSION = /^[\w.@+][\w.:@/+-]{0,127}$/u;
|
|
33204
33944
|
var HOST_SESSION_VARS2 = ["OPENCODE_SESSION_ID", "PI_SESSION_ID", "CODEX_THREAD_ID"];
|
|
33205
|
-
var teardownDir = (env) =>
|
|
33206
|
-
var teardownLog = (env) =>
|
|
33207
|
-
var reapStamp = (env) =>
|
|
33945
|
+
var teardownDir = (env) => path57.join(stateDir(env), "procs");
|
|
33946
|
+
var teardownLog = (env) => path57.join(teardownDir(env), "teardown.log");
|
|
33947
|
+
var reapStamp = (env) => path57.join(teardownDir(env), "reap-at-start.stamp");
|
|
33208
33948
|
function spawnDetached(request) {
|
|
33209
33949
|
try {
|
|
33210
33950
|
const shell = process.platform === "win32" && !/\.exe$/iu.test(request.command);
|
|
@@ -33231,13 +33971,13 @@ var teardownDeps = {
|
|
|
33231
33971
|
var openLog = (env, args) => {
|
|
33232
33972
|
try {
|
|
33233
33973
|
const file = teardownLog(env);
|
|
33234
|
-
|
|
33974
|
+
fs35.mkdirSync(path57.dirname(file), { recursive: true });
|
|
33235
33975
|
let size = 0;
|
|
33236
33976
|
try {
|
|
33237
|
-
size =
|
|
33977
|
+
size = fs35.statSync(file).size;
|
|
33238
33978
|
} catch {}
|
|
33239
|
-
const fd =
|
|
33240
|
-
|
|
33979
|
+
const fd = fs35.openSync(file, size > LOG_MAX_BYTES ? "w" : "a", 384);
|
|
33980
|
+
fs35.writeSync(fd, `${new Date(teardownDeps.now()).toISOString()} workit ${args.join(" ")}
|
|
33241
33981
|
`);
|
|
33242
33982
|
return fd;
|
|
33243
33983
|
} catch {
|
|
@@ -33249,7 +33989,7 @@ function runCli(args, env) {
|
|
|
33249
33989
|
try {
|
|
33250
33990
|
const cli = locateWorkitCli({ env });
|
|
33251
33991
|
const cwd = teardownDir(env);
|
|
33252
|
-
|
|
33992
|
+
fs35.mkdirSync(cwd, { recursive: true });
|
|
33253
33993
|
log = openLog(env, args);
|
|
33254
33994
|
const started = teardownDeps.spawn({
|
|
33255
33995
|
command: cli.command,
|
|
@@ -33259,7 +33999,7 @@ function runCli(args, env) {
|
|
|
33259
33999
|
log
|
|
33260
34000
|
});
|
|
33261
34001
|
if (!started && log !== null)
|
|
33262
|
-
|
|
34002
|
+
fs35.writeSync(log, ` not started
|
|
33263
34003
|
`);
|
|
33264
34004
|
return started;
|
|
33265
34005
|
} catch {
|
|
@@ -33267,7 +34007,7 @@ function runCli(args, env) {
|
|
|
33267
34007
|
} finally {
|
|
33268
34008
|
if (log !== null)
|
|
33269
34009
|
try {
|
|
33270
|
-
|
|
34010
|
+
fs35.closeSync(log);
|
|
33271
34011
|
} catch {}
|
|
33272
34012
|
}
|
|
33273
34013
|
}
|
|
@@ -33286,24 +34026,24 @@ function endSession(end, env = process.env) {
|
|
|
33286
34026
|
var claim2 = (file, now) => {
|
|
33287
34027
|
for (let attempt = 0;attempt < 2; attempt += 1)
|
|
33288
34028
|
try {
|
|
33289
|
-
|
|
34029
|
+
fs35.writeFileSync(file, String(now), { flag: "wx", mode: 384 });
|
|
33290
34030
|
return true;
|
|
33291
34031
|
} catch (error) {
|
|
33292
34032
|
if (error.code !== "EEXIST")
|
|
33293
34033
|
return false;
|
|
33294
34034
|
let held = Number.NaN;
|
|
33295
34035
|
try {
|
|
33296
|
-
held = Number(
|
|
34036
|
+
held = Number(fs35.readFileSync(file, "utf8"));
|
|
33297
34037
|
} catch {}
|
|
33298
34038
|
if (now - held < REAP_INTERVAL_MS)
|
|
33299
34039
|
return false;
|
|
33300
|
-
|
|
34040
|
+
fs35.rmSync(file, { force: true });
|
|
33301
34041
|
}
|
|
33302
34042
|
return false;
|
|
33303
34043
|
};
|
|
33304
34044
|
var fresh = (stamp, now) => {
|
|
33305
34045
|
try {
|
|
33306
|
-
const last = Number(
|
|
34046
|
+
const last = Number(fs35.readFileSync(stamp, "utf8"));
|
|
33307
34047
|
return now - last < REAP_INTERVAL_MS && last <= now;
|
|
33308
34048
|
} catch {
|
|
33309
34049
|
return false;
|
|
@@ -33316,7 +34056,7 @@ function reapAtStart(_cwd, env = process.env) {
|
|
|
33316
34056
|
try {
|
|
33317
34057
|
if (fresh(stamp, now))
|
|
33318
34058
|
return false;
|
|
33319
|
-
|
|
34059
|
+
fs35.mkdirSync(path57.dirname(stamp), { recursive: true });
|
|
33320
34060
|
if (!claim2(lock, now))
|
|
33321
34061
|
return false;
|
|
33322
34062
|
} catch {
|
|
@@ -33329,12 +34069,12 @@ function reapAtStart(_cwd, env = process.env) {
|
|
|
33329
34069
|
started = runCli(["stop", "--orphans", "--reason", "session-start"], env);
|
|
33330
34070
|
if (started) {
|
|
33331
34071
|
const temp = `${stamp}.${process.pid}`;
|
|
33332
|
-
|
|
33333
|
-
|
|
34072
|
+
fs35.writeFileSync(temp, String(now), { mode: 384 });
|
|
34073
|
+
fs35.renameSync(temp, stamp);
|
|
33334
34074
|
}
|
|
33335
34075
|
} catch {} finally {
|
|
33336
34076
|
try {
|
|
33337
|
-
|
|
34077
|
+
fs35.rmSync(lock, { force: true });
|
|
33338
34078
|
} catch {}
|
|
33339
34079
|
}
|
|
33340
34080
|
return started;
|
|
@@ -33913,118 +34653,26 @@ Retry as: ${retry}` : ""}`,
|
|
|
33913
34653
|
};
|
|
33914
34654
|
};
|
|
33915
34655
|
|
|
33916
|
-
// packages/workit-core/src/hooks/exit-pipe.ts
|
|
33917
|
-
var EXIT_VERBS = [
|
|
33918
|
-
["ci", "wait"],
|
|
33919
|
-
["check"],
|
|
33920
|
-
["pr", "merge"],
|
|
33921
|
-
["release"],
|
|
33922
|
-
["verify-delivery"]
|
|
33923
|
-
];
|
|
33924
|
-
var base2 = (word = "") => word.split(/[\\/]/u).at(-1) ?? "";
|
|
33925
|
-
var quote4 = (word) => /^[\w@%+=:,./-]+$/u.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
|
|
33926
|
-
var verbPath = (words) => {
|
|
33927
|
-
const path = [];
|
|
33928
|
-
for (let at = 0;at < words.length && path.length < 2; at += 1) {
|
|
33929
|
-
const word = words[at];
|
|
33930
|
-
if (word === "--")
|
|
33931
|
-
break;
|
|
33932
|
-
if (word === "--cwd")
|
|
33933
|
-
at += 1;
|
|
33934
|
-
else if (!word.startsWith("-"))
|
|
33935
|
-
path.push(word);
|
|
33936
|
-
}
|
|
33937
|
-
return path;
|
|
33938
|
-
};
|
|
33939
|
-
var exitCall = (words) => {
|
|
33940
|
-
let at = 0;
|
|
33941
|
-
while (at < words.length && /^[A-Za-z_]\w*=/u.test(words[at]))
|
|
33942
|
-
at += 1;
|
|
33943
|
-
if (!/^workit(?:-cli)?$/u.test(base2(words[at])))
|
|
33944
|
-
return null;
|
|
33945
|
-
const path = verbPath(words.slice(at + 1));
|
|
33946
|
-
return EXIT_VERBS.some((verb) => verb.every((word, index) => path[index] === word)) ? words.slice(at) : null;
|
|
33947
|
-
};
|
|
33948
|
-
var setsPipefail = (words) => words[0] === "set" && words.some((word, at) => word === "pipefail" && /^-[a-z]*o$/u.test(words[at - 1] ?? ""));
|
|
33949
|
-
var retryOf = ({ call, dir }) => {
|
|
33950
|
-
const at = call[1]?.startsWith("-") ? 1 : 2;
|
|
33951
|
-
const words = dir && !call.includes("--cwd") ? [...call.slice(0, at), "--cwd", dir, ...call.slice(at)] : call;
|
|
33952
|
-
return ["workit", ...words.slice(1)].map(quote4).join(" ");
|
|
33953
|
-
};
|
|
33954
|
-
function exitPipeGate(command, dialect = "posix") {
|
|
33955
|
-
if (!/\bworkit/u.test(command) || !/\b(?:tail|head)\b/u.test(command))
|
|
33956
|
-
return null;
|
|
33957
|
-
const groups = [];
|
|
33958
|
-
let carry = null;
|
|
33959
|
-
let pipefail = false;
|
|
33960
|
-
let dir = null;
|
|
33961
|
-
for (const segment of segmentsOf(command, dialect)) {
|
|
33962
|
-
if (segment.unanalyzable)
|
|
33963
|
-
return null;
|
|
33964
|
-
let words = segment.words;
|
|
33965
|
-
let closes = segment.group === "close";
|
|
33966
|
-
if (segment.group === "open") {
|
|
33967
|
-
groups.push(null);
|
|
33968
|
-
continue;
|
|
33969
|
-
}
|
|
33970
|
-
if (words[0] === "{") {
|
|
33971
|
-
groups.push(null);
|
|
33972
|
-
words = words.slice(1);
|
|
33973
|
-
} else if (words[0] === "}") {
|
|
33974
|
-
closes = true;
|
|
33975
|
-
words = words.slice(1);
|
|
33976
|
-
}
|
|
33977
|
-
if (closes) {
|
|
33978
|
-
const inner = groups.pop() ?? null;
|
|
33979
|
-
if (inner && groups.length)
|
|
33980
|
-
groups[groups.length - 1] = inner;
|
|
33981
|
-
carry = segment.pipe ? inner ?? carry : null;
|
|
33982
|
-
continue;
|
|
33983
|
-
}
|
|
33984
|
-
if (!words.length && !segment.stdin && !segment.subs)
|
|
33985
|
-
continue;
|
|
33986
|
-
if (setsPipefail(words))
|
|
33987
|
-
pipefail = true;
|
|
33988
|
-
const filter = base2(words[0]);
|
|
33989
|
-
if (carry && !pipefail && (filter === "tail" || filter === "head")) {
|
|
33990
|
-
const retry = retryOf(carry);
|
|
33991
|
-
return {
|
|
33992
|
-
kind: "deny",
|
|
33993
|
-
reason: `workit exit-pipe: piping \`workit ${verbPath(carry.call.slice(1)).join(" ")}\` into ${filter} hides its exit code.
|
|
33994
|
-
Retry as: ${retry}`,
|
|
33995
|
-
unblock: retry
|
|
33996
|
-
};
|
|
33997
|
-
}
|
|
33998
|
-
const call = exitCall(words);
|
|
33999
|
-
const found = call ? { call, dir } : null;
|
|
34000
|
-
if (found && groups.length)
|
|
34001
|
-
groups[groups.length - 1] = found;
|
|
34002
|
-
carry = segment.pipe ? found ?? carry : null;
|
|
34003
|
-
dir = words.length === 2 && words[0] === "cd" && !segment.expands?.length ? words[1] : null;
|
|
34004
|
-
}
|
|
34005
|
-
return null;
|
|
34006
|
-
}
|
|
34007
|
-
|
|
34008
34656
|
// packages/workit-core/src/agent-stamp.ts
|
|
34009
34657
|
import { randomBytes as randomBytes5 } from "node:crypto";
|
|
34010
|
-
import
|
|
34011
|
-
import
|
|
34658
|
+
import fs37 from "node:fs";
|
|
34659
|
+
import path59 from "node:path";
|
|
34012
34660
|
|
|
34013
34661
|
// packages/workit-core/src/hooks/stamp-hint.ts
|
|
34014
34662
|
import { createHash as createHash24 } from "node:crypto";
|
|
34015
|
-
import
|
|
34016
|
-
import
|
|
34663
|
+
import fs36 from "node:fs";
|
|
34664
|
+
import path58 from "node:path";
|
|
34017
34665
|
var stampHash = (text) => createHash24("sha256").update(text).digest("hex").slice(0, 32);
|
|
34018
|
-
var stampDir = (env, session) =>
|
|
34666
|
+
var stampDir = (env, session) => path58.join(stateDir(env), "agent-stamps", stampHash(session));
|
|
34019
34667
|
var callTag = (toolUseId) => stampHash(toolUseId).slice(0, 8);
|
|
34020
34668
|
var backgrounds = (command) => /(?:^|[^&<>])&(?![&>])/u.test(command);
|
|
34021
34669
|
var dropCallStamps = (env, session, toolUseId) => {
|
|
34022
34670
|
try {
|
|
34023
34671
|
const dir = stampDir(env, session);
|
|
34024
34672
|
const tag = `.${callTag(toolUseId)}-`;
|
|
34025
|
-
for (const name of
|
|
34673
|
+
for (const name of fs36.readdirSync(dir))
|
|
34026
34674
|
if (name.includes(tag))
|
|
34027
|
-
|
|
34675
|
+
fs36.rmSync(path58.join(dir, name), { force: true });
|
|
34028
34676
|
} catch {}
|
|
34029
34677
|
};
|
|
34030
34678
|
var STATE_NAME = "(?:WORKIT_(?:STATE_DIR|CONFIG)\\w*|XDG_(?:STATE|CONFIG|DATA)_HOME|HOME|USERPROFILE|(?:LOCAL)?APPDATA|OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDECODE|CLAUDE_CODE_ENTRYPOINT)";
|
|
@@ -34054,12 +34702,12 @@ var noteAgentStamps = (env, session, agentId, command, options = {}) => {
|
|
|
34054
34702
|
const call = options.toolUseId ? callTag(options.toolUseId) : randomBytes5(4).toString("hex");
|
|
34055
34703
|
try {
|
|
34056
34704
|
const dir = stampDir(env, session);
|
|
34057
|
-
|
|
34705
|
+
fs37.mkdirSync(dir, { recursive: true, mode: 448 });
|
|
34058
34706
|
const now = Date.now();
|
|
34059
|
-
for (const name of
|
|
34060
|
-
if (now -
|
|
34061
|
-
|
|
34062
|
-
calls.forEach((argv, index) =>
|
|
34707
|
+
for (const name of fs37.readdirSync(dir))
|
|
34708
|
+
if (now - fs37.statSync(path59.join(dir, name)).mtimeMs > STAMP_TTL_MS)
|
|
34709
|
+
fs37.rmSync(path59.join(dir, name), { force: true });
|
|
34710
|
+
calls.forEach((argv, index) => fs37.writeFileSync(path59.join(dir, `${stampHash(argv.join("\x00"))}.${agentId ?? ""}.${call}-${index}`), ""));
|
|
34063
34711
|
} catch {}
|
|
34064
34712
|
};
|
|
34065
34713
|
var dropAgentStamps = (env, session, toolUseId, command) => {
|
|
@@ -34069,7 +34717,7 @@ var dropAgentStamps = (env, session, toolUseId, command) => {
|
|
|
34069
34717
|
|
|
34070
34718
|
// packages/workit-core/src/hooks/hook-entry.ts
|
|
34071
34719
|
import { existsSync as existsSync19, readFileSync as readFileSync16, realpathSync as realpathSync14 } from "node:fs";
|
|
34072
|
-
import
|
|
34720
|
+
import path60 from "node:path";
|
|
34073
34721
|
var ENTRY_NAMES = /^(?:workit-hook(?:-[\w-]+)?\.(?:m?js|ts)|cursor-session-start\.js|workit-(?:codex|cursor)-hook|workit-cursor-session-start)(?:\.(?:cmd|ps1|exe))?$/iu;
|
|
34074
34722
|
var PACKAGE_ENTRIES = {
|
|
34075
34723
|
"claude-code": /^(?:bin\/workit-hook\.mjs|src\/(?:run|hook)\.ts|dist\/workit-hook\.js)$/u,
|
|
@@ -34110,7 +34758,7 @@ var MORE_WRAPPERS = new Set(["strace", "ltrace", "watch", "unshare", "firejail",
|
|
|
34110
34758
|
var baseName3 = (word) => (word.split(/[\\/]/u).at(-1) ?? "").replace(/\.exe$/iu, "").toLowerCase();
|
|
34111
34759
|
var packageAt = (dir) => {
|
|
34112
34760
|
try {
|
|
34113
|
-
const name = JSON.parse(readFileSync16(
|
|
34761
|
+
const name = JSON.parse(readFileSync16(path60.join(dir, "package.json"), "utf8")).name;
|
|
34114
34762
|
const short = /^@workit-dev\/(claude-code|codex|cursor|core)$/u.exec(String(name))?.[1];
|
|
34115
34763
|
if (short)
|
|
34116
34764
|
return short;
|
|
@@ -34121,7 +34769,7 @@ var packageAt = (dir) => {
|
|
|
34121
34769
|
[".cursor-plugin", "cursor"]
|
|
34122
34770
|
]) {
|
|
34123
34771
|
try {
|
|
34124
|
-
const name = JSON.parse(readFileSync16(
|
|
34772
|
+
const name = JSON.parse(readFileSync16(path60.join(dir, manifest, "plugin.json"), "utf8")).name;
|
|
34125
34773
|
if (name === "workit")
|
|
34126
34774
|
return short;
|
|
34127
34775
|
} catch {}
|
|
@@ -34129,12 +34777,12 @@ var packageAt = (dir) => {
|
|
|
34129
34777
|
return null;
|
|
34130
34778
|
};
|
|
34131
34779
|
var entryByPath = (file) => {
|
|
34132
|
-
let dir =
|
|
34133
|
-
for (let depth = 0;depth < 4; depth += 1, dir =
|
|
34780
|
+
let dir = path60.dirname(file);
|
|
34781
|
+
for (let depth = 0;depth < 4; depth += 1, dir = path60.dirname(dir)) {
|
|
34134
34782
|
const short = packageAt(dir);
|
|
34135
34783
|
if (short)
|
|
34136
|
-
return PACKAGE_ENTRIES[short]?.test(
|
|
34137
|
-
if (existsSync19(
|
|
34784
|
+
return PACKAGE_ENTRIES[short]?.test(path60.relative(dir, file).split(path60.sep).join("/")) ?? false;
|
|
34785
|
+
if (existsSync19(path60.join(dir, "package.json")))
|
|
34138
34786
|
return false;
|
|
34139
34787
|
}
|
|
34140
34788
|
return false;
|
|
@@ -34162,676 +34810,348 @@ var entryNamed = (word, scan) => {
|
|
|
34162
34810
|
const fd = /^\/(?:dev|proc\/self)\/fd\/(\d+)$/u.exec(text)?.[1];
|
|
34163
34811
|
if (fd !== undefined && scan.fds.has(fd))
|
|
34164
34812
|
return text;
|
|
34165
|
-
if (scan.dir === null && !
|
|
34813
|
+
if (scan.dir === null && !path60.isAbsolute(text))
|
|
34166
34814
|
return null;
|
|
34167
|
-
for (const file of expandGlob(
|
|
34815
|
+
for (const file of expandGlob(path60.resolve(scan.dir ?? "/", text))) {
|
|
34168
34816
|
if (scan.links.has(file) || entryByPath(file))
|
|
34169
|
-
return text;
|
|
34170
|
-
let real;
|
|
34171
|
-
try {
|
|
34172
|
-
real = realpathSync14(file);
|
|
34173
|
-
} catch {
|
|
34174
|
-
continue;
|
|
34175
|
-
}
|
|
34176
|
-
if (ENTRY_NAMES.test(baseName3(real)) || entryByPath(real))
|
|
34177
|
-
return text;
|
|
34178
|
-
}
|
|
34179
|
-
return null;
|
|
34180
|
-
};
|
|
34181
|
-
var noteLink = (words, scan) => {
|
|
34182
|
-
if (!/^(?:ln|cp|install)$/u.test(baseName3(words[0] ?? "")) || scan.dir === null)
|
|
34183
|
-
return;
|
|
34184
|
-
const names = words.slice(1).filter((word) => !word.startsWith("-"));
|
|
34185
|
-
const dest = names.at(-1);
|
|
34186
|
-
if (dest === undefined || names.length < 2)
|
|
34187
|
-
return;
|
|
34188
|
-
for (const source of names.slice(0, -1).filter((name) => entryNamed(name, scan))) {
|
|
34189
|
-
const to = path59.resolve(scan.dir, expand(dest, scan.vars));
|
|
34190
|
-
scan.links.add(to);
|
|
34191
|
-
scan.links.add(path59.join(to, path59.basename(source)));
|
|
34192
|
-
}
|
|
34193
|
-
};
|
|
34194
|
-
var runsEntry = (words, inputs, scan) => {
|
|
34195
|
-
const named = (word) => entryNamed(word, scan);
|
|
34196
|
-
let at = 0;
|
|
34197
|
-
while (at < words.length) {
|
|
34198
|
-
while (/^[A-Za-z_]\w*=/u.test(words[at] ?? ""))
|
|
34199
|
-
at += 1;
|
|
34200
|
-
const word = words[at];
|
|
34201
|
-
if (word === undefined)
|
|
34202
|
-
return null;
|
|
34203
|
-
const entry = named(word);
|
|
34204
|
-
if (entry)
|
|
34205
|
-
return entry;
|
|
34206
|
-
const name = baseName3(word);
|
|
34207
|
-
const split = name === "env" ? words.findIndex((w) => /^(?:-\w*S|--split-string)/u.test(w)) : -1;
|
|
34208
|
-
if (split > at) {
|
|
34209
|
-
const line = /^(?:-\w*S|--split-string=?)(.*)$/su.exec(words[split] ?? "")?.[1] || words[split + 1];
|
|
34210
|
-
const rest = words.slice(split + (line === words[split + 1] ? 2 : 1));
|
|
34211
|
-
return runsEntry([...(line ?? "").split(/\s+/u).filter(Boolean), ...rest], inputs, scan);
|
|
34212
|
-
}
|
|
34213
|
-
if (MORE_WRAPPERS.has(name)) {
|
|
34214
|
-
at = pastOptions(words, at + 1, "");
|
|
34215
|
-
continue;
|
|
34216
|
-
}
|
|
34217
|
-
const takes = WRAPPERS3[name];
|
|
34218
|
-
if (takes !== undefined && !PACKAGE_RUNNERS2.has(name)) {
|
|
34219
|
-
at = pastOptions(words, at + 1, takes, name === "env" || name === "sudo");
|
|
34220
|
-
continue;
|
|
34221
|
-
}
|
|
34222
|
-
if (name === "busybox" || name === "toybox") {
|
|
34223
|
-
at += 1;
|
|
34224
|
-
continue;
|
|
34225
|
-
}
|
|
34226
|
-
const runner = PACKAGE_RUNNERS2.has(name) || name === "bun" && words[at + 1] === "x" || /^(?:pnpm|yarn|npm)$/u.test(name) && /^(?:dlx|exec|x)$/u.test(words[at + 1] ?? "");
|
|
34227
|
-
if (runner) {
|
|
34228
|
-
at += PACKAGE_RUNNERS2.has(name) ? 1 : 2;
|
|
34229
|
-
while (words[at]?.startsWith("-"))
|
|
34230
|
-
at += /^(?:-p|--package|-c|--call)$/u.test(words[at] ?? "") ? 2 : 1;
|
|
34231
|
-
if (/^@workit-dev\/(?:claude-code|codex|cursor)(?:@|$)/u.test(words[at] ?? ""))
|
|
34232
|
-
return words[at] ?? null;
|
|
34233
|
-
continue;
|
|
34234
|
-
}
|
|
34235
|
-
if (!RUNNERS2.has(name))
|
|
34236
|
-
return null;
|
|
34237
|
-
if ((name === "bun" || name === "deno") && NO_RUN.has(words[at + 1] ?? ""))
|
|
34238
|
-
return null;
|
|
34239
|
-
for (const arg of [...words.slice(at + 1), ...inputs]) {
|
|
34240
|
-
const hit = named(arg);
|
|
34241
|
-
if (hit)
|
|
34242
|
-
return hit;
|
|
34243
|
-
}
|
|
34244
|
-
return null;
|
|
34245
|
-
}
|
|
34246
|
-
return null;
|
|
34247
|
-
};
|
|
34248
|
-
var KEYWORDS3 = new Set([
|
|
34249
|
-
"{",
|
|
34250
|
-
"}",
|
|
34251
|
-
"if",
|
|
34252
|
-
"then",
|
|
34253
|
-
"elif",
|
|
34254
|
-
"else",
|
|
34255
|
-
"fi",
|
|
34256
|
-
"while",
|
|
34257
|
-
"until",
|
|
34258
|
-
"do",
|
|
34259
|
-
"done",
|
|
34260
|
-
"esac",
|
|
34261
|
-
"!"
|
|
34262
|
-
]);
|
|
34263
|
-
var pastKeywords = (words) => {
|
|
34264
|
-
let at = 0;
|
|
34265
|
-
for (;; ) {
|
|
34266
|
-
if (words[at] === "function")
|
|
34267
|
-
at += 2;
|
|
34268
|
-
else if (KEYWORDS3.has(words[at] ?? ""))
|
|
34269
|
-
at += 1;
|
|
34270
|
-
else
|
|
34271
|
-
break;
|
|
34272
|
-
}
|
|
34273
|
-
return /^(?:for|case|select)$/u.test(words[at] ?? "") ? [] : words.slice(at);
|
|
34274
|
-
};
|
|
34275
|
-
var READERS2 = new Set(("cat less more head tail ls dir stat file wc grep egrep fgrep rg ag diff cmp sha256sum md5sum " + "shasum du bat echo printf cp mv rm chmod touch mkdir realpath readlink dirname basename tee " + "oxfmt oxlint prettier eslint tsc gh jq sort uniq cut tr nl od xxd hexdump strings code vim " + "nano sed get-content gc type select-string get-childitem test-path").split(" "));
|
|
34276
|
-
var TOKENS2 = /[\s'"`(),[\]{};|&<>=!]+/u;
|
|
34277
|
-
var ENTRY_SAMPLES = [
|
|
34278
|
-
"workit-hook.mjs",
|
|
34279
|
-
"workit-hook.js",
|
|
34280
|
-
"workit-hook.ts",
|
|
34281
|
-
"cursor-session-start.js"
|
|
34282
|
-
];
|
|
34283
|
-
var tokenEntry = (token, scan) => {
|
|
34284
|
-
if (!/[\\/]/u.test(token) && !existsSync19(path59.resolve(scan.dir ?? "/", token)))
|
|
34285
|
-
return null;
|
|
34286
|
-
return entryNamed(token, scan);
|
|
34287
|
-
};
|
|
34288
|
-
var opaqueEntry = (words, inputs, scan) => {
|
|
34289
|
-
let at = 0;
|
|
34290
|
-
while (/^[A-Za-z_]\w*=/u.test(words[at] ?? ""))
|
|
34291
|
-
at += 1;
|
|
34292
|
-
const head = baseName3(words[at] ?? "");
|
|
34293
|
-
if (READERS2.has(head))
|
|
34294
|
-
return null;
|
|
34295
|
-
if ((head === "bun" || head === "deno") && NO_RUN.has(words[at + 1] ?? ""))
|
|
34296
|
-
return null;
|
|
34297
|
-
if (head === "git" && !words.some((word) => word.includes("alias.")))
|
|
34298
|
-
return null;
|
|
34299
|
-
if (head === "workit" || head === "workit-cli")
|
|
34300
|
-
return null;
|
|
34301
|
-
const tokens = words.flatMap((word) => word.split(TOKENS2)).filter(Boolean);
|
|
34302
|
-
const runner = (token) => RUNNERS2.has(baseName3(token)) && !/^(?:\.|source)$/u.test(token) || PACKAGE_RUNNERS2.has(baseName3(token));
|
|
34303
|
-
if (!tokens.some(runner))
|
|
34304
|
-
return null;
|
|
34305
|
-
if (scan.fed)
|
|
34306
|
-
return scan.fed;
|
|
34307
|
-
for (const token of tokens) {
|
|
34308
|
-
const hit = tokenEntry(token, scan);
|
|
34309
|
-
if (hit)
|
|
34310
|
-
return hit;
|
|
34311
|
-
}
|
|
34312
|
-
if (head === "find")
|
|
34313
|
-
for (const [index, word] of words.entries()) {
|
|
34314
|
-
const named = /^-i?name$/u.test(words[index - 1] ?? "") && /^[^*?[]/u.test(word);
|
|
34315
|
-
const pattern = named ? globPattern(word) : null;
|
|
34316
|
-
if (pattern && ENTRY_SAMPLES.some((sample) => pattern.test(sample)))
|
|
34317
|
-
return word;
|
|
34318
|
-
}
|
|
34319
|
-
if (/^(?:xargs|parallel)$/u.test(head) || tokens.includes("xargs"))
|
|
34320
|
-
for (const input of inputs) {
|
|
34321
|
-
try {
|
|
34322
|
-
const file = path59.resolve(scan.dir ?? "/", expand(input, scan.vars));
|
|
34323
|
-
const text = readFileSync16(file, "utf8").slice(0, 65536);
|
|
34324
|
-
for (const token of text.split(/\s+/u).filter(Boolean)) {
|
|
34325
|
-
const hit = tokenEntry(token, scan);
|
|
34326
|
-
if (hit)
|
|
34327
|
-
return hit;
|
|
34328
|
-
}
|
|
34329
|
-
} catch {}
|
|
34330
|
-
}
|
|
34331
|
-
return null;
|
|
34332
|
-
};
|
|
34333
|
-
var expandedCommand = (words, scan) => {
|
|
34334
|
-
let at = 0;
|
|
34335
|
-
while (/^[A-Za-z_]\w*=/u.test(words[at] ?? ""))
|
|
34336
|
-
at += 1;
|
|
34337
|
-
if (!/[$`]/u.test(words[at] ?? ""))
|
|
34338
|
-
return null;
|
|
34339
|
-
for (const word of words.slice(at + 1)) {
|
|
34340
|
-
const hit = entryNamed(word, scan);
|
|
34341
|
-
if (hit)
|
|
34342
|
-
return hit;
|
|
34343
|
-
}
|
|
34344
|
-
return null;
|
|
34345
|
-
};
|
|
34346
|
-
var optionsEntry = (words, scan) => {
|
|
34347
|
-
for (const word of words) {
|
|
34348
|
-
const value = /^(?:NODE_OPTIONS|BUN_OPTIONS)=(.*)$/su.exec(word)?.[1];
|
|
34349
|
-
if (value === undefined)
|
|
34350
|
-
continue;
|
|
34351
|
-
for (const token of value.split(TOKENS2).filter(Boolean)) {
|
|
34352
|
-
const hit = tokenEntry(token, scan);
|
|
34353
|
-
if (hit)
|
|
34354
|
-
return hit;
|
|
34355
|
-
}
|
|
34356
|
-
}
|
|
34357
|
-
return null;
|
|
34358
|
-
};
|
|
34359
|
-
var scanCommand = (command, dialect, scan) => {
|
|
34360
|
-
let segments;
|
|
34361
|
-
try {
|
|
34362
|
-
segments = segmentsOf(command, dialect);
|
|
34363
|
-
} catch {
|
|
34364
|
-
return null;
|
|
34365
|
-
}
|
|
34366
|
-
for (const [index, segment] of segments.entries()) {
|
|
34367
|
-
if (segment.unanalyzable)
|
|
34368
|
-
continue;
|
|
34369
|
-
bindLoop(segment.words, segments.slice(index + 1), scan);
|
|
34370
|
-
const words = pastKeywords(segment.words);
|
|
34371
|
-
const fed = scan.fed;
|
|
34372
|
-
scan.fed = null;
|
|
34373
|
-
const hit = segmentEntry(segment, words, dialect, scan, fed);
|
|
34374
|
-
if (hit)
|
|
34375
|
-
return hit;
|
|
34376
|
-
}
|
|
34377
|
-
return null;
|
|
34378
|
-
};
|
|
34379
|
-
var bindLoop = (raw, after, scan) => {
|
|
34380
|
-
const at = raw.findIndex((word) => !KEYWORDS3.has(word));
|
|
34381
|
-
const verb = raw[at];
|
|
34382
|
-
if ((verb === "for" || verb === "select") && raw[at + 2] === "in") {
|
|
34383
|
-
const name = raw[at + 1] ?? "";
|
|
34384
|
-
const items = raw.slice(at + 3);
|
|
34385
|
-
const item = items.find((word) => entryNamed(word, scan)) ?? items[0];
|
|
34386
|
-
if (/^[A-Za-z_]\w*$/u.test(name) && item !== undefined)
|
|
34387
|
-
scan.vars.set(name, expand(item, scan.vars));
|
|
34388
|
-
return;
|
|
34389
|
-
}
|
|
34390
|
-
const read = raw.indexOf("read");
|
|
34391
|
-
if (read < 0 || !raw.slice(0, read).some((word) => word === "while" || word === "until"))
|
|
34392
|
-
return;
|
|
34393
|
-
const fed = after.find((segment) => segment.words[0] === "done")?.stdin?.[0];
|
|
34394
|
-
const line = fed?.split(`
|
|
34395
|
-
`)[0]?.trim();
|
|
34396
|
-
const name = raw.slice(read + 1).find((word) => /^[A-Za-z_]\w*$/u.test(word));
|
|
34397
|
-
if (name && line)
|
|
34398
|
-
scan.vars.set(name, line);
|
|
34399
|
-
};
|
|
34400
|
-
var segmentEntry = (segment, words, dialect, scan, fed) => {
|
|
34401
|
-
const verb = baseName3(words[0] ?? "");
|
|
34402
|
-
if (["cd", "pushd", "chdir", "set-location", "sl"].includes(verb)) {
|
|
34403
|
-
const target = words.slice(1).find((word) => !word.startsWith("-") || word === "-");
|
|
34404
|
-
const moved = target === undefined ? userHome(process.env) : expand(target, scan.vars);
|
|
34405
|
-
scan.dir = moved === "-" || /[$`]/u.test(moved) ? null : path59.isAbsolute(moved) ? moved : scan.dir && path59.resolve(scan.dir, moved);
|
|
34406
|
-
if (scan.dir === null)
|
|
34407
|
-
scan.vars.delete("PWD");
|
|
34408
|
-
else
|
|
34409
|
-
scan.vars.set("PWD", scan.dir);
|
|
34410
|
-
return null;
|
|
34411
|
-
}
|
|
34412
|
-
if (verb === "popd") {
|
|
34413
|
-
scan.dir = null;
|
|
34414
|
-
return null;
|
|
34415
|
-
}
|
|
34416
|
-
const declares = ["export", "declare", "typeset", "local", "readonly"].includes(verb);
|
|
34417
|
-
const assigns = declares ? words.slice(1) : words.every((word) => /^[A-Za-z_]\w*=/u.test(word)) ? words : [];
|
|
34418
|
-
for (const word of assigns) {
|
|
34419
|
-
const [name = "", value] = word.split(/=(.*)/su);
|
|
34420
|
-
if (value !== undefined && /^[A-Za-z_]\w*$/u.test(name))
|
|
34421
|
-
scan.vars.set(name, expand(value.replace(/^(['"])(.*)\1$/su, "$2"), scan.vars));
|
|
34422
|
-
}
|
|
34423
|
-
const options = optionsEntry(declares ? words.slice(1) : words, scan);
|
|
34424
|
-
if (options)
|
|
34425
|
-
return options;
|
|
34426
|
-
if (verb === "exec" && words.slice(1).every((word) => /^\d+$/u.test(word))) {
|
|
34427
|
-
for (const input of segment.inputs ?? [])
|
|
34428
|
-
if (entryNamed(input, scan))
|
|
34429
|
-
for (const fd of words.slice(1))
|
|
34430
|
-
scan.fds.add(fd);
|
|
34431
|
-
}
|
|
34432
|
-
const inputs = segment.inputs ?? [];
|
|
34433
|
-
const entry = runsEntry(words, inputs, scan) ?? expandedCommand(words, scan) ?? opaqueEntry(words, inputs, { ...scan, fed });
|
|
34434
|
-
if (entry)
|
|
34435
|
-
return entry;
|
|
34436
|
-
if (segment.pipe)
|
|
34437
|
-
scan.fed = words.slice(1).flatMap((word) => word.split(TOKENS2)).map((token) => token ? tokenEntry(token, scan) : null).find(Boolean) ?? null;
|
|
34438
|
-
noteLink(words, scan);
|
|
34439
|
-
if (scan.depth >= 4)
|
|
34440
|
-
return null;
|
|
34441
|
-
const nested = (script) => scanCommand(script, dialect, {
|
|
34442
|
-
...scan,
|
|
34443
|
-
vars: new Map(scan.vars),
|
|
34444
|
-
fed: null,
|
|
34445
|
-
depth: scan.depth + 1
|
|
34446
|
-
});
|
|
34447
|
-
const scripts = [...segment.subs ?? []];
|
|
34448
|
-
const script = shellScript2(words) ?? cmdScript2(words) ?? workitScript(words);
|
|
34449
|
-
if (script)
|
|
34450
|
-
scripts.push(script);
|
|
34451
|
-
if (!script && /^(?:ba|z|da|k)?sh$/u.test(verb) && words.slice(1).every((word) => word.startsWith("-")) || verb === "at" || verb === "batch")
|
|
34452
|
-
scripts.push(...segment.stdin ?? []);
|
|
34453
|
-
for (const text of scripts) {
|
|
34454
|
-
const hit = nested(text);
|
|
34455
|
-
if (hit)
|
|
34456
|
-
return hit;
|
|
34457
|
-
}
|
|
34458
|
-
return null;
|
|
34459
|
-
};
|
|
34460
|
-
var cmdScript2 = (words) => {
|
|
34461
|
-
const name = baseName3(words[0] ?? "");
|
|
34462
|
-
if (!/^(?:cmd|pwsh|powershell)$/u.test(name))
|
|
34463
|
-
return null;
|
|
34464
|
-
const flag = words.findIndex((word, at) => at > 0 && /^[/-](?:c|k|command)$/iu.test(word));
|
|
34465
|
-
return flag < 0 ? null : words.slice(flag + 1).join(" ").replaceAll("\\", "/") || null;
|
|
34466
|
-
};
|
|
34467
|
-
var workitScript = (words) => {
|
|
34468
|
-
if (!/^workit(?:-cli)?$/u.test(baseName3(words[0] ?? "")))
|
|
34469
|
-
return null;
|
|
34470
|
-
const dash = words.indexOf("--");
|
|
34471
|
-
return dash < 0 ? null : words.slice(dash + 1).join(" ") || null;
|
|
34472
|
-
};
|
|
34473
|
-
var substitutions2 = (command) => {
|
|
34474
|
-
const found = [];
|
|
34475
|
-
for (let at = command.indexOf("$(");at >= 0 && found.length < 32; at = command.indexOf("$(", at + 1)) {
|
|
34476
|
-
if (command[at + 2] === "(")
|
|
34477
|
-
continue;
|
|
34478
|
-
let depth = 1;
|
|
34479
|
-
let end = at + 2;
|
|
34480
|
-
for (;end < command.length && depth > 0; end += 1)
|
|
34481
|
-
if (command[end] === "(")
|
|
34482
|
-
depth += 1;
|
|
34483
|
-
else if (command[end] === ")")
|
|
34484
|
-
depth -= 1;
|
|
34485
|
-
found.push(command.slice(at + 2, end - 1));
|
|
34486
|
-
}
|
|
34487
|
-
return found;
|
|
34488
|
-
};
|
|
34489
|
-
var hookEntryRun = (cwd, command, dialect = "posix") => {
|
|
34490
|
-
const scan = (text) => scanCommand(text, dialect, {
|
|
34491
|
-
dir: cwd,
|
|
34492
|
-
vars: new Map([["PWD", cwd]]),
|
|
34493
|
-
links: new Set,
|
|
34494
|
-
fds: new Set,
|
|
34495
|
-
fed: null,
|
|
34496
|
-
depth: 0
|
|
34497
|
-
});
|
|
34498
|
-
const texts = dialect === "posix" && /[A-Za-z]:\\/u.test(command) ? [command, command.replaceAll("\\", "/")] : [command];
|
|
34499
|
-
for (const text of texts) {
|
|
34500
|
-
const hit = scan(text) ?? substitutions2(text).map(scan).find(Boolean);
|
|
34501
|
-
if (hit)
|
|
34502
|
-
return hit;
|
|
34503
|
-
}
|
|
34504
|
-
return null;
|
|
34505
|
-
};
|
|
34506
|
-
var hookEntryDeny = (entry) => `workit hook guard: this command runs Workit's hook entry ${entry}. Only the host runs Workit's hooks, with the payload it builds; a payload written in a shell would act as a session, subagent or agent that never existed. Use the workit CLI for what you need (\`workit doctor\` probes the installed hooks).`;
|
|
34507
|
-
|
|
34508
|
-
// packages/workit-core/src/hooks/identity-env.ts
|
|
34509
|
-
import fs37 from "node:fs";
|
|
34510
|
-
import path60 from "node:path";
|
|
34511
|
-
var NAMES = "WORKIT_(?:AGENT_ID|HOST|SESSION_ID|SESSION_TOKEN)";
|
|
34512
|
-
var IDENTITY = new RegExp(`^${NAMES}$`, "u");
|
|
34513
|
-
var HOST_NAMES = "(?:OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDECODE|CLAUDE_CODE_ENTRYPOINT)";
|
|
34514
|
-
var HOST_VARIABLE = new RegExp(`^${HOST_NAMES}$`, "u");
|
|
34515
|
-
var IDENTITY_ANY = new RegExp(NAMES, "iu");
|
|
34516
|
-
var STATE_NAMES = "(?:WORKIT_(?:STATE_DIR|CONFIG(?:_DIR)?)|XDG_(?:STATE|CONFIG|DATA)_HOME|HOME|USERPROFILE|(?:LOCAL)?APPDATA)";
|
|
34517
|
-
var STATE2 = new RegExp(`^${STATE_NAMES}$`, "u");
|
|
34518
|
-
var STATE_ANY = new RegExp(STATE_NAMES, "iu");
|
|
34519
|
-
var powershell = (names) => new RegExp([
|
|
34520
|
-
`\\$\\{?env:${names}\\}?\\s*(?:[-+*/]|\\?\\?)?=(?!=)`,
|
|
34521
|
-
`${PS_ITEM}[^;|\\n]*?(?<![$\\w{])env:[\\\\/]?${names}\\b`,
|
|
34522
|
-
`SetEnvironmentVariable\\(\\s*['"]${names}\\b`
|
|
34523
|
-
].join("|"), "iu");
|
|
34524
|
-
var PS_ITEM = "\\b(?:(?:Set|New|Remove|Clear|Rename|Move|Copy)-Item(?:Property)?|(?:Set|Add|Clear)-Content|si|ni|ri|rm|del|erase|rd|rmdir|cli|sc|ac|clc|rni|mi|move|mv|cpi|copy|cp|ren)\\b";
|
|
34525
|
-
var POWERSHELL = powershell(NAMES);
|
|
34526
|
-
var POWERSHELL_STATE = powershell(STATE_NAMES);
|
|
34527
|
-
var POWERSHELL_HOST = powershell(HOST_NAMES);
|
|
34528
|
-
var POWERSHELL_OPAQUE = new RegExp([
|
|
34529
|
-
`SetEnvironmentVariable\\(\\s*(?:[^'"\\s]|"[^"]*\\$)`,
|
|
34530
|
-
`${PS_ITEM}[^;|\\n]*?(?<![$\\w{])env:[\\\\/]?["']?[$(]`,
|
|
34531
|
-
`(?:^|[;&|{(\\n])\\s*\\.\\s+[^\\s.]`,
|
|
34532
|
-
`\\b(?:iex|Invoke-Expression)\\b`
|
|
34533
|
-
].join("|"), "iu");
|
|
34534
|
-
var cmdSet = (names) => new RegExp(`(?:^|[&|(\\s"])set\\s+(?:/[ap]\\s+)?"?(${names})\\s*\\+?=`, "iu");
|
|
34535
|
-
var CMD_IDENTITY = cmdSet(NAMES);
|
|
34536
|
-
var CMD_STATE = cmdSet(STATE_NAMES);
|
|
34537
|
-
var CMD_HOST = cmdSet(HOST_NAMES);
|
|
34538
|
-
var RUNS_TEXT = /\bworkit(?:-cli)?\b|@workit-dev[\\/]cli/iu;
|
|
34539
|
-
var DECLARES = new Set(["export", "unset", "declare", "typeset", "local", "readonly"]);
|
|
34540
|
-
var OPAQUE = new Set([
|
|
34541
|
-
"watch",
|
|
34542
|
-
"strace",
|
|
34543
|
-
"chroot",
|
|
34544
|
-
"flock",
|
|
34545
|
-
"ssh",
|
|
34546
|
-
"su",
|
|
34547
|
-
"runuser",
|
|
34548
|
-
"script",
|
|
34549
|
-
"parallel"
|
|
34550
|
-
]);
|
|
34551
|
-
var ENV_VALUED = new Set(["u", "C", "S", "a", "P"]);
|
|
34552
|
-
var ENV_FLAGS = new Set(["i", "0", "v"]);
|
|
34553
|
-
var ENV_LONG_VALUED = new Set(["--unset", "--chdir", "--split-string", "--argv0"]);
|
|
34554
|
-
var ENV_LONG_FLAGS = /^--(?:ignore-environment|null|debug|(?:block|default|ignore)-signal(?:=.*)?)$/u;
|
|
34555
|
-
var base3 = (word) => (word?.split(/[\\/]/u).at(-1) ?? "").replace(/\.exe$/iu, "");
|
|
34556
|
-
var ASSIGN = /^[A-Za-z_]\w*\+?=/u;
|
|
34557
|
-
var nameOf = (word) => (word?.split("=")[0] ?? "").replace(/\+$/u, "");
|
|
34558
|
-
var named = (word) => IDENTITY.test(nameOf(word));
|
|
34559
|
-
var stateNamed = (word) => STATE2.test(nameOf(word));
|
|
34560
|
-
var isWorkit = (word) => /^workit(?:-cli)?(?:\.[cm]?js)?$/u.test(base3(word)) || /@workit-dev[\\/]cli\b|workit-cli[\\/](?:dist|src)[\\/]/u.test(word ?? "");
|
|
34561
|
-
var linksToWorkit = (word, cwd) => {
|
|
34562
|
-
const places = /[\\/]/u.test(word) ? [path60.resolve(cwd ?? process.cwd(), word)] : (process.env.PATH ?? "").split(path60.delimiter).map((dir) => path60.join(dir, word));
|
|
34563
|
-
for (const place of places) {
|
|
34817
|
+
return text;
|
|
34818
|
+
let real;
|
|
34564
34819
|
try {
|
|
34565
|
-
|
|
34566
|
-
|
|
34567
|
-
|
|
34820
|
+
real = realpathSync14(file);
|
|
34821
|
+
} catch {
|
|
34822
|
+
continue;
|
|
34823
|
+
}
|
|
34824
|
+
if (ENTRY_NAMES.test(baseName3(real)) || entryByPath(real))
|
|
34825
|
+
return text;
|
|
34568
34826
|
}
|
|
34569
|
-
return
|
|
34827
|
+
return null;
|
|
34570
34828
|
};
|
|
34571
|
-
var
|
|
34572
|
-
|
|
34573
|
-
var note = (scan, seg, word) => {
|
|
34574
|
-
if (scan.identity && named(word)) {
|
|
34575
|
-
if (seg)
|
|
34576
|
-
seg.identity ??= nameOf(word);
|
|
34577
|
-
else
|
|
34578
|
-
scan.hit ??= nameOf(word);
|
|
34579
|
-
}
|
|
34580
|
-
if (!stateNamed(word) && !(scan.identity && HOST_VARIABLE.test(nameOf(word))))
|
|
34829
|
+
var noteLink = (words, scan) => {
|
|
34830
|
+
if (!/^(?:ln|cp|install)$/u.test(baseName3(words[0] ?? "")) || scan.dir === null)
|
|
34581
34831
|
return;
|
|
34582
|
-
|
|
34583
|
-
|
|
34584
|
-
|
|
34585
|
-
|
|
34832
|
+
const names = words.slice(1).filter((word) => !word.startsWith("-"));
|
|
34833
|
+
const dest = names.at(-1);
|
|
34834
|
+
if (dest === undefined || names.length < 2)
|
|
34835
|
+
return;
|
|
34836
|
+
for (const source of names.slice(0, -1).filter((name) => entryNamed(name, scan))) {
|
|
34837
|
+
const to = path60.resolve(scan.dir, expand(dest, scan.vars));
|
|
34838
|
+
scan.links.add(to);
|
|
34839
|
+
scan.links.add(path60.join(to, path60.basename(source)));
|
|
34840
|
+
}
|
|
34586
34841
|
};
|
|
34587
|
-
var
|
|
34588
|
-
|
|
34589
|
-
|
|
34590
|
-
|
|
34591
|
-
|
|
34592
|
-
|
|
34593
|
-
|
|
34594
|
-
if (
|
|
34595
|
-
|
|
34596
|
-
const
|
|
34597
|
-
|
|
34598
|
-
|
|
34599
|
-
|
|
34600
|
-
|
|
34601
|
-
|
|
34602
|
-
|
|
34603
|
-
|
|
34604
|
-
|
|
34605
|
-
return risky(UNREADABLE);
|
|
34606
|
-
}
|
|
34607
|
-
scanEnv([...tokens, ...words.slice(rest)], 0, expanded, scan, seg, depth + 1);
|
|
34608
|
-
};
|
|
34609
|
-
let options = true;
|
|
34610
|
-
for (let index = at;index < words.length; index += 1) {
|
|
34611
|
-
const word = words[index] ?? "";
|
|
34612
|
-
if (expands.has(index)) {
|
|
34613
|
-
risky(UNREADABLE);
|
|
34614
|
-
const built = options ? /^(?:--split-string=|-[\w]*S)(.+)$/su.exec(word) : null;
|
|
34615
|
-
if (built)
|
|
34616
|
-
return split(built[1], index + 1, true);
|
|
34617
|
-
continue;
|
|
34842
|
+
var runsEntry = (words, inputs, scan) => {
|
|
34843
|
+
const named = (word) => entryNamed(word, scan);
|
|
34844
|
+
let at = 0;
|
|
34845
|
+
while (at < words.length) {
|
|
34846
|
+
while (/^[A-Za-z_]\w*=/u.test(words[at] ?? ""))
|
|
34847
|
+
at += 1;
|
|
34848
|
+
const word = words[at];
|
|
34849
|
+
if (word === undefined)
|
|
34850
|
+
return null;
|
|
34851
|
+
const entry = named(word);
|
|
34852
|
+
if (entry)
|
|
34853
|
+
return entry;
|
|
34854
|
+
const name = baseName3(word);
|
|
34855
|
+
const split = name === "env" ? words.findIndex((w) => /^(?:-\w*S|--split-string)/u.test(w)) : -1;
|
|
34856
|
+
if (split > at) {
|
|
34857
|
+
const line = /^(?:-\w*S|--split-string=?)(.*)$/su.exec(words[split] ?? "")?.[1] || words[split + 1];
|
|
34858
|
+
const rest = words.slice(split + (line === words[split + 1] ? 2 : 1));
|
|
34859
|
+
return runsEntry([...(line ?? "").split(/\s+/u).filter(Boolean), ...rest], inputs, scan);
|
|
34618
34860
|
}
|
|
34619
|
-
if (
|
|
34620
|
-
|
|
34861
|
+
if (MORE_WRAPPERS.has(name)) {
|
|
34862
|
+
at = pastOptions(words, at + 1, "");
|
|
34621
34863
|
continue;
|
|
34622
34864
|
}
|
|
34623
|
-
|
|
34624
|
-
|
|
34865
|
+
const takes = WRAPPERS3[name];
|
|
34866
|
+
if (takes !== undefined && !PACKAGE_RUNNERS2.has(name)) {
|
|
34867
|
+
at = pastOptions(words, at + 1, takes, name === "env" || name === "sudo");
|
|
34625
34868
|
continue;
|
|
34626
34869
|
}
|
|
34627
|
-
if (
|
|
34628
|
-
|
|
34629
|
-
if (name === "--ignore-environment")
|
|
34630
|
-
risky("env -i");
|
|
34631
|
-
else if (name === "--unset")
|
|
34632
|
-
unset(value ?? words[index += 1]);
|
|
34633
|
-
else if (name === "--split-string")
|
|
34634
|
-
return split(value ?? words[index + 1], index + (value === undefined ? 2 : 1), expands.has(index + (value === undefined ? 1 : 0)));
|
|
34635
|
-
else if (ENV_LONG_VALUED.has(name))
|
|
34636
|
-
index += value === undefined ? 1 : 0;
|
|
34637
|
-
else if (!ENV_LONG_FLAGS.test(word))
|
|
34638
|
-
risky(UNREADABLE);
|
|
34870
|
+
if (name === "busybox" || name === "toybox") {
|
|
34871
|
+
at += 1;
|
|
34639
34872
|
continue;
|
|
34640
34873
|
}
|
|
34641
|
-
|
|
34642
|
-
|
|
34643
|
-
|
|
34644
|
-
|
|
34645
|
-
|
|
34646
|
-
|
|
34647
|
-
|
|
34648
|
-
else if (ENV_VALUED.has(letter)) {
|
|
34649
|
-
const glued = word.slice(at2 + 1);
|
|
34650
|
-
if (!glued)
|
|
34651
|
-
index += 1;
|
|
34652
|
-
const value = glued || words[index];
|
|
34653
|
-
if (letter === "u")
|
|
34654
|
-
unset(value);
|
|
34655
|
-
if (letter === "S")
|
|
34656
|
-
return split(value, index + 1, expands.has(index));
|
|
34657
|
-
break;
|
|
34658
|
-
} else {
|
|
34659
|
-
risky(UNREADABLE);
|
|
34660
|
-
break;
|
|
34661
|
-
}
|
|
34662
|
-
}
|
|
34874
|
+
const runner = PACKAGE_RUNNERS2.has(name) || name === "bun" && words[at + 1] === "x" || /^(?:pnpm|yarn|npm)$/u.test(name) && /^(?:dlx|exec|x)$/u.test(words[at + 1] ?? "");
|
|
34875
|
+
if (runner) {
|
|
34876
|
+
at += PACKAGE_RUNNERS2.has(name) ? 1 : 2;
|
|
34877
|
+
while (words[at]?.startsWith("-"))
|
|
34878
|
+
at += /^(?:-p|--package|-c|--call)$/u.test(words[at] ?? "") ? 2 : 1;
|
|
34879
|
+
if (/^@workit-dev\/(?:claude-code|codex|cursor)(?:@|$)/u.test(words[at] ?? ""))
|
|
34880
|
+
return words[at] ?? null;
|
|
34663
34881
|
continue;
|
|
34664
34882
|
}
|
|
34665
|
-
if (
|
|
34666
|
-
|
|
34667
|
-
|
|
34883
|
+
if (!RUNNERS2.has(name))
|
|
34884
|
+
return null;
|
|
34885
|
+
if ((name === "bun" || name === "deno") && NO_RUN.has(words[at + 1] ?? ""))
|
|
34886
|
+
return null;
|
|
34887
|
+
for (const arg of [...words.slice(at + 1), ...inputs]) {
|
|
34888
|
+
const hit = named(arg);
|
|
34889
|
+
if (hit)
|
|
34890
|
+
return hit;
|
|
34668
34891
|
}
|
|
34669
|
-
return
|
|
34892
|
+
return null;
|
|
34670
34893
|
}
|
|
34894
|
+
return null;
|
|
34671
34895
|
};
|
|
34672
|
-
var
|
|
34673
|
-
|
|
34674
|
-
|
|
34675
|
-
|
|
34676
|
-
|
|
34677
|
-
|
|
34678
|
-
|
|
34679
|
-
|
|
34680
|
-
|
|
34681
|
-
|
|
34682
|
-
|
|
34683
|
-
|
|
34684
|
-
|
|
34685
|
-
|
|
34686
|
-
|
|
34687
|
-
|
|
34688
|
-
|
|
34689
|
-
|
|
34896
|
+
var KEYWORDS3 = new Set([
|
|
34897
|
+
"{",
|
|
34898
|
+
"}",
|
|
34899
|
+
"if",
|
|
34900
|
+
"then",
|
|
34901
|
+
"elif",
|
|
34902
|
+
"else",
|
|
34903
|
+
"fi",
|
|
34904
|
+
"while",
|
|
34905
|
+
"until",
|
|
34906
|
+
"do",
|
|
34907
|
+
"done",
|
|
34908
|
+
"esac",
|
|
34909
|
+
"!"
|
|
34910
|
+
]);
|
|
34911
|
+
var pastKeywords = (words) => {
|
|
34912
|
+
let at = 0;
|
|
34913
|
+
for (;; ) {
|
|
34914
|
+
if (words[at] === "function")
|
|
34915
|
+
at += 2;
|
|
34916
|
+
else if (KEYWORDS3.has(words[at] ?? ""))
|
|
34917
|
+
at += 1;
|
|
34918
|
+
else
|
|
34690
34919
|
break;
|
|
34691
|
-
index = pastOptions(words, index + 1, takes);
|
|
34692
34920
|
}
|
|
34693
|
-
|
|
34694
|
-
|
|
34695
|
-
|
|
34696
|
-
|
|
34697
|
-
|
|
34698
|
-
|
|
34699
|
-
|
|
34700
|
-
|
|
34701
|
-
|
|
34702
|
-
|
|
34703
|
-
|
|
34704
|
-
if (
|
|
34705
|
-
|
|
34706
|
-
|
|
34707
|
-
|
|
34708
|
-
|
|
34709
|
-
|
|
34710
|
-
|
|
34921
|
+
return /^(?:for|case|select)$/u.test(words[at] ?? "") ? [] : words.slice(at);
|
|
34922
|
+
};
|
|
34923
|
+
var READERS2 = new Set(("cat less more head tail ls dir stat file wc grep egrep fgrep rg ag diff cmp sha256sum md5sum " + "shasum du bat echo printf cp mv rm chmod touch mkdir realpath readlink dirname basename tee " + "oxfmt oxlint prettier eslint tsc gh jq sort uniq cut tr nl od xxd hexdump strings code vim " + "nano sed get-content gc type select-string get-childitem test-path").split(" "));
|
|
34924
|
+
var TOKENS2 = /[\s'"`(),[\]{};|&<>=!]+/u;
|
|
34925
|
+
var ENTRY_SAMPLES = [
|
|
34926
|
+
"workit-hook.mjs",
|
|
34927
|
+
"workit-hook.js",
|
|
34928
|
+
"workit-hook.ts",
|
|
34929
|
+
"cursor-session-start.js"
|
|
34930
|
+
];
|
|
34931
|
+
var tokenEntry = (token, scan) => {
|
|
34932
|
+
if (!/[\\/]/u.test(token) && !existsSync19(path60.resolve(scan.dir ?? "/", token)))
|
|
34933
|
+
return null;
|
|
34934
|
+
return entryNamed(token, scan);
|
|
34935
|
+
};
|
|
34936
|
+
var opaqueEntry = (words, inputs, scan) => {
|
|
34937
|
+
let at = 0;
|
|
34938
|
+
while (/^[A-Za-z_]\w*=/u.test(words[at] ?? ""))
|
|
34939
|
+
at += 1;
|
|
34940
|
+
const head = baseName3(words[at] ?? "");
|
|
34941
|
+
if (READERS2.has(head))
|
|
34942
|
+
return null;
|
|
34943
|
+
if ((head === "bun" || head === "deno") && NO_RUN.has(words[at + 1] ?? ""))
|
|
34944
|
+
return null;
|
|
34945
|
+
if (head === "git" && !words.some((word) => word.includes("alias.")))
|
|
34946
|
+
return null;
|
|
34947
|
+
if (head === "workit" || head === "workit-cli")
|
|
34948
|
+
return null;
|
|
34949
|
+
const tokens = words.flatMap((word) => word.split(TOKENS2)).filter(Boolean);
|
|
34950
|
+
const runner = (token) => RUNNERS2.has(baseName3(token)) && !/^(?:\.|source)$/u.test(token) || PACKAGE_RUNNERS2.has(baseName3(token));
|
|
34951
|
+
if (!tokens.some(runner))
|
|
34952
|
+
return null;
|
|
34953
|
+
if (scan.fed)
|
|
34954
|
+
return scan.fed;
|
|
34955
|
+
for (const token of tokens) {
|
|
34956
|
+
const hit = tokenEntry(token, scan);
|
|
34957
|
+
if (hit)
|
|
34958
|
+
return hit;
|
|
34959
|
+
}
|
|
34960
|
+
if (head === "find")
|
|
34961
|
+
for (const [index, word] of words.entries()) {
|
|
34962
|
+
const named = /^-i?name$/u.test(words[index - 1] ?? "") && /^[^*?[]/u.test(word);
|
|
34963
|
+
const pattern = named ? globPattern(word) : null;
|
|
34964
|
+
if (pattern && ENTRY_SAMPLES.some((sample) => pattern.test(sample)))
|
|
34965
|
+
return word;
|
|
34966
|
+
}
|
|
34967
|
+
if (/^(?:xargs|parallel)$/u.test(head) || tokens.includes("xargs"))
|
|
34968
|
+
for (const input of inputs) {
|
|
34969
|
+
try {
|
|
34970
|
+
const file = path60.resolve(scan.dir ?? "/", expand(input, scan.vars));
|
|
34971
|
+
const text = readFileSync16(file, "utf8").slice(0, 65536);
|
|
34972
|
+
for (const token of text.split(/\s+/u).filter(Boolean)) {
|
|
34973
|
+
const hit = tokenEntry(token, scan);
|
|
34974
|
+
if (hit)
|
|
34975
|
+
return hit;
|
|
34976
|
+
}
|
|
34977
|
+
} catch {}
|
|
34711
34978
|
}
|
|
34712
|
-
|
|
34713
|
-
};
|
|
34714
|
-
var opaque = (rest, scan, seg, assigns) => {
|
|
34715
|
-
if (!rest.some(isWorkit))
|
|
34716
|
-
return;
|
|
34717
|
-
seg.runs = true;
|
|
34718
|
-
const changes = (word) => base3(word).toLowerCase() === "env" || DECLARES.has(word) || assigns && ASSIGN.test(word);
|
|
34719
|
-
if (rest.some(changes))
|
|
34720
|
-
scan.risky ??= UNREADABLE;
|
|
34721
|
-
if (rest.some((word) => base3(word).toLowerCase() === "env"))
|
|
34722
|
-
for (const word of rest)
|
|
34723
|
-
note(scan, seg, word);
|
|
34724
|
-
};
|
|
34725
|
-
var LOGIN_VALUED = {
|
|
34726
|
-
su: /[sgGwc]/u,
|
|
34727
|
-
runuser: /[sgGwuc]/u,
|
|
34728
|
-
script: /[cEIOBTmot]/u
|
|
34979
|
+
return null;
|
|
34729
34980
|
};
|
|
34730
|
-
var
|
|
34731
|
-
|
|
34732
|
-
|
|
34981
|
+
var expandedCommand = (words, scan) => {
|
|
34982
|
+
let at = 0;
|
|
34983
|
+
while (/^[A-Za-z_]\w*=/u.test(words[at] ?? ""))
|
|
34984
|
+
at += 1;
|
|
34985
|
+
if (!/[$`]/u.test(words[at] ?? ""))
|
|
34733
34986
|
return null;
|
|
34734
|
-
const
|
|
34735
|
-
|
|
34736
|
-
|
|
34737
|
-
|
|
34738
|
-
|
|
34739
|
-
|
|
34740
|
-
|
|
34741
|
-
|
|
34987
|
+
for (const word of words.slice(at + 1)) {
|
|
34988
|
+
const hit = entryNamed(word, scan);
|
|
34989
|
+
if (hit)
|
|
34990
|
+
return hit;
|
|
34991
|
+
}
|
|
34992
|
+
return null;
|
|
34993
|
+
};
|
|
34994
|
+
var optionsEntry = (words, scan) => {
|
|
34995
|
+
for (const word of words) {
|
|
34996
|
+
const value = /^(?:NODE_OPTIONS|BUN_OPTIONS)=(.*)$/su.exec(word)?.[1];
|
|
34997
|
+
if (value === undefined)
|
|
34742
34998
|
continue;
|
|
34743
|
-
const
|
|
34744
|
-
|
|
34745
|
-
|
|
34999
|
+
for (const token of value.split(TOKENS2).filter(Boolean)) {
|
|
35000
|
+
const hit = tokenEntry(token, scan);
|
|
35001
|
+
if (hit)
|
|
35002
|
+
return hit;
|
|
35003
|
+
}
|
|
34746
35004
|
}
|
|
34747
35005
|
return null;
|
|
34748
35006
|
};
|
|
34749
|
-
var scanCommand2 = (command, dialect,
|
|
35007
|
+
var scanCommand2 = (command, dialect, scan) => {
|
|
34750
35008
|
let segments;
|
|
34751
35009
|
try {
|
|
34752
35010
|
segments = segmentsOf(command, dialect);
|
|
34753
35011
|
} catch {
|
|
34754
|
-
return;
|
|
35012
|
+
return null;
|
|
34755
35013
|
}
|
|
34756
|
-
for (const
|
|
34757
|
-
|
|
34758
|
-
const seg = newSeg();
|
|
34759
|
-
let at = 0;
|
|
34760
|
-
while (ASSIGN.test(words[at] ?? ""))
|
|
34761
|
-
at += 1;
|
|
34762
|
-
for (const word of words.slice(0, at))
|
|
34763
|
-
note(scan, at === words.length ? null : seg, word);
|
|
34764
|
-
let declare = at;
|
|
34765
|
-
if (["builtin", "command"].includes(words[declare] ?? ""))
|
|
34766
|
-
declare += 1;
|
|
34767
|
-
if (DECLARES.has(words[declare] ?? "")) {
|
|
34768
|
-
for (let index = declare + 1;index < words.length; index += 1) {
|
|
34769
|
-
note(scan, null, words[index]);
|
|
34770
|
-
if (expanded.has(index))
|
|
34771
|
-
scan.risky ??= UNREADABLE;
|
|
34772
|
-
}
|
|
35014
|
+
for (const [index, segment] of segments.entries()) {
|
|
35015
|
+
if (segment.unanalyzable)
|
|
34773
35016
|
continue;
|
|
34774
|
-
|
|
34775
|
-
|
|
34776
|
-
const
|
|
34777
|
-
|
|
34778
|
-
|
|
34779
|
-
|
|
34780
|
-
|
|
34781
|
-
seg.runs ||= scan.runs;
|
|
34782
|
-
scan.runs ||= before;
|
|
34783
|
-
}
|
|
34784
|
-
scan.words.push(...seg.words);
|
|
34785
|
-
const reaches = () => seg.runs || mayBeWorkit(seg.words, scan.cwd);
|
|
34786
|
-
if (seg.identity && reaches())
|
|
34787
|
-
scan.hit ??= seg.identity;
|
|
34788
|
-
if (seg.scoped && reaches())
|
|
34789
|
-
scan.state ??= seg.scoped;
|
|
34790
|
-
scan.runs ||= seg.runs;
|
|
35017
|
+
bindLoop(segment.words, segments.slice(index + 1), scan);
|
|
35018
|
+
const words = pastKeywords(segment.words);
|
|
35019
|
+
const fed = scan.fed;
|
|
35020
|
+
scan.fed = null;
|
|
35021
|
+
const hit = segmentEntry(segment, words, dialect, scan, fed);
|
|
35022
|
+
if (hit)
|
|
35023
|
+
return hit;
|
|
34791
35024
|
}
|
|
35025
|
+
return null;
|
|
34792
35026
|
};
|
|
34793
|
-
var
|
|
34794
|
-
|
|
34795
|
-
const
|
|
34796
|
-
if (
|
|
34797
|
-
|
|
34798
|
-
|
|
35027
|
+
var bindLoop = (raw, after, scan) => {
|
|
35028
|
+
const at = raw.findIndex((word) => !KEYWORDS3.has(word));
|
|
35029
|
+
const verb = raw[at];
|
|
35030
|
+
if ((verb === "for" || verb === "select") && raw[at + 2] === "in") {
|
|
35031
|
+
const name = raw[at + 1] ?? "";
|
|
35032
|
+
const items = raw.slice(at + 3);
|
|
35033
|
+
const item = items.find((word) => entryNamed(word, scan)) ?? items[0];
|
|
35034
|
+
if (/^[A-Za-z_]\w*$/u.test(name) && item !== undefined)
|
|
35035
|
+
scan.vars.set(name, expand(item, scan.vars));
|
|
35036
|
+
return;
|
|
35037
|
+
}
|
|
35038
|
+
const read = raw.indexOf("read");
|
|
35039
|
+
if (read < 0 || !raw.slice(0, read).some((word) => word === "while" || word === "until"))
|
|
35040
|
+
return;
|
|
35041
|
+
const fed = after.find((segment) => segment.words[0] === "done")?.stdin?.[0];
|
|
35042
|
+
const line = fed?.split(`
|
|
35043
|
+
`)[0]?.trim();
|
|
35044
|
+
const name = raw.slice(read + 1).find((word) => /^[A-Za-z_]\w*$/u.test(word));
|
|
35045
|
+
if (name && line)
|
|
35046
|
+
scan.vars.set(name, line);
|
|
35047
|
+
};
|
|
35048
|
+
var segmentEntry = (segment, words, dialect, scan, fed) => {
|
|
35049
|
+
const verb = baseName3(words[0] ?? "");
|
|
35050
|
+
if (["cd", "pushd", "chdir", "set-location", "sl"].includes(verb)) {
|
|
35051
|
+
const target = words.slice(1).find((word) => !word.startsWith("-") || word === "-");
|
|
35052
|
+
const moved = target === undefined ? userHome(process.env) : expand(target, scan.vars);
|
|
35053
|
+
scan.dir = moved === "-" || /[$`]/u.test(moved) ? null : path60.isAbsolute(moved) ? moved : scan.dir && path60.resolve(scan.dir, moved);
|
|
35054
|
+
if (scan.dir === null)
|
|
35055
|
+
scan.vars.delete("PWD");
|
|
35056
|
+
else
|
|
35057
|
+
scan.vars.set("PWD", scan.dir);
|
|
34799
35058
|
return null;
|
|
34800
|
-
|
|
34801
|
-
if (
|
|
34802
|
-
|
|
34803
|
-
|
|
35059
|
+
}
|
|
35060
|
+
if (verb === "popd") {
|
|
35061
|
+
scan.dir = null;
|
|
35062
|
+
return null;
|
|
35063
|
+
}
|
|
35064
|
+
const declares = ["export", "declare", "typeset", "local", "readonly"].includes(verb);
|
|
35065
|
+
const assigns = declares ? words.slice(1) : words.every((word) => /^[A-Za-z_]\w*=/u.test(word)) ? words : [];
|
|
35066
|
+
for (const word of assigns) {
|
|
35067
|
+
const [name = "", value] = word.split(/=(.*)/su);
|
|
35068
|
+
if (value !== undefined && /^[A-Za-z_]\w*$/u.test(name))
|
|
35069
|
+
scan.vars.set(name, expand(value.replace(/^(['"])(.*)\1$/su, "$2"), scan.vars));
|
|
35070
|
+
}
|
|
35071
|
+
const options = optionsEntry(declares ? words.slice(1) : words, scan);
|
|
35072
|
+
if (options)
|
|
35073
|
+
return options;
|
|
35074
|
+
if (verb === "exec" && words.slice(1).every((word) => /^\d+$/u.test(word))) {
|
|
35075
|
+
for (const input of segment.inputs ?? [])
|
|
35076
|
+
if (entryNamed(input, scan))
|
|
35077
|
+
for (const fd of words.slice(1))
|
|
35078
|
+
scan.fds.add(fd);
|
|
35079
|
+
}
|
|
35080
|
+
const inputs = segment.inputs ?? [];
|
|
35081
|
+
const entry = runsEntry(words, inputs, scan) ?? expandedCommand(words, scan) ?? opaqueEntry(words, inputs, { ...scan, fed });
|
|
35082
|
+
if (entry)
|
|
35083
|
+
return entry;
|
|
35084
|
+
if (segment.pipe)
|
|
35085
|
+
scan.fed = words.slice(1).flatMap((word) => word.split(TOKENS2)).map((token) => token ? tokenEntry(token, scan) : null).find(Boolean) ?? null;
|
|
35086
|
+
noteLink(words, scan);
|
|
35087
|
+
if (scan.depth >= 4)
|
|
35088
|
+
return null;
|
|
35089
|
+
const nested = (script) => scanCommand2(script, dialect, {
|
|
35090
|
+
...scan,
|
|
35091
|
+
vars: new Map(scan.vars),
|
|
35092
|
+
fed: null,
|
|
35093
|
+
depth: scan.depth + 1
|
|
35094
|
+
});
|
|
35095
|
+
const scripts = [...segment.subs ?? []];
|
|
35096
|
+
const script = shellScript2(words) ?? cmdScript2(words) ?? workitScript(words);
|
|
35097
|
+
if (script)
|
|
35098
|
+
scripts.push(script);
|
|
35099
|
+
if (!script && /^(?:ba|z|da|k)?sh$/u.test(verb) && words.slice(1).every((word) => word.startsWith("-")) || verb === "at" || verb === "batch")
|
|
35100
|
+
scripts.push(...segment.stdin ?? []);
|
|
35101
|
+
for (const text of scripts) {
|
|
35102
|
+
const hit = nested(text);
|
|
35103
|
+
if (hit)
|
|
35104
|
+
return hit;
|
|
35105
|
+
}
|
|
35106
|
+
return null;
|
|
34804
35107
|
};
|
|
34805
|
-
var
|
|
34806
|
-
const
|
|
34807
|
-
if (
|
|
34808
|
-
return powershellChange(command, identity);
|
|
34809
|
-
if (!/WORKIT_|XDG_|HOME|USERPROFILE|APPDATA|OPENCODE_SESSION_ID|PI_SESSION_ID|CODEX_THREAD_ID|CLAUDE|EnvironmentVariable|\b(?:env|unset|export|declare|typeset|local|readonly)\b/iu.test(command))
|
|
35108
|
+
var cmdScript2 = (words) => {
|
|
35109
|
+
const name = baseName3(words[0] ?? "");
|
|
35110
|
+
if (!/^(?:cmd|pwsh|powershell)$/u.test(name))
|
|
34810
35111
|
return null;
|
|
34811
|
-
const
|
|
34812
|
-
|
|
34813
|
-
cwd: options.cwd,
|
|
34814
|
-
hit: null,
|
|
34815
|
-
state: null,
|
|
34816
|
-
shell: null,
|
|
34817
|
-
risky: null,
|
|
34818
|
-
runs: false,
|
|
34819
|
-
words: []
|
|
34820
|
-
};
|
|
34821
|
-
scanCommand2(command, dialect, 0, scan);
|
|
34822
|
-
if (scan.hit)
|
|
34823
|
-
return scan.hit;
|
|
34824
|
-
if (scan.state)
|
|
34825
|
-
return scan.state;
|
|
34826
|
-
if (scan.shell && (scan.runs || mayBeWorkit(scan.words, scan.cwd)))
|
|
34827
|
-
return scan.shell;
|
|
34828
|
-
return scan.runs ? scan.risky : null;
|
|
35112
|
+
const flag = words.findIndex((word, at) => at > 0 && /^[/-](?:c|k|command)$/iu.test(word));
|
|
35113
|
+
return flag < 0 ? null : words.slice(flag + 1).join(" ").replaceAll("\\", "/") || null;
|
|
34829
35114
|
};
|
|
34830
|
-
var
|
|
34831
|
-
if (
|
|
34832
|
-
return
|
|
34833
|
-
|
|
35115
|
+
var workitScript = (words) => {
|
|
35116
|
+
if (!/^workit(?:-cli)?$/u.test(baseName3(words[0] ?? "")))
|
|
35117
|
+
return null;
|
|
35118
|
+
const dash = words.indexOf("--");
|
|
35119
|
+
return dash < 0 ? null : words.slice(dash + 1).join(" ") || null;
|
|
35120
|
+
};
|
|
35121
|
+
var substitutions2 = (command) => {
|
|
35122
|
+
const found = [];
|
|
35123
|
+
for (let at = command.indexOf("$(");at >= 0 && found.length < 32; at = command.indexOf("$(", at + 1)) {
|
|
35124
|
+
if (command[at + 2] === "(")
|
|
35125
|
+
continue;
|
|
35126
|
+
let depth = 1;
|
|
35127
|
+
let end = at + 2;
|
|
35128
|
+
for (;end < command.length && depth > 0; end += 1)
|
|
35129
|
+
if (command[end] === "(")
|
|
35130
|
+
depth += 1;
|
|
35131
|
+
else if (command[end] === ")")
|
|
35132
|
+
depth -= 1;
|
|
35133
|
+
found.push(command.slice(at + 2, end - 1));
|
|
35134
|
+
}
|
|
35135
|
+
return found;
|
|
35136
|
+
};
|
|
35137
|
+
var hookEntryRun = (cwd, command, dialect = "posix") => {
|
|
35138
|
+
const scan = (text) => scanCommand2(text, dialect, {
|
|
35139
|
+
dir: cwd,
|
|
35140
|
+
vars: new Map([["PWD", cwd]]),
|
|
35141
|
+
links: new Set,
|
|
35142
|
+
fds: new Set,
|
|
35143
|
+
fed: null,
|
|
35144
|
+
depth: 0
|
|
35145
|
+
});
|
|
35146
|
+
const texts = dialect === "posix" && /[A-Za-z]:\\/u.test(command) ? [command, command.replaceAll("\\", "/")] : [command];
|
|
35147
|
+
for (const text of texts) {
|
|
35148
|
+
const hit = scan(text) ?? substitutions2(text).map(scan).find(Boolean);
|
|
35149
|
+
if (hit)
|
|
35150
|
+
return hit;
|
|
35151
|
+
}
|
|
35152
|
+
return null;
|
|
34834
35153
|
};
|
|
35154
|
+
var hookEntryDeny = (entry) => `workit hook guard: this command runs Workit's hook entry ${entry}. Only the host runs Workit's hooks, with the payload it builds; a payload written in a shell would act as a session, subagent or agent that never existed. Use the workit CLI for what you need (\`workit doctor\` probes the installed hooks).`;
|
|
34835
35155
|
|
|
34836
35156
|
// packages/workit-core/src/hooks/handle.ts
|
|
34837
35157
|
var usable2 = (value) => value === "native" || value === "partial";
|
|
@@ -35583,7 +35903,7 @@ var enforceToolPolicy = (event, ctx) => {
|
|
|
35583
35903
|
return approveInDialog(approve.ids, event, ctx);
|
|
35584
35904
|
const inline = typeof command === "string" ? inlineCallIn(command) : null;
|
|
35585
35905
|
if (inline && !inline.plain)
|
|
35586
|
-
return { block: true, reason: inlineCallDeny(inline.fix) };
|
|
35906
|
+
return { block: true, reason: inlineCallDeny(inline.fix, inline.file) };
|
|
35587
35907
|
if (inline?.plain && ctx.mode === "tui" && ctx.hasUI) {
|
|
35588
35908
|
const gated = isReleaseNotesCall(inline.words) && inlineAskFor(inline.words, inlineCallCwd(inline, ctx.cwd)) ? undefined : gate({
|
|
35589
35909
|
kind: "shell.pre",
|