Skip to content

test(plugins/agento11y): drive the real Claude Code and Codex CLIs against a fake model - #771

Open
adamshovav wants to merge 2 commits into
grafana:mainfrom
adamshovav:test/agent-cli-integration
Open

adamshovav wants to merge 2 commits into
grafana:mainfrom
adamshovav:test/agent-cli-integration

Conversation

@adamshovav

Copy link
Copy Markdown
Contributor

Refs #146. The first commit is #769, which the Claude run needs (#768); the harness is the second.

TestAgentIntegration loads each plugin from this checkout, runs the CLI headless against a fake model on loopback, and checks the export with #190's capture server, secret guard, and invariants. The golden scenarios replay hook payloads written by hand. Here the agent writes them, so a release that changes a payload, a transcript, or when a hook fires fails the run.

Run. One prompt carrying a Grafana Cloud token, one echo tool call, its result, and a final reply. Dummy keys and a throwaway HOME; it passes with outbound network blocked.

  • Claude Code: claude -p --plugin-dir plugins/claude-code with ANTHROPIC_BASE_URL. Checks 1 export, 2 generations, the session ID, tool call, tool result, reply, and redacted token.
  • Codex: codex plugin marketplace add and codex plugin add, then codex exec with a model_providers entry. Checks 1 export, 1 generation, the tool call and result IDs, reply, and redacted token.

The fake answers the Anthropic Messages and OpenAI Responses streaming APIs: a tool call first, then the reply once a request carries the call ID back.

Reuse. startHookExport, checkExports, and newLoopbackServer move out of runGoldenScenario and newGoldenServer unchanged. The golden tests pass as before.

CI. The CLIs are pinned in testdata/agent-clis: package.json and lockfile, installed with npm ci, listed in the dependency-pinning check, with allowScripts for claude-code's postinstall to match the pnpm allowBuilds policy. The step runs in the plugins/agento11y Go job and adds about 25s. zizmor reports nothing new. Codex runs with its sandbox off because it can't nest inside the runner's container; the only command the fake issues is echo.

Locally it's opt-in:

cd plugins/agento11y/internal/entry
npm ci --prefix testdata/agent-clis
PATH=$PWD/testdata/agent-clis/node_modules/.bin:$PATH AGENT_CLI_TESTS=claude,codex go test . -run TestAgentIntegration -count=1 -v

The test clears CLAUDE*, ANTHROPIC_*, CODEX_*, and OPENAI_* first: started from inside an agent session, the CLIs inherit variables that change the run, and CLAUDE_CODE_EAGER_FLUSH alone hides #768.

Checked. Dropping tool results from the Claude mapper, the tool call ID from the Codex mapper, or prompt redaction fails the run. The full CI matrix passes on this branch. With #770, the Claude run drops from about 4s to under 1s.

…to land

A headless `claude -p` run can fire Stop before its transcript writes
land. Depending on timing, the file does not exist yet, or ends at the
turn that called a tool: readTranscriptSettled either fails on the open
or settles on that complete tool-use turn, and the SessionEnd that would
catch up is cancelled when -p exits. The session exports nothing, or only
the tool call. With CLAUDE_CODE_EAGER_FLUSH set, as in sessions the Claude
desktop app hosts, the writes land in time, which hides the race.

A Stop means the turn is over, so on Stop the settle read also waits,
within the existing window, until the last complete assistant turn ended
the turn rather than stopping for a tool, and treats a missing file as
not yet written. Other events are unchanged, so a SessionEnd for a
session that never wrote a transcript does not delay the exit.
…ainst a fake model

TestAgentIntegration runs each CLI headless against a fake server for the
Anthropic Messages and OpenAI Responses APIs, then checks the export with
TestGoldenIntegration's capture server, secret guard, and invariants. The
golden scenarios replay hook payloads written by hand; here the agent
produces them, so a release that changes a payload, a transcript, or when
a hook fires fails the run.

The setup and checks the two tests share move out of runGoldenScenario,
unchanged, into startHookExport, checkExports, and newLoopbackServer.

CI installs the CLI versions pinned in testdata/agent-clis with npm ci and
runs the test in the plugins/agento11y Go module job.

Refs grafana#146

This branch has not been deployed

No deployments
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