diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
index c94bd675..e8225e9d 100644
--- a/.github/pull_request_template.md
+++ b/.github/pull_request_template.md
@@ -14,6 +14,7 @@
- [ ] PR title and description: `gh repo-checks pr-title "
"`, `gh repo-checks pr-body `
- [ ] Install: `pnpm install --frozen-lockfile`
- [ ] Format: `pnpm format:check`
+- [ ] Duplicate code: `pnpm duplicates` (any reported clone fails)
- [ ] Test: `pnpm test`
- [ ] Build: `pnpm build`
- [ ] Typecheck: `pnpm typecheck`
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 748a6593..7586fa81 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -32,6 +32,9 @@ jobs:
- run: pnpm format:check
+ # Fails on any clone jscpd reports (lint-config's zero-threshold profile).
+ - run: pnpm duplicates
+
- run: pnpm test
- run: pnpm build
diff --git a/.gitlab/merge_request_templates/default.md b/.gitlab/merge_request_templates/default.md
index fece2cba..356103fc 100644
--- a/.gitlab/merge_request_templates/default.md
+++ b/.gitlab/merge_request_templates/default.md
@@ -14,6 +14,7 @@
- [ ] MR title and description: `gh repo-checks pr-title ""`, `gh repo-checks pr-body `
- [ ] Install: `pnpm install --frozen-lockfile`
- [ ] Format: `pnpm format:check`
+- [ ] Duplicate code: `pnpm duplicates` (any reported clone fails)
- [ ] Test: `pnpm test`
- [ ] Build: `pnpm build`
- [ ] Typecheck: `pnpm typecheck`
diff --git a/AGENTS.md b/AGENTS.md
index 525c81ab..ebd4ec57 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -32,6 +32,7 @@ gh repo-checks pr-body pr-body.md
# Install, then the CI gates:
pnpm install --frozen-lockfile
pnpm format:check # `pnpm format` rewrites files
+pnpm duplicates # fails on any clone jscpd reports
pnpm test
pnpm build
pnpm typecheck
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index b9ee0a17..6210c3e8 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -52,13 +52,14 @@ gh repo-checks pr-body pr-body.md
# Install and CI gates:
pnpm install --frozen-lockfile
pnpm format:check
+pnpm duplicates
pnpm test
pnpm build
pnpm typecheck
pnpm lint
```
-`pnpm format` applies the formatting. `pnpm lint` is type-aware: it resolves workspace packages through their built `dist` types and room-web through the route types that `pnpm typecheck` generates, so run it after `pnpm build` and `pnpm typecheck`.
+`pnpm duplicates` runs jscpd with lint-config's gate profile, where any reported clone fails (the threshold is 0); the scan roots and ignores live in the script, and `pnpm duplicates:audit` writes a broader, report-only list to the git-ignored `tmp/duplicates-audit/`. `pnpm format` applies the formatting. `pnpm lint` is type-aware: it resolves workspace packages through their built `dist` types and room-web through the route types that `pnpm typecheck` generates, so run it after `pnpm build` and `pnpm typecheck`.
### Lint, Format, And TypeScript Config
diff --git a/packages/agent-task-loop/src/adapters/claude-lines.test.ts b/packages/agent-task-loop/src/adapters/claude-lines.test.ts
new file mode 100644
index 00000000..5cfa6bad
--- /dev/null
+++ b/packages/agent-task-loop/src/adapters/claude-lines.test.ts
@@ -0,0 +1,19 @@
+import { describe, expect, it } from 'vite-plus/test';
+import { claudeResultEvents } from './claude-lines';
+
+describe('claudeResultEvents', () => {
+ it('keeps only the object lines, last first', () => {
+ const output = [
+ '{"type":"system","subtype":"init"}',
+ 'null',
+ 'progress text',
+ '7',
+ '{"type":"result","subtype":"success","result":"done"}',
+ ].join('\n');
+
+ expect(claudeResultEvents(output)).toEqual([
+ { type: 'result', subtype: 'success', result: 'done' },
+ { type: 'system', subtype: 'init' },
+ ]);
+ });
+});
diff --git a/packages/agent-task-loop/src/adapters/claude-lines.ts b/packages/agent-task-loop/src/adapters/claude-lines.ts
index f84d1b8b..c2fd75d6 100644
--- a/packages/agent-task-loop/src/adapters/claude-lines.ts
+++ b/packages/agent-task-loop/src/adapters/claude-lines.ts
@@ -3,6 +3,8 @@
* progress/summary extractors read them.
*/
+import { jsonLinesLastFirst } from '../utils/json-lines';
+
export interface ClaudeStreamEvent {
type?: string;
subtype?: string;
@@ -31,22 +33,9 @@ export function claudeInnerEvent(event: ClaudeStreamEvent): ClaudeStreamEvent['e
/** The result events an output holds, last line first. */
export function claudeResultEvents(output: string): ClaudeStreamEvent[] {
- const events: ClaudeStreamEvent[] = [];
- const lines = output
- .split('\n')
- .map((line) => line.trim())
- .filter(Boolean);
- for (let index = lines.length - 1; index >= 0; index -= 1) {
- try {
- // A bare `null` (or any non-object) is valid JSON but not an event; the
- // result consumers read `.type` on every element, so it must not enter.
- const parsed: unknown = JSON.parse(lines[index]!);
- if (parsed !== null && typeof parsed === 'object') {
- events.push(parsed as ClaudeStreamEvent);
- }
- } catch {
- continue;
- }
- }
- return events;
+ // A bare `null` (or any non-object) is valid JSON but not an event; the
+ // result consumers read `.type` on every element, so it must not enter.
+ return jsonLinesLastFirst(output).filter(
+ (value): value is ClaudeStreamEvent => value !== null && typeof value === 'object',
+ );
}
diff --git a/packages/agent-task-loop/src/services/review-service.test.ts b/packages/agent-task-loop/src/services/review-service.test.ts
index 524e6524..58530c30 100644
--- a/packages/agent-task-loop/src/services/review-service.test.ts
+++ b/packages/agent-task-loop/src/services/review-service.test.ts
@@ -65,6 +65,35 @@ describe('ReviewService', () => {
expect(result.sessionName).toBe('task-202-review-codex-r2');
});
+ it('skips trailing JSON lines that are not objects', async () => {
+ const service = new ReviewService({
+ adapter: {
+ execute: vi.fn().mockResolvedValue({
+ status: 'success',
+ summary: ['{"verdict":"驳回","findings":["1. 缺少测试"]}', 'null', '42'].join('\n'),
+ workspacePath: '/tmp/workspace',
+ }),
+ } as never,
+ command: {
+ command: 'codex',
+ args: [],
+ env: {},
+ },
+ });
+
+ const result = await service.review({
+ taskId: 'TASK-203',
+ description: 'desc',
+ resultSummary: 'done',
+ workspacePath: '/tmp/workspace',
+ reviewRound: 1,
+ reviewerAgent: 'codex',
+ });
+
+ expect(result.verdict).toBe('驳回');
+ expect(result.findings).toBe('1. 缺少测试');
+ });
+
it('passes chairman acceptance feedback to the review prompt as hard constraints', async () => {
const execute = vi.fn().mockResolvedValue({
status: 'success',
diff --git a/packages/agent-task-loop/src/services/review-service.ts b/packages/agent-task-loop/src/services/review-service.ts
index 055275a8..465b9a6f 100644
--- a/packages/agent-task-loop/src/services/review-service.ts
+++ b/packages/agent-task-loop/src/services/review-service.ts
@@ -1,5 +1,6 @@
import type { AgentAdapter } from '../adapters/base';
import type { ReviewVerdict, TargetAgent } from '../types/task';
+import { jsonLinesLastFirst } from '../utils/json-lines';
export interface ReviewResult {
verdict: ReviewVerdict;
@@ -17,6 +18,18 @@ interface ParsedReviewPayload {
type ValidReviewPayload = ParsedReviewPayload & { verdict: ReviewVerdict };
+function asReviewPayload(value: unknown): ValidReviewPayload | undefined {
+ // Parsed JSON may be `null` or a primitive; only an object can carry the fields.
+ if (value === null || typeof value !== 'object') {
+ return undefined;
+ }
+ const payload = value as ParsedReviewPayload;
+ if ((payload.verdict === '通过' || payload.verdict === '驳回') && Array.isArray(payload.findings)) {
+ return payload as ValidReviewPayload;
+ }
+ return undefined;
+}
+
function tryParseReviewPayload(raw: string): ValidReviewPayload | undefined {
const trimmed = raw.trim();
if (!trimmed) {
@@ -24,27 +37,18 @@ function tryParseReviewPayload(raw: string): ValidReviewPayload | undefined {
}
try {
- const parsed = JSON.parse(trimmed) as ParsedReviewPayload;
- if ((parsed.verdict === '通过' || parsed.verdict === '驳回') && Array.isArray(parsed.findings)) {
- return parsed as ValidReviewPayload;
+ const whole = asReviewPayload(JSON.parse(trimmed));
+ if (whole) {
+ return whole;
}
} catch {
// Fall through and try line-by-line extraction.
}
- const lines = trimmed
- .split('\n')
- .map((line) => line.trim())
- .filter(Boolean);
-
- for (let index = lines.length - 1; index >= 0; index -= 1) {
- try {
- const parsed = JSON.parse(lines[index]!) as ParsedReviewPayload;
- if ((parsed.verdict === '通过' || parsed.verdict === '驳回') && Array.isArray(parsed.findings)) {
- return parsed as ValidReviewPayload;
- }
- } catch {
- continue;
+ for (const value of jsonLinesLastFirst(trimmed)) {
+ const payload = asReviewPayload(value);
+ if (payload) {
+ return payload;
}
}
diff --git a/packages/agent-task-loop/src/utils/json-lines.test.ts b/packages/agent-task-loop/src/utils/json-lines.test.ts
new file mode 100644
index 00000000..661e25ca
--- /dev/null
+++ b/packages/agent-task-loop/src/utils/json-lines.test.ts
@@ -0,0 +1,10 @@
+import { describe, expect, it } from 'vite-plus/test';
+import { jsonLinesLastFirst } from './json-lines';
+
+describe('jsonLinesLastFirst', () => {
+ it('returns the JSON lines last first and skips blank and non-JSON lines', () => {
+ const output = ['{"a":1}', '', 'not json', ' [2] ', 'null', '{"b":'].join('\n');
+
+ expect(jsonLinesLastFirst(output)).toEqual([null, [2], { a: 1 }]);
+ });
+});
diff --git a/packages/agent-task-loop/src/utils/json-lines.ts b/packages/agent-task-loop/src/utils/json-lines.ts
new file mode 100644
index 00000000..b7f96ffa
--- /dev/null
+++ b/packages/agent-task-loop/src/utils/json-lines.ts
@@ -0,0 +1,21 @@
+/**
+ * The JSON value of each non-empty line of an agent's output, last line first.
+ * A line that is not valid JSON is skipped; any other value, `null` included,
+ * is kept, so callers check the shape they need.
+ */
+export function jsonLinesLastFirst(output: string): unknown[] {
+ const values: unknown[] = [];
+ const lines = output.split('\n');
+ for (let index = lines.length - 1; index >= 0; index -= 1) {
+ const line = lines[index]!.trim();
+ if (!line) {
+ continue;
+ }
+ try {
+ values.push(JSON.parse(line));
+ } catch {
+ continue;
+ }
+ }
+ return values;
+}