faculties 0.21.0

An office suite for AI agents: kanban, wiki, files, messaging, and a Lissajous-backed viewer — all persisted in a TribleSpace pile.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#!/usr/bin/env python3
"""Read-only, race-detecting evidence for branch and worktree hygiene.

The JSON Lines interface is versioned by ``FORMAT_VERSION``.  This program is
deliberately incapable of cleanup: its only commands are ``report`` and
``due``, and every Git operation is checked against a read-only allow-list.
It never fetches or contacts other machines. Default discovery inventories
direct child main checkouts and their registered worktrees; pass nested
independent clone paths explicitly. This is local evidence, not a definitive
cross-machine or remote-publication audit.
"""

from __future__ import annotations

import argparse
import json
import os
from pathlib import Path
import subprocess
import sys
from typing import Any, Iterable


FORMAT_NAME = "faculties.worktree-audit"
FORMAT_VERSION = 1
INDETERMINATE = 126
PATCH_COMMIT_LIMIT = 256
READ_ONLY_GIT_COMMANDS = frozenset(
    {
        "cherry",
        "for-each-ref",
        "merge-base",
        "rev-list",
        "rev-parse",
        "status",
        "worktree",
    }
)


class AuditFailure(RuntimeError):
    state = "INDETERMINATE"


class AuditRace(AuditFailure):
    state = "RACED"


def display_bytes(value: bytes) -> str:
    return os.fsdecode(value)


def quote(value: str) -> str:
    return json.dumps(value, ensure_ascii=True)


class Git:
    def __init__(self, repository: Path) -> None:
        self.repository = repository
        # `git -C` does not override repository-selection variables inherited
        # from the caller.  A stray GIT_DIR/GIT_WORK_TREE (or alternate object
        # database/index) could therefore make an audit named for repository A
        # inspect repository B.  Start from the ordinary process environment,
        # but make every Git-specific input an explicit choice below.
        self.environment = {
            key: value
            for key, value in os.environ.items()
            if not key.startswith("GIT_")
        }
        self.environment["GIT_OPTIONAL_LOCKS"] = "0"
        # A partial clone may otherwise fetch an absent object as a side effect
        # of an apparently read-only ancestry/tree query.
        self.environment["GIT_NO_LAZY_FETCH"] = "1"
        self.environment["GIT_NO_REPLACE_OBJECTS"] = "1"
        self.environment["LC_ALL"] = "C"

    def run(
        self,
        *arguments: str,
        at: Path | None = None,
        allowed: tuple[int, ...] = (0,),
    ) -> subprocess.CompletedProcess[bytes]:
        if not arguments or arguments[0] not in READ_ONLY_GIT_COMMANDS:
            raise AssertionError(f"non-read-only Git command refused: {arguments!r}")
        if arguments[0] == "worktree" and arguments[1:2] != ("list",):
            raise AssertionError(
                f"mutating Git worktree command refused: {arguments!r}"
            )
        location = at if at is not None else self.repository
        try:
            result = subprocess.run(
                ["git", "-C", os.fspath(location), *arguments],
                env=self.environment,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
            )
        except OSError as error:
            raise AuditFailure(f"could not run git: {error}") from error
        if result.returncode not in allowed:
            detail = result.stderr.decode("utf-8", "replace").strip()
            rendered = " ".join(arguments)
            raise AuditFailure(
                f"git {rendered} failed with {result.returncode}"
                + (f": {detail}" if detail else "")
            )
        return result

    def refs(self) -> dict[str, dict[str, str | None]]:
        result = self.run(
            "for-each-ref",
            "--format=%(refname)%09%(objectname)%09%(symref)",
            "refs/heads",
            "refs/remotes",
        )
        refs: dict[str, dict[str, str | None]] = {}
        for raw_line in result.stdout.splitlines():
            fields = raw_line.split(b"\t")
            if len(fields) != 3 or not fields[0] or not fields[1]:
                raise AuditFailure("git for-each-ref returned malformed output")
            ref = display_bytes(fields[0])
            if ref in refs:
                raise AuditFailure(f"git for-each-ref repeated {ref!r}")
            refs[ref] = {
                "sha": fields[1].decode("ascii", "strict"),
                "symref": display_bytes(fields[2]) if fields[2] else None,
            }
        return refs

    def worktrees_raw(self) -> bytes:
        return self.run("worktree", "list", "--porcelain", "-z").stdout


