Documentation
¶
Overview ¶
Package fastapply implements the safety guards around "Fast Apply" — applying a targeted edit to a file by having a small/cheap model regenerate the ENTIRE file in one shot (à la Cursor's apply model). Regenerating the whole file is fast and forgiving of fuzzy edit instructions, but it has one dangerous failure mode: the model can silently return a truncated or partially-dropped file, and writing that result corrupts the file while reporting success.
This package contains the defensive harness that makes that failure mode impossible to commit silently. Every guard converts a suspect result into a typed error instead of a corrupted write. It is intentionally backend- agnostic: the actual model call is injected as a Generator, so Apply/CheckSize are pure and fully unit-testable without a live model.
Ported from the e2b coding agent's lib/fastApply.ts (QUA-765).
Index ¶
Constants ¶
const ( // MaxOutputTokens is the output-token budget Fast Apply assumes for the // model call. The whole file is regenerated within this budget. MaxOutputTokens = 12_000 // MaxFileChars caps the original file size. Fast Apply regenerates the ENTIRE // file in a single response, so the file must fit the OUTPUT budget — not just // the input context window. Anything larger is guaranteed to truncate, so it is // rejected before a model call is spent. ~20% headroom is left for lines the // edit adds: 12_000 * 4 * 0.8 = 38_400. MaxFileChars = MaxOutputTokens * charsPerToken * 8 / 10 // MaxInputChars is a secondary guard on total prompt size (file + edit) to // avoid input-context errors. MaxInputChars = 320_000 )
Tunables. These mirror the e2b implementation and assume the underlying model call is made with an output budget of ~MaxOutputTokens.
const SystemPrompt = `` /* 447-byte string literal not displayed */
SystemPrompt is the instruction given to the regeneration model. Exposed so a caller's Generator can reuse the exact wording the harness was tuned against.
Variables ¶
var ( ErrFileTooLarge = errors.New("file too large for fast apply") ErrEditTooLarge = errors.New("edit too large for fast apply") ErrTruncated = errors.New("fast apply output was truncated at the token limit") ErrAbnormalFinish = errors.New("fast apply did not complete cleanly") ErrEmptyOutput = errors.New("fast apply model returned an empty response") ErrDrasticShrink = errors.New("fast apply produced a suspiciously short result") )
Sentinel errors, one per refusal case. Callers can branch with errors.Is and the wrapped messages carry the specifics. A non-nil error from this package ALWAYS means "did not write" — never a silently corrupted file.
Functions ¶
func Apply ¶
Apply validates and finalizes a fast-apply result. Given the original file, the model's raw regenerated output, and how generation finished, it returns the content to write or a typed error. It NEVER returns corrupted content with a nil error — every suspect result becomes an error.
Use this directly when you already have the model output (e.g. in tests, or when the model call lives elsewhere); use FastApply to run the whole flow.
func BuildPrompt ¶
BuildPrompt assembles the user prompt from the original file and the edit.
Types ¶
type Finish ¶
type Finish int
Finish describes how the model's generation terminated. Backends map their own stop reasons onto this (see FinishFromAnthropic).
const ( // FinishOK — the model decided it was done (a clean stop). Only this value // allows the regenerated file to be written. FinishOK Finish = iota // FinishTruncated — generation hit the output-token cap; the file is cut off. FinishTruncated // FinishOther — any other abnormal termination (content filter, error, …); // the output may be partial. FinishOther )
func FinishFromAnthropic ¶
FinishFromAnthropic maps an Anthropic stop_reason onto a Finish.
- end_turn / stop_sequence → FinishOK (model stopped on its own)
- max_tokens → FinishTruncated
- anything else → FinishOther
tool_use never occurs here because Fast Apply passes no tools.
type Generator ¶
type Generator func(ctx context.Context, system, prompt string) (text string, finish Finish, err error)
Generator runs the underlying single-shot regeneration. It is expected to call the model with SystemPrompt + BuildPrompt(original, edit) and an output budget of ~MaxOutputTokens, returning the raw text and how generation finished. Injecting it keeps the guards backend-agnostic and unit-testable.