返回 DeepSeek-Reasonix
sessionReadActivity.ts
根目录 / desktop / frontend / src / lib / sessionReadActivity.ts
1 import type { ProjectNode } from "./types";
2 import { projectTreeReadActivityKey, topicReadRevision, topicUsesResultSequence, type ProjectTreeReadActivity } from "./projectTreeTopic";
3
4 export type ReadRecord = {
5 metric: "result" | "time";
6 value: number;
7 revision: number;
8 imported?: boolean;
9 verifiedVersion?: string;
10 repairVersion?: number;
11 readFloor?: number;
12 needsBaseline?: boolean;
13 migratedFromTime?: boolean;
14 };
15 export type ReadStore = { version: 3; baselineAt: number; records: Record<string, ReadRecord> };
16 export type BaselineObservation = { complete: boolean; resultSequence: number; eventVersion: string };
17
18 export function readActivityValues(store: ReadStore, nodes: readonly ProjectNode[] = []): ProjectTreeReadActivity {
19 const values = Object.fromEntries(Object.entries(store.records).map(([key, record]) => [key, record.needsBaseline ? Number.MAX_SAFE_INTEGER : record.value]));
20 const visit = (node: ProjectNode) => {
21 const key = projectTreeReadActivityKey(node);
22 // A downgrade/cached old row cannot compare a timestamp with a result count.
23 if (key && store.records[key]?.metric === "result" && !topicUsesResultSequence(node)) values[key] = Number.MAX_SAFE_INTEGER;
24 node.children?.forEach(visit);
25 };
26 nodes.forEach(visit);
27 return values;
28 }
29
30 export function markSessionRead(store: ReadStore, node: ProjectNode, now = Date.now()): ReadStore {
31 const key = projectTreeReadActivityKey(node);
32 if (!key) return store;
33 const metric = topicUsesResultSequence(node) ? "result" : "time";
34 if (node.remoteSession && metric === "result" && node.turnsState !== "ready") return store;
35 const value = metric === "result" ? topicReadRevision(node) : Math.max(topicReadRevision(node), now);
36 const previous = store.records[key];
37 if (previous?.metric === "result" && metric === "time") return store;
38 if (value < 0 || (value === 0 && (metric === "time" || node.turnsState !== "ready")) || (previous?.metric === metric && previous.value >= value && !previous.needsBaseline && (!previous.imported || (previous.readFloor ?? 0) >= value))) return store;
39 return { ...store, records: { ...store.records, [key]: { metric,
40 value: previous?.metric === metric ? Math.max(previous.value, value) : value,
41 revision: (previous?.revision ?? 0) + 1, repairVersion: previous?.repairVersion,
42 imported: previous?.metric === metric ? previous.imported : undefined,
43 migratedFromTime: previous?.migratedFromTime || previous?.metric === "time" && metric === "result",
44 readFloor: previous?.metric === metric && previous.imported ? Math.max(previous.readFloor ?? 0,value) : undefined } } };
45 }
46
47 export function repairReadBaseline(store: ReadStore, key: string, captured: ReadRecord, observation: BaselineObservation): ReadStore {
48 const current = store.records[key];
49 if (current?.needsBaseline && current.value === captured.value && current.revision === captured.revision
50 && observation.complete && observation.resultSequence > 0 && observation.eventVersion) {
51 return { ...store, records: { ...store.records, [key]: { ...current, metric: "result", value: observation.resultSequence,
52 revision: current.revision + 1, needsBaseline: false, verifiedVersion: observation.eventVersion } } };
53 }
54 if (!current || !current.imported || current.metric !== "result" || current.repairVersion
55 || current.value !== captured.value || current.revision !== captured.revision
56 || !observation.complete || observation.resultSequence <= 0 || observation.resultSequence < (current.readFloor ?? 0) || !observation.eventVersion) return store;
57 return { ...store, records: { ...store.records, [key]: { ...current,
58 value: Math.min(current.value, observation.resultSequence), imported: false,
59 revision: current.revision + 1, repairVersion: 1, verifiedVersion: observation.eventVersion } } };
60 }
61
62 /** Normal writes merge monotonically. A one-time repair is accepted only for
63 * the exact imported record revision it verified, never over a user's read. */
64 export function mergeReadStores(current: ReadStore, incoming: ReadStore): ReadStore {
65 let records = current.records;
66 for (const [key, next] of Object.entries(incoming.records)) {
67 const old = records[key];
68 if (old && !old.needsBaseline && next.needsBaseline) continue;
69 if (old?.repairVersion && next.imported) {
70 if (next.metric === old.metric && (next.readFloor ?? 0) > old.value) {
71 if (records === current.records) records = { ...records };
72 records[key] = { ...old, value: next.readFloor!, readFloor: next.readFloor, revision: Math.max(old.revision, next.revision) + 1 };
73 }
74 continue;
75 }
76 const metricMigration = old?.metric === "time" && next.metric === "result" && next.migratedFromTime;
77 if (old?.metric !== undefined && old.metric !== next.metric && !metricMigration) continue;
78 const repair = old?.imported && !old.repairVersion && next.repairVersion === 1
79 && next.verifiedVersion && next.revision === old.revision + 1;
80 if (!old || metricMigration || repair || next.value > old.value
81 || next.value === old.value && next.revision > old.revision) {
82 if (records === current.records) records = { ...records };
83 records[key] = next;
84 }
85 }
86 return records === current.records ? current : { ...current, records };
87 }
88
89 export function seedSessionReads(store: ReadStore, nodes: readonly ProjectNode[]): ReadStore {
90 let next = store;
91 const visit = (node: ProjectNode) => {
92 const key = projectTreeReadActivityKey(node);
93 const previous = key ? next.records[key] : undefined;
94 if (key && topicUsesResultSequence(node) && node.turnsState === "ready"
95 && (!previous || node.remoteSession && previous.metric === "time")) {
96 // Source time values must never become canonical result sequences.
97 const needsBaseline = !node.remoteSession && (node.identityAliases?.some(alias => next.records[alias]?.metric === "time") ?? false);
98 next = { ...next, records: { ...next.records, [key]: { metric: "result", value: needsBaseline ? 0 : topicReadRevision(node),
99 revision: (previous?.revision ?? 0) + 1, needsBaseline, migratedFromTime: previous?.metric === "time" } } };
100 }
101 node.children?.forEach(visit);
102 };
103 nodes.forEach(visit);
104 return next;
105 }
106
106 lines TYPESCRIPT