Skip to content

core: build one media inventory with kept dispositions and count-only sync reporting for hygiene review #2780

Description

@schlessera

Context

Part of the epic #1777 (resolve pending media through hygiene review). Read the epic for every maintainer ruling; this task builds the one per-file media inventory that both the sync count and hygiene review use, its dispositions and the count/report changes.

Today sync lists each media path in its report (...leftovers.media.map, packages/core/src/lib/sync/report.ts:76) and noninteractive sync asks for judgment about media (needsAgent, packages/core/src/cli/commands/sync.ts:220-226). Assessment skips Git-ignored files and applies media.ignore before MEDIA/LARGE classification (assess, packages/core/src/lib/sync/assess.ts:127-155; mediaPolicyClass, packages/core/src/lib/media.ts:47-51), so an ignored file is ARTIFACT, and generated brains ignore .brain/scratch/ (template/.gitignore:24-27). Hygiene states are open | snoozed | dismissed | resolved (HygieneState, packages/core/src/lib/hygiene.ts:41), read offline by readHygieneLog (:1506).

Design: rev 4 §1, §2 and §4; rev 5 §5.7; rev 6 §1-§4; rev 6.1 in full.

Maintainer decision (2026-10-24)

A/A/A/A selected for the four inventory-policy questions:

  1. Add only conversation-pinned scratch MEDIA/LARGE paths to the existing assessment population, including pinned scratch paths matching media.ignore. Do not admit ignored non-scratch media or walk unpinned scratch output. Ignore-over-track precedence remains unchanged for assessment, destinations and proposed rules.
  2. Pinned scratch findings are review-only and do not change Media awaiting review: N. Do not add media.linked or a linked-count report suffix.
  3. A valid ready:false pin snapshot contributes its already-listed positive evidence, with an explicit incomplete-scan notice. Missing, malformed or unreadable snapshots admit no scratch findings and report pins-unknown. Incomplete evidence never grants deletion permission or proves an exhaustive reference scan.
  4. Read at most 5,000 distinct pinned paths per inventory, with a trusted-host override. Overflow explicitly reports inventory-too-large/not fully checked, without claiming exhaustive discovery; ordinary assessment remains independent.

Use read-only offline pin discovery shared by CLI and server. Apply the proposal fingerprint/invalidation rule: hash sorted session keys that list the path, so unrelated pin changes do not invalidate a disposition; missing/unpinned originals stop being findings and stale apply writes nothing. Completed kept copies remain bound to copy/copySha256. The existing effect safety gates remain unchanged.

Scope

  • One inventory function, shared by offline brain sync and server review: identity is the exact raw repo-relative path; directories expand once; MEDIA plus LARGE on one file is one finding with both provenances; symlinks are counted with no actions (rev 4 §1).
  • The admission path for ignored and .brain/scratch/ conversation-linked media, under the approved narrow pinned-scratch exception, including media.ignore-matching scratch pins; ignored non-scratch files stay excluded.
  • Severity B and ordering: MEDIA informational, LARGE warning, then the existing severity → urgency (unknown) → age → identity order of docs/decisions/hygiene-review.md.
  • Fingerprints: content sha256, bytes, classes, matching track/ignore entries, the reference-inventory digest and, for scratch sources, the pin digest (rev 4 §1).
  • Leave pending (A): a per-pass deferral in review operational state that survives same-pass refresh, reconnect and restart, lapses on a fingerprint change, and is reselected by a new pass; an all-deferred pass ends with truthful counts.
  • Dispositions: the dedicated kept state in context/hygiene/kept.md (K1 A, rev 5 §5.7, rev 6 §1), read identically offline and on the server; kept.md is not created empty.
  • Count (K2 B): Media awaiting review: N excludes media with a valid dismissed, snoozed or kept disposition and includes per-pass deferrals; due snoozes and invalidated dispositions return. The routine report enumerates no paths; needsAgent loses its media clause.
  • Compatibility per rev 6.1: existing fields keep their values, types and names; the additive fields (copy, copySha256, keptOn with null defaults, reconcile.kept, counts.kept, media: {awaitingReview, setAside, kept}) are always present; no-op hygiene logs and the no-media sync report text stay byte-identical.

Out of scope

