typeclaw 0.47.1 → 0.48.0
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/package.json +1 -1
- package/src/agent/index.ts +1 -2
- package/src/agent/loop-guard.ts +71 -41
- package/src/agent/plugin-tools.ts +167 -50
- package/src/agent/retry-same-ref.ts +89 -9
- package/src/agent/subagents.ts +88 -15
- package/src/agent/system-prompt.ts +1 -1
- package/src/agent/turn-runner.ts +60 -5
- package/src/bundled-plugins/agent-browser/index.ts +0 -35
- package/src/bundled-plugins/agent-browser/skills/agent-browser/SKILL.md +1 -1
- package/src/bundled-plugins/guard/policies/non-workspace-write.ts +16 -1
- package/src/bundled-plugins/memory/append-tool.ts +75 -2
- package/src/bundled-plugins/memory/frontmatter.ts +13 -2
- package/src/bundled-plugins/memory/load-memory.ts +1 -1
- package/src/bundled-plugins/memory/memory-logger.ts +5 -1
- package/src/bundled-plugins/memory/secret-detector.ts +7 -0
- package/src/bundled-plugins/memory/vector/hybrid.ts +3 -1
- package/src/bundled-plugins/memory/vector/passages.ts +5 -1
- package/src/bundled-plugins/operator/operator.ts +9 -2
- package/src/bundled-plugins/researcher/researcher.ts +4 -6
- package/src/bundled-plugins/security/index.ts +148 -98
- package/src/bundled-plugins/security/policies/private-surface-read.ts +43 -28
- package/src/channels/adapters/discord-bot-edit.ts +3 -5
- package/src/channels/adapters/discord-bot-reactions.ts +4 -6
- package/src/channels/adapters/discord-bot-slash-commands.ts +3 -1
- package/src/channels/adapters/discord-bot.ts +20 -23
- package/src/channels/adapters/discord-edit.ts +3 -5
- package/src/channels/adapters/discord-reactions.ts +4 -6
- package/src/channels/adapters/discord.ts +1 -1
- package/src/channels/adapters/github/decoy-reviewer.ts +2 -1
- package/src/channels/adapters/github/history.ts +2 -1
- package/src/channels/adapters/github/inbound.ts +4 -11
- package/src/channels/adapters/github/index.ts +6 -9
- package/src/channels/adapters/github/outbound.ts +3 -6
- package/src/channels/adapters/github/reactions.ts +3 -2
- package/src/channels/adapters/github/reconcile-cooldown-store.ts +4 -6
- package/src/channels/adapters/github/reconcile-open-prs.ts +5 -4
- package/src/channels/adapters/github/recover-failed-deliveries.ts +2 -5
- package/src/channels/adapters/github/review-state.ts +3 -2
- package/src/channels/adapters/github/review-thread-resolver.ts +3 -2
- package/src/channels/adapters/github/webhook-register.ts +5 -8
- package/src/channels/adapters/instagram-channel-resolver.ts +3 -5
- package/src/channels/adapters/instagram.ts +1 -1
- package/src/channels/adapters/kakaotalk-author-resolver.ts +2 -5
- package/src/channels/adapters/kakaotalk-channel-resolver.ts +3 -5
- package/src/channels/adapters/kakaotalk-fetch-attachment.ts +2 -1
- package/src/channels/adapters/kakaotalk-membership.ts +2 -5
- package/src/channels/adapters/kakaotalk-typing.ts +1 -1
- package/src/channels/adapters/kakaotalk.ts +1 -1
- package/src/channels/adapters/line-channel-resolver.ts +3 -5
- package/src/channels/adapters/line.ts +1 -1
- package/src/channels/adapters/slack-bot-edit.ts +1 -1
- package/src/channels/adapters/slack-bot-reactions.ts +4 -6
- package/src/channels/adapters/slack-bot.ts +8 -8
- package/src/channels/adapters/slack-edit.ts +3 -5
- package/src/channels/adapters/slack-reactions.ts +4 -6
- package/src/channels/adapters/slack.ts +1 -1
- package/src/channels/adapters/teams-edit.ts +2 -5
- package/src/channels/adapters/teams.ts +1 -1
- package/src/channels/adapters/telegram-bot-edit.ts +4 -7
- package/src/channels/adapters/telegram-bot.ts +1 -1
- package/src/channels/adapters/webex-bot.ts +1 -1
- package/src/channels/adapters/webex-edit.ts +3 -5
- package/src/channels/adapters/webex.ts +1 -1
- package/src/channels/github-review-claim.ts +89 -4
- package/src/channels/github-token-bridge.ts +2 -1
- package/src/channels/manager.ts +478 -64
- package/src/channels/membership-cache.ts +2 -5
- package/src/channels/persistence.ts +3 -6
- package/src/channels/reloadable.ts +23 -6
- package/src/channels/router.ts +134 -56
- package/src/channels/tunnel-bridge.ts +2 -1
- package/src/channels/types.ts +6 -3
- package/src/cli/hostd.ts +56 -28
- package/src/config/config.ts +10 -0
- package/src/config/providers.ts +179 -0
- package/src/dependencies/bin-resolver.ts +88 -0
- package/src/dependencies/bins.ts +227 -0
- package/src/dependencies/index.ts +17 -0
- package/src/doctor/checks.ts +2 -0
- package/src/doctor/operational-incidents.ts +32 -0
- package/src/hostd/credential-rotation-apply.ts +94 -0
- package/src/init/dockerfile.ts +36 -18
- package/src/init/index.ts +1 -1
- package/src/init/models-dev.ts +17 -3
- package/src/init/validate-api-key.ts +26 -4
- package/src/operations/classify-tool-outcome.ts +151 -0
- package/src/operations/incidents.ts +274 -0
- package/src/operations/index.ts +3 -0
- package/src/operations/remediations.ts +41 -0
- package/src/reload/client.ts +8 -2
- package/src/reload/docker-exec-client.ts +10 -2
- package/src/reload/index.ts +1 -1
- package/src/reload/recover.ts +5 -3
- package/src/reload/registry.ts +5 -5
- package/src/reload/types.ts +9 -1
- package/src/run/index.ts +5 -0
- package/src/sandbox/dependency-bins.ts +125 -0
- package/src/sandbox/errors.ts +32 -0
- package/src/sandbox/hidden-paths.ts +4 -1
- package/src/sandbox/index.ts +8 -0
- package/src/sandbox/policy.ts +1 -1
- package/src/sandbox/privileged-runtime.ts +88 -7
- package/src/sandbox/runtime-private.ts +4 -0
- package/src/sandbox/writable-zones.ts +4 -4
- package/src/server/index.ts +7 -5
- package/src/shared/protocol.ts +6 -1
- package/src/skills/typeclaw-monorepo/SKILL.md +1 -1
- package/src/skills/typeclaw-troubleshooting/SKILL.md +1 -1
- package/typeclaw.schema.json +35 -5
- package/src/bundled-plugins/agent-browser/shim-install.ts +0 -158
- package/src/bundled-plugins/agent-browser/shim.ts +0 -108
- /package/src/channels/{adapters/describe-error.ts → describe-error.ts} +0 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readIncidentLedger } from '@/operations'
|
|
2
|
+
|
|
3
|
+
import type { DoctorCheck } from './types'
|
|
4
|
+
|
|
5
|
+
export function buildOperationalIncidentChecks(): DoctorCheck[] {
|
|
6
|
+
return [
|
|
7
|
+
{
|
|
8
|
+
name: 'operations.incidents',
|
|
9
|
+
category: 'operations',
|
|
10
|
+
description: 'deterministic operational incidents are resolved',
|
|
11
|
+
applies: (ctx) => ctx.hasAgentFolder,
|
|
12
|
+
async run(ctx) {
|
|
13
|
+
const ledger = await readIncidentLedger(ctx.cwd)
|
|
14
|
+
const unresolved = ledger.incidents.filter((incident) => incident.status === 'unresolved')
|
|
15
|
+
if (unresolved.length === 0) return { status: 'ok', message: 'no unresolved operational incidents' }
|
|
16
|
+
const recurrent = unresolved.filter((incident) => incident.recurrent)
|
|
17
|
+
return {
|
|
18
|
+
status: recurrent.length > 0 ? 'error' : 'warning',
|
|
19
|
+
message:
|
|
20
|
+
recurrent.length > 0
|
|
21
|
+
? `${recurrent.length} recurrent TypeClaw environment issue(s) require operator repair`
|
|
22
|
+
: `${unresolved.length} unresolved operational incident(s)`,
|
|
23
|
+
details: unresolved.map(
|
|
24
|
+
(incident) =>
|
|
25
|
+
`${incident.fingerprint} count=${incident.count} first=${incident.firstSeenAt} last=${incident.lastSeenAt}`,
|
|
26
|
+
),
|
|
27
|
+
fix: { description: 'Repair the TypeClaw environment defect, then retry the failed operation.' },
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
]
|
|
32
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { requestReloadWithFallback as RequestReloadWithFallback, ReloadResult } from '@/reload'
|
|
2
|
+
|
|
3
|
+
export type RenewableAdapter = 'kakaotalk' | 'webex' | 'teams'
|
|
4
|
+
|
|
5
|
+
export type CredentialRotationApplyInput = {
|
|
6
|
+
containerName: string
|
|
7
|
+
cwd: string
|
|
8
|
+
adapter: RenewableAdapter
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type CredentialRotationApplyDeps = {
|
|
12
|
+
resolveHostPort: (options: { cwd: string }) => Promise<number>
|
|
13
|
+
resolveTuiToken: (options: { cwd: string }) => Promise<string | null>
|
|
14
|
+
requestReloadWithFallback: typeof RequestReloadWithFallback
|
|
15
|
+
restartContainer: (input: { containerName: string; cwd: string }) => Promise<{ ok: boolean; reason?: string }>
|
|
16
|
+
timeoutMs?: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type CredentialRotationApplyResult =
|
|
20
|
+
| { kind: 'reloaded'; transport: 'host' | 'container-local' }
|
|
21
|
+
| { kind: 'restarted'; reloadError: string }
|
|
22
|
+
| { kind: 'failed'; reloadError: string; restartError: string }
|
|
23
|
+
|
|
24
|
+
const DEFAULT_TIMEOUT_MS = 30_000
|
|
25
|
+
|
|
26
|
+
// Applying a renewed credential used to stop, remove and recreate the whole
|
|
27
|
+
// container, which killed every in-flight session, cron job and subagent on
|
|
28
|
+
// every OTHER channel too. Teams tokens expire every ~6h, so that fired ~4x a
|
|
29
|
+
// day. Bounce just the one adapter over the existing reload transport instead,
|
|
30
|
+
// and keep the container restart only as the fallback for when neither
|
|
31
|
+
// transport can reach the container at all.
|
|
32
|
+
export async function applyCredentialRotation(
|
|
33
|
+
input: CredentialRotationApplyInput,
|
|
34
|
+
deps: CredentialRotationApplyDeps,
|
|
35
|
+
): Promise<CredentialRotationApplyResult> {
|
|
36
|
+
const reload = await requestAdapterReload(input, deps)
|
|
37
|
+
if (reload.error === null) return { kind: 'reloaded', transport: reload.transport }
|
|
38
|
+
|
|
39
|
+
const restart = await deps.restartContainer({ containerName: input.containerName, cwd: input.cwd })
|
|
40
|
+
if (restart.ok) return { kind: 'restarted', reloadError: reload.error }
|
|
41
|
+
return { kind: 'failed', reloadError: reload.error, restartError: restart.reason ?? 'restart failed' }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type AdapterReloadAttempt = { transport: 'host' | 'container-local'; error: string | null }
|
|
45
|
+
|
|
46
|
+
async function requestAdapterReload(
|
|
47
|
+
{ cwd, adapter }: CredentialRotationApplyInput,
|
|
48
|
+
deps: CredentialRotationApplyDeps,
|
|
49
|
+
): Promise<AdapterReloadAttempt> {
|
|
50
|
+
try {
|
|
51
|
+
const port = await deps.resolveHostPort({ cwd })
|
|
52
|
+
const token = await deps.resolveTuiToken({ cwd })
|
|
53
|
+
const url = new URL(`ws://127.0.0.1:${port}`)
|
|
54
|
+
if (token !== null) url.searchParams.set('token', token)
|
|
55
|
+
|
|
56
|
+
const response = await deps.requestReloadWithFallback({
|
|
57
|
+
url: url.toString(),
|
|
58
|
+
cwd,
|
|
59
|
+
token,
|
|
60
|
+
scope: 'channels',
|
|
61
|
+
cause: { kind: 'credential-rotation', adapter },
|
|
62
|
+
timeoutMs: deps.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
63
|
+
})
|
|
64
|
+
return { transport: response.transport, error: describeUnapplied(response.results, adapter) }
|
|
65
|
+
} catch (err) {
|
|
66
|
+
return { transport: 'host', error: err instanceof Error ? err.message : String(err) }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Only `stop-failed` warrants the fallback: the adapter is still live on the
|
|
71
|
+
// superseded credential, so nothing was applied. Every other outcome means the
|
|
72
|
+
// rotation landed or is landing — including `recovery-pending`, where the
|
|
73
|
+
// adapter is already down holding no stale token and supervision is retrying it
|
|
74
|
+
// on backoff. Recreating the container cannot fix a credential the platform
|
|
75
|
+
// rejected, so falling back there would destroy unrelated work for nothing.
|
|
76
|
+
function describeUnapplied(results: ReloadResult[], adapter: RenewableAdapter): string | null {
|
|
77
|
+
const channels = results.find((result) => result.scope === 'channels')
|
|
78
|
+
if (channels === undefined) return 'channels scope missing from reload result'
|
|
79
|
+
if (!channels.ok) return channels.reason
|
|
80
|
+
|
|
81
|
+
const applied = readCredentialApply(channels.details)
|
|
82
|
+
if (applied === null || applied.adapter !== adapter) return `reload did not apply the ${adapter} credential`
|
|
83
|
+
if (applied.outcome === 'stop-failed') return `${adapter} adapter could not be stopped to apply the credential`
|
|
84
|
+
return null
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function readCredentialApply(details: unknown): { adapter: string; outcome: string } | null {
|
|
88
|
+
if (typeof details !== 'object' || details === null) return null
|
|
89
|
+
const candidate = (details as { credentialApply?: unknown }).credentialApply
|
|
90
|
+
if (typeof candidate !== 'object' || candidate === null) return null
|
|
91
|
+
const { adapter, outcome } = candidate as { adapter?: unknown; outcome?: unknown }
|
|
92
|
+
if (typeof adapter !== 'string' || typeof outcome !== 'string') return null
|
|
93
|
+
return { adapter, outcome }
|
|
94
|
+
}
|
package/src/init/dockerfile.ts
CHANGED
|
@@ -1265,14 +1265,14 @@ RUN chmod +x ${TYPECLAW_CX_SESSION_START_HOOK_PATH} ${TYPECLAW_CX_STOP_HOOK_PATH
|
|
|
1265
1265
|
}
|
|
1266
1266
|
|
|
1267
1267
|
// Shared-library runtime deps Chrome for Testing needs to launch on amd64
|
|
1268
|
-
// Debian trixie (base of `oven/bun:1-slim`).
|
|
1269
|
-
//
|
|
1270
|
-
//
|
|
1271
|
-
//
|
|
1272
|
-
//
|
|
1273
|
-
//
|
|
1274
|
-
//
|
|
1275
|
-
//
|
|
1268
|
+
// Debian trixie (base of `oven/bun:1-slim`). This layer is the ONLY thing
|
|
1269
|
+
// that installs them. `agent-browser install --with-deps` looks like it
|
|
1270
|
+
// would, but it cannot: it shells out to `sudo apt-get` and there is no
|
|
1271
|
+
// sudo in the image. Its hardcoded list also omits `libglib2.0-0t64`
|
|
1272
|
+
// (still true at 0.33.0), so even where apt worked Chrome would die on
|
|
1273
|
+
// launch with `libglib-2.0.so.0: cannot open shared object file`. We
|
|
1274
|
+
// install the full Playwright-tested chromium dep set here so the bug
|
|
1275
|
+
// doesn't recur if upstream omits another package.
|
|
1276
1276
|
//
|
|
1277
1277
|
// Package list mirrors Playwright's `debian13-x64` chromium deps in
|
|
1278
1278
|
// nativeDeps.ts (https://github.com/microsoft/playwright). t64-suffixed
|
|
@@ -1488,7 +1488,7 @@ ${renderAgentBrowserInstallLayer(buildKit)}
|
|
|
1488
1488
|
|
|
1489
1489
|
${LAYER_4_5_AGENT_BROWSER_HEADED_WRAPPER}
|
|
1490
1490
|
|
|
1491
|
-
${renderChromeForTestingLayer(
|
|
1491
|
+
${renderChromeForTestingLayer()}
|
|
1492
1492
|
|
|
1493
1493
|
${cloudflaredBlock}${claudeCodeBlock}${codexCliBlock}${renderEntrypointShimLayer()}
|
|
1494
1494
|
${renderGitAskPassLayer()}
|
|
@@ -1566,7 +1566,7 @@ ${renderAgentBrowserInstallLayer(true)}
|
|
|
1566
1566
|
|
|
1567
1567
|
${LAYER_4_5_AGENT_BROWSER_HEADED_WRAPPER}
|
|
1568
1568
|
|
|
1569
|
-
${renderChromeForTestingLayer(
|
|
1569
|
+
${renderChromeForTestingLayer()}
|
|
1570
1570
|
|
|
1571
1571
|
${renderEntrypointShimLayer()}
|
|
1572
1572
|
${renderGitAskPassLayer()}
|
|
@@ -1641,7 +1641,7 @@ RUN if [ "$TARGETARCH" = "arm64" ]; then \\
|
|
|
1641
1641
|
const renderAgentBrowserInstallLayer = (buildKit: boolean): string =>
|
|
1642
1642
|
`# Layer 4 (volatile): install agent-browser globally so it survives the
|
|
1643
1643
|
# runtime bind-mount over /agent/node_modules.
|
|
1644
|
-
RUN ${bunCacheMount(buildKit)}bun install -g agent-browser@^0.
|
|
1644
|
+
RUN ${bunCacheMount(buildKit)}bun install -g agent-browser@^0.33.0`
|
|
1645
1645
|
|
|
1646
1646
|
// Layer 4.5: shim the agent-browser binary with a wrapper that calls
|
|
1647
1647
|
// \`agent-browser close\` before \`open\`/\`goto\`/\`navigate\` when headed
|
|
@@ -1692,6 +1692,7 @@ RUN mv /usr/local/bin/agent-browser /usr/local/bin/agent-browser.real \\
|
|
|
1692
1692
|
&& cat > /usr/local/bin/agent-browser <<'TYPECLAW_AGENT_BROWSER_WRAPPER_EOF' \\
|
|
1693
1693
|
&& chmod +x /usr/local/bin/agent-browser
|
|
1694
1694
|
#!/bin/sh
|
|
1695
|
+
# typeclaw-agent-browser-image-wrapper
|
|
1695
1696
|
# typeclaw wrapper for agent-browser — see src/init/dockerfile.ts.
|
|
1696
1697
|
set -e
|
|
1697
1698
|
real="\${TYPECLAW_AGENT_BROWSER_REAL:-/usr/local/bin/agent-browser.real}"
|
|
@@ -1701,6 +1702,17 @@ real="\${TYPECLAW_AGENT_BROWSER_REAL:-/usr/local/bin/agent-browser.real}"
|
|
|
1701
1702
|
if [ "\${_TYPECLAW_AGENT_BROWSER_HEADED_HANDLED:-}" = "1" ]; then
|
|
1702
1703
|
exec "$real" "$@"
|
|
1703
1704
|
fi
|
|
1705
|
+
# Match upstream precedence: an explicit --user-agent flag or non-empty
|
|
1706
|
+
# AGENT_BROWSER_USER_AGENT wins; otherwise use a current desktop Linux UA.
|
|
1707
|
+
has_user_agent=0
|
|
1708
|
+
for arg in "$@"; do
|
|
1709
|
+
case "$arg" in
|
|
1710
|
+
--user-agent|--user-agent=*) has_user_agent=1; break ;;
|
|
1711
|
+
esac
|
|
1712
|
+
done
|
|
1713
|
+
if [ -z "\${AGENT_BROWSER_USER_AGENT:-}" ] && [ "$has_user_agent" = "0" ]; then
|
|
1714
|
+
export AGENT_BROWSER_USER_AGENT='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
|
1715
|
+
fi
|
|
1704
1716
|
# Pre-close is only needed when the caller is requesting headed mode.
|
|
1705
1717
|
# Match upstream's env_var_is_truthy contract (cli/src/flags.rs:183):
|
|
1706
1718
|
# truthy = any non-empty value except case-insensitive "0", "false", "no".
|
|
@@ -1753,14 +1765,20 @@ TYPECLAW_AGENT_BROWSER_WRAPPER_EOF`
|
|
|
1753
1765
|
|
|
1754
1766
|
// Layer 5: download the pinned Chrome for Testing build into
|
|
1755
1767
|
// ~/.agent-browser/browsers/. NO cache mount on that path because the
|
|
1756
|
-
// runtime needs the binary in the image
|
|
1757
|
-
//
|
|
1758
|
-
//
|
|
1759
|
-
//
|
|
1760
|
-
|
|
1768
|
+
// runtime needs the binary in the image, and no apt cache mount either
|
|
1769
|
+
// because nothing in this layer runs apt.
|
|
1770
|
+
//
|
|
1771
|
+
// Deliberately NOT `--with-deps`. That flag shells out to `sudo apt-get`
|
|
1772
|
+
// and there is no sudo in the image, so it could never install anything
|
|
1773
|
+
// here. Through 0.27.x it failed silently and still exited 0, which is
|
|
1774
|
+
// why it looked like a working backstop; 0.33.0 made it fail-closed
|
|
1775
|
+
// ("fails if deps fail" in its own --help) and turned that dead no-op
|
|
1776
|
+
// into a hard build failure. Chrome's shared libraries are installed for
|
|
1777
|
+
// real in Layer 2.
|
|
1778
|
+
const renderChromeForTestingLayer = (): string =>
|
|
1761
1779
|
`# Layer 5 (heavy, amd64 only): Chrome for Testing download.
|
|
1762
|
-
RUN
|
|
1763
|
-
agent-browser install
|
|
1780
|
+
RUN if [ "$TARGETARCH" != "arm64" ]; then \\
|
|
1781
|
+
agent-browser install; \\
|
|
1764
1782
|
fi`
|
|
1765
1783
|
|
|
1766
1784
|
function defaultConfig(): DockerfileConfig {
|
package/src/init/index.ts
CHANGED
|
@@ -769,7 +769,7 @@ function addCustomModel(
|
|
|
769
769
|
// Must match the Dockerfile Layer 4 global install (dockerfile.ts); they are
|
|
770
770
|
// two installs of the same CLI and a skew is silent. Enforced by a guard test
|
|
771
771
|
// in packagejson.test.ts.
|
|
772
|
-
export const AGENT_BROWSER_VERSION = '^0.
|
|
772
|
+
export const AGENT_BROWSER_VERSION = '^0.33.0'
|
|
773
773
|
function buildPackageJson(root: string, name: string, platform: NodeJS.Platform): Record<string, unknown> {
|
|
774
774
|
return {
|
|
775
775
|
name,
|
package/src/init/models-dev.ts
CHANGED
|
@@ -16,7 +16,11 @@ const REQUEST_TIMEOUT_MS = 10_000
|
|
|
16
16
|
// KnownProviderId. Specifically, they ship Fireworks under `fireworks-ai`.
|
|
17
17
|
// This map is the single place that bridges the two namespaces; every other
|
|
18
18
|
// helper in this file works in OUR namespace.
|
|
19
|
-
|
|
19
|
+
// `null` means "never merge upstream models into this provider". Reserved for
|
|
20
|
+
// gateways whose ids are creator-qualified: models.dev lists bare ids like
|
|
21
|
+
// `gpt-5.5`, but OpenGateway only accepts `openai/gpt-5.5`, so merging would
|
|
22
|
+
// synthesize refs that pass `isModelRef()` and then 404 at request time.
|
|
23
|
+
const PROVIDER_TO_MODELS_DEV: Record<KnownProviderId, string | null> = {
|
|
20
24
|
openai: 'openai',
|
|
21
25
|
// openai-codex models live under the `openai` namespace on models.dev too
|
|
22
26
|
// (Codex is a backend, not a separate provider in their taxonomy). Curated
|
|
@@ -42,6 +46,16 @@ const PROVIDER_TO_MODELS_DEV: Record<KnownProviderId, string> = {
|
|
|
42
46
|
// metadata under `moonshot`, so we route lookups there; the curated
|
|
43
47
|
// `kimi-for-coding` alias is surfaced regardless of upstream membership.
|
|
44
48
|
'moonshot-coding': 'moonshot',
|
|
49
|
+
opengateway: null,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function upstreamProviderFor(
|
|
53
|
+
data: Record<string, ModelsDevProvider>,
|
|
54
|
+
providerId: KnownProviderId,
|
|
55
|
+
): ModelsDevProvider | undefined {
|
|
56
|
+
const modelsDevId = PROVIDER_TO_MODELS_DEV[providerId]
|
|
57
|
+
if (modelsDevId === null) return undefined
|
|
58
|
+
return data[modelsDevId]
|
|
45
59
|
}
|
|
46
60
|
|
|
47
61
|
export type ModelOption = {
|
|
@@ -156,7 +170,7 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
|
|
|
156
170
|
const seen = new Set<string>()
|
|
157
171
|
for (const providerId of Object.keys(KNOWN_PROVIDERS) as KnownProviderId[]) {
|
|
158
172
|
const known = KNOWN_PROVIDERS[providerId]
|
|
159
|
-
const upstream = data
|
|
173
|
+
const upstream = upstreamProviderFor(data, providerId)
|
|
160
174
|
const upstreamModels = upstream?.models ?? {}
|
|
161
175
|
for (const modelId of Object.keys(known.models)) {
|
|
162
176
|
const upstreamModel = upstreamModels[modelId]
|
|
@@ -167,7 +181,7 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
|
|
|
167
181
|
}
|
|
168
182
|
|
|
169
183
|
for (const providerId of Object.keys(KNOWN_PROVIDERS) as KnownProviderId[]) {
|
|
170
|
-
const upstream = data
|
|
184
|
+
const upstream = upstreamProviderFor(data, providerId)
|
|
171
185
|
const upstreamModels = upstream?.models ?? {}
|
|
172
186
|
for (const [fallbackModelId, upstreamModel] of Object.entries(upstreamModels)) {
|
|
173
187
|
const modelId = upstreamModel.id ?? fallbackModelId
|
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import { effectiveBaseUrl } from '@/agent/model-overrides'
|
|
2
2
|
import { KNOWN_PROVIDERS, type KnownProviderId } from '@/config/providers'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// `successShape` is what a 200 must look like to count as a validated key.
|
|
5
|
+
// Default `models-list` requires `{ data: [...] }`, which every /v1/models probe
|
|
6
|
+
// below returns. `json-object` is the weaker check for probes that answer with a
|
|
7
|
+
// bare object instead of a list — it still rejects the captive-portal HTML case
|
|
8
|
+
// that motivated shape-checking in the first place.
|
|
9
|
+
type ProbeSuccessShape = 'models-list' | 'json-object'
|
|
10
|
+
|
|
11
|
+
type Probe = {
|
|
12
|
+
url: string
|
|
13
|
+
authHeader: 'bearer' | 'x-api-key'
|
|
14
|
+
successShape?: ProbeSuccessShape
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const PROVIDER_PROBE: Partial<Record<KnownProviderId, Probe>> = {
|
|
5
18
|
openai: { url: 'https://api.openai.com/v1/models', authHeader: 'bearer' },
|
|
6
19
|
anthropic: { url: 'https://api.anthropic.com/v1/models', authHeader: 'x-api-key' },
|
|
7
20
|
fireworks: { url: 'https://api.fireworks.ai/inference/v1/models', authHeader: 'bearer' },
|
|
@@ -13,6 +26,12 @@ const PROVIDER_PROBE: Partial<Record<KnownProviderId, { url: string; authHeader:
|
|
|
13
26
|
upstage: { url: 'https://api.upstage.ai/v1/models', authHeader: 'bearer' },
|
|
14
27
|
moonshot: { url: 'https://api.moonshot.ai/v1/models', authHeader: 'bearer' },
|
|
15
28
|
'moonshot-coding': { url: 'https://api.kimi.com/coding/v1/models', authHeader: 'bearer' },
|
|
29
|
+
// NOT /v1/models: OpenGateway serves its catalog PUBLICLY (verified — HTTP 200
|
|
30
|
+
// with `{data:[...]}` for a missing key and for a garbage key), so probing it
|
|
31
|
+
// would hand back `ok` for any string the user pastes. That is worse than no
|
|
32
|
+
// validation, because init would vouch for a key that cannot complete a single
|
|
33
|
+
// request. /v1/me is the cheapest endpoint that actually 401s on a bad key.
|
|
34
|
+
opengateway: { url: 'https://apis.opengateway.ai/v1/me', authHeader: 'bearer', successShape: 'json-object' },
|
|
16
35
|
}
|
|
17
36
|
|
|
18
37
|
// When a base-URL override (ANTHROPIC_BASE_URL / OPENAI_BASE_URL) points at a
|
|
@@ -75,7 +94,7 @@ export async function validateApiKey(
|
|
|
75
94
|
// Treat the response as "ok" only if it parses as the expected
|
|
76
95
|
// JSON shape (`{ data: [...] }` for /v1/models on every probed
|
|
77
96
|
// provider).
|
|
78
|
-
const shapeOk = await
|
|
97
|
+
const shapeOk = await hasExpectedShape(res, probe.successShape ?? 'models-list')
|
|
79
98
|
if (shapeOk) return { kind: 'ok' }
|
|
80
99
|
return { kind: 'skipped', reason: 'network-error', detail: 'unexpected response shape' }
|
|
81
100
|
}
|
|
@@ -125,12 +144,14 @@ function isFireworksFirePassForbidden(body: string): boolean {
|
|
|
125
144
|
}
|
|
126
145
|
}
|
|
127
146
|
|
|
128
|
-
async function
|
|
147
|
+
async function hasExpectedShape(res: Response, shape: ProbeSuccessShape): Promise<boolean> {
|
|
129
148
|
const text = await readCapped(res, MAX_BODY_BYTES)
|
|
130
149
|
if (text === null) return false
|
|
131
150
|
try {
|
|
132
151
|
const parsed = JSON.parse(text) as unknown
|
|
133
|
-
|
|
152
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) return false
|
|
153
|
+
if (shape === 'json-object') return true
|
|
154
|
+
return Array.isArray((parsed as { data?: unknown }).data)
|
|
134
155
|
} catch {
|
|
135
156
|
return false
|
|
136
157
|
}
|
|
@@ -172,6 +193,7 @@ export const API_KEY_DASHBOARD_URL: Partial<Record<KnownProviderId, string>> = {
|
|
|
172
193
|
upstage: 'https://console.upstage.ai/api-keys',
|
|
173
194
|
moonshot: 'https://platform.moonshot.ai/console/api-keys',
|
|
174
195
|
'moonshot-coding': 'https://www.kimi.com/code/console',
|
|
196
|
+
opengateway: 'https://opengateway.ai/docs/reference/guides/authentication',
|
|
175
197
|
}
|
|
176
198
|
|
|
177
199
|
// MiniMax sells the same `minimax` provider under two billing surfaces that
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { isSecretShapedToken } from '@/bundled-plugins/memory/secret-detector'
|
|
2
|
+
|
|
3
|
+
export type OperationalIncidentFact =
|
|
4
|
+
| { kind: 'bash-command-not-found'; bin: string }
|
|
5
|
+
| { kind: 'declared-skill-bin-unresolved'; bin: string }
|
|
6
|
+
| { kind: 'sandbox-proc-unavailable' }
|
|
7
|
+
|
|
8
|
+
export type OperationalIncidentResolutionSignal = 'successful-bin' | 'successful-sandbox-real-proc'
|
|
9
|
+
|
|
10
|
+
export type OperationalIncidentSuccessInput = {
|
|
11
|
+
tool: string
|
|
12
|
+
args: Record<string, unknown>
|
|
13
|
+
sandboxedRealProcSucceeded: boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const OPERATIONAL_INCIDENT_RESOLUTION_SIGNAL_BY_KIND = {
|
|
17
|
+
'bash-command-not-found': 'successful-bin',
|
|
18
|
+
'declared-skill-bin-unresolved': 'successful-bin',
|
|
19
|
+
'sandbox-proc-unavailable': 'successful-sandbox-real-proc',
|
|
20
|
+
} as const satisfies Record<OperationalIncidentFact['kind'], OperationalIncidentResolutionSignal>
|
|
21
|
+
|
|
22
|
+
export class DeclaredSkillBinUnresolvedError extends Error {
|
|
23
|
+
override readonly name = 'DeclaredSkillBinUnresolvedError'
|
|
24
|
+
|
|
25
|
+
constructor(readonly bin: string) {
|
|
26
|
+
super(`declared skill bin could not be resolved: ${bin}`)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const SAFE_BIN = '[A-Za-z0-9][A-Za-z0-9._+-]{0,127}'
|
|
31
|
+
const COMMAND_NOT_FOUND = new RegExp(
|
|
32
|
+
`^(?:/bin/bash: |bash: line [1-9][0-9]{0,5}: )(${SAFE_BIN}): command not found\\n(?:\\n){1,2}Command exited with code 127$`,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
export function classifyToolOutcome(input: { tool: string; error: unknown }): OperationalIncidentFact | null {
|
|
36
|
+
if (input.error instanceof DeclaredSkillBinUnresolvedError && isSafeBin(input.error.bin)) {
|
|
37
|
+
return { kind: 'declared-skill-bin-unresolved', bin: input.error.bin }
|
|
38
|
+
}
|
|
39
|
+
if (input.tool !== 'bash' || !(input.error instanceof Error)) return null
|
|
40
|
+
if (input.error.name === 'SandboxDegradedProcError') return { kind: 'sandbox-proc-unavailable' }
|
|
41
|
+
|
|
42
|
+
const match = COMMAND_NOT_FOUND.exec(input.error.message)
|
|
43
|
+
const bin = match?.[1]
|
|
44
|
+
return bin === undefined ? null : bashCommandNotFoundFact(bin)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function deriveOperationalIncidentFactsForSuccess(
|
|
48
|
+
input: OperationalIncidentSuccessInput,
|
|
49
|
+
): OperationalIncidentFact[] {
|
|
50
|
+
if (input.tool !== 'bash') return []
|
|
51
|
+
const command = input.args.command
|
|
52
|
+
if (typeof command !== 'string') return []
|
|
53
|
+
// False resolution hides a live defect; under-resolution only leaves a visible stale warning.
|
|
54
|
+
// Accept only syntax where aggregate exit zero proves this exact invocation succeeded.
|
|
55
|
+
const commandWord = parseSingleUnconditionalInvocation(command)
|
|
56
|
+
if (commandWord === null) return []
|
|
57
|
+
|
|
58
|
+
const facts = successfulBinFacts(commandWord)
|
|
59
|
+
if (input.sandboxedRealProcSucceeded) facts.push({ kind: 'sandbox-proc-unavailable' })
|
|
60
|
+
return facts
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function bashCommandNotFoundFact(bin: string): OperationalIncidentFact | null {
|
|
64
|
+
return isSafeBin(bin) ? { kind: 'bash-command-not-found', bin } : null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function successfulBinFacts(bin: string): OperationalIncidentFact[] {
|
|
68
|
+
if (!isSafeBin(bin)) return []
|
|
69
|
+
return [
|
|
70
|
+
{ kind: 'bash-command-not-found', bin },
|
|
71
|
+
{ kind: 'declared-skill-bin-unresolved', bin },
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isSafeBin(bin: string): boolean {
|
|
76
|
+
return new RegExp(`^${SAFE_BIN}$`).test(bin) && !isSecretShapedToken(bin)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const SHELL_CONSTRUCTS = new Set([
|
|
80
|
+
'!',
|
|
81
|
+
'case',
|
|
82
|
+
'coproc',
|
|
83
|
+
'do',
|
|
84
|
+
'done',
|
|
85
|
+
'elif',
|
|
86
|
+
'else',
|
|
87
|
+
'esac',
|
|
88
|
+
'fi',
|
|
89
|
+
'for',
|
|
90
|
+
'function',
|
|
91
|
+
'if',
|
|
92
|
+
'in',
|
|
93
|
+
'select',
|
|
94
|
+
'then',
|
|
95
|
+
'time',
|
|
96
|
+
'until',
|
|
97
|
+
'while',
|
|
98
|
+
])
|
|
99
|
+
|
|
100
|
+
function parseSingleUnconditionalInvocation(command: string): string | null {
|
|
101
|
+
const words: string[] = []
|
|
102
|
+
let word = ''
|
|
103
|
+
let quote: '"' | "'" | null = null
|
|
104
|
+
let active = false
|
|
105
|
+
const endWord = (): void => {
|
|
106
|
+
if (!active) return
|
|
107
|
+
words.push(word)
|
|
108
|
+
word = ''
|
|
109
|
+
active = false
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (let i = 0; i < command.length; i++) {
|
|
113
|
+
const character = command[i]!
|
|
114
|
+
if (quote !== null) {
|
|
115
|
+
if (character === quote) {
|
|
116
|
+
quote = null
|
|
117
|
+
} else {
|
|
118
|
+
if (quote === '"' && (character === '`' || (character === '$' && command[i + 1] === '('))) return null
|
|
119
|
+
if (character === '\\' && quote === '"' && i + 1 < command.length) word += command[++i]
|
|
120
|
+
else word += character
|
|
121
|
+
}
|
|
122
|
+
active = true
|
|
123
|
+
continue
|
|
124
|
+
}
|
|
125
|
+
if (character === '"' || character === "'") {
|
|
126
|
+
quote = character
|
|
127
|
+
active = true
|
|
128
|
+
continue
|
|
129
|
+
}
|
|
130
|
+
if (character === '\\') {
|
|
131
|
+
if (i + 1 >= command.length) return null
|
|
132
|
+
word += command[++i]
|
|
133
|
+
active = true
|
|
134
|
+
continue
|
|
135
|
+
}
|
|
136
|
+
if (';&|<>\n\r()'.includes(character) || character === '`' || (character === '$' && command[i + 1] === '('))
|
|
137
|
+
return null
|
|
138
|
+
if (character === '#' && !active) return null
|
|
139
|
+
if (character === ' ' || character === '\t') {
|
|
140
|
+
endWord()
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
word += character
|
|
144
|
+
active = true
|
|
145
|
+
}
|
|
146
|
+
if (quote !== null) return null
|
|
147
|
+
endWord()
|
|
148
|
+
const commandWord = words.find((candidate) => !/^[A-Za-z_][A-Za-z0-9_]*=/.test(candidate))
|
|
149
|
+
if (commandWord === undefined || SHELL_CONSTRUCTS.has(commandWord)) return null
|
|
150
|
+
return commandWord
|
|
151
|
+
}
|