fix(audit): persist config audit rows written after the final save (DEP-1, MGT-1, TPL-3, WP1 task 1.4)

IAuditService.LogAsync only stages the row; every caller that audited
after its last SaveChangesAsync, outside the configuration-command
transaction, lost the row with the DI scope (rig: zero Disable/Enable/
Delete/DeployArtifacts rows, 16 Deploy rows for 62 deployments, one
Create/Instance row for ids up to 3876).

- New IAuditService.LogAndSaveAsync: stage + save, best-effort (logs and
  swallows any failure incl. cancellation, detaches the unsaved row).
- DeploymentService (deploy/failed/reconciled/lifecycle + timeouts),
  ArtifactDeploymentService (DeployArtifacts now before the best-effort
  stamp, RetryArtifactDeployment, DeploySiteSecrets), InstanceService,
  AreaService, SiteService: post-save LogAsync -> LogAndSaveAsync.
- ManagementActor: AuditAndSaveAsync for the non-configuration handlers
  (API keys, shared schemas, ES methods, data-connection move); move now
  audits the scrubbed public shape (MGT-4) so the fix does not start
  writing protocol credentials into AuditLogEntries.
- HandleCreateInstance no longer stages a second Create row on top of
  InstanceService's own.

Tests: real-context (SQLite) tests per family read the row back through
a second context - DeploymentService, ArtifactDeploymentService,
InstanceService, ManagementActor; each fails against the old code.
Test projects gain a ConfigurationDatabase + EF Sqlite reference.
This commit is contained in:
Joseph Doherty
2026-12-03 09:59:19 -05:00
parent 5469ece012
commit 5ca998dc56
26 changed files with 829 additions and 98 deletions
@@ -256,6 +256,12 @@ Audit entries are written **synchronously** within the same database transaction
- If the change fails and rolls back, the audit entry is also rolled back.
- No audit entries are lost due to process crashes between the change and the audit write.
#### `LogAndSaveAsync` — when the audit call comes after the last save
`LogAsync` only **stages** the row; the guarantee above holds only if something saves afterwards. A caller that audits AFTER its final `SaveChangesAsync` — and is not running inside the `ConfigurationCommandTransaction`, whose trailing save flushes staged rows — must call `IAuditService.LogAndSaveAsync(...)` instead (code review 2026-12-02 DEP-1 / MGT-1 / TPL-3). It adds the row and saves it immediately, **best-effort**: any failure (including cancellation) is logged at Warning and swallowed, never thrown, and the unsaved row is detached so it cannot fail or be silently written by a later save. Because the save flushes the whole unit of work, call it only once the operation's own changes are saved; inside an ambient transaction the row commits or rolls back with that transaction.
Before this, every post-save `LogAsync` with no later save was silently discarded with the DI scope: the rig showed zero `Disable`/`Enable`/`Delete`/`DeployArtifacts` rows, 16 `Deploy` rows for 62 deployments, and one `Create`/`Instance` row for instance ids up to 3876. Post-save callers now on `LogAndSaveAsync`: `DeploymentService` (deploy, deploy-failed, reconciled, disable/enable/delete and their timeouts), `ArtifactDeploymentService` (`DeployArtifacts` — now written before the best-effort revision stamp — `RetryArtifactDeployment`, `DeploySiteSecrets`), `InstanceService`, `AreaService`, `SiteService`, and the `ManagementActor` handlers outside the configuration-command list (API keys, shared schemas, external-system methods, data-connection move). Services that already save after `LogAsync` (`TemplateService`, `SharedScriptService`, `TemplateFolderService`, the Transport importer/exporter) are unchanged.
### Integration Example
```mermaid