Skip to content

identity

identity

Task ID generation, validation, and prefix resolution.

Task IDs are Crockford-base32 with a structural signature: a 16-letter non-hex head + a digit + three full-alphabet chars. The shape makes a terok task ID unmistakable from a podman container ID or git SHA at a glance.

__all__ = ['is_task_id', 'normalize_task_id_input', 'resolve_task_id'] module-attribute

normalize_task_id_input(raw)

Collapse user-input variants to the canonical lowercase form.

Strips hyphens, lowercases, and applies the Crockford I/L → 1, O → 0 substitutions. The result is still subject to _TASK_ID_PREFIX_RE downstream — this only widens what we accept, never what we emit.

Call-site discipline: only call this at user-interactive CLI boundaries — argparse dispatch handlers and argcomplete completers. Internal code paths (lib/*, tui/*, TUI pickers, clearance, anything reading task IDs from disk, OCI annotations, or runtime state) always work with canonical lowercase IDs and must never re-normalise. Leaking this tolerance inward quietly defeats the "we encode only in canonical form" invariant.

Source code in src/terok/lib/orchestration/tasks/identity.py
def normalize_task_id_input(raw: str) -> str:
    """Collapse user-input variants to the canonical lowercase form.

    Strips hyphens, lowercases, and applies the Crockford
    ``I/L → 1``, ``O → 0`` substitutions.  The result is still subject
    to `_TASK_ID_PREFIX_RE` downstream — this only widens what
    we accept, never what we emit.

    **Call-site discipline:** only call this at user-interactive CLI
    boundaries — argparse dispatch handlers and argcomplete completers.
    Internal code paths (``lib/*``, ``tui/*``, TUI pickers, clearance,
    anything reading task IDs from disk, OCI annotations, or runtime
    state) always work with canonical lowercase IDs and must *never*
    re-normalise.  Leaking this tolerance inward quietly defeats the
    "we encode only in canonical form" invariant.
    """
    return raw.replace("-", "").lower().translate(_TASK_ID_INPUT_TRANSLATE)

is_task_id(text)

Return True if text is a well-formed task ID.

Source code in src/terok/lib/orchestration/tasks/identity.py
def is_task_id(text: str) -> bool:
    """Return True if *text* is a well-formed task ID."""
    return bool(_TASK_ID_CROCKFORD_4_5_RE.fullmatch(text))

resolve_task_id(project_name, prefix)

Resolve a (possibly partial) task ID to its full form.

The prefix is first run through normalize_task_id_input, so callers may pass uppercase, hyphenated, or ambiguous-letter variants (K3-V8H, k3v8I) — they collapse to the canonical lowercase form before validation and lookup.

Raises SystemExit with an actionable message on zero or multiple matches.

Source code in src/terok/lib/orchestration/tasks/identity.py
def resolve_task_id(project_name: str, prefix: str) -> str:
    """Resolve a (possibly partial) task ID to its full form.

    The *prefix* is first run through [`normalize_task_id_input`][terok.lib.orchestration.tasks.normalize_task_id_input],
    so callers may pass uppercase, hyphenated, or ambiguous-letter
    variants (``K3-V8H``, ``k3v8I``) — they collapse to the canonical
    lowercase form before validation and lookup.

    Raises ``SystemExit`` with an actionable message on zero or multiple matches.
    """
    # Head position is letter-only, so an ``I/L/O`` there can't be a
    # Crockford body substitution — surface that as a specific error
    # instead of normalising it into a digit and letting downstream
    # mistake it for a legacy-hex prefix.
    stripped = prefix.replace("-", "").lower()
    if stripped[:1] in _TASK_ID_AMBIGUOUS_LETTERS:
        raise SystemExit(
            f"Invalid task ID prefix {prefix!r}; "
            f"task IDs never start with I, L, or O — "
            f"expected one of {_TASK_ID_HEAD_CHARS!r}"
        )
    prefix = stripped.translate(_TASK_ID_INPUT_TRANSLATE)
    _validate_task_id_prefix(prefix)
    meta_dir = tasks_meta_dir(project_name)
    if not meta_dir.is_dir():
        raise SystemExit(f"No tasks found for project {project_name}")
    if task_exists(project_name, prefix):
        return prefix
    matches = [tid for tid in iter_task_ids(meta_dir) if is_task_id(tid) and tid.startswith(prefix)]
    if len(matches) == 1:
        return matches[0]
    if not matches:
        raise SystemExit(f"No task matching '{prefix}' in project {project_name}")
    raise SystemExit(f"Ambiguous task ID '{prefix}' — matches: {', '.join(sorted(matches))}")