Skip to content

Commit 40a7ab7

Browse files
committed
refactor(task): reduce verified handoff surface
1 parent 3214f43 commit 40a7ab7

3 files changed

Lines changed: 44 additions & 41 deletions

File tree

‎packages/core/src/task-history/index.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ export type TaskFilePreImage = HistoryItem | typeof ABSENT_TASK_FILE_PREIMAGE |
2222
export function taskFilePreImage(existing: unknown, taskId: string, fileExists: boolean): TaskFilePreImage {
2323
const parsed = historyItemSchema.safeParse(existing)
2424
if (parsed.success && parsed.data.id === taskId) return structuredClone(existing as HistoryItem)
25-
if (existing !== null) return INVALID_TASK_FILE_PREIMAGE
26-
if (fileExists) return INVALID_TASK_FILE_PREIMAGE
27-
return ABSENT_TASK_FILE_PREIMAGE
25+
return existing === null && !fileExists ? ABSENT_TASK_FILE_PREIMAGE : INVALID_TASK_FILE_PREIMAGE
2826
}
2927

3028
export function isValidTaskFilePreImage(preImage: TaskFilePreImage): preImage is HistoryItem {

‎src/core/task-persistence/TaskHistoryStore.ts‎

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -908,15 +908,8 @@ export class TaskHistoryStore {
908908
}
909909
}
910910

911-
/**
912-
* Return only the fields in `incoming` that differ from `cached`.
913-
*/
914-
private computeDelta(cached: HistoryItem, incoming: Partial<HistoryItem>): Partial<HistoryItem> {
915-
return computeHistoryDelta(cached, incoming)
916-
}
917-
918911
private buildDelta(id: string, cached: HistoryItem, incoming: Partial<HistoryItem>): Partial<HistoryItem> {
919-
return { id, ...this.computeDelta(cached, incoming) }
912+
return { id, ...computeHistoryDelta(cached, incoming) }
920913
}
921914

