fix(pi): retain ownership for uncertain mutations

This commit is contained in:
2026-08-25 23:53:54 +08:00
parent 621ebb1781
commit 019cbb115a
21 changed files with 1113 additions and 67 deletions

View File

@@ -25,6 +25,7 @@ const MAX_ATTACHMENTS = 16;
const MAX_ACCEPTANCES = 512;
const REQUEST_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
const ATTACHMENT_ID_PATTERN = /^[A-Za-z0-9-]{1,64}$/;
const REQUEST_UNCERTAIN_MESSAGE = '请求确认延迟,可能仍在执行。请等待结果,或中止/恢复后再重试。';
export class CodingConversationServiceError extends Error {
constructor(
@@ -442,8 +443,13 @@ export class CodingConversationService {
try {
selected = await this.runtime.validateModel(model);
} catch (error) { runtimeError(error); }
await this.ensurePrepared(conversationId);
let snapshot: ConversationSnapshot;
try {
snapshot = await this.runtime.getSnapshot(conversationId);
} catch (error) { runtimeError(error); }
this.assertSnapshotAllowsMutation(snapshot);
if (conversation.modelResolution === 'resolved' && conversation.model) {
await this.ensurePrepared(conversationId);
let state: ConversationModelState;
try {
state = await this.runtime.setModel({
@@ -499,6 +505,7 @@ export class CodingConversationService {
} catch (error) {
runtimeError(error);
}
this.assertSnapshotAllowsMutation(sourceSnapshot);
const sourceNode = sourceSnapshot.nodes.find((node) => (
node.kind === 'message'
&& node.sourceEntryId === entryId
@@ -696,6 +703,16 @@ export class CodingConversationService {
}
}
private assertSnapshotAllowsMutation(snapshot: ConversationSnapshot): void {
if (snapshot.run.error?.code !== 'CODING_REQUEST_UNCERTAIN') return;
if (!['queued', 'running', 'retrying', 'compacting', 'aborting'].includes(snapshot.run.status)) return;
throw new CodingConversationServiceError(
409,
'CODING_REQUEST_UNCERTAIN',
REQUEST_UNCERTAIN_MESSAGE,
);
}
private async archiveSession(projectId: string, sessionKey: string | undefined): Promise<void> {
if (!sessionKey) return;
if (!this.options.archiveSession) {