Skip to content

Commit bbac803

Browse files
authored
fix(engine): make v1 chat history copying fast and lossless (#6721)
* fix(engine): restore staged history by session and reconcile stale copies Signed-off-by: Guillaume Loret <guillaume@openworklabs.com> * fix(engine): retain the exact unresolved payload safety check Signed-off-by: Guillaume Loret <guillaume@openworklabs.com> * fix(engine): record session ownership in history copy checkpoints Signed-off-by: Guillaume Loret <guillaume@openworklabs.com> * fix(engine): advance history sequence atomically with reconciliation Signed-off-by: Guillaume Loret <guillaume@openworklabs.com> * test(engine): prove restore progress in an isolated desktop Signed-off-by: Guillaume Loret <guillaume@openworklabs.com> * test(engine): make restore progress proof honest and portable Signed-off-by: Guillaume Loret <guillaume@openworklabs.com> * test(engine): retain observed restore assertions for evidence reports Signed-off-by: Guillaume Loret <guillaume@openworklabs.com> --------- Signed-off-by: Guillaume Loret <guillaume@openworklabs.com>
1 parent 4ec0548 commit bbac803

12 files changed

Lines changed: 919 additions & 63 deletions

‎apps/app/src/app/lib/openwork-server.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ export type OpenworkCloudProviderSyncStatus = {
8383

8484
export interface EngineV2MigrationStatus {
8585
state: "idle" | "running" | "completed" | "error";
86-
/** Only `copying` moves the counts; older servers omit it. */
87-
phase?: "starting" | "converting" | "copying";
86+
/** Restoring counts messages separately from the chat-copy counts. */
87+
phase?: "starting" | "converting" | "restoring" | "copying";
88+
restored?: number;
89+
restoreTotal?: number;
8890
imported: number;
8991
skipped: number;
9092
total: number;
@@ -116,10 +118,12 @@ function parseEngineV2Migration(value: unknown): EngineV2MigrationStatus | undef
116118
if (!("imported" in value) || typeof value.imported !== "number"
117119
|| !("skipped" in value) || typeof value.skipped !== "number"
118120
|| !("total" in value) || typeof value.total !== "number") return undefined;
119-
const phase = "phase" in value && (value.phase === "starting" || value.phase === "converting" || value.phase === "copying")
121+
const phase = "phase" in value && (value.phase === "starting" || value.phase === "converting" || value.phase === "restoring" || value.phase === "copying")
120122
? value.phase : undefined;
121123
return { state: value.state, imported: value.imported, skipped: value.skipped, total: value.total,
122124
...(phase ? { phase } : {}),
125+
...("restored" in value && typeof value.restored === "number" ? { restored: value.restored } : {}),
126+
...("restoreTotal" in value && typeof value.restoreTotal === "number" ? { restoreTotal: value.restoreTotal } : {}),
123127
...("startedAt" in value && typeof value.startedAt === "string" ? { startedAt: value.startedAt } : {}),
124128
...("backupPath" in value && typeof value.backupPath === "string" ? { backupPath: value.backupPath } : {}),
125129
...("notCopied" in value ? { notCopied: parseChats(value.notCopied) } : {}),

‎apps/app/src/i18n/locales/en.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ export default {
673673
"engine_migration.not_copied_other": "{left} chats couldn't be copied and stay in OpenCode v1.",
674674
"engine_migration.not_now": "Not now",
675675
"engine_migration.progress_copied": "{done} of {total} chats copied",
676+
"engine_migration.progress_restored": "Restoring messages: {done} of {total}",
676677
"engine_migration.progress_note": "OpenCode v2 chats are paused while history is copied. You can restart and resume without losing progress. Your v1 chats aren't changed.",
677678
"engine_migration.progress_title": "Migrating chats to OpenCode v2",
678679
"engine_migration.progress_total_one": "1 chat to copy",

‎apps/app/src/react-app/shell/engine-migration.tsx‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ const handledCount = (migration: EngineV2MigrationStatus) =>
127127
export function engineMigrationMessage(migration: EngineV2MigrationStatus | undefined): string | undefined {
128128
if (!migration) return undefined;
129129
if (migration.state === "running") {
130+
if (migration.phase === "restoring") return t("engine_migration.progress_restored", {
131+
done: formatCount(migration.restored ?? 0), total: formatCount(migration.restoreTotal ?? 0),
132+
});
130133
if (migration.phase === "starting" || migration.phase === "converting" || migration.total === 0) {
131134
return t("engine_migration.settings_starting");
132135
}
@@ -344,11 +347,13 @@ function useElapsed(startedAt: string | undefined, running: boolean) {
344347

345348
function MigrationProgressBar(props: { migration: EngineV2MigrationStatus }) {
346349
const { migration } = props;
347-
const done = handledCount(migration);
350+
const restoring = migration.phase === "restoring";
351+
const done = restoring ? migration.restored ?? 0 : handledCount(migration);
352+
const total = restoring ? migration.restoreTotal ?? 0 : migration.total;
348353
const copying = migration.phase === "copying" || (migration.phase === undefined && done > 0);
349354
const elapsed = useElapsed(migration.startedAt, migration.state === "running");
350-
const value = copying && migration.total > 0 ? Math.min(100, Math.round((done / migration.total) * 100)) : null;
351-
const label = copying && migration.total > 0
355+
const value = (copying || restoring) && total > 0 ? Math.min(100, Math.round((done / total) * 100)) : null;
356+
const label = restoring ? t("engine_migration.progress_restored", { done: formatCount(done), total: formatCount(total) }) : copying && migration.total > 0
352357
? t("engine_migration.progress_copied", { done: formatCount(done), total: formatCount(migration.total) })
353358
: migration.total > 0
354359
? t("engine_migration.progress_total", { count: migration.total, total: formatCount(migration.total) })
@@ -574,7 +579,7 @@ function MigrationFloatingRegion(props: PropsWithChildren) {
574579
function MigrationBanner(props: { progress: ProgressState }) {
575580
const { migration } = props.progress;
576581
const done = handledCount(migration);
577-
const count = migration.phase === "copying" && migration.total > 0
582+
const count = migration.phase === "restoring" ? engineMigrationMessage(migration) : migration.phase === "copying" && migration.total > 0
578583
? t("engine_migration.progress_copied", { done: formatCount(done), total: formatCount(migration.total) })
579584
: null;
580585
return (

‎apps/app/tests/engine-migration.test.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ async function renderOverlay(discoverRunningMigration?: boolean) {
9191
});
9292
}
9393

94+
test("restoring reports message counts rather than a stalled chat counter", async () => {
95+
Reflect.set(window, "__OPENWORK_ELECTRON__", {});
96+
const client = engineClient();
97+
const migration = { state: "running", phase: "restoring", imported: 0, skipped: 0, total: 2471, restored: 44000, restoreTotal: 208014 } satisfies EngineV2PreviewStatus["migration"];
98+
spyOn(client, "getEngineV2PreviewStatus").mockResolvedValue({ ...status, migration });
99+
useEngineMigrationStore.setState({ progress: {
100+
client, migration, selected: "v1", view: "dialog", retrying: false, runningTasks: 0, switchWhenDone: true,
101+
} });
102+
await renderOverlay();
103+
expect(document.querySelector('[data-testid="engine-migration-count"]')?.textContent).toBe("Restoring messages: 44,000 of 208,014");
104+
expect(document.querySelector('[role="progressbar"]')?.getAttribute("aria-valuenow")).toBe("21");
105+
});
106+
94107
test("web never upgrades the engine from another consumer's enabled feature cache", async () => {
95108
const client = engineClient();
96109
expect(isDesktopRuntime()).toBe(false);

0 commit comments

Comments
 (0)