Skip to content
2 min read · 374 words

@nhtio/adk/lib/patch ​

The structured apply_patch envelope: parser and applier for the *** Begin Patch multi-file dialect.

Remarks ​

Shared structured-patch primitives for workspace-shaped batteries. This dialect derives from the GitHub Copilot apply_patch format — one of the most robust and battle-tested patch formats in production use, which is also why models already know how to write it. The grammar (*** Begin Patch … *** End Patch, *** Add File: / *** Delete File: / *** Update File: (+ *** Move to:), @@ context hunks with +/-/space lines) is preserved exactly — inventing a "better" variation would forfeit the pretraining. Ported from the source server's doc.apply_patch adapter (its green e2e suite informs the spec coverage); the context matcher rejects ambiguity (a hunk whose context matches more than one location fails rather than guessing).

Interfaces ​

InterfaceDescription
AddOperation*** Add File: — create a new file from + lines.
DeleteOperation*** Delete File: — remove a file.
ParsedApplyPatchThe parsed envelope: ordered operations plus totals.
ParsedHunkOne parsed @@ hunk: the context+removal lines and their replacement.
UpdateOperation*** Update File: (+ optional *** Move to:) — apply hunks, optionally rename.
WorkspaceFileOne file in the virtual workspace a structured patch operates over.

Type Aliases ​

Type AliasDescription
PatchOperationAny one operation of a structured patch.

Functions ​

FunctionDescription
applyOperationsApply a parsed structured patch to a virtual workspace of files keyed by normalized path.
applyUpdateHunksApply an update operation's hunks to inputText. Context matching is exact and rejects ambiguity: a hunk whose old-lines match more than one location past the cursor fails rather than guessing.
inferTextMimeFromPathInfer a text MIME from a workspace path's extension (Add File outputs).
isStructuredPatchtrue when patch is the structured envelope rather than a unified diff.
normalizeWorkspacePathNormalize a workspace path: relative, no ./../empty segments, forward slashes.
parseStructuredPatchParse a structured *** Begin Patch envelope.