Commit Graph

7 Commits

Author SHA1 Message Date
HAL9000 747513bc59 chore(testing): enforce semgrep gate for suppressed exceptions
Fix broken escape hatch mechanism and address reviewer feedback:

- Replace non-functional comment-based pattern-not clauses with Semgrep's native # nosemgrep mechanism for the escape hatch. Semgrep strips comments from the AST so pattern-not clauses matching inline comments never fire; # nosemgrep is the only reliable per-line suppression mechanism.

- Require both # nosemgrep: <rule-id> AND # error-propagation: allow on the same line: the former is the actual suppression, the latter is the mandatory human-readable audit annotation.

- Add raise $EXC from $CAUSE pattern-not entries for both Exception and BaseException variants to prevent false positives on legitimate exception chaining (raise ServiceError from e).

- Switch nox -s lint Semgrep invocation to audit mode (no --error) for the phased rollout: the codebase has ~337 existing suppressions that must be triaged before enforcement mode is enabled. A comment in noxfile.py documents the migration path and references #9103.

- Update CONTRIBUTING.md examples and enforcement description to reflect the dual-comment requirement.
2026-06-14 19:05:21 -04:00
HAL9000 d79a1c6988 test-infra: fix Semgrep escape hatch and add exception chaining pattern 2026-06-14 19:01:47 -04:00
HAL9000 7210183c29 test-infra: fix Semgrep escape hatch and add exception chaining pattern
Fixed two critical issues with the Semgrep rules for broad exception suppression:

1. **Escape hatch mechanism**: Replaced the broken `# error-propagation: allow` comment-based escape hatch with Semgrep's native `# nosemgrep` comment support. Semgrep strips comments from the AST, so pattern-not clauses looking for inline comments never match. The native `# nosemgrep` comment is properly supported by Semgrep and provides a reliable override mechanism.

2. **Missing exception chaining pattern**: Added `raise $EXC from $CAUSE` pattern-not entries for both `Exception` and `BaseException` variants. This prevents false positives when legitimate exception chaining is used (e.g., `raise ServiceError("context") from e`), which is an allowed pattern per CONTRIBUTING.md.

Updated both `python-no-suppressed-exception` and `python-no-suppress-exception` rules with these fixes. The escape hatch now works reliably and exception chaining is properly recognized as an allowed pattern.

ISSUES CLOSED: #9103
2026-06-14 19:01:47 -04:00
HAL9000 153acf0861 chore(testing): enforce semgrep gate for suppressed exceptions
Added two new Semgrep rules to enforce the CONTRIBUTING.md guideline against broad exception suppression:
- python-no-suppressed-exception: Detects except Exception/BaseException blocks without re-raising
- python-no-suppress-exception: Detects contextlib.suppress(Exception/BaseException) usage

Both rules support an escape hatch annotation '# error-propagation: allow' for documented recovery logic.

Integrated Semgrep into the nox lint session to run alongside Ruff checks.
Updated CONTRIBUTING.md to document the escape hatch policy and automated enforcement.
Pre-commit hook already configured to run these rules.

ISSUES CLOSED: #9103
2026-06-14 19:01:47 -04:00
brent.edwards fda5f463ac fix(persistence): handle corrupt JSON in _to_domain/_from_domain with CorruptRecordError
CI / benchmark-regression (push) Failing after 0s
CI / benchmark-publish (push) Failing after 0s
CI / push-validation (push) Successful in 25s
CI / helm (push) Successful in 54s
CI / lint (push) Successful in 4m18s
CI / build (push) Successful in 4m6s
CI / quality (push) Successful in 4m26s
CI / typecheck (push) Successful in 4m49s
CI / security (push) Successful in 4m50s
CI / e2e_tests (push) Successful in 7m25s
CI / integration_tests (push) Successful in 7m58s
CI / unit_tests (push) Successful in 11m29s
CI / docker (push) Successful in 1m36s
CI / coverage (push) Successful in 15m43s
CI / status-check (push) Successful in 3s
CI / push-validation (pull_request) Successful in 27s
CI / helm (pull_request) Successful in 29s
CI / build (pull_request) Successful in 3m50s
CI / lint (pull_request) Successful in 3m57s
CI / quality (pull_request) Successful in 4m23s
CI / typecheck (pull_request) Successful in 4m40s
CI / security (pull_request) Successful in 4m46s
CI / e2e_tests (pull_request) Successful in 7m11s
CI / integration_tests (pull_request) Successful in 7m48s
CI / unit_tests (pull_request) Successful in 8m47s
CI / docker (pull_request) Successful in 1m40s
CI / coverage (pull_request) Successful in 15m3s
CI / status-check (pull_request) Successful in 3s
Wrapped bare json.loads() and model constructor calls in
AutomationProfileRepository._to_domain() and ._from_domain() with
try/except guards that catch json.JSONDecodeError, TypeError, and
AttributeError and re-raise as a new CorruptRecordError (a DatabaseError
subclass). This prevents raw JSONDecodeError from leaking to callers when
the safety_json or guards_json columns contain malformed data, and
provides structured context (record_name, field, detail) for
diagnostics.

