Skip to content

Commit 06589fe

Browse files
committed
feat(cli): os migrate audit-side-copies holds the audit seal lock across an --apply run and takes --sealer-stopped, as its three twins do
The merged base brings os migrate audit-side-copies, the fourth ledger-copy rewrite of the os migrate audit- family. ADR-0154 D3 as amended (seat ruling 6885005594, E + B) holds the lock for every rewrite of that family, so this one now runs its hot and archive rewrites inside one underAuditSealLock hold on the copy tables it rewrites (--object narrows them), declares --sealer-stopped, and exits non-zero when the lock's session ended mid-run. The contract test lists its objects; the seal-lock lines name all four commands; the changeset says the command is new in this release and ships with the hold. Claude-Session: https://claude.ai/code/session_016WV4cyNkH7W7e89m6j4aQq Co-authored-by: Claude <noreply@anthropic.com>
1 parent ebcb2de commit 06589fe

4 files changed

Lines changed: 37 additions & 18 deletions

File tree

‎.changeset/31908-p4b3-audit-seal-lock.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
feat(plugin-audit, cli)!: an `os migrate audit-* --apply` run holds the audit seal lock for its whole run, each sealer pass skips while it is held, and where the database offers no session lock a run on a sealed object needs `--sealer-stopped`
88

9-
Clause-②: yes (narrowing) — narrowed: `os migrate audit-key-material`, `os migrate audit-mail-bodies` and `os migrate audit-metadata-bodies` with `--apply` now refuse a run that rewrites an object declaring `lifecycle.seal` when that object's database offers no session lock (SQLite, Turso) unless `--sealer-stopped` is passed, and on Postgres or MySQL refuse when a sealer pass or another run still holds the audit seal lock after ten minutes; 18.4.0 ran both. Widened: the `--sealer-stopped` flag, and new exports — `@objectstack/objectql` `acquireDatabaseSessionLock`, `tryDatabaseSessionLock`, `databaseSessionLockPgKey` and the types `DatabaseSessionLock`, `DatabaseSessionLockWait`; `@objectstack/plugin-audit` `holdAuditSealLock`, `AUDIT_SEAL_LOCK_NAME`, `AUDIT_SEAL_LOCK_WAIT_MS`, `STORED_METADATA_BODY_REWRITTEN_OBJECTS` and the types `AuditSealLockEngine`, `AuditSealLockFinish`, `AuditSealLockHold`.
9+
Clause-②: yes (narrowing) — narrowed: `os migrate audit-key-material`, `os migrate audit-mail-bodies` and `os migrate audit-metadata-bodies` with `--apply` now refuse a run that rewrites an object declaring `lifecycle.seal` when that object's database offers no session lock (SQLite, Turso) unless `--sealer-stopped` is passed, and on Postgres or MySQL refuse when a sealer pass or another run still holds the audit seal lock after ten minutes; 18.4.0 ran both. `os migrate audit-side-copies`, new in this release, holds the lock and takes the flag from its first release. Widened: the `--sealer-stopped` flag, and new exports — `@objectstack/objectql` `acquireDatabaseSessionLock`, `tryDatabaseSessionLock`, `databaseSessionLockPgKey` and the types `DatabaseSessionLock`, `DatabaseSessionLockWait`; `@objectstack/plugin-audit` `holdAuditSealLock`, `AUDIT_SEAL_LOCK_NAME`, `AUDIT_SEAL_LOCK_WAIT_MS`, `STORED_METADATA_BODY_REWRITTEN_OBJECTS` and the types `AuditSealLockEngine`, `AuditSealLockFinish`, `AuditSealLockHold`.
1010
Metadata-protocol: none
1111

1212
<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is added, removed or renamed, and nothing stored is rewritten: the moved answer is whether an os migrate audit-* --apply run may proceed while a sealer could be sealing the rows it rewrites. The one-line fix for a refused run is in the body: stop the servers that seal the database and pass --sealer-stopped, or let the sealer pass finish. -->
@@ -16,7 +16,7 @@ Metadata-protocol: none
1616
**The audit seal lock (ADR-0154 D3, as amended 2026-11-14).** A sealer pass and an `os migrate audit-*` rewrite no longer work on a sealed object at the same time. That race let a pass seal a row's old value while the rewrite redacted it, and `os audit verify` then reported the sanctioned redaction as a modified row. The lock is a database session lock named `objectstack:audit-seal`, a Postgres advisory lock or a MySQL `GET_LOCK` on a connection of its own, taken on the database that holds each sealed object's rows (its hot store, where that differs from the primary database).
1717

