# 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="INSTRUCTIONS.md">
# Build Instructions: A Duplicate File Finder

## Objective

Build a command-line tool that finds duplicate files under a directory. Two files are duplicates
when their contents are byte-for-byte identical. The tool reports each group of duplicates and the
disk space that deleting all but one copy of each would recover. It never deletes, moves, or
modifies a file.

## Stack

Implement in Go 1.22 or newer using only the standard library. The application root is a Go
module, and its executable is the main package at `./cmd/dupfind`. Tests run under `go test`.

## Run Harness

`sources/run.sh` is the scoring entry point. It is supplied, not authored, and is staged verbatim
into the application root. `drydock uat` runs `sh sources/run.sh` from the completed application
root and takes its exit code and output as the score.

The harness runs `go build -o <temporary path> ./cmd/dupfind`, creates a fixed directory tree in a
temporary directory, and then runs 15 checks. Each check compares one command's exit code and
standard output with a known answer. Standard error is never compared. The harness prints `PASS`
or `FAIL` per check and ends with:

```text
DupFind: NN passed, NN failed (of 15)
```

`sources/run.sh` is the exam. Do not modify, rewrite, regenerate, or substitute it. Application
code never reads it and never embeds an answer from it.

## Command

```text
dupfind [--summary] [--min-size <bytes>] <directory>
```

Options precede the directory. Exactly one directory is required.

| Option | Meaning |
|---|---|
| `--summary` | Write the summary instead of the listing. |
| `--min-size <bytes>` | Ignore files smaller than `<bytes>`. A positive integer; default `1`. |

## Rules

1. **Scope.** The tool walks `<directory>` recursively, including hidden files and directories
   (names beginning `.`). It considers regular files only. It neither follows nor reports
   symbolic links.
2. **Empty files are never duplicates.** A file of zero bytes is always ignored, which the
   default `--min-size 1` guarantees.
3. **Duplicate test.** Files are grouped by size first. Only files that share a size are read.
   Within a size, files are grouped by the SHA-256 digest of their contents. A group of two or
   more files with the same size and digest is a duplicate group.
4. **Paths** are written relative to `<directory>`, with `/` as the separator and no leading
   `./`.
5. **Order.** Groups are ordered by reclaimable bytes descending, then by digest ascending.
   A group's reclaimable bytes are `size × (files − 1)`. Groups are numbered from 1 in that order.
   Within a group, paths are ordered by byte value ascending.
6. **Unreadable files.** A file that cannot be read is skipped with one warning line on standard
   error. It does not change the exit code.

## Output

Output is CSV on standard output: a header row, then data rows, with `\n` line endings. A field
that contains a comma, a double quote, or a newline is quoted as RFC 4180 specifies. Go's
`encoding/csv` writer produces this format.

**Listing** (default): header `group,size,sha256,path`, then one row per file in every duplicate
group. `sha256` is the lowercase hexadecimal digest. With no duplicates, only the header is
written.

```text
group,size,sha256,path
1,17,8a3c67892f82af22b58377b8e85bb41899053788d3aa3b81d2e6b4580e917c29,a/report.txt
1,17,8a3c67892f82af22b58377b8e85bb41899053788d3aa3b81d2e6b4580e917c29,b/report-copy.txt
```

**Summary** (`--summary`): header `groups,duplicate_files,reclaimable_bytes`, then one row.
`duplicate_files` is the sum of `files − 1` over all groups. `reclaimable_bytes` is the sum of
every group's reclaimable bytes. With no duplicates the row is `0,0,0`.

A failing command writes nothing to standard output and one diagnostic line to standard error.

## Exit codes

| Code | Meaning |
|---|---|
| `0` | Success, whether or not duplicates were found. |
| `1` | Operational failure: `<directory>` does not exist or is not a directory. |
| `2` | Usage error: missing or extra arguments, unknown option, `--min-size` not a positive integer. |

## Tests

The completed application provides a POSIX-compatible `bin/test.sh` that runs `go vet ./...` and
`go test ./...` from the application root and exits zero only when both pass. The suite covers
each rule, each output form, and each exit code against real temporary directories.
</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>
