| 1 | package extension |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | "sort" |
| 8 | |
| 9 | "reasonix/internal/provider" |
| 10 | ) |
| 11 | |
| 12 | // RuntimeSnapshot is the frozen, effective runtime configuration produced by |
| 13 | // one Builder run. It is immutable after Freeze: fields are private and every |
| 14 | // accessor returns defensive copies, so snapshots can be shared across turns, |
| 15 | // subagents, and frontends without locking. Two snapshots with equal |
| 16 | // CacheHash are interchangeable for provider caching purposes even if their |
| 17 | // provenance differs. |
| 18 | type RuntimeSnapshot struct { |
| 19 | generation uint64 |
| 20 | catalog *Catalog |
| 21 | systemPrompt string |
| 22 | toolSchemas []provider.ToolSchema |
| 23 | interceptorChain map[InterceptorPoint][]Contribution |
| 24 | replacements map[Slot]ContributionSource |
| 25 | diagnostics []string |
| 26 | cacheHash string |
| 27 | systemHash string |
| 28 | toolsHash string |
| 29 | } |
| 30 | |
| 31 | // Generation returns the build generation. Generations let stale cleanup |
| 32 | // (RuntimeSet.CloseIfGeneration) recognize that a snapshot has been |
| 33 | // superseded. |
| 34 | func (s *RuntimeSnapshot) Generation() uint64 { return s.generation } |
| 35 | |
| 36 | // Catalog returns the frozen catalog of effective (post-resolution) |
| 37 | // contributions. The catalog itself is immutable; its accessors return |
| 38 | // copies. |
| 39 | func (s *RuntimeSnapshot) Catalog() *Catalog { return s.catalog } |
| 40 | |
| 41 | // SystemPrompt returns the assembled system prompt text. |
| 42 | func (s *RuntimeSnapshot) SystemPrompt() string { return s.systemPrompt } |
| 43 | |
| 44 | // ToolSchemas returns the canonical provider-visible tool schemas, sorted by |
| 45 | // name. The slice is a copy — mutating it cannot affect the snapshot. |
| 46 | func (s *RuntimeSnapshot) ToolSchemas() []provider.ToolSchema { |
| 47 | out := make([]provider.ToolSchema, len(s.toolSchemas)) |
| 48 | copy(out, s.toolSchemas) |
| 49 | return out |
| 50 | } |
| 51 | |
| 52 | // InterceptorChain returns the ordered interceptor chain per point (a deep |
| 53 | // copy). Points with no interceptors are absent, not empty. |
| 54 | func (s *RuntimeSnapshot) InterceptorChain() map[InterceptorPoint][]Contribution { |
| 55 | out := make(map[InterceptorPoint][]Contribution, len(s.interceptorChain)) |
| 56 | for point, chain := range s.interceptorChain { |
| 57 | cp := make([]Contribution, len(chain)) |
| 58 | copy(cp, chain) |
| 59 | out[point] = cp |
| 60 | } |
| 61 | return out |
| 62 | } |
| 63 | |
| 64 | // Replacements returns the winning owner per replacement slot (a copy). |
| 65 | func (s *RuntimeSnapshot) Replacements() map[Slot]ContributionSource { |
| 66 | out := make(map[Slot]ContributionSource, len(s.replacements)) |
| 67 | for slot, owner := range s.replacements { |
| 68 | out[slot] = owner |
| 69 | } |
| 70 | return out |
| 71 | } |
| 72 | |
| 73 | // Diagnostics returns human-readable notes about the assembly — currently the |
| 74 | // shadowing disputes a ConflictCollect build resolved with its ordinary |
| 75 | // winner rules instead of failing. Each entry names the kind, the canonical |
| 76 | // ID, and every source that claimed it. The slice is a copy; empty means the |
| 77 | // build resolved cleanly. |
| 78 | func (s *RuntimeSnapshot) Diagnostics() []string { |
| 79 | out := make([]string, len(s.diagnostics)) |
| 80 | copy(out, s.diagnostics) |
| 81 | return out |
| 82 | } |
| 83 | |
| 84 | // CacheHash returns the fingerprint of the provider-visible prefix state. |
| 85 | func (s *RuntimeSnapshot) CacheHash() string { return s.cacheHash } |
| 86 | |
| 87 | // CacheShape returns the two halves of CacheHash — the system-prompt hash and |
| 88 | // the tool-schemas hash — so cache-miss diagnostics can say which half moved |
| 89 | // without re-hashing. |
| 90 | func (s *RuntimeSnapshot) CacheShape() (systemHash, toolsHash string) { |
| 91 | return s.systemHash, s.toolsHash |
| 92 | } |
| 93 | |
| 94 | // normalizeToolSchemas sorts schemas by (name, description, parameters) so a |
| 95 | // reordered input cannot change the hash. This mirrors the canonicalization |
| 96 | // in internal/agent/cache_shape.go; it is reimplemented here rather than |
| 97 | // imported because the kernel must stay below the agent package in the |
| 98 | // dependency graph. |
| 99 | func normalizeToolSchemas(schemas []provider.ToolSchema) []provider.ToolSchema { |
| 100 | out := make([]provider.ToolSchema, len(schemas)) |
| 101 | copy(out, schemas) |
| 102 | sort.Slice(out, func(i, j int) bool { |
| 103 | if out[i].Name != out[j].Name { |
| 104 | return out[i].Name < out[j].Name |
| 105 | } |
| 106 | if out[i].Description != out[j].Description { |
| 107 | return out[i].Description < out[j].Description |
| 108 | } |
| 109 | return string(out[i].Parameters) < string(out[j].Parameters) |
| 110 | }) |
| 111 | return out |
| 112 | } |
| 113 | |
| 114 | // cacheHashInput is the canonical hashed form: JSON object keys in |
| 115 | // declaration order, schemas pre-sorted, parameters pre-canonicalized at |
| 116 | // assemble time (provider.CanonicalizeSchema sorts schema keys, so the raw |
| 117 | // bytes are stable across processes). |
| 118 | type cacheHashInput struct { |
| 119 | SystemPrompt string `json:"systemPrompt"` |
| 120 | ToolSchemas []provider.ToolSchema `json:"toolSchemas"` |
| 121 | } |
| 122 | |
| 123 | func sha256Hex(b []byte) string { |
| 124 | sum := sha256.Sum256(b) |
| 125 | return hex.EncodeToString(sum[:]) |
| 126 | } |
| 127 | |
| 128 | // computeCacheShape hashes the system prompt and the canonical tool schemas. |
| 129 | // Keeping the two halves separate costs nothing and lets CacheShape explain |
| 130 | // prefix-cache misses the way internal/agent.CompareShape does. |
| 131 | func computeCacheShape(systemPrompt string, schemas []provider.ToolSchema) (systemHash, toolsHash, cacheHash string) { |
| 132 | canonical := normalizeToolSchemas(schemas) |
| 133 | toolsJSON, _ := json.Marshal(canonical) |
| 134 | systemHash = sha256Hex([]byte(systemPrompt)) |
| 135 | toolsHash = sha256Hex(toolsJSON) |
| 136 | combined, _ := json.Marshal(cacheHashInput{SystemPrompt: systemPrompt, ToolSchemas: canonical}) |
| 137 | cacheHash = sha256Hex(combined) |
| 138 | return systemHash, toolsHash, cacheHash |
| 139 | } |
| 140 |