| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/sessiontemp" |
| 10 | ) |
| 11 | |
| 12 | func TestWithSubagentSessionTempIsIndependent(t *testing.T) { |
| 13 | parent := sessiontemp.NewWithRoot(t.TempDir()) |
| 14 | parent.Retain() |
| 15 | defer parent.Release() |
| 16 | parentLease, err := parent.Acquire() |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | parentDir := parentLease.Dir() |
| 21 | if err := os.WriteFile(filepath.Join(parentDir, "parent.txt"), []byte("p"), 0o600); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | parentLease.Release() |
| 25 | |
| 26 | ctx := sessiontemp.WithManager(context.Background(), parent) |
| 27 | childCtx, releaseChild := withSubagentSessionTemp(ctx) |
| 28 | defer releaseChild() |
| 29 | |
| 30 | child := sessiontemp.FromContext(childCtx) |
| 31 | if child == nil || child == parent { |
| 32 | t.Fatal("sub-agent must install a distinct SessionTemp Manager") |
| 33 | } |
| 34 | if sessiontemp.FromContext(childCtx) != child { |
| 35 | t.Fatal("FromContext should return child manager") |
| 36 | } |
| 37 | |
| 38 | childLease, err := child.Acquire() |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | childDir := childLease.Dir() |
| 43 | if childDir == parentDir { |
| 44 | t.Fatal("child temporary directory must not equal parent") |
| 45 | } |
| 46 | if _, err := os.Stat(filepath.Join(childDir, "parent.txt")); !os.IsNotExist(err) { |
| 47 | t.Fatalf("child must not see parent temp files: %v", err) |
| 48 | } |
| 49 | if _, err := os.Stat(filepath.Join(parentDir, "parent.txt")); err != nil { |
| 50 | t.Fatalf("parent temp lost: %v", err) |
| 51 | } |
| 52 | childLease.Release() |
| 53 | |
| 54 | // Nested sibling also gets a fresh manager. |
| 55 | sibCtx, releaseSib := withSubagentSessionTemp(ctx) |
| 56 | defer releaseSib() |
| 57 | sib := sessiontemp.FromContext(sibCtx) |
| 58 | if sib == nil || sib == child || sib == parent { |
| 59 | t.Fatal("sibling sub-agent must get its own Manager") |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestContinueFromGetsFreshTempDirectory(t *testing.T) { |
| 64 | // continue_from restores conversation only; each RunSubAgentWithSession |
| 65 | // call installs a new Manager via withSubagentSessionTemp. |
| 66 | ctx1, release1 := withSubagentSessionTemp(context.Background()) |
| 67 | m1 := sessiontemp.FromContext(ctx1) |
| 68 | lease1, err := m1.Acquire() |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | dir1 := lease1.Dir() |
| 73 | lease1.Release() |
| 74 | release1() // seals m1 |
| 75 | |
| 76 | ctx2, release2 := withSubagentSessionTemp(context.Background()) |
| 77 | defer release2() |
| 78 | m2 := sessiontemp.FromContext(ctx2) |
| 79 | if m2 == m1 { |
| 80 | t.Fatal("second run must not reuse first Manager") |
| 81 | } |
| 82 | lease2, err := m2.Acquire() |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | defer lease2.Release() |
| 87 | if lease2.Dir() == dir1 { |
| 88 | t.Fatal("second run must get a new temporary directory") |
| 89 | } |
| 90 | if !m1.Sealed() { |
| 91 | t.Fatal("first run's manager should be sealed after release") |
| 92 | } |
| 93 | } |
| 94 |