def parse_worktrees(raw: bytes) -> list[dict[str, Any]]:
    records: list[dict[str, Any]] = []
    record: dict[str, Any] = {}
    for field in raw.split(b"\0"):
        if not field:
            if record:
                if "path" not in record:
                    raise AuditFailure("worktree record has no path")
                if not record.get("bare") and "head" not in record:
                    raise AuditFailure("non-bare worktree record has no HEAD")
                records.append(record)
                record = {}
            continue
        key_bytes, separator, value = field.partition(b" ")
        try:
            key = key_bytes.decode("ascii")
        except UnicodeDecodeError as error:
            raise AuditFailure("worktree record has a non-ASCII field name") from error
        if key == "worktree" and separator:
            if record:
                raise AuditFailure("worktree record is missing its separator")
            record["path"] = display_bytes(value)
        elif key == "HEAD" and separator:
            record["head"] = value.decode("ascii", "strict")
        elif key == "branch" and separator:
            record["branch"] = display_bytes(value)
        elif key == "detached" and not separator:
            record["detached"] = True
        elif key == "bare" and not separator:
            record["bare"] = True
        elif key in {"locked", "prunable"}:
            record[key] = display_bytes(value) if separator else True
        else:
            raise AuditFailure(f"unknown git worktree field {display_bytes(field)!r}")
    if record:
        raise AuditFailure("unterminated git worktree record")
    return records


def resolve_explicit_base(git: Git, value: str) -> tuple[str, str, str | None]:
    expression = f"{value}^{{commit}}"
    result = git.run(
        "rev-parse",
        "--verify",
        "--quiet",
        "--end-of-options",
        expression,
        allowed=(0, 1),
    )
    if result.returncode != 0:
        raise AuditFailure(f"explicit base {value!r} does not resolve to a commit")
    sha = result.stdout.decode("ascii", "strict").strip()
    symbolic = git.run(
        "rev-parse",
        "--symbolic-full-name",
        "--verify",
        "--quiet",
        "--end-of-options",
        value,
        allowed=(0, 1),
    )
    resolved_ref = symbolic.stdout.decode("utf-8", "surrogateescape").strip() or None
    return value, sha, resolved_ref


def select_base(
    git: Git, refs: dict[str, dict[str, str | None]], explicit: str | None
) -> dict[str, Any]:
    if explicit is not None:
        observed, sha, resolved = resolve_explicit_base(git, explicit)
        return {
            "ref": observed,
            "resolved_ref": resolved,
            "sha": sha,
            "selection": "explicit",
            "freshness": "not_checked",
        }

    remote_heads = sorted(
        ref
        for ref, entry in refs.items()
        if ref.startswith("refs/remotes/")
        and ref.endswith("/HEAD")
        and entry["symref"] is not None
        and ref != "refs/remotes/origin/HEAD"
    )
    remote_mains = sorted(
        ref
        for ref in refs
        if ref.startswith("refs/remotes/")
        and ref.endswith("/main")
        and ref != "refs/remotes/origin/main"
    )
    remote_masters = sorted(
        ref
        for ref in refs
        if ref.startswith("refs/remotes/")
        and ref.endswith("/master")
        and ref != "refs/remotes/origin/master"
    )
    precedence = [
        "refs/remotes/origin/HEAD",
        *remote_heads,
        "refs/remotes/origin/main",
        "refs/heads/main",
        *remote_mains,
        "refs/remotes/origin/master",
        "refs/heads/master",
        *remote_masters,
    ]
    for ref in precedence:
        if ref in refs:
            return {
                "ref": ref,
                "resolved_ref": refs[ref]["symref"] or ref,
                "sha": refs[ref]["sha"],
                "selection": "automatic",
                "freshness": "not_checked",
            }
    raise AuditFailure(
        "base unresolved (tried every remote HEAD plus conventional main/master refs)"
    )


