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
//
// AgentsView.swift
// AT-IRC
//
// Agents section: the app's agent VMs from the embedded socialweb market,
// with launch and release. See AgentsAPI for the flow.
//
import SwiftUI
struct AgentsView: View {
let kcp: KCPHost
let requesterConfig: () async -> String
@State private var store = AgentsStore()
@State private var safari: IdentifiableURL?
var body: some View {
NavigationStack {
List {
statusSection
agentsSection
if let errorText = store.errorText {
Section {
Text(errorText)
.font(.footnote)
.foregroundStyle(.red)
}
}
}
.navigationTitle("Agents")
.refreshable { await store.refresh() }
.sheet(item: $safari) { target in
SafariView(url: target.url)
.onDisappear {
Task { await store.refresh() }
}
}
.task {
// kcp boots only when the Agents section is opened; the chat
// never waits on it. The requester starts on top of kcp.
await kcp.start()
let config = await requesterConfig()
let configReady = config != "{}" && !config.isEmpty
if configReady {
await kcp.startRequester(configJSON: config)
}
await store.refresh()
if !configReady {
store.errorText = "The OAuth redirect listener did not start, so the market requester cannot sign in. Relaunch the app and try again."
}
#if DEBUG
if ProcessInfo.processInfo.environment["ATIRC_TEST_AGENT_LAUNCH"] == "1" {
// Launch, wait for Held/mint, send guest.agentStart like the
// page's Agent button, hold for the join, release in-process.
var waited = 0
while store.identityPhase != "Ready" && waited < 90 {
try? await Task.sleep(for: .seconds(2))
await store.refresh()
waited += 1
}
if store.identityPhase == "Ready", let contract = await store.launchAndWait() {
try? await Task.sleep(for: .seconds(40))
KCPHost.persistSnapshot()
await store.agentStart(contractID: contract)
try? await Task.sleep(for: .seconds(240))
await store.releaseAllHeld()
}
}
if ProcessInfo.processInfo.environment["ATIRC_DUMP_WORKSPACE"] == "1" {
// Stall diagnosis: the requester compose's Workspace app can
// stay Initializing; log why every 5 s.
for _ in 0..<24 {
await KCPHost.logWorkspaceState()
await KCPHost.logPolicyRuns()
try? await Task.sleep(for: .seconds(5))
}
}
if ProcessInfo.processInfo.environment["ATIRC_RELEASE_ALL"] == "1" {
// Test hook: release every contract the session infra still
// holds, so launch retries do not leave VMs running. The
// requester can finish composing after the bounded start,
// so wait for its API before releasing.
for _ in 0..<30 {
await store.refresh()
if store.availability == .ready {
await store.releaseAllHeld()
break
}
try? await Task.sleep(for: .seconds(10))
}
}
if ProcessInfo.processInfo.environment["ATIRC_MARKET_SIGNIN"] == "1",
store.identityPhase != "Ready" {
await store.beginSignIn(requesterConfigJSON: config)
}
if ProcessInfo.processInfo.environment["ATIRC_TEST_LAUNCH"] == "1" {
// The bounded requester start can return before the
// MarketIdentity finishes composing; wait for Ready so the
// test launch does not race it.
var waited = 0
while store.identityPhase != "Ready" && waited < 90 {
try? await Task.sleep(for: .seconds(2))
await store.refresh()
waited += 1
}
if store.identityPhase == "Ready" {
await store.launch()
// The mint lands a few seconds after the contract; give
// it time, then persist vault+infra so a restarted app
// can revoke before terminating the VM.
try? await Task.sleep(for: .seconds(40))
KCPHost.persistSnapshot()
for _ in 0..<24 {
try? await Task.sleep(for: .seconds(5))
await KCPHost.logPolicyRuns()
}
}
}
#endif
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
Task { await store.launch() }
} label: {
if store.launching {
ProgressView().controlSize(.small)
} else {
Label("Launch", systemImage: "plus")
}
}
.disabled(store.launching || store.availability != .ready)
}
}
}
}
private var statusSection: some View {
Section("Market") {
switch kcp.state {
case .idle, .starting:
HStack(spacing: 8) {
ProgressView().controlSize(.small)
Text("Starting the embedded kcp…").foregroundStyle(.secondary)
}
case .failed(let message):
Text("kcp failed: \(message)")
.font(.footnote)
.foregroundStyle(.red)
case .ready:
switch store.availability {
case .unknown:
Text("Checking the embedded kcp…").foregroundStyle(.secondary)
case .ready:
if !store.identityPhase.isEmpty && store.identityPhase != "Ready" {
Button("Sign in to market") {
if let url = store.signInURL {
safari = IdentifiableURL(url: url)
} else {
Task {
await store.beginSignIn(
requesterConfigJSON: await requesterConfig()
)
if let url = store.signInURL {
safari = IdentifiableURL(url: url)
}
}
}
}
Text("Sign in to let the requester act as you. Only needed once.")
.font(.footnote)
.foregroundStyle(.secondary)
} else if !store.identityPhase.isEmpty {
Text("market identity: \(store.identityPhase)")
.font(.footnote)
.foregroundStyle(.secondary)
}
Text("socialweb requester ready").foregroundStyle(.green)
case .unavailable(let reason):
Text(reason)
.font(.footnote)
.foregroundStyle(.secondary)
}
Button("Stop embedded kcp", role: .destructive) {
kcp.stop()
store.refreshAfterStop()
}
.font(.footnote)
Text("Frees the embedded control plane; Agents restarts it on demand.")
.font(.footnote)
.foregroundStyle(.secondary)
}
}
}
private var agentsSection: some View {
Section("Agent VMs") {
if store.agentContracts.isEmpty {
Text("No agent launches yet.")
.foregroundStyle(.secondary)
} else {
ForEach(store.agentContracts) { contract in
AgentRow(contract: contract, store: store)
}
}
}
}
}
private struct AgentRow: View {
let contract: AgentContract
let store: AgentsStore
var body: some View {
VStack(alignment: .leading, spacing: 4) {
HStack {
Text(contract.id)
.font(.callout.monospaced())
.lineLimit(1)
.truncationMode(.middle)
Spacer()
Text(contract.phase)
.font(.caption)
.foregroundStyle(phaseColor)
}
if let vm = contract.spec?.vmName {
Text(vm)
.font(.caption2)
.foregroundStyle(.secondary)
}
if contract.phase != "Released" {
HStack(spacing: 12) {
if contract.phase == "Held" {
Button("Start agent") {
Task { await store.agentStart(contractID: contract.id) }
}
.buttonStyle(.borderedProminent)
.controlSize(.small)
}
Button("Release") {
Task { await store.release(contract) }
}
.buttonStyle(.bordered)
.controlSize(.small)
}
}
}
.padding(.vertical, 2)
}
private var phaseColor: Color {
switch contract.phase {
case "Held":
return .green
case "Released":
return .secondary
case "Failed":
return .red
default:
return .orange
}
}
}