Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d41ddf5
test(plugin-audit): pin the late re-read, the lost-sequence seed drop…
claude Nov 14, 2026
88a4abc
fix(plugin-audit): admit and open each checkpoint once per redaction …
claude Nov 14, 2026
3bcc332
docs(spec): the plugin-audit group of the platform object names says …
claude Nov 14, 2026
5ada48f
test(plugin-audit): pin per-chain sealer isolation: a chain-local fau…
claude Nov 14, 2026
14873e9
fix(plugin-audit): isolate sealer faults per chain: a chain-local fau…
claude Nov 14, 2026
03a059d
test(plugin-audit): pin a host handing an app-composed AuditPlugin it…
claude Nov 14, 2026
a065011
feat(plugin-audit): AuditPlugin.adoptHostCryptoProvider takes a host'…
claude Nov 14, 2026
e1b0acc
test(cli): pin os serve handing its crypto provider to an app-supplie…
claude Nov 14, 2026
574c27c
fix(cli): os serve hands its crypto provider to an app-supplied Audit…
claude Nov 14, 2026
812588a
chore(changeset): plugin-audit and cli patch for per-chain sealer iso…
claude Nov 14, 2026
4cbd2fc
fix(plugin-audit): the skipped-chain report claims only what the seal…
claude Nov 14, 2026
22d4da3
test(plugin-audit): pin the window-major pass and that a lagging chai…
claude Nov 14, 2026
4412d2f
Merge origin/main into claude/area-A2-31908-p4b1 (P4a landed)
claude Nov 15, 2026
4827322
test(plugin-audit): pin bounded checkpoint parts, the duplicate-listi…
claude Nov 15, 2026
b080e65
feat(plugin-audit): seal a window into consecutive checkpoints of at …
claude Nov 15, 2026
0dd58a8
fix(plugin-audit): a checkpoint written before the parts amendment re…
claude Nov 15, 2026
b189644
test(plugin-audit): pin the part cut across a tie, rows that go betwe…
claude Nov 15, 2026
faadaaa
Merge remote-tracking branch 'origin/main' into claude/area-A2-31908-…
claude Nov 15, 2026
e930216
test(plugin-audit): the re-seal pin's precondition is that the cap ho…
claude Nov 15, 2026
6bb7f49
fix(plugin-audit): the idle cap counts only while a chain is skipped,…
claude Nov 15, 2026
ff13294
docs(changeset): P4b2 declares Clause-② yes (narrowing) with the BREA…
claude Nov 15, 2026
60960cd
Merge origin/main into claude/area-A2-31908-p4b2
claude Nov 15, 2026
bfece9b
test(plugin-audit, objectql): pin the audit seal lock beside audit.se…
claude Nov 15, 2026
90f8170
test(cli): pin that each os migrate audit-* runs every rewrite under …
claude Nov 15, 2026
d741b9e
feat(plugin-audit, cli)!: hold the audit seal lock across an os migra…
claude Nov 15, 2026
f08afca
docs(audit): an hour seals into checkpoints of at most 10,000 rows, v…
claude Nov 15, 2026
b234eee
test(plugin-audit): state a 30 s budget on the three stalled-chain re…
claude Nov 15, 2026
748f0d9
fix(plugin-audit): name the three audit rewrites in the seal-lock lin…
claude Nov 15, 2026
59ad96d
Merge origin/main into claude/area-A2-31908-p4b2
claude Nov 15, 2026
5ef334e
docs(changeset): name the two skills/objectstack-data files a follow-…
claude Nov 15, 2026
6acfe29
fix(cli): call the audit seal lock's warn line through the logger, so…
claude Nov 15, 2026
ebcb2de
Merge claude/area-A2-31908-p4b2 into claude/area-A2-31908-p4b3
claude Nov 15, 2026
06589fe
feat(cli): os migrate audit-side-copies holds the audit seal lock acr…
claude Nov 15, 2026
fbdc0b8
Merge origin/main into claude/area-A2-31908-p4b3
claude Nov 15, 2026
972c3ec
fix(plugin-audit, objectql): a run or sealer pass locks a shared data…
claude Nov 15, 2026
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
24 changes: 24 additions & 0 deletions .changeset/31908-p4b3-audit-seal-lock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@objectstack/plugin-audit': patch
'@objectstack/objectql': patch
'@objectstack/cli': patch
---

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`

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`, `DatabaseSessionLockRunOptions`, `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`.
Metadata-protocol: none

<!-- 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. -->

