Skip to content

Commit 55c9fcb

Browse files
sahrizviclaude
andcommitted
fix(workspace): flush syncs and mirrors together, abort before the signal flush, steadier tests
Bot review of 8da3abe on #1344. - The two exit flushes ran sequentially under independent 30s bounds — two stalled backends cost a minute. They run together now - On the first signal the event stream is aborted before the mirror snapshot is taken, so the run cannot enqueue a mirror behind the flush - The `altimate_memory_write` description names the workspace-sync gate too - Tests: the gated cases wait on a "request arrived" signal instead of a 20ms sleep, and every one releases its mirror in `finally`; the two-specifier test — which proves nothing under Bun's single record — asserts the `globalThis` anchor directly; the production-path enablement test waits for the bind's backfill and resets the poller memo it fills Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent 8da3abe commit 55c9fcb

5 files changed

Lines changed: 93 additions & 81 deletions

File tree

‎packages/opencode/src/cli/cmd/run.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,9 @@ You are speaking to a non-technical business executive. Follow these rules stric
10841084
if (signalled) return process.exit(code)
10851085
signalled = true
10861086
if (!CoreFlag.ALTIMATE_WORKSPACE) return process.exit(code)
1087+
// Stop the run first so no new mirror is enqueued behind the snapshot the
1088+
// flush takes; what is already on the wire is what gets the 2s.
1089+
eventAbort.abort()
10871090
void import("../../altimate/workspace/memory-sync")
10881091
.then((m) => m.flushPendingMirrors(2_000))
10891092
.catch(() => {})
@@ -1460,14 +1463,17 @@ You are speaking to a non-technical business executive. Follow these rules stric
14601463
// received its skills at all. Imported lazily and only when the feature is
14611464
// on, so an opted-out run does not load the module.
14621465
if (CoreFlag.ALTIMATE_WORKSPACE) {
1463-
await import("../../altimate/workspace/skill-sync")
1464-
.then((m) => m.flushPendingSyncs())
1465-
.catch(() => {})
14661466
// And the memory mirrors: a block saved on the last turn was uploaded
1467-
// fire-and-forget and lost the same race (#1332).
1468-
await import("../../altimate/workspace/memory-sync")
1469-
.then((m) => m.flushPendingMirrors())
1470-
.catch(() => {})
1467+
// fire-and-forget and lost the same race (#1332). Both flushes run together
1468+
// under their own bounds, so two stalled backends cost one wait, not two.
1469+
await Promise.all([
1470+
import("../../altimate/workspace/skill-sync")
1471+
.then((m) => m.flushPendingSyncs())
1472+
.catch(() => {}),
1473+
import("../../altimate/workspace/memory-sync")
1474+
.then((m) => m.flushPendingMirrors())
1475+
.catch(() => {}),
1476+
])
14711477
}
14721478
// altimate_change end
14731479

‎packages/opencode/src/memory/tools/memory-write.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MEMORY_MAX_BLOCK_SIZE, MEMORY_MAX_BLOCKS_PER_SCOPE, CitationSchema, Mem
66
const idSchema = MemoryBlockSchema.shape.id
77

