Skip to main content

Utilities for Coze coding client runtime context and helpers.

Project description

coze-coding-utils

Python utilities for the Coze coding client. Provides a simple runtime Context object and an async-task subsystem.

Installation

pip install coze-coding-utils

Modules

runtime_ctx

from coze_coding_utils.runtime_ctx.context import Context

ctx = Context(run_id="r", space_id="s", project_id="p", method="fetchData")
print(ctx.run_id, ctx.method)

async_tasks

Per-process async task runtime backed by PostgreSQL (single table, JSONB payload, partial indexes). Public API:

from coze_coding_utils.async_tasks import (
    AsyncTaskRuntime,        # construct one per process
    AsyncTask,               # SQLAlchemy ORM (single PK on task_id)
    AsyncTaskStatus,         # pending / running / completed / failed / timeout
    AsyncTaskBase,           # DeclarativeBase if caller needs to drive DDL
    AsyncTaskStorageError,   # raised on DB init/query failure (caller turns into 503)
    config,                  # env-overridable constants module
)

runtime = AsyncTaskRuntime(
    session_factory=get_session,   # Callable[[], sqlalchemy.orm.Session]
    engine=get_engine(),           # sqlalchemy.engine.Engine
    graph=async_graph,             # any object with .ainvoke(payload, config=...)
    checkpointer=checkpointer,     # any LangGraph-compatible checkpointer
)

# Lifespan / FastAPI usage:
async with lifespan(...):
    await runtime.submit(payload={...}, biz_context={"x-user-id": "u1"}, deadline_sec=600)
    row = await runtime.get(task_id)
    ...
    await runtime.shutdown()

The runtime is intentionally framework-agnostic:

  • No FastAPI dependency — caller maps AsyncTaskStorageError to HTTP 503 themselves.
  • No LangGraph dependency in [project.dependencies] — the runtime only calls graph.ainvoke(payload, config=run_config) and holds the checkpointer reference for resume scenarios.

Caller contracts (pre-conditions)

The runtime makes three assumptions about the caller's environment. Violating any one will silently break checkpoint cleanup or task lifecycle:

  1. Checkpointer compatible with PG. Construct the LangGraph with a checkpointer that writes to the schema configured by COZE_ASYNC_TASK_CHECKPOINT_SCHEMA (default memory), tables checkpoints / checkpoint_writes. langgraph-checkpoint-postgres works out of the box.
  2. thread_id == task_id. The runtime fixes {"configurable": {"thread_id": task_id}} when calling graph.ainvoke. The memory_cleanup_once scan deletes orphan checkpoint rows by joining on this equality. Don't override thread_id in your graph.
  3. Session schema access. The injected session_factory must produce sessions with read+write privileges on COZE_ASYNC_TASK_CHECKPOINT_SCHEMA schema (so cleanup can DELETE orphan rows).

Environment variables

All prefixed with COZE_ASYNC_TASK_. Override only when there's a specific ops reason.

Var Default Meaning
HEARTBEAT_INTERVAL_SEC 30 Per-task heartbeat write frequency
HEARTBEAT_FAIL_THRESHOLD 3 Consecutive failures → task self-aborts
ORPHAN_TIMEOUT_SEC 300 Running → failed after this many seconds of silent heartbeat
MAX_CLAIM_COUNT 3 max claim attempts before a row is left for scans to reap
DEFAULT_DEADLINE_SEC 86400 24h wall-clock cap; override per task via deadline_sec
CLEANUP_INTERVAL_SEC 21600 memory cleanup loop period
ORPHAN_SCAN_INTERVAL_SEC 60 orphan scan period
DEADLINE_SCAN_INTERVAL_SEC 60 deadline scan period
MAX_CONCURRENT_PER_INSTANCE 50 in-process concurrency semaphore
SCAN_BATCH_LIMIT 500 rows per orphan/deadline scan tick
MEMORY_CLEANUP_BATCH 200 rows per memory.checkpoints cleanup sub-batch
MEMORY_CLEANUP_MAX_ITERS 50 max batches per cleanup tick before deferring
RECURSION_LIMIT 100 LangGraph runtime recursion ceiling
CHECKPOINT_SCHEMA memory PG schema for LangGraph checkpoint tables
TASK_RETENTION_DAYS 7 terminal-status rows older than this are DELETEd
TASK_CLEANUP_INTERVAL_SEC 3600 task_cleanup_loop tick period
TASK_CLEANUP_BATCH 500 max rows DELETEd per task_cleanup tick

The runtime lazily issues CREATE TABLE IF NOT EXISTS on the first submit / get, so a fresh deployment needs no manual DDL.

Python Version

Requires Python 3.10+.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coze_coding_utils-0.2.8a5.tar.gz (71.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coze_coding_utils-0.2.8a5-py3-none-any.whl (89.6 kB view details)

Uploaded Python 3

File details

Details for the file coze_coding_utils-0.2.8a5.tar.gz.

File metadata

  • Download URL: coze_coding_utils-0.2.8a5.tar.gz
  • Upload date:
  • Size: 71.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for coze_coding_utils-0.2.8a5.tar.gz
Algorithm Hash digest
SHA256 1cf928eaeffb4dab83c45b89a436576f10fdd6ea44c0fd9f47ab4d34963458f5
MD5 430ddb83615ae849b5b148f0407141bf
BLAKE2b-256 97a0c70dc1c58ebf37bab9d68b4ccc7c1af638bfaea7648cd0b43e42282fd54c

See more details on using hashes here.

File details

Details for the file coze_coding_utils-0.2.8a5-py3-none-any.whl.

File metadata

File hashes

Hashes for coze_coding_utils-0.2.8a5-py3-none-any.whl
Algorithm Hash digest
SHA256 25fae5b8f593eb8987e134e39639e0794687583c48185fde451ee720d4435949
MD5 48cf85764b765fe18dcde4f19e4191e4
BLAKE2b-256 be806a5d63020387ff0b9fc5d8bd4283fbd7f139dcc513ba6f47197c2f04e4e2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page