Fix/agent session key normalization#15707
Conversation
Additional Comments (1)
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/sessions/session-key-utils.ts
Line: 17:20
Comment:
**Case-sensitive parseAgentSessionKey**
`parseAgentSessionKey` checks `parts[0] !== "agent"` using the original casing, but `classifySessionKeyShape` treats any `raw.toLowerCase().startsWith("agent:")` as `malformed_agent`. This means inputs like `Agent:main:main` will be misclassified as malformed and rejected by the new guards, even though the rest of the key is structurally valid (and other code paths lowercase agent keys, e.g. `toAgentStoreSessionKey`). Consider normalizing `parts[0]` (and/or all `parts`) to lowercase before comparison so valid agent-prefixed keys aren’t rejected just due to casing.
How can I resolve this? If you propose a fix, please make it concise. |
3075d5a to
ab0a699
Compare
ab0a699 to
2b9b5b3
Compare
|
PR #15707 - Fix/agent session key normalization (#15707) Merged via squash.
Thanks @rodrigouroz! |
Summary
This PR hardens gateway agent session-key validation so malformed agent-prefixed keys are rejected early and consistently.
Problem
Malformed values like
agent:mainwere not being rejected up front in some agent paths.Instead, they could flow deeper into runtime/workspace resolution and fail later with
UNAVAILABLE-style errors, which made the issue look like infra/runtime instabilityinstead of request validation.
Why this matters
agent:*keys have a strict shape and malformed values are client input errors.INVALID_REQUESTearly gives clients a stable, actionable contract.Root Cause / Context
agenthandler acceptedsessionKeystrings and trimmed them, but did not explicitly reject malformedagent:*shapes before execution.agent.identity.getsimilarly derived agent identity fromsessionKeywithout an explicit malformed-shape guard.fix(sessions): normalize absolute sessionFile paths for v2026.2.12 compatibility), but that does not covermalformed
agent:*request validation in these handlers.Fix
src/gateway/server-methods/agent.tsIn
agent:classifySessionKeyShape(...)malformed_agent, return:INVALID_REQUESTinvalid agent params: malformed session key "<key>"In
agent.identity.get:INVALID_REQUESTinvalid agent.identity.get params: malformed session key "<key>"Tests
src/gateway/server-methods/agent.test.tsagentagent.identity.getsrc/gateway/server.agent.gateway-server-agent-a.e2e.test.tsagentrejects malformed agent-prefixed session keysVerification run
pnpm test src/gateway/server-methods/agent.test.tspnpm vitest run --config vitest.e2e.config.ts src/gateway/server.agent.gateway-server-agent-a.e2e.test.ts -t "agent rejects malformed agent-prefixed session keys"Risk / Compatibility
Low risk:
agent:*inputsGreptile Overview
Greptile Summary
This PR adds early validation to the gateway
agentandagent.identity.gethandlers to reject malformedagent:*session keys usingclassifySessionKeyShape(...), returningINVALID_REQUESTinstead of allowing downstream failures.It also adds unit + e2e coverage to ensure malformed
agent:-prefixed keys (e.g.agent:main) fail fast and do not dispatchagentCommand.Confidence Score: 4/5
agent:maincase. However, session-key shape classification relies onparseAgentSessionKey, which currently treats theagentprefix as case-sensitive, so keys likeAgent:...will be rejected as malformed by the new guards even though they are structurally valid elsewhere in the codebase that lowercases agent keys.Last reviewed commit: dc3a0fa