GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — harness/tools.go at main · majorcontext/harness

Expand file tree

/

tools.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
236 lines (220 loc) · 7.99 KB
/

tools.go

Copy path

File metadata and controls

236 lines (220 loc) · 7.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package harness
import (
"cmp"
"context"
"fmt"
"maps"
"slices"
"github.com/majorcontext/harness/config"
"github.com/majorcontext/harness/plugin"
"github.com/majorcontext/harness/protocol"
iplugin "github.com/majorcontext/harness/internal/plugin"
"github.com/majorcontext/harness/internal/selftool"
"github.com/majorcontext/harness/internal/tool/builtin"
"github.com/majorcontext/harness/internal/tool/mcpsrc"
"github.com/majorcontext/harness/internal/tool/pluginsrc"
"github.com/majorcontext/harness/internal/tool/proc"
"github.com/majorcontext/harness/internal/toolresult"
"github.com/majorcontext/harness/internal/turn"
)
// sessionTool is a runtime tool that belongs to one session. Bind returns
// the tool for session id at depth, or nil when that session does not get it.
type sessionTool interface {
turn.Tool
Bind(id string, child bool, depth int) turn.Tool
}
// known reports whether a session at model can have a tool named name: a
// tool of Options.Tools, a started plugin tool, an MCP tool, a built-in tool
// of the WorkDir that a harness loop runs, the history tool, or a built-in
// tool of the backend of model. An empty model names no backend.
func (r *Runtime) known(model, name string) bool { return r.knownIn(model, name, false) }
// knownIn is known for a session that has session MCP servers when session is true.
func (r *Runtime) knownIn(model, name string, session bool) bool {
caps := r.models.Capabilities(model)
return slices.Contains(caps.Tools, name) || name == turn.HistoryTool || r.builtin(name) || name == turn.AskTool || r.skills != nil && name == selftool.LoadSkillName || caps.WebSearch && name == turn.WebSearchTool ||
(r.mcp.Active() || session) && mcpsrc.Reserved(name) || name == "mcp" && (r.mcp.Populated() || r.plugins.Configured()) || r.code != nil && name == r.code.Spec().Name || r.owns(name)
}
// owns reports whether name is a tool of Options.Tools or of a started plugin.
func (r *Runtime) owns(name string) bool {
return slices.ContainsFunc(r.tools, func(t turn.Tool) bool { return t.Spec().Name == name }) ||
r.plugins != nil && r.plugins.Has(name)
}
// builtin reports whether name is session_info or a built-in tool of the WorkDir.
func (r *Runtime) builtin(name string) bool {
return name == selftool.SessionInfoName || r.workDir != "" && (slices.Contains(builtin.Names, name) || name == toolresult.ToolName ||
r.web != nil && name == turn.WebFetchTool)
}
// shadowable reports whether name is a file, search, or shell tool of the
// WorkDir, which a tool of Options.Tools replaces.
func (r *Runtime) shadowable(name string) bool {
return r.workDir != "" && slices.Contains(builtin.Names, name)
}
// shadowed returns the shadowable built-in names that Options.Tools takes.
func (r *Runtime) shadowed() []string {
var out []string
for _, t := range r.tools {
if n := t.Spec().Name; r.shadowable(n) {
out = append(out, n)
}
}
return out
}
// source returns the tools of session id for each model call: the runtime
// tools, then the goal, file, MCP, and plugin tools of the models that take
// them. A session tool binds to the session or drops out. Each session has
// its own file tools, because the write_file guard belongs to one session,
// and its own session_info tool, which reads what sp recorded of the session.
func (r *Runtime) source(id, root string, child bool, depth int, plug *pluginsrc.Session, sp *selftool.Prompt, sm *mcpsrc.Source) turn.Source {
var static, loopOnly []turn.Tool
var model *selftool.Model
for _, t := range r.tools {
if b, ok := t.(sessionTool); ok {
if t = b.Bind(id, child, depth); t == nil {
continue
}
}
if m, ok := t.(selftool.Model); ok {
model = &m
continue
}
if _, ok := t.(selftool.Goal); ok {
loopOnly = append(loopOnly, t)
continue
}
static = append(static, t)
}
srcs := turn.Sources{boundTools{tools: static, session: id, root: root}}
if r.skills != nil {
srcs = append(srcs, r.skillSource(id, sp))
}
if len(loopOnly) > 0 {
goal := turn.Fixed(loopOnly)
srcs = append(srcs, perModel{r.models, func(c turn.Capabilities) turn.Source {
if c.OwnsLoop {
return nil
}
return goal
}})
}
if model != nil {
full, deferred := turn.Fixed{*model}, turn.Fixed{model.Deferred()}
srcs = append(srcs, perModel{r.models, func(c turn.Capabilities) turn.Source {
if c.OwnsLoop {
return deferred
}
return full
}})
}
if r.workDir != "" {
var env builtin.ShellEnv
if plug != nil {
env = plug.ShellEnv
}
taken := r.shadowed()
files := turn.Fixed(slices.DeleteFunc(builtin.Tools(r.workDir, env), func(t turn.Tool) bool { return slices.Contains(taken, t.Spec().Name) }))
if r.web != nil {
files = append(files, newWebTool(*r.web))
}
srcs = append(srcs, files)
srcs = append(srcs, perModel{r.models, func(c turn.Capabilities) turn.Source {
if c.OwnsLoop {
return nil
}
return proc.Notices{M: r.procs, WorkDir: r.workDir}
}})
}
srcs = append(srcs, r.sessionTools(id, sp))
if mcp := cmp.Or(sm, r.mcp); mcp != nil {
srcs = append(srcs, perModel{r.models, func(c turn.Capabilities) turn.Source {
if c.OwnsLoop && !mcp.Populated() && (plug == nil || !plug.Present()) {
return nil
}
if plug == nil || c.OwnsLoop {
return mcp
}
return mcp.With(plug)
}})
}
if plug != nil {
srcs = append(srcs, perModel{r.models, func(c turn.Capabilities) turn.Source {
if c.OwnsLoop {
return plug.Untransformed()
}
return plug
}})
}
return srcs
}
// sessionTools are session_info, and the code tool when code_mode is true.
func (r *Runtime) sessionTools(id string, sp *selftool.Prompt) turn.Fixed {
tools := turn.Fixed{selftool.NewSessionInfo(toolHost{r}, id, sp)}
if r.code != nil {
tools = append(tools, r.code)
}
return tools
}
// perModel gives each model call the Source that pick chooses for the
// capabilities of its model. pick returns nil for none.
type perModel struct {
backend turn.Backend
pick func(caps turn.Capabilities) turn.Source
}
func (p perModel) Toolset(ctx context.Context, history []protocol.LogMessage, allowed []string, model string) turn.Toolset {
src := p.pick(p.backend.Capabilities(model))
if src == nil {
return turn.Toolset{}
}
return src.Toolset(ctx, history, allowed, model)
}
// checkPlugins fails on a nil plugin, a plugin with no name, a
// ToolExecuteBefore with no Run, a tool with no Execute, a name that repeats
// one of cfg or of an earlier plugin, and a plugin tool that takes the name of
// a tool the runtime has or of an earlier plugin.
func (r *Runtime) checkPlugins(cfg []config.PluginSpec, plugins []*plugin.Plugin) error {
names, tools := map[string]bool{}, map[string]bool{}
for _, c := range cfg {
names[c.Name] = true
}
for i, p := range plugins {
switch {
case p == nil:
return fmt.Errorf("harness: plugin %d is nil", i)
case p.Name == "":
return fmt.Errorf("harness: plugin %d has no name", i)
case names[p.Name]:
return fmt.Errorf("harness: plugin name %q is repeated", p.Name)
}
names[p.Name] = true
if b := p.ToolExecuteBefore; b != nil && b.Run == nil {
return fmt.Errorf("harness: plugin %s: ToolExecuteBefore has no Run", p.Name)
}
if err := checkHookTimeouts(p); err != nil {
return err
}
for _, t := range p.Tools {
if t.Execute == nil {
return fmt.Errorf("harness: plugin %s: tool %q has no Execute", p.Name, t.Def.Name)
}
}
for _, d := range p.Manifest().Tools {
if d.Name == "" || tools[d.Name] || r.known("", d.Name) {
return fmt.Errorf("harness: plugin %s: tool name %q is empty or taken", p.Name, d.Name)
}
tools[d.Name] = true
}
}
return nil
}
// checkHookTimeouts validates the HookTimeouts of p and names the Duration that
// the author wrote when one is invalid.
func checkHookTimeouts(p *plugin.Plugin) error {
m := p.Manifest()
for _, h := range slices.Sorted(maps.Keys(m.HookTimeouts)) {
one := m
one.HookTimeouts = map[plugin.Hook]int{h: m.HookTimeouts[h]}
if err := iplugin.ValidateHookTimeouts(one); err != nil {
return fmt.Errorf("harness: %w (HookTimeouts[%s] is %v)", err, h, p.HookTimeouts[h])
}
}
return nil
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.