**BREAKING** for an `os migrate audit-* --apply` run on a deployment where an app declared `lifecycle.seal`. On SQLite or Turso the run now stops before writing anything and names the flag: stop every server that seals the database, then re-run with `--sealer-stopped`. On Postgres or MySQL the run waits up to ten minutes for a sealer pass in flight, then refuses, having changed nothing: run it again once that pass has finished. A run that rewrites no sealed object is unchanged on every database, and so is every dry run. No object declares `lifecycle.seal` by default, so a deployment that did not opt in sees no change.

**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).

- 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.
- 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.
- Two replicas with no cluster service now also keep their passes apart through this lock on Postgres and MySQL.
- Two datasources that reach one lock namespace — one Postgres database, or two schemas of one MySQL server — share one lock: a pass or a run takes it once there and never waits on itself.
- 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.

**`@objectstack/objectql`.** The schema-migration lock and the audit seal lock share one implementation of a named database session lock, now exported: `acquireDatabaseSessionLock` (wait, then refuse), `tryDatabaseSessionLock` (once, never waiting: the lock, `'held'`, or `null` where the database offers none) and `databaseSessionLockPgKey`. A held lock's `isHeld()` asks its own session whether it still stands. With `ownLocks` (`DatabaseSessionLockRunOptions`), a run that locks several datasources marks each session it opens, and a lock it finds held by one of its own sessions in the same database answers that lock instead of `'held'` or a wait. `acquireSchemaMigrationLock` takes the same statements, prints the same lines and refuses with the same message as before.
38 changes: 25 additions & 13 deletions packages/cli/src/commands/migrate/audit-key-material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { absentTableReads } from '../../utils/absent-table-reads.js';
import { exitOneShotCommand } from '../../utils/one-shot-exit.js';
import { archiveFailureCount, examineArchiveTargets } from '../../utils/ledger-copy-archive.js';
import { composeRedactionSeal } from '../../utils/audit-redaction-seal.js';
import { sealerStoppedFlag, underAuditSealLock } from '../../utils/audit-seal-lock.js';

async function confirm(question: string): Promise<boolean> {
if (!process.stdin.isTTY) return false; // non-interactive → require --yes
Expand Down Expand Up @@ -103,6 +104,7 @@ export default class MigrateAuditKeyMaterial extends Command {
default: false,
}),
json: Flags.boolean({ description: 'Output as JSON (implies non-interactive; requires --yes to apply)' }),
'sealer-stopped': sealerStoppedFlag,
};

