@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
97 lines • 4.11 kB
JavaScript
import { createHash } from "node:crypto";
import { z } from "zod";
/** Custom transcript entry for every non-input effect; inputs keep `rpc_client_message_admission`. */
export const RPC_EFFECT_ENTRY = "rpc_effect";
export const EFFECT_LEDGER_CAPABILITY = "effect_ledger";
const actorSchema = z.object({ connectionId: z.string() });
/**
* How an effect ended. `completed` needs no words; every other kind carries `reason` (the code) and
* `detail` (plain words), plus `action` whenever the caller can do something about it.
*/
export const effectOutcomeSchema = z.object({
kind: z.enum([
"completed",
"aborted",
"error",
"deadline",
"interrupted_by_reopen",
"dropped_by_replace",
"unknown_model",
"provider_unavailable",
"unsupported_thinking_level",
"superseded",
"context_budget",
"cleared",
// 2026.10.16-2 wrote an abort-drained input under this name; it must stay readable.
"cleared_by_abort",
"already_delivered",
"not_found",
"conflict",
"refused",
]),
reason: z.string().optional(),
detail: z.string().optional(),
action: z.string().optional(),
abortSource: z.string().optional(),
abortedBy: actorSchema.optional(),
});
export const rpcEffectRecordSchema = z.object({
effectId: z.string().min(1).max(256),
kind: z.enum(["abort", "unload", "set_model", "set_thinking_level", "dequeue", "edit_queued"]),
state: z.enum(["accepted", "applied", "done", "failed"]),
disposition: z.string().optional(),
outcome: effectOutcomeSchema.optional(),
// What a queued-input change answered beyond its status, so a replay answers exactly as the live call did.
answer: z
.object({
text: z.string().optional(),
revision: z.number().int().positive().optional(),
currentRevision: z.number().int().positive().optional(),
})
.optional(),
// Deliveries an interrupting unload handed back unwritten, so a replay names them as the live answer did.
dropped: z.object({ deliveries: z.array(z.string()) }).optional(),
// A refused unload's unwritten deliveries (senpi#7004), so a replay names them as the live refusal did.
pending: z.object({ deliveries: z.array(z.string()) }).optional(),
by: actorSchema,
acceptedAt: z.string(),
updatedAt: z.string(),
durableSessionId: z.string(),
fingerprint: z.string(),
});
export function isTerminalEffectState(state) {
return state === "done" || state === "failed";
}
export class EffectIdConflict extends Error {
constructor(effectId) {
super(`effect_id_conflict: effectId ${effectId} was already used for a different request`);
this.code = "effect_id_conflict";
this.reason = "effect_id_conflict: this effectId was already used for a different request";
this.action = "send the request again with a new effectId";
this.name = "EffectIdConflict";
this.effectId = effectId;
}
}
/**
* The SEMANTIC fields of each command kind, listed explicitly. The request `id`, the `effectId`, the
* routing `sessionId` (reallocated on every reopen) and every transport field are never hashed, so
* the same effect retried under a new routing handle after a reopen still replays.
*/
const SEMANTIC_FIELDS = {
abort: ["awaitIdle", "clearQueue", "graceMs"],
set_model: ["provider", "modelId", "thinkingLevel", "scope"],
set_thinking_level: ["level", "scope"],
unload: ["interrupt", "clearQueue", "children", "force", "deliveries"],
dequeue: ["queuedId", "expectedRevision"],
edit_queued: ["queuedId", "text", "expectedRevision"],
};
export function effectFingerprint(command, payload) {
const semantic = { kind: command };
for (const field of SEMANTIC_FIELDS[command]) {
const value = Reflect.get(payload, field);
if (value !== undefined)
semantic[field] = value;
}
return createHash("sha256").update(JSON.stringify(semantic)).digest("hex");
}
//# sourceMappingURL=effect-record.js.map