Skip to content

Commit 4f89839

Browse files
anandgupta42claude
andcommitted
fix(workspace): address review on the dogfood fixes
Mark sessions stale on a binding change instead of dropping in-flight loads; fail closed on any unresolvable pin in scope; report a failed memory lookup as incomplete, not off; seed on Attach from the cached dialog; tighten the hidden workspace conflict check; qualify routing copy for local integrations. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8f13d58 commit 4f89839

14 files changed

Lines changed: 181 additions & 60 deletions

File tree

‎docs/docs/usage/cli.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ altimate --agent analyst
5151
Workspace features are off unless `ALTIMATE_WORKSPACE=1` is set. With it:
5252

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.
54-
- `/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, **Open in browser** shows the workspace on the web, **Switch workspace** relinks the project, **Unlink** detaches it. In a project that is not linked yet, it offers **Link to a workspace** instead.
54+
- `/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, **Open in browser** (when a web URL is available) shows the workspace on the web, **Switch workspace** relinks the project, **Unlink** detaches it. In a project that is not linked yet, it offers **Link to a workspace** instead.
5555
- `altimate-code skill publish <name>` uploads a project skill to the linked workspace; see [Skills](../configure/skills.md#cli-commands).
5656
- 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.
57-
- 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, memory and warehouse tool routing, taking priority over whatever the project is linked to on the backend, and is scoped to the folder it was launched for. 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.
57+
- 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, memory and (unless `--integrations local` is set) warehouse tool routing, taking priority over whatever the project is linked to on the backend, and is scoped to the folder it was launched for. 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
6060

‎packages/opencode/src/altimate/workspace/api-client.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ export const HIDDEN_BINDING_MESSAGE =
102102
"Ask its owner to share it with you in the Altimate web app, or to unlink the project, then run `altimate-code link` again."
103103

104104
export function isHiddenBindingConflict(err: unknown): boolean {
105-
return err instanceof ConflictError && !err.detail.existing_datamate_name
105+
// A binding conflict always names the existing workspace's id; a 409 without one is some
106+
// other conflict and must not be explained as a teammate's private workspace.
107+
return (
108+
err instanceof ConflictError &&
109+
typeof err.detail.existing_datamate_id === "number" &&
110+
!err.detail.existing_datamate_name
111+
)
106112
}
107113

108114
export class ConflictError extends Error {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ function renderBody(outcome: BindingOutcome, opts: RenderOptions = {}): string {
124124
`The ${verifyNoun} could not be re-verified just now, so it may since have changed.`
125125
: `${subject} Altimate Workspace id ${id}; ${named}.`) +
126126
(pinned
127-
? " Skills, memory and warehouse tool routing follow this workspace, even where the " +
128-
"project's own link names a different one."
127+
? " Skills and memory follow this workspace, and so does warehouse tool routing unless " +
128+
"integrations are set to local, even where the project's own link names a different one."
129129
: ""),
130130
...(opts.teamMemory ? [TEAM_MEMORY_LINE] : []),
131131
`When ${TRIGGER}, the answer is this Altimate Workspace — never substitute ` +
@@ -407,7 +407,13 @@ export async function systemSection(): Promise<string> {
407407
// read is looser (a file with an empty key still names a tenant and host, and would
408408
// reach the network from here with no memo, no single-flight and a synchronous
409409
// `git remote` probe). Say so and do not invoke it.
410-
if (!scope) return render({ status: "unknown" }, MAX_SECTION_CHARS, { noAccount: true })
410+
// "No account" only when none is configured; a configured but incomplete one gets the
411+
// ordinary could-not-verify copy rather than a pointer to /connect.
412+
if (!scope)
413+
return render({ status: "unknown" }, MAX_SECTION_CHARS, {
414+
noAccount: !(await AltimateApi.isConfigured().catch(() => false)),
415+
pinned: readPin().kind !== "absent",
416+
})
411417
const key = keyFor(scope, directory)
412418
const hit = memo.get(key)
413419
if (fresh(hit)) return render(hit!.outcome, MAX_SECTION_CHARS, renderOptions(hit!.outcome))

‎packages/opencode/src/altimate/workspace/memory-backfill.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
// reaches this through a lazy dynamic import instead.
1212
import { MemoryStore } from "@/memory/store"
1313
import { Log } from "@/altimate/util/log"
14-
import { backfill, isEnabled } from "./memory-sync"
14+
import { backfill, isEnabled, memoryEnabledCached } from "./memory-sync"
1515
import type { CachedBinding } from "./state"
1616

1717
const log = Log.create({ service: "altimate-workspace-memory-backfill" })
1818

1919
/** What a bind's memory seed concluded. `off` is "never ran" (memory disabled here or
2020
* for the workspace), `incomplete` is "ran and left blocks behind"; `link` reports the
2121
* two differently, since only the second needs the user to retry a Sync. */
22-
export type SeedOutcome = { status: "seeded" | "off" | "incomplete"; sent: number; pending: number }
22+
export type SeedOutcome = { status: "seeded" | "already" | "off" | "incomplete"; sent: number; pending: number }
2323

2424
/** Push every non-expired local block. Throttled and resumable inside
2525
* ``backfill`` — blocks already synced at their current payload are skipped, so
@@ -40,7 +40,11 @@ export async function seedOnBind(directory: string, binding: CachedBinding): Pro
4040
if (blocks.length === 0) return { status: "seeded", sent: 0, pending: 0 }
4141
const result = await backfill(blocks, binding, directory)
4242
log.info("workspace memory seeded after bind", result)
43-
if (result.gated) return { status: "off", sent: 0, pending: 0 }
43+
// `gated` also covers a failed enablement lookup; only a confirmed toggle is "off".
44+
if (result.gated)
45+
return memoryEnabledCached(binding) === "disabled"
46+
? { status: "off", sent: 0, pending: 0 }
47+
: { status: "incomplete", sent: 0, pending: blocks.length }
4448
// Only a sweep that stored everything it meant to counts as seeded. A
4549
// failure here must leave the binding eligible for a retry, or local blocks
4650
// stay absent from the workspace until a rebind or an unrelated edit.

‎packages/opencode/src/altimate/workspace/memory-sync.ts‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ interface SessionMemory {
7575
touchedAt: number
7676
/** Set once a bounded wait expired, so later injections do not re-wait. */
7777
waitTimedOut?: boolean
78+
/** The load has settled; a stale session is reloaded only then. */
79+
settled?: boolean
80+
/** The binding changed after this session's load started: reload on the next turn. */
81+
stale?: boolean
7882
}
7983

8084
const sessions = new Map<string, SessionMemory>()
@@ -1065,11 +1069,22 @@ export function belongsHere(record: CloudMemoryRecord, ownWorkspace: string | un
10651069
* workspace memory blink out of the prompt whenever a fetch ran long. */
10661070
export async function hydrate(sessionID: string): Promise<void> {
10671071
if (!isEnabled()) return
1068-
const state = sessionState(sessionID)
1072+
let state = sessionState(sessionID)
1073+
// A relink since the last load: start over from the new binding. An in-flight
1074+
// load is left to finish (it may be the one that discovered the binding) and
1075+
// the reload happens on the turn after.
1076+
if (state.stale && state.settled) {
1077+
sessions.delete(sessionID)
1078+
state = sessionState(sessionID)
1079+
}
10691080
if (state.hydration) return state.hydration
10701081
// The overlay is deliberately NOT cleared before loading: clearing first made
10711082
// workspace memory blink out of the prompt whenever a fetch ran long.
1072-
state.hydration = loadWorkspaceMemory().then((outcome) => commitLoad(sessionID, state, outcome))
1083+
const launched = state
1084+
state.hydration = loadWorkspaceMemory().then((outcome) => {
1085+
launched.settled = true
1086+
commitLoad(sessionID, launched, outcome)
1087+
})
10731088
return state.hydration
10741089
}
10751090

@@ -1188,16 +1203,12 @@ export async function refresh(sessionID: string, directory?: string): Promise<Re
11881203
if (!isEnabled()) return { count: 0, ok: false, status: "off" }
11891204
return serialize("global", `refresh:${sessionID}`, async () => {
11901205
const previous = overlayBlocks(sessionID)
1191-
const generation = overlayGeneration
1206+
const epoch = bindingEpoch
11921207
// `directory` is threaded through rather than resolved from the ambient
11931208
// instance: the headless adapter this module serves has no instance, and
11941209
// `manage.refresh(directory, sessionID)` promises the directory it was
11951210
// given is the one that gets refreshed.
11961211
const outcome = await loadWorkspaceMemory(directory)
1197-
// A relink landed while this load was in flight: what it read (or the
1198-
// overlay it would restore) belongs to the previous workspace. Leave the
1199-
// session empty so its next turn hydrates from the new one.
1200-
if (generation !== overlayGeneration) return { count: 0, ok: false, status: "error" }
12011212
if (outcome.status === "error") {
12021213
// Keep what the session had. Emptying it because the network hiccuped is
12031214
// strictly worse than not reloading, and the user asked for a reload.
@@ -1210,6 +1221,10 @@ export async function refresh(sessionID: string, directory?: string): Promise<Re
12101221
sessions.delete(sessionID)
12111222
const state = sessionState(sessionID)
12121223
state.hydration = Promise.resolve()
1224+
state.settled = true
1225+
// A binding change landed while this load was in flight, so what it read may
1226+
// be the previous workspace's: keep it for now and reload on the next turn.
1227+
state.stale = epoch !== bindingEpoch
12131228
commitLoad(sessionID, state, outcome)
12141229
return {
12151230
count: state.overlay.length,
@@ -1219,16 +1234,15 @@ export async function refresh(sessionID: string, directory?: string): Promise<Re
12191234
})
12201235
}
12211236

1222-
/** Bumped by a full reset, so a `refresh` that started before it does not write back. */
1223-
let overlayGeneration = 0
1237+
/** Bumped on every binding change, so a `refresh` can tell one landed during its load. */
1238+
let bindingEpoch = 0
12241239

12251240
/** Forget a session's hydration, or all of them.
12261241
*
12271242
* Not called per turn: doing so defeated ``hydrate``'s idempotence and made
12281243
* every turn refetch. Exposed for tests and for a future session-end hook. */
12291244
export function resetOverlay(sessionID?: string): void {
12301245
if (sessionID === undefined) {
1231-
overlayGeneration++
12321246
sessions.clear()
12331247
// Both memos, not just the positive one. A refresh after memory was turned
12341248
// ON for a workspace last seen off otherwise kept reporting zero unsynced
@@ -1242,5 +1256,10 @@ export function resetOverlay(sessionID?: string): void {
12421256

12431257
// A link, relink, unlink or server-side rebind swaps the workspace under every open
12441258
// session, and `hydrate` loads once per session: without this, a session that pulled
1245-
// workspace A's memory keeps injecting it after the project is relinked to B.
1246-
onBindingChanged(() => resetOverlay())
1259+
// workspace A's memory keeps injecting it after the project is relinked to B. Marked
1260+
// rather than dropped: the notification can come from a load discovering its own
1261+
// binding, and discarding that load would leave the first turn without memory.
1262+
onBindingChanged(() => {
1263+
bindingEpoch++
1264+
for (const state of sessions.values()) state.stale = true
1265+
})

‎packages/opencode/src/altimate/workspace/skill-sync.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -807,17 +807,17 @@ export async function syncSkills(directory: string): Promise<{ changed: boolean
807807
if (outcome.status === "unbound") {
808808
if (await deactivate(canon, "this project is no longer bound to a workspace")) changed = true
809809
}
810-
// An IDE pin that cannot be honoured resolves `unknown` and fails closed for
811-
// memory and routing; a snapshot of some other workspace (usually the
812-
// project's own link) must not keep serving skills in its place. Only inside
813-
// the pinned root, which is the folder the pin speaks for; a snapshot of the
814-
// pinned workspace itself survives, since that may just be a blip.
810+
// An IDE pin that cannot be honoured resolves `unknown`, and memory and routing fail
811+
// closed on it; the snapshot must too, or its skills keep loading from disk. A blip after
812+
// a successful validation resolves `bound` (stale) instead, so `unknown` here means the
813+
// pin is malformed, refused, no longer visible, or was never confirmed. Scoped to the
814+
// folder a valid pin speaks for; a malformed pin names no folder, so it covers every one.
815815
const pin = readPin()
816-
if (outcome.status === "unknown" && pin.kind === "valid" && resolveWithinRoot(canon, pin.root)) {
817-
const manifest = await readManifest(canon)
818-
if (manifest && manifest.datamateId !== pin.datamateId) {
819-
if (await deactivate(canon, "the workspace pin could not be honoured")) changed = true
820-
}
816+
if (
817+
outcome.status === "unknown" &&
818+
(pin.kind === "invalid" || (pin.kind === "valid" && resolveWithinRoot(canon, pin.root)))
819+
) {
820+
if (await deactivate(canon, "the workspace pin could not be honoured")) changed = true
821821
}
822822
return
823823
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ export async function recordApprovedBinding(
11771177
// Skip only when this exact binding has already been seeded successfully. A
11781178
// warm after a failed or skipped seed must try again, or the blocks this
11791179
// machine already holds never reach the workspace.
1180-
if (alreadySeeded) return { status: "seeded", sent: 0, pending: 0 }
1180+
if (alreadySeeded) return { status: "already", sent: 0, pending: 0 }
11811181
if (opts?.seed === false) return null
11821182
const seeded = import("./memory-backfill")
11831183
.then((m) => m.seedOnBind(canonicalizeKey(directory), binding))

‎packages/opencode/src/cli/cmd/link.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,9 @@ async function bindOrRebind(
681681
try {
682682
res = await WorkspaceApi.bindExisting(targetDatamateId, identifier)
683683
} catch (err) {
684-
if (err instanceof ConflictError && !preCheckOk) {
684+
// A teammate's private workspace is not a pre-check race: rebinding it only fails
685+
// again (forbidden), and would hide the explanation the outer handler gives.
686+
if (err instanceof ConflictError && !preCheckOk && !isHiddenBindingConflict(err)) {
685687
// Pre-check failed and the server confirms this project IS linked
686688
// already. Retry as an unconditional rebind — we don't have an
687689
// ``expected_current_datamate_id`` (pre-check gave us nothing) so
@@ -784,7 +786,10 @@ export function seedMessage(seed: SeedOutcome | null): string {
784786
return seed.sent > 0
785787
? `Sent ${seed.sent} saved memor${seed.sent === 1 ? "y" : "ies"} to the workspace.`
786788
: "Saved memory is in sync with the workspace."
787-
return "Workspace memory is off, so saved memory stays on this machine."
789+
if (seed?.status === "already") return "Saved memory was already sent to this workspace."
790+
if (seed?.status === "off") return "Workspace memory is off, so saved memory stays on this machine."
791+
// null: the seed could not run here (no resolvable credentials), which is not "off".
792+
return "Saved memory could not be checked against the workspace. Run /workspace → Sync in the TUI to retry."
788793
}
789794

790795
/** Pick the rebind endpoint that matches which identifier the pre-check

‎packages/opencode/src/plugin/tui/altimate/workspace.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,7 @@ async function runFlow(api: TuiPluginApi, directory: string): Promise<void> {
12661266
driftedWas={hasDrift ? cachedIdent : undefined}
12671267
manageUrl={manageUrl}
12681268
unverified
1269+
onAttach={() => recordApprovedBinding(directory, local)}
12691270
/>
12701271
))
12711272
return

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ describe("systemSection", () => {
758758
const out = await inProject(systemSection)
759759
expect(out).toContain("This session is pinned by the IDE extension to Altimate Workspace id 237")
760760
expect(out).toContain('is "pinned-ws-server"')
761-
expect(out).toContain("Skills, memory and warehouse tool routing follow this workspace")
761+
expect(out).toContain("and so does warehouse tool routing unless integrations are set to local")
762762
expect(out).not.toContain("id 12")
763763
expect(out).not.toContain("This project is linked to")
764764
} finally {
@@ -805,6 +805,19 @@ describe("systemSection", () => {
805805
}
806806
})
807807

808+
test("no configured account says so and points at /connect", async () => {
809+
const api = AltimateApi as unknown as { isConfigured: () => Promise<boolean> }
810+
const original = api.isConfigured
811+
api.isConfigured = async () => false
812+
try {
813+
const out = await inProject(systemSection)
814+
expect(out).toContain("No Altimate account is connected")
815+
expect(out).toContain("/connect")
816+
} finally {
817+
api.isConfigured = original
818+
}
819+
})
820+
808821
test("a credentials file with an empty key renders unknown without invoking the resolver", async () => {
809822
// `accountScope` refuses the empty key, and nothing can verify a link without one —
810823
// so the resolver (whose own credential read still names a tenant and host, and
@@ -823,7 +836,9 @@ describe("systemSection", () => {
823836
const started = Date.now()
824837
const out = await inProject(systemSection)
825838
expect(Date.now() - started).toBeLessThan(500)
826-
expect(out).toContain("No Altimate account is connected")
839+
// Configured but incomplete is not "no account": the could-not-verify copy applies.
840+
expect(out).toContain("could not be verified")
841+
expect(out).not.toContain("No Altimate account is connected")
827842
expect(resolves).toBe(0)
828843
} finally {
829844
identityInternals.resolveBindingOutcome = realResolve

0 commit comments

Comments
 (0)