fastapply

package
v1.40.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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

View Source
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

func Apply(original, modelOutput string, finish Finish) (string, error)

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

func BuildPrompt(original, edit string) string

BuildPrompt assembles the user prompt from the original file and the edit.

func CheckSize

func CheckSize(original, edit string) error

CheckSize rejects inputs that cannot be fast-applied BEFORE a (billed) model call is spent. Returns ErrFileTooLarge / ErrEditTooLarge on rejection.

func FastApply

func FastApply(ctx context.Context, original, edit string, gen Generator) (string, error)

FastApply runs the full flow: pre-flight size check → model regeneration via gen → result validation. Returns the content to write or a typed error.

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

func FinishFromAnthropic(stopReason string) Finish

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL