Skip to content

SANDBOX-1810: Implement pod cache and session manager - #12

Merged
fbm3307 merged 3 commits into
masterfrom
SANDBOX-1810-pod-cache-session-manager
Jul 1, 2026
Merged

SANDBOX-1810: Implement pod cache and session manager#12
fbm3307 merged 3 commits into
masterfrom
SANDBOX-1810-pod-cache-session-manager

Conversation

@fbm3307

@fbm3307 fbm3307 commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Implements pkg/session — the pod cache and session manager for per-investigation sandbox pods (SANDBOX-1810).

  • Three-tier pod lookup: in-memory cache → K8s label discovery → idempotent create
  • Thread-safe PodCache with 30s TTL to avoid K8s API calls on every command
  • Hardened pod spec: non-root UID 1001, drop ALL caps, readiness probe
  • HMAC-SHA256 auth tokens delivered via K8s Secret → env var
  • Explicit cleanup (DELETE /sessions/{id}) + 30-minute idle TTL safety net

Assisted by: Cursor

Summary by CodeRabbit

  • New Features
    • Introduced a TTL-based in-memory pod cache and configurable sandbox session settings.
    • Added a Kubernetes-backed session manager that creates/authenticates sandbox pods on demand, reuses ready pods, proxies commands to the sandbox agent, and supports stale/inactive cleanup.
  • Bug Fixes
    • Improved resilience by expiring stale cache entries on failed communications and validating session identifiers.
  • Tests
    • Added comprehensive unit tests for caching, lifecycle management, cleanup, and command proxying.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 080e66f0-3059-4ed6-927e-8937688c2e4c

📥 Commits

Reviewing files that changed from the base of the PR and between 7a90257 and dd7948a.

📒 Files selected for processing (3)
  • pkg/session/config.go
  • pkg/session/manager.go
  • pkg/session/manager_test.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • codeready-toolchain/mcp-common (manual)
  • codeready-toolchain/mcp-server-devsandbox (manual)
  • codeready-toolchain/api (manual)
  • codeready-toolchain/toolchain-common (manual)
  • codeready-toolchain/host-operator (manual)
  • codeready-toolchain/toolchain-e2e (manual)
🚧 Files skipped from review as they are similar to previous changes (3)
  • pkg/session/config.go
  • pkg/session/manager_test.go
  • pkg/session/manager.go

Walkthrough

Adds a Kubernetes-backed SessionManager, a TTL-based PodCache, and SandboxConfig defaults, while updating go.mod to Kubernetes v0.33.4 dependencies.

Changes

Session Package: PodCache, SandboxConfig, and SessionManager

Layer / File(s) Summary
Dependencies and config
go.mod, pkg/session/config.go
Adds direct Kubernetes modules at v0.33.4, expands indirect requirements, and defines SandboxConfig plus DefaultConfig().
PodCache implementation
pkg/session/cache.go
Implements PodCache with TTL expiry, invalidation, eviction, and length reporting.
PodCache tests
pkg/session/cache_test.go
Covers cache hits, misses, expiry, overwrite, invalidation, eviction, concurrency, and default TTL behavior.
Manager setup and pod resolution
pkg/session/manager.go
Implements SessionManager setup, session ID validation, pod discovery/creation, pod spec and auth secret construction, token generation, and readiness waiting.
Exec and cleanup
pkg/session/manager.go
Implements command proxying to the sandbox agent, last-activity patching, session cleanup, and stale session cleanup.
Manager tests
pkg/session/manager_test.go
Covers validation, token generation, pod discovery, pod spec and secret shape, readiness detection, cleanup, stale cleanup, and exec proxy behavior.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested labels

feature, test, dependencies

Suggested reviewers

  • rajivnathan
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 17.24% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a pod cache and session manager for sandbox sessions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch SANDBOX-1810-pod-cache-session-manager

