Skip to content

Block overlapping lcm_expand_query delegations per origin session#222

Merged
jalehman merged 5 commits into
mainfrom
copilot/fix-deadlock-in-lcm-expand-query
Apr 3, 2026
Merged

Block overlapping lcm_expand_query delegations per origin session#222
jalehman merged 5 commits into
mainfrom
copilot/fix-deadlock-in-lcm-expand-query

Conversation

Copilot AI commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Concurrent lcm_expand_query calls from the same origin session could both pass the existing recursion guard, spawn delegated sub-agents, and deadlock on the shared session lane. This change adds a per-origin-session in-flight gate so overlapping expansions fail fast instead of wedging the gateway pipeline.

  • What changed

    • Added a concurrency guard in the delegated expansion path for lcm_expand_query.
    • The first request claims the origin session slot before any delegated sub-agent work starts.
    • A second overlapping request from the same origin session now returns immediately with:
      • errorCode: "EXPANSION_CONCURRENCY_BLOCKED"
      • reason: "origin_session_in_flight"
  • Guard behavior

    • The new guard is scoped to the origin session, not the delegated child session.
    • It is independent of the existing recursion/depth guard:
      • recursion guard still blocks delegated re-entry
      • concurrency guard blocks parallel top-level expansion from the same origin
    • The slot is always released after completion/failure via cleanup, so later retries can proceed normally.
  • Error surface

    • Returned errors include explicit recovery guidance to fall back to lighter tools such as lcm_grep or lcm_describe when another expansion is already running.
    • Telemetry continues to emit a block event for these fast-fail cases.
  • Release note

    • Added a patch changeset for this user-visible behavior change.
const concurrencyCheck = acquireExpansionConcurrencySlot({
  originSessionKey,
  requestId,
});

if (concurrencyCheck.blocked) {
  return jsonResult({
    errorCode: concurrencyCheck.code,
    error: concurrencyCheck.message,
    requestId: concurrencyCheck.requestId,
    originSessionKey: concurrencyCheck.originSessionKey,
    reason: concurrencyCheck.reason,
  });
}

Copilot AI changed the title [WIP] Fix deadlock issue caused by concurrent lcm_expand_query calls Block overlapping lcm_expand_query delegations per origin session Mar 31, 2026
Copilot AI requested a review from jalehman March 31, 2026 07:47
@jalehman jalehman marked this pull request as ready for review April 1, 2026 04:09
Delay origin-session concurrency slot acquisition until lcm_expand_query has
resolved scope and found summary IDs to delegate. This preserves the
concurrency block for real delegated sub-agent work without blocking
overlapping no-op or no-match requests that never touch the shared lane.

Add a regression test covering concurrent query calls that return no matches
so harmless probes remain unblocked.

Regeneration-Prompt: |
  Address the PR review finding that the new lcm_expand_query concurrency slot
  was acquired too early. Preserve the intended deadlock prevention for real
  delegated sub-agent runs, but do not serialize requests that exit before any
  delegation happens, such as missing-scope or no-match query paths. Keep the
  existing concurrency-block behavior for actual delegated expansions and add a
  regression test proving concurrent no-match requests both complete normally
  without any gateway agent calls.
@jalehman jalehman merged commit d8261d7 into main Apr 3, 2026
2 checks passed
@github-actions github-actions Bot mentioned this pull request Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Concurrent lcm_expand_query calls deadlock on shared session lane (v0.5.0)

2 participants