def primary_local_ref(base: dict[str, Any]) -> str | None:
    ref = base.get("resolved_ref") or base["ref"]
    if ref.startswith("refs/heads/"):
        return ref
    prefix = "refs/remotes/"
    if ref.startswith(prefix):
        remote_and_branch = ref[len(prefix) :]
        _, separator, branch = remote_and_branch.partition("/")
        if separator:
            return "refs/heads/" + branch
    return None


def dirty_state(git: Git, worktree: dict[str, Any]) -> dict[str, Any]:
    if worktree.get("bare"):
        return {"status": "not_applicable", "dirty": None}
    if "prunable" in worktree:
        return {
            "status": "unavailable",
            "dirty": None,
            "reason": "registered worktree is marked prunable",
        }
    output = git.run(
        "status",
        "--porcelain=v2",
        "-z",
        "--untracked-files=normal",
        at=Path(worktree["path"]),
    ).stdout
    return {"status": "observed", "dirty": bool(output)}


def is_ancestor(git: Git, older: str, newer: str) -> bool:
    result = git.run("merge-base", "--is-ancestor", older, newer, allowed=(0, 1))
    return result.returncode == 0


def tree_of(git: Git, sha: str) -> str:
    result = git.run("rev-parse", "--verify", "--end-of-options", f"{sha}^{{tree}}")
    return result.stdout.decode("ascii", "strict").strip()


PATCH_CAVEAT = (
    "git cherry patch equivalence is non-definitive: it does not establish "
    "semantic equivalence, preserve commit narrative, or authorize disposition"
)


def patch_evidence(git: Git, base: str, subject: str) -> dict[str, Any]:
    if base == subject:
        return {
            "status": "not_applicable",
            "reason": "subject is the exact base commit",
            "definitive_landed_disposition": False,
            "caveat": PATCH_CAVEAT,
        }
    # Cap the census itself. An omitted patch comparison must not first walk an
    # unbounded history merely to discover that it should be omitted.
    census_cap = PATCH_COMMIT_LIMIT + 1
    subject_only = int(
        git.run(
            "rev-list",
            "--count",
            f"--max-count={census_cap}",
            f"{base}..{subject}",
        ).stdout.strip()
        or b"0"
    )
    if subject_only == 0:
        return {
            "status": "not_applicable",
            "reason": "exact ancestry leaves no subject-only commits",
            "subject_only_commits": 0,
            "definitive_landed_disposition": False,
            "caveat": PATCH_CAVEAT,
        }
    base_only = int(
        git.run(
            "rev-list",
            "--count",
            f"--max-count={census_cap}",
            f"{subject}..{base}",
        ).stdout.strip()
        or b"0"
    )
    if subject_only + base_only > PATCH_COMMIT_LIMIT:
        return {
            "status": "not_checked",
            "reason": (
                f"comparison spans at least {subject_only + base_only} one-sided commits, "
                f"over the {PATCH_COMMIT_LIMIT}-commit audit bound"
            ),
            "subject_only_commits": subject_only,
            "base_only_commits": base_only,
            "definitive_landed_disposition": False,
            "caveat": PATCH_CAVEAT,
        }
    result = git.run("cherry", base, subject)
    equivalent: list[str] = []
    distinct: list[str] = []
    for line in result.stdout.splitlines():
        if len(line) < 3 or line[:1] not in {b"+", b"-"} or line[1:2] != b" ":
            raise AuditFailure("git cherry returned malformed output")
        sha = line[2:].decode("ascii", "strict")
        (equivalent if line[:1] == b"-" else distinct).append(sha)
    return {
        "status": "observed",
        "method": "git cherry with exact observed base and subject SHAs",
        "subject_only_commits": subject_only,
        "base_only_commits": base_only,
        "patch_equivalent_subject_commits": equivalent,
        "patch_distinct_subject_commits": distinct,
        "definitive_landed_disposition": False,
        "caveat": PATCH_CAVEAT,
    }


