Skip to content

Commit 4e3e28a

Browse files
fix(workspace): bound the catalog lookup on both paths, and stop overstating the late-attach fallback
Codex round 11. Two findings, both verified. **The allowlist bound covered only one of two call sites.** Last round bounded the fresh-spawn path and left a reused engine awaiting the same lookup with no limit — a partial fix that read as a complete one. Both paths now go through a single bounded helper, so there is one answer rather than two. The underlying request was genuinely unbounded, not merely slow: the generic API request performed a bare fetch with no abort signal, while two other functions in that same client already attach one. It does now, so a stalled server releases its socket instead of accumulating pending fetches across repair retries — which a Promise.race alone cannot do, since racing a promise does not cancel what it is racing. **Publishing a tool-change event does not refresh the running turn, and this module said otherwise.** The invocation's tool set is passed to the model before a late attach completes and cannot be rebuilt mid-call; the session's subscriber only logs, and the next resolveTools is what picks the tools up. So exceeding the bounded wait costs a turn, not a session. That is a correction to a claim this branch has repeated since the wait was introduced. Publishing the event remains right — nothing downstream could otherwise observe a late attach at all — but it is traceability and a hook for subscribers that act between turns, not a live refresh, and the comment now says so rather than promising delivery it cannot make. 1 test: a stalled catalog lookup no longer blocks the reuse path, and reuse still succeeds with only the optional reporting degraded. It fails with the reuse-path bound reverted.
1 parent fee2a0c commit 4e3e28a

3 files changed

