Get started
Locked YAML Writer — Safe Shared YAML Edits

Locked YAML Writer — Safe Shared YAML Edits

Edit a shared YAML task board safely when several agents or scripts write it at once. Takes an exclusive flock, edits only the target record's lines as text so comments and formatting survive, validates with safe_load, refuses the write if any other record would change, detects writers that skip the lock, replaces atomically, and rolls back on failure. CLI (set-field, append-note, add-record) and Python API. 11 tests. Local only: no network calls or credentials. #Engenharia

Avaliação
São necessárias mais avaliações
Vendas
0
Como usar
Baixar

What you get

  • Safe concurrent edits to one shared YAML file: no lost updates, no half-written reads
  • Text-level edits that keep comments, quoting and order, with checks and automatic rollback
  • A CLI and Python API, an instruction file for Claude Code, and 11 tests

What it does

Shared YAML task boards break in two common ways:

  • Lost updates. Two writers read the file, each changes something, and the second write silently erases the first.
  • Destroyed formatting. yaml.safe_load followed by yaml.dump rewrites the entire file: comments vanish, quoting and ordering change, and a one-line change becomes a whole-file diff.

A third, quieter failure: a reader opens the file while a writer is halfway through and gets half a document.

How it works

Every write runs the same sequence:

  1. Take an exclusive flock on a sidecar lock file (.<name>.lock next to the target).
  2. Read the file and edit only the lines of the target record, as text.
  3. Parse the result with yaml.safe_load and verify that every other record is unchanged, the top-level key order is kept, and the target holds the expected value.
  4. Re-read the file. If someone wrote it without the lock in the meantime, start again (up to 5 tries).
  5. Write to a temp file in the same directory, fsync, then os.replace (atomic on POSIX).
  6. Read back and verify again. If verification fails, restore the previous text.

If any check fails, nothing is written and the tool exits with code 1.

Inputs and outputs

Input: a YAML file shaped as below, plus one change (set a field, append to a note, or add a record).

Output: the same file with only that change, and an exit code.

File shape

A top-level mapping of records, each a mapping of fields:

# Comments are kept.
task_build_docs:
  status: open      # inline comments are kept too
  owner: alice
task_release_check:
  status: in_progress

Usage

# change one field
python3 scripts/locked_yaml_writer.py set-field examples/records.yaml task_build_docs status done

# append to a string field (creates it if missing)
python3 scripts/locked_yaml_writer.py append-note examples/records.yaml task_release_check notes "smoke test passed"

# add a new record at the end
python3 scripts/locked_yaml_writer.py add-record examples/records.yaml task_publish --field status=open --field owner=dan

Multi-line values are written as literal block scalars (|-), so colons, brackets and quotes in free text cannot break the file.

Exit codes

code meaning
0 written
1 refused or failed — the file was not changed
2 usage error
3 no change needed (value already set)

From Python

from locked_yaml_writer import set_field, append_note, add_record, update_text

set_field(path, "task_build_docs", "status", "done")
update_text(path, lambda text: text.replace("owner: bob", "owner: erin"), record="task_release_check")

Package contents

file purpose
SKILL.md instructions for Claude Code (or any coding agent)
scripts/locked_yaml_writer.py the tool (CLI + importable functions)
tests/test_locked_yaml_writer.py 11 tests, including 6 processes writing concurrently
examples/records.yaml sample record file
LICENSE license terms

Review and safety disclosure

  • It changes only the file you name, plus a .<name>.lock file next to it.
  • No network calls, credentials, background services, or installers.
  • POSIX only (fcntl.flock). Not for Windows.
  • The lock protects writers that use this tool. A writer that ignores it is detected by the re-read check, but only if it finishes before the final replace.
  • Records must be top-level keys made of letters, digits, _ and -; fields are edited at two-space indentation.
  • Nested values deeper than one level are written with yaml.safe_dump for that one field only.