Skip to content

Commit 7d2e594

Browse files
fix: keep one live record per block when an older URL form left a copy
Clients that compared remotes as raw strings wrote a second live record for a block once the binding's cached form of the repo changed. Both records now match the block, so: - `push` updates the record the index names, or else the newest copy. It defers when any copy is newer than the local block, and archives the other copies the way a split create's extras are archived: emptied, under this push's metadata. - A delete archives every live copy, including when the indexed record is already a tombstone. - A load serves the newest copy of each block (scope, id and, for a sibling, its project), and applies the TTL to that copy. Sync and display now read a record's project through one rule, `recordInProject`: an empty value counts as none, a record that names no project belongs to every project, and a non-string value belongs to none. `projectIdentityKey` groups records the way `sameProjectIdentity` compares them. Also: hoist the `TextEncoder`, mark `unifyPublicHost` internal, document the one-directional canonical step in `sameIdentifier`, and add 12 parity rows (queries, fragments, extra path segments, ports) generated under Python 3.10.22. The 229 existing rows are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01LmLXkY5NUqPL7nH2exe9zb
1 parent fe337e9 commit 7d2e594

5 files changed

Lines changed: 477 additions & 89 deletions

File tree

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

Lines changed: 127 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { TRAINING_META_COMMENT } from "@/altimate/training/types"
2626
// Aliased: `syncInternals.resolveBinding` below is an unrelated test seam.
2727
import { canonicalDirectory, onBindingChanged, resolveBinding as resolveProjectBinding, type CachedBinding } from "./state"
2828
import { indexKey, readIndex, readIndexEntry, recordIndexEntry } from "./memory-index"
29-
import { projectIdentity, sameProjectIdentity, type ProjectIdentity } from "./remote-identity"
29+
import { projectIdentity, projectIdentityKey, sameProjectIdentity, type ProjectIdentity } from "./remote-identity"
3030
import { WorkspaceApi } from "./api-client"
3131
import { AltimateApi } from "@/altimate/api/client"
3232
import {
@@ -151,8 +151,35 @@ export function projectIdentityFor(binding: CachedBinding): ProjectIdentity | un
151151
return projectIdentity(binding.repoRemote, binding.projectPath)
152152
}
153153

