typeclaw 0.39.2 → 0.39.4
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.
- package/auth.schema.json +110 -0
- package/package.json +2 -2
- package/secrets.schema.json +110 -0
- package/src/agent/index.ts +39 -18
- package/src/agent/model-fallback.ts +13 -4
- package/src/agent/plugin-tools.ts +12 -12
- package/src/agent/provider-error.ts +36 -1
- package/src/agent/session-origin.ts +2 -0
- package/src/agent/subagents.ts +29 -2
- package/src/agent/throttle-circuit.ts +50 -0
- package/src/agent/todo/store.ts +21 -1
- package/src/agent/tools/channel-reply.ts +18 -5
- package/src/agent/tools/channel-send.ts +13 -2
- package/src/agent/turn-runner.ts +162 -0
- package/src/bundled-plugins/doc-render/render.ts +2 -2
- package/src/bundled-plugins/github-cli-auth/approve-idempotency.ts +40 -18
- package/src/bundled-plugins/memory/README.md +12 -12
- package/src/bundled-plugins/memory/append-tool.ts +58 -6
- package/src/bundled-plugins/memory/dreaming.ts +1 -1
- package/src/bundled-plugins/memory/fragment-parser.ts +10 -3
- package/src/bundled-plugins/memory/load-memory.ts +28 -2
- package/src/bundled-plugins/memory/memory-logger.ts +14 -2
- package/src/bundled-plugins/memory/search-tool.ts +50 -7
- package/src/bundled-plugins/memory/stream-events.ts +23 -0
- package/src/bundled-plugins/memory/vector/hybrid.ts +9 -2
- package/src/bundled-plugins/security/index.ts +24 -1
- package/src/bundled-plugins/security/policies/cron-promotion.ts +94 -62
- package/src/bundled-plugins/security/policies/plugin-addition.ts +2 -4
- package/src/bundled-plugins/security/policies/role-promotion.ts +2 -4
- package/src/channels/adapters/discord-author-resolver.ts +52 -0
- package/src/channels/adapters/discord-bot.ts +1 -1
- package/src/channels/adapters/discord-channel-resolver.ts +44 -0
- package/src/channels/adapters/discord-classify.ts +121 -0
- package/src/channels/adapters/discord-reactions.ts +138 -0
- package/src/channels/adapters/discord.ts +416 -0
- package/src/channels/adapters/github/outbound.ts +23 -4
- package/src/channels/adapters/kakaotalk.ts +8 -1
- package/src/channels/adapters/line.ts +1 -1
- package/src/channels/adapters/slack-author-resolver.ts +54 -0
- package/src/channels/adapters/slack-bot.ts +3 -1
- package/src/channels/adapters/slack-channel-resolver.ts +49 -0
- package/src/channels/adapters/slack-classify.ts +83 -0
- package/src/channels/adapters/slack-reactions.ts +126 -0
- package/src/channels/adapters/slack.ts +416 -0
- package/src/channels/adapters/telegram-bot.ts +2 -1
- package/src/channels/adapters/webex-bot.ts +31 -27
- package/src/channels/adapters/webex-prefetch-limiter.ts +120 -0
- package/src/channels/adapters/webex.ts +81 -31
- package/src/channels/github-review-turn-ledger.ts +32 -0
- package/src/channels/github-verdict-activity.ts +60 -0
- package/src/channels/index.ts +6 -0
- package/src/channels/manager.ts +168 -37
- package/src/channels/pr-verdict-activity-bridge.ts +43 -0
- package/src/channels/router.ts +295 -13
- package/src/channels/schema.ts +4 -0
- package/src/channels/types.ts +25 -2
- package/src/cli/builtins.ts +6 -31
- package/src/cli/channel.ts +170 -18
- package/src/cli/command-meta.ts +93 -0
- package/src/cli/help.ts +24 -0
- package/src/cli/index.ts +45 -10
- package/src/cli/init.ts +2 -0
- package/src/cli/update-check.ts +14 -0
- package/src/cli/update-notify.ts +91 -0
- package/src/cli/update-suppression.ts +19 -0
- package/src/config/channels-mutation.ts +3 -1
- package/src/config/config.ts +78 -1
- package/src/config/index.ts +1 -0
- package/src/container/start.ts +52 -8
- package/src/doctor/channel-checks.ts +57 -3
- package/src/hostd/daemon.ts +77 -31
- package/src/hostd/index.ts +11 -1
- package/src/hostd/paths.ts +4 -0
- package/src/hostd/protocol.ts +15 -2
- package/src/hostd/spawn.ts +53 -19
- package/src/init/cli-version.ts +1 -1
- package/src/init/discord-auth.ts +50 -0
- package/src/init/index.ts +94 -4
- package/src/init/reconcile-plugin-deps.ts +30 -5
- package/src/init/run-owner-claim.ts +2 -0
- package/src/init/slack-auth.ts +51 -0
- package/src/mcp/manager.ts +94 -7
- package/src/mcp/tools.ts +7 -4
- package/src/permissions/permissions.ts +12 -0
- package/src/permissions/resolve.ts +2 -0
- package/src/role-claim/match-rule.ts +2 -0
- package/src/run/codex-fetch-observer.ts +11 -6
- package/src/run/index.ts +74 -29
- package/src/secrets/discord-store.ts +96 -0
- package/src/secrets/oauth-xai.ts +2 -2
- package/src/secrets/schema.ts +34 -0
- package/src/secrets/slack-store.ts +96 -0
- package/src/server/command-runner.ts +21 -0
- package/src/server/index.ts +27 -2
- package/src/skills/typeclaw-channels/SKILL.md +1 -1
- package/src/skills/typeclaw-config/SKILL.md +1 -0
- package/src/skills/typeclaw-cron/SKILL.md +20 -4
- package/src/skills/typeclaw-permissions/SKILL.md +18 -6
- package/src/tui/format.ts +0 -19
- package/src/tui/index.ts +2 -8
- package/src/update/check.ts +192 -0
- package/typeclaw.schema.json +332 -0
package/src/mcp/manager.ts
CHANGED
|
@@ -12,6 +12,8 @@ export type McpRefreshResult = { ok: true; name: string; toolCount: number } | {
|
|
|
12
12
|
|
|
13
13
|
export type McpManager = {
|
|
14
14
|
connectAll(opts?: { signal?: AbortSignal }): Promise<McpConnectResult[]>
|
|
15
|
+
ensureConnected(name: string): Promise<McpConnection | undefined>
|
|
16
|
+
whenInitialConnectSettled(opts?: { timeoutMs?: number }): Promise<void>
|
|
15
17
|
getConnection(name: string): McpConnection | undefined
|
|
16
18
|
listServers(): { name: string; description?: string; connected: boolean; toolCount?: number }[]
|
|
17
19
|
refresh(): Promise<McpRefreshResult[]>
|
|
@@ -31,11 +33,55 @@ export function createMcpManager(
|
|
|
31
33
|
const connect = opts.connect ?? connectMcpServer
|
|
32
34
|
const connections = new Map<string, McpConnection>()
|
|
33
35
|
const toolCounts = new Map<string, number>()
|
|
36
|
+
// In-flight connect promises keyed by server name. Shared by connectAll (boot
|
|
37
|
+
// warm-up) and ensureConnected (lazy, on a tool call) so a tool call racing
|
|
38
|
+
// the warm-up — or two concurrent tool calls — coalesce onto one connect
|
|
39
|
+
// attempt instead of spawning the server twice. connectServer stores the
|
|
40
|
+
// connection on success and clears the entry once settled.
|
|
41
|
+
const inflight = new Map<string, Promise<McpConnectResult>>()
|
|
42
|
+
// The first connectAll() (the boot warm-up). whenInitialConnectSettled awaits
|
|
43
|
+
// it so the catalog render can wait out the warm-up window.
|
|
44
|
+
let initialConnect: Promise<McpConnectResult[]> | null = null
|
|
45
|
+
// Shutdown latch. Because the boot warm-up runs un-awaited, closeAll() can
|
|
46
|
+
// return while a connect is still in flight; without this, that late attempt
|
|
47
|
+
// would cache (and never close) a live connection on an already-closed
|
|
48
|
+
// manager. Once set, a resolving attempt closes its connection instead of
|
|
49
|
+
// caching it, and new ensureConnected() calls are refused.
|
|
50
|
+
let closed = false
|
|
51
|
+
|
|
52
|
+
function connectServer(server: McpServer, signal: AbortSignal | undefined): Promise<McpConnectResult> {
|
|
53
|
+
const established = connections.get(server.name)
|
|
54
|
+
if (established !== undefined) {
|
|
55
|
+
return Promise.resolve({
|
|
56
|
+
ok: true,
|
|
57
|
+
name: server.name,
|
|
58
|
+
connection: established,
|
|
59
|
+
toolCount: toolCounts.get(server.name) ?? 0,
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
const pending = inflight.get(server.name)
|
|
63
|
+
if (pending !== undefined) return pending
|
|
64
|
+
|
|
65
|
+
const attempt = connectOne(server, opts.env, connect, signal).then(async (result) => {
|
|
66
|
+
inflight.delete(server.name)
|
|
67
|
+
if (result.ok) {
|
|
68
|
+
if (closed) {
|
|
69
|
+
await result.connection.close().catch(() => {})
|
|
70
|
+
} else {
|
|
71
|
+
connections.set(result.name, result.connection)
|
|
72
|
+
toolCounts.set(result.name, result.toolCount)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return result
|
|
76
|
+
})
|
|
77
|
+
inflight.set(server.name, attempt)
|
|
78
|
+
return attempt
|
|
79
|
+
}
|
|
34
80
|
|
|
35
81
|
return {
|
|
36
82
|
async connectAll(connectOpts: { signal?: AbortSignal } = {}): Promise<McpConnectResult[]> {
|
|
37
83
|
const firstIndexByName = new Map<string, number>()
|
|
38
|
-
const
|
|
84
|
+
const attempt = Promise.all(
|
|
39
85
|
activeServers.map((server, index): Promise<McpConnectResult> => {
|
|
40
86
|
// The name is the tool namespace and the connections key, so a second
|
|
41
87
|
// server sharing a name would silently shadow the first. Fail the
|
|
@@ -51,15 +97,51 @@ export function createMcpManager(
|
|
|
51
97
|
})
|
|
52
98
|
}
|
|
53
99
|
firstIndexByName.set(server.name, index)
|
|
54
|
-
return
|
|
100
|
+
return connectServer(server, connectOpts.signal)
|
|
55
101
|
}),
|
|
56
102
|
)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
103
|
+
initialConnect ??= attempt
|
|
104
|
+
return attempt
|
|
105
|
+
},
|
|
106
|
+
async ensureConnected(name: string): Promise<McpConnection | undefined> {
|
|
107
|
+
if (closed) return undefined
|
|
108
|
+
const established = connections.get(name)
|
|
109
|
+
if (established !== undefined) return established
|
|
110
|
+
const server = activeServers.find((candidate) => candidate.name === name)
|
|
111
|
+
if (server === undefined) return undefined
|
|
112
|
+
const result = await connectServer(server, undefined)
|
|
113
|
+
return result.ok ? result.connection : undefined
|
|
114
|
+
},
|
|
115
|
+
async whenInitialConnectSettled(readinessOpts: { timeoutMs?: number } = {}): Promise<void> {
|
|
116
|
+
if (initialConnect === null) return
|
|
117
|
+
const settled = initialConnect.then(
|
|
118
|
+
() => {},
|
|
119
|
+
() => {},
|
|
120
|
+
)
|
|
121
|
+
// No timeout (the catalog-render default) means await the real connectAll
|
|
122
|
+
// settle. A fixed cap can't be correct here: a server is cached only after
|
|
123
|
+
// connect() THEN the FULL paginated listTools(), whose total time is bounded
|
|
124
|
+
// only by the server's own per-phase deadlines (connect timeout + a
|
|
125
|
+
// configurable, up-to-10min request timeout, times the number of cursor
|
|
126
|
+
// pages). Any fixed bound would silently drop a healthy slow/paginated
|
|
127
|
+
// server from the first catalog. connectAll settles on its own per those
|
|
128
|
+
// deadlines; infinite pagination is a pre-existing connectAll liveness risk,
|
|
129
|
+
// not introduced here. An explicit timeoutMs is honored for tests/callers.
|
|
130
|
+
const timeoutMs = readinessOpts.timeoutMs
|
|
131
|
+
if (timeoutMs === undefined || timeoutMs <= 0) {
|
|
132
|
+
await settled
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
136
|
+
const timeout = new Promise<void>((resolve) => {
|
|
137
|
+
timer = setTimeout(resolve, timeoutMs)
|
|
138
|
+
timer.unref?.()
|
|
139
|
+
})
|
|
140
|
+
try {
|
|
141
|
+
await Promise.race([settled, timeout])
|
|
142
|
+
} finally {
|
|
143
|
+
if (timer !== undefined) clearTimeout(timer)
|
|
61
144
|
}
|
|
62
|
-
return results
|
|
63
145
|
},
|
|
64
146
|
getConnection(name: string): McpConnection | undefined {
|
|
65
147
|
return connections.get(name)
|
|
@@ -92,9 +174,14 @@ export function createMcpManager(
|
|
|
92
174
|
)
|
|
93
175
|
},
|
|
94
176
|
async closeAll(): Promise<void> {
|
|
177
|
+
// Latch shutdown first so any connect still in flight (the un-awaited boot
|
|
178
|
+
// warm-up) closes its own connection on resolve instead of caching it.
|
|
179
|
+
closed = true
|
|
95
180
|
await Promise.allSettled([...connections.values()].map((connection) => connection.close()))
|
|
96
181
|
connections.clear()
|
|
97
182
|
toolCounts.clear()
|
|
183
|
+
inflight.clear()
|
|
184
|
+
initialConnect = null
|
|
98
185
|
},
|
|
99
186
|
}
|
|
100
187
|
}
|
package/src/mcp/tools.ts
CHANGED
|
@@ -27,7 +27,7 @@ function createListToolsTool(manager: McpManager): Tool<McpListToolsArgs> {
|
|
|
27
27
|
server: z.string().describe('The MCP server name from the system prompt catalog.'),
|
|
28
28
|
}),
|
|
29
29
|
async execute(args) {
|
|
30
|
-
const connection = manager.
|
|
30
|
+
const connection = await manager.ensureConnected(args.server)
|
|
31
31
|
if (connection === undefined) return textResult(unknownServerMessage(manager, args.server))
|
|
32
32
|
|
|
33
33
|
const tools = await safeListTools(connection)
|
|
@@ -51,7 +51,7 @@ function createDescribeTool(manager: McpManager): Tool<McpDescribeArgs> {
|
|
|
51
51
|
}),
|
|
52
52
|
async execute(args) {
|
|
53
53
|
const resolved = resolveToolArgs(args.server, args.tool)
|
|
54
|
-
const connection = manager.
|
|
54
|
+
const connection = await manager.ensureConnected(resolved.server)
|
|
55
55
|
if (connection === undefined) return textResult(unknownServerMessage(manager, resolved.server))
|
|
56
56
|
|
|
57
57
|
const tools = await safeListTools(connection)
|
|
@@ -89,7 +89,7 @@ function createCallTool(manager: McpManager): Tool<McpCallArgs> {
|
|
|
89
89
|
}),
|
|
90
90
|
async execute(args) {
|
|
91
91
|
const resolved = resolveToolArgs(args.server, args.tool)
|
|
92
|
-
const connection = manager.
|
|
92
|
+
const connection = await manager.ensureConnected(resolved.server)
|
|
93
93
|
if (connection === undefined) return textResult(unknownServerMessage(manager, resolved.server))
|
|
94
94
|
|
|
95
95
|
const result = await safeCallTool(connection, resolved.tool, args.args ?? {})
|
|
@@ -104,9 +104,12 @@ function resolveToolArgs(server: string, tool: string): { server: string; tool:
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
function unknownServerMessage(manager: McpManager, server: string): string {
|
|
107
|
+
// List every configured server, not just connected ones: under lazy connect a
|
|
108
|
+
// valid server is reached only when ensureConnected returns undefined for a
|
|
109
|
+
// name that isn't configured at all, so filtering by `connected` here would
|
|
110
|
+
// report "none" while servers exist.
|
|
107
111
|
const available = manager
|
|
108
112
|
.listServers()
|
|
109
|
-
.filter((entry) => entry.connected)
|
|
110
113
|
.map((entry) => entry.name)
|
|
111
114
|
.join(', ')
|
|
112
115
|
return `Unknown MCP server ${JSON.stringify(server)}. Available servers: ${available || 'none'}.`
|
|
@@ -15,6 +15,14 @@ export type PermissionService = {
|
|
|
15
15
|
// either side and MUST be treated as deny, never allow — mistreating it
|
|
16
16
|
// as allow reopens the privilege-escalation hole this gate closes.
|
|
17
17
|
compareRoleSeverity(a: string, b: string): -1 | 0 | 1 | undefined
|
|
18
|
+
// Resolved (wildcard-expanded) permission set for a role NAME, or
|
|
19
|
+
// `undefined` for an unknown role. Unlike `describe`, which takes an
|
|
20
|
+
// origin, this answers "what can role X do" directly — needed for
|
|
21
|
+
// capability-dominance checks (e.g. "may the caller schedule deferred
|
|
22
|
+
// work as role X") that cannot use the coarse `compareRoleSeverity`
|
|
23
|
+
// tower, since custom roles are arbitrary permission bags with no
|
|
24
|
+
// inherent ordering. Callers MUST treat `undefined` as deny.
|
|
25
|
+
permissionsForRole(role: string): readonly string[] | undefined
|
|
18
26
|
// Rebuilds the resolved role table from the given roles config, preserving
|
|
19
27
|
// the same plugin-permission set captured at construction time. Used by
|
|
20
28
|
// the config reloadable so role match-rule edits (typeclaw role claim,
|
|
@@ -43,6 +51,7 @@ export const noopPermissionService: PermissionService = {
|
|
|
43
51
|
has: () => false,
|
|
44
52
|
resolveRole: () => 'guest',
|
|
45
53
|
compareRoleSeverity: () => undefined,
|
|
54
|
+
permissionsForRole: () => undefined,
|
|
46
55
|
describe: () => ({ role: 'guest', permissions: [] }),
|
|
47
56
|
replaceRoles: () => {},
|
|
48
57
|
replacePluginPermissions: () => {},
|
|
@@ -193,6 +202,9 @@ export function createPermissionService(opts: CreatePermissionServiceOptions = {
|
|
|
193
202
|
if (aRank > bRank) return 1
|
|
194
203
|
return 0
|
|
195
204
|
},
|
|
205
|
+
permissionsForRole(role) {
|
|
206
|
+
return byName.get(role)?.permissions
|
|
207
|
+
},
|
|
196
208
|
describe(origin) {
|
|
197
209
|
const name = resolveRole(origin)
|
|
198
210
|
const role = byName.get(name)
|
|
@@ -4,7 +4,9 @@ import type { AdapterId } from '@/channels/schema'
|
|
|
4
4
|
import type { MatchRule, Platform } from './match-rule'
|
|
5
5
|
|
|
6
6
|
const ADAPTER_TO_PLATFORM: Record<AdapterId, Platform> = {
|
|
7
|
+
slack: 'slack',
|
|
7
8
|
'slack-bot': 'slack',
|
|
9
|
+
discord: 'discord',
|
|
8
10
|
'discord-bot': 'discord',
|
|
9
11
|
github: 'github',
|
|
10
12
|
'telegram-bot': 'telegram',
|
|
@@ -29,7 +29,9 @@ const ADAPTER_TO_PLATFORM: Record<
|
|
|
29
29
|
ChannelKey['adapter'],
|
|
30
30
|
'slack' | 'discord' | 'telegram' | 'webex' | 'kakao' | 'line' | 'github'
|
|
31
31
|
> = {
|
|
32
|
+
slack: 'slack',
|
|
32
33
|
'slack-bot': 'slack',
|
|
34
|
+
discord: 'discord',
|
|
33
35
|
'discord-bot': 'discord',
|
|
34
36
|
github: 'github',
|
|
35
37
|
'telegram-bot': 'telegram',
|
|
@@ -42,11 +42,16 @@ export type CodexFetchObserverOptions = {
|
|
|
42
42
|
// request occupied 901s before Bun's OS socket deadline fired). On expiry the
|
|
43
43
|
// request is aborted with a retryable error, so this also bounds how long a
|
|
44
44
|
// user waits before the retry fires — keeping it low is a UX requirement, not
|
|
45
|
-
// just a safety net. Default
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
45
|
+
// just a safety net. Default 300_000 ms (raised from 120_000): the 120s cap
|
|
46
|
+
// was tuned against a light-turn sample (slowest healthy ~45s, p99 ~30s) and
|
|
47
|
+
// aborted heavy reasoning turns that were still legitimately trickling bytes
|
|
48
|
+
// — PR reviews, the `dreaming` memory consolidation, and long channel threads
|
|
49
|
+
// routinely run a slow-trickle stream past 2min (observed total_ms=120009 with
|
|
50
|
+
// body_bytes>700k on otherwise-progressing turns). Those are NOT the silent
|
|
51
|
+
// hang this timer exists to catch; the sliding `idleMs` (still 120s) already
|
|
52
|
+
// bounds genuine dead air per-chunk, so the wall-clock ceiling only needs to
|
|
53
|
+
// stop a never-terminating stream. 300s caps a real hang at ~5min while giving
|
|
54
|
+
// heavy turns the headroom they need. Set to 0 to disable just this timer.
|
|
50
55
|
overallMs?: number
|
|
51
56
|
// Schedule fn for tests. Receives (delayMs, callback) and returns a handle
|
|
52
57
|
// the wrapper can pass to `clear`. Default: `setTimeout`/`clearTimeout`.
|
|
@@ -67,7 +72,7 @@ const ENV_IDLE_MS = 'TYPECLAW_CODEX_IDLE_MS'
|
|
|
67
72
|
const ENV_OVERALL_MS = 'TYPECLAW_CODEX_OVERALL_MS'
|
|
68
73
|
const DEFAULT_TTFB_MS = 15_000
|
|
69
74
|
const DEFAULT_IDLE_MS = 120_000
|
|
70
|
-
const DEFAULT_OVERALL_MS =
|
|
75
|
+
const DEFAULT_OVERALL_MS = 300_000
|
|
71
76
|
const LOG_PREFIX = '[codex-fetch]'
|
|
72
77
|
|
|
73
78
|
const defaultScheduler: TimeoutScheduler = {
|
package/src/run/index.ts
CHANGED
|
@@ -28,19 +28,15 @@ import {
|
|
|
28
28
|
createChannelManager,
|
|
29
29
|
createChannelsReloadable,
|
|
30
30
|
createGithubTokenBridge,
|
|
31
|
+
createPrVerdictActivityBridge,
|
|
31
32
|
createSubagentCompletionBridge,
|
|
33
|
+
setReviewObserver,
|
|
32
34
|
type ChannelManager,
|
|
35
|
+
type PrVerdictActivityBridge,
|
|
33
36
|
type SubagentCompletionBridge,
|
|
34
37
|
} from '@/channels'
|
|
35
38
|
import { createTunnelBridge, type TunnelBridge } from '@/channels/tunnel-bridge'
|
|
36
|
-
import {
|
|
37
|
-
createConfigReloadable,
|
|
38
|
-
getConfig,
|
|
39
|
-
loadConfigSync,
|
|
40
|
-
loadPluginConfigsSync,
|
|
41
|
-
reloadConfig,
|
|
42
|
-
withDefaultPlugins,
|
|
43
|
-
} from '@/config'
|
|
39
|
+
import { createConfigReloadable, getConfig, loadConfigBundleSync, reloadConfig, withDefaultPlugins } from '@/config'
|
|
44
40
|
import {
|
|
45
41
|
type CountStore,
|
|
46
42
|
type CronConsumer,
|
|
@@ -161,23 +157,36 @@ export async function startAgent({
|
|
|
161
157
|
const tuiToken = process.env.TYPECLAW_TUI_TOKEN
|
|
162
158
|
const tuiTokenOpt = tuiToken !== undefined && tuiToken !== '' ? { tuiToken } : {}
|
|
163
159
|
|
|
164
|
-
const pluginConfigsByName =
|
|
160
|
+
const { config: cwdConfig, pluginConfigs: pluginConfigsByName } = loadConfigBundleSync(cwd)
|
|
165
161
|
// Vector agents omit the system-prompt `# Memory` section and inject memory
|
|
166
162
|
// per-turn instead. Derived once here: `memory.vector.enabled` is
|
|
167
163
|
// restart-required, so a single boot read is coherent for the process.
|
|
168
164
|
const suppressSystemMemory = vectorEnabledFromMemoryConfig(pluginConfigsByName.memory)
|
|
169
|
-
const cwdConfig = loadConfigSync(cwd)
|
|
170
165
|
const githubTokenBridge = createGithubTokenBridge()
|
|
171
166
|
const mcpManager =
|
|
172
167
|
cwdConfig.mcpServers.length > 0 ? createMcpManager(cwdConfig.mcpServers, { env: process.env }) : null
|
|
168
|
+
const mcpManagerOpt = mcpManager !== null ? { mcpManager } : {}
|
|
169
|
+
|
|
170
|
+
// Warm up MCP connections in the BACKGROUND so boot doesn't block on each
|
|
171
|
+
// server's subprocess spawn + listTools() (worst case the 15s connect
|
|
172
|
+
// timeout). Tool calls lazily ensureConnected() and the catalog render awaits
|
|
173
|
+
// whenInitialConnectSettled(), so correctness never depends on this finishing
|
|
174
|
+
// first. closeAll() below cleans these up if boot aborts.
|
|
173
175
|
if (mcpManager !== null) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
void mcpManager.connectAll().then((results) => {
|
|
177
|
+
for (const result of results) {
|
|
178
|
+
if (!result.ok) console.warn(`[mcp] ${result.name} failed to connect: ${result.error.message}`)
|
|
179
|
+
}
|
|
180
|
+
})
|
|
178
181
|
}
|
|
179
|
-
|
|
180
|
-
|
|
182
|
+
|
|
183
|
+
// loadPlugins and the vector startup unit have no mutual data dependency, so
|
|
184
|
+
// run them concurrently. loadPlugins stays UNCAUGHT so a bundled-plugin or
|
|
185
|
+
// plugin-security failure still aborts boot; when it does, close the background
|
|
186
|
+
// MCP warm-up so its servers can't leak — startAgent returns no stop() handler
|
|
187
|
+
// on this path, so this is the only cleanup chance. closeAll() also closes any
|
|
188
|
+
// connection the warm-up establishes after shutdown begins.
|
|
189
|
+
const pluginsLoadedPromise = loadPlugins({
|
|
181
190
|
entries: withDefaultPlugins(cwdConfig.plugins),
|
|
182
191
|
agentDir: cwd,
|
|
183
192
|
configsByName: pluginConfigsByName,
|
|
@@ -186,6 +195,15 @@ export async function startAgent({
|
|
|
186
195
|
hasGithubAppTokenResolver: githubTokenBridge.hasAppTokenResolver,
|
|
187
196
|
...(cwdConfig.roles !== undefined ? { roles: cwdConfig.roles } : {}),
|
|
188
197
|
})
|
|
198
|
+
const vectorStartupPromise = suppressSystemMemory ? runVectorStartup(cwd) : Promise.resolve()
|
|
199
|
+
let pluginsLoaded: LoadPluginsResult
|
|
200
|
+
try {
|
|
201
|
+
const [loaded] = await Promise.all([pluginsLoadedPromise, vectorStartupPromise])
|
|
202
|
+
pluginsLoaded = loaded
|
|
203
|
+
} catch (err) {
|
|
204
|
+
if (mcpManager !== null) await mcpManager.closeAll()
|
|
205
|
+
throw err
|
|
206
|
+
}
|
|
189
207
|
|
|
190
208
|
reloadRegistry.register(
|
|
191
209
|
createConfigReloadable({
|
|
@@ -224,19 +242,6 @@ export async function startAgent({
|
|
|
224
242
|
// v2-only parser rejects the file and hydrate below sees no channels. Runs
|
|
225
243
|
// exactly once per folder; a folder already at v2 is a no-op.
|
|
226
244
|
runStartupMigrations(cwd)
|
|
227
|
-
if (suppressSystemMemory) {
|
|
228
|
-
await buildStartupVectorIndex(cwd, embed).catch((err) => {
|
|
229
|
-
console.warn(`[vector] startup index build failed: ${err instanceof Error ? err.message : String(err)}`)
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
// Warm the embedder now (even when the index needed no rebuild above, which
|
|
233
|
-
// skips embed() entirely) so the first channel turn's query embed doesn't
|
|
234
|
-
// pay the one-time ONNX init on its critical path. Non-fatal: a failure here
|
|
235
|
-
// degrades to the per-turn lazy load, same as before this step existed.
|
|
236
|
-
await warmEmbedder().catch((err) => {
|
|
237
|
-
console.warn(`[vector] embedder warm-up failed: ${err instanceof Error ? err.message : String(err)}`)
|
|
238
|
-
})
|
|
239
|
-
}
|
|
240
245
|
|
|
241
246
|
// Channel adapters read `process.env[TOKEN_ENV]` (see channels/manager.ts).
|
|
242
247
|
// Hydrate fills any unset env var from secrets.json#channels via env-wins:
|
|
@@ -319,6 +324,14 @@ export async function startAgent({
|
|
|
319
324
|
claimHandler: claimController.claimHandler,
|
|
320
325
|
githubTokenBridge,
|
|
321
326
|
stream,
|
|
327
|
+
newestRunningChildSubagentStartedAt: (sessionId) =>
|
|
328
|
+
liveSubagentRegistry
|
|
329
|
+
.list({ parentSessionId: sessionId })
|
|
330
|
+
.reduce<number | null>(
|
|
331
|
+
(newest, child) =>
|
|
332
|
+
child.status === 'running' && (newest === null || child.startedAt > newest) ? child.startedAt : newest,
|
|
333
|
+
null,
|
|
334
|
+
),
|
|
322
335
|
onReload: async () => {
|
|
323
336
|
const { results } = await reloadRegistry.reloadAll()
|
|
324
337
|
return formatChannelReloadSummary(results)
|
|
@@ -657,6 +670,22 @@ export async function startAgent({
|
|
|
657
670
|
router: channelManager.router,
|
|
658
671
|
})
|
|
659
672
|
|
|
673
|
+
// Fan a landed formal review verdict out to the sibling sessions reviewing the
|
|
674
|
+
// same PR so they stand down from a redundant verdict (the per-thread fan-out
|
|
675
|
+
// duplicate-review fix). The github-cli-auth plugin records the verdict in the
|
|
676
|
+
// turn-ledger but has no stream access, so the ledger's review observer is the
|
|
677
|
+
// seam: it publishes onto the broadcast bus here, and the bridge routes it.
|
|
678
|
+
const prVerdictActivityBridge: PrVerdictActivityBridge = createPrVerdictActivityBridge({
|
|
679
|
+
stream,
|
|
680
|
+
router: channelManager.router,
|
|
681
|
+
})
|
|
682
|
+
setReviewObserver((review) => {
|
|
683
|
+
stream.publish({
|
|
684
|
+
target: { kind: 'broadcast' },
|
|
685
|
+
payload: { kind: 'pr.verdict-activity', ...review },
|
|
686
|
+
})
|
|
687
|
+
})
|
|
688
|
+
|
|
660
689
|
reloadRegistry.register(createChannelsReloadable({ manager: channelManager }))
|
|
661
690
|
|
|
662
691
|
// Two-phase channel restart-resume around adapter startup, to close the race
|
|
@@ -853,6 +882,8 @@ export async function startAgent({
|
|
|
853
882
|
void disposeMaterializedSkills(pluginRuntime)
|
|
854
883
|
tunnelBridge.stop()
|
|
855
884
|
subagentCompletionBridge.stop()
|
|
885
|
+
prVerdictActivityBridge.stop()
|
|
886
|
+
setReviewObserver(null)
|
|
856
887
|
await tunnelManager.stop()
|
|
857
888
|
await channelManager.stop()
|
|
858
889
|
await mcpManager?.closeAll()
|
|
@@ -895,6 +926,20 @@ export async function startAgent({
|
|
|
895
926
|
}
|
|
896
927
|
}
|
|
897
928
|
|
|
929
|
+
async function runVectorStartup(cwd: string): Promise<void> {
|
|
930
|
+
await buildStartupVectorIndex(cwd, embed).catch((err) => {
|
|
931
|
+
console.warn(`[vector] startup index build failed: ${err instanceof Error ? err.message : String(err)}`)
|
|
932
|
+
})
|
|
933
|
+
|
|
934
|
+
// Warm the embedder after the index pass (even when the index needed no
|
|
935
|
+
// rebuild above, which skips embed() entirely) so the first channel turn's
|
|
936
|
+
// query embed doesn't pay the one-time ONNX init on its critical path.
|
|
937
|
+
// Non-fatal: a failure here degrades to the per-turn lazy load.
|
|
938
|
+
await warmEmbedder().catch((err) => {
|
|
939
|
+
console.warn(`[vector] embedder warm-up failed: ${err instanceof Error ? err.message : String(err)}`)
|
|
940
|
+
})
|
|
941
|
+
}
|
|
942
|
+
|
|
898
943
|
function buildLocalTuiUrl(port: number, token: string | null): string {
|
|
899
944
|
if (token === null) return `ws://localhost:${port}`
|
|
900
945
|
const url = new URL(`ws://localhost:${port}`)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { sendHttp } from '@/hostd/client'
|
|
2
|
+
|
|
3
|
+
import { type DiscordAccountRecord, type DiscordChannelBlock, discordChannelBlockSchema } from './schema'
|
|
4
|
+
import { SecretsBackend } from './storage'
|
|
5
|
+
|
|
6
|
+
export type SetDiscordAccountInput = DiscordAccountRecord
|
|
7
|
+
|
|
8
|
+
export type SecretsDiscordCredentialStoreOptions =
|
|
9
|
+
| { mode: 'host'; secretsPath: string }
|
|
10
|
+
| { mode: 'container'; secretsPath: string; hostdUrl: string; restartToken: string; containerName: string }
|
|
11
|
+
|
|
12
|
+
const EMPTY_BLOCK: DiscordChannelBlock = { currentAccount: null, accounts: {} }
|
|
13
|
+
|
|
14
|
+
export class SecretsDiscordCredentialStore {
|
|
15
|
+
private readonly backend: SecretsBackend
|
|
16
|
+
private writeChain: Promise<void> = Promise.resolve()
|
|
17
|
+
|
|
18
|
+
constructor(private readonly options: SecretsDiscordCredentialStoreOptions) {
|
|
19
|
+
this.backend = new SecretsBackend(options.secretsPath)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async getAccount(id?: string): Promise<DiscordAccountRecord | null> {
|
|
23
|
+
const block = this.readBlock()
|
|
24
|
+
const accountId = id ?? block.currentAccount
|
|
25
|
+
if (!accountId) return null
|
|
26
|
+
return block.accounts[accountId] ?? null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async setAccount(account: SetDiscordAccountInput): Promise<void> {
|
|
30
|
+
await this.writeBlock((block) => {
|
|
31
|
+
const accounts = { ...block.accounts, [account.account_id]: account }
|
|
32
|
+
return { ...block, currentAccount: block.currentAccount ?? account.account_id, accounts }
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async removeAccount(id: string): Promise<void> {
|
|
37
|
+
await this.writeBlock((block) => {
|
|
38
|
+
const accounts = { ...block.accounts }
|
|
39
|
+
delete accounts[id]
|
|
40
|
+
const currentAccount = block.currentAccount === id ? (Object.keys(accounts)[0] ?? null) : block.currentAccount
|
|
41
|
+
return { ...block, currentAccount, accounts }
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async listAccounts(): Promise<Array<DiscordAccountRecord & { is_current: boolean }>> {
|
|
46
|
+
const block = this.readBlock()
|
|
47
|
+
return Object.values(block.accounts).map((account) => ({
|
|
48
|
+
...account,
|
|
49
|
+
is_current: account.account_id === block.currentAccount,
|
|
50
|
+
}))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async setCurrentAccount(id: string): Promise<void> {
|
|
54
|
+
await this.writeBlock((block) => ({ ...block, currentAccount: id }))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private readBlock(): DiscordChannelBlock {
|
|
58
|
+
const channels =
|
|
59
|
+
this.options.mode === 'container' ? this.backend.tryReadChannelsSync() : this.backend.readChannelsSync()
|
|
60
|
+
return parseBlock(channels?.discord)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async writeBlock(update: (current: DiscordChannelBlock) => DiscordChannelBlock): Promise<void> {
|
|
64
|
+
return this.enqueueWrite(async () => {
|
|
65
|
+
if (this.options.mode === 'container') {
|
|
66
|
+
const next = update(this.readBlock())
|
|
67
|
+
const response = await sendHttp(
|
|
68
|
+
{
|
|
69
|
+
kind: 'secrets-patch',
|
|
70
|
+
containerName: this.options.containerName,
|
|
71
|
+
patch: { channels: { discord: next } },
|
|
72
|
+
},
|
|
73
|
+
{ url: this.options.hostdUrl, token: this.options.restartToken },
|
|
74
|
+
)
|
|
75
|
+
if (!response.ok) throw new Error(`secrets-patch failed: ${response.reason}`)
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
await this.backend.updateChannelsAsync(async (channels) => {
|
|
80
|
+
const next = { ...channels, discord: update(parseBlock(channels.discord)) }
|
|
81
|
+
return { result: undefined, next }
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private enqueueWrite(op: () => Promise<void>): Promise<void> {
|
|
87
|
+
const next = this.writeChain.then(op, op)
|
|
88
|
+
this.writeChain = next.catch(() => {})
|
|
89
|
+
return next
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function parseBlock(value: unknown): DiscordChannelBlock {
|
|
94
|
+
if (value === undefined) return EMPTY_BLOCK
|
|
95
|
+
return discordChannelBlockSchema.parse(value)
|
|
96
|
+
}
|
package/src/secrets/oauth-xai.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createServer, type Server } from 'node:http'
|
|
2
2
|
|
|
3
|
-
import { registerOAuthProvider } from '@mariozechner/pi-ai/oauth'
|
|
3
|
+
import { getOAuthProvider, registerOAuthProvider } from '@mariozechner/pi-ai/oauth'
|
|
4
4
|
import type { OAuthCredentials, OAuthLoginCallbacks, OAuthProviderInterface } from '@mariozechner/pi-ai/oauth'
|
|
5
5
|
|
|
6
6
|
// xAI (Grok) OAuth 2.0. xAI runs a standard OIDC authorization server at
|
|
@@ -336,7 +336,7 @@ let registered = false
|
|
|
336
336
|
// so the provider is always present before `AuthStorage.login()` /
|
|
337
337
|
// `getApiKey()` look it up via `getOAuthProvider('xai')`.
|
|
338
338
|
export function registerXaiOAuthProvider(): void {
|
|
339
|
-
if (registered) return
|
|
339
|
+
if (registered && getOAuthProvider(XAI_OAUTH_PROVIDER_ID)) return
|
|
340
340
|
registerOAuthProvider(xaiOAuthProvider)
|
|
341
341
|
registered = true
|
|
342
342
|
}
|
package/src/secrets/schema.ts
CHANGED
|
@@ -35,6 +35,19 @@ const discordBotChannelSchema = z.object({
|
|
|
35
35
|
token: secretFieldSchema.optional(),
|
|
36
36
|
})
|
|
37
37
|
|
|
38
|
+
export const discordAccountRecordSchema = z.object({
|
|
39
|
+
account_id: z.string(),
|
|
40
|
+
token: z.string(),
|
|
41
|
+
username: z.string().optional(),
|
|
42
|
+
created_at: z.string(),
|
|
43
|
+
updated_at: z.string(),
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
export const discordChannelBlockSchema = z.object({
|
|
47
|
+
currentAccount: z.string().nullable(),
|
|
48
|
+
accounts: z.record(z.string(), discordAccountRecordSchema),
|
|
49
|
+
})
|
|
50
|
+
|
|
38
51
|
const telegramBotChannelSchema = z.object({
|
|
39
52
|
token: secretFieldSchema.optional(),
|
|
40
53
|
})
|
|
@@ -144,6 +157,21 @@ export const webexChannelBlockSchema = z.object({
|
|
|
144
157
|
accounts: z.record(z.string(), webexAccountRecordSchema),
|
|
145
158
|
})
|
|
146
159
|
|
|
160
|
+
export const slackAccountRecordSchema = z.object({
|
|
161
|
+
account_id: z.string(),
|
|
162
|
+
token: z.string(),
|
|
163
|
+
cookie: z.string(),
|
|
164
|
+
workspace_id: z.string(),
|
|
165
|
+
workspace_name: z.string().optional(),
|
|
166
|
+
created_at: z.string(),
|
|
167
|
+
updated_at: z.string(),
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
export const slackChannelBlockSchema = z.object({
|
|
171
|
+
currentAccount: z.string().nullable(),
|
|
172
|
+
accounts: z.record(z.string(), slackAccountRecordSchema),
|
|
173
|
+
})
|
|
174
|
+
|
|
147
175
|
export const kakaoPendingLoginRecordSchema = z.object({
|
|
148
176
|
device_uuid: z.string(),
|
|
149
177
|
device_type: z.union([z.literal('pc'), z.literal('tablet')]),
|
|
@@ -160,12 +188,14 @@ export const kakaoChannelBlockSchema = z.object({
|
|
|
160
188
|
export const channelsSchema = z
|
|
161
189
|
.object({
|
|
162
190
|
'slack-bot': slackBotChannelSchema.optional(),
|
|
191
|
+
discord: discordChannelBlockSchema.optional(),
|
|
163
192
|
'discord-bot': discordBotChannelSchema.optional(),
|
|
164
193
|
github: githubChannelSchema.optional(),
|
|
165
194
|
'telegram-bot': telegramBotChannelSchema.optional(),
|
|
166
195
|
line: lineChannelBlockSchema.optional(),
|
|
167
196
|
kakaotalk: kakaoChannelBlockSchema.optional(),
|
|
168
197
|
webex: webexChannelBlockSchema.optional(),
|
|
198
|
+
slack: slackChannelBlockSchema.optional(),
|
|
169
199
|
})
|
|
170
200
|
.catchall(z.unknown())
|
|
171
201
|
|
|
@@ -186,6 +216,8 @@ export type Channels = z.infer<typeof channelsSchema>
|
|
|
186
216
|
export type GithubPatAuthBlock = z.infer<typeof githubPatAuthSchema>
|
|
187
217
|
export type GithubAppAuthBlock = z.infer<typeof githubAppAuthSchema>
|
|
188
218
|
export type GithubSecretsBlock = z.infer<typeof githubChannelSchema>
|
|
219
|
+
export type DiscordAccountRecord = z.infer<typeof discordAccountRecordSchema>
|
|
220
|
+
export type DiscordChannelBlock = z.infer<typeof discordChannelBlockSchema>
|
|
189
221
|
export type LineAccountRecord = z.infer<typeof lineAccountRecordSchema>
|
|
190
222
|
export type LineChannelBlock = z.infer<typeof lineChannelBlockSchema>
|
|
191
223
|
export type KakaoAccountRecord = z.infer<typeof kakaoAccountRecordSchema>
|
|
@@ -194,6 +226,8 @@ export type KakaoChannelBlock = z.infer<typeof kakaoChannelBlockSchema>
|
|
|
194
226
|
export type WebexAccountRecord = z.infer<typeof webexAccountRecordSchema>
|
|
195
227
|
export type WebexChannelBlock = z.infer<typeof webexChannelBlockSchema>
|
|
196
228
|
export type WebexEncryptedPassword = z.infer<typeof webexEncryptedPasswordSchema>
|
|
229
|
+
export type SlackAccountRecord = z.infer<typeof slackAccountRecordSchema>
|
|
230
|
+
export type SlackChannelBlock = z.infer<typeof slackChannelBlockSchema>
|
|
197
231
|
export type SecretsFile = z.infer<typeof secretsFileSchema>
|
|
198
232
|
|
|
199
233
|
export type ParseSecretsResult = { ok: true; file: SecretsFile } | { ok: false; reason: string }
|