diff --git a/src/albedo/harness/extensions/work/legacy_migration.md b/src/albedo/harness/extensions/work/legacy_migration.md index f5b009d05..828d05c05 100644 --- a/src/albedo/harness/extensions/work/legacy_migration.md +++ b/src/albedo/harness/extensions/work/legacy_migration.md @@ -1,31 +1,125 @@ -# Recovering legacy work items +# Recover legacy work items -The workspace-scoped ledger keeps rows created by older versions under the reserved -`__albedo_legacy__` scope. No workspace sees these rows automatically: the old -schema did not record which workspace owned them. +The workspace-scoped ledger keeps unattributed old rows under the reserved +`__albedo_legacy__` scope. No workspace sees those rows automatically because +the old schema did not record their workspace. Assign only rows whose ownership +you can establish. Leave uncertain rows in the legacy scope. -1. Stop Albedo and back up `$ALBEDO_HOME/albedo.sqlite` before editing it. For - example, use `sqlite3 "$ALBEDO_HOME/albedo.sqlite" ".backup '/safe/path/work.sqlite'"`. -2. Inspect the old items and their parent relationships: +## Check applicability and back up - ```sql - SELECT id, title, parent FROM work WHERE cwd = '__albedo_legacy__'; +This procedure applies after storage migration has added `work.cwd`, when rows +still have the reserved scope. Workspace scoping was introduced in commit +`f06c6b3bff48f0fc1bb19ea39bf858f4d676fd44`. Use the schema, rather than an assumed +release number, to decide whether this repair applies. + +1. Identify the affected home and set `ALBEDO_HOME` to its actual path. Stop + Albedo under that environment and verify every process using the home has + stopped. Follow the [daemon ownership guidance](../../../../../AGENTS.md#own-private-daemons). +2. Confirm the database exists. Save a SQLite backup to a separate, protected + location before upgrading or editing. Replace the backup path below and + check that the command succeeds: + + ```sh + test -f "$ALBEDO_HOME/albedo.sqlite" || exit 1 + sqlite3 "$ALBEDO_HOME/albedo.sqlite" ".backup '/safe/path/before-work-repair.sqlite'" ``` -3. Choose an existing workspace's **absolute canonical path**. Move only items - you can attribute to it; move parents and children together. In SQLite, - verify the IDs and the destination before committing: +3. Inspect the schema without changing it: - ```sql - BEGIN IMMEDIATE; - UPDATE work SET cwd = '/absolute/workspace' WHERE id IN (12, 13); - SELECT id, cwd, parent FROM work WHERE id IN (12, 13); - COMMIT; + ```sh + sqlite3 -readonly "$ALBEDO_HOME/albedo.sqlite" 'PRAGMA table_info(work);' ``` -4. Restart Albedo and use `/work` in that workspace to verify visibility. - Restore the backup if the mapping was wrong. Leave uncertain rows in the - legacy scope; never assign every old row to every workspace. + If `cwd` is absent, do not run the repair SQL. After preserving the backup, + let normal Albedo startup complete its storage migration, then stop all + processes using the home again. Back up the migrated database to a different + file before continuing. The [migration](migrations/cwd.gleam) adds `cwd` + before its index. Startup does not infer or reassign legacy ownership. + +## Inspect and repair selected rows + +Use the exact workspace identifier of the intended existing session. For a +local workspace, this is its canonical local path. A remote workspace can use +an SSH location. [Linked workspaces](../../../../../robot-docs/workspaces.md) +can also see the reassigned rows. + +Open the stopped home's database in an interactive SQLite session: + +```sh +sqlite3 "$ALBEDO_HOME/albedo.sqlite" +``` + +Inspect the legacy rows and their parent relationships: + +```sql +SELECT id, title, cwd, parent FROM work +WHERE cwd = '__albedo_legacy__' ORDER BY id; +``` + +Choose IDs only after establishing ownership. Move related parents and children +together. Inspect their parent chains and children, including rows outside your +initial selection. The foreign key checks that a parent exists, not that it has +the same workspace scope. + +The following example selects IDs 12 and 13. Replace both the IDs and the +workspace string with your inspected values. Escape any single quote in a SQL +string as two single quotes. + +```sql +BEGIN IMMEDIATE; +SELECT id, title, cwd, parent FROM work +WHERE id IN (12, 13) OR parent IN (12, 13); +``` + +Stop and inspect the result. Confirm the selected rows are still legacy rows +and that the selection includes their related items. Then apply the guarded +update in the same SQLite session: + +```sql +UPDATE work SET cwd = '/absolute/workspace' +WHERE id IN (12, 13) AND cwd = '__albedo_legacy__'; +SELECT changes() AS moved_rows; +SELECT id, title, cwd, parent FROM work +WHERE id IN (12, 13) OR parent IN (12, 13); +``` + +Check that `moved_rows` equals the number of legacy rows you selected and that +all relationships and destination identifiers are correct. SQL errors or an +unexpected count require investigation. The transaction remains open for your +decision. To discard the edit, run: + +```sql +ROLLBACK; +``` + +Run the following instead only after inspection succeeds: + +```sql +COMMIT; +``` + +This offline repair bypasses normal revision increments and change notifications +in [`ledger.gleam`](ledger.gleam). Keep the daemon stopped for the entire repair. +Do not assign every old row to every workspace. + +## Verify and recover + +After committing and closing SQLite, restart Albedo under the same home. Use +`/work` in the intended workspace to verify visibility, accounting for its linked +workspaces. + +If attribution is wrong, stop all processes using that home again before any +repair or restoration. Preserve the current database with another SQLite backup. +Prefer another inspected transaction to correct selected rows when unrelated +changes made since the original backup must survive. + +Restoring a backup rolls back the **entire database**, including unrelated +sessions and ledger changes since the backup. Do not copy a backup over a live +SQLite database or mix its main file with another database's WAL files. If a +whole-database rollback is necessary, use SQLite's restore operation against the +stopped database, after preserving its current state. Verify the restored data +before restarting. -Startup adds the `cwd` column before creating its index on an older database; -restarting alone does not reassign legacy items. +[`ledger.gleam`](ledger.gleam) owns the table and workspace visibility. +The [upgrade scenario](../../../../../test/e2e/migrations_test.py) checks legacy +isolation and schema migration, rather than this manual attribution procedure.