154-
function recordProjectIdentity(meta: Record<string, unknown>): ProjectIdentity | undefined {
155-
return projectIdentity(meta.repo_remote, meta.project_path)
154+
/** The project a record's metadata names: its `repo_remote`, or its
155+
* `project_path` when it has no remote, an empty value counting as none, as
156+
* `buildMetadata` writes them. `"none"` when it names neither; `"invalid"` when
157+
* the value it names isn't a string. */
158+
function recordProject(meta: Record<string, unknown>): ProjectIdentity | "none" | "invalid" {
159+
const named = (value: unknown) => value != null && value !== ""
160+
if (named(meta.repo_remote))
161+
return typeof meta.repo_remote === "string" ? { kind: "remote", value: meta.repo_remote } : "invalid"
162+
if (named(meta.project_path))
163+
return typeof meta.project_path === "string" ? { kind: "path", value: meta.project_path } : "invalid"
164+
return "none"
165+
}
166+
167+
/** Does a record belong to this project? The one rule for sync and the reaper
168+
* (`mirrorIdentityOf`) and for display (`toBlock`).
169+
*
170+
* A record that names no project belongs to every project in its workspace:
171+
* `buildMetadata` writes one for a binding with neither field. One whose value
172+
* isn't a string belongs to none. */
173+
function recordInProject(meta: Record<string, unknown>, own: ProjectIdentity | undefined): boolean {
174+
const project = recordProject(meta)
175+
if (project === "none") return true
176+
return project !== "invalid" && sameProjectIdentity(project, own)
177+
}
178+
179+
/** A record's `block_updated`, or "" when it has none. */
180+
function blockUpdatedOf(record: CloudMemoryRecord | undefined): string {
181+
const value = (record?.metadata ?? {}).block_updated
182+
return typeof value === "string" ? value : ""
156183
}
157184

158185
/** ``directory`` is the tree that owned the operation. Fire-and-forget mirrors
@@ -429,15 +456,7 @@ function mirrorIdentityOf(record: CloudMemoryRecord, binding: CachedBinding | nu
429456
if (!blockId || !scope) return undefined
430457
if (scope === "project") {
431458
if (String(m.datamate_id ?? "") !== String(binding?.datamateId ?? "")) return undefined
432-
// The record's key as written. One that isn't a string, falsy or not, names
433-
// no project of ours, so it never matches.
434-
const recordKey = m.repo_remote ?? m.project_path
435-
if (
436-
recordKey != null &&
437-
binding &&
438-
!(typeof recordKey === "string" && sameProjectIdentity(recordProjectIdentity(m), projectIdentityFor(binding)))
439-
)
440-
return undefined
459+
if (binding && !recordInProject(m, projectIdentityFor(binding))) return undefined
441460
}
442461
return `${scope}:${blockId}`
443462
}
@@ -728,16 +747,27 @@ async function push(
728747
// that already has one — the duplicate this function works hardest to avoid.
729748
const indexedGone = existing?.memoryId !== undefined && indexed === undefined && !view.truncated
730749
const indexUsable = existing?.memoryId && !indexedGone && (!indexed || !isArchived(indexed))
731-
const match = (indexUsable ? existing?.memoryId : undefined) ?? view.records.find((r) => isSameBlock(r, block, binding))?.id
750+
// Every live record holding this block. More than one are copies: a client
751+
// that compared remotes as raw strings wrote a second record once its
752+
// binding's cached form of the repo changed.
753+
const copies = view.records.filter((r) => isSameBlock(r, block, binding))
754+
const newest = copies.reduce<CloudMemoryRecord | undefined>(
755+
(best, r) => (best && blockUpdatedOf(best) >= blockUpdatedOf(r) ? best : r),
756+
undefined,
757+
)
758+
const match = (indexUsable ? existing?.memoryId : undefined) ?? newest?.id
732759
if (match) {
733760
// Refuse to move a record backwards. Two machines editing the same block,
734761
// or a stale clone running a sweep, would otherwise overwrite a newer cloud
735762
// value with older local content — last-request-wins rather than
736763
// convergence. This narrows the window rather than closing it; closing it
737-
// needs a conditional update the service does not offer.
738-
const remote = view.records.find((r) => r.id === match)
739-
const remoteUpdated = remote && (remote.metadata ?? {}).block_updated
740-
if (typeof remoteUpdated === "string" && remoteUpdated > block.updated) {
764+
// needs a conditional update the service does not offer. Checked against
765+
// the newest copy too: the other copies are archived below, so a newer
766+
// one's content would be lost.
767+
const remoteUpdated = [view.records.find((r) => r.id === match), newest]
768+
.map(blockUpdatedOf)
769+
.reduce((a, b) => (a > b ? a : b))
770+
if (remoteUpdated > block.updated) {
741771
log.warn("declining to overwrite a newer workspace record with older local content", {
742772
id: block.id,
743773
localUpdated: block.updated,
@@ -747,6 +777,15 @@ async function push(
747777
}
748778
await MemoryApi.update(match, block.content, metadata)
749779
await recordIndexEntry(key, { memoryId: match, contentHash: hash, syncedAt: Date.now() })
780+
// One live record per block. The other copies are archived as a split
781+
// create's extras are below: emptied, under this push's metadata, so any
782+
// client that reads one as this block's tombstone also reads `match` as
783+
// its live record and keeps the block.
784+
for (const copy of copies) {
785+
if (copy.id === match) continue
786+
log.warn("archiving a second live record of one block", { id: block.id, memoryId: copy.id })
787+
await MemoryApi.update(copy.id, "", { ...metadata, archived: "true", archived_at: new Date().toISOString() })
788+
}
750789
return "stored"
751790
}
752791

@@ -918,8 +957,8 @@ async function archiveNow(
918957

919958
// Read through ``fetchKnownRecords`` so a truncated response is detectable
920959
// — a workspace with >= LIST_LIMIT records that includes the target block
921-
// beyond the window would silently fall through the ``!current`` branch and
922-
// the delete would no-op. The block then stays live in the cloud and gets
960+
// beyond the window would silently fall through the no-live-record branch
961+
// and the delete would no-op. The block then stays live in the cloud and gets
923962
// re-injected on every session with no diagnostic trace. (altimate-harness-
924963
// bot #1116 comment 3841102064.)
925964
const { records, truncated } = await fetchKnownRecords()
@@ -929,16 +968,35 @@ async function archiveNow(
929968
// that has lost its index leaves the record live, and every later session
930969
// re-injects it with no way to remove it. Match on logical identity instead,
931970
// exactly as the write path does.
932-
const current = entry
933-
? records.find((r) => r.id === entry.memoryId)
934-
: // Matching on workspace alone is too coarse: two projects in one
935-
// workspace may hold the same block id, and deleting one would archive
936-
// the other's record. Use the write path's own identity test so the
937-
// fallback can only ever hit the record this block would have written.
938-
records.find((r) =>
939-
isSameBlock(r, { id: blockId, scope, tags: [], content: "", created: "", updated: "" } as MemoryBlock, binding),
940-
)
941-
if (!current) {
971+
//
972+
// Every live record of the block, not just one: a block can hold copies (see
973+
// `push`), and archiving one would leave the block live through the others.
974+
// Matching on workspace alone is too coarse: two projects in one workspace
975+
// may hold the same block id, and deleting one would archive the other's
976+
// record. Use the write path's own identity test so the search can only ever
977+
// hit records this block would have written.
978+
const indexed = entry ? records.find((r) => r.id === entry.memoryId) : undefined
979+
const target = { id: blockId, scope, tags: [], content: "", created: "", updated: "" } as MemoryBlock
980+
const live = records.filter((r) => !isArchived(r) && (r.id === indexed?.id || isSameBlock(r, target, binding)))
981+
if (live.length === 0) {
982+
// The record the index names is already a tombstone: leave it exactly as
983+
// it is.
984+
//
985+
// Not merely a saved request. `archived_at` is read as the moment the record
986+
// stopped being wanted, and the reaper on another machine compares a local
987+
// block's `updated` against it. Re-stamping it to now moves that moment
988+
// forward, so a block legitimately recreated AFTER the original archive
989+
// starts looking older than the tombstone and gets deleted — on every
990+
// machine, taking the recreated record with it, because this function then
991+
// follows the index to whatever the block points at now. (review)
992+
if (indexed) {
993+
log.info("record is already archived; leaving its tombstone untouched", {
994+
blockId,
995+
scope,
996+
memoryId: indexed.id,
997+
})
998+
return
999+
}
9421000
if (truncated) {
9431001
// Distinguish "record isn't there" (silent no-op is correct) from
9441002
// "record is beyond the LIST_LIMIT window" (silent no-op leaves a
@@ -951,44 +1009,25 @@ async function archiveNow(
9511009
return
9521010
}
9531011

954-
// Already a tombstone: leave it exactly as it is.
955-
//
956-
// Not merely a saved request. `archived_at` is read as the moment the record
957-
// stopped being wanted, and the reaper on another machine compares a local
958-
// block's `updated` against it. Re-stamping it to now moves that moment
959-
// forward, so a block legitimately recreated AFTER the original archive
960-
// starts looking older than the tombstone and gets deleted — on every
961-
// machine, taking the recreated record with it, because this function then
962-
// follows the index to whatever the block points at now.
963-
//
964-
// The identity search below already skips archived records, so a caller with
965-
// no index entry lands here anyway. This closes the path a caller WITH one
966-
// takes. (review)
967-
if (isArchived(current)) {
968-
log.info("record is already archived; leaving its tombstone untouched", {
969-
blockId,
970-
scope,
971-
memoryId: current.id,
972-
})
973-
return
974-
}
975-
9761012
const now = new Date().toISOString()
977-
const metadata: MirrorMetadata = {
978-
...((current.metadata ?? {}) as unknown as MirrorMetadata),
979-
source: MIRROR_SOURCE,
980-
block_id: blockId,
981-
block_scope: scope,
982-
visibility: "private",
983-
archived: "true",
984-
archived_at: now,
1013+
for (const current of live) {
1014+
const metadata: MirrorMetadata = {
1015+
...((current.metadata ?? {}) as unknown as MirrorMetadata),
1016+
source: MIRROR_SOURCE,
1017+
block_id: blockId,
1018+
block_scope: scope,
1019+
visibility: "private",
1020+
archived: "true",
1021+
archived_at: now,
1022+
}
1023+
// Keep the text — archiving hides a record from injection, it does not erase
1024+
// what it said.
1025+
await MemoryApi.update(current.id, current.memory ?? "", metadata)
9851026
}
986-
// Keep the text — archiving hides a record from injection, it does not erase
987-
// what it said.
988-
await MemoryApi.update(current.id, current.memory ?? "", metadata)
989-
// Written only after the remote archive lands, so a failure leaves the entry
1027+
// Written only after the remote archives land, so a failure leaves the entry
9901028
// pointing at a record that is still live rather than losing track of it.
991-
await recordIndexEntry(key, { memoryId: current.id, contentHash: "", syncedAt: Date.now() })
1029+
const kept = live.find((r) => r.id === indexed?.id) ?? live[0]
1030+
await recordIndexEntry(key, { memoryId: kept.id, contentHash: "", syncedAt: Date.now() })
9921031
}
9931032

9941033
async function runQueue<T>(
@@ -1292,8 +1331,7 @@ export function toBlock(record: CloudMemoryRecord, ownProject: ProjectIdentity |
12921331
record.updated_at ??
12931332
new Date().toISOString()
12941333

1295-
const recordProject = recordProjectIdentity(meta)
1296-
const fromSibling = scope === "project" && !!recordProject?.value && !sameProjectIdentity(recordProject, ownProject)
1334+
const fromSibling = scope === "project" && !recordInProject(meta, ownProject)
12971335

12981336
return {
12991337
id: blockId,
@@ -1308,6 +1346,17 @@ export function toBlock(record: CloudMemoryRecord, ownProject: ProjectIdentity |
13081346
}
13091347
}
13101348

1349+
/** Which block a loaded record holds: its scope and id, and for a sibling
1350+
* project's block, that project. Records with one key are copies of one block. */
1351+
function copyKey(record: CloudMemoryRecord, block: RemoteMemoryBlock, ownProject: ProjectIdentity | undefined): string {
1352+
const id = `${block.scope}:${block.id}`
1353+
const meta = record.metadata ?? {}
1354+
if (block.scope === "global" || recordInProject(meta, ownProject)) return id
1355+
const project = recordProject(meta)
1356+
// A sibling's record whose project isn't a string can't be paired with another.
1357+
return typeof project === "object" ? `${id}\u0000${projectIdentityKey(project)}` : `${id}\u0000record:${record.id}`
1358+
}
1359+
13111360
/** Does this record apply to the workspace the current project is bound to?
13121361
*
13131362
* Project blocks apply to their workspace regardless of which project wrote
@@ -1444,18 +1493,25 @@ async function loadWorkspaceMemory(directory?: string): Promise<LoadOutcome> {
14441493
log.warn("could not reap archived blocks", { err: String(err) }),
14451494
)
14461495

1447-
const blocks: RemoteMemoryBlock[] = []
1496+
// One block per copy key. A block can hold several live records (see
1497+
// `push`), and serving each would inject it twice, one copy stale; the
1498+
// newest is served, as `push` keeps it on the block's next save.
1499+
const newest = new Map<string, RemoteMemoryBlock>()
14481500
for (const record of records) {
14491501
if (!record?.id) continue
14501502
if (!isMirrorRecord(record) || isArchived(record)) continue
14511503
if (!belongsHere(record, ownWorkspace)) continue
14521504
const block = toBlock(record, ownProject)
14531505
if (!block) continue
1454-
// A TTL'd block must expire everywhere, not just on the machine that
1455-
// wrote it. The cloud copy is not swept, so honour it on read.
1456-
if (block.expires && new Date(block.expires) <= new Date()) continue
1457-
blocks.push(block)
1506+
const key = copyKey(record, block, ownProject)
1507+
const prior = newest.get(key)
1508+
if (!prior || block.updated > prior.updated) newest.set(key, block)
14581509
}
1510+
// A TTL'd block must expire everywhere, not just on the machine that
1511+
// wrote it. The cloud copy is not swept, so honour it on read. Checked on
1512+
// the newest copy only: an older one without the TTL must not outlive it.
1513+
const now = new Date()
1514+
const blocks = [...newest.values()].filter((b) => !b.expires || new Date(b.expires) > now)
14591515
return { status: "loaded", blocks, epoch, dir }
14601516
} catch (err) {
14611517
log.warn("workspace memory load failed", { err: String(err) })

0 commit comments

Comments
 (0)