fix: 表达工作流部分保存结果并校验认证重放

Piper 的 chooses 刷新与删除检查业务码,保留空成功响应。主保存成功后刷新失败抛出携带已保存版本的 WorkflowSavePartialError;统一 JSON 错误保留可选 EkbError.details。
workflow save 在部分成功时返回非零与恢复信息,--in-place 仍同步已保存版本;新增带写确认的 refresh-chooses,仅重试元数据刷新,不重发完整快照。主保存失败不改本地版本。
认证重放保留最终鉴权响应检查(含 HTTP 200 的错误头),最多重放一次,其他 HTTP 错误维持 Ky 语义。补充 SDK、CLI、恢复与重放回归,并更新文档。

验证:bun test 922 通过、0 失败;bun run check、git diff --check 通过。所有业务请求均为离线假接口。
This commit is contained in:
gitea committed 2026-10-29 18:21:30 +08:00
1 parent 51fb54dfe8
commit 55d31e27e6
19 files changed
+298 -23

No files matched your search

+2
View File
@@ -60,3 +60,5 @@ src/core/
- 校验签发身份与目标一致,并在保存成功后更新内存配置;失败时不留下半更新的内存身份。
子域客户端在 401 后使缓存失效、重新换票,并从新缓存读取请求头重放原请求一次;同一客户端无需重建。持续鉴权失败停止重试,POST 请求体在重放时保持一致。
重放响应仍检查 HTTP 401 与 `error-type` 鉴权错误头(包括 HTTP 200 携带错误头的情况);再次失败抛出 `AuthenticationError`,不再第三次请求。非鉴权 HTTP 错误保持 Ky 的错误语义。
+2 -1
View File
@@ -2,7 +2,8 @@ export class EkbError extends Error {
constructor(
message: string,
public readonly code: string,
public readonly status?: number
public readonly status?: number,
public readonly details?: Record<string, unknown>
) {
super(message);
this.name = "EkbError";
+18 -6
View File
@@ -1,4 +1,10 @@
import ky, { KyInstance, type BeforeRequestState } from "ky";
import { AuthenticationError } from "../errors.js";
function isAuthenticationFailure(response: Response): boolean {
const errorType = response.headers.get("error-type");
return response.status === 401 || errorType === "UnauthenticatedException" || errorType === "UnauthorizedException";
}
export interface CreateClientOptions {
prefixUrl: string;
@@ -45,11 +51,7 @@ export function createAuthClient(options: CreateAuthClientOptions): KyInstance {
afterResponse: [
async ({ request, response }) => {
if (!response) return response;
const errorType = response.headers.get("error-type");
const isAuthError =
response.status === 401 ||
errorType === "UnauthenticatedException" ||
errorType === "UnauthorizedException";
const isAuthError = isAuthenticationFailure(response);
if (isAuthError && options.onUnauthorized && options.retryUnauthorized !== false) {
const replay = replays.get(request);
@@ -66,7 +68,17 @@ export function createAuthClient(options: CreateAuthClientOptions): KyInstance {
}
}
// Ky's Request.clone type omits Bun's extra Headers methods; the runtime object is unchanged.
return ky(replay as unknown as Request, { retry: 0, timeout: options.timeout ?? 30000 });
return ky(replay as unknown as Request, {
retry: 0,
timeout: options.timeout ?? 30000,
// 只执行最终鉴权检查,不再续期;非鉴权 HTTP 错误仍由 Ky 处理。
hooks: { afterResponse: [({ response: replayResponse }) => {
if (isAuthenticationFailure(replayResponse)) {
throw options.onAuthFailure?.(replayResponse) ?? new AuthenticationError("认证续期后仍失败,请重新登录");
}
return replayResponse;
}] },
});
}
if (isAuthError && options.onAuthFailure) {
throw options.onAuthFailure(response);
+45
View File
@@ -0,0 +1,45 @@
import { afterEach, expect, test } from "bun:test";
import { HTTPError } from "ky";
import { createAuthClient } from "./factory.js";
import { AuthenticationError } from "../errors.js";
const originalFetch = globalThis.fetch;
afterEach(() => { globalThis.fetch = originalFetch; });
for (const finalStatus of [200, 401]) {
test(`续期后 HTTP ${finalStatus} 的鉴权错误仍拒绝,且不第三次请求`, async () => {
const bodies: string[] = [];
let refreshes = 0;
globalThis.fetch = (async (input: Request) => {
bodies.push(await input.text());
return bodies.length === 1 ? new Response("expired", { status: 401 })
: Response.json({ error: true }, { status: finalStatus, headers: { "error-type": "UnauthenticatedException" } });
}) as unknown as typeof fetch;
const client = createAuthClient({ prefixUrl: "https://tenant.invalid", getHeaders: () => ({ token: "fake" }), onUnauthorized: () => { refreshes++; } });
await expect(client.post("action", { json: { amount: 42 } }).json()).rejects.toBeInstanceOf(AuthenticationError);
expect(refreshes).toBe(1);
expect(bodies).toEqual(['{"amount":42}', '{"amount":42}']);
});
}
test("重放保留正常成功和非鉴权 HTTP 错误的 Ky 语义", async () => {
for (const status of [200, 503]) {
let requests = 0;
globalThis.fetch = (async () => ++requests === 1 ? new Response("", { status: 401 }) : Response.json({ ok: true }, { status })) as unknown as typeof fetch;
const client = createAuthClient({ prefixUrl: "https://tenant.invalid", getHeaders: () => ({}), onUnauthorized: () => {} });
const result = client.get("action").json();
if (status === 200) expect(await result).toEqual({ ok: true });
else await expect(result).rejects.toBeInstanceOf(HTTPError);
expect(requests).toBe(2);
}
});
test("最终鉴权失败仍调用调用方提供的错误构造器", async () => {
let requests = 0;
let failures = 0;
globalThis.fetch = (async () => { requests++; return new Response("", { status: 401 }); }) as unknown as typeof fetch;
const client = createAuthClient({ prefixUrl: "https://tenant.invalid", getHeaders: () => ({}), onUnauthorized: () => {}, onAuthFailure: () => { failures++; return new AuthenticationError("重新登录"); } });
await expect(client.get("action")).rejects.toThrow("重新登录");
expect(requests).toBe(2);
expect(failures).toBe(1);
});