nebula-ai-agent 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +39 -0
  2. package/bin/nebula +11 -0
  3. package/package.json +50 -0
  4. package/src/commands/_agents.ts +14 -0
  5. package/src/commands/_unlock.ts +66 -0
  6. package/src/commands/agent-wallet.ts +90 -0
  7. package/src/commands/chat-telegram.ts +398 -0
  8. package/src/commands/chat.tsx +1308 -0
  9. package/src/commands/drain.ts +90 -0
  10. package/src/commands/gateway-logs.ts +49 -0
  11. package/src/commands/gateway-run.ts +42 -0
  12. package/src/commands/gateway-start.ts +216 -0
  13. package/src/commands/gateway-status.ts +90 -0
  14. package/src/commands/gateway-stop.ts +133 -0
  15. package/src/commands/gateway.ts +101 -0
  16. package/src/commands/identity.ts +178 -0
  17. package/src/commands/init/cost.ts +40 -0
  18. package/src/commands/init/funding-gate.ts +64 -0
  19. package/src/commands/init/model-picker.ts +25 -0
  20. package/src/commands/init/operator-picker.ts +233 -0
  21. package/src/commands/init/telegram-step.ts +245 -0
  22. package/src/commands/init/wizard-state.ts +94 -0
  23. package/src/commands/init.ts +439 -0
  24. package/src/commands/login.ts +86 -0
  25. package/src/commands/logs.ts +37 -0
  26. package/src/commands/model.ts +48 -0
  27. package/src/commands/pairing-approve.ts +65 -0
  28. package/src/commands/pairing-clear.ts +39 -0
  29. package/src/commands/pairing-list.ts +55 -0
  30. package/src/commands/pairing-revoke.ts +49 -0
  31. package/src/commands/pairing.ts +81 -0
  32. package/src/commands/status.ts +44 -0
  33. package/src/commands/telegram-remove.ts +62 -0
  34. package/src/commands/telegram-setup.ts +64 -0
  35. package/src/commands/telegram-status.ts +87 -0
  36. package/src/commands/telegram.ts +44 -0
  37. package/src/commands/trust.ts +196 -0
  38. package/src/config/load.ts +35 -0
  39. package/src/config/render.ts +99 -0
  40. package/src/index.ts +184 -0
  41. package/src/profile/crypto.ts +68 -0
  42. package/src/profile/derive.ts +25 -0
  43. package/src/profile/store.ts +86 -0
  44. package/src/profile/unlock.ts +29 -0
  45. package/src/ui/app.tsx +719 -0
  46. package/src/ui/approval-summary.ts +32 -0
  47. package/src/ui/markdown-parse.ts +219 -0
  48. package/src/ui/markdown.tsx +37 -0
  49. package/src/ui/state.ts +181 -0
  50. package/src/util/bootstrap-mode.ts +25 -0
  51. package/src/util/bootstrap-progress-box.ts +378 -0
  52. package/src/util/cli-version.ts +28 -0
  53. package/src/util/format.ts +11 -0
  54. package/src/util/gateway-spawn.ts +125 -0
  55. package/src/util/gateway-version.ts +154 -0
  56. package/src/util/github-releases.ts +79 -0
  57. package/src/util/profile-key.ts +25 -0
  58. package/src/util/ref-resolver.ts +55 -0
  59. package/src/util/silence-console.ts +40 -0
  60. package/src/util/telegram-secrets.ts +218 -0
