merge: integrate review-11-session-expiry

This commit is contained in:
gitea committed 2026-10-20 23:02:48 +08:00
commit 6f4608635b
5 files changed
+91 -37

No files matched your search

+2 -2
View File
@@ -88,9 +88,9 @@ export async function handleAiProLogs(ctx: AiProExecutionContext): Promise<AiPro
let runRecord: ApprovalRunRecord | null = null;
if (runHandle) {
runRecord = repo.getRunByHandle(runHandle);
runRecord = repo.getRunByHandle(runHandle, undefined, { allowExpired: true });
} else if (targetSn) {
const maybeRun = repo.getRunByHandle(targetSn);
const maybeRun = repo.getRunByHandle(targetSn, undefined, { allowExpired: true });
if (maybeRun?.shortId && maybeRun.shortId.toUpperCase() === targetSn.toUpperCase()) {
runRecord = maybeRun;
}
+2
View File
@@ -21,3 +21,5 @@ AI PRO 执行真实审批前必须校验会话企业与当前企业、客户端
核验状态在批量核验、存储及网关入口均限定为 `PENDING/PASS/FAIL/SKIP`;未知状态拒绝写入,历史异常状态阻断决策。批量状态预检失败不会部分更新要点。
`approval` 和 AI PRO 的注入、核验与决策支持统一 `--dry-run` 预览;该选项优先于 `--apply --yes`,预览不创建客户端、会话数据库,也不更新核验结果、决策或下发审批。
审批操作按短 ID、UUID 或单号访问会话时统一检查 6 小时有效期;重新注入会话会刷新有效期。历史日志显式使用只读过期访问,不删除历史记录,也不放宽审批入口。
+26 -8
View File
@@ -340,10 +340,11 @@ export class ApprovalRepository {
this.db.run(
`
UPDATE approval_runs
SET flow_id = ?, corp_id = ?, scope = ?, decision = ?, decision_summary = ?, matched_rules_count = ?, failed_rules_count = ?, document_snapshot = ?, updated_at = ?
SET expires_at = ?, flow_id = ?, corp_id = ?, scope = ?, decision = ?, decision_summary = ?, matched_rules_count = ?, failed_rules_count = ?, document_snapshot = ?, updated_at = ?
WHERE id = ?
`,
[
input.expiresAt ?? (now + 6 * 3600 * 1000),
(input.flowId !== undefined ? input.flowId : existing.flowId) ?? null,
(input.corpId !== undefined ? input.corpId : existing.corpId) ?? null,
(input.scope !== undefined ? input.scope : existing.scope) ?? null,
@@ -362,25 +363,42 @@ export class ApprovalRepository {
return this.createApprovalRun(input);
}
public getRunByHandle(handle: string, corpId?: string): ApprovalRunRecord | null {
public getRunByHandle(handle: string, corpId?: string, options: { allowExpired?: boolean } = {}): ApprovalRunRecord | null {
const trimmed = handle.trim();
const now = Date.now();
const requireActive = (run: ApprovalRunRecord | null) => {
if (run && corpId !== undefined && run.corpId !== corpId) return null;
if (run && run.expiresAt != null && run.expiresAt <= now && !options.allowExpired) {
throw new BusinessError(`审批会话 [${trimmed}] 已过期(有效时长 6 小时),请运行 hose-aipro init 重新发起审批会话`);
}
return run;
};
// 1. 如果符合 6 位大写短 ID 特征
if (/^[A-Za-z0-9]{6}$/.test(trimmed)) {
if (options.allowExpired) {
const historical = this.db.query("SELECT * FROM approval_runs WHERE UPPER(short_id) = UPPER(?)").get(trimmed) as any;
if (historical) {
const scoped = requireActive(this.mapRunRow(historical));
if (scoped) return scoped;
}
}
const row = this.db
.query("SELECT * FROM approval_runs WHERE UPPER(short_id) = UPPER(?) AND (expires_at IS NULL OR expires_at > ?)")
.get(trimmed, now) as any;
if (row) return this.mapRunRow(row);
if (row) {
const scoped = requireActive(this.mapRunRow(row));
if (scoped) return scoped;
}
const expiredRow = this.db.query("SELECT * FROM approval_runs WHERE UPPER(short_id) = UPPER(?)").get(trimmed) as any;
if (expiredRow) {
if (expiredRow && (corpId === undefined || expiredRow.corp_id === corpId)) {
throw new BusinessError(`审批会话 [${trimmed}] 已过期(有效时长 6 小时),请运行 hose-aipro init 重新发起审批会话`);
}
}
// 2. 尝试按长 UUID 查找
const byId = this.getRunById(trimmed);
if (byId) return byId;
if (byId) return requireActive(byId);
// 3. 尝试按单号查找最新未过期的运行记录
return this.getLatestRunBySn(trimmed, corpId);
return requireActive(this.getLatestRunBySn(trimmed, corpId));
}
public getLatestRunBySn(documentSn: string, corpId?: string): ApprovalRunRecord | null {
@@ -552,7 +570,7 @@ export class ApprovalRepository {
run: ApprovalRunRecord | null;
findings: ApprovalFindingRecord[];
} {
const run = this.getLatestRunBySn(documentSn, corpId);
const run = this.getRunByHandle(documentSn, corpId);
if (!run) {
return { run: null, findings: [] };
}
@@ -592,7 +610,7 @@ export class ApprovalRepository {
return {
id: row.id,
shortId: row.short_id ?? null,
expiresAt: row.expires_at ? Number(row.expires_at) : null,
expiresAt: row.expires_at != null ? Number(row.expires_at) : null,
flowId: row.flow_id,
documentSn: row.document_sn,
corpId: row.corp_id,
@@ -0,0 +1,34 @@
import { afterEach, expect, test } from "bun:test";
import { getDatabase, closeDatabase } from "../../core/db/index.js";
import { ApprovalRepository } from "./repository.js";
afterEach(closeDatabase);
test("all operation handles reject expired sessions while history remains readable", async () => {
const repo = new ApprovalRepository(getDatabase(":memory:"));
const run = repo.createApprovalRun({ documentSn: "EXPIRED-SN", corpId: "corp", expiresAt: Date.now() - 1000 });
for (const handle of [run.shortId!, run.id, run.documentSn]) {
expect(() => repo.getRunByHandle(handle)).toThrow("已过期");
expect(repo.getRunByHandle(handle, undefined, { allowExpired: true })?.id).toBe(run.id);
}
repo.logAiProAction({ action: "init", runId: run.shortId, documentSn: run.documentSn });
expect(repo.getRunById(run.id)?.id).toBe(run.id);
const reset = repo.createOrResetApprovalRun({ documentSn: run.documentSn, corpId: "corp" });
expect(reset.expiresAt!).toBeGreaterThan(Date.now());
expect(repo.getRunByHandle(run.shortId!)?.id).toBe(run.id);
});
test("epoch-zero expiry is expired rather than an unlimited session", () => {
const repo = new ApprovalRepository(getDatabase(":memory:"));
const run = repo.createApprovalRun({ documentSn: "ZERO-SN", expiresAt: 0 });
for (const handle of [run.shortId!, run.id, run.documentSn]) expect(() => repo.getRunByHandle(handle)).toThrow("已过期");
});
test("checklist operations enforce expiry and scoped handles cannot select another corporation", () => {
const repo = new ApprovalRepository(getDatabase(":memory:"));
const expired = repo.createApprovalRun({ documentSn: "OLD-SN", corpId: "A", expiresAt: Date.now() - 1 });
expect(() => repo.getAuditChecklist(expired.documentSn, "A")).toThrow("已过期");
const live = repo.createApprovalRun({ documentSn: "LIVE-SN", corpId: "A" });
expect(repo.getRunByHandle(live.shortId!, "B")).toBeNull();
expect(repo.getRunByHandle(live.id, "B")).toBeNull();
expect(repo.getAuditChecklist(live.documentSn, "B").run).toBeNull();
});