fix: match workspace memory and link drift across a repo's URL forms
Issue for this PR
Closes #1459
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
The workspace service stores one value per repo on github.com, gitlab.com and bitbucket.org, https://<host>/<path>, whatever form the clone used. Bindings linked earlier keep the form they were linked with, such as ssh://git@github.com/acme/x. The client compared remotes as raw strings, so one repo read as two projects:
- the link dialog reported drift for an SSH or
.gitremote that hadn't changed; - memory records written under one form stopped matching a binding held in another. Sync then created duplicates, and the user's own records showed as from a sibling project.
This PR ports the service's rule (canonicalization, then host unification) to remote-identity.ts. On those three hosts, record matching, the sibling label and both drift checks now compare remotes through it. Other remotes and paths still compare exactly, and nothing new is written: records and the local index keep the cached value.
The duplicates the bug already wrote now match the block too, so this PR also keeps one live record per block. A save keeps one record and archives the other copies, a delete archives every copy, and a load serves the newest.
Claims (for review)
- C1: On those three hosts, a project record under any URL form of the binding's repo is the same project. Sync updates it instead of creating a second record, delete and reap find it, and it isn't labelled as from a sibling.
- C2: The drift check, online and offline, reports no drift when the local remote as git prints it names the stored repo. A different repo is still drift.
- C3: The port returns the service's output on every parity vector: 241 rows generated by running the service's own functions under the Python version it runs. Off those hosts, memory compares remotes exactly, as before. The drift check also accepts the service's canonical form of the local remote, which is the value the service stores there.
- C4: Paths compare exactly and never equal a remote. A missing identity matches nothing, as before.
- C5: When a block has more than one live record:
- A save that changes the block updates the record the index names, or else the newest, and archives the rest. They are emptied and get the kept record's metadata, so any client that reads one as the block's tombstone also reads the kept record as live. The save is indexed only after the archives land, so a failed archive is retried by the next save.
- A save defers when any copy in the listing is newer than the local block.
- A delete archives every live copy in the listing.
- A load serves the newest copy of each block (scope, id and, for a sibling's block, its project), and applies the TTL to that copy.
- C6: Where the local index doesn't name a record, sync, delete and reap decide whether it is this project's by one rule, and display uses the same rule. A record the index names is the block's record, as on main. A record names its project by
repo_remote, or else byproject_path, and an empty value counts as none. A record that names no project belongs to every project in its workspace, and one whose value isn't a string belongs to none.
Residuals:
- R1: A cached remote is either the service's value or the remote as git printed it, and
x.gitnames a different repo in each. So on those three hosts the key drops every trailing.git, and a Bitbucket repo namedx.gitcompares equal tox. Bitbucket itself resolvesx.gitURLs tox, and GitHub and GitLab don't allow such names. Other hosts get no key. - R2: Python and the JS engine use different Unicode versions for lowercasing and NFKC, so characters added in between could compare differently. Every vector matches.
- R3: After the service moves a binding to another form, each block's first sync misses the local index and finds the record by identity, then updates it. That's one extra read per block.
- R4:
publicHostRepo, the body of the service's_unify_public_host, has a cyclomatic complexity of 15. It is kept line for line with the service's function so the two can be compared. - R5: Off the three hosts, memory still treats a record written under the remote as git printed it (
git@git.acme.internal:data/x.git) and the service's canonical form of it (ssh://git@git.acme.internal/data/x) as different projects. The drift check can take that canonical step because it knows which side is the service's. In memory either side may be, and canonicalizing the service's value again would match repox.gitwithx(R1). This is unchanged from main. - R6: A URL-form rewrite of a binding still counts as a relink (
sameBindingcompares exactly). That costs one backfill, which now updates records instead of duplicating them, plus one overlay reload. The backfill is also what re-indexes the blocks under the new cached value. Index entries under the old value are left behind, as on main. - R7: The reaper now acts on a tombstone written under an older URL form of this repo, so a block deleted elsewhere before the rewrite is removed here. This is intended, and worth a line in the release notes.
- R8: A block's copies stay live in the cloud until a client with this change next saves new content for the block. A save of unchanged content returns before reading the records, as on main. Meanwhile a load serves the newest copy. A client without this change, cached in the other form, can write a new copy on its next save, and the next changed save from an updated client archives that copy.
- R9: On a listing cut at the service limit, a save or a delete sees only the copies in reach.
- A save updates the record it finds, checks only the visible copies for a newer one, and archives only those. Main also updates on such a listing and refuses only creates. Deferring every save there would stop sync for any workspace at the limit.
- A delete archives the visible copies and logs that another may be out of reach. Refusing would leave the visible copies live too.
How did you verify your code works?
- Revert checks:
- With
memory-sync.tsat main, the 7 URL-form tests fail, and an 8th through the changedtoBlocksignature. - On
fe337e9cc2, 15 of the 20 tests added for copies and record keys fail. The 5 that pass cover cases that head already handled: no key, a null or undefined key, another URL form, and a delete on a truncated listing.
- With
- Mutations:
- Each of 9 mutations of the authority parser fails a parity test: the bracket, delimiter and control-character refusals, NFKC, port 0, empty port, port range, and the dropped user part.
- Each of 4 mutations of the key fails a test: keeping
.git, keying every host, no canonical form in the drift check, and no exact match. - Each of 17 mutations of the copy handling and the record rule fails a test, including indexing a save before its duplicates are archived, on both the copy and the split-create path. Among them: no archive, guard on the kept record only, first copy instead of newest, archived copies keeping their own metadata, a delete archiving one copy, no load dedupe, a dedupe that ignores the sibling's project, the TTL checked before the dedupe, an empty string read as a key, a non-string remote falling back to the path, and a group key without namespaces.
- Test runs:
bun test test/altimate/workspace: 1203 pass, on the merge with main atba98c93a27.- Merged with main's workspace sync state. Each memory load still serves the newest record per copy key, and the sync-state items are the records it served, so two live records of one block are one item. Main's test that listed two global records with one block id as two items now lists two projects' blocks with one id (two items), and a new test covers two copies of one block (one item, unchanged once the older copy is archived). Keying the item by the older copy fails it.
- The three other suites that touch memory sync (
overlay-merge,run-accounting,release-v0.9.7-adversarial): 89 pass. The TUI workspace plugin suites: 112 pass. tsgotypecheck is clean, and the marker guard is ok.
- Fixture: the 12 new rows were generated the same way, under Python 3.10.22. The 229 existing rows came out byte-identical.
- Live run against the hosted workspace service, for a clone with an SSH remote whose link holds the https value (results). On
main, the link dialog reports drift, and two live copies of a block load as two blocks, one labelled as another project's. On this branch there's no drift, the copies load as one block (the newest), and the next save updates the record its index names and archives the other copy.
Screenshots / recordings
N/A, no UI change beyond the drift check's verdict.
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
Appendix โ complexity delta
9281c2229a (main) โ ba98c93a27: 17 functions touched, 4 improved. loadWorkspaceMemory rose from cognitive 20 to 23 with the per-copy dedupe. push fell from cognitive 18 to 15 and CCN 23 to 17, because the duplicate archive moved into archiveDuplicates, which the split create now shares. toBlock and mirrorIdentityOf fell, and archiveNow rose 6 โ 9 (it archives every copy and logs a truncated listing). Among the new functions, publicHostRepo scores cognitive 12 and CCN 15 (R4). The CCN of 37 the tool gives the three-line blockUpdatedOf, and the cognitive 14 it gives the one-line isAscii, are boundary misreads: in each case it counts the code that follows.
๐ค Generated with Claude Code
https://claude.ai/code/session_01LmLXkY5NUqPL7nH2exe9zb
Summary by cubic
Fixes #1459: workspace memory and the link drift check treated different URL forms of one repo (ssh://, .git, www., case) on github.com, gitlab.com, and bitbucket.org as different projects, causing duplicated records and false drift warnings.
Changes
- Ports the service's canonicalization and host-unification rule to
remote-identity.ts; record matching, sibling labeling, and both drift checks now compare remotes through it. Paths still compare exactly. - Only the three public hosts unify a repo's URL forms. Self-hosted remotes compare exactly, so
X.gitandXstay distinct there; the link dialog's drift check also accepts the service's stored form of a self-hosted remote. - Sync updates a record instead of creating a new one when binding and record differ only by URL form; the link dialog no longer flags an unchanged remote. A record whose remote key isn't a string matches no project, falsy or not.
- A save is indexed only after its duplicate copies are archived, so a failed archive is retried on the next save; a delete on a listing cut at the service limit still archives the copies in reach.
- Parity-tested against 241 vectors generated by the service itself, under the Python version it runs.
- One known edge: on the public hosts, a repo literally named
x.gitcompares equal tox, matching existing service storage behavior.
Summary by CodeRabbit
- Bug Fixes
- Workspace projects now match consistently across equivalent public repository URL formats, while distinct repositories and self-hosted remotes remain separate.
- Memory updates preserve newer changes and archive duplicate copies consistently, including when removing memories.
- Workspace memories now load the newest matching copy and apply expiration and cleanup consistently across projects.
- Workspace binding drift checks now account for normalized remote identifiers while continuing to compare project paths exactly.