@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
66 lines • 2.87 kB
JavaScript
import { logContinuityEvent } from "./session-observability.js";
import { transitionToTainted } from "./session-registry-state.js";
/**
* Starts the live model switch without waiting for it: the claude CLI confirms the model with the API on every
* `setModel` (80-280 ms, 15 s when unreachable), and `model_select` must not hold `set_model` for it (senpi#6411).
* Switches chain on `entry.pendingModelSwitch` so they apply in order; the next turn admission awaits the chain.
* A failed switch is logged and leaves `modelId` on the last confirmed model, so that admission sees the drift and
* reattaches the same lineage on a fresh process with the selected model (`model_changed`), keeping the binding.
*/
export function switchEntryModel(registry, senpiSessionId, modelId, sdkModel = modelId) {
const entry = registry.get(senpiSessionId);
const setModel = entry?.query.setModel?.bind(entry.query);
if (!entry || !setModel)
return false;
const live = () => registry.get(senpiSessionId) === entry;
const apply = async () => {
if (!live())
return;
try {
await setModel(sdkModel);
}
catch (error) {
// Closing the process rejects its pending setModel; only a failure on the live process is news.
if (live()) {
const detail = error instanceof Error ? error.message : String(error);
logContinuityEvent("claude_sdk_oauth_model_switch_failed", { modelId, error: detail });
}
return;
}
entry.modelId = modelId;
registry.touch(entry);
};
const pending = { done: entry.pendingModelSwitch?.done.then(apply) ?? apply(), modelId };
entry.pendingModelSwitch = pending;
void pending.done.then(() => {
if (entry.pendingModelSwitch === pending)
entry.pendingModelSwitch = undefined;
});
return true;
}
export function annotatePendingFork(registry, senpiSessionId, reason, divergedPath) {
const entry = registry.get(senpiSessionId);
if (!entry)
return;
entry.pendingForkReason = reason;
// A fork recorded without a path (compaction, tree change) must not inherit an earlier rewrite's path.
entry.pendingForkDivergedPath = divergedPath ?? null;
registry.touch(entry);
}
export function annotateTainted(registry, senpiSessionId, reason) {
const entry = registry.get(senpiSessionId);
if (!entry)
return;
entry.taintedReason = reason;
if (entry.state !== "TAINTED")
transitionToTainted(entry);
registry.touch(entry);
}
export function annotateBranchInfo(registry, senpiSessionId, info) {
const entry = registry.get(senpiSessionId);
if (!entry)
return;
entry.branchInfo = { ...info };
registry.touch(entry);
}
//# sourceMappingURL=session-entry-annotations.js.map