922915
/**
@@ -1245,20 +1238,17 @@ export class TaskHistoryStore {
12451238
// Merge with existing cache entries before writing, mirroring upsertCore.
12461239
const mergedFirst = { ...first, ...updatedFirst }
12471240
const mergedSecond = { ...second, ...updatedSecond }
1248-
const holdFirstFileLock = Boolean(
1249-
options?.firstDiskGuard || options?.rollbackBothOnCallbackFailure || options?.whileFirstFileLocked,
1250-
)
1251-
const suppliedFirstFileLock = options?.firstFileLock
12521241
const firstFileLock =
1253-
suppliedFirstFileLock ??
1254-
(holdFirstFileLock ? await lockJsonFile(await this.getTaskFilePath(firstId)) : undefined)
1255-
const ownsFirstFileLock = Boolean(firstFileLock && !suppliedFirstFileLock)
1242+
options?.firstFileLock ??
1243+
(options?.firstDiskGuard || options?.rollbackBothOnCallbackFailure || options?.whileFirstFileLocked
1244+
? await lockJsonFile(await this.getTaskFilePath(firstId))
1245+
: undefined)
1246+
const ownsFirstFileLock = Boolean(firstFileLock && !options?.firstFileLock)
12561247

12571248
try {
12581249
let firstDiskSnapshot!: TaskFilePreImage
1259-
const firstDiskGuard = options?.firstDiskGuard
12601250
const firstDelta = this.buildDelta(firstId, first, updatedFirst)
1261-
const writtenFirst = await this.writeTaskFile(mergedFirst, firstDelta, firstDiskGuard, {
1251+
const writtenFirst = await this.writeTaskFile(mergedFirst, firstDelta, options?.firstDiskGuard, {
12621252
heldLock: firstFileLock,
12631253
capturePreImage: (preImage) => (firstDiskSnapshot = preImage),
12641254
})
@@ -1271,18 +1261,26 @@ export class TaskHistoryStore {
12711261
})
12721262
} catch (error) {
12731263
if (options?.rollbackBothOnCallbackFailure) {
1274-
const expectedFirst = Array.of(JSON.parse(JSON.stringify(writtenFirst)) as HistoryItem)
1275-
const firstRestoration = [firstId, firstDiskSnapshot, expectedFirst, firstFileLock] as const
1276-
const restorations = Array.of<TaskFileRestoration>(firstRestoration)
1264+
const restorations = Array.of<TaskFileRestoration>([
1265+
firstId,
1266+
firstDiskSnapshot,
1267+
Array.of(JSON.parse(JSON.stringify(writtenFirst)) as HistoryItem),
1268+
firstFileLock,
1269+
])
12771270
if (secondDiskSnapshot) {
12781271
const mergeSecond = mergeWithDisk(secondDelta)
1279-
const secondPreImage = isValidTaskFilePreImage(secondDiskSnapshot)
1280-
? secondDiskSnapshot
1281-
: null
1282-
const expectedSecond = mergeSecond(secondPreImage, mergedSecond) as HistoryItem
1283-
const persistedSecond = JSON.parse(JSON.stringify(expectedSecond)) as HistoryItem
1284-
const expectedSecondStates = expectedTaskFileStates(persistedSecond, secondDiskSnapshot)
1285-
restorations.unshift([secondId, secondDiskSnapshot, expectedSecondStates])
1272+
const expectedSecond = mergeSecond(
1273+
isValidTaskFilePreImage(secondDiskSnapshot) ? secondDiskSnapshot : null,
1274+
mergedSecond,
1275+
) as HistoryItem
1276+
restorations.unshift([
1277+
secondId,
1278+
secondDiskSnapshot,
1279+
expectedTaskFileStates(
1280+
JSON.parse(JSON.stringify(expectedSecond)) as HistoryItem,
1281+
secondDiskSnapshot,
1282+
),
1283+
])
12861284
}
12871285
const rollbackErrors = await this.restoreTaskFilePreImages(restorations)
12881286
if (rollbackErrors.length)
@@ -1308,11 +1306,19 @@ export class TaskHistoryStore {
13081306
if (!options?.rollbackBothOnCallbackFailure) throw error
13091307

13101308
// Restore second before first, preserving the original compensation order.
1311-
const expectedSecond = Array.of(JSON.parse(JSON.stringify(writtenSecond)) as HistoryItem)
1312-
const expectedFirst = Array.of(JSON.parse(JSON.stringify(writtenFirst)) as HistoryItem)
13131309
const compensationErrors = await this.restoreTaskFilePreImages([
1314-
[secondId, secondDiskSnapshot!, expectedSecond, undefined],
1315-
[firstId, firstDiskSnapshot!, expectedFirst, firstFileLock],
1310+
[
1311+
secondId,
1312+
secondDiskSnapshot!,
1313+
Array.of(JSON.parse(JSON.stringify(writtenSecond)) as HistoryItem),
1314+
undefined,
1315+
],
1316+
[
1317+
firstId,
1318+
firstDiskSnapshot!,
1319+
Array.of(JSON.parse(JSON.stringify(writtenFirst)) as HistoryItem),
1320+
firstFileLock,
1321+
],
13161322
])
13171323

13181324
if (this.onWrite) {
@@ -1323,8 +1329,8 @@ export class TaskHistoryStore {
13231329
}
13241330
}
13251331

1326-
const callbackErrors = Array.of(error, ...compensationErrors)
1327-
if (compensationErrors.length) throw new AggregateError(callbackErrors, "Pair compensation failed")
1332+
if (compensationErrors.length)
1333+
throw new AggregateError(Array.of(error, ...compensationErrors), "Pair compensation failed")
13281334
throw error
13291335
}
13301336
} finally {

‎src/core/webview/ClineProvider.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,9 +1319,7 @@ export class ClineProvider
13191319
// that only contain 'id' and 'name' fields. Activating such a profile would
13201320
// overwrite the CLI's working API configuration with empty settings.
13211321
const fullProfile = await this.providerSettingsManager.getProfile({ name: profile.name })
1322-
const hasActualSettings = !!fullProfile.apiProvider
1323-
1324-
if (hasActualSettings) {
1322+
if (fullProfile.apiProvider) {
13251323
await this.activateProviderProfile(
13261324
{ name: profile.name },
13271325
{ persistTaskHistory: false },
@@ -3880,8 +3878,9 @@ export class ClineProvider
38803878
mode: string
38813879
pendingActionId?: string
38823880
}): Promise<Task> {
3883-
const start = () => ClineProvider.prototype.delegateParentAndOpenChildUnlocked.call(this, params)
3884-
return ClineProvider.prototype.runDelegationTransition.call(this, params.parentTaskId, start) as Promise<Task>
3881+
return ClineProvider.prototype.runDelegationTransition.call(this, params.parentTaskId, () =>
3882+
ClineProvider.prototype.delegateParentAndOpenChildUnlocked.call(this, params),
3883+
) as Promise<Task>
38853884
}
38863885

38873886
private async delegateParentAndOpenChildUnlocked(params: {

0 commit comments

Comments
 (0)