Skip to content

feat(audit): add terminal audit outcomes - #53

Merged
mohanagy merged 5 commits into
developmentfrom
feat/issue-13-audit-outcomes
Jul 11, 2026
Merged

feat(audit): add terminal audit outcomes#53
mohanagy merged 5 commits into
developmentfrom
feat/issue-13-audit-outcomes

Conversation

@mohanagy

@mohanagy mohanagy commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • add one terminal audit scope for every MCP request and typed lifecycle audit records
  • harden JSONL audit output with private permissions, per-path write serialization, failure modes, and audit health
  • apply shared dynamic secret redaction across MCP, audit, lifecycle, health, and stderr boundaries

Validation

  • npm test (243 passing)
  • npm run lint
  • npm run typecheck
  • npm run build
  • npm run check:pack
  • git diff --check

Refs #13

Summary by CodeRabbit

  • New Features
    • Added structured JSONL audit logging for MCP operations and lifecycle transitions, including session/request/profile context, routing/policy/risk metadata where available, stable outcome/error codes, and duration.
    • Introduced configurable audit.failureMode (fail-closed default, fail-open) with explicit audit health behavior and AUDIT_WRITE_FAILED signaling.
    • Improved secret redaction with URI-aware and streaming-safe handling, plus serialized per-audit-path writes and more restrictive audit file/directory permissions.
  • Documentation
    • Updated audit logging, configuration, architecture, security guidance, and examples/presets for the new failure mode and event contract.
  • Tests
    • Expanded coverage for audit outcomes, redaction correctness, sink failure modes, concurrency, and lifecycle events.

mohanagy and others added 3 commits July 11, 2026 09:08
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d305132a-bcf6-4559-bd81-422f87b6a4af

📥 Commits

Reviewing files that changed from the base of the PR and between 3395a59 and 66712f9.

📒 Files selected for processing (2)
  • src/secrets/redact.ts
  • tests/secrets.test.ts

📝 Walkthrough

Walkthrough

Adds structured JSONL audit logging for MCP operations and lifecycle events, shared secret redaction, serialized secure writes, configurable failure modes, centralized server integration, and comprehensive tests and documentation.

Changes

Audit logging

