Skip to main content

Python SDK for Pugmark — the durable agent runtime built on object storage

Project description

Pugmark Python SDK

Python SDK for writing Pugmark handlers. A handler is a process the Pugmark runtime invokes on each new event in a session; it reads inputs from stdin, fetches object bodies over HTTP, and writes outputs back to stdout.

Install

pip install pugmark

Minimal handler

A pugmark session is an append-only log of JSON events. On every invocation pugmark feeds the handler the full log on stdin. Walk it, decide one next event, append it.

import pugmark

log = list(pugmark.read())
last = log[-1].decode_json() if log else None
match last:
    case {"kind": "input", "text": text}:
        pugmark.write({"kind": "echo", "text": text.upper()})
    case _:
        pugmark.pause("nothing to echo")

Run it under the Pugmark CLI:

pugmark start
pugmark push <<<'{"kind":"input","text":"hello"}'
pugmark run --once -- python handler.py
pugmark log -A           # input event, then echo event ({"text":"HELLO"})

Writing outputs

pugmark.write accepts either a pugmark.Object or a raw Python value. Content type is auto-detected unless overridden.

pugmark.write("hello", name="reply")                  # text/plain
pugmark.write({"answer": 42}, name="result")          # application/json
pugmark.write(b"\x00\x01", name="bin")                # application/octet-stream
pugmark.write("# Title", name="doc", content_type="text/markdown")

Explicit helpers when you'd rather not rely on type dispatch:

pugmark.write_text("hello", name="reply")
pugmark.write_json({"answer": 42}, name="result")
pugmark.write_bytes(b"\x00\x01", name="bin")

Reading inputs

pugmark.read() walks the full session log in order — including any inherited parent-session entries. This is the primary primitive for log-replay handlers; iterate it once per invocation to reconstruct everything you need.

for obj in pugmark.read():
    event = obj.decode_json()       # every entry is JSON
    fold(state, event)              # accumulate whatever the handler needs

For ad-hoc named lookups (slot-filling workflows where each name appears at most once), pugmark.state() returns a dict-like view of the session keyed by object name. Note that names are collapsing — only the most recently written object per name survives — so this isn't appropriate for conversations or any log with repeated entry kinds.

state = pugmark.state()             # dict-like; one entry per unique name
if "config" in state:
    cfg = state["config"].decode_json()

Named loads with the right decoder built in:

text  = pugmark.load_text("greeting")
reply = pugmark.load_json("result")
blob  = pugmark.load_bytes("bin")

Object methods

Every object (local or remote) supports:

Method Returns
body() raw bytes (auto-decompressed)
content_type() MIME type string
name() optional name set by the writer
metadata() Dict[str, str]
decode_text() UTF-8 decoded str
decode_json() parsed JSON value
decode_data() raw bytes

Control flow

pugmark.pause("waiting for next user message")     # suspend until next event
pugmark.sleep(60, reason="rate-limited")           # suspend for 60 seconds

Events

Each handler invocation is triggered by an event. Iterate pugmark.events() to inspect:

for event in pugmark.events():
    match event:
        case pugmark.StartEvent():       ...    # new root session
        case pugmark.ForkEvent():        ...    # forked from a parent
        case pugmark.WakeEvent():        ...    # resumed from pause/sleep
        case pugmark.PushEvent() as e:   ...    # object was pushed; e.object is the Object

OpenTelemetry

The SDK ships optional OpenTelemetry trace context propagation. Install with the otel extra to enable:

pip install 'pugmark[otel]'

Requirements

  • Python 3.11+
  • zstandard (installed automatically)

License

Apache License 2.0.

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

pugmark-0.16.6.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

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

pugmark-0.16.6-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file pugmark-0.16.6.tar.gz.

File metadata

  • Download URL: pugmark-0.16.6.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pugmark-0.16.6.tar.gz
Algorithm Hash digest
SHA256 866a927e2b0b7e888c30a6beb8d4918013bbe3243e5a9f9bd4be70cdc71d0db4
MD5 f74b849f9ecc08606f00869fe06f2d52
BLAKE2b-256 d4ce6fe5db79b44f42f3cc03d9a2532e6482e2fd3bdac9087987e5334fb48ba2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pugmark-0.16.6.tar.gz:

Publisher: release.yml on firetiger-oss/pugmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pugmark-0.16.6-py3-none-any.whl.

File metadata

  • Download URL: pugmark-0.16.6-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pugmark-0.16.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ea29ea38f8e54ee078a21bc51ea4900e25ae94aae254ec4cea08237d2f7000e0
MD5 9dfb229c28dc89d2fb0e8c75947fc05b
BLAKE2b-256 fae926c1e9226e872344f1089a904bbdac5b649fdac1f34bbb7d22d7d23a9bd9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pugmark-0.16.6-py3-none-any.whl:

Publisher: release.yml on firetiger-oss/pugmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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