Acceptance criteria

  • A pinned scratch media file matching media.ignore is reviewable; ignored non-scratch media and unpinned scratch output stay excluded. No scratch directory walk is added.
  • Valid ready:false pins are admitted with the incomplete notice; invalid/missing/unreadable snapshots admit none and report pins-unknown. A 5,001-path fixture hits the default bound and reports incomplete discovery; a trusted-host override is tested. Neither case grants deletion permission or exhaustive-scan status.
  • Pin fingerprints change only for session links to the same path; unrelated pin changes preserve deferrals. Pruned/unpinned sources resolve and stale apply writes nothing; completed kept copies retain their copy-based identity.
  • Scratch pins never alter the routine awaitingReview count or add media.linked/report suffixes; offline and server behavior agree for complete, incomplete, unknown and overflowing pin snapshots.
  • Offline brain sync and server review produce the same inventory, count and selection for the same tree, proven by a parity test including dismissed, snoozed, due-snooze, kept and deferred media.
  • Only the approved conversation-pinned scratch exception joins ordinary assessment; its findings enter review but not the routine count, with a fixture where a normally ignored, conversation-linked scratch file reaches Keep a permanent copy eligibility.
  • The routine sync report shows only Media awaiting review: N and a route into review; no media path appears; status and exit code do not change because of media.
  • Leave pending survives same-pass refresh, reconnect and restart, returns on a new pass, lapses on a relevant fingerprint change and not on an unrelated one.
  • The rev 6.1 G1-G6 goldens pass: file bytes, legacy JSON projection, always-present defaults, new complete goldens, report cases and no inappropriate kept enum value; its omission, ordering, empty-file, counter and report mutations each fail their intended assertion and are restored, recorded in the PR.

Verification

bun run test packages/core packages/ui-server
bun run typecheck
bun run lint

Odysseus fixture brains, real Git, offline.

Contract impact

Additive CLI --json and HTTP fields and the new kept hygiene state, per rev 6.1 §4: CONTRACT: commits, same-commit CLI and package contract updates, API reports as affected, minor core, ui-sdk and ui-server changesets. Changed human report wording is documented. No removal or retyping of an existing field.

2026-10-24 triage at main 77f35ef1ba34e4fab7092ada5362ae799852ea24: The later design supplies a concrete inventory proposal but leaves four policy choices unapproved. Folded those exact options and consequences into the body; retain needs: decision rather than treating proposed admission/count/partial-pin/bound defaults as approved. Existing epic rulings and additive compatibility commitments remain binding.

2026-10-24 maintainer ruling: selected A/A/A/A, incorporated the narrow pinned-scratch exception, review-only count, incomplete positive-evidence handling and 5,000-path bound into scope and parity proof. All four inventory-policy decisions are resolved; effect approvals and safety gates remain unchanged.

