@sema-agent/core
Version:
Stateless, task-oriented AI agent core
96 lines (95 loc) • 4.18 kB
JavaScript
import { gitFrameContextVisible, normalizeGitAnnouncement } from "../../internal/harness.js";
import { mintSystemReminder } from "../reminder-mint.js";
import { sanitizeUntrustedText, SHELLED_BODY_ENVELOPE_TAGS } from "../untrusted-text.js";
import { branchCarriesVisiblePositiveGitFrame, newestEngineGitFrame } from "./git-status-frame.js";
import { asThrownError } from "../thrown-value.js";
export function wrapGitFrame(body, mark) {
return mintSystemReminder(sanitizeUntrustedText(body, SHELLED_BODY_ENVELOPE_TAGS), mark);
}
export async function resolveGitLegDelivery(prepared, cpMirror, report) {
const ref = prepared.gitStatusRef;
const frame = ref.frame;
if (frame === undefined)
return undefined;
let prior;
let pending = false;
try {
const branchReadout = await prepared.session.getGitAnnouncement?.();
if (branchReadout !== undefined) {
if (branchReadout.status === "pending") {
pending = true;
prior = { kind: branchReadout.kind, hash: branchReadout.hash };
}
else {
prior = { kind: branchReadout.kind, hash: branchReadout.hash, ...(branchReadout.entryId !== undefined ? { entryId: branchReadout.entryId } : {}) };
}
}
else {
const shaped = normalizeGitAnnouncement(cpMirror);
if (shaped !== undefined) {
if (shaped.pending === true || shaped.entryId === undefined) {
pending = true;
prior = { kind: shaped.kind, hash: shaped.hash };
}
else if (gitFrameContextVisible(await prepared.session.getBranch(), shaped.entryId)) {
prior = { kind: shaped.kind, hash: shaped.hash, entryId: shaped.entryId };
}
else {
pending = true;
prior = { kind: shaped.kind, hash: shaped.hash };
}
}
}
}
catch (err) {
pending = true;
report(asThrownError(err));
}
if (prior !== undefined && !pending && prior.entryId !== undefined) {
try {
const newest = newestEngineGitFrame(await prepared.session.getBranch(), prepared.reminderMark);
if (newest === undefined || newest.entryId !== prior.entryId)
pending = true;
}
catch (err) {
pending = true;
report(asThrownError(err));
}
}
if (prior !== undefined)
ref.announced = pending ? { ...prior, pending: true } : { ...prior };
const negative = frame.kind === "unavailable" || frame.kind === "non-repo";
if (negative) {
if (prior === undefined && !pending) {
let mustDisown;
try {
mustDisown = branchCarriesVisiblePositiveGitFrame(await prepared.session.getBranch(), prepared.reminderMark);
}
catch (err) {
mustDisown = true;
report(asThrownError(err));
}
return mustDisown ? frame.body : undefined;
}
if (prior !== undefined && !pending && prior.kind === frame.kind && prior.hash === frame.hash)
return undefined;
return frame.body;
}
if (!pending && prior !== undefined && prior.kind === frame.kind && prior.hash === frame.hash) {
ref.protectedText = wrapGitFrame(frame.body, prepared.reminderMark);
if (frame.kind === "full" && frame.shrunk !== undefined) {
ref.wrappedShrink = { find: ref.protectedText, replace: wrapGitFrame(frame.shrunk.body, prepared.reminderMark) };
}
return undefined;
}
return frame.body;
}
export function gitRestateOption(prepared) {
const ref = prepared.gitStatusRef;
if (ref.frame === undefined || ref.announced === undefined || ref.reassert === undefined)
return {};
const use = ref.overBudgetShrunk && ref.frame.shrunk !== undefined
? { kind: "degraded", hash: ref.frame.shrunk.hash }
: { kind: ref.frame.kind, hash: ref.frame.hash };
return { gitRestate: { pending: use, land: ref.reassert } };
}