1818
- Each sealer pass takes it beside the cluster lock `audit.sealer`. While another session holds it, the pass seals nothing and says so at `info`, once an hour while it lasts; the next pass tries again. A checkpoint is written only while both locks still stand: a lock whose database session ended fails the object's pass, and the seed it had sealed is dropped.
19-
- An `--apply` rewrite holds it from before its first write to after its last, archive runs included. A run whose lock's database session ended before it finished says so, names `os audit verify`, and exits non-zero.
19+
- An `--apply` rewrite (`os migrate audit-key-material`, `audit-mail-bodies`, `audit-metadata-bodies` and `audit-side-copies`) holds it from before its first write to after its last, archive runs included. A run whose lock's database session ended before it finished says so, names `os audit verify`, and exits non-zero.
2020
- Two replicas with no cluster service now also keep their passes apart through this lock on Postgres and MySQL.
2121
- On MySQL, lock names are server-wide: a rewrite on one database of a server makes the sealer of another database on that server skip its passes while it runs.
2222

‎packages/cli/src/commands/migrate/audit-redaction-seal.contract.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ const COMMANDS: Record<string, readonly string[]> = {
3333
'audit-metadata-bodies.ts': ['migrateStoredMetadataBodyCopies'],
3434
};
3535

36-
/** Each command's list of the objects it rewrites, as plugin-audit exports it. */
36+
/**
37+
* Each command's list of the objects it rewrites, as plugin-audit exports it —
38+
* or, for `audit-side-copies`, the run's selection of `SIDE_COPY_TABLES`
39+
* (`--object` narrows it), the same list its hot and archive runs are handed.
40+
*/
3741
const REWRITTEN: Record<string, string> = {
3842
'audit-mail-bodies.ts': 'MAIL_LOG_BODY_COPY_OBJECTS',
3943
'audit-key-material.ts': 'KEY_MATERIAL_COPY_TABLES',
44+
'audit-side-copies.ts': 'objects',
4045
'audit-metadata-bodies.ts': 'STORED_METADATA_BODY_REWRITTEN_OBJECTS',
4146
};
4247

‎packages/cli/src/commands/migrate/audit-side-copies.ts‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { absentTableReads } from '../../utils/absent-table-reads.js';
2424
import { exitOneShotCommand } from '../../utils/one-shot-exit.js';
2525
import { archiveFailureCount, examineArchiveTargets } from '../../utils/ledger-copy-archive.js';
2626
import { composeRedactionSeal } from '../../utils/audit-redaction-seal.js';
27+
import { sealerStoppedFlag, underAuditSealLock } from '../../utils/audit-seal-lock.js';
2728
import { objectNotFoundError } from '@objectstack/core';
2829

2930
async function confirm(question: string): Promise<boolean> {
@@ -110,6 +111,7 @@ export default class MigrateAuditSideCopies extends Command {
110111
multiple: true,
111112
}),
112113
json: Flags.boolean({ description: 'Output as JSON (implies non-interactive; requires --yes to apply)' }),
114+
'sealer-stopped': sealerStoppedFlag,
113115
};
114116

