返回 DeepSeek-Reasonix
contract.go
根目录 / internal / tool / contract.go
1 package tool
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "sort"
8 "strings"
9
10 "reasonix/internal/provider"
11 )
12
13 // ContractEntry is the provider-visible contract for a tool schema snapshot.
14 type ContractEntry struct {
15 Name string
16 Description string
17 ReadOnly bool
18 Schema json.RawMessage
19 }
20
21 // BuiltinContractEntries returns a stable snapshot of compile-time built-ins.
22 func BuiltinContractEntries() []ContractEntry {
23 return contractEntriesFromTools(Builtins(), nil)
24 }
25
26 func contractEntriesFromTools(tools []Tool, canonical map[string]json.RawMessage) []ContractEntry {
27 entries := make([]ContractEntry, 0, len(tools))
28 for _, t := range tools {
29 schema := provider.CanonicalizeSchema(t.Schema())
30 if canonical != nil {
31 if c := canonical[t.Name()]; len(c) > 0 {
32 schema = append(json.RawMessage(nil), c...)
33 }
34 }
35 entries = append(entries, ContractEntry{
36 Name: t.Name(),
37 Description: strings.TrimSpace(t.Description()),
38 ReadOnly: t.ReadOnly(),
39 Schema: schema,
40 })
41 }
42 sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name })
43 return entries
44 }
45
46 // ContractEntries returns the registry's provider-visible contract snapshot.
47 // The tool list is captured under the lock, but the per-tool method calls
48 // (Schema/Description/ReadOnly) run AFTER it is released: a lazy MCP
49 // placeholder's ReadOnly takes the spawn mutex, and the spawn's trySwap takes
50 // this registry's write lock — holding the read lock across ReadOnly is an
51 // AB-BA deadlock (boot's snapshot assembly hit it with a live swap in flight).
52 func (r *Registry) ContractEntries() []ContractEntry {
53 r.mu.RLock()
54 tools := make([]Tool, 0, len(r.order))
55 canonical := make(map[string]json.RawMessage, len(r.order))
56 for _, name := range r.order {
57 t := r.tools[name]
58 if t == nil {
59 continue
60 }
61 tools = append(tools, t)
62 canonical[name] = r.canon[name]
63 }
64 r.mu.RUnlock()
65 return contractEntriesFromTools(tools, canonical)
66 }
67
68 // RenderContractMarkdown renders entries as committed documentation. Tests use
69 // the same entries, so docs drift when tool names, descriptions, read-only
70 // flags, or canonical schemas change.
71 func RenderContractMarkdown(entries []ContractEntry) string {
72 var b strings.Builder
73 b.WriteString("# Tool Contract\n\n")
74 b.WriteString("This document records the provider-visible contract for Reasonix compile-time built-in tools. It is generated from the same canonical schema path used by the runtime registry.\n\n")
75 b.WriteString("| Tool | Read-only | Description |\n")
76 b.WriteString("| --- | --- | --- |\n")
77 for _, e := range entries {
78 fmt.Fprintf(&b, "| `%s` | %t | %s |\n", e.Name, e.ReadOnly, markdownCell(e.Description))
79 }
80 b.WriteString("\n## Schemas\n")
81 for _, e := range entries {
82 fmt.Fprintf(&b, "\n### `%s`\n\n", e.Name)
83 fmt.Fprintf(&b, "- Read-only: `%t`\n", e.ReadOnly)
84 if e.Description != "" {
85 fmt.Fprintf(&b, "- Description: %s\n", e.Description)
86 }
87 b.WriteString("\n```json\n")
88 b.WriteString(prettyJSON(e.Schema))
89 b.WriteString("\n```\n")
90 }
91 return b.String()
92 }
93
94 func markdownCell(s string) string {
95 s = strings.ReplaceAll(s, "\n", " ")
96 s = strings.ReplaceAll(s, "|", `\|`)
97 return strings.Join(strings.Fields(s), " ")
98 }
99
100 func prettyJSON(raw json.RawMessage) string {
101 var out bytes.Buffer
102 if err := json.Indent(&out, raw, "", " "); err != nil {
103 return strings.TrimSpace(string(raw))
104 }
105 return out.String()
106 }
107
107 lines GO