# SaC Apply And Ledger

`univer sac apply <package.univer>` validates package-local source under `project/migrations/`, validates the package ledger, bundles pending packs, and writes workbook state plus `manifest.metadata.sac.migrationLedger` back to the same project workbook package.

By default, every successfully applied pack is also committed through workbook versioning as one local changeset. If the newest applied pack is wrong, roll back the package with `univer reset <package.univer> --hard HEAD~1`, inspect, then fix the migration source before applying again.

Use `--no-commit` only for compatibility or recovery workflows where SaC should write ledger metadata without creating workbook local changesets.

## Target Lifecycle

The package path is the target. `--target` is not supported, and `project/univer.config.ts` does not contain artifact output settings.

If the package has `project/univer.config.ts` but no SaC ledger, run `univer sac rebuild <package.univer>` to establish the package-local source/workbook baseline. Apply does not infer or repair a missing ledger.

## Ledger Contract

Apply updates the project workbook package pack by pack. Each applied pack writes a ledger entry with pack id, source hash, previous pack pointer, workbook content hash, runner metadata, timestamp, and origin.

Apply fails before executing pending source when:

- the package project is missing
- the package ledger is missing or malformed
- the package ledger is ahead of source
- an already-applied pack id has a different source hash
- workbook content has drifted since the ledger head
- automatic workbook commit finds no pending workbook mutations
- automatic workbook commit returns more than one local changeset for a single pack

If every source pack is already ledgered with matching hashes and the workbook content has not drifted, apply succeeds as an up-to-date no-op.

## Verification

Apply success is not the Spreadsheet TDD completion gate. For assertion-backed SaC work, run `univer sac verify <package.univer> --json` after apply and read the referenced `internal/sac/runs/<run-id>/verify-report.json`.

```bash
univer sac verify ./model.univer --json
univer inspect workbook ./model.univer
univer pipe out ./model.univer --range "Revenue!A1:C4" --format tsv
univer view ./model.univer
```
