Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .changeset/33155-audit-side-copies-door.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@objectstack/cli': patch
'@objectstack/plugin-audit': patch
---

feat(cli): `os migrate audit-side-copies` runs the at-rest side-copy rewrite on the live audit tables and the archive

Clause-②: yes (widening)
Metadata-protocol: none

**Run `os migrate audit-side-copies` once after upgrading from 18.4.0 or earlier, a dry run first:**

```bash
os migrate audit-side-copies # dry run: counts per table and store, writes no row
os migrate audit-side-copies --object sys_audit_log # only one copy table (repeatable)
os migrate audit-side-copies --apply # remove the columns (prompts; --yes skips the prompt)
os migrate audit-side-copies --apply --yes --json # CI / scripts
```

The audit writer no longer copies a side-copy column (another record's field values, today an approval request's `payload_json`) into `sys_audit_log` (`new_value`, `old_value`) or `sys_activity` (both halves of `metadata`). Copies written before that still carry it. Until they are rewritten, the read-time redaction drops the column and the query guard treats a query naming it as reaching every object, so the command is cleanup, not a gate.

- **The command.** It drives `@objectstack/plugin-audit`'s `migrateSideCopies` (the live `sys_audit_log` and `sys_activity` rows) and `migrateArchivedSideCopies` (the archive datasource `sys_audit_log` archives to). It is a dry run by default and writes only with `--apply`. Its flags are `os migrate audit-key-material`'s (`--database-url`, `--apply`, `--yes`, `--force`, `--json`) plus `--object`, which takes `sys_audit_log` or `sys_activity` and narrows the run, hot and archive, to the tables it names. Any other name is refused with `OBJECT_NOT_FOUND`, exit 1, before anything is read. The report gives, per copy table, the copies read and the copies rewritten (or to rewrite), for the hot tables and for the archive each, and never prints a value. It is idempotent: a second `--apply` rewrites nothing. A failed read or write, a sealed row it had to refuse, or an archive it could not reach exits 1, as for its twins.
- **The rewrite's name (`@objectstack/plugin-audit`).** A sealed row the rewrite amends (ADR-0154 D7) now records `rewritten_by: 'os migrate audit-side-copies'`, the command that rewrote it, as `audit-mail-bodies`, `audit-key-material` and `audit-metadata-bodies` record theirs. It recorded `side-copy-migration` before, a name no release shipped; a row amended under it on a pre-release build keeps it.
44 changes: 41 additions & 3 deletions content/docs/deployment/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,7 @@ where the data lives.
| `os migrate activity-changed-fields` | Stamp the two read-rule columns (`changed_fields`, `milestone_key`) on the `sys_activity` rows written before they existed. Until it runs, those rows are withheld from every reader but the system, so run it at once after upgrading. No flag; idempotent |
| `os migrate audit-mail-bodies` | Mask the mail bodies (`body_text`, `body_html`) that `sys_audit_log` and `sys_activity` copies of `sys_email` / `sys_identity_email` rows still hold in clear, written before the audit writer masked them. Covers the `archive` datasource `sys_audit_log` archives to as well. Reports counts only. No flag; idempotent. See [below](#os-migrate-audit-mail-bodies) |
| `os migrate audit-key-material` | Remove the encrypted key-material columns from the `sys_audit_log` and `sys_activity` copies written before the audit writer omitted them. Covers the `archive` datasource `sys_audit_log` archives to as well. Reports counts only. No flag; idempotent. See [below](#os-migrate-audit-key-material) |
| `os migrate audit-side-copies` | Remove the side-copy columns (another record's field values, such as an approval request's `payload_json`) from the `sys_audit_log` and `sys_activity` copies written before the audit writer omitted them. Covers the `archive` datasource `sys_audit_log` archives to as well. `--object` narrows it to one copy table. Reports counts only, per table and store. No flag; idempotent. See [below](#os-migrate-audit-side-copies) |
| `os migrate comment-parent-pair` | Stamp the parent pair (`object_name`, `record_id`) on the `sys_comment` rows written before it existed. Until it runs, a broad comment read past 2,000 candidate rows omits comments the reader may see, and its total agrees with the short list, so run it at once after upgrading. No flag; idempotent |
| `os migrate meta --stored` | Replay the metadata conversion chain over this deployment's `sys_metadata` rows and rewrite the ones still carrying a pre-protocol shape. Hygiene, not a gate — nothing depends on it having run |
| `os migrate security-catalog-overlays` | List the environment-wide permission-set and position rows (the legacy plurals `permissions` / `positions` included) stored over a name a configured package holds — the rows a v18 cold boot refuses — each with the package that holds its name; `--apply` deletes them, one history tombstone per row. Run it before the first v18 boot, with the flags and environment the deployment boots with: the platform security plugin's sets are held only where `os serve` composes it (an auth secret set, and the `auth` tier on), and the step takes `serve`'s `--preset` and `--dev` with `serve`'s meaning. Adopts nothing; exits 1 while it lists a row |
Expand All @@ -1502,7 +1503,7 @@ holds nothing, and answers with empty work and exit 0. `os migrate value-shapes`
clean over zero records and names the objects it did not read. `os migrate
recorded-by` has nothing to convert and `os migrate resume` no interrupted runs.
`os migrate meta --stored` has no stored metadata to examine, `os migrate
audit-metadata-bodies`, `os migrate audit-mail-bodies` and `os migrate audit-key-material` no audit copy to rewrite, and
audit-metadata-bodies`, `os migrate audit-mail-bodies`, `os migrate audit-key-material` and `os migrate audit-side-copies` no audit copy to rewrite, and
`os migrate account-issuer` no account to collide. `os secret orphans`, `os secret rewrap` and `os storage orphans`
report no secret and no file. A read the command cannot avoid and that fails for any
other reason still refuses and exits 1. Point `--database-url` at the deployment's
Expand Down Expand Up @@ -2114,10 +2115,47 @@ carries the same `archive` entries with the same `status` values and exit
codes. A declared archive this boot does not connect, or an active runtime
datasource row of that name, reads `not-connected` and exits `1`.

#### `os migrate audit-side-copies`

Some columns hold another record's field values, which that record's own row
serves only through its owner's per-reader narrowing: today the approval
request's `payload_json`, its snapshot of the record under approval. An audit
copy (`sys_audit_log.new_value` / `old_value`, both halves of
`sys_activity.metadata`) cannot be narrowed that way, so new copies omit those
columns. This command removes them from the copies written before that.

```bash
os migrate audit-side-copies # Dry run: counts, writes no rows
os migrate audit-side-copies --object sys_audit_log # Only one copy table (repeatable)
os migrate audit-side-copies --apply # Remove the columns (prompts)
os migrate audit-side-copies --apply --yes --json # CI / scripts
```

The report gives, per copy table, the copies it read and the copies it
rewrote (or, in a dry run, would rewrite), for the hot tables and for the
archive datasource each. Only the listed keys of a copy are removed: every
other key, and every other column of the row (its `updated_at` included),
stays as it was. A column of the same name in a copy of any other object is
left alone. The report holds counts only and never prints a value.

The flags are `os migrate audit-key-material`'s, one for one, plus
`--object`, which takes `sys_audit_log` or `sys_activity` and narrows the run,
hot and archive, to the tables it names. Any other name is refused with
`OBJECT_NOT_FOUND`, exit `1`, before anything is read.

**Idempotent**: a copy without the columns has nothing to remove, so a second
run rewrites nothing, and re-running is how an interrupted run is finished. A
failed read or write exits `1`. Run it once after upgrading, with the server
stopped. Until it has run, the served copies are still safe: the read-time
redaction drops the columns and the query guard treats a query naming one as
reaching every object. **The archive datasource** is covered exactly as for
[`os migrate audit-mail-bodies`](#os-migrate-audit-mail-bodies), with the same
`archive` entries, `status` values and exit codes.

#### Sealed audit rows (`os migrate audit-*`)

`os migrate audit-metadata-bodies`, `os migrate audit-mail-bodies` and
`os migrate audit-key-material` rewrite value columns of ledger rows, which an
`os migrate audit-metadata-bodies`, `os migrate audit-mail-bodies`,
`os migrate audit-key-material` and `os migrate audit-side-copies` rewrite value columns of ledger rows, which an
object can seal (`lifecycle.seal`, ADR-0154). Before a row that a checkpoint
seals is rewritten, the command writes a signed **redaction amendment**
(`sys_audit_amendment`): the checkpoint, the row and column, the commitment the
Expand Down
2 changes: 2 additions & 0 deletions content/docs/upgrading.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ the protocol-18 step, which `os migrate meta --from 17` lists for your sources

**Upgrading from 18.2.0 or earlier:** run [`os migrate audit-key-material`](/docs/deployment/cli#os-migrate-audit-key-material) once, a dry run first and then `--apply`; it removes the encrypted key-material columns from the audit-ledger copies those releases wrote.

**Upgrading from 18.4.0 or earlier:** run [`os migrate audit-side-copies`](/docs/deployment/cli#os-migrate-audit-side-copies) once, a dry run first and then `--apply`; it removes the side-copy columns (an approval request's `payload_json`) from the audit-ledger copies those releases wrote. Served copies are redacted at read time until it runs, so it is cleanup, not a gate.

The v17 and v18 lines are broken out per release. Each v17 minor shipped
breaking changes under the repo's launch-window convention — which ships a
breaking change as `minor` rather than `major`, so the version number is not the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
//
// [ADR-0154 D7] The three `os migrate audit-*` ledger-copy rewrites compose the
// [ADR-0154 D7] The four `os migrate audit-*` ledger-copy rewrites compose the
// crypto `os serve` seals with (`composeRedactionSeal`) and hand its `seal` to
// every run, hot and archive, so a sealed row is amended before it is rewritten
// or refused; and a run that refused one exits non-zero. What the `seal` does is
Expand All @@ -22,6 +22,7 @@ const HERE = path.dirname(fileURLToPath(import.meta.url));
const COMMANDS: Record<string, readonly string[]> = {
'audit-mail-bodies.ts': ['migrateMailLogBodyCopies', 'migrateArchivedMailLogBodyCopies'],
'audit-key-material.ts': ['migrateKeyMaterialCopies', 'migrateArchivedKeyMaterialCopies'],
'audit-side-copies.ts': ['migrateSideCopies', 'migrateArchivedSideCopies'],
'audit-metadata-bodies.ts': ['migrateStoredMetadataBodyCopies'],
};

Expand Down
Loading
Loading