refactor: use structured clone for local copies

This commit is contained in:
Peter Steinberger
2026-04-20 16:26:24 +01:00
parent 9fa204003f
commit 911cfe2adc
8 changed files with 9 additions and 19 deletions

View File

@@ -80,7 +80,7 @@ export function getLoadSessionStoreMock(): AnyMock {
}
export function setSessionStoreEntriesForTest(entries: SessionStore) {
sessionStoreEntries.value = JSON.parse(JSON.stringify(entries)) as SessionStore;
sessionStoreEntries.value = structuredClone(entries);
}
const { readChannelAllowFromStore, upsertChannelPairingRequest } = vi.hoisted(

View File

@@ -240,11 +240,7 @@ export async function loadCompactHooksHarness(): Promise<{
createAgentSession: vi.fn(async () => {
const session = {
sessionId: "session-1",
messages: sessionMessages.map((message) =>
typeof structuredClone === "function"
? structuredClone(message)
: JSON.parse(JSON.stringify(message)),
),
messages: sessionMessages.map((message) => structuredClone(message)),
agent: {
streamFn: vi.fn(),
transport: "sse",
@@ -253,7 +249,7 @@ export async function loadCompactHooksHarness(): Promise<{
return session.messages;
},
set messages(messages: unknown[]) {
session.messages = [...(messages as typeof session.messages)];
session.messages = [...messages];
},
},
},

View File

@@ -10,7 +10,7 @@ vi.mock("./models/shared.js", async () => {
return {
...actual,
updateConfig: async (mutator: (cfg: Record<string, unknown>) => Record<string, unknown>) => {
const next = mutator(JSON.parse(JSON.stringify(mocks.currentConfig)));
const next = mutator(structuredClone(mocks.currentConfig));
mocks.writtenConfig = next;
return next;
},

View File

@@ -9,10 +9,7 @@ type JsonSchemaObject = {
};
export function cloneSchema<T>(value: T): T {
if (typeof structuredClone === "function") {
return structuredClone(value);
}
return JSON.parse(JSON.stringify(value)) as T;
return structuredClone(value);
}
export function asSchemaObject(value: unknown): object | null {

View File

@@ -147,7 +147,7 @@ describe("runCronIsolatedAgentTurn — cron model override (#21057)", () => {
| { model?: string; modelProvider?: string; systemSent?: boolean }
| undefined;
if (entry) {
persistedSnapshots.push(JSON.parse(JSON.stringify(entry)));
persistedSnapshots.push(structuredClone(entry));
}
},
);

View File

@@ -579,7 +579,7 @@ async function prepareManualRun(
job,
startedAt: preflight.now,
});
const executionJob = JSON.parse(JSON.stringify(job)) as CronJob;
const executionJob = structuredClone(job);
return {
ok: true,
ran: true,

View File

@@ -100,7 +100,7 @@ function resolveClaudeCliUsage(raw: ClaudeCliUsage) {
}
function cloneJsonValue<T>(value: T): T {
return JSON.parse(JSON.stringify(value)) as T;
return structuredClone(value);
}
function normalizeClaudeCliContent(

View File

@@ -1,8 +1,5 @@
export function cloneConfigObject<T>(value: T): T {
if (typeof structuredClone === "function") {
return structuredClone(value);
}
return JSON.parse(JSON.stringify(value)) as T;
return structuredClone(value);
}
export function serializeConfigForm(form: Record<string, unknown>): string {