Skip to content

Fix project Relay workspace resolution#1344

Merged
khaliqgant merged 3 commits into
mainfrom
fix/project-workspace-key-resolution
Jul 21, 2026
Merged

Fix project Relay workspace resolution#1344
khaliqgant merged 3 commits into
mainfrom
fix/project-workspace-key-resolution

Conversation

@kjgbot

@kjgbot kjgbot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • expose project-aware workspace-key resolution through the public @agent-relay/cloud/workspace-key SDK entry point
  • make SDK-backed CLI consumers prefer the current checkout broker workspace before the machine-global active workspace
  • retain explicit flag/environment precedence and the existing secure atomic project-key persistence

Why

A Factory process could connect to the broker running in its checkout but publish through the machine-global Relay workspace. Restored babysitters then existed locally while delivery returned agent_not_found.

Verification

  • npm run typecheck
  • npm run format:check
  • npm run build:core
  • npm test (97 files passed, 2 skipped; 1300 tests passed, 20 skipped)
  • focused resolver/CLI/doctor suite (24 passed)
  • packed @agent-relay/cloud tarball install and public subpath import smoke

@kjgbot
kjgbot marked this pull request as ready for review July 20, 2026 19:38
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6f85e94e-fb51-4c8a-aa5c-cc6f6430fa7e

📥 Commits

Reviewing files that changed from the base of the PR and between 0b74a8d and a45df6c.

📒 Files selected for processing (2)
  • CHANGELOG.md
  • packages/cloud/src/project-workspace-key.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • CHANGELOG.md
  • packages/cloud/src/project-workspace-key.ts

📝 Walkthrough

Walkthrough

Changes

Workspace-key resolution

Layer / File(s) Summary
Cloud resolution and persistence
packages/cloud/src/project-workspace-key.ts, packages/cloud/src/project-workspace-key.test.ts
Adds project workspace-key storage, atomic writes, tolerant reads, source precedence, global fallback, and behavioral tests.
Cloud package API exposure
packages/cloud/src/workspace-key.ts, packages/cloud/src/index.ts, packages/cloud/package.json
Exports workspace-key helpers and types through the package root and dedicated subpath.
CLI resolver migration
packages/cli/src/cli/lib/sdk-client.ts, packages/cli/src/cli/lib/project-workspace-key.ts, CHANGELOG.md, .agentworkforce/trajectories/...
Migrates CLI resolution to the cloud resolver, preserves compatibility exports, and records the patch change and trajectory metadata.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI SDK client
  participant Cloud workspace resolver
  participant Project workspace key
  participant Global workspace store
  CLI SDK client->>Cloud workspace resolver: provide explicit key and environment
  Cloud workspace resolver->>Project workspace key: read project-local key when needed
  Project workspace key-->>Cloud workspace resolver: return project key or undefined
  Cloud workspace resolver->>Global workspace store: resolve fallback key
  Global workspace store-->>Cloud workspace resolver: return global key or undefined
  Cloud workspace resolver-->>CLI SDK client: return resolved key and source
Loading

Possibly related PRs

Suggested labels: size:L

Suggested reviewers: willwashburn, khaliqgant

Poem

I’m a rabbit with a key in my paws,
Cloud finds the right burrow without pause.
Flags hop first, env vars follow,
Project keys beat the global hollow.
The CLI now shares the trail—
Tests thump softly: all systems prevail.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.45% 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
Title check ✅ Passed The title clearly matches the main change: fixing project Relay workspace resolution.
Description check ✅ Passed The description covers Summary, Why, and Verification and is sufficiently complete for the template.
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 fix/project-workspace-key-resolution

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request centralizes project-aware workspace key resolution logic into the @agent-relay/cloud package under a new ./workspace-key subpath, migrating the CLI to use this unified implementation. Feedback is provided to append a random suffix to the temporary file path in packages/cloud/src/project-workspace-key.ts to prevent potential race conditions and file collisions in multi-threaded Node.js environments where threads share the same process ID.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread packages/cloud/src/project-workspace-key.ts Outdated

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
CHANGELOG.md (1)

8-12: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider "Unreleased - Minor" instead of "Patch."

