@czottmann/pi-automode 1.13.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project are documented in this file.
|
|
4
4
|
|
|
5
|
+
## [Unreleased]
|
|
6
|
+
|
|
7
|
+
## [1.14.0] - 2026-08-27
|
|
8
|
+
|
|
9
|
+
## Bug fixes
|
|
10
|
+
|
|
11
|
+
- **Classifier stream timeout** — Apply `classifierTimeoutMs` to the full response stream. Provider behavior cannot keep classifier calls pending after the deadline. Parent cancellation remains active. Reject values above the Node.js timer limit. (#30)
|
|
12
|
+
- **OS temp-directory deletes** — Stop hard-denying recursive-delete subtrees under `os.tmpdir()` and `/tmp`. On macOS these resolve into `/private/tmp` and `/private/var/folders`, which matched the `/private` system root and blocked every temp cleanup. Deleting a temp root itself stays blocked. (#31)
|
|
13
|
+
- **Validated temp-root declarations** — Derive the exempt temp roots only from launcher-declared values that stay safe: reject values such as `/`, empty strings, aliases of `HOME`, `/`, or a system root, and ancestors of `HOME`. Without validation, a malformed `TMPDIR` could disable deterministic denials for protected targets, and a broad `permissions.allow` rule could then allow the action without classifier review. Recompute candidates when the effective tmpdir changes. (#31)
|
|
14
|
+
|
|
5
15
|
## [1.13.0] - 2026-08-25
|
|
6
16
|
|
|
7
17
|
## New features
|
|
@@ -35,5 +45,7 @@ All notable changes to this project are documented in this file.
|
|
|
35
45
|
- **Project config trust gate** — Ignore `.pi/automode.local.json` and `.pi/automode.json` until Pi trusts the project. Apply the trust gate during startup and config reloads. (#16)
|
|
36
46
|
- **In-memory observability logs** — Write logs to an extension-owned directory (`~/.pi/agent/extensions/pi-automode/logs/`) instead of the launching project directory. Thanks, @HerbertGao! (#13)
|
|
37
47
|
|
|
48
|
+
[Unreleased]: https://github.com/czottmann/pi-automode/compare/v1.14.0...HEAD
|
|
49
|
+
[1.14.0]: https://github.com/czottmann/pi-automode/compare/v1.13.0...v1.14.0
|
|
38
50
|
[1.13.0]: https://github.com/czottmann/pi-automode/compare/v1.12.0...v1.13.0
|
|
39
51
|
[1.12.0]: https://github.com/czottmann/pi-automode/compare/v1.11.0...v1.12.0
|
package/README.md
CHANGED
|
@@ -101,7 +101,7 @@ The extension blocks these before any allow or classifier decision:
|
|
|
101
101
|
- SSH `authorized_keys` writes
|
|
102
102
|
- cron, launch agent, and system service persistence
|
|
103
103
|
- TLS/certificate/auth weakening patterns
|
|
104
|
-
- root, home, and system-path destructive deletes
|
|
104
|
+
- root, home, and system-path destructive deletes. Subtrees of validated launcher-declared temp directories are treated as disposable. Declared roots that alias `HOME`, `/`, or a system root, or that contain `HOME`, are rejected instead.
|
|
105
105
|
- edits to `.pi/automode*`, `.pi` auto-mode files, and this extension's safety-control files
|
|
106
106
|
|
|
107
107
|
After these checks, pi-automode applies `permissions.allow`. Protected `write` and `edit` targets continue to the classifier.
|
|
@@ -155,6 +155,55 @@ export type ClassifierCompletionPlan = {
|
|
|
155
155
|
reasoningLevel?: Exclude<EffectiveClassifierReasoningLevel, "off">;
|
|
156
156
|
};
|
|
157
157
|
|
|
158
|
+
async function completeClassifierAttempt(
|
|
159
|
+
completeFn: ClassifierCompletionFn,
|
|
160
|
+
model: Model<any>,
|
|
161
|
+
prompt: Parameters<ClassifierCompletionFn>[1],
|
|
162
|
+
parentSignal: AbortSignal | undefined,
|
|
163
|
+
options: Omit<Parameters<ClassifierCompletionFn>[2], "signal">,
|
|
164
|
+
): Promise<AssistantMessage> {
|
|
165
|
+
if (options.timeoutMs === undefined) {
|
|
166
|
+
return completeFn(model, prompt, {
|
|
167
|
+
...options,
|
|
168
|
+
...(parentSignal === undefined ? {} : { signal: parentSignal }),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const controller = new AbortController();
|
|
173
|
+
const onParentAbort = () => controller.abort(parentSignal?.reason);
|
|
174
|
+
if (parentSignal?.aborted) onParentAbort();
|
|
175
|
+
else parentSignal?.addEventListener("abort", onParentAbort, { once: true });
|
|
176
|
+
|
|
177
|
+
let onAbort: (() => void) | undefined;
|
|
178
|
+
const aborted = new Promise<never>((_resolve, reject) => {
|
|
179
|
+
onAbort = () => {
|
|
180
|
+
const reason = controller.signal.reason;
|
|
181
|
+
reject(reason instanceof Error ? reason : new Error("Classifier request aborted."));
|
|
182
|
+
};
|
|
183
|
+
if (controller.signal.aborted) onAbort();
|
|
184
|
+
else controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
185
|
+
});
|
|
186
|
+
const timer = setTimeout(() => {
|
|
187
|
+
controller.abort(
|
|
188
|
+
new Error(`Classifier request timed out after ${options.timeoutMs} ms.`),
|
|
189
|
+
);
|
|
190
|
+
}, options.timeoutMs);
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
return await Promise.race([
|
|
194
|
+
completeFn(model, prompt, {
|
|
195
|
+
...options,
|
|
196
|
+
signal: controller.signal,
|
|
197
|
+
}),
|
|
198
|
+
aborted,
|
|
199
|
+
]);
|
|
200
|
+
} finally {
|
|
201
|
+
clearTimeout(timer);
|
|
202
|
+
if (onAbort) controller.signal.removeEventListener("abort", onAbort);
|
|
203
|
+
parentSignal?.removeEventListener("abort", onParentAbort);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
158
207
|
/**
|
|
159
208
|
* Run normalized Pi AI completion through the provider in Pi's runtime registry.
|
|
160
209
|
* This temporary bridge is only valid until Pi exposes
|
|
@@ -435,14 +484,15 @@ export async function classifyWithRetry(
|
|
|
435
484
|
const started = Date.now();
|
|
436
485
|
let response: AssistantMessage;
|
|
437
486
|
try {
|
|
438
|
-
response = await
|
|
487
|
+
response = await completeClassifierAttempt(
|
|
488
|
+
completeFn,
|
|
439
489
|
classifier.model,
|
|
440
490
|
prompt,
|
|
491
|
+
signal,
|
|
441
492
|
{
|
|
442
493
|
apiKey: classifier.apiKey,
|
|
443
494
|
headers: classifier.headers,
|
|
444
495
|
env: classifier.env,
|
|
445
|
-
signal,
|
|
446
496
|
maxTokens,
|
|
447
497
|
...(temperature === undefined ? {} : { temperature }),
|
|
448
498
|
...(options.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs }),
|
|
@@ -505,7 +555,8 @@ export async function classifyInStages(
|
|
|
505
555
|
const fastStarted = Date.now();
|
|
506
556
|
let fastResponse: AssistantMessage;
|
|
507
557
|
try {
|
|
508
|
-
fastResponse = await
|
|
558
|
+
fastResponse = await completeClassifierAttempt(
|
|
559
|
+
completeFn,
|
|
509
560
|
classifier.model,
|
|
510
561
|
{
|
|
511
562
|
systemPrompt: prompt.systemPrompt,
|
|
@@ -515,11 +566,11 @@ export async function classifyInStages(
|
|
|
515
566
|
stageMessage(CLASSIFIER_FAST_INSTRUCTION),
|
|
516
567
|
],
|
|
517
568
|
},
|
|
569
|
+
signal,
|
|
518
570
|
{
|
|
519
571
|
apiKey: classifier.apiKey,
|
|
520
572
|
headers: classifier.headers,
|
|
521
573
|
env: classifier.env,
|
|
522
|
-
signal,
|
|
523
574
|
// Reasoning and OpenAI-compatible models may consume hidden reasoning,
|
|
524
575
|
// control, and EOS tokens before emitting the required visible digit.
|
|
525
576
|
maxTokens: options.fastClassifierMaxTokens ??
|
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
DEFAULT_MAX_USER_TRANSCRIPT_TOKENS,
|
|
23
23
|
DEFAULT_PROTECTED_PATHS,
|
|
24
24
|
DEFAULT_SOFT_DENY,
|
|
25
|
+
MAX_CLASSIFIER_TIMEOUT_MS,
|
|
25
26
|
PI_GLOBAL_SETTINGS,
|
|
26
27
|
PI_LEGACY_GLOBAL_SETTINGS,
|
|
27
28
|
PI_PROJECT_LOCAL_SETTINGS,
|
|
@@ -324,10 +325,11 @@ export function validateSettingsFile(
|
|
|
324
325
|
if (
|
|
325
326
|
hasOwn(autoMode, "classifierTimeoutMs") &&
|
|
326
327
|
(!Number.isInteger(autoMode.classifierTimeoutMs) ||
|
|
327
|
-
(autoMode.classifierTimeoutMs as number) < 1000
|
|
328
|
+
(autoMode.classifierTimeoutMs as number) < 1000 ||
|
|
329
|
+
(autoMode.classifierTimeoutMs as number) > MAX_CLASSIFIER_TIMEOUT_MS)
|
|
328
330
|
) {
|
|
329
331
|
diagnostics.push(
|
|
330
|
-
`${source}: autoMode.classifierTimeoutMs must be an integer
|
|
332
|
+
`${source}: autoMode.classifierTimeoutMs must be an integer from 1000 through ${MAX_CLASSIFIER_TIMEOUT_MS}`,
|
|
331
333
|
);
|
|
332
334
|
}
|
|
333
335
|
if (
|
|
@@ -591,7 +593,9 @@ function validFastClassifierBudget(value: unknown): value is number {
|
|
|
591
593
|
}
|
|
592
594
|
|
|
593
595
|
function validClassifierTimeout(value: unknown): value is number {
|
|
594
|
-
return Number.isInteger(value) &&
|
|
596
|
+
return Number.isInteger(value) &&
|
|
597
|
+
Number(value) >= 1000 &&
|
|
598
|
+
Number(value) <= MAX_CLASSIFIER_TIMEOUT_MS;
|
|
595
599
|
}
|
|
596
600
|
|
|
597
601
|
function applyAutoModeScalars(
|
|
@@ -61,6 +61,8 @@ export const DENIAL_HISTORY_LIMIT = 12;
|
|
|
61
61
|
|
|
62
62
|
/** Per-request timeout for classifier completions (fast and detailed stages). */
|
|
63
63
|
export const DEFAULT_CLASSIFIER_TIMEOUT_MS = 20_000;
|
|
64
|
+
/** Largest timeout that Node can represent without reducing it to 1 ms. */
|
|
65
|
+
export const MAX_CLASSIFIER_TIMEOUT_MS = 2_147_483_647;
|
|
64
66
|
|
|
65
67
|
/** Built-in trusted environment. Users extend this with `$defaults`. */
|
|
66
68
|
export const DEFAULT_ENVIRONMENT = [
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { statSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
2
3
|
import { resolve } from "node:path";
|
|
3
4
|
import {
|
|
4
5
|
analyzeBash,
|
|
@@ -77,9 +78,115 @@ function matchesPathRoot(path: string, root: string): boolean {
|
|
|
77
78
|
return isSameExistingPath(path.slice(0, root.length), root);
|
|
78
79
|
}
|
|
79
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Top-level directories whose deletion or wholesale modification is a
|
|
83
|
+
* system-wide event. Shared between candidate validation and path
|
|
84
|
+
* classification; do not inline copies.
|
|
85
|
+
*/
|
|
86
|
+
const SYSTEM_ROOTS: ReadonlyArray<string> = [
|
|
87
|
+
"/bin",
|
|
88
|
+
"/boot",
|
|
89
|
+
"/dev",
|
|
90
|
+
"/etc",
|
|
91
|
+
"/home",
|
|
92
|
+
"/lib",
|
|
93
|
+
"/lib64",
|
|
94
|
+
"/Library",
|
|
95
|
+
"/private",
|
|
96
|
+
"/proc",
|
|
97
|
+
"/root",
|
|
98
|
+
"/run",
|
|
99
|
+
"/sbin",
|
|
100
|
+
"/sys",
|
|
101
|
+
"/System",
|
|
102
|
+
"/usr",
|
|
103
|
+
"/var",
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Normalize a proposed temp-root value into a comparable absolute path.
|
|
108
|
+
* Returns undefined for values that cannot denote a subdirectory: the
|
|
109
|
+
* empty string, `/`, and slash-only artifacts.
|
|
110
|
+
*/
|
|
111
|
+
function normalizeRootCandidate(value: string): string | undefined {
|
|
112
|
+
const stripped = value.replace(/\/+$/, "");
|
|
113
|
+
// An empty stripped value must not fall through to `resolve()`, which
|
|
114
|
+
// would silently turn `/` into the process working directory.
|
|
115
|
+
if (!stripped) return undefined;
|
|
116
|
+
const normalized = resolve(stripped);
|
|
117
|
+
if (normalized === "/") return undefined;
|
|
118
|
+
return normalized;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* True when a candidate temp root would weaken the deterministic deny tiers:
|
|
123
|
+
* it aliases (exact, case-folded, or dev/inode) `HOME`, `/`, or any system
|
|
124
|
+
* root, or it is a proper ancestor of the canonical home directory.
|
|
125
|
+
*
|
|
126
|
+
* Dev/inode identity covers symlinked spellings of existing directories;
|
|
127
|
+
* string comparison alone handles candidates that do not exist yet. See
|
|
128
|
+
* issue #31: without these guards, `TMPDIR=/` reduced every absolute path
|
|
129
|
+
* below an empty-string prefix match, and `TMPDIR=/private` made
|
|
130
|
+
* `/private/etc/**` disposable.
|
|
131
|
+
*/
|
|
132
|
+
function conflictsWithProtectedRoots(candidate: string, home: string): boolean {
|
|
133
|
+
if (candidate === "/") return true;
|
|
134
|
+
for (const protectedRoot of ["/", home, ...SYSTEM_ROOTS]) {
|
|
135
|
+
const canonical = resolve(protectedRoot);
|
|
136
|
+
if (
|
|
137
|
+
candidate === canonical ||
|
|
138
|
+
candidate.toLowerCase() === canonical.toLowerCase()
|
|
139
|
+
) {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
if (isSameExistingPath(candidate, canonical)) return true;
|
|
143
|
+
}
|
|
144
|
+
const homeCanonical = resolvePathForPolicy(home) ?? resolve(home);
|
|
145
|
+
return homeCanonical.toLowerCase().startsWith(`${candidate.toLowerCase()}/`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let cachedTempRoots: ReadonlyArray<string> | undefined;
|
|
149
|
+
let cachedTmpdirValue: string | undefined;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Validated launcher-declared temp-dir roots whose subtrees are treated as
|
|
153
|
+
* disposable by `isRootHomeOrSystemPath`: the platform tmpdir, plus `/tmp` on
|
|
154
|
+
* macOS where it exists independently of `os.tmpdir()`. Each root appears in
|
|
155
|
+
* canonical and resolved spelling because callers pass symlink-resolved
|
|
156
|
+
* policy paths (`/tmp` → `/private/tmp` on macOS) and unresolved fallbacks
|
|
157
|
+
* alike. Values from `os.tmpdir()` are not trusted blindly; see
|
|
158
|
+
* `conflictsWithProtectedRoots`. The roots themselves are never exempt:
|
|
159
|
+
* deleting one is a system-wide delete.
|
|
160
|
+
*
|
|
161
|
+
* The memoization keys on the effective `os.tmpdir()` return value so tests
|
|
162
|
+
* can mutate `TMPDIR`, `TMP`, or `TEMP` between calls.
|
|
163
|
+
*/
|
|
164
|
+
export function tempRootCandidates(): ReadonlyArray<string> {
|
|
165
|
+
const currentTmpdir = tmpdir();
|
|
166
|
+
if (cachedTempRoots && cachedTmpdirValue === currentTmpdir) {
|
|
167
|
+
return cachedTempRoots;
|
|
168
|
+
}
|
|
169
|
+
cachedTmpdirValue = currentTmpdir;
|
|
170
|
+
cachedTempRoots = (() => {
|
|
171
|
+
const roots = new Set<string>();
|
|
172
|
+
const consider = (value: string) => {
|
|
173
|
+
const candidate = normalizeRootCandidate(value);
|
|
174
|
+
if (!candidate || conflictsWithProtectedRoots(candidate, HOME)) return;
|
|
175
|
+
roots.add(candidate);
|
|
176
|
+
const resolved = resolvePathForPolicy(candidate);
|
|
177
|
+
if (resolved) roots.add(resolved);
|
|
178
|
+
};
|
|
179
|
+
consider(currentTmpdir);
|
|
180
|
+
if (process.platform === "darwin") consider("/tmp");
|
|
181
|
+
return [...roots];
|
|
182
|
+
})();
|
|
183
|
+
return cachedTempRoots;
|
|
184
|
+
}
|
|
185
|
+
|
|
80
186
|
/**
|
|
81
187
|
* True for `/`, the user's home root, or a top-level system root such as
|
|
82
|
-
* `/etc`, `/usr`, or `/var`. Excludes the home *subtree
|
|
188
|
+
* `/etc`, `/usr`, or `/var`. Excludes the home *subtree* and the subtrees of
|
|
189
|
+
* platform temp directories (`os.tmpdir()` and `/tmp` on macOS).
|
|
83
190
|
*
|
|
84
191
|
* On some distros (e.g. Fedora Silverblue) HOME lives under `/var`, which is
|
|
85
192
|
* in `systemRoots`. Without the subtree exemption, `path.startsWith("/var/")`
|
|
@@ -87,32 +194,36 @@ function matchesPathRoot(path: string, root: string): boolean {
|
|
|
87
194
|
* `rm -rf ~/...`. HOME itself is still matched below, so `rm -rf ~` stays
|
|
88
195
|
* blocked. `home` is a parameter so this can be unit-tested with a synthetic
|
|
89
196
|
* `/var/home/...` value.
|
|
197
|
+
*
|
|
198
|
+
* The temp exemption mirrors the home one and covers cleanup of directories
|
|
199
|
+
* created with `mktemp`, `os.tmpdir()`, or plain `/tmp` paths. On macOS these
|
|
200
|
+
* resolve into `/private/tmp` or `/private/var/folders`, which used to match
|
|
201
|
+
* the `/private` system root and hard-deny every temp cleanup. Deleting a
|
|
202
|
+
* temp root itself still returns true; `tempRoots` is injectable for tests.
|
|
203
|
+
*
|
|
204
|
+
* Protection order matters (issue #31): exact `/`, the exact home root, and
|
|
205
|
+
* system roots win over every exemption. Injected `tempRoots` values are
|
|
206
|
+
* validated per call so hostile or malformed candidates cannot weaken the
|
|
207
|
+
* deterministic tiers.
|
|
90
208
|
*/
|
|
91
|
-
export function isRootHomeOrSystemPath(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
"/lib",
|
|
99
|
-
"/lib64",
|
|
100
|
-
"/Library",
|
|
101
|
-
"/private",
|
|
102
|
-
"/proc",
|
|
103
|
-
"/root",
|
|
104
|
-
"/run",
|
|
105
|
-
"/sbin",
|
|
106
|
-
"/sys",
|
|
107
|
-
"/System",
|
|
108
|
-
"/usr",
|
|
109
|
-
"/var",
|
|
110
|
-
];
|
|
209
|
+
export function isRootHomeOrSystemPath(
|
|
210
|
+
path: string,
|
|
211
|
+
home: string,
|
|
212
|
+
tempRoots: ReadonlyArray<string> = tempRootCandidates(),
|
|
213
|
+
): boolean {
|
|
214
|
+
if (path === "/") return true;
|
|
215
|
+
if (path === home || isSameExistingPath(path, home)) return true;
|
|
111
216
|
if (matchesPathRoot(path, home) && path.length > home.length) return false;
|
|
217
|
+
for (const root of tempRoots) {
|
|
218
|
+
const candidate = normalizeRootCandidate(root);
|
|
219
|
+
if (!candidate || conflictsWithProtectedRoots(candidate, home)) continue;
|
|
220
|
+
if (!matchesPathRoot(path, candidate)) continue;
|
|
221
|
+
// Subtree: disposable. Exact match: the temp root stays protected.
|
|
222
|
+
return path.length > candidate.length ? false : true;
|
|
223
|
+
}
|
|
112
224
|
return (
|
|
113
|
-
path === "/" ||
|
|
114
225
|
matchesPathRoot(path, home) ||
|
|
115
|
-
|
|
226
|
+
SYSTEM_ROOTS.some((root) => matchesPathRoot(path, root))
|
|
116
227
|
);
|
|
117
228
|
}
|
|
118
229
|
|