Repository navigation
Expand file tree
/
Copy pathcontext_bounds_test.go
More file actions
106 lines (100 loc) · 2.92 KB
/
Copy pathcontext_bounds_test.go
File metadata and controls
106 lines (100 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// SPDX-License-Identifier: AGPL-3.0-only
package agentruntime
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestBuildContextNeverExceedsDeclaredBound(t *testing.T) {
t.Parallel()
root := t.TempDir()
// header "\n--- A ---\n" is 11 bytes, the body is 1 byte, and BuildContext
// appends a newline because the body does not end with one: 13 bytes total.
writeTestFile(t, root, "A", "x")
w, err := OpenWorkspace(root)
if err != nil {
t.Fatal(err)
}
for limit := int64(1); limit <= 20; limit++ {
out, err := w.BuildContext([]string{"A"}, limit)
if err != nil {
continue
}
if int64(len(out)) > limit {
t.Fatalf("limit %d produced %d bytes: %q", limit, len(out), out)
}
}
if _, err := w.BuildContext([]string{"A"}, 12); err == nil {
t.Fatal("a 12-byte limit accepted a 13-byte assembled context")
}
out, err := w.BuildContext([]string{"A"}, 13)
if err != nil || len(out) != 13 {
t.Fatalf("exact-fit limit rejected: len=%d err=%v", len(out), err)
}
}
func TestBuildContextRejectsOversizedInstructionBeforeReadingIt(t *testing.T) {
t.Parallel()
root := t.TempDir()
path := filepath.Join(root, "BIG.md")
file, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
// A sparse file consumes no blocks but reports its full size, so reading it
// unconditionally would allocate half a gigabyte.
if err := file.Truncate(512 << 20); err != nil {
t.Fatal(err)
}
if err := file.Close(); err != nil {
t.Fatal(err)
}
w, err := OpenWorkspace(root)
if err != nil {
t.Fatal(err)
}
_, err = w.BuildContext([]string{"BIG.md"}, 1024)
if err == nil {
t.Fatal("oversized instruction accepted")
}
if !strings.Contains(err.Error(), "does not fit the remaining context budget") {
t.Fatalf("rejection did not come from file metadata, so the read was not bounded: %v", err)
}
}
func TestBuildContextBoundsTheCumulativeBudget(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeTestFile(t, root, "one.md", "one\n")
writeTestFile(t, root, "two.md", "two\n")
w, err := OpenWorkspace(root)
if err != nil {
t.Fatal(err)
}
full, err := w.BuildContext([]string{"one.md", "two.md"}, 1024)
if err != nil {
t.Fatal(err)
}
if _, err := w.BuildContext([]string{"one.md", "two.md"}, int64(len(full))-1); err == nil {
t.Fatal("cumulative budget was not enforced across instructions")
}
if _, err := w.BuildContext([]string{"one.md", "two.md"}, int64(len(full))); err != nil {
t.Fatalf("exact cumulative budget rejected: %v", err)
}
}
func TestResolveAcceptsWorkspaceRootAndRejectsEscape(t *testing.T) {
t.Parallel()
root := t.TempDir()
writeTestFile(t, root, "inside", "x")
w, err := OpenWorkspace(root)
if err != nil {
t.Fatal(err)
}
if _, err := w.ResolveDirectory("."); err != nil {
t.Fatalf("workspace root rejected as workdir: %v", err)
}
for _, escape := range []string{"..", "../..", "../sibling"} {
if _, err := w.Resolve(escape); err == nil {
t.Fatalf("escape %q accepted", escape)
}
}
}