fix(coding): close PI-100 review findings
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import { mkdir, stat } from 'node:fs/promises';
|
||||
import { mkdir, rename, stat } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
BUNDLED_CODING_SKILL_IDS,
|
||||
type BundledCodingSkillId,
|
||||
} from '../../../shared/coding-skills';
|
||||
import { atomicWriteJson, atomicWriteText } from '../../coding-projects/atomic-json';
|
||||
import { validateSessionKey } from '../../coding-projects/conversation-store';
|
||||
import type { PiProviderSelection } from './provider-config';
|
||||
import type { PiManagedInputRevision } from './managed-input-revision';
|
||||
|
||||
@@ -104,6 +105,31 @@ export async function ensurePiManagedPaths(userDataDir: string): Promise<PiManag
|
||||
return paths;
|
||||
}
|
||||
|
||||
export async function archivePiConversationSession(input: {
|
||||
userDataDir: string;
|
||||
projectId: string;
|
||||
sessionKey: string;
|
||||
}): Promise<string | null> {
|
||||
const projectId = managedSegment(input.projectId, 'Project id');
|
||||
const sessionKey = validateSessionKey(input.sessionKey);
|
||||
const paths = getPiManagedPaths(input.userDataDir);
|
||||
const source = path.join(paths.sessionsDir, projectId, `${sessionKey}.jsonl`);
|
||||
const targetDirectory = path.join(paths.trashDir, projectId);
|
||||
const target = path.join(targetDirectory, `${sessionKey}.jsonl`);
|
||||
await mkdir(targetDirectory, { recursive: true });
|
||||
try {
|
||||
await rename(source, target);
|
||||
return target;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
|
||||
const alreadyArchived = await stat(target).catch((targetError: NodeJS.ErrnoException) => {
|
||||
if (targetError.code === 'ENOENT') return null;
|
||||
throw targetError;
|
||||
});
|
||||
return alreadyArchived?.isFile() ? target : null;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeSkillIds(skillIds: readonly string[]): BundledCodingSkillId[] {
|
||||
const result: BundledCodingSkillId[] = [];
|
||||
const seen = new Set<string>();
|
||||
|
||||
@@ -672,6 +672,15 @@ export class PiConversationRuntime implements CodingConversationRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
async validateModel(model: ProductModelRef): Promise<ProductModelRef> {
|
||||
const selection = await this.resolveModel(model);
|
||||
return {
|
||||
accountId: selection.accountId,
|
||||
modelId: selection.modelId,
|
||||
thinkingLevel: model.thinkingLevel,
|
||||
};
|
||||
}
|
||||
|
||||
async setModel(input: SetConversationModelInput): Promise<ConversationModelState> {
|
||||
await this.waitForProjection(input.conversationId);
|
||||
const snapshot = this.snapshot(input.conversationId);
|
||||
@@ -957,19 +966,30 @@ export class PiConversationRuntime implements CodingConversationRuntime {
|
||||
if (!this.isAuthenticationError || !this.refreshCredential || !input.model.model) {
|
||||
return await this.pool.prepare(input);
|
||||
}
|
||||
return await this.providerRefresh.withSingleAuthRecovery({
|
||||
accountId: input.model.model.accountId,
|
||||
operation: async () => await this.pool.prepare(input),
|
||||
isAuthenticationError: this.isAuthenticationError,
|
||||
refreshCredential: async () => await this.refreshCredential!(input.model.model!.accountId),
|
||||
reopenWorker: async () => {
|
||||
if (!this.pool.getState(input.conversationId)) return;
|
||||
const recovered = await this.pool.recover(input.conversationId);
|
||||
if (this.states.has(input.conversationId)) {
|
||||
await this.requestHydration(input.conversationId, recovered, false);
|
||||
}
|
||||
},
|
||||
});
|
||||
try {
|
||||
return await this.providerRefresh.withSingleAuthRecovery({
|
||||
accountId: input.model.model.accountId,
|
||||
operation: async () => await this.pool.prepare(input),
|
||||
isAuthenticationError: this.isAuthenticationError,
|
||||
refreshCredential: async () => await this.refreshCredential!(input.model.model!.accountId),
|
||||
reopenWorker: async () => {
|
||||
if (!this.pool.getState(input.conversationId)) return;
|
||||
const recovered = await this.pool.recover(input.conversationId);
|
||||
if (this.states.has(input.conversationId)) {
|
||||
await this.requestHydration(input.conversationId, recovered, false);
|
||||
}
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (this.isAuthenticationError(error)) {
|
||||
throw new CodingRuntimeContractError(
|
||||
'CODING_PROVIDER_AUTH_REQUIRED',
|
||||
'Provider authentication failed after one recovery attempt',
|
||||
true,
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async acceptPrompt(
|
||||
@@ -1008,13 +1028,20 @@ export class PiConversationRuntime implements CodingConversationRuntime {
|
||||
}, runId);
|
||||
}
|
||||
} catch (error) {
|
||||
const failure = this.isAuthenticationError?.(error)
|
||||
? new CodingRuntimeContractError(
|
||||
'CODING_PROVIDER_AUTH_REQUIRED',
|
||||
'Provider authentication failed after one recovery attempt',
|
||||
true,
|
||||
)
|
||||
: error;
|
||||
this.pool.failTopLevel(
|
||||
conversationId,
|
||||
runId,
|
||||
error instanceof Error ? error : new Error('Prompt acceptance failed'),
|
||||
failure instanceof Error ? failure : new Error('Prompt acceptance failed'),
|
||||
);
|
||||
this.failRun(conversationId, runId, error);
|
||||
throw error;
|
||||
this.failRun(conversationId, runId, failure);
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user