# Attribute Stories to Source Requirements

You are the lineage attribution agent. You are given one imported source file and the complete
list of stories that already exist for this Target. Identify the distinct requirements the source
states, and for each one name the stories that implement it.

This is a matching task against a closed set. Every story you may name is listed in `<stories>`.
You are not decomposing work, proposing new stories, or judging whether the existing stories are
correct.

## Method

1. Read the source and identify each distinct requirement it states. A requirement is a thing the
   system must do, at whatever granularity the author wrote it. One sentence may state one
   requirement that several stories implement — for example "add a table and show it on screen"
   is one requirement implemented by a schema story, a route story, and a view story. Do not split
   a requirement to make the mapping tidier, and do not merge two requirements that a reader would
   act on separately.
2. Give each requirement a short kebab-case name that describes it. The name is an identifier, not
   a summary: `mark-book-read`, not `the-reader-can-mark-a-book-as-read`.
3. For each requirement, list every story that implements any part of it. A story may implement
   more than one requirement; a requirement may need more than one story.
4. List any story that implements no requirement in the source as `<unattached>`. This is expected
   and correct for foundational work — application scaffolding, configuration, shared UI framing,
   test harnesses — that the author never asked for by name. Do not force such a story onto an
   unrelated requirement.

## Rules

- Use only story ids that appear in `<stories>`. Never invent one.
- Every story must appear exactly once, either inside a `stories` attribute or as `<unattached>`.
- Quote the requirement text verbatim from the source in the tag body. Do not paraphrase it.
- Emit nothing but the tags below. No preamble, no commentary, no explanation.

## Output

```text
<requirement name="add-remove-books" stories="add-book,remove-book,database">
The reader can add a book with a title and author, view the books in the order added, and remove a book.
</requirement>
<requirement name="reject-empty-fields" stories="validate-book">
An empty title or author is rejected with a clear error message.
</requirement>
<unattached story="architecture"/>
<unattached story="ui-general"/>
```

# Attribution job

<source name="run.sh">
#!/bin/sh
# run.sh — DupFind grader. Do not filter, skip, or reinterpret.
#
# Builds ./cmd/dupfind with `go build`, creates a fixed directory tree, and compares every
# command's exit code and standard output with the known answer below. Standard error is never
# compared.
#
#   sh sources/run.sh                from the application root (how `drydock uat` runs it)
#   sh uat/DupFind/bin/run.sh APP    from anywhere, grading the application root APP
#
# Exit 0 only when the build succeeds and every check passes.
set -u

APP=$(cd "${1:-.}" && pwd)
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT INT TERM
BIN="$WORK/dupfind"

if ! (cd "$APP" && go build -o "$BIN" ./cmd/dupfind); then
    echo "error: go build ./cmd/dupfind failed at the application root $APP." >&2
    echo "The deliverable is a Go module whose main package is ./cmd/dupfind." >&2
    exit 1
fi

# make_tree <dir> — the exam tree. Every byte is fixed, so every hash is known.
make_tree() {
    T=$1
    mkdir -p "$T/a" "$T/b" "$T/c/deep/x" "$T/photos" "$T/music" "$T/notes" "$T/.hidden" "$T/big" \
        "$T/empty" "$T/unique"
    printf 'quarterly report\n' > "$T/a/report.txt"
    printf 'quarterly report\n' > "$T/b/report-copy.txt"
    printf 'quarterly report\n' > "$T/c/deep/x/report.txt"
    : > "$T/photos/img1.jpg"
    i=0
    while [ $i -lt 16 ]; do printf 'JPEG%04d' $i >> "$T/photos/img1.jpg"; i=$((i + 1)); done
    cp "$T/photos/img1.jpg" "$T/photos/img1 (copy).jpg"
    printf 'la la la\n' > "$T/music/song.mp3"
    printf 'la la la\n' > "$T/music/song,live.mp3"
    printf 'abcdefgh\n' > "$T/notes/one.txt"
    printf 'abcdefgX\n' > "$T/notes/two.txt"
    printf 'KEY=1\n' > "$T/.hidden/.env"
    printf 'KEY=1\n' > "$T/env.bak"
    head -c 1000 /dev/zero | tr '\0' z > "$T/big/blob-a.bin"
    head -c 1000 /dev/zero | tr '\0' z > "$T/big/blob-b.bin"
    head -c 1000 /dev/zero | tr '\0' y > "$T/big/blob-c.bin"
    : > "$T/empty/one"
    : > "$T/empty/two"
    ln -s ../a/report.txt "$T/notes/report-link.txt"
    printf 'only one of me\n' > "$T/unique/solo.txt"
}

TREE="$WORK/tree"
make_tree "$TREE"

PASSED=0
FAILED=0

# check <id> <expected exit> <dupfind args...>  — expected standard output on stdin.
check() {
    id=$1
    want_rc=$2
    shift 2
    cat > "$WORK/want"
    (cd "$WORK" && "$BIN" "$@") > "$WORK/got" 2> "$WORK/err"
    rc=$?
    if [ "$rc" -eq "$want_rc" ] && cmp -s "$WORK/want" "$WORK/got"; then
        PASSED=$((PASSED + 1))
        echo "PASS $id: dupfind $*"
    else
        FAILED=$((FAILED + 1))
        echo "FAIL $id: dupfind $*"
        echo "  exit: expected $want_rc, got $rc"
        diff "$WORK/want" "$WORK/got" | sed 's/^/  /'
        sed 's/^/  stderr: /' "$WORK/err"
    fi
}

# ── A. Full listing ──────────────────────────────────────────────────────────────────────
check A1 0 tree <<'EOF'
group,size,sha256,path
1,1000,950f88b09cf1d5e2cdbc5660c77dce3962265c548797950095629a0ea2daea46,big/blob-a.bin
1,1000,950f88b09cf1d5e2cdbc5660c77dce3962265c548797950095629a0ea2daea46,big/blob-b.bin
2,128,5c633a59e48621c6ca2a9c2c254792627ed975a86228be1fdf9d8a177520c0f7,photos/img1 (copy).jpg
2,128,5c633a59e48621c6ca2a9c2c254792627ed975a86228be1fdf9d8a177520c0f7,photos/img1.jpg
3,17,8a3c67892f82af22b58377b8e85bb41899053788d3aa3b81d2e6b4580e917c29,a/report.txt
3,17,8a3c67892f82af22b58377b8e85bb41899053788d3aa3b81d2e6b4580e917c29,b/report-copy.txt
3,17,8a3c67892f82af22b58377b8e85bb41899053788d3aa3b81d2e6b4580e917c29,c/deep/x/report.txt
4,9,31f86092953fd687403924ef23270af8da18b58147a0055468ecb4dc00b1c5c5,"music/song,live.mp3"
4,9,31f86092953fd687403924ef23270af8da18b58147a0055468ecb4dc00b1c5c5,music/song.mp3
5,6,929c28f541260cd8690babe97ae61472829cdf36e059c7b14e6b963f0f9ee77b,.hidden/.env
5,6,929c28f541260cd8690babe97ae61472829cdf36e059c7b14e6b963f0f9ee77b,env.bak
EOF
check A2 0 "$TREE" <<'EOF'
group,size,sha256,path
1,1000,950f88b09cf1d5e2cdbc5660c77dce3962265c548797950095629a0ea2daea46,big/blob-a.bin
1,1000,950f88b09cf1d5e2cdbc5660c77dce3962265c548797950095629a0ea2daea46,big/blob-b.bin
2,128,5c633a59e48621c6ca2a9c2c254792627ed975a86228be1fdf9d8a177520c0f7,photos/img1 (copy).jpg
2,128,5c633a59e48621c6ca2a9c2c254792627ed975a86228be1fdf9d8a177520c0f7,photos/img1.jpg
3,17,8a3c67892f82af22b58377b8e85bb41899053788d3aa3b81d2e6b4580e917c29,a/report.txt
3,17,8a3c67892f82af22b58377b8e85bb41899053788d3aa3b81d2e6b4580e917c29,b/report-copy.txt
3,17,8a3c67892f82af22b58377b8e85bb41899053788d3aa3b81d2e6b4580e917c29,c/deep/x/report.txt
4,9,31f86092953fd687403924ef23270af8da18b58147a0055468ecb4dc00b1c5c5,"music/song,live.mp3"
4,9,31f86092953fd687403924ef23270af8da18b58147a0055468ecb4dc00b1c5c5,music/song.mp3
5,6,929c28f541260cd8690babe97ae61472829cdf36e059c7b14e6b963f0f9ee77b,.hidden/.env
5,6,929c28f541260cd8690babe97ae61472829cdf36e059c7b14e6b963f0f9ee77b,env.bak
EOF

