typeclaw 0.47.2 → 0.48.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.
- 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 +234 -58
- package/src/agent/system-prompt.ts +1 -1
- package/src/agent/turn-runner.ts +12 -2
- package/src/agent-messenger/config-dir.ts +131 -0
- package/src/agent-messenger/host-config-dir.ts +121 -0
- package/src/bundled-plugins/agent-browser/index.ts +0 -38
- 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/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/channel.ts +15 -16
- package/src/cli/hostd.ts +61 -38
- package/src/cli/init.ts +21 -7
- package/src/cli/model.ts +6 -3
- package/src/cli/restart.ts +13 -15
- package/src/compose/restart.ts +14 -6
- package/src/config/config.ts +10 -0
- package/src/config/providers.ts +68 -0
- package/src/container/agent-operation-lock.ts +85 -0
- package/src/container/controller.ts +6 -0
- package/src/container/index.ts +8 -0
- package/src/container/restart.ts +55 -0
- package/src/container/shared.ts +5 -0
- package/src/container/start.ts +70 -4
- package/src/container/stop.ts +17 -1
- 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/hostd/daemon.ts +1 -1
- package/src/hostd/paths.ts +17 -0
- package/src/hostd/supervisor.ts +27 -10
- package/src/init/dockerfile.ts +6 -5
- package/src/init/env-file.ts +42 -18
- package/src/init/instagram-auth.ts +55 -31
- package/src/init/kakaotalk-auth.ts +1 -5
- package/src/init/line-auth.ts +45 -29
- package/src/init/models-dev.ts +131 -10
- package/src/init/opengateway-catalog.ts +120 -0
- 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/canonical-secrets.ts +10 -1
- package/src/sandbox/dependency-bins.ts +125 -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 +7 -12
- 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/test-helpers/fake-agent-operation-lock.ts +66 -0
- package/typeclaw.schema.json +10 -5
- package/src/bundled-plugins/agent-browser/shim-install.ts +0 -179
- package/src/bundled-plugins/agent-browser/shim.ts +0 -108
- /package/src/channels/{adapters/describe-error.ts → describe-error.ts} +0 -0
package/src/hostd/paths.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { isWindows } from '@/shared'
|
|
|
8
8
|
const SOCKET_FILE = 'hostd.sock'
|
|
9
9
|
const REGISTRATIONS_DIR = 'registrations'
|
|
10
10
|
const KEYS_DIR = 'keys'
|
|
11
|
+
const AGENT_OPERATION_LOCKS_DIR = 'agent-locks'
|
|
11
12
|
|
|
12
13
|
// Defense-in-depth: containerName arrives from RPC payloads (some of which
|
|
13
14
|
// originate inside the container). Docker already forbids slashes and most
|
|
@@ -67,6 +68,17 @@ export function registrationsDir(): string {
|
|
|
67
68
|
return join(runDir(), REGISTRATIONS_DIR)
|
|
68
69
|
}
|
|
69
70
|
|
|
71
|
+
export function agentOperationLocksDir(): string {
|
|
72
|
+
return join(runDir(), AGENT_OPERATION_LOCKS_DIR)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function agentOperationLockPath(containerName: string): string {
|
|
76
|
+
if (!SAFE_NAME.test(containerName)) {
|
|
77
|
+
throw new Error(`invalid container name for agent operation lock: ${JSON.stringify(containerName)}`)
|
|
78
|
+
}
|
|
79
|
+
return join(agentOperationLocksDir(), `${containerName}.lock`)
|
|
80
|
+
}
|
|
81
|
+
|
|
70
82
|
export function keysDir(): string {
|
|
71
83
|
return join(homeRoot(), KEYS_DIR)
|
|
72
84
|
}
|
|
@@ -101,3 +113,8 @@ export async function ensureDirs(): Promise<void> {
|
|
|
101
113
|
await chmod(keysDir(), 0o700).catch(() => {})
|
|
102
114
|
await chmod(modelsDir(), 0o700).catch(() => {})
|
|
103
115
|
}
|
|
116
|
+
|
|
117
|
+
export async function ensureAgentOperationLocksDir(): Promise<void> {
|
|
118
|
+
await mkdir(agentOperationLocksDir(), { recursive: true })
|
|
119
|
+
await chmod(agentOperationLocksDir(), 0o700).catch(() => {})
|
|
120
|
+
}
|
package/src/hostd/supervisor.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { CurrentHostDaemon } from '@/container'
|
|
2
|
+
import {
|
|
3
|
+
type AgentOperationLease,
|
|
4
|
+
reserveAgentOperationLock,
|
|
5
|
+
type ReserveAgentOperationLock,
|
|
6
|
+
} from '@/container/agent-operation-lock'
|
|
2
7
|
|
|
3
8
|
import type { Response } from './protocol'
|
|
4
9
|
|
|
@@ -13,6 +18,7 @@ export type SupervisorRestart = (input: {
|
|
|
13
18
|
// Injected by the daemon so the restart registers the container in-process
|
|
14
19
|
// instead of over the socket — see CurrentHostDaemon docs in src/container.
|
|
15
20
|
currentHostDaemon?: CurrentHostDaemon
|
|
21
|
+
operationLease?: AgentOperationLease
|
|
16
22
|
}) => Promise<{ ok: true } | { ok: false; reason: string }>
|
|
17
23
|
|
|
18
24
|
export type SupervisorOptions = {
|
|
@@ -30,34 +36,43 @@ export type Supervisor = {
|
|
|
30
36
|
cwd: string
|
|
31
37
|
build?: boolean
|
|
32
38
|
currentHostDaemon?: CurrentHostDaemon
|
|
33
|
-
}) => Response
|
|
39
|
+
}) => Promise<Response>
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
export type SupervisorBuildOptions = {
|
|
37
43
|
restart: SupervisorRestart
|
|
38
44
|
onLog: (event: SupervisorLogEvent) => void
|
|
39
45
|
isStopped: () => boolean
|
|
46
|
+
reserveLock?: ReserveAgentOperationLock
|
|
40
47
|
}
|
|
41
48
|
|
|
42
|
-
// The daemon
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
export function buildSupervisor({
|
|
49
|
+
// The daemon reserves the lifecycle lock before ACKing, then runs stop+start in
|
|
50
|
+
// the background so the agent's RPC connection can close before `docker stop`.
|
|
51
|
+
// The requester exits about 500 ms after the ACK, so a lock failure discovered
|
|
52
|
+
// later would be invisible; rejecting the ACK lets the agent surface and retry
|
|
53
|
+
// the error. Failures after scheduling are still surfaced through the log channel.
|
|
54
|
+
export function buildSupervisor({
|
|
55
|
+
restart,
|
|
56
|
+
onLog,
|
|
57
|
+
isStopped,
|
|
58
|
+
reserveLock = reserveAgentOperationLock,
|
|
59
|
+
}: SupervisorBuildOptions): Supervisor {
|
|
48
60
|
return {
|
|
49
|
-
scheduleRestart: ({ containerName, cwd, build = false, currentHostDaemon }): Response => {
|
|
61
|
+
scheduleRestart: async ({ containerName, cwd, build = false, currentHostDaemon }): Promise<Response> => {
|
|
50
62
|
if (isStopped()) return { ok: false, reason: 'daemon stopping' }
|
|
63
|
+
const reservation = await reserveLock({ agentDir: cwd, operation: 'restart' })
|
|
64
|
+
if (!reservation.ok) return { ok: false, reason: reservation.reason }
|
|
51
65
|
onLog({ kind: 'restart-scheduled', containerName, build })
|
|
52
|
-
void runRestart()
|
|
66
|
+
void runRestart(reservation.lease, reservation.release)
|
|
53
67
|
return { ok: true }
|
|
54
68
|
|
|
55
|
-
async function runRestart(): Promise<void> {
|
|
69
|
+
async function runRestart(lease: AgentOperationLease, release: () => Promise<void>): Promise<void> {
|
|
56
70
|
try {
|
|
57
71
|
const result = await restart({
|
|
58
72
|
containerName,
|
|
59
73
|
cwd,
|
|
60
74
|
build,
|
|
75
|
+
operationLease: lease,
|
|
61
76
|
...(currentHostDaemon ? { currentHostDaemon } : {}),
|
|
62
77
|
})
|
|
63
78
|
if (result.ok) onLog({ kind: 'restart-completed', containerName })
|
|
@@ -68,6 +83,8 @@ export function buildSupervisor({ restart, onLog, isStopped }: SupervisorBuildOp
|
|
|
68
83
|
containerName,
|
|
69
84
|
reason: error instanceof Error ? error.message : String(error),
|
|
70
85
|
})
|
|
86
|
+
} finally {
|
|
87
|
+
await release().catch(() => {})
|
|
71
88
|
}
|
|
72
89
|
}
|
|
73
90
|
},
|
package/src/init/dockerfile.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CONTAINER_AGENT_MESSENGER_CONFIG_DIR } from '@/agent-messenger/config-dir'
|
|
1
2
|
import { ASKPASS_SCRIPT, TYPECLAW_GIT_ASKPASS_PATH } from '@/bundled-plugins/github-cli-auth/git-askpass'
|
|
2
3
|
import { validateDockerfileAppendLine } from '@/config/config'
|
|
3
4
|
import type { DockerfileConfig, DockerfileFeatureToggle } from '@/config/config'
|
|
@@ -1380,11 +1381,11 @@ ENV NODE_ENV=production
|
|
|
1380
1381
|
# Persist first-party GWS config without changing global XDG/git config lookup.
|
|
1381
1382
|
ENV GWS_CONFIG_HOME=/agent/workspace/.config/gws
|
|
1382
1383
|
|
|
1383
|
-
# Keep agent-messenger
|
|
1384
|
-
#
|
|
1385
|
-
# credentials live in secrets.json#channels.kakaotalk and
|
|
1386
|
-
# through hostd's secrets-patch RPC.
|
|
1387
|
-
ENV AGENT_MESSENGER_CONFIG_DIR
|
|
1384
|
+
# Keep agent-messenger runtime storage inside workspace/. LINE writes E2EE key
|
|
1385
|
+
# material under line-storage/ here; Instagram stores its live session here.
|
|
1386
|
+
# KakaoTalk credentials instead live in secrets.json#channels.kakaotalk and
|
|
1387
|
+
# container writes go through hostd's secrets-patch RPC.
|
|
1388
|
+
ENV AGENT_MESSENGER_CONFIG_DIR=${CONTAINER_AGENT_MESSENGER_CONFIG_DIR}
|
|
1388
1389
|
|
|
1389
1390
|
${customLines}ENTRYPOINT ["${TYPECLAW_ENTRYPOINT_PATH}"]
|
|
1390
1391
|
CMD ["run"]
|
package/src/init/env-file.ts
CHANGED
|
@@ -3,10 +3,16 @@ import { join } from 'node:path'
|
|
|
3
3
|
|
|
4
4
|
const ENV_FILE = '.env'
|
|
5
5
|
|
|
6
|
-
// Parse the agent's `.env` into a key-value map,
|
|
7
|
-
// `--env-file` parser
|
|
8
|
-
//
|
|
9
|
-
//
|
|
6
|
+
// Parse the agent's `.env` into a key-value map, mirroring Docker's
|
|
7
|
+
// `--env-file` parser (docker/cli `pkg/kvfile.parseKeyValueFile`): a UTF-8 BOM
|
|
8
|
+
// is stripped from the FIRST line only, every line is left-trimmed of Unicode
|
|
9
|
+
// whitespace BEFORE the blank/`#` check, and nothing is trimmed around or after
|
|
10
|
+
// `=` (no quote stripping, no shell expansion). Last value wins on duplicates.
|
|
11
|
+
//
|
|
12
|
+
// The normalization is load-bearing, not cosmetic: Docker accepts a BOM-prefixed
|
|
13
|
+
// or indented `KEY=value` and passes it to the container, so a parser that missed
|
|
14
|
+
// those would report an operator-declared variable as absent — which callers read
|
|
15
|
+
// as "TypeClaw may manage this", e.g. migrating a deliberately pinned directory.
|
|
10
16
|
export function readEnvFile(cwd: string): Map<string, string> {
|
|
11
17
|
const out = new Map<string, string>()
|
|
12
18
|
let raw: string
|
|
@@ -16,18 +22,35 @@ export function readEnvFile(cwd: string): Map<string, string> {
|
|
|
16
22
|
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') return out
|
|
17
23
|
throw err
|
|
18
24
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const key = line.slice(0, eq)
|
|
25
|
-
const value = line.slice(eq + 1)
|
|
26
|
-
out.set(key, value)
|
|
25
|
+
const lines = raw.split(/\r?\n/)
|
|
26
|
+
for (const [index, rawLine] of lines.entries()) {
|
|
27
|
+
const parsed = parseEnvLine(rawLine, index)
|
|
28
|
+
if (parsed === null) continue
|
|
29
|
+
out.set(parsed.key, parsed.value)
|
|
27
30
|
}
|
|
28
31
|
return out
|
|
29
32
|
}
|
|
30
33
|
|
|
34
|
+
// Shared by the reader and the writer so a declaration one of them recognizes
|
|
35
|
+
// can never be invisible to the other — an indented key that parsed as declared
|
|
36
|
+
// but did not match on write would append a duplicate instead of replacing.
|
|
37
|
+
function parseEnvLine(rawLine: string, index: number): { key: string; value: string } | null {
|
|
38
|
+
const withoutBom = index === 0 ? rawLine.replace(/^\uFEFF/, '') : rawLine
|
|
39
|
+
// \p{White_Space}, not \s: JS \s also matches U+FEFF (and misses U+0085),
|
|
40
|
+
// so \s would strip a BOM on EVERY line while Go's unicode.IsSpace — which
|
|
41
|
+
// Docker uses — strips it on line 1 only.
|
|
42
|
+
const line = withoutBom.replace(/^\p{White_Space}+/u, '')
|
|
43
|
+
if (line.length === 0) return null
|
|
44
|
+
if (line.startsWith('#')) return null
|
|
45
|
+
const eq = line.indexOf('=')
|
|
46
|
+
if (eq <= 0) return null
|
|
47
|
+
const key = line.slice(0, eq)
|
|
48
|
+
// Docker rejects the whole file when a key contains a space or tab, so such
|
|
49
|
+
// a line can never reach a container; skip it rather than inventing a key.
|
|
50
|
+
if (/[ \t]/.test(key)) return null
|
|
51
|
+
return { key, value: line.slice(eq + 1) }
|
|
52
|
+
}
|
|
53
|
+
|
|
31
54
|
export function hasEnvKey(cwd: string, key: string): boolean {
|
|
32
55
|
const value = readEnvFile(cwd).get(key)
|
|
33
56
|
return value !== undefined && value.length > 0
|
|
@@ -52,13 +75,14 @@ export function appendOrReplaceEnvKey(cwd: string, key: string, value: string):
|
|
|
52
75
|
// regardless of replace-vs-append path.
|
|
53
76
|
if (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
|
|
54
77
|
let replaced = false
|
|
55
|
-
const next = lines.map((line) => {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (eq <= 0) return line
|
|
59
|
-
if (line.slice(0, eq) !== key) return line
|
|
78
|
+
const next = lines.map((line, index) => {
|
|
79
|
+
const parsed = parseEnvLine(line, index)
|
|
80
|
+
if (parsed === null || parsed.key !== key) return line
|
|
60
81
|
replaced = true
|
|
61
|
-
|
|
82
|
+
// Re-emit the BOM so replacing the first line does not silently strip the
|
|
83
|
+
// file's encoding marker.
|
|
84
|
+
const bom = index === 0 && line.startsWith('\uFEFF') ? '\uFEFF' : ''
|
|
85
|
+
return `${bom}${key}=${value}`
|
|
62
86
|
})
|
|
63
87
|
if (!replaced) next.push(`${key}=${value}`)
|
|
64
88
|
const out = `${next.join('\n')}\n`
|
|
@@ -8,10 +8,20 @@ import {
|
|
|
8
8
|
type InstagramAccount,
|
|
9
9
|
} from 'agent-messenger/instagram'
|
|
10
10
|
|
|
11
|
+
import {
|
|
12
|
+
prepareAgentMessengerHostConfigDir,
|
|
13
|
+
type HostAgentMessengerConfigResult,
|
|
14
|
+
} from '@/agent-messenger/host-config-dir'
|
|
15
|
+
import { type WithAgentOperationLock, withAgentOperationLock } from '@/container/agent-operation-lock'
|
|
11
16
|
import { SecretsInstagramCredentialStore } from '@/secrets/instagram-store'
|
|
12
17
|
|
|
13
18
|
export type InstagramBootstrapStatus = { ok: true } | { ok: false; reason: string }
|
|
14
19
|
|
|
20
|
+
export type AgentMessengerHostConfigDeps = {
|
|
21
|
+
prepareConfigDir?: (agentDir: string) => Promise<HostAgentMessengerConfigResult>
|
|
22
|
+
operationLock?: WithAgentOperationLock
|
|
23
|
+
}
|
|
24
|
+
|
|
15
25
|
// Instagram gates a fresh login behind one of two interactive second factors:
|
|
16
26
|
// a 2FA code (authenticator app / SMS the account already has enabled) or a
|
|
17
27
|
// checkpoint challenge (a suspicious-login verification code sent to email or
|
|
@@ -78,44 +88,58 @@ export function instagramSecretsPath(agentDir: string): string {
|
|
|
78
88
|
return join(agentDir, 'secrets.json')
|
|
79
89
|
}
|
|
80
90
|
|
|
81
|
-
export function
|
|
82
|
-
|
|
91
|
+
export async function runInstagramBootstrap(
|
|
92
|
+
input: InstagramLoginInput,
|
|
93
|
+
deps: AgentMessengerHostConfigDeps = {},
|
|
94
|
+
): Promise<InstagramBootstrapStatus> {
|
|
95
|
+
const operationLock = deps.operationLock ?? withAgentOperationLock
|
|
96
|
+
try {
|
|
97
|
+
const locked = await operationLock({ agentDir: input.agentDir, operation: 'instagram-auth' }, async () => {
|
|
98
|
+
return await runInstagramBootstrapLocked(input, deps)
|
|
99
|
+
})
|
|
100
|
+
return locked.ok ? locked.value : { ok: false, reason: locked.reason }
|
|
101
|
+
} catch (err) {
|
|
102
|
+
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
|
|
103
|
+
}
|
|
83
104
|
}
|
|
84
105
|
|
|
85
|
-
|
|
106
|
+
async function runInstagramBootstrapLocked(
|
|
107
|
+
input: InstagramLoginInput,
|
|
108
|
+
deps: AgentMessengerHostConfigDeps,
|
|
109
|
+
): Promise<InstagramBootstrapStatus> {
|
|
86
110
|
try {
|
|
111
|
+
const prepared = await (deps.prepareConfigDir ?? prepareAgentMessengerHostConfigDir)(input.agentDir)
|
|
112
|
+
if (!prepared.ok) return prepared
|
|
113
|
+
|
|
87
114
|
const store = new SecretsInstagramCredentialStore({
|
|
88
115
|
mode: 'host',
|
|
89
116
|
secretsPath: instagramSecretsPath(input.agentDir),
|
|
90
117
|
})
|
|
91
|
-
const result = await withInstagramConfigDir(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
)
|
|
118
|
+
const result = await withInstagramConfigDir(prepared.hostDir, async (): Promise<InstagramBootstrapStatus> => {
|
|
119
|
+
const sdkManager = new InstagramCredentialManager()
|
|
120
|
+
const manager = input.credentialManager ?? sdkManager
|
|
121
|
+
const accountId = createAccountId(input.username)
|
|
122
|
+
const paths = await manager.ensureAccountPaths(accountId)
|
|
123
|
+
const client = input.client ?? buildInstagramClient(sdkManager)
|
|
124
|
+
client.setSessionPath(paths.session_path)
|
|
125
|
+
|
|
126
|
+
// A checkpoint login persists a partial SDK session (challenge_path +
|
|
127
|
+
// cookies) inside authenticate(), before we know whether the operator
|
|
128
|
+
// can finish it. Snapshot the file up front and restore it on any
|
|
129
|
+
// failure — whether completeLogin returns a non-ok status (fail-closed
|
|
130
|
+
// / cancelled) or throws (e.g. challengeSubmitCode rejecting a wrong
|
|
131
|
+
// code) — so a half-written session is never left behind. Rethrow so
|
|
132
|
+
// the outer catch reports the error.
|
|
133
|
+
const sessionSnapshot = await snapshotSessionFile(paths.session_path)
|
|
134
|
+
try {
|
|
135
|
+
const result = await completeLogin({ store, manager, client, accountId, input })
|
|
136
|
+
if (!result.ok) await restoreSessionFile(paths.session_path, sessionSnapshot)
|
|
137
|
+
return result
|
|
138
|
+
} catch (err) {
|
|
139
|
+
await restoreSessionFile(paths.session_path, sessionSnapshot)
|
|
140
|
+
throw err
|
|
141
|
+
}
|
|
142
|
+
})
|
|
119
143
|
return result
|
|
120
144
|
} catch (err) {
|
|
121
145
|
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { join,
|
|
1
|
+
import { join, resolve } from 'node:path'
|
|
2
2
|
|
|
3
3
|
import { loginFlow as upstreamLoginFlow } from 'agent-messenger/kakaotalk'
|
|
4
4
|
|
|
@@ -33,10 +33,6 @@ export type LoginFlowFn = typeof upstreamLoginFlow
|
|
|
33
33
|
export type LoginFlowOptions = Parameters<LoginFlowFn>[0]
|
|
34
34
|
export type LoginFlowResult = Awaited<ReturnType<LoginFlowFn>>
|
|
35
35
|
|
|
36
|
-
export function kakaotalkConfigDir(agentDir: string): string {
|
|
37
|
-
return posix.join(agentDir, 'workspace', '.agent-messenger')
|
|
38
|
-
}
|
|
39
|
-
|
|
40
36
|
export function kakaotalkSecretsPath(agentDir: string): string {
|
|
41
37
|
return join(agentDir, 'secrets.json')
|
|
42
38
|
}
|
package/src/init/line-auth.ts
CHANGED
|
@@ -2,10 +2,20 @@ import { join } from 'node:path'
|
|
|
2
2
|
|
|
3
3
|
import { LineClient as RealLineClient, LineCredentialManager, type LineLoginResult } from 'agent-messenger/line'
|
|
4
4
|
|
|
5
|
+
import {
|
|
6
|
+
prepareAgentMessengerHostConfigDir,
|
|
7
|
+
type HostAgentMessengerConfigResult,
|
|
8
|
+
} from '@/agent-messenger/host-config-dir'
|
|
9
|
+
import { type WithAgentOperationLock, withAgentOperationLock } from '@/container/agent-operation-lock'
|
|
5
10
|
import { SecretsLineCredentialStore } from '@/secrets/line-store'
|
|
6
11
|
|
|
7
12
|
export type LineBootstrapStatus = { ok: true } | { ok: false; reason: string }
|
|
8
13
|
|
|
14
|
+
export type AgentMessengerHostConfigDeps = {
|
|
15
|
+
prepareConfigDir?: (agentDir: string) => Promise<HostAgentMessengerConfigResult>
|
|
16
|
+
operationLock?: WithAgentOperationLock
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
export type LineLoginCallbacks = {
|
|
10
20
|
onQRUrl?: (url: string) => void | Promise<void>
|
|
11
21
|
onPincode: (pin: string) => void
|
|
@@ -50,36 +60,41 @@ export function lineSecretsPath(agentDir: string): string {
|
|
|
50
60
|
return join(agentDir, 'secrets.json')
|
|
51
61
|
}
|
|
52
62
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
export async function runLineBootstrap(
|
|
64
|
+
input: LineLoginInput,
|
|
65
|
+
deps: AgentMessengerHostConfigDeps = {},
|
|
66
|
+
): Promise<LineBootstrapStatus> {
|
|
67
|
+
const operationLock = deps.operationLock ?? withAgentOperationLock
|
|
68
|
+
try {
|
|
69
|
+
const locked = await operationLock({ agentDir: input.agentDir, operation: 'line-auth' }, async () => {
|
|
70
|
+
return await runLineBootstrapLocked(input, deps)
|
|
71
|
+
})
|
|
72
|
+
return locked.ok ? locked.value : { ok: false, reason: locked.reason }
|
|
73
|
+
} catch (err) {
|
|
74
|
+
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
|
|
75
|
+
}
|
|
62
76
|
}
|
|
63
77
|
|
|
64
|
-
|
|
78
|
+
async function runLineBootstrapLocked(
|
|
79
|
+
input: LineLoginInput,
|
|
80
|
+
deps: AgentMessengerHostConfigDeps,
|
|
81
|
+
): Promise<LineBootstrapStatus> {
|
|
65
82
|
try {
|
|
83
|
+
const prepared = await (deps.prepareConfigDir ?? prepareAgentMessengerHostConfigDir)(input.agentDir)
|
|
84
|
+
if (!prepared.ok) return prepared
|
|
85
|
+
|
|
66
86
|
const store = new SecretsLineCredentialStore({ mode: 'host', secretsPath: lineSecretsPath(input.agentDir) })
|
|
67
87
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
// ~/.config/agent-messenger to keep in sync.
|
|
79
|
-
const client = input.client ?? buildLineClient(store)
|
|
80
|
-
|
|
81
|
-
return suppressLineTokenInfoDump(() =>
|
|
82
|
-
input.method === 'qr'
|
|
88
|
+
const result = await suppressLineTokenInfoDump(() =>
|
|
89
|
+
withLineConfigDir(prepared.hostDir, () => {
|
|
90
|
+
// The SDK writes Letter-Sealing keys under AGENT_MESSENGER_CONFIG_DIR.
|
|
91
|
+
// The prepared host path follows the live container when one is running,
|
|
92
|
+
// so reauth never strands E2EE material in a directory that runtime cannot read.
|
|
93
|
+
const client = input.client ?? buildLineClient(store)
|
|
94
|
+
|
|
95
|
+
// The LINE SDK persists the minted auth_token + certificate by calling
|
|
96
|
+
// setAccount() on the credential manager wired into the client.
|
|
97
|
+
return input.method === 'qr'
|
|
83
98
|
? client.loginWithQR({
|
|
84
99
|
onQRUrl: async (url) => {
|
|
85
100
|
await input.callbacks.onQRUrl?.(url)
|
|
@@ -90,9 +105,9 @@ export async function runLineBootstrap(input: LineLoginInput): Promise<LineBoots
|
|
|
90
105
|
email: input.email,
|
|
91
106
|
password: input.password,
|
|
92
107
|
onPincode: input.callbacks.onPincode,
|
|
93
|
-
})
|
|
94
|
-
)
|
|
95
|
-
|
|
108
|
+
})
|
|
109
|
+
}),
|
|
110
|
+
)
|
|
96
111
|
|
|
97
112
|
if (!result.authenticated || result.account_id === undefined) {
|
|
98
113
|
const reason = result.message ?? result.error ?? 'LINE login did not authenticate'
|
|
@@ -126,11 +141,12 @@ function buildLineClient(store: SecretsLineCredentialStore): LineLoginClient {
|
|
|
126
141
|
|
|
127
142
|
async function withLineConfigDir<T>(dir: string, fn: () => Promise<T>): Promise<T> {
|
|
128
143
|
const previous = process.env.AGENT_MESSENGER_CONFIG_DIR
|
|
129
|
-
|
|
144
|
+
process.env.AGENT_MESSENGER_CONFIG_DIR = dir
|
|
130
145
|
try {
|
|
131
146
|
return await fn()
|
|
132
147
|
} finally {
|
|
133
148
|
if (previous === undefined) delete process.env.AGENT_MESSENGER_CONFIG_DIR
|
|
149
|
+
else process.env.AGENT_MESSENGER_CONFIG_DIR = previous
|
|
134
150
|
}
|
|
135
151
|
}
|
|
136
152
|
|