Skip to content

P2: Keep Mongo completed-action result lookup indexed #24

Description

@YueZh127

Objective

Keep every MongoDB path used by interactive completed-action polling on an indexed identity under the embedded Task.sessionActions authority introduced by #29.

Review origin and current baseline

Current defect

MongoRepository.getSessionActionResult() first executes:

tasks.findOne({
  sessionActions: {
    $elemMatch: { id: actionId, state: 'completed' }
  }
})

There is no Task index containing sessionActions.id, so the current authoritative lookup can scan the tasks collection. SessionService.waitForResult() repeats this lookup every 250 ms until a result or deadline is reached.

The compatibility path for already-completed legacy pending_actions also queries { id: actionId, state: 'completed' }, while the legacy documents use _id=actionId and current indexes only cover in-flight migration states. That fallback can scan the legacy collection as well.

The action_results compatibility path already queries the default indexed _id and must remain readable.

Required behavior

  • Resolve current embedded Task.sessionActions results through a justified indexed identity without changing Task/action authority.
  • Resolve completed legacy pending_actions by indexed _id, or add a bounded and justified equivalent index.
  • Preserve existing action_results._id compatibility.
  • Keep result identity, immutable terminal completion, dispatch binding, one-active-action admission, and Memory/Mongo observable parity unchanged.
  • Avoid creating a second authoritative result store merely to make polling convenient.

Acceptance criteria

  • Add a Mongo query-plan contract for the current embedded Task result lookup and prove the winning plan does not contain COLLSCAN.
  • Add query-plan contracts for action_results and completed legacy pending_actions; prove each compatibility lookup is indexed and bounded.
  • Cover present, missing, wrong-state, and legacy completed results without changing the public response.
  • Verify that polling by one action ID cannot return another Task's result and that terminal result immutability remains intact.
  • Justify any new multikey index with its write/storage impact and ensure it supports the exact production predicate.
  • Run build, typecheck, lint, worker bundle, fixture drift, full tests, standalone Mongo contracts, and all four PR checks.
  • Do not use a timeout increase, longer polling interval, cache, or process-local map as the correctness fix.

Scope and dispatch gate

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions