Skip to content

feat(guard): [ADS-1758] identify the runtime hook surface - #561

Open
jakub-kassak wants to merge 4 commits into
mainfrom
feat/ADS-1758-fleet
Open

jakub-kassak wants to merge 4 commits into
mainfrom
feat/ADS-1758-fleet

Conversation

@jakub-kassak

@jakub-kassak jakub-kassak commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Regular hooks and runtime discovery send bounded environment markers in X-Agent-Surface-Evidence. Agent Monitor classifies the surface. Installer events omit both surface headers.

Release after snyk/agent-monitor#601 is deployed. The surface guide documents the marker rules. The Copilot VS Code marker was seen in the runtime process and still needs confirmation from a successful live hook.


Note

Medium Risk
Changes the hook wire contract and all forwarder paths Agent Monitor uses for surface attribution; incorrect evidence or installer leakage could misclassify runtime hooks across clients.

Overview
Runtime hook delivery now reports which agent surface actually invoked the hook by sending a JSON X-Agent-Surface-Evidence header with client-specific environment markers (Claude, Codex, Cursor, and Copilot each have their own set). X-Agent-Surface and Copilot-only classification are removed; Agent Monitor is expected to classify surfaces from this evidence.

Installer/setup traffic (hooksConfigured, server discovery during install, unconfirmed test events) does not attach surface evidence, so a guard install run inside an agent session is not misread as runtime. guard strips those marker env vars when it subprocesses hook scripts during install verification for the same reason.

Values are bounded and sanitized (per-client marker lists, 128 code-point cap, null for sensitive Cursor paths/sockets and oversize values) in Python, bash, and PowerShell forwarders. Unit and e2e tests assert the install-vs-runtime split and shared wire cases across forwarders and send_hook_event.

Reviewed by Cursor Bugbot for commit c2d8b1f. Bugbot is set up for automated code reviews on this repo. Configure here.

@jakub-kassak
jakub-kassak force-pushed the feat/ADS-1758-fleet branch 5 times, most recently from 79d54b2 to 87099a6 Compare November 24, 2026 13:27
@jakub-kassak
jakub-kassak changed the base branch from refactor/hook-config-file to refactor/hook-config-file-v2 November 24, 2026 13:27
@jakub-kassak
jakub-kassak marked this pull request as ready for review November 24, 2026 14:04
@jakub-kassak
jakub-kassak requested review from a team and marcelosousa as code owners November 24, 2026 14:04
@qodo-merge-etso

qodo-merge-etso Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Report Guard installation metadata and runtime agent surfaces

✨ Enhancement 🧪 Tests 🕐 40+ Minutes

Grey Divider

AI Description

• Persist install provenance and platform observations so hooks report a consistent fleet snapshot.
• Report client-specific runtime surfaces and evidence while marking installer events not
 applicable.
• Add cross-platform coverage for reinstall, discovery, and hook wire behavior.
Merge Risk 🟡 Medium

Blast radius 🟡 Medium 💥 2 capabilities

  • 2 affected capabilities within one bounded functional scope: agent-hook telemetry integration.
  • This is one bounded integration outcome: runtime hook events are forwarded to Agent Monitor with client-specific surface evidence.
  • The Python sender and POSIX/PowerShell forwarders are propagation hops for the same runtime attribution behavior, not independent capabilities.
  • This is a separate outcome within the same bounded integration: installation verification must report setup events without inheriting runtime-agent markers.
  • It is distinct from runtime attribution because it concerns setup correctness and telemetry classification, not events generated by an active agent hook.
Sources

src/agent_scan/guard.py[2150-2154]

    if env is None:
        env = dict(os.environ)
    # This subprocess verifies the install. Its inherited agent markers do not describe a hook invocation.
    for marker in SURFACE_EVIDENCE_MARKERS.get(hook_client, ()):
        env.pop(marker, None)

src/agent_scan/hooks/snyk-agent-guard.sh[371-374]

  local evidence
  evidence="$(get_surface_evidence "$client")"
  if [[ -n "$evidence" ]]; then
    curl_args+=(-H "X-Agent-Surface-Evidence: ${evidence}")
