-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtools.go
More file actions
583 lines (559 loc) · 19.6 KB
/
Copy pathtools.go
File metadata and controls
583 lines (559 loc) · 19.6 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package adaptor
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"github.com/agent-dance/agent-adaptor/driver"
"github.com/agent-dance/agent-adaptor/internal/engine"
"github.com/agent-dance/agent-adaptor/internal/mcpruntime"
"github.com/agent-dance/agent-adaptor/internal/toolidentity"
"github.com/agent-dance/agent-adaptor/internal/toolruntime"
"github.com/agent-dance/agent-adaptor/tool"
)
// configureTools validates the final construction-scope Tool declaration and
// prepares its immutable internal runtime projection. New cannot return an
// error, so declaration errors are retained and surfaced by openStream before
// Driver validation, resource acquisition, or provider launch.
func (a *Agent) configureTools() {
if a == nil || a.defaults.tools == nil || len(*a.defaults.tools) == 0 {
return
}
definitions := append([]tool.Definition(nil), (*a.defaults.tools)...)
seen := make(map[string]struct{}, len(definitions))
var missingRevision string
for index, definition := range definitions {
if definition == nil {
a.toolConfigErr = fmt.Errorf("%w: nil definition at index %d", tool.ErrInvalidDefinition, index)
return
}
descriptor, err := definition.Descriptor()
if err != nil {
a.toolConfigErr = err
return
}
if _, duplicate := seen[descriptor.Name]; duplicate {
a.toolConfigErr = fmt.Errorf("%w: duplicate tool name %q", tool.ErrInvalidDefinition, descriptor.Name)
return
}
seen[descriptor.Name] = struct{}{}
if missingRevision == "" && strings.TrimSpace(descriptor.Revision) == "" {
missingRevision = descriptor.Name
}
}
runtime, err := toolruntime.New(definitions)
if err != nil {
a.toolConfigErr = fmt.Errorf("prepare host-defined Tools: %w", err)
return
}
a.toolRuntime = runtime
a.toolProvider = &hostedToolProvider{
runtime: runtime,
fingerprint: runtime.Fingerprint(),
}
if missingRevision != "" {
a.toolThreadErr = &engine.SessionIncompatibleError{
Reason: fmt.Sprintf("host-defined tool %q has no semantic revision", missingRevision),
}
}
}
// hostedToolProvider is deliberately private. Public callers install Tools
// only through construction-scope WithTools and cannot smuggle this stable
// Agent capability into one call through WithRunServices.
type hostedToolProvider struct {
runtime *toolruntime.Runtime
fingerprint string
}
func (p *hostedToolProvider) AttachRun(ctx context.Context, _ string) (RunAttachment, error) {
if p == nil || p.runtime == nil {
return RunAttachment{}, toolruntime.ErrClosed
}
endpoint, err := p.runtime.Start(ctx)
if err != nil {
return RunAttachment{}, err
}
token, ok := p.runtime.BearerToken()
if !ok {
return RunAttachment{}, toolruntime.ErrClosed
}
mcpServer := driver.MCPServerSpec{
Key: toolruntime.ServerKey,
Transport: driver.MCPTransportHTTP,
URL: endpoint.URL,
BearerTokenEnvVar: endpoint.BearerTokenEnvVar,
Required: true,
RequiredReason: toolidentity.RequiredReason,
}
return RunAttachment{Services: []ServiceRef{{
ID: toolruntime.ServerKey,
Name: toolruntime.ServerKey,
URL: endpoint.URL,
Status: driver.RuntimeServiceRunning,
Lifecycle: driver.RuntimeLifecycleShared,
ReuseKey: p.fingerprint,
// A live listener is observable, but no MCP initialize/list probe has
// happened yet. Do not report an unobserved health check as success.
Health: driver.RuntimeHealthUnknown,
MCP: &mcpServer,
SecretEnv: []driver.EnvBinding{{
Name: endpoint.BearerTokenEnvVar,
Value: token,
}},
}}}, nil
}
func (p *hostedToolProvider) bearerTokenEnvVar() string {
if p == nil || p.runtime == nil {
return ""
}
return p.runtime.BearerTokenEnvVar()
}
func (a *Agent) validateHostedToolsPreflight(eff *RunSettings, caps driver.MCPCapability) error {
if a == nil || a.toolProvider == nil || eff == nil {
return nil
}
provider, ok := a.toolProvider.(*hostedToolProvider)
if !ok || provider == nil || provider.bearerTokenEnvVar() == "" {
return fmt.Errorf("host-defined Tool runtime has no credential carrier")
}
server := driver.MCPServerSpec{
Key: toolruntime.ServerKey,
Transport: driver.MCPTransportHTTP,
URL: "http://127.0.0.1:1/mcp",
BearerTokenEnvVar: provider.bearerTokenEnvVar(),
Required: true,
RequiredReason: toolidentity.RequiredReason,
}
payload, err := engine.ResolveMCPPayloadWithRuntime(
eff.engineMCPConfig(),
nil,
[]driver.RuntimeServiceRef{{ID: toolruntime.ServerKey, Name: toolruntime.ServerKey, MCP: &server}},
caps,
)
if err != nil {
return err
}
return a.validateHostedToolMCPAuthIsolation(payload)
}
// validateHostedToolMCPAuthIsolation prevents any other MCP endpoint from
// naming the private environment variable that carries this Agent's hosted
// Tool bearer token. It runs both in preflight and after runtime providers have
// attached, covering explicit WithMCP and typed runtime-service declarations.
func (a *Agent) validateHostedToolMCPAuthIsolation(payload driver.MCPPayload) error {
if a == nil || a.toolProvider == nil {
return nil
}
provider, ok := a.toolProvider.(*hostedToolProvider)
if !ok || provider == nil {
return nil
}
ownedEnv := provider.bearerTokenEnvVar()
for _, server := range payload.Servers {
if server.BearerTokenEnvVar == ownedEnv && server.Key != toolruntime.ServerKey {
return fmt.Errorf("%w: MCP server %q aliases the Agent-owned hosted Tool bearer environment variable", engine.ErrInvalidMCPConfig, server.Key)
}
}
return nil
}
type hostedToolProfileClaim struct {
driverType string
dir string
}
type hostedToolProfileSelection struct {
execution *driver.ProfileSelection
compatibility hostedToolProfileCompatibilityView
ownedDir string
}
type hostedToolProfileCompatibilityView struct {
Version string
SourceDir string
MaterializedFingerprint string
Requested *driver.ProfileSelection
}
// prepareHostedToolProfile derives a private execution clone from the actual
// configured source profile. Each Agent/identity receives a unique directory,
// eliminating provider-profile write races both within and across processes.
func (a *Agent) prepareHostedToolProfile(ctx context.Context, eff *RunSettings) error {
if eff == nil {
return nil
}
if a == nil {
eff.effectiveProfile = nil
return nil
}
if a.toolProvider == nil {
eff.effectiveProfile = engine.CloneProfileSelection(a.defaults.profile)
return nil
}
driverType := a.driver.Descriptor().Type
if !mcpruntime.SupportsHostedToolProfile(driverType) {
eff.effectiveProfile = engine.CloneProfileSelection(a.defaults.profile)
return nil
}
reporter, ok := a.driver.(driver.ProfileReporter)
if !ok {
return fmt.Errorf("driver %q persists MCP in a native profile but does not implement driver.ProfileReporter", driverType)
}
var identity driver.AgentIdentity
if eff.identity != nil {
identity = eff.identity.driverIdentity()
}
// ProfileReporter may materialize an explicitly selected clone. Serialize
// the complete source-resolution and private-clone allocation path so two
// first runs cannot race its check/copy/link sequence.
a.toolProfileMu.Lock()
defer a.toolProfileMu.Unlock()
source, err := reporter.GetProfile(ctx, nil, identity, engine.CloneProfileSelection(a.defaults.profile))
if err != nil {
return fmt.Errorf("resolve source profile for host-defined Tools: %w", err)
}
if source.Error != "" {
return fmt.Errorf("resolve source profile for host-defined Tools: %s", source.Error)
}
if !source.Supported || strings.TrimSpace(source.Dir) == "" {
return fmt.Errorf("driver %q did not report a usable source profile for host-defined Tools", driverType)
}
sourceDir, err := filepath.Abs(source.Dir)
if err != nil {
return fmt.Errorf("resolve source profile path for host-defined Tools: %w", err)
}
sourceDir = filepath.Clean(sourceDir)
semanticKey := engine.StableHash(
"adaptor/hosted-tool-profile-selection/v1",
driverType,
identity,
sourceDir,
a.defaults.profile,
)
if existing, ok := a.toolProfileSelections[semanticKey]; ok {
eff.effectiveProfile = engine.CloneProfileSelection(existing.execution)
return nil
}
ownedDir, err := os.MkdirTemp("", "agent-adaptor-tool-profile-")
if err != nil {
return fmt.Errorf("allocate isolated hosted Tool profile: %w", err)
}
if err := os.Chmod(ownedDir, 0o700); err != nil {
_ = os.RemoveAll(ownedDir)
return fmt.Errorf("secure isolated hosted Tool profile: %w", err)
}
execution := &driver.ProfileSelection{
Mode: driver.ProfileModeClone,
Dir: ownedDir,
From: sourceDir,
Clone: &driver.CloneProfileOptions{
IncludeSettings: true,
IncludeMCP: true,
IncludeSkills: true,
AuthMode: driver.CloneProfileAuthLink,
},
}
selection := hostedToolProfileSelection{
execution: execution,
compatibility: hostedToolProfileCompatibilityView{
Version: "isolated-clone/v1",
SourceDir: sourceDir,
Requested: engine.CloneProfileSelection(a.defaults.profile),
},
ownedDir: ownedDir,
}
if a.toolProfileSelections == nil {
a.toolProfileSelections = make(map[string]hostedToolProfileSelection)
}
a.toolProfileSelections[semanticKey] = selection
eff.effectiveProfile = engine.CloneProfileSelection(execution)
return nil
}
func (a *Agent) claimHostedToolProfile(ctx context.Context, identity driver.AgentIdentity, selection *driver.ProfileSelection) error {
if a == nil || a.toolProvider == nil {
return nil
}
driverType := a.driver.Descriptor().Type
if !mcpruntime.SupportsHostedToolProfile(driverType) {
return nil
}
reporter, ok := a.driver.(driver.ProfileReporter)
if !ok {
return fmt.Errorf("driver %q persists MCP in a native profile but does not implement driver.ProfileReporter", driverType)
}
// GetProfile materializes clone selections in every built-in Driver. Keep it
// under the same Agent lock as ownership registration so concurrent first
// runs cannot both create auth links or copy the same target.
a.toolProfileMu.Lock()
defer a.toolProfileMu.Unlock()
profile, err := reporter.GetProfile(ctx, nil, identity, engine.CloneProfileSelection(selection))
if err != nil {
return fmt.Errorf("resolve hosted Tool profile: %w", err)
}
if !profile.Supported || strings.TrimSpace(profile.Dir) == "" {
return fmt.Errorf("driver %q did not report a usable profile for host-defined Tools", driverType)
}
absDir, err := filepath.Abs(profile.Dir)
if err != nil {
return fmt.Errorf("resolve hosted Tool profile path: %w", err)
}
absDir = filepath.Clean(absDir)
key := engine.StableHash("adaptor/hosted-tool-profile/v1", driverType, absDir)
if _, exists := a.toolProfiles[key]; exists {
return nil
}
materializedFingerprint, err := hostedToolMaterializedProfileFingerprint(driverType, absDir)
if err != nil {
return fmt.Errorf("fingerprint isolated hosted Tool profile: %w", err)
}
for selectionKey, selected := range a.toolProfileSelections {
if selected.execution == nil || filepath.Clean(selected.execution.Dir) != absDir {
continue
}
selected.compatibility.MaterializedFingerprint = materializedFingerprint
a.toolProfileSelections[selectionKey] = selected
}
if a.toolProfiles == nil {
a.toolProfiles = make(map[string]hostedToolProfileClaim)
}
a.toolProfiles[key] = hostedToolProfileClaim{driverType: driverType, dir: absDir}
return nil
}
func (a *Agent) hostedToolProfileCompatibility(profile *driver.ProfileSelection) any {
if a == nil || profile == nil || strings.TrimSpace(profile.Dir) == "" {
return profile
}
wanted := filepath.Clean(profile.Dir)
a.toolProfileMu.Lock()
defer a.toolProfileMu.Unlock()
for _, selection := range a.toolProfileSelections {
if selection.execution != nil && filepath.Clean(selection.execution.Dir) == wanted {
return selection.compatibility
}
}
return profile
}
// releaseHostedToolProfiles removes the owned MCP projection and then the
// private clone directory for every identity used by this Agent.
func (a *Agent) releaseHostedToolProfiles(ctx context.Context) error {
if a == nil {
return nil
}
a.toolProfileMu.Lock()
defer a.toolProfileMu.Unlock()
for key, claim := range a.toolProfiles {
if err := mcpruntime.RemoveHostedToolProfile(ctx, claim.driverType, claim.dir); err != nil {
return fmt.Errorf("remove hosted Tool profile entry: %w", err)
}
delete(a.toolProfiles, key)
}
for key, selection := range a.toolProfileSelections {
if err := removeHostedToolProfileDir(selection.ownedDir); err != nil {
return fmt.Errorf("remove isolated hosted Tool profile: %w", err)
}
delete(a.toolProfileSelections, key)
}
return nil
}
func removeHostedToolProfileDir(dir string) error {
root, err := filepath.Abs(os.TempDir())
if err != nil {
return err
}
target, err := filepath.Abs(dir)
if err != nil {
return err
}
root, target = filepath.Clean(root), filepath.Clean(target)
if target == root || filepath.Dir(target) != root || !strings.HasPrefix(filepath.Base(target), "agent-adaptor-tool-profile-") {
return fmt.Errorf("refusing to remove non-owned profile path %q", target)
}
info, err := os.Lstat(target)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return fmt.Errorf("refusing to remove non-directory hosted Tool profile %q", target)
}
return os.RemoveAll(target)
}
type hostedToolProfileFingerprintEntry struct {
Path string
Mode fs.FileMode
Fingerprint string
}
// hostedToolMaterializedProfileFingerprint covers the provider-visible
// settings, MCP declarations, and skills copied into the isolated profile.
// Authentication files are deliberately excluded: they are linked rather
// than copied, may rotate independently, and must never enter durable hashes.
func hostedToolMaterializedProfileFingerprint(driverType, dir string) (string, error) {
roots, ok := map[string][]string{
"codex": {"config.json", "config.toml", "instructions.md", "skills"},
"claude": {"settings.json", "config.json", ".claude.json", "skills"},
"cursor": {"config.json", "settings.json", "mcp.json", "skills"},
"codebuddy": {"settings.json", ".mcp.json", "mcp.json", "skills"},
}[driverType]
if !ok {
return "", fmt.Errorf("unsupported hosted Tool profile driver %q", driverType)
}
sort.Strings(roots)
entries := make([]hostedToolProfileFingerprintEntry, 0)
const maxEntries = 20_000
const maxBytes = int64(64 << 20)
var totalBytes int64
for _, name := range roots {
logicalRoot := filepath.Join(dir, name)
resolvedRoot, err := filepath.EvalSymlinks(logicalRoot)
if os.IsNotExist(err) {
continue
}
if err != nil {
return "", err
}
rootInfo, err := os.Stat(resolvedRoot)
if err != nil {
return "", err
}
if !rootInfo.IsDir() {
raw, err := os.ReadFile(resolvedRoot)
if err != nil {
return "", err
}
totalBytes += int64(len(raw))
if totalBytes > maxBytes {
return "", fmt.Errorf("materialized profile exceeds %d bytes", maxBytes)
}
digest := sha256.Sum256(raw)
entries = append(entries, hostedToolProfileFingerprintEntry{
Path: name, Mode: rootInfo.Mode().Perm(), Fingerprint: hex.EncodeToString(digest[:]),
})
continue
}
err = filepath.WalkDir(resolvedRoot, func(current string, entry fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if entry.IsDir() {
return nil
}
info, err := entry.Info()
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return fmt.Errorf("unsupported profile entry %q", current)
}
raw, err := os.ReadFile(current)
if err != nil {
return err
}
totalBytes += int64(len(raw))
if totalBytes > maxBytes {
return fmt.Errorf("materialized profile exceeds %d bytes", maxBytes)
}
rel, err := filepath.Rel(resolvedRoot, current)
if err != nil {
return err
}
digest := sha256.Sum256(raw)
entries = append(entries, hostedToolProfileFingerprintEntry{
Path: filepath.ToSlash(filepath.Join(name, rel)),
Mode: info.Mode().Perm(), Fingerprint: hex.EncodeToString(digest[:]),
})
if len(entries) > maxEntries {
return fmt.Errorf("materialized profile exceeds %d entries", maxEntries)
}
return nil
})
if err != nil {
return "", err
}
}
sort.Slice(entries, func(i, j int) bool { return entries[i].Path < entries[j].Path })
return engine.StableHash("adaptor/hosted-tool-materialized-profile/v1", driverType, roots, entries), nil
}
func (*hostedToolProvider) DetachRun(context.Context, string) error { return nil }
// stabilizeHostedToolCompatibility separates the concrete connection used for
// this process from the semantic capability identity used by resumable
// sessions. The numeric loopback port is intentionally ephemeral across host
// restarts: Drivers must still materialize the real req.MCP URL, while Thread
// and provider session guards must see the stable catalog revision instead.
func (a *Agent) stabilizeHostedToolCompatibility(req *driver.Request) string {
if a == nil || req == nil || a.toolProvider == nil {
if req == nil {
return ""
}
return req.MCP.Fingerprint
}
provider, ok := a.toolProvider.(*hostedToolProvider)
if !ok || provider == nil || provider.fingerprint == "" {
return req.MCP.Fingerprint
}
servers := engine.CloneMCPServerSpecs(req.MCP.Servers)
found := false
for index := range servers {
if servers[index].Key != toolruntime.ServerKey {
continue
}
// Replacing the volatile endpoint and per-Agent credential carrier
// preserves every other normalized MCP dimension, including external
// servers composed with WithTools.
servers[index].URL = "agent-owned://host-defined-tools/" + provider.fingerprint
servers[index].BearerTokenEnvVar = toolidentity.CompatibilityBearerTokenEnvVar
found = true
}
if !found {
return req.MCP.Fingerprint
}
mcpFingerprint := engine.StableHash("mcp", servers)
profileMCP := req.ProfilePayload.MCP
profileMCP.Fingerprint = mcpFingerprint
compatibleProfile := engine.BuildProfilePayload(
req.ProfilePayload.Skills,
profileMCP,
req.ProfilePayload.Agents,
req.ProfilePayload.Hooks,
req.ProfilePayload.Instructions,
req.ProfilePayload.Config,
req.ProfilePayload.Declared,
)
// Keep req.MCP and req.ProfilePayload.MCP untouched: their concrete URL
// fingerprints drive collision-safe profile materialization. Only the
// provider's resume guard uses this semantic compatibility fingerprint. The
// Driver SPI requires every resumed invocation to apply the current Request,
// so a restarted Agent can safely rebind the new endpoint without treating a
// transport allocation detail as a new capability.
req.ProfilePayload.SessionCompatibilityFingerprint = compatibleProfile.Fingerprint
return mcpFingerprint
}
// normalizeHostedToolServiceCompatibility removes the same ephemeral URL from
// the runtime-service portion of the Thread fingerprint. ReuseKey already
// carries the deterministic catalog fingerprint and is checked here so an
// unrelated service using a similar name cannot be normalized accidentally.
func (a *Agent) normalizeHostedToolServiceCompatibility(view *threadRuntimeCompatibilityView) {
if a == nil || view == nil || a.toolProvider == nil {
return
}
provider, ok := a.toolProvider.(*hostedToolProvider)
if !ok || provider == nil || provider.fingerprint == "" {
return
}
for index := range view.Ensured {
ref := &view.Ensured[index]
if ref.ID == toolruntime.ServerKey && ref.Name == toolruntime.ServerKey && ref.ReuseKey == provider.fingerprint {
ref.URL = "agent-owned://host-defined-tools"
}
}
ownedEnv := provider.bearerTokenEnvVar()
for index, name := range view.SecretEnvNames {
if name == ownedEnv {
view.SecretEnvNames[index] = toolidentity.CompatibilityBearerTokenEnvVar
}
}
}
var (
_ RunServiceProvider = (*hostedToolProvider)(nil)
_ ownedToolRuntime = (*toolruntime.Runtime)(nil)
)