Skip to content

Commit 0ae7ca1

Browse files
sahrizviclaude
andcommitted
fix(workspace): address the bot and multi-model review on the v0.12.1 release PR
- `stripHostMarkers` deletes every spelling of a marker on Windows, where environment names are case-insensitive, and the exact name elsewhere. The nested `altimate_change` markers around its call site are gone. - `shell.ts` exposes `shellChildEnv` so the persistent shell's child environment is tested on values, not on this file's source text. - `readPin` treats a whitespace-only id, name or root as a broken pin. - Identity keeps the resolver behind the deadline even when no complete account is configured: the resolver's own credential read is looser than `accountScope` and can still reach the network. - The adversarial test file no longer mutates `XDG_STATE_HOME`; the preload already isolates state and nothing here reads it. - Docs: `ALTIMATE_CODE_SERVE` is set by `serve` itself, not only by the extension; ordinary and pinned sessions described separately. - CHANGELOG narrows the credential-scoping claim to the layer this release fixes: the resolver's own five-minute caches are still keyed by tenant and host (tracked separately). Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent 266e88b commit 0ae7ca1

9 files changed

Lines changed: 99 additions & 44 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Two workspace-pilot additions that landed right after 0.12.0, plus the fixes the
1717
### Fixed
1818

1919
- **Routing and identity can no longer contradict each other about the link.** The routing section used to say "this project is bound to workspace X" from a snapshot taken at tool resolution; it now says which workspace *serves* the tools, and only the identity section states the link. (#1330)
20-
- **A pinned session is described as pinned, with the routing caveat**, and a pin served from the offline grace window is marked "last known" like a cached link. Identity's per-account memo is keyed on the credential, not only the tenant, so two accounts on one tenant never share an answer; under a pin the deadline fallback never reaches for the project's own cached link. Found in this release's review.
20+
- **A pinned session is described as pinned, with the routing caveat**, and a pin served from the offline grace window is marked "last known" like a cached link. Identity's per-turn memo is keyed on the credential, not only the tenant, so two accounts on one tenant never share that memo; the resolver's own five-minute caches underneath it are still keyed by tenant and host, so a same-tenant account switch can still be answered from the previous account's cached link for up to five minutes (tracked separately). Under a pin the deadline fallback never reaches for the project's own cached link. Found in this release's review.
2121
- **The persistent `shell` tool strips the same host markers as `bash`** (`ALTIMATE_CODE_SERVE`, the pin variables, headless and non-interactive), so a nested `altimate-code serve` started from it cannot inherit a pin it was never given. Found in this release's review.
2222
- **Pin ids are decimal digits only** — `Number()` also accepted `1e3` and `0x10` — and the identity section's size cap now fits its longest shape with a maximum-length name instead of dropping the name. Found by this release's adversarial tests.
2323

‎docs/docs/usage/cli.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Workspace features are off unless `ALTIMATE_WORKSPACE=1` is set. With it:
5353
- `altimate-code link` links the current project to a workspace (or creates one). The sidebar then names the workspace and shows how many memories are not yet synced and when skills last synced.
5454
- `/workspace` in the TUI opens a menu: **Refresh** pulls the workspace's skills and memory into this project, **Sync** re-sends local memory the workspace never received, **Unlink** detaches the project.
5555
- `altimate-code skill publish <name>` uploads a project skill to the linked workspace; see [Skills](../configure/skills.md#cli-commands).
56-
- The agent is told every turn which workspace the project is linked to — or that none is, or that the link could not be verified just now — so "which workspace am I in?" always has an answer. It is told to treat that as the Altimate Workspace and not to confuse it with a Databricks workspace or an IDE workspace folder.
56+
- In an ordinary session the agent is told every turn which workspace the project is linked to — or that none is, or that the link could not be verified just now. In an extension-pinned session it is told the pinned workspace instead, and that it differs from the project's own link. Either way "which workspace am I in?" has an answer, and the agent is told not to confuse it with a Databricks workspace or an IDE workspace folder.
5757
- When `altimate-code serve` is launched by the VS Code / Cursor extension, the workspace selected in the extension's panel governs that session's skills and memory, taking priority over whatever the project is linked to on the backend, and is scoped to the folder it was launched for. Warehouse tool routing still follows the project's own link for now. A pin is fixed for the life of the `serve` process, so the extension relaunches `serve` when the selection changes; nothing updates a running one.
5858

5959
## Global Flags
@@ -98,7 +98,7 @@ Configuration can be controlled via environment variables:
9898
| `ALTIMATE_CLI_DISABLE_MODELS_FETCH` | Don't fetch models from models.dev |
9999
| `ALTIMATE_WORKSPACE` | Opt into the workspace pilot (`1`). Off by default; nothing about workspaces is active without it |
100100
| `ALTIMATE_INTEGRATIONS` | Set to `local` to keep warehouse tools local rather than routing them through a bound workspace's engine |
101-
| `ALTIMATE_CODE_SERVE` | Set by the IDE extension on the `serve` process it launches. Marks that process as the extension's host; stripped from every child the bash and shell tools start |
101+
| `ALTIMATE_CODE_SERVE` | Set to `1` by `altimate-code serve` itself, whether the IDE extension or you launched it. Marks that process as the extension's host — so it is the one that reads the pin variables — and is stripped from every child the bash and shell tools start |
102102
| `ALTIMATE_PINNED_WORKSPACE_ID` / `_NAME` / `_ROOT` | Set together by the IDE extension on `serve`: the workspace selected in its panel and the folder it applies to. All three or none — a partial pin is refused rather than ignored. Read only when `ALTIMATE_CODE_SERVE` is set; never persisted; stripped from child processes |
103103

104104
### Server & Security

‎packages/opencode/src/altimate/workspace/identity.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,19 +343,31 @@ export async function systemSection(): Promise<string> {
343343
try {
344344
const directory = Instance.directory
345345
const scope = await accountScope()
346-
// No account to ask with: nothing to memoise under, and the resolver answers
347-
// from the local cache alone without touching the network.
348-
if (!scope) return render(await resolveBindingOutcome(directory))
349-
const key = keyFor(scope, directory)
350-
const hit = memo.get(key)
351-
if (fresh(hit)) return render(hit!.outcome)
352346
const timers: ReturnType<typeof setTimeout>[] = []
353347
const after = (ms: number, value: () => BindingOutcome | Promise<BindingOutcome>) =>
354348
new Promise<BindingOutcome>((done) => {
355349
const t = setTimeout(() => done(value()), ms)
356350
t.unref?.()
357351
timers.push(t)
358352
})
353+
// No complete account to memoise under. The resolver is still behind the deadline:
354+
// its own credential read is looser than `accountScope` (a file with an empty key
355+
// still names a tenant and host), so it can reach the network from here.
356+
if (!scope) {
357+
try {
358+
return render(
359+
await Promise.race([
360+
identityInternals.resolveBindingOutcome(directory),
361+
after(RESOLVE_DEADLINE_MS, () => ({ status: "unknown" })),
362+
]),
363+
)
364+
} finally {
365+
for (const t of timers) clearTimeout(t)
366+
}
367+
}
368+
const key = keyFor(scope, directory)
369+
const hit = memo.get(key)
370+
if (fresh(hit)) return render(hit!.outcome)
359371
// The fallback is itself raced against a small budget, so the wait is
360372
// bounded by RESOLVE_DEADLINE_MS + FALLBACK_BUDGET_MS, not by the disk.
361373
const deadline = after(RESOLVE_DEADLINE_MS, () =>

‎packages/opencode/src/altimate/workspace/pin.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function readPin(env: NodeJS.ProcessEnv = process.env): PinState {
125125

126126
// Partial or empty is invalid, never "good enough". The extension sets all three or none;
127127
// anything else means something rewrote the environment and we no longer know what was intended.
128-
if (!rawId || !name || !root) {
128+
if (!rawId?.trim() || !name?.trim() || !root?.trim()) {
129129
return { kind: "invalid", reason: "pin is partially set or empty" }
130130
}
131131

‎packages/opencode/src/tool/bash.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,20 @@ export function stripRunModeMarkers(env: Record<string, string | undefined>) {
5656
* ever come from the process the extension launched.
5757
*
5858
* Shared by every tool that spawns a shell (`bash`, `shell`) so the two cannot drift. */
59-
export function stripHostMarkers(env: Record<string, string | undefined>) {
60-
delete env["ALTIMATE_NON_INTERACTIVE"]
61-
delete env["ALTIMATE_CODE_HEADLESS"]
62-
delete env["ALTIMATE_CODE_SERVE"]
63-
delete env["ALTIMATE_PINNED_WORKSPACE_ID"]
64-
delete env["ALTIMATE_PINNED_WORKSPACE_NAME"]
65-
delete env["ALTIMATE_PINNED_WORKSPACE_ROOT"]
59+
const HOST_MARKERS = new Set([
60+
"ALTIMATE_NON_INTERACTIVE",
61+
"ALTIMATE_CODE_HEADLESS",
62+
"ALTIMATE_CODE_SERVE",
63+
"ALTIMATE_PINNED_WORKSPACE_ID",
64+
"ALTIMATE_PINNED_WORKSPACE_NAME",
65+
"ALTIMATE_PINNED_WORKSPACE_ROOT",
66+
])
67+
export function stripHostMarkers(env: Record<string, string | undefined>, platform: NodeJS.Platform = process.platform) {
68+
// Windows environment names are case-insensitive: a child reads `altimate_code_serve`
69+
// as the marker, so every spelling goes there. POSIX keeps exact matching.
70+
for (const key of Object.keys(env)) {
71+
if (HOST_MARKERS.has(platform === "win32" ? key.toUpperCase() : key)) delete env[key]
72+
}
6673
return env
6774
}
6875
// altimate_change end
@@ -216,11 +223,9 @@ export const BashTool = Tool.define("bash", async () => {
216223

217224
// altimate_change start — prepend bundled tools dir (ALTIMATE_BIN_DIR) and user tools dirs to PATH
218225
const mergedEnv: Record<string, string | undefined> = { ...process.env, ...shellEnv.env }
219-
// altimate_change start — strip the markers that describe this host, not the child.
220-
// See `stripHostMarkers` for what each one is and why (PR #937 review, Issue #3, for
221-
// the first of them).
226+
// Strip the markers that describe this host, not the child. See `stripHostMarkers`
227+
// for what each one is and why (PR #937 review, Issue #3, for the first of them).
222228
stripHostMarkers(mergedEnv)
223-
// altimate_change end
224229
// altimate_change start — strip the run-mode markers for the same reason.
225230
stripRunModeMarkers(mergedEnv)
226231
// altimate_change end

‎packages/opencode/src/tool/shell.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { Effect, Stream } from "effect"
22
// altimate_change start — shared child-env hygiene with the bash tool
33
import { stripHostMarkers, stripRunModeMarkers } from "./bash"
4+
5+
/** The environment a persistent-shell child receives: the process environment plus
6+
* plugin extras, minus the markers that describe THIS host. Exported so the contract
7+
* is tested on values rather than on this file's source text. */
8+
export function shellChildEnv(
9+
extra: Record<string, string | undefined>,
10+
base: Record<string, string | undefined> = process.env,
11+
): Record<string, string | undefined> {
12+
return stripRunModeMarkers(stripHostMarkers({ ...base, ...extra }))
13+
}
414
// altimate_change end
515
import os from "os"
616
import { createWriteStream } from "node:fs"
@@ -432,12 +442,7 @@ export const ShellTool = Tool.define(
432442
// strips the same host markers (serve, headless, non-interactive, the IDE pin) and
433443
// run-mode markers; otherwise a nested `altimate-code serve` started from this tool
434444
// inherited a pin the session was never given, which `bash` already guarded against.
435-
return stripRunModeMarkers(
436-
stripHostMarkers({
437-
...process.env,
438-
...extra.env,
439-
}),
440-
)
445+
return shellChildEnv(extra.env)
441446
// altimate_change end
442447
})
443448

‎packages/opencode/test/altimate/workspace/identity-section.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,25 @@ describe("systemSection", () => {
732732
}
733733
})
734734

735+
test("a credentials file with an empty key still keeps the resolver behind the deadline", async () => {
736+
// `accountScope` refuses the empty key (nothing to memoise under), but the resolver's
737+
// own credential read still names a tenant and host and can reach the network — so
738+
// the no-scope path must be raced against the deadline too, or a hung server stalls
739+
// prompt assembly for the request timeout.
740+
const original = (AltimateApi as unknown as { getCredentials: () => Promise<Creds> }).getCredentials
741+
;(AltimateApi as unknown as { getCredentials: () => Promise<Creds> }).getCredentials = async () =>
742+
({ altimateInstanceName: "acme", altimateUrl: "https://api.example.com", altimateApiKey: "" }) as Creds
743+
globalThis.fetch = (() => new Promise(() => {})) as unknown as typeof fetch
744+
try {
745+
const started = Date.now()
746+
const out = await inProject(systemSection)
747+
expect(Date.now() - started).toBeLessThan(RESOLVE_DEADLINE_MS + 500)
748+
expect(out).toContain("could not be verified")
749+
} finally {
750+
;(AltimateApi as unknown as { getCredentials: () => Promise<Creds> }).getCredentials = original
751+
}
752+
})
753+
735754
test("degrades to the unverified copy outside an instance context rather than throwing", async () => {
736755
// `Instance.directory` throws outside a context; prompt assembly must not.
737756
const out = await systemSection()

‎packages/opencode/test/cli/run/run-mode.test.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,30 @@ describe("host markers do not leak into child processes", () => {
181181
expect(env["PATH"]).toBe("/bin")
182182
})
183183

184-
test("the shell tool's environment builder goes through the same stripping as bash", async () => {
185-
// Read the source of the seam, not the behaviour: `shellEnv` is an Effect inside
186-
// the tool factory with no public handle. The import is the contract.
187-
const src = await Bun.file(new URL("../../../src/tool/shell.ts", import.meta.url)).text()
188-
expect(src).toContain('import { stripHostMarkers, stripRunModeMarkers } from "./bash"')
189-
expect(src).toMatch(/stripRunModeMarkers\(\s*stripHostMarkers\(\{\s*\.\.\.process\.env/)
184+
test("the shell tool's child environment is stripped the same way as bash's", async () => {
185+
const { shellChildEnv } = await import("../../../src/tool/shell")
186+
const env = shellChildEnv(
187+
{ FROM_PLUGIN: "1", ALTIMATE_PINNED_WORKSPACE_ID: "from-plugin-too" },
188+
{ ALTIMATE_CODE_SERVE: "1", ALTIMATE_PINNED_WORKSPACE_ROOT: "/p", ALTIMATE_RUN_MODE: "1", PATH: "/bin" },
189+
)
190+
expect(env.ALTIMATE_CODE_SERVE).toBeUndefined()
191+
expect(env.ALTIMATE_PINNED_WORKSPACE_ROOT).toBeUndefined()
192+
expect(env.ALTIMATE_PINNED_WORKSPACE_ID).toBeUndefined() // a plugin cannot smuggle one in either
193+
expect(env.ALTIMATE_RUN_MODE).toBeUndefined()
194+
expect(env.FROM_PLUGIN).toBe("1")
195+
expect(env.PATH).toBe("/bin")
196+
})
197+
198+
test("on Windows every spelling of a marker is stripped; on POSIX only the exact name", () => {
199+
const env = { altimate_code_serve: "1", Altimate_Pinned_Workspace_Id: "7", ALTIMATE_CODE_SERVE: "1", PATH: "x" }
200+
const win = stripHostMarkers({ ...env }, "win32")
201+
expect(win.altimate_code_serve).toBeUndefined()
202+
expect(win.Altimate_Pinned_Workspace_Id).toBeUndefined()
203+
expect(win.ALTIMATE_CODE_SERVE).toBeUndefined()
204+
expect(win.PATH).toBe("x")
205+
const posix = stripHostMarkers({ ...env }, "linux")
206+
expect(posix.altimate_code_serve).toBe("1") // a different variable there
207+
expect(posix.ALTIMATE_CODE_SERVE).toBeUndefined()
190208
})
191209
})
192210

‎packages/opencode/test/skill/release-v0.12.1-adversarial.test.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,19 @@
2020
* binding, and a `stale` unbound outcome — the copy must carry the pin and the staleness
2121
* in every combination and never collapse into the plain "linked to" claim.
2222
*
23-
* Rules: no `mock.module()`; the real state dir is never touched; `readPin` and
24-
* `stripHostMarkers` are always handed an explicit env object.
23+
* Rules: no `mock.module()`; no process-global mutation (state isolation is the preload's);
24+
* `readPin` and `stripHostMarkers` are always handed an explicit env object.
2525
*/
2626
import { afterAll, describe, expect, test } from "bun:test"
2727
import { mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"
2828
import os from "node:os"
2929
import path from "node:path"
3030

31-
const ORIGINAL_XDG_STATE_HOME = process.env.XDG_STATE_HOME
31+
// State isolation comes from `test/preload.ts`; nothing here touches `Global.Path.state`,
32+
// and every `readPin`/`stripHostMarkers` call is handed an explicit environment.
3233
const SANDBOX = path.join(os.tmpdir(), `altimate-v0121-adv-${process.pid}-${Date.now()}`)
33-
mkdirSync(path.join(SANDBOX, "state"), { recursive: true })
34-
process.env.XDG_STATE_HOME = path.join(SANDBOX, "state")
34+
mkdirSync(SANDBOX, { recursive: true })
3535
afterAll(() => {
36-
if (ORIGINAL_XDG_STATE_HOME === undefined) delete process.env.XDG_STATE_HOME
37-
else process.env.XDG_STATE_HOME = ORIGINAL_XDG_STATE_HOME
3836
rmSync(SANDBOX, { recursive: true, force: true })
3937
})
4038

@@ -67,12 +65,10 @@ describe("v0.12.1 adversarial: readPin against hostile environments", () => {
6765
}
6866
})
6967

70-
test("whitespace-only values are a broken pin, not an absent one", () => {
68+
test("whitespace-only values are a broken pin, not an absent one and not a valid one", () => {
7169
for (const key of ["ALTIMATE_PINNED_WORKSPACE_ID", "ALTIMATE_PINNED_WORKSPACE_NAME", "ALTIMATE_PINNED_WORKSPACE_ROOT"]) {
7270
// Present-but-empty is already `invalid`; whitespace must not sneak past as content.
73-
const pin = readPin(pinEnv({ [key]: " " }))
74-
expect(pin.kind).not.toBe("absent")
75-
if (key === "ALTIMATE_PINNED_WORKSPACE_ROOT") expect(pin.kind).toBe("invalid") // not absolute
71+
expect(readPin(pinEnv({ [key]: " " })).kind).toBe("invalid")
7672
}
7773
})
7874

0 commit comments

Comments
 (0)