@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
156 lines • 8.74 kB
JavaScript
/**
* The session workspace's project trust and project-scope settings over RPC (senpi#6176).
*
* Trust: the choices are exactly the ones `/trust` offers (`getProjectTrustOptions` without the session-only
* entries), and choosing one saves exactly what that choice saves in `trust.json`. It is never implied: only an
* explicit `set_project_trust` writes, and a launch that named the trust (`--approve`/`--no-approve`) refuses it, because
* that flag outranks every saved decision. Then, in the order `resolveProjectTrusted` decides a session's trust, a folder
* with nothing trust gates stays trusted, and a `project_trust` handler of the extensions loaded before trust (never a
* project extension) decides before any saved choice: a choice it decides against is refused, with nothing saved and
* nothing applied. Otherwise the session takes the decision the way a Full access change does
* (`applyProjectTrustChange`), in line with the runtime's own trust changes: project settings, skills, prompts,
* extensions and MCP servers load or unload at once.
*
* `no_inherit` is the one choice `set_project_trust` takes that `get_project_trust` never lists, because it is for
* clients, not people (a desktop pull-request worktree of another repository): it saves an explicit no-decision entry,
* so the folder inherits no decision from above it or from its main checkout and is decided as if nothing were saved.
* It never adds trust to the session it is set in: a trust that came only from Full access stays (and leaving Full
* access still withdraws it), any other trust goes unless a `project_trust` handler decides the folder is trusted.
*
* Settings: the project settings file is read as stored (reading runs nothing, so trust does not gate it) and written
* one top-level key at a time through `SettingsManager`'s own project write path, which refuses an untrusted project.
*/
import { createHeadlessProjectTrustContext } from "../../cli/project-trust-headless.js";
import { emitProjectTrustEvent } from "../../core/extensions/runner.js";
import { getSettingsPath } from "../../core/settings-manager.js";
import { getProjectTrustNoInheritOption, getProjectTrustOptions, ProjectTrustStore, } from "../../core/trust-manager.js";
import { hasTrustRequiringProjectResourcesAsync } from "../../core/trust-requiring-resources.js";
import { RPC_ERROR_INVALID_ARGUMENT, RPC_ERROR_PROJECT_NOT_TRUSTED, RPC_ERROR_PROJECT_SETTINGS_UNREADABLE, RPC_ERROR_PROJECT_SETTINGS_WRITE_FAILED, RPC_ERROR_PROJECT_TRUST_EXTENSION_DECIDED, RPC_ERROR_PROJECT_TRUST_LOCKED, } from "./rpc-error-types.js";
/** A top-level settings key: an identifier, so no prototype key or path ever reaches the file. */
const SETTINGS_KEY = /^[A-Za-z][A-Za-z0-9_]*$/;
function choiceOf(option) {
if (!option.trusted)
return "do_not_trust";
return option.updates.length > 1 ? "trust_parent" : "trust";
}
function trustChoices(cwd) {
return getProjectTrustOptions(cwd).map((option) => ({ choice: choiceOf(option), option }));
}
function settableTrustChoices(cwd) {
return [...trustChoices(cwd), { choice: "no_inherit", option: getProjectTrustNoInheritOption(cwd) }];
}
export async function readProjectTrust(session) {
const cwd = session.sessionManager.getCwd();
const entry = new ProjectTrustStore(session.agentDir).getEntry(cwd);
return {
cwd,
projectTrusted: session.settingsManager.isProjectTrusted(),
requiresTrust: await hasTrustRequiringProjectResourcesAsync(cwd),
saved: entry === null ? null : { path: entry.path, trusted: entry.decision },
locked: session.settingsManager.isProjectTrustLocked(),
choices: trustChoices(cwd).map(({ choice, option }) => ({
choice,
label: option.label,
trusted: option.trusted,
path: option.savedPath ?? cwd,
})),
};
}
/**
* What a `project_trust` handler decides for `cwd`, asked as a session open asks it: the handlers of the extensions
* loaded before trust (`loadProjectTrustExtensions` leaves project extensions out), first decided answer wins, no UI.
* A handler that throws falls through, as in `resolveProjectTrusted`, and is reported on stderr like its warning.
*/
async function extensionTrustDecision(session, cwd) {
const loaded = session.resourceLoader.getExtensions();
const preTrust = { ...loaded, extensions: loaded.extensions.filter((ext) => ext.sourceInfo.scope !== "project") };
const { result, errors } = await emitProjectTrustEvent(preTrust, { type: "project_trust", cwd }, createHeadlessProjectTrustContext({ cwd, mode: "rpc", hasUI: false }));
for (const error of errors) {
process.stderr.write(`senpi rpc: Extension "${error.extensionPath}" project_trust error: ${error.error}\n`);
}
return result === undefined ? undefined : result.trusted === "yes";
}
export async function writeProjectTrust(session, choice, inTrustChangeOrder) {
const cwd = session.sessionManager.getCwd();
const selected = settableTrustChoices(cwd).find((entry) => entry.choice === choice);
if (selected === undefined) {
return {
ok: false,
message: `set_project_trust requires 'choice' to be one of: ${settableTrustChoices(cwd)
.map((entry) => entry.choice)
.join(", ")}.`,
errorCode: RPC_ERROR_INVALID_ARGUMENT,
};
}
const settings = session.settingsManager;
if (settings.isProjectTrustLocked()) {
return {
ok: false,
message: "This engine was launched with an explicit project trust flag; a saved decision cannot change it.",
errorCode: RPC_ERROR_PROJECT_TRUST_LOCKED,
};
}
return inTrustChangeOrder(async () => {
// As resolveProjectTrusted: a folder with nothing trust gates is trusted whatever is saved or decided.
const requiresTrust = await hasTrustRequiringProjectResourcesAsync(cwd);
const decided = requiresTrust ? await extensionTrustDecision(session, cwd) : undefined;
const noInherit = selected.choice === "no_inherit";
// no_inherit saves no decision, so a handler's decision does not contradict it: the handler keeps deciding.
if (!noInherit && decided !== undefined && decided !== selected.option.trusted) {
return {
ok: false,
message: `A project_trust extension handler decides this folder is ${decided ? "trusted" : "untrusted"}; nothing was saved.`,
errorCode: RPC_ERROR_PROJECT_TRUST_EXTENSION_DECIDED,
};
}
await new ProjectTrustStore(session.agentDir).setMany(selected.option.updates);
const trusted = !requiresTrust || (decided ?? (noInherit ? settings.isTrustFromFullAccess() : selected.option.trusted));
if (settings.isProjectTrusted() !== trusted) {
settings.setProjectTrusted(trusted);
await session.applyProjectTrustChange();
}
return { ok: true, data: await readProjectTrust(session) };
});
}
export function readProjectSettings(session) {
const cwd = session.sessionManager.getCwd();
const path = getSettingsPath(cwd, session.agentDir, "project");
return {
cwd,
projectTrusted: session.settingsManager.isProjectTrusted(),
...session.settingsManager.readProjectSettingsFile(path),
};
}
export async function writeProjectSetting(session, key, value) {
if (typeof key !== "string" || !SETTINGS_KEY.test(key) || value === undefined) {
return {
ok: false,
message: "set_project_setting requires an identifier 'key' and a JSON 'value' (null removes the key).",
errorCode: RPC_ERROR_INVALID_ARGUMENT,
};
}
const settings = session.settingsManager;
if (!settings.isProjectTrusted()) {
return {
ok: false,
message: "Project is not trusted; refusing to write project settings. Trust it with set_project_trust first.",
errorCode: RPC_ERROR_PROJECT_NOT_TRUSTED,
};
}
const file = readProjectSettings(session);
if (file.error !== null) {
return { ok: false, message: file.error, errorCode: RPC_ERROR_PROJECT_SETTINGS_UNREADABLE };
}
try {
await settings.setProjectSetting(key, value);
}
catch (error) {
return {
ok: false,
message: `Writing ${file.path} failed: ${error instanceof Error ? error.message : String(error)}`,
errorCode: RPC_ERROR_PROJECT_SETTINGS_WRITE_FAILED,
};
}
return { ok: true, data: readProjectSettings(session) };
}
//# sourceMappingURL=project-workspace-commands.js.map