Skip to content

Conversation

@jackschultz
Copy link
Owner

Summary

This PR establishes the JSON envelope structure and patterns for agent-friendly diagnostic outputs. It's the foundation for Phase 1 of the pgcrate roadmap.

Breaking Change: JSON output structure changed from flattened to nested data field. Consumers must update to read from data and handle new envelope fields.

Key Changes

  • Reason codes module - 27 stable codes across 3 categories (operational, policy, capability) for automation-friendly error handling
  • DiagnosticOutput envelope v2.0.0:
    • Added tool_version, generated_at, severity, partial fields
    • Added warnings[] and errors[] arrays with structured ReasonInfo
    • Command-specific data now nested under data field (no longer flattened)
  • New commands:
    • pgcrate context - Connection context, server info, extensions, privileges
    • pgcrate capabilities - Permission-aware feature discovery
  • JSON schemas - All schemas now use $ref composition with envelope.schema.json
  • JsonError consistency - Error responses now use same envelope structure as success responses

Review Feedback Addressed

  • JsonError uses envelope structure for consistent JSON contracts
  • SQLSTATE 57014 correctly disambiguates statement_timeout vs query_cancelled
  • data_directory gated behind --no-redact flag
  • Removed unused --include-fixes flag (was no-op)
  • Fixed indexes command to return Severity::Warning when findings exist
  • SQL injection fix in capabilities.rs (parameterized queries)
  • Cleaned up dead code with appropriate #[allow(dead_code)] annotations

Test plan

  • All 554 tests pass (cargo test)
  • cargo fmt passes
  • cargo clippy clean
  • Manual testing of new commands:
    • pgcrate context --json
    • pgcrate capabilities --json
    • pgcrate triage --json (verify new envelope structure)
  • Verify JSON error responses match envelope structure

This is a breaking change that establishes the JSON envelope structure
and patterns for agent-friendly diagnostic outputs.

Changes:
- Add reason_codes module with 27 codes across 3 categories
  (operational, policy, capability)
- Update DiagnosticOutput envelope (v2.0.0):
  - Add tool_version, generated_at, severity, partial fields
  - Add warnings[] and errors[] arrays with ReasonInfo
  - Data is now nested under 'data' field (no longer flattened)
- Update all diagnostic commands (triage, locks, xid, sequences, indexes)
  to derive severity and convert skipped checks to warnings
- Add 'pgcrate context' command for connection/server/privilege info
- Add 'pgcrate capabilities' command for permission-aware feature discovery
- Add --include-fixes flag to triage for structured actions
- Add JSON schema files for envelope, context, capabilities, action

Breaking: JSON output structure changed. Consumers must update to read
from nested 'data' field and handle new envelope fields.
- Remove unused actions.rs (NextAction in triage.rs is sufficient)
- Remove SkipReason, use ReasonCode directly in triage.rs
- Remove unused is_retryable() from ReasonCode
- Remove unused _quiet params from print_human functions
- Fix SQL injection pattern in capabilities.rs (use parameterized queries)
- Simplify classify_error to return ReasonCode directly
- Update integration tests for new data wrapper structure
- JsonError now uses envelope structure (schema_id, schema_version,
  tool_version, generated_at, severity, errors[], data) matching
  DiagnosticOutput for consistent JSON contracts
- Fix SQLSTATE 57014 disambiguation: check message for "statement
  timeout" before returning QueryCancelled
- Gate data_directory behind --no-redact flag (sensitive path info)
- Remove --include-fixes flag (was no-op placeholder)
- Fix indexes.rs to return Severity::Warning when findings exist
- Delete unused DiagnosticError/DiagnosticErrorCode types
- Add #[allow(dead_code)] to intentionally kept unused code
- Update all JSON schemas to use $ref composition with envelope.schema.json
  instead of duplicating envelope properties
- Update integration tests for new errors[] structure
- Add Diagnostics section to README with new commands
- Add context, capabilities, locks, xid, sequences, indexes to Commands table
- Update CI/CD section with JSON envelope example
- Add Unreleased section to CHANGELOG documenting Phase 1 changes
- Fix needless_borrow warnings (remove & from client references)
- Fix unneeded_struct_pattern (Commands::Triage is now unit variant)
- Fix manual_strip warning (use strip_suffix instead of ends_with + slice)
- Fix vec_init_then_push (use vec![] macro in capabilities.rs)
- Run cargo fmt
@jackschultz jackschultz merged commit c7d46cb into main Jan 19, 2026
8 checks passed
@jackschultz jackschultz deleted the feature/phase1-json-contracts branch January 19, 2026 14:09
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.

2 participants