Skip to content

feat: add outbound stateful code bridge - #66

Open
danny-avila wants to merge 3 commits into
mainfrom
danny-avila/remote-code-bridge
Open

feat: add outbound stateful code bridge#66
danny-avila wants to merge 3 commits into
mainfrom
danny-avila/remote-code-bridge

Conversation

@danny-avila

@danny-avila danny-avila commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • add a fenced Redis-backed remote-bridge Code API sandbox backend
  • add the provider-neutral @librechat/code protocol and outbound worker CLI
  • preserve stateful runtime session IDs while keeping the VM free of public ingress
  • document lifecycle, deployment configuration, and the sandbox/egress threat boundary
  • repair the direct NsJail rootfs handoff used by the macOS no-KVM integration path

Security model

The worker authenticates with a separate operator token and only makes outbound long-poll requests. Code API retains public auth, policy, manifest, deadline, and normalization responsibilities. Assignments are single-active, generation-fenced, deadline-bound, cancellable, and settled with a one-time lease token. The bridge is transport, not a replacement for the hardened microVM/NsJail and egress controls.

Verification

  • npm test in packages/code (2 passed)
  • focused Code API Bun suites (24 passed)
  • Code API Rollup production build
  • npm pack --dry-run for @librechat/code
  • isolated Docker/Playwright integration with the LibreChat companion branch: two conversation turns reused one attached sandbox workspace and read the file written in turn one

Companion LibreChat implementation: danny-avila/LibreChat#15352

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review exact head 7cf93507726119148d21ca6269e5431e05547abd. Please report only actionable findings introduced by this PR.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-08-31T01:14:12.654915Z 9be996f Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 7cf9350772

ℹ️ 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".

Comment thread packages/code/src/worker.ts Outdated
Comment thread packages/code/src/cli.ts Outdated
Comment thread service/src/sandbox-backend/remote-bridge.ts
Comment thread packages/code/src/worker.ts
Comment thread service/src/sandbox-backend/remote-bridge.ts
Comment thread docker/start-direct-sandbox.sh
Comment thread packages/code/src/worker.ts
Comment thread service/src/bridge/store.ts Outdated
Comment thread service/src/bridge/router.ts
Comment thread service/src/bridge/store.ts
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 9be996f4ae

ℹ️ 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".

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

Please review the current PR head 9be996f. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

@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: 9be996f4ae

ℹ️ 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 assignment = await this.readAssignment(assignmentId);
if (assignment == null || assignment.workerId !== workerId) continue;
if (assignment.incarnationId !== incarnationId) continue;

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 Avoid consuming assignments for another incarnation

During a rolling restart, a superseded worker can still have an outstanding /lease request when the replacement registers. Because every incarnation consumes the same queue and LPOP is destructive, the old request can remove an assignment intended for the replacement and this continue permanently discards it, leaving dispatch blocked until its deadline despite a live worker. Fresh evidence beyond the earlier fencing comment is that the current head performs the incarnation mismatch check only after removing the item; use per-incarnation queues or atomically claim only matching assignments.

Useful? React with 👍 / 👎.

Comment on lines +212 to +216
await this.request<BridgeSettlementResponse>(
this.assignmentUrl(assignment, 'settle'),
settlement,
signal,
);

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 Quarantine state after ambiguous settlement failures

If a stateful sandbox execution succeeds but this settlement request fails before reaching Code API, executeAndSettle throws and the worker proceeds to reconnect without retrying the settlement. The server eventually times out and releases the lock, yet the session-routed sandbox retains the execution's mutations, so the next request observes state from an execution reported as failed. Fresh evidence beyond the finalization fix is that the current head quarantines only finalizer failures; retry settlement through the assignment deadline or quarantine/reset the workspace when delivery remains ambiguous.

Useful? React with 👍 / 👎.

return Math.max(
1,
Math.min(
MAX_ASSIGNMENT_TTL_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.

P2 Badge Keep assignment TTL aligned with the configured timeout

When JOB_TIMEOUT is configured above ten minutes, this cap expires the assignment record and worker lock before deadlineAtMs. Cancellation polling then sees the missing assignment and aborts the sandbox, while expiration of the lock can also admit another dispatch before the original deadline; consequently supported long-running jobs cannot complete safely. Size the Redis TTL from the full configured deadline plus grace, or reject timeout values exceeding this cap.

Useful? React with 👍 / 👎.

}

private sandboxEndpointFor(assignment: BridgeAssignment): string {
if (assignment.runtimeSessionId == null) return this.sandboxEndpoint;

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 Route stateless affinity assignments away from the template

When the documented stateful worker configuration uses a {runtimeSessionId} endpoint template with CODEAPI_RUNTIME_SESSION_MODE=affinity, any hintless request is intentionally dispatched without a runtime session. This branch then returns the unresolved template, so the worker posts to a literal /sessions/{runtimeSessionId}/... route and the affinity mode's stateless fallback fails. Provide a separate stateless endpoint, substitute an ephemeral assignment-specific route, or require strict mode for templated workers.

Useful? React with 👍 / 👎.

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