Behavioral complexity 🟡 Medium 🧩 Compatibility cases, Interacting branches

  • Before, only Copilot produced a derived surface string.
  • After, four clients select different marker sets, preserve absent versus empty values, cap values, encode Unicode, and emit a new header.
  • The new correctness conditions are client-specific marker selection, installer-event exclusion, cross-platform serialization equivalence, and safe handling of unsupported values.
  • The branches are newly interacting behavior rather than merely reused mapping infrastructure.
Sources

src/agent_scan/hooks/snyk-agent-guard.sh[371-374]

  local evidence
  evidence="$(get_surface_evidence "$client")"
  if [[ -n "$evidence" ]]; then
    curl_args+=(-H "X-Agent-Surface-Evidence: ${evidence}")
Potential damage 🟡 Medium 🩹 Incorrect results

  • If the changed evidence contract produces wrong or unusable values, Agent Monitor could attribute runtime hook events to the wrong surface or to no surface.
  • The existing endpoint and hook validation path otherwise remain unchanged; downstream handling is not available in this repository.
  • Reverting restores the prior sender behavior for future events, but already misattributed telemetry would require downstream repair or replay if the service stores it.
Sources

src/agent_scan/hooks/snyk-agent-guard.sh[371-374]

  local evidence
  evidence="$(get_surface_evidence "$client")"
  if [[ -n "$evidence" ]]; then
    curl_args+=(-H "X-Agent-Surface-Evidence: ${evidence}")
Assessed commit c2d8b1f
Diagram

graph TD
  I["Guard installer"] --> P["Platform collector"] --> C[("Guard config")] --> F["Hook forwarders"] --> M["Agent Monitor"]
  C --> D["Discovery hook"] --> S["Python sender"] --> M
  I --> S
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Route every hook through the Python sender
  • ➕ One implementation of metadata and surface reporting would reduce cross-language drift.
  • ➖ Standalone hooks would depend on starting the CLI for every event, increasing latency and changing their failure behavior.

Recommendation: Keep the installed snapshot and standalone forwarders: they preserve install-time facts without adding a CLI dependency to each hook. The shared surface cases and end-to-end tests are important because this approach maintains the wire contract in three languages.

Files changed (12) +1376 / -104

Enhancement (7) +523 / -58
guard.pyPersist installation facts and distinguish installer telemetry +67/-9

Persist installation facts and distinguish installer telemetry

• Collects platform, source, and mode during installation and reads the installed metadata for discovery and fallback telemetry. Marks installer events explicitly and prevents inherited runtime markers from reaching the installer handshake.

src/agent_scan/guard.py

guard_platform.pyCollect bounded, install-time platform observations +213/-0

Collect bounded, install-time platform observations

• Adds best-effort OS version, kernel release, and process-architecture collection across Linux, macOS, and Windows using the ADS platform vocabulary. Omits unavailable observations.

src/agent_scan/guard_platform.py

hook_events.pyAdd fleet metadata and runtime surface headers to Python events +59/-17

Add fleet metadata and runtime surface headers to Python events

• Includes installed metadata in X-User and reports client-specific surface and bounded marker evidence for runtime events. Installer events instead report not_applicable without runtime evidence.

src/agent_scan/hook_events.py

snyk-agent-guard-discover.ps1Pass the installed config path to Windows discovery +2/-0

Pass the installed config path to Windows discovery

• Sets the discovery child's config path from the adjacent installation file, replacing any inherited path.

src/agent_scan/hooks/snyk-agent-guard-discover.ps1

snyk-agent-guard-discover.shPass an absolute installed config path to POSIX discovery +3/-0

Pass an absolute installed config path to POSIX discovery

• Resolves the adjacent config path and exports it for the discovery child, replacing any inherited path.

src/agent_scan/hooks/snyk-agent-guard-discover.sh

snyk-agent-guard.ps1Report installed facts and runtime surfaces from PowerShell hooks +77/-23

Report installed facts and runtime surfaces from PowerShell hooks

• Adds installation metadata to X-User and identifies Claude Code, Codex, and Cursor surfaces while retaining Copilot routing behavior. Installer events report not_applicable; runtime events can include bounded, client-specific evidence.

