diff --git a/.gitignore b/.gitignore index bee8a64..af4461c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ __pycache__ +# local test-run logs captured from stdout (e.g. oauth-test-output.txt) +*-test-output.txt diff --git a/ai/liii/pi/ai/auth.scm b/ai/liii/pi/ai/auth.scm index 90bd2d3..80ede21 100644 --- a/ai/liii/pi/ai/auth.scm +++ b/ai/liii/pi/ai/auth.scm @@ -262,38 +262,18 @@ ) ;and ) ;define - ;; oauth 凭据 → access token:未过期直接返回,过期透明刷新并落盘; - ;; config-dir 缺失时回退默认配置目录(落盘/清除回调注入 resolve 层, - ;; 避免循环依赖) + ;; 配置解析只返回凭据快照中的 access,不在启动/切模时联网刷新。 + ;; 请求前的 request-oauth-key 钩子统一检查 expires 并更新内存 store。 (define (%resolve-oauth-credential-key cred opt-env opt-config-dir) - (let ((dir - (or (and (pair? opt-config-dir) (car opt-config-dir)) - (path->string (path-join (path-home) ".gf" "agent")) - ) ;or - ) ;dir - ) ; - (pi-resolve-oauth-credential-key cred - opt-env - (lambda (c) - (pi-save-auth-oauth! (pi-credential-provider c) - (pi-credential-access c) - (pi-credential-refresh c) - (pi-credential-expires c) - (pi-credential-region c) - dir - ) ;pi-save-auth-oauth! - ) ;lambda - (lambda (p) (pi-remove-auth-provider! p dir)) - ) ;pi-resolve-oauth-credential-key - ) ;let + (pi-credential-access cred) ) ;define - ;; 解析服务商生效密钥。opt 尾参兼容两种形态: - ;; (provider auth-alist) 旧形态 - ;; (provider auth-alist env) env 为 '((name . val) ...) - ;; (provider auth-alist env config-dir) 新增:oauth 刷新落盘目录 - ;; oauth 凭据:access 未过期直接返回,过期透明刷新(见 - ;; %resolve-oauth-credential-key);env API Key 优先于一切 auth 条目 + ;; 解析服务商生效密钥。opt 尾参兼容旧调用形态: + ;; (provider auth-alist) + ;; (provider auth-alist env) + ;; (provider auth-alist env config-dir) + ;; oauth 仅返回内存快照中的 access;请求前由动态钩子按需刷新。 + ;; env API Key 优先于一切 auth 条目。 (define (pi-resolve-provider-key provider auth-alist . opt) (let* ((p-str (if (symbol? provider) (symbol->string provider) provider)) (opt-env diff --git a/ai/liii/pi/ai/auth/oauth/store.scm b/ai/liii/pi/ai/auth/oauth/store.scm new file mode 100644 index 0000000..01c2e4a --- /dev/null +++ b/ai/liii/pi/ai/auth/oauth/store.scm @@ -0,0 +1,82 @@ +;; +;; Copyright (C) 2026 The Goldfish Scheme Authors +;; +;; Licensed under the MIT License +;; + +;; OAuth 运行时凭据存储:与请求配置分离,按 provider 保存最新凭据。 +;; store 自身可随不可变配置副本共享;刷新只替换 store 中的凭据,不修改 +;; 配置对象,也不写 auth.json。 +(define-library (liii pi ai auth oauth store) + (import (scheme base) (liii list) (liii pi ai config credential)) + (export pi-make-oauth-store pi-oauth-store? pi-oauth-store-ref + pi-oauth-store-set! pi-oauth-store-load! pi-oauth-store-remove! + pi-oauth-store-clear! + ) ;export + (begin + + ;; 单元素 vector 保存凭据 alist。替换条目而非修改凭据 record。 + (define (pi-make-oauth-store . opt-credentials) + (vector (if (pair? opt-credentials) (car opt-credentials) '())) + ) ;define + + (define (pi-oauth-store? value) + (and (vector? value) (= (vector-length value) 1)) + ) ;define + + (define (pi-oauth-store-ref store provider) + (let ((p-str (if (symbol? provider) (symbol->string provider) provider))) + (pi-find-credential p-str (vector-ref store 0)) + ) ;let + ) ;define + + (define (pi-find-credential provider credentials) + (find + (lambda (cred) + (and (pi-credential-oauth? cred) + (equal? (pi-credential-provider cred) provider) + ) ;and + ) ;lambda + credentials + ) ;find + ) ;define + + (define (pi-oauth-store-set! store cred) + (let ((provider (pi-credential-provider cred))) + (vector-set! store + 0 + (cons cred + (filter + (lambda (old) (not (equal? (pi-credential-provider old) provider))) + (vector-ref store 0) + ) ;filter + ) ;cons + ) ;vector-set! + ) ;let + cred + ) ;define + + (define (pi-oauth-store-load! store credentials) + (vector-set! store 0 (filter pi-credential-oauth? credentials)) + store + ) ;define + + (define (pi-oauth-store-remove! store provider) + (let ((p-str (if (symbol? provider) (symbol->string provider) provider))) + (vector-set! store + 0 + (filter + (lambda (cred) (not (equal? (pi-credential-provider cred) p-str))) + (vector-ref store 0) + ) ;filter + ) ;vector-set! + ) ;let + #f + ) ;define + + (define (pi-oauth-store-clear! store) + (vector-set! store 0 '()) + ) ;define + + ) ;begin +) ;define-library diff --git a/ai/liii/pi/ai/keyring.scm b/ai/liii/pi/ai/keyring.scm index 69c6fa4..520753e 100644 --- a/ai/liii/pi/ai/keyring.scm +++ b/ai/liii/pi/ai/keyring.scm @@ -28,8 +28,13 @@ ;; 每次请求开始时取本次使用的 key: ;; 无钩子时回退 config 的 api-key(auth.json 单 key 路径)。 (define (pi-request-api-key config) - (let ((hook (%config-ref config 'request-key))) - (if (procedure? hook) (hook config) (%config-ref config 'api-key)) + (let ((hook (%config-ref config 'request-key)) + (oauth-hook (%config-ref config 'request-oauth-key)) + ) ; + (if (procedure? hook) + (hook config) + (if (procedure? oauth-hook) (oauth-hook config) (%config-ref config 'api-key)) + ) ;if ) ;let ) ;define diff --git a/ai/tests/liii/pi/ai/auth/oauth/store/README.md b/ai/tests/liii/pi/ai/auth/oauth/store/README.md new file mode 100644 index 0000000..f50f233 --- /dev/null +++ b/ai/tests/liii/pi/ai/auth/oauth/store/README.md @@ -0,0 +1,32 @@ +# (liii pi ai auth oauth store) 模块函数索引 + +`(liii pi ai auth oauth store)` 提供 OAuth 运行时凭据存储:与请求配置分离,按 provider 保存最新凭据。store 可随不可变配置副本共享;刷新只替换 store 中的凭据,不修改配置对象,也不写 auth.json。 + +使用时导入: + +```scheme +(import (liii pi ai auth oauth store)) +``` + +## 查看函数文档 + +使用以下命令查看函数文档和测试用例: + +```bash +gf doc pi-oauth-store-ref +``` + +## 函数分类索引 + +### 创建与判定 +- `pi-make-oauth-store` - 创建 store,可选初始凭据列表 +- `pi-oauth-store?` - 判断值是否为 store(单元素 vector) + +### 读取 +- `pi-oauth-store-ref` - 按 provider 精确匹配 oauth 凭据 + +### 更新 +- `pi-oauth-store-set!` - 替换同 provider 条目 +- `pi-oauth-store-load!` - 用凭据列表整体重载(只保留 oauth) +- `pi-oauth-store-remove!` - 移除指定 provider 条目 +- `pi-oauth-store-clear!` - 清空全部凭据 diff --git a/ai/tests/liii/pi/ai/auth/oauth/store/pi-make-oauth-store-test.scm b/ai/tests/liii/pi/ai/auth/oauth/store/pi-make-oauth-store-test.scm new file mode 100644 index 0000000..0ba1b60 --- /dev/null +++ b/ai/tests/liii/pi/ai/auth/oauth/store/pi-make-oauth-store-test.scm @@ -0,0 +1,50 @@ +;; pi-make-oauth-store +;; 创建 OAuth 运行时凭据 store:单元素 vector 保存凭据列表, +;; 可随不可变配置副本共享;替换条目而非修改凭据 record。 +;; +;; 语法 +;; ---- +;; (pi-make-oauth-store) +;; (pi-make-oauth-store credentials) +;; +;; 参数 +;; ---- +;; credentials : list? +;; 可选,初始凭据列表(缺省为空表)。 +;; +;; 返回值 +;; ---- +;; pi-oauth-store? +;; 新建 store;给定 credentials 时可按 provider 查到其中的 oauth 凭据。 + +(import (liii check) + (liii pi ai config credential) + (liii pi ai auth oauth store) +) ;import + +(check-set-mode! 'report-failed) + +;; 无参:空 store。 +(let ((store (pi-make-oauth-store))) + (check-true (pi-oauth-store? store)) + (check (pi-oauth-store-ref store "kimi-coding") => #f) +) ;let + +;; 传入初始凭据列表:可按 provider 查到。 +(let* ((cred (pi-credential "kimi-coding" #f "oauth" "acc" "ref" 4102444800000 #f)) + (store (pi-make-oauth-store (list cred))) + ) ; + (check-true (pi-oauth-store? store)) + (check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) => "acc") + (check (pi-oauth-store-ref store "google") => #f) +) ;let* + +;; 每次调用返回独立 store,互不影响。 +(let ((a (pi-make-oauth-store)) (b (pi-make-oauth-store))) + (pi-oauth-store-set! a + (pi-credential "kimi-coding" #f "oauth" "acc-a" "ref" 4102444800000 #f) + ) ;pi-oauth-store-set! + (check (pi-oauth-store-ref b "kimi-coding") => #f) +) ;let + +(check-report) diff --git a/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-clear-bang-test.scm b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-clear-bang-test.scm new file mode 100644 index 0000000..ccaa1c5 --- /dev/null +++ b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-clear-bang-test.scm @@ -0,0 +1,51 @@ +;; pi-oauth-store-clear! +;; 清空 store 中的全部凭据;store 身份不变,共享该 store 的 +;; 配置副本随之看到空 store。 +;; +;; 语法 +;; ---- +;; (pi-oauth-store-clear! store) +;; +;; 参数 +;; ---- +;; store : pi-oauth-store? +;; OAuth 运行时凭据 store。 +;; +;; 返回值 +;; ---- +;; 未指定 +;; 仅原地清空,不依赖返回值。 + +(import (liii check) + (liii pi ai config credential) + (liii pi ai auth oauth store) +) ;import + +(check-set-mode! 'report-failed) + +(let ((store (pi-make-oauth-store (list (pi-credential "kimi-coding" #f "oauth" "acc-k" "ref" 1000 #f) + (pi-credential "google" #f "oauth" "acc-g" "ref" 2000 #f) + ) ;list + ) ;pi-make-oauth-store + ) ;store + ) ; + (pi-oauth-store-clear! store) + (check (pi-oauth-store-ref store "kimi-coding") => #f) + (check (pi-oauth-store-ref store "google") => #f) + ;; 清空后仍可正常写入。 + (pi-oauth-store-set! store + (pi-credential "kimi-coding" #f "oauth" "acc-new" "ref" 4102444800000 #f) + ) ;pi-oauth-store-set! + (check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) + => + "acc-new" + ) ;check +) ;let + +;; 空 store 上清空幂等。 +(let ((store (pi-make-oauth-store))) + (pi-oauth-store-clear! store) + (check-true (pi-oauth-store? store)) +) ;let + +(check-report) diff --git a/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-load-bang-test.scm b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-load-bang-test.scm new file mode 100644 index 0000000..c653481 --- /dev/null +++ b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-load-bang-test.scm @@ -0,0 +1,55 @@ +;; pi-oauth-store-load! +;; 用凭据列表整体重载 store:只保留 oauth 类型凭据, +;; 替换原有全部内容(/login 后 reload 场景)。 +;; +;; 语法 +;; ---- +;; (pi-oauth-store-load! store credentials) +;; +;; 参数 +;; ---- +;; store : pi-oauth-store? +;; OAuth 运行时凭据 store。 +;; +;; credentials : list? +;; 新凭据列表;非 oauth 凭据被过滤。 +;; +;; 返回值 +;; ---- +;; pi-oauth-store? +;; 传入的 store(原地重载)。 + +(import (liii check) + (liii pi ai config credential) + (liii pi ai auth oauth store) +) ;import + +(check-set-mode! 'report-failed) + +(let* ((store (pi-make-oauth-store (list (pi-credential "old" #f "oauth" "acc-old" "ref" 1000 #f)) + ) ;pi-make-oauth-store + ) ;store + (loaded (pi-oauth-store-load! store + (list (pi-credential "kimi-coding" #f "oauth" "acc" "ref" 4102444800000 #f) + (pi-credential "google" "sk-google") + ) ;list + ) ;pi-oauth-store-load! + ) ;loaded + ) ; + (check loaded => store) + ;; 旧条目被整体替换,api_key 凭据被过滤。 + (check (pi-oauth-store-ref store "old") => #f) + (check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) => "acc") + (check (pi-oauth-store-ref store "google") => #f) +) ;let* + +;; 空列表清空 store。 +(let ((store (pi-make-oauth-store (list (pi-credential "kimi-coding" #f "oauth" "acc" "ref" 1000 #f)) + ) ;pi-make-oauth-store + ) ;store + ) ; + (pi-oauth-store-load! store '()) + (check (pi-oauth-store-ref store "kimi-coding") => #f) +) ;let + +(check-report) diff --git a/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-p-test.scm b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-p-test.scm new file mode 100644 index 0000000..0ed773e --- /dev/null +++ b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-p-test.scm @@ -0,0 +1,30 @@ +;; pi-oauth-store? +;; 判断值是否为 OAuth 运行时凭据 store(单元素 vector)。 +;; +;; 语法 +;; ---- +;; (pi-oauth-store? value) +;; +;; 参数 +;; ---- +;; value : any? +;; 任意值。 +;; +;; 返回值 +;; ---- +;; boolean? +;; 单元素 vector(含 pi-make-oauth-store 的结果)为 #t,其余为 #f。 + +(import (liii check) (liii pi ai auth oauth store)) + +(check-set-mode! 'report-failed) + +(check-true (pi-oauth-store? (pi-make-oauth-store))) +(check-true (pi-oauth-store? (vector '()))) +(check-false (pi-oauth-store? (vector '() '()))) +(check-false (pi-oauth-store? (vector))) +(check-false (pi-oauth-store? '())) +(check-false (pi-oauth-store? #f)) +(check-false (pi-oauth-store? "store")) + +(check-report) diff --git a/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-ref-test.scm b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-ref-test.scm new file mode 100644 index 0000000..5eb26b1 --- /dev/null +++ b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-ref-test.scm @@ -0,0 +1,46 @@ +;; pi-oauth-store-ref +;; 按 provider 精确匹配 store 中的 oauth 凭据;provider 可为 symbol。 +;; api_key 类型凭据即使同 provider 也不返回。 +;; +;; 语法 +;; ---- +;; (pi-oauth-store-ref store provider) +;; +;; 参数 +;; ---- +;; store : pi-oauth-store? +;; OAuth 运行时凭据 store。 +;; +;; provider : (or string? symbol?) +;; 服务商标识。 +;; +;; 返回值 +;; ---- +;; (or pi-credential? #f) +;; 匹配的 oauth 凭据;不存在时返回 #f。 + +(import (liii check) + (liii pi ai config credential) + (liii pi ai auth oauth store) +) ;import + +(check-set-mode! 'report-failed) + +(define KIMI-OAUTH + (pi-credential "kimi-coding" #f "oauth" "acc" "ref" 4102444800000 #f) +) ;define + +(let ((store (pi-make-oauth-store (list KIMI-OAUTH (pi-credential "google" "sk-google"))) + ) ;store + ) ; + ;; string 与 symbol 两种 provider 写法等价。 + (check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) => "acc") + (check (pi-credential-access (pi-oauth-store-ref store 'kimi-coding)) => "acc") + ;; api_key 凭据不匹配 oauth 查找。 + (check (pi-oauth-store-ref store "google") => #f) + (check (pi-oauth-store-ref store "missing") => #f) +) ;let + +(check (pi-oauth-store-ref (pi-make-oauth-store) "kimi-coding") => #f) + +(check-report) diff --git a/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-remove-bang-test.scm b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-remove-bang-test.scm new file mode 100644 index 0000000..317e2b5 --- /dev/null +++ b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-remove-bang-test.scm @@ -0,0 +1,45 @@ +;; pi-oauth-store-remove! +;; 从 store 中移除指定 provider 的凭据(invalid_grant / 登出场景); +;; 其他 provider 的条目保持不变,provider 可为 symbol。 +;; +;; 语法 +;; ---- +;; (pi-oauth-store-remove! store provider) +;; +;; 参数 +;; ---- +;; store : pi-oauth-store? +;; OAuth 运行时凭据 store。 +;; +;; provider : (or string? symbol?) +;; 要移除的服务商标识。 +;; +;; 返回值 +;; ---- +;; #f +;; 固定返回 #f;移除不存在的 provider 不报错。 + +(import (liii check) + (liii pi ai config credential) + (liii pi ai auth oauth store) +) ;import + +(check-set-mode! 'report-failed) + +(let ((store (pi-make-oauth-store (list (pi-credential "kimi-coding" #f "oauth" "acc-k" "ref" 1000 #f) + (pi-credential "google" #f "oauth" "acc-g" "ref" 2000 #f) + ) ;list + ) ;pi-make-oauth-store + ) ;store + ) ; + (check (pi-oauth-store-remove! store "kimi-coding") => #f) + (check (pi-oauth-store-ref store "kimi-coding") => #f) + ;; 其他 provider 不受影响。 + (check (pi-credential-access (pi-oauth-store-ref store "google")) => "acc-g") + ;; symbol provider 等价;移除不存在的 provider 幂等。 + (check (pi-oauth-store-remove! store 'google) => #f) + (check (pi-oauth-store-remove! store 'google) => #f) + (check (pi-oauth-store-ref store "google") => #f) +) ;let + +(check-report) diff --git a/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-set-bang-test.scm b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-set-bang-test.scm new file mode 100644 index 0000000..30cc37d --- /dev/null +++ b/ai/tests/liii/pi/ai/auth/oauth/store/pi-oauth-store-set-bang-test.scm @@ -0,0 +1,57 @@ +;; pi-oauth-store-set! +;; 替换 store 中同 provider 的凭据条目(不修改凭据 record 本身), +;; 其他 provider 的条目保持不变;共享同一 store 的配置副本可见更新。 +;; +;; 语法 +;; ---- +;; (pi-oauth-store-set! store cred) +;; +;; 参数 +;; ---- +;; store : pi-oauth-store? +;; OAuth 运行时凭据 store。 +;; +;; cred : pi-credential? +;; 新凭据;provider 相同的旧条目被替换。 +;; +;; 返回值 +;; ---- +;; pi-credential? +;; 传入的 cred。 + +(import (liii check) + (liii pi ai config credential) + (liii pi ai auth oauth store) +) ;import + +(check-set-mode! 'report-failed) + +(let* ((old (pi-credential "kimi-coding" #f "oauth" "acc-1" "ref-1" 1000 #f)) + (other (pi-credential "google" #f "oauth" "acc-g" "ref-g" 2000 #f)) + (store (pi-make-oauth-store (list old other))) + (new (pi-credential "kimi-coding" #f "oauth" "acc-2" "ref-2" 3000 #f)) + ) ; + (check (pi-oauth-store-set! store new) => new) + ;; 同 provider 条目被替换,其余保留。 + (check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) + => + "acc-2" + ) ;check + (check (pi-credential-refresh (pi-oauth-store-ref store "kimi-coding")) + => + "ref-2" + ) ;check + (check (pi-credential-access (pi-oauth-store-ref store "google")) => "acc-g") + ;; 原凭据 record 未被修改(替换而非原地变更)。 + (check (pi-credential-access old) => "acc-1") +) ;let* + +;; 空 store 上新增 provider。 +(let ((store (pi-make-oauth-store))) + (pi-oauth-store-set! store + (pi-credential "kimi-coding" #f "oauth" "acc" "ref" 4102444800000 #f) + ) ;pi-oauth-store-set! + (check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) => "acc") +) ;let + +(check-report) diff --git a/ai/tests/liii/pi/ai/auth/pi-resolve-provider-key-oauth-test.scm b/ai/tests/liii/pi/ai/auth/pi-resolve-provider-key-oauth-test.scm index 51dff40..f38b37a 100644 --- a/ai/tests/liii/pi/ai/auth/pi-resolve-provider-key-oauth-test.scm +++ b/ai/tests/liii/pi/ai/auth/pi-resolve-provider-key-oauth-test.scm @@ -1,37 +1,32 @@ ;; pi-resolve-provider-key-oauth -;; pi-resolve-provider-key 的 oauth 分支:未过期返回 access、过期透明刷新 -;; 落盘、401 清除凭据、env API Key 优先(1402 测试项 2/4/5 的单元化)。 +;; 配置解析只读取 OAuth 凭据快照,不联网刷新、不写 auth.json。 +;; 过期与 401 的刷新由请求前 request-oauth-key 钩子统一处理。 ;; ;; 语法 ;; ---- -;; (pi-resolve-provider-key provider auth-alist) -;; (pi-resolve-provider-key provider auth-alist opt-env opt-config-dir) +;; (pi-resolve-provider-key provider credentials opt-env opt-config-dir) ;; ;; 参数 ;; ---- ;; provider : string? -;; 服务商 id(kimi-coding)。 +;; 服务商标识(如 "kimi-coding")。 ;; -;; auth-alist : list? -;; pi-load-auth 返回的凭据列表。 +;; credentials : list? +;; pi-load-auth 读取的凭据列表。 ;; ;; opt-env : list? -;; 自定义环境变量表。 +;; 环境变量 alist;命中的 API Key 环境变量优先于 OAuth 凭据。 ;; -;; opt-config-dir : string? -;; oauth 刷新落盘目录。 +;; opt-config-dir : (or string? path?) +;; 配置目录,用于探测 auth.json;本函数不写文件。 ;; ;; 返回值 ;; ---- -;; string? 或 #f -;; 生效 access token / API Key;刷新失败 #f。 +;; (or string? #f) +;; 生效的 API Key / access token;无凭据时返回 #f。 -(import (scheme base) - (liii check) - (liii string) - (liii list) +(import (liii check) (liii path) - (liii subprocess) (liii tempfile) (liii pi ai time) (liii pi ai config credential) @@ -40,21 +35,7 @@ (check-set-mode! 'report-failed) -(define (parse-port-line line) - (if (and (string? line) (string-starts? line "PORT:")) - (string->number (string-trim-both (substring line 5 (string-length line)))) - #f - ) ;if -) ;define - -(define stub-file - (if (file-exists? "tests/stub_kimi_oauth_server.py") - "tests/stub_kimi_oauth_server.py" - "ai/tests/stub_kimi_oauth_server.py" - ) ;if -) ;define - -;; 1. oauth 未过期:直接返回 access(无网络) +;; 未过期:直接返回 access;env API Key 仍优先。 (let* ((tmp (make-temp-dir "gf-resolve-oauth-" "")) (_ (pi-save-auth-oauth! "kimi-coding" "fresh-acc" @@ -67,7 +48,6 @@ (creds (pi-load-auth tmp)) ) ; (check (pi-resolve-provider-key "kimi-coding" creds '() tmp) => "fresh-acc") - ;; env API Key 优先于 oauth 凭据(测试项 5 回归) (check (pi-resolve-provider-key "kimi-coding" creds '(("KIMI_API_KEY" . "sk-env")) tmp) => @@ -77,65 +57,23 @@ (path-rmdir tmp) ) ;let* -;; 2. oauth 已过期:透明刷新并落盘新 access/refresh/expires(测试项 4) -(let* ((proc (process-spawn (list "python3" stub-file "refresh-ok") :stdout 'pipe)) - (port (parse-port-line (process-read-line proc))) - (host (string-append "http://127.0.0.1:" (number->string port))) - (tmp (make-temp-dir "gf-resolve-oauth-" "")) - (_ (pi-save-auth-oauth! "kimi-coding" - "old-acc" - "stub-refresh-token" - (- (pi-current-time-millis) 1000) - host - tmp - ) ;pi-save-auth-oauth! - ) ;_ - (creds (pi-load-auth tmp)) - ) ; - (check (pi-resolve-provider-key "kimi-coding" creds '() tmp) - => - "stub-access-token-2" - ) ;check - ;; 落盘验证:重新加载后 access/refresh 已轮换,region 保留 - (let* ((creds2 (pi-load-auth tmp)) - (c - (find (lambda (x) (equal? (pi-credential-provider x) "kimi-coding")) creds2) - ) ;c - ) ; - (check (pi-credential-access c) => "stub-access-token-2") - (check (pi-credential-refresh c) => "stub-refresh-token-2") - (check (pi-credential-region c) => host) - (check-true (> (pi-credential-expires c) (pi-current-time-millis))) - ;; 新凭据未过期:再次解析不触发网络 - (check (pi-resolve-provider-key "kimi-coding" creds2 '() tmp) - => - "stub-access-token-2" - ) ;check - ) ;let* - (process-kill proc) - (path-unlink (path (path-join tmp "auth.json"))) - (path-rmdir tmp) -) ;let* - -;; 3. 刷新 401 invalid_grant:清除凭据并返回 #f(测试项 4 后半) -(let* ((proc (process-spawn (list "python3" stub-file "refresh-401") :stdout 'pipe)) - (port (parse-port-line (process-read-line proc))) - (host (string-append "http://127.0.0.1:" (number->string port))) - (tmp (make-temp-dir "gf-resolve-oauth-" "")) +;; 已过期:仍返回配置快照中的 access,不在配置阶段刷新或修改文件。 +(let* ((tmp (make-temp-dir "gf-resolve-oauth-" "")) (_ (pi-save-auth-oauth! "kimi-coding" - "old-acc" - "stub-refresh-token" + "expired-acc" + "ref" (- (pi-current-time-millis) 1000) - host + #f tmp ) ;pi-save-auth-oauth! ) ;_ (creds (pi-load-auth tmp)) ) ; - (check (pi-resolve-provider-key "kimi-coding" creds '() tmp) => #f) - ;; 凭据已被清除 - (check (pi-load-auth tmp) => '()) - (process-kill proc) + (check (pi-resolve-provider-key "kimi-coding" creds '() tmp) => "expired-acc") + (let ((c (car (pi-load-auth tmp)))) + (check (pi-credential-access c) => "expired-acc") + (check (pi-credential-refresh c) => "ref") + ) ;let (path-unlink (path (path-join tmp "auth.json"))) (path-rmdir tmp) ) ;let* diff --git a/code/liii/pi/code/config/oauth-hook.scm b/code/liii/pi/code/config/oauth-hook.scm index 283b9c5..e2384f8 100644 --- a/code/liii/pi/code/config/oauth-hook.scm +++ b/code/liii/pi/code/config/oauth-hook.scm @@ -4,79 +4,126 @@ ;; Licensed under the MIT License ;; -;; 1404:oauth 401 强制刷新钩子的构造与按 provider 重同步。 -;; resolve-pi-config 初始注入,以及会话内切模(含同 provider)/ -;; /login 后 reload 时都须调用 pi-sync-oauth-refresh-hook: -;; 钩子闭包捕获 provider 无关的 env/dir,按 cfg 当前 provider 现查 -;; auth.json,有 oauth 凭据则注入,无则移除旧钩子(避免切到非 -;; oauth provider 后残留钩子误刷新)。 +;; OAuth 凭据在独立的内存 store 中维护,请求配置保持副本语义。 +;; access token 仅存在于内存(短命,不值得落盘);refresh token 轮换后 +;; 连同 expires 一起写回 auth.json,确保重启后能用新 refresh token 继续刷新。 +;; invalid_grant 时同时清除内存与 auth.json 条目(对应原 OAuth2 语义)。 (define-library (liii pi code config oauth-hook) (import (scheme base) (scheme process-context) (liii path) (liii pi ai auth) - ;; save!/remove! 闭包经 procedure-source 在 go worker 会话重建求值, - ;; pi-credential-* 访问器须随本库进入会话活动库列表,否则 save! 抛 - ;; unbound 被吞、新 token 不落盘(下次启动仍用旧 token 反复 401) (liii pi ai config credential) (liii pi ai auth oauth resolve) + (liii pi ai auth oauth store) (liii pi ai config runtime) + (liii pi ai time) ) ;import - (export pi-make-refresh-oauth-hook pi-sync-oauth-refresh-hook) + (export pi-make-refresh-oauth-hook pi-sync-oauth-refresh-hook + pi-make-request-oauth-key-hook pi-oauth-store-for-config + ) ;export (begin - ;; 构造 401 后强制刷新 oauth 凭据的闭包钩子。落盘/清除回调 - ;; 注入 auth.json 目录(captured-dir),env 表 captured-env 透传。 + (define (pi-oauth-store-for-config cfg captured-env captured-dir) + (or (pi-cfg-ref cfg 'oauth-store #f) + (pi-make-oauth-store (if (pair? captured-env) + (pi-load-auth captured-dir captured-env) + (pi-load-auth captured-dir) + ) ;if + ) ;pi-make-oauth-store + ) ;or + ) ;define + + ;; 刷新成功时把 refresh/expires 落盘(保留盘上旧 access,不写入新 access: + ;; access 短命不值得持久化,重启后由 request-oauth-key 检测到过期再刷新)。 + ;; 落盘失败仅记日志,不阻断本次请求。 + (define (%persist-refresh! captured-dir new-cred) + (let* ((dir captured-dir) + (provider (pi-credential-provider new-cred)) + (disk-cred (pi-find-oauth-credential provider (pi-load-auth dir))) + (disk-access (if disk-cred (pi-credential-access disk-cred) (pi-credential-access new-cred)) + ) ;disk-access + ) ; + (pi-save-auth-oauth! provider + disk-access + (pi-credential-refresh new-cred) + (pi-credential-expires new-cred) + (pi-credential-region new-cred) + dir + ) ;pi-save-auth-oauth! + ) ;let* + ) ;define + + (define (%refresh! store cred captured-env captured-dir) + ;; resolve 的 opt-env 约定为 (env-alist),不能直接把 env-alist 传入, + ;; 否则下游会把首个环境变量 pair 当作完整 alist 交给 assoc。 + ;; save! 更新内存 store 并落盘 refresh/expires;remove! 清内存与盘上条目。 + (let ((next (pi-force-refresh-oauth-credential-key cred + (if (pair? captured-env) (list captured-env) '()) + (lambda (c) (pi-oauth-store-set! store c) (%persist-refresh! captured-dir c)) + (lambda (p) + (pi-oauth-store-remove! store p) + (pi-remove-auth-provider! p captured-dir) + ) ;lambda + ) ;pi-force-refresh-oauth-credential-key + ) ;next + ) ; + next + ) ;let + ) ;define + + ;; 每次新请求解析 key。配置副本可共享 store,但 store 不依赖配置身份; + ;; 未过期直接返回 access,过期时透明刷新并替换内存中的完整凭据。 + (define (pi-make-request-oauth-key-hook captured-env captured-dir) + (lambda (cfg) + (let* ((p (pi-cfg-ref cfg 'provider #f)) + (store (pi-oauth-store-for-config cfg captured-env captured-dir)) + (cred (and p (pi-oauth-store-ref store p))) + ) ; + (and cred + (let ((access (pi-credential-access cred)) (expires (pi-credential-expires cred))) + (if + (and access expires (> expires (+ (pi-current-time-millis) 60000))) + access + (%refresh! store cred captured-env captured-dir) + ) ;if + ) ;let + ) ;and + ) ;let* + ) ;lambda + ) ;define + + ;; 401 后强制刷新:更新内存 store 并把 refresh/expires 落盘;access 仅内存。 (define (pi-make-refresh-oauth-hook captured-env captured-dir) (lambda (cfg current-key) (let* ((p (pi-cfg-ref cfg 'provider #f)) - (creds (if (pair? captured-env) - (pi-load-auth captured-dir captured-env) - (pi-load-auth captured-dir) - ) ;if - ) ;creds - (cred (pi-find-oauth-credential p creds)) + (store (pi-oauth-store-for-config cfg captured-env captured-dir)) + (cred (and p (pi-oauth-store-ref store p))) ) ; - (if cred - (pi-force-refresh-oauth-credential-key cred - (if (pair? captured-env) captured-env '()) - (lambda (c) - (pi-save-auth-oauth! (pi-credential-provider c) - (pi-credential-access c) - (pi-credential-refresh c) - (pi-credential-expires c) - (pi-credential-region c) - captured-dir - ) ;pi-save-auth-oauth! - ) ;lambda - (lambda (pp) (pi-remove-auth-provider! pp captured-dir)) - ) ;pi-force-refresh-oauth-credential-key - #f - ) ;if + (and cred (%refresh! store cred captured-env captured-dir)) ) ;let* ) ;lambda ) ;define - ;; 按 cfg 当前 provider 同步 refresh-oauth-key 钩子: - ;; 有 oauth 凭据 → 注入(覆盖旧钩子,目录取 cfg 现值); - ;; 无 → 置 #f 移除残留钩子(非 oauth provider 的 401 按现状失败)。 - ;; opt-env 为 (env-alist) 形式,缺省取当前进程环境变量。 + ;; 按 cfg 当前 provider 同步 request-oauth-key / refresh-oauth-key。 + ;; 初始无 store 时从 auth.json 初始化;切模复制配置会保留同一 store, + ;; /login 后 reload 移除旧 store 并重新载入登录后的 auth.json。 (define (pi-sync-oauth-refresh-hook cfg . opt-env) (let* ((env (if (pair? opt-env) (car opt-env) (get-environment-variables))) - ;; 与 pi-cfg-config-dir 同义:优先 config-dir,缺失回退 agent-dir - ;; (就地取避免引入 (liii pi code config) 循环依赖) (dir (or (pi-cfg-ref cfg 'config-dir) (pi-cfg-ref cfg 'agent-dir))) (dir-str (if (path? dir) (path->string dir) dir)) (provider (pi-cfg-ref cfg 'provider #f)) - (creds (pi-load-auth dir-str env)) + (store (pi-oauth-store-for-config cfg env dir-str)) + (cred (and provider (pi-oauth-store-ref store provider))) ) ; - (pi-cfg-set cfg - 'refresh-oauth-key - (if (pi-has-oauth-credential? provider creds) - (pi-make-refresh-oauth-hook env dir-str) - #f - ) ;if - ) ;pi-cfg-set + (pi-cfg-merge cfg + (list (cons 'oauth-store store) + (cons 'request-oauth-key + (if cred (pi-make-request-oauth-key-hook env dir-str) #f) + ) ;cons + (cons 'refresh-oauth-key (if cred (pi-make-refresh-oauth-hook env dir-str) #f)) + ) ;list + ) ;pi-cfg-merge ) ;let* ) ;define diff --git a/code/tests/liii/pi/code/config/oauth-hook/README.md b/code/tests/liii/pi/code/config/oauth-hook/README.md index 10664c8..279e281 100644 --- a/code/tests/liii/pi/code/config/oauth-hook/README.md +++ b/code/tests/liii/pi/code/config/oauth-hook/README.md @@ -18,3 +18,9 @@ bin/gf test code/tests/liii/pi/code/config/oauth-hook/ - `pi-make-refresh-oauth-hook-test.scm` — 钩子端到端:stub OAuth 服务 refresh 成功落盘返回新 access;invalid_grant 清除凭据返回 #f; 非 oauth provider 返回 #f +- `pi-request-provider-oauth-refresh-test.scm` — 请求端到端:首次 401 + 刷新后成功,后续配置副本从共享 store 读新 token 不再刷新 +- `pi-make-request-oauth-key-hook-test.scm` — 请求前取 key:未过期直接 + 返回 access 不联网;无 oauth 凭据 / 无 provider 返回 #f +- `pi-oauth-store-for-config-test.scm` — 取配置绑定 store(保持身份) + 或从 auth.json 初始化;无 auth.json 返回空 store diff --git a/code/tests/liii/pi/code/config/oauth-hook/pi-make-refresh-oauth-hook-test.scm b/code/tests/liii/pi/code/config/oauth-hook/pi-make-refresh-oauth-hook-test.scm index ee587b0..ea3f324 100644 --- a/code/tests/liii/pi/code/config/oauth-hook/pi-make-refresh-oauth-hook-test.scm +++ b/code/tests/liii/pi/code/config/oauth-hook/pi-make-refresh-oauth-hook-test.scm @@ -1,23 +1,26 @@ ;; pi-make-refresh-oauth-hook -;; 构造 401 后强制刷新 oauth 凭据的闭包钩子:按 cfg 当前 provider 现查 -;; auth.json,刷新成功落盘返回新 access;invalid_grant 清除凭据返回 #f。 +;; 401 强制刷新更新内存 store,并把轮换后的 refresh/expires 落盘 auth.json +;; (access token 短命,仅保留内存;盘上 access 保持登录时的旧值)。 +;; 后续 request-oauth-key 即使使用配置副本,也读取最新 access token。 +;; invalid_grant 时同时清除内存 store 与 auth.json 条目。 ;; ;; 语法 ;; ---- -;; (pi-make-refresh-oauth-hook captured-env captured-dir) -> procedure? +;; (pi-make-refresh-oauth-hook captured-env captured-dir) ;; ;; 参数 ;; ---- ;; captured-env : list? -;; 环境变量 alist(KIMI_CODE_OAUTH_HOST 覆盖)。 +;; 环境变量 alist(可为空表)。 ;; -;; captured-dir : string? -;; auth.json 所在配置目录(落盘/清除回调使用)。 +;; captured-dir : (or string? path?) +;; 配置目录(含 auth.json)。 ;; ;; 返回值 ;; ---- ;; procedure? -;; 钩子 (lambda (cfg current-key) ...):返回新 access 字符串或 #f。 +;; (lambda (cfg current-key) ...):刷新成功返回新 access token(string?), +;; 无 oauth 凭据或 invalid_grant 时返回 #f。 (import (scheme base) (scheme file) @@ -27,8 +30,11 @@ (liii string) (liii subprocess) (liii pi ai config runtime) + (liii pi ai config credential) (liii pi ai auth) + (liii pi ai keyring) (liii pi ai time) + (liii pi ai auth oauth store) (liii pi code config oauth-hook) ) ;import @@ -61,9 +67,6 @@ ) ;if ) ;define -;; 写 kimi-coding oauth 凭据:region 指向 stub host,expires 仍在未来 -;; (401 强制刷新不查 expires) - (define (write-auth! host refresh-token) (path-write-text (path->string (path-join (path TMP-DIR) (path "auth.json"))) (string-append "{\"kimi-coding\":{\"type\":\"oauth\",\"access\":\"old-acc\"," @@ -78,50 +81,103 @@ ) ;path-write-text ) ;define -(define (make-cfg) - (make-pi-config :config-dir TMP-DIR :provider "kimi-coding") +(define (stored-credential) + (car (pi-load-auth TMP-DIR)) ) ;define -;; 1. refresh 成功:返回新 access,auth.json 落盘新凭据 +;; 刷新成功:401 重试取得新 token,配置副本从同一 store 取得新 token。 (let* ((proc (process-spawn (list "python3" stub-file "refresh-ok") :stdout 'pipe)) (port (parse-port-line (process-read-line proc))) (host (string-append "http://127.0.0.1:" (number->string port))) ) ; (write-auth! host "stub-refresh-token") - (let* ((hook (pi-make-refresh-oauth-hook '() TMP-DIR)) - (next (hook (make-cfg) "old-acc")) + (let* ((cfg (pi-sync-oauth-refresh-hook (make-pi-config :config-dir TMP-DIR :provider "kimi-coding" :api-key "old-acc") + '() + ) ;pi-sync-oauth-refresh-hook + ) ;cfg + (copy (pi-cfg-set cfg 'thinking-level "high")) + (next ((pi-cfg-ref cfg 'refresh-oauth-key) cfg "old-acc")) ) ; (check next => "stub-access-token-2") - (check (length (pi-load-auth TMP-DIR)) => 1) - (check (pi-resolve-provider-key "kimi-coding" (pi-load-auth TMP-DIR) '() TMP-DIR) - => - "stub-access-token-2" - ) ;check + (check (pi-request-api-key copy) => "stub-access-token-2") + (check (pi-cfg-ref cfg 'api-key) => "old-acc") + (check (pi-cfg-ref copy 'api-key) => "old-acc") + ;; 刷新后 access 仅内存,盘上 access 仍为旧值;轮换后的 refresh token 落盘。 + (check (pi-credential-access (stored-credential)) => "old-acc") + (check (pi-credential-refresh (stored-credential)) => "stub-refresh-token-2") + ;; 后续读取不再触发刷新;再次强刷会使用轮换后的 refresh token。 + (check (pi-request-api-key cfg) => "stub-access-token-2") + ) ;let* + (process-kill proc) +) ;let* + +;; 过期 token 在请求前刷新,而不是在配置解析时刷新。 +(let* ((proc (process-spawn (list "python3" stub-file "refresh-ok") :stdout 'pipe)) + (port (parse-port-line (process-read-line proc))) + (host (string-append "http://127.0.0.1:" (number->string port))) + ) ; + (write-auth! host "stub-refresh-token") + (let* ((cfg (pi-sync-oauth-refresh-hook (make-pi-config :config-dir TMP-DIR :provider "kimi-coding" :api-key "old-acc") + '() + ) ;pi-sync-oauth-refresh-hook + ) ;cfg + ) ; + (check (pi-request-api-key cfg) => "old-acc") + (check (pi-credential-access (stored-credential)) => "old-acc") + ;; 上述快照未过期;用 store 的公开写入接口替换为过期凭据。 + ((lambda (store) + (pi-oauth-store-clear! store) + (pi-oauth-store-load! store + (list (pi-credential "kimi-coding" + #f + "oauth" + "expired-acc" + "stub-refresh-token" + (- (pi-current-time-millis) 1000) + host + ) ;pi-credential + ) ;list + ) ;pi-oauth-store-load! + ) ;lambda + (pi-cfg-ref cfg 'oauth-store) + ) ; + (check (pi-request-api-key cfg) => "stub-access-token-2") + (check (pi-credential-access (stored-credential)) => "old-acc") + ;; 过期刷新同样把轮换后的 refresh token 落盘。 + (check (pi-credential-refresh (stored-credential)) => "stub-refresh-token-2") ) ;let* (process-kill proc) ) ;let* -;; 2. refresh 失败(401 invalid_grant):清除凭据返回 #f +;; 刷新 unauthorized:清内存与 auth.json 条目,返回 #f。 (let* ((proc (process-spawn (list "python3" stub-file "refresh-401") :stdout 'pipe)) (port (parse-port-line (process-read-line proc))) (host (string-append "http://127.0.0.1:" (number->string port))) ) ; (write-auth! host "stub-refresh-token") - (let* ((hook (pi-make-refresh-oauth-hook '() TMP-DIR)) - (next (hook (make-cfg) "old-acc")) + (let* ((cfg (pi-sync-oauth-refresh-hook (make-pi-config :config-dir TMP-DIR :provider "kimi-coding") + '() + ) ;pi-sync-oauth-refresh-hook + ) ;cfg + (next ((pi-cfg-ref cfg 'refresh-oauth-key) cfg "old-acc")) ) ; (check next => #f) - (check (pi-load-auth TMP-DIR) => '()) + (check (pi-request-api-key cfg) => #f) + (check (length (pi-load-auth TMP-DIR)) => 0) ) ;let* (process-kill proc) ) ;let* -;; 3. cfg provider 无 oauth 凭据:返回 #f(不动其他 provider 凭据) +;; provider 无 oauth 凭据:不影响其他 provider。 (path-write-text (path->string (path-join (path TMP-DIR) (path "auth.json"))) "{\"google\":{\"type\":\"api_key\",\"key\":\"fixture-google-key\"}}" ) ;path-write-text -(let ((hook (pi-make-refresh-oauth-hook '() TMP-DIR))) - (check (hook (make-cfg) "old-acc") => #f) +(let ((cfg (pi-sync-oauth-refresh-hook (make-pi-config :config-dir TMP-DIR :provider "kimi-coding") + '() + ) ;pi-sync-oauth-refresh-hook + ) ;cfg + ) ; + (check (pi-cfg-ref cfg 'refresh-oauth-key) => #f) (check (length (pi-load-auth TMP-DIR)) => 1) ) ;let diff --git a/code/tests/liii/pi/code/config/oauth-hook/pi-make-request-oauth-key-hook-test.scm b/code/tests/liii/pi/code/config/oauth-hook/pi-make-request-oauth-key-hook-test.scm new file mode 100644 index 0000000..52c6aff --- /dev/null +++ b/code/tests/liii/pi/code/config/oauth-hook/pi-make-request-oauth-key-hook-test.scm @@ -0,0 +1,94 @@ +;; pi-make-request-oauth-key-hook +;; 构造每次请求前解析 key 的钩子:未过期直接返回 store 中的 access, +;; 不联网、不写盘;无 oauth 凭据返回 #f。(过期透明刷新与 401 强制刷新 +;; 的端到端行为分别见 pi-make-refresh-oauth-hook-test 与 +;; pi-request-provider-oauth-refresh-test。) +;; +;; 语法 +;; ---- +;; (pi-make-request-oauth-key-hook captured-env captured-dir) +;; +;; 参数 +;; ---- +;; captured-env : list? +;; 环境变量 alist(可为空表)。 +;; +;; captured-dir : (or string? path?) +;; 配置目录(含 auth.json)。 +;; +;; 返回值 +;; ---- +;; procedure? +;; (lambda (cfg) ...):未过期返回 access token(string?); +;; 配置 provider 无 oauth 凭据时返回 #f。 + +(import (scheme base) + (scheme file) + (liii check) + (liii path) + (liii shutil) + (liii pi ai config runtime) + (liii pi ai config credential) + (liii pi ai auth oauth store) + (liii pi ai time) + (liii pi code config oauth-hook) +) ;import + +(check-set-mode! 'report-failed) + +(define TMP-DIR + (path->string (path-join (path-temp-dir) "pi-make-request-oauth-key-hook-test")) +) ;define + +(when (file-exists? TMP-DIR) + (rmtree TMP-DIR) +) ;when +(path-mkdir TMP-DIR :parents #t :exist-ok #t) + +(define CFG (make-pi-config :config-dir TMP-DIR :provider "kimi-coding")) + +(define HOOK (pi-make-request-oauth-key-hook '() TMP-DIR)) + +(check-true (procedure? HOOK)) + +;; 1. store 中凭据未过期:直接返回 access,auth.json 不被修改。 +(let* ((cred (pi-credential "kimi-coding" + #f + "oauth" + "fresh-acc" + "ref" + (+ (pi-current-time-millis) 3600000) + #f + ) ;pi-credential + ) ;cred + (cfg (pi-cfg-set CFG 'oauth-store (pi-make-oauth-store (list cred)))) + ) ; + (check (HOOK cfg) => "fresh-acc") + (check-false + (file-exists? (path->string (path-join (path TMP-DIR) (path "auth.json")))) + ) ;check-false +) ;let* + +;; 2. 配置 provider 无 oauth 凭据:返回 #f。 +(check (HOOK (pi-cfg-set CFG 'oauth-store (pi-make-oauth-store))) => #f) + +;; 3. provider 缺失:返回 #f。 +(check (HOOK (pi-cfg-set (pi-cfg-set CFG 'provider #f) 'oauth-store (pi-make-oauth-store)) + ) ;HOOK + => + #f +) ;check + +;; 4. 未绑定 store 时从 auth.json 初始化并返回未过期 access。 +(path-write-text (path->string (path-join (path TMP-DIR) (path "auth.json"))) + (string-append "{\"kimi-coding\":{\"type\":\"oauth\",\"access\":\"disk-acc\"," + "\"refresh\":\"ref\",\"expires\":" + (number->string (+ (pi-current-time-millis) 3600000)) + "}}" + ) ;string-append +) ;path-write-text +(check (HOOK CFG) => "disk-acc") + +(rmtree TMP-DIR) + +(check-report) diff --git a/code/tests/liii/pi/code/config/oauth-hook/pi-oauth-store-for-config-test.scm b/code/tests/liii/pi/code/config/oauth-hook/pi-oauth-store-for-config-test.scm new file mode 100644 index 0000000..7a8727c --- /dev/null +++ b/code/tests/liii/pi/code/config/oauth-hook/pi-oauth-store-for-config-test.scm @@ -0,0 +1,84 @@ +;; pi-oauth-store-for-config +;; 取配置绑定的 oauth store;配置未携带 store 时从 auth.json 载入 +;; oauth 凭据新建。切模复制配置共享同一 store,/login 后 reload 重建。 +;; +;; 语法 +;; ---- +;; (pi-oauth-store-for-config cfg captured-env captured-dir) +;; +;; 参数 +;; ---- +;; cfg : (or pi-config? list?) +;; 请求配置;可携带 oauth-store 键。 +;; +;; captured-env : list? +;; 环境变量 alist;空表时按无环境变量加载 auth.json。 +;; +;; captured-dir : (or string? path?) +;; 配置目录(含 auth.json)。 +;; +;; 返回值 +;; ---- +;; pi-oauth-store? +;; 配置已绑定则原样返回(保持身份);否则新建并载入盘上 oauth 凭据。 + +(import (scheme base) + (scheme file) + (liii check) + (liii path) + (liii shutil) + (liii pi ai config runtime) + (liii pi ai config credential) + (liii pi ai auth oauth store) + (liii pi ai time) + (liii pi code config oauth-hook) +) ;import + +(check-set-mode! 'report-failed) + +(define TMP-DIR + (path->string (path-join (path-temp-dir) "pi-oauth-store-for-config-test")) +) ;define + +(when (file-exists? TMP-DIR) + (rmtree TMP-DIR) +) ;when +(path-mkdir TMP-DIR :parents #t :exist-ok #t) + +(define (write-auth! text) + (path-write-text (path->string (path-join (path TMP-DIR) (path "auth.json"))) + text + ) ;path-write-text +) ;define + +(define CFG (make-pi-config :config-dir TMP-DIR :provider "kimi-coding")) + +;; 1. 配置已绑定 store:原样返回,保持对象身份。 +(let* ((bound (pi-make-oauth-store)) (cfg (pi-cfg-set CFG 'oauth-store bound))) + (check (pi-oauth-store-for-config cfg '() TMP-DIR) => bound) +) ;let* + +;; 2. 未绑定:从 auth.json 载入 oauth 凭据(api_key 被过滤)。 +(write-auth! + (string-append "{\"kimi-coding\":{\"type\":\"oauth\",\"access\":\"acc\"," + "\"refresh\":\"ref\",\"expires\":" + (number->string (+ (pi-current-time-millis) 3600000)) + "},\"google\":{\"type\":\"api_key\",\"key\":\"fixture-google-key\"}}" + ) ;string-append +) ;write-auth! +(let ((store (pi-oauth-store-for-config CFG '() TMP-DIR))) + (check-true (pi-oauth-store? store)) + (check (pi-credential-access (pi-oauth-store-ref store "kimi-coding")) => "acc") + (check (pi-oauth-store-ref store "google") => #f) +) ;let + +;; 3. 无 auth.json:返回空 store,不抛异常。 +(path-unlink (path (path-join TMP-DIR "auth.json"))) +(let ((store (pi-oauth-store-for-config CFG '() TMP-DIR))) + (check-true (pi-oauth-store? store)) + (check (pi-oauth-store-ref store "kimi-coding") => #f) +) ;let + +(rmtree TMP-DIR) + +(check-report) diff --git a/code/tests/liii/pi/code/config/oauth-hook/pi-request-provider-oauth-refresh-test.scm b/code/tests/liii/pi/code/config/oauth-hook/pi-request-provider-oauth-refresh-test.scm new file mode 100644 index 0000000..c6a0dee --- /dev/null +++ b/code/tests/liii/pi/code/config/oauth-hook/pi-request-provider-oauth-refresh-test.scm @@ -0,0 +1,150 @@ +;; OAuth 凭据保存在独立的内存 store 中。 +;; 第一次请求旧 token 收到 401,刷新后成功;第二次即使使用新的配置副本, +;; 也从共享 store 读取新 token,不再次刷新,不修改配置或 auth.json。 +;; +;; 语法 +;; ---- +;; (pi-sync-oauth-refresh-hook cfg . opt-env) +;; (pi-request cfg ...) 经由 request-oauth-key / refresh-oauth-key 钩子取 key +;; +;; 参数 +;; ---- +;; cfg : pi-config? +;; 含 provider 与 config-dir 的请求配置。 +;; +;; opt-env : list? +;; 可选 (env-alist),缺省取当前进程环境变量。 +;; +;; 返回值 +;; ---- +;; pi-config? +;; 注入共享 oauth-store 与钩子后的配置;401 刷新后新 token 生效, +;; 轮换的 refresh token 落盘、access 仅内存。 + +(import (scheme base) + (scheme file) + (scheme process-context) + (liii check) + (liii path) + (liii shutil) + (liii string) + (liii subprocess) + (liii pi ai config model-metadata) + (liii pi ai config runtime) + (liii pi ai message) + (liii pi ai dispatch) + (liii pi ai time) + (liii pi code config oauth-hook) + (liii pi ai auth) +) ;import + +(check-set-mode! 'report-failed) + +(define TMP-DIR + (path->string (path-join (path-temp-dir) "pi-request-provider-oauth-refresh-test") + ) ;path->string +) ;define + +(when (file-exists? TMP-DIR) + (rmtree TMP-DIR) +) ;when +(path-mkdir TMP-DIR :parents #t :exist-ok #t) + +(define stub-file + (if (file-exists? "tests/stub_oauth_refresh_request_server.py") + "tests/stub_oauth_refresh_request_server.py" + "code/tests/stub_oauth_refresh_request_server.py" + ) ;if +) ;define + +(define oauth-stub-file + (if (file-exists? "tests/stub_kimi_oauth_server.py") + "tests/stub_kimi_oauth_server.py" + "code/tests/stub_kimi_oauth_server.py" + ) ;if +) ;define + +(define (parse-port-line line) + (if (and (string? line) (string-starts? line "PORT:")) + (string->number (string-trim-both (substring line 5 (string-length line)))) + #f + ) ;if +) ;define + +(define (response-text resp) + (pi-text-block-text (car (pi-assistant-message-content resp))) +) ;define + +(define (request cfg text) + (pi-request-provider "kimi-coding" cfg (list (pi-user-message text)) #f '() 10) +) ;define + +(let* ((proc (process-spawn (list "python3" stub-file) :stdout 'pipe)) + (port (parse-port-line (process-read-line proc))) + (base-url (string-append "http://127.0.0.1:" (number->string port))) + (oauth-proc (process-spawn (list "python3" oauth-stub-file "refresh-ok") :stdout 'pipe) + ) ;oauth-proc + (oauth-port (parse-port-line (process-read-line oauth-proc))) + (oauth-host (string-append "http://127.0.0.1:" (number->string oauth-port))) + (auth-file (path->string (path-join (path TMP-DIR) (path "auth.json")))) + ) ; + (path-write-text auth-file + (string-append "{\"kimi-coding\":{\"type\":\"oauth\",\"access\":\"old-access\"," + "\"refresh\":\"stub-refresh-token\",\"expires\":" + (number->string (+ (pi-current-time-millis) 3600000)) + ",\"region\":\"" + oauth-host + "\"}}" + ) ;string-append + ) ;path-write-text + (let ((cfg + (pi-sync-oauth-refresh-hook + (make-pi-config + :config-dir TMP-DIR + :provider "kimi-coding" + :provider-api "openai-completions" + :model-id "kimi-for-coding" + :model-meta + (pi-model-metadata "kimi-for-coding" "Kimi" "kimi-coding" base-url 4096 128) + :api-key "old-access" + :settings '((retry (enabled . #f))) + :extras (list (cons 'headers '())) + ) ;make-pi-config + ;; 使用真实进程环境变量,覆盖首个 pair 被误当成完整 env-alist 的回归。 + (get-environment-variables) + ) ;pi-sync-oauth-refresh-hook + ) ;cfg + ) ; + (let ((first (request cfg "first"))) + (when (not (equal? (pi-assistant-message-stop-reason first) "stop")) + (display (pi-assistant-message-error-message first)) + (newline) + ) ;when + (check (pi-assistant-message-stop-reason first) => "stop") + (check (response-text first) => "PlainOK:stub-access-token-2") + (check (pi-cfg-ref cfg 'api-key) => "old-access") + ) ;let + ;; 同时改变多个字段,强制走 dispatch 的补齐\/复制路径。 + ;; 注意:不能覆盖 model-meta,否则 base-url 会回退到 provider 默认值, + ;; 导致请求发往真实 API 而非 stub 服务器。 + (let* ((copy (pi-cfg-merge cfg '((tools . #f) (settings . #f)))) + (second (request copy "second")) + ) ; + (check (pi-assistant-message-stop-reason second) => "stop") + (check (response-text second) => "PlainOK:stub-access-token-2") + (check (pi-cfg-ref copy 'api-key) => "old-access") + ) ;let* + ;; 刷新后 access 仅内存,盘上 access 仍为旧值;轮换后的 refresh token 落盘。 + (check-true (string-contains? (path-read-text auth-file) "old-access")) + (check-true (string-contains? (path-read-text auth-file) "stub-refresh-token-2") + ) ;check-true + (check-false (string-contains? (path-read-text auth-file) "stub-access-token-2") + ) ;check-false + ) ;let + (process-kill oauth-proc) + (process-kill proc) +) ;let* + +(rmtree TMP-DIR) + +(check-report) diff --git a/code/tests/liii/pi/code/config/oauth-hook/pi-sync-oauth-refresh-hook-test.scm b/code/tests/liii/pi/code/config/oauth-hook/pi-sync-oauth-refresh-hook-test.scm index 3daea1d..2a87551 100644 --- a/code/tests/liii/pi/code/config/oauth-hook/pi-sync-oauth-refresh-hook-test.scm +++ b/code/tests/liii/pi/code/config/oauth-hook/pi-sync-oauth-refresh-hook-test.scm @@ -92,7 +92,9 @@ (let ((cfg (pi-sync-oauth-refresh-hook (make-cfg "kimi-coding") '()))) (check-false (pi-oauth-refresh-enabled? cfg)) (write-auth! (oauth-json (+ (pi-current-time-millis) 3600000))) - (check-true (pi-oauth-refresh-enabled? (pi-sync-oauth-refresh-hook cfg '()))) + (check-true (pi-oauth-refresh-enabled? (pi-sync-oauth-refresh-hook (pi-cfg-set cfg 'oauth-store #f) '()) + ) ;pi-oauth-refresh-enabled? + ) ;check-true ) ;let ;; 5. 无 auth.json:不注入且不抛异常 diff --git a/code/tests/stub_oauth_refresh_request_server.py b/code/tests/stub_oauth_refresh_request_server.py new file mode 100644 index 0000000..3c7b4ea --- /dev/null +++ b/code/tests/stub_oauth_refresh_request_server.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""OpenAI endpoint for the OAuth access-token propagation regression test.""" + +import http.server +import json + + +class Handler(http.server.BaseHTTPRequestHandler): + protocol_version = "HTTP/1.1" + + def _send(self, status: int, obj: dict) -> None: + body = json.dumps(obj).encode("utf-8") + self.send_response(status) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + def do_POST(self) -> None: # noqa: N802 (BaseHTTPRequestHandler API) + length = int(self.headers.get("content-length") or 0) + self.rfile.read(length) + if self.headers.get("Authorization") != "Bearer stub-access-token-2": + self._send(401, {"error": {"message": "expired access token"}}) + return + self._send(200, { + "id": "chatcmpl-oauth-refresh", + "object": "chat.completion", + "created": 1, + "model": "kimi-for-coding", + "choices": [{ + "index": 0, + "message": {"role": "assistant", "content": "PlainOK:stub-access-token-2"}, + "finish_reason": "stop", + }], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }) + + def log_message(self, *args) -> None: + pass + + +def main() -> None: + server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler) + print("PORT:%d" % server.server_address[1], flush=True) + server.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/tui/liii/pi/tui/interactive/worker-model.scm b/tui/liii/pi/tui/interactive/worker-model.scm index 377a662..72457ea 100644 --- a/tui/liii/pi/tui/interactive/worker-model.scm +++ b/tui/liii/pi/tui/interactive/worker-model.scm @@ -86,7 +86,8 @@ ;; 401 无法强制刷新重试。 (pi-sync-oauth-refresh-hook (pi-cfg-merge cfg - (list (cons 'provider provider) + (list (cons 'oauth-store #f) + (cons 'provider provider) (cons 'provider-api (pi-cfg-provider-api cfg provider model-id)) (cons 'headers (pi-resolve-request-headers models-store provider model-id api-key) diff --git a/tui/liii/pi/tui/interactive/worker.scm b/tui/liii/pi/tui/interactive/worker.scm index 521812f..ac62035 100644 --- a/tui/liii/pi/tui/interactive/worker.scm +++ b/tui/liii/pi/tui/interactive/worker.scm @@ -19,9 +19,6 @@ (liii path) (liii pi ai abort) (liii pi ai auth) - ;; 1404:oauth 401 刷新钩子的 save!/remove! 闭包经 procedure-source 在 - ;; worker 会话重建,pi-credential-* 访问器须在本会话可见,否则 save! - ;; 抛 unbound 被吞、新 token 不落盘(下次启动仍用旧 token 反复 401) (liii pi ai config credential) (liii pi ai config model-metadata) (liii pi ai models meta) @@ -205,7 +202,9 @@ ;; 1404:/login 或刷新凭据后 reload 须重同步 oauth 401 刷新钩子, ;; 否则启动时无 oauth 凭据的会话登录后仍按现状 401 失败 (pi-sync-oauth-refresh-hook - (%cfg-override cfg + (%cfg-override + ;; /login 已更新 auth.json;丢弃旧内存快照后重新初始化。 + (pi-cfg-set cfg 'oauth-store #f) (list (cons 'models-store (pi-build-user-catalog dir-str)) (cons 'settings (pi-load-settings dir)) (cons 'api-key new-key)