Comment @coderabbitai help to get the list of available commands.

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

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/session/config.go`:
- Around line 22-35: The production default in DefaultConfig() uses a mutable
:latest image tag, which can change behavior unexpectedly between deployments.
Update the SandboxConfig.Image value in DefaultConfig() to use a pinned
immutable version or digest instead, and keep the rest of the default config
unchanged.

In `@pkg/session/manager.go`:
- Around line 56-67: NewSessionManager currently allows SandboxConfig values
with an empty HMACKey, which can lead to token generation with an insecure
default. Add a validation check in NewSessionManager (and/or a helper it calls)
to reject empty config.HMACKey at construction time, returning or triggering a
fast failure before the SessionManager is created. Use NewSessionManager,
SandboxConfig, and computeToken as the key symbols to locate the fix.
- Around line 98-103: `ExecuteCommand()` is losing the resolved pod name from
`discoverPod()`, so `updateLastActivity()` later rebuilds
`podNamePrefix+sessionID` and may touch the wrong pod. Update the
`ExecuteCommand` flow to preserve and pass the actual `podName` returned by
`discoverPod()` through to `updateLastActivity()`, and adjust that helper so it
uses the resolved pod name instead of reconstructing one. Make the same change
anywhere else in the 405-430 range where last-activity refresh is triggered, so
labeled pods discovered by `discoverPod` get their annotations updated
correctly.
- Around line 379-380: Build the exec proxy URL using net.JoinHostPort instead
of formatting the host and port manually, so the request works for both IPv4 and
IPv6 pod IPs. Update the URL construction in the code that creates the HTTP
request in the session manager flow (the logic around the exec request in the
relevant manager method) to join podIP and m.config.AgentPort safely before
appending the /exec path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 56dacb08-6c08-43d4-9294-228928eb102e

📥 Commits

Reviewing files that changed from the base of the PR and between f128d90 and 7a90257.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (6)
  • go.mod
  • pkg/session/cache.go
  • pkg/session/cache_test.go
  • pkg/session/config.go
  • pkg/session/manager.go
  • pkg/session/manager_test.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • codeready-toolchain/mcp-common (manual)
  • codeready-toolchain/mcp-server-devsandbox (manual)
  • codeready-toolchain/api (manual)
  • codeready-toolchain/toolchain-common (manual)
  • codeready-toolchain/host-operator (manual)
  • codeready-toolchain/toolchain-e2e (manual)
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**

⚙️ CodeRabbit configuration file

-Focus on major issues impacting performance, readability, maintainability and security. Avoid nitpicks and avoid verbosity.

Files:

  • pkg/session/config.go
  • pkg/session/cache.go
  • go.mod
  • pkg/session/cache_test.go
  • pkg/session/manager_test.go
  • pkg/session/manager.go
🪛 ast-grep (0.44.0)
pkg/session/manager.go

[warning] 37-37: A credential is hard-coded as a string literal. Secrets stored in source code, such as passwords, API keys, and tokens, can be leaked through version control or binaries and used by internal or external malicious actors. Rotate the exposed secret and load it at runtime from a secure secret vault, a Hardware Security Module (HSM), or an environment variable if permitted by your company policy (e.g. password := os.Getenv("APP_PASSWORD")).
Context: secretNamePrefix = "cli-mcp-sandbox-auth-"
Note: [CWE-798] Use of Hard-coded Credentials.

(hardcoded-credentials-string-literal-go)


[warning] 228-228: Narrowing a non-constant integer to a smaller fixed-width type (int8/int16/int32, uint8/uint16/uint32) can silently overflow or wrap, yielding negative or truncated values that are dangerous in size, length, or index logic. Validate the source value is within the target type's range before converting (e.g. bounds-check, or use a checked helper), and avoid narrowing untrusted or len()/parsed values.
Context: int32(m.config.AgentPort)
Note: [CWE-190] Integer Overflow or Wraparound.

(integer-overflow-narrowing-conversion-go)

🪛 golangci-lint (2.12.2)
pkg/session/manager.go

[error] 394-394: Error return value of resp.Body.Close is not checked

(errcheck)

Comment thread pkg/session/config.go
Comment thread pkg/session/manager.go Outdated
Comment thread pkg/session/manager.go
Comment thread pkg/session/manager.go Outdated
- Remove mutable :latest tag from DefaultConfig; require explicit Image
- Validate HMACKey and Image are non-empty in NewSessionManager (fail-fast)
- Use resolved pod name from cache in updateLastActivity instead of reconstructing it
- Use net.JoinHostPort for IPv6-safe agent URL construction
- Fix unchecked resp.Body.Close (errcheck lint)
- Add TestNewSessionManagerValidation covering config rejection

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: Feny Mehta <fbm3307@gmail.com>
@fbm3307
fbm3307 merged commit 4ea0a2f into master Jul 1, 2026
4 of 5 checks passed
@fbm3307
fbm3307 deleted the SANDBOX-1810-pod-cache-session-manager branch July 1, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant