UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

95 lines (94 loc) • 5.73 kB
import { deliverEngineNotice } from "./engine-notice.js"; import { entriesNotAppliedDetail } from "./entries-not-applied.js"; import { announceDigest } from "./notice-fact.js"; export const SHARED_ARTIFACT_READS_SETTINGS = Object.freeze(["ask", "allow"]); export const SETTING_NOT_APPLIED_REASONS = Object.freeze(["unrecognized", "layer_not_read", "organization_decides"]); const ENTRY_SHOWN = 80; const REPO_SHOWN = 16; function quoted(v) { let s; try { s = typeof v === "string" ? v : (JSON.stringify(v) ?? String(v)); } catch { s = "(a value that cannot be shown)"; } return s.length > ENTRY_SHOWN ? `${s.slice(0, ENTRY_SHOWN)}…` : s; } function ownValue(layers, key) { return layers !== undefined && layers !== null && Object.prototype.hasOwnProperty.call(layers, key) ? layers[key] : undefined; } function readLayer(v) { if (v === undefined || v === null) return { kind: "unset" }; return typeof v === "string" && SHARED_ARTIFACT_READS_SETTINGS.includes(v) ? { kind: "word", word: v } : { kind: "unrecognized", raw: v }; } export function resolveSharedArtifactReads(layers) { const managed = readLayer(ownValue(layers, "managed")); const user = readLayer(ownValue(layers, "user")); const deciding = managed.kind !== "unset" ? "managed" : user.kind !== "unset" ? "user" : undefined; const decider = deciding === "managed" ? managed : user; const resolved = deciding === undefined ? { value: "ask", decidedBy: "default" } : decider.kind === "word" ? (decider.word === "allow" ? { value: "allow", decidedBy: deciding } : { value: "ask", decidedBy: deciding }) : { value: "ask", decidedBy: "unreadable" }; const notApplied = []; if (managed.kind === "unrecognized") notApplied.push({ layer: "managed", entry: quoted(managed.raw), reason: "unrecognized" }); if (user.kind === "unrecognized") notApplied.push({ layer: "user", entry: quoted(user.raw), reason: "unrecognized" }); else if (user.kind === "word" && deciding === "managed" && user.word !== resolved.value) notApplied.push({ layer: "user", entry: user.word, reason: "organization_decides" }); const repo = ownValue(layers, "repo"); const repoValues = []; let repoNotLookedAt = 0; if (Array.isArray(repo)) { const len = repo.length; const n = typeof len === "number" && Number.isSafeInteger(len) && len > 0 ? len : 0; for (let i = 0; i < Math.min(n, REPO_SHOWN); i++) if (Object.prototype.hasOwnProperty.call(repo, i)) repoValues.push(repo[i]); repoNotLookedAt = Math.max(0, n - REPO_SHOWN); } else if (repo !== undefined && repo !== null) repoValues.push(repo); for (const v of repoValues) if (v !== undefined && v !== null) notApplied.push({ layer: "repo", entry: quoted(v), reason: "layer_not_read" }); return { ...resolved, notApplied, ...(repoNotLookedAt > 0 ? { repoNotLookedAt } : {}) }; } export function sharedArtifactReadsLayersOf(seat) { return typeof seat === "function" ? seat() : seat; } const LANE = "config.entries_not_applied:setting_not_applied"; const WHY = { unrecognized: "it is not one of the setting's words (ask, allow)", layer_not_read: "a project's or a checkout's local settings are not read for this setting (only the user's own settings and the organization's are)", organization_decides: "the organization's settings state a value for this setting, and theirs decides", }; export function sharedArtifactReadsNotice(r, ids) { const unseen = r.repoNotLookedAt ?? 0; if (r.notApplied.length === 0 && unseen === 0) return undefined; const clauses = r.notApplied.map((e) => `the ${e.layer === "repo" ? "project" : e.layer === "managed" ? "organization" : "user"} value ${JSON.stringify(e.entry)} did not apply: ${WHY[e.reason]}`); if (unseen > 0) clauses.push(`the project settings list has ${unseen} more place${unseen === 1 ? "" : "s"} past the first ${REPO_SHOWN}, not looked at (project settings have no vote for this setting)`); const entries = r.notApplied.map((e) => ({ entry: e.entry, setting: "sharedArtifactReads", layer: e.layer, reason: e.reason })); return { code: "config.entries_not_applied", message: `The sharedArtifactReads setting: ${clauses.join("; ")}. ` + `In force: ${r.value === "allow" ? "allow (reading an artifact another person shared does not ask)" : "ask (reading an artifact another person shared is approved as without the setting)"}. ` + `${r.notApplied.length > 0 ? "Correct or remove the entry" : "Project values have no vote for this setting; remove any there"}. Said once per conversation for this set of entries.`, detail: { ...entriesNotAppliedDetail("setting_not_applied", entries), inForce: r.value, decidedBy: r.decidedBy, ...(unseen > 0 ? { repoNotLookedAt: unseen } : {}), ...ids }, sessionId: ids.sessionId, }; } export function announceSharedArtifactReads(r, input) { const { address } = input.legNotice.route; const notice = sharedArtifactReadsNotice(r, { ...address, ...(input.runId !== undefined ? { runId: input.runId } : {}) }); if (notice === undefined) return; const deliver = () => deliverEngineNotice(input.onNotice, notice, input.legNotice.admission); if (input.oncePerSession === undefined) return deliver(); const identity = notice.detail.identity; const key = (r.repoNotLookedAt !== undefined ? `${identity}:${r.repoNotLookedAt}` : identity) + (address.sourceSessionId !== undefined ? `\n\n${address.sourceSessionId}` : ""); input.oncePerSession(announceDigest(LANE, key), deliver); }