@@ -0,0 +1,1308 @@
1
+ import { mkdir, readFile } from 'node:fs/promises'
2
+ import { homedir } from 'node:os'
3
+ import { spinner } from '@clack/prompts'
4
+ import {
5
+ ActivityLog,
6
+ type BrainMessage,
7
+ type ClaudeAgent,
8
+ type ClaudeCommand,
9
+ HookBus,
10
+ type Listener,
11
+ LocalBackend,
12
+ McpManager,
13
+ type NebulaConfig,
14
+ OpenAIBrain,
15
+ type PermissionDecision,
16
+ type PermissionMode,
17
+ type PermissionRequest,
18
+ PermissionService,
19
+ type PostToolCallContext,
20
+ type PreToolCallContext,
21
+ type PreToolCallResult,
22
+ type SandboxBackend,
23
+ type SkillRef,
24
+ ToolRegistry,
25
+ type VisionInferFn,
26
+ agentPaths,
27
+ applyPerms,
28
+ applyYolo,
29
+ buildFrozenPrefix,
30
+ createFsHistoryPersist,
31
+ decodeKeystoreBytes,
32
+ decryptAgentKey,
33
+ detectFetchEscalation,
34
+ discoverClaudeExtras,
35
+ discoverMcpServers,
36
+ explorerTxUrl,
37
+ loadPlugins,
38
+ makeMemoryListTool,
39
+ makeMemoryReadTool,
40
+ makeMemorySaveTool,
41
+ makeSandboxBackend,
42
+ makeToolSearchTool,
43
+ makeViemClients,
44
+ matchSkillTriggers,
45
+ newEventId,
46
+ placeholderAgentId,
47
+ readIndexFile,
48
+ runEscalation,
49
+ scanSkills,
50
+ } from 'nebula-ai-core'
51
+ import {
52
+ ONCHAIN_GUIDANCE,
53
+ type OnchainRuntimeContext,
54
+ policyFromEnv,
55
+ policyRequiresApprovalForCall,
56
+ } from 'nebula-ai-plugin-onchain'
57
+ import {
58
+ TELEGRAM_GUIDANCE,
59
+ type TelegramApprovalBridge,
60
+ type TelegramRuntimeContext,
61
+ formatInboundPreview as formatTelegramInboundPreview,
62
+ } from 'nebula-ai-plugin-telegram'
63
+ import { type Address, type Hex, formatEther } from 'viem'
64
+ import { findAndLoadConfig } from '../config/load'
65
+ import { writeConfigTs } from '../config/render'
66
+ import { tryProfileUnlock } from '../profile/unlock'
67
+ import { shortAddr } from '../util/format'
68
+ import { loadTelegramSecrets, telegramSecretsExist } from '../util/telegram-secrets'
69
+ import {
70
+ type TelegramDispatchSlot,
71
+ buildTelegramDispatch,
72
+ buildTelegramRuntimeContext,
73
+ } from './chat-telegram'
74
+ import { loadOrPickOperatorSigner } from './init/operator-picker'
75
+
76
+ export async function runChat(opts?: { cwd?: string; yolo?: boolean }): Promise<void> {
77
+ const found = await findAndLoadConfig(opts?.cwd)
78
+ if (!found) {
79
+ console.log('No nebula.config.ts found. Run `nebula init` first.')
80
+ process.exit(1)
81
+ }
82
+ let { config } = found
83
+ const configPath = found.path
84
+
85
+ if (!config.identity.agent) {
86
+ console.log('Config has no agent yet. Re-run `nebula init`.')
87
+ process.exit(1)
88
+ }
89
+ // Phase 14: if a local gateway daemon is running for this agent (socket
90
+ // present at ~/.nebula/agents/<id>/gateway.sock), route to the same thin
91
+ // client over a unix socket. The TUI no longer holds the runtime — the
92
+ // gateway daemon does. Closing the TUI doesn't stop the listeners.
93
+ //
94
+ // v0.21.5: when no daemon is running but an operator session is fresh,
95
+ // AUTO-SPAWN the daemon as a child process and attach as thin-client.
96
+ // Without this, embedded TUI fallthrough silently disables (a) Telegram
97
+ // pairing-store wiring (no inbound delivery) and (b) AutoTopupManager
98
+ // polling. NEBULA_FORCE_EMBEDDED=1 escape hatch keeps the legacy path
99
+ // available for tests / debugging.
100
+ // Identity is the agent EOA; chat always runs embedded. To bring telegram +
101
+ // pairing online as an always-on daemon, run `nebula gateway start`.
102
+ const agentAddress = config.identity.agent as Address
103
+ const agentId = placeholderAgentId(agentAddress)
104
+ const paths = agentPaths.agent(agentId)
105
+
106
+ // Password-profile fast path: a live session or the profile password unlocks
107
+ // the agent key without an operator-wallet signature. `operator` stays null in
108
+ // that case (only the keystore-encrypted Telegram secrets need it, and those
109
+ // are skipped below). Falls back to the operator unlock when no profile exists.
110
+ let agentPrivkey: Hex
111
+ let operator: Awaited<ReturnType<typeof loadOrPickOperatorSigner>> | null = null
112
+ const profileKey = await tryProfileUnlock(agentAddress)
113
+ if (profileKey) {
114
+ agentPrivkey = profileKey
115
+ } else {
116
+ operator = await loadOrPickOperatorSigner({
117
+ network: config.network,
118
+ hint: config.operator,
119
+ })
120
+ if (!operator) {
121
+ console.log('No operator wallet available; cannot decrypt keystore.')
122
+ process.exit(1)
123
+ }
124
+
125
+ const sUnlock = spinner()
126
+ sUnlock.start('Decrypting agent keystore via operator wallet')
127
+ try {
128
+ // The encrypted agent keystore lives only on disk, decryptable by the
129
+ // operator wallet signature.
130
+ const raw = await readFile(paths.keystore, 'utf8')
131
+ const keystore = decodeKeystoreBytes(new TextEncoder().encode(raw))
132
+ agentPrivkey = (await decryptAgentKey({ signer: operator, agentAddress, keystore })) as Hex
133
+ sUnlock.stop('unlocked (keystore source: local)')
134
+ } catch (e) {
135
+ sUnlock.stop(`unlock failed: ${(e as Error).message.slice(0, 160)}`)
136
+ await operator.close?.()
137
+ process.exit(1)
138
+ }
139
+ }
140
+
141
+ // Phase 12: decrypt telegram-secrets blob (if any) using the SAME operator
142
+ // signer we already have unlocked. Avoids a second keychain prompt later.
143
+ // We only attempt this if the operator opted in via `nebula telegram setup`
144
+ // (presence of the encrypted blob); the plugin opt-in is independent and
145
+ // checked again below at plugin filter time.
146
+ let telegramSecrets: Awaited<ReturnType<typeof loadTelegramSecrets>> = null
147
+ const envTgToken = process.env.TELEGRAM_BOT_TOKEN
148
+ if (envTgToken) {
149
+ // Env-configured bot: works without `nebula telegram setup`. TELEGRAM_CHAT_ID
150
+ // (optional) is the sole allowed DM user; blank = open access.
151
+ const envChatId = process.env.TELEGRAM_CHAT_ID
152
+ telegramSecrets = {
153
+ botToken: envTgToken,
154
+ botUsername: process.env.TELEGRAM_USERNAME,
155
+ allowedUserIds: envChatId ? [Number(envChatId)] : [],
156
+ }
157
+ if (!(config.plugins ?? []).includes('telegram')) {
158
+ config = { ...config, plugins: [...(config.plugins ?? []), 'telegram'] }
159
+ }
160
+ } else if (
161
+ operator &&
162
+ telegramSecretsExist(agentId) &&
163
+ (config.plugins ?? []).includes('telegram')
164
+ ) {
165
+ const sTg = spinner()
166
+ sTg.start('Decrypting telegram secrets')
167
+ try {
168
+ telegramSecrets = await loadTelegramSecrets({ signer: operator, agentAddress, agentId })
169
+ sTg.stop(`telegram unlocked (bot @${telegramSecrets?.botUsername ?? '?'})`)
170
+ } catch (e) {
171
+ sTg.stop(`telegram decrypt failed: ${(e as Error).message.slice(0, 160)}`)
172
+ // Soft-fail: telegram is opt-in. Boot continues without it.
173
+ }
174
+ }
175
+
176
+ await operator?.close?.()
177
+
178
+ if (!config.brain.provider) {
179
+ const updated = await runModelPicker(config, configPath)
180
+ if (!updated) process.exit(1)
181
+ config = updated
182
+ }
183
+
184
+ const tools = new ToolRegistry(config.tools)
185
+ tools.register(makeMemorySaveTool({ agentId }) as Parameters<typeof tools.register>[0])
186
+ tools.register(makeMemoryReadTool({ agentId }) as Parameters<typeof tools.register>[0])
187
+ tools.register(makeMemoryListTool({ agentId }) as Parameters<typeof tools.register>[0])
188
+ tools.register(makeToolSearchTool(tools) as Parameters<typeof tools.register>[0])
189
+
190
+ const initialMode: PermissionMode = opts?.yolo ? 'off' : (config.approvals?.mode ?? 'prompt')
191
+ const permission = new PermissionService({ mode: initialMode })
192
+ const hooks = new HookBus()
193
+
194
+ // Plugin failures are reported but do not abort startup; the brain still has
195
+ // memory tools.
196
+ //
197
+ // The dynamic `import()` MUST happen from the CLI package context: that's
198
+ // where the workspace deps `nebula-ai-plugin-*` live. Passing this
199
+ // resolver pins the import site to chat.tsx so bun's resolver finds them.
200
+ // Claude Code extras (commands + agents) discovery happens BEFORE plugin
201
+ // load so delegate.task can surface agents.
202
+ let claudeCommands: ClaudeCommand[] = []
203
+ let claudeAgents: ClaudeAgent[] = []
204
+ try {
205
+ const extras = await discoverClaudeExtras({
206
+ importsClaudeCode: config.imports?.claudeCode ?? true,
207
+ })
208
+ claudeCommands = extras.commands
209
+ claudeAgents = extras.agents
210
+ } catch {
211
+ // Discovery failed; continue without commands/agents.
212
+ }
213
+ const commandIndex = new Map<string, ClaudeCommand>()
214
+ for (const cmd of claudeCommands) {
215
+ if (!commandIndex.has(cmd.name)) commandIndex.set(cmd.name, cmd)
216
+ if (!commandIndex.has(cmd.id)) commandIndex.set(cmd.id, cmd)
217
+ }
218
+
219
+ // OpenAI-compatible LLM config (env-driven; default gpt-4o-mini, swappable to Z.AI/Tencent).
220
+ const llmApiKey = process.env.OPENAI_API_KEY ?? process.env.NEBULA_LLM_API_KEY ?? ''
221
+ const llmBaseUrl = process.env.NEBULA_LLM_BASE_URL
222
+ const llmModel = process.env.NEBULA_LLM_MODEL ?? config.brain?.model ?? 'gpt-4o-mini'
223
+
224
+ // Sub-brain factory for delegate.task (Phase 9.3). The factory creates a
225
+ // fresh OpenAIBrain with a custom system prompt. Tools default to none for
226
+ // delegated work; the parent calls delegate.task only when isolation matters.
227
+ const delegateFactory: import('nebula-ai-core').DelegateBrainFactory = async ({
228
+ systemPrompt,
229
+ tools: subTools,
230
+ }) => {
231
+ const subBrain = new OpenAIBrain({
232
+ apiKey: llmApiKey,
233
+ baseUrl: llmBaseUrl,
234
+ model: llmModel,
235
+ tools: subTools,
236
+ prefix: buildFrozenPrefix({
237
+ systemPrompt,
238
+ memoryIndex: null,
239
+ identity: null,
240
+ persona: null,
241
+ loadedToolNames: [],
242
+ skills: [],
243
+ timestamp: null,
244
+ }),
245
+ })
246
+ await subBrain.init()
247
+ return subBrain as unknown as import('nebula-ai-core').DelegateBrainHandle
248
+ }
249
+
250
+ // Phase 9.5: build sandbox backend BEFORE plugins load. Tools that spawn
251
+ // subprocesses (shell.run, code.execute, shell.process_start) wrap their
252
+ // spawn argv through this backend. NEBULA_SANDBOX_MODE env var wins over
253
+ // config (matches hermes' TERMINAL_ENV pattern — per-launch override
254
+ // without editing config).
255
+ const envOverride = process.env.NEBULA_SANDBOX_MODE
256
+ const sandboxMode: 'none' | 'os' | 'docker' =
257
+ envOverride === 'none' || envOverride === 'os' || envOverride === 'docker'
258
+ ? envOverride
259
+ : (config.sandbox?.mode ?? 'none')
260
+ let sandbox: SandboxBackend
261
+ try {
262
+ sandbox = makeSandboxBackend({
263
+ mode: sandboxMode,
264
+ agentDir: paths.dir,
265
+ workspaceRoot: process.cwd(),
266
+ homedir: homedir(),
267
+ dockerImage: config.sandbox?.dockerImage,
268
+ dockerMountWorkspace: config.sandbox?.dockerMountWorkspace,
269
+ dockerRuntimePath: config.sandbox?.dockerRuntimePath,
270
+ dockerCpu: config.sandbox?.dockerCpu,
271
+ dockerMemoryMb: config.sandbox?.dockerMemoryMb,
272
+ dockerDiskMb: config.sandbox?.dockerDiskMb,
273
+ dockerNoNetwork: config.sandbox?.dockerNoNetwork,
274
+ })
275
+ } catch (err) {
276
+ process.stderr.write(
277
+ `nebula: sandbox init failed (${(err as Error).message}), continuing without sandbox\n`,
278
+ )
279
+ sandbox = new LocalBackend()
280
+ }
281
+ if (sandbox.mode === 'os') {
282
+ process.stderr.write(
283
+ `nebula: sandbox active [${sandbox.label}] — limb spawns gated to agentDir + cwd + /tmp/nebula-* + /var/folders; reads of ~/.ssh ~/.aws ~/Library/Keychains ~/.config/gcloud denied\n`,
284
+ )
285
+ } else if (sandbox.mode === 'docker') {
286
+ process.stderr.write(
287
+ `nebula: container sandbox active [${sandbox.label}] — every shell-class spawn runs inside the container; host fs invisible to those tools${config.sandbox?.dockerMountWorkspace ? ' except mounted /workspace' : ''}\n`,
288
+ )
289
+ }
290
+ // Register dispose hook so docker containers don't leak when nebula exits.
291
+ // Signal handlers MUST await dispose before exiting; sync `process.exit(0)`
292
+ // would discard the dispose promise and leave the container orphaned.
293
+ if (sandbox.dispose) {
294
+ const disposeOnce = (() => {
295
+ let done = false
296
+ return async () => {
297
+ if (done) return
298
+ done = true
299
+ await sandbox.dispose?.().catch(() => {})
300
+ }
301
+ })()
302
+ process.once('SIGINT', () => {
303
+ void disposeOnce().then(() => process.exit(0))
304
+ })
305
+ process.once('SIGTERM', () => {
306
+ void disposeOnce().then(() => process.exit(0))
307
+ })
308
+ }
309
+
310
+ // Vision routing via the OpenAI-compatible brain is a follow-up; disabled for now.
311
+ const visionInfer: VisionInferFn | null = null
312
+
313
+ // Plugin filter: system + onchain ship; telegram is opt-in via
314
+ // `nebula telegram setup` which writes ~/.nebula/agents/<id>/telegram-secrets.encrypted
315
+ // and adds 'telegram' to config.plugins.
316
+ const pluginNames = (config.plugins ?? []).filter(
317
+ p => p === 'system' || p === 'onchain' || p === 'telegram',
318
+ )
319
+ // viem clients are built up front so the agent-EOA balance refresher works
320
+ // regardless of which plugins are loaded.
321
+ const viemClients = makeViemClients({ network: config.network, privkeyHex: agentPrivkey })
322
+ // Onchain side-band ctx: viem clients (already built above) + agent EOA.
323
+ // `mintBlock` is the Transfer-event scan floor for token discovery; with a
324
+ // plain-EOA identity there is no mint, so it starts at genesis (0n).
325
+ let onchain: OnchainRuntimeContext | undefined
326
+ if (pluginNames.includes('onchain')) {
327
+ onchain = {
328
+ agentEoa: agentAddress,
329
+ network: config.network,
330
+ policy: policyFromEnv(),
331
+ publicClient: viemClients.publicClient,
332
+ walletClient: viemClients.walletClient,
333
+ agentDir: paths.dir,
334
+ mintBlock: 0n,
335
+ brainProvider: config.brain.provider,
336
+ brainModel: config.brain.model,
337
+ }
338
+ }
339
+ // Phase 12: telegram side-band ctx. We build the runtime context now (before
340
+ // brain.init) so the plugin can register its listener via ctx.registerListener,
341
+ // but the dispatch callback is deferred — the slot's `.current` is null until
342
+ // brain.init resolves and we wire it below. Same for the system-row sink:
343
+ // populated once state exists.
344
+ const telegramSlot: TelegramDispatchSlot = { current: null }
345
+ const telegramSystemRowSink: { current: ((text: string) => void) | null } = { current: null }
346
+ const telegramInboundRowSink: { current: ((text: string) => void) | null } = { current: null }
347
+ const telegramAssistantRowSink: { current: ((text: string) => void) | null } = { current: null }
348
+ // Bridge for inline-keyboard approval. Listener fills the inner refs on
349
+ // start; chat-telegram's runOne reads them at turn time.
350
+ const telegramApprovalBridge: TelegramApprovalBridge = {
351
+ sendApproval: { current: null },
352
+ installCallbackHandler: { current: null },
353
+ }
354
+ let telegram: TelegramRuntimeContext | undefined
355
+ if (telegramSecrets && pluginNames.includes('telegram')) {
356
+ telegram = buildTelegramRuntimeContext({
357
+ botToken: telegramSecrets.botToken,
358
+ allowedUserIds: telegramSecrets.allowedUserIds,
359
+ agentName: `agent-${agentId.slice(0, 8)}`,
360
+ slot: telegramSlot,
361
+ systemRowSink: telegramSystemRowSink,
362
+ })
363
+ telegram.approvalBridge = telegramApprovalBridge
364
+ }
365
+ // Local listener registry: plugins register listeners via ctx.registerListener
366
+ // (e.g. telegram's inbound poller); we collect them here so chat can start them
367
+ // once brain init is done.
368
+ const collectedListeners: Listener[] = []
369
+ const skillsDisabled = { current: [...(config.skills?.disabled ?? [])] }
370
+ const loadResult = await loadPlugins(pluginNames, {
371
+ tools,
372
+ hooks,
373
+ listeners: {
374
+ register: l => {
375
+ collectedListeners.push(l)
376
+ },
377
+ },
378
+ agentDir: paths.dir,
379
+ agentId,
380
+ network: config.network,
381
+ configPath,
382
+ imports: { claudeCode: config.imports?.claudeCode ?? true },
383
+ skillsDisabled,
384
+ activityLogPath: paths.activityLog,
385
+ workspaceRoot: process.cwd(),
386
+ delegateFactory,
387
+ claudeAgents,
388
+ brainSupportsVision: false,
389
+ brainModelLabel: config.brain.model ?? config.brain.provider,
390
+ visionInfer,
391
+ sandbox,
392
+ onchain,
393
+ telegram,
394
+ resolve: async name => {
395
+ switch (name) {
396
+ case 'system':
397
+ return await import('nebula-ai-plugin-system')
398
+ case 'onchain':
399
+ return await import('nebula-ai-plugin-onchain')
400
+ case 'telegram':
401
+ return await import('nebula-ai-plugin-telegram')
402
+ default:
403
+ throw new Error(`unknown first-party plugin: ${name}`)
404
+ }
405
+ },
406
+ })
407
+ if (loadResult.errors.length > 0 || process.env.NEBULA_DEBUG_PLUGINS) {
408
+ const { writeFile } = await import('node:fs/promises')
409
+ const { join } = await import('node:path')
410
+ await writeFile(
411
+ join(paths.dir, 'plugin-debug.log'),
412
+ JSON.stringify(
413
+ {
414
+ ts: Date.now(),
415
+ pluginNames,
416
+ loadResult,
417
+ registeredTools: tools.list().map(t => t.name),
418
+ },
419
+ null,
420
+ 2,
421
+ ),
422
+ ).catch(() => {})
423
+ }
424
+
425
+ // MCP discovery: scan ~/.nebula/.mcp.json + ~/.claude/.mcp.json + plugin
426
+ // cache, spawn each stdio server, register tools as deferred. Failures are
427
+ // logged but never block startup.
428
+ let mcpManager: McpManager | null = null
429
+ try {
430
+ const { servers } = await discoverMcpServers({
431
+ importsClaudeCode: config.imports?.claudeCode ?? true,
432
+ })
433
+ if (servers.length > 0) {
434
+ mcpManager = new McpManager(servers)
435
+ const mcpResult = await mcpManager.registerAll(def =>
436
+ tools.register(def as Parameters<typeof tools.register>[0]),
437
+ )
438
+ if (mcpResult.failed.length > 0 || process.env.NEBULA_DEBUG_PLUGINS) {
439
+ const { writeFile } = await import('node:fs/promises')
440
+ const { join } = await import('node:path')
441
+ await writeFile(
442
+ join(paths.dir, 'mcp-debug.log'),
443
+ JSON.stringify(
444
+ { ts: Date.now(), servers: servers.map(s => s.name), result: mcpResult },
445
+ null,
446
+ 2,
447
+ ),
448
+ ).catch(() => {})
449
+ }
450
+ }
451
+ } catch {
452
+ // Discovery itself failed (probably I/O); proceed without MCP.
453
+ }
454
+
455
+ // Memory is local-only; the on-chain (iNFT slot) sync was removed. This
456
+ // no-op preserves the per-turn flush call sites; memory persists as files
457
+ // via the memory.* tools.
458
+ const sync = {
459
+ flushTurn: async (): Promise<{ txHash: Hex | null; changedSlots: string[] }> => ({
460
+ txHash: null,
461
+ changedSlots: [],
462
+ }),
463
+ flushAll: async (): Promise<{ txHash: Hex | null; changedSlots: string[] }> => ({
464
+ txHash: null,
465
+ changedSlots: [],
466
+ }),
467
+ }
468
+
469
+ await mkdir(paths.memoryDir, { recursive: true })
470
+ const [memoryIndex, identityText, personaText, scannedSkills] = await Promise.all([
471
+ readIndexFile(paths.memoryIndex).catch(() => null),
472
+ readMemoryFileOrNull(`${paths.memoryDir}/agent/identity.md`),
473
+ readMemoryFileOrNull(`${paths.memoryDir}/agent/persona.md`),
474
+ scanSkills({ importsClaudeCode: config.imports?.claudeCode ?? true }).catch(
475
+ () => [] as SkillRef[],
476
+ ),
477
+ ])
478
+ // Use tools.list() (includes deferred) for guidance lookup — guidance
479
+ // fires per-tool-namespace, not per-prompt-schema. tools.schemas() is the
480
+ // separate set the brain SEES in its prompt; deferred tools stay hidden
481
+ // there until tool.search loads them. But the brain still needs to know
482
+ // they EXIST via guidance, otherwise it never thinks to search.
483
+ const loadedToolNames = tools.list().map(t => t.name)
484
+ const disabledSkillSet = new Set(skillsDisabled.current)
485
+ const skillsRef: { current: SkillRef[] } = {
486
+ current: scannedSkills.filter(s => !disabledSkillSet.has(s.id)),
487
+ }
488
+ const promptAppend = config.prompt?.append ?? null
489
+ // Surface sandbox awareness so the brain doesn't have to empirically discover
490
+ // its container/profile via pwd + ls + uname round-trips. Without it,
491
+ // qwen3.6-plus would hit fs.read('/workspace/X') → ENOENT (fs.* runs on host),
492
+ // sed -i '' (BSD) → fails on Linux GNU sed, and answer "where am I?" only
493
+ // after probing. Each wasted call costs latency + tokens.
494
+ const envInfo = {
495
+ cwd: process.cwd(),
496
+ platform: process.platform,
497
+ sandbox: sandbox.envHint?.() ?? null,
498
+ }
499
+ // Plugin-contributed prompt sections.
500
+ const extraGuidance: string[] = []
501
+ if (onchain) extraGuidance.push(ONCHAIN_GUIDANCE)
502
+ if (telegram) extraGuidance.push(TELEGRAM_GUIDANCE)
503
+
504
+ const buildPrefix = async () => {
505
+ const idx = await readIndexFile(paths.memoryIndex).catch(() => null)
506
+ return buildFrozenPrefix({
507
+ memoryIndex: idx,
508
+ identity: identityText,
509
+ persona: personaText,
510
+ loadedToolNames,
511
+ skills: skillsRef.current,
512
+ promptAppend,
513
+ envInfo,
514
+ extraGuidance,
515
+ })
516
+ }
517
+ const prefix = buildFrozenPrefix({
518
+ memoryIndex,
519
+ identity: identityText,
520
+ persona: personaText,
521
+ loadedToolNames,
522
+ skills: skillsRef.current,
523
+ promptAppend,
524
+ envInfo,
525
+ extraGuidance,
526
+ })
527
+ const activity = new ActivityLog(paths.activityLog)
528
+
529
+ // Brain init must happen BEFORE createCliRenderer. clack/prompts spinner
530
+ // calls setRawMode(false) + stdin.pause() on stop, which undoes the
531
+ // stdin.resume() that opentui's setupTerminal sets up. If brain init
532
+ // (and its spinner) ran AFTER createCliRenderer, the stop would flip
533
+ // stdin back into a state where opentui can't read keypresses, AND the
534
+ // event loop would empty (no stdin keepalive) so the process exits.
535
+ // The fix: every clack interaction finishes before opentui takes the wheel.
536
+ const { render } = await import('@opentui/solid')
537
+ const { createCliRenderer } = await import('@opentui/core')
538
+ const { createChatState } = await import('../ui/state')
539
+ const { ChatApp } = await import('../ui/app')
540
+
541
+ const state = createChatState({
542
+ initialSystem: opts?.yolo
543
+ ? 'connected. YOLO mode: approval prompts disabled.'
544
+ : 'connected. type messages and press enter.',
545
+ // Show the configured agent name when set, else the 16-char agent ID hash.
546
+ // Use the FULL agent EOA (no shortAddr) so operators see the complete
547
+ // address — useful for chain explorers.
548
+ identityLabel: `agent ${agentId} ${agentAddress}`,
549
+ approvalsMode: initialMode,
550
+ // v0.24.4: embedded chat runs in-process on the operator's machine — by
551
+ // definition local. Tag it so the statusbar hides the sandbox-billing
552
+ // segment, matching the standalone-local-gateway path.
553
+ isLocalGateway: true,
554
+ })
555
+
556
+ // Phase 12: now that state exists, point the telegram row sinks at it. The
557
+ // dispatch slot stays null until brain.init resolves below.
558
+ if (telegram) {
559
+ telegramSystemRowSink.current = (text: string) => state.pushRow({ role: 'system', text })
560
+ telegramInboundRowSink.current = (text: string) => state.pushRow({ role: 'inbox-tg', text })
561
+ telegramAssistantRowSink.current = (text: string) =>
562
+ state.pushRow({ role: 'telegram-assistant', text })
563
+ }
564
+
565
+ // Statusline balance refreshers; fired at boot, post-turn, and post-/sync.
566
+ const refreshEoaBalance = () => {
567
+ viemClients.publicClient
568
+ .getBalance({ address: agentAddress })
569
+ .then(wei => state.setEoaBalance(Number(formatEther(wei))))
570
+ .catch(() => {})
571
+ }
572
+ const refreshBalances = () => {
573
+ refreshEoaBalance()
574
+ }
575
+
576
+ permission.setPrompter(req => {
577
+ return new Promise<PermissionDecision>(resolve => {
578
+ // Value-moving onchain ops carry amount/recipient/token so we render a
579
+ // friendlier "send 0.05 Mantle to 0xC635...87Ec" instead of a raw command.
580
+ const detail =
581
+ req.amount !== undefined
582
+ ? `${req.amount}${req.token ? ` ${req.token}` : ''}${req.recipient ? ` to ${req.recipient}` : ''}`
583
+ : (req.command ?? req.path ?? '(?)')
584
+ state.pushRow({
585
+ role: 'system',
586
+ text: `[approval requested] ${req.reason}: ${detail}`,
587
+ })
588
+ state.setPendingApproval({ request: req, resolve })
589
+ })
590
+ })
591
+
592
+ hooks.add<PreToolCallContext, PreToolCallResult>('pre_tool_call', async ({ call }) => {
593
+ const checks = describePermissionCheck(call)
594
+ if (!checks) return undefined
595
+ // Deterministic policy floor: escalate to approval beneath the session mode
596
+ // (even YOLO) when the on-chain policy flags this call as material-risk.
597
+ if (
598
+ !checks.force &&
599
+ policyRequiresApprovalForCall(
600
+ call.name,
601
+ (call.args ?? {}) as Record<string, unknown>,
602
+ policyFromEnv(),
603
+ )
604
+ ) {
605
+ checks.force = true
606
+ }
607
+ const result = await permission.resolve(checks)
608
+ if (result.allowed) return undefined
609
+ return {
610
+ short: {
611
+ ok: false,
612
+ error: `Denied: ${result.reason ?? 'permission check failed'} (mode=${permission.getMode()}). Operator rejected this call. Do NOT retry, instruct another tool, or claim the transaction is queued. Surface the rejection to the operator and ask whether to proceed differently.`,
613
+ },
614
+ }
615
+ })
616
+
617
+ // Skills auto-trigger: when a tool call matches a skill's filePattern or
618
+ // bashPattern, surface a system row so the operator sees the auto-load AND
619
+ // queue the SKILL.md body for next-turn injection via brain.injectContext().
620
+ const pendingSkillInjections = new Set<string>()
621
+ hooks.add<PostToolCallContext, void>('post_tool_call', async ({ call, result }) => {
622
+ if (result.ok === false) return
623
+ const matches = matchSkillTriggers({ name: call.name, args: call.args }, skillsRef.current)
624
+ for (const match of matches) {
625
+ if (pendingSkillInjections.has(match.skill.id)) continue
626
+ pendingSkillInjections.add(match.skill.id)
627
+ state.pushRow({
628
+ role: 'system',
629
+ text: `↳ skill auto-loaded: ${match.skill.id} (matched ${match.reason}). use skills.view to read body.`,
630
+ })
631
+ }
632
+ })
633
+
634
+ const bootSpinner = spinner()
635
+ bootSpinner.start(`Connecting to model ${llmModel}`)
636
+ const persistConversations = config.brain?.persistConversations !== false
637
+ const brain = new OpenAIBrain({
638
+ apiKey: llmApiKey,
639
+ baseUrl: llmBaseUrl,
640
+ model: llmModel,
641
+ tools: tools.schemas(),
642
+ prefix,
643
+ maxOutputTokens: config.brain?.maxOutputTokens,
644
+ compaction:
645
+ config.brain?.compaction === null
646
+ ? null
647
+ : {
648
+ threshold: config.brain?.compaction?.threshold ?? 0.5,
649
+ contextWindow: config.brain?.contextWindow ?? 1_000_000,
650
+ keepRecent: config.brain?.compaction?.keepRecent ?? 8,
651
+ },
652
+ persist: persistConversations
653
+ ? createFsHistoryPersist({ dir: `${paths.dir}/conversations` })
654
+ : undefined,
655
+ onToolCall: async call => {
656
+ state.pushRow({
657
+ role: 'tool-call',
658
+ text: '',
659
+ toolName: call.name,
660
+ args: summarizeArgs(call.args),
661
+ })
662
+ const pre = await hooks.runPreToolCall({ call })
663
+ if (pre.short) {
664
+ await activity.append({
665
+ ts: Date.now(),
666
+ kind: 'tool-call',
667
+ data: { call, result: pre.short, blocked: true },
668
+ })
669
+ state.pushRow({
670
+ role: 'tool-result',
671
+ text: summarizeToolResult(pre.short),
672
+ failed: pre.short.ok === false,
673
+ })
674
+ return { role: 'tool', content: JSON.stringify(pre.short) } as BrainMessage
675
+ }
676
+ const effectiveCall = pre.call ?? call
677
+ const result = await tools.dispatch(effectiveCall)
678
+ await hooks.runPostToolCall({ call: effectiveCall, result })
679
+ await activity.append({
680
+ ts: Date.now(),
681
+ kind: 'tool-call',
682
+ data: { call: effectiveCall, result },
683
+ })
684
+ state.pushRow({
685
+ role: 'tool-result',
686
+ text: summarizeToolResult(result),
687
+ failed: result.ok === false,
688
+ })
689
+ // v0.21.2 R1: deterministic browser.navigate retry when web.fetch hits
690
+ // a bot-block. Mirror block in build-runtime.ts; both share orchestration
691
+ // via runEscalation so any future change lands in one place. Sinks differ:
692
+ // TUI pushes rows here, gateway publishes SSE events.
693
+ const escalation = detectFetchEscalation(effectiveCall, result)
694
+ if (escalation.needed) {
695
+ const merged = await runEscalation(escalation, result, {
696
+ runPreCall: c => hooks.runPreToolCall({ call: c }),
697
+ runPostCall: (c, r) => hooks.runPostToolCall({ call: c, result: r }),
698
+ dispatch: c => tools.dispatch(c),
699
+ appendActivity: (c, r) =>
700
+ activity.append({
701
+ ts: Date.now(),
702
+ kind: 'tool-call',
703
+ data: { call: c, result: r, autoEscalated: true },
704
+ }),
705
+ onStart: c =>
706
+ state.pushRow({
707
+ role: 'tool-call',
708
+ text: '',
709
+ toolName: c.name,
710
+ args: summarizeArgs(c.args),
711
+ autoEscalated: true,
712
+ }),
713
+ onEnd: (_c, r) =>
714
+ state.pushRow({
715
+ role: 'tool-result',
716
+ text: summarizeToolResult(r),
717
+ failed: r.ok === false,
718
+ autoEscalated: true,
719
+ }),
720
+ })
721
+ return { role: 'tool', content: JSON.stringify(merged) } as BrainMessage
722
+ }
723
+ return {
724
+ role: 'tool',
725
+ content: JSON.stringify(result),
726
+ } as BrainMessage
727
+ },
728
+ })
729
+ try {
730
+ await brain.init()
731
+ bootSpinner.stop('Connected')
732
+ } catch (e) {
733
+ bootSpinner.stop(`Connection failed: ${(e as Error).message.slice(0, 120)}`)
734
+ process.exit(1)
735
+ }
736
+
737
+ // Phase 12: brain is up. Wire the deferred TG dispatch slot so any inbound
738
+ // TG message that lands once collectedListeners[i].start() fires below
739
+ // routes through brain.infer with source=telegram.
740
+ if (telegram) {
741
+ const handle = buildTelegramDispatch({
742
+ activity,
743
+ sync,
744
+ permission,
745
+ pushAssistantRow: text => telegramAssistantRowSink.current?.(text),
746
+ pushInboundRow: text => telegramInboundRowSink.current?.(text),
747
+ isBusy: () => state.status() === 'thinking',
748
+ buildPrefix,
749
+ brain,
750
+ setThinking: on => state.setStatus(on ? 'thinking' : 'idle'),
751
+ setActiveAbort: ctrl => state.setActiveAbort(ctrl),
752
+ refreshBalances,
753
+ formatInboundPreview: input =>
754
+ formatTelegramInboundPreview({
755
+ chatId: input.chatId,
756
+ username: input.username,
757
+ displayName: input.displayName,
758
+ text: input.text.replace(/^<channel[^>]*>([\s\S]*)<\/channel>$/, '$1'),
759
+ }),
760
+ approvalBridge: telegramApprovalBridge,
761
+ })
762
+ telegramSlot.current = handle.dispatch
763
+ // Drain queued TG messages whenever the brain returns to idle (closes G4
764
+ // starvation: a stdin turn ending while a TG message was queued used to
765
+ // leave it stuck until the next inbound).
766
+ state.onStatusChange(next => {
767
+ if (next === 'idle' && handle.getQueueSize() > 0) handle.drainQueue()
768
+ })
769
+ }
770
+
771
+ // Initial balances for the status bar (best-effort, never blocks boot).
772
+ refreshBalances()
773
+
774
+ // Redirect noisy SDK chatter (Mantle storage progress, ethers RPC errors) to a
775
+ // log file so it doesn't fall through opentui's alt-screen and pollute the
776
+ // chat UI. Keep process.stdout intact - opentui itself needs to write there.
777
+ const { createWriteStream } = await import('node:fs')
778
+ const chatLog = createWriteStream(`${paths.dir}/chat.log`, { flags: 'a' })
779
+ const stringifyArg = (a: unknown): string => {
780
+ if (typeof a === 'string') return a
781
+ if (a instanceof Error) return a.stack ?? a.message
782
+ try {
783
+ return JSON.stringify(a, (_k, v) => (typeof v === 'bigint' ? `${v}n` : v))
784
+ } catch {
785
+ return String(a)
786
+ }
787
+ }
788
+ const logTo =
789
+ (level: string) =>
790
+ (...args: unknown[]) => {
791
+ const line = args.map(stringifyArg).join(' ')
792
+ chatLog.write(`[${new Date().toISOString()}] [${level}] ${line}\n`)
793
+ }
794
+ console.log = logTo('log') as typeof console.log
795
+ console.warn = logTo('warn') as typeof console.warn
796
+ console.error = logTo('error') as typeof console.error
797
+ console.info = logTo('info') as typeof console.info
798
+ console.debug = logTo('debug') as typeof console.debug
799
+ process.on('unhandledRejection', err => {
800
+ chatLog.write(`[unhandled] ${(err as Error)?.stack ?? String(err)}\n`)
801
+ })
802
+
803
+ const renderer = await createCliRenderer({
804
+ exitOnCtrlC: false,
805
+ consoleMode: 'disabled',
806
+ openConsoleOnError: false,
807
+ })
808
+
809
+ // Listener catch-up + WS subscribe runs in the background. `start` only
810
+ // resolves after catch-up finishes, which can be slow on long-restored
811
+ // agents; awaiting it would block the chat from accepting input.
812
+ for (const l of collectedListeners) {
813
+ l.start(undefined as never).catch(e => {
814
+ state.pushRow({
815
+ role: 'system',
816
+ text: `listener ${l.name} failed to start: ${(e as Error).message.slice(0, 160)}`,
817
+ })
818
+ })
819
+ }
820
+
821
+ const handleSubmit = async (text: string): Promise<void> => {
822
+ const trimmed = text.trim()
823
+ if (trimmed.startsWith('/')) {
824
+ const handled = await handleSlash(trimmed)
825
+ if (handled) {
826
+ // Slash commands skip brain.infer; reset thinking → idle so the
827
+ // spinner row stops. (The keyboard handler in app.tsx flips
828
+ // status='thinking' on every Enter, regardless of payload.)
829
+ state.setStatus('idle')
830
+ return
831
+ }
832
+ }
833
+ // Per-turn AbortController. Esc in the TUI calls .abort() on this.
834
+ // Stored on state so the keyboard handler can reach it from app.tsx.
835
+ const abortCtrl = new AbortController()
836
+ state.setActiveAbort(abortCtrl)
837
+ try {
838
+ // Refresh per-turn user-context (MEMORY.md may have grown last turn).
839
+ // The system prefix stays cached; only the user-msg context updates.
840
+ const refreshed = await buildPrefix()
841
+ brain.refreshUserContext(refreshed)
842
+ await activity.append({
843
+ ts: Date.now(),
844
+ kind: 'wake',
845
+ data: { source: 'stdin', text },
846
+ })
847
+ const turn = await brain.infer({
848
+ event: {
849
+ id: newEventId(),
850
+ source: 'stdin',
851
+ payload: { label: 'user-message', data: text },
852
+ ts: Date.now(),
853
+ },
854
+ channelKey: 'tui:stdin',
855
+ signal: abortCtrl.signal,
856
+ onCompactionEvent: ev => {
857
+ state.pushRow({
858
+ role: 'system',
859
+ text: `✂︎ context compacted (${ev.from} → ${ev.to} messages, ~${Math.round(ev.promptTokens / 1000)}K tokens)`,
860
+ })
861
+ },
862
+ })
863
+ await activity.append({
864
+ ts: Date.now(),
865
+ kind: 'brain-response',
866
+ data: {
867
+ content: turn.content,
868
+ toolCalls: turn.toolCalls.length,
869
+ finishReason: turn.finishReason,
870
+ usage: turn.usage,
871
+ },
872
+ })
873
+ state.pushRow({ role: 'assistant', text: turn.content ?? '(no content)' })
874
+ state.setStatus('idle')
875
+ // Compute ledger drains via inference; agent EOA via tool chain writes.
876
+ refreshBalances()
877
+ if (turn.usage) {
878
+ state.setUsage({
879
+ total: turn.usage.totalTokens,
880
+ cached: turn.usage.cachedTokens,
881
+ })
882
+ }
883
+ // Per-turn auto-sync: upload changed memory + activity-log to Mantle Storage,
884
+ // anchor in iNFT. Fire-and-forget; chat doesn't wait. Errors surface
885
+ // as a system row every turn — repetition is the signal that a real
886
+ // upstream issue persists, not noise to suppress.
887
+ sync
888
+ .flushTurn()
889
+ .then(res => {
890
+ if (res.txHash && res.changedSlots.length > 0) {
891
+ state.pushRow({
892
+ role: 'system',
893
+ text: `synced ${res.changedSlots.join(', ')} → ${explorerTxUrl(config.network, res.txHash)}`,
894
+ })
895
+ }
896
+ })
897
+ .catch(e => {
898
+ state.pushRow({
899
+ role: 'system',
900
+ text: `sync error: ${summarizeError(e)}`,
901
+ })
902
+ })
903
+ } catch (e) {
904
+ // AbortError = operator pressed Esc; render as a clean sys row, NOT an
905
+ // error. The activity log gets a paired entry so the post-mortem reflects
906
+ // operator intent, not a real fault.
907
+ if ((e instanceof Error && e.name === 'AbortError') || abortCtrl.signal.aborted) {
908
+ state.pushRow({
909
+ role: 'system',
910
+ text: 'turn interrupted (esc). brain stopped at the last completed step.',
911
+ })
912
+ await activity.append({
913
+ ts: Date.now(),
914
+ kind: 'brain-response',
915
+ data: { content: '(aborted by operator)', toolCalls: 0, finishReason: 'aborted' },
916
+ })
917
+ state.setStatus('idle')
918
+ return
919
+ }
920
+ // Mirror real errors to chat.log too — render-layer bugs can swallow the
921
+ // sys row before it hits the screen, and chat.log is the only artifact
922
+ // the operator can read post-mortem.
923
+ const errMsg = e instanceof Error ? e.message : String(e ?? 'unknown error')
924
+ const dumped = e instanceof Error ? (e.stack ?? e.message) : errMsg
925
+ console.error('[handleSubmit] error:', dumped)
926
+ state.pushRow({ role: 'system', text: `error: ${errMsg.slice(0, 300)}` })
927
+ state.setStatus('error')
928
+ } finally {
929
+ state.setActiveAbort(null)
930
+ }
931
+ }
932
+
933
+ const handleSlash = async (cmd: string): Promise<boolean> => {
934
+ if (cmd === '/exit' || cmd === '/quit') {
935
+ state.pushRow({ role: 'system', text: 'goodbye.' })
936
+ handleExit()
937
+ return true
938
+ }
939
+ if (cmd === '/model') {
940
+ state.pushRow({
941
+ role: 'system',
942
+ text: 'Switching brain. (Quit chat first; run `nebula model` to pick a new brain, then re-launch `nebula`.)',
943
+ })
944
+ return true
945
+ }
946
+ if (cmd === '/sync') {
947
+ state.pushRow({ role: 'system', text: 'force-syncing memory + activity to Mantle…' })
948
+ try {
949
+ const res = await sync.flushAll()
950
+ if (res.txHash) {
951
+ state.pushRow({
952
+ role: 'system',
953
+ text: `synced ${res.changedSlots.join(', ')} → ${explorerTxUrl(config.network, res.txHash)}`,
954
+ })
955
+ refreshEoaBalance()
956
+ } else {
957
+ state.pushRow({ role: 'system', text: 'nothing to sync (everything up to date)' })
958
+ }
959
+ } catch (e) {
960
+ state.pushRow({ role: 'system', text: `sync error: ${summarizeError(e)}` })
961
+ }
962
+ return true
963
+ }
964
+ if (cmd === '/yolo') {
965
+ const result = applyYolo(permission)
966
+ state.setApprovalsMode(result.mode)
967
+ state.pushRow({ role: 'system', text: result.message })
968
+ return true
969
+ }
970
+ if (cmd === '/perms' || cmd.startsWith('/perms ')) {
971
+ const arg = cmd.split(/\s+/)[1]
972
+ const result = applyPerms(permission, arg)
973
+ state.setApprovalsMode(result.mode)
974
+ state.pushRow({ role: 'system', text: result.message })
975
+ return true
976
+ }
977
+ if (cmd === '/reset') {
978
+ try {
979
+ await brain.clearChannel('tui:stdin')
980
+ state.pushRow({ role: 'system', text: 'conversation reset (TUI channel cleared)' })
981
+ } catch (e) {
982
+ state.pushRow({ role: 'system', text: `reset error: ${summarizeError(e)}` })
983
+ }
984
+ return true
985
+ }
986
+ if (cmd === '/jobs') {
987
+ const tool = tools.find('market.listMyJobs')
988
+ if (!tool) {
989
+ state.pushRow({
990
+ role: 'system',
991
+ text: 'market plugin not loaded; cannot list jobs.',
992
+ })
993
+ return true
994
+ }
995
+ state.pushRow({ role: 'system', text: 'fetching active jobs…' })
996
+ try {
997
+ const res = await tool.handler({ status: 'active', limit: 20 } as never)
998
+ const data = (res as { ok: boolean; data?: { jobs: unknown[] } }).data
999
+ const jobs = (data?.jobs ?? []) as Array<{
1000
+ jobId: string
1001
+ role: string
1002
+ counterparty: string | null
1003
+ amount0g: string
1004
+ status: string
1005
+ }>
1006
+ if (jobs.length === 0) {
1007
+ state.pushRow({ role: 'system', text: 'no active escrow jobs.' })
1008
+ } else {
1009
+ const lines = jobs.map(
1010
+ j =>
1011
+ ` job#${j.jobId} · ${j.role}${j.counterparty ? ` w/ ${shortAddr(j.counterparty)}` : ''} · ${j.amount0g} Mantle · ${j.status}`,
1012
+ )
1013
+ state.pushRow({
1014
+ role: 'system',
1015
+ text: `active jobs (${jobs.length}):\n${lines.join('\n')}`,
1016
+ })
1017
+ }
1018
+ } catch (e) {
1019
+ state.pushRow({ role: 'system', text: `jobs error: ${summarizeError(e)}` })
1020
+ }
1021
+ return true
1022
+ }
1023
+ if (cmd === '/help') {
1024
+ const builtins =
1025
+ " /sync force memory + activity flush to Mantle\n /jobs list active escrow jobs\n /model switch brain (run nebula model after exiting)\n /yolo toggle approval prompts off/on for this session\n /perms <mode> set permission mode (off|prompt|strict); no arg shows current\n /reset clear this channel's conversation history\n /exit quit nebula (drains Mantle storage flush, releases process)\n /help this message"
1026
+ const claudeBlock =
1027
+ commandIndex.size === 0
1028
+ ? ''
1029
+ : `\n\nClaude Code commands (auto-loaded):\n${[
1030
+ ...new Set([...commandIndex.values()].map(c => c.name)),
1031
+ ]
1032
+ .sort()
1033
+ .map(name => {
1034
+ const c = commandIndex.get(name)!
1035
+ return ` /${c.name} ${c.description.slice(0, 80)}`
1036
+ })
1037
+ .join('\n')}`
1038
+ state.pushRow({
1039
+ role: 'system',
1040
+ text: `slash commands:\n${builtins}${claudeBlock}`,
1041
+ })
1042
+ return true
1043
+ }
1044
+ // Claude Code command match. Strip leading `/`, take first whitespace
1045
+ // segment as the command name, treat the rest as the user-supplied args.
1046
+ if (cmd.startsWith('/')) {
1047
+ const rest = cmd.slice(1).trim()
1048
+ if (!rest) return false
1049
+ const space = rest.indexOf(' ')
1050
+ const name = space === -1 ? rest : rest.slice(0, space)
1051
+ const args = space === -1 ? '' : rest.slice(space + 1).trim()
1052
+ const command = commandIndex.get(name)
1053
+ if (!command) return false
1054
+ const trimmedBody = command.body.trim()
1055
+ const inlined = args
1056
+ ? `# Command: /${command.name}${command.argumentHint ? ` (${command.argumentHint})` : ''}\n# User args: ${args}\n\n${trimmedBody}`
1057
+ : `# Command: /${command.name}\n\n${trimmedBody}`
1058
+ state.pushRow({
1059
+ role: 'system',
1060
+ text: `↳ command: /${command.name} (${command.id}, ${command.body.length} bytes inlined as user message)`,
1061
+ })
1062
+ // Send the command body as a user message so the brain executes it.
1063
+ try {
1064
+ const refreshed = await buildPrefix()
1065
+ brain.refreshUserContext(refreshed)
1066
+ const turn = await brain.infer({
1067
+ event: {
1068
+ id: newEventId(),
1069
+ source: 'stdin',
1070
+ payload: { label: 'user-message', data: inlined },
1071
+ ts: Date.now(),
1072
+ },
1073
+ channelKey: 'tui:stdin',
1074
+ })
1075
+ state.pushRow({ role: 'assistant', text: turn.content ?? '(no content)' })
1076
+ state.setStatus('idle')
1077
+ } catch (e) {
1078
+ state.pushRow({
1079
+ role: 'system',
1080
+ text: `command error: ${(e as Error).message.slice(0, 200)}`,
1081
+ })
1082
+ }
1083
+ return true
1084
+ }
1085
+ return false
1086
+ }
1087
+
1088
+ // @opentui/solid's render() resolves once the component mounts; it does not
1089
+ // block. On macOS the renderer's animation loop runs in a worker thread, so
1090
+ // the main thread has no JS task keeping the event loop alive after render
1091
+ // returns. Anchor: a never-resolving promise after render(); handleExit is
1092
+ // the only escape via process.exit.
1093
+ const handleExit = (): void => {
1094
+ try {
1095
+ renderer.destroy()
1096
+ } catch {}
1097
+ try {
1098
+ mcpManager?.closeAll()
1099
+ } catch {}
1100
+ // Best-effort: kill any background processes registered via shell.process.
1101
+ try {
1102
+ const { killAllProcesses } = require('nebula-ai-plugin-system') as {
1103
+ killAllProcesses: () => void
1104
+ }
1105
+ killAllProcesses()
1106
+ } catch {}
1107
+ // Best-effort drain: if a flush is mid-flight, await it. Caps at 30s so
1108
+ // we never hang the CLI on a wedged RPC.
1109
+ Promise.race([sync.flushTurn(), new Promise(r => setTimeout(r, 30_000))]).finally(() =>
1110
+ process.exit(0),
1111
+ )
1112
+ }
1113
+
1114
+ // Map Claude Code commands into SlashCommand shape so the slash
1115
+ // autocomplete popup lists them alongside the bundled registry.
1116
+ const extraSlashCommands = [...new Set([...commandIndex.values()].map(c => c.name))].map(name => {
1117
+ const c = commandIndex.get(name)!
1118
+ return {
1119
+ name: c.name.toLowerCase(),
1120
+ description: c.description ?? `Claude Code command (${c.id})`,
1121
+ surfaces: ['tui'] as ('tui' | 'tg')[],
1122
+ scope: 'local' as const,
1123
+ bypassesBrain: false,
1124
+ argHint: c.argumentHint,
1125
+ }
1126
+ })
1127
+
1128
+ await render(
1129
+ () => (
1130
+ <ChatApp
1131
+ state={state}
1132
+ onSubmit={handleSubmit}
1133
+ onExit={handleExit}
1134
+ extraSlashCommands={extraSlashCommands}
1135
+ />
1136
+ ),
1137
+ renderer,
1138
+ )
1139
+
1140
+ await new Promise<void>(() => {
1141
+ // Block forever; only handleExit (via process.exit) escapes this.
1142
+ })
1143
+ }
1144
+
1145
+ async function runModelPicker(
1146
+ config: NebulaConfig,
1147
+ configPath: string,
1148
+ ): Promise<NebulaConfig | null> {
1149
+ // Nebula uses a fixed OpenAI-compatible model (env-configured); no live catalog.
1150
+ const model = process.env.NEBULA_LLM_MODEL ?? config.brain?.model ?? 'gpt-4o-mini'
1151
+ const updated: NebulaConfig = {
1152
+ ...config,
1153
+ brain: { provider: 'openai-compatible', model },
1154
+ }
1155
+ await writeConfigTs(configPath, updated)
1156
+ return updated
1157
+ }
1158
+
1159
+ /**
1160
+ * Squash a ToolResult down to a single-line summary for the chat row. The TUI
1161
+ * adds the `⎿` indent + color from the role, so this returns just the content:
1162
+ * - failed → the error message (truncated)
1163
+ * - ok+path → the file path the tool acted on
1164
+ * - ok+data → "ok"
1165
+ * - done → "done" (legacy: pre-ok results)
1166
+ */
1167
+ function summarizeToolResult(result: unknown): string {
1168
+ const r = result as { ok?: boolean; error?: string; data?: { path?: string } } | null | undefined
1169
+ if (!r || r.ok === undefined) return 'done'
1170
+ if (r.ok === false) return (r.error ?? 'failed').slice(0, 200)
1171
+ const path = typeof r.data?.path === 'string' ? r.data.path : null
1172
+ return path ? path : 'ok'
1173
+ }
1174
+
1175
+ /**
1176
+ * Squash an Error into a single-line, length-capped string for the TUI.
1177
+ * ethers / viem multi-line stack traces blow up the chat UX otherwise.
1178
+ * Strategy: collapse whitespace, drop everything after the first ` (action=`
1179
+ * marker (where ethers appends transaction blobs), cap at 90 chars so the
1180
+ * row stays on one terminal line in any reasonably-sized pane.
1181
+ */
1182
+ function summarizeError(e: unknown): string {
1183
+ const raw = e instanceof Error ? e.message : String(e)
1184
+ let s = raw.replace(/\s+/g, ' ').trim()
1185
+ const annotIdx = s.indexOf(' (action=')
1186
+ if (annotIdx >= 0) s = s.slice(0, annotIdx)
1187
+ return s.length > 90 ? `${s.slice(0, 87)}...` : s
1188
+ }
1189
+
1190
+ type PermArgs = Record<string, unknown>
1191
+ const _str = (v: unknown): string => (typeof v === 'string' ? v : '')
1192
+ const _strOpt = (v: unknown): string | undefined => (typeof v === 'string' ? v : undefined)
1193
+
1194
+ const PERMISSION_DESCRIBERS: Record<string, (a: PermArgs) => PermissionRequest | null> = {
1195
+ 'shell.run': a => ({
1196
+ kind: 'shell.run',
1197
+ command: _str(a.command),
1198
+ reason: 'shell command execution',
1199
+ }),
1200
+ 'code.execute': a => ({
1201
+ kind: 'code.execute',
1202
+ command: `[${_str(a.language) || '?'}] ${_str(a.code)}`,
1203
+ reason: 'arbitrary code execution',
1204
+ }),
1205
+ 'shell.process_start': a => ({
1206
+ kind: 'shell.process',
1207
+ command: _str(a.command),
1208
+ reason: 'background process start',
1209
+ }),
1210
+ 'shell.process_output': () => null,
1211
+ 'shell.process_list': () => null,
1212
+ 'shell.process_kill': () => null,
1213
+ 'fs.write': a => ({ kind: 'fs.write', path: _str(a.path), reason: 'fs.write request' }),
1214
+ 'fs.patch': a => ({ kind: 'fs.patch', path: _str(a.path), reason: 'fs.patch request' }),
1215
+ // Phase 10: value-moving on-chain tools. Pre-fill amount/recipient/token
1216
+ // so the modal renders "send 0.05 Mantle to 0xC635..." not a raw command.
1217
+ 'chain.send': a => ({
1218
+ kind: 'chain.send',
1219
+ amount: _strOpt(a.amount) ?? '?',
1220
+ recipient: _strOpt(a.to) ?? '?',
1221
+ token: _strOpt(a.token) ?? 'MNT',
1222
+ reason: 'native/ERC-20 transfer',
1223
+ }),
1224
+ 'swap.execute': a => ({
1225
+ kind: 'chain.swap',
1226
+ amount: _strOpt(a.amountIn) ?? '?',
1227
+ token: `${_strOpt(a.tokenIn) ?? '?'}→${_strOpt(a.tokenOut) ?? '?'}`,
1228
+ reason: 'Agni swap execution',
1229
+ }),
1230
+ 'moe.swap': a => ({
1231
+ kind: 'chain.swap',
1232
+ amount: _strOpt(a.amountIn) ?? '?',
1233
+ token: `${_strOpt(a.tokenIn) ?? '?'}→${_strOpt(a.tokenOut) ?? '?'}`,
1234
+ reason: 'Merchant Moe swap execution',
1235
+ }),
1236
+ 'swap.best': a => ({
1237
+ kind: 'chain.swap',
1238
+ amount: _strOpt(a.amountIn) ?? '?',
1239
+ token: `${_strOpt(a.tokenIn) ?? '?'}→${_strOpt(a.tokenOut) ?? '?'}`,
1240
+ reason: 'best-execution swap',
1241
+ }),
1242
+ 'aave.supply': a => ({
1243
+ kind: 'chain.write',
1244
+ command: `aave.supply ${_strOpt(a.amount) ?? '?'} ${_strOpt(a.token) ?? '?'}`,
1245
+ reason: 'supply collateral to Aave V3',
1246
+ }),
1247
+ 'aave.withdraw': a => ({
1248
+ kind: 'chain.write',
1249
+ command: `aave.withdraw ${_strOpt(a.amount) ?? '?'} ${_strOpt(a.token) ?? '?'}`,
1250
+ reason: 'withdraw collateral from Aave V3',
1251
+ }),
1252
+ 'aave.borrow': a => ({
1253
+ kind: 'chain.write',
1254
+ command: `aave.borrow ${_strOpt(a.amount) ?? '?'} ${_strOpt(a.token) ?? '?'}`,
1255
+ reason: 'borrow from Aave V3 (leverage)',
1256
+ }),
1257
+ 'aave.repay': a => ({
1258
+ kind: 'chain.write',
1259
+ command: `aave.repay ${_strOpt(a.amount) ?? '?'} ${_strOpt(a.token) ?? '?'}`,
1260
+ reason: 'repay Aave V3 debt',
1261
+ }),
1262
+ 'chain.wrap': a => ({
1263
+ kind: 'chain.send',
1264
+ amount: _strOpt(a.amount) ?? '?',
1265
+ token: 'MNT→WMNT',
1266
+ reason: 'wrap native to WMNT',
1267
+ }),
1268
+ 'chain.unwrap': a => ({
1269
+ kind: 'chain.send',
1270
+ amount: _strOpt(a.amount) ?? '?',
1271
+ token: 'WMNT→MNT',
1272
+ reason: 'unwrap WMNT to native',
1273
+ }),
1274
+ 'chain.write': a => ({
1275
+ kind: 'chain.write',
1276
+ recipient: _strOpt(a.to) ?? '?',
1277
+ command: _strOpt(a.signature) ?? '?',
1278
+ amount: _strOpt(a.value) ? `${_strOpt(a.value)} wei` : undefined,
1279
+ reason: 'arbitrary state-changing call',
1280
+ }),
1281
+ }
1282
+
1283
+ function describePermissionCheck(call: { name: string; args: unknown }): PermissionRequest | null {
1284
+ const fn = PERMISSION_DESCRIBERS[call.name]
1285
+ return fn ? fn((call.args ?? {}) as PermArgs) : null
1286
+ }
1287
+
1288
+ function summarizeArgs(args: unknown): string {
1289
+ if (typeof args !== 'object' || args === null) return String(args ?? '').slice(0, 60)
1290
+ const entries = Object.entries(args as Record<string, unknown>)
1291
+ return entries
1292
+ .map(([k, v]) => {
1293
+ const s = typeof v === 'string' ? v : JSON.stringify(v)
1294
+ return `${k}=${s.length > 40 ? `${s.slice(0, 40)}…` : s}`
1295
+ })
1296
+ .slice(0, 3)
1297
+ .join(', ')
1298
+ }
1299
+
1300
+ async function readMemoryFileOrNull(path: string): Promise<string | null> {
1301
+ try {
1302
+ const { readFile } = await import('node:fs/promises')
1303
+ return await readFile(path, 'utf8')
1304
+ } catch (e) {
1305
+ if ((e as NodeJS.ErrnoException).code === 'ENOENT') return null
1306
+ throw e
1307
+ }
1308
+ }