Skip to content

Commit 56d8a3d

Browse files
committed
feat(archive): move archived conversation data into cold storage
🤖 Co-authored by GPT-6 in Codex via T3 Code
1 parent d2c9281 commit 56d8a3d

94 files changed

Lines changed: 8704 additions & 711 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.agents/skills/test-t3-app/references/sqlite-fixtures.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Load this reference only when inspecting or seeding local T3 state directly.
44

55
## Select the correct database
66

7-
When `--base-dir` or `--home-dir` is explicit, runtime state lives under `<base-dir>/userdata` and the database path is `<base-dir>/userdata/state.sqlite`. The `<base-dir>/dev` state directory is only the fallback for an implicit development home, preventing an ordinary `vp run dev` from touching production state.
7+
When `--base-dir` or `--home-dir` is explicit, runtime state lives under `<base-dir>/userdata`. Hot projections and runtime state are stored in `state.sqlite`; cold archive manifests and compressed chunks are stored in `archive.sqlite`. The `<base-dir>/dev` state directory is only the fallback for an implicit development home, preventing an ordinary `vp run dev` from touching production state.
88

99
Start the target runtime once before seeding so all migrations have run. Use an isolated base directory. Stop the server before writes to avoid racing application state or an active projection.
1010

@@ -18,6 +18,15 @@ node apps/server/scripts/t3-sqlite-state.ts query \
1818
--sql "SELECT name FROM sqlite_schema WHERE type = 'table' ORDER BY name"
1919
```
2020

21+
The helper targets `state.sqlite` by default. Select the cold archive database explicitly for read-only inspection:
22+
23+
```bash
24+
node apps/server/scripts/t3-sqlite-state.ts query \
25+
--base-dir <base-dir> \
26+
--database archive \
27+
--sql "SELECT t.thread_id, t.archive_version, COUNT(c.chunk_index) AS chunk_count FROM archive_threads AS t LEFT JOIN archive_thread_chunks AS c ON c.thread_id = t.thread_id GROUP BY t.thread_id, t.archive_version ORDER BY t.thread_id"
28+
```
29+
2130
Inspect current columns before writing a fixture:
2231

2332
```bash
@@ -36,6 +45,8 @@ node apps/server/scripts/t3-sqlite-state.ts exec \
3645

3746
Use one statement per invocation for both `query` and `exec`; the helper wraps writes in a transaction and prints the backup path after a successful mutation. Use a single insert with multiple value rows when a fixture needs several records.
3847

48+
Cold archive manifests and chunks are authoritative recovery data. Prefer `--database archive` for queries only. Exercise archive and restore behavior through application commands and APIs instead of constructing or changing archive bundles directly; only use archive writes for a deliberately isolated corruption or compatibility fixture.
49+
3950
## Seed projection data carefully
4051

4152
The web UI primarily reads these projection tables:

‎BRANCH_DETAILS.md‎

Lines changed: 121 additions & 0 deletions
Large diffs are not rendered by default.

‎apps/mobile/src/features/archive/ArchivedThreadsRouteScreen.tsx‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { EnvironmentId } from "@t3tools/contracts";
22
import * as Arr from "effect/Array";
33
import * as Order from "effect/Order";
44
import { useFocusEffect } from "@react-navigation/native";
5-
import { useCallback, useMemo, useState } from "react";
5+
import { useCallback, useMemo, useRef, useState } from "react";
66

77
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
88
import { useArchivedThreadListActions } from "../home/useThreadListActions";
@@ -15,12 +15,18 @@ import {
1515
refreshArchivedThreadsForEnvironment,
1616
useArchivedThreadSnapshots,
1717
} from "./useArchivedThreadSnapshots";
18+
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
19+
import { scopedThreadKey } from "../../lib/scopedEntities";
1820

1921
export function ArchivedThreadsRouteScreen() {
2022
const { savedConnectionsById } = useSavedRemoteConnections();
2123
const [searchQuery, setSearchQuery] = useState("");
2224
const [selectedEnvironmentId, setSelectedEnvironmentId] = useState<EnvironmentId | null>(null);
2325
const [sortOrder, setSortOrder] = useState<ArchivedThreadSortOrder>("newest");
26+
const [unarchivingThreadKeys, setUnarchivingThreadKeys] = useState<ReadonlySet<string>>(
27+
() => new Set(),
28+
);
29+
const unarchivingThreadKeysRef = useRef(new Set<string>());
2430
const environments = useMemo<ReadonlyArray<ArchivedThreadsHeaderEnvironment>>(
2531
() =>
2632
Arr.sort(
@@ -65,6 +71,25 @@ export function ArchivedThreadsRouteScreen() {
6571
);
6672
const { unarchiveThread, confirmDeleteThread } =
6773
useArchivedThreadListActions(refreshChangedEnvironment);
74+
const handleUnarchiveThread = useCallback(
75+
async (thread: EnvironmentThreadShell) => {
76+
const threadKey = scopedThreadKey(thread.environmentId, thread.id);
77+
if (unarchivingThreadKeysRef.current.has(threadKey)) return;
78+
unarchivingThreadKeysRef.current.add(threadKey);
79+
setUnarchivingThreadKeys((current) => new Set(current).add(threadKey));
80+
try {
81+
await unarchiveThread(thread);
82+
} finally {
83+
unarchivingThreadKeysRef.current.delete(threadKey);
84+
setUnarchivingThreadKeys((current) => {
85+
const next = new Set(current);
86+
next.delete(threadKey);
87+
return next;
88+
});
89+
}
90+
},
91+
[unarchiveThread],
92+
);
6893

6994
useFocusEffect(
7095
useCallback(() => {
@@ -83,10 +108,11 @@ export function ArchivedThreadsRouteScreen() {
83108
onRefresh={refresh}
84109
onSearchQueryChange={setSearchQuery}
85110
onSortOrderChange={setSortOrder}
86-
onUnarchiveThread={unarchiveThread}
111+
onUnarchiveThread={(thread) => void handleUnarchiveThread(thread)}
87112
searchQuery={searchQuery}
88113
selectedEnvironmentId={selectedEnvironmentId}
89114
sortOrder={sortOrder}
115+
unarchivingThreadKeys={unarchivingThreadKeys}
90116
/>
91117
);
92118
}

‎apps/mobile/src/features/archive/ArchivedThreadsScreen.tsx‎

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ import { AppText as Text } from "../../components/AppText";
2727
import { EmptyState } from "../../components/EmptyState";
2828
import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";
2929
import { ProjectFavicon } from "../../components/ProjectFavicon";
30-
import { relativeTime } from "../../lib/time";
30+
import { scopedThreadKey } from "../../lib/scopedEntities";
3131
import { useUniwindTheme } from "../../lib/useUniwindTheme";
3232
import { useServerConfigs } from "../../state/entities";
3333
import { ThreadSwipeable } from "../home/thread-swipe-actions";
34-
import type { ArchivedThreadGroup, ArchivedThreadSortOrder } from "./archivedThreadList";
34+
import {
35+
formatArchivedThreadRelativeTime,
36+
type ArchivedThreadGroup,
37+
type ArchivedThreadSortOrder,
38+
} from "./archivedThreadList";
3539
import { SettingsScreenContent } from "../settings/components/SettingsScreen";
3640

3741
export interface ArchivedThreadsHeaderEnvironment {
@@ -182,6 +186,7 @@ function ArchivedThreadRow(props: {
182186
readonly environmentLabel: string | null;
183187
readonly isFirst: boolean;
184188
readonly isLast: boolean;
189+
readonly isUnarchiving: boolean;
185190
readonly onDelete: () => void;
186191
readonly onSwipeableClose: (methods: SwipeableMethods) => void;
187192
readonly onSwipeableWillOpen: (methods: SwipeableMethods) => void;
@@ -193,10 +198,13 @@ function ArchivedThreadRow(props: {
193198
}) {
194199
const { width: windowWidth } = useWindowDimensions();
195200
const cardColor = useUniwindTheme()["--color-card"];
196-
const timestamp = relativeTime(props.thread.archivedAt ?? props.thread.updatedAt);
201+
const timestamp = formatArchivedThreadRelativeTime(
202+
props.thread.archivedAt ?? props.thread.updatedAt,
203+
);
197204
const subtitle = [props.environmentLabel, props.thread.branch].filter((part): part is string =>
198205
Boolean(part),
199206
);
207+
const onDelete = props.isUnarchiving ? () => undefined : props.onDelete;
200208
return (
201209
<ThreadSwipeable
202210
resetKey={`${props.thread.environmentId}:${props.thread.id}`}
@@ -211,15 +219,16 @@ function ArchivedThreadRow(props: {
211219
borderBottomRightRadius: props.isLast ? 20 : 0,
212220
overflow: "hidden",
213221
}}
222+
enabled={!props.isUnarchiving}
214223
fullSwipeWidth={windowWidth - 32}
215-
onDelete={props.onDelete}
224+
onDelete={onDelete}
216225
onSwipeableClose={props.onSwipeableClose}
217226
onSwipeableWillOpen={props.onSwipeableWillOpen}
218227
primaryAction={{
219228
accessibilityLabel: `Unarchive ${props.thread.title}`,
220229
icon: "arrow.uturn.backward",
221230
label: "Unarchive",
222-
onPress: props.onUnarchive,
231+
onPress: props.isUnarchiving ? () => undefined : props.onUnarchive,
223232
}}
224233
simultaneousWithExternalGesture={props.simultaneousSwipeGesture}
225234
threadTitle={props.thread.title}
@@ -229,12 +238,16 @@ function ArchivedThreadRow(props: {
229238
className={`flex-row items-center gap-3 bg-card px-4 py-3 ${props.isLast ? "" : "border-b border-separator"}`}
230239
>
231240
<View className="h-[34px] w-[34px] items-center justify-center rounded-[11px] bg-subtle">
232-
<SymbolView
233-
name="archivebox.fill"
234-
size={15}
235-
tintColorClassName="accent-icon-subtle"
236-
type="monochrome"
237-
/>
241+
{props.isUnarchiving ? (
242+
<ActivityIndicator colorClassName="accent-icon-subtle" size="small" />
243+
) : (
244+
<SymbolView
245+
name="archivebox.fill"
246+
size={15}
247+
tintColorClassName="accent-icon-subtle"
248+
type="monochrome"
249+
/>
250+
)}
238251
</View>
239252

240253
<View className="min-w-0 flex-1 gap-1">
@@ -245,9 +258,11 @@ function ArchivedThreadRow(props: {
245258
>
246259
{props.thread.title}
247260
</Text>
248-
<Text className="min-w-[30px] text-right text-xs tabular-nums text-foreground-tertiary">
249-
{timestamp}
250-
</Text>
261+
{timestamp ? (
262+
<Text className="min-w-[30px] text-right text-xs tabular-nums text-foreground-tertiary">
263+
{timestamp}
264+
</Text>
265+
) : null}
251266
</View>
252267
{subtitle.length > 0 ? (
253268
<View className="flex-row items-center gap-1.5">
@@ -300,6 +315,7 @@ export function ArchivedThreadsScreen(props: {
300315
readonly onSearchQueryChange: (query: string) => void;
301316
readonly onSortOrderChange: (sortOrder: ArchivedThreadSortOrder) => void;
302317
readonly onUnarchiveThread: (thread: EnvironmentThreadShell) => void;
318+
readonly unarchivingThreadKeys: ReadonlySet<string>;
303319
}) {
304320
const { onDeleteThread, onUnarchiveThread } = props;
305321
const openSwipeableRef = useRef<SwipeableMethods | null>(null);
@@ -371,6 +387,9 @@ export function ArchivedThreadsScreen(props: {
371387
environmentLabel={item.environmentLabel}
372388
isFirst={item.isFirst}
373389
isLast={item.isLast}
390+
isUnarchiving={props.unarchivingThreadKeys.has(
391+
scopedThreadKey(item.thread.environmentId, item.thread.id),
392+
)}
374393
onDelete={() => onDeleteThread(item.thread)}
375394
onSwipeableClose={handleSwipeableClose}
376395
onSwipeableWillOpen={handleSwipeableWillOpen}
@@ -386,6 +405,7 @@ export function ArchivedThreadsScreen(props: {
386405
handleSwipeableWillOpen,
387406
onDeleteThread,
388407
onUnarchiveThread,
408+
props.unarchivingThreadKeys,
389409
],
390410
);
391411
const listEmptyComponent = useMemo(() => {

‎apps/mobile/src/features/archive/archivedThreadList.test.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { OrchestrationProjectShell, OrchestrationThreadShell } from "@t3too
33
import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
44
import { describe, expect, it } from "vite-plus/test";
55

6-
import { buildArchivedThreadGroups } from "./archivedThreadList";
6+
import { buildArchivedThreadGroups, formatArchivedThreadRelativeTime } from "./archivedThreadList";
77

88
const environmentId = EnvironmentId.make("environment-1");
99

@@ -144,4 +144,66 @@ describe("buildArchivedThreadGroups", () => {
144144

145145
expect(result).toEqual([]);
146146
});
147+
148+
it("sorts invalid archive timestamps after valid timestamps in either direction", () => {
149+
const project = makeProject({ id: ProjectId.make("project-1"), title: "T3 Code" });
150+
const invalid = makeThread({
151+
archivedAt: "not-a-timestamp",
152+
id: ThreadId.make("thread-invalid"),
153+
projectId: project.id,
154+
title: "Invalid",
155+
});
156+
const anotherInvalid = makeThread({
157+
archivedAt: "also-not-a-timestamp",
158+
id: ThreadId.make("thread-another-invalid"),
159+
projectId: project.id,
160+
title: "Another invalid",
161+
});
162+
const older = makeThread({
163+
id: ThreadId.make("thread-older"),
164+
projectId: project.id,
165+
title: "Older",
166+
});
167+
const newer = makeThread({
168+
archivedAt: "2026-06-03T00:00:00.000Z",
169+
id: ThreadId.make("thread-newer"),
170+
projectId: project.id,
171+
title: "Newer",
172+
});
173+
const snapshots = [makeSnapshot([project], [invalid, anotherInvalid, older, newer])];
174+
175+
const newest = buildArchivedThreadGroups({
176+
snapshots,
177+
environmentLabels: {},
178+
environmentId: null,
179+
searchQuery: "",
180+
sortOrder: "newest",
181+
});
182+
const oldest = buildArchivedThreadGroups({
183+
snapshots,
184+
environmentLabels: {},
185+
environmentId: null,
186+
searchQuery: "",
187+
sortOrder: "oldest",
188+
});
189+
190+
expect(newest[0]?.threads.map((thread) => thread.id)).toEqual([
191+
"thread-newer",
192+
"thread-older",
193+
"thread-another-invalid",
194+
"thread-invalid",
195+
]);
196+
expect(oldest[0]?.threads.map((thread) => thread.id)).toEqual([
197+
"thread-older",
198+
"thread-newer",
199+
"thread-another-invalid",
200+
"thread-invalid",
201+
]);
202+
});
203+
});
204+
205+
describe("formatArchivedThreadRelativeTime", () => {
206+
it("omits invalid archive timestamps instead of presenting them as recent", () => {
207+
expect(formatArchivedThreadRelativeTime("not-a-timestamp")).toBeNull();
208+
});
147209
});

‎apps/mobile/src/features/archive/archivedThreadList.ts‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as Arr from "effect/Array";
1010
import * as Order from "effect/Order";
1111

1212
import { scopedProjectKey } from "../../lib/scopedEntities";
13+
import { relativeTime } from "../../lib/time";
1314

1415
export type ArchivedThreadSortOrder = "newest" | "oldest";
1516

@@ -19,15 +20,33 @@ export interface ArchivedThreadGroup {
1920
readonly threads: ReadonlyArray<EnvironmentThreadShell>;
2021
}
2122

22-
function archiveTimestamp(thread: EnvironmentThreadShell): number {
23-
const timestamp = Date.parse(thread.archivedAt ?? thread.updatedAt);
24-
return Number.isNaN(timestamp) ? 0 : timestamp;
23+
function parseTimestamp(input: string): number | null {
24+
const timestamp = Date.parse(input);
25+
return Number.isNaN(timestamp) ? null : timestamp;
26+
}
27+
28+
function archiveTimestamp(thread: EnvironmentThreadShell): number | null {
29+
return parseTimestamp(thread.archivedAt ?? thread.updatedAt);
30+
}
31+
32+
export function formatArchivedThreadRelativeTime(input: string): string | null {
33+
return parseTimestamp(input) === null ? null : relativeTime(input);
2534
}
2635

2736
function matchesQuery(value: string | null, query: string): boolean {
2837
return value?.toLocaleLowerCase().includes(query) ?? false;
2938
}
3039

40+
function makeArchiveTimestampOrder(sortOrder: ArchivedThreadSortOrder): Order.Order<number | null> {
41+
const timestampOrder = sortOrder === "newest" ? Order.flip(Order.Number) : Order.Number;
42+
return Order.make((left, right) => {
43+
if (left === null && right === null) return 0;
44+
if (left === null) return 1;
45+
if (right === null) return -1;
46+
return timestampOrder(left, right);
47+
});
48+
}
49+
3150
export function buildArchivedThreadGroups(input: {
3251
readonly snapshots: ReadonlyArray<ArchivedSnapshotEntry>;
3352
readonly environmentLabels: Readonly<Record<string, string>>;
@@ -72,14 +91,18 @@ export function buildArchivedThreadGroups(input: {
7291
continue;
7392
}
7493

75-
const timestampOrder = input.sortOrder === "newest" ? Order.flip(Order.Number) : Order.Number;
94+
const timestampOrder = makeArchiveTimestampOrder(input.sortOrder);
7695
groups.push({
7796
key: scopedProjectKey(project.environmentId, project.id),
7897
project,
7998
threads: Arr.sort(
8099
matchingThreads,
81100
Order.mapInput(
82-
Order.Struct({ timestamp: timestampOrder, title: Order.String, id: Order.String }),
101+
Order.Struct({
102+
timestamp: timestampOrder,
103+
title: Order.String,
104+
id: Order.String,
105+
}),
83106
(thread: EnvironmentThreadShell) => ({
84107
timestamp: archiveTimestamp(thread),
85108
title: thread.title,
@@ -91,13 +114,13 @@ export function buildArchivedThreadGroups(input: {
91114
}
92115
}
93116

94-
const timestampOrder = input.sortOrder === "newest" ? Order.flip(Order.Number) : Order.Number;
117+
const timestampOrder = makeArchiveTimestampOrder(input.sortOrder);
95118
return Arr.sort(
96119
groups,
97120
Order.mapInput(
98121
Order.Struct({ timestamp: timestampOrder, title: Order.String, key: Order.String }),
99122
(group: ArchivedThreadGroup) => ({
100-
timestamp: group.threads[0] ? archiveTimestamp(group.threads[0]) : 0,
123+
timestamp: group.threads[0] ? archiveTimestamp(group.threads[0]) : null,
101124
title: group.project.title,
102125
key: group.key,
103126
}),

0 commit comments

Comments
 (0)