optio icon indicating copy to clipboard operation
optio copied to clipboard

watch_task returns a snapshot instead of waiting for terminal task state

Open MicroMilo opened this issue 5 months ago • 0 comments

1. Problem

watch_task is exposed to the Optio assistant as a long-running watch tool, but the executor currently performs a single request to the task detail endpoint and returns that response immediately.

Source anchors:

  • packages/shared/src/optio-tools.ts:455 defines the watch_task tool.
  • packages/shared/src/optio-tools.ts:457-460 describes it as watching until a terminal state, polling at a configurable interval, and returning the final state.
  • packages/shared/src/optio-tools.ts:471-483 exposes pollIntervalSeconds and timeoutMinutes inputs.
  • packages/shared/src/optio-tools.ts:462-463 maps the tool to GET /api/tasks/:id.
  • apps/api/src/services/optio-tool-executor.ts:37-64 converts path params and appends remaining GET params as a query string.
  • apps/api/src/services/optio-tool-executor.ts:79-84 calls app.inject(...) once and returns response.body.
  • apps/api/src/routes/tasks.ts:326-337 defines GET /api/tasks/:id as a single task detail fetch with enrichment, not a polling endpoint.
  • apps/api/src/ws/optio-chat.ts:519-527 executes read tools immediately through executeToolCall(...).

Minimal reproduction path:

  1. Have a task that is still queued, pending, waiting_on_deps, or running.

  2. Ask the assistant to watch it, or invoke:

    {
      "name": "watch_task",
      "input": {
        "id": "<task-id>",
        "pollIntervalSeconds": 5,
        "timeoutMinutes": 10
      }
    }
    
  3. The executor sends one request equivalent to:

    GET /api/tasks/<task-id>?pollIntervalSeconds=5&timeoutMinutes=10
    
  4. GET /api/tasks/:id returns the current enriched task snapshot. There is no server-side polling loop and the tool executor does not poll client-side.

Actual behavior:

  • watch_task returns immediately with the current task snapshot.
  • pollIntervalSeconds and timeoutMinutes are serialized as query params but are not consumed by the route.
  • A request that asks Optio to "watch until completion" cannot be reliably fulfilled by this tool contract.

Expected behavior:

  • Either watch_task should poll GET /api/tasks/:id until the task reaches a terminal state or the timeout expires, or the tool should be renamed/described as a one-shot task detail read.
  • If the task is already terminal, it can return immediately.
  • If the timeout expires, the assistant should receive a clear timeout result that includes the latest observed state.

User impact:

This is not only a wording mismatch. The machine-readable assistant tool contract promises a long-running watch operation with interval and timeout controls, but the runtime behavior is a single snapshot read. Users asking the assistant to wait for a task to finish can get an immediate non-terminal snapshot instead of an actual watch result, and the exposed interval/timeout options are misleading because they have no effect.

2. Existing issue / PR coverage

I checked current open and closed GitHub issues and PRs with these searches:

  • watch_task pollIntervalSeconds timeoutMinutes
  • watch_task optio-tool-executor
  • watch task terminal state

Relevant historical result:

  • PR #205, feat: Optio tool definitions, system-status endpoint, and auth passthrough, touched packages/shared/src/optio-tools.ts, but it does not cover the current root cause. The latest source still defines watch_task as a polling/final-state tool while the executor performs one request.

Coverage assessment: not covered.

3. Should this be fixed?

Yes. This is a focused contract-drift bug between the tool schema, the generic executor, the task route, and the assistant feedback path. The behavior is deterministic and has a small fix surface.

4. Proposed fix / PR draft

Suggested implementation direction:

  • Add dedicated handling for watch_task in the tool executor, or route it through a purpose-built watch helper instead of the generic one-shot REST adapter.
  • Poll GET /api/tasks/:id until the task reaches a terminal state such as completed, failed, or cancelled.
  • Respect pollIntervalSeconds and timeoutMinutes, including bounds already documented in the tool input schema.
  • Return the final task detail when a terminal state is reached.
  • Return a clear timeout result with the latest task detail if the timeout expires.
  • Add tests for:
    • a running task does not return as a successful final watch result immediately;
    • an already terminal task returns immediately;
    • timeout returns a timeout-shaped result and includes the latest observed state;
    • interval and timeout inputs are not silently ignored.

Non-goals:

  • This does not require changing the normal GET /api/tasks/:id detail endpoint.
  • This does not require changing unrelated task state semantics.

MicroMilo avatar Jun 16 '26 10:06 MicroMilo