# diff.apply

> Apply a previously computed diff payload to the current document. The document fingerprint must match the diff base fingerprint. Omitted changeMode applies story content directly; explicit tracked mode governs all four story families (body, header/footer parts, footnotes, endnotes). Comments, styles, and numbering are always applied directly. Supported mixed full-diff payloads apply atomically across body, comments, styles, numbering, and header/footers. Unsupported/deferred families such as package-graph/media/hyperlink closure, settings/theme, and textboxes fail closed before mutation.



- Member path: `doc.diff.apply(…)`
- Mutates document: yes
- Idempotency: `conditional`
- Supports tracked mode: yes
- Supports dry run: no

## Expected result

Returns a DiffApplyResult with applied operation count and diagnostics.

## Input schema

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {},
  "type": "object",
  "properties": {
    "diff": {
      "type": "object",
      "properties": {
        "version": {
          "type": "string",
          "enum": [
            "sd-diff-payload/v1",
            "sd-diff-payload/v2"
          ]
        },
        "engine": {
          "type": "string",
          "enum": [
            "superdoc-v2"
          ]
        },
        "baseFingerprint": {
          "type": "string"
        },
        "targetFingerprint": {
          "type": "string"
        },
        "coverage": {
          "type": "object",
          "properties": {
            "body": {
              "type": "boolean",
              "const": true
            },
            "comments": {
              "type": "boolean"
            },
            "styles": {
              "type": "boolean"
            },
            "numbering": {
              "type": "boolean"
            },
            "headerFooters": {
              "type": "boolean"
            }
          },
          "additionalProperties": false,
          "required": [
            "body",
            "comments",
            "styles",
            "numbering",
            "headerFooters"
          ]
        },
        "summary": {
          "type": "object",
          "properties": {
            "hasChanges": {
              "type": "boolean"
            },
            "changedComponents": {
              "type": "array",
              "items": {
                "type": "string",
                "enum": [
                  "body",
                  "comments",
                  "styles",
                  "numbering",
                  "headerFooters",
                  "parts"
                ]
              }
            },
            "body": {
              "type": "object",
              "properties": {
                "hasChanges": {
                  "type": "boolean"
                }
              },
              "additionalProperties": false,
              "required": [
                "hasChanges"
              ]
            },
            "comments": {
              "type": "object",
              "properties": {
                "hasChanges": {
                  "type": "boolean"
                }
              },
              "additionalProperties": false,
              "required": [
                "hasChanges"
              ]
            },
            "styles": {
              "type": "object",
              "properties": {
                "hasChanges": {
                  "type": "boolean"
                }
              },
              "additionalProperties": false,
              "required": [
                "hasChanges"
              ]
            },
            "numbering": {
              "type": "object",
              "properties": {
                "hasChanges": {
                  "type": "boolean"
                }
              },
              "additionalProperties": false,
              "required": [
                "hasChanges"
              ]
            },
            "headerFooters": {
              "type": "object",
              "properties": {
                "hasChanges": {
                  "type": "boolean"
                }
              },
              "additionalProperties": false,
              "required": [
                "hasChanges"
              ]
            },
            "parts": {
              "type": "object",
              "properties": {
                "hasChanges": {
                  "type": "boolean"
                }
              },
              "additionalProperties": false,
              "required": [
                "hasChanges"
              ]
            }
          },
          "additionalProperties": false,
          "required": [
            "hasChanges",
            "changedComponents",
            "body",
            "comments",
            "styles",
            "numbering",
            "headerFooters",
            "parts"
          ]
        },
        "payload": {
          "type": "object",
          "description": "Opaque engine-owned diff data."
        }
      },
      "additionalProperties": false,
      "required": [
        "version",
        "engine",
        "baseFingerprint",
        "targetFingerprint",
        "coverage",
        "summary",
        "payload"
      ]
    }
  },
  "additionalProperties": false,
  "required": [
    "diff"
  ]
}
```

## Output schema

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {},
  "type": "object",
  "properties": {
    "appliedOperations": {
      "type": "integer"
    },
    "baseFingerprint": {
      "type": "string"
    },
    "targetFingerprint": {
      "type": "string"
    },
    "coverage": {
      "type": "object",
      "properties": {
        "body": {
          "type": "boolean",
          "const": true
        },
        "comments": {
          "type": "boolean"
        },
        "styles": {
          "type": "boolean"
        },
        "numbering": {
          "type": "boolean"
        },
        "headerFooters": {
          "type": "boolean"
        }
      },
      "additionalProperties": false,
      "required": [
        "body",
        "comments",
        "styles",
        "numbering",
        "headerFooters"
      ]
    },
    "summary": {
      "type": "object",
      "properties": {
        "hasChanges": {
          "type": "boolean"
        },
        "changedComponents": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "body",
              "comments",
              "styles",
              "numbering",
              "headerFooters",
              "parts"
            ]
          }
        },
        "body": {
          "type": "object",
          "properties": {
            "hasChanges": {
              "type": "boolean"
            }
          },
          "additionalProperties": false,
          "required": [
            "hasChanges"
          ]
        },
        "comments": {
          "type": "object",
          "properties": {
            "hasChanges": {
              "type": "boolean"
            }
          },
          "additionalProperties": false,
          "required": [
            "hasChanges"
          ]
        },
        "styles": {
          "type": "object",
          "properties": {
            "hasChanges": {
              "type": "boolean"
            }
          },
          "additionalProperties": false,
          "required": [
            "hasChanges"
          ]
        },
        "numbering": {
          "type": "object",
          "properties": {
            "hasChanges": {
              "type": "boolean"
            }
          },
          "additionalProperties": false,
          "required": [
            "hasChanges"
          ]
        },
        "headerFooters": {
          "type": "object",
          "properties": {
            "hasChanges": {
              "type": "boolean"
            }
          },
          "additionalProperties": false,
          "required": [
            "hasChanges"
          ]
        },
        "parts": {
          "type": "object",
          "properties": {
            "hasChanges": {
              "type": "boolean"
            }
          },
          "additionalProperties": false,
          "required": [
            "hasChanges"
          ]
        }
      },
      "additionalProperties": false,
      "required": [
        "hasChanges",
        "changedComponents",
        "body",
        "comments",
        "styles",
        "numbering",
        "headerFooters",
        "parts"
      ]
    },
    "diagnostics": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  },
  "additionalProperties": false,
  "required": [
    "appliedOperations",
    "baseFingerprint",
    "targetFingerprint",
    "coverage",
    "summary",
    "diagnostics"
  ]
}
```

## Pre-apply throws

- `INVALID_INPUT`
- `CAPABILITY_UNSUPPORTED`
- `PRECONDITION_FAILED`
- `CAPABILITY_UNAVAILABLE`

## Non-applied receipt codes

- None

