Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions src/core/task-persistence/__tests__/historySnapshot.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions src/core/task-persistence/historySnapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { types } from "node:util"

const nativeCloneRequired = Symbol("native history snapshot required")

function clonePlainHistory(value: unknown): unknown {
const seen = new WeakMap<object, object>()
const copy = (current: unknown): unknown => {
if (typeof current === "function" || typeof current === "symbol") throw nativeCloneRequired
if (current === null || typeof current !== "object") return current
if (types.isProxy(current)) throw nativeCloneRequired
const previous = seen.get(current)
if (previous !== undefined) return previous

const prototype: unknown = Object.getPrototypeOf(current)
if (!Array.isArray(current) && prototype !== Object.prototype && prototype !== null) throw nativeCloneRequired
const snapshot: object = Array.isArray(current)
? new Array<unknown>(current.length)
: prototype === null
? Object.create(null)
: {}
seen.set(current, snapshot)
for (const key of Object.keys(current)) {
const descriptor = Object.getOwnPropertyDescriptor(current, key)
// Native cloning handles accessors once, rather than evaluating them in
// this pass and evaluating them again if a later property needs fallback.
if (!descriptor || !("value" in descriptor)) throw nativeCloneRequired
Object.defineProperty(snapshot, key, {
value: copy(descriptor.value),
enumerable: true,
configurable: true,
writable: true,
})
}
return snapshot
}
return copy(value)
}

/** Isolate JSON history's mutable containers without copying immutable string payloads. */
export function snapshotHistory<T>(value: T): T {
try {
// Every supported container is rebuilt, preserving the input's graph/type.
return clonePlainHistory(value) as T
} catch (error) {
if (error !== nativeCloneRequired) throw error
// Clone the WHOLE graph for non-JSON legacy/provider data. Cloning special
// objects separately would break aliases/cycles crossing Maps, Sets or views.
// The plain-clone frame has unwound, releasing its partial snapshot first.
return structuredClone(value)
}
}
5 changes: 3 additions & 2 deletions src/core/task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { v7 as uuidv7 } from "uuid"
import EventEmitter from "events"

import { AskIgnoredError } from "./AskIgnoredError"
import { snapshotHistory } from "../task-persistence/historySnapshot"
import { RateLimitClock, createRateLimitClock } from "./RateLimitClock"

import { Anthropic } from "@anthropic-ai/sdk"
Expand Down Expand Up @@ -1648,7 +1649,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
private async saveApiConversationHistory(merge = true): Promise<boolean> {
try {
await saveApiMessages({
messages: structuredClone(this.apiConversationHistory),
messages: snapshotHistory(this.apiConversationHistory),
taskId: this.taskId,
globalStoragePath: this.globalStoragePath,
merge,
Expand Down Expand Up @@ -1853,7 +1854,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
private async saveClineMessages(merge = true): Promise<boolean> {
try {
await saveTaskMessages({
messages: structuredClone(this.clineMessages),
messages: snapshotHistory(this.clineMessages),
taskId: this.taskId,
globalStoragePath: this.globalStoragePath,
merge,
Expand Down
128 changes: 128 additions & 0 deletions src/core/task/__tests__/Task.persistence.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading