diff --git a/skills/seal-use/references/report-templates.md b/skills/seal-use/references/report-templates.md index 6c62d3f..e583ff8 100644 --- a/skills/seal-use/references/report-templates.md +++ b/skills/seal-use/references/report-templates.md @@ -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 `(别名 `--max-runs ` / `-n `,例如 `--limit 2000`)。 + --- ### 模式 B:CLI 命令行实时研讨驱动 diff --git a/src/cli/commands/report.ts b/src/cli/commands/report.ts index f60265b..96dde41 100644 --- a/src/cli/commands/report.ts +++ b/src/cli/commands/report.ts @@ -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 --end-date [--templates ] [--max-runs ] [--out ] [--corp ]" + "Usage: seal-use report ledger --start-date --end-date [--templates ] [--limit ] [--out ] [--corp ]" ); } @@ -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 --end-date ) [--templates ] [--max-runs ] [--corp ]" + "Usage: seal-use report data (--ledger <台账.xlsx> | --start-date --end-date ) [--templates ] [--limit ] [--corp ]" ); } @@ -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 --end-date ) [--templates ] [--template ] [--report-id ] [--corp ]" + "Usage: seal-use report generate (--ledger <台账.xlsx> | --start-date --end-date ) [--templates ] [--limit ] [--template ] [--report-id ] [--corp ]" ); } @@ -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; diff --git a/src/cli/help.ts b/src/cli/help.ts index d34e00c..4a8819f 100644 --- a/src/cli/help.ts +++ b/src/cli/help.ts @@ -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 --end-date [--templates ] [--out ] [--corp ]"], + usage: ["seal-use report ledger --start-date --end-date [--templates ] [--limit ] [--out ] [--corp ]"], options: [ { flag: "--start-date ", description: "Statistics start date, YYYY-MM-DD (e.g. 2026-08-12)" }, { flag: "--end-date ", description: "Statistics end date, YYYY-MM-DD (e.g. 2026-09-18)" }, @@ -633,20 +633,20 @@ export const HELP_REGISTRY: AreaHelp[] = [ { flag: "--ledger ", description: "Inspect or convert an existing ledger file" }, { flag: "--out ", description: "Output path (.xlsx or .csv); defaults to 台账--.xlsx" }, { flag: "--format ", description: "Export format (default: xlsx, or inferred from --out)" }, - { flag: "--max-runs ", description: "Cap approval-run fetch, default 500" } + { flag: "--limit ", 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 --end-date ) [--templates ] [--max-runs ] [--corp ]"], + usage: ["seal-use report data (--ledger <台账.xlsx> | --start-date --end-date ) [--templates ] [--limit ] [--corp ]"], options: [ { flag: "--start-date ", description: "Statistics start date, YYYY-MM-DD" }, { flag: "--end-date ", description: "Statistics end date, YYYY-MM-DD" }, { flag: "--ledger ", description: "Directly parse an edited Excel/CSV ledger file without remote requests" }, { flag: "--templates ", description: "Comma-separated template whitelist" }, - { flag: "--max-runs ", description: "Cap approval-run detail fetch, default 500 (max 500)" } + { flag: "--limit ", 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 --end-date ) [--templates ] [--out <报告.md>] [--template ] [--report-id ] [--max-runs ] [--allow-unreviewed] [--corp ]"], + usage: ["seal-use report generate (--ledger <台账.xlsx> | --start-date --end-date ) [--templates ] [--out <报告.md>] [--template ] [--report-id ] [--limit ] [--allow-unreviewed] [--corp ]"], options: [ { flag: "--start-date ", description: "Statistics start date, YYYY-MM-DD (e.g. 2026-08-12)" }, { flag: "--end-date ", description: "Statistics end date, YYYY-MM-DD (e.g. 2026-09-18)" }, @@ -685,7 +685,7 @@ export const HELP_REGISTRY: AreaHelp[] = [ { flag: "--out ", 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 ", description: "Custom report id; defaults to RPT--" }, - { flag: "--max-runs ", description: "Cap approval-run detail fetch, default 500 (max 500)" }, + { flag: "--limit ", 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." diff --git a/src/domains/seal/capabilities/capabilities.test.ts b/src/domains/seal/capabilities/capabilities.test.ts index 16abbd7..382b2ac 100644 --- a/src/domains/seal/capabilities/capabilities.test.ts +++ b/src/domains/seal/capabilities/capabilities.test.ts @@ -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 () => { diff --git a/src/domains/seal/capabilities/reports.ts b/src/domains/seal/capabilities/reports.ts index e65f2e3..4ad05b6 100644 --- a/src/domains/seal/capabilities/reports.ts +++ b/src/domains/seal/capabilities/reports.ts @@ -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, diff --git a/src/domains/seal/reports/pipeline/collect.ts b/src/domains/seal/reports/pipeline/collect.ts index b057309..0d66266 100644 --- a/src/domains/seal/reports/pipeline/collect.ts +++ b/src/domains/seal/reports/pipeline/collect.ts @@ -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 { const range = resolveReportPeriod(options); - const maxRuns = Math.min(options.maxRuns ?? 500, 500); + const maxRuns = options.maxRuns ?? DEFAULT_MAX_REPORT_RUNS; const fetched: Record = { 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) => { diff --git a/src/domains/seal/reports/pipeline/reports.test.ts b/src/domains/seal/reports/pipeline/reports.test.ts index 4eeba8d..326efa8 100644 --- a/src/domains/seal/reports/pipeline/reports.test.ts +++ b/src/domains/seal/reports/pipeline/reports.test.ts @@ -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", () => {