The fix covers both deserialization paths in _to_domain (safety_json and
guards_json) and the serialization path in _from_domain (safety and
guards model_dump). Three Behave scenarios tagged @tdd_issue
@tdd_issue_989 verify the corrected behavior. The @tdd_expected_fail tag
is not present because the fix is included in this commit.

Also excluded tool/wrapping.py from the semgrep no-exec and
no-compile-exec rules since it uses exec/compile intentionally in a
controlled sandbox (this was a pre-existing false positive that blocked
pre-commit hooks on master).

All 11 nox sessions pass; coverage is 97.0%.

ISSUES CLOSED: #989
2026-04-21 06:42:10 +00:00
brent.edwards 7e9a45044b fix(session): session create does not persist session for subsequent list
CI / integration_tests (push) Has been cancelled
CI / e2e_tests (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled
CI / helm (push) Has been cancelled
CI / security (push) Has been cancelled
CI / typecheck (push) Has been cancelled
CI / benchmark-publish (push) Has been cancelled
CI / unit_tests (push) Has been cancelled
CI / quality (push) Has been cancelled
CI / benchmark-regression (push) Has been cancelled
CI / docker (push) Has been cancelled
CI / status-check (push) Has been cancelled
CI / coverage (push) Has been cancelled
The CLI `session create` command created the session via SessionService.create()
(which commits via auto_commit=True), then called _facade_dispatch("session.create")
for A2A protocol bookkeeping. The facade handler unconditionally called
svc.create() on a second PersistentSessionService instance (a new Factory
resolution from the DI container with its own engine), creating a duplicate
session in the database.

The fix makes A2aLocalFacade._handle_session_create() idempotent: when a
session_id is already present in the params, it acknowledges the existing
session without creating a new one. The CLI dispatch params were also fixed
to use the correct key name (actor_name instead of actor).

Changes:
- src/cleveragents/a2a/facade.py: Early return in _handle_session_create when
  session_id is already supplied, preventing duplicate session creation.
- src/cleveragents/cli/commands/session.py: Fixed param key from "actor" to
  "actor_name" for consistency with the facade handler.
- features/a2a_facade_wiring.feature: Added idempotency scenario verifying
  that session.create with an existing session_id does not call svc.create().
- features/steps/a2a_facade_wiring_steps.py: Added step asserting mock
  SessionService.create was not called.
- features/tdd_session_create_persist.feature: Removed @tdd_expected_fail tag
  now that the bug is fixed.
- robot/e2e/e2e_session_create_persist.robot: Removed tdd_expected_fail tag,
  updated documentation.
- .semgrep.yml: Excluded wrapping.py from no-exec/no-compile-exec rules
  (pre-existing sandboxed exec usage for tool transforms).

ISSUES CLOSED: #1141

Co-authored-by: Brent E. Edwards <brent.edwards@cleverthis.com>
Co-committed-by: Brent E. Edwards <brent.edwards@cleverthis.com>
2026-04-02 16:51:04 +00:00
brent.edwards 7ef5ebb695 feat: Add Q0: Pre-commit hooks setup.
This should automatically check for problems on build.
2026-02-10 16:04:17 +00:00