-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_harness_instance.go
More file actions
203 lines (189 loc) · 7.84 KB
/
Copy pathmain_harness_instance.go
File metadata and controls
203 lines (189 loc) · 7.84 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
package main
import (
"crypto/rand"
"encoding/hex"
"log"
"os"
"path/filepath"
"sync"
"time"
"agent-overflow/internal/appidentity"
"agent-overflow/internal/atomicfile"
"agent-overflow/internal/harness/instanceinfo"
"agent-overflow/internal/transport"
)
var instanceIdentityCache sync.Map // canonical data root -> instanceinfo.Identity
// instanceIdentityFor returns one immutable identity for a backend lifetime.
// The bootstrap line and discovery files are written in separate boot phases,
// so they must obtain the same nonce from this cache.
func instanceIdentityFor(paths harnessPaths, mode instanceinfo.Mode, windowed bool, launcherPID int, launcherStartTime, launcherExecutable, launcherProfile, launcherWebviewProfile string) instanceinfo.Identity {
key, err := instanceinfo.CanonicalPath(paths.DataRoot)
if err != nil {
key = filepath.Clean(paths.DataRoot)
}
if existing, ok := instanceIdentityCache.Load(key); ok {
return existing.(instanceinfo.Identity)
}
nonceBytes := make([]byte, 16)
if _, err := rand.Read(nonceBytes); err != nil {
panic("harness: generate boot nonce: " + err.Error())
}
identity := instanceinfo.Identity{
IdentityVersion: instanceinfo.IdentityVersion,
ID: instanceinfo.ID(paths.DataRoot),
Mode: mode,
Window: windowed,
BootNonce: hex.EncodeToString(nonceBytes),
Worktree: bootWorkingDir(),
StartedAt: time.Now().UTC().Format(time.RFC3339Nano),
LauncherPid: launcherPID,
PIDNamespace: instanceinfo.CurrentPIDNamespace(),
}
if launcherPID > 0 {
identity.LauncherProcessStartTime = launcherStartTime
identity.LauncherExecutablePath = launcherExecutable
identity.LauncherProfile = launcherProfile
identity.LauncherDataRoot = paths.DataRoot
identity.LauncherWebviewProfile = launcherWebviewProfile
identity.LauncherPIDNamespace = "windows"
}
if process, err := instanceinfo.CaptureProcessIdentity(os.Getpid()); err == nil {
identity.ProcessStartTime = process.StartTime
identity.ExecutablePath = process.Executable
} else {
log.Printf("harness: capture process identity: %v", err)
}
actual, loaded := instanceIdentityCache.LoadOrStore(key, identity)
if loaded {
return actual.(instanceinfo.Identity)
}
return identity
}
// Instance discovery for isolated boots (--harness / --soak, windowed or
// not). Two files, written at the same moment — right after MarkReady,
// which is the first instant an attach would succeed — and removed on
// graceful shutdown:
//
// - <dataDir>/harness-instance.json (0600): the full bootstrap payload
// plus the identity block. This is what lets any tool attach to a
// RUNNING instance without having parsed its stdout, which stdout
// only offered to whoever spawned it.
// - <registryDir>/<id>.json: discovery only, no token
// (internal/harness/instanceinfo).
//
// Neither is load-bearing for the boot itself, so a failure to write
// either is logged and the instance keeps running: the harness works
// exactly as it did before these files existed, minus discovery.
// harnessRegistryDir is where discovery rows are filed, resolved at
// package init — which is to say BEFORE isolateWebviewStorage points
// XDG_CACHE_HOME at the instance's own data root. os.UserCacheDir()
// reads that variable, so resolving it any later files the row inside
// the isolated tree that only this instance can see, and a discovery
// tool finds nothing (observed the first time this ran windowed,
// 2026-08-26). Empty when no cache dir is resolvable; publishInstance
// says so and skips the row rather than guessing at a path.
var harnessRegistryDir = func() string {
dir, err := instanceinfo.RegistryDir()
if err != nil {
return ""
}
return dir
}()
// harnessInstanceFile is the data-dir file's shape: the same bootstrap
// payload agents already parse off stdout, plus the identity fields that
// tie it to a registry row. Embedding rather than restating means a
// field added to either half reaches this file automatically.
type harnessInstanceFile struct {
harnessBootstrap
instanceinfo.Identity
}
// publishedInstance is the handle a boot keeps so it can withdraw its
// discovery files. Removal is idempotent, so the shutdown path and an
// error path may both call it.
type publishedInstance struct {
id string
filePath string
// registryDir is captured per instance so removal withdraws the row
// from the directory the write used, whatever the environment has
// done to XDG_CACHE_HOME since.
registryDir string
}
// publishInstance writes both discovery files for a live isolated boot.
// Never fails a boot: every problem is a log line.
//
// launcherPID is the Windows launcher hosting this backend's window, or
// 0 when nobody does — the caller knows, because it is the boot flag the
// launcher spelled.
func publishInstance(srv *transport.Server, paths harnessPaths, mode instanceinfo.Mode, windowed bool, launcherPID int, launcherStartTime, launcherExecutable, launcherProfile, launcherWebviewProfile string) publishedInstance {
identity := instanceIdentityFor(paths, mode, windowed, launcherPID, launcherStartTime, launcherExecutable, launcherProfile, launcherWebviewProfile)
published := publishedInstance{
id: identity.ID,
filePath: filepath.Join(paths.DataDir, instanceinfo.InstanceFileName),
registryDir: harnessRegistryDir,
}
bootstrap := newHarnessBootstrap(srv, paths, nil)
bootstrap.Identity = identity
file := harnessInstanceFile{
harnessBootstrap: bootstrap,
Identity: identity,
}
// atomicfile writes 0600: this file carries the transport token.
if err := atomicfile.WriteJSON(published.filePath, file); err != nil {
log.Printf("harness: write %s: %v (tools must fall back to the bootstrap line)", published.filePath, err)
}
row := instanceinfo.Row{
Identity: identity,
PID: os.Getpid(),
Port: portFromAddr(srv.Addr()),
DataRoot: paths.DataRoot,
DataDir: paths.DataDir,
Version: version,
}
switch {
case published.registryDir == "":
log.Printf("harness: no user cache dir; instance %s will not be listed by discovery tools", identity.ID)
default:
if err := instanceinfo.WriteIn(published.registryDir, row); err != nil {
log.Printf("harness: register instance %s: %v (this instance will not be listed)", identity.ID, err)
}
}
log.Printf("harness: instance %s (%s, window=%v) published at %s, registry %s", identity.ID, mode, windowed, published.filePath, published.registryDir)
return published
}
// remove withdraws both discovery files. Called on every graceful
// shutdown path; a reader that finds a row we failed to remove treats a
// dead pid as stale, so this is tidiness rather than correctness.
func (p publishedInstance) remove() {
if p.id == "" {
return
}
if err := os.Remove(p.filePath); err != nil && !os.IsNotExist(err) {
log.Printf("harness: remove %s: %v", p.filePath, err)
}
if p.registryDir == "" {
return
}
if err := instanceinfo.RemoveIn(p.registryDir, p.id); err != nil {
log.Printf("harness: deregister instance %s: %v", p.id, err)
}
}
// bootWorkingDir reports the checkout an instance was launched from, or
// "" when the working directory is unreadable (a deleted cwd) — an empty
// worktree field is a discovery inconvenience, never a boot failure.
func bootWorkingDir() string {
cwd, err := os.Getwd()
if err != nil {
log.Printf("harness: read working directory: %v", err)
return ""
}
return cwd
}
// isolatedWindowTitle names a windowed isolated instance:
// "Agent Overflow (harness · 0f3a91cc)". Humans tell windows apart with
// it; tools use the registry. It carries the instance id rather than a
// profile word because N of these can be on screen at once — one per
// checkout — which is the case appidentity.AppTitle's fixed
// dev/soak/prod axis cannot name.
func isolatedWindowTitle(mode instanceinfo.Mode, id string) string {
return appidentity.Name + " (" + string(mode) + " · " + id + ")"
}