Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [ ] PR title and description: `gh repo-checks pr-title "<title>"`, `gh repo-checks pr-body <body-file>`
- [ ] 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`
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitlab/merge_request_templates/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [ ] MR title and description: `gh repo-checks pr-title "<title>"`, `gh repo-checks pr-body <body-file>`
- [ ] 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`
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions packages/agent-task-loop/src/adapters/claude-lines.test.ts
Original file line number Diff line number Diff line change
@@ -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' },
]);
});
});
25 changes: 7 additions & 18 deletions packages/agent-task-loop/src/adapters/claude-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* progress/summary extractors read them.
*/

import { jsonLinesLastFirst } from '../utils/json-lines';

export interface ClaudeStreamEvent {
type?: string;
subtype?: string;
Expand Down Expand Up @@ -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',
);
}
29 changes: 29 additions & 0 deletions packages/agent-task-loop/src/services/review-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
36 changes: 20 additions & 16 deletions packages/agent-task-loop/src/services/review-service.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,34 +18,37 @@ 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) {
return 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;
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/agent-task-loop/src/utils/json-lines.test.ts
Original file line number Diff line number Diff line change
@@ -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 }]);
});
});
21 changes: 21 additions & 0 deletions packages/agent-task-loop/src/utils/json-lines.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading