Skip to content

Latest commit

 

History

History
101 lines (77 loc) · 5.06 KB

journal.md

File metadata and controls

101 lines (77 loc) · 5.06 KB

journal.sh — guide

Revision: 1 Last modified: 2026-12-04T00:00:00Z

Library: scripts/pr/lib/journal.sh (sourced by scripts/pr/lib/steps.sh; never executed on its own). Engine guide: docs/scripts/pr_deliver.md. Manual: docs/manuals/pr_delivery_engine_manual.md.

Overview

Make an interrupted pull-request delivery RESUMABLE, and make "what did this run actually do" answerable without asking the provider. Every state the engine is about to reach is appended to a per-task journal BEFORE the mutation is attempted, so a crash can never lose the fact that a branch, a push or a pull request already exists.

Two files per task, both under the journal directory:

File Shape Read cost
<task>.jsonl append-only event stream, one JSON object per line O(history) — used for history and pr_journal_get
<task>.state atomic snapshot of the LAST event (temp → sync → rename) O(1) — what pr_journal_state reads

Prerequisites

  • bash 4+; sed, find, mktemp; sync is used when present and skipped when absent.
  • The journal directory must resolve to an absolute path (see Edge cases).
  • No provider, no credential and no network: the journal is local state only.

Usage examples

The library is sourced, so every call is a plain shell function:

. "$PR_SELF_DIR/lib/journal.sh"

pr_journal_init "$task"          # sets PR_JOURNAL_FILE / PR_JOURNAL_SNAPSHOT
pr_journal_append pushed head="$sha" branch="$branch"
pr_journal_state                 # -> "pushed"
pr_journal_get head              # -> the head recorded by the LAST event carrying it
pr_journal_list                  # every <task>.jsonl in the directory, sorted

Choosing where it lives — first match wins:

PR_JOURNAL_DIR=/var/tmp/pr-journal bash scripts/pr/pr_deliver.sh status   # 1. environment
delivery_journal_dir=/var/tmp/pr-journal      # 2. key in config/delivery.conf
                                              # 3. $HOME/.local/state/helix-pr-delivery/<owner__repo>

Read one task's history without any provider contact:

$ bash scripts/pr/pr_deliver.sh status --task CON-641
JOURNAL: go_posted
JOURNAL-PR: 128
JOURNAL-HEAD: 4f2c1a9…

Edge cases

  • A relative journal directory is refused (pr_journal_init prints to stderr and returns 1). A journal whose location depends on the caller's working directory would silently scatter one task's history across several files.
  • No journal yet is not an error: pr_journal_state returns 1 and prints nothing, and status reports no delivery journal in <dir>. Callers must treat that as "nothing recorded", never as "done".
  • Appending before pr_journal_init prints journal: not initialised and returns 1 — the engine never writes a journal it has not pointed at a task.
  • A torn last line is impossible to observe through pr_journal_state: it reads the snapshot, which is replaced by rename(2) only after its own sync. The .jsonl is the append log; a hard stop can leave a partial trailing line there, and pr_journal_get ignores lines that do not parse as "key":"value".
  • pr_journal_get returns the value from the LAST event that carried the key, not the first. The journal is append-only, so a later event wins by construction.
  • sync is best-effort (command -v sync guard): on a host without it the write is still ordered by the filesystem, and the atomic rename keeps the snapshot self-consistent.

Internal behaviour

  • pr_json_escape escapes \ " \n \r \t so a single-line JSON object is always produced without jq.
  • pr_journal_append STATE [key=value …] writes one line {"at":"<UTC>","state":"<s>",…} to the .jsonl, calls sync on it, then writes the same line to <snapshot>.tmp.$$, syncs it, and renames it over <snapshot>. Any failure to write the snapshot removes the temp file and leaves the previous snapshot.
  • The journal records state and identifiers only — never a credential (the engine redacts output through pr_redact in lib/common.sh, and no adapter passes a token here).
  • States written by lib/steps.sh: preflighted, branch_ready, committed, pushed, pr_opened, reviewed, approval_recorded, go_posted, merge_ready, merging, merge_called, merged, parity_ok, cleaned, done, plus the terminal-for-this-run refused, blocked, failed and the stale_branch event.

Related scripts

  • steps.sh — the only writer, and the state machine the states belong to.
  • lock.sh — takes its lock directory from the same journal directory.
  • common.sh — pr_now, pr_say, pr_out and the redaction this library relies on.
  • pr_deliver.md — status and recover, the two subcommands that read a journal.

Last verified

2026-12-04 — read against scripts/pr/lib/journal.sh (133 lines) at the revision this page was written. The status transcript above is the shape the engine prints (TASK: / JOURNAL: / JOURNAL-PR: / JOURNAL-HEAD: / PROVIDER:), not a captured run of a specific task.