Layer / File(s) Summary
Audit contracts and sink
src/audit/*, src/config/*, src/utils/errors.ts, docs/*, README.md, examples/*
Defines audit event, health, and failure-mode contracts; implements redacted, serialized, restrictive JSONL writes; documents and configures fail-open and fail-closed behavior.
Shared redaction and upstream lifecycle
src/secrets/redact.ts, src/cli/create-runtime.ts, src/upstream/*
Introduces shared SecretRedactor instances, streaming redaction, lifecycle listeners, and upstream start, crash, restart, and shutdown events.
Audited MCP request flow
src/mcp/server/*
Wraps MCP handlers in AuditScope, enriches proxied operations, records lifecycle events, applies safe errors, and redacts returned data.
Audit behavior validation
tests/audit*.test.ts, tests/upstream-manager.test.ts, tests/operation-pipeline.test.ts, tests/secrets.test.ts, tests/config-*.test.ts, tests/mcp-wrapper.test.ts, tests/fixtures/*
Covers audit outcomes, lifecycle records, redaction, failure modes, permissions, concurrency, configuration, and listener isolation.

Estimated code review effort: 4 (Complex) | ~60 minutes

Possibly related issues

Possibly related PRs

  • mohanagy/miftah#41 — Changes the shared secret-redaction behavior used by audit events and other outputs.
  • mohanagy/miftah#43 — Changes the audit configuration schema that this PR extends with audit.failureMode.
  • mohanagy/miftah#51 — Changes upstream lifecycle and restart behavior consumed by audit lifecycle recording.

Poem

I’m a rabbit who logs every hop,
With secrets tucked safely, they never can drop.
JSONL trails and lifecycle cheer,
Fail-open or closed, the rules are clear.
Audit paths bloom in the moonlit night—
Every MCP journey recorded right.


Caution

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • Ignore

❌ Failed checks (1 error)

Check name Status Explanation Resolution
Docstring Coverage ❌ Error Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding terminal audit outcomes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/issue-13-audit-outcomes

Comment @coderabbitai help to get the list of available commands.

@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 8

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/upstream/upstream-process-manager.ts (1)

302-310: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Early start failures skip lifecycle events

PROFILE_NOT_FOUND and the missing-stdio-command guard run before the try, so these reachable get()/restart() paths never publish start-failure/restart-failure. If these errors should be part of the lifecycle record, move the checks into the try or emit the failure event before throwing.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/upstream/upstream-process-manager.ts` around lines 302 - 310, Ensure
early validation failures in the private start() method participate in lifecycle
tracking: move the profile existence and missing-stdio-command checks inside its
try block, or emit the appropriate start-failure/restart-failure event before
rethrowing. Preserve the existing MiftahError codes and messages while ensuring
get() and restart() record these failures consistently.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/audit/audit-logger.ts`:
- Around line 44-52: Reduce redundant filesystem preflights in
AuditLogger.ensureWritable by adding a short-lived, path-aware cache of
successful readiness checks; skip enqueueing prepareFile when the cached success
is still within the chosen TTL, and invalidate it whenever writeLine or
prepareFile fails so fail-closed behavior remains intact. Update the related
log/write flow around ensureWritable and log to reuse this state without
changing failure recording.
- Around line 29-42: Capture the event timestamp before calling enqueue in
AuditLogger.log, then close over that fixed ISO timestamp when constructing the
JSON payload inside the queued write callback. Preserve enqueue ordering and
failure handling while ensuring each audit event records the time log() was
invoked rather than the later write execution time.
- Around line 86-97: Update openAuditFile to explicitly enforce restrictive
permissions on the audit directory after mkdir, regardless of whether it was
newly created or already existed. Apply setRestrictiveMode to the directory path
with mode 0o700 before opening the audit file, while preserving existing error
handling and cleanup behavior.

In `@src/audit/audit-trail.ts`:
- Around line 115-139: Document the fail-closed behavior in `runAudited` and the
audit configuration guidance: when `finish()` fails after a successful
non-idempotent operation, the caller receives `AUDIT_WRITE_FAILED` even though
the side effect may have completed. Warn operators that retries can duplicate
execution, and recommend pairing fail-closed with `restartOnCrash` or downstream
retry handling for destructive/write tools.

In `@src/secrets/redact.ts`:
- Line 122: Update the retainedLength calculation in the relevant write method
to use Array.from(this.secretValues, secret => secret.length) instead of
spreading the Set before mapping, eliminating the intermediate array while
preserving the existing Math.max behavior.
- Around line 111-148: Preserve URI redaction across chunk boundaries in
createTextStream and safeTextBoundary: do not emit a split segment that can
separate a URI scheme from its credentials or query data. Retain enough
preceding text to keep URI tokens parseable, or maintain URI parsing state
across write calls, and ensure flush() redacts the reconstructed complete token
before returning output. Verify the stderr handling in the upstream process
manager remains safe for long newline-free lines.

In `@src/upstream/upstream-process-manager.ts`:
- Around line 884-902: Extract the duplicated notifyListeners and
publishLifecycle dispatch logic from UpstreamProcessManager and
MultiUpstreamProcessManager into a shared listener-notification utility.
Preserve structuredClone handling, listener iteration, error isolation, and the
existing MIFTAH_LISTENER_FAILED warning details, then update both managers to
use the shared implementation and retain their lifecycle publishing behavior.

In `@tests/audit-outcomes.test.ts`:
- Around line 47-583: Extract the repeated temporary-directory, configuration,
server/transport/client construction, connection, and cleanup logic into a
shared test helper such as createWiredServer. Have it accept configuration
overrides and return the connected client, wrapper, auditPath, and cleanup or
async-dispose capability; update the affected tests to use this helper while
preserving each test’s custom profiles, security, audit, and manager options.

---

Outside diff comments:
In `@src/upstream/upstream-process-manager.ts`:
- Around line 302-310: Ensure early validation failures in the private start()
method participate in lifecycle tracking: move the profile existence and
missing-stdio-command checks inside its try block, or emit the appropriate
start-failure/restart-failure event before rethrowing. Preserve the existing
MiftahError codes and messages while ensuring get() and restart() record these
failures consistently.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: e72bbbee-771e-4fa9-bed6-7c59db9fbe72

📥 Commits

Reviewing files that changed from the base of the PR and between ae90e97 and 14655e3.

📒 Files selected for processing (28)
  • README.md
  • docs/architecture.md
  • docs/config.md
  • docs/security.md
  • examples/github.miftah.json
  • examples/multi-upstream.miftah.json
  • src/audit/audit-logger.ts
  • src/audit/audit-trail.ts
  • src/audit/audit-types.ts
  • src/cli/create-runtime.ts
  • src/config/presets.ts
  • src/config/schema.ts
  • src/config/types.ts
  • src/mcp/server/miftah-server.ts
  • src/mcp/server/operation-pipeline.ts
  • src/secrets/redact.ts
  • src/upstream/multi-upstream-process-manager.ts
  • src/upstream/upstream-process-manager.ts
  • src/utils/errors.ts
  • tests/audit-outcomes.test.ts
  • tests/audit.test.ts
  • tests/config-runtime-parity.test.ts
  • tests/config-schema-contract.test.ts
  • tests/fixtures/fake-upstream.mjs
  • tests/mcp-wrapper.test.ts
  • tests/operation-pipeline.test.ts
  • tests/secrets.test.ts
  • tests/upstream-manager.test.ts

Comment thread src/audit/audit-logger.ts
Comment thread src/audit/audit-logger.ts
Comment thread src/audit/audit-logger.ts
Comment thread src/audit/audit-trail.ts
Comment thread src/secrets/redact.ts
Comment thread src/secrets/redact.ts Outdated
Comment thread src/upstream/upstream-process-manager.ts
Comment thread tests/audit-outcomes.test.ts
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohanagy

Copy link
Copy Markdown
Owner Author

Outside-diff lifecycle finding disposition: not changing early PROFILE_NOT_FOUND and missing-stdio-command guards. Supported configuration rejects a missing stdio command before manager construction, and normal profile selection validates profiles before any process attempt. These paths are terminal request-validation errors already captured by the outer audit scope; emitting start-failure/restart-failure without a managed process transition would create inaccurate lifecycle and health records.

@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/secrets/redact.ts`:
- Around line 142-152: Update createTextStream and its suppress helper so
redactedStreamLineMarker only suppresses the current overlong line, not
subsequent input. Reset suppressingOutput when the next line begins or after the
marker is emitted, while preserving the marker for the affected line and
ensuring later write()/flush() calls continue producing output.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 32563dc2-a64c-43a8-8b1b-c1f8dafad578

📥 Commits

Reviewing files that changed from the base of the PR and between 14655e3 and 3395a59.

📒 Files selected for processing (7)
  • README.md
  • docs/config.md
  • docs/security.md
  • src/audit/audit-logger.ts
  • src/secrets/redact.ts
  • tests/audit.test.ts
  • tests/secrets.test.ts

Comment thread src/secrets/redact.ts
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant