Skip to content

Commit 92852c4

Browse files
sahrizviclaude
andcommitted
fix(workspace): judge a confirmed link by the row written, not the seed outcome (review)
`recordApprovedBinding` returns `account-changed` both before its write and after it, when only the memory seed was skipped. `link` now checks for an approved, non-adopted row under the captured account (`isApprovedRow`) and reports the seed outcome separately. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018fJ3X7pcGT4R9yzjsJnqsV
1 parent a68b4c4 commit 92852c4

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,20 @@ export function peekRowUnscoped(directory: string): UnscopedRow | null {
916916
}
917917
}
918918

919+
/** Whether this directory holds an approved (not adopted) row for `datamateId` written under `account`.
920+
* `recordApprovedBinding` returns "account-changed" both before its write and after it (at the memory seed);
921+
* this tells the caller which one happened. */
922+
export function isApprovedRow(directory: string, account: string, datamateId: number): boolean {
923+
try {
924+
const cache = readCache()
925+
if (!cache || cache.account !== account) return false
926+
const row = primaryRow(cache, directory)
927+
return row?.datamateId === datamateId && !row.adopted
928+
} catch {
929+
return false
930+
}
931+
}
932+
919933
/** The row reads win for a directory: the canonical key, or failing that the
920934
* newest alias. */
921935
function primaryRow(cache: CacheFile, directory: string): CachedBinding | undefined {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343
resolveWorkspaceWebUrl,
4444
type HandoffResult,
4545
} from "@/altimate/workspace/browser-handoff"
46-
import { accountDigest, credentialDigest, recordApprovedBinding } from "@/altimate/workspace/state"
46+
import { accountDigest, credentialDigest, isApprovedRow, recordApprovedBinding } from "@/altimate/workspace/state"
4747
import type { SeedOutcome } from "@/altimate/workspace/memory-backfill"
4848
import {
4949
confirmsNamesake,
@@ -1152,8 +1152,9 @@ function linkHeadlessDeps(directory: string): LinkHeadlessDeps {
11521152
// Pinned to the credential the link was looked up with: refused if the configured account is another one now.
11531153
const account = credentialDigest(actAs.url, actAs.instance, actAs.apiKey)
11541154
if ((await accountDigest()) !== account) return false
1155+
const key = identifier.projectPath ?? directory
11551156
const seed = await recordApprovedBinding(
1156-
identifier.projectPath ?? directory,
1157+
key,
11571158
{
11581159
datamateId: existing.datamate.id,
11591160
datamateName: existing.datamate.name,
@@ -1163,7 +1164,9 @@ function linkHeadlessDeps(directory: string): LinkHeadlessDeps {
11631164
},
11641165
{ awaitBackfill: true, account },
11651166
)
1166-
if (seed === null || seed.status === "account-changed") return false
1167+
// The row, not the seed outcome, says whether the approval was written: "account-changed" also comes back
1168+
// after the write, when only the memory seed was skipped (seedMessage says so).
1169+
if (!isApprovedRow(key, account, existing.datamate.id)) return false
11671170
UI.println(seedMessage(seed))
11681171
return true
11691172
},

‎packages/opencode/test/altimate/workspace/state-account-scope.test.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const SANDBOX = path.join(os.tmpdir(), `altimate-state-account-${process.pid}-${
2121
mkdirSync(path.join(SANDBOX, "state"), { recursive: true })
2222
process.env.OPENCODE_TEST_STATE_HOME = path.join(SANDBOX, "state")
2323

24-
const { recordApprovedBinding, readLocalBinding, clearLocalBinding, cachePath, credentialDigest } =
24+
const { recordApprovedBinding, readLocalBinding, clearLocalBinding, cachePath, credentialDigest, isApprovedRow } =
2525
await import("../../../src/altimate/workspace/state")
2626
const { AltimateApi } = await import("../../../src/altimate/api/client")
2727

@@ -178,3 +178,35 @@ describe("binding cache is scoped to the account, not the tenant", () => {
178178
expect(a).not.toContain("key-A")
179179
})
180180
})
181+
182+
describe("isApprovedRow: whether an approval was actually written", () => {
183+
const A = credentialDigest(API_URL, TENANT, "key-a")
184+
185+
test("a row written under the account is approved", async () => {
186+
asAccount("key-a")
187+
await recordApprovedBinding(ROOT, binding(35, "Growth"), { account: A, seed: false })
188+
expect(isApprovedRow(ROOT, A, 35)).toBe(true)
189+
expect(isApprovedRow(ROOT, A, 36)).toBe(false)
190+
})
191+
192+
test("a write refused because the account changed first leaves nothing approved", async () => {
193+
asAccount("key-b")
194+
const out = await recordApprovedBinding(ROOT, binding(35, "Growth"), { account: A })
195+
expect(out?.status).toBe("account-changed")
196+
expect(isApprovedRow(ROOT, A, 35)).toBe(false)
197+
})
198+
199+
test("a row another account wrote is not this account's approval", async () => {
200+
asAccount("key-b")
201+
const B = credentialDigest(API_URL, TENANT, "key-b")
202+
await recordApprovedBinding(ROOT, binding(35, "Growth"), { account: B, seed: false })
203+
expect(isApprovedRow(ROOT, B, 35)).toBe(true)
204+
expect(isApprovedRow(ROOT, A, 35)).toBe(false)
205+
})
206+
207+
test("an adopted row for the same workspace is not an approval", async () => {
208+
asAccount("key-a")
209+
await recordApprovedBinding(ROOT, { ...binding(35, "Growth"), adopted: true }, { account: A, seed: false })
210+
expect(isApprovedRow(ROOT, A, 35)).toBe(false)
211+
})
212+
})

0 commit comments

Comments
 (0)