Skip to content

API Reference parrot.sdd.scripts.reserve_ids

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.sdd.scripts.reserve_ids

View source

reserve_ids.py — the git-native compare-and-swap TASK/FEAT ID allocator.

Replaces the "scan existing files, take the max, +1" numbering used by /sdd-task//sdd-spec with an optimistic-concurrency-with-retry allocator (FEAT-387): read sdd/tasks/.id_ledger.json (TASK-1963), compute the requested reservation, build a ledger-only commit on top of origin/<base_branch>, and push it. A non-fast-forward push rejection means another allocation landed first — fetch, re-read the now-current ledger, recompute, and retry (bounded, with jittered backoff), instead of silently succeeding with a stale, already-claimed number.

The reservation is a compare-and-swap against origin/<base_branch> alone. The candidate commit is assembled with git plumbing (hash-object / read-tree / write-tree / commit-tree) in a throwaway index, so the local branch, the local index and the working tree are never mutated by an attempt — a lost race costs one dangling commit object and nothing else. In particular this allocator never runs git reset --hard and never pushes HEAD, so it can neither destroy nor publish local-only commits on the base branch.

Alias IdLedger

Re-exported from parrot.sdd.scripts.id_ledger.IdLedger.

Class IdReservation

Result handed back to the calling SDD command.

Fields:

Field Type Default Description
kind str
first_id int
count int Number of IDs reserved; must be at least 1.
ids list[str]

Class IdReservationError

Raised when reserve_ids() exhausts its retry budget, or when a non-retryable precondition (dirty working tree, wrong branch, invalid push failure) is violated.

Alias LEDGER_PATH

Re-exported from parrot.sdd.scripts.id_ledger.LEDGER_PATH.

Function existing_feature_id

Return the feature id a slug already owns, if any.

A slug has ONE feature id for its lifetime. Re-running /sdd-spec over an existing spec must reuse that id, never reserve a second one: a second reservation forks the feature's identity — the spec and the task index move to the new number while the task files, the branch and the worktree keep the old one, and DevelopmentNode._find_feature_slug (which matches strictly on the index header's feature_id inside the worktree) then finds no index at all and silently degrades a whole multi-agent pool to a single agent.

existing_feature_id(root: Path, label: str) -> tuple[str, str] | None

Parameter Type Default Description
root Path Repository root.
label str The feature slug (--label), i.e. the stem of sdd/specs/<slug>.spec.md.

Returns: tuple[str, str] | None — (feature_id, source_path) when the slug already carries an id, or None when it does not (no spec, no index, or a spec still holding the template's FEAT-<NNN> placeholder).

Function main

CLI entry point.

Prints the reserved IDs one per line on success (exit 0); prints an error to stderr and exits 1 on failure. reserve_ids() itself refuses to run (see its docstring) if tracked files have uncommitted changes besides the ledger file, if the current branch does not match --base-branch, or if --count is not positive — this CLI surfaces all three as a clean error message rather than a traceback.

main(argv: list[str] | None = None) -> int

Parameter Type Default Description
argv list[str] | None None

Returns: int

Function reserve_ids

Atomically reserve count sequential TASK or FEAT numbers.

Reads sdd/tasks/.id_ledger.json as of origin/<base_branch>, computes the next count numbers of the given kind, builds a ledger-only commit on that tip with git plumbing, and pushes it to refs/heads/<base_branch>. The push IS the compare-and-swap: on a non-fast-forward rejection the remote moved first, so the allocator fetches, re-reads the now-current ledger, recomputes, and retries up to max_retries times (with jittered backoff). It raises IdReservationError if retries are exhausted, or if the push fails for a reason OTHER than a non-fast-forward rejection (e.g. auth/network failure), since those are not safe to retry the same way.

The pushed commit touches ONLY sdd/tasks/.id_ledger.json and has origin/<base_branch> as its parent, so the reservation race is decided by a single-line JSON diff — never by whatever else the caller happens to have committed locally.

Local commits on the base branch are never published and never destroyed. Attempts run entirely in the object database plus a throwaway index; no attempt mutates the local branch, the index or the working tree, and no code path here runs git reset. After a successful push the local branch is fast-forwarded onto the reservation as a convenience; if it cannot be (because it carries unpushed commits), it is left untouched and a warning explains how to reconcile.

reserve_ids(kind: Literal['task', 'feature'], count: int, base_branch: str, label: str, *, max_retries: int = 5, repo_root: Path | None = None, sleep_fn: Callable[[float], None] = time.sleep) -> IdReservation

Parameter Type Default Description
kind Literal['task', 'feature'] "task" or "feature".
count int Number of sequential IDs to reserve. Must be >= 1 — a non-positive count would silently rewind the ledger's counter below IDs already issued, reopening the exact collision this allocator exists to close.
base_branch str The branch to push the ledger-only commit to (e.g. "dev"). reserve_ids() refuses to run unless this is also the currently checked-out branch in repo_root.
label str Free-text origin of this reservation (feature slug or session id) — diagnostic only, stored in the ledger's updated_by field.
max_retries int 5 Maximum number of read-build-push attempts before raising IdReservationError.
repo_root Path | None None Directory to run all git operations in. Defaults to the current working directory. Tests pass a throwaway clone here so the retry loop never touches the real repository.
sleep_fn Callable[[float], None] time.sleep Callable used for the jittered backoff between retries. Defaults to time.sleep; tests inject a no-op to avoid waiting in real time.

Returns: IdReservation — The IdReservation describing the IDs that were successfully reserved.

Raises:

  • ValueError — If count < 1.
  • IdReservationError — When every attempt is rejected up to max_retries, when a push fails for a non-retryable reason, when the contract check (clean tree, correct branch) fails, or — for kind="feature" — when label already owns a feature id (see :func:existing_feature_id).

Alias save_ledger

Re-exported from parrot.sdd.scripts.id_ledger.save_ledger.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally