@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
597 lines • 26.7 kB
JavaScript
import { calculateCost, createAssistantMessageDiagnostic, createAssistantMessageEventStream, } from "@earendil-works/pi-ai";
import { getAgentDir } from "../../../../config.js";
import { AuthStorage } from "../../../auth-storage.js";
import { estimateTokens } from "../../../compaction/compaction.js";
import { emptyCredential, listAccounts } from "./accounts.js";
import { DEFAULT_CURSOR_AFFINITY_KEY } from "./affinity.js";
import { defaultCursorAgentExecutableDeps, resolveCursorAgentExecutable, } from "./executable.js";
import { runCursorCliFailover } from "./failover.js";
import { applyCursorCliDenyConfig, CursorCliExecutionRefusalError, createCursorCliGuardrailSession, resolveCursorCliExecutionPolicy, } from "./guardrails.js";
import { runInCursorAccountHome } from "./home-store.js";
import { cursorCliDiagnostic } from "./provider-diagnostic.js";
import { CURSOR_CLI_OAUTH_PROVIDER_ID, createCursorCliOauthConfig, cursorAgentNotInstalledError, isCursorCliOauthLaneEnabled, } from "./oauth-login.js";
import { cursorCliSessionRouter, } from "./session-router.js";
import { loadCursorCliOauthProviderSettingsFromDisk } from "./settings.js";
import { resolveCursorCliSpawnModel } from "./spawn-model.js";
import { CursorCliAbortError, spawnCursorCli } from "./transport.js";
export { CURSOR_CLI_OAUTH_PROVIDER_ID } from "./oauth-login.js";
const DISABLED_MESSAGE = "disabled by settings";
const NO_ACCOUNTS_MESSAGE = "no accounts: run /login cursor-cli-oauth";
const RECENT_EXCHANGE_LIMIT = 12;
/**
* A non-zero cursor-agent exit that produced no result event. Carries the
* captured stderr so `classifyCursorCliError` can match the real cause instead
* of the generic "no assistant text" wrapper.
*/
class CursorCliProcessFailureError extends Error {
constructor(exitCode, stderr) {
super(stderr.trim().length > 0 ? stderr.trim() : `cursor-agent exited with code ${String(exitCode)}`);
this.name = "CursorCliProcessFailureError";
this.exitCode = exitCode;
this.stderr = stderr;
}
}
/** Minimal async queue bridging the home-store-wrapped transport run into a pullable iterable. */
class CursorCliEventBuffer {
constructor() {
this.values = [];
this.waiters = [];
this.ended = false;
this.settled = false;
}
wake(error) {
const waiter = this.waiters.shift();
if (waiter)
waiter(error);
}
push(value) {
if (this.ended || this.failure !== undefined)
return;
this.values.push(value);
this.wake();
}
close() {
if (this.settled)
return;
this.settled = true;
this.ended = true;
for (const waiter of this.waiters.splice(0))
waiter();
}
fail(error) {
if (this.settled)
return;
this.settled = true;
this.failure = error;
for (const waiter of this.waiters.splice(0))
waiter(error);
}
async *events() {
while (true) {
const value = this.values.shift();
if (value !== undefined) {
yield value;
continue;
}
if (this.failure !== undefined)
throw this.failure;
if (this.ended)
return;
await new Promise((resolve, reject) => {
this.waiters.push((error) => {
if (error !== undefined)
reject(error);
else
resolve();
});
});
}
}
}
function emptyCursorCliOutput(model) {
return {
role: "assistant",
content: [],
api: model.api,
provider: model.provider,
model: model.id,
usage: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason: "stop",
timestamp: Date.now(),
};
}
function ensureStarted(mapper) {
if (mapper.started)
return;
mapper.started = true;
mapper.stream.push({ type: "start", partial: mapper.output });
}
function openBlock(mapper, kind) {
ensureStarted(mapper);
if (kind === "thinking") {
mapper.output.content.push({ type: "thinking", thinking: "" });
}
else {
mapper.output.content.push({ type: "text", text: "" });
}
mapper.openKind = kind;
mapper.openIndex = mapper.output.content.length - 1;
mapper.openText = "";
mapper.textAccumulated = "";
mapper.stream.push({
type: kind === "thinking" ? "thinking_start" : "text_start",
contentIndex: mapper.openIndex,
partial: mapper.output,
});
}
function closeOpen(mapper) {
if (mapper.openKind === undefined)
return;
const block = mapper.output.content[mapper.openIndex];
if (mapper.openKind === "thinking") {
if (block !== undefined && block.type === "thinking")
block.thinking = mapper.openText;
mapper.stream.push({
type: "thinking_end",
contentIndex: mapper.openIndex,
content: mapper.openText,
partial: mapper.output,
});
}
else {
if (block !== undefined && block.type === "text")
block.text = mapper.openText;
mapper.stream.push({
type: "text_end",
contentIndex: mapper.openIndex,
content: mapper.openText,
partial: mapper.output,
});
}
mapper.openKind = undefined;
}
function ensureOpen(mapper, kind) {
if (mapper.openKind === kind)
return;
closeOpen(mapper);
openBlock(mapper, kind);
}
function resetTextSegment(mapper) {
closeOpen(mapper);
mapper.textAccumulated = "";
}
function pushTextDelta(mapper, delta) {
const block = mapper.output.content[mapper.openIndex];
mapper.openText += delta;
if (block !== undefined && block.type === "text")
block.text = mapper.openText;
mapper.stream.push({ type: "text_delta", contentIndex: mapper.openIndex, delta, partial: mapper.output });
}
function pushThinkingDelta(mapper, delta) {
const block = mapper.output.content[mapper.openIndex];
mapper.openText += delta;
if (block !== undefined && block.type === "thinking")
block.thinking = mapper.openText;
mapper.stream.push({
type: "thinking_delta",
contentIndex: mapper.openIndex,
delta,
partial: mapper.output,
});
}
/**
* Appends one assistant fragment as a text delta. The CLI streams partial
* output deltas and then repeats the full message in a final frame, so a
* fragment that already starts with the accumulated text is the cumulative
* snapshot: replace instead of appending so the answer is never duplicated.
*/
function appendAssistantFragment(mapper, fragment) {
if (fragment.length === 0)
return;
ensureOpen(mapper, "text");
let delta;
if (fragment.length >= mapper.textAccumulated.length &&
(mapper.textAccumulated.length === 0 || fragment.startsWith(mapper.textAccumulated))) {
delta = fragment.slice(mapper.textAccumulated.length);
mapper.textAccumulated = fragment;
}
else {
delta = fragment;
mapper.textAccumulated += fragment;
}
if (delta.length > 0)
pushTextDelta(mapper, delta);
}
function appendNotice(mapper, message) {
ensureOpen(mapper, "text");
pushTextDelta(mapper, `${message}\n`);
}
/**
* Records the result event: senpi's own token estimate of the payload it sent,
* the CLI's output tokens, and the CLI's input/cache numbers quarantined in a
* telemetry diagnostic. estimateContextTokens treats the last assistant usage
* as the authoritative context base, so CLI-side context numbers must never
* reach `usage` or senpi would demand a compaction only the CLI could perform.
*/
function applyResultEvent(mapper, model, sentPromptTokens, usage, requestId, durationMs) {
const output = mapper.output;
output.usage.input = sentPromptTokens;
output.usage.output = usage.outputTokens;
output.usage.cacheRead = 0;
output.usage.cacheWrite = 0;
output.usage.totalTokens = 0;
output.usage.cost = calculateCost(model, output.usage);
output.stopReason = "stop";
output.diagnostics = [
...(output.diagnostics ?? []),
createAssistantMessageDiagnostic("cursor_cli_oauth_cli_usage", "cursor-agent reported usage (telemetry only; never used for senpi context accounting)", {
inputTokens: usage.inputTokens,
outputTokens: usage.outputTokens,
cacheReadTokens: usage.cacheReadTokens,
cacheWriteTokens: usage.cacheWriteTokens,
requestId,
durationMs,
}),
];
}
function blockText(blocks) {
const parts = [];
for (const block of blocks) {
if (typeof block !== "object" || block === null)
continue;
const candidate = block;
if (candidate.type === "text" && typeof candidate.text === "string" && candidate.text.length > 0) {
parts.push(candidate.text);
}
}
return parts.join("\n");
}
function userText(message) {
return typeof message.content === "string" ? message.content : blockText(message.content);
}
/**
* The prompt for this turn: every user-role message after the last non-user message, in order.
* A turn can carry more than the request - hidden extension messages such as the first-turn plan
* reminder reach providers as user messages after it - and the CLI takes a single prompt, so
* sending only the last one dropped the request itself (senpi#2139).
*/
export function turnPrompt(context) {
const trailing = [];
for (let index = context.messages.length - 1; index >= 0; index -= 1) {
const message = context.messages[index];
if (message === undefined || message.role !== "user")
break;
const text = userText(message);
if (text.length > 0)
trailing.unshift(text);
}
if (trailing.length > 0)
return trailing.join("\n\n");
for (let index = context.messages.length - 1; index >= 0; index -= 1) {
const message = context.messages[index];
if (message?.role === "user")
return userText(message);
}
throw new Error("cursor-cli-oauth needs a user message to prompt the Cursor CLI");
}
function recapExchanges(context) {
const exchanges = [];
for (const message of context.messages) {
if (message.role === "user") {
const text = typeof message.content === "string" ? message.content : blockText(message.content);
if (text.length > 0)
exchanges.push({ role: "user", text });
}
else if (message.role === "assistant") {
const text = blockText(message.content);
if (text.length > 0)
exchanges.push({ role: "assistant", text });
}
}
return exchanges.slice(-RECENT_EXCHANGE_LIMIT);
}
function isFailoverNotice(event) {
return event.type === "cursor_account_changed";
}
function isRestartNotice(event) {
return event.type === "cursor_chat_restarted";
}
function turnErrorMessage(error) {
// Failover wraps every surfaced failure and the router hands classification
// inputs around as { thrown } records; the innermost cause is what the user needs.
let detail = error;
for (let depth = 0; depth < 3 && typeof detail === "object" && detail !== null; depth += 1) {
const wrapper = detail;
if (wrapper.original !== undefined)
detail = wrapper.original;
else if (wrapper.thrown !== undefined)
detail = wrapper.thrown;
else
break;
}
if (detail instanceof Error)
return detail.message;
if (typeof detail === "string")
return detail;
const message = detail?.message;
return typeof message === "string" && message.length > 0 ? message : "Cursor CLI turn failed";
}
/**
* Streams one Cursor CLI print-mode turn onto senpi's assistant event stream.
* The CLI owns tool execution; senpi owns the context and reports only its own
* token estimates, so no CLI-side number ever feeds compaction accounting.
*/
export function streamCursorCliOauth(model, context, options, deps = {}) {
const stream = createAssistantMessageEventStream();
void (async () => {
const mapper = {
stream,
output: emptyCursorCliOutput(model),
started: false,
openKind: undefined,
openIndex: -1,
openText: "",
textAccumulated: "",
};
const now = deps.now ?? Date.now;
const cwdDirectory = deps.cwd ?? process.cwd();
const agentDir = deps.agentDir ?? getAgentDir();
const store = deps.store ?? AuthStorage.create();
const router = deps.router ?? cursorCliSessionRouter;
const signal = options?.signal;
let wasAborted = signal?.aborted === true;
let sentPromptTokens = 0;
let runningHandle;
let handleRunning = false;
const onAbort = () => {
wasAborted = true;
};
if (!wasAborted)
signal?.addEventListener("abort", onAbort, { once: true });
try {
// Fresh per turn: settings, credentials, and executable resolution are
// never cached across turns, so back-to-back turns observe changes.
const settings = deps.settings ?? loadCursorCliOauthProviderSettingsFromDisk(cwdDirectory);
// An explicit `enabled: false` is the kill switch; the flagless ambient
// case is refused below, once the stored slots are known, so the turn path
// and `assessConfiguration` share one opt-in rule.
if (settings.explicitlyDisabled)
throw new Error(DISABLED_MESSAGE);
const executableDeps = {
...defaultCursorAgentExecutableDeps(),
settings: { executablePath: settings.executablePath },
};
// The executable gates the TURN before any guardrail or auth work: the
// oauth check keeps a credential-backed lane selectable so turns reach
// this point, and `cursor-agent not installed` + the install guidance
// must originate here - before a spawn could report it without the marker.
try {
resolveCursorAgentExecutable(deps.executableDeps ?? executableDeps);
}
catch (error) {
throw cursorAgentNotInstalledError(error);
}
// Guardrails decide before any spawn: an unacknowledged force attempt is
// the turn's error outcome, and only the policy's force/plan/sandbox
// verdict reaches the argv. Warnings surface as turn notices.
const guardrailSession = createCursorCliGuardrailSession();
const policy = resolveCursorCliExecutionPolicy({
forceExecution: settings.forceExecution,
noApprovalAcknowledgedAt: settings.noApprovalAcknowledgedAt,
executionMode: settings.executionMode,
sandboxMode: settings.sandboxMode,
}, guardrailSession, settings.denyCommands ?? []);
if (policy.status === "refused")
throw new CursorCliExecutionRefusalError(policy.refusal);
for (const warning of policy.warnings)
appendNotice(mapper, warning.message);
const stored = await store.read(CURSOR_CLI_OAUTH_PROVIDER_ID);
const storedAccounts = stored?.type === "oauth" ? listAccounts(stored) : [];
if (!isCursorCliOauthLaneEnabled(settings, storedAccounts.length))
throw new Error(DISABLED_MESSAGE);
if (storedAccounts.length === 0)
throw new Error(NO_ACCOUNTS_MESSAGE);
const prompt = turnPrompt(context);
const senpiSessionId = options?.affinitySessionId ?? options?.sessionId ?? DEFAULT_CURSOR_AFFINITY_KEY;
const spawnModel = resolveCursorCliSpawnModel(model, options?.thinkingSelection);
const turnInput = {
prompt,
model: spawnModel,
recentExchanges: recapExchanges(context),
};
const spawnAndStream = (attempt, slot) => {
sentPromptTokens = estimateTokens({
role: "user",
content: attempt.prompt,
timestamp: now(),
});
const buffer = new CursorCliEventBuffer();
void (async () => {
let sawResult = false;
// Malformed events are held until the exit outcome is known: a
// non-zero exit without a result means the captured stderr is the
// real cause, and the parser's trailing incomplete_stream event
// would otherwise mask it.
const heldMalformed = [];
try {
await runInCursorAccountHome(agentDir, slot, async ({ home }) => {
// The CLI rewrites cli-config.json during every invocation, so the
// deny list must ride the same per-spawn cadence as the home-store
// auth.json preparation directly above this spawn.
applyCursorCliDenyConfig(home, policy.denyCommands);
const handle = spawnCursorCli({
prompt: attempt.prompt,
model: spawnModel,
resumeChatId: attempt.resumeChatId,
force: policy.force,
executionMode: policy.executionMode,
sandboxMode: policy.sandboxMode,
accountHome: home,
cwd: cwdDirectory,
signal,
executableDeps: deps.executableDeps ?? executableDeps,
});
runningHandle = handle;
handleRunning = true;
if (wasAborted)
handle.abort();
try {
for await (const event of handle.events) {
if (event instanceof CursorCliAbortError) {
buffer.fail(event);
return;
}
if (event.type === "result")
sawResult = true;
if (event.type === "malformed_stream") {
heldMalformed.push(event);
continue;
}
buffer.push(event);
}
const outcome = await handle.completed;
if (outcome.type === "completed" && outcome.exitCode !== 0 && !sawResult) {
buffer.fail(new CursorCliProcessFailureError(outcome.exitCode, outcome.stderr));
return;
}
for (const event of heldMalformed)
buffer.push(event);
buffer.close();
}
finally {
handleRunning = false;
}
}, deps.log);
buffer.close();
}
catch (error) {
buffer.fail(error);
}
})();
return buffer.events();
};
const turn = runCursorCliFailover({
store,
providerId: CURSOR_CLI_OAUTH_PROVIDER_ID,
affinity: {
sessionId: senpiSessionId,
pinnedAccount: settings.pinnedAccount,
now: now(),
},
now: () => now(),
runAttempt: async (selected, _options) => {
let slot = selected;
const current = await store.read(CURSOR_CLI_OAUTH_PROVIDER_ID);
if (current?.type === "oauth") {
const fresh = current.accounts?.find((candidate) => candidate.name === slot.name);
if (fresh)
slot = fresh;
}
// An expired access token would classify as auth_error and block the
// account until re-login; refresh it while the refresh token exists.
if (now() >= slot.expires) {
const oauth = deps.oauth ??
createCursorCliOauthConfig({
readCurrent: () => store.read(CURSOR_CLI_OAUTH_PROVIDER_ID),
readSettings: () => settings,
resolveExecutable: (oauthSettings) => resolveCursorAgentExecutable({
...(deps.executableDeps ?? executableDeps),
settings: { executablePath: oauthSettings.executablePath },
}),
});
const storedCredential = current?.type === "oauth" ? current : emptyCredential();
const refreshed = await oauth.refreshToken(storedCredential, signal ?? new AbortController().signal);
// refreshToken re-applies the sentinel fields over the stored credential,
// so the result keeps the persisted oauth shape.
await store.modify(CURSOR_CLI_OAUTH_PROVIDER_ID, async () => refreshed);
const reread = await store.read(CURSOR_CLI_OAUTH_PROVIDER_ID);
const updated = reread?.type === "oauth"
? reread.accounts?.find((candidate) => candidate.name === slot.name)
: undefined;
if (!updated) {
throw new Error(`cursor-cli-oauth account '${slot.name}' disappeared during token refresh`);
}
slot = updated;
}
// Cross-account chats live in each account's HOME, so a fresh-chat
// failover needs no flag here: the router resumes only its own account's chat.
return router.runTurn({
senpiSessionId,
accountName: slot.name,
runAttempt: (attempt) => spawnAndStream(attempt, slot),
resumeMode: settings.resumeMode,
contextRecapOnModelSwitch: settings.contextRecapOnModelSwitch,
now: () => now(),
}, turnInput);
},
});
for await (const event of turn) {
if (isFailoverNotice(event) || isRestartNotice(event)) {
appendNotice(mapper, event.message);
continue;
}
switch (event.type) {
case "system":
// The session router already recorded chat id and model from init.
break;
case "thinking":
if (event.subtype === "completed") {
if (mapper.openKind === "thinking")
closeOpen(mapper);
}
else if (event.text.length > 0) {
ensureOpen(mapper, "thinking");
pushThinkingDelta(mapper, event.text);
}
break;
case "assistant":
for (const block of event.message.content)
appendAssistantFragment(mapper, block.text);
break;
case "tool_call":
// Preserve the text-segment boundary and cumulative snapshot reset,
// but do not expose provider protocol frames as assistant text or map
// them to host tool calls that Senpi could execute a second time.
resetTextSegment(mapper);
break;
case "result":
if (event.subtype === "success" && !event.is_error) {
applyResultEvent(mapper, model, sentPromptTokens, event.usage, event.request_id, event.duration_ms);
}
break;
default:
// malformed_stream events are converted to failures inside the
// session router and never reach this loop.
break;
}
}
closeOpen(mapper);
stream.push({ type: "done", reason: "stop", message: mapper.output });
}
catch (error) {
closeOpen(mapper);
const aborted = wasAborted || signal?.aborted === true;
mapper.output.stopReason = aborted ? "aborted" : "error";
mapper.output.errorMessage = turnErrorMessage(error);
const diagnostic = mapper.output.stopReason === "error" ? cursorCliDiagnostic(error) : undefined;
if (diagnostic !== undefined)
mapper.output.providerDiagnostic = diagnostic;
stream.push({ type: "error", reason: mapper.output.stopReason, error: mapper.output });
}
finally {
signal?.removeEventListener("abort", onAbort);
if (handleRunning)
runningHandle?.abort();
stream.end();
}
})();
return stream;
}
//# sourceMappingURL=stream.js.map