Files
cleveragents-core/tools/controller/worker/identity.py
T
drew 0bc734c020 style: ruff format the controller-state-machine branch (288 files)
Applies `ruff format` to the accumulated formatting debt on this branch.
Formatting-only — no behavioral changes. Required for CI/lint's format
gate (`nox -s format -- --check`), which the branch was failing on 288
tracked files that drifted from ruff's canonical style.

In-progress WIP files are intentionally excluded so this commit stays a
clean formatting-only diff.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-20 00:09:17 -04:00

35 lines
1.1 KiB
Python

"""Worker instance identity.
Per plan v9 ``instance_id`` format: ``{hostname}/{pid}/{worker_uuid}``.
- ``hostname`` — which machine. Lets operator ``controller-cli`` /
log queries say "kill all workers on host X."
- ``pid`` — which process. Lets operator inspect logs / strace.
- ``worker_uuid`` — unique per worker instance even if pid recycles
(Linux pid wrap-around or worker restart with same pid).
Slash delimiter (not ``:``) so IPv6 hostnames don't trip parsing.
"""
from __future__ import annotations
import os
import socket
import uuid
def build_instance_id(*, hostname: str | None = None) -> str:
"""Generate a fresh worker instance id.
Call once at worker startup and keep the result for the worker's
lifetime. Each attempt holds the same instance_id; the heartbeat
UPDATEs use it as the WHERE clause's authoritative owner check.
"""
host = hostname or socket.gethostname() or "unknown-host"
pid = os.getpid()
worker_uuid = uuid.uuid4().hex[:12]
return f"{host}/{pid}/{worker_uuid}"
__all__ = ["build_instance_id"]