Refine report data collection to support unlimited runs without 500 cap
This commit is contained in:
1 parent
bea6a9cf4e
commit
04915c6ad6
7 files changed
+104
-21
No files matched your search
@@ -186,6 +186,9 @@ seal-use report validate reports/审核报告.md
|
||||
seal-use report render reports/审核报告.md --out build/审核报告/
|
||||
```
|
||||
|
||||
> [!TIP]
|
||||
> **全量取数与分页控制**:`seal-use report ledger`、`report data` 与 `report generate` 默认自动分页拉取所选时间区间内的**全部审批单据**(内置 100,000 笔安全防死循环上限),无任何 500 条的人工截断限制。如需做小样本分析或限定抓取笔数,可传入 `--limit <n>`(别名 `--max-runs <n>` / `-n <n>`,例如 `--limit 2000`)。
|
||||
|
||||
---
|
||||
|
||||
### 模式 B:CLI 命令行实时研讨驱动
|
||||
|
||||
@@ -95,6 +95,16 @@ export async function handleLocalReportCommand(action: string | undefined, args:
|
||||
if (!result.validation.valid) throw new CliError("报告校验未通过,未生成渲染产物。请按 validation.issues 修正后重试。", 1);
|
||||
}
|
||||
|
||||
function resolveMaxRuns(args: ParsedArgs): number | undefined {
|
||||
return (
|
||||
numberOption(args.options.limit) ??
|
||||
numberOption(args.options.n) ??
|
||||
numberOption(args.options.count) ??
|
||||
numberOption(args.options["max-runs"]) ??
|
||||
numberOption(args.options.maxRuns)
|
||||
);
|
||||
}
|
||||
|
||||
/** `seal-use report generate` / `ledger` / `data` — AI 审核报告。 */
|
||||
export async function handleReportCommand(
|
||||
action: string | undefined,
|
||||
@@ -125,7 +135,7 @@ export async function handleReportCommand(
|
||||
|
||||
if ((!startDate || !endDate) && !month) {
|
||||
throw usageError(
|
||||
"Usage: seal-use report ledger --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD> [--templates <t1,t2...>] [--max-runs <n>] [--out <file.xlsx|csv>] [--corp <corpId>]"
|
||||
"Usage: seal-use report ledger --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD> [--templates <t1,t2...>] [--limit <n>] [--out <file.xlsx|csv>] [--corp <corpId>]"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -136,7 +146,7 @@ export async function handleReportCommand(
|
||||
month,
|
||||
corp: { id: corp.id, name: corp.name },
|
||||
templates,
|
||||
maxRuns: numberOption(args.options["max-runs"]) ?? numberOption(args.options.maxRuns)
|
||||
maxRuns: resolveMaxRuns(args)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -186,7 +196,7 @@ export async function handleReportCommand(
|
||||
|
||||
if (!ledgerPath && (!startDate || !endDate) && !month) {
|
||||
throw usageError(
|
||||
"Usage: seal-use report data (--ledger <台账.xlsx> | --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD>) [--templates <t1,t2...>] [--max-runs <n>] [--corp <corpId>]"
|
||||
"Usage: seal-use report data (--ledger <台账.xlsx> | --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD>) [--templates <t1,t2...>] [--limit <n>] [--corp <corpId>]"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -202,7 +212,7 @@ export async function handleReportCommand(
|
||||
month,
|
||||
templates,
|
||||
ledgerPath,
|
||||
maxRuns: numberOption(args.options["max-runs"]) ?? numberOption(args.options.maxRuns)
|
||||
maxRuns: resolveMaxRuns(args)
|
||||
}
|
||||
);
|
||||
printJson(result, outputOptions(args.options));
|
||||
@@ -225,7 +235,7 @@ export async function handleReportCommand(
|
||||
|
||||
if (!ledgerPath && (!startDate || !endDate) && !month) {
|
||||
throw usageError(
|
||||
"Usage: seal-use report generate (--ledger <台账.xlsx> | --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD>) [--templates <t1,t2...>] [--template <file>] [--report-id <RPT-...>] [--corp <corpId>]"
|
||||
"Usage: seal-use report generate (--ledger <台账.xlsx> | --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD>) [--templates <t1,t2...>] [--limit <n>] [--template <file>] [--report-id <RPT-...>] [--corp <corpId>]"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -243,7 +253,7 @@ export async function handleReportCommand(
|
||||
ledgerPath,
|
||||
templatePath: stringOption(args.options.template),
|
||||
reportId: stringOption(args.options["report-id"]) ?? stringOption(args.options.reportId),
|
||||
maxRuns: numberOption(args.options["max-runs"]) ?? numberOption(args.options.maxRuns),
|
||||
maxRuns: resolveMaxRuns(args),
|
||||
allowUnreviewed: booleanOption(args.options["allow-unreviewed"]) ?? booleanOption(args.options.force)
|
||||
}
|
||||
) as MonthlyReportResult;
|
||||
|
||||
+6
-6
@@ -625,7 +625,7 @@ export const HELP_REGISTRY: AreaHelp[] = [
|
||||
{
|
||||
name: "ledger",
|
||||
summary: "Export standalone audit run ledger (.xlsx / .csv) with full AI & human opinions",
|
||||
usage: ["seal-use report ledger --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD> [--templates <t1,t2...>] [--out <file.xlsx>] [--corp <corpId>]"],
|
||||
usage: ["seal-use report ledger --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD> [--templates <t1,t2...>] [--limit <n>] [--out <file.xlsx>] [--corp <corpId>]"],
|
||||
options: [
|
||||
{ flag: "--start-date <YYYY-MM-DD>", description: "Statistics start date, YYYY-MM-DD (e.g. 2026-08-12)" },
|
||||
{ flag: "--end-date <YYYY-MM-DD>", description: "Statistics end date, YYYY-MM-DD (e.g. 2026-09-18)" },
|
||||
@@ -633,20 +633,20 @@ export const HELP_REGISTRY: AreaHelp[] = [
|
||||
{ flag: "--ledger <file>", description: "Inspect or convert an existing ledger file" },
|
||||
{ flag: "--out <file>", description: "Output path (.xlsx or .csv); defaults to 台账-<corpId>-<period>.xlsx" },
|
||||
{ flag: "--format <xlsx|csv>", description: "Export format (default: xlsx, or inferred from --out)" },
|
||||
{ flag: "--max-runs <n>", description: "Cap approval-run fetch, default 500" }
|
||||
{ flag: "--limit <n>", description: "Cap approval-run fetch (alias: --max-runs, default: all records in period, max 100000)" }
|
||||
],
|
||||
description: "Exports full audit ledger containing recordId, SN, template, AI decisions, full AI opinions & conditions, human results & comments, agreement, and review verdict columns. Agents can freely trim rows/templates and fill verdicts."
|
||||
},
|
||||
{
|
||||
name: "data",
|
||||
summary: "Extract pure structured report data, metrics, diverged runs, and triggers without templates",
|
||||
usage: ["seal-use report data (--ledger <台账.xlsx> | --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD>) [--templates <t1,t2...>] [--max-runs <n>] [--corp <corpId>]"],
|
||||
usage: ["seal-use report data (--ledger <台账.xlsx> | --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD>) [--templates <t1,t2...>] [--limit <n>] [--corp <corpId>]"],
|
||||
options: [
|
||||
{ flag: "--start-date <YYYY-MM-DD>", description: "Statistics start date, YYYY-MM-DD" },
|
||||
{ flag: "--end-date <YYYY-MM-DD>", description: "Statistics end date, YYYY-MM-DD" },
|
||||
{ flag: "--ledger <file>", description: "Directly parse an edited Excel/CSV ledger file without remote requests" },
|
||||
{ flag: "--templates <names>", description: "Comma-separated template whitelist" },
|
||||
{ flag: "--max-runs <n>", description: "Cap approval-run detail fetch, default 500 (max 500)" }
|
||||
{ flag: "--limit <n>", description: "Cap approval-run detail fetch (alias: --max-runs, default: all records in period, max 100000)" }
|
||||
],
|
||||
description: "Template-free data pack for Agent authoring: returns performance metrics, formula fractions, template groups, top risk tags, diverged runs with exact triggered conditions, and reviews."
|
||||
},
|
||||
@@ -675,7 +675,7 @@ export const HELP_REGISTRY: AreaHelp[] = [
|
||||
name: "generate",
|
||||
aliases: ["monthly", "month"],
|
||||
summary: "Fill the audit report template with performance, risk, and approval-run data",
|
||||
usage: ["seal-use report generate (--ledger <台账.xlsx> | --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD>) [--templates <t1,t2...>] [--out <报告.md>] [--template <file>] [--report-id <RPT-...>] [--max-runs <n>] [--allow-unreviewed] [--corp <corpId>]"],
|
||||
usage: ["seal-use report generate (--ledger <台账.xlsx> | --start-date <YYYY-MM-DD> --end-date <YYYY-MM-DD>) [--templates <t1,t2...>] [--out <报告.md>] [--template <file>] [--report-id <RPT-...>] [--limit <n>] [--allow-unreviewed] [--corp <corpId>]"],
|
||||
options: [
|
||||
{ flag: "--start-date <YYYY-MM-DD>", description: "Statistics start date, YYYY-MM-DD (e.g. 2026-08-12)" },
|
||||
{ flag: "--end-date <YYYY-MM-DD>", description: "Statistics end date, YYYY-MM-DD (e.g. 2026-09-18)" },
|
||||
@@ -685,7 +685,7 @@ export const HELP_REGISTRY: AreaHelp[] = [
|
||||
{ flag: "--out <file>", description: "Save Markdown plus local images and Excel/CSV ledgers beside the report; stdout stays JSON" },
|
||||
{ flag: "--overwrite", description: "Explicitly permit replacing existing output files" },
|
||||
{ flag: "--report-id <RPT-...>", description: "Custom report id; defaults to RPT-<dateTag>-<random>" },
|
||||
{ flag: "--max-runs <n>", description: "Cap approval-run detail fetch, default 500 (max 500)" },
|
||||
{ flag: "--limit <n>", description: "Cap approval-run detail fetch (alias: --max-runs, default: all records in period, max 100000)" },
|
||||
{ flag: "--allow-unreviewed", description: "Bypass pre-flight adjudication gate to generate draft when unreviewed diverged runs exist" }
|
||||
],
|
||||
description: "The template is the mutable skin: section headings, tables, and order may change and data demands are inferred from headings. Supports direct ledger-driven generation where user/Agent modifications to templates and rows are strictly respected."
|
||||
|
||||
@@ -1275,6 +1275,12 @@ describe("sealCapabilities", () => {
|
||||
expect(rangeRes.period.start).toBe("2026-08-12");
|
||||
expect(rangeRes.period.end).toBe("2026-09-18");
|
||||
expect(rangeRes.templateGroups[0]?.template).toBe("差旅报销");
|
||||
|
||||
// 验证 maxRuns 突破了原本的 500 限制,传入 2000 校验合法
|
||||
expect(() => tool!.parameters.parse({ month: "2026-08", maxRuns: 2000 })).not.toThrow();
|
||||
const monthlyTool = sealCapabilities.find((item) => item.name === "sealReportMonthlyGet");
|
||||
expect(monthlyTool).toBeDefined();
|
||||
expect(() => monthlyTool!.parameters.parse({ month: "2026-08", maxRuns: 2000 })).not.toThrow();
|
||||
});
|
||||
|
||||
test("deploy hosecloud configure saves integration and rejects missing customApiUrl", async () => {
|
||||
|
||||
@@ -36,9 +36,9 @@ export const reportCapabilities = [
|
||||
.number()
|
||||
.int()
|
||||
.positive()
|
||||
.max(500)
|
||||
.max(100000)
|
||||
.optional()
|
||||
.describe("拉取审批运行明细分块的上限,默认 500"),
|
||||
.describe("拉取审批运行明细分块的上限(可选,未指定时默认拉取指定时间区间内的全量单据,最大安全上限 100000)"),
|
||||
allowUnreviewed: z
|
||||
.boolean()
|
||||
.optional()
|
||||
@@ -102,9 +102,9 @@ export const reportCapabilities = [
|
||||
.number()
|
||||
.int()
|
||||
.positive()
|
||||
.max(500)
|
||||
.max(100000)
|
||||
.optional()
|
||||
.describe("拉取审批运行明细分块的上限,默认 500")
|
||||
.describe("拉取审批运行明细分块的上限(可选,未指定时默认拉取指定时间区间内的全量单据,最大安全上限 100000)")
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
|
||||
@@ -95,7 +95,7 @@ export type CollectOptions = {
|
||||
corp: { id: string; name: string };
|
||||
/** 单据模板白名单,未指定则拉取全量模板 */
|
||||
templates?: string[];
|
||||
/** 用于拉明细分块的上限,防御性封顶 */
|
||||
/** 用于拉明细分块的上限(可选,未指定时默认拉取指定时间区间全量单据,最大安全上限 100000) */
|
||||
maxRuns?: number;
|
||||
/** 是否载入本地人工裁决(默认载入);关掉则 humanAccuracy 恒为不可得 */
|
||||
loadReviews?: boolean;
|
||||
@@ -165,6 +165,9 @@ export function resolveReportPeriod(options: {
|
||||
throw validationError("缺少统计起止时间:请指定 startDate 和 endDate(如 2026-08-12 至 2026-09-18)");
|
||||
}
|
||||
|
||||
/** 默认明细拉取安全兜底上限(100,000 笔),未指定限制时默认拉取全区间单据 */
|
||||
export const DEFAULT_MAX_REPORT_RUNS = 100_000;
|
||||
|
||||
/** 拉取报告所需数据。只取 demand 需要的部分,模板没提到的章节不发请求。 */
|
||||
export async function collectReportData(
|
||||
client: KyInstance,
|
||||
@@ -172,7 +175,7 @@ export async function collectReportData(
|
||||
options: CollectOptions
|
||||
): Promise<ReportData> {
|
||||
const range = resolveReportPeriod(options);
|
||||
const maxRuns = Math.min(options.maxRuns ?? 500, 500);
|
||||
const maxRuns = options.maxRuns ?? DEFAULT_MAX_REPORT_RUNS;
|
||||
const fetched: Record<ReportDemand, boolean> = {
|
||||
performance: false,
|
||||
riskStats: false,
|
||||
@@ -254,17 +257,19 @@ export async function collectReportData(
|
||||
let offset = 0;
|
||||
try {
|
||||
while (runs.length < maxRuns) {
|
||||
const fetchLimit = Math.min(100, maxRuns - runs.length);
|
||||
const page = await api.listApprovalRuns(client, {
|
||||
offset,
|
||||
limit: 100,
|
||||
limit: fetchLimit,
|
||||
startDate: range.startDate,
|
||||
endDate: range.endDate
|
||||
});
|
||||
const records = page.records ?? [];
|
||||
if (!records.length) break;
|
||||
runs.push(...records);
|
||||
const toTake = records.slice(0, maxRuns - runs.length);
|
||||
runs.push(...toTake);
|
||||
offset += records.length;
|
||||
if (records.length < 100) break;
|
||||
if (records.length < fetchLimit || runs.length >= maxRuns) break;
|
||||
}
|
||||
|
||||
let blocks: ReportBlock[] = runs.map((record) => {
|
||||
|
||||
@@ -353,6 +353,65 @@ describe("review store + human accuracy join", () => {
|
||||
const xlsx = await buildReportLedgerXlsx(data);
|
||||
expect(xlsx.byteLength).toBeGreaterThan(1000);
|
||||
});
|
||||
|
||||
test("fetches over 500 records across pages without being clamped when maxRuns is omitted", async () => {
|
||||
// 模拟服务端有 650 条记录(7 页:6 页 100 条 + 1 页 50 条)
|
||||
const totalRecords = 650;
|
||||
const fakeClient = {
|
||||
get: async (_url: string, opts?: { searchParams?: URLSearchParams }) => {
|
||||
const offset = Number(opts?.searchParams?.get("offset") ?? 0);
|
||||
const limit = Number(opts?.searchParams?.get("limit") ?? 100);
|
||||
const count = Math.max(0, Math.min(limit, totalRecords - offset));
|
||||
const records = Array.from({ length: count }, (_, i) => ({
|
||||
id: `run-${offset + i}`,
|
||||
sourceDocumentSN: `SN-${offset + i}`,
|
||||
result: { decision: "同意" },
|
||||
pipelineData: { humanResult: "同意" }
|
||||
}));
|
||||
return {
|
||||
json: async () => ({ data: { records, total: totalRecords } })
|
||||
};
|
||||
}
|
||||
} as never;
|
||||
|
||||
const data = await collectReportData(fakeClient, new Set(["blocks"]), {
|
||||
month: "2026-08",
|
||||
corp: { id: "corp-a", name: "A" }
|
||||
});
|
||||
|
||||
// 验证突破了原本的 500 条限制,全部 650 条完整拉出
|
||||
expect(data.blocks).toHaveLength(650);
|
||||
});
|
||||
|
||||
test("respects explicit maxRuns limit and slices precisely across pages", async () => {
|
||||
const totalRecords = 500;
|
||||
const fakeClient = {
|
||||
get: async (_url: string, opts?: { searchParams?: URLSearchParams }) => {
|
||||
const offset = Number(opts?.searchParams?.get("offset") ?? 0);
|
||||
const limit = Number(opts?.searchParams?.get("limit") ?? 100);
|
||||
const count = Math.max(0, Math.min(limit, totalRecords - offset));
|
||||
const records = Array.from({ length: count }, (_, i) => ({
|
||||
id: `run-${offset + i}`,
|
||||
sourceDocumentSN: `SN-${offset + i}`,
|
||||
result: { decision: "同意" },
|
||||
pipelineData: { humanResult: "同意" }
|
||||
}));
|
||||
return {
|
||||
json: async () => ({ data: { records, total: totalRecords } })
|
||||
};
|
||||
}
|
||||
} as never;
|
||||
|
||||
const data = await collectReportData(fakeClient, new Set(["blocks"]), {
|
||||
month: "2026-08",
|
||||
corp: { id: "corp-a", name: "A" },
|
||||
maxRuns: 150
|
||||
});
|
||||
|
||||
expect(data.blocks).toHaveLength(150);
|
||||
expect(data.blocks[0]?.sn).toBe("SN-0");
|
||||
expect(data.blocks[149]?.sn).toBe("SN-149");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMonthlyReportData", () => {
|
||||
|
||||
Reference in new issue
Block a user