@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
89 lines • 4.1 kB
JavaScript
import { AsyncLocalStorage } from "node:async_hooks";
import { hasFunctions, realmRegistry } from "@earendil-works/pi-realm-registry";
const inputMarks = new WeakMap();
const currentOrigins = new WeakMap();
const continuingTurns = new WeakSet();
const recordScope = realmRegistry("@code-yeongyu/senpi/turn-origin/v1", () => {
const scopes = new AsyncLocalStorage();
const delegated = () => {
const scope = scopes.getStore();
return scope?.open === true && scope.origin === "delegated";
};
return {
// Any origin other than "local" (one a newer copy defines) is treated as delegated: it fails closed.
run: (origin, handle) => scopes.run({ origin: origin === "local" ? "local" : "delegated", open: true }, handle),
delegated,
closeAtRunStart: () => {
const opened = delegated();
const scope = scopes.getStore();
if (scope !== undefined)
scope.open = false;
return opened;
},
};
}, (value) => hasFunctions(value, ["run", "delegated", "closeAtRunStart"]));
/** The mark an input gets where it is created: explicit for client records, `local` for the TUI, else `engine`. */
export function inputTurnMark(input) {
if (input.mode === "steer")
return "join";
if (input.connectionOrigin !== undefined)
return input.connectionOrigin;
if (recordIsDelegated())
return "delegated";
return input.source === undefined || input.source === "interactive" ? "local" : "engine";
}
export function markInputTurnOrigin(message, mark, personStarted = false) {
inputMarks.set(message, { origin: mark, personStarted });
}
/** Carries `from`'s origin mark onto `to`, a copy of the same input: a copy without it would open or join a turn unmarked. */
export function copyInputTurnMark(from, to) {
const mark = inputMarks.get(from);
if (mark !== undefined)
inputMarks.set(to, mark);
}
/** Whether a message carries an origin mark: a user message always opens a turn's origin, any other only when marked. */
export function hasInputTurnMark(message) {
return inputMarks.has(message);
}
/** Runs `handle`, and every async task it starts, as the record `origin` describes. */
export function runInRecordScope(origin, handle) {
return recordScope.run(origin, handle);
}
/** Whether the work being created belongs to an open cross-account record's own handling. */
export function recordIsDelegated() {
return recordScope.delegated() === true;
}
/**
* A run starts as engine work, or as delegated work when a cross-account record's own handling starts it; a retry keeps
* the turn it continues. The user message that opens a turn sets its origin, a steer keeps it.
*/
export function noteRunStart(sessionManager) {
if (continuingTurns.delete(sessionManager))
return;
currentOrigins.set(sessionManager, { origin: recordScope.closeAtRunStart() === true ? "delegated" : "engine" });
}
/** The next retry or recovery run repeats this turn; its start consumes the continuation mark. */
export function noteTurnContinuation(sessionManager) {
continuingTurns.add(sessionManager);
}
export function endTurnContinuation(sessionManager) {
continuingTurns.delete(sessionManager);
}
export function noteUserMessageStart(sessionManager, message) {
const mark = inputMarks.get(message);
if (mark?.origin === "join")
return;
currentOrigins.set(sessionManager, {
origin: mark?.origin ?? "engine",
personTurn: mark?.personStarted ? {} : undefined,
});
}
/** A person's admitted message owns this identity; only an explicit continuation preserves it across run starts. */
export function currentPersonTurn(sessionManager) {
return currentOrigins.get(sessionManager)?.personTurn;
}
/** Whether the deciding turn counts as delegated-origin: a cross-account device started this turn or this run. */
export function decidingTurnIsDelegated(sessionManager) {
return currentOrigins.get(sessionManager)?.origin === "delegated";
}
//# sourceMappingURL=turn-origin.js.map