Activity

  1. added
    type: feat
    area: core
    area: ui-server
    priority: p2
    needs: design
    contract
    on Oct 24, 2026
  2. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    Needs design (2026-10-24): how ignored and .brain/scratch/ conversation-linked media enter the media inventory.

    Decided: one shared inventory feeds the sync count and hygiene review; Persist here must handle a conflicting media.ignore (ignore-conflict ruling B); Keep a permanent copy exists for conversation-linked scratch media (Q2 B); the count excludes valid dismissed, snoozed and kept media (K2 B). Left open: the inherited inventory rule (rev 2 §1.1, carried through rev 4 §1) says media.ignore matches are not pending, and the inventory is built from assessment, which skips every Git-ignored file, including .brain/scratch/. So the files two approved actions target never reach review. The code cannot settle this: it is a choice about which files a person is asked about and what the routine count means.

    Design the media-inventory admission path for brain-kit issue #2780 (schlessera/brain-kit), a child of epic #1777.
    
    Read first:
    - Epic #1777 (all rulings in its body) and #2780's body.
    - assess, packages/core/src/lib/sync/assess.ts:127-155 (skips Git-ignored files, applies media policy before MEDIA/LARGE).
    - mediaPolicyClass, packages/core/src/lib/media.ts:47-51 (media.ignore wins over media.track; ignore yields ARTIFACT).
    - matchesAnyPattern / globToRegex, packages/core/src/lib/tool-leftovers.ts:30-46.
    - template/.gitignore:24-27 (.brain/scratch/ is ignored, pruned after 7 days or past 1 GB).
    - scratchLinks and PIN_FILE, packages/ui-server/src/cron/scratch-pins.ts:10-44 (conversation links held in .brain/scratch-pins/pins.json).
    - Design comments on #1777: rev 2 §1.1 (comment 6162562264), rev 4 §1, §4 and §5 (6166364429), rev 5 §5.7 (6187571597), rev 6 §1-§2 (6197387842).
    - docs/decisions/hygiene-review.md (identity, fingerprints, ordering), docs/media.md, docs/decisions/native-brain-storage.md.
    
    The problem: an agent writes .brain/scratch/2026-07-12/ithaca-harbour.png and links it in a conversation. The reader wants to keep it. Keep a permanent copy is the approved action for exactly this file, but assess never sees it because .brain/scratch/ is Git-ignored, so it never becomes a finding. Likewise a file under a media.ignore glob is ARTIFACT and never pending, though Persist here's ignore-conflict proposal assumes it can be selected.
    
    Design:
    1. Which ignored files are admitted to the inventory: conversation-linked scratch media only (pins), files matching media.ignore, both, or another bounded set. Name the discovery source for each (pins, a bounded walk, assessment) and its limits.
    2. How deliberate disposable output (renders, drafts, tool leftovers) stays out, so a scratch area full of transient files does not flood review.
    3. Whether admitted ignored media count in "Media awaiting review: N", appear only in review, or count separately; and how offline brain sync and the server get the same answer without the CLI reading server-only state (pins.json is a file under the brain, readable offline; say whether that is enough).
    4. Their eligibility per action (rev 4 §4 matrix), and what happens when pins are not ready (pins-unknown).
    5. Fingerprint and invalidation for admitted ignored files, and what a later prune of the scratch original does to a pending finding.
    
    Requirements to hold: every ruling in the epic body (count-only report, MEDIA info / LARGE warning, per-pass Leave pending, rollback A, M1 A no negation syntax, Q1 A bounds, Q2 B, K1 A, K2 B); no second policy store; no change to media.ignore/track precedence; no automatic commit; Odysseus fixtures and the pinned 2026-07-12 date; offline/server parity.
    
    Deliverable: a design comment on #2780 answering 1-5 with the exact rule, the bounded discovery, count semantics and test fixtures (including one where a normally ignored, conversation-linked scratch file reaches Keep a permanent copy). "Only pinned scratch media are admitted, and they appear in review but not in the routine count" is an acceptable kind of answer if it is justified; so is a recommendation that needs a maintainer ruling, stated as options.
    
  3. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    Design brief (1/2): admitting ignored and scratch-linked media

    Answers the design prompt. Read at main: assess (sync/assess.ts:127-155), mediaPolicyClass (media.ts:47-51), template/.gitignore:24-27, scratchLinks/openScratchPins (cron/scratch-pins.ts), the epic rulings and the rollback-A ruling. The rollback-A ruling already expects review to touch ignored scratch: "Explicitly retain required media bytes even when ignored scratch would otherwise receive metadata-only observation."

    Summary

    • Admit one extra bounded set: conversation-linked scratch media. These are paths listed in .brain/scratch-pins/pins.json that exist under .brain/scratch/ as regular files and are MEDIA or LARGE.
    • Do not admit media.ignore matches, or Git-ignored files outside scratch. An ignore rule is the user's own recorded decision that the file is disposable. Asking about it again would turn every render folder into review work.
    • Ruling B (ignore conflicts) still applies: it fires when a Persist / Keep-copy destination or a proposed track rule collides with media.ignore, not when the source is ignored (§4).
    • Scratch-linked media appear in review only. They do not add to Media awaiting review: N, because scratch prunes itself and needs no decision to stay correct. See Decision 2.

    1 · Admission rule (exact)

    A path enters the inventory if either condition holds:

    Source Condition Discovery
    Assessment (unchanged) not Git-ignored, not media.ignore, MEDIA or LARGE existing assess walk
    Scratch pins (new) listed in any sessions[*] of pins.json; starts with .brain/scratch/; lstat is a regular file; isMediaPath or bytes ≥ maxTrackedBytes read pins.json only. No walk of .brain/scratch/.
    • Paths come from the pins file as written; scratchLinks already normalizes them and refuses .., \ and control characters. They are re-checked here before any lstat, and anything outside .brain/scratch/ is dropped.
    • A pinned path that matches media.ignore is still admitted. Pins are stronger evidence ("someone linked this in a conversation") than a broad rule, and the scratch area is never tracked anyway.
    • A pinned symlink is counted on the existing symlink line with no actions (rev 4 §1). A pinned missing path is not a finding.
    • Bound: at most 5,000 distinct pinned paths are read per inventory (Decision 4). Past that, the scratch section returns inventory-too-large and is honestly marked not fully checked, in the same way as Q1 A. The assessment part is unaffected.

    2 · Keeping disposable output out

    • Admission requires a conversation link. An unlinked render, draft or tool leftover in .brain/scratch/ never becomes a finding, however large it is.
    • media.ignore matches outside scratch stay ARTIFACT (mediaPolicyClass is unchanged; M1 A, no precedence change).
    • Non-media under scratch is admitted only when LARGE, so a linked .json or .md under 5 MiB stays out.
    • Scratch already prunes after 7 days or past 1 GB, so the set shrinks without user action.

    3 · Count and offline/server parity

    Population In Media awaiting review: N In review
    Assessment media yes (K2 B exclusions apply) yes
    Scratch-linked media no yes, after assessment media, in a From conversations group
    • pins.json is an ordinary file under the brain. The offline CLI reads it read-only, with O_NOFOLLOW, as a regular file, and the version is checked. It never writes to it, and no server state is needed. Both sides run the same pure inventory(tree, policy, pinsSnapshot), so selection is the same.
    • The count does not depend on pins at all. This keeps the routine report deterministic even when pins are unreadable.
    • The rev 6.1 additive shape needs no new field for this. If Decision 2 picks (b), it adds media.linked (a number, default 0). That is additive.

    Part 2 covers eligibility, pins-unknown, fingerprints, prune, fixtures and decisions.

  4. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    Design brief (2/2): eligibility, pins-unknown, fingerprints, fixtures, decisions

    4 · Eligibility (rev 4 §4 matrix, scratch-linked rows)

    Action Scratch-linked finding Why
    Leave pending yes per-pass deferral, as for any finding
    Keep a permanent copy yes (its only purpose, Q2 B) the original's bytes, path and pins are untouched
    Persist here refused: Files in the scratch area can't be kept in place. Use Keep a permanent copy. .brain/** is protected; scratch is never tracked
    Move refused (Q2 B, linked original) history can't be rewritten
    Delete yes, with Linked from N conversations; those links will stop working. as in rev 3 §3.3
    Dismiss / Snooze yes K2 B dispositions

    Where ruling B (ignore conflict) fires:

    1. A Keep-copy destination matches media.ignore. Example: copying to places/ithaca/harbour.png while media.ignore has "places/**/*.png". The copy's exact track rule would lose, so the governed adjustment is proposed, with its own approval.
    2. Persist here on an assessment finding, when the exact-file track rule or a broader proposed rule overlaps an existing ignore entry. For example, a newly added ignore glob covers the file between preview and apply: the apply-time recheck returns stale, and a fresh preview offers the adjustment.

    5 · Pins not ready or unreadable

    pins.json state Admission Review line
    ready: true listed paths none
    ready: false (scan incomplete) listed paths are still admitted: a listed pin is positive evidence Conversation links are still being checked; more files may appear.
    missing, unreadable, not a regular file, bad version none (pins-unknown) Conversation-linked files couldn't be checked.

    Nothing is lost while pins are unknown: the existing pruner already suspends deletion when the snapshot is not ready.

    6 · Fingerprint and invalidation

    Fingerprint fields: content sha256, bytes, classes, matching track/ignore entries, and the pin digest = sha256 of the sorted session keys that list this path. The rest of the pins file is not part of it.

    • A new conversation linking the same file changes the digest. Deferral and snooze lapse, and the file comes back on the next pass.
    • An unrelated session's pins changing doesn't affect it.
    • Prune of the original (7 days / 1 GB): the finding is resolved ("not detected"). A deferral drops, and an in-flight Keep-copy apply fails its source-sha premise and returns stale, writing nothing. A completed Keep copy stays valid, because kept binds to copy + copySha256, not to the original.
    • Unpin (session deleted): same as prune, the finding resolves.

    7 · Fixtures (Odysseus, pinned 2026-07-12, real Git, offline)

    1. Keep copy reaches eligibility: .brain/scratch/2026-07-12/ithaca-harbour.png (PNG, 1.2 MB) is listed under session ["pi","voyage-home"], with ready: true. It is admitted, appears in review under From conversations, and the count is unchanged. Keep a permanent copy → places/ithaca/harbour.png gives eligibility plus a preview.
    2. Unpinned .brain/scratch/2026-07-12/loom-draft-03.png is not admitted.
    3. media.ignore: ["assets/renders/"] with assets/renders/loom.png: ARTIFACT, not admitted, not counted.
    4. Fixture 1 + media.ignore: ["places/**/*.png"] gives the ruling-B adjustment proposal on the copy destination.
    5. ready: false with the fixture 1 listing: admitted with the "still being checked" line. A missing pins.json gives pins-unknown, no scratch findings, and the count is unchanged.
    6. A pinned symlink is on the symlink line with no actions. A pinned ../etc/passwd-style entry is dropped.
    7. Pin digest: adding an unrelated session keeps the deferral; a second session linking the harbour file lapses it.
    8. Prune after deferral resolves the finding; prune between preview and apply gives stale with the bytes unchanged.
    9. Parity: the CLI and the server get identical inventory and selection for fixtures 1–8. Mutation: ignoring pins.json on the CLI fails parity.

    Open decisions

    1. Admit media.ignore matches? (a) No; ruling B applies to destinations and rules (above). (b) Yes, review-only, in a "Set aside by your rules" group. Recommend (a): an ignore rule is a recorded decision, and (b) makes every ignored render folder review work again.
    2. Count scratch-linked media? (a) Review only, with no count change. (b) Counted under an additive media.linked, with the report line Media awaiting review: N (+M from conversations). Recommend (a): scratch self-prunes, and a count that grows with every illustrated chat isn't actionable.
    3. Admit listed pins when ready: false? (a) Yes, with the notice. (b) No, treat it as pins-unknown. Recommend (a): listed paths are never wrong positives for "was linked".
    4. Bound on pinned paths read: 5,000, trusted-host configurable like Q1 A. This is a new number; it is far above a 1 GB scratch's realistic linked count.
  5. added
    needs: decision
    and removed
    needs: design
    on Oct 24, 2026
  6. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    2026-10-24 triage at main 77f35ef1ba34e4fab7092ada5362ae799852ea24: The later design supplies a concrete inventory proposal but leaves four policy choices unapproved. Folded those exact options and consequences into the body; retain needs: decision rather than treating proposed admission/count/partial-pin/bound defaults as approved. Existing epic rulings and additive compatibility commitments remain binding.

  7. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    Decision (maintainer, 2026-10-24): A/A/A/A. Add conversation-pinned scratch media only (including media.ignore-matching scratch pins), without admitting ignored non-scratch files or walking unpinned scratch. Scratch findings are review-only and do not change the routine media count or add media.linked. Valid ready:false snapshots admit listed positive evidence with an incomplete-scan notice; missing/malformed/unreadable snapshots admit none and report pins-unknown. Default bound: 5,000 distinct pinned paths, trusted-host configurable, with explicit incomplete reporting on overflow. This grants no deletion permission or exhaustive-scan claim; existing effect/reference safety gates remain binding. The body now selects all four branches with offline/server parity and fingerprint proof.

  8. removed
    needs: decision
    on Oct 24, 2026
  9. 3 remaining items

  10. added this to the 0.45.0 milestone on Nov 2, 2026
  11. schlessera commented on Nov 2, 2026

    @schlessera
    OwnerAuthor

    Delivered to the epic integration branch epic/1777-media-hygiene as 98a15b92a via #3288; ready-candidate proof passed against the exact epic base. This issue closes when the epic batch lands on main.

  12. removed their assignment
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: corearea: ui-servercontractpriority: p2type: feat

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions