Search and contents

Task Execution logging

log_task_execution in engine/task_activity_log.py connects a Task's own run (engine/workflow.py) to the ActivityLogEntry that the Node Type Agreement's Activity Log facet requires for each Task run.

Task and TaskResult are deliberately Activity-Log-agnostic. Gate, TryCatch and Workflow carry no Postgres or ActivityLog dependency at all, which is why their own tests run with no database fixture.

This module is a separate wrapper on top, not a change to Task.execute() itself, so that purity stays intact for any caller that doesn't need logging. A nested Task invoked as a Workflow step is one example: typically only the outermost run needs its own entry.

1. Build a context and run the wiring

from engine.activity_log import ActivityLog
from engine.task_activity_log import TaskRunContext, log_task_execution
from engine.workflow import WorkflowRegistry

context = TaskRunContext(
    node_type="MES",
    node_instance_id="mes-line-3",
    trigger="order_release",
    task_type="ingestion-time classification",
)

task = WorkflowRegistry().prompt_chaining("classify-document", raw_text, steps=[classify])

result = log_task_execution(
    ActivityLog(),
    context,
    task,
    outcome=lambda r: f"classified:{r.value}",
    peer_nodes_queried=lambda r: ["ERP"] if r.value.needs_erp_lookup else [],
    reference_ids=lambda r: {"document_id": r.value.document_id},
)
  • outcome is required. Every Task run needs some outcome string, and no generic default would mean anything.
  • peer_nodes_queried and reference_ids default to empty when omitted, for a Task whose domain genuinely has neither.

2. What gets written

One ActivityLogEntry per call, through ActivityLog.write:

  • workflow_pattern comes straight off Task.pattern or TaskResult.pattern. No extraction function is needed, since a Task always knows its own pattern.
  • outcome, peer_nodes_queried and reference_ids come from the three extraction functions, which are called with the TaskResult the Task itself just produced.

The written entry is queryable back out the same way as any other Activity Log entry (ActivityLog.query).

3. The failure path

An exception that escapes Task.execute() is a genuine bug or an unhandled edge case. It is not a UnresponsiveServiceFailure or PromptCausedFailure caught by Try/Catch, because TryCatch.run already resolves both to a normal AttemptResult instead of raising.

Such an exception still produces a terminal, logged entry before log_task_execution re-raises it:

try:
    log_task_execution(activity_log, context, task, outcome=...)
except Exception:
    ...  # entry is already written; re-raise has already happened

On this path:

  • outcome is f"{ERROR_OUTCOME_PREFIX}: {exc}", for example "error: ...".
  • peer_nodes_queried and reference_ids are empty, since there is no TaskResult to extract them from.
  • The three extraction functions are never called. Only str(exc) is needed.

4. A Try/Catch-wrapped step: log_try_catch_task

engine/task_activity_log.py also holds log_try_catch_task:

log_try_catch_task(
    activity_log, context, name, data, step,
    *, retry=None, repair=None, reference_ids=..., peer_nodes_queried=..., outcome=None,
)

It is the shared composition of the pieces above, used by:

  • dispatch_claimed_entry (trigger-dispatch-queue.md);
  • run_fault_tolerant_ingestion in engine/conformance.py;
  • PeerClient.query in engine/communication_interface.py.

Each of them uses it instead of keeping a copy of its own. It wraps step in a TryCatch (retry, repair), runs that as a one-step Prompt Chaining Task named name over data, logs exactly one entry through log_task_execution, and returns the Try/Catch AttemptResult.

  • reference_ids and peer_nodes_queried take the AttemptResult here, not a TaskResult. A caller can therefore record which peer was queried whether the call succeeded or gave up.
  • The logged outcome is the Try/Catch outcome (succeeded, gave_up, exhausted) unless outcome= is given. That is a callable on the AttemptResult returning the string to log instead, for a step that reports its own result. An example is a fetch whose peer answered with a refusal, which Try/Catch alone would log as succeeded.
  • Any exception from step other than the two Try/Catch failure classes is a genuine bug and takes the path in section 3: one error: ... entry is written and the exception propagates.

Pitfalls

  • Extraction functions run only on success. outcome, peer_nodes_queried and reference_ids are never called when Task.execute() raises, so don't rely on them for any error-path side effect.
  • This wraps one Task's own top-level run, not every step inside it. A Prompt Chaining step or an Orchestrator-Workers worker doesn't get its own entry just by being called from inside a wrapped Task. Wrap the specific Task whose run deserves its own Activity Log record.
  • node_type and node_instance_id aren't derived from anything, so supply the real ones. Nothing here validates that they match a real Node Type Agreement identity. That is the caller's responsibility, as it already was when constructing an ActivityLogEntry directly.

Release: v0.11.0