@sema-agent/core
Version:
Stateless, task-oriented AI agent core
59 lines (58 loc) • 3.8 kB
JavaScript
import { anchorSpelling, canonicalizeTarget, pathFamilyOf, targetPathsOf } from "../tools/fs/safety.js";
import { requestReleasedRows, screenRequestReleasableSeat } from "../tools/fs/read-deny.js";
import { uuidv7 } from "../internal/harness.js";
import { deliverEngineNotice } from "./engine-notice.js";
import { brandPolicyAskClass } from "./ask-class.js";
import { pathTargetBaseOf } from "./effective-path-target.js";
import { chainHopsOrNone, symlinkChainHops } from "./path-identity.js";
import { isProtectedWrite, protectivePathTargetOf } from "./tool-registry.js";
export function releasedRowWriteQuestion(target, row) {
return `changing "${target}" needs a person's approval: this session's request released the built-in read deny row "${row}" so the file can be read, and a change to it is answered only by a person — no permission mode, classifier, stored rule or blanket approver answers it.`;
}
export function createReleasedReadRowWritePolicy(opts) {
if (opts.released.length === 0)
return undefined;
const released = (hit) => hit !== null && opts.released.includes(hit.pattern);
const family = (cwd) => pathFamilyOf({ root: opts.rootPath, cwd: cwd ?? opts.env.cwd });
const ask = (target, row) => ({ action: "ask", decisionReason: "mode", requiresRealApproval: true, message: releasedRowWriteQuestion(target, row) });
const policy = {
async check(req, signal) {
if (!isProtectedWrite(req))
return { action: "allow" };
const protective = protectivePathTargetOf(req);
for (const path of targetPathsOf(req, protective)) {
const fam = family(req.cwd);
const base = pathTargetBaseOf(protective, { root: opts.rootPath, cwd: req.cwd });
const spelled = base !== undefined ? anchorSpelling(fam, base, path) : path;
const canon = await canonicalizeTarget(opts.env, path, signal, fam, base).catch(() => undefined);
const chain = await symlinkChainHops(opts.env, path, signal, fam, base).catch(() => undefined);
const hops = chain === undefined ? [] : chainHopsOrNone(chain);
if (canon === undefined || !canon.ok) {
const met = [spelled, ...hops].map((v) => opts.readDeny.matchPath(v)).find(released);
if (met !== undefined)
return ask(path, met.pattern);
continue;
}
const hit = opts.readDeny.matchTarget(canon.key, spelled, { ...(canon.aliasResolved === true ? { aliasResolved: true } : {}), hops });
if (released(hit))
return ask(path, hit.pattern);
}
return { action: "allow" };
},
};
return brandPolicyAskClass(policy, "external_authority");
}
export function releasedReadRowWriteLayers(input) {
if (input.readDeny === undefined)
return [];
const released = requestReleasedRows(screenRequestReleasableSeat(input.deps.readDenyBuiltinRequestExcludable), input.spec.readDenyBuiltinExclude);
const policy = createReleasedReadRowWritePolicy({ env: input.env, released, readDeny: input.readDeny, rootPath: input.rootPath });
if (policy === undefined)
return [];
deliverEngineNotice(input.deps.onNotice, {
code: "config.read_deny_request_excluded",
message: `This session may read ${released.map((r) => JSON.stringify(r)).join(", ")} with the Read tool: its contents, including any keys in it, reach this conversation, and every change to the file through the edit tools waits for your approval (the shell still refuses it).`,
detail: { rows: [...released], sessionId: input.sessionId, occurrenceId: uuidv7() },
});
return [policy];
}