Skip to content

fix(api): every exec session gets exactly one recorded closing (closes #489) - #511

Merged
samuelmbabhazi merged 2 commits into
mainfrom
fix/489-exec-session-closing-audit
Nov 26, 2026
Merged

samuelmbabhazi merged 2 commits into
mainfrom
fix/489-exec-session-closing-audit

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

What changed

  • ExecSessionRegistry: the connections are still released at once. The end's audit write is now tried again when it fails, with a delay that doubles from 1 s to 1 min, for about 15 minutes.
    • Each retry first asks the audit reader whether the session's closing is recorded. A write whose commit succeeded but whose answer was lost is therefore not recorded twice.
    • Logs carry identifiers and the error's kind only.
  • New ExecSessionChecks (fleet infrastructure), a pg-boss queue named exec-session-check:
    • ExecSessions.open queues the session's check in the transaction that records exec.session.opened, for 5 minutes after endsAt.
    • The check does nothing if the session is still live, in which case it schedules itself again, or if an exec.session.ended or exec.session.interrupted event exists. Otherwise it records exec.session.interrupted: system actor, the session's person, reason: unknown.
    • main.ts starts it before the API listens, and its startup sweep runs the queued checks at once. Sessions live in one API process only, so those of the stopped process are gone.
  • AuditModule exports AUDIT_EVENT_READER, and FleetModule imports it. There is no cycle: the audit module does not depend on fleet.
  • SPEC (exec sessions) describes the retries, the check and the interrupted record.

Why

Closes #489. ExecSessionRegistry.ended removed the session, tried the audit write once, and forgot it on failure. A process that stopped left its sessions with an opening and no end. Recovery of the database retried nothing.

How it was tested

  • exec-relay.spec.ts: the store fails once, then commits while the answer is lost. The relay is released at once, the write is tried 3 times, and exactly one exec.session.ended is recorded. This test failed on the old code.
  • exec-sessions.spec.ts: an opening queues its check after its action and its audit event, with the session's endsAt.
  • exec-session-checks.integration.spec.ts (real PostgreSQL and pg-boss):
    • A check queued for a session that never ended is run by the startup sweep and records exec.session.interrupted. Run again, it finds the closing and records nothing.
    • A session whose exec.session.ended exists is left as it is.
  • The API unit suite passes (105 files, 904 tests), as do api:lint and api:typecheck. The integration suite passes locally except the known PostgreSQL 18 and Docker-only files.

Security considerations

The queue's job data holds identifiers only (organization, session, person), never what the session ran or showed. The interrupted record claims nothing beyond "unknown", and the closing check reads the organization's own audit events under its row security. Transport cleanup does not wait for the audit store.

…#489)

The end of a session was written once; a failure was logged and forgotten, and a process that stopped left its sessions without an end. The end is now tried again with a growing delay, looking first for an earlier write whose answer was lost. A check queued with the opening, in the same transaction, records the session interrupted when no closing exists once it must have ended, and the checks of a stopped process run at the next start.

Signed-off-by: samuel mbabhazi <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 17 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f59d778 and f0503e3.

Files selected for processing: 14

  • apps/api/src/app.http.spec.ts
  • apps/api/src/main.ts
  • apps/api/src/modules/audit/audit.module.ts
  • apps/api/src/modules/deployments/application/exec-sessions.spec.ts
  • apps/api/src/modules/deployments/application/exec-sessions.ts
  • apps/api/src/modules/deployments/interface/exec-socket.spec.ts
  • apps/api/src/modules/fleet/application/exec-relay.spec.ts
  • apps/api/src/modules/fleet/application/exec-relay.ts
  • apps/api/src/modules/fleet/application/ports.ts
  • apps/api/src/modules/fleet/exec-session-checks.integration.spec.ts
  • apps/api/src/modules/fleet/fleet.module.ts
  • apps/api/src/modules/fleet/infrastructure/exec-session-checks.ts
  • apps/api/src/modules/fleet/interface/exec-stream.spec.ts
  • docs/SPEC.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@samuelmbabhazi
samuelmbabhazi merged commit 661f966 into main Nov 26, 2026
27 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the fix/489-exec-session-closing-audit branch November 26, 2026 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Persist and recover exec-session closing audit events

1 participant