src/agent_scan/hooks/snyk-agent-guard.ps1

snyk-agent-guard.shReport installed facts and runtime surfaces from Bash hooks +102/-9

Report installed facts and runtime surfaces from Bash hooks

• Reads persisted fleet metadata into X-User and adds client-specific surface and evidence headers. A dedicated installer flag suppresses runtime evidence and reports not_applicable.

src/agent_scan/hooks/snyk-agent-guard.sh

Tests (5) +853 / -46
test_guard_install.pyExercise installed metadata and surface reporting end to end +496/-1

Exercise installed metadata and surface reporting end to end

• Covers user and managed installs, reinstall snapshots, platform and architecture observations, inherited-config isolation, and installer-versus-runtime headers. Includes native Windows and installed PowerShell scenarios.

tests/e2e/test_guard_install.py

_hook_surface_cases.pyShare runtime surface expectations across sender tests +61/-0

Share runtime surface expectations across sender tests

• Defines client marker fixtures, expected surfaces, and evidence expectations used to compare Python discovery with installed forwarders.

tests/unit/_hook_surface_cases.py

test_guard.pyVerify forwarder surface headers and installer isolation +148/-38

Verify forwarder surface headers and installer isolation

• Adds Bash and PowerShell wire tests for client-scoped markers, missing or empty evidence, and installer events. Adjusts install assertions for the additional persisted metadata.

tests/unit/test_guard.py

test_guard_platform.pyTest platform collection at OS boundaries +111/-0

Test platform collection at OS boundaries

• Checks Windows NT build and process architecture reporting, failed probes, and Linux distribution fallbacks.

tests/unit/test_guard_platform.py

test_hook_events.pyVerify Python surface and evidence wire behavior +37/-7

Verify Python surface and evidence wire behavior

• Tests client-specific runtime markers, evidence headers, and the distinction between installer and session discovery events, including machine serial reporting.

tests/unit/test_hook_events.py

@qodo-merge-etso

qodo-merge-etso Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Copilot events without markers get a new label 🐞 Bug ≡ Correctness
Description
For github-copilot, get_agent_surface in the Bash and PowerShell forwarders now returns
unknown when neither AI_AGENT nor COPILOT_CLI identifies a surface, while Python's
agent_surface still returns None and omits the header. Markerless Copilot hook events therefore
send X-Agent-Surface: unknown and bypass Agent Monitor's former event-type fallback, while
session-start discovery from the same environment sends no surface header.
Code

src/agent_scan/hooks/snyk-agent-guard.sh[R241-249]

+  elif [[ "$client" != "github-copilot" ]]; then
+    printf '%s' "unknown"
+  elif [[ "${AI_AGENT:-}" == "github_copilot_vscode_agent" ]]; then
    printf '%s' "copilot-vscode"
  elif [[ "${AI_AGENT:-}" == github_copilot_* || -n "${COPILOT_CLI:-}" ]]; then
    printf '%s' "copilot"
+  else
+    printf '%s' "unknown"
  fi
Relevance

●●● Strong

Code contradicts the PR's stated intent (omit header when marker absent) and the Python test
expecting None; a consistency fix is likely accepted. No rejection precedent found.

PR-#544

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
Both forwarders now explicitly use unknown in the unmatched Copilot branch, where they previously
left the value empty; the removed PowerShell comment explained that an empty value omits the header
and lets Agent Monitor fall back to attribution by event type. Python's agent_surface still
returns None for that environment and adds the header only for a non-None value. The tests
reflect the split: forwarder tests expect unknown for ("github-copilot", {}), while
test_hook_events expects None for {}. This also conflicts with the PR description's claim that
Copilot keeps its existing routing values.

