Skip to content

Add wait_for_plan_implementation to sync and async knowledge clients - #154

Open
Nivas2129 wants to merge 1 commit into
prooflinedev:mainfrom
Nivas2129:feat/wait-for-plan-implementation
Open

Nivas2129 wants to merge 1 commit into
prooflinedev:mainfrom
Nivas2129:feat/wait-for-plan-implementation

Conversation

@Nivas2129

Copy link
Copy Markdown

Problem & who this helps

Engineers and automation pipelines using client.knowledge.start_plan_implementation(plan_id) currently have to hand-roll polling around get_plan_implementation(implementation_id) until the attempt settles, while also validating the returned identity (id, plan_version, attempt), handling MCP tool refusals (isError) without retrying, checking layer validation states, and bounding total wait time across both synchronous and asynchronous (asyncio / Trio) callers.

Approach & design decisions

  • Typed polling helper on KnowledgeClient and AsyncKnowledgeClient (proofline/knowledge.py):
    • Adds wait_for_plan_implementation(implementation_id, *, timeout=300, plan_version=None, attempt=None, organization_id=None) on both sync and async knowledge clients, returning a frozen PlanImplementationOutcome (with LayerOutcome and LayerValidation, exported from proofline).
    • Mirrors wait_for_review semantics: validates implementation_id (UUID), timeout, plan_version, and attempt before sending any request; polls with exponential backoff (5s -> 10s -> 20s -> 40s -> 60s) capped to the remaining wait budget; supports timeout=0 single-read checks; and works on both asyncio and Trio via anyio.sleep.
  • Fail-closed identity & layer evidence checks:
    • Verifies structuredContent.data.id matches the requested implementation_id (raising urn:proofline:sdk:invalid-response on mismatch or malformed metadata).
    • Supports pinning plan_version and attempt from start_plan_implementation: a mismatch ends the wait immediately on that read with status="INCOMPLETE", and missing pinned fields never produce VALIDATED.
    • Reports status="VALIDATED" only when top-level status is VALIDATED, layer metadata is present and non-empty, and every layer reports VALIDATED; unrecognized statuses fail closed as INCOMPLETE.
    • Keeps raw result, layers, layer, step, and error_code out of repr() so untrusted text is not dumped into diagnostic output.
  • Preserves MCP no-retry and refusal semantics (proofline/_mcp.py, proofline/knowledge.py):
    • Tool refusals (isError: True) end the wait immediately with status="REFUSED", is_error=True, optional error_code, and the raw KnowledgeResult intact.
    • HTTP errors (such as 429 and 503) and MCP protocol errors propagate immediately without retry.
    • Distinguishes httpx2.TimeoutException in _mcp.py as urn:proofline:sdk:timeout so in-flight timeouts at the wait deadline cleanly return TIMED_OUT with the last observed result, while pre-deadline per-request timeouts still raise ProoflineError.

Verification

  • Added 98 test cases in tests/test_plan_implementation.py exercising sync, asyncio, and Trio clients against the real loopback HTTP server: backoff/polling to VALIDATED, terminal statuses (VALIDATION_FAILED, REFUSED, FAILED, STOPPED), fail-closed layer checks, pinned plan_version/attempt mismatch and missing fields, tool refusals (isError), HTTP 429/503 no-retry, timeout=0, response decoded at deadline, real in-flight deadline vs pre-deadline timeout, input validation, malformed payload rejection, and repr() safety.
  • Ran python3 scripts/check.py locally (generation check, operation/vocabulary contracts, ruff check, ruff format --check, strict mypy, pytest with 332 passed, wheel + sdist builds, and installed-artifact smoke tests for both wheel and sdist) — all passed.

This branch has not been deployed

No deployments
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.

2 participants