Lines changed: 63 additions & 20 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,22 @@ export namespace AltimateApi {
232232

233233
async function request(creds: AltimateCredentials, method: string, endpoint: string, body?: unknown) {
234234
const url = `${creds.altimateUrl}${endpoint}`
235+
// altimate_change start — upstream_fix: bound every API request. Without a
236+
// signal a stalled server holds the caller indefinitely; the workspace attach
237+
// awaited this on its critical path.
238+
const controller = new AbortController()
239+
const timeout = setTimeout(() => controller.abort(), 15_000)
240+
// altimate_change end
235241
const res = await fetch(url, {
242+
signal: controller.signal,
236243
method,
237244
headers: {
238245
"Content-Type": "application/json",
239246
Authorization: `Bearer ${creds.altimateApiKey}`,
240247
"x-tenant": creds.altimateInstanceName,
241248
},
242249
...(body ? { body: JSON.stringify(body) } : {}),
243-
})
250+
}).finally(() => clearTimeout(timeout))
244251
if (!res.ok) {
245252
throw new Error(`API ${method} ${endpoint} failed with status ${res.status}`)
246253
}

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

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,16 @@ async function declared(datamateId: string): Promise<Declared | null> {
392392

393393
/** Tell the session its tool list changed.
394394
*
395-
* `MCP.add` stores the client but publishes nothing, so an attach that lands
396-
* after the turn's bounded wait — or on a repair retry, which never waits —
397-
* produced tools the session had no way to learn about until the user sent
398-
* another message. The documented fallback for exceeding the cap depends on
399-
* this event existing, so it has to be published here. */
395+
* `MCP.add` stores the client but publishes nothing, so nothing downstream could
396+
* even observe a late attach. This restores that signal.
397+
*
398+
* What it does NOT do, stated plainly because this module claimed otherwise for
399+
* several revisions: it does not give tools to the invocation already running.
400+
* That turn's tool set was passed to the model before the attach finished and
401+
* cannot be rebuilt mid-call — the session's subscriber only logs, and the next
402+
* `resolveTools` is what picks the tools up. So exceeding the bounded wait costs
403+
* a turn, not a session. The event is worth publishing for traceability and for
404+
* any subscriber that can act between turns; it is not a live refresh. */
400405
async function announceToolsChanged(): Promise<void> {
401406
if (syncInternals.toolsChanged) return syncInternals.toolsChanged()
402407
try {
@@ -408,6 +413,31 @@ async function announceToolsChanged(): Promise<void> {
408413
}
409414
}
410415

416+
/** The workspace allowlist, bounded.
417+
*
418+
* Reporting only — the attach must never wait on it. The bound was previously
419+
* applied to the spawn path alone, leaving a reused engine awaiting it with no
420+
* limit. Both paths go through here now, so there is one answer rather than two.
421+
*
422+
* The underlying request is separately abortable (the API client attaches a
423+
* signal), so a stalled server releases its socket instead of accumulating
424+
* pending fetches across repair retries. */
425+
async function declaredBounded(workspaceId: string): Promise<Declared | null> {
426+
return Promise.race([
427+
declared(workspaceId),
428+
new Promise<null>((resolve) => {
429+
const timer = setTimeout(() => {
430+
log.warn("workspace allowlist lookup timed out; continuing without the declared-vs-delivered report", {
431+
workspaceId,
432+
timeoutMs: DECLARED_TIMEOUT_MS,
433+
})
434+
resolve(null)
435+
}, DECLARED_TIMEOUT_MS)
436+
timer.unref?.()
437+
}),
438+
])
439+
}
440+
411441
async function notify(toast: Toast): Promise<void> {
412442
if (syncInternals.notify) return syncInternals.notify(toast)
413443
try {
@@ -638,7 +668,7 @@ async function run(): Promise<Outcome> {
638668
// attach used to say so. Reuse is the COMMON path, so staying silent
639669
// here is where the gap would actually go unnoticed.
640670
const present = engineToolKeys(await client.tools())
641-
const declaredKeys = await declared(workspaceId)
671+
const declaredKeys = await declaredBounded(workspaceId)
642672
const missing = declaredKeys ? declaredKeys.keys.filter((k) => !present.has(k)) : []
643673
const available = present.size
644674
if (declaredKeys && missing.length > 0) {
@@ -696,19 +726,7 @@ async function run(): Promise<Outcome> {
696726
// launched and its HTTP layer has no abort timeout — so an API that accepts a
697727
// connection and then stalls stopped a good cached binding and an installed
698728
// engine from ever attaching. Reporting degrades; attaching does not wait.
699-
const declaredKeys = await Promise.race([
700-
declared(workspaceId),
701-
new Promise<null>((resolve) => {
702-
const timer = setTimeout(() => {
703-
log.warn("workspace allowlist lookup timed out; attaching without the declared-vs-delivered report", {
704-
workspaceId,
705-
timeoutMs: DECLARED_TIMEOUT_MS,
706-
})
707-
resolve(null)
708-
}, DECLARED_TIMEOUT_MS)
709-
timer.unref?.()
710-
}),
711-
])
729+
const declaredKeys = await declaredBounded(workspaceId)
712730
const declaredCount = declaredKeys?.keys.length ?? 0
713731

714732
// Rule 2 / 3 — opportunistic use, or an offer. Never an install.

‎packages/opencode/test/altimate/workspace/engine-sync.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,3 +1189,21 @@ describe("ensure — round 10", () => {
11891189
expect(h.connects).toHaveLength(0)
11901190
})
11911191
})
1192+
1193+
describe("ensure — round 11", () => {
1194+
test("a stalled catalog lookup cannot block the REUSE path either", async () => {
1195+
// The bound added last round covered only the fresh-spawn path; a compatible
1196+
// pinned engine still awaited the lookup with no limit, and the generic API
1197+
// request attaches no abort signal at all.
1198+
const h = install({
1199+
existing: { type: "local", command: ["datamate", "start-stdio", "--datamate", "42"] },
1200+
statuses: [{ datamate: { status: "connected" } }],
1201+
tools: { datamate_dbt_build_model: 1 },
1202+
})
1203+
syncInternals.declared = () => new Promise(() => {}) // accepts, then stalls
1204+
const outcome = await ensure("s1")
1205+
// Reuse still succeeds; only the optional reporting degrades.
1206+
expect(outcome).toMatchObject({ kind: "reused", available: 1 })
1207+
expect(h.added).toHaveLength(0)
1208+
})
1209+
})

0 commit comments

Comments
 (0)