High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(snapshot): reserve framing only for compact and text replies and hold back a token for the truncator's rounding
Luigi Agosti75a33a621056
diff --git a/internal/handlers/snapshot.go b/internal/handlers/snapshot.goindex b7ef86cb..8a27befc 100644--- a/internal/handlers/snapshot.go+++ b/internal/handlers/snapshot.go@@ -103,6 +103,24 @@ func (h *Handlers) snapshotFramingReserve(format, title, url string, count int,return n}+// snapshotFramingIsReserved reports whether the reply carries the plain-text framing+// the reserve prices. json and yaml carry no header or advisory — only the node+// array is budgeted — and a file export writes its own layout.+func snapshotFramingIsReserved(format, output string) bool {+ return output != "file" && (format == "compact" || format == "text")+}++// snapshotNodeBudget is what is left of maxTokens for nodes once the framing is paid+// for. It holds back one token more than the framing: the truncator rounds its+// charge down, so it can spend up to three bytes past the budget it is handed.+func (h *Handlers) snapshotNodeBudget(format, output, title, url string, count int, scope *frameDisclosure, hint string, ignored []string, maxTokens int) int {+ if !snapshotFramingIsReserved(format, output) {+ return maxTokens+ }+ reserve := estimateSnapshotTokens(h.snapshotFramingReserve(format, title, url, count, scope, hint, ignored, maxTokens))+ return max(maxTokens-reserve-1, 0)+}+func ignoredParamsComment(ignored []string) string {if len(ignored) == 0 {return ""@@ -298,11 +316,7 @@ func (h *Handlers) HandleSnapshot(w http.ResponseWriter, r *http.Request) {// so where the two readings differ this takes the larger one. The node count// in the header is the pre-truncation one for the same reason — it has at// least as many digits as the count that will be printed.- reserve := estimateSnapshotTokens(h.snapshotFramingReserve(format, title, url, len(flat), scopeInfo, scopedEmptyHint, controls.Ignored, maxTokens))- nodeBudget := maxTokens - reserve- if nodeBudget < 0 {- nodeBudget = 0- }+ nodeBudget := h.snapshotNodeBudget(format, output, title, url, len(flat), scopeInfo, scopedEmptyHint, controls.Ignored, maxTokens)flat, truncated = bridge.TruncateToTokens(flat, nodeBudget, format)}diff --git a/internal/handlers/snapshot_budget_framing_test.go b/internal/handlers/snapshot_budget_framing_test.goindex a0c4fcb7..fe6d3930 100644--- a/internal/handlers/snapshot_budget_framing_test.go+++ b/internal/handlers/snapshot_budget_framing_test.go@@ -49,12 +49,7 @@ func TestSnapshotBudgetCoversTheFramingNotJustTheNodes(t *testing.T) {for _, format := range []string{"compact", "text"} {for _, budget := range []int{100, 150, 300, 600} {t.Run(fmt.Sprintf("%s/%d", format, budget), func(t *testing.T) {- reserve := estimateSnapshotTokens(- h.snapshotFramingReserve(format, title, url, len(nodes), nil, "", nil, budget))- nodeBudget := budget - reserve- if nodeBudget < 0 {- nodeBudget = 0- }+ nodeBudget := h.snapshotNodeBudget(format, "", title, url, len(nodes), nil, "", nil, budget)kept, truncated := bridge.TruncateToTokens(nodes, nodeBudget, format)header := snapshotCompactHeader(title, url, len(kept), nil)@@ -130,3 +125,29 @@ func TestFramingReserveChargesTheUntrustedContentWrapper(t *testing.T) {t.Errorf("wrapper charged only %d bytes; the advisory alone is larger than that", overhead)}}++// json and yaml carry no plain-text header or advisory, and a file export writes its+// own layout, so none of them give up node budget to framing they never send. json+// is the default format: charging it the compact header and the advisory left a+// 100-token budget with no nodes at all on a long title and URL.+func TestNodeBudgetIsNotChargedFramingTheReplyDoesNotCarry(t *testing.T) {+ h := &Handlers{+ Config: &config.RuntimeConfig{IDPI: config.IDPIConfig{Enabled: true, WrapContent: true}},+ IDPIGuard: idpi.NewGuard(config.IDPIConfig{Enabled: true, WrapContent: true}, nil),+ }+ const (+ title = "Enterprise Platform Pricing, Plans and Frequently Asked Questions"+ url = "https://example.com/a-fairly-long-marketing-path/with-nested-segments/"+ )++ for _, tc := range []struct{ format, output string }{+ {"json", ""}, {"yaml", ""}, {"compact", "file"}, {"text", "file"}, {"json", "file"},+ } {+ if got := h.snapshotNodeBudget(tc.format, tc.output, title, url, 80, nil, "", nil, 100); got != 100 {+ t.Errorf("format=%s output=%q: node budget %d, want the full 100", tc.format, tc.output, got)+ }+ }+ if got := h.snapshotNodeBudget("compact", "", title, url, 80, nil, "", nil, 100); got >= 100 {+ t.Errorf("compact: node budget %d, want the framing reserved out of 100", got)+ }+}