fix(coding): preserve runtime model and failure contracts
This commit is contained in:
@@ -38,7 +38,7 @@ import {
|
||||
reduceConversationPatch,
|
||||
type ConversationReducerState,
|
||||
} from '../conversation-reducer';
|
||||
import { CodingRuntimeContractError } from '../in-memory-conversation-runtime';
|
||||
import { CodingRuntimeContractError } from '../runtime-errors';
|
||||
import {
|
||||
PiEventProjector,
|
||||
type PiEventProjectorOptions,
|
||||
|
||||
@@ -12,6 +12,7 @@ import type {
|
||||
ConversationModelState,
|
||||
PrepareConversationInput,
|
||||
} from '../contracts';
|
||||
import { CodingRuntimeContractError } from '../runtime-errors';
|
||||
|
||||
export interface PiRegisteredConversation {
|
||||
projectPath: string;
|
||||
@@ -22,6 +23,7 @@ export interface PiRegisteredConversation {
|
||||
|
||||
export interface PiSessionRegistryOptions {
|
||||
projectStore: CodingProjectStore;
|
||||
createConversationStore?: typeof createCodingConversationStore;
|
||||
}
|
||||
|
||||
interface RegistryRecord extends PiRegisteredConversation {
|
||||
@@ -55,11 +57,13 @@ function sameModelState(left: ConversationModelState, right: ConversationModelSt
|
||||
|
||||
export class PiSessionRegistry {
|
||||
private readonly projectStore: CodingProjectStore;
|
||||
private readonly createConversationStore: typeof createCodingConversationStore;
|
||||
private readonly records = new Map<string, RegistryRecord>();
|
||||
private readonly prepareFlights = new Map<string, Promise<RegistryRecord>>();
|
||||
|
||||
constructor(options: PiSessionRegistryOptions) {
|
||||
this.projectStore = options.projectStore;
|
||||
this.createConversationStore = options.createConversationStore ?? createCodingConversationStore;
|
||||
}
|
||||
|
||||
async prepare(input: PrepareConversationInput): Promise<PiRegisteredConversation> {
|
||||
@@ -71,7 +75,9 @@ export class PiSessionRegistry {
|
||||
createBinding: () => Promise<PiSessionBinding>,
|
||||
): Promise<PiRegisteredConversation> {
|
||||
const record = await this.prepareRecord(input);
|
||||
const conversation = await record.store.ensureSessionBinding(input.conversationId, createBinding);
|
||||
const conversation = await this.persistWrite(
|
||||
() => record.store.ensureSessionBinding(input.conversationId, createBinding),
|
||||
);
|
||||
record.conversation = conversation;
|
||||
record.session = {
|
||||
piSessionId: conversation.piSessionId as string,
|
||||
@@ -86,7 +92,9 @@ export class PiSessionRegistry {
|
||||
): Promise<ConversationModelState> {
|
||||
const record = this.records.get(conversationId);
|
||||
if (!record) throw new Error('Conversation is not registered');
|
||||
record.conversation = await record.store.setModelState(conversationId, model);
|
||||
record.conversation = await this.persistWrite(
|
||||
() => record.store.setModelState(conversationId, model),
|
||||
);
|
||||
return modelStateOf(record.conversation);
|
||||
}
|
||||
|
||||
@@ -118,7 +126,7 @@ export class PiSessionRegistry {
|
||||
candidate.id === input.agentId && candidate.enabled && !candidate.archivedAt
|
||||
));
|
||||
if (!agent) throw new Error('Coding Agent does not exist');
|
||||
const store = createCodingConversationStore(project.path);
|
||||
const store = this.createConversationStore(project.path);
|
||||
const conversation = await store.get(input.conversationId);
|
||||
if (!conversation || conversation.agentId !== input.agentId) {
|
||||
throw new Error('Coding Conversation does not exist for the selected Agent');
|
||||
@@ -138,4 +146,17 @@ export class PiSessionRegistry {
|
||||
this.records.set(input.conversationId, record);
|
||||
return record;
|
||||
}
|
||||
|
||||
private async persistWrite<T>(operation: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (error) {
|
||||
if (error instanceof CodingRuntimeContractError) throw error;
|
||||
throw new CodingRuntimeContractError(
|
||||
'CODING_STORAGE_WRITE_FAILED',
|
||||
'Coding Conversation state could not be persisted',
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user