# ── B. Summary ───────────────────────────────────────────────────────────────────────────
check B1 0 --summary tree <<'EOF'
groups,duplicate_files,reclaimable_bytes
5,6,1177
EOF

# ── C. Minimum size ──────────────────────────────────────────────────────────────────────
check C1 0 --min-size 20 tree <<'EOF'
group,size,sha256,path
1,1000,950f88b09cf1d5e2cdbc5660c77dce3962265c548797950095629a0ea2daea46,big/blob-a.bin
1,1000,950f88b09cf1d5e2cdbc5660c77dce3962265c548797950095629a0ea2daea46,big/blob-b.bin
2,128,5c633a59e48621c6ca2a9c2c254792627ed975a86228be1fdf9d8a177520c0f7,photos/img1 (copy).jpg
2,128,5c633a59e48621c6ca2a9c2c254792627ed975a86228be1fdf9d8a177520c0f7,photos/img1.jpg
EOF
check C2 0 --summary --min-size 20 tree <<'EOF'
groups,duplicate_files,reclaimable_bytes
2,2,1128
EOF
check C3 0 --min-size 200 tree <<'EOF'
group,size,sha256,path
1,1000,950f88b09cf1d5e2cdbc5660c77dce3962265c548797950095629a0ea2daea46,big/blob-a.bin
1,1000,950f88b09cf1d5e2cdbc5660c77dce3962265c548797950095629a0ea2daea46,big/blob-b.bin
EOF

# ── D. No duplicates ─────────────────────────────────────────────────────────────────────
check D1 0 tree/unique <<'EOF'
group,size,sha256,path
EOF
check D2 0 --summary tree/empty <<'EOF'
groups,duplicate_files,reclaimable_bytes
0,0,0
EOF

# ── E. Operational failures ──────────────────────────────────────────────────────────────
check E1 1 tree/no-such-directory < /dev/null
check E2 1 tree/env.bak < /dev/null

# ── U. Usage errors ──────────────────────────────────────────────────────────────────────
check U1 2 < /dev/null
check U2 2 tree tree/a < /dev/null
check U3 2 --min-size abc tree < /dev/null
check U4 2 --min-size 0 tree < /dev/null
check U5 2 --frobnicate tree < /dev/null

TOTAL=$((PASSED + FAILED))
echo "DupFind: $PASSED passed, $FAILED failed (of $TOTAL)"
[ "$FAILED" -eq 0 ]
</source>

<stories>
  <story id="foundation" implements="ARCHITECTURE.md">Stand up the Go module, package tree, and test launcher that every other story builds into.</story>
  <story id="arg-parsing" implements="FEATURE-ArgParsing.md">Parse --summary, --min-size, and exactly one directory argument; reject malformed usage with exit 2.</story>
  <story id="module-build" implements="FEATURE-ModuleBuild.md">Provide the cmd/dupfind main package that builds with go build and delegates to cli.Run.</story>
  <story id="directory-validation" implements="FEATURE-DirectoryValidation.md">Exit 1 with empty stdout when the directory is missing or is not a directory.</story>
  <story id="walk" implements="FEATURE-Walk.md">Walk the directory recursively and yield eligible regular files at or above the minimum size.</story>
  <story id="size-grouping" implements="FEATURE-SizeGrouping.md">Group candidate files by exact size and keep only sizes shared by two or more files.</story>
  <story id="digest-grouping" implements="FEATURE-DigestGrouping.md">Hash same-size files with SHA-256 and form duplicate groups, skipping unreadable files with a warning.</story>
  <story id="group-ordering" implements="FEATURE-GroupOrdering.md">Compute reclaimable bytes, order groups, number them from 1, and order paths within each group.</story>
  <story id="listing" implements="FEATURE-Listing.md">Write the CSV duplicate listing with header group,size,sha256,path using encoding/csv.</story>
  <story id="summary" implements="FEATURE-Summary.md">Write the one-row summary of groups, duplicate files, and reclaimable bytes with --summary.</story>
  <story id="test-gate" implements="FEATURE-TestGate.md">Provide POSIX bin/test.sh and a Go test suite covering every rule, output form, and exit code.</story>
  <story id="verification" implements="VERIFICATION.md">Assemble the finished tool and confirm the supplied grader passes end to end.</story>
</stories>
