Skip to content

Commit dd48f2b

Browse files
fix: index a save only after its duplicate records are archived
An index entry at the block's hash makes the next save of the same content return "unchanged". Written before the archive, a failed archive of a copy or of a split create's extra was never retried. Both paths now index last, so the next save finds the duplicates by identity and archives them. A delete on a listing cut at the service limit still archives the copies in reach, and now logs that another copy may be out of reach. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01LmLXkY5NUqPL7nH2exe9zb
1 parent 44b7750 commit dd48f2b

2 files changed

Lines changed: 83 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,12 @@ async function push(
778778
return "deferred"
779779
}
780780
await MemoryApi.update(match, block.content, metadata)
781-
await recordIndexEntry(key, { memoryId: match, contentHash: hash, syncedAt: Date.now() })
782-
// One live record per block.
781+
// One live record per block. Indexed only after the copies are archived:
782+
// an entry at this hash makes the next save of the same content return
783+
// "unchanged", and a failed archive would then never be retried.
783784
const others = copies.filter((r) => r.id !== match).map((r) => r.id)
784785
await archiveDuplicates(others, block, metadata, "archiving a second live record of one block")
786+
await recordIndexEntry(key, { memoryId: match, contentHash: hash, syncedAt: Date.now() })
785787
return "stored"
786788
}
787789

@@ -806,13 +808,14 @@ async function push(
806808
// dict, restoring the block exactly as written.
807809
const [primary, ...extras] = created
808810
await MemoryApi.update(primary, block.content, metadata)
809-
await recordIndexEntry(key, { memoryId: primary, contentHash: hash, syncedAt: Date.now() })
810811

811812
// An extractor may split one submission into several records, each carrying
812813
// the metadata we sent. Only one can represent the block; the rest would
813814
// otherwise be injected as duplicates under the same block id, holding text
814-
// the user never wrote.
815+
// the user never wrote. Indexed after, as for copies above: a failed archive
816+
// leaves the block unindexed, and the next save finds the extras by identity.
815817
await archiveDuplicates(extras, block, metadata, "archiving an extra record produced by one create")
818+
await recordIndexEntry(key, { memoryId: primary, contentHash: hash, syncedAt: Date.now() })
816819
return "stored"
817820
}
818821

@@ -1032,6 +1035,17 @@ async function archiveNow(
10321035
// what it said.
10331036
await MemoryApi.update(current.id, current.memory ?? "", metadata)
10341037
}
1038+
// A cut-short listing can hold one copy of the block and not another.
1039+
// Archiving the copies in reach is still right; refusing would leave them
1040+
// live too.
1041+
if (truncated) {
1042+
log.warn("archived the copies in reach; the record set is truncated at LIST_LIMIT, so another may still be live", {
1043+
blockId,
1044+
scope,
1045+
archived: live.length,
1046+
limit: LIST_LIMIT,
1047+
})
1048+
}
10351049
// Written only after the remote archives land, so a failure leaves the entry
10361050
// pointing at a record that is still live rather than losing track of it.
10371051
const kept = live.find((r) => r.id === indexed?.id) ?? live[0]

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ let createResult: any = [{ id: "mem-new" }]
8181
let workspaces: any[] = [{ id: 42, name: "acme", memory_enabled: true }]
8282
/** When set, GET /list fails -- used to prove a failed lookup never creates. */
8383
let listFails = false
84+
/** When set, a PATCH to this record id fails. */
85+
let failPatchOf: string | undefined
8486

8587
function stubFetch() {
8688
globalThis.fetch = (async (input: any, init?: any) => {
@@ -90,6 +92,9 @@ function stubFetch() {
9092
if (listFails && url.includes("/datamates/memory/list")) {
9193
return new Response(JSON.stringify({ detail: "boom" }), { status: 500 })
9294
}
95+
if (failPatchOf && method === "PATCH" && url.endsWith(`/datamates/memory/${failPatchOf}`)) {
96+
return new Response(JSON.stringify({ detail: "boom" }), { status: 500 })
97+
}
9398
const payload = (() => {
9499
// Server-side binding lookup, used when no local cache entry exists.
95100
if (url.includes("/datamate-project-bindings/by-")) {
@@ -164,6 +169,7 @@ beforeEach(() => {
164169
captured = []
165170
listResponse = []
166171
listFails = false
172+
failPatchOf = undefined
167173
createResult = [{ id: "mem-new" }]
168174
workspaces = [{ id: 42, name: "acme", memory_enabled: true }]
169175
serverBinding = null
@@ -2326,6 +2332,51 @@ describe("two live records of one block", () => {
23262332
expect(callsTo("/datamates/memory/", "POST").length).toBe(0)
23272333
})
23282334

2335+
test("a copy whose archive failed is archived by the next save of the same content", async () => {
2336+
boundTo({ repoRemote: HTTPS })
2337+
listResponse = [
2338+
copy("mem-retry-ssh", "copies-retry", { repo_remote: SSH, block_updated: OLDER }),
2339+
copy("mem-retry-https", "copies-retry", { repo_remote: HTTPS, block_updated: NEWER }),
2340+
]
2341+
failPatchOf = "mem-retry-ssh"
2342+
const b = edited("copies-retry")
2343+
await mirrorBlock(b).catch(() => undefined)
2344+
expect(callsTo("/datamates/memory/mem-retry-ssh", "PATCH").length).toBe(1)
2345+
2346+
failPatchOf = undefined
2347+
captured = []
2348+
await mirrorBlock(b)
2349+
const archived = callsTo("/datamates/memory/mem-retry-ssh", "PATCH")
2350+
expect(archived.length).toBe(1)
2351+
expect(archived[0].body.metadata.archived).toBe("true")
2352+
})
2353+
2354+
test("a split create's extra whose archive failed is archived by the next save", async () => {
2355+
boundTo({ repoRemote: HTTPS })
2356+
createResult = [{ id: "mem-split-primary" }, { id: "mem-split-extra" }]
2357+
failPatchOf = "mem-split-extra"
2358+
const b = edited("copies-split")
2359+
await mirrorBlock(b).catch(() => undefined)
2360+
expect(callsTo("/datamates/memory/mem-split-extra", "PATCH").length).toBe(1)
2361+
// What the service now holds: both records under the metadata the create
2362+
// sent, the primary's rewritten by its repair update.
2363+
const sent = callsTo("/datamates/memory/", "POST")[0].body.memory_options.metadata
2364+
const repair = callsTo("/datamates/memory/mem-split-primary", "PATCH")[0].body
2365+
listResponse = [
2366+
{ id: "mem-split-primary", memory: repair.memory, metadata: repair.metadata },
2367+
{ id: "mem-split-extra", memory: "rewritten by the extractor", metadata: sent },
2368+
]
2369+
2370+
failPatchOf = undefined
2371+
captured = []
2372+
await mirrorBlock(b)
2373+
expect(callsTo("/datamates/memory/", "POST").length).toBe(0)
2374+
const patched = ["mem-split-primary", "mem-split-extra"].map(
2375+
(id) => callsTo(`/datamates/memory/${id}`, "PATCH")[0]?.body.metadata.archived,
2376+
)
2377+
expect(patched.sort()).toEqual(["true", undefined].sort())
2378+
})
2379+
23292380
test("a delete archives every live copy", async () => {
23302381
boundTo({ repoRemote: HTTPS })
23312382
listResponse = [
@@ -2346,6 +2397,20 @@ describe("two live records of one block", () => {
23462397
expect(callsTo("/datamates/memory/mem-del-other", "PATCH").length).toBe(0)
23472398
})
23482399

2400+
test("a delete on a truncated listing archives the copies in reach", async () => {
2401+
boundTo({ repoRemote: HTTPS })
2402+
listResponse = [
2403+
copy("mem-trunc-ssh", "copies-trunc", { repo_remote: SSH, block_updated: OLDER }),
2404+
...Array.from({ length: LIST_LIMIT - 1 }, (_, i) => ({
2405+
id: `trunc-filler-${i}`,
2406+
memory: "x",
2407+
metadata: { source: MIRROR_SOURCE, block_id: `filler/${i}`, block_scope: "global" },
2408+
})),
2409+
]
2410+
await archiveBlock("project", "copies-trunc")
2411+
expect(callsTo("/datamates/memory/mem-trunc-ssh", "PATCH")[0].body.metadata.archived).toBe("true")
2412+
})
2413+
23492414
test("a delete whose indexed record is already archived still archives a live copy", async () => {
23502415
boundTo({ repoRemote: HTTPS })
23512416
await recordIndexEntry(

0 commit comments

Comments
 (0)