115117
/**
@@ -253,24 +255,34 @@ export default class MigrateAuditSideCopies extends Command {
253255
};
254256

255257
const seal = redaction.seal(stack.kernel, engine);
256-
const report = await migrateSideCopies(readView, logger, { apply, objects, seal });
257-
258-
// Each copy table's declared archive target, then the same rewrite
259-
// through that datasource's driver.
260-
const archive = await examineArchiveTargets({
258+
// [ADR-0154 D3 amended] An --apply run holds the audit seal lock from before its first rewrite to after its
259+
// last, archive runs included, on the copy tables it rewrites (`--object` narrows them), so no sealer pass
260+
// seals a row it rewrites; where the database offers no session lock, a run on a sealed object needs
261+
// --sealer-stopped (seat ruling 6885005594, E + B), as for its twins.
262+
const { result: { report, archive }, lostLock } = await underAuditSealLock(
261263
engine,
262-
kernel: stack.kernel,
263-
reads: readView,
264-
tables: objects,
265-
run: (driver, archived) => migrateArchivedSideCopies(driver, logger, { apply, objects: archived, seal }),
266-
});
264+
{ apply, sealerStopped: flags['sealer-stopped'], objects: objects, warn: (line) => logger.warn(line) },
265+
async () => {
266+
const report = await migrateSideCopies(readView, logger, { apply, objects, seal });
267+
// Each copy table's declared archive target, then the same rewrite
268+
// through that datasource's driver.
269+
const archive = await examineArchiveTargets({
270+
engine,
271+
kernel: stack.kernel,
272+
reads: readView,
273+
tables: objects,
274+
run: (driver, archived) => migrateArchivedSideCopies(driver, logger, { apply, objects: archived, seal }),
275+
});
276+
return { report, archive };
277+
},
278+
);
267279
const failures = report.failures + archiveFailureCount(archive);
268280
const refused = report.refused + archive.reduce((n, a) => n + (a.report?.refused ?? 0), 0);
269281

270282
if (flags.json) {
271283
reads.notice(true);
272-
await emitJson({ database: stack.dbLabel, apply, objects, report, archive, duration: timer.elapsed() });
273-
if (failures > 0 || refused > 0) this.exit(1);
284+
await emitJson({ database: stack.dbLabel, apply, objects, report, archive, ...(lostLock ? { sealLockLost: lostLock } : {}), duration: timer.elapsed() });
285+
if (failures > 0 || refused > 0 || lostLock) this.exit(1);
274286
return;
275287
}
276288

@@ -325,6 +337,7 @@ export default class MigrateAuditSideCopies extends Command {
325337

326338
const rewritten = report.rewritten + archive.reduce((n, a) => n + (a.report?.rewritten ?? 0), 0);
327339
if (refused > 0) printError(redaction.refusedLine(refused));
340+
if (lostLock) printError(lostLock);
328341
if (failures > 0) {
329342
printError(`${failures} read(s), row(s) or archive datasource(s) failed — re-run to finish them.`);
330343
} else if (apply && rewritten > 0) {
@@ -338,7 +351,7 @@ export default class MigrateAuditSideCopies extends Command {
338351
}
339352
console.log(chalk.dim(` ${timer.display()}`));
340353
console.log('');
341-
if (failures > 0 || refused > 0) this.exit(1);
354+
if (failures > 0 || refused > 0 || lostLock) this.exit(1);
342355
} catch (error: any) {
343356
if (isExitSignal(error)) throw error;
344357
if (flags.json) { await emitJson({ error: error.message, ...errorCodeFields(error) }, 0, { compact: true }); this.exit(1); }

‎packages/plugins/plugin-audit/src/audit-seal/seal-lock.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ export const AUDIT_SEAL_PG_LOCK_KEY = databaseSessionLockPgKey(AUDIT_SEAL_LOCK_N
6060
export const AUDIT_SEAL_LOCK_WAIT_MS = 10 * 60_000;
6161

6262
/**
63-
* The three rewrites that hold the lock, spelled as an operator types them, for
63+
* The four rewrites that hold the lock, spelled as an operator types them, for
6464
* the lines that tell an operator what else may hold it. Each is a full
6565
* command id, so `check:cli-command-ids` holds every one to a real command.
6666
*/
6767
export const AUDIT_SEAL_LOCK_REWRITE_COMMANDS =
68-
'`os migrate audit-key-material`, `os migrate audit-mail-bodies` or `os migrate audit-metadata-bodies`';
68+
'`os migrate audit-key-material`, `os migrate audit-mail-bodies`, `os migrate audit-metadata-bodies` or ' +
69+
'`os migrate audit-side-copies`';
6970

7071
/** The engine surface a hold reads: which objects are sealed, and where each one's rows live. */
7172
export interface AuditSealLockEngine {

0 commit comments

Comments
 (0)