pi-keeper 0.1.0 → 0.1.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/CHANGELOG.md +78 -0
- package/extensions/ask-mailbox.ts +22 -4
- package/extensions/ask-server.ts +15 -6
- package/extensions/child-cwd.ts +4 -2
- package/extensions/config.ts +24 -12
- package/extensions/index.ts +254 -68
- package/extensions/modes.ts +59 -7
- package/extensions/options.ts +9 -0
- package/extensions/preview.ts +5 -3
- package/extensions/project-rules.ts +81 -12
- package/extensions/remote-relay.ts +10 -5
- package/extensions/rulebook.ts +61 -1
- package/extensions/rules-view.ts +35 -16
- package/extensions/rules.ts +189 -22
- package/extensions/session-grant-store.ts +18 -0
- package/extensions/session-grants.ts +9 -3
- package/extensions/settings-panel.ts +80 -22
- package/extensions/telemetry.ts +99 -2
- package/extensions/ui-relay.ts +6 -1
- package/extensions/utils.ts +1108 -105
- package/package.json +5 -2
- package/prompt-templates/new-rule.md +30 -0
package/extensions/index.ts
CHANGED
|
@@ -96,6 +96,7 @@ import {
|
|
|
96
96
|
} from "./rules.ts";
|
|
97
97
|
import {
|
|
98
98
|
approvalBreakdown,
|
|
99
|
+
commandEffects,
|
|
99
100
|
createScratchDir,
|
|
100
101
|
deriveCommandPrefixes,
|
|
101
102
|
exactRuleTokens,
|
|
@@ -105,6 +106,8 @@ import {
|
|
|
105
106
|
readGrantDir,
|
|
106
107
|
fetchGrantHosts,
|
|
107
108
|
hasRemoval,
|
|
109
|
+
gitGrantRoots,
|
|
110
|
+
gitRepoRootOf,
|
|
108
111
|
isSubagentOutputPath,
|
|
109
112
|
patternLadder,
|
|
110
113
|
scopeSuffix,
|
|
@@ -123,6 +126,8 @@ import {
|
|
|
123
126
|
readDirRuleFor,
|
|
124
127
|
fetchRuleFor,
|
|
125
128
|
parseFetchHost,
|
|
129
|
+
parseVerbatim,
|
|
130
|
+
verbatimRuleFor,
|
|
126
131
|
fileSignature,
|
|
127
132
|
findMatchingRule,
|
|
128
133
|
loadProjectRules,
|
|
@@ -135,6 +140,7 @@ import {
|
|
|
135
140
|
RULES_FILE,
|
|
136
141
|
rulesPath,
|
|
137
142
|
saveRules,
|
|
143
|
+
isInsideDir,
|
|
138
144
|
sha256,
|
|
139
145
|
trustedHash,
|
|
140
146
|
type LoadedRules,
|
|
@@ -149,6 +155,7 @@ import {
|
|
|
149
155
|
matchRules,
|
|
150
156
|
ruleKey,
|
|
151
157
|
userRuleDir,
|
|
158
|
+
ensureUserRuleDir,
|
|
152
159
|
RULE_FIRED_ENTRY,
|
|
153
160
|
RULE_MESSAGE,
|
|
154
161
|
RULES_PER_RUN_CAP,
|
|
@@ -167,12 +174,12 @@ import { isDialogLocked, withDialogLock } from "./dialog-lock.ts";
|
|
|
167
174
|
import { permissionDialog, selectModeDialog, setPreviewTextColor } from "./dialog.ts";
|
|
168
175
|
import { ccExtensionsStatus, ccSettingsPath, excludeExitPlanMode, type CcExtensionsStatus } from "./cc-extensions-compat.ts";
|
|
169
176
|
import { commandHighlighter } from "./shell-highlight-bridge.ts";
|
|
170
|
-
import { backgroundAsksEnabled, CONFIG_FILE,
|
|
171
|
-
import { settingsPanel, type PanelItem, type PanelResult } from "./settings-panel.ts";
|
|
177
|
+
import { backgroundAsksEnabled, CONFIG_FILE, effectiveCommandColors, formatDuration, loadConfig, MAX_BACKGROUND_ASK_TIMEOUT_MS, MIN_BACKGROUND_ASK_TIMEOUT_MS, parseDuration, POPUP_MODES, PREVIEW_TEXT_COLORS, saveConfig, type BackgroundAskPopup, type KeeperConfig, type PreviewTextColor } from "./config.ts";
|
|
178
|
+
import { settingsPanel, type PanelItem, type PanelResult, type PanelSection } from "./settings-panel.ts";
|
|
172
179
|
import { asPlanDetails, PLAN_KEEP, PlanView, selectPlanAction, type PlanChoice, type PlanDetails } from "./plan-approval.ts";
|
|
173
180
|
import { childCwdProblem } from "./child-cwd.ts";
|
|
174
181
|
import { rulesLine } from "./rules-line.ts";
|
|
175
|
-
import { PARENT_SESSION_ENV, publishGrants, readParentGrants, sweepStaleGrants, withdrawGrants } from "./session-grants.ts";
|
|
182
|
+
import { PARENT_SESSION_ENV, publishGrants, readParentGrants, sweepStaleGrants, withdrawGrants, type SessionGitRules } from "./session-grants.ts";
|
|
176
183
|
import { SessionGrants } from "./session-grant-store.ts";
|
|
177
184
|
import { findUiRelay, registerUiRelay, type UiRelay, type UiRelaySessionRules } from "./ui-relay.ts";
|
|
178
185
|
import {
|
|
@@ -196,6 +203,7 @@ import {
|
|
|
196
203
|
logRemoteMode,
|
|
197
204
|
logRulesReview,
|
|
198
205
|
logSession,
|
|
206
|
+
redactCommand,
|
|
199
207
|
targetOf,
|
|
200
208
|
} from "./telemetry.ts";
|
|
201
209
|
import {
|
|
@@ -214,6 +222,7 @@ import {
|
|
|
214
222
|
projectReadGrantLabel,
|
|
215
223
|
fetchGrantLabel,
|
|
216
224
|
projectFetchGrantLabel,
|
|
225
|
+
gitGrantLabel,
|
|
217
226
|
SWITCH_TO_AUTO,
|
|
218
227
|
} from "./options.ts";
|
|
219
228
|
import { DESCRIBE_COMMAND_LIMIT, previewFor, sanitizeForTerminal, type Preview } from "./preview.ts";
|
|
@@ -462,7 +471,7 @@ function describe(call: ToolCall): string {
|
|
|
462
471
|
|
|
463
472
|
function parseMode(value: unknown): ModeName | undefined {
|
|
464
473
|
if (value === "accept-edits") return "auto"; // renamed; migrate old sessions
|
|
465
|
-
return typeof value === "string" && value
|
|
474
|
+
return typeof value === "string" && Object.hasOwn(MODES, value) ? (value as ModeName) : undefined;
|
|
466
475
|
}
|
|
467
476
|
|
|
468
477
|
/** Options of the child entry files (child-plan.ts, child-default.ts); pi itself calls the extension with just `pi`. */
|
|
@@ -528,6 +537,8 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
528
537
|
let rulebookReviewKey: string | undefined;
|
|
529
538
|
let rulebookIgnoredKey: string | undefined;
|
|
530
539
|
let rulebookWarnedKey: string | undefined;
|
|
540
|
+
/** Rulebook files the user loaded this session, by path → content hash: their trust when the store cannot be written. */
|
|
541
|
+
const rulebookApproved = new Map<string, string>();
|
|
531
542
|
/** Fires in this run that asked for a continuation (bounded by RULES_PER_RUN_CAP). */
|
|
532
543
|
let runFires = 0;
|
|
533
544
|
/** Rules files (file + hash) the user loaded or ignored this session, and whose warnings were shown. */
|
|
@@ -628,7 +639,9 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
628
639
|
const dirs = [...grants.outsideReadDirs, ...(shared?.outsideReadDirs ?? []), ...project.readDirs.flatMap((dir) => readGrantDir(dir, "/") ?? [])];
|
|
629
640
|
// fetch grants: this session's, the parent's (a child), and the project's trusted Fetch(<host>) rules
|
|
630
641
|
const hosts = [...grants.fetchHosts, ...(shared?.fetchHosts ?? []), ...project.fetchHosts];
|
|
631
|
-
|
|
642
|
+
// git repos granted in this session, and the parent's (a child): git in them is judged like git in the cwd's repo
|
|
643
|
+
const gitRepos = [...grants.gitRepos, ...(shared?.gitRepos ?? [])];
|
|
644
|
+
return { cwd: subject.cwd, agentDir: getAgentDir(), planFile: subject.planFile, scratchDir: subject.scratchDir, outsideReadDirs: dirs, skillDirs: skillDirsOf(currentSystemPrompt(ctx)), fetchHosts: hosts, gitRepos };
|
|
632
645
|
}
|
|
633
646
|
|
|
634
647
|
/** The session scratch dir (0700 under the OS temp dir), created lazily; undefined if it can't be. */
|
|
@@ -716,7 +729,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
716
729
|
ctx.ui.notify(sanitizeForTerminal(`pi-keeper: ignored in ${loaded.file}:\n${problems.join("\n")}`), "warning");
|
|
717
730
|
}
|
|
718
731
|
// command, read and fetch rules all take effect only once the file is trusted
|
|
719
|
-
const allow = [...loaded.rules.allow, ...loaded.rules.readDirs, ...loaded.rules.fetchHosts];
|
|
732
|
+
const allow = [...loaded.rules.allow, ...loaded.rules.readDirs, ...loaded.rules.fetchHosts, ...loaded.rules.allowVerbatim];
|
|
720
733
|
if (allow.length === 0 || allowActive(loaded) || ignoredKey === key || !ctx.hasUI) return;
|
|
721
734
|
const shown = allow.slice(0, 20).map((r) => ` ${r.text}`);
|
|
722
735
|
if (allow.length > 20) shown.push(` …and ${allow.length - 20} more`);
|
|
@@ -731,8 +744,9 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
731
744
|
approvedKey = key;
|
|
732
745
|
try {
|
|
733
746
|
recordTrust(getAgentDir(), loaded.file, loaded.hash ?? "");
|
|
734
|
-
} catch {
|
|
747
|
+
} catch (error) {
|
|
735
748
|
// approved for this session only
|
|
749
|
+
if (ctx.hasUI) ctx.ui.notify(sanitizeForTerminal((error as Error).message), "warning");
|
|
736
750
|
}
|
|
737
751
|
ctx.ui.notify(`Loaded ${allow.length} allow rule${allow.length === 1 ? "" : "s"} from ${RULES_FILE}`, "info");
|
|
738
752
|
} else {
|
|
@@ -752,6 +766,8 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
752
766
|
countWork("rulebookSync");
|
|
753
767
|
const agentDir = getAgentDir();
|
|
754
768
|
const trust = (file: string): string | undefined => {
|
|
769
|
+
const approved = rulebookApproved.get(file);
|
|
770
|
+
if (approved !== undefined) return approved;
|
|
755
771
|
try {
|
|
756
772
|
return trustedHash(agentDir, file);
|
|
757
773
|
} catch {
|
|
@@ -781,13 +797,17 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
781
797
|
ctx.signal === undefined ? undefined : { signal: ctx.signal },
|
|
782
798
|
), ctx.signal));
|
|
783
799
|
if (choice === LOAD_RULES) {
|
|
800
|
+
let failure: string | undefined;
|
|
784
801
|
for (const entry of book.untrusted) {
|
|
802
|
+
rulebookApproved.set(entry.file, entry.hash);
|
|
785
803
|
try {
|
|
786
804
|
recordTrust(getAgentDir(), entry.file, entry.hash);
|
|
787
|
-
} catch {
|
|
805
|
+
} catch (error) {
|
|
788
806
|
// approved for this session only
|
|
807
|
+
failure ??= (error as Error).message;
|
|
789
808
|
}
|
|
790
809
|
}
|
|
810
|
+
if (failure !== undefined) ctx.ui.notify(sanitizeForTerminal(failure), "warning");
|
|
791
811
|
rulebookReviewKey = key;
|
|
792
812
|
ctx.ui.notify(`Loaded ${book.untrusted.length === 1 ? "1 rulebook rule" : `${book.untrusted.length} rulebook rules`} from this project`, "info");
|
|
793
813
|
} else {
|
|
@@ -800,7 +820,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
800
820
|
* the parent's process (foreground), else the snapshot the parent publishes
|
|
801
821
|
* for background children (session-grants.ts); undefined otherwise.
|
|
802
822
|
*/
|
|
803
|
-
function sharedRules():
|
|
823
|
+
function sharedRules(): SessionGitRules | undefined {
|
|
804
824
|
if (childMode === undefined) return undefined;
|
|
805
825
|
try {
|
|
806
826
|
const relay = findUiRelay();
|
|
@@ -811,8 +831,8 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
811
831
|
}
|
|
812
832
|
|
|
813
833
|
/** This session's own grants (a parent's; what it relays and publishes to its children). */
|
|
814
|
-
function ownGrants(cwd: string | undefined):
|
|
815
|
-
return { prefixes: grants.prefixes, exact: grants.exact, edits: sessionEdits, outsideReads: grants.outsideReadsAllowed, outsideReadDirs: grants.outsideReadDirs, fetchHosts: grants.fetchHosts, ...(cwd !== undefined ? { cwd } : {}), ...(parentThinking !== undefined ? { thinking: parentThinking } : {}) };
|
|
834
|
+
function ownGrants(cwd: string | undefined): SessionGitRules {
|
|
835
|
+
return { prefixes: grants.prefixes, exact: grants.exact, edits: sessionEdits, outsideReads: grants.outsideReadsAllowed, outsideReadDirs: grants.outsideReadDirs, fetchHosts: grants.fetchHosts, gitRepos: grants.gitRepos, ...(cwd !== undefined ? { cwd } : {}), ...(parentThinking !== undefined ? { thinking: parentThinking } : {}) };
|
|
816
836
|
}
|
|
817
837
|
|
|
818
838
|
/** This session's thinking level, from the harness (the session's own level wins over the event copy). */
|
|
@@ -871,8 +891,9 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
871
891
|
...pathContext(ctx, subject, project),
|
|
872
892
|
remembered: shared === undefined ? grants.prefixes : [...grants.prefixes, ...shared.prefixes],
|
|
873
893
|
rememberedExact: shared === undefined ? grants.exact : [...grants.exact, ...shared.exact],
|
|
874
|
-
// an exact-command grant and a workflow command's grants are the parent user's own: a child never sees them
|
|
875
|
-
|
|
894
|
+
// an exact-command grant and a workflow command's grants are the parent user's own: a child never sees them;
|
|
895
|
+
// the project's trusted BashExact entries apply everywhere (strictest wins in rules.ts)
|
|
896
|
+
rememberedVerbatim: [...(remote ? [] : grants.verbatim), ...(project.allow.verbatim ?? [])],
|
|
876
897
|
readOnlyTools: config.readOnlyTools,
|
|
877
898
|
outsideReadsAllowed: grants.outsideReadsAllowed || (shared?.outsideReads ?? false),
|
|
878
899
|
sessionEdits: sessionEdits || (shared?.edits ?? false),
|
|
@@ -1009,6 +1030,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1009
1030
|
sessionRules: () => ownGrants(ctx.cwd),
|
|
1010
1031
|
grantReadDirs: (dirs) => addReadDirs(dirs),
|
|
1011
1032
|
grantFetchHosts: (hosts) => addFetchHosts(hosts),
|
|
1033
|
+
grantGitRepos: (roots) => addGitRepos(roots),
|
|
1012
1034
|
rememberSession: (patterns) => rememberForSession(patterns),
|
|
1013
1035
|
setSessionFlag: (flag) => {
|
|
1014
1036
|
if (flag === "edits") sessionEdits = true;
|
|
@@ -1098,14 +1120,22 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1098
1120
|
if (ctx === undefined) return "deny";
|
|
1099
1121
|
const prefix = `Background subagent ${sanitizeForTerminal(request.childLabel)}:`;
|
|
1100
1122
|
const ui: AskUI = {
|
|
1101
|
-
dialog: (title, preview, options, escLabel) => permissionDialog(ctx, prefixTitle(`${prefix} `, title), preview, options, escLabel, signal, (data) => runModeShortcut(data, ctx)),
|
|
1123
|
+
dialog: (title, preview, options, escLabel, recheck) => permissionDialog(ctx, prefixTitle(`${prefix} `, title), preview, options, escLabel, signal, (data) => runModeShortcut(data, ctx), recheck),
|
|
1102
1124
|
input: (title) => withDialogLock(() => ctx.ui.input(`${prefix} ${title}`, undefined, { signal }), signal),
|
|
1103
1125
|
notify: (message, type) => ctx.ui.notify(message, type),
|
|
1104
1126
|
};
|
|
1105
1127
|
const subject = remoteSubject(request, bgChildMode(request), { ui, signal, receivedAt });
|
|
1106
1128
|
const decision = remoteDecision(request, ctx, subject);
|
|
1107
1129
|
if (decision.action !== "ask") return decision.action;
|
|
1108
|
-
const
|
|
1130
|
+
const call: ToolCall = { toolName: request.call.toolName, input: request.call.input };
|
|
1131
|
+
// Inside the dialog lock, a grant a dialog opened first just made may cover this call: judge again, and skip the dialog then (as the parent's own asks do)
|
|
1132
|
+
const recheck = (): boolean => {
|
|
1133
|
+
const again = remoteDecision(request, ctx, subject);
|
|
1134
|
+
if (again.action !== "allow") return false;
|
|
1135
|
+
logDecision(pi, "redecide", request.id, call, subject.mode, again);
|
|
1136
|
+
return true;
|
|
1137
|
+
};
|
|
1138
|
+
const verdict = await ask(call, decision, ctx, request.id, subject, recheck);
|
|
1109
1139
|
return verdict === undefined ? "allow" : "deny";
|
|
1110
1140
|
}
|
|
1111
1141
|
|
|
@@ -1117,14 +1147,15 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1117
1147
|
return;
|
|
1118
1148
|
}
|
|
1119
1149
|
const outcome: ChoiceOutcome =
|
|
1120
|
-
kind === "auto-allow" ? "remote-auto-allow" : kind === "auto-deny" ? "remote-auto-deny" : kind === "parent-gone" ? "parent-gone" : kind === "cancelled" || kind === "dropped" ? "remote-cancelled" : "remote-denied";
|
|
1150
|
+
kind === "auto-allow" ? "remote-auto-allow" : kind === "auto-deny" ? "remote-auto-deny" : kind === "parent-gone" ? "parent-gone" : kind === "asks-off" ? "asks-off" : kind === "cancelled" || kind === "dropped" ? "remote-cancelled" : "remote-denied";
|
|
1121
1151
|
const call: ToolCall = { toolName: request.call.toolName, input: request.call.input };
|
|
1122
1152
|
logChoice(pi, { toolCallId: request.id, tool: call.toolName, mode, target: targetOf(call), matcher: verdict?.matcher ?? null, options: { primary: [], secondary: [] }, choice: null, outcome, waitMs, via: "background" });
|
|
1123
1153
|
}
|
|
1124
1154
|
|
|
1125
1155
|
/** Parent only: (re)start the server for this session's mailbox; any earlier one is stopped and its pending requests denied. */
|
|
1126
1156
|
function startAsks(ctx: ExtensionContext): void {
|
|
1127
|
-
|
|
1157
|
+
// turning asks off says why to the waiting children; any other restart is the session's end
|
|
1158
|
+
stopAsks(backgroundAsksEnabled(config) ? undefined : "asks-turned-off");
|
|
1128
1159
|
if (childMode !== undefined) return;
|
|
1129
1160
|
// nobody to answer (pi -p, json mode): write no heartbeat, so children see no listening parent and fall back at launch
|
|
1130
1161
|
if (!ctx.hasUI) return;
|
|
@@ -1161,9 +1192,12 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1161
1192
|
});
|
|
1162
1193
|
}
|
|
1163
1194
|
|
|
1164
|
-
/**
|
|
1165
|
-
|
|
1166
|
-
|
|
1195
|
+
/**
|
|
1196
|
+
* Deny what is pending, close its dialogs, stop polling: "parent-session-ended" when the session ends, or
|
|
1197
|
+
* "asks-turned-off" when background asks are turned off (startAsks passes it on that restart).
|
|
1198
|
+
*/
|
|
1199
|
+
function stopAsks(reason?: "asks-turned-off"): void {
|
|
1200
|
+
askServer?.stop(reason);
|
|
1167
1201
|
askServer = undefined;
|
|
1168
1202
|
askCtx = undefined;
|
|
1169
1203
|
}
|
|
@@ -1182,6 +1216,19 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1182
1216
|
syncGrants();
|
|
1183
1217
|
}
|
|
1184
1218
|
|
|
1219
|
+
/**
|
|
1220
|
+
* Add granted git repo roots (deduplicated). Each one is resolved again to its repo root (gitRepoRootOf): "/", the
|
|
1221
|
+
* home dir and its ancestors, and paths without a .git are dropped, and the realpath is stored.
|
|
1222
|
+
*/
|
|
1223
|
+
function addGitRepos(roots: readonly string[]): void {
|
|
1224
|
+
for (const root of roots) {
|
|
1225
|
+
if (typeof root !== "string" || !isAbsolute(root)) continue;
|
|
1226
|
+
const real = gitRepoRootOf(root);
|
|
1227
|
+
if (real !== undefined) grants.addGitRepo(real);
|
|
1228
|
+
}
|
|
1229
|
+
syncGrants();
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1185
1232
|
/** Add granted fetch hosts (deduplicated). Checked again like addReadDirs: a child's relay call arrives from outside. */
|
|
1186
1233
|
function addFetchHosts(hosts: readonly string[]): void {
|
|
1187
1234
|
for (const host of hosts) {
|
|
@@ -1292,7 +1339,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1292
1339
|
}
|
|
1293
1340
|
|
|
1294
1341
|
function refreshStatus(ctx: ExtensionContext): void {
|
|
1295
|
-
ctx.ui.setStatus(STATUS_KEY, formatModeStatus(ctx.ui.theme, currentMode, sessionEdits && currentMode === "default"));
|
|
1342
|
+
ctx.ui.setStatus(STATUS_KEY, config.modeStatus ? formatModeStatus(ctx.ui.theme, currentMode, sessionEdits && currentMode === "default") : undefined);
|
|
1296
1343
|
}
|
|
1297
1344
|
|
|
1298
1345
|
let flashTimer: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -1311,13 +1358,13 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1311
1358
|
/**
|
|
1312
1359
|
* The working row reads `<icon> <badge> · Working` in plan and auto mode (default mode keeps
|
|
1313
1360
|
* pi's own text), so the mode is in front of you while a run is active. It stays set between
|
|
1314
|
-
* runs: pi shows it only while streaming.
|
|
1361
|
+
* runs: pi shows it only while streaming. Off by default (`modeWorkingRow`).
|
|
1315
1362
|
*/
|
|
1316
1363
|
let workingMessageSet = false;
|
|
1317
1364
|
function syncWorkingMessage(ctx: ExtensionContext, force = false): void {
|
|
1318
1365
|
if (!ctx.hasUI) return;
|
|
1319
1366
|
try {
|
|
1320
|
-
if (childMode !== undefined || currentMode === "default" || config.
|
|
1367
|
+
if (childMode !== undefined || currentMode === "default" || !config.modeWorkingRow) {
|
|
1321
1368
|
// nothing to show: undo only what this function set (or what a dialog overwrote)
|
|
1322
1369
|
if (workingMessageSet || force) ctx.ui.setWorkingMessage();
|
|
1323
1370
|
workingMessageSet = false;
|
|
@@ -1436,7 +1483,9 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1436
1483
|
const mode = parseMode(data?.mode);
|
|
1437
1484
|
if (mode !== undefined) {
|
|
1438
1485
|
currentMode = mode;
|
|
1439
|
-
|
|
1486
|
+
// only a plan file inside the plans dir: anything else would be writable in every mode
|
|
1487
|
+
const restored = typeof data?.planFilePath === "string" ? data.planFilePath : undefined;
|
|
1488
|
+
planFilePath = restored !== undefined && isInsideDir(restored, plansDir()) ? restored : undefined;
|
|
1440
1489
|
persistedState = JSON.stringify({ mode: data?.mode, planFilePath });
|
|
1441
1490
|
}
|
|
1442
1491
|
// A fork-context child copies the parent's state entries: ignore them (never auto, no plan file)
|
|
@@ -1523,6 +1572,8 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1523
1572
|
if (result.reason === "timeout") return "the parent's user did not answer in time";
|
|
1524
1573
|
if (result.reason === "error") return "the parent could not be reached";
|
|
1525
1574
|
if (result.reason === "no-ui") return "the parent session has no interactive user";
|
|
1575
|
+
if (result.reason === "asks-turned-off") return "background asks were turned off in the parent session";
|
|
1576
|
+
if (result.reason === "request-size") return "the request is too large to send to the parent";
|
|
1526
1577
|
return "the parent session has ended";
|
|
1527
1578
|
}
|
|
1528
1579
|
|
|
@@ -1613,16 +1664,20 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1613
1664
|
// must also be representable in the project file (single segment, safe
|
|
1614
1665
|
// tokens, nothing a rule may never cover). Offered to children too: like
|
|
1615
1666
|
// the prefix project option, it saves to the child's own project.
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1667
|
+
// Gated on exactCommand, like REMEMBER_EXACT. Without a token rule (quoted text, a loop), the exact text itself is saved.
|
|
1668
|
+
// Not for a program-running flag or a write outside the project: the session option only.
|
|
1669
|
+
const projectExactAllowed = config.projectExactCommands && exactCommand && !runsProgramOrWritesOutside(commandOf(call.input), ctx, subject);
|
|
1670
|
+
const projectExactCommandsTokens = projectExactAllowed ? projectExactCommandsTokensOf(commandOf(call.input), ctx, subject) : undefined;
|
|
1671
|
+
const projectExactVerbatim = projectExactAllowed && projectExactCommandsTokens === undefined ? projectVerbatimOf(commandOf(call.input)) : undefined;
|
|
1672
|
+
const projectExactOffered = projectExactCommandsTokens !== undefined || projectExactVerbatim !== undefined;
|
|
1620
1673
|
// an outside read: offer to allow the directories (not for "/" or the home dir; those take the all-reads option)
|
|
1621
1674
|
const grantDirs = decision.rule?.match === "outsideCwdRead" ? readGrantDirsOf(call, pathContext(ctx, subject)) : undefined;
|
|
1622
1675
|
// a curl/wget GET to a host not yet allowed: offer the host (only when that is all the ask is about)
|
|
1623
1676
|
const fetchMatch = decision.rule?.match === undefined || decision.rule.match === "unapprovedBash";
|
|
1624
1677
|
const grantHosts = call.toolName === "bash" && fetchMatch ? fetchGrantHosts(commandOf(call.input), subject.cwd, subject.scratchDir, pathContext(ctx, subject).fetchHosts) : [];
|
|
1625
1678
|
const hostGrant = grantHosts.length > 0 ? grantHosts : undefined;
|
|
1679
|
+
// a cd into another git repository (CD_GIT_REASON): offer its repo roots; only where a plain bash ask or the default fallback reaches
|
|
1680
|
+
const gitRoots = call.toolName === "bash" && (decision.rule === undefined || decision.rule.match === "unapprovedBash") ? gitGrantRoots(commandOf(call.input), subject.cwd, subject.scratchDir, pathContext(ctx, subject).gitRepos) : undefined;
|
|
1626
1681
|
const { primary, secondary } = dialogOptions({
|
|
1627
1682
|
mode: subject.mode,
|
|
1628
1683
|
toolName: call.toolName,
|
|
@@ -1630,11 +1685,12 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1630
1685
|
sessionLabel,
|
|
1631
1686
|
projectLabel,
|
|
1632
1687
|
exactCommand,
|
|
1633
|
-
projectExactCommands:
|
|
1688
|
+
projectExactCommands: projectExactOffered,
|
|
1634
1689
|
readGrantLabel: grantDirs === undefined ? undefined : readGrantLabel(grantDirs),
|
|
1635
1690
|
projectReadGrantLabel: grantDirs === undefined || !config.projectReadApprovals ? undefined : projectReadGrantLabel(grantDirs),
|
|
1636
1691
|
fetchGrantLabel: hostGrant === undefined ? undefined : fetchGrantLabel(hostGrant),
|
|
1637
1692
|
projectFetchGrantLabel: hostGrant === undefined || !config.projectCommandApprovals ? undefined : projectFetchGrantLabel(hostGrant),
|
|
1693
|
+
gitGrantLabel: gitRoots === undefined ? undefined : gitGrantLabel(gitRoots),
|
|
1638
1694
|
hasLadder: ladder !== undefined,
|
|
1639
1695
|
child: subject.child,
|
|
1640
1696
|
// a background child's reply carries no text back, so there is nowhere for feedback to go
|
|
@@ -1642,7 +1698,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1642
1698
|
sharedEdits: subject.remote !== undefined,
|
|
1643
1699
|
});
|
|
1644
1700
|
|
|
1645
|
-
const why = notReadOnly ? formatBreakdown(approvalBreakdown(commandOf(call.input), subject.cwd, coverSources(ctx, subject), subject.scratchDir, pathContext(ctx, subject).fetchHosts)) : [];
|
|
1701
|
+
const why = notReadOnly ? formatBreakdown(approvalBreakdown(commandOf(call.input), subject.cwd, coverSources(ctx, subject), subject.scratchDir, pathContext(ctx, subject).fetchHosts, pathContext(ctx, subject).gitRepos)) : [];
|
|
1646
1702
|
const hints = [...why, askHint(decision.rule?.match, commandOf(call.input), pathContext(ctx, subject), decision.detail)].filter((line) => line !== undefined);
|
|
1647
1703
|
// The command/path is model-controlled: segments are sanitized for the terminal (title.ts)
|
|
1648
1704
|
const { label, main, lang } = describeParts(call);
|
|
@@ -1665,8 +1721,11 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1665
1721
|
}),
|
|
1666
1722
|
]);
|
|
1667
1723
|
// With a preview the dialog is re-opened for the "More…" list, so the diff or command stays visible
|
|
1724
|
+
// a recheck that answers the dialog is logged as "covered", not as the user's answer
|
|
1725
|
+
let covered = false;
|
|
1726
|
+
const watched = recheck === undefined ? undefined : (): boolean => (covered = recheck());
|
|
1668
1727
|
const show = (options: string[], back: boolean): Promise<string | undefined> =>
|
|
1669
|
-
ui.dialog(back ? backTitle() : title, preview, options, back ? "back" : "reject",
|
|
1728
|
+
ui.dialog(back ? backTitle() : title, preview, options, back ? "back" : "reject", watched);
|
|
1670
1729
|
// The relayed dialog rings and sets the parent's working row itself; a background request
|
|
1671
1730
|
// rings from the ask server, when it decides to open
|
|
1672
1731
|
if (!subject.child && subject.remote === undefined) attention(ctx, "pi: permission needed");
|
|
@@ -1688,14 +1747,15 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1688
1747
|
// a request that ended some other way (cancelled, session over) is logged by the ask server
|
|
1689
1748
|
if (subject.signal?.aborted === true) return;
|
|
1690
1749
|
const denied = outcome === "reject" || outcome === "reject-feedback";
|
|
1691
|
-
|
|
1750
|
+
const remoteOutcome: ChoiceOutcome = outcome === "covered" ? "covered" : denied ? "remote-denied" : "remote-allowed";
|
|
1751
|
+
logChoice(pi, { toolCallId, tool: call.toolName, mode, target, matcher, options: { primary, secondary }, choice: choice ?? null, outcome: remoteOutcome, waitMs: Date.now() - subject.remote.receivedAt, via: "background", ...extra });
|
|
1692
1752
|
return;
|
|
1693
1753
|
}
|
|
1694
1754
|
logChoice(pi, { toolCallId, tool: call.toolName, mode, target, matcher, options: { primary, secondary }, choice: choice ?? null, outcome, waitMs, ...(childMode !== undefined ? { via: "child-relay" as const } : {}), ...extra });
|
|
1695
1755
|
};
|
|
1696
1756
|
|
|
1697
1757
|
if (choice === ALLOW_ONCE) {
|
|
1698
|
-
report("allow-once");
|
|
1758
|
+
report(covered ? "covered" : "allow-once");
|
|
1699
1759
|
return undefined;
|
|
1700
1760
|
}
|
|
1701
1761
|
if ((choice === ALLOW_EDITS || choice === ALLOW_EDITS_SHARED) && primary.includes(choice)) {
|
|
@@ -1768,6 +1828,21 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1768
1828
|
saveFetchHostsToProject(subject, ui, hostGrant);
|
|
1769
1829
|
return undefined;
|
|
1770
1830
|
}
|
|
1831
|
+
if (gitRoots !== undefined && choice === gitGrantLabel(gitRoots)) {
|
|
1832
|
+
report("remember-session", { remembered: `git in ${gitRoots.map((r) => sanitizeForTerminal(`${r}/`)).join(", ")}`, scope: "session" });
|
|
1833
|
+
addGitRepos(gitRoots);
|
|
1834
|
+
if (relay?.grantGitRepos !== undefined) {
|
|
1835
|
+
try {
|
|
1836
|
+
relay.grantGitRepos(gitRoots);
|
|
1837
|
+
} catch {
|
|
1838
|
+
// the local grant stands
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
ui.notify(`Git allowed in ${gitRoots.map((r) => sanitizeForTerminal(`${r}/`)).join(", ")} for this session. Dangerous git commands still ask.`, "info");
|
|
1842
|
+
// judged again: the rest of the command may still need an answer
|
|
1843
|
+
const again = decide(call, ctx, subject);
|
|
1844
|
+
return again.action === "allow" ? undefined : ask(call, again, ctx, toolCallId, subject, recheck);
|
|
1845
|
+
}
|
|
1771
1846
|
if (choice === REJECT_WITH_FEEDBACK) {
|
|
1772
1847
|
const feedback = (await ui.input("What should pi do instead? (empty: just say no)"))?.trim();
|
|
1773
1848
|
// With feedback the agent continues with it; without, a plain reject
|
|
@@ -1785,16 +1860,19 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1785
1860
|
}
|
|
1786
1861
|
if (choice === exactLabel && exactCommand) {
|
|
1787
1862
|
const command = commandOf(call.input);
|
|
1788
|
-
|
|
1863
|
+
// redacted before it is cut: a cut inside a quoted secret would leave a half that no rule can see
|
|
1864
|
+
const shown = redactCommand(command);
|
|
1865
|
+
report("remember-session", { remembered: `exact: ${shown.length > 80 ? `${shown.slice(0, 80)}\u2026` : shown}`, scope: "session" });
|
|
1789
1866
|
if (exactTokens === undefined) rememberVerbatim(command);
|
|
1790
1867
|
else rememberForSession([{ tokens: exactTokens, exact: true }]);
|
|
1791
1868
|
ui.notify("Allowed for this session: this exact command, character for character. Plan mode still asks; /permissions lists and removes it.", "warning");
|
|
1792
1869
|
return undefined;
|
|
1793
1870
|
}
|
|
1794
|
-
if (choice === PROJECT_EXACT &&
|
|
1795
|
-
const
|
|
1796
|
-
report("remember-project", { remembered: `exact: ${
|
|
1797
|
-
saveToProject(subject, ui, [{ tokens: projectExactCommandsTokens, exact: true }]);
|
|
1871
|
+
if (choice === PROJECT_EXACT && projectExactOffered) {
|
|
1872
|
+
const shown = redactCommand(commandOf(call.input));
|
|
1873
|
+
report("remember-project", { remembered: `exact: ${shown.length > 80 ? `${shown.slice(0, 80)}\u2026` : shown}`, scope: "project" });
|
|
1874
|
+
if (projectExactCommandsTokens !== undefined) saveToProject(subject, ui, [{ tokens: projectExactCommandsTokens, exact: true }]);
|
|
1875
|
+
else if (projectExactVerbatim !== undefined) saveVerbatimToProject(subject, ui, projectExactVerbatim);
|
|
1798
1876
|
return undefined;
|
|
1799
1877
|
}
|
|
1800
1878
|
if (choice !== undefined && choice === sessionLabel && prefixes !== undefined) {
|
|
@@ -1870,10 +1948,13 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1870
1948
|
|
|
1871
1949
|
/** Why a bash call ran without a dialog, from the rule that allowed it: `allowed by project rule: git commit *`. Read-only calls get none. */
|
|
1872
1950
|
function allowNote(call: ToolCall, decision: Decision, ctx: ExtensionContext): string | undefined {
|
|
1873
|
-
if (call.toolName === "bash" && decision.rule?.match === "rememberedVerbatim")
|
|
1951
|
+
if (call.toolName === "bash" && decision.rule?.match === "rememberedVerbatim") {
|
|
1952
|
+
// the session list and the project's BashExact entries share this matcher: say which one
|
|
1953
|
+
return grants.verbatim.includes(verbatimKey(commandOf(call.input))) ? "allowed for this session: exact command" : "allowed by this project's exact-command rule";
|
|
1954
|
+
}
|
|
1874
1955
|
if (call.toolName !== "bash" || (decision.rule?.match !== "rememberedBash" && decision.rule?.match !== "projectAllowBash")) return undefined;
|
|
1875
1956
|
const sources = coverSources(ctx);
|
|
1876
|
-
const used = coveringPatterns(commandOf(call.input), sources, ctx.cwd, scratchDir, pathContext(ctx).fetchHosts);
|
|
1957
|
+
const used = coveringPatterns(commandOf(call.input), sources, ctx.cwd, scratchDir, pathContext(ctx).fetchHosts, pathContext(ctx).gitRepos);
|
|
1877
1958
|
const groups = sources.map((source) => ({ source, list: used.filter((u) => u.label === source.label) })).filter((g) => g.list.length > 0);
|
|
1878
1959
|
if (groups.length === 0) return undefined;
|
|
1879
1960
|
return sanitizeForTerminal(groups.map((g) => `${g.source.badge}: ${g.list.map((u) => patternString(u.tokens, u.exact)).join(", ")}`).join(" \u00b7 "));
|
|
@@ -1893,6 +1974,49 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
1893
1974
|
return "error" in parsed ? undefined : tokens;
|
|
1894
1975
|
}
|
|
1895
1976
|
|
|
1977
|
+
/**
|
|
1978
|
+
* A program-running flag (`sed '1e id'`, `git -c`, `find -exec`), or a write
|
|
1979
|
+
* whose targets don't pass the project check (outside, protected, secret or
|
|
1980
|
+
* unknown): no project exact rule for it. A write inside the project keeps
|
|
1981
|
+
* the option (deriveCommandPrefixes is undefined exactly for the failing writes).
|
|
1982
|
+
*/
|
|
1983
|
+
function runsProgramOrWritesOutside(command: string, ctx: ExtensionContext, subject: AskSubject): boolean {
|
|
1984
|
+
const pc = pathContext(ctx, subject);
|
|
1985
|
+
const effects = commandEffects(command, pc.cwd, pc.scratchDir, pc.fetchHosts, pc.gitRepos);
|
|
1986
|
+
if (effects?.includes("exec") === true) return true;
|
|
1987
|
+
return effects?.includes("write") === true && deriveCommandPrefixes(command, pc) === undefined;
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
/** The exact command text for a project `BashExact(...)` rule, when the file can hold it (no control characters); undefined otherwise. */
|
|
1991
|
+
function projectVerbatimOf(command: string): string | undefined {
|
|
1992
|
+
const key = verbatimKey(command);
|
|
1993
|
+
const parsed = parseVerbatim(verbatimRuleFor(key));
|
|
1994
|
+
return parsed === undefined || "error" in parsed ? undefined : key;
|
|
1995
|
+
}
|
|
1996
|
+
|
|
1997
|
+
/**
|
|
1998
|
+
* Write `BashExact(<command text>)` to the project file and tell the user.
|
|
1999
|
+
* Like saveToProject: if the file holds unreviewed rules, or the write
|
|
2000
|
+
* fails, the command applies for the session instead.
|
|
2001
|
+
*/
|
|
2002
|
+
function saveVerbatimToProject(where: { cwd: string }, ui: AskUI, command: string): void {
|
|
2003
|
+
const { loaded } = syncProject(where);
|
|
2004
|
+
try {
|
|
2005
|
+
const saved = saveRules(where.cwd, getAgentDir(), "allow", [verbatimRuleFor(command)], allowActive(loaded) ? loaded.hash : undefined);
|
|
2006
|
+
projectState = undefined;
|
|
2007
|
+
if (saved.trusted) {
|
|
2008
|
+
ui.notify(`Saved to ${RULES_FILE} as this exact text, character for character (default mode only; /permissions lists it)`, "info");
|
|
2009
|
+
} else {
|
|
2010
|
+
// the file held rules nobody reviewed: don't bless them with our write
|
|
2011
|
+
rememberVerbatim(command);
|
|
2012
|
+
ui.notify(`Saved to ${RULES_FILE}, but it holds unreviewed rules, so this exact command applies only for this session`, "warning");
|
|
2013
|
+
}
|
|
2014
|
+
} catch (error) {
|
|
2015
|
+
rememberVerbatim(command);
|
|
2016
|
+
ui.notify(sanitizeForTerminal(`Could not save to ${RULES_FILE}: ${error instanceof Error ? error.message : String(error)}\nAllowed for this session instead.`), "error");
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
|
|
1896
2020
|
/**
|
|
1897
2021
|
* Write allow rules to the project file and tell the user. If the file
|
|
1898
2022
|
* holds unreviewed rules, or the write fails, the rules apply for the
|
|
@@ -2402,10 +2526,10 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2402
2526
|
const { loaded } = syncProject(ctx);
|
|
2403
2527
|
const lines = [`Current mode: ${MODES[currentMode].badge}`];
|
|
2404
2528
|
if (loaded.exists) {
|
|
2405
|
-
const { allow, ask, deny, readDirs, fetchHosts } = loaded.rules;
|
|
2406
|
-
const state = allow.length + readDirs.length + fetchHosts.length === 0 ? "" : allowActive(loaded) ? " (loaded)" : ignoredKey === keyOf(loaded) ? " (ignored this session)" : " (awaiting review)";
|
|
2407
|
-
lines.push(`Project rules (${loaded.file}): allow ${allow.length + readDirs.length + fetchHosts.length}${state}, ask ${ask.length}, deny ${deny.length}`);
|
|
2408
|
-
for (const [list, rules] of [["allow", allow], ["ask", ask], ["deny", deny]] as const) {
|
|
2529
|
+
const { allow, ask, deny, readDirs, fetchHosts, allowVerbatim } = loaded.rules;
|
|
2530
|
+
const state = allow.length + readDirs.length + fetchHosts.length + allowVerbatim.length === 0 ? "" : allowActive(loaded) ? " (loaded)" : ignoredKey === keyOf(loaded) ? " (ignored this session)" : " (awaiting review)";
|
|
2531
|
+
lines.push(`Project rules (${loaded.file}): allow ${allow.length + readDirs.length + fetchHosts.length + allowVerbatim.length}${state}, ask ${ask.length}, deny ${deny.length}`);
|
|
2532
|
+
for (const [list, rules] of [["allow", [...allow, ...allowVerbatim]], ["ask", ask], ["deny", deny]] as const) {
|
|
2409
2533
|
for (const rule of rules) lines.push(` ${list}: ${rule.text}`);
|
|
2410
2534
|
}
|
|
2411
2535
|
}
|
|
@@ -2501,6 +2625,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2501
2625
|
return items.length > 0 ? items : null;
|
|
2502
2626
|
},
|
|
2503
2627
|
handler: async (args, ctx) => {
|
|
2628
|
+
ensureUserRuleDir(getAgentDir()); // first use: create the folder and an example rule
|
|
2504
2629
|
const book = syncRulebook(ctx);
|
|
2505
2630
|
const wanted = args.trim().toLowerCase();
|
|
2506
2631
|
const fires = (name: string): string => (firedRules.has(name) ? "1 fire" : "not fired");
|
|
@@ -2522,7 +2647,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2522
2647
|
return;
|
|
2523
2648
|
}
|
|
2524
2649
|
if (book.rules.length === 0 && book.problems.length === 0 && book.untrusted.length === 0) {
|
|
2525
|
-
ctx.ui.notify(sanitizeForTerminal(`No rulebook rules.
|
|
2650
|
+
ctx.ui.notify(sanitizeForTerminal(`No rulebook rules. Put one .md file per rule in ${userRuleDir(getAgentDir())}/ (all projects) or <project>/.pi/rules/ (one project):\n\n---\ndescription: One line on what must not happen.\ncondition: (?i)\\bshould work\\b\n---\nDo not say "should work". Run it and quote the result.\n\n"condition" is a JavaScript regex matched against the end of the model's answer; the text below the dashes is the correction the model receives.`), "info");
|
|
2526
2651
|
return;
|
|
2527
2652
|
}
|
|
2528
2653
|
// a TUI gets the coloured view (rules-view.ts); anything else the plain listing
|
|
@@ -2596,6 +2721,15 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2596
2721
|
},
|
|
2597
2722
|
});
|
|
2598
2723
|
}
|
|
2724
|
+
for (const repo of [...grants.gitRepos]) {
|
|
2725
|
+
entries.push({
|
|
2726
|
+
label: `[session] git in ${sanitizeForTerminal(repo)}/ allowed`,
|
|
2727
|
+
remove: () => {
|
|
2728
|
+
grants.removeGitRepo(repo);
|
|
2729
|
+
syncGrants();
|
|
2730
|
+
},
|
|
2731
|
+
});
|
|
2732
|
+
}
|
|
2599
2733
|
if (grants.outsideReadsAllowed) {
|
|
2600
2734
|
entries.push({
|
|
2601
2735
|
label: "[session] reads outside the working directory allowed",
|
|
@@ -2617,6 +2751,16 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2617
2751
|
},
|
|
2618
2752
|
});
|
|
2619
2753
|
}
|
|
2754
|
+
for (const rule of loaded.rules.allowVerbatim) {
|
|
2755
|
+
entries.push({
|
|
2756
|
+
label: `[project allow] ${sanitizeForTerminal(rule.text)} (exact text)${state}`,
|
|
2757
|
+
remove: () => {
|
|
2758
|
+
const result = removeRule(ctx.cwd, getAgentDir(), "allow", rule.text, allowActive(loaded) ? loaded.hash : undefined);
|
|
2759
|
+
projectState = undefined;
|
|
2760
|
+
if (!result.removed) ctx.ui.notify(`${rule.text} is no longer in ${RULES_FILE}`, "warning");
|
|
2761
|
+
},
|
|
2762
|
+
});
|
|
2763
|
+
}
|
|
2620
2764
|
for (const rule of loaded.rules.readDirs) {
|
|
2621
2765
|
entries.push({
|
|
2622
2766
|
label: `[project allow] ${rule.text} (reads; secrets still ask)${state}`,
|
|
@@ -2745,8 +2889,6 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2745
2889
|
readonly kind: "choice";
|
|
2746
2890
|
readonly choices: readonly string[];
|
|
2747
2891
|
value(): string;
|
|
2748
|
-
/** The panel row's label when it carries more than name() (command colors: the value in effect and its note). */
|
|
2749
|
-
panelLabel?(): string;
|
|
2750
2892
|
/** True while the value is the built-in default, so choosing that same value still saves it. */
|
|
2751
2893
|
unsaved?(): boolean;
|
|
2752
2894
|
set(value: string, ctx: ExtensionContext): string | undefined;
|
|
@@ -2781,7 +2923,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2781
2923
|
}
|
|
2782
2924
|
function modeFlashNote(): string {
|
|
2783
2925
|
const secs = config.modeFlashSecs;
|
|
2784
|
-
return secs <= 0 ? "off" : `${secs} s
|
|
2926
|
+
return secs <= 0 ? "off" : `${secs} s`;
|
|
2785
2927
|
}
|
|
2786
2928
|
|
|
2787
2929
|
/** Whether pi-shell-highlight's highlighter is registered now (read at each use, so a later load counts). */
|
|
@@ -2862,7 +3004,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2862
3004
|
kind: "toggle",
|
|
2863
3005
|
key: "project-exact-commands",
|
|
2864
3006
|
value: () => config.projectExactCommands,
|
|
2865
|
-
name: () =>
|
|
3007
|
+
name: () => "Allow exact commands in project",
|
|
2866
3008
|
label: () => `Offer "always allow in this project" for exact commands: ${config.projectExactCommands ? "on" : "off"}`,
|
|
2867
3009
|
summary: "On: for a command no prefix can safely cover (an interpreter call such as `python3 x.py`), the dialog also offers \"always allow\" for that exact command, in the current project only, saved to .pi/settings.local.json. Off: such a command can be allowed for this session at most.",
|
|
2868
3010
|
set(value, ctx) {
|
|
@@ -2876,7 +3018,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2876
3018
|
kind: "toggle",
|
|
2877
3019
|
key: "project-command-approvals",
|
|
2878
3020
|
value: () => config.projectCommandApprovals,
|
|
2879
|
-
name: () =>
|
|
3021
|
+
name: () => "Allow commands in project",
|
|
2880
3022
|
label: () => `Offer "always allow in this project" for commands: ${config.projectCommandApprovals ? "on" : "off"}`,
|
|
2881
3023
|
summary: "On: the command dialog also offers \"always allow\" for the command's prefix (for example `npm test *`) and for fetch hosts, in the current project only. It is saved to this project's .pi/settings.local.json; other projects still ask. Off: command approvals last for one call or this session.",
|
|
2882
3024
|
set(value) {
|
|
@@ -2890,7 +3032,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2890
3032
|
kind: "toggle",
|
|
2891
3033
|
key: "project-read-approvals",
|
|
2892
3034
|
value: () => config.projectReadApprovals,
|
|
2893
|
-
name: () =>
|
|
3035
|
+
name: () => "Allow read dirs in project",
|
|
2894
3036
|
label: () => `Offer "always allow in this project" for outside read directories: ${config.projectReadApprovals ? "on" : "off"}`,
|
|
2895
3037
|
summary: "Reads inside the current project never ask. On: when the agent reads a directory outside the project, the dialog also offers \"always allow reading from that directory while working in this project\", saved to this project's .pi/settings.local.json; other projects still ask, and secret files still ask. Off: an outside directory can be allowed once or for this session.",
|
|
2896
3038
|
set(value) {
|
|
@@ -2939,7 +3081,7 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2939
3081
|
kind: "toggle",
|
|
2940
3082
|
key: "background-auto-fallback",
|
|
2941
3083
|
value: () => config.backgroundAutoFallback,
|
|
2942
|
-
name: () => "
|
|
3084
|
+
name: () => "Auto when background can't ask",
|
|
2943
3085
|
label: () => `Run auto when a background subagent can't ask: ${config.backgroundAutoFallback ? "on" : "off"}`,
|
|
2944
3086
|
summary: "Only for a background subagent that can't ask you. On: it runs in auto; dangerous commands, secrets and protected paths are still denied. Off: it runs in default, so its asks are denied.",
|
|
2945
3087
|
set(value, ctx) {
|
|
@@ -2990,9 +3132,8 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
2990
3132
|
key: "command-colors",
|
|
2991
3133
|
choices: PREVIEW_TEXT_COLORS,
|
|
2992
3134
|
value: () => effectiveCommandColors(config.commandColors, highlighterLoaded()),
|
|
2993
|
-
panelLabel: () => `Command colors in the dialog: ${commandColorsNote()}`,
|
|
2994
3135
|
unsaved: () => config.commandColors === undefined,
|
|
2995
|
-
name: () => "Command colors
|
|
3136
|
+
name: () => "Command colors",
|
|
2996
3137
|
label: () => `Command colors in the dialog: ${commandColorsNote()}`,
|
|
2997
3138
|
summary: "How the dialog colors long commands. shell-highlight colors command names and heredoc languages; it is the default when pi-shell-highlight is installed (pi install npm:pi-shell-highlight). builtin-syntax: pi's own highlighter. dim, muted, text: plain colors.",
|
|
2998
3139
|
set(value) {
|
|
@@ -3003,15 +3144,44 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
3003
3144
|
return undefined;
|
|
3004
3145
|
},
|
|
3005
3146
|
},
|
|
3147
|
+
{
|
|
3148
|
+
kind: "toggle",
|
|
3149
|
+
key: "mode-status",
|
|
3150
|
+
value: () => config.modeStatus,
|
|
3151
|
+
name: () => "Mode in the footer",
|
|
3152
|
+
label: () => `Mode in the footer: ${config.modeStatus ? "on" : "off"}`,
|
|
3153
|
+
summary: "Shows the current mode (default, auto, plan) in the footer status line. Off hides it; the mode still applies, and the mode line after a switch and Alt+M work as before.",
|
|
3154
|
+
set(value, ctx) {
|
|
3155
|
+
const problem = saveConfig(getAgentDir(), { modeStatus: value });
|
|
3156
|
+
if (problem !== undefined) return problem;
|
|
3157
|
+
config = { ...config, modeStatus: value };
|
|
3158
|
+
if (ctx.hasUI && childMode === undefined) refreshStatus(ctx); // the footer follows at once
|
|
3159
|
+
return undefined;
|
|
3160
|
+
},
|
|
3161
|
+
},
|
|
3162
|
+
{
|
|
3163
|
+
kind: "toggle",
|
|
3164
|
+
key: "mode-working-row",
|
|
3165
|
+
value: () => config.modeWorkingRow,
|
|
3166
|
+
name: () => "Mode in the working row",
|
|
3167
|
+
label: () => `Mode in the working row: ${config.modeWorkingRow ? "on" : "off"}`,
|
|
3168
|
+
summary: "While the agent works in plan or auto mode, the working row reads \"\u23f5\u23f5 auto \u00b7 Working\" so the mode is in front of you. Off by default: the footer already shows the mode. Default mode keeps pi's own text.",
|
|
3169
|
+
set(value, ctx) {
|
|
3170
|
+
const problem = saveConfig(getAgentDir(), { modeWorkingRow: value });
|
|
3171
|
+
if (problem !== undefined) return problem;
|
|
3172
|
+
config = { ...config, modeWorkingRow: value };
|
|
3173
|
+
syncWorkingMessage(ctx); // the working row follows at once
|
|
3174
|
+
return undefined;
|
|
3175
|
+
},
|
|
3176
|
+
},
|
|
3006
3177
|
{
|
|
3007
3178
|
kind: "choice",
|
|
3008
3179
|
key: "mode-flash",
|
|
3009
3180
|
choices: MODE_FLASH_CHOICES,
|
|
3010
3181
|
value: () => modeFlashChoice(config.modeFlashSecs),
|
|
3011
|
-
panelLabel: () => `Mode line after a switch: ${modeFlashNote()}`,
|
|
3012
3182
|
name: () => "Mode line after a switch",
|
|
3013
3183
|
label: () => `Mode line after a switch: ${modeFlashNote()}`,
|
|
3014
|
-
summary: "Shows the mode, in its color: above the prompt for this long after a switch while idle (1s, 2s
|
|
3184
|
+
summary: "Shows the mode, in its color: above the prompt for this long after a switch while idle (1s, 2s, 3s or 5s). For another length between 0.1 and 10 seconds, set modeFlashSecs in the file.",
|
|
3015
3185
|
set(value, ctx) {
|
|
3016
3186
|
const secs = value === "off" ? 0 : Number.parseFloat(value);
|
|
3017
3187
|
const problem = saveConfig(getAgentDir(), { modeFlashSecs: secs });
|
|
@@ -3029,37 +3199,48 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
3029
3199
|
* exactly one section, in the order listed here; the headless listing and the
|
|
3030
3200
|
* completions stay flat (and keep the array's order).
|
|
3031
3201
|
*/
|
|
3032
|
-
function flowSections(): { id: string; label: string; keys: readonly string[] }[] {
|
|
3202
|
+
function flowSections(): { id: string; label: string; groups: { label: string; keys: readonly string[] }[] }[] {
|
|
3033
3203
|
return [
|
|
3034
|
-
{
|
|
3204
|
+
{
|
|
3205
|
+
id: "settings",
|
|
3206
|
+
label: "Settings",
|
|
3207
|
+
groups: [
|
|
3208
|
+
{ label: "Project approvals", keys: ["project-command-approvals", "project-read-approvals", "project-exact-commands"] },
|
|
3209
|
+
{ label: "Dialog & mode line", keys: ["command-colors", "mode-flash", "mode-status", "mode-working-row"] },
|
|
3210
|
+
],
|
|
3211
|
+
},
|
|
3035
3212
|
{
|
|
3036
3213
|
id: "subagents",
|
|
3037
3214
|
label: "Subagents",
|
|
3038
|
-
keys: ["subagents-follow-auto", "background-asks", "background-auto-fallback", "subagent-thinking", "background-ask-popup", "background-ask-timeout"],
|
|
3215
|
+
groups: [{ label: "", keys: ["subagents-follow-auto", "background-asks", "background-auto-fallback", "subagent-thinking", "background-ask-popup", "background-ask-timeout"] }],
|
|
3039
3216
|
},
|
|
3040
|
-
{ id: "dialog", label: "Dialog", keys: ["command-colors", "mode-flash"] },
|
|
3041
3217
|
];
|
|
3042
3218
|
}
|
|
3043
3219
|
|
|
3044
3220
|
/** The panel's sections, with each row's current value and help text. */
|
|
3045
|
-
function flowPanelSections():
|
|
3221
|
+
function flowPanelSections(): PanelSection[] {
|
|
3046
3222
|
const settings = flowSettings();
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
label: section.label,
|
|
3050
|
-
items: section.keys.flatMap((key): PanelItem[] => {
|
|
3223
|
+
const rows = (keys: readonly string[]): PanelItem[] =>
|
|
3224
|
+
keys.flatMap((key): PanelItem[] => {
|
|
3051
3225
|
const setting = settings.find((s) => s.key === key);
|
|
3052
3226
|
if (setting === undefined) return [];
|
|
3053
3227
|
const values = setting.kind === "toggle" ? ["on", "off"] : setting.kind === "choice" ? [...setting.choices] : undefined;
|
|
3054
3228
|
return [
|
|
3055
3229
|
{
|
|
3056
3230
|
id: setting.key,
|
|
3057
|
-
label: setting.
|
|
3058
|
-
|
|
3231
|
+
label: setting.name(),
|
|
3232
|
+
// the colors row carries a note about the value in effect; the value column has room for the name only
|
|
3233
|
+
description: setting.key === "command-colors" ? `${setting.summary} Now: ${commandColorsNote()}.` : setting.summary,
|
|
3059
3234
|
...(values === undefined ? { placeholder: `e.g. ${(setting as { presets: readonly string[] }).presets.slice(0, 3).join(", ")}` } : { values }),
|
|
3060
3235
|
},
|
|
3061
3236
|
];
|
|
3062
|
-
})
|
|
3237
|
+
});
|
|
3238
|
+
return flowSections().map((section) => ({
|
|
3239
|
+
id: section.id,
|
|
3240
|
+
label: section.label,
|
|
3241
|
+
items: section.groups.flatMap((group) => rows(group.keys)),
|
|
3242
|
+
// a single unnamed group needs no heading
|
|
3243
|
+
...(section.groups.length > 1 || section.groups.some((g) => g.label !== "") ? { groups: section.groups.map((g) => ({ label: g.label, items: rows(g.keys) })) } : {}),
|
|
3063
3244
|
}));
|
|
3064
3245
|
}
|
|
3065
3246
|
|
|
@@ -3378,7 +3559,12 @@ export default function keeperExtension(pi: ExtensionAPI, { childMode, askServer
|
|
|
3378
3559
|
rulebookReviewKey = undefined;
|
|
3379
3560
|
rulebookIgnoredKey = undefined;
|
|
3380
3561
|
rulebookWarnedKey = undefined;
|
|
3562
|
+
rulebookApproved.clear();
|
|
3563
|
+
ignoredKey = undefined;
|
|
3564
|
+
approvedKey = undefined;
|
|
3565
|
+
warnedKey = undefined;
|
|
3381
3566
|
if (childMode === undefined) {
|
|
3567
|
+
ensureUserRuleDir(getAgentDir()); // an updated install gets the rules folder and the example too
|
|
3382
3568
|
// session grants end with the session. The guard is defensive: a child's grants come
|
|
3383
3569
|
// from the parent through the relay, and a child holds none of its own to clear
|
|
3384
3570
|
grants.reset();
|