@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
156 lines • 7.37 kB
JavaScript
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { createCursorCliOauthSandboxModeValidator } from "./settings.js";
/**
* Guardrails for unattended Cursor CLI execution.
*
* The Cursor CLI runs its own tools the moment `--force` is on the argv, so
* that decision is never implicit: it requires the explicit
* `noApprovalAcknowledgedAt` acknowledgement, plan mode never forces, and a
* disabled force merely warns. Command denylists ride along through the
* per-account HOME's `cli-config.json`, strictly in the probe-proven shape
* (task 2: `permissions.deny` with exact full-command `Shell(...)` entries).
*/
/** Sandbox modes the installed CLI accepts (task 2 probe: allowed choices are exactly these). */
export const CURSOR_CLI_OAUTH_SANDBOX_MODES = ["enabled", "disabled"];
export const CURSOR_CLI_OAUTH_NO_APPROVAL_EXPLANATION = "The Cursor CLI executes its own tools autonomously: with --force there is no senpi approval, no senpi sandboxing, and no tool-level audit for what it runs.";
export const CURSOR_CLI_OAUTH_ACKNOWLEDGEMENT_STEP = 'Set "cursorCliOauthProvider.noApprovalAcknowledgedAt" to the current ISO-8601 timestamp (for example "2026-08-17T12:00:00.000Z") in senpi settings to acknowledge this once.';
/** One warning per session when force is disabled in agent mode (contracts block, task 18). */
export const CURSOR_CLI_OAUTH_FORCE_DISABLED_WARNING = 'cursor-cli-oauth: forceExecution is disabled in agent mode, so the Cursor CLI will auto-reject every tool call; set executionMode to "plan" for planning turns that do not need execution.';
export function createCursorCliGuardrailSession() {
const warnings = [];
const emitted = new Set();
const warn = (code, message, key = code) => {
if (emitted.has(key))
return false;
emitted.add(key);
warnings.push({ kind: "warning", code, message });
return true;
};
const validateSandboxMode = createCursorCliOauthSandboxModeValidator(new Set(CURSOR_CLI_OAUTH_SANDBOX_MODES), (message) => {
warn("sandbox_mode_ignored", message, `sandbox_mode_ignored:${message}`);
});
return {
warnings,
warn,
validateSandboxMode,
};
}
/** Typed refusal for an unacknowledged force attempt; never confused with a warning. */
export class CursorCliExecutionRefusalError extends Error {
constructor(refusal) {
super(refusal.message);
this.code = "no_approval_acknowledgement_required";
this.name = "CursorCliExecutionRefusalError";
this.refusal = refusal;
}
}
/** Glob breadth is unproven (task 2 probed exact full commands only), so glob-bearing entries are dropped. */
const GLOB_METACHARACTERS = /[*?[]/u;
export function sanitizeCursorCliDenyCommands(entries, warn) {
const kept = [];
for (const entry of entries) {
const command = entry.trim();
if (command.length === 0) {
warn?.("Ignoring an empty Cursor CLI deny command entry (exact full commands only)");
continue;
}
if (GLOB_METACHARACTERS.test(command)) {
warn?.(`Ignoring glob-bearing Cursor CLI deny command (exact full commands only; glob support is unproven): ${command}`);
continue;
}
kept.push(command);
}
return kept;
}
/**
* Decide whether a turn may run unattended.
*
* - `--force` is emitted only in agent mode, only when force is requested, and
* only after `noApprovalAcknowledgedAt` is set; otherwise the decision is a
* typed refusal naming the acknowledgement step.
* - Plan mode never emits `--force` (the CLI only plans), so it needs no
* acknowledgement.
* - Force disabled in agent mode is allowed but warns once per session.
* - Unproven sandbox modes are ignored with one warning per distinct value.
*/
/**
* True while the settings guarantee an unattended agent-mode refusal: force is
* requested outside plan mode but `noApprovalAcknowledgedAt` was never set.
* Shared by the execution policy and the fallback-eligibility hook so the two
* can never disagree about when the lane refuses.
*/
export function cursorCliForceRefusalPending(input) {
return input.executionMode !== "plan" && input.forceExecution && input.noApprovalAcknowledgedAt === undefined;
}
export function resolveCursorCliExecutionPolicy(input, session, denyCommands = []) {
const planMode = input.executionMode === "plan";
const wantsForce = !planMode && input.forceExecution;
if (cursorCliForceRefusalPending(input)) {
const refusal = {
kind: "refusal",
code: "no_approval_acknowledgement_required",
message: `${CURSOR_CLI_OAUTH_NO_APPROVAL_EXPLANATION} ${CURSOR_CLI_OAUTH_ACKNOWLEDGEMENT_STEP}`,
acknowledgementStep: CURSOR_CLI_OAUTH_ACKNOWLEDGEMENT_STEP,
};
return { status: "refused", refusal };
}
if (!planMode && !input.forceExecution) {
session.warn("force_execution_disabled", CURSOR_CLI_OAUTH_FORCE_DISABLED_WARNING);
}
const sandboxMode = session.validateSandboxMode(input.sandboxMode);
const sanitizedDenyCommands = sanitizeCursorCliDenyCommands(denyCommands, (message) => {
session.warn("deny_command_rejected", message, `deny_command_rejected:${message}`);
});
return {
status: "allowed",
force: wantsForce,
executionMode: input.executionMode,
sandboxMode,
denyCommands: sanitizedDenyCommands,
warnings: [...session.warnings],
};
}
const CLI_CONFIG_RELATIVE_PATH = join(".cursor", "cli-config.json");
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function shellDenyEntry(command) {
return `Shell(${command})`;
}
/**
* Pre-spawn hook: (re-)apply the deny list to the sandbox HOME's
* `cli-config.json`.
*
* The CLI rewrites this file during every invocation (task 2 observed its own
* keys appearing and `cli-config.json.bad` sidecars for malformed content), so
* this must run immediately before each spawn, composing with the home-store
* prepare pass that re-writes `auth.json` on the same cadence. Writes only the
* probe-proven shape (`permissions.deny` with exact `Shell(<full command>)`
* entries) and preserves every CLI-owned key already present. Writes nothing
* when no deny commands are configured.
*/
export function applyCursorCliDenyConfig(home, denyCommands) {
if (denyCommands.length === 0)
return;
const configPath = join(home, CLI_CONFIG_RELATIVE_PATH);
let existing = {};
if (existsSync(configPath)) {
try {
const parsed = JSON.parse(readFileSync(configPath, "utf8"));
if (isRecord(parsed))
existing = parsed;
}
catch {
// The CLI quarantines unparseable config as cli-config.json.bad and
// proceeds with defaults; start from the proven minimal shape.
}
}
const permissions = isRecord(existing.permissions) ? { ...existing.permissions } : {};
const config = {
...existing,
permissions: { ...permissions, deny: denyCommands.map(shellDenyEntry) },
};
writeFileSync(configPath, `${JSON.stringify(config, null, "\t")}\n`, "utf8");
}
//# sourceMappingURL=guardrails.js.map