@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
104 lines • 3.59 kB
JavaScript
export function initialCompactionLifecycleState() {
return { status: "idle", generation: 0 };
}
export function beginCompactionOperation(state, operation) {
return {
status: "running",
generation: state.generation + 1,
...operation,
};
}
export function promoteCompactionOperation(state, operationId) {
if (state.status !== "running" || state.operationId !== operationId || state.stage === "execution")
return state;
return { ...state, stage: "execution" };
}
export function finishCompactionOperation(state, event) {
if (state.status !== "running" || state.operationId !== event.operationId)
return state;
const operation = {
generation: state.generation,
operationId: state.operationId,
stage: state.stage,
reason: state.reason,
model: state.model,
startedRevision: state.startedRevision,
};
if (event.status === "completed") {
return { ...operation, status: "completed", endedRevision: event.endedRevision };
}
if (event.status === "aborted") {
return {
...operation,
status: "aborted",
endedRevision: event.endedRevision,
errorMessage: event.errorMessage,
};
}
return {
...operation,
status: "failed",
endedRevision: event.endedRevision,
rejectionCause: event.rejectionCause,
errorMessage: event.errorMessage,
};
}
/**
* Couples lifecycle reducer transitions with the controller that owns the
* active generation. AgentSession owns event emission and durable work; this
* coordinator only protects state transitions from stale callers.
*/
export class CompactionLifecycleCoordinator {
constructor() {
this._state = initialCompactionLifecycleState();
}
get state() {
return this._state;
}
begin(operation, controller) {
if (this._state.status === "running") {
if (this._controller === controller) {
const runningOperationId = this._state.operationId;
if (operation.stage === "execution") {
this._state = promoteCompactionOperation(this._state, runningOperationId);
}
return runningOperationId;
}
this._controller?.abort();
}
this._controller = controller;
this._state = beginCompactionOperation(this._state, operation);
return operation.operationId;
}
isCurrent(operationId, controller) {
return (this._state.status === "running" &&
this._state.operationId === operationId &&
this._controller === controller &&
!controller.signal.aborted);
}
hasCurrentSignal(signal) {
return this._state.status === "running" && this._controller?.signal === signal && !signal.aborted;
}
finish(event) {
const next = finishCompactionOperation(this._state, event);
if (next === this._state)
return false;
this._state = next;
this._controller = undefined;
return true;
}
abort(endedRevision) {
if (this._state.status !== "running")
return undefined;
const { operationId, reason, stage } = this._state;
this._controller?.abort();
this.finish({
operationId,
status: "aborted",
endedRevision,
errorMessage: "Compaction cancelled",
});
return { reason, stage };
}
}
//# sourceMappingURL=lifecycle.js.map