This entry documents a new public export (@agent-relay/cloud/workspace-key subpath) alongside a precedence behavior change. Adding new public API surface is typically a Minor-level change under SemVer, not a Patch. Per coding guidelines, the unreleased level should reflect the most significant change bundled into it.

📝 Proposed heading change
-## [Unreleased - Patch]
+## [Unreleased - Minor]
🤖 Prompt for 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.

In `@CHANGELOG.md` around lines 8 - 12, Change the changelog heading from
“Unreleased - Patch” to “Unreleased - Minor” to reflect the new public
workspace-key export documented in the entry, while leaving the release notes
unchanged.

Source: Coding guidelines

🤖 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.

Nitpick comments:
In `@CHANGELOG.md`:
- Around line 8-12: Change the changelog heading from “Unreleased - Patch” to
“Unreleased - Minor” to reflect the new public workspace-key export documented
in the entry, while leaving the release notes unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0caa06c8-0982-4dca-8804-02569d3fead9

📥 Commits

Reviewing files that changed from the base of the PR and between 09d2e35 and 40b137c.

📒 Files selected for processing (10)
  • .agentworkforce/trajectories/factory-recovery/completed/2026-07/traj_t8kdxk32okbu/summary.md
  • .agentworkforce/trajectories/factory-recovery/completed/2026-07/traj_t8kdxk32okbu/trajectory.json
  • CHANGELOG.md
  • packages/cli/src/cli/lib/project-workspace-key.ts
  • packages/cli/src/cli/lib/sdk-client.ts
  • packages/cloud/package.json
  • packages/cloud/src/index.ts
  • packages/cloud/src/project-workspace-key.test.ts
  • packages/cloud/src/project-workspace-key.ts
  • packages/cloud/src/workspace-key.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/cloud/src/project-workspace-key.ts (1)

51-61: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Do not delete a colliding temporary file.

If openSync returns EEXIST, rmSync(tmp) can delete another writer’s temporary file before retrying with the same path. Generate a fresh nonce and retry, or propagate the collision; otherwise the collision-safe atomic-write guarantee is undermined.

Proposed fix
-  const tmp = `${file}.tmp.${process.pid}.${randomUUID()}`;
+  let tmp = `${file}.tmp.${process.pid}.${randomUUID()}`;
   const data = `${JSON.stringify({ workspaceKey: key } satisfies ProjectWorkspaceKeyFile, null, 2)}\n`;

   let fd: number;
   try {
     fd = fs.openSync(tmp, 'wx', 0o600);
   } catch (error) {
     if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error;
-    fs.rmSync(tmp, { force: true });
+    tmp = `${file}.tmp.${process.pid}.${randomUUID()}`;
     fd = fs.openSync(tmp, 'wx', 0o600);
   }
🤖 Prompt for 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.

In `@packages/cloud/src/project-workspace-key.ts` around lines 51 - 61, Update the
temporary-file creation logic around the write function’s fs.openSync call so an
EEXIST collision never removes the existing temporary file. On collision,
generate a fresh randomUUID-based temporary path and retry exclusive creation,
or propagate the error; preserve the atomic-write behavior and avoid retrying
with the same path.
🤖 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.

Outside diff comments:
In `@packages/cloud/src/project-workspace-key.ts`:
- Around line 51-61: Update the temporary-file creation logic around the write
function’s fs.openSync call so an EEXIST collision never removes the existing
temporary file. On collision, generate a fresh randomUUID-based temporary path
and retry exclusive creation, or propagate the error; preserve the atomic-write
behavior and avoid retrying with the same path.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 467737a1-4eee-42d9-ab6c-f551b05f1e37

📥 Commits

Reviewing files that changed from the base of the PR and between 40b137c and 0b74a8d.

📒 Files selected for processing (1)
  • packages/cloud/src/project-workspace-key.ts

@kjgbot

kjgbot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the follow-up review in a45df6c: EEXIST now retries with a fresh nonce and never deletes another writer’s temp file. I also moved the changelog entry to Unreleased - Minor for the new public subpath. Focused resolver/CLI tests (15/15), cloud build, formatting, and diff checks pass.

@khaliqgant
khaliqgant merged commit de13f5e into main Jul 21, 2026
45 checks passed
@khaliqgant
khaliqgant deleted the fix/project-workspace-key-resolution branch July 21, 2026 06:37
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.

2 participants