Skip to content

fix(partition): adopt pre-#546 legacy-named partitions instead of stranding them - #551

Merged
jeff-r2026 merged 3 commits into
mainfrom
feat/partition-slug-legacy-adoption
Sep 14, 2026
Merged

jeff-r2026 merged 3 commits into
mainfrom
feat/partition-slug-legacy-adoption

Conversation

@jeff-r2026

Copy link
Copy Markdown
Collaborator

Why

PR #546 widened the partition slug prefix from the anchor's basename to its whole path (teamai-cli-<hash> → Users-x-Project-teamai-cli-<hash>) but kept the sha256 suffix — without migrating existing installs. On an upgraded CLI, a partition still named <basename>-<hash> (every install created before #546) stops matching projectSlug(anchor), so:

  • detectProjectConfig finds no config → the project looks uninitialized
  • planMigration would re-copy a retired workspace into a second, empty partition
  • status --all flags the perfectly good data as corrupt — dir name does not match anchor

#546 argued no compatibility shim was needed because "no stable release has shipped this data layout." That is true for stable — but the partition layout has shipped in every 0.23.x/0.24.x beta since v0.23.0-beta.1, and any dev machine that ran a partitioned build has an old-format partition on disk. Those are silently stranded today.

What

Both name formats share the same hash, so an anchor's legacy name is computable exactly — no directory scanning. A new resolvePartitionDir(anchor) becomes the single seam for "the partition that actually holds this project's data" and every resolver (detection, init, migration) goes through it:

  • When the canonical (current-format) dir is absent and a legacy-named one exists → the legacy partition is atomically renamed into place (a same-parent metadata move — no data copied; an interruption leaves either name intact, never a half-moved dir).
  • An authoritative current-format partition is never clobbered by a leftover legacy one.
  • An empty canonical leftover (a crashed init's bare mkdir) is replaced by the full legacy partition (POSIX rename does this in one call; Windows takes the explicit rmdir-then-rename path).
  • A rename that is genuinely impossible (read-only home) keeps serving the legacy directory, so no data is stranded.
  • status --all stays read-only: it recognizes the legacy name via legacyProjectSlug and reports active (legacy name; renamed automatically on next command) instead of corrupt.

The authoritative reverse lookup remains the per-partition anchor file; the readable slug is a convenience only.

Test Plan

Type + build

  • npx tsc --noEmit — clean
  • npm run build — clean

Unit (new + extended)

  • legacyProjectSlug keeps the old <basename>-<hash> format and shares the hash with the current slug
  • resolvePartitionDir: fresh install (pure resolution, no mkdir) / adopts a legacy dir by rename / keeps an authoritative canonical & leaves a stale legacy alone / replaces an empty canonical leftover / idempotent after adoption
  • detectProjectConfig adopts a pre-feat(partition): encode the full project path into the data-home slug #546 legacy-named partition on detection
  • planMigration adopts before planning (retire-only, not a re-copy into a second empty partition)
  • status --all reports a legacy-named partition as active (legacy name), not corrupt
  • full suite: 3105 passed (224 files), 0 regressions

Real-CLI end-to-end (isolated HOME, dist/index.js, real git repo; slugs computed via the CLI's own projectSlug/legacyProjectSlug so case-folding matches production)

  • seed a pre-feat(partition): encode the full project path into the data-home slug #546 <basename>-<hash> partition (config + state + env.local + anchor)
  • status --all → [active (legacy name; renamed automatically on next command)], not corrupt, and does not rename (stays read-only)
  • running a command from the project cwd (detection) → partition renamed to the current whole-path slug
  • data intact after adoption: config.yaml, state.json, env.local (plaintext content preserved), anchor all carried over
  • idempotent: a second detection stays on the current name
  • status --all after adoption → [active]

Docs

…anding them

#546 widened the partition slug prefix from the anchor's basename to its
whole path but kept the sha256 suffix — without migrating existing
installs. On an upgraded CLI, a partition still named <basename>-<hash>
(every install created before #546) stops matching projectSlug(anchor):
detectProjectConfig finds no config (the project looks uninitialized),
planMigration would re-copy a retired workspace into a second, empty
partition, and status --all flags the perfectly good data as "corrupt —
dir name does not match anchor".

Both formats share the same hash, so an anchor's legacy name is
computable exactly — no directory scanning. resolvePartitionDir becomes
the seam for "the partition that actually holds this project's data"
(detection, init, migration): when the canonical current-format
directory is absent and a legacy-named one exists, it ATOMICALLY RENAMES
the legacy partition into place — a same-parent metadata move, no data
copied, and an interruption leaves either name intact. An authoritative
current-format partition is never clobbered by a leftover legacy one; a
rename that is genuinely impossible (read-only home) keeps serving the
legacy directory so no data is stranded. status --all stays read-only
and reports a legacy-named partition as "active (legacy name; renamed
automatically on next command)" instead of corrupt.

Verified end-to-end with the real CLI in an isolated HOME: a legacy
.teamai migrates into the readable whole-path slug; status --all
reverse-resolves it as [active]; a partition renamed to the pre-#546
format is reported active-legacy (no rename, no corrupt), then adopted
by the next command (detection) with data intact, and pull runs through
the adopted partition.
A pre-#546 partition stores repo.localPath as an ABSOLUTE path to its
team-repo clone (<legacyPartition>/team-repo). resolvePartitionDir renamed
the directory but left config.yaml pointing at the now-gone old path, so
every later `pull` read the team config from a dead directory and silently
skipped the sync ("Team config (teamai.yaml) not found. Skipping.", exit 0)
— the project could never sync again after an upgrade.

Adoption now rebases repo.localPath onto the new partition (mirroring
migrate.ts's rebaseConfigPaths). The rewrite is idempotent and self-healing:
a modern install whose localPath already sits in the canonical dir is left
untouched, an external clone outside the partition is left untouched, and an
adoption interrupted between the rename and the config rewrite is finished by
the next command.

Reproduced with the real CLI (isolated HOME, real local git team repo): a
legacy-named partition + two `pull --force` runs printed "Team config not
found. Skipping." (exit 0) before this fix; after it both runs print
"Synced 1 skills" and localPath points at the live clone.

Regression tests: localPath rebased off the legacy dir / external localPath
left alone / modern-install no-op. Verified they fail when the rebase is
disabled.
@jeff-r2026

Copy link
Copy Markdown
Collaborator Author

Confirmed and fixed in 5b1c2df. Thanks for the precise repro — this was a real P1 introduced by this PR, and my original e2e only exercised status, not pull, which is exactly why it slipped through.

Root cause: repo.localPath is persisted in config.yaml as an absolute path to the team-repo clone (<legacyPartition>/team-repo). resolvePartitionDir renamed the directory but left config.yaml pointing at the now-gone old path, so every later pull read the team config from a dead directory and silently skipped the sync (Team config (teamai.yaml) not found. Skipping., exit 0). migrate.ts already had rebaseConfigPaths for exactly this — the adoption path just failed to do the equivalent.

Fix: adoption now rebases repo.localPath onto the new partition after the rename. It is idempotent and self-healing:

  • a modern install whose localPath already sits in the canonical dir → left untouched;
  • an external clone outside the partition → left untouched;
  • an adoption interrupted between the rename and the config rewrite → finished by the next command.

Reproduced + verified with the real CLI (isolated HOME, real local git team repo, legacy-named partition, two pull --force):

  • before: both runs → Team config (teamai.yaml) not found. Skipping. (exit 0); localPath pointed at a gone path
  • after: both runs → ✔ Synced 1 skills (1 new); localPath points at the live clone under the new partition name

Regression tests (in partition.test.ts): localPath rebased off the legacy dir / external localPath left alone / modern-install no-op. Verified they fail when the rebase is disabled (no-op-test check). Full suite: 3108 passed, 0 regressions; tsc clean.

…runcation

The localPath rebase overwrote config.yaml with a plain (non-atomic) write.
By that point the legacy partition has already been renamed away, so
config.yaml is the partition's ONLY copy — a write that fails partway
(ENOSPC, EFBIG, crash mid-write) truncates it with no source to recover from,
and the CLI still prints "Your original data is unchanged, re-run to retry"
while the data is in fact corrupt and cannot self-recover.

Add writeFileAtomic (same-dir temp + rename, preserving mode) alongside the
existing writeJsonAtomic, and use it for the adopted config.yaml. rename(2)
is atomic, so a failed write removes the temp file and leaves the original
config.yaml byte-for-byte intact; the next command retries the idempotent
rebase and converges.

Reproduced with the real CLI (isolated HOME, real local git team repo, NO fs
mock) by injecting a write failure with RLIMIT_FSIZE=128:
  before: pull --force reports EFBIG, config.yaml truncated 453 -> 128 bytes,
          legacy partition already moved, retry after lifting the limit exits
          0 but never syncs and cannot recover
  after:  config.yaml preserved at 453 bytes, retry after lifting the limit
          prints "Synced 1 skills" and localPath points at the live clone

Regression test: the localPath rewrite failing mid-write leaves config.yaml
intact with no leftover temp file. Verified it fails when the write is made
non-atomic. Full suite: 3109 passed, 0 regressions; tsc clean.
@jeff-r2026

Copy link
Copy Markdown
Collaborator Author

Confirmed and fixed in 1efe512. Another real P1 — thank you, and the suggested fix (same-dir temp file + atomic rename, keep the original on failure) is exactly right.

Root cause: the localPath rebase from the previous commit overwrote config.yaml with a plain, non-atomic writeFile. By that point the legacy partition has already been renamed away, so config.yaml is the partition's only copy — a write that fails partway (ENOSPC / EFBIG / crash mid-write) truncates it with no source to recover from. Worse, the CLI still printed "Your original data is unchanged, re-run to retry" while the data was in fact corrupt and could not self-recover.

Fix: added writeFileAtomic (same-dir temp + rename, preserving mode) next to the existing writeJsonAtomic, and use it for the adopted config.yaml. rename(2) is atomic, so a failed write removes the temp file and leaves the original config.yaml byte-for-byte intact; the next command retries the idempotent rebase and converges.

Reproduced + verified with the real CLI (isolated HOME, real local git team repo, no fs mock) by injecting a write failure with RLIMIT_FSIZE=128:

  • before: pull --force → EFBIG; config.yaml truncated 453 → 128 bytes; legacy partition already moved; retry after lifting the limit → exit 0 but never syncs and cannot recover
  • after: config.yaml preserved at 453 bytes; retry after lifting the limit → ✔ Synced 1 skills, localPath points at the live clone

Regression test (partition.test.ts): the localPath rewrite failing mid-write leaves config.yaml intact with no leftover temp file; verified it fails when the write is made non-atomic. Full suite: 3109 passed, 0 regressions; tsc clean.

@jeff-r2026
jeff-r2026 merged commit d05b9d6 into main Sep 14, 2026
7 checks passed
@jeff-r2026
jeff-r2026 deleted the feat/partition-slug-legacy-adoption branch September 14, 2026 07:59
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
… of stranding them (Tencent#551)

* fix(partition): adopt pre-Tencent#546 legacy-named partitions instead of stranding them

Tencent#546 widened the partition slug prefix from the anchor's basename to its
whole path but kept the sha256 suffix — without migrating existing
installs. On an upgraded CLI, a partition still named <basename>-<hash>
(every install created before Tencent#546) stops matching projectSlug(anchor):
detectProjectConfig finds no config (the project looks uninitialized),
planMigration would re-copy a retired workspace into a second, empty
partition, and status --all flags the perfectly good data as "corrupt —
dir name does not match anchor".

Both formats share the same hash, so an anchor's legacy name is
computable exactly — no directory scanning. resolvePartitionDir becomes
the seam for "the partition that actually holds this project's data"
(detection, init, migration): when the canonical current-format
directory is absent and a legacy-named one exists, it ATOMICALLY RENAMES
the legacy partition into place — a same-parent metadata move, no data
copied, and an interruption leaves either name intact. An authoritative
current-format partition is never clobbered by a leftover legacy one; a
rename that is genuinely impossible (read-only home) keeps serving the
legacy directory so no data is stranded. status --all stays read-only
and reports a legacy-named partition as "active (legacy name; renamed
automatically on next command)" instead of corrupt.

Verified end-to-end with the real CLI in an isolated HOME: a legacy
.teamai migrates into the readable whole-path slug; status --all
reverse-resolves it as [active]; a partition renamed to the pre-Tencent#546
format is reported active-legacy (no rename, no corrupt), then adopted
by the next command (detection) with data intact, and pull runs through
the adopted partition.

* fix(partition): rebase repo.localPath when adopting a legacy partition

A pre-Tencent#546 partition stores repo.localPath as an ABSOLUTE path to its
team-repo clone (<legacyPartition>/team-repo). resolvePartitionDir renamed
the directory but left config.yaml pointing at the now-gone old path, so
every later `pull` read the team config from a dead directory and silently
skipped the sync ("Team config (teamai.yaml) not found. Skipping.", exit 0)
— the project could never sync again after an upgrade.

Adoption now rebases repo.localPath onto the new partition (mirroring
migrate.ts's rebaseConfigPaths). The rewrite is idempotent and self-healing:
a modern install whose localPath already sits in the canonical dir is left
untouched, an external clone outside the partition is left untouched, and an
adoption interrupted between the rename and the config rewrite is finished by
the next command.

Reproduced with the real CLI (isolated HOME, real local git team repo): a
legacy-named partition + two `pull --force` runs printed "Team config not
found. Skipping." (exit 0) before this fix; after it both runs print
"Synced 1 skills" and localPath points at the live clone.

Regression tests: localPath rebased off the legacy dir / external localPath
left alone / modern-install no-op. Verified they fail when the rebase is
disabled.

* fix(partition): write the adopted config.yaml atomically to prevent truncation

The localPath rebase overwrote config.yaml with a plain (non-atomic) write.
By that point the legacy partition has already been renamed away, so
config.yaml is the partition's ONLY copy — a write that fails partway
(ENOSPC, EFBIG, crash mid-write) truncates it with no source to recover from,
and the CLI still prints "Your original data is unchanged, re-run to retry"
while the data is in fact corrupt and cannot self-recover.

Add writeFileAtomic (same-dir temp + rename, preserving mode) alongside the
existing writeJsonAtomic, and use it for the adopted config.yaml. rename(2)
is atomic, so a failed write removes the temp file and leaves the original
config.yaml byte-for-byte intact; the next command retries the idempotent
rebase and converges.

Reproduced with the real CLI (isolated HOME, real local git team repo, NO fs
mock) by injecting a write failure with RLIMIT_FSIZE=128:
  before: pull --force reports EFBIG, config.yaml truncated 453 -> 128 bytes,
          legacy partition already moved, retry after lifting the limit exits
          0 but never syncs and cannot recover
  after:  config.yaml preserved at 453 bytes, retry after lifting the limit
          prints "Synced 1 skills" and localPath points at the live clone

Regression test: the localPath rewrite failing mid-write leaves config.yaml
intact with no leftover temp file. Verified it fails when the write is made
non-atomic. Full suite: 3109 passed, 0 regressions; tsc clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant