Skip to content

Commit 5d13bb4

Browse files
authored
fix(sync): stop firing a full Atlas sync on every message (#61)
getSession() rebuilt the session object with only api_key/user_id/ device_name, dropping cached_v and last_check_ts. refreshIfStale() reads both, so its TTL gate and version dedupe were dead code: every CLI invocation and every processor iteration fired a /api/cli/sync/version probe plus a full background /api/cli/sync, each rewriting synced-env and session files. updateSession (getSession + spread + save) also erased whichever bookkeeping field it wasn't patching. Carry the two fields through getSession so the 10s probe TTL and the version dedupe work as designed.
1 parent 4a0bd75 commit 5d13bb4

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

backend/cli/src/openscience/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ export namespace OpenScience {
305305
api_key: data.api_key,
306306
user_id: data.user_id || "",
307307
device_name: data.device_name,
308+
// Sync bookkeeping. Dropping these made refreshIfStale's TTL and
309+
// version dedupe dead code: every message fired a version probe
310+
// plus a full background sync, and updateSession (getSession +
311+
// spread + save) erased whichever field it wasn't patching.
312+
cached_v: data.cached_v,
313+
last_check_ts: data.last_check_ts,
308314
}
309315
} catch {
310316
return null
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { test, expect, afterEach } from "bun:test"
2+
import path from "path"
3+
import fs from "fs/promises"
4+
import { Global } from "../src/global"
5+
import { OpenScience } from "../src/openscience"
6+
7+
const file = path.join(Global.Path.data, "openscience-session.json")
8+
9+
afterEach(async () => {
10+
await fs.rm(file, { force: true }).catch(() => {})
11+
})
12+
13+
test("getSession carries the sync bookkeeping fields", async () => {
14+
await fs.mkdir(Global.Path.data, { recursive: true })
15+
await Bun.write(
16+
file,
17+
JSON.stringify({
18+
api_key: "thk_test.secret",
19+
user_id: "user-1",
20+
device_name: "test-device",
21+
cached_v: 7,
22+
last_check_ts: 1751700000000,
23+
}),
24+
)
25+
26+
const session = await OpenScience.getSession()
27+
expect(session).not.toBeNull()
28+
expect(session!.cached_v).toBe(7)
29+
expect(session!.last_check_ts).toBe(1751700000000)
30+
})

0 commit comments

Comments
 (0)