def lineage_state(
    subject: str, base: str, subject_is_ancestor: bool, base_is_ancestor: bool
) -> str:
    if subject == base:
        return "exact_base"
    if subject_is_ancestor:
        return "ancestor_of_base"
    if base_is_ancestor:
        return "descendant_of_base"
    return "diverged"


def make_subjects(
    refs: dict[str, dict[str, str | None]], worktrees: list[dict[str, Any]]
) -> list[dict[str, Any]]:
    subjects: list[dict[str, Any]] = []
    for ref in sorted(name for name in refs if name.startswith("refs/heads/")):
        subjects.append({"kind": "local_head", "name": ref, "sha": refs[ref]["sha"]})
    for index, worktree in enumerate(worktrees):
        if worktree.get("bare"):
            continue
        subjects.append(
            {
                "kind": "worktree_head",
                "name": f"HEAD@{index}",
                "path": worktree["path"],
                "branch": worktree.get("branch"),
                "detached": bool(worktree.get("detached")),
                "sha": worktree["head"],
            }
        )
    return subjects


def audit_repository(repository: Path, explicit_base: str | None) -> dict[str, Any]:
    git = Git(repository)
    refs_before = git.refs()
    worktrees_raw_before = git.worktrees_raw()
    worktrees = parse_worktrees(worktrees_raw_before)
    base = select_base(git, refs_before, explicit_base)

    custody: list[dict[str, Any]] = []
    for worktree in worktrees:
        observed = dict(worktree)
        observed["dirty"] = dirty_state(git, worktree)
        custody.append(observed)

    subjects = make_subjects(refs_before, worktrees)
    all_shas = {subject["sha"] for subject in subjects}
    all_shas.update(entry["sha"] for entry in refs_before.values())
    all_shas.add(base["sha"])
    trees = {sha: tree_of(git, sha) for sha in sorted(all_shas)}

    refs_by_sha: dict[str, list[str]] = {}
    refs_by_tree: dict[str, list[str]] = {}
    for ref, entry in refs_before.items():
        refs_by_sha.setdefault(str(entry["sha"]), []).append(ref)
        refs_by_tree.setdefault(trees[str(entry["sha"])], []).append(ref)
    worktrees_by_sha: dict[str, list[str]] = {}
    worktrees_by_tree: dict[str, list[str]] = {}
    for worktree in worktrees:
        if worktree.get("bare"):
            continue
        worktrees_by_sha.setdefault(worktree["head"], []).append(worktree["path"])
        worktrees_by_tree.setdefault(trees[worktree["head"]], []).append(
            worktree["path"]
        )

    ancestry_cache: dict[str, tuple[bool, bool]] = {}
    patch_cache: dict[str, dict[str, Any]] = {}
    lineage: list[dict[str, Any]] = []
    for subject in subjects:
        sha = subject["sha"]
        if sha not in ancestry_cache:
            ancestry_cache[sha] = (
                is_ancestor(git, sha, base["sha"]),
                is_ancestor(git, base["sha"], sha),
            )
        if sha not in patch_cache:
            patch_cache[sha] = patch_evidence(git, base["sha"], sha)
        subject_is_ancestor, base_is_ancestor = ancestry_cache[sha]
        item = dict(subject)
        item.update(
            {
                "state": lineage_state(
                    sha, base["sha"], subject_is_ancestor, base_is_ancestor
                ),
                "subject_is_ancestor_of_base": subject_is_ancestor,
                "base_is_ancestor_of_subject": base_is_ancestor,
                "exact_sha_witnesses": {
                    "refs": sorted(refs_by_sha.get(sha, [])),
                    "worktrees": sorted(worktrees_by_sha.get(sha, [])),
                },
                "tree": trees[sha],
                "exact_tree_witnesses": {
                    "base": trees[sha] == trees[base["sha"]],
                    "refs": sorted(refs_by_tree.get(trees[sha], [])),
                    "worktrees": sorted(worktrees_by_tree.get(trees[sha], [])),
                },
                "patch_evidence": patch_cache[sha],
            }
        )
        lineage.append(item)

    refs_after = git.refs()
    worktrees_raw_after = git.worktrees_raw()
    if refs_after != refs_before or worktrees_raw_after != worktrees_raw_before:
        raise AuditRace("refs or registered worktree HEADs changed during the audit")
    worktrees_after = parse_worktrees(worktrees_raw_after)
    dirty_after = [dirty_state(git, worktree) for worktree in worktrees_after]
    dirty_before = [worktree["dirty"] for worktree in custody]
    if dirty_after != dirty_before:
        raise AuditRace("worktree or index dirtiness changed during the audit")
    if explicit_base is not None:
        _, final_sha, final_ref = resolve_explicit_base(git, explicit_base)
        if final_sha != base["sha"] or final_ref != base["resolved_ref"]:
            raise AuditRace("explicit base changed during the audit")

    primary = primary_local_ref(base)
    attention: list[str] = []
    for worktree in custody:
        dirty = worktree["dirty"]
        if dirty["status"] == "unavailable":
            attention.append(f"unavailable custody at {worktree['path']!r}")
        elif dirty.get("dirty"):
            attention.append(f"dirty custody at {worktree['path']!r}")
        if worktree.get("detached"):
            attention.append(f"detached HEAD at {worktree['path']!r}")
    for subject in lineage:
        if subject["kind"] != "local_head":
            continue
        if subject["name"] == primary and subject["sha"] == base["sha"]:
            continue
        attention.append(
            f"local head {subject['name']} is {subject['state']} relative to base"
        )

    repository_name = worktrees[0]["path"] if worktrees else os.fspath(repository)
    return {
        "schema": FORMAT_NAME,
        "version": FORMAT_VERSION,
        "type": "repository",
        "repository": repository_name,
        "state": "ATTENTION" if attention else "QUIET",
        "base": base,
        "remote_freshness": {
            "status": "not_checked",
            "reason": "the audit never fetches or contacts a remote",
        },
        "live_process_custody": {
            "status": "not_checked",
            "reason": "portable live-process ownership is not inferred from Git metadata",
        },
        "custody": custody,
        "lineage": lineage,
        "attention": attention,
    }


