177b5596b5
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
61 lines
2.7 KiB
Go
61 lines
2.7 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// MaxPlanContentBytes bounds the plan content a single write may store,
|
|
// measured in UTF-8 bytes via len(content). Stored plan content is read back
|
|
// and injected into every session handover for its task, so an unbounded
|
|
// write lets one caller impose a repeated, unbounded cost on every later
|
|
// launch. The unit is bytes rather than runes on purpose: unlike
|
|
// planTruncationDetected, which counts runes because it reasons about how
|
|
// much of a document survived, this bounds memory and scan cost, which is
|
|
// proportional to bytes. Matches maxUserStateBodyBytes in
|
|
// internal/plugins/user_state_handlers.go.
|
|
const MaxPlanContentBytes = 256 << 10 // 256 KiB
|
|
|
|
// ErrPlanContentTooLarge is the sentinel matched via errors.Is. The message
|
|
// carries the submitted size and is only available through
|
|
// PlanContentTooLargeError, so a fixed sentinel alone is not enough for
|
|
// callers that need to report the actual numbers.
|
|
var ErrPlanContentTooLarge = errors.New("plan content exceeds the size limit")
|
|
|
|
// PlanContentTooLargeError reports a plan write refused for exceeding
|
|
// MaxPlanContentBytes, carrying both numbers so the caller (agent or browser)
|
|
// can act on the rejection instead of retrying it unchanged.
|
|
type PlanContentTooLargeError struct {
|
|
Submitted int
|
|
Limit int
|
|
}
|
|
|
|
func (e *PlanContentTooLargeError) Error() string {
|
|
return fmt.Sprintf(
|
|
"plan content is %d bytes, exceeding the %d-byte limit; nothing was stored and the task's existing plan is unchanged. Shorten the document you are holding before writing again — do not resubmit this content unchanged, and do not reconstruct the plan from memory.",
|
|
e.Submitted, e.Limit,
|
|
)
|
|
}
|
|
|
|
// Is lets errors.Is(err, ErrPlanContentTooLarge) match this type without
|
|
// exposing the exact submitted/limit values to a caller that only cares
|
|
// about the class of failure.
|
|
func (e *PlanContentTooLargeError) Is(target error) bool {
|
|
return target == ErrPlanContentTooLarge
|
|
}
|
|
|
|
// checkPlanContentSize is the shared write-seam admission check: a pure
|
|
// function of the content it is measuring, called from two different points
|
|
// depending on mode. In replace mode (validatePlanWrite) it runs once, before
|
|
// plan storage or the per-task write lock, against the submitted content. In
|
|
// append mode (upsertPlan) it runs a second time, inside the per-task write
|
|
// lock, against the composed content (stored content plus fragment) once
|
|
// composition has produced it — not the submitted fragment alone, which
|
|
// could be small enough to admit an oversized document.
|
|
func checkPlanContentSize(content string) error {
|
|
if n := len(content); n > MaxPlanContentBytes {
|
|
return &PlanContentTooLargeError{Submitted: n, Limit: MaxPlanContentBytes}
|
|
}
|
|
return nil
|
|
}
|