mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
Merge remote-tracking branch 'private/cloud/integration' into cloud/w2-ttfk
# Conflicts: # src/renderer/src/i18n/locales/en.json
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
// included hour is one hour on the Standard 4 vCPU / 8 GB box and a Large box burns 2x.
|
||||
// Included hours are a MONTHLY per-seat allowance pooled across the org, for annual plans too.
|
||||
import { z } from 'zod'
|
||||
import { ORCA_CLOUD_IDLE_SLEEP_MINUTES } from '../../../../src/shared/hosted-sandbox/hosted-sandbox-wire'
|
||||
import type { Env } from './env'
|
||||
|
||||
export const PLAN_IDS = ['team'] as const
|
||||
@@ -65,7 +66,8 @@ export const DEFAULT_PLANS: PlanCatalog = {
|
||||
includedHoursPerSeat: 60,
|
||||
concurrentSandboxesPerSeat: 3,
|
||||
machineSizes: ['standard', 'large'],
|
||||
idleSleepMinutes: 15,
|
||||
// Same constant the control plane enforces and serves (GET /v1/config); never a literal here.
|
||||
idleSleepMinutes: ORCA_CLOUD_IDLE_SLEEP_MINUTES,
|
||||
overageCentsPerHour: 75,
|
||||
defaultSpendCapCents: 2500,
|
||||
maxSpendCapCents: 500_000,
|
||||
|
||||
@@ -18,7 +18,9 @@ export const VM_SCRIPTS = [
|
||||
'orca-sandbox-grant',
|
||||
'orca-sandbox-revoke',
|
||||
'orca-sandbox-members',
|
||||
'orca-git-as'
|
||||
'orca-git-as',
|
||||
'orca-git-identity',
|
||||
'git'
|
||||
] as const
|
||||
|
||||
const Sha256 = z.string().regex(/^[a-f0-9]{64}$/)
|
||||
|
||||
@@ -128,11 +128,14 @@ install_desktop() {
|
||||
rm -rf "$dir" && mkdir -p "$dir/novnc" "$dir/websockify" || return 1
|
||||
tar -xzf "$tmp/novnc.tgz" -C "$dir/novnc" --strip-components 1 || return 1
|
||||
tar -xzf "$tmp/websockify.tgz" -C "$dir/websockify" --strip-components 1 || return 1
|
||||
# Orca tab fit: title "Remote Desktop" (Xvnc's -desktop name), no noVNC favicon, control bar on
|
||||
# the right (away from Orca's sidebar edge), and no reconnect loop on a stale per-boot password.
|
||||
# Orca tab fit: title "Remote desktop" (Xvnc's -desktop name), no noVNC favicon, control bar on
|
||||
# the right (away from Orca's sidebar edge); a stale per-boot password ends the session with one
|
||||
# plain message instead of a reconnect loop. "Unencrypted" is noVNC's view of plain ws:// to
|
||||
# localhost; the hop to the sandbox rides the end-to-end encrypted runtime connection.
|
||||
sed -i -e 's/document.title = e.detail.name + " - " + PAGE_TITLE;/document.title = e.detail.name;/' \
|
||||
-e "s/WebUtil.readSetting('controlbar_pos') === 'right'/WebUtil.readSetting('controlbar_pos', 'right') === 'right'/" \
|
||||
-e 's/securityFailed(e) {/securityFailed(e) { UI.inhibitReconnect = true;/' \
|
||||
-e 's/securityFailed(e) {/securityFailed(e) { UI.inhibitReconnect = true; UI.showStatus("This remote desktop session ended. Reopen it from the workspace menu.", "error"); return;/' \
|
||||
-e 's/_("Connected (unencrypted) to ")/_("Connected to ")/' \
|
||||
"$dir/novnc/app/ui.js" || return 1
|
||||
sed -i '/rel="icon"/d; /rel="apple-touch-icon"/d' "$dir/novnc/vnc.html" || return 1
|
||||
rm -rf "$tmp"
|
||||
@@ -152,7 +155,7 @@ if [ -n "${NOVNC_VERSION:-}" ]; then
|
||||
fi
|
||||
|
||||
log 'Orca entry points'
|
||||
for name in orca-sandbox-start orca-sandbox-drain orca-sandbox-env orca-session-env.sh orca-egress-probe orca-image-wipe orca-share-proxy orca-sshd orca-ssh-keys orca-desktop-start orca-sandbox-grant orca-sandbox-revoke orca-sandbox-members orca-git-as; do
|
||||
for name in orca-sandbox-start orca-sandbox-drain orca-sandbox-env orca-session-env.sh orca-egress-probe orca-image-wipe orca-share-proxy orca-sshd orca-ssh-keys orca-desktop-start orca-sandbox-grant orca-sandbox-revoke orca-sandbox-members orca-git-as orca-git-identity git; do
|
||||
var="ORCA_VM_SCRIPT_${name//[-.]/_}"
|
||||
printf '%s' "${!var}" | base64 -d >"$ORCA_HOME/bin/$name"
|
||||
chmod 0755 "$ORCA_HOME/bin/$name"
|
||||
|
||||
File diff suppressed because one or more lines are too long
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
# git for agents in Orca Cloud workspaces (first on orcad's PATH). Commands that author or publish
|
||||
# work run as the teammate whose message started this agent's turn (orca-git-as: orcad attests the
|
||||
# sender's paired device; root-only member identities). Everything else is plain git, untouched.
|
||||
real=/usr/bin/git
|
||||
sub=''
|
||||
skip=0
|
||||
for arg in "$@"; do
|
||||
if [ "$skip" = 1 ]; then skip=0; continue; fi
|
||||
case "$arg" in
|
||||
-C | -c | --git-dir | --work-tree | --namespace | --exec-path | --config-env) skip=1 ;;
|
||||
-*) ;;
|
||||
*) sub="$arg"; break ;;
|
||||
esac
|
||||
done
|
||||
case "$sub" in
|
||||
commit | push | merge | rebase | cherry-pick | revert | am | pull | tag)
|
||||
# Outside an agent turn (no attested sender) orca-git-as falls back to the workspace identity.
|
||||
if [ -n "${ORCA_TERMINAL_HANDLE:-}${ORCA_AGENT_SESSION_ID:-}" ] && [ -x /opt/orca/bin/orca-git-as ]; then
|
||||
PATH="/usr/bin:/bin:$PATH" exec /opt/orca/bin/orca-git-as "$@" 2> >(grep -v '^orca-git-as: no teammate identity' >&2)
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
exec "$real" "$@"
|
||||
@@ -76,7 +76,7 @@ if ! alive xvnc || [ ! -s "$STATE/password" ]; then
|
||||
start xvnc Xvnc ":$DISPLAY_NUM" -geometry "$GEOMETRY" -depth 24 -nolisten tcp \
|
||||
-rfbport -1 -rfbunixpath "$SOCKET" -rfbunixmode 0600 \
|
||||
-SecurityTypes VncAuth -PasswordFile "$STATE/passwd" \
|
||||
-AlwaysShared -AcceptSetDesktopSize -desktop 'Remote Desktop'
|
||||
-AlwaysShared -AcceptSetDesktopSize -desktop 'Remote desktop'
|
||||
for _ in $(seq 1 100); do
|
||||
[ -S "$SOCKET" ] && break
|
||||
alive xvnc || { tail -n 20 "$STATE/xvnc.log" >&2; fail 'Xvnc exited'; }
|
||||
|
||||
@@ -1,14 +1,31 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs git as a workspace member: their GitHub identity for pushes/PRs and their commit author.
|
||||
# orca-git-as <member user id> <git args...> e.g. orca-git-as "$ORCA_SENDER_ID" push origin HEAD
|
||||
# Falls back to plain git (the workspace's app identity) when the member never connected GitHub.
|
||||
# Runs git as the teammate whose message started this agent's turn: their GitHub identity for
|
||||
# pushes/PRs (this repository only) and their commit author.
|
||||
# orca-git-as <git args...> e.g. orca-git-as push origin HEAD
|
||||
# orca-git-as --as <member id> <git args> refuses unless that member really sent the message
|
||||
# Orca attests the sender from the paired device that sent the message, never from arguments or
|
||||
# message text. Without an attested sender (not run from an agent, or the owner's local session) it
|
||||
# runs plain git with the workspace identity.
|
||||
set -euo pipefail
|
||||
user="${1:-}"
|
||||
shift || true
|
||||
case "$user" in '' | *[!A-Za-z0-9_.:-]*) echo 'usage: orca-git-as <member id> <git args...>' >&2; exit 2 ;; esac
|
||||
file="$HOME/.orca-sandbox/members/$(printf '%s' "$user" | tr ':' '_').gitconfig"
|
||||
if [ ! -f "$file" ]; then
|
||||
echo "orca-git-as: $user has not connected GitHub; using the workspace identity" >&2
|
||||
exec git "$@"
|
||||
expect=''
|
||||
if [ "${1:-}" = '--as' ]; then
|
||||
expect="${2:-}"
|
||||
shift 2 || { echo 'usage: orca-git-as [--as <member id>] <git args...>' >&2; exit 2; }
|
||||
case "$expect" in '' | *[!A-Za-z0-9_.:-]*) echo 'orca-git-as: bad member id' >&2; exit 2 ;; esac
|
||||
fi
|
||||
exec git -c "include.path=$file" "$@"
|
||||
set +e
|
||||
file="$(ORCA_GIT_AS="$expect" sudo -n --preserve-env=ORCA_GIT_AS,ORCA_TERMINAL_HANDLE,ORCA_PANE_KEY,ORCA_AGENT_LAUNCH_TOKEN,ORCA_AGENT_SESSION_ID \
|
||||
/opt/orca/bin/orca-git-identity)"
|
||||
status=$?
|
||||
set -e
|
||||
case "$status" in
|
||||
0)
|
||||
trap 'rm -f "$file"' EXIT
|
||||
git -c "include.path=$file" "$@"
|
||||
;;
|
||||
3)
|
||||
echo 'orca-git-as: no teammate identity for this turn; using the workspace identity' >&2
|
||||
exec git "$@"
|
||||
;;
|
||||
*) exit 4 ;;
|
||||
esac
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env bash
|
||||
# Root helper behind `orca-git-as` (sudoers: the VM user may run only this). Asks orcad, as the
|
||||
# calling agent, which paired device sent the message that started the agent's latest turn
|
||||
# (`orca sender`: orcad identifies the agent from its launch evidence, never from arguments), maps
|
||||
# that device to a workspace member, and prints that member's git config file for the caller to
|
||||
# include. Optional ORCA_GIT_AS: the member the agent expects; any other answer is refused.
|
||||
# Exit codes: 0 printed a config path; 3 no member identity (use the workspace identity); 4 refused.
|
||||
set -euo pipefail
|
||||
DIR=/etc/orca/members
|
||||
caller="${SUDO_USER:-}"
|
||||
case "$caller" in '' | root | *[!a-z0-9_-]*) echo 'orca-git-identity: run through orca-git-as' >&2; exit 4 ;; esac
|
||||
# orcad's own data dir and HOME (the agent's user may not use its passwd home, e.g. HOME=/vercel).
|
||||
orcad_pid="$(pgrep -u "$caller" -f orcad-server.js | head -n 1 || true)"
|
||||
[ -n "$orcad_pid" ] || exit 3
|
||||
orcad_env() { tr '\0' '\n' <"/proc/$orcad_pid/environ" | sed -n "s/^$1=//p" | head -n 1; }
|
||||
home="$(orcad_env HOME)"
|
||||
data="$(orcad_env ORCA_USER_DATA)"
|
||||
data="${data:-$home/.orcad-data}"
|
||||
node="$(find /opt/orca/runtime/runtimes -type f -name node | head -n 1)"
|
||||
cli=/opt/orca/runtime/orcad/out/cli/index.js
|
||||
# Only the evidence variables orcad needs reach the query, which runs as the caller, not root.
|
||||
answer="$(sudo -u "$caller" env -i HOME="$home" PATH=/usr/bin:/bin ORCA_USER_DATA_PATH="$data" \
|
||||
${ORCA_TERMINAL_HANDLE:+ORCA_TERMINAL_HANDLE="$ORCA_TERMINAL_HANDLE"} \
|
||||
${ORCA_PANE_KEY:+ORCA_PANE_KEY="$ORCA_PANE_KEY"} \
|
||||
${ORCA_AGENT_LAUNCH_TOKEN:+ORCA_AGENT_LAUNCH_TOKEN="$ORCA_AGENT_LAUNCH_TOKEN"} \
|
||||
${ORCA_AGENT_SESSION_ID:+ORCA_AGENT_SESSION_ID="$ORCA_AGENT_SESSION_ID"} \
|
||||
timeout 15 "$node" "$cli" sender --json 2>/dev/null || true)"
|
||||
device="$(jq -r '.result.sender.deviceId // empty' <<<"$answer" 2>/dev/null || true)"
|
||||
if [ -z "$device" ] || [ ! -s "$DIR/devices.json" ]; then
|
||||
if [ -n "${ORCA_GIT_AS:-}" ]; then
|
||||
echo "orca-git-as: Orca cannot confirm that $ORCA_GIT_AS sent this agent's latest message; refusing to push as them." >&2
|
||||
exit 4
|
||||
fi
|
||||
exit 3
|
||||
fi
|
||||
member="$(jq -r --arg d "$device" '.[$d] // empty' "$DIR/devices.json")"
|
||||
if [ -z "$member" ]; then
|
||||
[ -n "${ORCA_GIT_AS:-}" ] && { echo "orca-git-as: the sender's device is not linked to a workspace member; refusing." >&2; exit 4; }
|
||||
exit 3
|
||||
fi
|
||||
if [ -n "${ORCA_GIT_AS:-}" ] && [ "$ORCA_GIT_AS" != "$member" ]; then
|
||||
echo "orca-git-as: this agent's latest message came from $member, not $ORCA_GIT_AS; refusing to push as someone else." >&2
|
||||
exit 4
|
||||
fi
|
||||
file="$DIR/$(printf '%s' "$member" | tr ':' '_').gitconfig"
|
||||
[ -s "$file" ] || exit 3
|
||||
# The caller reads the identity through this one-shot copy (owned by them, removed by orca-git-as).
|
||||
out="$(sudo -u "$caller" mktemp)"
|
||||
cat "$file" >"$out"
|
||||
echo "$out"
|
||||
@@ -1,14 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# Writes one git config per workspace member for `orca-git-as`. No token ever lands here: each file
|
||||
# carries the member's egress placeholder (swapped for their GitHub token by the control plane's
|
||||
# egress proxy, for this workspace's repository only) and their GitHub author identity.
|
||||
# Env: ORCA_MEMBERS_B64 = base64 JSON [{userId, name, email, authorization}]. Prints {"members":<n>}.
|
||||
# Runs as root (control plane, sudo). Installs who-may-push-as-whom for `orca-git-as`:
|
||||
# /etc/orca/members/<member>.gitconfig egress placeholder header + GitHub author identity
|
||||
# /etc/orca/members/devices.json {"<orcad device id>": "<member id>"} (each member's paired devices)
|
||||
# Both root-only: agents never read another member's placeholder. `orca-git-identity` (root, via a
|
||||
# sudoers rule) hands an agent only the identity of the member whose device drove its turn, as orcad
|
||||
# attests it. No token lives here: the placeholder is swapped at egress, for this repository only.
|
||||
# Env: ORCA_MEMBERS_B64 = base64 JSON [{userId, name, email, authorization, deviceIds:[...]}]
|
||||
# The VM user agents run as is orcad's own user (detected). Prints {"members":<n>,"devices":<n>}.
|
||||
set -euo pipefail
|
||||
umask 077
|
||||
DIR="$HOME/.orca-sandbox/members"
|
||||
mkdir -p "$DIR"
|
||||
DIR=/etc/orca/members
|
||||
orcad_pid="$(pgrep -f orcad-server.js | head -n 1 || true)"
|
||||
USER_NAME="$( [ -n "$orcad_pid" ] && ps -o user= -p "$orcad_pid" | tr -d ' ' || true)"
|
||||
USER_NAME="${USER_NAME:-${ORCA_SANDBOX_USER:-${SUDO_USER:-}}}"
|
||||
case "$USER_NAME" in '' | *[!a-z0-9_-]*) echo 'bad user' >&2; exit 2 ;; esac
|
||||
mkdir -p /etc/orca
|
||||
json="$(printf '%s' "${ORCA_MEMBERS_B64:-W10=}" | base64 -d)"
|
||||
tmp="$(mktemp -d "$DIR.XXXXXX")"
|
||||
tmp="$(mktemp -d /etc/orca/members.XXXXXX)"
|
||||
count=0
|
||||
while IFS= read -r row; do
|
||||
user="$(jq -r '.userId' <<<"$row")"
|
||||
@@ -21,8 +29,33 @@ while IFS= read -r row; do
|
||||
git config -f "$file" user.email "$(jq -r '.email' <<<"$row")"
|
||||
count=$((count + 1))
|
||||
done < <(jq -c '.[]' <<<"$json")
|
||||
jq -c '[.[] | select(.userId | test("^[A-Za-z0-9_.:-]+$")) | .userId as $u | (.deviceIds // [])[] | select(test("^[A-Za-z0-9_-]{1,128}$")) | {key: ., value: $u}] | from_entries' <<<"$json" >"$tmp/devices.json"
|
||||
devices="$(jq 'length' "$tmp/devices.json")"
|
||||
chmod 0700 "$tmp" && chmod 0600 "$tmp"/*
|
||||
rm -rf "$DIR.old"
|
||||
[ -d "$DIR" ] && mv "$DIR" "$DIR.old"
|
||||
mv "$tmp" "$DIR"
|
||||
rm -rf "$DIR.old"
|
||||
echo "{\"members\":$count}"
|
||||
# Agents may ask for their own sender's identity, and only that (the helper checks with orcad).
|
||||
rule="$USER_NAME ALL=(root) NOPASSWD:SETENV: /opt/orca/bin/orca-git-identity"
|
||||
printf '%s\n' "$rule" >/etc/sudoers.d/orca-git-identity.new
|
||||
chmod 0440 /etc/sudoers.d/orca-git-identity.new
|
||||
if visudo -cf /etc/sudoers.d/orca-git-identity.new >/dev/null; then
|
||||
mv -f /etc/sudoers.d/orca-git-identity.new /etc/sudoers.d/orca-git-identity
|
||||
else
|
||||
rm -f /etc/sudoers.d/orca-git-identity.new
|
||||
echo 'sudoers rule rejected' >&2
|
||||
exit 1
|
||||
fi
|
||||
# Pre-attestation layout (agent-readable member files) must not linger.
|
||||
user_home="$(getent passwd "$USER_NAME" | cut -d: -f6)"
|
||||
[ -n "$orcad_pid" ] && user_home="$(tr '\0' '\n' <"/proc/$orcad_pid/environ" | sed -n 's/^HOME=//p' | head -n 1)"
|
||||
[ -n "$user_home" ] && rm -rf "$user_home/.orca-sandbox/members"
|
||||
# Commits outside a teammate's turn still need an author (images older than orca-sandbox-start's default).
|
||||
if [ -n "$user_home" ]; then
|
||||
sudo -u "$USER_NAME" env HOME="$user_home" git config --global user.email >/dev/null 2>&1 \
|
||||
|| sudo -u "$USER_NAME" env HOME="$user_home" git config --global user.email 'cloud@users.noreply.onorca.dev'
|
||||
sudo -u "$USER_NAME" env HOME="$user_home" git config --global user.name >/dev/null 2>&1 \
|
||||
|| sudo -u "$USER_NAME" env HOME="$user_home" git config --global user.name 'Orca Cloud'
|
||||
fi
|
||||
echo "{\"members\":$count,\"devices\":$devices}"
|
||||
|
||||
@@ -78,6 +78,9 @@ if [ -n "${ORCA_GIT_EXTRAHEADER:-}" ]; then
|
||||
# Every github.com request carries the placeholder; the egress policy swaps in a 1h repo token.
|
||||
git config --global http.https://github.com/.extraheader "$ORCA_GIT_EXTRAHEADER"
|
||||
fi
|
||||
# Commits outside a teammate's turn (or before any member connected GitHub) need an author too.
|
||||
git config --global user.email >/dev/null 2>&1 || git config --global user.email 'cloud@users.noreply.onorca.dev'
|
||||
git config --global user.name >/dev/null 2>&1 || git config --global user.name 'Orca Cloud'
|
||||
# First boot clones while orcad starts (orcad only needs the directory to exist); ready waits for both.
|
||||
clone_pid=''
|
||||
if [ ! -d "$PROJECT" ]; then
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
-- When each member last sent a message to a shared workspace, reported by the member's own
|
||||
-- signed-in client (their Orca token, not the VM). The egress proxy honors a member's GitHub
|
||||
-- identity only within a short window after this, so nothing inside the VM can push as a member
|
||||
-- who is not actively driving the workspace.
|
||||
CREATE TABLE workspace_member_sends (
|
||||
sandbox_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
sent_at INTEGER NOT NULL,
|
||||
PRIMARY KEY (sandbox_id, user_id)
|
||||
);
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Hosts the workspace's own agent tools need (remote MCP servers synced from the desktop), added to
|
||||
-- the egress allowlist on every boot (comma list). Set by PUT /v1/sandboxes/:id/egress/hosts; unlike
|
||||
-- egress_allow_hosts it is not recomputed from secrets.
|
||||
ALTER TABLE sandboxes ADD COLUMN egress_user_hosts TEXT;
|
||||
@@ -0,0 +1,5 @@
|
||||
-- Build VMs are metered like workspaces: one usage segment per finished build (createdAt..finishedAt,
|
||||
-- the build's sandbox size), attributed to the member who started it. Null until billing accepted it;
|
||||
-- the cron retries unreported builds.
|
||||
ALTER TABLE environment_builds ADD COLUMN usage_reported_at INTEGER;
|
||||
CREATE INDEX environment_builds_unreported ON environment_builds (usage_reported_at, finished_at);
|
||||
@@ -24,6 +24,8 @@ const ClaimsSchema = z.object({
|
||||
orgRole: z.enum(['owner', 'admin', 'member']).optional(),
|
||||
/** Same role as Orca Cloud auth spells it in its tokens (`org_role`); either is accepted. */
|
||||
org_role: z.enum(['owner', 'admin', 'member']).optional(),
|
||||
/** Display name (auth sanitizes it, ≤80 chars). Untrusted text: label only, never identity. */
|
||||
name: z.string().max(200).optional(),
|
||||
purpose: z.literal('sandbox')
|
||||
})
|
||||
|
||||
@@ -37,6 +39,14 @@ export type SandboxCaller = {
|
||||
dev?: boolean
|
||||
/** Set when a companion device (phone) signed this request with its own enrolled key. */
|
||||
deviceSessionId?: string
|
||||
/** Display name from the token, for labels next to the user id; never used for access. */
|
||||
name?: string
|
||||
}
|
||||
|
||||
/** One line of plain text, ≤80 chars; anything else is dropped rather than trusted. */
|
||||
function displayName(value: string | undefined): string | undefined {
|
||||
const clean = value?.replace(/[\u0000-\u001f\u007f"\\]+/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 80)
|
||||
return clean || undefined
|
||||
}
|
||||
|
||||
const keySets = new Map<string, JWTVerifyGetKey>()
|
||||
@@ -86,6 +96,7 @@ async function verify(
|
||||
return {
|
||||
userId: claims.sub,
|
||||
accountId: accountIdFor(claims),
|
||||
...(displayName(claims.name) ? { name: displayName(claims.name) } : {}),
|
||||
...(claims.org ? { orgId: claims.org, ...((claims.orgRole ?? claims.org_role) ? { orgRole: claims.orgRole ?? claims.org_role } : {}) } : {})
|
||||
}
|
||||
} catch {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Hosts a workspace's own agent tools need beyond the default allowlist — today the remote MCP
|
||||
// servers the desktop syncs into the sandbox (MCP config sync). The desktop declares them with
|
||||
// PUT /v1/sandboxes/:id/egress/hosts {hosts}; they persist on the row (every boot re-applies them)
|
||||
// and a running sandbox's policy is rewritten at once. Public hostnames only: no wildcards, IPs,
|
||||
// localhost or private suffixes, so the fail-closed allowlist can't be turned into "open".
|
||||
import { z } from 'zod'
|
||||
import { egressPolicy, refreshGitCredentials, type LifecycleDeps } from './lifecycle'
|
||||
import { transition, type SandboxRow } from './store'
|
||||
|
||||
export const MAX_USER_HOSTS = 32
|
||||
const HOST = /^([a-z0-9-]+\.)+[a-z]{2,63}$/
|
||||
const PRIVATE_SUFFIXES = ['.local', '.localhost', '.internal', '.lan', '.home', '.corp', '.intranet']
|
||||
|
||||
export const EgressHostsSchema = z
|
||||
.object({ hosts: z.array(z.string().min(1).max(253)).max(MAX_USER_HOSTS) })
|
||||
.strict()
|
||||
|
||||
/** Normalized, deduplicated, sorted; null when any entry is not an acceptable public hostname. */
|
||||
export function normalizeUserHosts(hosts: readonly string[]): string[] | null {
|
||||
const out = new Set<string>()
|
||||
for (const raw of hosts) {
|
||||
const host = raw.trim().toLowerCase().replace(/\.$/, '')
|
||||
if (!HOST.test(host) || PRIVATE_SUFFIXES.some((suffix) => host.endsWith(suffix))) {
|
||||
return null
|
||||
}
|
||||
out.add(host)
|
||||
}
|
||||
return [...out].sort()
|
||||
}
|
||||
|
||||
export function egressUserHosts(row: Pick<SandboxRow, 'egress_user_hosts'>): string[] {
|
||||
return (row.egress_user_hosts ?? '').split(',').filter(Boolean)
|
||||
}
|
||||
|
||||
/** Records the hosts and, for a running sandbox, rewrites its policy now. */
|
||||
export async function setEgressUserHosts(deps: LifecycleDeps, row: SandboxRow, hosts: string[]): Promise<SandboxRow | null> {
|
||||
const value = hosts.join(',') || null
|
||||
if (value === row.egress_user_hosts) {
|
||||
return row
|
||||
}
|
||||
const next = await transition(deps.db, row, { egress_user_hosts: value }, deps.now())
|
||||
if (!next) {
|
||||
return null
|
||||
}
|
||||
if (next.status === 'running' && next.session_id) {
|
||||
// A policy write replaces the whole policy, so the git route is re-minted alongside.
|
||||
const git = await refreshGitCredentials(deps, next, next.session_id)
|
||||
if (!git) {
|
||||
await deps.provider.updateNetworkPolicy(next.session_id, egressPolicy(deps, next))
|
||||
}
|
||||
}
|
||||
// Counts only; hostnames are not secrets but stay out of logs anyway (they can name private tools).
|
||||
console.log(JSON.stringify({ event: 'egress_user_hosts_set', sandboxId: row.id, count: hosts.length }))
|
||||
return next
|
||||
}
|
||||
@@ -20,6 +20,8 @@ export type Env = {
|
||||
GOLDEN_SNAPSHOT_OVERRIDE?: string
|
||||
/** Git as the sender: `on`, `accounts:<id>,<id>` or off (default). See memberGitEnabled in index.ts. */
|
||||
MEMBER_GIT_IDENTITY?: string
|
||||
/** How long after a member's own send their GitHub identity stays usable (default 30). */
|
||||
MEMBER_GIT_WINDOW_MINUTES?: string
|
||||
/** `release:latest` | `release:v1.2.3`: build golden images from Orca's released orca-server tarball. */
|
||||
GOLDEN_IMAGE_SOURCE?: string
|
||||
/** TTL for Worker-built golden images; unset/0 keeps them until rotated out. */
|
||||
|
||||
@@ -32,6 +32,8 @@ const STARTING_STUCK_MS = 5 * 60_000
|
||||
const SNAPSHOTTING_STUCK_MS = 3 * 60_000
|
||||
/** A secret value shorter than this is not redacted from logs (too likely to match ordinary text). */
|
||||
const MIN_REDACTED_SECRET = 6
|
||||
/** Event-stream keepalive; desktop watchers treat ~90 s of silence as a dead connection. */
|
||||
const HEARTBEAT_MS = 20_000
|
||||
/** Succeeded builds whose snapshot is kept (rollback window); older ones keep their record and log. */
|
||||
export const KEPT_BUILDS = 5
|
||||
const MAX_LOG_BYTES = 2 * 1024 * 1024
|
||||
@@ -75,6 +77,8 @@ export type BuildRow = {
|
||||
updated_at: number
|
||||
generation: number
|
||||
snapshot_deleted_at: number | null
|
||||
/** When billing accepted this build's VM time (null: not yet). */
|
||||
usage_reported_at: number | null
|
||||
}
|
||||
|
||||
export type BuildConfig = {
|
||||
@@ -457,6 +461,7 @@ async function completeBuild(deps: BuildDeps, row: BuildRow, snapshotId: string)
|
||||
// Ours to delete only when this pass is the one that failed the build.
|
||||
await deps.provider.deleteSnapshot(snapshotId).catch(() => undefined)
|
||||
log('environment_build_failed', { buildId: row.id, number: row.number, step: 'snapshot', error: 'smoke_failed' })
|
||||
await reportBuildUsage(deps, failed)
|
||||
return failed
|
||||
}
|
||||
return (await getBuild(deps.db, row.environment_id, row.id)) ?? row
|
||||
@@ -485,6 +490,7 @@ async function completeBuild(deps: BuildDeps, row: BuildRow, snapshotId: string)
|
||||
log('environment_build_prune_failed', { buildId: row.id, error: String(error).slice(0, 200) })
|
||||
)
|
||||
log('environment_build_succeeded', { buildId: row.id, number: row.number, ms: now - row.created_at, snapshotId })
|
||||
await reportBuildUsage(deps, done)
|
||||
return done
|
||||
}
|
||||
|
||||
@@ -570,6 +576,7 @@ async function failBuild(deps: BuildDeps, row: BuildRow, step: string, error: st
|
||||
}
|
||||
await deps.provider.deleteSandbox(row.provider_name).catch(() => undefined)
|
||||
log('environment_build_failed', { buildId: row.id, number: row.number, step, error })
|
||||
await reportBuildUsage(deps, failed)
|
||||
}
|
||||
return failed ?? (await getBuild(deps.db, row.environment_id, row.id))
|
||||
}
|
||||
@@ -590,6 +597,7 @@ export async function cancelBuild(deps: BuildDeps, caller: SandboxCaller, row: B
|
||||
await storeLogs(deps, canceled, { live: true }).catch(() => undefined)
|
||||
await deps.provider.deleteSandbox(row.provider_name).catch(() => undefined)
|
||||
log('environment_build_canceled', { buildId: row.id })
|
||||
await reportBuildUsage(deps, canceled)
|
||||
return (await getBuild(deps.db, row.environment_id, row.id)) ?? canceled
|
||||
}
|
||||
|
||||
@@ -692,12 +700,61 @@ export async function storedLogs(db: D1Database, buildId: string): Promise<strin
|
||||
return results.map((r) => r.data).join('')
|
||||
}
|
||||
|
||||
/**
|
||||
* A build ran a sandbox from its creation to its end (including the smoke boot): bill that time like
|
||||
* a workspace's, at the build's sandbox size, to the member who started it. Idempotent per build
|
||||
* (`build:<id>`); a billing outage leaves it for the cron.
|
||||
*/
|
||||
export async function reportBuildUsage(deps: Pick<BuildDeps, 'db' | 'entitlements' | 'now'>, row: BuildRow): Promise<boolean> {
|
||||
if (row.usage_reported_at !== null || row.finished_at === null || !TERMINAL.has(row.status)) {
|
||||
return false
|
||||
}
|
||||
// A build that never got a VM (refused before boot) used nothing.
|
||||
const startedAt = row.created_at
|
||||
const endedAt = Math.max(row.finished_at, startedAt)
|
||||
if (!row.session_id && row.status !== 'succeeded') {
|
||||
await deps.db.prepare('UPDATE environment_builds SET usage_reported_at = ? WHERE id = ?').bind(deps.now(), row.id).run()
|
||||
return false
|
||||
}
|
||||
let machineSize: MachineSize = 'standard'
|
||||
try {
|
||||
machineSize = parseConfig(row).machineSize ?? 'standard'
|
||||
} catch {
|
||||
machineSize = 'standard'
|
||||
}
|
||||
try {
|
||||
await deps.entitlements.reportUsage({
|
||||
usageId: `build:${row.id}`,
|
||||
accountId: row.account_id,
|
||||
sandboxId: row.id,
|
||||
machineSize,
|
||||
startedAt,
|
||||
endedAt,
|
||||
userId: row.created_by
|
||||
})
|
||||
} catch (error) {
|
||||
log('environment_build_usage_failed', { buildId: row.id, error: String(error).slice(0, 120) })
|
||||
return false
|
||||
}
|
||||
await deps.db.prepare('UPDATE environment_builds SET usage_reported_at = ? WHERE id = ?').bind(deps.now(), row.id).run()
|
||||
return true
|
||||
}
|
||||
|
||||
/** Cron: advance every unfinished build (finalize the ones nobody is watching). */
|
||||
export async function advanceAllBuilds(deps: BuildDeps): Promise<number> {
|
||||
const { results } = await deps.db
|
||||
.prepare(`SELECT * FROM environment_builds WHERE status IN ('starting', 'running', 'snapshotting') ORDER BY updated_at LIMIT 20`)
|
||||
.all<BuildRow>()
|
||||
let moved = 0
|
||||
const { results: unbilled } = await deps.db
|
||||
.prepare(
|
||||
`SELECT * FROM environment_builds WHERE usage_reported_at IS NULL AND finished_at IS NOT NULL
|
||||
AND status IN ('succeeded', 'failed', 'canceled') ORDER BY finished_at LIMIT 20`
|
||||
)
|
||||
.all<BuildRow>()
|
||||
for (const row of unbilled) {
|
||||
await reportBuildUsage(deps, row)
|
||||
}
|
||||
for (const row of results) {
|
||||
try {
|
||||
const next = await advanceBuild(deps, row)
|
||||
@@ -726,6 +783,11 @@ export function streamBuildEvents(
|
||||
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>()
|
||||
const writer = writable.getWriter()
|
||||
const send = (event: unknown) => writer.write(encoder.encode(`${JSON.stringify(event)}\n`))
|
||||
// A quiet step (snapshot, a long compile) prints nothing for minutes: a heartbeat tells watchers the
|
||||
// stream is alive so they don't reconnect (and replay the whole log) for nothing.
|
||||
const heartbeat = setInterval(() => {
|
||||
send({ type: 'heartbeat', at: deps.now() }).catch(() => clearInterval(heartbeat))
|
||||
}, HEARTBEAT_MS)
|
||||
const run = async () => {
|
||||
let row = initial
|
||||
await send({ type: 'build', build: await view(row) })
|
||||
@@ -809,7 +871,10 @@ export function streamBuildEvents(
|
||||
log('environment_build_stream_failed', { buildId: initial.id, error: String(error).slice(0, 200) })
|
||||
return send({ type: 'error', error: 'stream_failed' }).catch(() => undefined)
|
||||
})
|
||||
.finally(() => writer.close().catch(() => undefined))
|
||||
.finally(() => {
|
||||
clearInterval(heartbeat)
|
||||
return writer.close().catch(() => undefined)
|
||||
})
|
||||
return readable
|
||||
}
|
||||
|
||||
|
||||
@@ -314,6 +314,30 @@ async function stepBuild(deps: GoldenImageDeps, row: GoldenRow): Promise<GoldenT
|
||||
|
||||
/** Wipe identity, snapshot (stops the box), drop the build box; the snapshot then waits for its smoke test. */
|
||||
/** Rewrites /opt/orca/bin/<entry point> from VM_SCRIPT_SOURCES (same bytes a release build installs). */
|
||||
/** Stable fingerprint of the control plane's VM entry points (FNV-1a over names and bytes). */
|
||||
export function entryPointsVersion(): string {
|
||||
let hash = 0x811c9dc5
|
||||
for (const name of VM_SCRIPTS) {
|
||||
for (const char of `${name}\u0000${VM_SCRIPT_SOURCES[name]}\u0000`) {
|
||||
hash = Math.imul(hash ^ char.charCodeAt(0), 0x01000193) >>> 0
|
||||
}
|
||||
}
|
||||
return hash.toString(16).padStart(8, '0')
|
||||
}
|
||||
|
||||
/**
|
||||
* Sandboxes booted from an older image get the control plane's current entry points (grant, revoke,
|
||||
* member identities, sender-aware git) once per boot: a marker check, and an install only if stale.
|
||||
*/
|
||||
export async function ensureEntryPoints(deps: { provider: Pick<SandboxProviderApi, 'runCommand'> }, sessionId: string): Promise<void> {
|
||||
const probe = await deps.provider
|
||||
.runCommand(sessionId, { command: 'test', args: ['-e', `/opt/orca/bin/.entry-points-${entryPointsVersion()}`], timeoutMs: 15_000 })
|
||||
.catch(() => null)
|
||||
if (probe?.exitCode !== 0) {
|
||||
await installEntryPoints(deps, sessionId)
|
||||
}
|
||||
}
|
||||
|
||||
export async function installEntryPoints(deps: { provider: Pick<SandboxProviderApi, 'runCommand'> }, sessionId: string): Promise<void> {
|
||||
const env = provisionEnv({ manifest: MANIFEST, provisionScript: PROVISION_SCRIPT, vmScripts: VM_SCRIPT_SOURCES, buildId: 'sync' })
|
||||
const scriptEnv = Object.fromEntries(Object.entries(env).filter(([key]) => key.startsWith('ORCA_VM_SCRIPT_')))
|
||||
@@ -325,6 +349,8 @@ export async function installEntryPoints(deps: { provider: Pick<SandboxProviderA
|
||||
' sudo install -o root -g root -m 0755 "/tmp/$name.new" "/opt/orca/bin/$name"',
|
||||
' rm -f "/tmp/$name.new"',
|
||||
'done',
|
||||
// Marks this set of entry points as installed (ensureEntryPoints skips the install next time).
|
||||
`sudo rm -f /opt/orca/bin/.entry-points-* && sudo touch /opt/orca/bin/.entry-points-${entryPointsVersion()}`,
|
||||
'echo synced'
|
||||
].join('\n')
|
||||
const result = await deps.provider.runCommand(sessionId, { command: 'bash', args: ['-c', install], env: scriptEnv, timeoutMs: 60_000 })
|
||||
|
||||
@@ -11,7 +11,7 @@ import { z } from 'zod'
|
||||
import type { SandboxCaller } from './auth'
|
||||
import { markRevoked, runVmRevoke, type GrantRow } from './grant-revokes'
|
||||
import { LifecycleError, resumeSandbox, syncMemberGit, type LifecycleDeps } from './lifecycle'
|
||||
import { installEntryPoints } from './golden-images'
|
||||
import { ensureEntryPoints } from './golden-images'
|
||||
import type { CommandResult } from './sandbox-provider'
|
||||
import { ORCAD_PORT } from './sandbox-vm-commands'
|
||||
import { getSandbox, touchActivity, type SandboxRow } from './store'
|
||||
@@ -55,15 +55,9 @@ export async function withEntryPoints(
|
||||
command: string,
|
||||
run: () => Promise<CommandResult>
|
||||
): Promise<CommandResult> {
|
||||
const first = await run().catch((error: unknown) => ({ exitCode: -1, stdout: '', stderr: String(error) }))
|
||||
if (first.exitCode === 0) {
|
||||
return first
|
||||
}
|
||||
const probe = await deps.provider.runCommand(sessionId, { command: 'test', args: ['-x', command], timeoutMs: 15_000 }).catch(() => null)
|
||||
if (!probe || probe.exitCode === 0) {
|
||||
return first
|
||||
}
|
||||
await installEntryPoints(deps, sessionId)
|
||||
// Stale or missing scripts from an older image are replaced first (one marker check when current).
|
||||
await ensureEntryPoints(deps, sessionId).catch(() => undefined)
|
||||
void command
|
||||
return run()
|
||||
}
|
||||
|
||||
@@ -275,17 +269,17 @@ export function revokeGrantsForDeviceSession(deps: LifecycleDeps, deviceSessionI
|
||||
* The creator's desktop pairs with the create's own offer; record that device (its id is in the
|
||||
* offer, not secret) so reassigning the workspace or removing the member revokes it like any grant.
|
||||
*/
|
||||
export async function recordOwnerDevice(db: D1Database, row: SandboxRow, pairingCode: string, now: number): Promise<void> {
|
||||
export async function recordOwnerDevice(db: D1Database, row: SandboxRow, pairingCode: string, now: number): Promise<boolean> {
|
||||
const deviceId = pairedDeviceIdOf(pairingCode)
|
||||
if (!deviceId) {
|
||||
return
|
||||
return false
|
||||
}
|
||||
const existing = await db
|
||||
.prepare('SELECT id FROM workspace_grants WHERE sandbox_id = ? AND orcad_device_id = ?')
|
||||
.bind(row.id, deviceId)
|
||||
.first<{ id: string }>()
|
||||
if (existing) {
|
||||
return
|
||||
return false
|
||||
}
|
||||
await db
|
||||
.prepare(
|
||||
@@ -294,6 +288,14 @@ export async function recordOwnerDevice(db: D1Database, row: SandboxRow, pairing
|
||||
)
|
||||
.bind(randomGrantId(), row.id, row.account_id, row.user_id, deviceId, now)
|
||||
.run()
|
||||
return true
|
||||
}
|
||||
|
||||
/** Records the owner's device and, when it is new, lets the VM map it to the owner (git as the sender). */
|
||||
export async function recordOwnerDeviceAndSync(deps: LifecycleDeps, row: SandboxRow, pairingCode: string): Promise<void> {
|
||||
if ((await recordOwnerDevice(deps.db, row, pairingCode, deps.now())) && row.status === 'running' && row.session_id) {
|
||||
await syncMemberGit(deps, row, row.session_id)
|
||||
}
|
||||
}
|
||||
|
||||
export function pairedDeviceIdOf(pairingCode: string): string | null {
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
// Operator (OPERATOR_TOKEN): /v1/internal/* (see operator.ts)
|
||||
// Public share links (src/share-gateway.ts): /s/<token>/*, plus the root-path cookie fallback
|
||||
// Vercel network-policy forwardURL target (Vercel sandbox OIDC): /v1/egress/proxy
|
||||
import { ORCA_CLOUD_IDLE_SLEEP_MINUTES } from '../../../../src/shared/hosted-sandbox/hosted-sandbox-wire'
|
||||
import { z } from 'zod'
|
||||
import { maintainPool, poolConfigFromEnv } from './pool'
|
||||
import { authenticateDesktop } from './auth'
|
||||
@@ -84,6 +85,7 @@ import { reconcile } from './reconcile'
|
||||
import { egressSecretHosts, secretPlaceholder } from './secret-egress'
|
||||
import { SecretError, handleSecretsRoute, keyringFor, type SecretChange } from './secrets'
|
||||
import { refreshAfterSecretChange, refreshSandboxEnv } from './env-refresh'
|
||||
import { EgressHostsSchema, egressUserHosts, normalizeUserHosts, setEgressUserHosts } from './egress-hosts'
|
||||
import { cachedProviderEnv } from './session-env'
|
||||
import { credentialTargets } from './provider-env'
|
||||
import type { Keyring } from './secrets-crypto'
|
||||
@@ -114,7 +116,7 @@ import {
|
||||
GrantRequestSchema,
|
||||
issueGrant,
|
||||
listGrants,
|
||||
recordOwnerDevice,
|
||||
recordOwnerDeviceAndSync,
|
||||
revokeForAccessChange,
|
||||
revokeGrantsForDeviceSession,
|
||||
revokeGrantsForMember,
|
||||
@@ -130,7 +132,7 @@ import {
|
||||
storeUserToken,
|
||||
userAuthorizeUrl
|
||||
} from './github-user-tokens'
|
||||
import { memberGitRoutes } from './member-git'
|
||||
import { MEMBER_GIT_WINDOW_MS, memberGitRoutes, memberSentWithin, recordMemberSend } from './member-git'
|
||||
import {
|
||||
PatchSchema,
|
||||
PresenceSchema,
|
||||
@@ -216,8 +218,10 @@ function egressRoutes(env: Env, deps: Deps): (row: SandboxRow, host: string) =>
|
||||
db: env.SANDBOX_DB,
|
||||
identity: async (row, userId) => {
|
||||
const keyring = keyringFor(env)
|
||||
return keyring && memberGit(row.account_id)
|
||||
? memberGitIdentity(env.SANDBOX_DB, await keyring, githubConnect(env, deps), row.account_id, userId, (deps.now ?? Date.now)())
|
||||
const now = (deps.now ?? Date.now)()
|
||||
// Only while the member is actively driving this workspace (their own client reported a send).
|
||||
return keyring && memberGit(row.account_id) && (await memberSentWithin(env.SANDBOX_DB, row.id, userId, now, memberGitWindowMs(env)))
|
||||
? memberGitIdentity(env.SANDBOX_DB, await keyring, githubConnect(env, deps), row.account_id, userId, now)
|
||||
: null
|
||||
}
|
||||
})
|
||||
@@ -587,18 +591,28 @@ export function egressPlaceholderMode(value: string | undefined): boolean | Read
|
||||
}
|
||||
|
||||
/**
|
||||
* Git as the sender is opt-in (MEMBER_GIT_IDENTITY = on | accounts:<id>,<id>; default off): members
|
||||
* of a shared workspace share one VM user and the sender id is not yet stamped by orcad, so a member
|
||||
* could push as another member of the same workspace (to that repository only). Off = app identity.
|
||||
* Git as the sender (MEMBER_GIT_IDENTITY: unset = on for every account, `off`, `on`, or
|
||||
* `accounts:<id>,<id>`). orcad attests which paired device sent each turn and the root-only
|
||||
* orca-git-identity hands an agent only that member's identity. Residual risk: members share the VM
|
||||
* user, so a member with a shell could subvert orcad itself; agents and prompts cannot.
|
||||
*/
|
||||
function memberGitWindowMs(env: Env): number {
|
||||
const minutes = Number(env.MEMBER_GIT_WINDOW_MINUTES)
|
||||
return Number.isFinite(minutes) && minutes > 0 ? minutes * 60_000 : MEMBER_GIT_WINDOW_MS
|
||||
}
|
||||
|
||||
export function memberGitEnabled(env: Env): (accountId: string) => boolean {
|
||||
// On by default: orcad attests each turn's sender and orca-git-identity refuses any other member.
|
||||
if (env.MEMBER_GIT_IDENTITY === undefined || env.MEMBER_GIT_IDENTITY.trim() === '') {
|
||||
return () => true
|
||||
}
|
||||
const mode = egressPlaceholderMode(env.MEMBER_GIT_IDENTITY)
|
||||
return (accountId) => mode === true || (mode instanceof Set && mode.has(accountId))
|
||||
}
|
||||
|
||||
async function memberIdentityFor(env: Env, deps: Deps): Promise<LifecycleDeps['memberIdentity']> {
|
||||
const keyring = keyringFor(env)
|
||||
if (!keyring || env.MEMBER_GIT_IDENTITY === undefined || env.MEMBER_GIT_IDENTITY === 'off') {
|
||||
if (!keyring || env.MEMBER_GIT_IDENTITY === 'off') {
|
||||
return undefined
|
||||
}
|
||||
const enabled = memberGitEnabled(env)
|
||||
@@ -751,6 +765,10 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {})
|
||||
})) ?? fail('not_found', 404)
|
||||
)
|
||||
}
|
||||
if (url.pathname === '/v1/config' && request.method === 'GET') {
|
||||
// Public product constants (no auth): every client reads these instead of hardcoding them.
|
||||
return json({ idleSleepMinutes: ORCA_CLOUD_IDLE_SLEEP_MINUTES })
|
||||
}
|
||||
if (url.pathname === '/v1/github/dev-connect' && env.SANDBOX_ENV !== 'prod') {
|
||||
// Dev-only, link-shareable start of the user flow: mints a fresh 10-min state per click for a dev:<sub>
|
||||
// account (desktop builds call POST /v1/github/connect instead).
|
||||
@@ -967,7 +985,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {})
|
||||
return await workspaceRoute(request, url, env, deps, caller)
|
||||
}
|
||||
const match =
|
||||
/^\/v1\/sandboxes(?:\/([A-Za-z0-9_]+)(?:\/(suspend|resume|lease|rollover|wake|restart|status|archive|unarchive|env\/refresh|ssh-keys|desktop))?)?$/.exec(
|
||||
/^\/v1\/sandboxes(?:\/([A-Za-z0-9_]+)(?:\/(suspend|resume|lease|rollover|wake|restart|status|archive|unarchive|env\/refresh|egress\/hosts|ssh-keys|desktop))?)?$/.exec(
|
||||
url.pathname
|
||||
)
|
||||
if (!match) {
|
||||
@@ -1001,7 +1019,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {})
|
||||
return fail('invalid_request', 400)
|
||||
}
|
||||
const ready = await createSandbox(life, caller, body.data)
|
||||
await recordOwnerDevice(life.db, ready.row, ready.connection.pairingCode, life.now())
|
||||
await recordOwnerDeviceAndSync(life, ready.row, ready.connection.pairingCode)
|
||||
return readyResponse(ready)
|
||||
}
|
||||
const row = await getSandbox(env.SANDBOX_DB, id)
|
||||
@@ -1034,12 +1052,12 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {})
|
||||
return json({ sandbox: view(await suspendSandbox(life, row)) })
|
||||
case 'POST resume': {
|
||||
const ready = await resumeSandbox(life, row)
|
||||
await recordOwnerDevice(life.db, ready.row, ready.connection.pairingCode, life.now())
|
||||
await recordOwnerDeviceAndSync(life, ready.row, ready.connection.pairingCode)
|
||||
return readyResponse(ready)
|
||||
}
|
||||
case 'POST rollover': {
|
||||
const ready = await rolloverSandbox(life, row)
|
||||
await recordOwnerDevice(life.db, ready.row, ready.connection.pairingCode, life.now())
|
||||
await recordOwnerDeviceAndSync(life, ready.row, ready.connection.pairingCode)
|
||||
return readyResponse(ready)
|
||||
}
|
||||
case 'POST wake':
|
||||
@@ -1048,7 +1066,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {})
|
||||
// User-triggered and unbounded otherwise: shares the per-account start rate limit.
|
||||
await consumeCreateToken(life.db, life.config.guardrails!, caller.accountId, life.now())
|
||||
const ready = await restartSandbox(life, row)
|
||||
await recordOwnerDevice(life.db, ready.row, ready.connection.pairingCode, life.now())
|
||||
await recordOwnerDeviceAndSync(life, ready.row, ready.connection.pairingCode)
|
||||
return json(readyView(ready.row, ready.connection))
|
||||
}
|
||||
case 'GET status':
|
||||
@@ -1068,6 +1086,20 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {})
|
||||
}
|
||||
case 'POST env/refresh':
|
||||
return json({ applied: await refreshSandboxEnv(life, row) })
|
||||
case 'GET egress/hosts':
|
||||
return json({ hosts: egressUserHosts(row) })
|
||||
case 'PUT egress/hosts': {
|
||||
const body = EgressHostsSchema.safeParse(await readJson(request))
|
||||
const hosts = body.success ? normalizeUserHosts(body.data.hosts) : null
|
||||
if (!hosts) {
|
||||
return fail('invalid_request', 400, 'invalid_host')
|
||||
}
|
||||
const next = await setEgressUserHosts(life, row, hosts)
|
||||
if (!next) {
|
||||
return fail('conflict', 409, 'concurrent_transition')
|
||||
}
|
||||
return json({ hosts: egressUserHosts(next) })
|
||||
}
|
||||
case 'POST desktop': {
|
||||
// Runs only the image's fixed entry point; the desktop listens on the VM's loopback and is
|
||||
// reached through the paired runtime's port forward, never a public route.
|
||||
@@ -1153,7 +1185,7 @@ async function workspaceRoute(request: Request, url: URL, env: Env, deps: Deps,
|
||||
if (url.pathname === '/v1/team/workspaces') {
|
||||
return request.method === 'GET' ? json(await listWorkspaces(db, caller, url, now)) : fail('not_found', 404)
|
||||
}
|
||||
const match = /^\/v1\/team\/workspaces\/(sbx_[0-9a-f]{24})(?:\/(presence|wake|lease|grants)(?:\/(grt_[0-9a-f]{24}))?)?$/.exec(url.pathname)
|
||||
const match = /^\/v1\/team\/workspaces\/(sbx_[0-9a-f]{24})(?:\/(presence|wake|lease|grants|sent)(?:\/(grt_[0-9a-f]{24}))?)?$/.exec(url.pathname)
|
||||
if (!match) {
|
||||
return fail('not_found', 404)
|
||||
}
|
||||
@@ -1164,6 +1196,14 @@ async function workspaceRoute(request: Request, url: URL, env: Env, deps: Deps,
|
||||
if (route === 'GET ') {
|
||||
return json({ workspace: await view() })
|
||||
}
|
||||
if (route === 'POST sent') {
|
||||
// The caller's own client: "I just sent a message here" (opens their git-as-sender window).
|
||||
if (!canOpen(caller, row)) {
|
||||
throw new WorkspaceError('not_found')
|
||||
}
|
||||
await recordMemberSend(db, row.id, caller.userId, now)
|
||||
return json({ ok: true })
|
||||
}
|
||||
if (route === 'POST presence') {
|
||||
const body = PresenceSchema.safeParse(await readJson(request))
|
||||
if (!body.success) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
} from './guardrails'
|
||||
import { DEFAULT_EGRESS_ALLOWLIST, compileNetworkPolicy, gitBasicAuthorization, type CredentialRoute } from './network-policy'
|
||||
import { applyQueuedRevokes } from './grant-revokes'
|
||||
import { ensureEntryPoints } from './golden-images'
|
||||
import { memberPlaceholder, type MemberGitIdentity } from './github-user-tokens'
|
||||
import { connectedMembers, memberGitConfigs } from './member-git'
|
||||
import type { RetentionConfig } from './retention'
|
||||
@@ -300,7 +301,7 @@ async function boot(
|
||||
// Nothing in the VM uses git or model keys before orcad answers and the desktop attaches.
|
||||
const needsPolicy =
|
||||
!options.policyApplied &&
|
||||
(git || routed.egress_secret_hosts || row.egress_secret_hosts || routed.egress_allow_hosts || row.egress_allow_hosts)
|
||||
(git || routed.egress_secret_hosts || row.egress_secret_hosts || routed.egress_allow_hosts || row.egress_allow_hosts || row.egress_user_hosts)
|
||||
// Members revoked while the VM slept must be gone before orcad accepts its first sign-in.
|
||||
await applyQueuedRevokes(deps.db, deps.provider, row.id, state.session.id, deps.now())
|
||||
const [, connection] = await Promise.all([
|
||||
@@ -382,10 +383,11 @@ export async function syncMemberGit(deps: LifecycleDeps, row: SandboxRow, sessio
|
||||
return 0
|
||||
}
|
||||
try {
|
||||
await ensureEntryPoints(deps, sessionId)
|
||||
await refreshGitCredentials(deps, row, sessionId)
|
||||
const configs = await memberGitConfigs({ db: deps.db, identity: deps.memberIdentity }, row)
|
||||
const encoded = btoa(String.fromCharCode(...new TextEncoder().encode(JSON.stringify(configs))))
|
||||
await deps.provider.runCommand(sessionId, { command: MEMBERS_COMMAND, env: { ORCA_MEMBERS_B64: encoded }, timeoutMs: 30_000 })
|
||||
await deps.provider.runCommand(sessionId, { command: MEMBERS_COMMAND, env: { ORCA_MEMBERS_B64: encoded }, sudo: true, timeoutMs: 30_000 })
|
||||
return configs.length
|
||||
} catch {
|
||||
return 0
|
||||
|
||||
@@ -2,15 +2,38 @@
|
||||
// grant) who connected GitHub gets a placeholder of their own. The VM's `orca-git-as <member>` sends
|
||||
// it; the egress proxy swaps it for that member's GitHub user token, and only for the workspace
|
||||
// repository (git smart HTTP on github.com, REST under /repos/<owner>/<repo> on api.github.com).
|
||||
// Trust note: members of a shared workspace share one VM user, so a member can push as another
|
||||
// member of the same workspace to the same repository; tokens never enter the VM and cannot be
|
||||
// used for any other repository or API.
|
||||
// Who may use which identity is decided in the VM by orcad's attestation: orca-git-identity (root)
|
||||
// maps the paired device that sent the agent's latest turn to a member and refuses any other member;
|
||||
// member files are root-only. Tokens never enter the VM and work for this repository only. Residual
|
||||
// risk: members share the VM user, so a member with a shell could subvert orcad itself.
|
||||
import type { ProxiedRoute } from './egress-proxy'
|
||||
import { parseGitHubRepo } from './github-app'
|
||||
import { memberPlaceholder, type MemberGitIdentity } from './github-user-tokens'
|
||||
import { gitBasicAuthorization } from './network-policy'
|
||||
import type { SandboxRow } from './store'
|
||||
|
||||
/** A member's GitHub identity works only this long after they themselves sent a message (default). */
|
||||
export const MEMBER_GIT_WINDOW_MS = 30 * 60_000
|
||||
|
||||
/** Their own client reports each send (their Orca token): the window cannot be opened from the VM. */
|
||||
export async function recordMemberSend(db: D1Database, sandboxId: string, userId: string, now: number): Promise<void> {
|
||||
await db
|
||||
.prepare(
|
||||
`INSERT INTO workspace_member_sends (sandbox_id, user_id, sent_at) VALUES (?, ?, ?)
|
||||
ON CONFLICT (sandbox_id, user_id) DO UPDATE SET sent_at = excluded.sent_at`
|
||||
)
|
||||
.bind(sandboxId, userId, now)
|
||||
.run()
|
||||
}
|
||||
|
||||
export async function memberSentWithin(db: D1Database, sandboxId: string, userId: string, now: number, windowMs: number): Promise<boolean> {
|
||||
const row = await db
|
||||
.prepare('SELECT sent_at FROM workspace_member_sends WHERE sandbox_id = ? AND user_id = ?')
|
||||
.bind(sandboxId, userId)
|
||||
.first<{ sent_at: number }>()
|
||||
return row !== null && now - row.sent_at <= windowMs
|
||||
}
|
||||
|
||||
export type MemberGitDeps = {
|
||||
db: D1Database
|
||||
identity: (row: SandboxRow, userId: string) => Promise<MemberGitIdentity | null>
|
||||
@@ -86,7 +109,7 @@ export function memberGitRoutes(deps: MemberGitDeps) {
|
||||
|
||||
/** What the VM writes per member for `orca-git-as` (no token: the placeholder and author identity). */
|
||||
export async function memberGitConfigs(deps: MemberGitDeps, row: SandboxRow) {
|
||||
const out: { userId: string; name: string; email: string; authorization: string }[] = []
|
||||
const out: { userId: string; name: string; email: string; authorization: string; deviceIds: string[] }[] = []
|
||||
for (const userId of await connectedMembers(deps.db, row)) {
|
||||
const identity = await deps.identity(row, userId)
|
||||
if (identity) {
|
||||
@@ -94,9 +117,21 @@ export async function memberGitConfigs(deps: MemberGitDeps, row: SandboxRow) {
|
||||
userId,
|
||||
name: identity.name,
|
||||
email: identity.email,
|
||||
authorization: gitBasicAuthorization(await memberPlaceholder(row.id, userId))
|
||||
authorization: gitBasicAuthorization(await memberPlaceholder(row.id, userId)),
|
||||
// The member's paired orcad devices: orca-git-identity maps orcad's attested sender to a member.
|
||||
deviceIds: await memberDeviceIds(deps.db, row.id, userId)
|
||||
})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
async function memberDeviceIds(db: D1Database, sandboxId: string, userId: string): Promise<string[]> {
|
||||
const { results } = await db
|
||||
.prepare(
|
||||
`SELECT orcad_device_id FROM workspace_grants WHERE sandbox_id = ? AND user_id = ? AND status = 'active' AND orcad_device_id IS NOT NULL`
|
||||
)
|
||||
.bind(sandboxId, userId)
|
||||
.all<{ orcad_device_id: string }>()
|
||||
return results.map((r) => r.orcad_device_id)
|
||||
}
|
||||
|
||||
@@ -84,6 +84,7 @@ export async function sessionSecretEnv(
|
||||
return { env: swapped.env, egressSecretHosts: encodeEgressSecretHosts(swapped.targets), egressAllowHosts }
|
||||
}
|
||||
|
||||
export function egressAllowHosts(row: Pick<SandboxRow, 'egress_allow_hosts'>): string[] {
|
||||
return (row.egress_allow_hosts ?? '').split(',').filter(Boolean)
|
||||
/** Provider hosts from the session's secrets plus the workspace's declared tool hosts (MCP). */
|
||||
export function egressAllowHosts(row: Pick<SandboxRow, 'egress_allow_hosts'> & Partial<Pick<SandboxRow, 'egress_user_hosts'>>): string[] {
|
||||
return [...(row.egress_allow_hosts ?? '').split(','), ...(row.egress_user_hosts ?? '').split(',')].filter(Boolean)
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ export type SandboxRow = {
|
||||
egress_secret_hosts: string | null
|
||||
/** Comma list of custom model-provider hosts added to this session's egress allowlist. */
|
||||
egress_allow_hosts: string | null
|
||||
/** Comma list of hosts the workspace's own tools need (synced remote MCP servers); set by the desktop. */
|
||||
egress_user_hosts: string | null
|
||||
retention_warned_at: number | null
|
||||
/** Environment build this sandbox first booted from (null: the golden image). Never changes. */
|
||||
environment_id: string | null
|
||||
@@ -178,7 +180,7 @@ type Patch = Partial<Omit<SandboxRow, 'id' | 'account_id' | 'generation' | 'crea
|
||||
const PATCHABLE = new Set<string>([
|
||||
'status', 'session_id', 'session_started_at', 'session_deadline_at', 'lease_expires_at',
|
||||
'usage_settled_at', 'last_error', 'destroyed_at', 'github_token_expires_at',
|
||||
'egress_secret_hosts', 'egress_allow_hosts', 'repo_url', 'retention_warned_at', 'orcad_endpoint', 'ready_at',
|
||||
'egress_secret_hosts', 'egress_allow_hosts', 'egress_user_hosts', 'repo_url', 'retention_warned_at', 'orcad_endpoint', 'ready_at',
|
||||
'archived_at', 'archive_expires_at'
|
||||
])
|
||||
|
||||
|
||||
@@ -327,3 +327,27 @@ describe('staged egress placeholder rollout', () => {
|
||||
expect(row!.egress_secret_hosts).toBe('ANTHROPIC_AUTH_TOKEN@llm.example.com')
|
||||
})
|
||||
})
|
||||
|
||||
describe('workspace tool hosts (synced remote MCP servers)', () => {
|
||||
it('opens declared hosts on the running sandbox now and on every later boot; refuses wildcards and private names', async () => {
|
||||
const { h, as } = await setup()
|
||||
const mia = await as('mia')
|
||||
const created = await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-1', machineSize: 'standard' }, bearer: mia })
|
||||
const id: string = created.body.sandbox.id
|
||||
const box = () => [...h.vercel.boxes.values()][0]!
|
||||
expect(box().policy.allow['mcp.linear.app']).toBeUndefined()
|
||||
const put = await h.call('PUT', `/v1/sandboxes/${id}/egress/hosts`, { bearer: mia, body: { hosts: ['MCP.linear.app', 'mcp.notion.com.', 'mcp.linear.app'] } })
|
||||
expect(put.body).toEqual({ hosts: ['mcp.linear.app', 'mcp.notion.com'] })
|
||||
expect(box().policy.allow['mcp.linear.app']).toEqual([])
|
||||
expect(box().policy.allow['*']).toBeUndefined()
|
||||
for (const bad of ['*', '*.example.com', '10.0.0.1', 'localhost', 'box.internal', 'http://x.example.com']) {
|
||||
expect((await h.call('PUT', `/v1/sandboxes/${id}/egress/hosts`, { bearer: mia, body: { hosts: [bad] } })).status).toBe(400)
|
||||
}
|
||||
expect((await h.call('PUT', `/v1/sandboxes/${id}/egress/hosts`, { bearer: await as('bob'), body: { hosts: ['x.example.com'] } })).status).toBe(404)
|
||||
// Sleep and wake: the hosts come back with the new session's policy.
|
||||
await h.call('POST', `/v1/sandboxes/${id}/suspend`, { bearer: mia })
|
||||
await h.call('POST', `/v1/sandboxes/${id}/resume`, { bearer: mia })
|
||||
expect(box().policy.allow['mcp.notion.com']).toEqual([])
|
||||
expect((await h.call('GET', `/v1/sandboxes/${id}/egress/hosts`, { bearer: mia })).body).toEqual({ hosts: ['mcp.linear.app', 'mcp.notion.com'] })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
HostedSandboxConfigResponseSchema,
|
||||
ORCA_CLOUD_IDLE_SLEEP_MINUTES
|
||||
} from '../../../../src/shared/hosted-sandbox/hosted-sandbox-wire'
|
||||
import { DEFAULT_PLANS } from '../../billing/src/plans'
|
||||
import { createHarness } from './harness'
|
||||
|
||||
describe('GET /v1/config', () => {
|
||||
it('serves the idle-sleep minutes every client reads; billing advertises the same constant', async () => {
|
||||
const h = await createHarness()
|
||||
const res = await h.send(new Request('https://sandbox.test/v1/config'))
|
||||
expect(res.status).toBe(200)
|
||||
const body = HostedSandboxConfigResponseSchema.parse(await res.json())
|
||||
expect(body.idleSleepMinutes).toBe(ORCA_CLOUD_IDLE_SLEEP_MINUTES)
|
||||
expect(DEFAULT_PLANS.team.idleSleepMinutes).toBe(ORCA_CLOUD_IDLE_SLEEP_MINUTES)
|
||||
})
|
||||
})
|
||||
@@ -93,6 +93,37 @@ describe('org environments', () => {
|
||||
expect([...h.vercel.boxes.keys()].filter((name) => name.includes('envbuild'))).toEqual([])
|
||||
})
|
||||
|
||||
it('bills build sandbox time to the member who started it, retrying when billing is down, and refuses blocked orgs', async () => {
|
||||
const { h, adminCall, envId, build } = await setup()
|
||||
await adminCall('PATCH', `/v1/environments/${envId}`, { machineSize: 'large' })
|
||||
h.billing.down = true
|
||||
const first = await build(0)
|
||||
expect(h.billing.usage).toEqual([])
|
||||
const unreported = h.raw.prepare('SELECT usage_reported_at FROM environment_builds WHERE id = ?').get(first.id) as { usage_reported_at: number | null }
|
||||
expect(unreported.usage_reported_at).toBeNull()
|
||||
h.billing.down = false
|
||||
const { advanceAllBuilds } = await import('../src/environment-builds')
|
||||
await advanceAllBuilds({
|
||||
db: h.env.SANDBOX_DB,
|
||||
provider: h.vercel,
|
||||
entitlements: { checkStart: async () => h.billing.decision, reportUsage: async (s) => { h.billing.usage.push(s); return h.billing.usageAnswer }, listSuspensions: async () => [] },
|
||||
now: h.now,
|
||||
config: { environment: 'test', goldenSnapshotId: 'snap_golden', region: 'iad1' }
|
||||
})
|
||||
expect(h.billing.usage).toEqual([
|
||||
expect.objectContaining({ usageId: `build:${first.id}`, sandboxId: first.id, machineSize: 'large', userId: 'alice', accountId: 'org:acme' })
|
||||
])
|
||||
const failed = await build(20)
|
||||
expect(h.billing.usage.at(-1)).toMatchObject({ usageId: `build:${failed.id}`, userId: 'alice' })
|
||||
const usage = h.billing.usage.at(-1)!
|
||||
expect(usage.endedAt).toBeGreaterThan(usage.startedAt)
|
||||
// Out of sandbox hours / payment failed: builds are refused like starts.
|
||||
h.billing.decision = { allow: false, reason: 'hours_exhausted', suspendRunning: false }
|
||||
const refused = await adminCall('POST', `/v1/environments/${envId}/builds`)
|
||||
expect(refused.status).toBe(403)
|
||||
expect(refused.body.reason).toBe('hours_exhausted')
|
||||
})
|
||||
|
||||
it('refuses a second concurrent build and can cancel one', async () => {
|
||||
const { adminCall, envId } = await setup()
|
||||
const started = await adminCall('POST', `/v1/environments/${envId}/builds`)
|
||||
|
||||
@@ -12,7 +12,7 @@ const KEK = Buffer.alloc(32, 7).toString('base64')
|
||||
const OP = 'o'.repeat(40)
|
||||
const REPO = 'https://github.com/acme/app'
|
||||
|
||||
async function setup(memberGit: string | undefined = 'on') {
|
||||
async function setup(memberGit: string | undefined = undefined) {
|
||||
const { privateKey, publicKey } = await generateKeyPair('RS256')
|
||||
const jwks = { keys: [{ ...(await exportJWK(publicKey)), kid: 'v1', alg: 'RS256' }] }
|
||||
const upstream: { url: string; authorization: string | null }[] = []
|
||||
@@ -37,11 +37,19 @@ async function setup(memberGit: string | undefined = 'on') {
|
||||
body: { workspaceId: 'ws-1', machineSize: 'standard', repo: { url: REPO }, visibility: 'team' }
|
||||
})
|
||||
const id: string = created.body.sandbox.id
|
||||
const seed = (userId: string, login: string, githubUserId: number) =>
|
||||
h.call('POST', '/v1/internal/dev/member-github', {
|
||||
const sent = async (userId: string) =>
|
||||
h.call('POST', `/v1/team/workspaces/${id}/sent`, { bearer: await as(userId) })
|
||||
// Connect GitHub, then (their own client) report a send so their identity window is open.
|
||||
const seed = async (userId: string, login: string, githubUserId: number, send = true) => {
|
||||
const stored = await h.call('POST', '/v1/internal/dev/member-github', {
|
||||
bearer: OP,
|
||||
body: { accountId: 'org:acme', userId, repoUrl: REPO, login, githubUserId, name: `${login} name` }
|
||||
})
|
||||
if (send) {
|
||||
await sent(userId)
|
||||
}
|
||||
return stored
|
||||
}
|
||||
async function oidc() {
|
||||
return new SignJWT({ team_id: 'team_orca', project_id: 'prj_orca', sandbox_id: 'sbx-v', sandbox_name: `orca-test-${id}` })
|
||||
.setProtectedHeader({ alg: 'RS256', kid: 'v1' })
|
||||
@@ -68,7 +76,7 @@ async function setup(memberGit: string | undefined = 'on') {
|
||||
)
|
||||
return { status: response.status, body: await response.text() }
|
||||
}
|
||||
return { h, id, as, seed, proxied, upstream }
|
||||
return { h, id, as, seed, sent, proxied, upstream }
|
||||
}
|
||||
|
||||
describe('git as the sender', () => {
|
||||
@@ -109,6 +117,10 @@ describe('git as the sender', () => {
|
||||
expect(githubRules).toContain(bobPh)
|
||||
const sync = t.h.vercel.commands.filter((c) => c.command === MEMBERS_COMMAND).at(-1)!
|
||||
const members = JSON.parse(Buffer.from(sync.env.ORCA_MEMBERS_B64!, 'base64').toString())
|
||||
// orca-git-identity maps orcad's attested sender device to the member (bob's grant device here).
|
||||
expect(members.find((m: { userId: string }) => m.userId === 'bob').deviceIds).toEqual([
|
||||
[...t.h.vercel.boxes.values()][0]!.devices.at(-1)
|
||||
])
|
||||
expect(members.map((m: { userId: string; email: string }) => [m.userId, m.email])).toEqual([
|
||||
['alice', '11+alice-gh@users.noreply.github.com'],
|
||||
['bob', '22+bob-gh@users.noreply.github.com']
|
||||
@@ -121,11 +133,26 @@ describe('git as the sender', () => {
|
||||
expect(await t.proxied('github.com', '/acme/app.git/info/refs', bobPh)).toEqual({ status: 403, body: 'placeholder_missing' })
|
||||
})
|
||||
|
||||
it('is off unless the org opted in: member placeholders get no credential, the VM no member configs', async () => {
|
||||
it('MEMBER_GIT_IDENTITY=off turns it off: member placeholders get no credential, the VM no member configs', async () => {
|
||||
const t = await setup('off')
|
||||
await t.seed('alice', 'alice-gh', 11)
|
||||
const alicePh = await memberPlaceholder(t.id, 'alice')
|
||||
expect(await t.proxied('github.com', '/acme/app.git/git-receive-pack', gitBasicAuthorization(alicePh))).toEqual({ status: 403, body: 'credential_unavailable' })
|
||||
expect(t.h.vercel.commands.some((c) => c.command === MEMBERS_COMMAND)).toBe(false)
|
||||
})
|
||||
|
||||
it('honors a member identity only within the window after their own client reported a send', async () => {
|
||||
const t = await setup()
|
||||
await t.seed('alice', 'alice-gh', 11, false)
|
||||
const ph = gitBasicAuthorization(await memberPlaceholder(t.id, 'alice'))
|
||||
// Connected, but never sent: nothing in the VM can use Alice's identity.
|
||||
expect(await t.proxied('github.com', '/acme/app.git/git-receive-pack', ph)).toEqual({ status: 403, body: 'credential_unavailable' })
|
||||
await t.sent('alice')
|
||||
expect((await t.proxied('github.com', '/acme/app.git/git-receive-pack', ph)).status).toBe(200)
|
||||
t.h.advance(31 * 60_000)
|
||||
expect(await t.proxied('github.com', '/acme/app.git/git-receive-pack', ph)).toEqual({ status: 403, body: 'credential_unavailable' })
|
||||
// A teammate cannot open Alice's window for her.
|
||||
expect((await t.h.call('POST', `/v1/team/workspaces/${t.id}/sent`, { bearer: await t.as('bob') })).status).toBe(200)
|
||||
expect(await t.proxied('github.com', '/acme/app.git/git-receive-pack', ph)).toEqual({ status: 403, body: 'credential_unavailable' })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -38,8 +38,6 @@
|
||||
// Golden image from image/build-golden-image.ts; lifecycle routes answer 503 without one.
|
||||
"GOLDEN_SNAPSHOT_ID": "snap_vpcmcCAgTtISiTZgsCosOlThQbFd",
|
||||
"GITHUB_APP_SLUG": "orca-cloud-dev",
|
||||
"EGRESS_PROXY_URL": "https://orca-sandbox-control-dev.lovecast.workers.dev/v1/egress/proxy",
|
||||
// Git as the sender is opt-in per org (see memberGitEnabled); dev: only the W4 rehearsal org.
|
||||
"MEMBER_GIT_IDENTITY": "accounts:dev:org:w4org"
|
||||
"EGRESS_PROXY_URL": "https://orca-sandbox-control-dev.lovecast.workers.dev/v1/egress/proxy"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,5 +264,19 @@
|
||||
"text": "text-[13px] leading-5",
|
||||
"dynamic": false,
|
||||
"count": 1
|
||||
},
|
||||
{
|
||||
"filePath": "src/renderer/src/components/cloud-environment/cloud-environment-fixture-data.ts",
|
||||
"kind": "object-property:error",
|
||||
"text": "install_failed",
|
||||
"dynamic": false,
|
||||
"count": 1
|
||||
},
|
||||
{
|
||||
"filePath": "src/renderer/src/components/cloud-environment/cloud-environment-fixture-data.ts",
|
||||
"kind": "object-property:error",
|
||||
"text": "canceled",
|
||||
"dynamic": false,
|
||||
"count": 1
|
||||
}
|
||||
]
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
# Gate for merging into cloud/integration (no CI on nwparker/orca-private): run from your branch's clone.
|
||||
# Merges the current private/cloud/integration into a throwaway worktree with your branch, then requires
|
||||
# typecheck, the full lint suite (pre-commit hooks only lint staged files) and the tests your branch changed.
|
||||
set -euo pipefail
|
||||
branch=$(git rev-parse --abbrev-ref HEAD)
|
||||
git fetch -q private +cloud/integration:refs/remotes/private/cloud/integration
|
||||
dir=$(mktemp -d "${TMPDIR:-/tmp}/cloud-premerge-XXXX")
|
||||
trap 'git worktree remove --force "$dir" >/dev/null 2>&1 || true' EXIT
|
||||
git worktree add -q --detach "$dir" HEAD
|
||||
cd "$dir"
|
||||
git merge -q --no-edit private/cloud/integration
|
||||
pnpm install --frozen-lockfile >/dev/null
|
||||
pnpm typecheck
|
||||
pnpm lint
|
||||
tests=$(git diff --name-only --diff-filter=AM private/cloud/integration...HEAD -- 'src/*.test.ts' 'src/*.test.tsx' | tr '\n' ' ')
|
||||
if [[ -n "$tests" ]]; then
|
||||
node config/scripts/native-build/node-addons/ensure-native-runtime.mjs --runtime=node >/dev/null
|
||||
npx vitest run --config config/vitest.config.ts $tests
|
||||
fi
|
||||
for app in $(git diff --name-only private/cloud/integration...HEAD -- cloud/apps | cut -d/ -f1-3 | sort -u); do
|
||||
(cd "$app" && npx vitest run)
|
||||
done
|
||||
echo "premerge OK: $branch merges cleanly into cloud/integration with typecheck, lint and tests green."
|
||||
@@ -233,6 +233,17 @@ export const HANDLER_GROUPS: readonly HandlerGroup[] = [
|
||||
keys: ['diagnostics memory', 'terminal-daemon status', 'terminal-daemon restart'],
|
||||
load: async () => (await import('./handlers/diagnostics.js')).DIAGNOSTICS_HANDLERS
|
||||
},
|
||||
{
|
||||
name: 'cloud-env',
|
||||
keys: [
|
||||
'cloud env show',
|
||||
'cloud env set-install-script',
|
||||
'cloud env build',
|
||||
'cloud env builds',
|
||||
'cloud env logs'
|
||||
],
|
||||
load: async () => (await import('./handlers/cloud-env.js')).CLOUD_ENV_HANDLERS
|
||||
},
|
||||
{
|
||||
name: 'speech',
|
||||
keys: ['speech openai-key status', 'speech openai-key set', 'speech openai-key clear'],
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
// Human-readable output for `orca cloud env ...`.
|
||||
import type {
|
||||
CloudEnvironmentBuildsData,
|
||||
CloudEnvironmentShowData
|
||||
} from '../../shared/hosted-sandbox/hosted-environment-cli-contract'
|
||||
import type { HostedEnvironmentBuildView } from '../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
|
||||
export function formatOutcome(build: HostedEnvironmentBuildView): string {
|
||||
if (build.status === 'succeeded') {
|
||||
return `Build #${build.number} succeeded in ${formatDuration(build.durationMs)}.${build.active ? ' It is now the active build.' : ''}`
|
||||
}
|
||||
if (build.status === 'canceled') {
|
||||
return `Build #${build.number} was canceled.`
|
||||
}
|
||||
if (build.status === 'failed') {
|
||||
const step = build.failedStep ? ` at the ${build.failedStep} step` : ''
|
||||
return `Build #${build.number} failed${step}: ${build.error ?? 'unknown error'}.`
|
||||
}
|
||||
return `Build #${build.number} is ${build.status}.`
|
||||
}
|
||||
|
||||
export function formatShow(data: CloudEnvironmentShowData): string {
|
||||
const env = data.environment
|
||||
const lines = [
|
||||
`Environment: ${env.name} (${env.id})`,
|
||||
`Sandbox size: ${env.machineSize === 'large' ? 'Large (8 vCPU · 16 GB)' : 'Standard (4 vCPU · 8 GB)'} Revision: ${env.revision}${data.canAdmin ? '' : ' (read-only: ask an owner or admin to change it)'}`,
|
||||
'Repos:',
|
||||
...(env.repos.length > 0
|
||||
? env.repos.map(
|
||||
(repo, index) =>
|
||||
` ${repo.url}${repo.defaultBranch ? ` (${repo.defaultBranch})` : ''}${index === 0 ? ' <- install script runs here' : ''}`
|
||||
)
|
||||
: [' (none)']),
|
||||
`Variables from Cloud secrets (Shared) passed to the install script: ${env.envVarNames.length > 0 ? env.envVarNames.join(', ') : '(none)'}`,
|
||||
`Active build: ${data.activeBuild ? describeBuild(data.activeBuild) : '(none)'}`,
|
||||
`Latest build: ${data.latestBuild ? describeBuild(data.latestBuild) : '(none)'}`
|
||||
]
|
||||
if (data.staleness) {
|
||||
lines.push(
|
||||
`Freshness: ${describeStaleness(data.staleness.recommendation, data.staleness.configChanged)}`
|
||||
)
|
||||
}
|
||||
lines.push('Install script:')
|
||||
lines.push(env.installScript ? indent(env.installScript) : ' (empty)')
|
||||
if (data.environments.length > 1) {
|
||||
lines.push(
|
||||
`Other environments (pick with --env): ${data.environments
|
||||
.filter((other) => other.id !== env.id)
|
||||
.map((other) => `${other.name} (${other.id})`)
|
||||
.join(', ')}`
|
||||
)
|
||||
}
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
function describeStaleness(recommendation: string, configChanged: boolean): string {
|
||||
switch (recommendation) {
|
||||
case 'no_build':
|
||||
return 'no active build yet'
|
||||
case 'deps_changed':
|
||||
return 'a lockfile changed since the active build; rebuild recommended'
|
||||
case 'config_changed':
|
||||
return 'the configuration changed since the active build; rebuild recommended'
|
||||
case 'image_updated':
|
||||
return 'the base image was updated since the active build; rebuild recommended'
|
||||
case 'branch_moved':
|
||||
return 'the default branch moved since the active build'
|
||||
case 'up_to_date':
|
||||
return configChanged ? 'configuration changed since the active build' : 'up to date'
|
||||
default:
|
||||
return 'unknown'
|
||||
}
|
||||
}
|
||||
|
||||
function describeBuild(build: HostedEnvironmentBuildView): string {
|
||||
const parts = [`#${build.number} ${build.status}`]
|
||||
if (build.status === 'failed') {
|
||||
parts.push(
|
||||
`${build.failedStep ? `at ${build.failedStep}` : ''}${build.error ? ` (${build.error})` : ''}`.trim()
|
||||
)
|
||||
}
|
||||
if (build.durationMs !== null) {
|
||||
parts.push(formatDuration(build.durationMs))
|
||||
}
|
||||
parts.push(new Date(build.createdAt).toISOString())
|
||||
parts.push(build.id)
|
||||
return parts.filter(Boolean).join(' ')
|
||||
}
|
||||
|
||||
export function formatBuilds(data: CloudEnvironmentBuildsData): string {
|
||||
if (data.builds.length === 0) {
|
||||
return `${data.environment.name} has no builds yet. Start one with: orca cloud env build --wait`
|
||||
}
|
||||
const outcomes = data.builds.map((build) =>
|
||||
build.status === 'failed'
|
||||
? `failed${build.failedStep ? ` at ${build.failedStep}` : ''}${build.error ? ` (${build.error})` : ''}`
|
||||
: build.status
|
||||
)
|
||||
// Column width from the widest outcome, so a long failure never shifts the columns after it.
|
||||
const width = Math.max(...outcomes.map((outcome) => outcome.length))
|
||||
const rows = data.builds.map((build, index) => {
|
||||
const outcome = outcomes[index]!
|
||||
return `${build.active ? '*' : ' '} #${String(build.number).padEnd(4)} ${outcome.padEnd(width)} ${formatDuration(build.durationMs).padEnd(8)} ${new Date(build.createdAt).toISOString()} ${build.id}`
|
||||
})
|
||||
return [`Builds of ${data.environment.name} (newest first, * = active):`, ...rows].join('\n')
|
||||
}
|
||||
|
||||
function formatDuration(ms: number | null): string {
|
||||
if (ms === null) {
|
||||
return '-'
|
||||
}
|
||||
const seconds = Math.round(ms / 1000)
|
||||
return seconds < 60
|
||||
? `${seconds}s`
|
||||
: `${Math.floor(seconds / 60)}m${String(seconds % 60).padStart(2, '0')}s`
|
||||
}
|
||||
|
||||
function indent(text: string): string {
|
||||
return text
|
||||
.split('\n')
|
||||
.map((line) => ` ${line}`)
|
||||
.join('\n')
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { CLOUD_ENV_HANDLERS } from './cloud-env'
|
||||
import type { HandlerContext } from '../dispatch'
|
||||
import { CLOUD_ENVIRONMENT_METHODS } from '../../shared/hosted-sandbox/hosted-environment-cli-contract'
|
||||
|
||||
const ENV = { id: 'env_03b89d4bf1c74787a7ce', name: 'Web' }
|
||||
const build = (status: string, extra: Record<string, unknown> = {}) => ({
|
||||
id: 'bld_0123456789abcdef',
|
||||
number: 7,
|
||||
status,
|
||||
active: false,
|
||||
durationMs: 61_000,
|
||||
failedStep: null,
|
||||
error: null,
|
||||
...extra
|
||||
})
|
||||
|
||||
function context(
|
||||
responder: (method: string, params: Record<string, unknown>) => unknown,
|
||||
flags: Record<string, string | boolean> = {}
|
||||
): HandlerContext {
|
||||
return {
|
||||
flags: new Map(Object.entries(flags)),
|
||||
cwd: '/tmp',
|
||||
json: flags.json === true,
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the cloud env handlers only use client.call
|
||||
client: {
|
||||
call: vi.fn(async (method: string, params: Record<string, unknown>) => ({
|
||||
id: '1',
|
||||
ok: true,
|
||||
result: responder(method, params),
|
||||
_meta: { runtimeId: 'r' }
|
||||
}))
|
||||
} as unknown as HandlerContext['client']
|
||||
}
|
||||
}
|
||||
|
||||
let stdout: string
|
||||
let stderr: string
|
||||
beforeEach(() => {
|
||||
stdout = ''
|
||||
stderr = ''
|
||||
process.exitCode = undefined
|
||||
vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => {
|
||||
stdout += String(chunk)
|
||||
return true
|
||||
})
|
||||
vi.spyOn(process.stderr, 'write').mockImplementation((chunk) => {
|
||||
stderr += String(chunk)
|
||||
return true
|
||||
})
|
||||
vi.spyOn(console, 'log').mockImplementation((value) => {
|
||||
stdout += `${String(value)}\n`
|
||||
})
|
||||
})
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
process.exitCode = undefined
|
||||
})
|
||||
|
||||
describe('orca cloud env build --wait', () => {
|
||||
it('streams the log and exits non-zero with the failed step', async () => {
|
||||
const polls = [
|
||||
{
|
||||
chunks: [{ stream: 'stdout', data: '::orca-step:: install\n' }],
|
||||
done: false,
|
||||
build: build('running')
|
||||
},
|
||||
{
|
||||
chunks: [{ stream: 'stderr', data: 'npm ERR! missing\n' }],
|
||||
done: true,
|
||||
build: build('failed', { failedStep: 'install', error: 'install_failed' })
|
||||
}
|
||||
]
|
||||
let poll = 0
|
||||
const ctx = context(
|
||||
(method) => {
|
||||
if (method === CLOUD_ENVIRONMENT_METHODS.startBuild) {
|
||||
return { ok: true, data: { environment: ENV, build: build('running') } }
|
||||
}
|
||||
const next = polls[poll++]
|
||||
return {
|
||||
ok: true,
|
||||
data: { environment: ENV, cursor: poll, truncated: false, error: null, ...next }
|
||||
}
|
||||
},
|
||||
{ wait: true }
|
||||
)
|
||||
await CLOUD_ENV_HANDLERS['cloud env build'](ctx)
|
||||
expect(stdout).toContain('::orca-step:: install')
|
||||
expect(stderr).toContain('npm ERR! missing')
|
||||
expect(stderr).toContain('Build #7 failed at the install step: install_failed.')
|
||||
expect(process.exitCode).toBe(1)
|
||||
})
|
||||
|
||||
it('surfaces control-plane refusals as errors', async () => {
|
||||
const ctx = context(() => ({
|
||||
ok: false,
|
||||
error: {
|
||||
code: 'conflict',
|
||||
reason: 'build_in_progress',
|
||||
message: 'A build is already running.'
|
||||
}
|
||||
}))
|
||||
await expect(CLOUD_ENV_HANDLERS['cloud env build'](ctx)).rejects.toMatchObject({
|
||||
code: 'cloud_conflict',
|
||||
message: 'A build is already running.'
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,289 @@
|
||||
// `orca cloud env ...`: lets a local agent read the org environment, replace its install script and
|
||||
// run builds. The desktop app holds the Orca Cloud session, so every call goes through its runtime.
|
||||
import { readFile, stat } from 'node:fs/promises'
|
||||
import { resolve } from 'node:path'
|
||||
import type { CommandHandler, HandlerContext } from '../dispatch'
|
||||
import { RuntimeClientError } from '../runtime-client'
|
||||
import {
|
||||
CLOUD_ENVIRONMENT_METHODS,
|
||||
type CloudEnvironmentBuildsData,
|
||||
type CloudEnvironmentFollowData,
|
||||
type CloudEnvironmentRpcResult,
|
||||
type CloudEnvironmentSetInstallScriptData,
|
||||
type CloudEnvironmentShowData,
|
||||
type CloudEnvironmentStartBuildData
|
||||
} from '../../shared/hosted-sandbox/hosted-environment-cli-contract'
|
||||
import { HOSTED_ENVIRONMENT_MAX_INSTALL_SCRIPT_CHARS } from '../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
import { formatBuilds, formatOutcome, formatShow } from './cloud-env-format'
|
||||
|
||||
const FOLLOW_WAIT_MS = 10_000
|
||||
const FOLLOW_CALL_TIMEOUT_MS = 30_000
|
||||
// `--wait --json` reports this much of the end of the log, enough to see why a build failed.
|
||||
const JSON_LOG_TAIL_CHARS = 16_000
|
||||
// A transient desktop hiccup should not abandon a 10-minute build.
|
||||
const MAX_FOLLOW_FAILURES = 5
|
||||
|
||||
type Client = Pick<HandlerContext['client'], 'call'>
|
||||
|
||||
async function callCloud<T>(
|
||||
client: Client,
|
||||
method: string,
|
||||
params: Record<string, unknown>,
|
||||
timeoutMs?: number
|
||||
): Promise<T> {
|
||||
let response
|
||||
try {
|
||||
response = await client.call<CloudEnvironmentRpcResult<T>>(
|
||||
method,
|
||||
params,
|
||||
timeoutMs ? { timeoutMs } : undefined
|
||||
)
|
||||
} catch (error) {
|
||||
if (error instanceof RuntimeClientError && error.code === 'method_not_found') {
|
||||
throw new RuntimeClientError(
|
||||
'incompatible_runtime',
|
||||
'This Orca app does not support Orca Cloud environments yet. Update Orca and try again.'
|
||||
)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
const result = response.result
|
||||
if (!result.ok) {
|
||||
throw new RuntimeClientError(
|
||||
`cloud_${result.error.code}`,
|
||||
result.error.message,
|
||||
result.error.reason ? { reason: result.error.reason } : undefined
|
||||
)
|
||||
}
|
||||
return result.data
|
||||
}
|
||||
|
||||
function envParam(flags: HandlerContext['flags']): { env?: string } {
|
||||
const env = flags.get('env')
|
||||
return typeof env === 'string' && env.trim() ? { env: env.trim() } : {}
|
||||
}
|
||||
|
||||
export const CLOUD_ENV_HANDLERS: Record<string, CommandHandler> = {
|
||||
'cloud env show': async ({ flags, client, json }) => {
|
||||
const data = await callCloud<CloudEnvironmentShowData>(client, CLOUD_ENVIRONMENT_METHODS.show, {
|
||||
...envParam(flags),
|
||||
...(flags.get('refresh') === true ? { refresh: true } : {})
|
||||
})
|
||||
console.log(json ? JSON.stringify(data, null, 2) : formatShow(data))
|
||||
},
|
||||
|
||||
'cloud env set-install-script': async ({ flags, client, cwd, json }) => {
|
||||
const file = flags.get('file')
|
||||
if (typeof file !== 'string' || !file) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
'Pass the script with --file <path> (or --file - for stdin).'
|
||||
)
|
||||
}
|
||||
const script = file === '-' ? await readStdin() : await readScriptFile(resolve(cwd, file))
|
||||
if (script.length > HOSTED_ENVIRONMENT_MAX_INSTALL_SCRIPT_CHARS) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
`The install script is ${script.length} characters; the limit is ${HOSTED_ENVIRONMENT_MAX_INSTALL_SCRIPT_CHARS}.`
|
||||
)
|
||||
}
|
||||
const data = await callCloud<CloudEnvironmentSetInstallScriptData>(
|
||||
client,
|
||||
CLOUD_ENVIRONMENT_METHODS.setInstallScript,
|
||||
{ ...envParam(flags), script }
|
||||
)
|
||||
if (json) {
|
||||
console.log(JSON.stringify(data, null, 2))
|
||||
return
|
||||
}
|
||||
const flag = flags.get('env') ? ` --env ${data.environment.id}` : ''
|
||||
console.log(
|
||||
`Saved the install script for ${data.environment.name} (${script.length} chars, revision ${data.environment.revision}).\nBuild it with: orca cloud env build${flag} --wait`
|
||||
)
|
||||
},
|
||||
|
||||
'cloud env build': async ({ flags, client, json }) => {
|
||||
const started = await callCloud<CloudEnvironmentStartBuildData>(
|
||||
client,
|
||||
CLOUD_ENVIRONMENT_METHODS.startBuild,
|
||||
envParam(flags)
|
||||
)
|
||||
if (flags.get('wait') !== true) {
|
||||
console.log(
|
||||
json
|
||||
? JSON.stringify(started, null, 2)
|
||||
: `Started build #${started.build.number} (${started.build.id}) for ${started.environment.name}.\nFollow it with: orca cloud env logs ${started.build.number}${flags.get('env') ? ` --env ${started.environment.id}` : ''}`
|
||||
)
|
||||
return
|
||||
}
|
||||
if (!json) {
|
||||
process.stderr.write(
|
||||
`Started build #${started.build.number} (${started.build.id}) for ${started.environment.name}. Streaming its log...\n`
|
||||
)
|
||||
}
|
||||
const final = await followBuild(client, started.environment.id, started.build.id, { json })
|
||||
reportFinal(final, json)
|
||||
},
|
||||
|
||||
'cloud env builds': async ({ flags, client, json }) => {
|
||||
const data = await callCloud<CloudEnvironmentBuildsData>(
|
||||
client,
|
||||
CLOUD_ENVIRONMENT_METHODS.builds,
|
||||
envParam(flags)
|
||||
)
|
||||
console.log(json ? JSON.stringify(data, null, 2) : formatBuilds(data))
|
||||
},
|
||||
|
||||
'cloud env logs': async ({ flags, client, json }) => {
|
||||
const build = flags.get('build')
|
||||
if (typeof build !== 'string' || !build) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
'Pass a build number or id: orca cloud env logs <build>.'
|
||||
)
|
||||
}
|
||||
// Resolve the number to an id once, so polls skip the lookups.
|
||||
const first = await callCloud<CloudEnvironmentFollowData>(
|
||||
client,
|
||||
CLOUD_ENVIRONMENT_METHODS.followBuild,
|
||||
{ ...envParam(flags), build, cursor: 0, waitMs: 0 },
|
||||
FOLLOW_CALL_TIMEOUT_MS
|
||||
)
|
||||
// Before the stream's first event there is no build view yet; the loop picks it up.
|
||||
const final = await followBuild(client, first.environment.id, first.build?.id ?? build, {
|
||||
json,
|
||||
initial: first
|
||||
})
|
||||
reportFinal(final, json, { quietSuccess: true })
|
||||
}
|
||||
}
|
||||
|
||||
type FollowOutcome = {
|
||||
data: CloudEnvironmentFollowData
|
||||
logTail: string
|
||||
}
|
||||
|
||||
async function followBuild(
|
||||
client: Client,
|
||||
envId: string,
|
||||
build: string,
|
||||
options: { json: boolean; initial?: CloudEnvironmentFollowData }
|
||||
): Promise<FollowOutcome> {
|
||||
let tail = ''
|
||||
const take = (data: CloudEnvironmentFollowData): void => {
|
||||
if (data.truncated && !options.json) {
|
||||
process.stderr.write(
|
||||
'[orca] some log output was lost while following (dropped from the buffer, or the log restarted and lines may repeat)\n'
|
||||
)
|
||||
}
|
||||
for (const chunk of data.chunks) {
|
||||
if (options.json) {
|
||||
tail = (tail + chunk.data).slice(-JSON_LOG_TAIL_CHARS)
|
||||
} else if (chunk.stream === 'stderr') {
|
||||
process.stderr.write(chunk.data)
|
||||
} else {
|
||||
process.stdout.write(chunk.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
let data = options.initial
|
||||
if (data) {
|
||||
take(data)
|
||||
}
|
||||
let cursor = data?.cursor ?? 0
|
||||
let failures = 0
|
||||
while (!data?.done) {
|
||||
try {
|
||||
data = await callCloud<CloudEnvironmentFollowData>(
|
||||
client,
|
||||
CLOUD_ENVIRONMENT_METHODS.followBuild,
|
||||
{ env: envId, build: data?.build?.id ?? build, cursor, waitMs: FOLLOW_WAIT_MS },
|
||||
FOLLOW_CALL_TIMEOUT_MS
|
||||
)
|
||||
failures = 0
|
||||
} catch (error) {
|
||||
// Cloud-side refusals (not found, signed out) are final; transport hiccups get retried.
|
||||
if (
|
||||
++failures > MAX_FOLLOW_FAILURES ||
|
||||
(error instanceof RuntimeClientError && error.code.startsWith('cloud_'))
|
||||
) {
|
||||
throw error
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 2_000 * failures))
|
||||
continue
|
||||
}
|
||||
take(data)
|
||||
cursor = data.cursor
|
||||
}
|
||||
return { data, logTail: tail }
|
||||
}
|
||||
|
||||
function reportFinal(
|
||||
outcome: FollowOutcome,
|
||||
json: boolean,
|
||||
options: { quietSuccess?: boolean } = {}
|
||||
): void {
|
||||
const { data } = outcome
|
||||
const build = data.build
|
||||
const succeeded = build?.status === 'succeeded'
|
||||
if (json) {
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
environment: data.environment,
|
||||
build,
|
||||
ok: succeeded,
|
||||
...(data.error ? { followError: data.error } : {}),
|
||||
logTail: outcome.logTail
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
)
|
||||
} else if (data.error) {
|
||||
process.stderr.write(`\n[orca] ${data.error}\n`)
|
||||
} else if (build && !(succeeded && options.quietSuccess)) {
|
||||
process.stderr.write(`\n${formatOutcome(build)}\n`)
|
||||
}
|
||||
// `logs` of a finished build succeeds whatever the build's outcome; `build --wait` reports it.
|
||||
if (data.error || (!options.quietSuccess && !succeeded)) {
|
||||
process.exitCode = 1
|
||||
}
|
||||
}
|
||||
|
||||
async function readScriptFile(path: string): Promise<string> {
|
||||
const info = await stat(path).catch(() => null)
|
||||
if (!info?.isFile()) {
|
||||
throw new RuntimeClientError('invalid_argument', `No file at ${path}.`)
|
||||
}
|
||||
// Characters, not bytes, are limited; 4 bytes per char bounds the read.
|
||||
if (info.size > HOSTED_ENVIRONMENT_MAX_INSTALL_SCRIPT_CHARS * 4) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
`${path} is too large for an install script (limit ${HOSTED_ENVIRONMENT_MAX_INSTALL_SCRIPT_CHARS} characters).`
|
||||
)
|
||||
}
|
||||
return readFile(path, 'utf8')
|
||||
}
|
||||
|
||||
async function readStdin(
|
||||
stdin: AsyncIterable<unknown> & { isTTY?: boolean } = process.stdin
|
||||
): Promise<string> {
|
||||
if (stdin.isTTY) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
'Pipe the script on stdin when using --file -.'
|
||||
)
|
||||
}
|
||||
const chunks: Buffer[] = []
|
||||
let bytes = 0
|
||||
for await (const chunk of stdin) {
|
||||
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))
|
||||
bytes += buffer.length
|
||||
if (bytes > HOSTED_ENVIRONMENT_MAX_INSTALL_SCRIPT_CHARS * 4) {
|
||||
throw new RuntimeClientError('invalid_argument', 'The install script on stdin is too large.')
|
||||
}
|
||||
chunks.push(buffer)
|
||||
}
|
||||
return Buffer.concat(chunks).toString('utf8')
|
||||
}
|
||||
@@ -7,6 +7,10 @@ import {
|
||||
type PairingCreateOfferParams,
|
||||
type PairingCreateOfferResult
|
||||
} from '../../shared/remote-runtime/pairing/pairing-create-offer-contract'
|
||||
import {
|
||||
TURN_SENDER_METHOD,
|
||||
type TurnSenderResult
|
||||
} from '../../shared/remote-runtime/pairing/turn-sender-contract'
|
||||
import {
|
||||
PAIRING_REVOKE_DEVICE_METHOD,
|
||||
type PairingRevokeDeviceResult
|
||||
@@ -76,5 +80,13 @@ export const PAIR_HANDLERS: Record<string, CommandHandler> = {
|
||||
if (!response.result.available) {
|
||||
process.exitCode = 1
|
||||
}
|
||||
},
|
||||
sender: async ({ client, json }) => {
|
||||
const response = await client.call<TurnSenderResult>(TURN_SENDER_METHOD, undefined)
|
||||
printResult(response, json, (result) =>
|
||||
result.sender
|
||||
? `Last message from device ${result.sender.deviceId}.`
|
||||
: 'No paired device has messaged this agent.'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,11 @@ export const ROOT_HELP_TEXT_PRIMARY = [
|
||||
'Environment Recipes:',
|
||||
' vm recipe doctor Validate a per-workspace environment recipe',
|
||||
' vm env doctor Check that this environment can run agents',
|
||||
' cloud env show Show the Orca Cloud environment and its builds',
|
||||
' cloud env set-install-script Replace the Orca Cloud install script from a file',
|
||||
' cloud env build Build the Orca Cloud environment (--wait streams the log)',
|
||||
' cloud env builds List recent Orca Cloud environment builds',
|
||||
' cloud env logs Print or follow an Orca Cloud build log',
|
||||
'',
|
||||
'Automations:',
|
||||
' automations list List scheduled Orca automations',
|
||||
|
||||
@@ -9,7 +9,7 @@ export const SERVE_PAIRING_HANDLER_GROUPS: readonly HandlerGroup[] = [
|
||||
},
|
||||
{
|
||||
name: 'pair',
|
||||
keys: ['pair'],
|
||||
keys: ['pair', 'sender'],
|
||||
load: async () => (await import('./handlers/pair.js')).PAIR_HANDLERS
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import type { CommandSpec } from '../args'
|
||||
import { GLOBAL_FLAGS } from '../args'
|
||||
|
||||
const ENV_NOTE =
|
||||
'With several environments in the org, pick one with --env <name|id>; without it the first one is used.'
|
||||
|
||||
export const CLOUD_ENV_COMMAND_SPECS: CommandSpec[] = [
|
||||
{
|
||||
path: ['cloud', 'env', 'show'],
|
||||
summary: 'Show the Orca Cloud environment: repos, install script, builds and freshness',
|
||||
usage: 'orca cloud env show [--env <name|id>] [--refresh] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, 'env', 'refresh'],
|
||||
notes: [
|
||||
'Needs the Orca desktop app signed in to Orca Cloud; the CLI asks it.',
|
||||
ENV_NOTE,
|
||||
'--refresh re-checks the repos on GitHub instead of using the minute-old answer.'
|
||||
],
|
||||
examples: ['orca cloud env show', 'orca cloud env show --env web --json']
|
||||
},
|
||||
{
|
||||
path: ['cloud', 'env', 'set-install-script'],
|
||||
summary: 'Replace the environment install script with the contents of a file',
|
||||
usage: 'orca cloud env set-install-script --file <path|-> [--env <name|id>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, 'env', 'file'],
|
||||
notes: [
|
||||
'The script is bash; each build runs it once with `set -euo pipefail` in the first repo, with passwordless sudo. The log’s first line names the base image and its package manager (currently Ubuntu with apt-get).',
|
||||
'Only the install script changes. Repos, secrets and machine size are edited in Settings by org owners and admins.',
|
||||
'Fails with "Someone else saved changes; reload." when the environment changed since it was read.',
|
||||
ENV_NOTE
|
||||
],
|
||||
examples: [
|
||||
'orca cloud env set-install-script --file /tmp/install.sh',
|
||||
'cat install.sh | orca cloud env set-install-script --file -'
|
||||
]
|
||||
},
|
||||
{
|
||||
path: ['cloud', 'env', 'build'],
|
||||
summary: 'Start a build of the environment, optionally waiting for it and streaming its log',
|
||||
usage: 'orca cloud env build [--wait] [--env <name|id>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, 'env', 'wait'],
|
||||
notes: [
|
||||
'A build boots a VM, clones the repos, runs the install script and snapshots the result. A succeeded build becomes the active one new workspaces start from.',
|
||||
'--wait streams the log to stdout/stderr and exits 1 unless the build succeeds, printing the failed step and error. With --json it prints the final build and the end of the log instead.',
|
||||
'Interrupting --wait leaves the build running; follow it again with `orca cloud env logs <number>`.',
|
||||
ENV_NOTE
|
||||
],
|
||||
examples: ['orca cloud env build --wait', 'orca cloud env build --wait --json']
|
||||
},
|
||||
{
|
||||
path: ['cloud', 'env', 'builds'],
|
||||
summary: 'List the environment’s recent builds, newest first',
|
||||
usage: 'orca cloud env builds [--env <name|id>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, 'env'],
|
||||
notes: [ENV_NOTE],
|
||||
examples: ['orca cloud env builds', 'orca cloud env builds --json']
|
||||
},
|
||||
{
|
||||
path: ['cloud', 'env', 'logs'],
|
||||
summary: 'Print a build’s log, following it until the build finishes',
|
||||
usage: 'orca cloud env logs <build-number|build-id> [--env <name|id>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, 'env', 'build'],
|
||||
positionalArgs: ['build'],
|
||||
notes: [
|
||||
'Lines starting with `::orca-step::` mark build steps; `::orca-error::` lines are Orca’s explanation of a failure.',
|
||||
ENV_NOTE
|
||||
],
|
||||
examples: ['orca cloud env logs 12', 'orca cloud env logs bld_0123456789abcdef']
|
||||
}
|
||||
]
|
||||
@@ -24,6 +24,7 @@ import { JIRA_COMMAND_SPECS } from './jira'
|
||||
import { LINEAR_COMMAND_SPECS } from './linear'
|
||||
import { VM_COMMAND_SPECS } from './vm'
|
||||
import { VM_ENV_COMMAND_SPECS } from './vm-env'
|
||||
import { CLOUD_ENV_COMMAND_SPECS } from './cloud-env'
|
||||
import { CLOUD_SSH_COMMAND_SPECS } from './cloud-ssh'
|
||||
import { SKILL_COMMAND_SPECS } from './skills'
|
||||
import { ARTIFACT_COMMAND_SPECS } from './artifacts'
|
||||
@@ -62,6 +63,7 @@ export const COMMAND_SPECS: CommandSpec[] = [
|
||||
...LINEAR_COMMAND_SPECS,
|
||||
...VM_COMMAND_SPECS,
|
||||
...VM_ENV_COMMAND_SPECS,
|
||||
...CLOUD_ENV_COMMAND_SPECS,
|
||||
...CLOUD_SSH_COMMAND_SPECS,
|
||||
...EMULATOR_COMMAND_SPECS,
|
||||
...SKILL_COMMAND_SPECS,
|
||||
|
||||
@@ -34,5 +34,16 @@ export const PAIR_COMMAND_SPECS: CommandSpec[] = [
|
||||
'orca pair --address 100.64.1.20',
|
||||
'orca pair --mobile --rotate --json'
|
||||
]
|
||||
},
|
||||
{
|
||||
path: ['sender'],
|
||||
summary: 'Show which paired device sent the latest message to the agent running this command',
|
||||
usage: 'orca sender [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS],
|
||||
notes: [
|
||||
'Run it from inside an agent terminal or chat: Orca identifies the caller from its own launch evidence, not from arguments, so an agent only ever learns who drove it.',
|
||||
'Prints nothing useful outside an agent Orca launched. Shared Orca Cloud workspaces use it to run git as the teammate who asked for the work.'
|
||||
],
|
||||
examples: ['orca sender --json']
|
||||
}
|
||||
]
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { CloudApiError, classifyCloudApiResponse, createCloudApiClient } from './cloud-api-client'
|
||||
|
||||
const KEY = {
|
||||
id: 'key_1',
|
||||
name: 'CI',
|
||||
prefix: 'orca_sk_Ab3x',
|
||||
scopes: ['workspaces:read', 'future:scope'],
|
||||
createdBy: 'u1',
|
||||
createdAt: 1,
|
||||
lastUsedAt: null,
|
||||
expiresAt: null
|
||||
}
|
||||
|
||||
function respond(status: number, body: unknown, headers: Record<string, string> = {}) {
|
||||
return new Response(body === undefined ? null : JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'content-type': 'application/json', ...headers }
|
||||
})
|
||||
}
|
||||
|
||||
function client(fetchImpl: typeof fetch, getAccessToken = async () => 'tok_secret') {
|
||||
return createCloudApiClient({
|
||||
controlPlaneUrl: 'https://cp.example.dev/',
|
||||
getAccessToken,
|
||||
fetchImpl
|
||||
})
|
||||
}
|
||||
|
||||
async function cloudApiErrorOf(promise: Promise<unknown>): Promise<CloudApiError> {
|
||||
const caught = await promise.then(
|
||||
() => null,
|
||||
(error: unknown) => error
|
||||
)
|
||||
if (!(caught instanceof CloudApiError)) {
|
||||
throw new Error(`expected a CloudApiError, got ${String(caught)}`)
|
||||
}
|
||||
return caught
|
||||
}
|
||||
|
||||
async function codeOf(promise: Promise<unknown>): Promise<string> {
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
if (error instanceof CloudApiError) {
|
||||
return error.code
|
||||
}
|
||||
throw error
|
||||
}
|
||||
return 'ok'
|
||||
}
|
||||
|
||||
describe('createCloudApiClient', () => {
|
||||
it('sends the bearer token, JSON body and query, and validates the answer', async () => {
|
||||
const fetchImpl = vi.fn<typeof fetch>(async () =>
|
||||
respond(200, { keys: [KEY], availableScopes: ['audit:read'] })
|
||||
)
|
||||
const keys = await client(fetchImpl).listApiKeys()
|
||||
expect(keys.keys[0]?.prefix).toBe('orca_sk_Ab3x')
|
||||
// Unknown scopes from a newer server are dropped, not fatal.
|
||||
expect(keys.keys[0]?.scopes).toEqual(['workspaces:read'])
|
||||
const [url, init] = fetchImpl.mock.calls[0] ?? []
|
||||
expect(url).toBe('https://cp.example.dev/v1/api-keys')
|
||||
expect(new Headers(init?.headers).get('authorization')).toBe('Bearer tok_secret')
|
||||
expect(init?.redirect).toBe('error')
|
||||
|
||||
const created = vi.fn<typeof fetch>(async () =>
|
||||
respond(201, { key: KEY, secret: `orca_sk_${'a'.repeat(32)}` })
|
||||
)
|
||||
await client(created).createApiKey({
|
||||
name: 'CI',
|
||||
scopes: ['workspaces:read']
|
||||
})
|
||||
const [, createInit] = created.mock.calls[0] ?? []
|
||||
expect(createInit?.method).toBe('POST')
|
||||
expect(JSON.parse(String(createInit?.body))).toEqual({
|
||||
name: 'CI',
|
||||
scopes: ['workspaces:read']
|
||||
})
|
||||
|
||||
const runs = vi.fn<typeof fetch>(async () => respond(200, { runs: [], nextBefore: null }))
|
||||
await client(runs).listRoutineRuns('rt/1', { limit: 20, before: 99 })
|
||||
expect(runs.mock.calls[0]?.[0]).toBe(
|
||||
'https://cp.example.dev/v1/routines/rt%2F1/runs?limit=20&before=99'
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects a malformed answer as invalid-response', async () => {
|
||||
const fetchImpl = vi.fn<typeof fetch>(async () => respond(200, { keys: 'nope' }))
|
||||
expect(await codeOf(client(fetchImpl).listApiKeys())).toBe('invalid-response')
|
||||
const badSecret = vi.fn<typeof fetch>(async () =>
|
||||
respond(201, { key: KEY, secret: 'not-a-key' })
|
||||
)
|
||||
expect(
|
||||
await codeOf(
|
||||
client(badSecret).createApiKey({
|
||||
name: 'x',
|
||||
scopes: ['audit:read']
|
||||
})
|
||||
)
|
||||
).toBe('invalid-response')
|
||||
})
|
||||
|
||||
it('maps network and token failures without leaking the token', async () => {
|
||||
const offline = vi.fn<typeof fetch>(async () => {
|
||||
throw new TypeError('fetch failed tok_secret')
|
||||
})
|
||||
const error = await cloudApiErrorOf(client(offline).me())
|
||||
expect(error.code).toBe('network')
|
||||
expect(error.message).not.toContain('tok_secret')
|
||||
|
||||
const noToken = client(vi.fn<typeof fetch>(), async () => {
|
||||
throw new Error('signed out')
|
||||
})
|
||||
expect(await codeOf(noToken.me())).toBe('signed-out')
|
||||
})
|
||||
|
||||
it('treats a missing routines route as not-available but a missing routine as not-found', async () => {
|
||||
const missing = vi.fn<typeof fetch>(async () => respond(404, { error: 'not_found' }))
|
||||
const c = client(missing)
|
||||
expect(await codeOf(c.listRoutines())).toBe('not-available')
|
||||
expect(await codeOf(c.getRoutine('rt_1'))).toBe('not-found')
|
||||
})
|
||||
|
||||
it('carries retry-after on rate limits and accepts empty DELETE bodies', async () => {
|
||||
const limited = vi.fn<typeof fetch>(async () =>
|
||||
respond(429, { error: 'rate_limited' }, { 'retry-after': '7' })
|
||||
)
|
||||
const error = await cloudApiErrorOf(client(limited).listApiKeys())
|
||||
expect(error.code).toBe('rate-limited')
|
||||
expect(error.retryAfterSeconds).toBe(7)
|
||||
|
||||
const deleted = vi.fn<typeof fetch>(async () => new Response(null, { status: 200 }))
|
||||
await expect(client(deleted).revokeApiKey('key_1')).resolves.toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('response hardening', () => {
|
||||
it('keeps a freshly minted secret even if the key metadata drifted', async () => {
|
||||
const secret = `orca_sk_${'b'.repeat(32)}`
|
||||
const fetchImpl = vi.fn<typeof fetch>(async () => respond(201, { key: { id: 7 }, secret }))
|
||||
const created = await client(fetchImpl).createApiKey({
|
||||
name: 'x',
|
||||
scopes: ['workspaces:read']
|
||||
})
|
||||
expect(created).toEqual({ key: null, secret })
|
||||
})
|
||||
|
||||
it('drops non-https links from the server', async () => {
|
||||
const run = {
|
||||
id: 'run_1',
|
||||
routineId: 'rt_1',
|
||||
trigger: 'manual',
|
||||
triggerDetail: null,
|
||||
status: 'succeeded',
|
||||
workspaceId: null,
|
||||
result: { prUrl: 'javascript:alert(1)' },
|
||||
error: null,
|
||||
createdAt: 1,
|
||||
startedAt: 1,
|
||||
finishedAt: 2,
|
||||
durationMs: 1
|
||||
}
|
||||
const fetchImpl = vi.fn<typeof fetch>(async () => respond(202, { run }))
|
||||
// A bad link is dropped, never opened, and doesn't fail the whole response.
|
||||
const { run: parsed } = await client(fetchImpl).runRoutine('rt_1')
|
||||
expect(parsed.result?.prUrl).toBeUndefined()
|
||||
expect(parsed.status).toBe('succeeded')
|
||||
})
|
||||
})
|
||||
|
||||
describe('classifyCloudApiResponse', () => {
|
||||
it.each([
|
||||
[401, { error: 'unauthorized' }, 'signed-out'],
|
||||
[403, { error: 'forbidden', reason: 'admin_required' }, 'admin-required'],
|
||||
[403, { error: 'forbidden', reason: 'admin_scope:audit:read' }, 'admin-required'],
|
||||
[403, { error: 'insufficient_scope', reason: 'routines:write' }, 'forbidden'],
|
||||
[400, { error: 'invalid_request', reason: 'name: Too small' }, 'invalid-request'],
|
||||
[503, { error: 'not_configured' }, 'not-available'],
|
||||
[500, null, 'server'],
|
||||
[502, { error: 'workspace_unavailable' }, 'server']
|
||||
])('%i %j → %s', (status, body, code) => {
|
||||
expect(classifyCloudApiResponse(status, body).code).toBe(code)
|
||||
})
|
||||
|
||||
it('keeps the server validation reason as detail, truncated', () => {
|
||||
expect(classifyCloudApiResponse(400, { reason: 'name: Too small' }).detail).toBe(
|
||||
'name: Too small'
|
||||
)
|
||||
expect(classifyCloudApiResponse(400, { reason: 'x'.repeat(1000) }).detail).toHaveLength(200)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,217 @@
|
||||
// HTTP client for the Orca Cloud control plane's public /v1 API, used by the desktop's API keys and
|
||||
// routines settings. Pure (fetch + token source injected) so it is testable without Electron. The
|
||||
// bearer token never leaves this module: callers get validated data or a CloudApiError code.
|
||||
import { z } from 'zod'
|
||||
import {
|
||||
CloudApiApprovalSchema,
|
||||
CloudApiAuditPageSchema,
|
||||
CloudApiCreatedKeySchema,
|
||||
CloudApiGrantListSchema,
|
||||
CloudApiInstallationListSchema,
|
||||
CloudApiKeyListSchema,
|
||||
CloudApiMeSchema,
|
||||
CloudApiProjectListSchema,
|
||||
CloudApiSettingsSchema,
|
||||
RoutineCreatedSchema,
|
||||
RoutineEnvelopeSchema,
|
||||
RoutineListSchema,
|
||||
RoutineRunEnvelopeSchema,
|
||||
RoutineRunPageSchema,
|
||||
RoutineUpdatedSchema,
|
||||
RoutineWebhookSchema,
|
||||
type CloudApiErrorCode,
|
||||
type CreateCloudApiApprovalInput,
|
||||
type CreateCloudApiKeyInput,
|
||||
type RoutineInput,
|
||||
type RoutinePatch,
|
||||
type RoutineRunsQuery
|
||||
} from '../../shared/cloud-api/cloud-api-types'
|
||||
|
||||
const REQUEST_TIMEOUT_MS = 20_000
|
||||
const MAX_DETAIL_CHARS = 200
|
||||
|
||||
export class CloudApiError extends Error {
|
||||
constructor(
|
||||
readonly code: CloudApiErrorCode,
|
||||
/** Server-provided reason (e.g. a validation message), safe to show; never a secret. */
|
||||
readonly detail?: string,
|
||||
readonly retryAfterSeconds?: number
|
||||
) {
|
||||
super(detail ? `${code}: ${detail}` : code)
|
||||
this.name = 'CloudApiError'
|
||||
}
|
||||
}
|
||||
|
||||
export type CloudApiClientDeps = {
|
||||
controlPlaneUrl: string
|
||||
getAccessToken: () => Promise<string>
|
||||
fetchImpl?: typeof fetch
|
||||
/** Maps a token-source failure (signed out, auth down) to an error code. */
|
||||
classifyTokenError?: (error: unknown) => CloudApiErrorCode
|
||||
}
|
||||
|
||||
type RequestOptions = {
|
||||
body?: unknown
|
||||
query?: Record<string, string | number | undefined>
|
||||
/** A 404 on this route means the deployment predates it, not a missing record. */
|
||||
notFoundMeansUnavailable?: boolean
|
||||
}
|
||||
|
||||
function readServerError(body: unknown): { error?: string; reason?: string } {
|
||||
if (typeof body !== 'object' || body === null) {
|
||||
return {}
|
||||
}
|
||||
const pick = (value: unknown): string | undefined =>
|
||||
typeof value === 'string' ? value.slice(0, MAX_DETAIL_CHARS) : undefined
|
||||
return {
|
||||
error: pick('error' in body ? body.error : undefined),
|
||||
reason: pick('reason' in body ? body.reason : undefined)
|
||||
}
|
||||
}
|
||||
|
||||
/** Maps a non-2xx control-plane answer to a stable code the renderer can word. */
|
||||
export function classifyCloudApiResponse(
|
||||
status: number,
|
||||
body: unknown,
|
||||
options: { notFoundMeansUnavailable?: boolean } = {}
|
||||
): { code: CloudApiErrorCode; detail?: string } {
|
||||
const { error, reason } = readServerError(body)
|
||||
if (status === 401) {
|
||||
return { code: 'signed-out' }
|
||||
}
|
||||
if (status === 403) {
|
||||
if (reason === 'admin_required' || reason?.startsWith('admin_scope:')) {
|
||||
return { code: 'admin-required' }
|
||||
}
|
||||
return { code: 'forbidden', ...(reason ? { detail: reason } : {}) }
|
||||
}
|
||||
if (status === 404) {
|
||||
return { code: options.notFoundMeansUnavailable ? 'not-available' : 'not-found' }
|
||||
}
|
||||
if (status === 400 || status === 409 || status === 422) {
|
||||
return { code: 'invalid-request', ...(reason ? { detail: reason } : {}) }
|
||||
}
|
||||
if (status === 429) {
|
||||
return { code: 'rate-limited' }
|
||||
}
|
||||
if (status === 503 && error === 'not_configured') {
|
||||
return { code: 'not-available' }
|
||||
}
|
||||
return { code: 'server', detail: error ?? `HTTP ${status}` }
|
||||
}
|
||||
|
||||
export function createCloudApiClient(deps: CloudApiClientDeps) {
|
||||
const fetchImpl = deps.fetchImpl ?? fetch
|
||||
const base = `${deps.controlPlaneUrl.replace(/\/+$/, '')}/`
|
||||
|
||||
async function request<S extends z.ZodType>(
|
||||
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
|
||||
path: string,
|
||||
schema: S,
|
||||
options: RequestOptions = {}
|
||||
): Promise<z.infer<S>> {
|
||||
let token: string
|
||||
try {
|
||||
token = await deps.getAccessToken()
|
||||
} catch (error) {
|
||||
throw new CloudApiError(deps.classifyTokenError?.(error) ?? 'signed-out')
|
||||
}
|
||||
const url = new URL(path.replace(/^\//, ''), base)
|
||||
for (const [name, value] of Object.entries(options.query ?? {})) {
|
||||
if (value !== undefined) {
|
||||
url.searchParams.set(name, String(value))
|
||||
}
|
||||
}
|
||||
let response: Response
|
||||
try {
|
||||
response = await fetchImpl(url.toString(), {
|
||||
method,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
accept: 'application/json',
|
||||
...(options.body !== undefined ? { 'content-type': 'application/json' } : {})
|
||||
},
|
||||
...(options.body !== undefined ? { body: JSON.stringify(options.body) } : {}),
|
||||
redirect: 'error',
|
||||
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
||||
})
|
||||
} catch {
|
||||
throw new CloudApiError('network')
|
||||
}
|
||||
const body: unknown = await response.json().catch(() => null)
|
||||
if (!response.ok) {
|
||||
const { code, detail } = classifyCloudApiResponse(response.status, body, options)
|
||||
const retryAfter = Number(response.headers.get('retry-after'))
|
||||
throw new CloudApiError(
|
||||
code,
|
||||
detail,
|
||||
code === 'rate-limited' && Number.isFinite(retryAfter) && retryAfter > 0
|
||||
? retryAfter
|
||||
: undefined
|
||||
)
|
||||
}
|
||||
const parsed = schema.safeParse(body)
|
||||
if (!parsed.success) {
|
||||
throw new CloudApiError('invalid-response')
|
||||
}
|
||||
return parsed.data
|
||||
}
|
||||
|
||||
const routinePath = (id: string, sub = ''): string =>
|
||||
`v1/routines/${encodeURIComponent(id)}${sub}`
|
||||
|
||||
return {
|
||||
me: () => request('GET', 'v1/me', CloudApiMeSchema),
|
||||
listApiKeys: () => request('GET', 'v1/api-keys', CloudApiKeyListSchema),
|
||||
createApiKey: (input: CreateCloudApiKeyInput) =>
|
||||
request('POST', 'v1/api-keys', CloudApiCreatedKeySchema, { body: input }),
|
||||
revokeApiKey: async (id: string): Promise<void> => {
|
||||
await request('DELETE', `v1/api-keys/${encodeURIComponent(id)}`, z.unknown())
|
||||
},
|
||||
createApproval: (input: CreateCloudApiApprovalInput = {}) =>
|
||||
request('POST', 'v1/oauth-approvals', CloudApiApprovalSchema, {
|
||||
body: input,
|
||||
notFoundMeansUnavailable: true
|
||||
}),
|
||||
listGrants: () =>
|
||||
request('GET', 'v1/oauth-grants', CloudApiGrantListSchema, {
|
||||
notFoundMeansUnavailable: true
|
||||
}),
|
||||
revokeGrant: async (id: string): Promise<void> => {
|
||||
await request('DELETE', `v1/oauth-grants/${encodeURIComponent(id)}`, z.unknown())
|
||||
},
|
||||
getSettings: () => request('GET', 'v1/settings', CloudApiSettingsSchema),
|
||||
putSettings: (settings: { vmCliEnabled: boolean }) =>
|
||||
request('PUT', 'v1/settings', CloudApiSettingsSchema, { body: settings }),
|
||||
listAudit: (query: { limit?: number; before?: number } = {}) =>
|
||||
request('GET', 'v1/audit', CloudApiAuditPageSchema, { query }),
|
||||
listProjects: () => request('GET', 'v1/projects', CloudApiProjectListSchema),
|
||||
listInstallations: () =>
|
||||
request('GET', 'v1/github/installations', CloudApiInstallationListSchema, {
|
||||
notFoundMeansUnavailable: true
|
||||
}),
|
||||
listRoutines: () =>
|
||||
request('GET', 'v1/routines', RoutineListSchema, { notFoundMeansUnavailable: true }),
|
||||
getRoutine: (id: string) => request('GET', routinePath(id), RoutineEnvelopeSchema),
|
||||
createRoutine: (input: RoutineInput) =>
|
||||
request('POST', 'v1/routines', RoutineCreatedSchema, { body: input }),
|
||||
updateRoutine: (id: string, patch: RoutinePatch) =>
|
||||
request('PATCH', routinePath(id), RoutineUpdatedSchema, { body: patch }),
|
||||
deleteRoutine: async (id: string): Promise<void> => {
|
||||
await request('DELETE', routinePath(id), z.unknown())
|
||||
},
|
||||
runRoutine: (id: string) => request('POST', routinePath(id, '/run'), RoutineRunEnvelopeSchema),
|
||||
rotateRoutineWebhook: (id: string) =>
|
||||
request('POST', routinePath(id, '/rotate-webhook'), RoutineWebhookSchema),
|
||||
listRoutineRuns: (id: string, query: RoutineRunsQuery = {}) =>
|
||||
request('GET', routinePath(id, '/runs'), RoutineRunPageSchema, { query }),
|
||||
getRoutineRun: (id: string, runId: string) =>
|
||||
request(
|
||||
'GET',
|
||||
routinePath(id, `/runs/${encodeURIComponent(runId)}`),
|
||||
RoutineRunEnvelopeSchema
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export type CloudApiClient = ReturnType<typeof createCloudApiClient>
|
||||
@@ -0,0 +1,79 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const callCloudApi = vi.fn(async (run: (client: unknown) => Promise<unknown>) => ({
|
||||
ok: true,
|
||||
data: await run(fakeClient)
|
||||
}))
|
||||
const fakeClient = {
|
||||
createApiKey: vi.fn(async (input: unknown) => ({ echoed: input })),
|
||||
revokeApiKey: vi.fn(async () => undefined),
|
||||
createRoutine: vi.fn(async (input: unknown) => ({ echoed: input }))
|
||||
}
|
||||
|
||||
vi.mock('electron', () => ({ ipcMain: { handle: vi.fn(), removeHandler: vi.fn() } }))
|
||||
vi.mock('./cloud-api-service', () => ({
|
||||
callCloudApi: (run: (client: unknown) => Promise<unknown>) => callCloudApi(run),
|
||||
getCloudApiAvailability: () => ({ status: 'unconfigured' })
|
||||
}))
|
||||
|
||||
const { cloudApiHandlerFor, CLOUD_API_CHANNELS } = await import('./cloud-api-ipc')
|
||||
|
||||
describe('cloud API IPC', () => {
|
||||
beforeEach(() => {
|
||||
callCloudApi.mockClear()
|
||||
})
|
||||
|
||||
it('rejects malformed renderer input before any network call', async () => {
|
||||
const create = cloudApiHandlerFor('cloudApi:createApiKey')!
|
||||
expect(await create({ name: '', scopes: ['workspaces:read'] })).toEqual({
|
||||
ok: false,
|
||||
error: 'invalid-request'
|
||||
})
|
||||
expect(await create({ name: 'x', scopes: ['root:all'] })).toEqual({
|
||||
ok: false,
|
||||
error: 'invalid-request'
|
||||
})
|
||||
expect(await create({ name: 'x', scopes: ['audit:read'], extra: 1 })).toEqual({
|
||||
ok: false,
|
||||
error: 'invalid-request'
|
||||
})
|
||||
expect(await cloudApiHandlerFor('cloudApi:revokeApiKey')!(42)).toEqual({
|
||||
ok: false,
|
||||
error: 'invalid-request'
|
||||
})
|
||||
expect(
|
||||
await cloudApiHandlerFor('cloudApi:createRoutine')!({
|
||||
name: 'r',
|
||||
repo: { url: 'http://insecure.example/repo' },
|
||||
trigger: { kind: 'webhook' },
|
||||
agent: 'claude',
|
||||
prompt: 'p',
|
||||
postResults: 'none'
|
||||
})
|
||||
).toEqual({ ok: false, error: 'invalid-request' })
|
||||
expect(callCloudApi).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('passes validated input through to the client', async () => {
|
||||
const result = await cloudApiHandlerFor('cloudApi:createApiKey')!({
|
||||
name: ' CI ',
|
||||
scopes: ['workspaces:read'],
|
||||
expiresInDays: 30
|
||||
})
|
||||
expect(result).toEqual({
|
||||
ok: true,
|
||||
data: { echoed: { name: 'CI', scopes: ['workspaces:read'], expiresInDays: 30 } }
|
||||
})
|
||||
})
|
||||
|
||||
it('registers every channel the preload bridge uses', () => {
|
||||
expect(CLOUD_API_CHANNELS).toEqual(
|
||||
expect.arrayContaining([
|
||||
'cloudApi:availability',
|
||||
'cloudApi:createApproval',
|
||||
'cloudApi:listRoutineRuns',
|
||||
'cloudApi:rotateRoutineWebhook'
|
||||
])
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,101 @@
|
||||
import { ipcMain } from 'electron'
|
||||
import { z } from 'zod'
|
||||
import {
|
||||
CreateCloudApiApprovalInputSchema,
|
||||
CreateCloudApiKeyInputSchema,
|
||||
RoutineInputSchema,
|
||||
RoutinePatchSchema,
|
||||
RoutineRunsQuerySchema,
|
||||
type CloudApiResult
|
||||
} from '../../shared/cloud-api/cloud-api-types'
|
||||
import type { CloudApiClient } from './cloud-api-client'
|
||||
import {
|
||||
callCloudApi,
|
||||
findLocalWorkspaceForSandbox,
|
||||
getCloudApiAvailability
|
||||
} from './cloud-api-service'
|
||||
|
||||
const Id = z.string().min(1).max(128)
|
||||
const AuditQuery = z
|
||||
.object({
|
||||
limit: z.number().int().min(1).max(200).optional(),
|
||||
before: z.number().int().nonnegative().optional()
|
||||
})
|
||||
.strict()
|
||||
const SettingsInput = z.object({ vmCliEnabled: z.boolean() }).strict()
|
||||
|
||||
type Handler = (...args: unknown[]) => Promise<CloudApiResult<unknown>>
|
||||
|
||||
/** Validates renderer input before it reaches the network; bad input never leaves main. */
|
||||
function call<A extends unknown[]>(
|
||||
schemas: { [K in keyof A]: z.ZodType<A[K]> },
|
||||
run: (client: CloudApiClient, ...args: A) => Promise<unknown>
|
||||
): Handler {
|
||||
return async (...raw) => {
|
||||
const parsed: unknown[] = []
|
||||
for (const [index, schema] of schemas.entries()) {
|
||||
const result = schema.safeParse(raw[index])
|
||||
if (!result.success) {
|
||||
return { ok: false, error: 'invalid-request' }
|
||||
}
|
||||
parsed.push(result.data)
|
||||
}
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: parsed[i] is schemas[i]'s output and schemas is typed per A[i]; TS cannot prove a tuple built in a loop.
|
||||
return callCloudApi((client) => run(client, ...(parsed as A)))
|
||||
}
|
||||
}
|
||||
|
||||
const HANDLERS: Record<string, Handler> = {
|
||||
'cloudApi:me': call([], (c) => c.me()),
|
||||
'cloudApi:listApiKeys': call([], (c) => c.listApiKeys()),
|
||||
'cloudApi:createApiKey': call([CreateCloudApiKeyInputSchema], (c, input) =>
|
||||
c.createApiKey(input)
|
||||
),
|
||||
'cloudApi:revokeApiKey': call([Id], (c, id) => c.revokeApiKey(id)),
|
||||
'cloudApi:createApproval': call([CreateCloudApiApprovalInputSchema.optional()], (c, input) =>
|
||||
c.createApproval(input ?? {})
|
||||
),
|
||||
'cloudApi:listGrants': call([], (c) => c.listGrants()),
|
||||
'cloudApi:revokeGrant': call([Id], (c, id) => c.revokeGrant(id)),
|
||||
'cloudApi:getSettings': call([], (c) => c.getSettings()),
|
||||
'cloudApi:putSettings': call([SettingsInput], (c, input) => c.putSettings(input)),
|
||||
'cloudApi:listAudit': call([AuditQuery.optional()], (c, q) => c.listAudit(q ?? {})),
|
||||
'cloudApi:listProjects': call([], (c) => c.listProjects()),
|
||||
'cloudApi:listInstallations': call([], (c) => c.listInstallations()),
|
||||
'cloudApi:listRoutines': call([], (c) => c.listRoutines()),
|
||||
'cloudApi:createRoutine': call([RoutineInputSchema], (c, input) => c.createRoutine(input)),
|
||||
'cloudApi:updateRoutine': call([Id, RoutinePatchSchema], (c, id, patch) =>
|
||||
c.updateRoutine(id, patch)
|
||||
),
|
||||
'cloudApi:deleteRoutine': call([Id], (c, id) => c.deleteRoutine(id)),
|
||||
'cloudApi:runRoutine': call([Id], (c, id) => c.runRoutine(id)),
|
||||
'cloudApi:rotateRoutineWebhook': call([Id], (c, id) => c.rotateRoutineWebhook(id)),
|
||||
'cloudApi:listRoutineRuns': call([Id, RoutineRunsQuerySchema.optional()], (c, id, q) =>
|
||||
c.listRoutineRuns(id, q ?? {})
|
||||
)
|
||||
}
|
||||
|
||||
export const CLOUD_API_CHANNELS = [
|
||||
'cloudApi:availability',
|
||||
'cloudApi:localWorkspaceFor',
|
||||
...Object.keys(HANDLERS)
|
||||
]
|
||||
|
||||
/** Exposed for tests: the validated handler behind a channel. */
|
||||
export function cloudApiHandlerFor(channel: string): Handler | undefined {
|
||||
return HANDLERS[channel]
|
||||
}
|
||||
|
||||
export function registerCloudApiHandlers(): void {
|
||||
for (const channel of CLOUD_API_CHANNELS) {
|
||||
ipcMain.removeHandler(channel)
|
||||
}
|
||||
ipcMain.handle('cloudApi:availability', () => getCloudApiAvailability())
|
||||
ipcMain.handle('cloudApi:localWorkspaceFor', (_event, sandboxId: unknown) => {
|
||||
const parsed = Id.safeParse(sandboxId)
|
||||
return parsed.success ? findLocalWorkspaceForSandbox(parsed.data) : null
|
||||
})
|
||||
for (const [channel, handler] of Object.entries(HANDLERS)) {
|
||||
ipcMain.handle(channel, (_event, ...args: unknown[]) => handler(...args))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import { app } from 'electron'
|
||||
// Orca Cloud API access for the desktop settings. Uses the same hosted control plane + session
|
||||
// token as Orca Cloud sandboxes, so it is only on in builds that configure ORCA_CLOUD_SANDBOX_URL.
|
||||
// Every call resolves to a CloudApiResult value; nothing throws across IPC and no token is returned.
|
||||
import type {
|
||||
CloudApiAvailability,
|
||||
CloudApiErrorCode,
|
||||
CloudApiResult
|
||||
} from '../../shared/cloud-api/cloud-api-types'
|
||||
import { withCloudBuildDefaults } from '../cloud-build-defaults'
|
||||
import { EnvironmentProviderError } from '../environment-providers/environment-provider-driver'
|
||||
import { getHostedSandboxConfig } from '../hosted-sandbox/hosted-sandbox-config'
|
||||
import { findCloudRuntime } from '../hosted-sandbox/hosted-workspace-runtime'
|
||||
import { listEphemeralVmRuntimes } from '../../shared/ephemeral-vm-runtime-store'
|
||||
import { artifactCloudIdentity } from '../orca-profiles/profile-artifact-cloud-cleanup'
|
||||
import { ensureActiveOrcaProfile } from '../orca-profiles/profile-index-store'
|
||||
import { CloudApiError, createCloudApiClient, type CloudApiClient } from './cloud-api-client'
|
||||
|
||||
let cached: { key: string; client: CloudApiClient; controlPlaneUrl: string } | null = null
|
||||
|
||||
export function classifyTokenError(error: unknown): CloudApiErrorCode {
|
||||
if (error instanceof EnvironmentProviderError) {
|
||||
return error.code === 'unauthorized' ? 'signed-out' : 'network'
|
||||
}
|
||||
return 'signed-out'
|
||||
}
|
||||
|
||||
function resolveClient(): { client: CloudApiClient; controlPlaneUrl: string } | null {
|
||||
const userDataPath = app.getPath('userData')
|
||||
// Token caches are per profile *and* signed-in identity (user, cloud profile, active team):
|
||||
// switching any of them must never reuse the previous account's token.
|
||||
const profile = ensureActiveOrcaProfile(userDataPath).profile
|
||||
const profileKey = `${profile.id}:${artifactCloudIdentity(profile.cloud)}`
|
||||
if (cached?.key === profileKey) {
|
||||
return cached
|
||||
}
|
||||
const config = getHostedSandboxConfig(
|
||||
userDataPath,
|
||||
withCloudBuildDefaults(process.env),
|
||||
app.isPackaged
|
||||
)
|
||||
if (!config) {
|
||||
cached = null
|
||||
return null
|
||||
}
|
||||
cached = {
|
||||
key: profileKey,
|
||||
controlPlaneUrl: config.controlPlaneUrl,
|
||||
client: createCloudApiClient({
|
||||
controlPlaneUrl: config.controlPlaneUrl,
|
||||
getAccessToken: () => config.getAccessToken(),
|
||||
classifyTokenError
|
||||
})
|
||||
}
|
||||
return cached
|
||||
}
|
||||
|
||||
export function getCloudApiAvailability(): CloudApiAvailability {
|
||||
const resolved = resolveClient()
|
||||
return resolved
|
||||
? { status: 'ready', controlPlaneUrl: resolved.controlPlaneUrl }
|
||||
: { status: 'unconfigured' }
|
||||
}
|
||||
|
||||
export async function callCloudApi<T>(
|
||||
run: (client: CloudApiClient) => Promise<T>
|
||||
): Promise<CloudApiResult<T>> {
|
||||
const resolved = resolveClient()
|
||||
if (!resolved) {
|
||||
return { ok: false, error: 'unconfigured' }
|
||||
}
|
||||
try {
|
||||
return { ok: true, data: await run(resolved.client) }
|
||||
} catch (error) {
|
||||
if (error instanceof CloudApiError) {
|
||||
return {
|
||||
ok: false,
|
||||
error: error.code,
|
||||
...(error.detail ? { detail: error.detail } : {}),
|
||||
...(error.retryAfterSeconds ? { retryAfterSeconds: error.retryAfterSeconds } : {})
|
||||
}
|
||||
}
|
||||
// Unknown failures stay generic: their messages could carry request internals.
|
||||
return { ok: false, error: 'server' }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The desktop workspace (worktree) that runs on a given Orca Cloud sandbox, if this computer
|
||||
* has it (e.g. a routine run's workspace that was opened here before). Null otherwise.
|
||||
*/
|
||||
export function findLocalWorkspaceForSandbox(sandboxId: string): string | null {
|
||||
const userDataPath = app.getPath('userData')
|
||||
for (const runtime of listEphemeralVmRuntimes(userDataPath)) {
|
||||
if (runtime.recipe?.provider !== 'orca-cloud' || !runtime.workspaceId) {
|
||||
continue
|
||||
}
|
||||
if (findCloudRuntime(userDataPath, runtime.workspaceId)?.sandboxId === sandboxId) {
|
||||
return runtime.workspaceId
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { cloudSecretsFailureCode, createCloudSecretsClient } from './cloud-secrets-client'
|
||||
|
||||
type Seen = { method: string; url: string; body: string | null; authorization: string | null }
|
||||
|
||||
function fakeControlPlane(respond: (seen: Seen) => Response) {
|
||||
const seen: Seen[] = []
|
||||
const fetchImpl: typeof fetch = async (input, init) => {
|
||||
const request: Seen = {
|
||||
method: init?.method ?? 'GET',
|
||||
url: String(input),
|
||||
body: typeof init?.body === 'string' ? init.body : null,
|
||||
authorization: new Headers(init?.headers).get('authorization')
|
||||
}
|
||||
seen.push(request)
|
||||
return respond(request)
|
||||
}
|
||||
const client = createCloudSecretsClient({
|
||||
baseUrl: 'https://control.test',
|
||||
getAccessToken: async () => 'tok',
|
||||
fetch: fetchImpl
|
||||
})
|
||||
return { client, seen }
|
||||
}
|
||||
|
||||
const VIEW = {
|
||||
scope: 'repo',
|
||||
name: 'DATABASE_URL',
|
||||
kind: 'env',
|
||||
hint: 'ab12',
|
||||
updatedAt: 5,
|
||||
updatedBy: 'u1',
|
||||
repo: 'github.com/acme/app'
|
||||
}
|
||||
|
||||
describe('createCloudSecretsClient', () => {
|
||||
it('addresses scope, name and repo on the wire and sends the value only in the body', async () => {
|
||||
const { client, seen } = fakeControlPlane(({ method }) =>
|
||||
Response.json(method === 'GET' ? { secrets: [VIEW] } : { secret: VIEW })
|
||||
)
|
||||
expect(await client.list({ scope: 'repo', repo: 'acme/app' })).toEqual([VIEW])
|
||||
await client.put({ scope: 'repo', repo: 'acme/app' }, 'DATABASE_URL', 'postgres://secret')
|
||||
expect(seen).toEqual([
|
||||
{
|
||||
method: 'GET',
|
||||
url: 'https://control.test/v1/secrets?scope=repo&repo=acme%2Fapp',
|
||||
body: null,
|
||||
authorization: 'Bearer tok'
|
||||
},
|
||||
{
|
||||
method: 'PUT',
|
||||
url: 'https://control.test/v1/secrets/repo/DATABASE_URL?repo=acme%2Fapp',
|
||||
body: '{"value":"postgres://secret"}',
|
||||
authorization: 'Bearer tok'
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
it('posts batches and reads partial success', async () => {
|
||||
const result = {
|
||||
secrets: [{ ...VIEW, scope: 'personal', repo: undefined }],
|
||||
deleted: ['OLD'],
|
||||
errors: [{ name: 'PATH', error: 'invalid_request', reason: 'reserved_name' }]
|
||||
}
|
||||
const { client, seen } = fakeControlPlane(() => Response.json(result))
|
||||
const out = await client.batch(
|
||||
{ scope: 'personal' },
|
||||
{ set: { DATABASE_URL: 'x', PATH: '/bin' }, delete: ['OLD'] }
|
||||
)
|
||||
expect(out.errors).toEqual([
|
||||
{ name: 'PATH', error: 'invalid_request', reason: 'reserved_name' }
|
||||
])
|
||||
expect(seen[0].url).toBe('https://control.test/v1/secrets/personal/batch')
|
||||
})
|
||||
|
||||
it('maps server refusals to their reason and never quotes the request', async () => {
|
||||
const { client } = fakeControlPlane(() =>
|
||||
Response.json(
|
||||
{ error: 'invalid_request', reason: 'subscription_token_refused' },
|
||||
{ status: 400 }
|
||||
)
|
||||
)
|
||||
const error = await client
|
||||
.put({ scope: 'personal' }, 'ANTHROPIC_API_KEY', 'sk-ant-oat01-very-secret')
|
||||
.catch((caught: unknown) => caught)
|
||||
expect(cloudSecretsFailureCode(error)).toBe('subscription_token_refused')
|
||||
expect(String(error)).not.toContain('very-secret')
|
||||
})
|
||||
|
||||
it.each([
|
||||
[403, { error: 'forbidden', reason: 'org_admin_required' }, 'org_admin_required'],
|
||||
[409, { error: 'conflict', reason: 'secret_limit' }, 'secret_limit'],
|
||||
[401, { error: 'unauthorized' }, 'unauthorized'],
|
||||
[503, { error: 'internal_error' }, 'unavailable']
|
||||
])('maps HTTP %i to %s', async (status, body, code) => {
|
||||
const { client } = fakeControlPlane(() => Response.json(body, { status }))
|
||||
const error = await client.list({ scope: 'org' }).catch((caught: unknown) => caught)
|
||||
expect(cloudSecretsFailureCode(error)).toBe(code)
|
||||
})
|
||||
|
||||
it('rejects a response that does not match the schema', async () => {
|
||||
const { client } = fakeControlPlane(() => Response.json({ secrets: [{ name: 1 }] }))
|
||||
const error = await client.list({ scope: 'personal' }).catch((caught: unknown) => caught)
|
||||
expect(cloudSecretsFailureCode(error)).toBe('unavailable')
|
||||
})
|
||||
|
||||
it('reads the effective view and refreshes a sandbox env', async () => {
|
||||
const effective = {
|
||||
caller: {
|
||||
userId: 'u1',
|
||||
orgId: 'org_1',
|
||||
orgRole: 'member',
|
||||
canWrite: { personal: true, org: false, repo: false }
|
||||
},
|
||||
precedence: { env: ['repo', 'personal', 'org'], agent_key: ['org', 'repo', 'personal'] },
|
||||
secrets: [
|
||||
{
|
||||
name: 'ANTHROPIC_API_KEY',
|
||||
kind: 'agent_key',
|
||||
scope: 'org',
|
||||
hint: 'zz99',
|
||||
updatedAt: 1,
|
||||
shadowed: [{ scope: 'personal', hint: null }]
|
||||
}
|
||||
]
|
||||
}
|
||||
const { client, seen } = fakeControlPlane(({ url }) =>
|
||||
Response.json(url.includes('/env/refresh') ? { applied: true } : effective)
|
||||
)
|
||||
expect(await client.effective('acme/app')).toEqual(effective)
|
||||
expect(await client.refreshEnv('sbx_1')).toBe(true)
|
||||
expect(seen.map((request) => `${request.method} ${request.url}`)).toEqual([
|
||||
'GET https://control.test/v1/secrets/effective?repo=acme%2Fapp',
|
||||
'POST https://control.test/v1/sandboxes/sbx_1/env/refresh'
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,121 @@
|
||||
// Orca Cloud secrets on the sandbox control plane. Write-only for values: requests carry them, no
|
||||
// response or error does, and nothing here logs a request body.
|
||||
import {
|
||||
CloudEnvRefreshResponseSchema,
|
||||
CloudSecretBatchResponseSchema,
|
||||
CloudSecretDeleteResponseSchema,
|
||||
CloudSecretListResponseSchema,
|
||||
CloudSecretPutResponseSchema,
|
||||
CloudSecretsEffectiveResponseSchema,
|
||||
cloudSecretsErrorCode,
|
||||
type CloudSecretBatchResult,
|
||||
type CloudSecretsErrorCode,
|
||||
type CloudSecretTarget,
|
||||
type CloudSecretView,
|
||||
type CloudSecretsEffective
|
||||
} from '../../shared/cloud-secrets/cloud-secrets-wire'
|
||||
import { isEnvironmentProviderError } from '../environment-providers/environment-provider-driver'
|
||||
import {
|
||||
createHostedSandboxRequest,
|
||||
type HostedSandboxControlPlaneClientOptions
|
||||
} from '../hosted-sandbox/hosted-sandbox-request'
|
||||
|
||||
export type CloudSecretsClient = {
|
||||
list(target: CloudSecretTarget, signal?: AbortSignal): Promise<CloudSecretView[]>
|
||||
put(target: CloudSecretTarget, name: string, value: string): Promise<CloudSecretView>
|
||||
remove(target: CloudSecretTarget, name: string): Promise<void>
|
||||
batch(
|
||||
target: CloudSecretTarget,
|
||||
change: { set?: Record<string, string>; delete?: string[] }
|
||||
): Promise<CloudSecretBatchResult>
|
||||
effective(repo?: string, signal?: AbortSignal): Promise<CloudSecretsEffective>
|
||||
refreshEnv(sandboxId: string): Promise<boolean>
|
||||
}
|
||||
|
||||
function withRepo(path: string, repo: string | undefined): string {
|
||||
return repo ? `${path}?${new URLSearchParams({ repo })}` : path
|
||||
}
|
||||
|
||||
export function createCloudSecretsClient(
|
||||
options: HostedSandboxControlPlaneClientOptions
|
||||
): CloudSecretsClient {
|
||||
const call = createHostedSandboxRequest(options)
|
||||
const secretPath = (target: CloudSecretTarget, suffix: string): string =>
|
||||
withRepo(`v1/secrets/${encodeURIComponent(target.scope)}/${suffix}`, target.repo)
|
||||
|
||||
return {
|
||||
list: async (target, signal) =>
|
||||
(
|
||||
await call(
|
||||
'GET',
|
||||
`v1/secrets?${new URLSearchParams({
|
||||
scope: target.scope,
|
||||
...(target.repo ? { repo: target.repo } : {})
|
||||
})}`,
|
||||
CloudSecretListResponseSchema,
|
||||
{ kind: 'control', signal }
|
||||
)
|
||||
).secrets,
|
||||
put: async (target, name, value) =>
|
||||
(
|
||||
await call(
|
||||
'PUT',
|
||||
secretPath(target, encodeURIComponent(name)),
|
||||
CloudSecretPutResponseSchema,
|
||||
{
|
||||
body: { value },
|
||||
kind: 'control'
|
||||
}
|
||||
)
|
||||
).secret,
|
||||
remove: async (target, name) => {
|
||||
await call(
|
||||
'DELETE',
|
||||
secretPath(target, encodeURIComponent(name)),
|
||||
CloudSecretDeleteResponseSchema,
|
||||
{ kind: 'control' }
|
||||
)
|
||||
},
|
||||
batch: (target, change) =>
|
||||
call('POST', secretPath(target, 'batch'), CloudSecretBatchResponseSchema, {
|
||||
body: change,
|
||||
kind: 'control'
|
||||
}),
|
||||
effective: (repo, signal) =>
|
||||
call('GET', withRepo('v1/secrets/effective', repo), CloudSecretsEffectiveResponseSchema, {
|
||||
kind: 'control',
|
||||
signal
|
||||
}),
|
||||
refreshEnv: async (sandboxId) =>
|
||||
(
|
||||
await call(
|
||||
'POST',
|
||||
`v1/sandboxes/${encodeURIComponent(sandboxId)}/env/refresh`,
|
||||
CloudEnvRefreshResponseSchema,
|
||||
{ kind: 'control' }
|
||||
)
|
||||
).applied
|
||||
}
|
||||
}
|
||||
|
||||
/** The code the renderer turns into copy: the server reason when it gave one. */
|
||||
export function cloudSecretsFailureCode(error: unknown): CloudSecretsErrorCode {
|
||||
if (!isEnvironmentProviderError(error)) {
|
||||
return 'unknown'
|
||||
}
|
||||
switch (error.code) {
|
||||
case 'unauthorized':
|
||||
return cloudSecretsErrorCode(error.reason, 'unauthorized')
|
||||
case 'not_found':
|
||||
return 'not_found'
|
||||
case 'timeout':
|
||||
return 'timeout'
|
||||
case 'invalid_request':
|
||||
case 'conflict':
|
||||
return cloudSecretsErrorCode(error.reason)
|
||||
case 'unavailable':
|
||||
case 'aborted':
|
||||
case 'invalid_response':
|
||||
return 'unavailable'
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
// Renderer bridge to Orca Cloud secrets. Values travel renderer -> main -> control plane once per save;
|
||||
// nothing returns them, and failures surface as codes only (an error message could quote a value).
|
||||
import { app, ipcMain } from 'electron'
|
||||
import { z } from 'zod'
|
||||
import {
|
||||
CLOUD_SECRET_SCOPES,
|
||||
MAX_CLOUD_SECRET_BATCH,
|
||||
type CloudSecretsResult,
|
||||
type CloudSecretsStatus
|
||||
} from '../../shared/cloud-secrets/cloud-secrets-wire'
|
||||
import { listEphemeralVmRuntimes } from '../../shared/ephemeral-vm-runtime-store'
|
||||
import { environmentHandleFromRecipeResult } from '../environment-providers/environment-provider-recipe-adapter'
|
||||
import { withCloudBuildDefaults } from '../cloud-build-defaults'
|
||||
import { getHostedSandboxConfig } from '../hosted-sandbox/hosted-sandbox-config'
|
||||
import { HOSTED_SANDBOX_DRIVER_ID } from '../hosted-sandbox/hosted-sandbox-driver'
|
||||
import { getOrcaCloudAvailability } from '../hosted-sandbox/hosted-sandbox-install'
|
||||
import {
|
||||
cloudSecretsFailureCode,
|
||||
createCloudSecretsClient,
|
||||
type CloudSecretsClient
|
||||
} from './cloud-secrets-client'
|
||||
|
||||
const Repo = z.string().trim().min(3).max(256)
|
||||
const Target = z.object({ scope: z.enum(CLOUD_SECRET_SCOPES), repo: Repo.optional() })
|
||||
const Name = z
|
||||
.string()
|
||||
.max(128)
|
||||
.regex(/^[A-Z_][A-Z0-9_]*$/)
|
||||
const Value = z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(64 * 1024)
|
||||
const PutRequest = z.object({ target: Target, name: Name, value: Value })
|
||||
const RemoveRequest = z.object({ target: Target, name: Name })
|
||||
const BatchRequest = z
|
||||
.object({
|
||||
target: Target,
|
||||
set: z.record(Name, Value).optional(),
|
||||
delete: z.array(Name).max(MAX_CLOUD_SECRET_BATCH).optional()
|
||||
})
|
||||
.refine(
|
||||
(request) =>
|
||||
Object.keys(request.set ?? {}).length + (request.delete?.length ?? 0) <=
|
||||
MAX_CLOUD_SECRET_BATCH
|
||||
)
|
||||
const EffectiveRequest = z.object({ repo: Repo.optional() })
|
||||
const RefreshRequest = z.object({ runtimeEnvironmentId: z.string().min(1).max(256) })
|
||||
|
||||
const CHANNELS = [
|
||||
'cloudSecrets:status',
|
||||
'cloudSecrets:list',
|
||||
'cloudSecrets:put',
|
||||
'cloudSecrets:remove',
|
||||
'cloudSecrets:batch',
|
||||
'cloudSecrets:effective',
|
||||
'cloudSecrets:refreshEnv'
|
||||
] as const
|
||||
|
||||
async function attempt<T>(
|
||||
client: CloudSecretsClient | null,
|
||||
run: (client: CloudSecretsClient) => Promise<T>
|
||||
): Promise<CloudSecretsResult<T>> {
|
||||
if (!client) {
|
||||
return { ok: false, error: 'not_configured' }
|
||||
}
|
||||
try {
|
||||
return { ok: true, value: await run(client) }
|
||||
} catch (error) {
|
||||
const code = cloudSecretsFailureCode(error)
|
||||
console.warn(`[cloud-secrets] request failed: ${code}`)
|
||||
return { ok: false, error: code }
|
||||
}
|
||||
}
|
||||
|
||||
function sandboxIdFor(userDataPath: string, runtimeEnvironmentId: string): string | null {
|
||||
const runtime = listEphemeralVmRuntimes(userDataPath).find(
|
||||
(record) =>
|
||||
record.runtimeEnvironmentId === runtimeEnvironmentId &&
|
||||
record.recipe?.provider === 'orca-cloud'
|
||||
)
|
||||
const handle = runtime?.recipeResult
|
||||
? environmentHandleFromRecipeResult(runtime.recipeResult, HOSTED_SANDBOX_DRIVER_ID)
|
||||
: null
|
||||
return handle?.environmentId ?? null
|
||||
}
|
||||
|
||||
export function registerCloudSecretsHandlers(userDataPath: string): void {
|
||||
// Packaged integration builds read no shell env; their control-plane URL is baked in.
|
||||
const config = getHostedSandboxConfig(
|
||||
userDataPath,
|
||||
withCloudBuildDefaults(process.env),
|
||||
app.isPackaged
|
||||
)
|
||||
const client = config
|
||||
? createCloudSecretsClient({
|
||||
baseUrl: config.controlPlaneUrl,
|
||||
getAccessToken: config.getAccessToken
|
||||
})
|
||||
: null
|
||||
for (const channel of CHANNELS) {
|
||||
ipcMain.removeHandler(channel)
|
||||
}
|
||||
// A malformed request is a renderer bug, not something the user typed.
|
||||
const invalid = { ok: false, error: 'unknown' } as const
|
||||
|
||||
ipcMain.handle('cloudSecrets:status', async (): Promise<CloudSecretsStatus> => ({
|
||||
available: client !== null && (await getOrcaCloudAvailability()) === 'offered'
|
||||
}))
|
||||
ipcMain.handle('cloudSecrets:list', (_event, raw: unknown) => {
|
||||
const target = Target.safeParse(raw)
|
||||
return target.success ? attempt(client, (c) => c.list(target.data)) : invalid
|
||||
})
|
||||
ipcMain.handle('cloudSecrets:put', (_event, raw: unknown) => {
|
||||
const request = PutRequest.safeParse(raw)
|
||||
return request.success
|
||||
? attempt(client, (c) => c.put(request.data.target, request.data.name, request.data.value))
|
||||
: invalid
|
||||
})
|
||||
ipcMain.handle('cloudSecrets:remove', (_event, raw: unknown) => {
|
||||
const request = RemoveRequest.safeParse(raw)
|
||||
return request.success
|
||||
? attempt(client, async (c) => {
|
||||
await c.remove(request.data.target, request.data.name)
|
||||
return true as const
|
||||
})
|
||||
: invalid
|
||||
})
|
||||
ipcMain.handle('cloudSecrets:batch', (_event, raw: unknown) => {
|
||||
const request = BatchRequest.safeParse(raw)
|
||||
return request.success
|
||||
? attempt(client, (c) =>
|
||||
c.batch(request.data.target, { set: request.data.set, delete: request.data.delete })
|
||||
)
|
||||
: invalid
|
||||
})
|
||||
ipcMain.handle('cloudSecrets:effective', (_event, raw: unknown) => {
|
||||
const request = EffectiveRequest.safeParse(raw ?? {})
|
||||
return request.success ? attempt(client, (c) => c.effective(request.data.repo)) : invalid
|
||||
})
|
||||
ipcMain.handle('cloudSecrets:refreshEnv', (_event, raw: unknown) => {
|
||||
const request = RefreshRequest.safeParse(raw)
|
||||
if (!request.success) {
|
||||
return invalid
|
||||
}
|
||||
const sandboxId = sandboxIdFor(userDataPath, request.data.runtimeEnvironmentId)
|
||||
return sandboxId
|
||||
? attempt(client, (c) => c.refreshEnv(sandboxId))
|
||||
: { ok: false, error: 'not_found' }
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// HTTPS client for the team workspace registry on the sandbox control plane. Same auth as the
|
||||
// hosted sandbox driver: a short-lived Orca token (aud=orca-sandbox), never a provider credential.
|
||||
import type { z } from 'zod'
|
||||
import { z } from 'zod'
|
||||
import { errorMessage } from '../../shared/error-message'
|
||||
import {
|
||||
CloudWorkspaceGrantIssuedSchema,
|
||||
@@ -15,6 +15,8 @@ import {
|
||||
type OrgPrompts
|
||||
} from '../../shared/cloud-workspaces/cloud-workspaces-wire'
|
||||
|
||||
const NoteSentSchema = z.object({ ok: z.literal(true) })
|
||||
|
||||
export class CloudWorkspacesError extends Error {
|
||||
constructor(
|
||||
readonly status: number,
|
||||
@@ -127,6 +129,9 @@ export function createCloudWorkspacesClient(options: CloudWorkspacesClientOption
|
||||
async patch(id: string, patch: CloudWorkspacePatch) {
|
||||
return call('PATCH', ws(id), CloudWorkspaceResponseSchema.extend({}).passthrough(), patch)
|
||||
},
|
||||
async noteSent(id: string) {
|
||||
await call('POST', `${ws(id)}/sent`, NoteSentSchema, {})
|
||||
},
|
||||
async presence(
|
||||
id: string,
|
||||
body: { clientId: string; state: 'viewing' | 'typing' | 'left'; chatId?: string | null }
|
||||
|
||||
@@ -139,6 +139,7 @@ export function createCloudWorkspacesService(deps: {
|
||||
putOrgPrompts: (document: OrgPrompts['document'], revision: number) =>
|
||||
wrap(() => client.putOrgPrompts(document, revision)),
|
||||
localWorkspaces: () => localWorkspaces(),
|
||||
noteSent: (id: string) => wrap(() => client.noteSent(id)),
|
||||
|
||||
/**
|
||||
* A per-device credential for a workspace this desktop does not own: a fresh X25519 key lives
|
||||
@@ -193,6 +194,7 @@ export function createCloudWorkspacesService(deps: {
|
||||
return
|
||||
}
|
||||
const changed = active?.sandboxId !== args.sandboxId
|
||||
const previousChatId = active?.chatId ?? null
|
||||
if (changed && active) {
|
||||
void client
|
||||
.presence(active.sandboxId, { clientId: deps.clientId, state: 'left' })
|
||||
@@ -200,7 +202,8 @@ export function createCloudWorkspacesService(deps: {
|
||||
}
|
||||
active = {
|
||||
sandboxId: args.sandboxId,
|
||||
chatId: args.chatId ?? active?.chatId ?? null,
|
||||
// undefined keeps the chat (typing beats); null means no chat is in front any more.
|
||||
chatId: args.chatId !== undefined ? args.chatId : changed ? null : (active?.chatId ?? null),
|
||||
typingUntil: args.typing
|
||||
? Date.now() + TYPING_HOLD_MS
|
||||
: changed
|
||||
@@ -208,7 +211,7 @@ export function createCloudWorkspacesService(deps: {
|
||||
: (active?.typingUntil ?? 0),
|
||||
leased: args.leased ?? active?.leased ?? false
|
||||
}
|
||||
if (changed || args.typing) {
|
||||
if (changed || args.typing || active.chatId !== previousChatId) {
|
||||
void beat()
|
||||
}
|
||||
renewLeaseIfDue()
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
type EnvironmentDoctorDeps
|
||||
} from './environment-doctor-check'
|
||||
import type { GitCheckoutProbe } from './environment-doctor-git'
|
||||
import { hasEnvironmentBuildMarker } from '../workspace-hooks/environment-build-checkout'
|
||||
|
||||
// Orca writes .cmd or .sh on Windows depending on the script's shell, so look for both.
|
||||
const SETUP_RUNNER_NAMES = ['orca/setup-runner.sh', 'orca/setup-runner.cmd'] as const
|
||||
@@ -26,6 +27,19 @@ export async function checkSetup(
|
||||
const yaml = await context.readRepoFile('orca.yaml')
|
||||
const script = yaml ? parseOrcaYaml(yaml)?.scripts.setup?.trim() : undefined
|
||||
if (!script) {
|
||||
if (
|
||||
context.checkout.kind === 'checkout' &&
|
||||
(await hasEnvironmentBuildMarker(context.checkout.commonDir, context.root))
|
||||
) {
|
||||
return [
|
||||
check(
|
||||
'setup',
|
||||
'setup.script',
|
||||
'pass',
|
||||
'Dependencies come from the Orca Cloud environment build.'
|
||||
)
|
||||
]
|
||||
}
|
||||
return [
|
||||
context.impliesInstall
|
||||
? check('setup', 'setup.script', 'warn', 'orca.yaml has no setup script.', {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Queued prompts on disk: sealed with the OS keychain; without one they live in memory only.
|
||||
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { z } from 'zod'
|
||||
import type { CloudQueuedPrompt } from '../../shared/hosted-sandbox/cloud-workspace-offline-api'
|
||||
import type { RuntimeRpcResponse } from '../../shared/runtime-rpc-envelope'
|
||||
import type { OfflineCacheCodec } from './cloud-offline-cache'
|
||||
@@ -13,38 +14,103 @@ export type StoredPrompt = CloudQueuedPrompt & {
|
||||
nextAttemptAt: number
|
||||
}
|
||||
|
||||
export function readSealedPrompts(path: string, codec: OfflineCacheCodec | null): StoredPrompt[] {
|
||||
// The sealed file is ours, but only its shape is checked here; a prompt that does not match is dropped.
|
||||
const StoredPromptSchema = z.looseObject({
|
||||
id: z.string(),
|
||||
clientOperationId: z.string(),
|
||||
workspaceId: z.string(),
|
||||
sessionId: z.string(),
|
||||
text: z.string(),
|
||||
state: z.enum(['queued', 'sending', 'delivered', 'failed', 'unconfirmed']),
|
||||
createdAt: z.number(),
|
||||
updatedAt: z.number(),
|
||||
failures: z.number(),
|
||||
nextAttemptAt: z.number(),
|
||||
error: z.string().optional()
|
||||
})
|
||||
const SealedQueueSchema = z.object({ version: z.literal(2), prompts: z.array(z.unknown()) })
|
||||
|
||||
export async function readSealedPrompts(
|
||||
path: string,
|
||||
codec: OfflineCacheCodec | null
|
||||
): Promise<StoredPrompt[]> {
|
||||
if (!codec) {
|
||||
return []
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(codec.decrypt(readFileSync(path))) as {
|
||||
version: 2
|
||||
prompts: StoredPrompt[]
|
||||
const parsed = SealedQueueSchema.safeParse(JSON.parse(codec.decrypt(await readFile(path))))
|
||||
if (!parsed.success) {
|
||||
return []
|
||||
}
|
||||
return parsed.version === 2 && Array.isArray(parsed.prompts) ? parsed.prompts : []
|
||||
return parsed.data.prompts.flatMap((raw) => {
|
||||
const prompt = StoredPromptSchema.safeParse(raw)
|
||||
return prompt.success ? [prompt.data] : []
|
||||
})
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export function writeSealedPrompts(
|
||||
export async function writeSealedPrompts(
|
||||
path: string,
|
||||
codec: OfflineCacheCodec | null,
|
||||
prompts: readonly StoredPrompt[]
|
||||
): void {
|
||||
): Promise<void> {
|
||||
if (!codec) {
|
||||
return
|
||||
}
|
||||
mkdirSync(dirname(path), { recursive: true, mode: 0o700 })
|
||||
const sealed = codec.encrypt(JSON.stringify({ version: 2, prompts }))
|
||||
await mkdir(dirname(path), { recursive: true, mode: 0o700 })
|
||||
const temp = `${path}.${process.pid}.tmp`
|
||||
writeFileSync(temp, codec.encrypt(JSON.stringify({ version: 2, prompts })), { mode: 0o600 })
|
||||
renameSync(temp, path)
|
||||
await writeFile(temp, sealed, { mode: 0o600 })
|
||||
await rename(temp, path)
|
||||
}
|
||||
|
||||
/** The first build kept the queue as plaintext JSON beside the sealed file; it is never read. */
|
||||
export function removeLegacyPlaintextQueue(path: string): void {
|
||||
rmSync(join(dirname(path), 'orca-cloud-queued-prompts.json'), { force: true })
|
||||
export function removeLegacyPlaintextQueue(path: string): Promise<void> {
|
||||
return rm(join(dirname(path), 'orca-cloud-queued-prompts.json'), { force: true })
|
||||
}
|
||||
|
||||
/** The sealed queue file: read once, then every write and removal in call order. */
|
||||
export function createSealedPromptDisk(
|
||||
path: string,
|
||||
codec: OfflineCacheCodec | null,
|
||||
log: (line: string) => void
|
||||
) {
|
||||
const load = (async () => {
|
||||
await removeLegacyPlaintextQueue(path).catch(() => undefined)
|
||||
return readSealedPrompts(path, codec)
|
||||
})()
|
||||
// Nothing is written before the sealed file has been read.
|
||||
let tail: Promise<unknown> = load
|
||||
function serial(task: () => Promise<void>): Promise<void> {
|
||||
const run = tail.then(task)
|
||||
tail = run.catch((error: unknown) =>
|
||||
log(`[orca-cloud] queued prompts write failed: ${String(error)}`)
|
||||
)
|
||||
return run
|
||||
}
|
||||
return {
|
||||
load,
|
||||
write: (prompts: readonly StoredPrompt[]) =>
|
||||
serial(() => writeSealedPrompts(path, codec, prompts)),
|
||||
remove: () =>
|
||||
serial(async () => {
|
||||
await removeLegacyPlaintextQueue(path)
|
||||
await rm(path, { force: true })
|
||||
}),
|
||||
flushed: () => serial(async () => {})
|
||||
}
|
||||
}
|
||||
|
||||
export function earliestOwedAttempt(prompts: readonly StoredPrompt[]): number | null {
|
||||
let earliest: number | null = null
|
||||
for (const prompt of prompts) {
|
||||
if (prompt.state === 'queued' || prompt.state === 'sending') {
|
||||
earliest = earliest === null ? prompt.nextAttemptAt : Math.min(earliest, prompt.nextAttemptAt)
|
||||
}
|
||||
}
|
||||
return earliest
|
||||
}
|
||||
|
||||
/** Refusals that mean "not now", not "never": the same operation is sent again. */
|
||||
@@ -87,11 +153,24 @@ export async function currentFence(
|
||||
? { refused: code }
|
||||
: null
|
||||
}
|
||||
const result = response.result as { ok: boolean; page?: { fence?: number }; fence?: number }
|
||||
const parsed = FenceResultSchema.safeParse(response.result)
|
||||
const result = parsed.success ? parsed.data : { ok: false }
|
||||
const fence = result.page?.fence ?? (result.ok ? undefined : result.fence)
|
||||
return typeof fence === 'number' ? fence : null
|
||||
}
|
||||
|
||||
const FenceResultSchema = z.looseObject({
|
||||
ok: z.boolean(),
|
||||
page: z.looseObject({ fence: z.number().optional() }).optional(),
|
||||
fence: z.number().optional()
|
||||
})
|
||||
|
||||
/** `agentSession.send` answer: delivered, or refused with a code. */
|
||||
export const SendResultSchema = z.looseObject({
|
||||
ok: z.boolean(),
|
||||
refusal: z.looseObject({ code: z.string().optional(), message: z.string().optional() }).optional()
|
||||
})
|
||||
|
||||
export type WorkspaceAvailability =
|
||||
| { kind: 'up'; environmentId: string }
|
||||
| { kind: 'asleep'; environmentId: string }
|
||||
@@ -133,4 +212,8 @@ export type CloudMessageQueue = {
|
||||
failPending(workspaceId: string, reason: string): void
|
||||
/** Sign-out / profile switch: forget everything. */
|
||||
clear(): void
|
||||
/** Resolves once every disk write requested so far has finished (shutdown, tests). */
|
||||
flushed(): Promise<void>
|
||||
/** Earliest background attempt still owed (ms epoch), or null when nothing is pending. */
|
||||
nextAttemptAt(): number | null
|
||||
}
|
||||
|
||||
@@ -18,18 +18,14 @@ import {
|
||||
import type { OfflineCacheCodec } from './cloud-offline-cache'
|
||||
|
||||
const SESSION = '0f8e7c1a-2b3d-4e5f-8a9b-0c1d2e3f4a5b'
|
||||
/** Fixed test epoch: operation ids and the host's admission window both read this clock. */
|
||||
const T0 = 1_800_000_000_000
|
||||
|
||||
const codec: OfflineCacheCodec = {
|
||||
encrypt: (plain) => Buffer.from(Buffer.from(plain, 'utf8').map((b) => b ^ 0x5a)),
|
||||
decrypt: (sealed) => Buffer.from(sealed.map((b) => b ^ 0x5a)).toString('utf8')
|
||||
}
|
||||
|
||||
type Envelope = {
|
||||
clientOperationId: string
|
||||
expectedRuntimeFence: number
|
||||
payloadFingerprint: string
|
||||
}
|
||||
|
||||
/**
|
||||
* A host that admits operations with the real ledger rule (id format, age, replay by id) and
|
||||
* validates params with the real send schema.
|
||||
@@ -59,7 +55,7 @@ function realishHost(clock: () => number) {
|
||||
}
|
||||
}
|
||||
const parsed = SendParams.parse(params)
|
||||
const envelope = parsed.envelope as Envelope
|
||||
const envelope = parsed.envelope
|
||||
const decision = evaluateAgentSessionOperation({
|
||||
rows,
|
||||
callerKey: 'desktop',
|
||||
@@ -78,9 +74,10 @@ function realishHost(clock: () => number) {
|
||||
}
|
||||
if (decision.decision === 'admit') {
|
||||
rows.set(agentSessionOperationKey('desktop', envelope.clientOperationId), decision.row)
|
||||
const block = parsed.body.blocks[0]
|
||||
effects.push({
|
||||
text: (parsed.body.blocks[0] as { text: string }).text,
|
||||
fence: envelope.expectedRuntimeFence
|
||||
text: block?.type === 'text' ? block.text : '',
|
||||
fence: envelope.expectedRuntimeFence ?? 0
|
||||
})
|
||||
}
|
||||
if (loseNextReply) {
|
||||
@@ -113,7 +110,7 @@ function setup(
|
||||
clock?: { now: number }
|
||||
} = {}
|
||||
) {
|
||||
const clock = opts.clock ?? { now: Date.now() }
|
||||
const clock = opts.clock ?? { now: T0 }
|
||||
const path = opts.path ?? join(mkdtempSync(join(tmpdir(), 'orca-q-')), 'q.bin')
|
||||
const host = opts.host ?? realishHost(() => clock.now)
|
||||
let asleep = true
|
||||
@@ -132,14 +129,11 @@ function setup(
|
||||
call: host.call,
|
||||
log: () => {}
|
||||
})
|
||||
const settle = () => new Promise((resolve) => setTimeout(resolve, 0))
|
||||
return { queue, host, wake, path, clock, settle, sleep: () => (asleep = true) }
|
||||
return { queue, host, wake, path, clock, sleep: () => (asleep = true) }
|
||||
}
|
||||
|
||||
async function until(check: () => boolean) {
|
||||
for (let i = 0; i < 100 && !check(); i++) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1))
|
||||
}
|
||||
await vi.waitFor(() => expect(check()).toBe(true))
|
||||
}
|
||||
|
||||
describe('cloud message queue', () => {
|
||||
@@ -155,15 +149,18 @@ describe('cloud message queue', () => {
|
||||
})
|
||||
|
||||
it('re-mints a never-sent id before it ages out of the host admission window', async () => {
|
||||
const clock = { now: Date.now() }
|
||||
const clock = { now: T0 }
|
||||
const host = realishHost(() => clock.now)
|
||||
const place = { current: { kind: 'asleep', environmentId: 'env' } as WorkspaceAvailability }
|
||||
const place: { current: WorkspaceAvailability } = {
|
||||
current: { kind: 'asleep', environmentId: 'env' }
|
||||
}
|
||||
const parked = setup({ clock, host, place: () => place.current })
|
||||
parked.wake.mockResolvedValueOnce(false)
|
||||
await parked.queue.enqueue({ workspaceId: 'w', sessionId: SESSION, text: 'a day later' })
|
||||
// The wake failed: the prompt waits, queued, for the VM to be up.
|
||||
await until(() => parked.wake.mock.calls.length === 1)
|
||||
await new Promise((resolve) => setTimeout(resolve, 5))
|
||||
// Joins the in-flight pass (never wakes on its own).
|
||||
await parked.queue.drive('w')
|
||||
expect(parked.queue.list('w')[0]!.state).toBe('queued')
|
||||
clock.now += AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS + 60_000
|
||||
place.current = { kind: 'up', environmentId: 'env' }
|
||||
@@ -177,9 +174,10 @@ describe('cloud message queue', () => {
|
||||
first.wake.mockResolvedValueOnce(false)
|
||||
await first.queue.enqueue({ workspaceId: 'w', sessionId: SESSION, text: 'after restart' })
|
||||
await until(() => first.wake.mock.calls.length === 1)
|
||||
await new Promise((resolve) => setTimeout(resolve, 5))
|
||||
await first.queue.drive('w')
|
||||
await first.queue.flushed()
|
||||
expect(readFileSync(first.path).toString('utf8')).not.toContain('after restart')
|
||||
const host = realishHost(() => Date.now())
|
||||
const host = realishHost(() => first.clock.now)
|
||||
let up = false
|
||||
const second = setup({
|
||||
path: first.path,
|
||||
@@ -187,6 +185,7 @@ describe('cloud message queue', () => {
|
||||
place: () =>
|
||||
up ? { kind: 'up', environmentId: 'env' } : { kind: 'asleep', environmentId: 'env' }
|
||||
})
|
||||
await second.queue.flushed()
|
||||
expect(second.queue.list('w').map((p) => p.state)).toEqual(['queued'])
|
||||
await second.queue.drive()
|
||||
expect(second.wake).not.toHaveBeenCalled()
|
||||
@@ -198,7 +197,7 @@ describe('cloud message queue', () => {
|
||||
})
|
||||
|
||||
it('is at most once when the reply is lost: the retry reuses the id and the host replays', async () => {
|
||||
const clock = { now: Date.now() }
|
||||
const clock = { now: T0 }
|
||||
const host = realishHost(() => clock.now)
|
||||
const { queue, path } = setup({
|
||||
clock,
|
||||
@@ -208,6 +207,7 @@ describe('cloud message queue', () => {
|
||||
host.loseNextReply()
|
||||
await queue.enqueue({ workspaceId: 'w', sessionId: SESSION, text: 'once' })
|
||||
await until(() => queue.list('w')[0]!.state === 'sending')
|
||||
await queue.flushed()
|
||||
clock.now += 60 * 60_000
|
||||
const restarted = setup({
|
||||
path,
|
||||
@@ -222,7 +222,7 @@ describe('cloud message queue', () => {
|
||||
})
|
||||
|
||||
it('never wakes from the background, and stops after its failure budget', async () => {
|
||||
const clock = { now: Date.now() }
|
||||
const clock = { now: T0 }
|
||||
const host = realishHost(() => clock.now)
|
||||
host.setHistoryFails(true)
|
||||
const { queue, wake } = setup({
|
||||
@@ -241,7 +241,7 @@ describe('cloud message queue', () => {
|
||||
})
|
||||
|
||||
it('backs off between background attempts', async () => {
|
||||
const clock = { now: Date.now() }
|
||||
const clock = { now: T0 }
|
||||
const host = realishHost(() => clock.now)
|
||||
host.setHistoryFails(true)
|
||||
const { queue } = setup({ clock, host, place: () => ({ kind: 'up', environmentId: 'env' }) })
|
||||
@@ -280,7 +280,7 @@ describe('cloud message queue', () => {
|
||||
})
|
||||
|
||||
it('does not send a prompt cancelled or edited while its fence was being read', async () => {
|
||||
const clock = { now: Date.now() }
|
||||
const clock = { now: T0 }
|
||||
const host = realishHost(() => clock.now)
|
||||
let release: () => void = () => {}
|
||||
const gate = new Promise<void>((resolve) => (release = resolve))
|
||||
@@ -304,7 +304,6 @@ describe('cloud message queue', () => {
|
||||
expect(queue.cancel(a.id)).toBe(true)
|
||||
release()
|
||||
await queue.drive('w')
|
||||
await new Promise((resolve) => setTimeout(resolve, 5))
|
||||
expect(host.effects).toHaveLength(0)
|
||||
})
|
||||
|
||||
@@ -359,7 +358,7 @@ describe('cloud message queue', () => {
|
||||
})
|
||||
|
||||
it('does not resurrect a prompt a Sleep failed while its fence was being read', async () => {
|
||||
const clock = { now: Date.now() }
|
||||
const clock = { now: T0 }
|
||||
const host = realishHost(() => clock.now)
|
||||
let release: () => void = () => {}
|
||||
const gate = new Promise<void>((resolve) => (release = resolve))
|
||||
@@ -382,13 +381,14 @@ describe('cloud message queue', () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 0))
|
||||
queue.failPending('w', 'The workspace was put to sleep before this was sent.')
|
||||
release()
|
||||
await new Promise((resolve) => setTimeout(resolve, 5))
|
||||
// Joins the pass that was waiting on the fence.
|
||||
await queue.drive('w')
|
||||
expect(host.effects).toHaveLength(0)
|
||||
expect(queue.list('w')[0]!.state).toBe('failed')
|
||||
})
|
||||
|
||||
it('ends a sent-but-unconfirmed prompt as unconfirmed, not failed, at its budget', async () => {
|
||||
const clock = { now: Date.now() }
|
||||
const clock = { now: T0 }
|
||||
const host = realishHost(() => clock.now)
|
||||
const lossy: CloudMessageQueueDeps['call'] = async (env, method, params, t) => {
|
||||
const answer = await host.call(env, method, params, t)
|
||||
@@ -430,9 +430,11 @@ describe('cloud message queue', () => {
|
||||
},
|
||||
log: () => {}
|
||||
})
|
||||
await queue.flushed()
|
||||
expect(existsSync(legacy)).toBe(false)
|
||||
writeFileSync(legacy, 'again')
|
||||
queue.clear()
|
||||
await queue.flushed()
|
||||
expect(existsSync(legacy)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
// Only the person queuing a prompt wakes a VM. Background retries deliver only to a VM that is
|
||||
// already up, never to one asleep, archived or manually disconnected, and stop after a budget.
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { rmSync } from 'node:fs'
|
||||
import { CLOUD_QUEUED_PROMPT_MAX_CHARS } from '../../shared/hosted-sandbox/cloud-workspace-offline-api'
|
||||
import type { RuntimeRpcResponse } from '../../shared/runtime-rpc-envelope'
|
||||
import { structuredAgentSessionMessageSendMutation } from '../../shared/agent-session/requests/structured-agent-session-send-mutation'
|
||||
@@ -21,12 +20,12 @@ import {
|
||||
import { createStructuredAgentSessionOperationId } from '../../shared/agent-session/lifecycle/structured-agent-session-mutation'
|
||||
import { SendParams } from '../../shared/rpc-contract/structured-agent-session-params'
|
||||
import {
|
||||
readSealedPrompts,
|
||||
createSealedPromptDisk,
|
||||
currentFence,
|
||||
earliestOwedAttempt,
|
||||
publicView,
|
||||
RETRYABLE_REFUSALS,
|
||||
removeLegacyPlaintextQueue,
|
||||
writeSealedPrompts,
|
||||
SendResultSchema,
|
||||
type CloudMessageQueue,
|
||||
type CloudMessageQueueDeps,
|
||||
type StoredPrompt
|
||||
@@ -52,26 +51,33 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
const log = deps.log ?? ((line: string) => console.error(line))
|
||||
const driving = new Map<string, Promise<void>>()
|
||||
const mintId = () => createStructuredAgentSessionOperationId(randomUUID, now())
|
||||
removeLegacyPlaintextQueue(deps.path)
|
||||
let prompts: StoredPrompt[] = readSealedPrompts(deps.path, deps.codec)
|
||||
let prompts: StoredPrompt[] = []
|
||||
// Bumped by clear() so a load still reading the old file cannot bring cleared prompts back.
|
||||
let generation = 0
|
||||
const disk = createSealedPromptDisk(deps.path, deps.codec, log)
|
||||
const loaded = disk.load.then((stored) => {
|
||||
prompts = generation === 0 ? [...stored, ...prompts] : prompts
|
||||
})
|
||||
|
||||
function save(): void {
|
||||
/** Resolves once the queue is on disk as of this call; rejects if that write failed. */
|
||||
function save(): Promise<void> {
|
||||
const cutoff = now() - DELIVERED_RETENTION_MS
|
||||
prompts = prompts.filter((prompt) => prompt.state !== 'delivered' || prompt.updatedAt >= cutoff)
|
||||
writeSealedPrompts(deps.path, deps.codec, prompts)
|
||||
return disk.write(prompts)
|
||||
}
|
||||
|
||||
function update(prompt: StoredPrompt, patch: Partial<StoredPrompt>): void {
|
||||
function update(prompt: StoredPrompt, patch: Partial<StoredPrompt>): Promise<void> {
|
||||
Object.assign(prompt, patch, { updatedAt: now() })
|
||||
save()
|
||||
const saved = save()
|
||||
deps.onChange?.(prompt.workspaceId)
|
||||
return saved
|
||||
}
|
||||
|
||||
function failOnce(prompt: StoredPrompt, error: string): void {
|
||||
const failures = prompt.failures + 1
|
||||
if (failures >= MAX_FAILURES) {
|
||||
// A prompt that reached `sending` may already be on the host: say so, so nobody re-sends it.
|
||||
update(
|
||||
void update(
|
||||
prompt,
|
||||
prompt.state === 'sending'
|
||||
? {
|
||||
@@ -83,7 +89,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
)
|
||||
return
|
||||
}
|
||||
update(prompt, {
|
||||
void update(prompt, {
|
||||
failures,
|
||||
nextAttemptAt: now() + Math.min(BACKOFF_MAX_MS, BACKOFF_BASE_MS * 2 ** (failures - 1))
|
||||
})
|
||||
@@ -104,7 +110,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
prompt.state === 'queued' &&
|
||||
now() - mintedAt > AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS - REMINT_MARGIN_MS
|
||||
) {
|
||||
update(prompt, { clientOperationId: mintId() })
|
||||
await update(prompt, { clientOperationId: mintId() })
|
||||
}
|
||||
const before = { id: prompt.clientOperationId, text: prompt.text, state: prompt.state }
|
||||
let fence: number | { refused: string } | null
|
||||
@@ -129,7 +135,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
return 'stop'
|
||||
}
|
||||
if (typeof fence !== 'number') {
|
||||
update(prompt, { state: 'failed', error: 'This chat is no longer on the workspace.' })
|
||||
await update(prompt, { state: 'failed', error: 'This chat is no longer on the workspace.' })
|
||||
return 'next'
|
||||
}
|
||||
const params = structuredAgentSessionMessageSendMutation({
|
||||
@@ -142,11 +148,11 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
})
|
||||
// The host's own schema: a prompt it would reject outright is failed here, never retried.
|
||||
if (!SendParams.safeParse(params).success) {
|
||||
update(prompt, { state: 'failed', error: 'This message cannot be sent to that chat.' })
|
||||
await update(prompt, { state: 'failed', error: 'This message cannot be sent to that chat.' })
|
||||
return 'next'
|
||||
}
|
||||
// Durable before the send: from here on, any retry resends this same operation id.
|
||||
update(prompt, { state: 'sending' })
|
||||
await update(prompt, { state: 'sending' })
|
||||
let response: RuntimeRpcResponse<unknown>
|
||||
try {
|
||||
response = await deps.call(environmentId, 'agentSession.send', params, SEND_TIMEOUT_MS)
|
||||
@@ -159,9 +165,10 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
failOnce(prompt, response.error.message.slice(0, 200))
|
||||
return 'stop'
|
||||
}
|
||||
const result = response.result as { ok: boolean; refusal?: { code?: string; message?: string } }
|
||||
const parsed = SendResultSchema.safeParse(response.result)
|
||||
const result = parsed.success ? parsed.data : { ok: false, refusal: undefined }
|
||||
if (result.ok) {
|
||||
update(prompt, { state: 'delivered', error: undefined })
|
||||
await update(prompt, { state: 'delivered', error: undefined })
|
||||
return 'next'
|
||||
}
|
||||
const code = result.refusal?.code ?? 'unknown'
|
||||
@@ -169,11 +176,12 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
failOnce(prompt, result.refusal?.message ?? code)
|
||||
return 'stop'
|
||||
}
|
||||
update(prompt, { state: 'failed', error: result.refusal?.message ?? code })
|
||||
await update(prompt, { state: 'failed', error: result.refusal?.message ?? code })
|
||||
return 'next'
|
||||
}
|
||||
|
||||
async function driveWorkspace(workspaceId: string, allowWake: boolean): Promise<void> {
|
||||
await loaded
|
||||
if (pendingOf(workspaceId).length === 0) {
|
||||
return
|
||||
}
|
||||
@@ -233,6 +241,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
if (!trimmed || trimmed.length > CLOUD_QUEUED_PROMPT_MAX_CHARS) {
|
||||
throw new Error('Prompt is empty or too long.')
|
||||
}
|
||||
await loaded
|
||||
const place = await deps.locate(workspaceId)
|
||||
if (place.kind === 'blocked') {
|
||||
throw new Error(place.reason)
|
||||
@@ -254,7 +263,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
nextAttemptAt: at
|
||||
}
|
||||
prompts.push(prompt)
|
||||
save()
|
||||
await save()
|
||||
deps.onChange?.(workspaceId)
|
||||
void run(workspaceId, true)
|
||||
return publicView(prompt)
|
||||
@@ -268,7 +277,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
return false
|
||||
}
|
||||
prompts = prompts.filter((p) => p !== prompt)
|
||||
save()
|
||||
void save()
|
||||
deps.onChange?.(prompt.workspaceId)
|
||||
return true
|
||||
},
|
||||
@@ -284,9 +293,11 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
return false
|
||||
}
|
||||
// Never sent, so a new text is a new operation; nothing on the host knows the old id.
|
||||
update(prompt, { text: trimmed, clientOperationId: mintId() })
|
||||
void update(prompt, { text: trimmed, clientOperationId: mintId() })
|
||||
return true
|
||||
},
|
||||
nextAttemptAt: () => earliestOwedAttempt(prompts),
|
||||
flushed: () => disk.flushed(),
|
||||
drive(workspaceId) {
|
||||
const ids = workspaceId
|
||||
? [workspaceId]
|
||||
@@ -309,15 +320,15 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
save()
|
||||
void save()
|
||||
deps.onChange?.(workspaceId)
|
||||
}
|
||||
},
|
||||
clear() {
|
||||
removeLegacyPlaintextQueue(deps.path)
|
||||
generation++
|
||||
const workspaces = new Set(prompts.map((p) => p.workspaceId))
|
||||
prompts = []
|
||||
rmSync(deps.path, { force: true })
|
||||
void disk.remove().catch(() => undefined)
|
||||
for (const workspaceId of workspaces) {
|
||||
deps.onChange?.(workspaceId)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createOwedWorkBeat } from './cloud-offline-beat'
|
||||
|
||||
const T0 = 1_800_000_000_000
|
||||
|
||||
describe('owed-work beat', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
vi.setSystemTime(T0)
|
||||
})
|
||||
afterEach(() => vi.useRealTimers())
|
||||
|
||||
it('stays idle with nothing owed, and backs off while passes change nothing', async () => {
|
||||
let due: number | null = null
|
||||
const pass = vi.fn(async () => {})
|
||||
const beat = createOwedWorkBeat({
|
||||
nextDueAt: () => due,
|
||||
pass,
|
||||
baseMs: 1_000,
|
||||
maxBackoffSteps: 2
|
||||
})
|
||||
beat.poke()
|
||||
await vi.advanceTimersByTimeAsync(60_000)
|
||||
expect(pass).not.toHaveBeenCalled()
|
||||
|
||||
due = T0
|
||||
beat.poke()
|
||||
await vi.advanceTimersByTimeAsync(1_000)
|
||||
expect(pass).toHaveBeenCalledTimes(1)
|
||||
// Unchanged after that pass: the next waits 2 s, then 4 s (the cap).
|
||||
await vi.advanceTimersByTimeAsync(1_999)
|
||||
expect(pass).toHaveBeenCalledTimes(1)
|
||||
await vi.advanceTimersByTimeAsync(1)
|
||||
expect(pass).toHaveBeenCalledTimes(2)
|
||||
await vi.advanceTimersByTimeAsync(4_000)
|
||||
expect(pass).toHaveBeenCalledTimes(3)
|
||||
await vi.advanceTimersByTimeAsync(4_000)
|
||||
expect(pass).toHaveBeenCalledTimes(4)
|
||||
|
||||
due = null
|
||||
await vi.advanceTimersByTimeAsync(4_000)
|
||||
const settled = pass.mock.calls.length
|
||||
await vi.advanceTimersByTimeAsync(60_000)
|
||||
expect(pass).toHaveBeenCalledTimes(settled)
|
||||
beat.dispose()
|
||||
})
|
||||
|
||||
it('waits for a later due attempt and stops on dispose', async () => {
|
||||
const pass = vi.fn(async () => {})
|
||||
const beat = createOwedWorkBeat({
|
||||
nextDueAt: () => T0 + 10_000,
|
||||
pass,
|
||||
baseMs: 1_000,
|
||||
maxBackoffSteps: 2
|
||||
})
|
||||
beat.poke()
|
||||
await vi.advanceTimersByTimeAsync(9_999)
|
||||
expect(pass).not.toHaveBeenCalled()
|
||||
await vi.advanceTimersByTimeAsync(1)
|
||||
expect(pass).toHaveBeenCalledTimes(1)
|
||||
beat.dispose()
|
||||
await vi.advanceTimersByTimeAsync(60_000)
|
||||
expect(pass).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
// Background delivery for queued Orca Cloud prompts runs only while prompts are owed: at the next
|
||||
// due attempt, never sooner than the base cadence, doubling while a pass changes nothing.
|
||||
export type OwedWorkBeat = {
|
||||
/** Something changed (a prompt was queued or settled): arm at the base cadence again. */
|
||||
poke(): void
|
||||
/** Run a pass now (startup), then keep the cadence while work is owed. */
|
||||
runNow(): Promise<void>
|
||||
dispose(): void
|
||||
}
|
||||
|
||||
export function createOwedWorkBeat(options: {
|
||||
/** Earliest owed attempt (ms epoch), or null when nothing is owed: the beat stops. */
|
||||
nextDueAt: () => number | null
|
||||
pass: () => Promise<void>
|
||||
baseMs: number
|
||||
maxBackoffSteps: number
|
||||
now?: () => number
|
||||
}): OwedWorkBeat {
|
||||
const now = options.now ?? Date.now
|
||||
let timer: ReturnType<typeof setTimeout> | null = null
|
||||
let idlePasses = 0
|
||||
let disposed = false
|
||||
|
||||
function arm(): void {
|
||||
const due = options.nextDueAt()
|
||||
if (timer || disposed || due === null) {
|
||||
return
|
||||
}
|
||||
const idleDelay = options.baseMs * 2 ** Math.min(idlePasses, options.maxBackoffSteps)
|
||||
timer = setTimeout(() => void runNow(), Math.max(due - now(), idleDelay))
|
||||
timer.unref?.()
|
||||
}
|
||||
|
||||
async function runNow(): Promise<void> {
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
timer = null
|
||||
}
|
||||
const before = options.nextDueAt()
|
||||
await options.pass()
|
||||
idlePasses = options.nextDueAt() === before ? idlePasses + 1 : 0
|
||||
arm()
|
||||
}
|
||||
|
||||
return {
|
||||
poke() {
|
||||
idlePasses = 0
|
||||
arm()
|
||||
},
|
||||
runNow,
|
||||
dispose() {
|
||||
disposed = true
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
timer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
// Types, limits and small helpers of the Orca Cloud offline cache (see cloud-offline-cache.ts).
|
||||
import { readdirSync, rmSync } from 'node:fs'
|
||||
import { createHash } from 'node:crypto'
|
||||
import { readdir, rm } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import { z } from 'zod'
|
||||
import type { RuntimeRpcSuccess } from '../../shared/runtime-rpc-envelope'
|
||||
|
||||
/** Read-only methods whose answers stay meaningful while the VM sleeps. Nothing that mutates. */
|
||||
@@ -76,14 +78,15 @@ export type CloudOfflineCache = {
|
||||
params: unknown,
|
||||
response: RuntimeRpcSuccess<unknown>
|
||||
): void
|
||||
lookup(environmentId: string, method: string, params: unknown): OfflineCacheEntry | null
|
||||
entries(environmentId: string, method?: string): OfflineCacheEntry[]
|
||||
forget(environmentId: string): void
|
||||
lookup(environmentId: string, method: string, params: unknown): Promise<OfflineCacheEntry | null>
|
||||
entries(environmentId: string, method?: string): Promise<OfflineCacheEntry[]>
|
||||
forget(environmentId: string): Promise<void>
|
||||
/** Drop every environment not in `keep` (removed workspaces). */
|
||||
prune(keep: ReadonlySet<string>): void
|
||||
prune(keep: ReadonlySet<string>): Promise<void>
|
||||
/** Delete everything (sign-out, profile switch). */
|
||||
clear(): void
|
||||
flush(): void
|
||||
clear(): Promise<void>
|
||||
/** Writes pending changes; resolves once earlier records and file work have landed. */
|
||||
flush(): Promise<void>
|
||||
}
|
||||
|
||||
/** Key-order independent, so `{a,b}` and `{b,a}` hit the same entry. */
|
||||
@@ -105,14 +108,51 @@ export function stableKey(method: string, params: unknown): string {
|
||||
return `${method} ${JSON.stringify(sort(params ?? null))}`
|
||||
}
|
||||
|
||||
export function removeLegacyPlaintextFiles(root: string): void {
|
||||
export async function removeLegacyPlaintextFiles(root: string): Promise<void> {
|
||||
try {
|
||||
for (const name of readdirSync(root)) {
|
||||
for (const name of await readdir(root)) {
|
||||
if (name.endsWith('.json')) {
|
||||
rmSync(join(root, name), { force: true })
|
||||
await rm(join(root, name), { force: true })
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// No cache directory yet.
|
||||
}
|
||||
}
|
||||
|
||||
export const EnvironmentFileSchema = z.object({
|
||||
version: z.literal(2),
|
||||
environmentId: z.string(),
|
||||
updatedAt: z.number(),
|
||||
entries: z.record(
|
||||
z.string(),
|
||||
z.object({
|
||||
method: z.string(),
|
||||
params: z.unknown(),
|
||||
resultJson: z.string(),
|
||||
runtimeId: z.string(),
|
||||
at: z.number(),
|
||||
bytes: z.number()
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
export function fileName(environmentId: string): string {
|
||||
return `${createHash('sha256').update(environmentId).digest('hex').slice(0, 32)}.bin`
|
||||
}
|
||||
|
||||
export function decode(entry: StoredEntry): OfflineCacheEntry {
|
||||
const { resultJson, ...rest } = entry
|
||||
const result: unknown = JSON.parse(resultJson)
|
||||
return { ...rest, result }
|
||||
}
|
||||
|
||||
export const DISABLED_OFFLINE_CACHE: CloudOfflineCache = {
|
||||
record: () => {},
|
||||
lookup: async () => null,
|
||||
entries: async () => [],
|
||||
forget: async () => {},
|
||||
prune: async () => {},
|
||||
clear: async () => {},
|
||||
flush: async () => {}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
import {
|
||||
archiveCheckoutsFromCache,
|
||||
buildOfflineSnapshot,
|
||||
stringParam,
|
||||
stripAnsi
|
||||
} from './cloud-offline-snapshot'
|
||||
|
||||
@@ -41,17 +42,17 @@ function setup(limits = {}) {
|
||||
}
|
||||
|
||||
describe('cloud offline cache', () => {
|
||||
it('keeps only allowlisted read-only methods', () => {
|
||||
it('keeps only allowlisted read-only methods', async () => {
|
||||
const { cache } = setup()
|
||||
cache.record('env', 'git.status', { worktree: 'id:w' }, ok({ entries: [] }))
|
||||
cache.record('env', 'agentSession.send', { sessionId: 's' }, ok({}))
|
||||
cache.record('env', 'git.commit', { worktree: 'id:w' }, ok({}))
|
||||
expect(cache.entries('env').map((e) => e.method)).toEqual(['git.status'])
|
||||
expect((await cache.entries('env')).map((e) => e.method)).toEqual(['git.status'])
|
||||
expect(OFFLINE_READ_METHODS.has('terminal.send')).toBe(false)
|
||||
expect(cache.lookup('env', 'agentSession.send', { sessionId: 's' })).toBeNull()
|
||||
expect(await cache.lookup('env', 'agentSession.send', { sessionId: 's' })).toBeNull()
|
||||
})
|
||||
|
||||
it('seals files at rest and reads them back after a reload', () => {
|
||||
it('seals files at rest and reads them back after a reload', async () => {
|
||||
const { root, cache } = setup()
|
||||
cache.record(
|
||||
'env',
|
||||
@@ -60,23 +61,27 @@ describe('cloud offline cache', () => {
|
||||
ok({ items: ['secret subject'] })
|
||||
)
|
||||
expect(stableKey('m', { a: 1, b: 2 })).toBe(stableKey('m', { b: 2, a: 1 }))
|
||||
await cache.flush()
|
||||
const [name] = readdirSync(root)
|
||||
expect(readFileSync(join(root, name!)).toString('utf8')).not.toContain('secret subject')
|
||||
const reloaded = createCloudOfflineCache({ root, codec: testCodec, flushDelayMs: 0 })
|
||||
expect(reloaded.lookup('env', 'git.history', { limit: 20, worktree: 'id:w' })?.result).toEqual({
|
||||
expect(
|
||||
(await reloaded.lookup('env', 'git.history', { limit: 20, worktree: 'id:w' }))?.result
|
||||
).toEqual({
|
||||
items: ['secret subject']
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps nothing when the OS cannot seal files', () => {
|
||||
it('keeps nothing when the OS cannot seal files', async () => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'orca-offline-'))
|
||||
const cache = createCloudOfflineCache({ root, codec: null, flushDelayMs: 0 })
|
||||
cache.record('env', 'git.status', {}, ok({ entries: [] }))
|
||||
expect(cache.entries('env')).toEqual([])
|
||||
expect(await cache.entries('env')).toEqual([])
|
||||
await cache.flush()
|
||||
expect(readdirSync(root)).toEqual([])
|
||||
})
|
||||
|
||||
it('does not re-serialize a key polled again within the interval', () => {
|
||||
it('does not re-serialize a key polled again within the interval', async () => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'orca-offline-'))
|
||||
let now = 0
|
||||
const cache = createCloudOfflineCache({
|
||||
@@ -88,27 +93,24 @@ describe('cloud offline cache', () => {
|
||||
cache.record('env', 'terminal.read', { terminal: 't' }, ok({ terminal: { tail: ['a'] } }))
|
||||
now = 1_000
|
||||
cache.record('env', 'terminal.read', { terminal: 't' }, ok({ terminal: { tail: ['b'] } }))
|
||||
expect(cache.lookup('env', 'terminal.read', { terminal: 't' })?.result).toEqual({
|
||||
expect((await cache.lookup('env', 'terminal.read', { terminal: 't' }))?.result).toEqual({
|
||||
terminal: { tail: ['a'] }
|
||||
})
|
||||
now = 6_000
|
||||
cache.record('env', 'terminal.read', { terminal: 't' }, ok({ terminal: { tail: ['c'] } }))
|
||||
expect(cache.lookup('env', 'terminal.read', { terminal: 't' })?.result).toEqual({
|
||||
expect((await cache.lookup('env', 'terminal.read', { terminal: 't' }))?.result).toEqual({
|
||||
terminal: { tail: ['c'] }
|
||||
})
|
||||
})
|
||||
|
||||
it('evicts the oldest entries past the per-workspace bound and refuses oversized answers', () => {
|
||||
it('evicts the oldest entries past the per-workspace bound and refuses oversized answers', async () => {
|
||||
const { cache, tick } = setup({ maxEntriesPerEnvironment: 3, maxEntryBytes: 200 })
|
||||
for (let i = 0; i < 5; i++) {
|
||||
tick()
|
||||
cache.record('env', 'terminal.read', { terminal: `t${i}` }, ok({ terminal: { tail: ['x'] } }))
|
||||
}
|
||||
expect(
|
||||
cache
|
||||
.entries('env')
|
||||
.map((e) => (e.params as { terminal: string }).terminal)
|
||||
.sort()
|
||||
(await cache.entries('env')).map((e) => stringParam(e.params, 'terminal')).sort()
|
||||
).toEqual(['t2', 't3', 't4'])
|
||||
cache.record(
|
||||
'env',
|
||||
@@ -116,50 +118,53 @@ describe('cloud offline cache', () => {
|
||||
{ terminal: 't4' },
|
||||
ok({ terminal: { tail: ['y'.repeat(500)] } })
|
||||
)
|
||||
expect(cache.lookup('env', 'terminal.read', { terminal: 't4' })).toBeNull()
|
||||
expect(await cache.lookup('env', 'terminal.read', { terminal: 't4' })).toBeNull()
|
||||
})
|
||||
|
||||
it('holds a bounded number of workspaces in memory and still reads evicted ones from disk', () => {
|
||||
it('holds a bounded number of workspaces in memory and still reads evicted ones from disk', async () => {
|
||||
const { cache, tick } = setup({ maxLoadedEnvironments: 2 })
|
||||
for (const env of ['a', 'b', 'c', 'd']) {
|
||||
tick()
|
||||
cache.record(env, 'git.status', { worktree: env }, ok({ branch: env }))
|
||||
}
|
||||
expect(cache.lookup('a', 'git.status', { worktree: 'a' })?.result).toEqual({ branch: 'a' })
|
||||
expect((await cache.lookup('a', 'git.status', { worktree: 'a' }))?.result).toEqual({
|
||||
branch: 'a'
|
||||
})
|
||||
})
|
||||
|
||||
it('evicts whole least-recent workspaces past the total bound, prunes removed ones, and clears on sign-out', () => {
|
||||
it('evicts whole least-recent workspaces past the total bound, prunes removed ones, and clears on sign-out', async () => {
|
||||
const { root, cache, tick } = setup({ maxTotalBytes: 900 })
|
||||
for (const env of ['a', 'b', 'c']) {
|
||||
tick(1000)
|
||||
cache.record(env, 'git.status', { worktree: env }, ok({ entries: ['z'.repeat(300)] }))
|
||||
}
|
||||
await cache.flush()
|
||||
expect(readdirSync(root).length).toBeLessThan(3)
|
||||
expect(cache.lookup('c', 'git.status', { worktree: 'c' })).not.toBeNull()
|
||||
cache.prune(new Set(['c']))
|
||||
expect(await cache.lookup('c', 'git.status', { worktree: 'c' })).not.toBeNull()
|
||||
await cache.prune(new Set(['c']))
|
||||
expect(readdirSync(root)).toHaveLength(1)
|
||||
cache.clear()
|
||||
await cache.clear()
|
||||
expect(existsSync(root)).toBe(false)
|
||||
expect(cache.lookup('c', 'git.status', { worktree: 'c' })).toBeNull()
|
||||
expect(await cache.lookup('c', 'git.status', { worktree: 'c' })).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('legacy plaintext cache files', () => {
|
||||
it('are removed whatever the codec', () => {
|
||||
it('are removed whatever the codec', async () => {
|
||||
for (const sealing of [testCodec, null]) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'orca-offline-'))
|
||||
writeFileSync(
|
||||
join(root, 'abc.json'),
|
||||
'{"version":1,"entries":{"k":{"result":"old plaintext"}}}'
|
||||
)
|
||||
createCloudOfflineCache({ root, codec: sealing, flushDelayMs: 0 })
|
||||
await createCloudOfflineCache({ root, codec: sealing, flushDelayMs: 0 }).flush()
|
||||
expect(readdirSync(root)).toEqual([])
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('offline snapshot', () => {
|
||||
it('builds chats, terminals and git state from cached answers', () => {
|
||||
it('builds chats, terminals and git state from cached answers', async () => {
|
||||
const { cache, tick } = setup()
|
||||
cache.record(
|
||||
'env',
|
||||
@@ -222,7 +227,7 @@ describe('offline snapshot', () => {
|
||||
}
|
||||
})
|
||||
)
|
||||
const snapshot = buildOfflineSnapshot(cache, 'env', 'w', true)
|
||||
const snapshot = await buildOfflineSnapshot(cache, 'env', 'w', true)
|
||||
expect(snapshot.asleep).toBe(true)
|
||||
expect(snapshot.terminals).toEqual([
|
||||
{ handle: 'h1', title: 'Dev server', lines: ['ready on :3000'], at: 1000 }
|
||||
@@ -237,7 +242,8 @@ describe('offline snapshot', () => {
|
||||
['assistant', 'Done.']
|
||||
])
|
||||
expect(snapshot.capturedAt).toBe(1001)
|
||||
const [checkout] = archiveCheckoutsFromCache(cache, 'env', '/vercel/sandbox/project')!
|
||||
const [checkout] =
|
||||
(await archiveCheckoutsFromCache(cache, 'env', '/vercel/sandbox/project')) ?? []
|
||||
expect(checkout).toMatchObject({
|
||||
branch: 'feat',
|
||||
head: 'abcdef1',
|
||||
@@ -245,7 +251,7 @@ describe('offline snapshot', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('strips terminal escapes', () => {
|
||||
it('strips terminal escapes', async () => {
|
||||
expect(stripAnsi('\u001b]0;title\u0007a\u001b[1;31mb\u001b[0m\rc')).toBe('abc')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,28 +3,21 @@
|
||||
// servers; Orca keeps them on the VM (end-to-end encrypted), so the only copy outside the VM is
|
||||
// this one, on the user's own disk, sealed with the OS keychain. Without a keychain nothing is kept.
|
||||
// Bounded per workspace, in total, and in memory; one file per environment.
|
||||
import { createHash } from 'node:crypto'
|
||||
import {
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
writeFileSync
|
||||
} from 'node:fs'
|
||||
import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
decode,
|
||||
DEFAULT_OFFLINE_CACHE_LIMITS,
|
||||
DISABLED_OFFLINE_CACHE,
|
||||
EnvironmentFileSchema,
|
||||
fileName,
|
||||
OFFLINE_READ_METHODS,
|
||||
removeLegacyPlaintextFiles,
|
||||
stableKey,
|
||||
type CloudOfflineCache,
|
||||
type EnvironmentFile,
|
||||
type OfflineCacheCodec,
|
||||
type OfflineCacheEntry,
|
||||
type OfflineCacheLimits,
|
||||
type StoredEntry
|
||||
type OfflineCacheLimits
|
||||
} from './cloud-offline-cache-types'
|
||||
|
||||
export {
|
||||
@@ -37,90 +30,98 @@ export {
|
||||
type OfflineCacheLimits
|
||||
} from './cloud-offline-cache-types'
|
||||
|
||||
function fileName(environmentId: string): string {
|
||||
return `${createHash('sha256').update(environmentId).digest('hex').slice(0, 32)}.bin`
|
||||
}
|
||||
|
||||
function decode(entry: StoredEntry): OfflineCacheEntry {
|
||||
const { resultJson, ...rest } = entry
|
||||
return { ...rest, result: JSON.parse(resultJson) as unknown }
|
||||
}
|
||||
|
||||
const DISABLED: CloudOfflineCache = {
|
||||
record: () => {},
|
||||
lookup: () => null,
|
||||
entries: () => [],
|
||||
forget: () => {},
|
||||
prune: () => {},
|
||||
clear: () => {},
|
||||
flush: () => {}
|
||||
}
|
||||
|
||||
export function createCloudOfflineCache(options: {
|
||||
root: string
|
||||
/** Null: the OS cannot seal files, so nothing is cached. */
|
||||
codec: OfflineCacheCodec | null
|
||||
limits?: Partial<OfflineCacheLimits>
|
||||
now?: () => number
|
||||
/** Write delay; 0 writes synchronously (tests). */
|
||||
/** Write delay; 0 writes as soon as a record lands (tests). */
|
||||
flushDelayMs?: number
|
||||
}): CloudOfflineCache {
|
||||
const codec = options.codec
|
||||
// All file work runs one task at a time, in call order, off the main thread's sync fs path.
|
||||
let io: Promise<unknown> = Promise.resolve()
|
||||
function serial<T>(task: () => Promise<T>): Promise<T> {
|
||||
const run = io.then(task, task)
|
||||
io = run.catch(() => undefined)
|
||||
return run
|
||||
}
|
||||
// Plaintext v1 files from the first build are never read; remove them whatever the codec.
|
||||
removeLegacyPlaintextFiles(options.root)
|
||||
const legacyRemoved = serial(() => removeLegacyPlaintextFiles(options.root))
|
||||
const codec = options.codec
|
||||
if (!codec) {
|
||||
return DISABLED
|
||||
return { ...DISABLED_OFFLINE_CACHE, flush: () => legacyRemoved }
|
||||
}
|
||||
const limits = { ...DEFAULT_OFFLINE_CACHE_LIMITS, ...options.limits }
|
||||
const now = options.now ?? Date.now
|
||||
const flushDelayMs = options.flushDelayMs ?? 1_000
|
||||
// Map insertion order is the LRU order: a touched environment is re-inserted at the end.
|
||||
const loaded = new Map<string, EnvironmentFile>()
|
||||
const loading = new Map<string, Promise<EnvironmentFile>>()
|
||||
const dirty = new Set<string>()
|
||||
// Bumped by forget/prune/clear so a read that started before them is not cached afterwards.
|
||||
let generation = 0
|
||||
let timer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
const pathOf = (environmentId: string) => join(options.root, fileName(environmentId))
|
||||
|
||||
function write(environmentId: string): void {
|
||||
const file = loaded.get(environmentId)
|
||||
if (!file) {
|
||||
return
|
||||
}
|
||||
mkdirSync(options.root, { recursive: true, mode: 0o700 })
|
||||
const target = pathOf(environmentId)
|
||||
const temp = `${target}.${process.pid}.tmp`
|
||||
writeFileSync(temp, codec!.encrypt(JSON.stringify(file)), { mode: 0o600 })
|
||||
renameSync(temp, target)
|
||||
function write(file: EnvironmentFile): Promise<void> {
|
||||
const serialized = codec!.encrypt(JSON.stringify(file))
|
||||
return serial(async () => {
|
||||
await mkdir(options.root, { recursive: true, mode: 0o700 })
|
||||
const target = pathOf(file.environmentId)
|
||||
const temp = `${target}.${process.pid}.tmp`
|
||||
await writeFile(temp, serialized, { mode: 0o600 })
|
||||
await rename(temp, target)
|
||||
})
|
||||
}
|
||||
|
||||
function load(environmentId: string): EnvironmentFile {
|
||||
const hit = loaded.get(environmentId)
|
||||
if (hit) {
|
||||
loaded.delete(environmentId)
|
||||
loaded.set(environmentId, hit)
|
||||
return hit
|
||||
}
|
||||
let file: EnvironmentFile = { version: 2, environmentId, updatedAt: 0, entries: {} }
|
||||
async function readFromDisk(environmentId: string): Promise<EnvironmentFile> {
|
||||
try {
|
||||
const parsed = JSON.parse(
|
||||
codec!.decrypt(readFileSync(pathOf(environmentId)))
|
||||
) as EnvironmentFile
|
||||
if (parsed.version === 2 && parsed.environmentId === environmentId && parsed.entries) {
|
||||
file = parsed
|
||||
const parsed = EnvironmentFileSchema.safeParse(
|
||||
JSON.parse(codec!.decrypt(await readFile(pathOf(environmentId))))
|
||||
)
|
||||
if (parsed.success && parsed.data.environmentId === environmentId) {
|
||||
return parsed.data
|
||||
}
|
||||
} catch {
|
||||
// Missing, unreadable or sealed by another keychain: start empty. A cache is never a source of truth.
|
||||
}
|
||||
loaded.set(environmentId, file)
|
||||
while (loaded.size > limits.maxLoadedEnvironments) {
|
||||
const oldest = loaded.keys().next().value!
|
||||
if (dirty.has(oldest)) {
|
||||
write(oldest)
|
||||
dirty.delete(oldest)
|
||||
}
|
||||
loaded.delete(oldest)
|
||||
return { version: 2, environmentId, updatedAt: 0, entries: {} }
|
||||
}
|
||||
|
||||
function load(environmentId: string): Promise<EnvironmentFile> {
|
||||
const hit = loaded.get(environmentId)
|
||||
if (hit) {
|
||||
loaded.delete(environmentId)
|
||||
loaded.set(environmentId, hit)
|
||||
return Promise.resolve(hit)
|
||||
}
|
||||
return file
|
||||
const pending = loading.get(environmentId)
|
||||
if (pending) {
|
||||
return pending
|
||||
}
|
||||
const startedAt = generation
|
||||
const reading = serial(() => readFromDisk(environmentId)).then((file) => {
|
||||
loading.delete(environmentId)
|
||||
const raced = loaded.get(environmentId)
|
||||
if (raced || startedAt !== generation) {
|
||||
return raced ?? file
|
||||
}
|
||||
loaded.set(environmentId, file)
|
||||
while (loaded.size > limits.maxLoadedEnvironments) {
|
||||
const oldest = loaded.keys().next().value!
|
||||
const evicted = loaded.get(oldest)
|
||||
if (evicted && dirty.has(oldest)) {
|
||||
dirty.delete(oldest)
|
||||
void write(evicted).catch(logWriteFailure)
|
||||
}
|
||||
loaded.delete(oldest)
|
||||
}
|
||||
return file
|
||||
})
|
||||
loading.set(environmentId, reading)
|
||||
return reading
|
||||
}
|
||||
|
||||
function evictWithin(file: EnvironmentFile): void {
|
||||
@@ -138,15 +139,16 @@ export function createCloudOfflineCache(options: {
|
||||
}
|
||||
|
||||
/** Whole environments go least-recently-written first once the total passes its bound. */
|
||||
function evictTotal(): void {
|
||||
async function evictTotal(): Promise<void> {
|
||||
let files: { name: string; size: number; mtime: number }[]
|
||||
try {
|
||||
files = readdirSync(options.root)
|
||||
.filter((name) => name.endsWith('.bin'))
|
||||
.map((name) => {
|
||||
const stat = statSync(join(options.root, name))
|
||||
return { name, size: stat.size, mtime: stat.mtimeMs }
|
||||
const names = (await readdir(options.root)).filter((name) => name.endsWith('.bin'))
|
||||
files = await Promise.all(
|
||||
names.map(async (name) => {
|
||||
const info = await stat(join(options.root, name))
|
||||
return { name, size: info.size, mtime: info.mtimeMs }
|
||||
})
|
||||
)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
@@ -155,7 +157,7 @@ export function createCloudOfflineCache(options: {
|
||||
if (total <= limits.maxTotalBytes) {
|
||||
break
|
||||
}
|
||||
rmSync(join(options.root, file.name), { force: true })
|
||||
await rm(join(options.root, file.name), { force: true })
|
||||
for (const environmentId of loaded.keys()) {
|
||||
if (fileName(environmentId) === file.name) {
|
||||
loaded.delete(environmentId)
|
||||
@@ -165,30 +167,33 @@ export function createCloudOfflineCache(options: {
|
||||
}
|
||||
}
|
||||
|
||||
function flush(): void {
|
||||
function logWriteFailure(error: unknown): void {
|
||||
console.error(`[orca-cloud] offline cache write failed: ${String(error)}`)
|
||||
}
|
||||
|
||||
// Records apply after their environment loads; flush waits for those still landing.
|
||||
const applying = new Set<Promise<void>>()
|
||||
|
||||
async function flush(): Promise<void> {
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
timer = null
|
||||
}
|
||||
for (const environmentId of dirty) {
|
||||
try {
|
||||
write(environmentId)
|
||||
} catch (error) {
|
||||
console.error(`[orca-cloud] offline cache write failed: ${String(error)}`)
|
||||
}
|
||||
}
|
||||
await Promise.all(applying)
|
||||
const files = [...dirty].flatMap((environmentId) => loaded.get(environmentId) ?? [])
|
||||
dirty.clear()
|
||||
evictTotal()
|
||||
await Promise.all(files.map((file) => write(file).catch(logWriteFailure)))
|
||||
await serial(evictTotal)
|
||||
}
|
||||
|
||||
function markDirty(environmentId: string): void {
|
||||
dirty.add(environmentId)
|
||||
if (flushDelayMs === 0) {
|
||||
flush()
|
||||
void flush()
|
||||
return
|
||||
}
|
||||
if (!timer) {
|
||||
timer = setTimeout(flush, flushDelayMs)
|
||||
timer = setTimeout(() => void flush(), flushDelayMs)
|
||||
timer.unref?.()
|
||||
}
|
||||
}
|
||||
@@ -198,81 +203,86 @@ export function createCloudOfflineCache(options: {
|
||||
if (!OFFLINE_READ_METHODS.has(method)) {
|
||||
return
|
||||
}
|
||||
const file = load(environmentId)
|
||||
const key = stableKey(method, params)
|
||||
const existing = file.entries[key]
|
||||
const at = now()
|
||||
if (existing && at - existing.at < limits.minRecordIntervalMs) {
|
||||
return
|
||||
}
|
||||
const resultJson = JSON.stringify(response.result ?? null)
|
||||
const bytes = resultJson.length + key.length
|
||||
if (bytes > limits.maxEntryBytes) {
|
||||
// Too big to keep; don't serve an older, smaller answer as if it were current either.
|
||||
if (existing) {
|
||||
delete file.entries[key]
|
||||
markDirty(environmentId)
|
||||
const applied = load(environmentId).then((file) => {
|
||||
const key = stableKey(method, params)
|
||||
const existing = file.entries[key]
|
||||
if (existing && at - existing.at < limits.minRecordIntervalMs) {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
file.entries[key] = {
|
||||
method,
|
||||
params: params ?? null,
|
||||
resultJson,
|
||||
runtimeId: response._meta.runtimeId,
|
||||
at,
|
||||
bytes
|
||||
}
|
||||
file.updatedAt = at
|
||||
evictWithin(file)
|
||||
markDirty(environmentId)
|
||||
const resultJson = JSON.stringify(response.result ?? null)
|
||||
const bytes = resultJson.length + key.length
|
||||
if (bytes > limits.maxEntryBytes) {
|
||||
// Too big to keep; don't serve an older, smaller answer as if it were current either.
|
||||
if (existing) {
|
||||
delete file.entries[key]
|
||||
markDirty(environmentId)
|
||||
}
|
||||
return
|
||||
}
|
||||
file.entries[key] = {
|
||||
method,
|
||||
params: params ?? null,
|
||||
resultJson,
|
||||
runtimeId: response._meta.runtimeId,
|
||||
at,
|
||||
bytes
|
||||
}
|
||||
file.updatedAt = at
|
||||
evictWithin(file)
|
||||
markDirty(environmentId)
|
||||
})
|
||||
applying.add(applied)
|
||||
void applied.finally(() => applying.delete(applied))
|
||||
},
|
||||
lookup(environmentId, method, params) {
|
||||
async lookup(environmentId, method, params) {
|
||||
if (!OFFLINE_READ_METHODS.has(method)) {
|
||||
return null
|
||||
}
|
||||
const entry = load(environmentId).entries[stableKey(method, params)]
|
||||
await Promise.all(applying)
|
||||
const entry = (await load(environmentId)).entries[stableKey(method, params)]
|
||||
return entry ? decode(entry) : null
|
||||
},
|
||||
entries(environmentId, method) {
|
||||
return Object.values(load(environmentId).entries)
|
||||
async entries(environmentId, method) {
|
||||
await Promise.all(applying)
|
||||
return Object.values((await load(environmentId)).entries)
|
||||
.filter((entry) => method === undefined || entry.method === method)
|
||||
.sort((a, b) => b.at - a.at)
|
||||
.map(decode)
|
||||
},
|
||||
forget(environmentId) {
|
||||
generation++
|
||||
loaded.delete(environmentId)
|
||||
dirty.delete(environmentId)
|
||||
rmSync(pathOf(environmentId), { force: true })
|
||||
return serial(() => rm(pathOf(environmentId), { force: true }))
|
||||
},
|
||||
prune(keep) {
|
||||
const keepFiles = new Set([...keep].map(fileName))
|
||||
let names: string[] = []
|
||||
try {
|
||||
names = readdirSync(options.root)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
for (const name of names) {
|
||||
if (!keepFiles.has(name)) {
|
||||
rmSync(join(options.root, name), { force: true })
|
||||
}
|
||||
}
|
||||
generation++
|
||||
for (const environmentId of loaded.keys()) {
|
||||
if (!keep.has(environmentId)) {
|
||||
loaded.delete(environmentId)
|
||||
dirty.delete(environmentId)
|
||||
}
|
||||
}
|
||||
const keepFiles = new Set([...keep].map(fileName))
|
||||
return serial(async () => {
|
||||
const names = await readdir(options.root).catch(() => [])
|
||||
for (const name of names) {
|
||||
if (!keepFiles.has(name)) {
|
||||
await rm(join(options.root, name), { force: true })
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
clear() {
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
timer = null
|
||||
}
|
||||
generation++
|
||||
loaded.clear()
|
||||
dirty.clear()
|
||||
rmSync(options.root, { recursive: true, force: true })
|
||||
return serial(() => rm(options.root, { recursive: true, force: true }))
|
||||
},
|
||||
flush
|
||||
}
|
||||
|
||||
@@ -26,9 +26,15 @@ import {
|
||||
type CloudOfflineCache,
|
||||
type OfflineCacheCodec
|
||||
} from './cloud-offline-cache'
|
||||
import { createOwedWorkBeat } from './cloud-offline-beat'
|
||||
import { setCloudOfflineReset } from './cloud-offline-reset'
|
||||
import { safeStorageCodec, setCloudOfflineObserver } from './cloud-offline-seam'
|
||||
import { archiveCheckoutsFromCache, buildOfflineSnapshot } from './cloud-offline-snapshot'
|
||||
import {
|
||||
archiveCheckoutsFromCache,
|
||||
buildOfflineSnapshot,
|
||||
stringParam,
|
||||
terminalHandlesOf
|
||||
} from './cloud-offline-snapshot'
|
||||
import { getHostedWorkspaceService, setArchiveCheckoutsProvider } from './hosted-workspace-service'
|
||||
|
||||
type CloudRuntimeInfo = {
|
||||
@@ -40,6 +46,9 @@ type CloudRuntimeInfo = {
|
||||
|
||||
const CAPTURE_BUDGET_MS = 8_000
|
||||
const MAX_CAPTURED_TERMINALS = 12
|
||||
/** Background delivery cadence while prompts are owed; doubles while nothing moves. */
|
||||
const BEAT_MS = 30_000
|
||||
const MAX_BEAT_BACKOFF_STEPS = 4
|
||||
|
||||
export type CloudOfflineService = {
|
||||
cache: CloudOfflineCache
|
||||
@@ -155,9 +164,8 @@ export function installCloudOfflineService(deps: {
|
||||
// Chats the window has read stay current: refresh their tail pages.
|
||||
const sessions = [
|
||||
...new Set(
|
||||
cache
|
||||
.entries(env, 'agentSession.history')
|
||||
.map((entry) => (entry.params as { sessionId?: string } | null)?.sessionId)
|
||||
(await cache.entries(env, 'agentSession.history'))
|
||||
.map((entry) => stringParam(entry.params, 'sessionId'))
|
||||
.filter((id): id is string => typeof id === 'string')
|
||||
)
|
||||
].slice(0, 20)
|
||||
@@ -174,15 +182,11 @@ export function installCloudOfflineService(deps: {
|
||||
)
|
||||
)
|
||||
])
|
||||
const terminals = (
|
||||
(list as { terminals?: { handle: string }[] } | null)?.terminals ?? []
|
||||
).slice(0, MAX_CAPTURED_TERMINALS)
|
||||
const handles = terminalHandlesOf(list).slice(0, MAX_CAPTURED_TERMINALS)
|
||||
await Promise.all(
|
||||
terminals.map((terminal) =>
|
||||
readInto(env, 'terminal.read', { terminal: terminal.handle, limit: 400 }, deadline)
|
||||
)
|
||||
handles.map((terminal) => readInto(env, 'terminal.read', { terminal, limit: 400 }, deadline))
|
||||
)
|
||||
cache.flush()
|
||||
await cache.flush()
|
||||
}
|
||||
|
||||
async function locate(workspaceId: string): Promise<WorkspaceAvailability> {
|
||||
@@ -218,23 +222,43 @@ export function installCloudOfflineService(deps: {
|
||||
return opening === 'up'
|
||||
},
|
||||
call: deps.call,
|
||||
onChange: (workspaceId) => broadcast(CH.queuedPromptsChanged, { workspaceId }),
|
||||
onChange: (workspaceId) => {
|
||||
broadcast(CH.queuedPromptsChanged, { workspaceId })
|
||||
beat.poke()
|
||||
},
|
||||
log
|
||||
})
|
||||
|
||||
// Background: deliver to VMs already up (never wakes) and drop removed workspaces, only while
|
||||
// prompts are owed. A live answer from a workspace (it is up) delivers its prompts at once.
|
||||
const beat = createOwedWorkBeat({
|
||||
nextDueAt: () => queue.nextAttemptAt(),
|
||||
pass: async () => {
|
||||
invalidate()
|
||||
await queue.drive()
|
||||
void cache.prune(new Set(runtimes().byEnvironment.keys()))
|
||||
},
|
||||
baseMs: BEAT_MS,
|
||||
maxBackoffSteps: MAX_BEAT_BACKOFF_STEPS
|
||||
})
|
||||
|
||||
setCloudOfflineObserver((environmentId, method, params, response) => {
|
||||
if (response.ok && runtimes().byEnvironment.has(environmentId)) {
|
||||
const info = runtimes().byEnvironment.get(environmentId)
|
||||
if (response.ok && info) {
|
||||
cache.record(environmentId, method, params, response)
|
||||
if (queue.nextAttemptAt() !== null) {
|
||||
void queue.drive(info.workspaceId)
|
||||
}
|
||||
}
|
||||
})
|
||||
setCloudOfflineReset(() => {
|
||||
cache.clear()
|
||||
void cache.clear()
|
||||
queue.clear()
|
||||
})
|
||||
setArchiveCheckoutsProvider(async (workspaceId) => {
|
||||
const info = runtimes().byWorkspace.get(workspaceId)
|
||||
return info
|
||||
? archiveCheckoutsFromCache(
|
||||
? await archiveCheckoutsFromCache(
|
||||
cache,
|
||||
info.environmentId,
|
||||
info.projectRoot ?? '/vercel/sandbox/project'
|
||||
@@ -247,11 +271,11 @@ export function installCloudOfflineService(deps: {
|
||||
}
|
||||
ipcMain.handle(
|
||||
CH.getSnapshot,
|
||||
(_event, args: { workspaceId: string }): CloudOfflineSnapshot | null => {
|
||||
async (_event, args: { workspaceId: string }): Promise<CloudOfflineSnapshot | null> => {
|
||||
invalidate()
|
||||
const info = runtimes().byWorkspace.get(args.workspaceId)
|
||||
return info
|
||||
? buildOfflineSnapshot(cache, info.environmentId, args.workspaceId, info.asleep)
|
||||
? await buildOfflineSnapshot(cache, info.environmentId, args.workspaceId, info.asleep)
|
||||
: null
|
||||
}
|
||||
)
|
||||
@@ -273,14 +297,8 @@ export function installCloudOfflineService(deps: {
|
||||
queue.edit(args.id, args.text)
|
||||
)
|
||||
|
||||
// Background: deliver to VMs that are already up (never wakes), and drop removed workspaces.
|
||||
const beat = setInterval(() => {
|
||||
invalidate()
|
||||
void queue.drive()
|
||||
cache.prune(new Set(runtimes().byEnvironment.keys()))
|
||||
}, 30_000)
|
||||
beat.unref?.()
|
||||
const startup = setTimeout(() => void queue.drive(), 5_000)
|
||||
// Startup: deliver what survived a restart (never wakes), and drop removed workspaces.
|
||||
const startup = setTimeout(() => void beat.runNow(), 5_000)
|
||||
startup.unref?.()
|
||||
|
||||
return {
|
||||
@@ -288,9 +306,10 @@ export function installCloudOfflineService(deps: {
|
||||
queue,
|
||||
capture,
|
||||
dispose() {
|
||||
clearInterval(beat)
|
||||
beat.dispose()
|
||||
clearTimeout(startup)
|
||||
cache.flush()
|
||||
void cache.flush()
|
||||
void queue.flushed()
|
||||
setCloudOfflineObserver(null)
|
||||
setCloudOfflineReset(null)
|
||||
setArchiveCheckoutsProvider(null)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// Turns cached RPC answers into what the asleep view shows, and into archive git metadata.
|
||||
import { z } from 'zod'
|
||||
import type {
|
||||
CloudOfflineChat,
|
||||
CloudOfflineChatMessage,
|
||||
@@ -26,40 +27,98 @@ export function stripAnsi(text: string): string {
|
||||
return text.replace(ANSI, '').replace(/\r(?!\n)/g, '')
|
||||
}
|
||||
|
||||
type HistoryResult = {
|
||||
ok?: boolean
|
||||
page?: {
|
||||
sessionId?: string
|
||||
items?: {
|
||||
itemId: string
|
||||
observedAt: number
|
||||
body: { kind: string; role?: string; blocks?: { type: string; text?: string }[] }
|
||||
}[]
|
||||
}
|
||||
// Cached answers came from the VM: parse leniently, and a malformed piece is skipped, never trusted.
|
||||
const Text = z.string()
|
||||
const ChatItemSchema = z.looseObject({
|
||||
itemId: Text,
|
||||
observedAt: z.number(),
|
||||
body: z.looseObject({
|
||||
kind: Text,
|
||||
role: Text.optional(),
|
||||
blocks: z.array(z.looseObject({ type: Text, text: z.unknown().optional() })).optional()
|
||||
})
|
||||
})
|
||||
const HistoryResultSchema = z.looseObject({
|
||||
page: z
|
||||
.looseObject({ sessionId: Text.optional(), items: z.array(z.unknown()).optional() })
|
||||
.optional()
|
||||
})
|
||||
const TerminalListSchema = z.looseObject({
|
||||
terminals: z
|
||||
.array(
|
||||
z.looseObject({
|
||||
handle: Text,
|
||||
title: Text.nullable().optional(),
|
||||
paneTitle: Text.optional()
|
||||
})
|
||||
)
|
||||
.catch([])
|
||||
.optional()
|
||||
})
|
||||
// `terminal.read` answers `{terminal: RuntimeTerminalRead}`.
|
||||
const TerminalReadSchema = z.looseObject({
|
||||
terminal: z.looseObject({ handle: Text.optional(), tail: z.array(Text).optional() }).optional()
|
||||
})
|
||||
const StatusResultSchema = z.looseObject({
|
||||
entries: z
|
||||
.array(z.looseObject({ path: Text, status: Text, area: Text }))
|
||||
.catch([])
|
||||
.optional(),
|
||||
head: Text.optional().catch(undefined),
|
||||
branch: Text.optional().catch(undefined)
|
||||
})
|
||||
const HistoryItemsSchema = z.looseObject({
|
||||
items: z
|
||||
.array(
|
||||
z.looseObject({
|
||||
id: Text,
|
||||
subject: Text,
|
||||
author: Text.optional(),
|
||||
timestamp: z.number().optional()
|
||||
})
|
||||
)
|
||||
.catch([])
|
||||
.optional()
|
||||
})
|
||||
const ParamsSchema = z.record(z.string(), z.unknown())
|
||||
|
||||
/** A string field of a cached call's params, if it has one. */
|
||||
export function stringParam(params: unknown, key: string): string | undefined {
|
||||
const parsed = ParamsSchema.safeParse(params)
|
||||
const value = parsed.success ? parsed.data[key] : undefined
|
||||
return typeof value === 'string' ? value : undefined
|
||||
}
|
||||
|
||||
/** Terminal handles from a cached `terminal.list` answer. */
|
||||
export function terminalHandlesOf(result: unknown): string[] {
|
||||
const parsed = TerminalListSchema.safeParse(result)
|
||||
return parsed.success ? (parsed.data.terminals ?? []).map((terminal) => terminal.handle) : []
|
||||
}
|
||||
|
||||
function chatOf(entry: OfflineCacheEntry): CloudOfflineChat | null {
|
||||
const result = entry.result as HistoryResult
|
||||
const sessionId =
|
||||
result?.page?.sessionId ?? (entry.params as { sessionId?: string } | null)?.sessionId
|
||||
if (!sessionId || !Array.isArray(result?.page?.items)) {
|
||||
const parsed = HistoryResultSchema.safeParse(entry.result)
|
||||
const page = parsed.success ? parsed.data.page : undefined
|
||||
const sessionId = page?.sessionId ?? stringParam(entry.params, 'sessionId')
|
||||
if (!sessionId || !Array.isArray(page?.items)) {
|
||||
return null
|
||||
}
|
||||
const messages: CloudOfflineChatMessage[] = []
|
||||
for (const item of result.page.items) {
|
||||
if (item.body?.kind !== 'message' || !Array.isArray(item.body.blocks)) {
|
||||
for (const raw of page.items) {
|
||||
const item = ChatItemSchema.safeParse(raw)
|
||||
if (!item.success || item.data.body.kind !== 'message' || !item.data.body.blocks) {
|
||||
continue
|
||||
}
|
||||
const text = item.body.blocks
|
||||
.filter((block) => block.type === 'text' && typeof block.text === 'string')
|
||||
.map((block) => block.text)
|
||||
const text = item.data.body.blocks
|
||||
.flatMap((block) =>
|
||||
block.type === 'text' && typeof block.text === 'string' ? [block.text] : []
|
||||
)
|
||||
.join('\n')
|
||||
.trim()
|
||||
const role = item.body.role
|
||||
const role = item.data.body.role
|
||||
if (!text || (role !== 'user' && role !== 'assistant' && role !== 'system')) {
|
||||
continue
|
||||
}
|
||||
messages.push({ itemId: item.itemId, role, text, at: item.observedAt })
|
||||
messages.push({ itemId: item.data.itemId, role, text, at: item.data.observedAt })
|
||||
}
|
||||
return { sessionId, at: entry.at, messages: messages.slice(-MAX_MESSAGES) }
|
||||
}
|
||||
@@ -68,7 +127,7 @@ function chatOf(entry: OfflineCacheEntry): CloudOfflineChat | null {
|
||||
function chatsOf(entries: OfflineCacheEntry[]): CloudOfflineChat[] {
|
||||
const bySession = new Map<string, CloudOfflineChat>()
|
||||
for (const entry of entries) {
|
||||
const direction = (entry.params as { direction?: string } | null)?.direction
|
||||
const direction = stringParam(entry.params, 'direction')
|
||||
if (direction && direction !== 'tail') {
|
||||
continue
|
||||
}
|
||||
@@ -85,17 +144,15 @@ function chatsOf(entries: OfflineCacheEntry[]): CloudOfflineChat[] {
|
||||
return [...bySession.values()].sort((a, b) => b.at - a.at)
|
||||
}
|
||||
|
||||
function terminalsOf(cache: CloudOfflineCache, environmentId: string): CloudOfflineTerminal[] {
|
||||
async function terminalsOf(
|
||||
cache: CloudOfflineCache,
|
||||
environmentId: string
|
||||
): Promise<CloudOfflineTerminal[]> {
|
||||
// Every listing contributes titles (a filtered listing can be empty); newest first wins.
|
||||
const titles = new Map<string, string | null>()
|
||||
for (const list of cache.entries(environmentId, 'terminal.list')) {
|
||||
const listed =
|
||||
(
|
||||
list.result as {
|
||||
terminals?: { handle: string; title: string | null; paneTitle?: string }[]
|
||||
} | null
|
||||
)?.terminals ?? []
|
||||
for (const terminal of listed) {
|
||||
for (const list of await cache.entries(environmentId, 'terminal.list')) {
|
||||
const parsed = TerminalListSchema.safeParse(list.result)
|
||||
for (const terminal of parsed.success ? (parsed.data.terminals ?? []) : []) {
|
||||
if (!titles.has(terminal.handle)) {
|
||||
titles.set(terminal.handle, terminal.paneTitle ?? terminal.title ?? null)
|
||||
}
|
||||
@@ -103,12 +160,11 @@ function terminalsOf(cache: CloudOfflineCache, environmentId: string): CloudOffl
|
||||
}
|
||||
const seen = new Set<string>()
|
||||
const terminals: CloudOfflineTerminal[] = []
|
||||
for (const entry of cache.entries(environmentId, 'terminal.read')) {
|
||||
// `terminal.read` answers `{terminal: RuntimeTerminalRead}`.
|
||||
const wrapped = entry.result as { terminal?: { handle?: string; tail?: string[] } } | null
|
||||
const result = wrapped?.terminal
|
||||
const handle = result?.handle ?? (entry.params as { terminal?: string } | null)?.terminal
|
||||
if (!handle || seen.has(handle) || !Array.isArray(result?.tail)) {
|
||||
for (const entry of await cache.entries(environmentId, 'terminal.read')) {
|
||||
const parsed = TerminalReadSchema.safeParse(entry.result)
|
||||
const result = parsed.success ? parsed.data.terminal : undefined
|
||||
const handle = result?.handle ?? stringParam(entry.params, 'terminal')
|
||||
if (!handle || seen.has(handle) || !result?.tail) {
|
||||
continue
|
||||
}
|
||||
seen.add(handle)
|
||||
@@ -122,22 +178,19 @@ function terminalsOf(cache: CloudOfflineCache, environmentId: string): CloudOffl
|
||||
return terminals
|
||||
}
|
||||
|
||||
type StatusResult = {
|
||||
entries?: { path: string; status: string; area: string }[]
|
||||
head?: string
|
||||
branch?: string
|
||||
}
|
||||
type HistoryItems = {
|
||||
items?: { id: string; subject: string; author?: string; timestamp?: number }[]
|
||||
}
|
||||
|
||||
function gitOf(cache: CloudOfflineCache, environmentId: string): CloudOfflineGit | null {
|
||||
const status = cache.entries(environmentId, 'git.status')[0]
|
||||
const history = cache.entries(environmentId, 'git.history')[0]
|
||||
async function gitOf(
|
||||
cache: CloudOfflineCache,
|
||||
environmentId: string
|
||||
): Promise<CloudOfflineGit | null> {
|
||||
const status = (await cache.entries(environmentId, 'git.status'))[0]
|
||||
const history = (await cache.entries(environmentId, 'git.history'))[0]
|
||||
if (!status && !history) {
|
||||
return null
|
||||
}
|
||||
const statusResult = (status?.result ?? {}) as StatusResult
|
||||
const parsedStatus = StatusResultSchema.safeParse(status?.result ?? {})
|
||||
const statusResult = parsedStatus.success ? parsedStatus.data : {}
|
||||
const parsedHistory = HistoryItemsSchema.safeParse(history?.result ?? {})
|
||||
const items = parsedHistory.success ? (parsedHistory.data.items ?? []) : []
|
||||
const entries = statusResult.entries ?? []
|
||||
return {
|
||||
branch: statusResult.branch?.replace(/^refs\/heads\//, '') ?? null,
|
||||
@@ -146,7 +199,7 @@ function gitOf(cache: CloudOfflineCache, environmentId: string): CloudOfflineGit
|
||||
.slice(0, MAX_CHANGES)
|
||||
.map(({ path, status: s, area }) => ({ path, status: s, area })),
|
||||
changeCount: entries.length,
|
||||
commits: (((history?.result ?? {}) as HistoryItems).items ?? []).slice(0, 20).map((item) => ({
|
||||
commits: items.slice(0, 20).map((item) => ({
|
||||
id: item.id,
|
||||
subject: item.subject,
|
||||
author: item.author ?? null,
|
||||
@@ -156,15 +209,15 @@ function gitOf(cache: CloudOfflineCache, environmentId: string): CloudOfflineGit
|
||||
}
|
||||
}
|
||||
|
||||
export function buildOfflineSnapshot(
|
||||
export async function buildOfflineSnapshot(
|
||||
cache: CloudOfflineCache,
|
||||
environmentId: string,
|
||||
workspaceId: string,
|
||||
asleep: boolean
|
||||
): CloudOfflineSnapshot {
|
||||
const git = gitOf(cache, environmentId)
|
||||
const terminals = terminalsOf(cache, environmentId)
|
||||
const chats = chatsOf(cache.entries(environmentId, 'agentSession.history'))
|
||||
): Promise<CloudOfflineSnapshot> {
|
||||
const git = await gitOf(cache, environmentId)
|
||||
const terminals = await terminalsOf(cache, environmentId)
|
||||
const chats = chatsOf(await cache.entries(environmentId, 'agentSession.history'))
|
||||
const times = [git?.at ?? 0, ...terminals.map((t) => t.at), ...chats.map((c) => c.at)].filter(
|
||||
(t) => t > 0
|
||||
)
|
||||
@@ -188,12 +241,12 @@ const STATUS_LETTER: Record<string, string> = {
|
||||
}
|
||||
|
||||
/** Archive metadata from the cache, for archiving a VM that is already asleep. */
|
||||
export function archiveCheckoutsFromCache(
|
||||
export async function archiveCheckoutsFromCache(
|
||||
cache: CloudOfflineCache,
|
||||
environmentId: string,
|
||||
projectRoot: string
|
||||
): HostedArchiveCheckout[] | null {
|
||||
const git = gitOf(cache, environmentId)
|
||||
): Promise<HostedArchiveCheckout[] | null> {
|
||||
const git = await gitOf(cache, environmentId)
|
||||
if (!git || (!git.branch && !git.head)) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
// prepare: pack in the VM → download in chunks → fetch the branch into the local repo.
|
||||
// finish (after the window created the local worktree): apply staged/unstaged diffs, untracked
|
||||
// files, and install Claude/Codex sessions remapped to the new path so native resume works.
|
||||
import { execFile } from 'node:child_process'
|
||||
import { createWriteStream } from 'node:fs'
|
||||
import { cp, lstat, mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises'
|
||||
import { join, relative } from 'node:path'
|
||||
import { promisify } from 'node:util'
|
||||
import { z } from 'zod'
|
||||
import { gitExecFileAsync } from '../git/command-runner/git-exec-file'
|
||||
import {
|
||||
CLOUD_TRANSFER_DIR,
|
||||
CLOUD_TRANSFER_LOG,
|
||||
@@ -24,7 +24,6 @@ import {
|
||||
} from '../../shared/hosted-sandbox/cloud-transfer-pack'
|
||||
import type { RuntimeCall } from './hosted-agent-auto-resume'
|
||||
|
||||
const run = promisify(execFile)
|
||||
const CHUNK_BYTES = 512 * 1024
|
||||
const PACK_TIMEOUT_MS = 10 * 60_000
|
||||
|
||||
@@ -36,10 +35,33 @@ export type Prepared = {
|
||||
}
|
||||
|
||||
async function git(cwd: string, args: string[]): Promise<string> {
|
||||
const { stdout } = await run('git', args, { cwd, maxBuffer: 64 * 1024 * 1024 })
|
||||
const { stdout } = await gitExecFileAsync(args, {
|
||||
cwd,
|
||||
maxBuffer: 64 * 1024 * 1024,
|
||||
// Fetching a large bundle can outlast the default read deadline.
|
||||
timeout: PACK_TIMEOUT_MS
|
||||
})
|
||||
return stdout.trim()
|
||||
}
|
||||
|
||||
// orcad RPC replies, validated instead of trusted: the VM side is not this process.
|
||||
const TerminalCreatedSchema = z.looseObject({
|
||||
terminal: z.union([z.string(), z.looseObject({ handle: z.string().optional() })]).optional()
|
||||
})
|
||||
const FileStatSchema = z.looseObject({ size: z.number().optional() })
|
||||
const FileChunkSchema = z.looseObject({
|
||||
contentBase64: z.string(),
|
||||
bytesRead: z.number().optional(),
|
||||
eof: z.boolean().optional()
|
||||
})
|
||||
|
||||
async function readChunk(
|
||||
call: RuntimeCall,
|
||||
params: { worktree: string; relativePath: string; offset: number; length: number }
|
||||
): Promise<z.infer<typeof FileChunkSchema>> {
|
||||
return FileChunkSchema.parse(await call('files.readChunk', params))
|
||||
}
|
||||
|
||||
export async function fileSize(path: string): Promise<number> {
|
||||
return stat(path)
|
||||
.then((s) => s.size)
|
||||
@@ -48,6 +70,14 @@ export async function fileSize(path: string): Promise<number> {
|
||||
|
||||
export type WorktreeRow = { id?: unknown; path?: unknown }
|
||||
|
||||
/** `worktree.list` reply: rows stay loose and pickCloudWorktree checks their fields. */
|
||||
export const WorktreeListingSchema = z.looseObject({
|
||||
worktrees: z
|
||||
.array(z.looseObject({ id: z.unknown(), path: z.unknown() }))
|
||||
.optional()
|
||||
.catch(undefined)
|
||||
})
|
||||
|
||||
/** The orcad-side worktree for this workspace: its id, else the VM project root, else the only one. */
|
||||
export function pickCloudWorktree(
|
||||
rows: WorktreeRow[],
|
||||
@@ -75,14 +105,16 @@ async function runInCloud(
|
||||
commandLine: string,
|
||||
title: string
|
||||
): Promise<void> {
|
||||
const created = (await call('terminal.create', {
|
||||
worktree,
|
||||
command: commandLine,
|
||||
title,
|
||||
presentation: 'background',
|
||||
focus: false,
|
||||
activate: false
|
||||
})) as { terminal?: { handle?: string } | string }
|
||||
const created = TerminalCreatedSchema.parse(
|
||||
await call('terminal.create', {
|
||||
worktree,
|
||||
command: commandLine,
|
||||
title,
|
||||
presentation: 'background',
|
||||
focus: false,
|
||||
activate: false
|
||||
})
|
||||
)
|
||||
const handle = typeof created.terminal === 'string' ? created.terminal : created.terminal?.handle
|
||||
if (!handle) {
|
||||
throw new Error('Could not run a command in the cloud workspace.')
|
||||
@@ -127,16 +159,9 @@ async function readCloudTail(
|
||||
worktree: string,
|
||||
relativePath: string
|
||||
): Promise<string> {
|
||||
const stat = (await call('files.stat', { worktree, relativePath })) as { size?: number }
|
||||
const stat = FileStatSchema.parse(await call('files.stat', { worktree, relativePath }))
|
||||
const offset = Math.max(0, (stat.size ?? 0) - 8192)
|
||||
const page = (await call('files.readChunk', {
|
||||
worktree,
|
||||
relativePath,
|
||||
offset,
|
||||
length: 8192
|
||||
})) as {
|
||||
contentBase64: string
|
||||
}
|
||||
const page = await readChunk(call, { worktree, relativePath, offset, length: 8192 })
|
||||
return Buffer.from(page.contentBase64, 'base64').toString('utf8')
|
||||
}
|
||||
|
||||
@@ -155,14 +180,7 @@ async function readCloudText(
|
||||
worktree: string,
|
||||
relativePath: string
|
||||
): Promise<string> {
|
||||
const page = (await call('files.readChunk', {
|
||||
worktree,
|
||||
relativePath,
|
||||
offset: 0,
|
||||
length: CHUNK_BYTES
|
||||
})) as {
|
||||
contentBase64: string
|
||||
}
|
||||
const page = await readChunk(call, { worktree, relativePath, offset: 0, length: CHUNK_BYTES })
|
||||
return Buffer.from(page.contentBase64, 'base64').toString('utf8')
|
||||
}
|
||||
|
||||
@@ -178,16 +196,7 @@ export async function downloadCloudFile(
|
||||
let offset = 0
|
||||
try {
|
||||
for (;;) {
|
||||
const page = (await call('files.readChunk', {
|
||||
worktree,
|
||||
relativePath,
|
||||
offset,
|
||||
length: CHUNK_BYTES
|
||||
})) as {
|
||||
contentBase64: string
|
||||
bytesRead: number
|
||||
eof: boolean
|
||||
}
|
||||
const page = await readChunk(call, { worktree, relativePath, offset, length: CHUNK_BYTES })
|
||||
const bytes = Buffer.from(page.contentBase64, 'base64')
|
||||
if (!out.write(bytes)) {
|
||||
await new Promise<void>((resolve) => out.once('drain', () => resolve()))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { execFileSync } from 'node:child_process'
|
||||
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { mkdirSync, mkdtempSync, readFileSync, utimesSync, writeFileSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
@@ -128,9 +128,12 @@ describe('cloud transfer housekeeping', () => {
|
||||
mkdirSync(join(root, 'abandoned'))
|
||||
writeFileSync(join(root, 'abandoned', 'transfer.tar'), 'x')
|
||||
mkdirSync(join(root, 'kept', 'unapplied'), { recursive: true })
|
||||
expect(await sweepCloudTransfers(root, Date.now())).toBe(1)
|
||||
expect(await sweepCloudTransfers(root, Date.now() + 31 * 86_400_000)).toBe(1)
|
||||
expect(await sweepCloudTransfers(join(root, 'missing'), Date.now())).toBe(0)
|
||||
// Fixed times: the leftovers were written at T0; the sweep runs a minute and a month later.
|
||||
const T0 = 1_800_000_000_000
|
||||
utimesSync(join(root, 'kept', 'unapplied'), T0 / 1000, T0 / 1000)
|
||||
expect(await sweepCloudTransfers(root, T0 + 60_000)).toBe(1)
|
||||
expect(await sweepCloudTransfers(root, T0 + 31 * 86_400_000)).toBe(1)
|
||||
expect(await sweepCloudTransfers(join(root, 'missing'), T0)).toBe(0)
|
||||
})
|
||||
|
||||
it('installs only session transcripts, never other files from the project folder', async () => {
|
||||
|
||||
@@ -33,7 +33,7 @@ import {
|
||||
packInCloud,
|
||||
pickCloudWorktree,
|
||||
type Prepared,
|
||||
type WorktreeRow
|
||||
WorktreeListingSchema
|
||||
} from './cloud-transfer-steps'
|
||||
import { createHostedRuntimeEnvironmentCall } from './hosted-runtime-environment-call'
|
||||
import { findCloudRuntime } from './hosted-workspace-service'
|
||||
@@ -112,7 +112,7 @@ export function installCloudTransferHandlers(deps: {
|
||||
let selector: string | null = null
|
||||
try {
|
||||
step('packing')
|
||||
const listing = (await call('worktree.list', {})) as { worktrees?: WorktreeRow[] }
|
||||
const listing = WorktreeListingSchema.parse(await call('worktree.list', {}))
|
||||
const connection = runtime.recipeResult
|
||||
? getEphemeralVmRecipeResultConnection(runtime.recipeResult)
|
||||
: null
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
createHostedEnvironmentBuildFollowers,
|
||||
type HostedEnvironmentBuildFollowers
|
||||
} from './hosted-environment-build-followers'
|
||||
import {
|
||||
BUILD_ID,
|
||||
ENV_ID,
|
||||
buildView,
|
||||
line,
|
||||
ndjsonBody
|
||||
} from './hosted-environment-fixtures.test-support'
|
||||
|
||||
const finishedBuild = async () => buildView({ status: 'succeeded' })
|
||||
|
||||
let followers: HostedEnvironmentBuildFollowers | null = null
|
||||
afterEach(() => followers?.dispose())
|
||||
|
||||
async function drain(
|
||||
f: HostedEnvironmentBuildFollowers,
|
||||
client: Parameters<HostedEnvironmentBuildFollowers['poll']>[0]
|
||||
): Promise<{ text: string; status: string | undefined; polls: number }> {
|
||||
let cursor = 0
|
||||
let text = ''
|
||||
for (let polls = 1; polls < 50; polls++) {
|
||||
const result = await f.poll(client, ENV_ID, BUILD_ID, cursor, 1_000)
|
||||
text += result.chunks.map((chunk) => chunk.data).join('')
|
||||
cursor = result.cursor
|
||||
if (result.done) {
|
||||
return { text, status: result.build?.status, polls }
|
||||
}
|
||||
}
|
||||
throw new Error('never finished')
|
||||
}
|
||||
|
||||
describe('createHostedEnvironmentBuildFollowers', () => {
|
||||
it('long-polls the log and skips what a reconnect replays', async () => {
|
||||
let pass = 0
|
||||
const client = {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async () =>
|
||||
pass++ === 0
|
||||
? ndjsonBody([
|
||||
line({ type: 'build', build: buildView() }),
|
||||
line({ type: 'log', stream: 'stdout', data: 'one\n' }),
|
||||
line({ type: 'log', stream: 'stdout', data: 'tw' })
|
||||
])
|
||||
: ndjsonBody([
|
||||
line({ type: 'build', build: buildView() }),
|
||||
line({ type: 'log', stream: 'stdout', data: 'one\ntwo\n' }),
|
||||
line({ type: 'log', stream: 'stderr', data: 'three\n' }),
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }),
|
||||
line({ type: 'end' })
|
||||
])
|
||||
}
|
||||
followers = createHostedEnvironmentBuildFollowers({ backoffMs: [1] })
|
||||
const result = await drain(followers, client)
|
||||
expect(result.text).toBe('one\ntwo\nthree\n')
|
||||
expect(result.status).toBe('succeeded')
|
||||
})
|
||||
|
||||
it('reports a watch that gave up as done with its error', async () => {
|
||||
followers = createHostedEnvironmentBuildFollowers()
|
||||
const client = {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async () => {
|
||||
const { HostedEnvironmentRequestError } = await import('./hosted-environment-client')
|
||||
throw new HostedEnvironmentRequestError('not_found', null, 'gone', 404)
|
||||
}
|
||||
}
|
||||
let cursor = 0
|
||||
for (;;) {
|
||||
const result = await followers.poll(client, ENV_ID, BUILD_ID, cursor, 1_000)
|
||||
cursor = result.cursor
|
||||
if (result.done) {
|
||||
expect(result.error).toBe('That environment or build no longer exists.')
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('reuses one watch for concurrent pollers of the same build', async () => {
|
||||
let opens = 0
|
||||
const client = {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async () => {
|
||||
opens++
|
||||
return ndjsonBody([
|
||||
line({ type: 'build', build: buildView({ status: 'failed', error: 'install_failed' }) }),
|
||||
line({ type: 'log', stream: 'stdout', data: 'x' }),
|
||||
line({ type: 'end' })
|
||||
])
|
||||
}
|
||||
}
|
||||
followers = createHostedEnvironmentBuildFollowers()
|
||||
const [a, b] = await Promise.all([drain(followers, client), drain(followers, client)])
|
||||
expect(a.text).toBe('x')
|
||||
expect(b.text).toBe('x')
|
||||
expect(opens).toBe(1)
|
||||
})
|
||||
|
||||
it('restarts from the first byte when a cursor outlived its follower', async () => {
|
||||
const client = {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async () =>
|
||||
ndjsonBody([
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }),
|
||||
line({ type: 'log', stream: 'stdout', data: 'a' }),
|
||||
line({ type: 'log', stream: 'stdout', data: 'b' }),
|
||||
line({ type: 'end' })
|
||||
])
|
||||
}
|
||||
followers = createHostedEnvironmentBuildFollowers()
|
||||
// A CLI still holding cursor 40 from a follower that was swept and recreated.
|
||||
const result = await followers.poll(client, ENV_ID, BUILD_ID, 40, 1_000)
|
||||
expect(result.truncated).toBe(true)
|
||||
let text = result.chunks.map((chunk) => chunk.data).join('')
|
||||
let cursor = result.cursor
|
||||
let done = result.done
|
||||
while (!done) {
|
||||
const next = await followers.poll(client, ENV_ID, BUILD_ID, cursor, 1_000)
|
||||
expect(next.truncated).toBe(false)
|
||||
text += next.chunks.map((chunk) => chunk.data).join('')
|
||||
cursor = next.cursor
|
||||
done = next.done
|
||||
}
|
||||
expect(text).toBe('ab')
|
||||
})
|
||||
|
||||
it('caps the log buffered across every follower', async () => {
|
||||
const chunk = 'x'.repeat(100)
|
||||
const client = {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async () =>
|
||||
ndjsonBody([
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }),
|
||||
...Array.from({ length: 10 }, () => line({ type: 'log', stream: 'stdout', data: chunk })),
|
||||
line({ type: 'end' })
|
||||
])
|
||||
}
|
||||
followers = createHostedEnvironmentBuildFollowers({ maxTotalBufferedChars: 500 })
|
||||
const other = 'bld_fedcba9876543210'
|
||||
// Let both watches finish before reading, so both buffers filled up.
|
||||
await followers.poll(client, ENV_ID, BUILD_ID, 0, 0)
|
||||
await followers.poll(client, ENV_ID, other, 0, 0)
|
||||
const [a, b] = await vi.waitFor(async () => {
|
||||
const both = await Promise.all([
|
||||
followers!.poll(client, ENV_ID, BUILD_ID, 0, 0),
|
||||
followers!.poll(client, ENV_ID, other, 0, 0)
|
||||
])
|
||||
expect(both.every((result) => result.done)).toBe(true)
|
||||
return both
|
||||
})
|
||||
const total = [...a.chunks, ...b.chunks].reduce((sum, entry) => sum + entry.data.length, 0)
|
||||
expect(total).toBeLessThanOrEqual(500)
|
||||
expect(a.truncated || b.truncated).toBe(true)
|
||||
// Each keeps at least its newest chunk.
|
||||
expect(a.chunks.length).toBeGreaterThan(0)
|
||||
expect(b.chunks.length).toBeGreaterThan(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,282 @@
|
||||
// Pull-based build following for the CLI: the runtime RPC is request/response, so main keeps one
|
||||
// watch per build, buffers its log (bounded), and answers cursor polls. Reconnect replays are
|
||||
// deduplicated here, so a CLI tailing the log never prints a line twice.
|
||||
import type { HostedEnvironmentBuildView } from '../../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
import { watchHostedEnvironmentBuild } from './hosted-environment-build-watch'
|
||||
import type { HostedEnvironmentClient } from './hosted-environment-client'
|
||||
|
||||
export type HostedEnvironmentBuildLogChunk = { stream: 'stdout' | 'stderr'; data: string }
|
||||
|
||||
export type HostedEnvironmentBuildFollowPoll = {
|
||||
build: HostedEnvironmentBuildView | null
|
||||
chunks: HostedEnvironmentBuildLogChunk[]
|
||||
/** Pass back on the next poll. */
|
||||
cursor: number
|
||||
/** Output was lost between polls: the cursor fell behind the buffer (older log dropped), or the
|
||||
* follower was recreated and the log restarts from its first byte (some lines may repeat). */
|
||||
truncated: boolean
|
||||
/** Final status reached and the whole log delivered (or the watch failed: see `error`). */
|
||||
done: boolean
|
||||
error: string | null
|
||||
}
|
||||
|
||||
type Follower = {
|
||||
chunks: { seq: number; chunk: HostedEnvironmentBuildLogChunk }[]
|
||||
bufferedChars: number
|
||||
nextSeq: number
|
||||
build: HostedEnvironmentBuildView | null
|
||||
done: boolean
|
||||
error: string | null
|
||||
lastPollAt: number
|
||||
stop: () => void
|
||||
waiters: Set<() => void>
|
||||
}
|
||||
|
||||
export type HostedEnvironmentBuildFollowers = {
|
||||
poll(
|
||||
client: Pick<HostedEnvironmentClient, 'openBuildEvents' | 'getBuild'>,
|
||||
envId: string,
|
||||
buildId: string,
|
||||
cursor: number,
|
||||
waitMs: number
|
||||
): Promise<HostedEnvironmentBuildFollowPoll>
|
||||
dispose(): void
|
||||
}
|
||||
|
||||
const MAX_BUFFERED_CHARS = 4 * 1024 * 1024
|
||||
// Across every follower: 16 followers at their own cap would otherwise hold 64 MB.
|
||||
const MAX_TOTAL_BUFFERED_CHARS = 16 * 1024 * 1024
|
||||
const MAX_CHUNKS_PER_POLL = 2_000
|
||||
const MAX_FOLLOWERS = 16
|
||||
// A CLI that stopped polling (Ctrl-C) lets its follower go after this.
|
||||
const ABANDONED_AFTER_MS = 60_000
|
||||
|
||||
export function createHostedEnvironmentBuildFollowers(
|
||||
options: {
|
||||
now?: () => number
|
||||
idleTimeoutMs?: number
|
||||
backoffMs?: readonly number[]
|
||||
maxTotalBufferedChars?: number
|
||||
} = {}
|
||||
): HostedEnvironmentBuildFollowers {
|
||||
const now = options.now ?? Date.now
|
||||
const maxTotal = options.maxTotalBufferedChars ?? MAX_TOTAL_BUFFERED_CHARS
|
||||
const followers = new Map<string, Follower>()
|
||||
let totalChars = 0
|
||||
|
||||
const dropOldest = (follower: Follower): boolean => {
|
||||
// The newest chunk stays so a poller always has something to resume from.
|
||||
if (follower.chunks.length <= 1) {
|
||||
return false
|
||||
}
|
||||
const dropped = follower.chunks.shift()
|
||||
const size = dropped?.chunk.data.length ?? 0
|
||||
follower.bufferedChars -= size
|
||||
totalChars -= size
|
||||
return true
|
||||
}
|
||||
|
||||
const enforceTotalCap = (): void => {
|
||||
while (totalChars > maxTotal) {
|
||||
let largest: Follower | null = null
|
||||
for (const candidate of followers.values()) {
|
||||
if (
|
||||
candidate.chunks.length > 1 &&
|
||||
(!largest || candidate.bufferedChars > largest.bufferedChars)
|
||||
) {
|
||||
largest = candidate
|
||||
}
|
||||
}
|
||||
if (!largest || !dropOldest(largest)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const remove = (key: string): void => {
|
||||
const follower = followers.get(key)
|
||||
if (follower) {
|
||||
followers.delete(key)
|
||||
totalChars -= follower.bufferedChars
|
||||
follower.stop()
|
||||
for (const wake of follower.waiters) {
|
||||
wake()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const sweep = (): void => {
|
||||
for (const [key, follower] of followers) {
|
||||
if (now() - follower.lastPollAt > ABANDONED_AFTER_MS) {
|
||||
remove(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const makeRoom = (): void => {
|
||||
while (followers.size >= MAX_FOLLOWERS) {
|
||||
const oldest = followers.keys().next().value
|
||||
if (oldest === undefined) {
|
||||
break
|
||||
}
|
||||
remove(oldest)
|
||||
}
|
||||
}
|
||||
|
||||
// Followers whose CLI went away keep streaming until swept, so sweep on a slow beat too.
|
||||
// Runs only while followers exist and stops once the last one is swept.
|
||||
let sweeper: ReturnType<typeof setTimeout> | null = null
|
||||
const ensureSweeper = (): void => {
|
||||
if (sweeper) {
|
||||
return
|
||||
}
|
||||
sweeper = setTimeout(() => {
|
||||
sweeper = null
|
||||
sweep()
|
||||
if (followers.size > 0) {
|
||||
ensureSweeper()
|
||||
}
|
||||
}, ABANDONED_AFTER_MS / 2)
|
||||
sweeper.unref?.()
|
||||
}
|
||||
|
||||
const start = (
|
||||
client: Pick<HostedEnvironmentClient, 'openBuildEvents' | 'getBuild'>,
|
||||
envId: string,
|
||||
buildId: string
|
||||
): Follower => {
|
||||
const follower: Follower = {
|
||||
chunks: [],
|
||||
bufferedChars: 0,
|
||||
nextSeq: 0,
|
||||
build: null,
|
||||
done: false,
|
||||
error: null,
|
||||
lastPollAt: now(),
|
||||
stop: () => undefined,
|
||||
waiters: new Set()
|
||||
}
|
||||
// Chars of the log already buffered across passes, and how far the current replay has reached.
|
||||
let deliveredChars = 0
|
||||
let replayChars = 0
|
||||
const wake = (): void => {
|
||||
for (const resolve of follower.waiters) {
|
||||
resolve()
|
||||
}
|
||||
follower.waiters.clear()
|
||||
}
|
||||
const push = (chunk: HostedEnvironmentBuildLogChunk): void => {
|
||||
follower.chunks.push({ seq: follower.nextSeq++, chunk })
|
||||
follower.bufferedChars += chunk.data.length
|
||||
totalChars += chunk.data.length
|
||||
while (follower.bufferedChars > MAX_BUFFERED_CHARS && dropOldest(follower)) {
|
||||
// Trimmed one chunk.
|
||||
}
|
||||
enforceTotalCap()
|
||||
}
|
||||
const watch = watchHostedEnvironmentBuild({
|
||||
client,
|
||||
envId,
|
||||
buildId,
|
||||
...(options.idleTimeoutMs ? { idleTimeoutMs: options.idleTimeoutMs } : {}),
|
||||
...(options.backoffMs ? { backoffMs: options.backoffMs } : {}),
|
||||
onEvent: (event) => {
|
||||
switch (event.type) {
|
||||
case 'build':
|
||||
follower.build = event.build
|
||||
break
|
||||
case 'reset':
|
||||
replayChars = 0
|
||||
break
|
||||
case 'reconnecting':
|
||||
break
|
||||
case 'log': {
|
||||
// Skip the part of the replay already delivered, then pass the rest through.
|
||||
const start = replayChars
|
||||
replayChars += event.data.length
|
||||
if (replayChars <= deliveredChars) {
|
||||
return
|
||||
}
|
||||
const fresh =
|
||||
start >= deliveredChars ? event.data : event.data.slice(deliveredChars - start)
|
||||
deliveredChars = replayChars
|
||||
push({ stream: event.stream, data: fresh })
|
||||
break
|
||||
}
|
||||
case 'end':
|
||||
follower.done = true
|
||||
break
|
||||
case 'error':
|
||||
follower.done = true
|
||||
follower.error = event.message
|
||||
break
|
||||
}
|
||||
wake()
|
||||
}
|
||||
})
|
||||
follower.stop = watch.stop
|
||||
return follower
|
||||
}
|
||||
|
||||
return {
|
||||
async poll(client, envId, buildId, requestedCursor, waitMs) {
|
||||
const key = `${envId}/${buildId}`
|
||||
sweep()
|
||||
ensureSweeper()
|
||||
let follower = followers.get(key)
|
||||
if (!follower) {
|
||||
makeRoom()
|
||||
follower = start(client, envId, buildId)
|
||||
followers.set(key, follower)
|
||||
}
|
||||
follower.lastPollAt = now()
|
||||
// A cursor from a previous follower (swept or evicted, then recreated) points past this one's
|
||||
// log: restart from its first byte and say output may repeat.
|
||||
const restarted = requestedCursor > follower.nextSeq
|
||||
const cursor = restarted ? 0 : requestedCursor
|
||||
const hasNews = (): boolean => follower.done || (follower.chunks.at(-1)?.seq ?? -1) >= cursor
|
||||
if (!hasNews() && waitMs > 0) {
|
||||
await new Promise<void>((resolve) => {
|
||||
const timer = setTimeout(done, waitMs)
|
||||
timer.unref?.()
|
||||
function done(): void {
|
||||
clearTimeout(timer)
|
||||
follower!.waiters.delete(done)
|
||||
resolve()
|
||||
}
|
||||
follower!.waiters.add(done)
|
||||
})
|
||||
}
|
||||
const first = follower.chunks[0]?.seq ?? follower.nextSeq
|
||||
const truncated = restarted || cursor < first
|
||||
const pending = follower.chunks.filter((entry) => entry.seq >= cursor)
|
||||
const page = pending.slice(0, MAX_CHUNKS_PER_POLL)
|
||||
const last = page.at(-1)
|
||||
const nextCursor = last ? last.seq + 1 : Math.max(cursor, first)
|
||||
const drained = page.length === pending.length
|
||||
const result: HostedEnvironmentBuildFollowPoll = {
|
||||
build: follower.build,
|
||||
chunks: page.map((entry) => entry.chunk),
|
||||
cursor: nextCursor,
|
||||
truncated,
|
||||
done: follower.done && drained,
|
||||
error: follower.done && drained ? follower.error : null
|
||||
}
|
||||
if (result.done) {
|
||||
// Keep it briefly so a retried final poll still answers; the sweep drops it later.
|
||||
follower.lastPollAt = now() - ABANDONED_AFTER_MS + 5_000
|
||||
}
|
||||
return result
|
||||
},
|
||||
dispose() {
|
||||
if (sweeper) {
|
||||
clearTimeout(sweeper)
|
||||
sweeper = null
|
||||
}
|
||||
// Deleting the current key while iterating a Map is safe.
|
||||
for (const key of followers.keys()) {
|
||||
remove(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,366 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import type { OrcaCloudEnvironmentBuildStreamEvent } from '../../../shared/hosted-sandbox/hosted-environment-api'
|
||||
import { watchHostedEnvironmentBuild } from './hosted-environment-build-watch'
|
||||
import { HostedEnvironmentRequestError } from './hosted-environment-client'
|
||||
import {
|
||||
BUILD_ID,
|
||||
ENV_ID,
|
||||
buildView,
|
||||
line,
|
||||
ndjsonBody
|
||||
} from './hosted-environment-fixtures.test-support'
|
||||
|
||||
const noSleep = async (): Promise<void> => undefined
|
||||
// The build route the idle probe asks; by default the build already finished (no vouching).
|
||||
const finishedBuild = async () => buildView({ status: 'succeeded' })
|
||||
|
||||
function summarize(events: OrcaCloudEnvironmentBuildStreamEvent[]): string[] {
|
||||
return events.map((event) => {
|
||||
switch (event.type) {
|
||||
case 'build':
|
||||
return `build:${event.build.status}`
|
||||
case 'log':
|
||||
return `log:${event.stream}:${event.data}`
|
||||
case 'error':
|
||||
return `error:${event.message}`
|
||||
case 'end':
|
||||
case 'reset':
|
||||
case 'reconnecting':
|
||||
return event.type
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
describe('watchHostedEnvironmentBuild', () => {
|
||||
it('parses lines split across chunks and stops at end once the build is final', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
const openBuildEvents = vi.fn(async () =>
|
||||
ndjsonBody([
|
||||
line({ type: 'build', build: buildView() }).slice(0, 20),
|
||||
line({ type: 'build', build: buildView() }).slice(20),
|
||||
`${line({ type: 'log', stream: 'stdout', data: 'hello\n', at: 5 })}not json\n`,
|
||||
line({ type: 'log', stream: 'stderr', data: 'warn\n' }),
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }),
|
||||
line({ type: 'end' })
|
||||
])
|
||||
)
|
||||
const watch = watchHostedEnvironmentBuild({
|
||||
client: { openBuildEvents, getBuild: finishedBuild },
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => events.push(event),
|
||||
sleep: noSleep
|
||||
})
|
||||
await watch.done
|
||||
expect(summarize(events)).toEqual([
|
||||
'build:running',
|
||||
'log:stdout:hello\n',
|
||||
'log:stderr:warn\n',
|
||||
'build:succeeded',
|
||||
'end'
|
||||
])
|
||||
expect(events[1]).toMatchObject({ at: 5 })
|
||||
expect(openBuildEvents).toHaveBeenCalledTimes(1)
|
||||
expect(openBuildEvents).toHaveBeenCalledWith(ENV_ID, BUILD_ID, expect.any(AbortSignal))
|
||||
})
|
||||
|
||||
it('reconnects with a reset when the stream drops before a final status', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
const passes = [
|
||||
[
|
||||
line({ type: 'build', build: buildView() }),
|
||||
line({ type: 'log', stream: 'stdout', data: 'a' })
|
||||
],
|
||||
// The control plane ended early (build still booting): follow again.
|
||||
[
|
||||
line({ type: 'build', build: buildView() }),
|
||||
line({ type: 'error', error: 'stream_failed' })
|
||||
],
|
||||
[
|
||||
line({ type: 'build', build: buildView() }),
|
||||
line({ type: 'log', stream: 'stdout', data: 'ab' }),
|
||||
line({
|
||||
type: 'build',
|
||||
build: buildView({ status: 'failed', failedStep: 'install', error: 'install_failed' })
|
||||
}),
|
||||
line({ type: 'end' })
|
||||
]
|
||||
]
|
||||
const sleep = vi.fn(noSleep)
|
||||
let pass = 0
|
||||
const watch = watchHostedEnvironmentBuild({
|
||||
client: { openBuildEvents: async () => ndjsonBody(passes[pass++]), getBuild: finishedBuild },
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => events.push(event),
|
||||
sleep
|
||||
})
|
||||
await watch.done
|
||||
expect(summarize(events)).toEqual([
|
||||
'build:running',
|
||||
'log:stdout:a',
|
||||
'reconnecting',
|
||||
'reset',
|
||||
'build:running',
|
||||
'reconnecting',
|
||||
'reset',
|
||||
'build:running',
|
||||
'log:stdout:ab',
|
||||
'build:failed',
|
||||
'end'
|
||||
])
|
||||
expect(sleep).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('treats an end before a final status as a drop', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
let pass = 0
|
||||
const watch = watchHostedEnvironmentBuild({
|
||||
client: {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async () =>
|
||||
ndjsonBody(
|
||||
pass++ === 0
|
||||
? [
|
||||
line({ type: 'build', build: buildView({ status: 'starting' }) }),
|
||||
line({ type: 'end' })
|
||||
]
|
||||
: [
|
||||
line({ type: 'build', build: buildView({ status: 'canceled' }) }),
|
||||
line({ type: 'end' })
|
||||
]
|
||||
)
|
||||
},
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => events.push(event),
|
||||
sleep: noSleep
|
||||
})
|
||||
await watch.done
|
||||
expect(summarize(events)).toEqual([
|
||||
'build:starting',
|
||||
'reconnecting',
|
||||
'reset',
|
||||
'build:canceled',
|
||||
'end'
|
||||
])
|
||||
})
|
||||
|
||||
it('reconnects after the idle timeout when the build route says the build finished', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
let pass = 0
|
||||
const watch = watchHostedEnvironmentBuild({
|
||||
client: {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async (_env, _build, signal) =>
|
||||
pass++ === 0
|
||||
? ndjsonBody([line({ type: 'build', build: buildView() })], { hang: true, signal })
|
||||
: ndjsonBody([
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }),
|
||||
line({ type: 'end' })
|
||||
])
|
||||
},
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => events.push(event),
|
||||
idleTimeoutMs: 30,
|
||||
sleep: noSleep
|
||||
})
|
||||
await watch.done
|
||||
expect(summarize(events)).toEqual([
|
||||
'build:running',
|
||||
'reconnecting',
|
||||
'reset',
|
||||
'build:succeeded',
|
||||
'end'
|
||||
])
|
||||
})
|
||||
|
||||
it('gives up on a final refusal and after repeated drops without progress', async () => {
|
||||
const refused: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
await watchHostedEnvironmentBuild({
|
||||
client: {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async () => {
|
||||
throw new HostedEnvironmentRequestError('not_found', null, 'gone', 404)
|
||||
}
|
||||
},
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => refused.push(event),
|
||||
sleep: noSleep
|
||||
}).done
|
||||
expect(summarize(refused)).toEqual(['error:That environment or build no longer exists.'])
|
||||
|
||||
const flaky: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
const openBuildEvents = vi.fn(async () => {
|
||||
throw new HostedEnvironmentRequestError('unavailable', null, 'down')
|
||||
})
|
||||
await watchHostedEnvironmentBuild({
|
||||
client: { openBuildEvents, getBuild: finishedBuild },
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => flaky.push(event),
|
||||
maxStalledReconnects: 3,
|
||||
sleep: noSleep
|
||||
}).done
|
||||
expect(openBuildEvents).toHaveBeenCalledTimes(4)
|
||||
expect(flaky.map((event) => event.type)).toEqual([
|
||||
'reconnecting',
|
||||
'reconnecting',
|
||||
'reconnecting',
|
||||
'error'
|
||||
])
|
||||
})
|
||||
|
||||
it('stops quietly on stop() while the stream is open', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
const watch = watchHostedEnvironmentBuild({
|
||||
client: {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async (_env, _build, signal) =>
|
||||
ndjsonBody([line({ type: 'build', build: buildView() })], { hang: true, signal })
|
||||
},
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => {
|
||||
events.push(event)
|
||||
watch.stop()
|
||||
},
|
||||
sleep: noSleep
|
||||
})
|
||||
await watch.done
|
||||
expect(summarize(events)).toEqual(['build:running'])
|
||||
})
|
||||
|
||||
it('drops a line longer than the bound instead of buffering it', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
const huge = 'x'.repeat(1024 * 1024 + 10)
|
||||
await watchHostedEnvironmentBuild({
|
||||
client: {
|
||||
getBuild: finishedBuild,
|
||||
openBuildEvents: async () =>
|
||||
ndjsonBody([
|
||||
huge,
|
||||
`${huge}\n`,
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }),
|
||||
line({ type: 'end' })
|
||||
])
|
||||
},
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => events.push(event),
|
||||
sleep: noSleep
|
||||
}).done
|
||||
expect(summarize(events)).toEqual(['build:succeeded', 'end'])
|
||||
})
|
||||
|
||||
it('treats heartbeats as liveness and never forwards them', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
const getBuild = vi.fn(finishedBuild)
|
||||
const encoder = new TextEncoder()
|
||||
const openBuildEvents = vi.fn(async () => {
|
||||
let beats = 0
|
||||
return new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(line({ type: 'build', build: buildView() })))
|
||||
},
|
||||
// Six heartbeats 15 ms apart span three idle windows of 30 ms without a single log line.
|
||||
async pull(controller) {
|
||||
// ast-grep-ignore: test-no-real-time-wait -- the heartbeats must arrive in real time to straddle the stream's idle window.
|
||||
await new Promise((resolve) => setTimeout(resolve, 15))
|
||||
if (beats++ < 6) {
|
||||
controller.enqueue(encoder.encode(line({ type: 'heartbeat', at: beats })))
|
||||
return
|
||||
}
|
||||
controller.enqueue(
|
||||
encoder.encode(
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }) +
|
||||
line({ type: 'end' })
|
||||
)
|
||||
)
|
||||
controller.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
await watchHostedEnvironmentBuild({
|
||||
client: { openBuildEvents, getBuild },
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => events.push(event),
|
||||
idleTimeoutMs: 30,
|
||||
sleep: noSleep
|
||||
}).done
|
||||
expect(summarize(events)).toEqual(['build:running', 'build:succeeded', 'end'])
|
||||
expect(openBuildEvents).toHaveBeenCalledTimes(1)
|
||||
expect(getBuild).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps a silent stream open while the build route says the build is running', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
const getBuild = vi.fn(async () => buildView())
|
||||
const encoder = new TextEncoder()
|
||||
const openBuildEvents = vi.fn(
|
||||
async () =>
|
||||
new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(line({ type: 'build', build: buildView() })))
|
||||
// An old Worker: no heartbeat, then output after two idle windows.
|
||||
setTimeout(() => {
|
||||
controller.enqueue(
|
||||
encoder.encode(
|
||||
line({ type: 'log', stream: 'stdout', data: 'done installing\n' }) +
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }) +
|
||||
line({ type: 'end' })
|
||||
)
|
||||
)
|
||||
controller.close()
|
||||
}, 70)
|
||||
}
|
||||
})
|
||||
)
|
||||
await watchHostedEnvironmentBuild({
|
||||
client: { openBuildEvents, getBuild },
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => events.push(event),
|
||||
idleTimeoutMs: 30,
|
||||
sleep: noSleep
|
||||
}).done
|
||||
expect(summarize(events)).toEqual([
|
||||
'build:running',
|
||||
'log:stdout:done installing\n',
|
||||
'build:succeeded',
|
||||
'end'
|
||||
])
|
||||
expect(openBuildEvents).toHaveBeenCalledTimes(1)
|
||||
expect(getBuild).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('never gives up on a quiet build the API keeps vouching for', async () => {
|
||||
const events: OrcaCloudEnvironmentBuildStreamEvent[] = []
|
||||
let opens = 0
|
||||
const openBuildEvents = vi.fn(async (_env: string, _build: string, signal: AbortSignal) =>
|
||||
// Every pass replays the same status and no new bytes, then goes silent.
|
||||
++opens <= 6
|
||||
? ndjsonBody([line({ type: 'build', build: buildView() })], { hang: true, signal })
|
||||
: ndjsonBody([
|
||||
line({ type: 'build', build: buildView({ status: 'succeeded' }) }),
|
||||
line({ type: 'end' })
|
||||
])
|
||||
)
|
||||
await watchHostedEnvironmentBuild({
|
||||
client: { openBuildEvents, getBuild: async () => buildView() },
|
||||
envId: ENV_ID,
|
||||
buildId: BUILD_ID,
|
||||
onEvent: (event) => events.push(event),
|
||||
idleTimeoutMs: 5,
|
||||
maxQuietProbes: 1,
|
||||
maxStalledReconnects: 2,
|
||||
sleep: noSleep
|
||||
}).done
|
||||
expect(openBuildEvents).toHaveBeenCalledTimes(7)
|
||||
expect(events.at(-1)).toEqual({ type: 'end' })
|
||||
expect(events.some((event) => event.type === 'error')).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,321 @@
|
||||
// Follows one environment build's ndjson event stream until the build is final. The control plane
|
||||
// always replays the log from byte 0, so a reconnect first emits `reset` for consumers to clear.
|
||||
import type { OrcaCloudEnvironmentBuildStreamEvent } from '../../../shared/hosted-sandbox/hosted-environment-api'
|
||||
import { hostedEnvironmentErrorMessage } from '../../../shared/hosted-sandbox/hosted-environment-errors'
|
||||
import {
|
||||
HostedEnvironmentBuildStreamLineSchema,
|
||||
isTerminalHostedEnvironmentBuildStatus,
|
||||
type HostedEnvironmentBuildStatus
|
||||
} from '../../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
import {
|
||||
HostedEnvironmentRequestError,
|
||||
type HostedEnvironmentClient
|
||||
} from './hosted-environment-client'
|
||||
|
||||
export type HostedEnvironmentBuildWatch = {
|
||||
stop(): void
|
||||
/** Settles after the final event (`end` or `error`) or after stop(). */
|
||||
done: Promise<void>
|
||||
}
|
||||
|
||||
export type HostedEnvironmentBuildWatchOptions = {
|
||||
client: Pick<HostedEnvironmentClient, 'openBuildEvents' | 'getBuild'>
|
||||
envId: string
|
||||
buildId: string
|
||||
onEvent: (event: OrcaCloudEnvironmentBuildStreamEvent) => void
|
||||
/** No bytes (heartbeats included) for this long: ask the build route whether it is still alive. */
|
||||
idleTimeoutMs?: number
|
||||
/** Quiet periods in a row the build route vouched for before reopening the stream anyway. */
|
||||
maxQuietProbes?: number
|
||||
/** Delay before reconnect attempt N (1-based), clamped to the last entry. */
|
||||
backoffMs?: readonly number[]
|
||||
/** Reconnects in a row that brought no new status or log before giving up. */
|
||||
maxStalledReconnects?: number
|
||||
sleep?: (ms: number, signal: AbortSignal) => Promise<void>
|
||||
}
|
||||
|
||||
const DEFAULT_IDLE_TIMEOUT_MS = 90_000
|
||||
const DEFAULT_BACKOFF_MS = [1_000, 2_000, 5_000, 10_000, 30_000] as const
|
||||
const DEFAULT_MAX_STALLED_RECONNECTS = 12
|
||||
// Older control planes send no heartbeat: a silent install is normal, so a quiet stream whose build
|
||||
// the API still reports as running is kept; only after this many quiet periods is it reopened.
|
||||
const DEFAULT_MAX_QUIET_PROBES = 3
|
||||
// One ndjson line holds one log chunk; anything bigger is dropped rather than buffered.
|
||||
const MAX_LINE_CHARS = 1024 * 1024
|
||||
const FATAL_CODES = new Set([
|
||||
'not_found',
|
||||
'forbidden',
|
||||
'unauthorized',
|
||||
'invalid_request',
|
||||
'not_configured',
|
||||
'entitlement_denied'
|
||||
])
|
||||
|
||||
type PassOutcome =
|
||||
| { kind: 'end' }
|
||||
| { kind: 'fatal'; message: string }
|
||||
| { kind: 'dropped' }
|
||||
| { kind: 'stopped' }
|
||||
|
||||
export function watchHostedEnvironmentBuild(
|
||||
options: HostedEnvironmentBuildWatchOptions
|
||||
): HostedEnvironmentBuildWatch {
|
||||
const idleTimeoutMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS
|
||||
const backoff = options.backoffMs ?? DEFAULT_BACKOFF_MS
|
||||
const maxStalled = options.maxStalledReconnects ?? DEFAULT_MAX_STALLED_RECONNECTS
|
||||
const sleep = options.sleep ?? abortableSleep
|
||||
const maxQuietProbes = options.maxQuietProbes ?? DEFAULT_MAX_QUIET_PROBES
|
||||
const stopper = new AbortController()
|
||||
|
||||
let lastStatus: HostedEnvironmentBuildStatus | null = null
|
||||
let emittedAny = false
|
||||
// Progress is measured against the longest replay so far: a replay of the same bytes is no progress.
|
||||
let bestLogChars = 0
|
||||
let passLogChars = 0
|
||||
let passStatusChanged = false
|
||||
// The build route confirmed the build alive during a quiet pass: its reconnect is not a stall.
|
||||
let passVouched = false
|
||||
|
||||
const emit = (event: OrcaCloudEnvironmentBuildStreamEvent): void => {
|
||||
if (stopper.signal.aborted) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
options.onEvent(event)
|
||||
} catch (error) {
|
||||
console.warn('[cloud-environments] build event listener threw:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const handleLine = (line: string): 'end' | 'error' | null => {
|
||||
if (!line.trim()) {
|
||||
return null
|
||||
}
|
||||
let raw: unknown
|
||||
try {
|
||||
raw = JSON.parse(line)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
const parsed = HostedEnvironmentBuildStreamLineSchema.safeParse(raw)
|
||||
if (!parsed.success) {
|
||||
return null
|
||||
}
|
||||
const value = parsed.data
|
||||
switch (value.type) {
|
||||
case 'build':
|
||||
if (value.build.status !== lastStatus) {
|
||||
passStatusChanged = true
|
||||
}
|
||||
lastStatus = value.build.status
|
||||
emittedAny = true
|
||||
emit({ type: 'build', build: value.build })
|
||||
return null
|
||||
case 'log':
|
||||
passLogChars += value.data.length
|
||||
emittedAny = true
|
||||
emit({
|
||||
type: 'log',
|
||||
stream: value.stream,
|
||||
data: value.data,
|
||||
...(value.at === undefined ? {} : { at: value.at })
|
||||
})
|
||||
return null
|
||||
case 'heartbeat':
|
||||
// The read already re-armed the idle timer; nothing for consumers.
|
||||
return null
|
||||
case 'end':
|
||||
return 'end'
|
||||
case 'error':
|
||||
return 'error'
|
||||
}
|
||||
}
|
||||
|
||||
const runPass = async (): Promise<PassOutcome> => {
|
||||
passLogChars = 0
|
||||
passStatusChanged = false
|
||||
passVouched = false
|
||||
const pass = new AbortController()
|
||||
const onStop = (): void => pass.abort()
|
||||
stopper.signal.addEventListener('abort', onStop, { once: true })
|
||||
let idle: ReturnType<typeof setTimeout> | null = null
|
||||
let quietProbes = 0
|
||||
// A quiet stream is not a dead one: ask the build route before tearing the stream down.
|
||||
const onIdle = async (): Promise<void> => {
|
||||
idle = null
|
||||
if (pass.signal.aborted) {
|
||||
return
|
||||
}
|
||||
quietProbes += 1
|
||||
const build = await options.client
|
||||
.getBuild(options.envId, options.buildId, pass.signal)
|
||||
.catch(() => null)
|
||||
if (pass.signal.aborted) {
|
||||
return
|
||||
}
|
||||
const alive = build !== null && !isTerminalHostedEnvironmentBuildStatus(build.status)
|
||||
if (alive) {
|
||||
passVouched = true
|
||||
}
|
||||
if (alive && build.status === lastStatus && quietProbes < maxQuietProbes) {
|
||||
armIdle(false)
|
||||
return
|
||||
}
|
||||
// Finished, changed status, unreachable, or quiet too long: reopen to replay the truth.
|
||||
pass.abort()
|
||||
}
|
||||
const armIdle = (fromData = true): void => {
|
||||
if (idle) {
|
||||
clearTimeout(idle)
|
||||
}
|
||||
if (fromData) {
|
||||
quietProbes = 0
|
||||
}
|
||||
idle = setTimeout(() => void onIdle(), idleTimeoutMs)
|
||||
idle.unref?.()
|
||||
}
|
||||
try {
|
||||
armIdle()
|
||||
let body: ReadableStream<Uint8Array>
|
||||
try {
|
||||
body = await options.client.openBuildEvents(options.envId, options.buildId, pass.signal)
|
||||
} catch (error) {
|
||||
if (stopper.signal.aborted) {
|
||||
return { kind: 'stopped' }
|
||||
}
|
||||
if (error instanceof HostedEnvironmentRequestError && FATAL_CODES.has(error.code)) {
|
||||
return { kind: 'fatal', message: hostedEnvironmentErrorMessage(error.code, error.reason) }
|
||||
}
|
||||
return { kind: 'dropped' }
|
||||
}
|
||||
if (emittedAny) {
|
||||
emit({ type: 'reset' })
|
||||
}
|
||||
const reader = body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffered = ''
|
||||
let discarding = false
|
||||
try {
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) {
|
||||
break
|
||||
}
|
||||
armIdle()
|
||||
buffered += decoder.decode(value, { stream: true })
|
||||
let newline = buffered.indexOf('\n')
|
||||
while (newline !== -1) {
|
||||
const line = buffered.slice(0, newline)
|
||||
buffered = buffered.slice(newline + 1)
|
||||
if (discarding) {
|
||||
discarding = false
|
||||
} else {
|
||||
const verdict = handleLine(line)
|
||||
if (verdict === 'end') {
|
||||
return lastStatus && isTerminalHostedEnvironmentBuildStatus(lastStatus)
|
||||
? { kind: 'end' }
|
||||
: // Ended before a final status (still booting): follow again.
|
||||
{ kind: 'dropped' }
|
||||
}
|
||||
if (verdict === 'error') {
|
||||
return { kind: 'dropped' }
|
||||
}
|
||||
}
|
||||
newline = buffered.indexOf('\n')
|
||||
}
|
||||
if (buffered.length > MAX_LINE_CHARS) {
|
||||
buffered = ''
|
||||
discarding = true
|
||||
}
|
||||
}
|
||||
const tail = discarding ? '' : buffered + decoder.decode()
|
||||
if (
|
||||
tail &&
|
||||
handleLine(tail) === 'end' &&
|
||||
lastStatus &&
|
||||
isTerminalHostedEnvironmentBuildStatus(lastStatus)
|
||||
) {
|
||||
return { kind: 'end' }
|
||||
}
|
||||
} catch {
|
||||
// Aborted by idle timer or stop, or the socket broke.
|
||||
} finally {
|
||||
reader.cancel().catch(() => undefined)
|
||||
}
|
||||
return stopper.signal.aborted ? { kind: 'stopped' } : { kind: 'dropped' }
|
||||
} finally {
|
||||
if (idle) {
|
||||
clearTimeout(idle)
|
||||
}
|
||||
stopper.signal.removeEventListener('abort', onStop)
|
||||
pass.abort()
|
||||
}
|
||||
}
|
||||
|
||||
const run = async (): Promise<void> => {
|
||||
let stalled = 0
|
||||
for (;;) {
|
||||
const outcome = await runPass()
|
||||
if (outcome.kind === 'stopped' || stopper.signal.aborted) {
|
||||
return
|
||||
}
|
||||
if (outcome.kind === 'end') {
|
||||
emit({ type: 'end' })
|
||||
return
|
||||
}
|
||||
if (outcome.kind === 'fatal') {
|
||||
emit({ type: 'error', message: outcome.message })
|
||||
return
|
||||
}
|
||||
// A replay of the same status and bytes is no progress, unless the API vouched the build alive.
|
||||
const progressed = passStatusChanged || passVouched || passLogChars > bestLogChars
|
||||
bestLogChars = Math.max(bestLogChars, passLogChars)
|
||||
stalled = progressed ? 1 : stalled + 1
|
||||
if (stalled > maxStalled) {
|
||||
emit({
|
||||
type: 'error',
|
||||
message: 'Lost the connection to this build’s log. Reopen it to retry.'
|
||||
})
|
||||
return
|
||||
}
|
||||
emit({ type: 'reconnecting' })
|
||||
try {
|
||||
await sleep(backoff[Math.min(stalled, backoff.length) - 1] ?? 1_000, stopper.signal)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
if (stopper.signal.aborted) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const done = run().catch((error: unknown) => {
|
||||
console.warn('[cloud-environments] build watch failed:', error)
|
||||
emit({ type: 'error', message: 'Could not follow this build.' })
|
||||
})
|
||||
|
||||
return {
|
||||
stop: () => stopper.abort(),
|
||||
done
|
||||
}
|
||||
}
|
||||
|
||||
function abortableSleep(ms: number, signal: AbortSignal): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (signal.aborted) {
|
||||
reject(new Error('aborted'))
|
||||
return
|
||||
}
|
||||
const timer = setTimeout(() => {
|
||||
signal.removeEventListener('abort', onAbort)
|
||||
resolve()
|
||||
}, ms)
|
||||
timer.unref?.()
|
||||
const onAbort = (): void => {
|
||||
clearTimeout(timer)
|
||||
reject(new Error('aborted'))
|
||||
}
|
||||
signal.addEventListener('abort', onAbort, { once: true })
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
followCloudEnvironmentBuild,
|
||||
resolveCloudEnvironment,
|
||||
setCloudEnvironmentInstallScript,
|
||||
showCloudEnvironment
|
||||
} from './hosted-environment-cli-operations'
|
||||
import type { HostedEnvironmentBuildFollowers } from './hosted-environment-build-followers'
|
||||
import type { HostedEnvironmentClient } from './hosted-environment-client'
|
||||
import { toHostedEnvironmentError } from './hosted-environment-service'
|
||||
import {
|
||||
BUILD_ID,
|
||||
ENV_ID,
|
||||
buildView,
|
||||
environmentEntry
|
||||
} from './hosted-environment-fixtures.test-support'
|
||||
|
||||
const OTHER_ID = 'env_ffffffffffffffffffff'
|
||||
|
||||
function fakeClient(overrides: Partial<HostedEnvironmentClient> = {}): HostedEnvironmentClient {
|
||||
const client: HostedEnvironmentClient = {
|
||||
list: vi.fn(async () => ({
|
||||
environments: [environmentEntry(), environmentEntry({ id: OTHER_ID, name: 'Mobile' })],
|
||||
canAdmin: true
|
||||
})),
|
||||
get: vi.fn(async () => ({
|
||||
environment: environmentEntry(),
|
||||
builds: [buildView({ number: 4, id: 'bld_aaaaaaaaaaaaaaaa' }), buildView()],
|
||||
canAdmin: true
|
||||
})),
|
||||
create: vi.fn(),
|
||||
update: vi.fn(async () => environmentEntry({ revision: 3 })),
|
||||
remove: vi.fn(),
|
||||
startBuild: vi.fn(),
|
||||
getBuild: vi.fn(),
|
||||
cancelBuild: vi.fn(),
|
||||
activateBuild: vi.fn(),
|
||||
staleness: vi.fn(async () => {
|
||||
throw new Error('github down')
|
||||
}),
|
||||
openBuildEvents: vi.fn(),
|
||||
...overrides
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
describe('hosted environment CLI operations', () => {
|
||||
it('picks the first environment by default, else by id or case-insensitive name', async () => {
|
||||
const client = fakeClient()
|
||||
expect((await resolveCloudEnvironment(client, undefined)).entry.id).toBe(ENV_ID)
|
||||
expect((await resolveCloudEnvironment(client, OTHER_ID)).entry.name).toBe('Mobile')
|
||||
expect((await resolveCloudEnvironment(client, 'mobile')).entry.id).toBe(OTHER_ID)
|
||||
const missing = await resolveCloudEnvironment(client, 'nope').catch((e: unknown) => e)
|
||||
expect(toHostedEnvironmentError(missing).message).toBe(
|
||||
'No environment named "nope". Known: Web, Mobile.'
|
||||
)
|
||||
})
|
||||
|
||||
it('sends only the install script, guarded by the revision it read', async () => {
|
||||
const client = fakeClient()
|
||||
await setCloudEnvironmentInstallScript(client, undefined, 'npm ci\n')
|
||||
expect(client.update).toHaveBeenCalledWith(ENV_ID, {
|
||||
installScript: 'npm ci\n',
|
||||
expectedRevision: 2
|
||||
})
|
||||
})
|
||||
|
||||
it('shows the environment even when the staleness check fails', async () => {
|
||||
const data = await showCloudEnvironment(fakeClient(), undefined)
|
||||
expect(data.staleness).toBeNull()
|
||||
expect(data.environment).not.toHaveProperty('activeBuild')
|
||||
expect(data.environments).toEqual([
|
||||
{ id: ENV_ID, name: 'Web' },
|
||||
{ id: OTHER_ID, name: 'Mobile' }
|
||||
])
|
||||
})
|
||||
|
||||
it('resolves a build number to its id before following', async () => {
|
||||
const poll = vi.fn(async () => ({
|
||||
build: null,
|
||||
chunks: [],
|
||||
cursor: 0,
|
||||
truncated: false,
|
||||
done: false,
|
||||
error: null
|
||||
}))
|
||||
const followers: HostedEnvironmentBuildFollowers = { poll, dispose: vi.fn() }
|
||||
const client = fakeClient()
|
||||
await followCloudEnvironmentBuild(client, followers, { build: '3' })
|
||||
expect(poll).toHaveBeenLastCalledWith(client, ENV_ID, BUILD_ID, 0, 0)
|
||||
const unknown = await followCloudEnvironmentBuild(client, followers, { build: '99' }).catch(
|
||||
(e: unknown) => e
|
||||
)
|
||||
expect(toHostedEnvironmentError(unknown).code).toBe('not_found')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,156 @@
|
||||
// What `orca cloud env ...` may do, resolved against the control plane. Deliberately narrow: an agent
|
||||
// configuring the environment reads it, replaces the install script and runs builds. Repos, env var
|
||||
// names, machine size and secrets stay with people in Settings.
|
||||
import type {
|
||||
CloudEnvironmentBuildsData,
|
||||
CloudEnvironmentFollowData,
|
||||
CloudEnvironmentRef,
|
||||
CloudEnvironmentSetInstallScriptData,
|
||||
CloudEnvironmentShowData,
|
||||
CloudEnvironmentStartBuildData
|
||||
} from '../../../shared/hosted-sandbox/hosted-environment-cli-contract'
|
||||
import {
|
||||
isHostedEnvironmentBuildId,
|
||||
type HostedEnvironmentListEntry
|
||||
} from '../../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
import type { HostedEnvironmentBuildFollowers } from './hosted-environment-build-followers'
|
||||
import {
|
||||
HostedEnvironmentUserError,
|
||||
type HostedEnvironmentClient
|
||||
} from './hosted-environment-client'
|
||||
|
||||
const knownEnvironmentNames = new Map<string, string>()
|
||||
|
||||
export async function resolveCloudEnvironment(
|
||||
client: Pick<HostedEnvironmentClient, 'list'>,
|
||||
selector: string | undefined
|
||||
): Promise<{ entry: HostedEnvironmentListEntry; all: CloudEnvironmentRef[]; canAdmin: boolean }> {
|
||||
const { environments, canAdmin } = await client.list()
|
||||
const all = environments.map((environment) => ({ id: environment.id, name: environment.name }))
|
||||
knownEnvironmentNames.clear()
|
||||
for (const environment of all) {
|
||||
knownEnvironmentNames.set(environment.id, environment.name)
|
||||
}
|
||||
if (environments.length === 0) {
|
||||
throw new HostedEnvironmentUserError(
|
||||
'not_found',
|
||||
'no_environments',
|
||||
'This org has no environment yet. Create one in Settings > Orca Cloud.'
|
||||
)
|
||||
}
|
||||
if (!selector) {
|
||||
return { entry: environments[0], all, canAdmin }
|
||||
}
|
||||
const lowered = selector.toLowerCase()
|
||||
const entry =
|
||||
environments.find((environment) => environment.id === selector) ??
|
||||
environments.find((environment) => environment.name.toLowerCase() === lowered)
|
||||
if (!entry) {
|
||||
throw new HostedEnvironmentUserError(
|
||||
'not_found',
|
||||
'environment_not_found',
|
||||
`No environment named "${selector.slice(0, 128)}". Known: ${all.map((env) => env.name).join(', ')}.`
|
||||
)
|
||||
}
|
||||
return { entry, all, canAdmin }
|
||||
}
|
||||
|
||||
const ref = (entry: { id: string; name: string }): CloudEnvironmentRef => ({
|
||||
id: entry.id,
|
||||
name: entry.name
|
||||
})
|
||||
|
||||
export async function showCloudEnvironment(
|
||||
client: HostedEnvironmentClient,
|
||||
selector: string | undefined,
|
||||
refresh = false
|
||||
): Promise<CloudEnvironmentShowData> {
|
||||
const { entry, all, canAdmin } = await resolveCloudEnvironment(client, selector)
|
||||
const { activeBuild, latestBuild, ...environment } = entry
|
||||
// Staleness asks GitHub; a failure there should not hide the environment itself.
|
||||
const staleness = await client.staleness(entry.id, refresh).catch(() => null)
|
||||
return { environment, activeBuild, latestBuild, staleness, canAdmin, environments: all }
|
||||
}
|
||||
|
||||
export async function setCloudEnvironmentInstallScript(
|
||||
client: HostedEnvironmentClient,
|
||||
selector: string | undefined,
|
||||
script: string,
|
||||
expectedRevision?: number
|
||||
): Promise<CloudEnvironmentSetInstallScriptData> {
|
||||
const { entry } = await resolveCloudEnvironment(client, selector)
|
||||
// Only the script: this surface must never carry repos, env var names or machine size.
|
||||
const environment = await client.update(entry.id, {
|
||||
installScript: script,
|
||||
expectedRevision: expectedRevision ?? entry.revision
|
||||
})
|
||||
return { environment }
|
||||
}
|
||||
|
||||
export async function startCloudEnvironmentBuild(
|
||||
client: HostedEnvironmentClient,
|
||||
selector: string | undefined
|
||||
): Promise<CloudEnvironmentStartBuildData> {
|
||||
const { entry } = await resolveCloudEnvironment(client, selector)
|
||||
return { environment: ref(entry), build: await client.startBuild(entry.id) }
|
||||
}
|
||||
|
||||
export async function listCloudEnvironmentBuilds(
|
||||
client: HostedEnvironmentClient,
|
||||
selector: string | undefined
|
||||
): Promise<CloudEnvironmentBuildsData> {
|
||||
const { entry } = await resolveCloudEnvironment(client, selector)
|
||||
const detail = await client.get(entry.id)
|
||||
return { environment: ref(entry), builds: detail.builds }
|
||||
}
|
||||
|
||||
export async function followCloudEnvironmentBuild(
|
||||
client: HostedEnvironmentClient,
|
||||
followers: HostedEnvironmentBuildFollowers,
|
||||
args: { env?: string; build: string; cursor?: number; waitMs?: number }
|
||||
): Promise<CloudEnvironmentFollowData> {
|
||||
// Polls repeat every few seconds: once the CLI holds both ids, skip the list round trip.
|
||||
const envId = args.env
|
||||
const known = envId ? knownEnvironmentNames.get(envId) : undefined
|
||||
const environment =
|
||||
envId && known !== undefined && isHostedEnvironmentBuildId(args.build)
|
||||
? { id: envId, name: known }
|
||||
: ref((await resolveCloudEnvironment(client, args.env)).entry)
|
||||
const buildId = await resolveBuildId(client, environment.id, args.build)
|
||||
const poll = await followers.poll(
|
||||
client,
|
||||
environment.id,
|
||||
buildId,
|
||||
args.cursor ?? 0,
|
||||
args.waitMs ?? 0
|
||||
)
|
||||
return { environment, ...poll }
|
||||
}
|
||||
|
||||
async function resolveBuildId(
|
||||
client: Pick<HostedEnvironmentClient, 'get'>,
|
||||
envId: string,
|
||||
selector: string
|
||||
): Promise<string> {
|
||||
if (isHostedEnvironmentBuildId(selector)) {
|
||||
return selector
|
||||
}
|
||||
const number = /^#?(\d{1,9})$/.exec(selector)?.[1]
|
||||
if (!number) {
|
||||
throw new HostedEnvironmentUserError(
|
||||
'invalid_request',
|
||||
'build',
|
||||
'Pass a build number (e.g. 12) or a build id (bld_...).'
|
||||
)
|
||||
}
|
||||
const { builds } = await client.get(envId)
|
||||
const build = builds.find((candidate) => candidate.number === Number(number))
|
||||
if (!build) {
|
||||
throw new HostedEnvironmentUserError(
|
||||
'not_found',
|
||||
'build_not_found',
|
||||
`Build #${number} is not among the 30 most recent builds; pass its id instead.`
|
||||
)
|
||||
}
|
||||
return build.id
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { EnvironmentProviderError } from '../../environment-providers/environment-provider-driver'
|
||||
import {
|
||||
createHostedEnvironmentClient,
|
||||
HostedEnvironmentRequestError
|
||||
} from './hosted-environment-client'
|
||||
import { toHostedEnvironmentError } from './hosted-environment-service'
|
||||
import {
|
||||
BUILD_ID,
|
||||
ENV_ID,
|
||||
buildView,
|
||||
environmentEntry
|
||||
} from './hosted-environment-fixtures.test-support'
|
||||
|
||||
const BASE = 'https://control.example.test'
|
||||
|
||||
function client(handler: (url: URL, init: RequestInit) => Response | Promise<Response>) {
|
||||
const fetchMock = vi.fn(async (input: URL, init: RequestInit) => handler(new URL(input), init))
|
||||
return {
|
||||
fetchMock,
|
||||
client: createHostedEnvironmentClient({
|
||||
baseUrl: BASE,
|
||||
getAccessToken: async () => 'tok',
|
||||
fetch: fetchMock
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const json = (body: unknown, status = 200): Response =>
|
||||
new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } })
|
||||
|
||||
describe('createHostedEnvironmentClient', () => {
|
||||
it('lists environments with the bearer token and never follows redirects', async () => {
|
||||
const { client: c, fetchMock } = client(() =>
|
||||
json({ environments: [{ ...environmentEntry(), futureField: 1 }], canAdmin: true })
|
||||
)
|
||||
const result = await c.list()
|
||||
expect(result.canAdmin).toBe(true)
|
||||
expect(result.environments[0].name).toBe('Web')
|
||||
const [url, init] = fetchMock.mock.calls[0]
|
||||
expect(String(url)).toBe(`${BASE}/v1/environments`)
|
||||
expect(init?.redirect).toBe('manual')
|
||||
expect(init?.headers).toMatchObject({ authorization: 'Bearer tok' })
|
||||
})
|
||||
|
||||
it('maps each route to its method and path', async () => {
|
||||
const calls: string[] = []
|
||||
const { client: c } = client((url, init) => {
|
||||
calls.push(`${init.method} ${url.pathname}${url.search}`)
|
||||
if (url.pathname.endsWith('/staleness')) {
|
||||
return json({
|
||||
staleness: {
|
||||
checkedAt: 1,
|
||||
activeBuildId: null,
|
||||
configChanged: false,
|
||||
recommendation: 'something_new',
|
||||
repos: []
|
||||
}
|
||||
})
|
||||
}
|
||||
if (
|
||||
url.pathname.endsWith('/activate') ||
|
||||
init.method === 'PATCH' ||
|
||||
(init.method === 'POST' && url.pathname === '/v1/environments')
|
||||
) {
|
||||
return json({ environment: environmentEntry() })
|
||||
}
|
||||
if (init.method === 'DELETE') {
|
||||
return json({ deleted: true })
|
||||
}
|
||||
if (url.pathname.includes('/builds')) {
|
||||
return json({ build: buildView() })
|
||||
}
|
||||
return json({ environment: environmentEntry(), builds: [buildView()], canAdmin: false })
|
||||
})
|
||||
await c.get(ENV_ID)
|
||||
await c.create({ name: 'Web', repos: [{ url: 'acme/web' }] })
|
||||
await c.update(ENV_ID, { installScript: 'x', expectedRevision: 2 })
|
||||
await c.remove(ENV_ID)
|
||||
await c.startBuild(ENV_ID)
|
||||
await c.getBuild(ENV_ID, BUILD_ID)
|
||||
await c.cancelBuild(ENV_ID, BUILD_ID)
|
||||
await c.activateBuild(ENV_ID, BUILD_ID)
|
||||
const staleness = await c.staleness(ENV_ID, true)
|
||||
expect(staleness.recommendation).toBe('unknown')
|
||||
expect(calls).toEqual([
|
||||
`GET /v1/environments/${ENV_ID}`,
|
||||
'POST /v1/environments',
|
||||
`PATCH /v1/environments/${ENV_ID}`,
|
||||
`DELETE /v1/environments/${ENV_ID}`,
|
||||
`POST /v1/environments/${ENV_ID}/builds`,
|
||||
`GET /v1/environments/${ENV_ID}/builds/${BUILD_ID}`,
|
||||
`POST /v1/environments/${ENV_ID}/builds/${BUILD_ID}/cancel`,
|
||||
`POST /v1/environments/${ENV_ID}/builds/${BUILD_ID}/activate`,
|
||||
`GET /v1/environments/${ENV_ID}/staleness?refresh=1`
|
||||
])
|
||||
})
|
||||
|
||||
it('refuses ids that could change the path', async () => {
|
||||
const { client: c, fetchMock } = client(() => json({}))
|
||||
await expect(c.get('env_../../v1/sandboxes')).rejects.toMatchObject({ code: 'invalid_request' })
|
||||
await expect(c.cancelBuild(ENV_ID, 'bld_x/../y')).rejects.toMatchObject({
|
||||
code: 'invalid_request'
|
||||
})
|
||||
expect(fetchMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('maps control-plane refusals to friendly errors', async () => {
|
||||
const { client: c } = client(() => json({ error: 'conflict', reason: 'stale_revision' }, 409))
|
||||
const error = await c.update(ENV_ID, { installScript: 'x' }).catch((e: unknown) => e)
|
||||
expect(error).toBeInstanceOf(HostedEnvironmentRequestError)
|
||||
expect(toHostedEnvironmentError(error)).toEqual({
|
||||
code: 'conflict',
|
||||
reason: 'stale_revision',
|
||||
message: 'Someone else saved changes. Reload to see them.'
|
||||
})
|
||||
const admin = createHostedEnvironmentClient({
|
||||
baseUrl: BASE,
|
||||
getAccessToken: async () => 'tok',
|
||||
fetch: async () => json({ error: 'forbidden', reason: 'admin_required' }, 403)
|
||||
})
|
||||
expect(
|
||||
toHostedEnvironmentError(await admin.startBuild(ENV_ID).catch((e: unknown) => e))
|
||||
).toMatchObject({
|
||||
message: 'Only owners and admins can change the environment. Ask an owner or admin.'
|
||||
})
|
||||
})
|
||||
|
||||
it('treats redirects, oversized and malformed bodies as errors', async () => {
|
||||
const redirect = client(
|
||||
() => new Response(null, { status: 302, headers: { location: 'https://evil.test' } })
|
||||
)
|
||||
await expect(redirect.client.list()).rejects.toMatchObject({ code: 'invalid_response' })
|
||||
const huge = client(() => new Response('x'.repeat(3 * 1024 * 1024)))
|
||||
await expect(huge.client.list()).rejects.toMatchObject({ code: 'invalid_response' })
|
||||
const wrong = client(() => json({ environments: 'nope' }))
|
||||
await expect(wrong.client.list()).rejects.toMatchObject({ code: 'invalid_response' })
|
||||
})
|
||||
|
||||
it('reports sign-in problems from token minting', async () => {
|
||||
const c = createHostedEnvironmentClient({
|
||||
baseUrl: BASE,
|
||||
getAccessToken: async () => {
|
||||
throw new EnvironmentProviderError('unauthorized', 'Sign in to Orca to use Orca Cloud.')
|
||||
},
|
||||
fetch: async () => json({})
|
||||
})
|
||||
expect(toHostedEnvironmentError(await c.list().catch((e: unknown) => e))).toMatchObject({
|
||||
code: 'unauthorized',
|
||||
message: 'Sign in to Orca to use Orca Cloud.'
|
||||
})
|
||||
})
|
||||
|
||||
it('requires an https control plane', () => {
|
||||
expect(() =>
|
||||
createHostedEnvironmentClient({
|
||||
baseUrl: 'http://control.example.test',
|
||||
getAccessToken: async () => ''
|
||||
})
|
||||
).toThrow(/HTTPS/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,252 @@
|
||||
// HTTPS client for org environments on the Orca sandbox control plane (/v1/environments). Same
|
||||
// bearer as /v1/sandboxes; the desktop holds only a short-lived Orca token.
|
||||
import type { z } from 'zod'
|
||||
import {
|
||||
HostedEnvironmentBuildResponseSchema,
|
||||
HostedEnvironmentDeletedResponseSchema,
|
||||
HostedEnvironmentDetailResponseSchema,
|
||||
HostedEnvironmentListResponseSchema,
|
||||
HostedEnvironmentResponseSchema,
|
||||
HostedEnvironmentStalenessResponseSchema,
|
||||
isHostedEnvironmentBuildId,
|
||||
isHostedEnvironmentId,
|
||||
type HostedEnvironmentBuildView,
|
||||
type HostedEnvironmentCreateInput,
|
||||
type HostedEnvironmentDetailResponse,
|
||||
type HostedEnvironmentListResponse,
|
||||
type HostedEnvironmentStaleness,
|
||||
type HostedEnvironmentUpdateInput,
|
||||
type HostedEnvironmentView
|
||||
} from '../../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
import {
|
||||
errorFromResponse,
|
||||
HostedEnvironmentRequestError,
|
||||
parseBaseUrl,
|
||||
readJson,
|
||||
transportError
|
||||
} from './hosted-environment-http'
|
||||
|
||||
export {
|
||||
HostedEnvironmentRequestError,
|
||||
HostedEnvironmentUserError
|
||||
} from './hosted-environment-http'
|
||||
|
||||
export type HostedEnvironmentClient = {
|
||||
list(signal?: AbortSignal): Promise<HostedEnvironmentListResponse>
|
||||
get(envId: string, signal?: AbortSignal): Promise<HostedEnvironmentDetailResponse>
|
||||
create(input: HostedEnvironmentCreateInput, signal?: AbortSignal): Promise<HostedEnvironmentView>
|
||||
update(
|
||||
envId: string,
|
||||
patch: HostedEnvironmentUpdateInput,
|
||||
signal?: AbortSignal
|
||||
): Promise<HostedEnvironmentView>
|
||||
remove(envId: string, signal?: AbortSignal): Promise<{ deleted: true }>
|
||||
startBuild(envId: string, signal?: AbortSignal): Promise<HostedEnvironmentBuildView>
|
||||
getBuild(
|
||||
envId: string,
|
||||
buildId: string,
|
||||
signal?: AbortSignal
|
||||
): Promise<HostedEnvironmentBuildView>
|
||||
cancelBuild(
|
||||
envId: string,
|
||||
buildId: string,
|
||||
signal?: AbortSignal
|
||||
): Promise<HostedEnvironmentBuildView>
|
||||
activateBuild(
|
||||
envId: string,
|
||||
buildId: string,
|
||||
signal?: AbortSignal
|
||||
): Promise<HostedEnvironmentView>
|
||||
staleness(
|
||||
envId: string,
|
||||
refresh?: boolean,
|
||||
signal?: AbortSignal
|
||||
): Promise<HostedEnvironmentStaleness>
|
||||
/** Opens the ndjson event stream; the caller owns the body and its cancellation via `signal`. */
|
||||
openBuildEvents(
|
||||
envId: string,
|
||||
buildId: string,
|
||||
signal: AbortSignal
|
||||
): Promise<ReadableStream<Uint8Array>>
|
||||
}
|
||||
|
||||
export type HostedEnvironmentClientOptions = {
|
||||
baseUrl: string
|
||||
getAccessToken: (signal?: AbortSignal) => Promise<string>
|
||||
fetch?: (input: URL, init: RequestInit) => Promise<Response>
|
||||
/** Per-call ceiling for JSON routes; the event stream has none (the watcher runs an idle timer). */
|
||||
timeoutMs?: number
|
||||
}
|
||||
|
||||
const DEFAULT_TIMEOUT_MS = 60_000
|
||||
|
||||
export function createHostedEnvironmentClient(
|
||||
options: HostedEnvironmentClientOptions
|
||||
): HostedEnvironmentClient {
|
||||
const base = parseBaseUrl(options.baseUrl)
|
||||
const doFetch = options.fetch ?? fetch
|
||||
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS
|
||||
|
||||
async function send(
|
||||
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
|
||||
path: string,
|
||||
init: { body?: unknown; signal?: AbortSignal; accept?: string; timeout?: AbortSignal }
|
||||
): Promise<Response> {
|
||||
const signals = [init.signal, init.timeout].filter((s): s is AbortSignal => s !== undefined)
|
||||
const signal = signals.length > 1 ? AbortSignal.any(signals) : signals[0]
|
||||
try {
|
||||
const token = await options.getAccessToken(signal)
|
||||
return await doFetch(new URL(path, base), {
|
||||
method,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
accept: init.accept ?? 'application/json',
|
||||
...(init.body === undefined ? {} : { 'content-type': 'application/json' })
|
||||
},
|
||||
body: init.body === undefined ? undefined : JSON.stringify(init.body),
|
||||
// Why: a redirect could forward the bearer token to another origin.
|
||||
redirect: 'manual',
|
||||
signal
|
||||
})
|
||||
} catch (error) {
|
||||
throw transportError(error, init.signal, init.timeout)
|
||||
}
|
||||
}
|
||||
|
||||
async function call<T>(
|
||||
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
|
||||
path: string,
|
||||
schema: z.ZodType<T>,
|
||||
init: { body?: unknown; signal?: AbortSignal } = {}
|
||||
): Promise<T> {
|
||||
const timeout = AbortSignal.timeout(timeoutMs)
|
||||
const response = await send(method, path, { ...init, timeout })
|
||||
let payload: unknown
|
||||
try {
|
||||
payload = await readJson(response)
|
||||
} catch (error) {
|
||||
throw transportError(error, init.signal, timeout)
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw errorFromResponse(response.status, payload)
|
||||
}
|
||||
const parsed = schema.safeParse(payload)
|
||||
if (!parsed.success) {
|
||||
throw new HostedEnvironmentRequestError(
|
||||
'invalid_response',
|
||||
null,
|
||||
'Orca Cloud returned an unexpected response.',
|
||||
response.status
|
||||
)
|
||||
}
|
||||
return parsed.data
|
||||
}
|
||||
|
||||
return {
|
||||
list: (signal) =>
|
||||
call('GET', 'v1/environments', HostedEnvironmentListResponseSchema, { signal }),
|
||||
get: async (envId, signal) =>
|
||||
call('GET', envPath(envId), HostedEnvironmentDetailResponseSchema, { signal }),
|
||||
create: async (input, signal) =>
|
||||
(
|
||||
await call('POST', 'v1/environments', HostedEnvironmentResponseSchema, {
|
||||
body: input,
|
||||
signal
|
||||
})
|
||||
).environment,
|
||||
update: async (envId, patch, signal) =>
|
||||
(
|
||||
await call('PATCH', envPath(envId), HostedEnvironmentResponseSchema, {
|
||||
body: patch,
|
||||
signal
|
||||
})
|
||||
).environment,
|
||||
remove: async (envId, signal) => {
|
||||
await call('DELETE', envPath(envId), HostedEnvironmentDeletedResponseSchema, { signal })
|
||||
return { deleted: true }
|
||||
},
|
||||
startBuild: async (envId, signal) =>
|
||||
(
|
||||
await call('POST', envPath(envId, '/builds'), HostedEnvironmentBuildResponseSchema, {
|
||||
signal
|
||||
})
|
||||
).build,
|
||||
getBuild: async (envId, buildId, signal) =>
|
||||
(
|
||||
await call('GET', buildPath(envId, buildId), HostedEnvironmentBuildResponseSchema, {
|
||||
signal
|
||||
})
|
||||
).build,
|
||||
cancelBuild: async (envId, buildId, signal) =>
|
||||
(
|
||||
await call(
|
||||
'POST',
|
||||
buildPath(envId, buildId, '/cancel'),
|
||||
HostedEnvironmentBuildResponseSchema,
|
||||
{ signal }
|
||||
)
|
||||
).build,
|
||||
activateBuild: async (envId, buildId, signal) =>
|
||||
(
|
||||
await call(
|
||||
'POST',
|
||||
buildPath(envId, buildId, '/activate'),
|
||||
HostedEnvironmentResponseSchema,
|
||||
{
|
||||
signal
|
||||
}
|
||||
)
|
||||
).environment,
|
||||
staleness: async (envId, refresh, signal) =>
|
||||
(
|
||||
await call(
|
||||
'GET',
|
||||
envPath(envId, refresh ? '/staleness?refresh=1' : '/staleness'),
|
||||
HostedEnvironmentStalenessResponseSchema,
|
||||
{ signal }
|
||||
)
|
||||
).staleness,
|
||||
openBuildEvents: async (envId, buildId, signal) => {
|
||||
// Only the response headers are bounded; the body streams for the whole build.
|
||||
const headerTimeout = AbortSignal.timeout(timeoutMs)
|
||||
const controller = new AbortController()
|
||||
const onAbort = (): void => controller.abort()
|
||||
signal.addEventListener('abort', onAbort, { once: true })
|
||||
headerTimeout.addEventListener('abort', onAbort, { once: true })
|
||||
let response: Response
|
||||
try {
|
||||
response = await send('GET', buildPath(envId, buildId, '/events'), {
|
||||
signal: controller.signal,
|
||||
accept: 'application/x-ndjson'
|
||||
})
|
||||
} catch (error) {
|
||||
throw transportError(error, signal, headerTimeout)
|
||||
} finally {
|
||||
headerTimeout.removeEventListener('abort', onAbort)
|
||||
}
|
||||
if (!response.ok || !response.body) {
|
||||
const payload = await readJson(response).catch(() => null)
|
||||
throw errorFromResponse(response.status, payload)
|
||||
}
|
||||
return response.body
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function envPath(envId: string, suffix = ''): string {
|
||||
if (!isHostedEnvironmentId(envId)) {
|
||||
throw new HostedEnvironmentRequestError(
|
||||
'invalid_request',
|
||||
'environment_id',
|
||||
'Invalid environment id.'
|
||||
)
|
||||
}
|
||||
return `v1/environments/${envId}${suffix}`
|
||||
}
|
||||
|
||||
function buildPath(envId: string, buildId: string, suffix = ''): string {
|
||||
if (!isHostedEnvironmentBuildId(buildId)) {
|
||||
throw new HostedEnvironmentRequestError('invalid_request', 'build_id', 'Invalid build id.')
|
||||
}
|
||||
return envPath(envId, `/builds/${buildId}${suffix}`)
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import type {
|
||||
HostedEnvironmentBuildView,
|
||||
HostedEnvironmentListEntry
|
||||
} from '../../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
|
||||
export const ENV_ID = 'env_03b89d4bf1c74787a7ce'
|
||||
export const BUILD_ID = 'bld_0123456789abcdef'
|
||||
|
||||
export function buildView(
|
||||
overrides: Partial<HostedEnvironmentBuildView> = {}
|
||||
): HostedEnvironmentBuildView {
|
||||
return {
|
||||
id: BUILD_ID,
|
||||
number: 3,
|
||||
status: 'running',
|
||||
active: false,
|
||||
createdBy: 'user_1',
|
||||
createdAt: 1_700_000_000_000,
|
||||
startedAt: 1_700_000_002_000,
|
||||
snapshotStartedAt: null,
|
||||
finishedAt: null,
|
||||
durationMs: null,
|
||||
activatable: false,
|
||||
error: null,
|
||||
failedStep: null,
|
||||
revision: 2,
|
||||
machineSize: 'standard',
|
||||
repos: [{ url: 'https://github.com/acme/web.git', branch: 'main', commit: null }],
|
||||
diskUsedBytes: null,
|
||||
diskTotalBytes: null,
|
||||
logBytes: 0,
|
||||
logTruncated: false,
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
export function environmentEntry(
|
||||
overrides: Partial<HostedEnvironmentListEntry> = {}
|
||||
): HostedEnvironmentListEntry {
|
||||
return {
|
||||
id: ENV_ID,
|
||||
name: 'Web',
|
||||
repos: [{ url: 'https://github.com/acme/web.git', defaultBranch: 'main' }],
|
||||
installScript: 'pnpm install',
|
||||
envVarNames: ['NPM_TOKEN'],
|
||||
machineSize: 'standard',
|
||||
activeBuildId: null,
|
||||
revision: 2,
|
||||
createdBy: 'user_1',
|
||||
updatedBy: 'user_1',
|
||||
createdAt: 1_700_000_000_000,
|
||||
updatedAt: 1_700_000_000_000,
|
||||
activeBuild: null,
|
||||
latestBuild: null,
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
/** A body that yields the given ndjson chunks, then either closes or stays open until aborted. */
|
||||
export function ndjsonBody(
|
||||
chunks: readonly string[],
|
||||
options: { hang?: boolean; signal?: AbortSignal } = {}
|
||||
): ReadableStream<Uint8Array> {
|
||||
const encoder = new TextEncoder()
|
||||
let index = 0
|
||||
return new ReadableStream<Uint8Array>({
|
||||
pull(controller) {
|
||||
if (index < chunks.length) {
|
||||
controller.enqueue(encoder.encode(chunks[index++]))
|
||||
return
|
||||
}
|
||||
if (!options.hang) {
|
||||
controller.close()
|
||||
return
|
||||
}
|
||||
return new Promise<void>((resolve) => {
|
||||
const fail = (): void => {
|
||||
controller.error(new Error('aborted'))
|
||||
resolve()
|
||||
}
|
||||
if (options.signal?.aborted) {
|
||||
fail()
|
||||
} else {
|
||||
options.signal?.addEventListener('abort', fail, { once: true })
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const line = (value: unknown): string => `${JSON.stringify(value)}\n`
|
||||
@@ -0,0 +1,115 @@
|
||||
// Transport helpers for the environment client: bounded JSON reads and error mapping.
|
||||
import { HostedEnvironmentErrorResponseSchema } from '../../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
import { EnvironmentProviderError } from '../../environment-providers/environment-provider-driver'
|
||||
|
||||
// A detail view carries 30 builds plus a 20k-char script; list carries every environment.
|
||||
const MAX_RESPONSE_BYTES = 2 * 1024 * 1024
|
||||
|
||||
/** `code` is the control plane's error (`forbidden`, `conflict`, ...) or a transport one. */
|
||||
export class HostedEnvironmentRequestError extends Error {
|
||||
constructor(
|
||||
readonly code: string,
|
||||
readonly reason: string | null,
|
||||
message: string,
|
||||
readonly status: number | null = null
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'HostedEnvironmentRequestError'
|
||||
}
|
||||
}
|
||||
|
||||
/** A desktop-side refusal whose message is already written for the user (or the agent). */
|
||||
export class HostedEnvironmentUserError extends HostedEnvironmentRequestError {}
|
||||
|
||||
export function parseBaseUrl(value: string): URL {
|
||||
const url = new URL(value.endsWith('/') ? value : `${value}/`)
|
||||
const loopback = url.hostname === '127.0.0.1' || url.hostname === 'localhost'
|
||||
if (url.protocol !== 'https:' && !(url.protocol === 'http:' && loopback)) {
|
||||
throw new Error('Orca Cloud control plane must be HTTPS.')
|
||||
}
|
||||
if (url.username || url.password || url.search || url.hash) {
|
||||
throw new Error('Orca Cloud control plane URL must not carry credentials, query or fragment.')
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
/** Reads at most MAX_RESPONSE_BYTES; a larger body reads as unparseable instead of buffering it all. */
|
||||
export async function readJson(response: Response): Promise<unknown> {
|
||||
if (!response.body) {
|
||||
return null
|
||||
}
|
||||
const reader = response.body.getReader()
|
||||
const chunks: Uint8Array[] = []
|
||||
let total = 0
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) {
|
||||
break
|
||||
}
|
||||
total += value.byteLength
|
||||
if (total > MAX_RESPONSE_BYTES) {
|
||||
await reader.cancel().catch(() => undefined)
|
||||
return null
|
||||
}
|
||||
chunks.push(value)
|
||||
}
|
||||
const text = Buffer.concat(chunks).toString('utf8')
|
||||
try {
|
||||
return text ? JSON.parse(text) : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function errorFromResponse(status: number, payload: unknown): HostedEnvironmentRequestError {
|
||||
const parsed = HostedEnvironmentErrorResponseSchema.safeParse(payload)
|
||||
if (parsed.success) {
|
||||
return new HostedEnvironmentRequestError(
|
||||
parsed.data.error,
|
||||
parsed.data.reason ?? null,
|
||||
`Orca Cloud refused the request (${parsed.data.error}${parsed.data.reason ? `: ${parsed.data.reason}` : ''}).`,
|
||||
status
|
||||
)
|
||||
}
|
||||
if (status >= 300 && status < 400) {
|
||||
return new HostedEnvironmentRequestError(
|
||||
'invalid_response',
|
||||
null,
|
||||
'Orca Cloud answered with a redirect.',
|
||||
status
|
||||
)
|
||||
}
|
||||
return new HostedEnvironmentRequestError(
|
||||
status === 401
|
||||
? 'unauthorized'
|
||||
: status === 403
|
||||
? 'forbidden'
|
||||
: status === 404
|
||||
? 'not_found'
|
||||
: 'unavailable',
|
||||
null,
|
||||
`Orca Cloud request failed (HTTP ${status}).`,
|
||||
status
|
||||
)
|
||||
}
|
||||
|
||||
export function transportError(
|
||||
error: unknown,
|
||||
callerSignal: AbortSignal | undefined,
|
||||
timeoutSignal: AbortSignal | undefined
|
||||
): HostedEnvironmentRequestError {
|
||||
if (error instanceof HostedEnvironmentRequestError) {
|
||||
return error
|
||||
}
|
||||
// Token minting reports sign-in problems as provider errors.
|
||||
if (error instanceof EnvironmentProviderError) {
|
||||
return new HostedEnvironmentRequestError(error.code, error.reason ?? null, error.message)
|
||||
}
|
||||
if (callerSignal?.aborted) {
|
||||
return new HostedEnvironmentRequestError('aborted', null, 'Orca Cloud request was cancelled.')
|
||||
}
|
||||
if (timeoutSignal?.aborted) {
|
||||
return new HostedEnvironmentRequestError('timeout', null, 'Orca Cloud did not answer in time.')
|
||||
}
|
||||
return new HostedEnvironmentRequestError('unavailable', null, 'Could not reach Orca Cloud.')
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { app } from 'electron'
|
||||
import { withCloudBuildDefaults } from '../../cloud-build-defaults'
|
||||
import { getHostedSandboxConfig } from '../hosted-sandbox-config'
|
||||
import {
|
||||
createHostedEnvironmentService,
|
||||
getHostedEnvironmentService,
|
||||
registerHostedEnvironmentService,
|
||||
type HostedEnvironmentService
|
||||
} from './hosted-environment-service'
|
||||
|
||||
/** One service per process, built on first use so startup never reads the profile for it. */
|
||||
export function installHostedEnvironmentService(): HostedEnvironmentService {
|
||||
const existing = getHostedEnvironmentService()
|
||||
if (existing) {
|
||||
return existing
|
||||
}
|
||||
const service = createHostedEnvironmentService(() =>
|
||||
getHostedSandboxConfig(
|
||||
app.getPath('userData'),
|
||||
withCloudBuildDefaults(process.env),
|
||||
app.isPackaged
|
||||
)
|
||||
)
|
||||
registerHostedEnvironmentService(service)
|
||||
return service
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import { readRepoSetupImports } from './hosted-environment-repo-imports'
|
||||
import { toHostedEnvironmentError } from './hosted-environment-service'
|
||||
|
||||
let base: string
|
||||
let repo: string
|
||||
let outside: string
|
||||
|
||||
beforeEach(async () => {
|
||||
base = await mkdtemp(join(tmpdir(), 'orca-env-imports-'))
|
||||
repo = join(base, 'repo')
|
||||
outside = join(base, 'outside')
|
||||
await mkdir(repo)
|
||||
await mkdir(outside)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await rm(base, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
const devcontainer = (name: string) =>
|
||||
JSON.stringify({ postCreateCommand: 'npm ci', containerEnv: { [name]: 'value' } })
|
||||
|
||||
describe('readRepoSetupImports', () => {
|
||||
it('reads names and a setup script from a registered repo', async () => {
|
||||
await mkdir(join(repo, '.devcontainer'))
|
||||
await writeFile(join(repo, '.devcontainer/devcontainer.json'), devcontainer('NPM_TOKEN'))
|
||||
const result = await readRepoSetupImports(repo, [repo])
|
||||
expect(result.envVarNames).toEqual(['NPM_TOKEN'])
|
||||
expect(result.installScript).toContain('npm ci')
|
||||
expect(result.sources).toEqual([
|
||||
'Dev Container (.devcontainer/devcontainer.json)',
|
||||
'Environment variable names (.devcontainer/devcontainer.json)'
|
||||
])
|
||||
})
|
||||
|
||||
it('refuses a folder that is not a project in Orca', async () => {
|
||||
const error = await readRepoSetupImports(outside, [repo]).catch((e: unknown) => e)
|
||||
expect(toHostedEnvironmentError(error)).toMatchObject({
|
||||
code: 'not_found',
|
||||
reason: 'repo_not_registered'
|
||||
})
|
||||
const relative = await readRepoSetupImports('repo', [repo]).catch((e: unknown) => e)
|
||||
expect(toHostedEnvironmentError(relative).code).toBe('invalid_request')
|
||||
})
|
||||
|
||||
it('ignores config reached through a symlink that leaves the repo', async () => {
|
||||
await mkdir(join(outside, 'dc'))
|
||||
await writeFile(join(outside, 'dc/devcontainer.json'), devcontainer('LEAKED_NAME'))
|
||||
await symlink(join(outside, 'dc'), join(repo, '.devcontainer'))
|
||||
await writeFile(join(outside, 'secrets.env'), 'OUTSIDE_SECRET=1\n')
|
||||
await symlink(join(outside, 'secrets.env'), join(repo, '.env.example'))
|
||||
const result = await readRepoSetupImports(repo, [repo])
|
||||
expect(result.envVarNames).toEqual([])
|
||||
expect(result.installScript).toBeUndefined()
|
||||
expect(result.sources).toEqual([])
|
||||
})
|
||||
|
||||
it('follows a symlink that stays inside the repo', async () => {
|
||||
await mkdir(join(repo, 'config'))
|
||||
await writeFile(join(repo, 'config/env.example'), 'INSIDE_NAME=\n')
|
||||
await symlink(join(repo, 'config/env.example'), join(repo, '.env.example'))
|
||||
const result = await readRepoSetupImports(repo, [repo])
|
||||
expect(result.envVarNames).toEqual(['INSIDE_NAME'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,81 @@
|
||||
// Seeds a new org environment from a local checkout: the first setup command Orca's importers find
|
||||
// (Conductor, dev container, Cursor, ...) and the env var NAMES those configs expect. No values.
|
||||
import { readFile, realpath, stat } from 'node:fs/promises'
|
||||
import { isAbsolute, join, relative, sep } from 'node:path'
|
||||
import type { OrcaCloudRepoSetupImports } from '../../../shared/hosted-sandbox/hosted-environment-api'
|
||||
import { HOSTED_ENVIRONMENT_MAX_INSTALL_SCRIPT_CHARS } from '../../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
import { inspectSetupEnvVarNames } from '../../../shared/workspace-setup/setup-env-var-name-imports'
|
||||
import { SETUP_SCRIPT_IMPORT_FILE_MAX_BYTES } from '../../../shared/workspace-setup/setup-script-import-limits'
|
||||
import { inspectSetupScriptImportCandidates } from '../../../shared/workspace-setup/setup-script-imports'
|
||||
import { HostedEnvironmentUserError } from './hosted-environment-client'
|
||||
|
||||
/**
|
||||
* `registeredRepoPaths` are the local checkouts in main's repo store; the renderer can only name
|
||||
* one of them. Every file is resolved through realpath and must stay inside the repo, so a
|
||||
* symlinked `.devcontainer/` or `.conductor/` cannot point the importer elsewhere on disk.
|
||||
*/
|
||||
export async function readRepoSetupImports(
|
||||
repoPath: unknown,
|
||||
registeredRepoPaths: readonly string[]
|
||||
): Promise<OrcaCloudRepoSetupImports> {
|
||||
if (typeof repoPath !== 'string' || !isAbsolute(repoPath) || repoPath.length > 4096) {
|
||||
throw new HostedEnvironmentUserError('invalid_request', 'repo_path', 'Invalid repository path.')
|
||||
}
|
||||
const root = await realpath(repoPath).catch(() => null)
|
||||
const registered = await Promise.all(
|
||||
registeredRepoPaths.map((path) => realpath(path).catch(() => null))
|
||||
)
|
||||
if (!root || !registered.includes(root)) {
|
||||
throw new HostedEnvironmentUserError(
|
||||
'not_found',
|
||||
'repo_not_registered',
|
||||
'That folder is not a project in Orca. Add it as a project first.'
|
||||
)
|
||||
}
|
||||
if (!(await stat(root).catch(() => null))?.isDirectory()) {
|
||||
throw new HostedEnvironmentUserError('not_found', null, 'Repository folder not found.')
|
||||
}
|
||||
|
||||
/** The file's real path when it is a regular file inside the repo, else null. */
|
||||
const resolveInside = async (relativePath: string): Promise<string | null> => {
|
||||
if (isAbsolute(relativePath)) {
|
||||
return null
|
||||
}
|
||||
const real = await realpath(join(root, relativePath)).catch(() => null)
|
||||
if (!real) {
|
||||
return null
|
||||
}
|
||||
const rel = relative(root, real)
|
||||
if (!rel || rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
|
||||
return null
|
||||
}
|
||||
const info = await stat(real).catch(() => null)
|
||||
return info?.isFile() && info.size <= SETUP_SCRIPT_IMPORT_FILE_MAX_BYTES ? real : null
|
||||
}
|
||||
const fileExists = async (relativePath: string): Promise<boolean> =>
|
||||
(await resolveInside(relativePath)) !== null
|
||||
const read = async (relativePath: string): Promise<string | null> => {
|
||||
const real = await resolveInside(relativePath)
|
||||
return real ? readFile(real, 'utf8').catch(() => null) : null
|
||||
}
|
||||
|
||||
const [candidates, envNames] = await Promise.all([
|
||||
inspectSetupScriptImportCandidates(read, { fileExists }),
|
||||
inspectSetupEnvVarNames(read)
|
||||
])
|
||||
const sources: string[] = []
|
||||
const first = candidates.find(
|
||||
(candidate) => candidate.setup.length <= HOSTED_ENVIRONMENT_MAX_INSTALL_SCRIPT_CHARS
|
||||
)
|
||||
if (first) {
|
||||
sources.push(`${first.label} (${first.files.join(', ')})`)
|
||||
}
|
||||
for (const file of envNames.files) {
|
||||
sources.push(`Environment variable names (${file})`)
|
||||
}
|
||||
return {
|
||||
envVarNames: envNames.names,
|
||||
...(first ? { installScript: first.setup } : {}),
|
||||
sources
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// The desktop's one handle on org environments: a lazily built control-plane client plus the
|
||||
// Result mapping the IPC and runtime RPC surfaces share.
|
||||
import type {
|
||||
OrcaCloudEnvironmentError,
|
||||
OrcaCloudEnvironmentResult
|
||||
} from '../../../shared/hosted-sandbox/hosted-environment-api'
|
||||
import { hostedEnvironmentError } from '../../../shared/hosted-sandbox/hosted-environment-errors'
|
||||
import type { HostedSandboxConfig } from '../hosted-sandbox-config'
|
||||
import {
|
||||
createHostedEnvironmentClient,
|
||||
HostedEnvironmentRequestError,
|
||||
HostedEnvironmentUserError,
|
||||
type HostedEnvironmentClient
|
||||
} from './hosted-environment-client'
|
||||
|
||||
export type HostedEnvironmentService = {
|
||||
isConfigured(): boolean
|
||||
/** Throws HostedEnvironmentRequestError('not_configured') when this build has no control plane. */
|
||||
client(): HostedEnvironmentClient
|
||||
}
|
||||
|
||||
export function createHostedEnvironmentService(
|
||||
loadConfig: () => HostedSandboxConfig | null,
|
||||
fetchImpl?: typeof fetch
|
||||
): HostedEnvironmentService {
|
||||
let resolved: { client: HostedEnvironmentClient | null } | null = null
|
||||
const resolve = (): HostedEnvironmentClient | null => {
|
||||
if (!resolved) {
|
||||
const config = loadConfig()
|
||||
resolved = {
|
||||
client: config
|
||||
? createHostedEnvironmentClient({
|
||||
baseUrl: config.controlPlaneUrl,
|
||||
getAccessToken: config.getAccessToken,
|
||||
fetch: fetchImpl
|
||||
})
|
||||
: null
|
||||
}
|
||||
}
|
||||
return resolved.client
|
||||
}
|
||||
return {
|
||||
isConfigured: () => resolve() !== null,
|
||||
client: () => {
|
||||
const client = resolve()
|
||||
if (!client) {
|
||||
throw new HostedEnvironmentRequestError(
|
||||
'not_configured',
|
||||
null,
|
||||
'Orca Cloud is not set up in this build of Orca.'
|
||||
)
|
||||
}
|
||||
return client
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function toHostedEnvironmentError(error: unknown): OrcaCloudEnvironmentError {
|
||||
if (error instanceof HostedEnvironmentUserError) {
|
||||
return {
|
||||
code: error.code,
|
||||
...(error.reason ? { reason: error.reason } : {}),
|
||||
message: error.message
|
||||
}
|
||||
}
|
||||
if (error instanceof HostedEnvironmentRequestError) {
|
||||
return hostedEnvironmentError(error.code, error.reason)
|
||||
}
|
||||
console.warn('[cloud-environments] unexpected failure:', error)
|
||||
return hostedEnvironmentError('unavailable')
|
||||
}
|
||||
|
||||
export async function hostedEnvironmentResult<T>(
|
||||
work: () => Promise<T>
|
||||
): Promise<OrcaCloudEnvironmentResult<T>> {
|
||||
try {
|
||||
return { ok: true, data: await work() }
|
||||
} catch (error) {
|
||||
return { ok: false, error: toHostedEnvironmentError(error) }
|
||||
}
|
||||
}
|
||||
|
||||
let registered: HostedEnvironmentService | null = null
|
||||
|
||||
/** Desktop main publishes the service for the runtime RPC (CLI); a headless host has none. */
|
||||
export function registerHostedEnvironmentService(service: HostedEnvironmentService | null): void {
|
||||
registered = service
|
||||
}
|
||||
|
||||
export function getHostedEnvironmentService(): HostedEnvironmentService | null {
|
||||
return registered
|
||||
}
|
||||
@@ -122,7 +122,7 @@ async function mintSandboxToken(
|
||||
/** Dev builds only: sign with the dev operator key the dev control plane trusts (issuer orca-sandbox-dev-test). */
|
||||
async function mintDevSandboxToken(
|
||||
keyFile: string,
|
||||
cloud: { userId: string; activeOrgId?: string } | undefined,
|
||||
cloud: { userId: string; activeOrgId?: string; displayName?: string } | undefined,
|
||||
env: NodeJS.ProcessEnv
|
||||
): Promise<{ token: string; expiresAt: number }> {
|
||||
const userId = cloud?.userId
|
||||
@@ -144,6 +144,8 @@ async function mintDevSandboxToken(
|
||||
})
|
||||
const payload = encode({
|
||||
sub: userId ?? 'desktop-dogfood',
|
||||
// Auth mints a display-name claim; dev tokens carry one so labels look real.
|
||||
...(cloud?.displayName ? { name: cloud.displayName.slice(0, 80) } : {}),
|
||||
...org,
|
||||
purpose: 'sandbox',
|
||||
iss: 'orca-sandbox-dev-test',
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Which provider runs the VM (Vercel today) is a control-plane detail the desktop never sees.
|
||||
import {
|
||||
HOSTED_SANDBOX_MACHINE_SIZES,
|
||||
ORCA_CLOUD_IDLE_SLEEP_MINUTES,
|
||||
type HostedSandboxMachineSize,
|
||||
type HostedSandboxReadyResponse,
|
||||
type HostedSandboxView
|
||||
@@ -30,7 +31,7 @@ export const HOSTED_SANDBOX_CAPABILITIES: HostedEnvironmentCapabilities = Object
|
||||
maxSessionMs: 24 * 60 * 60_000,
|
||||
fsPersistOnStop: true,
|
||||
egressInjection: true,
|
||||
idleStopMs: 20 * 60_000
|
||||
idleStopMs: ORCA_CLOUD_IDLE_SLEEP_MINUTES * 60_000
|
||||
})
|
||||
|
||||
const STATUS: Record<HostedSandboxView['status'], EnvironmentStatus> = {
|
||||
@@ -154,7 +155,13 @@ export function toReady(ready: HostedSandboxReadyResponse): HostedEnvironmentRea
|
||||
const handle: EnvironmentHandle = {
|
||||
driverId: HOSTED_SANDBOX_DRIVER_ID,
|
||||
environmentId: ready.sandbox.id,
|
||||
driverState: { sandboxId: ready.sandbox.id }
|
||||
driverState: {
|
||||
sandboxId: ready.sandbox.id,
|
||||
// Booted from an org environment build: dependencies are installed, so no setup-script nudge.
|
||||
...(ready.sandbox.environmentBuildId
|
||||
? { environmentBuildId: ready.sandbox.environmentBuildId }
|
||||
: {})
|
||||
}
|
||||
}
|
||||
return {
|
||||
handle,
|
||||
|
||||
@@ -25,6 +25,10 @@ import {
|
||||
import { createHostedSessionRegistry, type HostedSessionRegistry } from './hosted-session-registry'
|
||||
import { registerCloudShareHandlers } from './hosted-sandbox-share-ipc'
|
||||
import { setHostedSessionSync } from './hosted-session-sync'
|
||||
import {
|
||||
registerOrcaCloudEnvironmentHandlers,
|
||||
type OrcaCloudEnvironmentRepoSource
|
||||
} from '../ipc/cloud-environments/orca-cloud-environment-handlers'
|
||||
import { installCloudTransferHandlers } from './cloud-transfer'
|
||||
import { installCloudOfflineService, setCloudOfflineService } from './cloud-offline-service'
|
||||
import {
|
||||
@@ -46,7 +50,10 @@ export function getHostedSandboxControlPlaneClient(): HostedSandboxControlPlaneC
|
||||
return controlPlaneClient
|
||||
}
|
||||
|
||||
export function installHostedSandbox(): void {
|
||||
/** `repos` is main's repo store: org-environment repo imports only read checkouts listed there. */
|
||||
export function installHostedSandbox(repos?: OrcaCloudEnvironmentRepoSource): void {
|
||||
// Idempotent; answers isConfigured() = false itself when this build has no control plane.
|
||||
registerOrcaCloudEnvironmentHandlers(repos ? { repos } : {})
|
||||
if (installed) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ const DEFAULT_TIMEOUTS = { boot: 180_000, control: 60_000 }
|
||||
const MAX_RESPONSE_BYTES = 256 * 1024
|
||||
|
||||
export type HostedSandboxRequest = <T>(
|
||||
method: 'GET' | 'POST' | 'DELETE',
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
|
||||
path: string,
|
||||
schema: z.ZodType<T>,
|
||||
init: { body?: unknown; kind: 'boot' | 'control'; signal?: AbortSignal }
|
||||
@@ -36,7 +36,7 @@ export function createHostedSandboxRequest(
|
||||
const timeouts = { ...DEFAULT_TIMEOUTS, ...options.timeouts }
|
||||
|
||||
return async function call<T>(
|
||||
method: 'GET' | 'POST' | 'DELETE',
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
|
||||
path: string,
|
||||
schema: z.ZodType<T>,
|
||||
init: { body?: unknown; kind: 'boot' | 'control'; signal?: AbortSignal }
|
||||
|
||||
@@ -0,0 +1,281 @@
|
||||
// IPC for window.api.orcaCloudEnvironments. Every call answers a Result so the renderer never sees
|
||||
// a thrown control-plane error; build watches stream to the window that opened them.
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { ipcMain, type IpcMainInvokeEvent, type WebContents } from 'electron'
|
||||
import { z } from 'zod'
|
||||
import { getRepoDialableSshTargetId } from '../../../shared/execution-host'
|
||||
import type { Repo } from '../../../shared/repo-types'
|
||||
import {
|
||||
ORCA_CLOUD_ENVIRONMENT_BUILD_EVENT_CHANNEL,
|
||||
type OrcaCloudEnvironmentBuildEvent,
|
||||
type OrcaCloudEnvironmentResult
|
||||
} from '../../../shared/hosted-sandbox/hosted-environment-api'
|
||||
import { hostedEnvironmentError } from '../../../shared/hosted-sandbox/hosted-environment-errors'
|
||||
import {
|
||||
HOSTED_ENVIRONMENT_BUILD_ID,
|
||||
HOSTED_ENVIRONMENT_ID,
|
||||
HostedEnvironmentCreateInputSchema,
|
||||
HostedEnvironmentUpdateInputSchema
|
||||
} from '../../../shared/hosted-sandbox/hosted-environment-wire'
|
||||
import { watchHostedEnvironmentBuild } from '../../hosted-sandbox/environments/hosted-environment-build-watch'
|
||||
import { installHostedEnvironmentService } from '../../hosted-sandbox/environments/hosted-environment-install'
|
||||
import { readRepoSetupImports } from '../../hosted-sandbox/environments/hosted-environment-repo-imports'
|
||||
import {
|
||||
hostedEnvironmentResult,
|
||||
type HostedEnvironmentService
|
||||
} from '../../hosted-sandbox/environments/hosted-environment-service'
|
||||
|
||||
const EnvId = z.string().regex(HOSTED_ENVIRONMENT_ID)
|
||||
const BuildId = z.string().regex(HOSTED_ENVIRONMENT_BUILD_ID)
|
||||
// A settings page shows one build log at a time; more than this per window is a leak.
|
||||
const MAX_WATCHES_PER_SENDER = 8
|
||||
|
||||
const CHANNELS = [
|
||||
'orcaCloudEnvironments:isConfigured',
|
||||
'orcaCloudEnvironments:list',
|
||||
'orcaCloudEnvironments:get',
|
||||
'orcaCloudEnvironments:create',
|
||||
'orcaCloudEnvironments:update',
|
||||
'orcaCloudEnvironments:remove',
|
||||
'orcaCloudEnvironments:startBuild',
|
||||
'orcaCloudEnvironments:cancelBuild',
|
||||
'orcaCloudEnvironments:activateBuild',
|
||||
'orcaCloudEnvironments:staleness',
|
||||
'orcaCloudEnvironments:watchBuild',
|
||||
'orcaCloudEnvironments:unwatchBuild',
|
||||
'orcaCloudEnvironments:readRepoSetupImports'
|
||||
] as const
|
||||
|
||||
type Watch = {
|
||||
senderId: number
|
||||
stop: () => void
|
||||
/** Sends one last event to the renderer that owns the watch. */
|
||||
notify: (event: OrcaCloudEnvironmentBuildEvent['event']) => void
|
||||
}
|
||||
|
||||
export type OrcaCloudEnvironmentRepoSource = { getRepos(): Repo[] }
|
||||
|
||||
const EVICTED_MESSAGE =
|
||||
'Stopped following this build: too many build logs are open in this window. Reopen it to resume.'
|
||||
|
||||
// Main's repo store, set by whoever wires the handlers; without it no repo can be imported from.
|
||||
let repoSource: OrcaCloudEnvironmentRepoSource | null = null
|
||||
|
||||
/** Local checkouts the user added to Orca; SSH-backed repos never fall through to local reads. */
|
||||
function registeredLocalRepoPaths(): string[] {
|
||||
return (repoSource?.getRepos() ?? [])
|
||||
.filter((repo) => !getRepoDialableSshTargetId(repo))
|
||||
.map((repo) => repo.path)
|
||||
}
|
||||
|
||||
function invalid<T>(field: string): OrcaCloudEnvironmentResult<T> {
|
||||
return { ok: false, error: hostedEnvironmentError('invalid_request', field) }
|
||||
}
|
||||
|
||||
let registered = false
|
||||
|
||||
/** Idempotent: startup may wire Orca Cloud more than once. */
|
||||
export function registerOrcaCloudEnvironmentHandlers(
|
||||
options: { repos?: OrcaCloudEnvironmentRepoSource; service?: HostedEnvironmentService } = {}
|
||||
): void {
|
||||
if (options.repos) {
|
||||
repoSource = options.repos
|
||||
}
|
||||
if (registered) {
|
||||
return
|
||||
}
|
||||
const service = options.service ?? installHostedEnvironmentService()
|
||||
registered = true
|
||||
for (const channel of CHANNELS) {
|
||||
ipcMain.removeHandler(channel)
|
||||
}
|
||||
const watches = new Map<string, Watch>()
|
||||
const trackedSenders = new Set<number>()
|
||||
|
||||
const stopWatch = (watchId: string): void => {
|
||||
const watch = watches.get(watchId)
|
||||
if (watch) {
|
||||
watches.delete(watchId)
|
||||
watch.stop()
|
||||
}
|
||||
}
|
||||
|
||||
const trackSender = (sender: WebContents): void => {
|
||||
if (trackedSenders.has(sender.id)) {
|
||||
return
|
||||
}
|
||||
trackedSenders.add(sender.id)
|
||||
const senderId = sender.id
|
||||
const cleanup = (): void => {
|
||||
trackedSenders.delete(senderId)
|
||||
for (const [watchId, watch] of watches) {
|
||||
if (watch.senderId === senderId) {
|
||||
stopWatch(watchId)
|
||||
}
|
||||
}
|
||||
}
|
||||
sender.once('destroyed', cleanup)
|
||||
// A reload keeps the WebContents but drops every renderer listener.
|
||||
sender.on('did-start-navigation', (details) => {
|
||||
if (details.isMainFrame && !details.isSameDocument) {
|
||||
for (const [watchId, watch] of watches) {
|
||||
if (watch.senderId === senderId) {
|
||||
stopWatch(watchId)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:isConfigured', () => service.isConfigured())
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:list', () =>
|
||||
hostedEnvironmentResult(() => service.client().list())
|
||||
)
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:get', (_event, envId: unknown) => {
|
||||
const id = EnvId.safeParse(envId)
|
||||
return id.success
|
||||
? hostedEnvironmentResult(() => service.client().get(id.data))
|
||||
: invalid('environment_id')
|
||||
})
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:create', (_event, input: unknown) => {
|
||||
const parsed = HostedEnvironmentCreateInputSchema.safeParse(input)
|
||||
if (!parsed.success) {
|
||||
return invalid(parsed.error.issues[0]?.path.join('.') || 'input')
|
||||
}
|
||||
return hostedEnvironmentResult(async () => ({
|
||||
environment: await service.client().create(parsed.data)
|
||||
}))
|
||||
})
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:update', (_event, envId: unknown, patch: unknown) => {
|
||||
const id = EnvId.safeParse(envId)
|
||||
const parsed = HostedEnvironmentUpdateInputSchema.safeParse(patch)
|
||||
if (!id.success) {
|
||||
return invalid('environment_id')
|
||||
}
|
||||
if (!parsed.success) {
|
||||
return invalid(parsed.error.issues[0]?.path.join('.') || 'patch')
|
||||
}
|
||||
return hostedEnvironmentResult(async () => ({
|
||||
environment: await service.client().update(id.data, parsed.data)
|
||||
}))
|
||||
})
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:remove', (_event, envId: unknown) => {
|
||||
const id = EnvId.safeParse(envId)
|
||||
return id.success
|
||||
? hostedEnvironmentResult(() => service.client().remove(id.data))
|
||||
: invalid('environment_id')
|
||||
})
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:startBuild', (_event, envId: unknown) => {
|
||||
const id = EnvId.safeParse(envId)
|
||||
return id.success
|
||||
? hostedEnvironmentResult(async () => ({ build: await service.client().startBuild(id.data) }))
|
||||
: invalid('environment_id')
|
||||
})
|
||||
|
||||
ipcMain.handle(
|
||||
'orcaCloudEnvironments:cancelBuild',
|
||||
(_event, envId: unknown, buildId: unknown) => {
|
||||
const id = EnvId.safeParse(envId)
|
||||
const bid = BuildId.safeParse(buildId)
|
||||
if (!id.success || !bid.success) {
|
||||
return invalid(id.success ? 'build_id' : 'environment_id')
|
||||
}
|
||||
return hostedEnvironmentResult(async () => ({
|
||||
build: await service.client().cancelBuild(id.data, bid.data)
|
||||
}))
|
||||
}
|
||||
)
|
||||
|
||||
ipcMain.handle(
|
||||
'orcaCloudEnvironments:activateBuild',
|
||||
(_event, envId: unknown, buildId: unknown) => {
|
||||
const id = EnvId.safeParse(envId)
|
||||
const bid = BuildId.safeParse(buildId)
|
||||
if (!id.success || !bid.success) {
|
||||
return invalid(id.success ? 'build_id' : 'environment_id')
|
||||
}
|
||||
return hostedEnvironmentResult(async () => ({
|
||||
environment: await service.client().activateBuild(id.data, bid.data)
|
||||
}))
|
||||
}
|
||||
)
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:staleness', (_event, envId: unknown, refresh: unknown) => {
|
||||
const id = EnvId.safeParse(envId)
|
||||
return id.success
|
||||
? hostedEnvironmentResult(async () => ({
|
||||
staleness: await service.client().staleness(id.data, refresh === true)
|
||||
}))
|
||||
: invalid('environment_id')
|
||||
})
|
||||
|
||||
ipcMain.handle(
|
||||
'orcaCloudEnvironments:watchBuild',
|
||||
(event: IpcMainInvokeEvent, envId: unknown, buildId: unknown) => {
|
||||
const id = EnvId.safeParse(envId)
|
||||
const bid = BuildId.safeParse(buildId)
|
||||
if (!id.success || !bid.success) {
|
||||
return invalid(id.success ? 'build_id' : 'environment_id')
|
||||
}
|
||||
return hostedEnvironmentResult(async () => {
|
||||
const client = service.client()
|
||||
const sender = event.sender
|
||||
trackSender(sender)
|
||||
const senderWatches = [...watches].filter(([, watch]) => watch.senderId === sender.id)
|
||||
// Oldest first (Map keeps insertion order): drop the ones a leaky caller forgot.
|
||||
for (const [watchId, evicted] of senderWatches.slice(
|
||||
0,
|
||||
Math.max(0, senderWatches.length - MAX_WATCHES_PER_SENDER + 1)
|
||||
)) {
|
||||
// Tell the evicted log why it went quiet before stopping it.
|
||||
evicted.notify({ type: 'error', message: EVICTED_MESSAGE })
|
||||
stopWatch(watchId)
|
||||
}
|
||||
const watchId = randomUUID()
|
||||
const send = (streamEvent: OrcaCloudEnvironmentBuildEvent['event']): void => {
|
||||
if (sender.isDestroyed()) {
|
||||
stopWatch(watchId)
|
||||
return
|
||||
}
|
||||
const payload: OrcaCloudEnvironmentBuildEvent = {
|
||||
watchId,
|
||||
envId: id.data,
|
||||
buildId: bid.data,
|
||||
event: streamEvent
|
||||
}
|
||||
sender.send(ORCA_CLOUD_ENVIRONMENT_BUILD_EVENT_CHANNEL, payload)
|
||||
}
|
||||
const watch = watchHostedEnvironmentBuild({
|
||||
client,
|
||||
envId: id.data,
|
||||
buildId: bid.data,
|
||||
onEvent: send
|
||||
})
|
||||
watches.set(watchId, { senderId: sender.id, stop: watch.stop, notify: send })
|
||||
void watch.done.finally(() => {
|
||||
if (watches.get(watchId)?.stop === watch.stop) {
|
||||
watches.delete(watchId)
|
||||
}
|
||||
})
|
||||
return { watchId }
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:unwatchBuild', (event, watchId: unknown) => {
|
||||
// Only the window that opened a watch may stop it.
|
||||
if (typeof watchId === 'string' && watches.get(watchId)?.senderId === event.sender.id) {
|
||||
stopWatch(watchId)
|
||||
}
|
||||
})
|
||||
|
||||
ipcMain.handle('orcaCloudEnvironments:readRepoSetupImports', (_event, repoPath: unknown) =>
|
||||
hostedEnvironmentResult(() => readRepoSetupImports(repoPath, registeredLocalRepoPaths()))
|
||||
)
|
||||
}
|
||||
@@ -155,6 +155,13 @@ export function registerCloudWorkspacesHandlers(): void {
|
||||
const s = await getService()
|
||||
return !s ? NOT_CONFIGURED : parsed.success ? s.get(parsed.data.id) : INVALID
|
||||
})
|
||||
handle('cloudWorkspaces:noteSent', async (args) => {
|
||||
const parsed = z.object({ id: Id }).safeParse(args)
|
||||
const s = await getService()
|
||||
if (s && parsed.success) {
|
||||
await s.noteSent(parsed.data.id)
|
||||
}
|
||||
})
|
||||
handle('cloudWorkspaces:patch', async (args) => {
|
||||
const parsed = z.object({ id: Id, patch: Patch }).safeParse(args)
|
||||
const s = await getService()
|
||||
|
||||
@@ -64,6 +64,15 @@ export function listRecipes(
|
||||
}
|
||||
}
|
||||
|
||||
// Glossary: an Orca Cloud machine is a sandbox; "VM" stays only for bring-your-own recipes.
|
||||
export function buildEphemeralEnvironmentName(
|
||||
repo: string,
|
||||
runtimeId: string,
|
||||
recipe: { provider?: string }
|
||||
): string {
|
||||
return `${repo} ${recipe.provider === 'orca-cloud' ? 'sandbox' : 'VM'} ${runtimeId.slice(-8)}`
|
||||
}
|
||||
|
||||
/** The Run-on picker's list, plus Orca Cloud availability so it can offer sign-in or an upsell. */
|
||||
export async function listPickerRecipes(
|
||||
store: Store,
|
||||
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
listRecipeCatalog,
|
||||
listRecipes,
|
||||
listPickerRecipes,
|
||||
buildEphemeralEnvironmentName,
|
||||
resolveRecipeForRepo,
|
||||
type EphemeralVmRecipeCatalogEntry
|
||||
} from './ephemeral-vm-recipe-context'
|
||||
@@ -76,7 +77,7 @@ export type EphemeralVmProvisionIpcResult =
|
||||
} & EphemeralVmFailure)
|
||||
|
||||
export function registerEphemeralVmHandlers(store: Store, pluginService?: PluginService): void {
|
||||
installHostedSandbox()
|
||||
installHostedSandbox(store)
|
||||
ipcMain.removeHandler('ephemeralVm:listRecipes')
|
||||
ipcMain.removeHandler('ephemeralVm:listRecipeCatalog')
|
||||
ipcMain.removeHandler('ephemeralVm:doctor')
|
||||
@@ -251,7 +252,7 @@ export function registerEphemeralVmHandlers(store: Store, pluginService?: Plugin
|
||||
let environment: ReturnType<typeof addEnvironmentFromPairingCode>
|
||||
try {
|
||||
environment = addEnvironmentFromPairingCode(getRuntimeEnvironmentRegistryRoot(), {
|
||||
name: buildEphemeralEnvironmentName(repo.repo.displayName, result.runtime.id),
|
||||
name: buildEphemeralEnvironmentName(repo.repo.displayName, result.runtime.id, recipe),
|
||||
pairingCode: connection.pairingCode,
|
||||
source: 'ephemeral-vm'
|
||||
})
|
||||
@@ -304,10 +305,6 @@ export function registerEphemeralVmHandlers(store: Store, pluginService?: Plugin
|
||||
)
|
||||
}
|
||||
|
||||
function buildEphemeralEnvironmentName(repoName: string, runtimeId: string): string {
|
||||
return `${repoName} VM ${runtimeId.slice(-8)}`
|
||||
}
|
||||
|
||||
function provisionFailure(failure: EphemeralVmFailure): EphemeralVmProvisionIpcResult {
|
||||
return { ok: false, ...failure, stdout: '', stderr: '' }
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ORCA_ORG_NAME_MAX_LENGTH } from '../../../shared/orca-org-management'
|
||||
import { isValidOrgName, ORCA_ORG_NAME_MAX_LENGTH } from '../../../shared/orca-org-management'
|
||||
import type { OrcaProfileOrgCreateResult } from '../../../shared/orca-org-management'
|
||||
import { ipcMain } from 'electron'
|
||||
import type {
|
||||
@@ -85,7 +85,7 @@ function orgMemberRemoveArgsFromUnknown(args: unknown): OrcaProfileOrgMemberRemo
|
||||
function orgCreateNameFromUnknown(args: unknown): string {
|
||||
const record = args && typeof args === 'object' ? (args as Record<string, unknown>) : {}
|
||||
const name = typeof record.name === 'string' ? record.name.trim() : ''
|
||||
if (!name || name.length > ORCA_ORG_NAME_MAX_LENGTH) {
|
||||
if (!name || name.length > ORCA_ORG_NAME_MAX_LENGTH || !isValidOrgName(name)) {
|
||||
throw new Error('invalid_orca_org_name')
|
||||
}
|
||||
return name
|
||||
|
||||
@@ -99,11 +99,16 @@ function findProjectsByPathArgsFromUnknown(args: unknown): FindOrcaProfileProjec
|
||||
}
|
||||
}
|
||||
|
||||
function orgIdFromUnknown(args: unknown): string {
|
||||
function orgIdFromUnknown(args: unknown): string | null {
|
||||
if (!args || typeof args !== 'object') {
|
||||
throw new Error('invalid_orca_profile_org_selection')
|
||||
}
|
||||
const orgId = (args as SelectOrcaProfileOrgArgs).orgId?.trim()
|
||||
const raw = (args as SelectOrcaProfileOrgArgs).orgId
|
||||
// Explicit null = the personal account.
|
||||
if (raw === null) {
|
||||
return null
|
||||
}
|
||||
const orgId = raw?.trim()
|
||||
if (!orgId) {
|
||||
throw new Error('invalid_orca_profile_org_selection')
|
||||
}
|
||||
|
||||
@@ -2,8 +2,28 @@
|
||||
// opening request is held to its caller's pairing as of before that wake, then to the pairing the
|
||||
// wake made; any other change still refuses it.
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { KnownRuntimeEnvironment } from '../../../shared/remote-runtime/host-catalog/runtime-environments'
|
||||
|
||||
const BEFORE = { id: 'env-vm', pairingRevision: 1, runtimeId: 'before', createdAt: 0 }
|
||||
const BEFORE: KnownRuntimeEnvironment = {
|
||||
id: 'env-vm',
|
||||
name: 'VM',
|
||||
pairingRevision: 1,
|
||||
runtimeId: 'before',
|
||||
createdAt: 0,
|
||||
updatedAt: 0,
|
||||
lastUsedAt: null,
|
||||
endpoints: [
|
||||
{
|
||||
id: 'ep',
|
||||
kind: 'websocket',
|
||||
label: 'VM',
|
||||
endpoint: 'ws://vm.test',
|
||||
deviceToken: 'token',
|
||||
publicKeyB64: 'key'
|
||||
}
|
||||
],
|
||||
preferredEndpointId: 'ep'
|
||||
}
|
||||
const AFTER_WAKE = { id: 'env-vm', pairingRevision: 2, runtimeId: 'after', createdAt: 0 }
|
||||
|
||||
type Prepared = {
|
||||
@@ -73,7 +93,7 @@ describe('a fenced request that waited through a wake already in flight', () =>
|
||||
undefined,
|
||||
{ pairingRevision, runtimeId: 'before' },
|
||||
undefined,
|
||||
{ before: BEFORE as never, wokenPairingRevision }
|
||||
{ before: BEFORE, wokenPairingRevision }
|
||||
)
|
||||
|
||||
it('passes the pre-wake fence and then expects the pairing that wake made', async () => {
|
||||
|
||||
@@ -254,7 +254,8 @@ export async function createOrcaCloudProfile(
|
||||
export async function selectOrcaCloudOrg(
|
||||
config: OrcaCloudAuthConfig,
|
||||
session: OrcaCloudSession,
|
||||
orgId: string
|
||||
/** null asks auth to clear the active team (personal account). */
|
||||
orgId: string | null
|
||||
): Promise<SelectOrgResponse> {
|
||||
const response = await postJson<{
|
||||
cloud: unknown
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { isValidOrgName } from '../../shared/orca-org-management'
|
||||
import type { OrcaProfileOrgCreateResult } from '../../shared/orca-org-management'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { addDevOrganization, removeDevOrganization } from './profile-cloud-dev-auth'
|
||||
@@ -160,7 +161,7 @@ export function removeDevOrcaCloudOrgMember(
|
||||
|
||||
export function createDevOrcaCloudOrg(name: string): OrcaProfileOrgCreateResult {
|
||||
const trimmed = name.trim()
|
||||
if (!trimmed) {
|
||||
if (!isValidOrgName(trimmed)) {
|
||||
return { status: 'invalid' }
|
||||
}
|
||||
const orgId = `dev-team-${randomUUID().slice(0, 8)}`
|
||||
|
||||
@@ -71,7 +71,7 @@ export function refreshDevOrcaCloudProfile(
|
||||
export function selectDevOrcaCloudOrg(
|
||||
active: ActiveOrcaProfileState,
|
||||
userDataPath: string,
|
||||
orgId: string
|
||||
orgId: string | null
|
||||
): DevMutationResult {
|
||||
if (
|
||||
!active.profile.cloud ||
|
||||
@@ -82,7 +82,7 @@ export function selectDevOrcaCloudOrg(
|
||||
const session = createDevOrcaCloudSession({
|
||||
localProfileId: active.profile.id,
|
||||
cloudProfileId: active.profile.cloud.cloudProfileId,
|
||||
orgId
|
||||
orgId: orgId ?? undefined
|
||||
})
|
||||
signInCloudSession(active.profile.id, userDataPath, session)
|
||||
return {
|
||||
|
||||
@@ -285,6 +285,9 @@ export async function createOrcaProfileOrg(
|
||||
if (result.error.statusCode === 429) {
|
||||
return { status: 'rate-limited' }
|
||||
}
|
||||
if (result.error.statusCode === 409) {
|
||||
return { status: 'name-taken' }
|
||||
}
|
||||
return result.error.statusCode === 400
|
||||
? { status: 'invalid' }
|
||||
: { status: 'failed', error: result.error.message }
|
||||
|
||||
@@ -13,7 +13,8 @@ export async function selectCloudOrg(input: {
|
||||
config: OrcaCloudAuthConfig
|
||||
active: ActiveOrcaProfileState
|
||||
userDataPath: string
|
||||
orgId: string
|
||||
/** null clears the selection (personal account). */
|
||||
orgId: string | null
|
||||
}): Promise<OrcaProfileListState | null> {
|
||||
const profileId = input.active.profile.id
|
||||
const cloud = input.active.profile.cloud
|
||||
@@ -43,7 +44,7 @@ export async function selectCloudOrg(input: {
|
||||
if (
|
||||
selected.cloud.userId !== cloud.userId ||
|
||||
selected.cloud.cloudProfileId !== cloud.cloudProfileId ||
|
||||
selected.cloud.activeOrgId !== input.orgId
|
||||
(selected.cloud.activeOrgId ?? null) !== input.orgId
|
||||
) {
|
||||
throw new Error('orca_cloud_org_selection_identity_mismatch')
|
||||
}
|
||||
@@ -51,5 +52,17 @@ export async function selectCloudOrg(input: {
|
||||
if (!list) {
|
||||
throw new Error('stale_cloud_session_mutation')
|
||||
}
|
||||
// /auth/org updates the stored selection but not the access token, and some services read the
|
||||
// team from the token. Refresh now so they follow the switch (best effort; the next refresh
|
||||
// would pick it up anyway).
|
||||
const current = readCloudSession(profileId, input.userDataPath)
|
||||
if (current.status === 'found') {
|
||||
await forceRefreshOrcaCloudSession(
|
||||
input.config,
|
||||
input.active,
|
||||
input.userDataPath,
|
||||
current.session
|
||||
).catch(() => undefined)
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { createHash, randomBytes } from 'node:crypto'
|
||||
import { createServer, type Server, type ServerResponse } from 'node:http'
|
||||
import { shell } from 'electron'
|
||||
import { appendFile } from 'node:fs/promises'
|
||||
import { app, shell } from 'electron'
|
||||
import type { OrcaCloudAuthConfig } from './profile-cloud-auth-config'
|
||||
import {
|
||||
ORCA_CLOUD_CALLBACK_RESPONSE_HEADERS,
|
||||
@@ -149,7 +150,15 @@ export function beginOrcaCloudPkceFlow(
|
||||
authorizeUrl.searchParams.set('code_challenge_method', 'S256')
|
||||
authorizeUrl.searchParams.set('local_profile_id', localProfileId)
|
||||
options.onAuthorizeUrl?.(authorizeUrl.toString())
|
||||
void shell.openExternal(authorizeUrl.toString()).catch((error) => {
|
||||
// Dev automation only (unpackaged): hand the sign-in URL to a test driver instead of
|
||||
// opening the developer's own browser. Same pattern as ORCA_BILLING_DEV_OPEN_URL_FILE.
|
||||
const devUrlFile = app.isPackaged
|
||||
? ''
|
||||
: (process.env.ORCA_CLOUD_DEV_OPEN_URL_FILE?.trim() ?? '')
|
||||
const open = devUrlFile
|
||||
? appendFile(devUrlFile, `${authorizeUrl.toString()}\n`, { mode: 0o600 })
|
||||
: shell.openExternal(authorizeUrl.toString())
|
||||
void open.catch((error) => {
|
||||
rejectFlow(
|
||||
error instanceof Error ? error : new Error('orca_cloud_auth_browser_open_failed')
|
||||
)
|
||||
|
||||
@@ -246,7 +246,7 @@ export async function createCloudLinkedOrcaProfile(
|
||||
|
||||
export async function selectCurrentOrcaProfileOrg(
|
||||
userDataPath: string,
|
||||
orgId: string
|
||||
orgId: string | null
|
||||
): Promise<SelectOrcaProfileOrgResult> {
|
||||
const active = ensureActiveOrcaProfile(userDataPath)
|
||||
if (isOrcaCloudDevAuthEnabled()) {
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { readOrcadDiskUsage, type DiskUsageFs } from './orcad-disk-usage'
|
||||
|
||||
const GIB = 1024 ** 3
|
||||
|
||||
function fakeFs(
|
||||
mounts: Record<string, { dev: number; blocks: number; bfree: number; bavail: number }>
|
||||
): DiskUsageFs & { statfsCalls: string[] } {
|
||||
const statfsCalls: string[] = []
|
||||
return {
|
||||
statfsCalls,
|
||||
stat: vi.fn(async (path: string) => {
|
||||
const mount = mounts[path]
|
||||
if (!mount) {
|
||||
throw Object.assign(new Error('ENOENT'), { code: 'ENOENT' })
|
||||
}
|
||||
return { dev: mount.dev }
|
||||
}),
|
||||
statfs: vi.fn(async (path: string) => {
|
||||
statfsCalls.push(path)
|
||||
const mount = mounts[path]!
|
||||
return { bsize: 4096, blocks: mount.blocks, bfree: mount.bfree, bavail: mount.bavail }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const blocks = (bytes: number): number => bytes / 4096
|
||||
|
||||
describe('readOrcadDiskUsage', () => {
|
||||
it('reports total, free, available and df-style used percent', async () => {
|
||||
const fs = fakeFs({
|
||||
'/vercel/sandbox': {
|
||||
dev: 1,
|
||||
blocks: blocks(32 * GIB),
|
||||
bfree: blocks(2 * GIB),
|
||||
bavail: blocks(1 * GIB)
|
||||
}
|
||||
})
|
||||
const result = await readOrcadDiskUsage(['/vercel/sandbox'], { fs, now: () => 42 })
|
||||
expect(result).toEqual({
|
||||
checkedAt: 42,
|
||||
volumes: [
|
||||
{
|
||||
path: '/vercel/sandbox',
|
||||
totalBytes: 32 * GIB,
|
||||
freeBytes: 2 * GIB,
|
||||
availableBytes: 1 * GIB,
|
||||
// used 30 / (used 30 + avail 1)
|
||||
usedPercent: (30 / 31) * 100
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
|
||||
it('folds paths on one filesystem into one volume and keeps a distinct home', async () => {
|
||||
const fs = fakeFs({
|
||||
'/vercel/sandbox': { dev: 1, blocks: 100, bfree: 50, bavail: 50 },
|
||||
'/vercel/sandbox/repo': { dev: 1, blocks: 100, bfree: 50, bavail: 50 },
|
||||
'/home/user': { dev: 2, blocks: 200, bfree: 10, bavail: 10 }
|
||||
})
|
||||
const result = await readOrcadDiskUsage(
|
||||
['/vercel/sandbox', '/vercel/sandbox/repo', '/vercel/sandbox', '/home/user'],
|
||||
{ fs }
|
||||
)
|
||||
expect(result.volumes.map((v) => v.path)).toEqual(['/vercel/sandbox', '/home/user'])
|
||||
expect(fs.statfsCalls).toEqual(['/vercel/sandbox', '/home/user'])
|
||||
})
|
||||
|
||||
it('measures a missing workspace dir at its nearest existing ancestor', async () => {
|
||||
const fs = fakeFs({ '/home/user': { dev: 2, blocks: 100, bfree: 90, bavail: 90 } })
|
||||
const result = await readOrcadDiskUsage(['/home/user/orca/workspaces'], { fs })
|
||||
expect(result.volumes.map((v) => v.path)).toEqual(['/home/user'])
|
||||
})
|
||||
|
||||
it('skips paths whose statfs fails without failing the probe', async () => {
|
||||
const fs = fakeFs({
|
||||
'/a': { dev: 1, blocks: 100, bfree: 1, bavail: 1 },
|
||||
'/b': { dev: 2, blocks: 100, bfree: 1, bavail: 1 }
|
||||
})
|
||||
fs.statfs = vi.fn(async (path: string) => {
|
||||
if (path === '/a') {
|
||||
throw new Error('EACCES')
|
||||
}
|
||||
return { bsize: 4096, blocks: 100, bfree: 1, bavail: 1 }
|
||||
})
|
||||
const result = await readOrcadDiskUsage(['/a', '/b', null, undefined, ''], { fs })
|
||||
expect(result.volumes.map((v) => v.path)).toEqual(['/b'])
|
||||
})
|
||||
|
||||
it('reads 0% used for an empty filesystem rather than dividing by zero', async () => {
|
||||
const fs = fakeFs({ '/': { dev: 1, blocks: 0, bfree: 0, bavail: 0 } })
|
||||
const result = await readOrcadDiskUsage(['/'], { fs })
|
||||
expect(result.volumes[0]!.usedPercent).toBe(0)
|
||||
})
|
||||
|
||||
it('statfs-es the real filesystem by default', async () => {
|
||||
const result = await readOrcadDiskUsage([process.cwd()])
|
||||
expect(result.volumes).toHaveLength(1)
|
||||
expect(result.volumes[0]!.totalBytes).toBeGreaterThan(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,84 @@
|
||||
// Free space on the filesystems a runtime host writes to, for the desktop's low-disk banner.
|
||||
// statfs/stat only: this runs on a poll from every connected desktop and must stay cheap.
|
||||
import { stat, statfs } from 'node:fs/promises'
|
||||
import { dirname } from 'node:path'
|
||||
import type { OrcadDiskUsageResult, OrcadDiskVolumeUsage } from '../../shared/orcad-disk-usage'
|
||||
|
||||
export type DiskUsageFs = {
|
||||
statfs: (
|
||||
path: string
|
||||
) => Promise<{ bsize: number; blocks: number; bfree: number; bavail: number }>
|
||||
/** Only `dev` is read, to fold paths on one filesystem into one volume. */
|
||||
stat: (path: string) => Promise<{ dev: number }>
|
||||
}
|
||||
|
||||
const nodeFs: DiskUsageFs = {
|
||||
statfs: (path) => statfs(path),
|
||||
stat: (path) => stat(path)
|
||||
}
|
||||
|
||||
// Why a cap: repo lists can be long and repos almost always share the workspace filesystem.
|
||||
const MAX_PROBED_PATHS = 16
|
||||
|
||||
/** The path itself, else its nearest existing ancestor (a configured workspace dir may not exist yet). */
|
||||
async function existingAncestor(fs: DiskUsageFs, path: string): Promise<string | null> {
|
||||
let current = path
|
||||
for (;;) {
|
||||
try {
|
||||
await fs.stat(current)
|
||||
return current
|
||||
} catch {
|
||||
const parent = dirname(current)
|
||||
if (parent === current) {
|
||||
return null
|
||||
}
|
||||
current = parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function toVolume(
|
||||
path: string,
|
||||
stats: { bsize: number; blocks: number; bfree: number; bavail: number }
|
||||
): OrcadDiskVolumeUsage {
|
||||
const totalBytes = stats.blocks * stats.bsize
|
||||
const freeBytes = stats.bfree * stats.bsize
|
||||
const availableBytes = stats.bavail * stats.bsize
|
||||
// df's Use%: used over (used + available), so root-reserved blocks don't read as free.
|
||||
const used = Math.max(0, totalBytes - freeBytes)
|
||||
const denominator = used + availableBytes
|
||||
const usedPercent = denominator > 0 ? Math.min(100, (used / denominator) * 100) : 0
|
||||
return { path, totalBytes, freeBytes, availableBytes, usedPercent }
|
||||
}
|
||||
|
||||
/** `paths` in priority order (workspace root first); unreadable paths are skipped, not fatal. */
|
||||
export async function readOrcadDiskUsage(
|
||||
paths: readonly (string | null | undefined)[],
|
||||
options: { fs?: DiskUsageFs; now?: () => number } = {}
|
||||
): Promise<OrcadDiskUsageResult> {
|
||||
const fs = options.fs ?? nodeFs
|
||||
const seenPaths = new Set<string>()
|
||||
const seenDevices = new Set<number>()
|
||||
const volumes: OrcadDiskVolumeUsage[] = []
|
||||
for (const candidate of paths) {
|
||||
if (!candidate || seenPaths.has(candidate) || seenPaths.size >= MAX_PROBED_PATHS) {
|
||||
continue
|
||||
}
|
||||
seenPaths.add(candidate)
|
||||
try {
|
||||
const path = await existingAncestor(fs, candidate)
|
||||
if (!path) {
|
||||
continue
|
||||
}
|
||||
const { dev } = await fs.stat(path)
|
||||
if (seenDevices.has(dev)) {
|
||||
continue
|
||||
}
|
||||
seenDevices.add(dev)
|
||||
volumes.push(toVolume(path, await fs.statfs(path)))
|
||||
} catch {
|
||||
// A path that vanished or denies access says nothing about free space elsewhere.
|
||||
}
|
||||
}
|
||||
return { volumes, checkedAt: (options.now ?? Date.now)() }
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import { resolveHookCommandSourcePolicy } from '../../../shared/hook-command-sou
|
||||
import { getSetupConfig, getSetupTrustContent } from '../../../shared/orca-hook-setup-config'
|
||||
import { isENOENT } from '../../ipc/filesystem/access/filesystem-path-containment'
|
||||
import { isFolderRepo } from '../../../shared/repo-kind'
|
||||
import { isEnvironmentBuildCheckout } from '../../workspace-hooks/environment-build-checkout'
|
||||
import { inspectSetupScriptImportCandidates } from '../../../shared/workspace-setup/setup-script-imports'
|
||||
import { joinWorktreeRelativePath } from '../files/paths/runtime-relative-paths'
|
||||
import { blockedWslDistroForPath } from '../../wsl/filesystem/wsl-unc-access-gate'
|
||||
@@ -88,7 +89,11 @@ export class RuntimeRepositoryHooksCommands {
|
||||
status: 'ok' as const,
|
||||
hasHooks: has,
|
||||
hooks,
|
||||
mayNeedUpdate: has && !hooks && hasUnrecognizedOrcaYamlKeys(repo.path)
|
||||
mayNeedUpdate: has && !hooks && hasUnrecognizedOrcaYamlKeys(repo.path),
|
||||
// Dependencies come from an Orca Cloud environment build: no setup-script nudge.
|
||||
...((await isEnvironmentBuildCheckout(repo.path))
|
||||
? { environmentProvisioned: true as const }
|
||||
: {})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user