def discover_repositories(root: Path, explicit: Iterable[str]) -> list[Path]:
    supplied = [Path(path).expanduser() for path in explicit]
    if supplied:
        return supplied
    root = root.expanduser()
    if (root / ".git").exists():
        return [root]
    try:
        children = sorted(root.iterdir(), key=lambda path: os.fsencode(path.name))
    except OSError as error:
        raise AuditFailure(f"cannot inspect audit root {root}: {error}") from error
    # Default workspace discovery intentionally selects main checkouts. Linked
    # worktrees normally carry a .git *file* and are inventoried by their main
    # repository's `git worktree list`, avoiding duplicate audits.
    repositories = [child for child in children if (child / ".git").is_dir()]
    if not repositories:
        raise AuditFailure(f"no Git repositories found directly under {root}")
    return repositories


def failure_record(repository: Path | None, error: AuditFailure) -> dict[str, Any]:
    return {
        "schema": FORMAT_NAME,
        "version": FORMAT_VERSION,
        "type": "repository" if repository is not None else "audit",
        "repository": os.fspath(repository) if repository is not None else None,
        "state": error.state,
        "diagnostic": str(error),
    }


def emit_jsonl(records: list[dict[str, Any]]) -> None:
    for record in records:
        print(
            json.dumps(record, ensure_ascii=True, sort_keys=True, separators=(",", ":"))
        )


