Skip to content

API Reference parrot.mcp.agent_jobs

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.mcp.agent_jobs

View source

Job handles for long-running agent methods (FEAT-477, Module 4, G7).

Agent flows and crews exceed the 300s connector tool-call ceiling (AgentMCPMountConfig.call_deadline_seconds, TASK-2606), so they need a durable handle rather than a blocking call. This module implements the declared start_* -> job_id, *_status, *_result trio:

  • :meth:AgentJobs.start persists an :class:AgentJobRecord and returns its job_id immediately — it never blocks on the work, which runs as a background asyncio task (no queue framework exists in parrot.mcp to import; state is persisted and polled instead).
  • :meth:AgentJobs.status / :meth:AgentJobs.result project a manifest — counts, summaries, references — never the raw payload, which would blow the response ceiling TASK-2606 enforces.
  • Every read is scoped to the caller's (tenant_id, principal); a mismatched principal gets the same response as a missing job (no existence oracle).

:class:AgentJobStore persists to Redis reusing :class:~parrot.human.suspended_store.SuspendedExecutionStore's semantics (caller-provided TTL, a tombstone on delete) — it does not force agent jobs through the HITL-specific SuspendedExecution record type, which this module has no business depending on.

Class AgentJobRecord

A durable handle for one long-running agent-method invocation.

Spec §2 Data Models, field-for-field, no extensions. Bookkeeping AgentJobStore itself needs (the caller's intended TTL, save timestamp) lives in the store's own persisted envelope, not here.

Fields:

Field Type Default Description
job_id str Unique job identifier.
agent str Configured agent name that owns the method.
tool str The @mcp_tool-decorated method's MCP name.
tenant_id str Tenant the calling principal belongs to.
principal str The calling principal's identity.
status JobStatus Current lifecycle state. "expired" is a terminal outcome alternative to "succeeded"/"failed" — a job that did not reach either before its retention TTL elapsed.
created_at datetime UTC timestamp the job was created.
manifest dict[str, Any] | None None Projection of the result — counts, summaries, references. Never the raw payload.
error str | None None Human-readable failure reason, when status == "failed".

Class AgentJobStore

Redis-backed store for AgentJobRecords.

Key format: mcp:agent-job:{job_id}. Reuses SuspendedExecutionStore's semantics (human/suspended_store.py:64) — caller-provided TTL, a tombstone on delete — without forcing agent jobs through the HITL-specific SuspendedExecution record type.

The underlying Redis key survives ttl + _EXPIRY_GRACE_SECONDS seconds so a read after the caller's intended ttl has elapsed can still observe status="expired" (a real, loadable record) instead of a bare miss; only once the grace period also elapses does the job become indistinguishable from one that never existed.

Parameter Type Default Description
redis Any An async Redis-like client exposing setex/get/delete.
clock Callable[[], float] time.time Zero-argument callable returning the current epoch seconds. Defaults to time.time — injectable so tests can fast-forward a fake clock instead of sleeping.

Method AgentJobStore.save

Persist record with a caller-provided TTL.

save(record: AgentJobRecord, ttl: int) -> None

Parameter Type Default Description
record AgentJobRecord The AgentJobRecord to persist.
ttl int Caller-intended retention in seconds. The underlying Redis key is kept slightly longer (_EXPIRY_GRACE_SECONDS) so a post-TTL read can still observe "expired" rather than a bare miss.

Method AgentJobStore.load

Load job_id, promoting a non-terminal record to "expired".

load(job_id: str) -> AgentJobRecord | None

Parameter Type Default Description
job_id str The job identifier.

Returns: AgentJobRecord | None — The AgentJobRecord — with status promoted to "expired" when it was still "pending"/"running" and the caller's intended ttl has elapsed — or None if it was never saved or its grace period has also elapsed.

Method AgentJobStore.delete

Delete job_id's live record, leaving a short-lived tombstone.

Mirrors SuspendedExecutionStore.delete()'s tombstone semantics: the live record disappears, but a companion key survives briefly so a delete is observably distinct from the record never having existed at all.

delete(job_id: str) -> None

Parameter Type Default Description
job_id str The job identifier.

Class AgentJobs

The start_* -> job_id, *_status, *_result trio (spec §3 Module 4).

Parameter Type Default Description
store AgentJobStore The AgentJobStore persisting job records.
method_resolver Callable[[str, str], Callable[..., Any]] | None None (agent_name, tool_name) -> async callable, resolving which bound method start() runs in the background. None (default) fails every started job immediately with an error manifest — wire in the real exposure-set lookup (TASK-2600/2602's build_exposure_set) when mounting this.
default_ttl int 3600 Retention TTL (seconds) used when start()'s caller does not pass one explicitly.

Method AgentJobs.start

Start a long-running call and return its job_id immediately.

Persists a "pending" record and schedules the work as a background asyncio task — this coroutine returns as soon as that record is saved, never waiting on the work itself.

start(agent_name: str, tool_name: str, arguments: dict[str, Any], pctx: PermissionContext, *, ttl: int | None = None) -> str

Parameter Type Default Description
agent_name str The owning agent's configured name.
tool_name str The @mcp_tool-decorated method's MCP name.
arguments dict[str, Any] Call arguments for the method.
pctx PermissionContext The caller's resolved PermissionContext.
ttl int | None None Retention TTL in seconds. Defaults to default_ttl.

Returns: str — The new job's job_id.

Method AgentJobs.status

Return a status projection for job_id, scoped to pctx.

status(job_id: str, pctx: PermissionContext) -> dict[str, Any] | None

Parameter Type Default Description
job_id str The job identifier.
pctx PermissionContext The caller's resolved PermissionContext.

Returns: dict[str, Any] | None — {"job_id", "status", "agent", "tool", "created_at"}, or None if missing or owned by a different (tenant_id, principal).

Method AgentJobs.result

Return a manifest projection for job_id, scoped to pctx.

Never returns the raw payload — only the small manifest start()'s background run computed on success.

result(job_id: str, pctx: PermissionContext) -> dict[str, Any] | None

Parameter Type Default Description
job_id str The job identifier.
pctx PermissionContext The caller's resolved PermissionContext.

Returns: dict[str, Any] | None — {"job_id", "status", "manifest", "error"}, or None if missing or owned by a different (tenant_id, principal).

Method AgentJobs.delete

Delete job_id's live record, leaving a tombstone.

delete(job_id: str) -> None

Parameter Type Default Description
job_id str The job identifier.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally