Skip to content

feat: inject RECOUP_ORG_ID into sandbox + auto-install artist-workspace skill#16

Merged
sweetmantech merged 1 commit into
mainfrom
feat/recoup-org-id-injection
Apr 24, 2026
Merged

feat: inject RECOUP_ORG_ID into sandbox + auto-install artist-workspace skill#16
sweetmantech merged 1 commit into
mainfrom
feat/recoup-org-id-injection

Conversation

@sweetmantech
Copy link
Copy Markdown
Contributor

@sweetmantech sweetmantech commented Apr 24, 2026

Summary

Fixes the "artists" half of the sandbox-context work so a user opening a sandbox for a specific org gets the sandbox's artists back — not their personal cross-org list — when they ask "what artists do I have".

Companion to recoupable/skills#15, which expanded `artist-workspace` to cover inventory phrasings and documented `RECOUP_ORG_ID` in `recoup-api`.

Background — what the user hit

In an open-agents sandbox opened for a specific org, the user asked "what artists do I have?". The agent did `GET /api/artists` on the user-scoped Privy token and returned the user's artists across every org they belong to — a completely different set than what the sandbox tree (`orgs/{org-slug}/artists/{artist-slug}/RECOUP.md`, seeded by `setup-sandbox` during provisioning) actually contains.

Two root causes in open-agents:

  1. `artist-workspace` wasn't auto-installed. It already describes the filesystem layout authoritatively, but `DEFAULT_GLOBAL_SKILL_REFS` only listed `recoup-api`, so the agent never loaded `artist-workspace` and defaulted to API calls for inventory questions.
  2. No `RECOUP_ORG_ID` in the sandbox env. Even when the agent chose the API, it had no way to scope to this sandbox's org — the user-scoped token spans every org the user belongs to, so unscoped list endpoints mislead.

Changes

New extractor (deriving org ID from the clone URL)

  • `apps/web/lib/recoupable/extract-org-id.ts` — tail-match the UUID-v4 pattern from a repo name or clone URL. Recoupable org repos follow `org--`, so the UUID is always the trailing 36 chars. No schema column or migration needed — we derive on the fly from `sessionRecord.cloneUrl` that's already persisted.
  • `apps/web/lib/recoupable/extract-org-id.test.ts` — 8 cases (full URL, `.git` suffix, trailing slash, bare repo name, uppercase UUID, wrong owner, missing UUID, empty/malformed).

Plumbing (mirrors the `recoupAccessToken` path added in #13)

  • `apps/web/app/api/chat/route.ts` — derive `recoupOrgId` from `sessionRecord.cloneUrl` (or `repoName` as fallback) and forward into `agentOptions`.
  • `packages/agent/open-harness-agent.ts` — `recoupOrgId` added to the call-options schema and forwarded into `experimental_context`.
  • `packages/agent/types.ts` — `recoupOrgId?: string` on `AgentContext`.
  • `packages/agent/tools/get-recoup-org-id.ts` — mirror of `get-recoup-access-token.ts`.
  • `packages/agent/tools/build-recoup-exec-env.ts` — now returns `Record<string, string> | undefined`, merging `RECOUP_ACCESS_TOKEN` and `RECOUP_ORG_ID` when available. No change to bash/fetch consumers — they spread the result into `env` as before.

Skills

  • `apps/web/lib/skills/default-global-skills.ts` — add `artist-workspace` alongside `recoup-api` so inventory guidance is always reachable. Both skills are descriptor-only until `skillTool` fires.
  • `apps/web/lib/recoup-api-skill-prompt.ts` — rewrite the always-on nudge to route by intent: `artist-workspace` for sandbox inventory ("what artists / orgs do I have"), `recoup-api` for live data, and explicitly tell the agent to scope list endpoints to `$RECOUP_ORG_ID` when it's set.

Tests

  • `packages/agent/tools/tools.test.ts` — two new cases: `bashTool` forwards `RECOUP_ORG_ID` alongside the access token; and `RECOUP_ORG_ID` alone injects correctly when no token is present.
  • `apps/web/lib/sandbox/install-session-global-skills.test.ts` — updated all three existing cases to expect `artist-workspace` in the installed set.

Why this shape

  • No DB work. The org UUID is already embedded in the clone URL via the `org--` naming convention used by Recoupable org repos. Deriving server-side avoids a migration, a new session column, and client tampering.
  • No new skill. `artist-workspace` already owned the filesystem contract — the fix was to (a) auto-install it and (b) broaden its trigger-phrase description (done in feat: artist-workspace covers sandbox inventory + recoup-api documents RECOUP_ORG_ID skills#15).
  • `recoup-api` stays portable. The `RECOUP_ORG_ID` guidance goes in its Environment section as an API-scoping concern; non-sandbox consumers just have the env unset and behave identically to today.

Test plan

  • `bun run check` clean
  • `bun run typecheck` clean (cache-miss run completed)
  • `extract-org-id.test.ts` — 8/8 isolated
  • `tools.test.ts` — 21/21 isolated (19 prior + 2 new)
  • `install-session-global-skills.test.ts` — 3/3 isolated
  • `chat/route.test.ts` — 19/19 isolated
  • Integration verify: once feat: artist-workspace covers sandbox inventory + recoup-api documents RECOUP_ORG_ID skills#15 is merged, open a fresh sandbox for an org and ask "what artists do I have?" on turn 1 — the agent should load `artist-workspace`, run `ls -d orgs//artists//`, and return the sandbox-accurate list. Then ask for socials/metrics on one of those artists and verify the call goes to `/api/organizations/$RECOUP_ORG_ID/...` with the token.

Known unrelated failure

`apps/web/app/api/pr/route.test.ts` fails with `server-only` import + 403 on unmodified `main` — pre-existing (reproduced on the prior PR #15 too), not caused by this change.

🤖 Generated with Claude Code


Summary by cubic

Injects the sandbox’s Recoup org ID and auto-installs artist-workspace so inventory questions return the sandbox’s artists, not the user’s cross-org list. We derive the org UUID from the sandbox repo and expose it as RECOUP_ORG_ID for scoped API calls.

  • New Features
    • Derive org UUID from clone URL/repo name and pass as recoupOrgId; expose to tools as RECOUP_ORG_ID for scoping list endpoints.
    • Auto-install artist-workspace alongside recoup-api so inventory uses the sandbox filesystem; keep API for live data.
    • Update the always-on prompt to route inventory to artist-workspace and scope recoup-api calls to /api/organizations/$RECOUP_ORG_ID/... when set.
    • Add extractOrgId helper and tests; extend tool and skill-install tests to cover RECOUP_ORG_ID and the new default skill.

Written for commit 77f17a0. Summary will update on new commits.

Two related changes fix "what artists do I have" returning a
cross-org list instead of the sandbox's artists:

1. Make the sandbox's Recoupable org available to the agent as
   RECOUP_ORG_ID. Org UUIDs are the trailing 36 chars of the repo
   name (org-<slug>-<uuid-v4>), so derive server-side from
   sessionRecord.cloneUrl — no schema column or migration needed.
   Thread recoupOrgId through experimental_context alongside the
   per-prompt recoupAccessToken and inject it into exec env via
   buildRecoupExecEnv so curl/CLI calls can scope to
   /api/organizations/$RECOUP_ORG_ID/... instead of hitting the
   user-scoped unscoped list endpoints.

2. Auto-install artist-workspace in every sandbox. It already
   describes the orgs/{org-slug}/artists/{artist-slug}/RECOUP.md
   tree seeded by setup-sandbox, but it wasn't in
   DEFAULT_GLOBAL_SKILL_REFS so the agent never loaded it and fell
   through to API calls for inventory questions.

The always-on prompt nudge is rewritten to pick the right tool by
intent — artist-workspace for sandbox inventory, recoup-api (with
RECOUP_ORG_ID scoping) for live data not already in the tree.

Depends on recoupable/skills#15, which expands the artist-workspace
description to cover inventory phrasings and documents RECOUP_ORG_ID
in recoup-api.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 24, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
open-agents Ready Ready Preview Apr 24, 2026 3:01pm

Request Review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 24, 2026

Warning

Rate limit exceeded

@sweetmantech has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 6 minutes and 50 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 6 minutes and 50 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4526e8aa-6737-4748-85cf-1f9cd29910f5

📥 Commits

Reviewing files that changed from the base of the PR and between 9620cd8 and 77f17a0.

📒 Files selected for processing (11)
  • apps/web/app/api/chat/route.ts
  • apps/web/lib/recoup-api-skill-prompt.ts
  • apps/web/lib/recoupable/extract-org-id.test.ts
  • apps/web/lib/recoupable/extract-org-id.ts
  • apps/web/lib/sandbox/install-session-global-skills.test.ts
  • apps/web/lib/skills/default-global-skills.ts
  • packages/agent/open-harness-agent.ts
  • packages/agent/tools/build-recoup-exec-env.ts
  • packages/agent/tools/get-recoup-org-id.ts
  • packages/agent/tools/tools.test.ts
  • packages/agent/types.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/recoup-org-id-injection

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 and usage tips.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 11 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/web/app/api/chat/route.ts">

<violation number="1" location="apps/web/app/api/chat/route.ts:214">
P2: The `repoName` fallback can mis-detect recoupOrgId for non-recoupable repositories when a bare repo name ends with a UUID; repoName should be validated (owner/prefix) before calling extractOrgId.</violation>
</file>

You're on the cubic free plan with 13 free PR reviews remaining this month. Upgrade for unlimited reviews.

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

// tampering and needs no schema column.
const recoupOrgId =
(sessionRecord.cloneUrl && extractOrgId(sessionRecord.cloneUrl)) ||
(sessionRecord.repoName && extractOrgId(sessionRecord.repoName)) ||
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot Apr 24, 2026

Choose a reason for hiding this comment

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

P2: The repoName fallback can mis-detect recoupOrgId for non-recoupable repositories when a bare repo name ends with a UUID; repoName should be validated (owner/prefix) before calling extractOrgId.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/web/app/api/chat/route.ts, line 214:

<comment>The `repoName` fallback can mis-detect recoupOrgId for non-recoupable repositories when a bare repo name ends with a UUID; repoName should be validated (owner/prefix) before calling extractOrgId.</comment>

<file context>
@@ -204,6 +205,15 @@ export async function POST(req: Request) {
+  // tampering and needs no schema column.
+  const recoupOrgId =
+    (sessionRecord.cloneUrl && extractOrgId(sessionRecord.cloneUrl)) ||
+    (sessionRecord.repoName && extractOrgId(sessionRecord.repoName)) ||
+    undefined;
+
</file context>
Fix with Cubic

@sweetmantech sweetmantech merged commit 448eb0a into main Apr 24, 2026
3 checks passed
@sweetmantech sweetmantech deleted the feat/recoup-org-id-injection branch April 24, 2026 15:19
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