src/agent_scan/hooks/snyk-agent-guard.sh[241-249]
src/agent_scan/hooks/snyk-agent-guard.ps1[192-200]
src/agent_scan/hook_events.py[175-180]
src/agent_scan/hooks/snyk-agent-guard.sh[243-249]
src/agent_scan/hooks/snyk-agent-guard.ps1[194-200]
src/agent_scan/hook_events.py[175-181]
src/agent_scan/hook_events.py[276-288]
PR-#468

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
For a `github-copilot` session without recognized Copilot markers, the Bash and PowerShell forwarders send `X-Agent-Surface: unknown`, while Python session-start discovery omits the header. This changes hook-event attribution by bypassing Agent Monitor's former event-type fallback and reports different surface values for the same session.

## Fix Focus Areas
- src/agent_scan/hooks/snyk-agent-guard.sh[241-249]
- src/agent_scan/hooks/snyk-agent-guard.ps1[192-200]
- src/agent_scan/hook_events.py[175-180]
- tests/unit/test_guard.py[3943-3947]

## Recommended Fix
Use one behavior across all three senders. To preserve Copilot's existing routing values, leave the surface empty in both forwarders when neither `AI_AGENT` nor `COPILOT_CLI` identifies it, so they omit the header as Python discovery does; update the markerless forwarder test expectations and verify they match discovery. If Agent Monitor #601 instead requires `unknown`, return `"unknown"` from Python's `agent_surface` for markerless `github-copilot` as well, and update the `test_hook_events` expectations.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


Grey Divider

Context sources
❌ Tickets: failed to load — Ticket collection could not establish complete evidence
✅ Compliance rules (platform): 6 rules
✅ Cross-repo context — repo relationships
✅ Context preparation
  Past issues: summarized; full text retained for this review
Review mode: Auto: 🧠 Deep: Dense cross-platform installer, hook, telemetry, and metadata logic across independent paths.

Grey Divider

Tip of the day
💡 Did you know, you can hide the parts of a finding you never read, like the evidence or the agent prompt

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8fd2e45. Configure here.

Comment thread src/agent_scan/hook_events.py Resolved
@jakub-kassak jakub-kassak changed the title [ADS-1758] Report Agent Guard fleet metadata feat(guard): [ADS-1758] identify the runtime hook surface Nov 25, 2026
@jakub-kassak
jakub-kassak changed the base branch from refactor/hook-config-file-v2 to feat/ADS-1758-system-details November 25, 2026 10:09
@jakub-kassak
jakub-kassak force-pushed the feat/ADS-1758-fleet branch 2 times, most recently from 544dc72 to 1bfcc9c Compare November 25, 2026 11:11
@jakub-kassak
jakub-kassak changed the base branch from feat/ADS-1758-system-details to main November 25, 2026 11:11
@jakub-kassak
jakub-kassak force-pushed the feat/ADS-1758-fleet branch 5 times, most recently from f1251f4 to fdb2d92 Compare November 25, 2026 13:03
@jakub-kassak

Copy link
Copy Markdown
Contributor Author

The Qodo finding “Copilot events without markers get a new label” is addressed in the current diff. Bash, PowerShell, and Python discovery now send the same bounded evidence format. When both AI_AGENT and COPILOT_CLI are unset, all three omit both surface headers.

Agent Monitor #601 records that request as surface=missing and preserves the generic vscode route. An explicit legacy X-Agent-Surface: unknown still records surface=unknown. The event-name override applies to installer events, which remain attributed to github-copilot.

Coverage includes the shared markerless sender case, the Bash and PowerShell forwarder test, the Python discovery test, and the receiver’s log and metric assertions.

Comment thread README.md Resolved
Related: ADS-1758

Co-Authored-By: Codex <codex@openai.com>
jakub-kassak and others added 3 commits November 30, 2026 09:29
Send the Cursor socket marker without its path in all hook senders.
Cover Codex CLI hooks without an originator and remove the internal
documentation link from the public README.

Related: ADS-1758
Co-authored-by: FORGE <forge@snyk.io>
Encode Unicode in the evidence header and bound values to 128 code
points across Bash, PowerShell, and Python. Avoid switching Bash's
locale while preparing the header.

Related: ADS-1758
Co-authored-by: FORGE <forge@snyk.io>
Filter inherited surface markers in the installer's child process
while keeping the Windows argument renderer's original contract.

Related: ADS-1758
Co-authored-by: FORGE <forge@snyk.io>
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