/**
Expand Down Expand Up @@ -227,24 +229,33 @@ export default class MigrateAuditKeyMaterial extends Command {
};

const seal = redaction.seal(stack.kernel, engine);
const report = await migrateKeyMaterialCopies(readView, logger, { apply, seal });

// Each copy table's declared archive target, then the same rewrite
// through that datasource's driver.
const archive = await examineArchiveTargets({
// [ADR-0154 D3 amended] An --apply run holds the audit seal lock from before its first rewrite to after its
// last, archive runs included, so no sealer pass seals a row it rewrites; where the database offers no session
// lock, a run on a sealed object needs --sealer-stopped (seat ruling 6885005594, E + B).
const { result: { report, archive }, lostLock } = await underAuditSealLock(
engine,
kernel: stack.kernel,
reads: readView,
tables: KEY_MATERIAL_COPY_TABLES,
run: (driver, objects) => migrateArchivedKeyMaterialCopies(driver, logger, { apply, objects, seal }),
});
{ apply, sealerStopped: flags['sealer-stopped'], objects: KEY_MATERIAL_COPY_TABLES, warn: (line) => logger.warn(line) },
async () => {
const report = await migrateKeyMaterialCopies(readView, logger, { apply, seal });
// Each copy table's declared archive target, then the same rewrite
// through that datasource's driver.
const archive = await examineArchiveTargets({
engine,
kernel: stack.kernel,
reads: readView,
tables: KEY_MATERIAL_COPY_TABLES,
run: (driver, objects) => migrateArchivedKeyMaterialCopies(driver, logger, { apply, objects, seal }),
});
return { report, archive };
},
);
const failures = report.failures + archiveFailureCount(archive);
const refused = report.refused + archive.reduce((n, a) => n + (a.report?.refused ?? 0), 0);

if (flags.json) {
reads.notice(true);
await emitJson({ database: stack.dbLabel, apply, report, archive, duration: timer.elapsed() });
if (failures > 0 || refused > 0) this.exit(1);
await emitJson({ database: stack.dbLabel, apply, report, archive, ...(lostLock ? { sealLockLost: lostLock } : {}), duration: timer.elapsed() });
if (failures > 0 || refused > 0 || lostLock) this.exit(1);
return;
}

Expand Down Expand Up @@ -299,6 +310,7 @@ export default class MigrateAuditKeyMaterial extends Command {

const rewritten = report.rewritten + archive.reduce((n, a) => n + (a.report?.rewritten ?? 0), 0);
if (refused > 0) printError(redaction.refusedLine(refused));
if (lostLock) printError(lostLock);
if (failures > 0) {
printError(`${failures} read(s), row(s) or archive datasource(s) failed — re-run to finish them.`);
} else if (apply && rewritten > 0) {
Expand All @@ -312,7 +324,7 @@ export default class MigrateAuditKeyMaterial extends Command {
}
console.log(chalk.dim(` ${timer.display()}`));
console.log('');
if (failures > 0 || refused > 0) this.exit(1);
if (failures > 0 || refused > 0 || lostLock) this.exit(1);
} catch (error: any) {
if (isExitSignal(error)) throw error;
if (flags.json) { await emitJson({ error: error.message, ...errorCodeFields(error) }, 0, { compact: true }); this.exit(1); }
Expand Down
38 changes: 25 additions & 13 deletions packages/cli/src/commands/migrate/audit-mail-bodies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { absentTableReads } from '../../utils/absent-table-reads.js';
import { exitOneShotCommand } from '../../utils/one-shot-exit.js';
import { archiveFailureCount, examineArchiveTargets } from '../../utils/ledger-copy-archive.js';
import { composeRedactionSeal } from '../../utils/audit-redaction-seal.js';
import { sealerStoppedFlag, underAuditSealLock } from '../../utils/audit-seal-lock.js';

async function confirm(question: string): Promise<boolean> {
if (!process.stdin.isTTY) return false; // non-interactive → require --yes
Expand Down Expand Up @@ -110,6 +111,7 @@ export default class MigrateAuditMailBodies extends Command {
default: false,
}),
json: Flags.boolean({ description: 'Output as JSON (implies non-interactive; requires --yes to apply)' }),
'sealer-stopped': sealerStoppedFlag,
};

/**
Expand Down Expand Up @@ -235,25 +237,34 @@ export default class MigrateAuditMailBodies extends Command {
};

const seal = redaction.seal(stack.kernel, engine);
const report = await migrateMailLogBodyCopies(readView, logger, { apply, seal });

// [#24899] Each copy table's declared archive target, then the same
// runner through that datasource's driver (`examineArchiveTargets`).
const archive = await examineArchiveTargets({
// [ADR-0154 D3 amended] An --apply run holds the audit seal lock from before its first rewrite to after its
// last, archive runs included, so no sealer pass seals a row it rewrites; where the database offers no session
// lock, a run on a sealed object needs --sealer-stopped (seat ruling 6885005594, E + B).
const { result: { report, archive }, lostLock } = await underAuditSealLock(
engine,
kernel: stack.kernel,
reads: readView,
tables: MAIL_LOG_BODY_COPY_OBJECTS,
run: (driver, objects) => migrateArchivedMailLogBodyCopies(driver, logger, { apply, objects, seal }),
});
{ apply, sealerStopped: flags['sealer-stopped'], objects: MAIL_LOG_BODY_COPY_OBJECTS, warn: (line) => logger.warn(line) },
async () => {
const report = await migrateMailLogBodyCopies(readView, logger, { apply, seal });
// [#24899] Each copy table's declared archive target, then the same
// runner through that datasource's driver (`examineArchiveTargets`).
const archive = await examineArchiveTargets({
engine,
kernel: stack.kernel,
reads: readView,
tables: MAIL_LOG_BODY_COPY_OBJECTS,
run: (driver, objects) => migrateArchivedMailLogBodyCopies(driver, logger, { apply, objects, seal }),
});
return { report, archive };
},
);
const archiveFailures = archiveFailureCount(archive);
const failures = report.failures + archiveFailures;
const refused = report.refused + archive.reduce((n, a) => n + (a.report?.refused ?? 0), 0);

if (flags.json) {
reads.notice(true);
await emitJson({ database: stack.dbLabel, apply, report, archive, duration: timer.elapsed() });
if (failures > 0 || refused > 0) this.exit(1);
await emitJson({ database: stack.dbLabel, apply, report, archive, ...(lostLock ? { sealLockLost: lostLock } : {}), duration: timer.elapsed() });
if (failures > 0 || refused > 0 || lostLock) this.exit(1);
return;
}

Expand Down Expand Up @@ -308,6 +319,7 @@ export default class MigrateAuditMailBodies extends Command {

const rewritten = report.rewritten + archive.reduce((n, a) => n + (a.report?.rewritten ?? 0), 0);
if (refused > 0) printError(redaction.refusedLine(refused));
if (lostLock) printError(lostLock);
if (failures > 0) {
printError(`${failures} read(s), row(s) or archive datasource(s) failed — re-run to finish them.`);
} else if (apply && rewritten > 0) {
Expand All @@ -321,7 +333,7 @@ export default class MigrateAuditMailBodies extends Command {
}
console.log(chalk.dim(` ${timer.display()}`));
console.log('');
if (failures > 0 || refused > 0) this.exit(1);
if (failures > 0 || refused > 0 || lostLock) this.exit(1);
} catch (error: any) {
if (isExitSignal(error)) throw error;
if (flags.json) { await emitJson({ error: error.message, ...errorCodeFields(error) }, 0, { compact: true }); this.exit(1); }
Expand Down
21 changes: 16 additions & 5 deletions packages/cli/src/commands/migrate/audit-metadata-bodies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { describeOccupancy, JOURNAL_MODE_SWITCH_NOTE } from '../../utils/sqlite-
import { buildDataMigrationPlugins } from '../../utils/data-migration-plugins.js';
import { absentTableReads } from '../../utils/absent-table-reads.js';
import { composeRedactionSeal } from '../../utils/audit-redaction-seal.js';
import { sealerStoppedFlag, underAuditSealLock } from '../../utils/audit-seal-lock.js';

async function confirm(question: string): Promise<boolean> {
if (!process.stdin.isTTY) return false; // non-interactive → require --yes
Expand Down Expand Up @@ -88,6 +89,7 @@ export default class MigrateAuditMetadataBodies extends Command {
default: false,
}),
json: Flags.boolean({ description: 'Output as JSON (implies non-interactive; requires --yes to apply)' }),
'sealer-stopped': sealerStoppedFlag,
};

async run(): Promise<void> {
Expand Down Expand Up @@ -177,7 +179,7 @@ export default class MigrateAuditMetadataBodies extends Command {
throw new Error('No ObjectQL engine on this stack — cannot rewrite audit rows.');
}

const { migrateStoredMetadataBodyCopies } = await import('@objectstack/plugin-audit');
const { migrateStoredMetadataBodyCopies, STORED_METADATA_BODY_REWRITTEN_OBJECTS } = await import('@objectstack/plugin-audit');

const logger = flags.json
? { info: (m: string) => console.error(m), warn: (m: string) => console.error(m) }
Expand Down Expand Up @@ -207,12 +209,20 @@ export default class MigrateAuditMetadataBodies extends Command {
update: (object, data, options) => engine.update(object, data, options),
};

const report = await migrateStoredMetadataBodyCopies(readView, logger, { apply, seal: redaction.seal(stack.kernel, engine) });
const seal = redaction.seal(stack.kernel, engine);
// [ADR-0154 D3 amended] An --apply run holds the audit seal lock from before its first rewrite to after its
// last, archive runs included, so no sealer pass seals a row it rewrites; where the database offers no session
// lock, a run on a sealed object needs --sealer-stopped (seat ruling 6885005594, E + B).
const { result: report, lostLock } = await underAuditSealLock(
engine,
{ apply, sealerStopped: flags['sealer-stopped'], objects: STORED_METADATA_BODY_REWRITTEN_OBJECTS, warn: (line) => logger.warn(line) },
() => migrateStoredMetadataBodyCopies(readView, logger, { apply, seal }),
);

if (flags.json) {
reads.notice(true);
await emitJson({ database: stack.dbLabel, apply, report, duration: timer.elapsed() });
if (report.failures > 0 || report.refused > 0) this.exit(1);
await emitJson({ database: stack.dbLabel, apply, report, ...(lostLock ? { sealLockLost: lostLock } : {}), duration: timer.elapsed() });
if (report.failures > 0 || report.refused > 0 || lostLock) this.exit(1);
return;
}

Expand All @@ -228,6 +238,7 @@ export default class MigrateAuditMetadataBodies extends Command {
console.log('');

if (report.refused > 0) printError(redaction.refusedLine(report.refused));
if (lostLock) printError(lostLock);
if (report.failures > 0) {
printError(`${report.failures} row(s) could not be rewritten — re-run to finish them.`);
} else if (apply && report.rewritten > 0) {
Expand All @@ -243,7 +254,7 @@ export default class MigrateAuditMetadataBodies extends Command {
}
console.log(chalk.dim(` ${timer.display()}`));
console.log('');
if (report.failures > 0 || report.refused > 0) this.exit(1);
if (report.failures > 0 || report.refused > 0 || lostLock) this.exit(1);
} catch (error: any) {
if (isExitSignal(error)) throw error;
if (flags.json) { await emitJson({ error: error.message, ...errorCodeFields(error) }, 0, { compact: true }); this.exit(1); }
Expand Down
Loading
Loading