88
export const MemoryWriteTool = Tool.define("altimate_memory_write", {
9-
description: `Save an Altimate Memory block for cross-session persistence. Use this to store information worth remembering across sessions — warehouse configurations, naming conventions, team preferences, data model notes, or past analysis decisions. When the project is linked to an Altimate Workspace with workspace memory enabled this is the TEAM's memory: blocks sync to the workspace and to every linked checkout, so a decision saved here is what teammates' sessions read. Before saving, call \`altimate_memory_read\` for an existing block on the same subject and update it rather than add a duplicate. The datamate_* memory tools (datamate_add_memories, datamate_search_memory) are the engine's separate store and are not what linked checkouts read. Each block is a Markdown file persisted to disk. Max ${MEMORY_MAX_BLOCK_SIZE} chars per block, ${MEMORY_MAX_BLOCKS_PER_SCOPE} blocks per scope. Supports hierarchical IDs with slashes (e.g., 'warehouse/snowflake-config'), optional TTL expiration, and citation-backed memories.`,
9+
description: `Save an Altimate Memory block for cross-session persistence. Use this to store information worth remembering across sessions — warehouse configurations, naming conventions, team preferences, data model notes, or past analysis decisions. When the project is linked to an Altimate Workspace with workspace sync on and workspace memory enabled this is the TEAM's memory: blocks sync to the workspace and to every linked checkout, so a decision saved here is what teammates' sessions read. Before saving, call \`altimate_memory_read\` for an existing block on the same subject and update it rather than add a duplicate. The datamate_* memory tools (datamate_add_memories, datamate_search_memory) are the engine's separate store and are not what linked checkouts read. Each block is a Markdown file persisted to disk. Max ${MEMORY_MAX_BLOCK_SIZE} chars per block, ${MEMORY_MAX_BLOCKS_PER_SCOPE} blocks per scope. Supports hierarchical IDs with slashes (e.g., 'warehouse/snowflake-config'), optional TTL expiration, and citation-backed memories.`,
1010
parameters: z.object({
1111
id: idSchema
1212
.describe(

‎packages/opencode/test/altimate/workspace/identity-section.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ describe("systemSection", () => {
141141
// same one the write path and the backfill sweep run) asks GET /datamates/ and
142142
// caches a yes; the section reads that. Deleting the cache write would hide the
143143
// line for good, and the previous test would not notice.
144-
const { memoryEnabledForPoller, resetEnablementMemoForTests } = await import("../../../src/altimate/workspace/memory-sync")
144+
const { memoryEnabledForPoller, resetEnablementMemoForTests, resetPollMemoForTests } = await import(
145+
"../../../src/altimate/workspace/memory-sync"
146+
)
145147
globalThis.fetch = (async (input: any) =>
146148
new Response(
147149
JSON.stringify(
@@ -150,15 +152,19 @@ describe("systemSection", () => {
150152
{ status: 200, headers: { "content-type": "application/json" } },
151153
)) as unknown as typeof fetch
152154
const binding = { datamateId: 91, datamateName: "Team", repoRemote: null, projectPath: projectDir, linkedAt: Date.now() }
153-
await recordApprovedBinding(projectDir, binding)
155+
// The bind's own backfill sweep runs the same check; wait for it so it cannot
156+
// refill the memo between the reset and the first assertion. (bot review)
157+
await recordApprovedBinding(projectDir, binding, { awaitBackfill: true })
154158
resetEnablementMemoForTests()
159+
resetPollMemoForTests()
155160
try {
156161
expect(await inProject(systemSection)).not.toContain("Team memory:")
157162
expect(await memoryEnabledForPoller(binding as never)).toBe("enabled")
158163
resetOutcomeMemoForTests()
159164
expect(await inProject(systemSection)).toContain("Team memory:")
160165
} finally {
161166
resetEnablementMemoForTests()
167+
resetPollMemoForTests()
162168
}
163169
})
164170

‎packages/opencode/test/altimate/workspace/memory-sync.test.ts‎

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -339,97 +339,103 @@ describe("buildMetadata", () => {
339339
})
340340
})
341341

342+
/** A fetch that parks every request behind a gate and reports when the first one
343+
* has arrived — the readiness signal the gated tests wait on instead of a sleep. */
344+
function gatedFetch() {
345+
let release!: () => void
346+
const gate = new Promise<void>((r) => (release = r))
347+
let entered!: () => void
348+
const firstRequest = new Promise<void>((r) => (entered = r))
349+
const original = globalThis.fetch
350+
let requests = 0
351+
globalThis.fetch = (async (input: any, init?: any) => {
352+
requests++
353+
entered()
354+
await gate
355+
return original(input, init)
356+
}) as unknown as typeof fetch
357+
return {
358+
release,
359+
firstRequest,
360+
requests: () => requests,
361+
restore: () => {
362+
globalThis.fetch = original
363+
},
364+
}
365+
}
366+
342367
// ── write path ──────────────────────────────────────────────────────────────
343368
describe("mirrorBlock", () => {
344369
test("flushPendingMirrors waits for a mirror a short-lived process would abandon (#1332)", async () => {
345370
// `MemoryStore.write` fires the mirror and forgets it; a one-shot `run` exits
346371
// when the turn ends, routinely before the upload lands. The flush holds the
347372
// exit for it, the way `skill-sync.flushPendingSyncs` holds it for a skill sync.
348-
let release!: () => void
349-
const gate = new Promise<void>((r) => (release = r))
350-
const original = globalThis.fetch
351-
let requests = 0
352-
globalThis.fetch = (async (input: any, init?: any) => {
353-
requests++
354-
await gate // the server is slow: nothing completes until we say so
355-
return original(input, init)
356-
}) as unknown as typeof fetch
373+
const net = gatedFetch()
357374
createResult = [{ id: "mem-slow" }]
358375
let settled = false
359-
void mirrorBlock(block({ id: "slow" })).then(() => (settled = true))
360-
await Bun.sleep(20)
361-
expect(settled).toBe(false)
362-
expect(requests).toBeGreaterThan(0) // it is genuinely on the wire
363-
const flush = flushPendingMirrors()
364-
let flushed = false
365-
void flush.then(() => (flushed = true))
366-
await Bun.sleep(20)
367-
expect(flushed).toBe(false) // the flush is holding for the mirror
368-
release()
369-
await flush
370-
expect(settled).toBe(true)
371-
// Nothing in flight: an immediate return.
372-
const started = Date.now()
373-
await flushPendingMirrors()
374-
expect(Date.now() - started).toBeLessThan(50)
375-
})
376-
377-
test("a mirror tracked through one module specifier is flushed through another (codex on #1344)", async () => {
378-
// `MemoryStore` reaches this module via `@/…`, the `run` exit path via a relative
379-
// path. Whether or not the runtime keeps one record per specifier, the set the
380-
// flush reads must be the set the writer filled — hence the `globalThis` anchor.
381-
const viaAlias = await import("@/altimate/workspace/memory-sync")
382-
let release!: () => void
383-
const gate = new Promise<void>((r) => (release = r))
384-
const original = globalThis.fetch
385-
globalThis.fetch = (async (input: any, init?: any) => {
386-
await gate
387-
return original(input, init)
388-
}) as unknown as typeof fetch
389-
createResult = [{ id: "mem-alias" }]
390-
let settled = false
391-
const mirror = viaAlias.mirrorBlock(block({ id: "via-alias" })).then(() => (settled = true))
376+
const mirror = mirrorBlock(block({ id: "slow" })).then(() => (settled = true))
392377
try {
393-
await Bun.sleep(20)
394-
const flush = flushPendingMirrors() // the relative-import copy
378+
await net.firstRequest // it is genuinely on the wire
379+
expect(settled).toBe(false)
380+
const flush = flushPendingMirrors()
395381
let flushed = false
396382
void flush.then(() => (flushed = true))
397383
await Bun.sleep(20)
398-
expect(flushed).toBe(false) // it is holding for the alias copy's mirror
399-
release()
384+
expect(flushed).toBe(false) // the flush is holding for the mirror
385+
net.release()
400386
await flush
401387
expect(settled).toBe(true)
388+
// Nothing in flight: an immediate return.
389+
const started = Date.now()
390+
await flushPendingMirrors()
391+
expect(Date.now() - started).toBeLessThan(50)
402392
} finally {
403-
release()
393+
net.release()
404394
await mirror
405-
globalThis.fetch = original
395+
net.restore()
396+
}
397+
})
398+
399+
test("the in-flight set lives on globalThis, so every copy of this module shares it (codex on #1344)", async () => {
400+
// `MemoryStore` reaches this module via `@/…`, the `run` exit path via a relative
401+
// path. Bun hands both the same record today, so a two-specifier test proves
402+
// nothing; what is asserted is the anchor itself: a tracked mirror is visible on
403+
// the process-global state, which is what a second module record would read.
404+
const net = gatedFetch()
405+
createResult = [{ id: "mem-anchor" }]
406+
const mirror = mirrorBlock(block({ id: "anchored" }))
407+
try {
408+
await net.firstRequest
409+
const state = (globalThis as any)[Symbol.for("altimate.memory-sync.state")]
410+
expect(state?.mirrorsInFlight?.size).toBe(1)
411+
net.release()
412+
await mirror
413+
expect(state.mirrorsInFlight.size).toBe(0)
414+
} finally {
415+
net.release()
416+
await mirror
417+
net.restore()
406418
}
407419
})
408420

409421
test("flushPendingMirrors gives up after its bound rather than hanging exit forever", async () => {
410-
// Gated, not hung forever: `mirrorsInFlight` is module-level, and a mirror that
422+
// Gated, not hung forever: `mirrorsInFlight` is process-global, and a mirror that
411423
// never settles would make every later default-bound flush in this process wait
412424
// the full 30s. (bot review)
413-
let release!: () => void
414-
const gate = new Promise<void>((r) => (release = r))
415-
const original = globalThis.fetch
416-
globalThis.fetch = (async (input: any, init?: any) => {
417-
await gate
418-
return original(input, init)
419-
}) as unknown as typeof fetch
425+
const net = gatedFetch()
420426
createResult = [{ id: "mem-hung" }]
421427
const mirror = mirrorBlock(block({ id: "hung" }))
422428
try {
423-
await Bun.sleep(20)
429+
await net.firstRequest
424430
const started = Date.now()
425431
await flushPendingMirrors(100)
426432
const waited = Date.now() - started
427433
expect(waited).toBeGreaterThanOrEqual(90)
428434
expect(waited).toBeLessThan(1000)
429435
} finally {
430-
release()
436+
net.release()
431437
await mirror
432-
globalThis.fetch = original
438+
net.restore()
433439
}
434440
})
435441

@@ -443,30 +449,24 @@ describe("mirrorBlock", () => {
443449
listResponse = [
444450
{ id: "mem-archive-late", memory: b.content, metadata: { source: MIRROR_SOURCE, block_id: "to-archive-late", block_scope: "global" } },
445451
]
446-
let release!: () => void
447-
const gate = new Promise<void>((r) => (release = r))
448-
const original = globalThis.fetch
449-
globalThis.fetch = (async (input: any, init?: any) => {
450-
await gate
451-
return original(input, init)
452-
}) as unknown as typeof fetch
452+
const net = gatedFetch()
453453
let settled = false
454454
const archive = archiveBlock("global", "to-archive-late").then(() => (settled = true))
455455
try {
456-
await Bun.sleep(20)
456+
await net.firstRequest
457457
expect(settled).toBe(false)
458458
const flush = flushPendingMirrors()
459459
let flushed = false
460460
void flush.then(() => (flushed = true))
461461
await Bun.sleep(20)
462462
expect(flushed).toBe(false) // the flush is holding for the archive
463-
release()
463+
net.release()
464464
await flush
465465
expect(settled).toBe(true)
466466
} finally {
467-
release()
467+
net.release()
468468
await archive
469-
globalThis.fetch = original
469+
net.restore()
470470
}
471471
})
472472

‎packages/opencode/test/cli/run-accounting.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ describe("run command request/stream lifecycle contracts", () => {
514514
expect(source).toMatch(
515515
// Each flush must be AWAITED: a `void import(...)` keeps the same tokens and
516516
// brings back the lost-upload race. (bot review)
517-
/if \(CoreFlag\.ALTIMATE_WORKSPACE\) \{[\s\S]{0,400}?await import\("\.\.\/\.\.\/altimate\/workspace\/skill-sync"\)\s*\.then\(\(m\) => m\.flushPendingSyncs\(\)\)[\s\S]{0,600}?await import\("\.\.\/\.\.\/altimate\/workspace\/memory-sync"\)\s*\.then\(\(m\) => m\.flushPendingMirrors\(\)\)/,
517+
/if \(CoreFlag\.ALTIMATE_WORKSPACE\) \{[\s\S]{0,400}?await Promise\.all\(\[\s*import\("\.\.\/\.\.\/altimate\/workspace\/skill-sync"\)\s*\.then\(\(m\) => m\.flushPendingSyncs\(\)\)[\s\S]{0,200}?import\("\.\.\/\.\.\/altimate\/workspace\/memory-sync"\)\s*\.then\(\(m\) => m\.flushPendingMirrors\(\)\)[\s\S]{0,100}?\]\)/,
518518
)
519519
})
520520

0 commit comments

Comments
 (0)