typeclaw 0.48.12 → 0.48.13
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 +6 -3
- package/src/agent/attention-escalation.ts +8 -5
- package/src/agent/auth-reloadable.ts +2 -3
- package/src/agent/auth.ts +59 -79
- package/src/agent/compaction.ts +2 -2
- package/src/agent/index.ts +31 -15
- package/src/agent/llm-replay-sanitizer.ts +1 -1
- package/src/agent/model-overrides.ts +1 -1
- package/src/agent/multimodal/look-at.ts +3 -3
- package/src/agent/multimodal/read-redirect.ts +1 -1
- package/src/agent/plugin-tools.ts +26 -31
- package/src/agent/reload-tool.ts +1 -1
- package/src/agent/subagents.ts +1 -1
- package/src/agent/tool-error-remediation.ts +1 -1
- package/src/agent/tool-result-budget.ts +2 -2
- package/src/agent/tools/channel-disengage.ts +2 -2
- package/src/agent/tools/channel-edit.ts +2 -2
- package/src/agent/tools/channel-fetch-attachment.ts +2 -2
- package/src/agent/tools/channel-history.ts +2 -2
- package/src/agent/tools/channel-react.ts +2 -2
- package/src/agent/tools/channel-read.ts +2 -2
- package/src/agent/tools/channel-reply.ts +2 -2
- package/src/agent/tools/channel-send.ts +2 -2
- package/src/agent/tools/grant-role.ts +2 -2
- package/src/agent/tools/post-github-review.ts +2 -2
- package/src/agent/tools/restart.ts +2 -2
- package/src/agent/tools/skip-response.ts +2 -2
- package/src/agent/tools/spawn-subagent.ts +2 -2
- package/src/agent/tools/stream-snapshot.ts +2 -2
- package/src/agent/tools/subagent-cancel.ts +2 -2
- package/src/agent/tools/subagent-output.ts +2 -2
- package/src/agent/tools/todo/index.ts +2 -2
- package/src/agent/tools/webfetch/tool.ts +2 -2
- package/src/agent/tools/websearch.ts +2 -2
- package/src/channels/github-review-verdict-coordinator.ts +26 -3
- package/src/channels/router.ts +50 -25
- package/src/cli/model.ts +7 -5
- package/src/cli/oauth-callbacks.ts +38 -23
- package/src/config/config.ts +62 -25
- package/src/config/providers.ts +272 -47
- package/src/init/index.ts +4 -4
- package/src/init/oauth-login.ts +57 -17
- package/src/inspect/transcript-view.ts +6 -3
- package/src/run/channel-session-factory.ts +5 -4
- package/src/run/index.ts +11 -11
- package/src/secrets/refresh-provider-oauth.ts +30 -76
- package/src/secrets/schema.ts +3 -4
- package/src/secrets/storage.ts +133 -234
- package/src/server/index.ts +1 -1
- package/src/sessions/index.ts +1 -1
- package/src/skills/typeclaw-config/SKILL.md +4 -4
- package/src/skills/typeclaw-plugins/SKILL.md +5 -5
- package/src/tui/index.ts +5 -3
- package/src/tui/theme.ts +1 -1
- package/typeclaw.schema.json +32 -1
- package/src/agent/adaptive-thinking-compat.ts +0 -79
- package/src/secrets/oauth-xai.ts +0 -348
package/src/secrets/storage.ts
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
2
2
|
import { dirname } from 'node:path'
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
type AuthStorageBackend,
|
|
7
|
-
AuthStorage as AuthStorageImpl,
|
|
8
|
-
} from '@mariozechner/pi-coding-agent'
|
|
4
|
+
import type { AuthOperationOptions, Credential, CredentialInfo, CredentialStore } from '@earendil-works/pi-ai'
|
|
5
|
+
import { operationSignal, raceWithAbortSignal } from '@earendil-works/pi-ai/utils/abort'
|
|
9
6
|
import lockfile from 'proper-lockfile'
|
|
10
7
|
|
|
11
8
|
import { providerKeyDefaultEnv } from './defaults'
|
|
12
|
-
import {
|
|
13
|
-
import { resolveSecret, type Secret } from './resolve'
|
|
9
|
+
import { resolveSecret } from './resolve'
|
|
14
10
|
import {
|
|
15
11
|
type Channels,
|
|
16
12
|
type GithubCliSecrets,
|
|
@@ -30,112 +26,115 @@ const DIR_MODE = 0o700
|
|
|
30
26
|
const SYNC_LOCK_RETRIES = 10
|
|
31
27
|
const SYNC_LOCK_DELAY_MS = 20
|
|
32
28
|
|
|
33
|
-
|
|
29
|
+
// `realpath: false` matches the sync lock, so both paths always contend on the
|
|
30
|
+
// same `<secrets.json>.lock` even when secrets.json is a symlink; otherwise the
|
|
31
|
+
// shared `stale` threshold below would not make them exclude each other.
|
|
32
|
+
export const ASYNC_LOCK_OPTIONS = {
|
|
34
33
|
retries: { retries: 10, factor: 2, minTimeout: 100, maxTimeout: 10000, randomize: true },
|
|
35
34
|
stale: 30000,
|
|
35
|
+
realpath: false,
|
|
36
36
|
} as const
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// the read-time `readSnapshot`:
|
|
57
|
-
// * Provider unchanged (flatKey === readSnapshot[providerId]) → preserve
|
|
58
|
-
// on-disk Secret bytes verbatim (no flatten, no rewrap). This is the
|
|
59
|
-
// idempotency rule that prevents OAuth-refresh from accidentally
|
|
60
|
-
// persisting env-resolved api-key values into the file.
|
|
61
|
-
// * Provider changed → rewrap as Secret, preserving any prior `env`
|
|
62
|
-
// field the user authored.
|
|
63
|
-
// * Provider added → write as string shorthand (no env binding).
|
|
64
|
-
// * Provider removed → actually remove (do NOT resurrect).
|
|
65
|
-
// - OAuth credentials stay flat strings in the envelope (no Secret
|
|
66
|
-
// wrapping) — they're not env-injectable.
|
|
67
|
-
// - Unknown credential `type` values pass through verbatim, in case
|
|
68
|
-
// upstream adds a third type in a future release.
|
|
69
|
-
// - Empty/missing `key` from AuthStorage on api-key is treated as no-op
|
|
70
|
-
// (preserve prior on-disk Secret if any). The schema requires non-empty
|
|
71
|
-
// `value`, so writing an empty key would corrupt the file at next read.
|
|
72
|
-
//
|
|
73
|
-
// Locking and durability mirror upstream's FileAuthStorageBackend: sync
|
|
74
|
-
// busy-loop retry on ELOCKED to keep callers synchronous, 0o600 file, 0o700
|
|
75
|
-
// parent, atomic temp+rename.
|
|
76
|
-
export class SecretsBackend implements AuthStorageBackend {
|
|
77
|
-
constructor(private readonly secretsPath: string) {}
|
|
78
|
-
|
|
79
|
-
withLock<T>(fn: (current: string | undefined) => { result: T; next?: string }): T {
|
|
80
|
-
this.ensureParentDir()
|
|
81
|
-
this.ensureFileExists()
|
|
82
|
-
let release: (() => void) | undefined
|
|
83
|
-
try {
|
|
84
|
-
release = this.acquireSyncLockWithRetry()
|
|
85
|
-
const envelope = this.readEnvelope()
|
|
86
|
-
const { flatJson, readSnapshot } = flattenProvidersForAuthStorage(envelope.providers, process.env)
|
|
38
|
+
type AsyncLockOptions = Omit<typeof ASYNC_LOCK_OPTIONS, 'stale'> & { stale: number }
|
|
39
|
+
|
|
40
|
+
// The pi CredentialStore contract requires the lock only around serialized
|
|
41
|
+
// read-modify-write operations. Its reads are lock-free snapshots: every writer
|
|
42
|
+
// publishes a complete envelope through writeEnvelopeAtomic's temp-file rename,
|
|
43
|
+
// so they see either the old or new file. This also keeps request-time reads
|
|
44
|
+
// available while an OAuth refresh holds the writer lock across network I/O.
|
|
45
|
+
export class SecretsBackend implements CredentialStore {
|
|
46
|
+
constructor(
|
|
47
|
+
private readonly secretsPath: string,
|
|
48
|
+
private readonly lockOptions: AsyncLockOptions = ASYNC_LOCK_OPTIONS,
|
|
49
|
+
) {}
|
|
50
|
+
|
|
51
|
+
async read(providerId: string, options?: AuthOperationOptions): Promise<Credential | undefined> {
|
|
52
|
+
options?.signal?.throwIfAborted()
|
|
53
|
+
if (!existsSync(this.secretsPath)) return undefined
|
|
54
|
+
return toPiCredential(this.readEnvelope().providers[providerId], process.env)
|
|
55
|
+
}
|
|
87
56
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
} finally {
|
|
95
|
-
release?.()
|
|
96
|
-
}
|
|
57
|
+
async list(options?: AuthOperationOptions): Promise<readonly CredentialInfo[]> {
|
|
58
|
+
options?.signal?.throwIfAborted()
|
|
59
|
+
if (!existsSync(this.secretsPath)) return []
|
|
60
|
+
return Object.entries(this.readEnvelope().providers)
|
|
61
|
+
.filter(([, credential]) => credential.type === 'api_key' || credential.type === 'oauth')
|
|
62
|
+
.map(([providerId, credential]) => ({ providerId, type: credential.type }))
|
|
97
63
|
}
|
|
98
64
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
65
|
+
modify(
|
|
66
|
+
providerId: string,
|
|
67
|
+
fn: (current: Credential | undefined) => Promise<Credential | undefined>,
|
|
68
|
+
options?: AuthOperationOptions,
|
|
69
|
+
): Promise<Credential | undefined> {
|
|
70
|
+
const signal = operationSignal(options?.signal)
|
|
71
|
+
const operation = Promise.resolve().then(() => {
|
|
72
|
+
signal.throwIfAborted()
|
|
73
|
+
this.ensureParentDir()
|
|
74
|
+
this.ensureFileExists()
|
|
75
|
+
return this.withProviderLock(async (assertHeld) => {
|
|
76
|
+
signal.throwIfAborted()
|
|
77
|
+
const envelope = this.readEnvelope()
|
|
78
|
+
const prior = envelope.providers[providerId]
|
|
79
|
+
const next = await fn(toPiCredential(prior, process.env))
|
|
80
|
+
assertHeld()
|
|
81
|
+
signal.throwIfAborted()
|
|
82
|
+
if (next === undefined) return toPiCredential(prior, process.env)
|
|
83
|
+
const credential = fromPiCredential(next, prior, providerId, process.env)
|
|
84
|
+
this.writeEnvelopeAtomic({
|
|
85
|
+
...envelope,
|
|
86
|
+
$schema: envelope.$schema ?? SCHEMA_REL,
|
|
87
|
+
version: SECRETS_FILE_VERSION,
|
|
88
|
+
providers: { ...envelope.providers, [providerId]: credential },
|
|
89
|
+
})
|
|
90
|
+
return toPiCredential(credential, process.env)
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
return raceWithAbortSignal(operation, signal)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
delete(providerId: string, options?: AuthOperationOptions): Promise<void> {
|
|
97
|
+
const signal = operationSignal(options?.signal)
|
|
98
|
+
const operation = Promise.resolve().then(() => {
|
|
99
|
+
signal.throwIfAborted()
|
|
100
|
+
if (!existsSync(this.secretsPath)) return
|
|
101
|
+
return this.withProviderLock(() => {
|
|
102
|
+
signal.throwIfAborted()
|
|
103
|
+
const envelope = this.readEnvelope()
|
|
104
|
+
if (!(providerId in envelope.providers)) return
|
|
105
|
+
const { [providerId]: _removed, ...providers } = envelope.providers
|
|
106
|
+
this.writeEnvelopeAtomic({ ...envelope, providers })
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
return raceWithAbortSignal(operation, signal)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private async withProviderLock<T>(fn: (assertHeld: () => void) => T | Promise<T>): Promise<T> {
|
|
102
113
|
let release: (() => Promise<void>) | undefined
|
|
103
114
|
let lockCompromised = false
|
|
104
115
|
let lockCompromisedError: Error | undefined
|
|
105
|
-
const
|
|
116
|
+
const assertHeld = (): void => {
|
|
106
117
|
if (lockCompromised) {
|
|
107
118
|
throw lockCompromisedError ?? new Error('Secrets store lock was compromised')
|
|
108
119
|
}
|
|
109
120
|
}
|
|
110
121
|
try {
|
|
111
122
|
release = await lockfile.lock(this.secretsPath, {
|
|
112
|
-
...
|
|
123
|
+
...this.lockOptions,
|
|
113
124
|
onCompromised: (err: Error) => {
|
|
114
125
|
lockCompromised = true
|
|
115
126
|
lockCompromisedError = err
|
|
116
127
|
},
|
|
117
128
|
})
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const { result, next } = await fn(flatJson)
|
|
123
|
-
throwIfCompromised()
|
|
124
|
-
if (next !== undefined) {
|
|
125
|
-
const merged = mergeProvidersIntoEnvelope(envelope, next, readSnapshot)
|
|
126
|
-
this.writeEnvelopeAtomic(merged)
|
|
127
|
-
}
|
|
128
|
-
throwIfCompromised()
|
|
129
|
+
assertHeld()
|
|
130
|
+
const result = await fn(assertHeld)
|
|
131
|
+
assertHeld()
|
|
129
132
|
return result
|
|
130
133
|
} finally {
|
|
131
134
|
if (release) {
|
|
132
135
|
try {
|
|
133
136
|
await release()
|
|
134
|
-
} catch {
|
|
135
|
-
// Ignore unlock errors when the lock is compromised — there is
|
|
136
|
-
// nothing useful we can do, and surfacing the secondary error would
|
|
137
|
-
// mask the primary failure (mirrors upstream behaviour).
|
|
138
|
-
}
|
|
137
|
+
} catch {}
|
|
139
138
|
}
|
|
140
139
|
}
|
|
141
140
|
}
|
|
@@ -177,12 +176,9 @@ export class SecretsBackend implements AuthStorageBackend {
|
|
|
177
176
|
release?.()
|
|
178
177
|
}
|
|
179
178
|
}
|
|
180
|
-
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
// to inspect what's on disk without forcing AuthStorage's env-wins flatten —
|
|
184
|
-
// we want to show users which providers are file-backed vs env-overridden,
|
|
185
|
-
// and the flatten path collapses that distinction.
|
|
179
|
+
// Returns a shallow snapshot of providers for host-stage CLI inspection
|
|
180
|
+
// without resolving env overrides, so file-backed versus env-backed state
|
|
181
|
+
// remains visible to the operator.
|
|
186
182
|
tryReadProvidersSync(): Providers {
|
|
187
183
|
if (!existsSync(this.secretsPath)) return {}
|
|
188
184
|
let release: (() => void) | undefined
|
|
@@ -277,7 +273,7 @@ export class SecretsBackend implements AuthStorageBackend {
|
|
|
277
273
|
}
|
|
278
274
|
try {
|
|
279
275
|
release = await lockfile.lock(this.secretsPath, {
|
|
280
|
-
...
|
|
276
|
+
...this.lockOptions,
|
|
281
277
|
onCompromised: (err: Error) => {
|
|
282
278
|
lockCompromised = true
|
|
283
279
|
lockCompromisedError = err
|
|
@@ -328,13 +324,8 @@ export class SecretsBackend implements AuthStorageBackend {
|
|
|
328
324
|
}
|
|
329
325
|
}
|
|
330
326
|
|
|
331
|
-
//
|
|
332
|
-
//
|
|
333
|
-
// merged into `providers.<id>` verbatim — same shape the schema accepts on
|
|
334
|
-
// read. Used by `typeclaw provider add/set` to write api-key credentials
|
|
335
|
-
// without going through AuthStorage's flatten/unflatten round-trip.
|
|
336
|
-
// OAuth flows MUST continue to go through `AuthStorage.login()` so refresh
|
|
337
|
-
// tokens land in the correct shape; this method is api-key oriented.
|
|
327
|
+
// Host-stage CLI API-key write. OAuth login and refresh use CredentialStore
|
|
328
|
+
// through ModelRuntime so rotating token state remains serialized.
|
|
338
329
|
writeProviderCredentialSync(providerId: string, credential: ProviderCredential): void {
|
|
339
330
|
this.ensureParentDir()
|
|
340
331
|
this.ensureFileExists()
|
|
@@ -441,7 +432,7 @@ export class SecretsBackend implements AuthStorageBackend {
|
|
|
441
432
|
}
|
|
442
433
|
try {
|
|
443
434
|
release = await lockfile.lock(this.secretsPath, {
|
|
444
|
-
...
|
|
435
|
+
...this.lockOptions,
|
|
445
436
|
onCompromised: (err: Error) => {
|
|
446
437
|
lockCompromised = true
|
|
447
438
|
lockCompromisedError = err
|
|
@@ -477,18 +468,19 @@ export class SecretsBackend implements AuthStorageBackend {
|
|
|
477
468
|
}
|
|
478
469
|
}
|
|
479
470
|
|
|
471
|
+
// Seeding runs only when the file is absent, so steady-state calls do no
|
|
472
|
+
// I/O. Two processes seeding a first-ever store at once can still race; that
|
|
473
|
+
// window is accepted rather than publishing through link(), which some
|
|
474
|
+
// bind-mounted filesystems reject.
|
|
480
475
|
private ensureFileExists(): void {
|
|
481
|
-
if (existsSync(this.secretsPath))
|
|
482
|
-
const seed = newEmptyEnvelope()
|
|
483
|
-
writeFileSync(this.secretsPath, stringifyEnvelope(seed), 'utf8')
|
|
484
|
-
chmodSync(this.secretsPath, FILE_MODE)
|
|
476
|
+
if (!existsSync(this.secretsPath)) this.writeEnvelopeAtomic(newEmptyEnvelope())
|
|
485
477
|
}
|
|
486
478
|
|
|
487
479
|
private acquireSyncLockWithRetry(): () => void {
|
|
488
480
|
let lastError: unknown
|
|
489
481
|
for (let attempt = 1; attempt <= SYNC_LOCK_RETRIES; attempt++) {
|
|
490
482
|
try {
|
|
491
|
-
return lockfile.lockSync(this.secretsPath, { realpath: false })
|
|
483
|
+
return lockfile.lockSync(this.secretsPath, { realpath: false, stale: this.lockOptions.stale })
|
|
492
484
|
} catch (error) {
|
|
493
485
|
const code =
|
|
494
486
|
typeof error === 'object' && error !== null && 'code' in error
|
|
@@ -522,7 +514,7 @@ export class SecretsBackend implements AuthStorageBackend {
|
|
|
522
514
|
}
|
|
523
515
|
|
|
524
516
|
private writeEnvelopeAtomic(envelope: SecretsFile): void {
|
|
525
|
-
const tmp = `${this.secretsPath}.${process.pid}.${Date.now()}.tmp`
|
|
517
|
+
const tmp = `${this.secretsPath}.${process.pid}.${Date.now()}.${tempSequence++}.tmp`
|
|
526
518
|
writeFileSync(tmp, stringifyEnvelope(envelope), { encoding: 'utf8', mode: FILE_MODE })
|
|
527
519
|
try {
|
|
528
520
|
renameSync(tmp, this.secretsPath)
|
|
@@ -538,11 +530,12 @@ export class SecretsBackend implements AuthStorageBackend {
|
|
|
538
530
|
}
|
|
539
531
|
}
|
|
540
532
|
|
|
541
|
-
export function createSecretsStoreForAgent(secretsPath: string):
|
|
542
|
-
|
|
543
|
-
return AuthStorageImpl.fromStorage(new SecretsBackend(secretsPath))
|
|
533
|
+
export function createSecretsStoreForAgent(secretsPath: string): SecretsBackend {
|
|
534
|
+
return new SecretsBackend(secretsPath)
|
|
544
535
|
}
|
|
545
536
|
|
|
537
|
+
let tempSequence = 0
|
|
538
|
+
|
|
546
539
|
function newEmptyEnvelope(): SecretsFile {
|
|
547
540
|
return { $schema: SCHEMA_REL, version: SECRETS_FILE_VERSION, providers: {}, channels: {}, mcp: {} }
|
|
548
541
|
}
|
|
@@ -551,134 +544,40 @@ function stringifyEnvelope(envelope: SecretsFile): string {
|
|
|
551
544
|
return `${JSON.stringify(envelope, null, 2)}\n`
|
|
552
545
|
}
|
|
553
546
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
//
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
// and never enter the snapshot — they don't participate in env-wins.
|
|
562
|
-
function flattenProvidersForAuthStorage(
|
|
563
|
-
providers: Providers,
|
|
547
|
+
// Preserve an untouched API-key Secret object byte-for-byte. Only an explicit
|
|
548
|
+
// non-empty credential mutation rewrites value while retaining an authored env
|
|
549
|
+
// binding; empty keys remain a no-op because v2 forbids empty Secret values.
|
|
550
|
+
function fromPiCredential(
|
|
551
|
+
next: Credential,
|
|
552
|
+
prior: ProviderCredential | undefined,
|
|
553
|
+
providerId: string,
|
|
564
554
|
env: NodeJS.ProcessEnv,
|
|
565
|
-
):
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
const defaultEnv = providerKeyDefaultEnv(providerId)
|
|
571
|
-
const resolved = resolveSecret(cred.key, defaultEnv, env) ?? cred.key.value ?? ''
|
|
572
|
-
flat[providerId] = { type: 'api_key', key: resolved }
|
|
573
|
-
readSnapshot.set(providerId, resolved)
|
|
574
|
-
} else {
|
|
575
|
-
flat[providerId] = cred
|
|
576
|
-
}
|
|
555
|
+
): ProviderCredential {
|
|
556
|
+
if (next.type === 'oauth') return next as ProviderCredential
|
|
557
|
+
if (!next.key) {
|
|
558
|
+
if (prior) return prior
|
|
559
|
+
throw new Error('Cannot persist an empty API key')
|
|
577
560
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
// decide for each provider whether to:
|
|
584
|
-
// - preserve the prior on-disk Secret bytes verbatim (untouched provider,
|
|
585
|
-
// detected by comparing AuthStorage's flat value to the read-time
|
|
586
|
-
// snapshot, NOT a re-resolution against current env),
|
|
587
|
-
// - reconstruct as Secret with prior `env` preserved (api-key value changed),
|
|
588
|
-
// - write as new shape (provider added),
|
|
589
|
-
// and we drop providers that disappeared from the flat slice (real removal).
|
|
590
|
-
function mergeProvidersIntoEnvelope(
|
|
591
|
-
envelope: SecretsFile,
|
|
592
|
-
nextProvidersJson: string,
|
|
593
|
-
readSnapshot: ReadSnapshot,
|
|
594
|
-
): SecretsFile {
|
|
595
|
-
let parsed: unknown
|
|
596
|
-
try {
|
|
597
|
-
parsed = JSON.parse(nextProvidersJson)
|
|
598
|
-
} catch (err) {
|
|
599
|
-
throw new Error(
|
|
600
|
-
`AuthStorage produced invalid JSON for the providers slice: ${err instanceof Error ? err.message : String(err)}`,
|
|
601
|
-
)
|
|
561
|
+
if (
|
|
562
|
+
prior?.type === 'api_key' &&
|
|
563
|
+
(resolveSecret(prior.key, providerKeyDefaultEnv(providerId), env) ?? prior.key.value) === next.key
|
|
564
|
+
) {
|
|
565
|
+
return prior
|
|
602
566
|
}
|
|
603
|
-
if (!isPlainObject(parsed)) {
|
|
604
|
-
throw new Error('AuthStorage produced a non-object providers slice')
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
const nextProviders: Providers = {}
|
|
608
|
-
for (const [providerId, raw] of Object.entries(parsed)) {
|
|
609
|
-
const reconstructed = reconstructProviderCredential(
|
|
610
|
-
raw,
|
|
611
|
-
envelope.providers[providerId],
|
|
612
|
-
readSnapshot.get(providerId),
|
|
613
|
-
)
|
|
614
|
-
if (reconstructed !== undefined) {
|
|
615
|
-
nextProviders[providerId] = reconstructed
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
|
|
619
567
|
return {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
version: SECRETS_FILE_VERSION,
|
|
623
|
-
providers: nextProviders,
|
|
568
|
+
type: 'api_key',
|
|
569
|
+
key: prior?.type === 'api_key' && prior.key.env ? { value: next.key, env: prior.key.env } : { value: next.key },
|
|
624
570
|
}
|
|
625
571
|
}
|
|
626
572
|
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
// Empty/missing key from AuthStorage on an api-key credential cannot
|
|
639
|
-
// round-trip: the schema requires `value` to be non-empty, so writing
|
|
640
|
-
// `{ value: '' }` would make the file unparseable on next read. Treat
|
|
641
|
-
// it as "no-op" and preserve the prior on-disk Secret if any. A real
|
|
642
|
-
// deletion comes through as the provider being absent from `next`,
|
|
643
|
-
// which is handled by the caller dropping it.
|
|
644
|
-
if (flatKey === '') {
|
|
645
|
-
if (priorOnDisk !== undefined) return priorOnDisk
|
|
646
|
-
return undefined
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
// Idempotency: if AuthStorage's flat key matches the resolved value
|
|
650
|
-
// captured at read time, the credential is untouched. Preserve the
|
|
651
|
-
// on-disk Secret verbatim — including any `env` binding and any string
|
|
652
|
-
// shorthand the user authored. Comparing against the read-time snapshot
|
|
653
|
-
// (not a re-resolution against current process.env) is what makes this
|
|
654
|
-
// safe against env mutations between read and write.
|
|
655
|
-
if (priorOnDisk && priorOnDisk.type === 'api_key' && resolvedAtRead === flatKey) {
|
|
656
|
-
return priorOnDisk
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
// Mutation path: rewrap as Secret, preserving the user's `env` binding
|
|
660
|
-
// when prior was also an api-key so the next boot's env-wins still
|
|
661
|
-
// consults the right variable.
|
|
662
|
-
if (priorOnDisk && priorOnDisk.type === 'api_key' && priorOnDisk.key.env !== undefined) {
|
|
663
|
-
return { type: 'api_key', key: { value: flatKey, env: priorOnDisk.key.env } satisfies Secret }
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
return { type: 'api_key', key: { value: flatKey } }
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
if (type === 'oauth') {
|
|
670
|
-
// OAuth credentials are not env-injectable. Pass through verbatim,
|
|
671
|
-
// preserving every upstream-controlled field (access, refresh, expires,
|
|
672
|
-
// and any future additions covered by the catchall).
|
|
673
|
-
return raw as ProviderCredential
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
// Unknown credential type. Pass through verbatim as a defensive measure
|
|
677
|
-
// against upstream adding a third type in a future release. Better to
|
|
678
|
-
// round-trip user data than drop it.
|
|
679
|
-
return raw as ProviderCredential
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
683
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
573
|
+
// The store returns what is persisted: read() after modify() yields the value
|
|
574
|
+
// just written. Env-over-disk and OAuth-over-env precedence is applied once, by
|
|
575
|
+
// the runtime overlay in src/agent/auth.ts (setRuntimeApiKey). Resolving the
|
|
576
|
+
// provider's default env var here too would duplicate that policy and make a
|
|
577
|
+
// store read return an ambient key instead of the stored one.
|
|
578
|
+
function toPiCredential(credential: ProviderCredential | undefined, env: NodeJS.ProcessEnv): Credential | undefined {
|
|
579
|
+
if (!credential) return undefined
|
|
580
|
+
if (credential.type === 'oauth') return credential as Credential
|
|
581
|
+
const key = resolveSecret(credential.key, undefined, env) ?? credential.key.value
|
|
582
|
+
return key ? { type: 'api_key', key } : undefined
|
|
684
583
|
}
|
package/src/server/index.ts
CHANGED
package/src/sessions/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { mkdirSync } from 'node:fs'
|
|
2
2
|
import { join } from 'node:path'
|
|
3
3
|
|
|
4
|
-
import { SessionManager } from '@
|
|
4
|
+
import { SessionManager } from '@earendil-works/pi-coding-agent'
|
|
5
5
|
|
|
6
6
|
export type SessionFactory = {
|
|
7
7
|
createPersisted(): SessionManager
|
|
@@ -16,7 +16,7 @@ The runtime reads `typeclaw.json` at container startup. Some fields are picked u
|
|
|
16
16
|
- `port` — the TCP port the websocket server binds to inside the container. The TUI on the host stage connects to this. Default `8973`. **Restart-required.**
|
|
17
17
|
- `model` — a fully-qualified `<provider>/<model-id>` string. The runtime resolves this against the built-in provider registry to decide which API to call for every turn. **Live-reloadable.**
|
|
18
18
|
- `models.<profile>` fallback chain — a profile may be an array of refs. The chain advances on failure: cron/batch turns advance on **any** error; interactive turns (TUI, channels, subagents) advance only when the active ref is **throttled/overloaded** (`server_is_overloaded`/`429`/`503`/rate-limit), never mid-turn after output or a tool call, and a repeatedly-throttled ref is skipped for a short cooldown. A single-ref profile fails fast with a provider notice instead of stalling. To make an agent resilient to per-account provider throttling, declare a fallback ref (ideally a different provider/account). **Live-reloadable** (rides `models`).
|
|
19
|
-
- `thinkingLevel` (per-profile) — reasoning effort lives **inside `models`**, not as a top-level field. A profile value may be an object like `{ "model": "<ref>", "thinkingLevel": "high" }` (or `{ "models": ["<ref>", …], "thinkingLevel": … }` for a chain). A session resolves its effort as **the profile's own level → the `default` profile's level → the SDK default (`medium`)** — so the `default` profile's `thinkingLevel` is the de-facto global default, mirroring how `models.default` is the default model. Levels: `off`, `minimal`, `low`, `medium`, `high`, `xhigh`. Clamped to each model's capabilities (`
|
|
19
|
+
- `thinkingLevel` (per-profile) — reasoning effort lives **inside `models`**, not as a top-level field. A profile value may be an object like `{ "model": "<ref>", "thinkingLevel": "high" }` (or `{ "models": ["<ref>", …], "thinkingLevel": … }` for a chain). A session resolves its effort as **the profile's own level → the `default` profile's level → the SDK default (`medium`)** — so the `default` profile's `thinkingLevel` is the de-facto global default, mirroring how `models.default` is the default model. Levels: `off`, `minimal`, `low`, `medium`, `high`, `xhigh`, `max`. Clamped to each model's capabilities (`max` exists only on models that offer it, such as GPT-6 and Claude Opus 5.5; elsewhere it clamps down). An attention-escalation cue in a turn bumps that single turn to `xhigh`, and a profile already at `max` stays at `max`. Bare string/array profile values keep working; a profile becomes an object only when a level is attached. **Live-reloadable** (rides `models`).
|
|
20
20
|
- `mounts` — additional host directories the user has chosen to expose to you. Each entry produces a `docker run -v <hostPath>:/agent/mounts/<name>` flag at `typeclaw start` time, so the directory shows up at `mounts/<name>` inside your agent folder. **The launcher reads this; the running container does not.** Editing `mounts` only takes effect on the next `typeclaw start`. **Restart-required.**
|
|
21
21
|
- `plugins` — array of plugin module specifiers loaded at server boot: npm package names for published plugins, or relative paths for local plugins you are authoring. **Restart-required.**
|
|
22
22
|
- `alias` — additional names the agent answers to when a channel message contains its name in plain text (no `<@id>` mention). The agent folder's directory name (`basename(agentDir)`) is always implicit; `alias` adds further forms (Latin transliteration, nicknames, Korean particles, etc.). Used by the channel engagement layer alongside the structural mention/reply/dm triggers. **Live-reloadable.**
|
|
@@ -48,7 +48,7 @@ You yourself cannot run `typeclaw restart` — that is a host-stage command and
|
|
|
48
48
|
| `$schema` | no | string | Path to `typeclaw.schema.json` for editor autocompletion. Scaffolded as `./node_modules/typeclaw/typeclaw.schema.json`. Leave it alone unless the user moves it. |
|
|
49
49
|
| `port` | no | integer | 1–65535. Defaults to `8973` (T9 spelling of "TYPE"). Change only if the default collides with something on the user's host. **Restart-required.** |
|
|
50
50
|
| `model` | no | string | Must be one of the values listed in the **Allowed models** section below. Defaults to `openai/gpt-5.4-nano`. **Live-reloadable.** |
|
|
51
|
-
| `models.<profile>.thinkingLevel` | no | string | Per-profile reasoning effort (NOT a top-level field): `off` \| `minimal` \| `low` \| `medium` \| `high` \| `xhigh`. Lives in a profile object `{ "model": "<ref>", "thinkingLevel": … }`. Resolves as profile's own → `default` profile's → SDK default (`medium`). Set with `typeclaw model set <profile> <ref> --thinking <level>` or `typeclaw model thinking <level>` (for `default`). **Live-reloadable.**
|
|
51
|
+
| `models.<profile>.thinkingLevel` | no | string | Per-profile reasoning effort (NOT a top-level field): `off` \| `minimal` \| `low` \| `medium` \| `high` \| `xhigh` \| `max`. Lives in a profile object `{ "model": "<ref>", "thinkingLevel": … }`. Resolves as profile's own → `default` profile's → SDK default (`medium`). Set with `typeclaw model set <profile> <ref> --thinking <level>` or `typeclaw model thinking <level>` (for `default`). **Live-reloadable.** |
|
|
52
52
|
| `mounts` | no | array of objects | Host directories bind-mounted into your container. Defaults to `[]` (no host paths exposed). Omitted from scaffolded `typeclaw.json` — add it only when the user wants host paths exposed. See **Mounts** section below. **Restart-required.** |
|
|
53
53
|
| `plugins` | no | array of strings | Plugin module specifiers loaded at server boot: use npm package names for published plugins (for example, `typeclaw-gws-multi-account`) and relative paths only for local plugins you are authoring (for example, `./packages/my-plugin`). Defaults to `[]`. **Restart-required.** Plugin-owned config blocks live alongside as additional top-level keys; see **Plugin config blocks**. |
|
|
54
54
|
| `alias` | no | array of strings | Additional names the agent answers to in channel engagement, on top of the implicit `basename(agentDir)`. Each entry is a non-empty trimmed string matched case-insensitively as a substring of the inbound text. Defaults to `[]`. Hatching populates this with the agent's chosen name. See **Channels and Alias** below for schema/edit mechanics; the matching behavior lives in the `typeclaw-channels` skill. **Live-reloadable.** |
|
|
@@ -435,8 +435,8 @@ Do **not** edit `typeclaw.json` to a model the registry doesn't know, even if th
|
|
|
435
435
|
|
|
436
436
|
**Model boundary:** this section documents operator-owned state; it is not an editing runbook for the agent. Model-driven tools must never read, create, modify, validate, or delete `.env`, `secrets.json`, or `auth.json`, even when the user supplies a value or an acknowledgement flag. Tell the operator to use the TypeClaw host-stage init/provider/channel commands and restart the container. Do not ask them to paste credentials into chat.
|
|
437
437
|
|
|
438
|
-
- **`./secrets.json`** (canonical structured store): a `v2` envelope managed by `SecretsBackend
|
|
439
|
-
- `providers.*` — per-provider credentials. API-key providers store `{ type: 'api_key', key: <Secret> }`. OAuth providers store the
|
|
438
|
+
- **`./secrets.json`** (canonical structured store): a `v2` envelope managed by `SecretsBackend`, pi-ai's `CredentialStore` for `ModelRuntime`. Written by host-stage `typeclaw init`, container-stage OAuth refresh, and explicit user-driven rotation. Two top-level slices:
|
|
439
|
+
- `providers.*` — per-provider credentials. API-key providers store `{ type: 'api_key', key: <Secret> }`. OAuth providers store the pi-ai token record `{ type: 'oauth', access, refresh, expires, ... }`. The container auto-refreshes OAuth tokens under the file lock; api-key writes only happen on explicit user-driven rotation.
|
|
440
440
|
- `channels.*` — per-adapter credentials, with named fields per adapter:
|
|
441
441
|
- `discord-bot: { token: <Secret> }`
|
|
442
442
|
- `slack-bot: { botToken: <Secret>, appToken: <Secret> }`
|
|
@@ -16,14 +16,14 @@ This skill covers BOTH authoring new plugins AND operating existing ones (config
|
|
|
16
16
|
Three layers, sharply separated:
|
|
17
17
|
|
|
18
18
|
```
|
|
19
|
-
Plugin API (typeclaw/plugin) ← plugins live here. NO @
|
|
19
|
+
Plugin API (typeclaw/plugin) ← plugins live here. NO @earendil-works/* imports.
|
|
20
20
|
↓
|
|
21
21
|
TypeClaw runtime (src/plugin, src/agent, src/run, src/server, src/cron)
|
|
22
22
|
↓
|
|
23
|
-
Engine (@
|
|
23
|
+
Engine (@earendil-works/pi-coding-agent) ← never visible to plugins
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
**MUST NOT** import anything from `@
|
|
26
|
+
**MUST NOT** import anything from `@earendil-works/*` in plugin code. The single bridge file is `src/agent/plugin-tools.ts` (runtime layer, not plugin layer). The boundary is enforced by convention — no lint rule today, but `grep` confirms no `src/plugin/**` file imports `@earendil-works/*`.
|
|
27
27
|
|
|
28
28
|
**Allowed plugin imports**: `typeclaw/plugin`, `zod`, Node built-ins, your own modules.
|
|
29
29
|
|
|
@@ -761,7 +761,7 @@ When something goes wrong, you'll see one of these. Memorize the patterns.
|
|
|
761
761
|
|
|
762
762
|
```ts
|
|
763
763
|
// WRONG — boundary violation
|
|
764
|
-
import { something } from '@
|
|
764
|
+
import { something } from '@earendil-works/pi-coding-agent'
|
|
765
765
|
```
|
|
766
766
|
|
|
767
767
|
**Plugins use `typeclaw/plugin` only.** The runtime translates to engine types behind the scenes.
|
|
@@ -974,4 +974,4 @@ export default definePlugin({
|
|
|
974
974
|
|
|
975
975
|
**`exec → LLM` from cron** (best practice): plugin `cronJobs` entry with `kind: 'handler'` — a TypeScript function the cron consumer invokes directly with `ctx.prompt` / `ctx.subagent` / `ctx.exec`. No shell-out, no WS round-trip. Fall back to `surface: 'container'` command + cron `exec` pointing at `["typeclaw", "<cmd>"]` ONLY when the same logic must also be invocable as a reusable CLI command, the user owns the cadence for someone else's command, or the work needs `surface: 'host'`.
|
|
976
976
|
|
|
977
|
-
**Boundary**: `src/plugin/**` MUST NOT import `@
|
|
977
|
+
**Boundary**: `src/plugin/**` MUST NOT import `@earendil-works/*`.
|
package/src/tui/index.ts
CHANGED
|
@@ -7,8 +7,8 @@ import {
|
|
|
7
7
|
ProcessTerminal,
|
|
8
8
|
type Terminal,
|
|
9
9
|
Text,
|
|
10
|
-
|
|
11
|
-
} from '@
|
|
10
|
+
TuiMainScreen,
|
|
11
|
+
} from '@earendil-works/pi-tui'
|
|
12
12
|
|
|
13
13
|
import { parseCommand } from '@/commands'
|
|
14
14
|
|
|
@@ -97,7 +97,9 @@ export function createTui({
|
|
|
97
97
|
}: TuiOptions) {
|
|
98
98
|
async function run(): Promise<TuiRunResult> {
|
|
99
99
|
const terminal = createTerminal()
|
|
100
|
-
|
|
100
|
+
// pi-tui 0.87 exports TUI only as an interface; TuiMainScreen is the
|
|
101
|
+
// concrete scrollback renderer (dist/tui.d.ts:213-244, tui-main-screen.d.ts:11-12).
|
|
102
|
+
const tui = new TuiMainScreen(terminal)
|
|
101
103
|
const displayUrl = redactUrl(url)
|
|
102
104
|
|
|
103
105
|
const status = new Text(colors.dim(`connecting to ${displayUrl}...`), 0, 0)
|
package/src/tui/theme.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EditorTheme, MarkdownTheme } from '@
|
|
1
|
+
import type { EditorTheme, MarkdownTheme } from '@earendil-works/pi-tui'
|
|
2
2
|
|
|
3
3
|
const wrap = (code: string) => (text: string) => `\x1b[${code}m${text}\x1b[0m`
|
|
4
4
|
const wrapRgb = (r: number, g: number, b: number) => (text: string) => `\x1b[38;2;${r};${g};${b}m${text}\x1b[0m`
|