Skip to content

Commit 6d25263

Browse files
sahrizviclaude
andcommitted
fix(workspace): count only the approval this link call wrote (review)
An approved row recorded earlier no longer vouches for a later write that was refused: `isApprovedRow` also matches the `linkedAt` this call stamped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018fJ3X7pcGT4R9yzjsJnqsV
1 parent 92852c4 commit 6d25263

3 files changed

Lines changed: 34 additions & 20 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,15 +916,16 @@ 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`.
919+
/** Whether this directory holds the approved (not adopted) row a `recordApprovedBinding` call wrote: `datamateId`
920+
* under `account`, stamped with the `linkedAt` that call passed, so an approval recorded earlier does not count.
920921
* `recordApprovedBinding` returns "account-changed" both before its write and after it (at the memory seed);
921922
* this tells the caller which one happened. */
922-
export function isApprovedRow(directory: string, account: string, datamateId: number): boolean {
923+
export function isApprovedRow(directory: string, account: string, datamateId: number, linkedAt: number): boolean {
923924
try {
924925
const cache = readCache()
925926
if (!cache || cache.account !== account) return false
926927
const row = primaryRow(cache, directory)
927-
return row?.datamateId === datamateId && !row.adopted
928+
return row?.datamateId === datamateId && row.linkedAt === linkedAt && !row.adopted
928929
} catch {
929930
return false
930931
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,20 +1153,21 @@ function linkHeadlessDeps(directory: string): LinkHeadlessDeps {
11531153
const account = credentialDigest(actAs.url, actAs.instance, actAs.apiKey)
11541154
if ((await accountDigest()) !== account) return false
11551155
const key = identifier.projectPath ?? directory
1156+
const linkedAt = Date.now()
11561157
const seed = await recordApprovedBinding(
11571158
key,
11581159
{
11591160
datamateId: existing.datamate.id,
11601161
datamateName: existing.datamate.name,
11611162
repoRemote: existing.binding.repo_remote,
11621163
projectPath: existing.binding.project_path ?? identifier.projectPath ?? null,
1163-
linkedAt: Date.now(),
1164+
linkedAt,
11641165
},
11651166
{ awaitBackfill: true, account },
11661167
)
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
1168+
// The row this call wrote, not the seed outcome, says whether the approval was recorded: "account-changed" also
1169+
// comes back after the write, when only the memory seed was skipped (seedMessage says so).
1170+
if (!isApprovedRow(key, account, existing.datamate.id, linkedAt)) return false
11701171
UI.println(seedMessage(seed))
11711172
return true
11721173
},

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,34 +179,46 @@ describe("binding cache is scoped to the account, not the tenant", () => {
179179
})
180180
})
181181

182-
describe("isApprovedRow: whether an approval was actually written", () => {
182+
describe("isApprovedRow: whether this call's approval was written", () => {
183183
const A = credentialDigest(API_URL, TENANT, "key-a")
184184

185-
test("a row written under the account is approved", async () => {
185+
test("the row this call wrote is approved", async () => {
186186
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)
187+
const row = binding(35, "Growth")
188+
await recordApprovedBinding(ROOT, row, { account: A, seed: false })
189+
expect(isApprovedRow(ROOT, A, 35, row.linkedAt)).toBe(true)
190+
expect(isApprovedRow(ROOT, A, 36, row.linkedAt)).toBe(false)
190191
})
191192

192193
test("a write refused because the account changed first leaves nothing approved", async () => {
193194
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)
195+
const row = binding(35, "Growth")
196+
expect((await recordApprovedBinding(ROOT, row, { account: A }))?.status).toBe("account-changed")
197+
expect(isApprovedRow(ROOT, A, 35, row.linkedAt)).toBe(false)
198+
})
199+
200+
test("an approval recorded earlier does not vouch for a later write that was refused", async () => {
201+
asAccount("key-a")
202+
await recordApprovedBinding(ROOT, { ...binding(35, "Growth"), linkedAt: Date.now() - 60_000 }, { account: A, seed: false })
203+
asAccount("key-b")
204+
const attempt = binding(35, "Growth")
205+
expect((await recordApprovedBinding(ROOT, attempt, { account: A }))?.status).toBe("account-changed")
206+
expect(isApprovedRow(ROOT, A, 35, attempt.linkedAt)).toBe(false)
197207
})
198208

199209
test("a row another account wrote is not this account's approval", async () => {
200210
asAccount("key-b")
201211
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)
212+
const row = binding(35, "Growth")
213+
await recordApprovedBinding(ROOT, row, { account: B, seed: false })
214+
expect(isApprovedRow(ROOT, B, 35, row.linkedAt)).toBe(true)
215+
expect(isApprovedRow(ROOT, A, 35, row.linkedAt)).toBe(false)
205216
})
206217

207218
test("an adopted row for the same workspace is not an approval", async () => {
208219
asAccount("key-a")
209-
await recordApprovedBinding(ROOT, { ...binding(35, "Growth"), adopted: true }, { account: A, seed: false })
210-
expect(isApprovedRow(ROOT, A, 35)).toBe(false)
220+
const row = { ...binding(35, "Growth"), adopted: true }
221+
await recordApprovedBinding(ROOT, row, { account: A, seed: false })
222+
expect(isApprovedRow(ROOT, A, 35, row.linkedAt)).toBe(false)
211223
})
212224
})

0 commit comments

Comments
 (0)