Skip to content

fix(realtime): hoist cross-tier tenant/membership resolution; dead-code sweep; retire machine vocabulary - #2271

Merged
2witstudios merged 1 commit into
masterfrom
pu/audit-realtime-crosstier
Jul 30, 2026
Merged

2witstudios merged 1 commit into
masterfrom
pu/audit-realtime-crosstier

Conversation

@2witstudios

@2witstudios 2witstudios commented Jul 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

Follow-up from the post-merge audit of PR #2258 (realtime + cross-tier consistency slice). Fixes all 7 findings in #2265:

  • Fail-closed tenant resolution (finding 1): hoisted resolveSessionTenantId into packages/lib/src/services/agent-sessions/agent-session-tenant.ts, imported by both apps/realtime/src/index.ts and apps/web/src/lib/agent-sessions/agent-sessions-runtime.ts. Kills the web tier's fail-open drive?.ownerId ?? session.ownerId fallback — a vanished drive now denies on both tiers via the same function.
  • One membership-gathering implementation (finding 2): resolveDriveMembership + the canRunCode reduction also live in the new module, replacing the copy-pasted DB queries in both tiers.
  • Dead code (finding 3): removed unused checkMachineRuntimeGuardrail/recordMachineActivity imports, the dead conversations schema import, and the unused resolveDriveOwnerContext helper + type from apps/realtime/src/index.ts.
  • Stale doc comments (finding 4): fixed the tenantId docblock in index.ts, the payer prose in shell-access.ts, XtermTerminal.tsx's reference to a deleted method, and useAgentSessionChat.test.ts's header referencing deleted modules.
  • Dead pageId plumbing (finding 5): deleted TerminalSession.pageId... UPDATE: PR fix(billing): unify sandbox payer on the SESSION; retire page-keyed payer helper #2273 (billing payer unification, issue fix(billing): unify sandbox payer on the SESSION; retire page-keyed payer helper; first-class storage attribution [HIGH] #2260) merged to master mid-review and fully addressed this independently — it deleted TerminalSession.pageId, added driveId/agentSessionId fields, and threaded them through all three trackUsage call sites plus the first-class driveId/sessionId columns on AIMonitoring.trackUsage. After rebasing onto master, this branch's own (less complete) version of the same fix was dropped entirely in favor of fix(billing): unify sandbox payer on the SESSION; retire page-keyed payer helper #2273's — confirmed via git diff origin/master...HEAD -- apps/realtime/src/terminal/terminal-session-map.ts packages/lib/src/services/sandbox/sandbox-billing.ts showing zero diff against master. Finding 5 is fully satisfied by master; nothing left for this PR to do there.
  • composeSocketKey export (finding 6): exported from shell-handler.ts; index.ts now calls it instead of re-spelling the socket-key format inline.
  • Machine → session vocabulary (finding 7): SandboxSurface's 'machine' value and every quota.ts guardrail identifier (checkMachineRuntimeGuardrail → checkSessionRuntimeGuardrail, recordMachineActivity → recordSessionActivity, MACHINE_ACTIVITY_GRACE_MS → SESSION_ACTIVITY_GRACE_MS, etc.) renamed, touching both tiers' call sites and tests. machine_sprite_reclaims table name deliberately untouched (migration-documented).

Issue #2260's PR (#2273) merged with a more complete billing-attribution fix than finding 5 anticipated, so this branch defers to it entirely rather than duplicating.

Rebase note: this branch was rebased onto master twice during review to pick up PR #2273 (billing payer unification) and PRs #2269/#2268 (session-tools scoping, pane lifecycle) as they landed. All five overlapping files (shell-handler.ts, terminal-session-map.ts, shell-handler.test.ts, sandbox-tools-runtime.ts, tool-runners.ts) plus agent-sessions-runtime.ts (overlap with #2269's bounded listSessionConversationsBulk) were reconciled by hand — master's versions won wherever there was real overlap; this PR's renames/hoists/dead-code deletions sit cleanly on top.

Test plan

  • New agent-session-tenant.test.ts pins fail-closed-on-vanished-drive for resolveSessionTenantId, plus resolveDriveMembership/canRunCodeForSession
  • apps/realtime full suite passes
  • apps/web sandbox-tools-runtime + agent-sessions route tests pass
  • packages/lib full suite passes
  • bun run typecheck, bun run lint, bun run knip:check all pass (monorepo + per-package for realtime/web/lib)
  • cd packages/lib && bun run typecheck passes
  • bun run test:unit — 15122/15129 pass; the 1 failure (grouping.test.ts midnight-crossing) is a pre-existing TZ-env issue in a file this branch never touches
  • bun run test:security — 44/50 suites pass; the 6 failures are pre-existing stale references in scripts/test-security.sh (test files deleted in fix(security): remove all password-based authentication #861, and vitest-config-excluded files), confirmed via git log --diff-filter=D, unrelated to this branch

Closes #2265

🤖 Generated with Claude Code

https://claude.ai/code/session_018DrdwiwAmX57oXSpiBcmAF

@coderabbitai

coderabbitai Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@2witstudios, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 2 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b647e818-2ec4-4e7b-a765-ddc5a7b68a8e

📥 Commits

Reviewing files that changed from the base of the PR and between e333057 and a93e577.

📒 Files selected for processing (22)
  • apps/realtime/src/__tests__/index.test.ts
  • apps/realtime/src/index.ts
  • apps/realtime/src/terminal/__tests__/shell-handler.test.ts
  • apps/realtime/src/terminal/shell-access.ts
  • apps/realtime/src/terminal/shell-handler.ts
  • apps/web/src/components/agents/chat/__tests__/useAgentSessionChat.test.ts
  • apps/web/src/components/agents/shell/XtermTerminal.tsx
  • apps/web/src/lib/agent-sessions/agent-sessions-runtime.ts
  • apps/web/src/lib/ai/tools/__tests__/sandbox-tools-runtime.test.ts
  • apps/web/src/lib/ai/tools/sandbox-tools-runtime.ts
  • knip.json
  • packages/lib/package.json
  • packages/lib/src/services/agent-sessions/__tests__/agent-session-tenant.test.ts
  • packages/lib/src/services/agent-sessions/agent-session-tenant.ts
  • packages/lib/src/services/sandbox/__tests__/checkpoint-policy.test.ts
  • packages/lib/src/services/sandbox/__tests__/egress-ip.test.ts
  • packages/lib/src/services/sandbox/__tests__/network-options.test.ts
  • packages/lib/src/services/sandbox/__tests__/quota.test.ts
  • packages/lib/src/services/sandbox/checkpoint-policy.ts
  • packages/lib/src/services/sandbox/network-options.ts
  • packages/lib/src/services/sandbox/quota.ts
  • packages/lib/src/services/sandbox/tool-runners.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e8613fb20d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


const MACHINE_MAX_ACTIVE_SECONDS_ENV = 'TERMINAL_MACHINE_MAX_ACTIVE_SECONDS';
const DEFAULT_MACHINE_MAX_ACTIVE_SECONDS = 4 * 60 * 60; // 4 hours
const SESSION_MAX_ACTIVE_SECONDS_ENV = 'TERMINAL_SESSION_MAX_ACTIVE_SECONDS';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the deployed runtime-cap variable

When an existing deployment sets TERMINAL_MACHINE_MAX_ACTIVE_SECONDS, this rename silently stops reading that value and falls back to four hours; for example, a five-minute cost guardrail becomes a four-hour guardrail after deployment. Keep the old name as a compatibility fallback or provide a coordinated configuration migration and release note before removing it.

AGENTS.md reference: AGENTS.md:L223-L228

Useful? React with 👍 / 👎.

…de sweep; retire machine vocabulary

Follow-up from the post-merge audit of PR #2258 (realtime + cross-tier
consistency slice).

- Hoist resolveSessionTenantId (fail-closed on vanished drive) and
  resolveDriveMembership/canRunCode reduction into ONE new lib module
  (packages/lib/src/services/agent-sessions/agent-session-tenant.ts),
  imported by both apps/realtime and apps/web. Kills the web tier's
  fail-open `drive?.ownerId ?? session.ownerId` fallback.
- Delete dead imports (checkMachineRuntimeGuardrail, recordMachineActivity,
  conversations schema) and the unused resolveDriveOwnerContext helper in
  apps/realtime/src/index.ts.
- Fix five stale doc comments left by prior re-keying (index.ts tenantId
  docblock, shell-access.ts payer prose, XtermTerminal.tsx, and the
  useAgentSessionChat test header).
- Delete the permanently-undefined TerminalSession.pageId field; attribute
  driveId on shell trackUsage calls instead (SandboxBillingDeps.trackUsage
  gains an optional driveId alongside the AI-tool-runner path's pageId).
- Export composeSocketKey from shell-handler instead of re-spelling the
  socket-key format inline in index.ts.
- Rename SandboxSurface's 'machine' value and quota.ts's guardrail
  identifiers to session vocabulary (checkSessionRuntimeGuardrail,
  recordSessionActivity, SESSION_ACTIVITY_GRACE_MS, etc.), touching both
  tiers' call sites and tests. Keeps the machine_sprite_reclaims table name.

Closes #2265

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018DrdwiwAmX57oXSpiBcmAF
@2witstudios
2witstudios force-pushed the pu/audit-realtime-crosstier branch from e8613fb to a93e577 Compare July 30, 2026 07:47
@2witstudios
2witstudios merged commit 13a63ec into master Jul 30, 2026
10 checks passed
@2witstudios
2witstudios deleted the pu/audit-realtime-crosstier branch July 30, 2026 08:49
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.

fix(realtime): hoist cross-tier tenant/membership resolution into lib; dead-code sweep; retire machine vocabulary in live identifiers [MED]

1 participant