@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
107 lines • 5.06 kB
JavaScript
import { LONG_CONTEXT_FALLBACK_DIAGNOSTIC, LONG_CONTEXT_FALLBACK_NOTICE } from "../../../core/provider-notices.js";
import { theme } from "../theme/theme.js";
export const CONTINUITY_DIAGNOSTIC_TYPE = "claude_sdk_oauth_session_continuity";
export const RESUME_FALLBACK_DIAGNOSTIC_TYPE = "claude_sdk_oauth_resume_fallback";
/** Only degradation kinds reach the transcript; healthy kinds stay in session.log and RPC. */
const DEGRADATION_KINDS = new Set(["flatten", "disabled"]);
const KIND_LABELS = {
flatten: "Session continuity lost - resent the full conversation",
disabled: "Session continuity disabled (resumeMode: off) - resending the conversation each turn",
};
const RESUME_FALLBACK_LABEL = "Session continuity lost - resume failed, resent the full conversation";
const SETTING_SOURCE_LABELS = {
env: "SENPI_CLAUDE_SDK_OAUTH_RESUME",
project: "project settings",
global: "global settings",
};
function continuityDetails(diagnostic) {
if (diagnostic.type !== CONTINUITY_DIAGNOSTIC_TYPE)
return undefined;
const details = diagnostic.details;
if (!details || typeof details.kind !== "string")
return undefined;
return {
kind: details.kind,
...(typeof details.reason === "string" ? { reason: details.reason } : {}),
...(typeof details.payloadBytes === "number" ? { payloadBytes: details.payloadBytes } : {}),
...(typeof details.collapsedDirectives === "number" ? { collapsedDirectives: details.collapsedDirectives } : {}),
...(typeof details.settingSource === "string" ? { settingSource: details.settingSource } : {}),
};
}
/** Renders a byte count the way the transcript shows sizes: one decimal, KB/MB. */
function formatBytes(bytes) {
if (bytes < 1024)
return `${bytes}B`;
if (bytes < 1024 * 1024)
return `${(bytes / 1024).toFixed(1)}KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
}
/**
* Appends the re-send cost to a degradation notice so the token bill is visible
* at the moment it is paid: how much was resent, and how many duplicate
* ultrawork directive blocks were collapsed out of it.
*/
function payloadSuffix(details) {
if (details.payloadBytes === undefined)
return "";
const collapsed = details.collapsedDirectives !== undefined && details.collapsedDirectives > 0
? `, ${details.collapsedDirectives} duplicate ultrawork blocks collapsed`
: "";
return ` - sent ${formatBytes(details.payloadBytes)}${collapsed}`;
}
/**
* Builds the muted single-line transcript notice for a completed assistant
* message. `disabled` renders once per session so the escape hatch does not nag;
* healthy kinds render nothing at all.
*/
export class ContinuityNoticeTracker {
constructor() {
this.renderedDisabled = false;
}
/**
* Clear the suppression state. Called when the transcript is rebuilt
* (initial load, post-compaction rebuild, session switch): the rebuilt
* transcript re-derives notices from persisted messages, so the first
* disabled notice must be allowed to render again.
*/
reset() {
this.renderedDisabled = false;
}
noticeFor(message) {
const notices = [];
for (const diagnostic of message.diagnostics ?? []) {
if (diagnostic.type === RESUME_FALLBACK_DIAGNOSTIC_TYPE) {
notices.push(this.format(RESUME_FALLBACK_LABEL));
continue;
}
if (diagnostic.type === LONG_CONTEXT_FALLBACK_DIAGNOSTIC) {
notices.push(this.format(LONG_CONTEXT_FALLBACK_NOTICE));
continue;
}
const details = continuityDetails(diagnostic);
if (!details || !DEGRADATION_KINDS.has(details.kind))
continue;
if (details.kind === "disabled") {
// Only the resumeMode setting disables resume; a request with no session (registry_miss) is not a degradation.
if (details.reason !== undefined && details.reason !== "resume_mode_off")
continue;
if (this.renderedDisabled)
continue;
this.renderedDisabled = true;
const where = details.settingSource === undefined ? undefined : SETTING_SOURCE_LABELS[details.settingSource];
if (where !== undefined) {
notices.push(this.format(`Session continuity disabled (resumeMode: off, set by ${where}) - resending the conversation each turn${payloadSuffix(details)}`));
continue;
}
}
const label = KIND_LABELS[details.kind] ?? "Session continuity degraded";
const base = details.reason ? `${label} (${details.reason})` : label;
notices.push(this.format(`${base}${payloadSuffix(details)}`));
}
return notices.length > 0 ? notices.join("\n") : undefined;
}
format(text) {
return theme.fg("muted", text);
}
}
//# sourceMappingURL=continuity-notice.js.map