@cloud411716/fancy-webnovel 4.30.22 → 4.30.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud411716/fancy-webnovel",
3
- "version": "4.30.22",
3
+ "version": "4.30.23",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -1831,6 +1831,8 @@ export async function runGenerationWithPlan(ctx, invocation, session, taskList,
1831
1831
  manualBlock,
1832
1832
  team,
1833
1833
  signal: invocation.signal,
1834
+ validate: (candidate) => validateBatch(candidate, item, groupId),
1835
+ batchLabel: item.kind + (item.subkind ? '_' + item.subkind : ''),
1834
1836
  batchId,
1835
1837
  batchNo,
1836
1838
  });
@@ -171,6 +171,27 @@ function writeJsonFile(file, value) {
171
171
  fs.writeFileSync(file, JSON.stringify(value, null, 2) + '\n', 'utf8');
172
172
  }
173
173
 
174
+ /** Preserve the previous attempt's A/B/review artifacts before a retry overwrites them. */
175
+ function archivePreviousTeamArtifacts(runDir) {
176
+ const metaFile = path.join(runDir, 'team-meta.json');
177
+ let meta;
178
+ try { meta = JSON.parse(fs.readFileSync(metaFile, 'utf8')); } catch { return; }
179
+ const label = String(meta.batchLabel || meta.batchNo || 'batch').replace(/[^\w\u4e00-\u9fa5.-]+/g, '-');
180
+ const stamp = String(meta.startedAt || Date.now()).replace(/[:.]/g, '-');
181
+ const dir = path.join(runDir, 'team', `${label}-${stamp}`);
182
+ try {
183
+ fs.mkdirSync(dir, { recursive: true });
184
+ for (const name of [
185
+ 'team-prompt-a.txt', 'team-prompt-b.txt', 'team-rubric.md',
186
+ 'team-a.jsonl', 'team-b.jsonl', 'team-final.jsonl',
187
+ 'team-meta.json', 'team-review.json',
188
+ ]) {
189
+ const source = path.join(runDir, name);
190
+ if (fs.existsSync(source)) fs.copyFileSync(source, path.join(dir, name));
191
+ }
192
+ } catch { /* best-effort diagnostics */ }
193
+ }
194
+
174
195
  const TEAM_RUBRIC = `
175
196
  # Team 评审 rubric
176
197
 
@@ -340,6 +361,13 @@ const judgePrompt = (round, aPassed, bPassed, history, fileA, fileB, finalFile,
340
361
  '如果选择 PICK_A、PICK_B 或 MERGE,把最终 JSONL 写到 ' + finalFile + '。' +
341
362
  '返回 {"decision":"...","rationale":"..."}。';
342
363
 
364
+ const repairPrompt = (candidateFile, detail, finalFile) =>
365
+ '你是主编。当前最终文件没有通过插件校验。\n读取 ' + candidateFile + '。\n' +
366
+ '校验失败原因:\n' + String(detail || '').slice(0, 12000) + '\n' +
367
+ '只修复被点名的条目;保持其余条目、条数、group_id、subkind、world_id、genres 和 source 不变。' +
368
+ '把修复后的完整 JSONL 写到 ' + finalFile + '。' +
369
+ '返回 {"decision":"MERGE","rationale":"..."}。';
370
+
343
371
  /**
344
372
  * Run one batch through two generators, bounded cross-review, and a judge.
345
373
  * Uses the host subagent service directly so the plugin does not depend on a
@@ -355,6 +383,8 @@ export async function runTeamBatch({
355
383
  manualBlock = '',
356
384
  team,
357
385
  signal,
386
+ validate,
387
+ batchLabel = 'batch',
358
388
  batchId = '',
359
389
  batchNo = 1,
360
390
  }) {
@@ -372,6 +402,7 @@ export async function runTeamBatch({
372
402
  const promptAFile = path.join(runDir, 'team-prompt-a.txt');
373
403
  const promptBFile = path.join(runDir, 'team-prompt-b.txt');
374
404
  const rubricFile = path.join(runDir, 'team-rubric.md');
405
+ archivePreviousTeamArtifacts(runDir);
375
406
  for (const stale of [aFile, bFile, finalFile]) {
376
407
  try { fs.unlinkSync(stale); } catch { /* first run */ }
377
408
  }
@@ -383,6 +414,7 @@ export async function runTeamBatch({
383
414
  const reviewFile = path.join(runDir, 'team-review.json');
384
415
  const meta = {
385
416
  batchId,
417
+ batchLabel,
386
418
  batchNo,
387
419
  startedAt: new Date().toISOString(),
388
420
  generatorA: team.generatorA,
@@ -433,7 +465,8 @@ export async function runTeamBatch({
433
465
 
434
466
  // Keep one call in reserve for the judge. A full review-response-revision
435
467
  // round costs up to six calls, so the budget check happens before each stage.
436
- const canSpend = (n) => calls + n <= maxCalls - 1;
468
+ const reserve = typeof validate === 'function' ? 2 : 1;
469
+ const canSpend = (n) => calls + n <= maxCalls - reserve;
437
470
 
438
471
  const saveReview = () => writeJsonFile(reviewFile, { round, aPassed, bPassed, calls, stopReason, history });
439
472
 
@@ -547,22 +580,69 @@ export async function runTeamBatch({
547
580
  meta.calls = calls;
548
581
  meta.finishedAt = new Date().toISOString();
549
582
 
550
- if (fs.existsSync(finalFile)) {
551
- fs.copyFileSync(finalFile, file);
552
- meta.finalFile = finalFile;
553
- writeJsonFile(metaFile, meta);
554
- return { ok: true, result: { round, aPassed, bPassed, calls, judge: judge.value }, finalFile };
583
+ let candidate = fs.existsSync(finalFile) ? finalFile : '';
584
+ let fallback = false;
585
+ if (!candidate) {
586
+ const decision = judge.value && judge.value.decision;
587
+ const pick = decision === 'PICK_A' ? aFile : decision === 'PICK_B' ? bFile : '';
588
+ if (pick && fs.existsSync(pick)) {
589
+ candidate = pick;
590
+ fallback = true;
591
+ }
555
592
  }
556
- const decision = judge.value && judge.value.decision;
557
- const fallback = decision === 'PICK_A' ? aFile : decision === 'PICK_B' ? bFile : '';
558
- if (fallback && fs.existsSync(fallback)) {
559
- fs.copyFileSync(fallback, file);
560
- meta.finalFile = fallback;
561
- meta.fallback = true;
593
+ if (!candidate) {
594
+ meta.error = '裁判没有写出 final.jsonl(decision=' + ((judge.value && judge.value.decision) || 'none') + ')';
562
595
  writeJsonFile(metaFile, meta);
563
- return { ok: true, result: { round, aPassed, bPassed, calls, judge: judge.value }, finalFile: fallback, fallback: true };
596
+ return { ok: false, error: 'team workflow 完成,但' + meta.error + '。' };
564
597
  }
565
- meta.error = '裁判没有写出 final.jsonl(decision=' + (decision || 'none') + ')';
598
+
599
+ if (typeof validate === 'function') {
600
+ let checked = await validate(candidate);
601
+ if (!checked.ok) {
602
+ meta.validationError = checked.detail;
603
+ writeJsonFile(metaFile, meta);
604
+ if (calls >= maxCalls) {
605
+ meta.error = '最终文件未通过校验,且没有剩余调用可修复:' + checked.detail;
606
+ writeJsonFile(metaFile, meta);
607
+ return { ok: false, error: meta.error };
608
+ }
609
+ const repaired = await call(
610
+ repairPrompt(candidate, checked.detail, finalFile),
611
+ team.judge,
612
+ 'repair',
613
+ JUDGE_SCHEMA,
614
+ );
615
+ if (!repaired.ok) {
616
+ meta.error = '裁判修复失败:' + repaired.error;
617
+ writeJsonFile(metaFile, meta);
618
+ return { ok: false, error: meta.error };
619
+ }
620
+ meta.repair = repaired.value;
621
+ if (!fs.existsSync(finalFile)) {
622
+ meta.error = '裁判修复后没有写出 final.jsonl';
623
+ writeJsonFile(metaFile, meta);
624
+ return { ok: false, error: meta.error };
625
+ }
626
+ checked = await validate(finalFile);
627
+ if (!checked.ok) {
628
+ meta.error = '裁判修复后仍未通过校验:' + checked.detail;
629
+ writeJsonFile(metaFile, meta);
630
+ return { ok: false, error: meta.error };
631
+ }
632
+ candidate = finalFile;
633
+ fallback = false;
634
+ }
635
+ }
636
+
637
+ fs.copyFileSync(candidate, file);
638
+ meta.finalFile = candidate;
639
+ meta.fallback = fallback;
640
+ meta.validated = typeof validate === 'function';
566
641
  writeJsonFile(metaFile, meta);
567
- return { ok: false, error: 'team workflow 完成,但' + meta.error + '。' };
642
+ return {
643
+ ok: true,
644
+ result: { round, aPassed, bPassed, calls, judge: judge.value },
645
+ finalFile: candidate,
646
+ fallback,
647
+ };
568
648
  }