copilot-operator 0.1.70 → 0.1.80
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/src/api/operator.controller.js +9 -5
- package/dist/src/api/operator.service.js +107 -23
- package/dist/src/cli.js +1 -0
- package/dist/src/config/live.js +122 -0
- package/dist/src/config/schema.js +7 -0
- package/dist/src/exec/authorizer.js +2 -2
- package/dist/src/exec/commandHeads.js +11 -2
- package/dist/src/exec/locations.js +194 -0
- package/dist/src/exec/network.js +3 -1
- package/dist/src/exec/policy.js +14 -2
- package/dist/src/exec/policyManifest.js +2 -0
- package/dist/src/exec/processes.js +8 -3
- package/dist/src/exec/scriptFiles.js +66 -23
- package/dist/src/exec/scriptKinds.js +7 -7
- package/dist/src/exec/stops.js +147 -0
- package/dist/src/orchestrator/chosenAnswers.js +22 -0
- package/dist/src/orchestrator/handoff.js +37 -2
- package/dist/src/orchestrator/hold.js +83 -4
- package/dist/src/orchestrator/leadRunner.js +39 -22
- package/dist/src/orchestrator/taskRunner.js +130 -50
- package/dist/src/plan/kerrigan.js +36 -15
- package/dist/src/plan/systemGuide.js +72 -32
- package/dist/src/protocol/leadSchema.js +6 -1
- package/dist/src/protocol/replySchema.js +6 -0
- package/dist/src/queue/queue.js +6 -0
- package/dist/src/session/model.js +12 -0
- package/dist/src/util/pacing.js +6 -0
- package/dist/src/vcs/newBranch.js +69 -10
- package/dist/web/404.html +2 -2
- package/dist/web/_next/static/chunks/1024-17c74da39fb738c9.js +1 -0
- package/dist/web/_next/static/chunks/1077.ad76ce55c8120db5.js +1 -0
- package/dist/web/_next/static/chunks/1548.4dc2e419eee42487.js +1 -0
- package/dist/web/_next/static/chunks/2163.9f5c1d052712579f.js +1 -0
- package/dist/web/_next/static/chunks/714.3f04498b624bc1a4.js +1 -0
- package/dist/web/_next/static/chunks/7220.43612a34a3cb5146.js +1 -0
- package/dist/web/_next/static/chunks/7921-df716a0338420836.js +1 -0
- package/dist/web/_next/static/chunks/8124-2f07cc63c1494443.js +1 -0
- package/dist/web/_next/static/chunks/8584.36fd8289becb2d75.js +1 -0
- package/dist/web/_next/static/chunks/895-9c1f0751920e6624.js +1 -0
- package/dist/web/_next/static/chunks/9067.eff8198294aec025.js +1 -0
- package/dist/web/_next/static/chunks/9747.43e5b7201aa505ba.js +1 -0
- package/dist/web/_next/static/chunks/app/layout-b8a1e0075a48b025.js +1 -0
- package/dist/web/_next/static/chunks/app/page-4a7cf5d83c000d73.js +1 -0
- package/dist/web/_next/static/chunks/app/queue/{page-82f724d0842f4870.js → page-317151444eda8115.js} +1 -1
- package/dist/web/_next/static/chunks/app/settings/page-ec85a538fda29964.js +1 -0
- package/dist/web/_next/static/chunks/{webpack-950df0f512eef7ec.js → webpack-a7f5c4d1eda36000.js} +1 -1
- package/dist/web/_next/static/css/0e586e29189e8783.css +1 -0
- package/dist/web/defaults.html +2 -2
- package/dist/web/defaults.txt +8 -8
- package/dist/web/index.html +3 -3
- package/dist/web/index.txt +9 -9
- package/dist/web/queue.html +2 -2
- package/dist/web/queue.txt +9 -9
- package/dist/web/settings.html +2 -2
- package/dist/web/settings.txt +9 -9
- package/dist/web/system.html +2 -2
- package/dist/web/system.txt +9 -9
- package/package.json +4 -3
- package/prompts/lead1.md +5 -2
- package/prompts/level1.md +50 -38
- package/dist/web/_next/static/chunks/1024-67625a65f58057ae.js +0 -1
- package/dist/web/_next/static/chunks/1077.9fe1c0425bf51375.js +0 -1
- package/dist/web/_next/static/chunks/1548.cae3ba2196c8d52f.js +0 -1
- package/dist/web/_next/static/chunks/2163.5c9f8477d98b75a4.js +0 -1
- package/dist/web/_next/static/chunks/714.a0b32193f71e0e91.js +0 -1
- package/dist/web/_next/static/chunks/7220.5d6da026e0b34169.js +0 -1
- package/dist/web/_next/static/chunks/7921-0f7954cb5a9f697b.js +0 -1
- package/dist/web/_next/static/chunks/8124-a39d85dff0db6600.js +0 -1
- package/dist/web/_next/static/chunks/8584.6aa7ce56f7552f17.js +0 -1
- package/dist/web/_next/static/chunks/9067.2e4ba2e0e572f21e.js +0 -1
- package/dist/web/_next/static/chunks/9747.763da2026719a863.js +0 -1
- package/dist/web/_next/static/chunks/9929-2e0b1d006ebd5345.js +0 -1
- package/dist/web/_next/static/chunks/app/layout-52a2079ffa83c21f.js +0 -1
- package/dist/web/_next/static/chunks/app/page-44eac9d137f26dc5.js +0 -1
- package/dist/web/_next/static/chunks/app/settings/page-2df196e25cb7bfe1.js +0 -1
- package/dist/web/_next/static/css/cfe8ccb4adf3a311.css +0 -1
- /package/dist/web/_next/static/{DzoLjnxX5FvdxuPasWlnb → NP3hd8WmgkJYg_cn8gcXy}/_buildManifest.js +0 -0
- /package/dist/web/_next/static/{DzoLjnxX5FvdxuPasWlnb → NP3hd8WmgkJYg_cn8gcXy}/_ssgManifest.js +0 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The folder each part of a line runs in (2026-11-03). `Push-Location 'rules-engine'; .\switch-env.ps1 qa` runs the
|
|
3
|
+
* script of `rules-engine`, and the scripts a line runs (`scriptFiles.ts`) were looked for in the folder the line
|
|
4
|
+
* started in: the project's own committed script was "not one of the project's own — it does not exist", the chat was
|
|
5
|
+
* told to hand it over, and the card of commands handed over could not show it.
|
|
6
|
+
*
|
|
7
|
+
* So a line is read statement by statement, as the shell runs it, and a statement that changes the folder — `cd`,
|
|
8
|
+
* `Set-Location`, `Push-Location` and `Pop-Location`, by their aliases too, and cmd's `cd /d`, `pushd` and `popd` —
|
|
9
|
+
* changes it for what follows. Only a folder written out is followed: one chosen at run time (`cd $dir`, `cd ~`,
|
|
10
|
+
* `cd %X%`) leaves the folder as it was, which is what every reader here did before, and a script then looked for in
|
|
11
|
+
* the wrong folder is not found and not run. A change inside a block or a condition is not followed either.
|
|
12
|
+
*/
|
|
13
|
+
import { resolve } from 'node:path';
|
|
14
|
+
/** Where the top-level statements of a PowerShell line start and end. */
|
|
15
|
+
function powerShellStatements(s) {
|
|
16
|
+
const out = [];
|
|
17
|
+
let start = 0;
|
|
18
|
+
let depth = 0;
|
|
19
|
+
const cut = (end, next) => {
|
|
20
|
+
if (end > start)
|
|
21
|
+
out.push([start, end]);
|
|
22
|
+
start = next;
|
|
23
|
+
};
|
|
24
|
+
for (let i = 0; i < s.length; i++) {
|
|
25
|
+
const c = s[i];
|
|
26
|
+
if (c === '`') {
|
|
27
|
+
i += 1;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (c === "'" || c === '‘' || c === '’') {
|
|
31
|
+
// '' inside a single-quoted string is a quote.
|
|
32
|
+
for (i += 1; i < s.length; i++) {
|
|
33
|
+
if (s[i] === "'" || s[i] === '‘' || s[i] === '’') {
|
|
34
|
+
if (s[i + 1] === "'")
|
|
35
|
+
i += 1;
|
|
36
|
+
else
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (c === '"' || c === '“' || c === '”') {
|
|
43
|
+
for (i += 1; i < s.length; i++) {
|
|
44
|
+
if (s[i] === '`')
|
|
45
|
+
i += 1;
|
|
46
|
+
else if (s[i] === '"' || s[i] === '“' || s[i] === '”') {
|
|
47
|
+
if (s[i + 1] === '"')
|
|
48
|
+
i += 1;
|
|
49
|
+
else
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (c === '@' && (s[i + 1] === "'" || s[i + 1] === '"') && (s[i + 2] === '\n' || (s[i + 2] === '\r' && s[i + 3] === '\n'))) {
|
|
56
|
+
const close = s.indexOf(`\n${s[i + 1]}@`, i + 2);
|
|
57
|
+
i = close < 0 ? s.length : close + 2;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (c === '<' && s[i + 1] === '#') {
|
|
61
|
+
const close = s.indexOf('#>', i + 2);
|
|
62
|
+
i = close < 0 ? s.length : close + 1;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (c === '#' && (i === 0 || /\s/.test(s[i - 1]))) {
|
|
66
|
+
const nl = s.indexOf('\n', i);
|
|
67
|
+
i = nl < 0 ? s.length : nl - 1;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (c === '(' || c === '{' || c === '[')
|
|
71
|
+
depth += 1;
|
|
72
|
+
else if (c === ')' || c === '}' || c === ']')
|
|
73
|
+
depth = Math.max(0, depth - 1);
|
|
74
|
+
else if (depth === 0 && (c === ';' || c === '\n'))
|
|
75
|
+
cut(i, i + 1);
|
|
76
|
+
else if (depth === 0 && (c === '&' || c === '|') && s[i + 1] === c) {
|
|
77
|
+
cut(i, i + 2);
|
|
78
|
+
i += 1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
cut(s.length, s.length);
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
/** Where cmd's commands start and end: `&`, `&&`, `||` and newlines, outside quotes and brackets. */
|
|
85
|
+
function cmdStatements(s) {
|
|
86
|
+
const out = [];
|
|
87
|
+
let start = 0;
|
|
88
|
+
let depth = 0;
|
|
89
|
+
let quoted = false;
|
|
90
|
+
const cut = (end, next) => {
|
|
91
|
+
if (end > start)
|
|
92
|
+
out.push([start, end]);
|
|
93
|
+
start = next;
|
|
94
|
+
};
|
|
95
|
+
for (let i = 0; i < s.length; i++) {
|
|
96
|
+
const c = s[i];
|
|
97
|
+
if (c === '^' && !quoted) {
|
|
98
|
+
i += 1;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (c === '"')
|
|
102
|
+
quoted = !quoted;
|
|
103
|
+
if (quoted)
|
|
104
|
+
continue;
|
|
105
|
+
if (c === '(')
|
|
106
|
+
depth += 1;
|
|
107
|
+
else if (c === ')')
|
|
108
|
+
depth = Math.max(0, depth - 1);
|
|
109
|
+
else if (depth === 0 && c === '\n')
|
|
110
|
+
cut(i, i + 1);
|
|
111
|
+
else if (depth === 0 && (c === '&' || c === '|') && s[i + 1] === c) {
|
|
112
|
+
cut(i, i + 2);
|
|
113
|
+
i += 1;
|
|
114
|
+
}
|
|
115
|
+
else if (depth === 0 && c === '&')
|
|
116
|
+
cut(i, i + 1);
|
|
117
|
+
}
|
|
118
|
+
cut(s.length, s.length);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
/** A folder written out in full, its quotes taken off; null for one chosen at run time. */
|
|
122
|
+
function literalFolder(arg, cmd) {
|
|
123
|
+
let a = arg.trim();
|
|
124
|
+
const q = a[0];
|
|
125
|
+
if (q === '"' || (!cmd && q === "'")) {
|
|
126
|
+
if (a.length < 2 || a[a.length - 1] !== q)
|
|
127
|
+
return null;
|
|
128
|
+
a = a.slice(1, -1);
|
|
129
|
+
if (q === "'")
|
|
130
|
+
a = a.replace(/''/g, "'");
|
|
131
|
+
}
|
|
132
|
+
else if (!cmd && /\s/.test(a)) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
if (!a || a === '-' || a === '+' || a.startsWith('~'))
|
|
136
|
+
return null;
|
|
137
|
+
if (cmd ? /[%!]/.test(a) : /[$`(){}@]/.test(a) || (q === '"' && /[$`]/.test(a)))
|
|
138
|
+
return null;
|
|
139
|
+
// A PowerShell drive other than a file system's (`HKLM:`, `Env:`): not a folder.
|
|
140
|
+
if (/^[A-Za-z]{2,}:/.test(a))
|
|
141
|
+
return null;
|
|
142
|
+
return a;
|
|
143
|
+
}
|
|
144
|
+
/** What a statement does to the folder, when it does anything a reader can follow. */
|
|
145
|
+
function moveOf(statement, cmd) {
|
|
146
|
+
const s = statement.trim();
|
|
147
|
+
if (cmd) {
|
|
148
|
+
if (/^popd$/i.test(s))
|
|
149
|
+
return { pop: true };
|
|
150
|
+
const m = /^(cd|chdir|pushd)(?:\s+\/d)?\s+(.+)$/i.exec(s);
|
|
151
|
+
if (!m)
|
|
152
|
+
return null;
|
|
153
|
+
const to = literalFolder(m[2], true);
|
|
154
|
+
if (to === null)
|
|
155
|
+
return null;
|
|
156
|
+
return m[1].toLowerCase() === 'pushd' ? { push: to } : { to };
|
|
157
|
+
}
|
|
158
|
+
if (/^(?:Pop-Location|popd)$/i.test(s))
|
|
159
|
+
return { pop: true };
|
|
160
|
+
const m = /^(Set-Location|sl|cd|chdir|Push-Location|pushd)(?:\s+-(?:Path|LiteralPath)(?::)?)?\s+(.+)$/i.exec(s);
|
|
161
|
+
if (!m)
|
|
162
|
+
return null;
|
|
163
|
+
const to = literalFolder(m[2], false);
|
|
164
|
+
if (to === null)
|
|
165
|
+
return null;
|
|
166
|
+
return /^(?:Push-Location|pushd)$/i.test(m[1]) ? { push: to } : { to };
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The line cut where it changes folder, each stretch with the folder it runs in. A line that changes none is one
|
|
170
|
+
* stretch, in `cwd`: what every reader here was given before.
|
|
171
|
+
*/
|
|
172
|
+
export function foldersAlong(command, cwd, shell) {
|
|
173
|
+
const cmd = shell === 'cmd';
|
|
174
|
+
const statements = cmd ? cmdStatements(command) : powerShellStatements(command);
|
|
175
|
+
const out = [];
|
|
176
|
+
const stack = [];
|
|
177
|
+
let folder = cwd;
|
|
178
|
+
let from = 0;
|
|
179
|
+
for (const [start, end] of statements) {
|
|
180
|
+
const move = moveOf(command.slice(start, end), cmd);
|
|
181
|
+
if (!move)
|
|
182
|
+
continue;
|
|
183
|
+
const next = 'pop' in move ? (stack.pop() ?? folder) : resolve(folder, 'to' in move ? move.to : move.push);
|
|
184
|
+
if ('push' in move)
|
|
185
|
+
stack.push(folder);
|
|
186
|
+
if (next === folder)
|
|
187
|
+
continue;
|
|
188
|
+
out.push({ text: command.slice(from, end), cwd: folder });
|
|
189
|
+
folder = next;
|
|
190
|
+
from = end;
|
|
191
|
+
}
|
|
192
|
+
out.push({ text: command.slice(from), cwd: folder });
|
|
193
|
+
return out.filter((s, i) => i === out.length - 1 || s.text.trim());
|
|
194
|
+
}
|
package/dist/src/exec/network.js
CHANGED
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
*/
|
|
52
52
|
import { existsSync } from 'node:fs';
|
|
53
53
|
import { dirname, join, resolve } from 'node:path';
|
|
54
|
+
import { stopsTheBot } from './stops.js';
|
|
54
55
|
import { botRootDir } from './workDir.js';
|
|
55
56
|
import { chosenHome, defaultHome, installLayout } from '../config/layout.js';
|
|
56
57
|
const FETCHERS = [
|
|
@@ -279,6 +280,7 @@ export function networkFetchReason(command, cwd) {
|
|
|
279
280
|
* that approves its steps. Found on 2026-09-27: with the token in its environment, a step could call
|
|
280
281
|
* `127.0.0.1:4000` and answer its own held download. The token is no longer passed (`stepEnv.ts`);
|
|
281
282
|
* this refuses the attempt as well, so a step that goes looking gets a reason instead of a 401.
|
|
283
|
+
* And a line that stops the processes the bot runs as, by their name or its port (`stops.ts`, 2026-11-03).
|
|
282
284
|
*/
|
|
283
285
|
export function botSelfRefusal(command, ports = botPorts(), root = botRootDir(), homes = botHomeFolders()) {
|
|
284
286
|
const port = `(?:${ports.map((p) => String(Math.trunc(p))).join('|')})`;
|
|
@@ -316,7 +318,7 @@ export function botSelfRefusal(command, ports = botPorts(), root = botRootDir(),
|
|
|
316
318
|
return ('refused: this reaches the bot itself — its API, its web page or its key. A task works on its own project; ' +
|
|
317
319
|
'the bot that runs it is not part of the work. Use a different port for a server the task starts.');
|
|
318
320
|
}
|
|
319
|
-
return
|
|
321
|
+
return stopsTheBot(command, ports);
|
|
320
322
|
}
|
|
321
323
|
/** A path or a command as compared here: one backslash for any run of slashes, lower case. */
|
|
322
324
|
function foldPath(text) {
|
package/dist/src/exec/policy.js
CHANGED
|
@@ -5,7 +5,7 @@ import { inlineCodeRefusal, programRefusal } from './programs.js';
|
|
|
5
5
|
import { unattendedIsolationRefusal } from './isolation.js';
|
|
6
6
|
import { confinementRefusal } from './confinement.js';
|
|
7
7
|
import { botSelfRefusal, networkFetchReason, networkFetchRefusal } from './network.js';
|
|
8
|
-
import { ownScriptRefusal, scriptFileRefusal } from './scriptFiles.js';
|
|
8
|
+
import { ownScriptRefusal, scratchScriptRefusal, scriptFileRefusal } from './scriptFiles.js';
|
|
9
9
|
import { existsSync, readdirSync } from 'node:fs';
|
|
10
10
|
import { dirname, join } from 'node:path';
|
|
11
11
|
import { readOnlyRefusal, readOnlyUnaskedRefusal, readOnlyUnclearRefusal, readOnlyVerdict } from './readOnly.js';
|
|
@@ -218,7 +218,7 @@ export function checkAccessRefusal(command, shell, cfg) {
|
|
|
218
218
|
const reading = readOnlyVerdict(command, shell, cfg.readOnlyPrograms);
|
|
219
219
|
if (reading?.kind === 'unclear')
|
|
220
220
|
return readOnlyUnclearRefusal(reading.program, reading.why);
|
|
221
|
-
const platform = cfg.questions === 'agree' ? null : platformReason(command, shell, reading?.kind === 'reads' ?
|
|
221
|
+
const platform = cfg.questions === 'agree' ? null : platformReason(command, shell, notHeldFor(cfg, reading?.kind === 'reads' ? reading.programs : []));
|
|
222
222
|
if (!platform)
|
|
223
223
|
return null;
|
|
224
224
|
return `refused: this check reaches ${platform} with the operator's access, which only the operator may allow, and a check runs with nobody asked. Check it another way, or do it in a step, where it is put to the operator.`;
|
|
@@ -248,6 +248,14 @@ export function writtenScriptScreen(cfg, confinement, env = process.env) {
|
|
|
248
248
|
return null;
|
|
249
249
|
};
|
|
250
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* The programs whose platform a command is not held for: those set to run without asking (2026-10-28), and those set to
|
|
253
|
+
* read only whose every command on the line only reads.
|
|
254
|
+
*/
|
|
255
|
+
export function notHeldFor(cfg, reading) {
|
|
256
|
+
const all = [...(cfg.runWithoutAsking ?? []), ...reading].map((p) => p.trim().toLowerCase().replace(/\.(?:exe|cmd)$/, '')).filter(Boolean);
|
|
257
|
+
return all.length ? new Set(all) : undefined;
|
|
258
|
+
}
|
|
251
259
|
/** A fetch inside a script the line runs — held or refused exactly as one on the line would be. */
|
|
252
260
|
export function scriptNetworkReason(command, cwd) {
|
|
253
261
|
let found = null;
|
|
@@ -305,6 +313,10 @@ actor) {
|
|
|
305
313
|
* next. See `scriptFiles.ts`.
|
|
306
314
|
*/
|
|
307
315
|
if (confinement) {
|
|
316
|
+
// No script written only to be run (operator, 2026-10-28): one written into .cop-tmp is refused at the writing.
|
|
317
|
+
const scratch = scratchScriptRefusal(step.cmd, actor === 'review');
|
|
318
|
+
if (scratch)
|
|
319
|
+
return { action: 'skip', reason: scratch };
|
|
308
320
|
const own = ownScriptRefusal(step.cmd, confinement.cwd, confinement.workStartedAt, effectiveShell(step.shell), {
|
|
309
321
|
rules: cfg.scripts,
|
|
310
322
|
reviewer: actor === 'review',
|
|
@@ -79,6 +79,7 @@ export function collectPolicyManifest(input, env = process.env) {
|
|
|
79
79
|
},
|
|
80
80
|
readOnly: readOnlyInForce(input.readOnlyPrograms ?? [], input.allowedPrograms),
|
|
81
81
|
scripts: scriptsStatement(input.scripts ?? DEFAULT_SCRIPT_RULES, input.writtenScripts ?? 'ask', input.mode),
|
|
82
|
+
runWithoutAsking: [...(input.runWithoutAsking ?? [])],
|
|
82
83
|
chatFiles: 'the chat cannot supply a file: this runner has no file step and executes nothing it did not receive as a command',
|
|
83
84
|
networkFetch: networkFetchStatement(input.mode, input.networkFetch),
|
|
84
85
|
denyPatterns: { count: input.denyPatterns.length, digest: digestOf(input.denyPatterns) },
|
|
@@ -101,6 +102,7 @@ export function describePolicyManifest(m) {
|
|
|
101
102
|
`allowlist : ${m.allowlist.enforced ? `${m.allowlist.count} programs, digest ${m.allowlist.digest}` : 'NOT ENFORCED — any program could be started'}`,
|
|
102
103
|
`read only : ${m.readOnly.length ? `${m.readOnly.join(', ')} — a command that changes something, signs in or out or shows a secret is refused` : 'none'}`,
|
|
103
104
|
`scripts : ${m.scripts}`,
|
|
105
|
+
`no card for : ${m.runWithoutAsking.length ? `${m.runWithoutAsking.join(', ')} — reaching a platform with the operator's access, run without asking, changes included` : 'none — every platform command was put to the operator'}`,
|
|
104
106
|
`chat files : ${m.chatFiles}`,
|
|
105
107
|
`downloads : ${m.networkFetch}`,
|
|
106
108
|
`deny list : ${m.denyPatterns.count} patterns, digest ${m.denyPatterns.digest}`,
|
|
@@ -144,12 +144,17 @@ export async function processTable(since) {
|
|
|
144
144
|
}))
|
|
145
145
|
.filter((p) => p.pid !== process.pid);
|
|
146
146
|
}
|
|
147
|
-
/**
|
|
147
|
+
/**
|
|
148
|
+
* The ports these processes are listening on — asked about them only, not the whole machine. A socket with no other end
|
|
149
|
+
* (remote port 0) counts as well as one in the Listen state: seen on 2026-10-29, Windows on this machine reported a
|
|
150
|
+
* server's listening socket, which answered requests, as "Closed", and `-State Listen` found nothing.
|
|
151
|
+
*/
|
|
148
152
|
async function listenersOf(pids) {
|
|
149
153
|
if (pids.length === 0)
|
|
150
154
|
return [];
|
|
151
|
-
const text = await powershell(`Get-NetTCPConnection -
|
|
152
|
-
|
|
155
|
+
const text = await powershell(`Get-NetTCPConnection -OwningProcess ${pids.map((p) => Math.trunc(p)).join(',')} -ErrorAction SilentlyContinue | ` +
|
|
156
|
+
"Where-Object { $_.State -eq 'Listen' -or $_.RemotePort -eq 0 } | " +
|
|
157
|
+
'Select-Object LocalPort, OwningProcess -Unique | ConvertTo-Json -Compress');
|
|
153
158
|
return parseJsonList(text).map((l) => ({ port: l.LocalPort, pid: l.OwningProcess }));
|
|
154
159
|
}
|
|
155
160
|
/**
|
|
@@ -32,6 +32,7 @@ import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
|
32
32
|
import { basename, dirname, isAbsolute, relative, resolve } from 'node:path';
|
|
33
33
|
import { gitSync, RUNNER_EMAIL } from '../vcs/git.js';
|
|
34
34
|
import { scanCommandHeads } from './commandHeads.js';
|
|
35
|
+
import { foldersAlong } from './locations.js';
|
|
35
36
|
import { packageScriptsRun, packageScriptWritten, writesPackageScripts } from './npmScripts.js';
|
|
36
37
|
import { DEFAULT_SCRIPT_RULES, kindForChat, kindOfFile } from './scriptKinds.js';
|
|
37
38
|
/** Larger than this is not a step's script; it is not read, and running it is refused. */
|
|
@@ -40,8 +41,8 @@ const MAX_SCRIPT_BYTES = 256 * 1024;
|
|
|
40
41
|
export function scriptPathsRun(command) {
|
|
41
42
|
const paths = new Set();
|
|
42
43
|
const patterns = [
|
|
43
|
-
/\b(?:pwsh|powershell)(?:\.exe)?\b[^|;\n]*?\s-(?:File|f)\s+(?:"([^"]+)"|'([^']+)'|(
|
|
44
|
-
/(?:^|[|;&(\n{]\s*)(?:[.&]\s+)?(?:"([^"]+\.ps1)"|'([^']+\.ps1)'|(
|
|
44
|
+
/\b(?:pwsh|powershell)(?:\.exe)?\b[^|;\n]*?\s-(?:File|f)\s+(?:"([^"]+)"|'([^']+)'|([^\s;|&)}]+))/gi,
|
|
45
|
+
/(?:^|[|;&(\n{]\s*)(?:[.&]\s+)?(?:"([^"]+\.ps1)"|'([^']+\.ps1)'|([^\s;|&)}]+\.ps1))(?=$|[\s;|&)}])/gi,
|
|
45
46
|
];
|
|
46
47
|
for (const re of patterns) {
|
|
47
48
|
for (const m of command.matchAll(re)) {
|
|
@@ -56,10 +57,14 @@ export function scriptPathsRun(command) {
|
|
|
56
57
|
export function writesScript(command) {
|
|
57
58
|
return /\b(Set-Content|Add-Content|Out-File|New-Item|Copy-Item|Move-Item|Rename-Item|sc|ac|cp|copy|mv|move|ren|ni)\b[^|;\n]*\.ps1\b|>\s*["']?[^\s"']*\.ps1\b/i.test(command);
|
|
58
59
|
}
|
|
59
|
-
/** Reads the scripts a command runs, relative to `cwd
|
|
60
|
+
/** Reads the scripts a command runs, relative to `cwd`, or to the folder the line has moved to (`foldersAlong`). */
|
|
60
61
|
export function scriptFilesRun(command, cwd) {
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
const paths = new Set();
|
|
63
|
+
for (const seg of foldersAlong(command, cwd)) {
|
|
64
|
+
for (const p of scriptPathsRun(seg.text))
|
|
65
|
+
paths.add(isAbsolute(p) ? p : resolve(seg.cwd, p));
|
|
66
|
+
}
|
|
67
|
+
return [...paths].map((path) => {
|
|
63
68
|
try {
|
|
64
69
|
const size = statSync(path).size;
|
|
65
70
|
if (size > MAX_SCRIPT_BYTES)
|
|
@@ -101,21 +106,35 @@ const RUNS_A_FILE = String.raw `(?:node|nodejs|python3?|py|pwsh|powershell|cmd|t
|
|
|
101
106
|
* Windows Script Host's `.vbs`, `.wsf` and the rest — as full paths:
|
|
102
107
|
* given to `-File`, dot-sourced, called with `&` or by a path, passed to `cmd /c`, started with Start-Process, or
|
|
103
108
|
* imported as a module. A bare name counts only when that file is in the folder, where cmd and Start-Process look
|
|
104
|
-
* first; otherwise it is a program found on PATH — `npm.cmd`, `npx.cmd` — and not one of the project's files.
|
|
109
|
+
* first; otherwise it is a program found on PATH — `npm.cmd`, `npx.cmd` — and not one of the project's files. Each is
|
|
110
|
+
* looked for in the folder the line is in where it runs it (`foldersAlong`, 2026-11-03).
|
|
105
111
|
*/
|
|
106
|
-
export function shellScriptsRun(command, cwd) {
|
|
112
|
+
export function shellScriptsRun(command, cwd, shell) {
|
|
113
|
+
return alongTheLine(command, cwd, shell, shellScriptsRunIn);
|
|
114
|
+
}
|
|
115
|
+
/** What `find` gives for each stretch of the line in its own folder, each file once. */
|
|
116
|
+
function alongTheLine(command, cwd, shell, find) {
|
|
117
|
+
const files = new Map();
|
|
118
|
+
for (const seg of foldersAlong(command, cwd, shell)) {
|
|
119
|
+
for (const file of find(seg.text, seg.cwd))
|
|
120
|
+
if (!files.has(file.toLowerCase()))
|
|
121
|
+
files.set(file.toLowerCase(), file);
|
|
122
|
+
}
|
|
123
|
+
return [...files.values()];
|
|
124
|
+
}
|
|
125
|
+
function shellScriptsRunIn(command, cwd) {
|
|
107
126
|
const named = new Set(scriptPathsRun(command));
|
|
108
127
|
const patterns = [
|
|
109
|
-
/(?:^|[|;&(\n{]\s*)(?:[.&]\s+)?(?:"([^"]+\.(?:ps1|psm1|cmd|bat))"|'([^']+\.(?:ps1|psm1|cmd|bat))'|(
|
|
110
|
-
/\bcmd(?:\.exe)?\b[^|;\n]*?\s\/[ck]\s+(?:"([^"]+\.(?:cmd|bat))"|'([^']+\.(?:cmd|bat))'|(
|
|
128
|
+
/(?:^|[|;&(\n{]\s*)(?:[.&]\s+)?(?:"([^"]+\.(?:ps1|psm1|cmd|bat))"|'([^']+\.(?:ps1|psm1|cmd|bat))'|([^\s;|&)}]+\.(?:ps1|psm1|cmd|bat)))(?=$|[\s;|&)}])/gi,
|
|
129
|
+
/\bcmd(?:\.exe)?\b[^|;\n]*?\s\/[ck]\s+(?:"([^"]+\.(?:cmd|bat))"|'([^']+\.(?:cmd|bat))'|([^\s;|&)}]+\.(?:cmd|bat)))/gi,
|
|
111
130
|
/\b(?:Start-Process|saps)\b[^|;\n]*?(?:"([^"]+\.(?:ps1|cmd|bat))"|'([^']+\.(?:ps1|cmd|bat))'|([^\s'",]+\.(?:ps1|cmd|bat)))/gi,
|
|
112
|
-
/\b(?:Import-Module|ipmo)\b[^|;\n]*?(?:"([^"]+\.psm1)"|'([^']+\.psm1)'|(
|
|
131
|
+
/\b(?:Import-Module|ipmo)\b[^|;\n]*?(?:"([^"]+\.psm1)"|'([^']+\.psm1)'|([^\s;|&)}]+\.psm1))/gi,
|
|
113
132
|
// A shell script for bash and its kin (Git Bash, WSL), by a path or given to the shell.
|
|
114
|
-
/(?:^|[|;&(\n{]\s*)(?:"([^"]+\.(?:sh|bash|zsh))"|'([^']+\.(?:sh|bash|zsh))'|(
|
|
115
|
-
/\b(?:bash|sh|zsh|ksh|dash|wsl)(?:\.exe)?\b[^|;\n]*?\s(?:"([^"]+\.(?:sh|bash|zsh))"|'([^']+\.(?:sh|bash|zsh))'|(
|
|
133
|
+
/(?:^|[|;&(\n{]\s*)(?:"([^"]+\.(?:sh|bash|zsh))"|'([^']+\.(?:sh|bash|zsh))'|([^\s;|&)}]+\.(?:sh|bash|zsh)))(?=$|[\s;|&)}])/gi,
|
|
134
|
+
/\b(?:bash|sh|zsh|ksh|dash|wsl)(?:\.exe)?\b[^|;\n]*?\s(?:"([^"]+\.(?:sh|bash|zsh))"|'([^']+\.(?:sh|bash|zsh))'|([^\s;|&)}]+\.(?:sh|bash|zsh)))/gi,
|
|
116
135
|
// Windows Script Host's scripts.
|
|
117
|
-
/\b(?:wscript|cscript)(?:\.exe)?\b[^|;\n]*?\s(?:"([^"]+\.(?:vbs|vbe|js|jse|wsf|wsh))"|'([^']+\.(?:vbs|vbe|js|jse|wsf|wsh))'|(
|
|
118
|
-
/(?:^|[|;&(\n{]\s*)(?:[.&]\s+)?(?:"([^"]+\.(?:vbs|vbe|wsf|wsh|hta))"|'([^']+\.(?:vbs|vbe|wsf|wsh|hta))'|(
|
|
136
|
+
/\b(?:wscript|cscript)(?:\.exe)?\b[^|;\n]*?\s(?:"([^"]+\.(?:vbs|vbe|js|jse|wsf|wsh))"|'([^']+\.(?:vbs|vbe|js|jse|wsf|wsh))'|([^\s;|&)}]+\.(?:vbs|vbe|js|jse|wsf|wsh)))/gi,
|
|
137
|
+
/(?:^|[|;&(\n{]\s*)(?:[.&]\s+)?(?:"([^"]+\.(?:vbs|vbe|wsf|wsh|hta))"|'([^']+\.(?:vbs|vbe|wsf|wsh|hta))'|([^\s;|&)}]+\.(?:vbs|vbe|wsf|wsh|hta)))(?=$|[\s;|&)}])/gi,
|
|
119
138
|
];
|
|
120
139
|
for (const re of patterns) {
|
|
121
140
|
for (const m of command.matchAll(re)) {
|
|
@@ -218,9 +237,12 @@ function startedProcesses(command) {
|
|
|
218
237
|
* started with Start-Process (`Start-Process node -ArgumentList x.js`). The first argument that is not a switch is the
|
|
219
238
|
* file; a line whose first one is not a file of code (`node --version`, `python -m pytest`) gives none. The project's
|
|
220
239
|
* test runners are not this (operator, 2026-10-26: "tests as before"): `node --test` is left alone, as are `npm test`,
|
|
221
|
-
* `npx playwright test` and the rest, which are not runtimes given a file.
|
|
240
|
+
* `npx playwright test` and the rest, which are not runtimes given a file. Each in the folder the line is in there.
|
|
222
241
|
*/
|
|
223
242
|
export function codeFilesRun(command, cwd, shell) {
|
|
243
|
+
return alongTheLine(command, cwd, shell, (text, folder) => codeFilesRunIn(text, folder, shell));
|
|
244
|
+
}
|
|
245
|
+
function codeFilesRunIn(command, cwd, shell) {
|
|
224
246
|
const files = new Set();
|
|
225
247
|
const add = (literal) => void files.add(isAbsolute(literal) ? literal : resolve(cwd, literal));
|
|
226
248
|
for (const call of [...scanCommandHeads(command, shell ?? 'pwsh').calls, ...startedProcesses(command)]) {
|
|
@@ -343,7 +365,7 @@ export function scriptsRun(command, cwd, since, shell) {
|
|
|
343
365
|
seen.add(key);
|
|
344
366
|
return true;
|
|
345
367
|
};
|
|
346
|
-
for (const file of shellScriptsRun(command, cwd)) {
|
|
368
|
+
for (const file of shellScriptsRun(command, cwd, shell)) {
|
|
347
369
|
if (first(file))
|
|
348
370
|
out.push({ file, kind: kindOfFile(file, /\.jse?$/i.test(file)), written: notTheProjects(file, since), shell: true });
|
|
349
371
|
}
|
|
@@ -353,13 +375,16 @@ export function scriptsRun(command, cwd, since, shell) {
|
|
|
353
375
|
const written = inScratch(file) ? `it is in ${SCRATCH}, the folder for scratch files` : writesFileNamed(command, file) ? SAME_LINE : writtenDuringTheWork(file, since);
|
|
354
376
|
out.push({ file, kind: kindOfFile(file), written });
|
|
355
377
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
378
|
+
const segments = foldersAlong(command, cwd, shell);
|
|
379
|
+
for (const seg of segments) {
|
|
380
|
+
for (const m of seg.text.matchAll(SCRATCH_RUN)) {
|
|
381
|
+
const file = resolve(seg.cwd, m[1]);
|
|
382
|
+
if (first(file))
|
|
383
|
+
out.push({ file, kind: kindOfFile(file), written: `it is in ${SCRATCH}, the folder for scratch files` });
|
|
384
|
+
}
|
|
360
385
|
}
|
|
361
386
|
const sameLine = writesPackageScripts(command);
|
|
362
|
-
for (const run of packageScriptsRun(
|
|
387
|
+
for (const run of segments.flatMap((seg) => packageScriptsRun(seg.text, seg.cwd, shell))) {
|
|
363
388
|
const written = packageScriptWritten(run, since, sameLine);
|
|
364
389
|
// No such script and nothing writes it: npm says so, and nothing runs.
|
|
365
390
|
if (run.command === null && written === null)
|
|
@@ -441,7 +466,25 @@ export function writtenScriptsLetRun(command, cwd, since, shell, rules) {
|
|
|
441
466
|
return scriptsRun(command, cwd, since, shell).filter((r) => r.written !== null && r.written !== SAME_LINE && r.kind !== null && rules[r.kind].written);
|
|
442
467
|
}
|
|
443
468
|
/** What the chat is told to do instead (2026-10-26). */
|
|
444
|
-
const HAND_IT_OVER = 'Put what it does in the step itself — a command may be several lines
|
|
445
|
-
'the
|
|
469
|
+
const HAND_IT_OVER = 'Put what it does in the step itself, in the terminal — a command may be several lines; you never write a script to run something ' +
|
|
470
|
+
'(the operator\'s rule). Only what the runner cannot run at all goes to the operator, as commands, with status "handoff".';
|
|
471
|
+
/** The extensions of a script, as `.cop-tmp` must never hold one (2026-10-28). */
|
|
472
|
+
const SCRIPT_EXT = String.raw `(?:ps1|psm1|cmd|bat|sh|bash|zsh|js|mjs|cjs|jsx|ts|mts|cts|tsx|py|pyw|rb|pl|php|vbs|vbe|wsf|wsh|hta|jse|csx)`;
|
|
473
|
+
/** A line that writes a file into `.cop-tmp`: a cmdlet or an alias that writes, a .NET file write, or a redirection. */
|
|
474
|
+
const SCRATCH_SCRIPT_WRITE = new RegExp(String.raw `\b(?:Set-Content|Add-Content|Out-File|New-Item|Copy-Item|Move-Item|Rename-Item|Tee-Object|sc|ac|ni|cp|copy|mv|move|ren|tee|WriteAllText|WriteAllLines|WriteAllBytes|AppendAllText)\b[^|;\n]*?\.cop-tmp[\\/][^\s'"|;),]*\.${SCRIPT_EXT}\b` +
|
|
475
|
+
String.raw `|>{1,2}\s*["']?[^\s"'|;]*\.cop-tmp[\\/][^\s"'|;]*\.${SCRIPT_EXT}\b`, 'i');
|
|
476
|
+
/**
|
|
477
|
+
* Why a chat's step must not write a script into `.cop-tmp`, or null (operator, 2026-10-28: "I want the bot not to make
|
|
478
|
+
* scripts: when something has to run, it runs it dynamically in the terminal; it creates a file only when it is code of
|
|
479
|
+
* the project"). `.cop-tmp` holds what is not the work, so a script there is one written only to be run — refused at
|
|
480
|
+
* the writing, before the operator is asked about running it.
|
|
481
|
+
*/
|
|
482
|
+
export function scratchScriptRefusal(command, reviewer = false) {
|
|
483
|
+
if (!SCRATCH_SCRIPT_WRITE.test(command))
|
|
484
|
+
return null;
|
|
485
|
+
return ('refused: this writes a script into .cop-tmp, and you never write a script to run something (the operator\'s rule): put what it ' +
|
|
486
|
+
'would do in the step itself, in the terminal, on as many lines as it needs. ' +
|
|
487
|
+
(reviewer ? REVIEW_INSTEAD : 'Write a file only when it is code of the project, part of the work.'));
|
|
488
|
+
}
|
|
446
489
|
/** What the reviewer is told instead: a review hands nothing over. */
|
|
447
490
|
const REVIEW_INSTEAD = 'A review runs commands only: check what it would show with commands of your own, written out in the step itself, or say in a finding that the operator has to run it.';
|
|
@@ -60,9 +60,9 @@ export function kindForChat(kind) {
|
|
|
60
60
|
return kind === 'npm' ? 'package.json "scripts" (npm run, npm test, pnpm, yarn)' : SCRIPT_EXTENSIONS[kind].map((e) => `.${e}`).join(' ');
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
|
-
* What the chat is told of the operator's boxes, in the opening, when they are not the defaults: which kinds of
|
|
64
|
-
* it writes the runner runs itself, and which of the project's own it does not run. Nothing
|
|
65
|
-
*
|
|
63
|
+
* What the chat is told of the operator's boxes, in the opening, when they are not the defaults: which kinds of file of
|
|
64
|
+
* code it writes as part of the work the runner runs itself, and which of the project's own it does not run. Nothing
|
|
65
|
+
* when they are as shipped. Either way it never writes a script only to run something (operator, 2026-10-28).
|
|
66
66
|
*/
|
|
67
67
|
export function scriptsNote(rules, written) {
|
|
68
68
|
const mine = SCRIPT_KINDS.filter((k) => rules[k].written);
|
|
@@ -71,16 +71,16 @@ export function scriptsNote(rules, written) {
|
|
|
71
71
|
return undefined;
|
|
72
72
|
const lines = ['## Scripts on this machine', ''];
|
|
73
73
|
if (mine.length > 0) {
|
|
74
|
-
lines.push(`The operator lets the runner run
|
|
75
|
-
'
|
|
74
|
+
lines.push(`The operator lets the runner run files of these kinds that you write as part of the work — code of the project: ${mine.map(kindForChat).join('; ')}. ` +
|
|
75
|
+
'Write such a file in one step and run it in the next, never both in one line, ' +
|
|
76
76
|
(written === 'ask' ? 'and the operator is shown it, in full, before it runs. ' : 'and it runs as any other step. ') +
|
|
77
|
-
'Its text passes the same rules as a step.
|
|
77
|
+
'Its text passes the same rules as a step. Still, never write a script only to run something: that goes in the step itself.');
|
|
78
78
|
}
|
|
79
79
|
if (notOwn.length > 0) {
|
|
80
80
|
if (mine.length > 0)
|
|
81
81
|
lines.push('');
|
|
82
82
|
lines.push(`The operator does not let the runner run the project's own scripts of these kinds: ${notOwn.map(kindForChat).join('; ')}. ` +
|
|
83
|
-
'Do what they do in the step itself
|
|
83
|
+
'Do what they do in the step itself.');
|
|
84
84
|
}
|
|
85
85
|
return lines.join('\n');
|
|
86
86
|
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lines that stop what they should not (operator, 2026-11-03: "this command stops the cop package — it kills its
|
|
3
|
+
* terminal outright"). Two of them, read with the lexer (`commandHeads.ts`) so that a word in a string is not taken
|
|
4
|
+
* for a command:
|
|
5
|
+
*
|
|
6
|
+
* - `stopsTheBot`: a line or a script that stops processes by the name the bot runs under — `node`, which is the bot
|
|
7
|
+
* itself, its API and its web page, and `msedge`, the window its chat is in — or by the bot's own port. An
|
|
8
|
+
* environment switch that "restarts the server" with `Stop-Process -Name node` stops the bot with it, whichever
|
|
9
|
+
* terminal it is typed in. The runner refuses it in a step; the card of commands handed over says so.
|
|
10
|
+
* - `endsTheShell`: a line handed to the operator that ends the PowerShell it is typed in — `exit`, and its kin. The
|
|
11
|
+
* chat writes one out of habit, to pass on an exit code, as a step run in a process of its own would; typed into
|
|
12
|
+
* the operator's open terminal, it closes that terminal, and what it printed with it, which is what the operator
|
|
13
|
+
* was asked to send back.
|
|
14
|
+
*/
|
|
15
|
+
import { scanCommandHeads } from './commandHeads.js';
|
|
16
|
+
/** The programs and cmdlets that stop processes: by name, by id, or by a filter given to them. */
|
|
17
|
+
const KILL_HEADS = new Set([
|
|
18
|
+
'stop-process', 'spps', 'kill', 'taskkill', 'tskill', 'pkill', 'killall', 'kill-port', 'fkill', 'wmic',
|
|
19
|
+
'remove-ciminstance', 'invoke-cimmethod', 'invoke-wmimethod', 'remove-wmiobject',
|
|
20
|
+
]);
|
|
21
|
+
/** The cmdlets that list processes, to hand to one that stops them. */
|
|
22
|
+
const LIST_HEADS = new Set(['get-process', 'gps', 'ps', 'get-ciminstance', 'gcim', 'get-wmiobject', 'gwmi']);
|
|
23
|
+
/** The cmdlets and programs that say which process listens on a port. */
|
|
24
|
+
const PORT_HEADS = new Set(['get-nettcpconnection', 'get-netudpendpoint', 'findstr', 'select-string', 'sls', 'grep', 'kill-port', 'fkill']);
|
|
25
|
+
/** A process stopped by a method of its own: `(Get-Process node).Kill()`, a CIM instance's `Terminate()`. */
|
|
26
|
+
const KILL_METHOD = /\.\s*(?:Kill|Terminate)\s*\(/i;
|
|
27
|
+
/** A filter by name on a list of processes: `Where-Object { $_.Name -eq 'node' }`, `-Filter "Name='node.exe'"`. */
|
|
28
|
+
const NAME_FILTER = /\b(?:Process)?Name\b['"]?\s*(?:-[ic]?(?:eq|like|match)\s*|=\s*\\?)['"]?\*?(node|msedge)(?:\.exe)?\b/i;
|
|
29
|
+
/** The bot's own processes, by the names a line stops them under. */
|
|
30
|
+
const BOT_PROCESSES = [
|
|
31
|
+
{ name: 'node', what: 'the bot itself runs on node (its API and its web page)' },
|
|
32
|
+
{ name: 'msedge', what: "the bot's chat is in an Edge window" },
|
|
33
|
+
];
|
|
34
|
+
/** The operator's terminal, by the names a line stops it under. */
|
|
35
|
+
const SHELL_PROCESSES = ['pwsh', 'powershell', 'cmd', 'windowsterminal', 'conhost', 'wt'];
|
|
36
|
+
/** A head as compared: its file name, lower case, without the extension Windows adds. */
|
|
37
|
+
const headName = (head) => (head.split(/[\\/]/).pop() ?? head).toLowerCase().replace(/\.(?:exe|cmd|bat|com)$/, '');
|
|
38
|
+
/** The words in a call's literal arguments, quoted ones split too: `"IMAGENAME eq node.exe"` gives three. */
|
|
39
|
+
function words(call) {
|
|
40
|
+
return call.args.filter((a) => !a.dynamic).flatMap((a) => a.literal.split(/[\s,;=()'"]+/)).filter(Boolean);
|
|
41
|
+
}
|
|
42
|
+
/** The programs that stop a process only when told to: `wmic … delete`, `Invoke-CimMethod -MethodName Terminate`. */
|
|
43
|
+
const KILL_WHEN_TOLD = new Set(['wmic', 'invoke-cimmethod', 'invoke-wmimethod']);
|
|
44
|
+
/** Whether the call stops processes: a cmdlet or program for it, or `npx kill-port`. */
|
|
45
|
+
function isKill(call) {
|
|
46
|
+
const head = headName(call.head);
|
|
47
|
+
if (head === 'npx')
|
|
48
|
+
return words(call).some((w) => /^(?:kill-port|fkill(?:-cli)?)$/i.test(w));
|
|
49
|
+
if (!KILL_HEADS.has(head))
|
|
50
|
+
return false;
|
|
51
|
+
return !KILL_WHEN_TOLD.has(head) || words(call).some((w) => /^(?:delete|terminate)$/i.test(w));
|
|
52
|
+
}
|
|
53
|
+
/** Whether a word, as a process name with PowerShell's and taskkill's wildcards, takes in `name`. */
|
|
54
|
+
function namesProcess(word, name) {
|
|
55
|
+
if (/^[-/]/.test(word))
|
|
56
|
+
return false;
|
|
57
|
+
const bare = word.replace(/\.exe$/i, '');
|
|
58
|
+
if (!/[*?[]/.test(bare))
|
|
59
|
+
return bare.toLowerCase() === name;
|
|
60
|
+
const pattern = bare.replace(/[.+^${}()|\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.');
|
|
61
|
+
try {
|
|
62
|
+
return new RegExp(`^${pattern}$`, 'i').test(name);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** The process among `names` that the line stops by name, or null. */
|
|
69
|
+
function stoppedByName(text, calls, names) {
|
|
70
|
+
const kills = calls.filter(isKill);
|
|
71
|
+
if (kills.length === 0 && !KILL_METHOD.test(text))
|
|
72
|
+
return null;
|
|
73
|
+
const named = (call) => {
|
|
74
|
+
for (const w of words(call)) {
|
|
75
|
+
const hit = names.find((n) => namesProcess(w, n));
|
|
76
|
+
if (hit)
|
|
77
|
+
return hit;
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
};
|
|
81
|
+
for (const c of kills) {
|
|
82
|
+
const hit = named(c);
|
|
83
|
+
if (hit)
|
|
84
|
+
return hit;
|
|
85
|
+
}
|
|
86
|
+
for (const c of calls.filter((x) => LIST_HEADS.has(headName(x.head)))) {
|
|
87
|
+
const hit = named(c);
|
|
88
|
+
if (hit)
|
|
89
|
+
return hit;
|
|
90
|
+
}
|
|
91
|
+
const filter = NAME_FILTER.exec(text);
|
|
92
|
+
if (filter && names.includes(filter[1].toLowerCase()))
|
|
93
|
+
return filter[1].toLowerCase();
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
/** The bot's port that the line stops a process on, or null: `Get-NetTCPConnection -LocalPort 4000 | … Stop-Process`. */
|
|
97
|
+
function stoppedByPort(text, calls, ports) {
|
|
98
|
+
if (!calls.some(isKill) && !KILL_METHOD.test(text))
|
|
99
|
+
return null;
|
|
100
|
+
for (const c of calls) {
|
|
101
|
+
const head = headName(c.head);
|
|
102
|
+
if (!PORT_HEADS.has(head) && head !== 'npx')
|
|
103
|
+
continue;
|
|
104
|
+
for (const w of words(c)) {
|
|
105
|
+
const n = Number(w.replace(/^:/, ''));
|
|
106
|
+
if (Number.isInteger(n) && ports.includes(n))
|
|
107
|
+
return n;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Why a line or a script must not run because it stops the bot, or null. `ports` are the bot's own (`botPorts`).
|
|
114
|
+
*/
|
|
115
|
+
export function stopsTheBot(text, ports) {
|
|
116
|
+
const { calls } = scanCommandHeads(text);
|
|
117
|
+
const name = stoppedByName(text, calls, BOT_PROCESSES.map((p) => p.name));
|
|
118
|
+
const port = name ? null : stoppedByPort(text, calls, ports);
|
|
119
|
+
if (!name && port === null)
|
|
120
|
+
return null;
|
|
121
|
+
const what = name ? `every process named ${name}, and ${BOT_PROCESSES.find((p) => p.name === name).what}` : `the process on port ${port}, which is the bot's own`;
|
|
122
|
+
return (`refused: this stops ${what}, so it stops the bot too, and the task with it. Stop only the process the work started: ` +
|
|
123
|
+
'by the id Start-Process -PassThru gave for it, or by the port it listens on (Get-NetTCPConnection -LocalPort <its port>), ' +
|
|
124
|
+
'never by the name node or msedge.');
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Why a line typed into the operator's own terminal would end that terminal, or null: `exit` anywhere outside a
|
|
128
|
+
* string, `[Environment]::Exit`, `$host.SetShouldExit`, its own process stopped by `$PID`, or the shell stopped by name.
|
|
129
|
+
*/
|
|
130
|
+
export function endsTheShell(line) {
|
|
131
|
+
const scan = scanCommandHeads(line);
|
|
132
|
+
if (scan.exits)
|
|
133
|
+
return '"exit" closes the PowerShell it is typed in';
|
|
134
|
+
if (/\[\s*(?:System\.)?Environment\s*\]\s*::\s*Exit\s*\(/i.test(line))
|
|
135
|
+
return '[Environment]::Exit closes the PowerShell it is typed in';
|
|
136
|
+
if (/\$host\s*\.\s*SetShouldExit\s*\(/i.test(line))
|
|
137
|
+
return '$host.SetShouldExit closes the PowerShell it is typed in';
|
|
138
|
+
if (/\bGetCurrentProcess\s*\(\s*\)\s*\.\s*Kill\s*\(/i.test(line))
|
|
139
|
+
return 'stopping its own process closes the PowerShell it is typed in';
|
|
140
|
+
const own = scan.calls.some((c) => isKill(c) && c.args.some((a) => /^\$pid$/i.test(a.literal.trim())));
|
|
141
|
+
if (own || /\b(?:Stop-Process|spps|kill)\b[^;|\n]*\$PID\b/i.test(line))
|
|
142
|
+
return 'stopping $PID closes the PowerShell it is typed in';
|
|
143
|
+
const shell = stoppedByName(line, scan.calls, SHELL_PROCESSES);
|
|
144
|
+
if (shell)
|
|
145
|
+
return `stopping every ${shell} process closes the terminal it is typed in`;
|
|
146
|
+
return null;
|
|
147
|
+
}
|