def emit_human(records: list[dict[str, Any]]) -> None:
    print(f"worktree-audit format v{FORMAT_VERSION} (strictly read-only)")
    for record in records:
        repository = record.get("repository")
        print()
        print(
            f"repository {quote(repository) if repository is not None else '<audit>'}"
        )
        print(f"  state: {record['state']}")
        if record["state"] in {"INDETERMINATE", "RACED"}:
            print(f"  diagnostic: {record['diagnostic']}")
            continue
        base = record["base"]
        print(
            f"  base: {base['ref']} @ {base['sha']}"
            + (
                f" (resolved {base['resolved_ref']})"
                if base["resolved_ref"] != base["ref"]
                else ""
            )
        )
        print("  remote freshness: NOT CHECKED (this audit never fetches)")
        print("  live-process custody: NOT CHECKED")
        print("  custody:")
        for custody in record["custody"]:
            dirty = custody["dirty"]
            if dirty["status"] == "observed":
                condition = "DIRTY" if dirty["dirty"] else "clean"
            else:
                condition = dirty["status"].upper()
            branch = custody.get("branch") or "(detached)"
            print(
                f"    {condition:11} {branch} @ {custody.get('head', '-')} "
                f"path={quote(custody['path'])}"
            )
        print("  lineage (custody-independent):")
        for subject in record["lineage"]:
            identity = subject["name"]
            if subject["kind"] == "worktree_head":
                identity += f" path={quote(subject['path'])}"
            print(f"    {subject['state']:18} {identity} @ {subject['sha']}")
            sha_refs = subject["exact_sha_witnesses"]["refs"]
            tree_refs = subject["exact_tree_witnesses"]["refs"]
            print(f"      exact-SHA ref witnesses: {', '.join(sha_refs) or 'none'}")
            print(
                f"      tree {subject['tree']} · exact-tree ref witnesses: "
                f"{', '.join(tree_refs) or 'none'}"
            )
            patch = subject["patch_evidence"]
            if patch["status"] == "observed":
                print(
                    "      patch evidence (NON-DEFINITIVE): "
                    f"{len(patch['patch_equivalent_subject_commits'])} equivalent, "
                    f"{len(patch['patch_distinct_subject_commits'])} distinct"
                )
            else:
                print(
                    f"      patch evidence (NON-DEFINITIVE): {patch['status']} — "
                    f"{patch['reason']}"
                )
        if record["attention"]:
            print("  attention:")
            for reason in record["attention"]:
                print(f"    - {reason}")


def build_parser() -> argparse.ArgumentParser:
    class AuditArgumentParser(argparse.ArgumentParser):
        def error(self, message: str) -> None:
            raise AuditFailure(f"invalid arguments: {message}")

    parser = AuditArgumentParser(
        description="Produce read-only branch/worktree custody and lineage evidence"
    )
    commands = parser.add_subparsers(dest="command", required=True)
    for name in ("report", "due"):
        command = commands.add_parser(name)
        command.add_argument(
            "--jsonl", action="store_true", help="emit versioned JSON Lines"
        )
        command.add_argument(
            "--base",
            help="explicit base revision (otherwise use the documented precedence)",
        )
        command.add_argument(
            "--root",
            default=os.environ.get("WORKTREE_AUDIT_ROOT", "."),
            help="local workspace root for direct-child main checkouts; name nested independent clones explicitly (default: current directory)",
        )
        command.add_argument("repositories", nargs="*")
    return parser


def main(argv: list[str] | None = None) -> int:
    try:
        args = build_parser().parse_args(argv)
    except AuditFailure as error:
        print(f"worktree-audit: {error}", file=sys.stderr)
        return INDETERMINATE
    records: list[dict[str, Any]] = []
    try:
        repositories = discover_repositories(Path(args.root), args.repositories)
    except AuditFailure as error:
        records.append(failure_record(None, error))
    else:
        for repository in repositories:
            try:
                records.append(audit_repository(repository, args.base))
            except AuditFailure as error:
                records.append(failure_record(repository, error))
            except (OSError, UnicodeError, ValueError) as error:
                records.append(
                    failure_record(
                        repository,
                        AuditFailure(
                            f"could not interpret repository evidence: {error}"
                        ),
                    )
                )

    if args.jsonl:
        emit_jsonl(records)
    elif args.command == "report":
        emit_human(records)

    if any(record["state"] in {"INDETERMINATE", "RACED"} for record in records):
        return INDETERMINATE
    if args.command == "due":
        return 0 if any(record["state"] == "ATTENTION" for record in records) else 1
    return 0


if __name__ == "__main__":
    sys.exit(main())