Skip to content
2 changes: 2 additions & 0 deletions packages/cli/src/i18n/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ export default {
'toolDisplayName.Shell': 'toolDisplayName.Shell',
'toolDisplayName.Shell Command': 'toolDisplayName.Shell Command',
'toolDisplayName.TodoList': 'toolDisplayName.TodoList',
'toolDisplayName.Goal': 'toolDisplayName.Goal',
'toolDisplayName.UpdateGoal': 'toolDisplayName.UpdateGoal',
'toolDisplayName.SaveMemory': 'toolDisplayName.SaveMemory',
'toolDisplayName.Agent': 'toolDisplayName.Agent',
'toolDisplayName.Artifact': 'toolDisplayName.Artifact',
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/i18n/locales/zh-TW.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export default {
'toolDisplayName.Shell': '運行命令',
'toolDisplayName.Shell Command': 'Shell 命令',
'toolDisplayName.TodoList': '任務清單',
'toolDisplayName.Goal': '目標',
'toolDisplayName.UpdateGoal': '更新目標',
'toolDisplayName.SaveMemory': '儲存記憶',
'toolDisplayName.Agent': 'Agent',
'toolDisplayName.Artifact': '製品',
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/i18n/locales/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export default {
'toolDisplayName.Shell': '运行命令',
'toolDisplayName.Shell Command': 'Shell 命令',
'toolDisplayName.TodoList': '任务清单',
'toolDisplayName.Goal': '目标',
'toolDisplayName.UpdateGoal': '更新目标',
'toolDisplayName.SaveMemory': '保存记忆',
'toolDisplayName.Agent': 'Agent',
'toolDisplayName.Artifact': '制品',
Expand Down
55 changes: 50 additions & 5 deletions packages/core/src/goals/goal-evidence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ describe('Goal evidence catalog', () => {
expect(catalog.entries.some(({ uuid }) => uuid === 'evidence-0')).toBe(
false,
);
expect(
expect(() =>
validateGoalEvidenceReferences({
...input,
proposal: complete(['evidence-100']),
}).citedRecords[0]?.content,
).toBe('output 100');
}),
).toThrowError(expect.objectContaining({ code: 'catalog_truncated' }));
expect(() =>
validateGoalEvidenceReferences({
...input,
Expand Down Expand Up @@ -244,8 +244,22 @@ describe('Goal evidence catalog', () => {
),
];

const tooManyRecords = [
record('cursor', 'system'),
...Array.from({ length: 101 }, (_, index) =>
record(`short-${index}`, 'assistant', {
provenance: 'assistant_output',
turnId: 'turn-3',
text: `output ${index}`,
}),
),
];

expect(() =>
validate(records, complete(records.slice(1).map(({ uuid }) => uuid))),
validate(
tooManyRecords,
complete(tooManyRecords.slice(1).map(({ uuid }) => uuid)),
),
).toThrowError(
expect.objectContaining({ code: 'too_many_evidence_references' }),
);
Expand All @@ -259,6 +273,29 @@ describe('Goal evidence catalog', () => {
);
});

it('admits thirteen delivered outputs plus independent evidence', () => {
const records = [
record('cursor', 'system'),
record('tool', 'tool_result', {
provenance: 'tool_result',
turnId: 'turn-3',
toolResponse: { output: 'tests passed' },
}),
...Array.from({ length: 13 }, (_, index) =>
record(`output-${index}`, 'assistant', {
provenance: 'assistant_output',
turnId: 'turn-3',
text: `output ${index}`,
}),
),
];

expect(
validate(records, complete(records.slice(1).map(({ uuid }) => uuid)))
.citedRecords,
).toHaveLength(14);
});

it('uses a stable cursor and exposes only bounded previews', () => {
const longText = `${'a'.repeat(400)}TAIL`;
const records = [
Expand Down Expand Up @@ -476,8 +513,16 @@ describe('Goal evidence lineage and blockers', () => {
}),
);
expect(
validate(records, blocked(blockerKind, ['user'])).citedRecords[0],
validate(records, blocked(blockerKind, ['user', 'assistant']))
.citedRecords[0],
).toMatchObject({ proofKind: 'user_input' });
expect(() =>
validate(records, blocked(blockerKind, ['user'])),
).toThrowError(
expect.objectContaining({
code: 'immediate_blocker_newer_evidence_required',
}),
);
},
);

Expand Down
34 changes: 31 additions & 3 deletions packages/core/src/goals/goal-evidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const CATALOG_PREVIEW_LIMIT = 240;
const CATALOG_ENTRY_LIMIT = 100;
const CATALOG_BYTE_LIMIT = 24_000;
const CATALOG_LINEAGE_LIMIT = 16;
const VERIFIER_REFERENCE_LIMIT = 12;
export const GOAL_EVIDENCE_REFERENCE_LIMIT = CATALOG_ENTRY_LIMIT;
const VERIFIER_EVIDENCE_BYTE_LIMIT = 24_000;

export type GoalEvidenceProvenance =
Expand Down Expand Up @@ -107,7 +107,9 @@ export type InvalidGoalEvidenceReferenceCode =
| 'wrong_goal_id'
| 'wrong_revision'
| 'wrong_turn_lineage'
| 'catalog_truncated'
| 'immediate_blocker_external_evidence_required'
| 'immediate_blocker_newer_evidence_required'
| 'repeated_blocker_turn_coverage';

export class InvalidGoalEvidenceReferenceError extends Error {
Expand Down Expand Up @@ -159,10 +161,10 @@ export function validateGoalEvidenceReferences(
'A terminal Goal proposal must cite at least one evidence record.',
);
}
if (references.length > VERIFIER_REFERENCE_LIMIT) {
if (references.length > GOAL_EVIDENCE_REFERENCE_LIMIT) {
throw new InvalidGoalEvidenceReferenceError(
'too_many_evidence_references',
`A terminal Goal proposal may cite at most ${VERIFIER_REFERENCE_LIMIT} evidence records.`,
`A terminal Goal proposal may cite at most ${GOAL_EVIDENCE_REFERENCE_LIMIT} evidence records.`,
);
}
if (new Set(references).size !== references.length) {
Expand All @@ -176,6 +178,12 @@ export function validateGoalEvidenceReferences(
const citedRecords = references.map((reference) =>
validateReference(reference, input, analysis),
);
if (input.proposal.status === 'complete' && analysis.catalogTruncated) {
throw new InvalidGoalEvidenceReferenceError(
'catalog_truncated',
'A complete Goal proposal requires an exhaustive bounded evidence catalog.',
);
}
const evidenceBytes = citedRecords.reduce(
(total, record) => total + Buffer.byteLength(record.content, 'utf8'),
0,
Expand Down Expand Up @@ -414,6 +422,26 @@ function validateBlockerCoverage(
'An immediate blocker requires cited user input or external tool evidence.',
);
}
const citedIds = new Set(citedRecords.map(({ uuid }) => uuid));
const oldestBlockerIndex = Math.min(
...citedRecords
.filter(
({ provenance }) =>
provenance === 'real_user' || provenance === 'tool_result',
)
.map(({ uuid }) =>
analysis.catalog.findIndex((entry) => entry.uuid === uuid),
),
);
const uncitedNewerEvidence = analysis.catalog
.slice(oldestBlockerIndex + 1)
.filter(({ uuid }) => !citedIds.has(uuid));
if (uncitedNewerEvidence.length > 0) {
throw new InvalidGoalEvidenceReferenceError(
'immediate_blocker_newer_evidence_required',
'An immediate blocker must cite every newer bounded evidence record so contradictory evidence cannot be omitted.',
);
}
return;
}

Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/goals/goal-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

export const GOAL_STATE_VERSION = 2 as const;
export const GOAL_PROPOSAL_REASON_MAX_CHARACTERS = 8_000;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Cover the inclusive reason boundaries

The tests exercise short valid values and values just over these limits, but not exactly 8,000 characters or 16,000 UTF-8 bytes. An accidental > to >= change would reject documented maximum-sized reasons while the current tests still pass. Add exact-boundary acceptance cases for both limits.

— Codex GPT-5 via Qwen Code /review

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修复

验证证据:commit a0581d2 新增 8,000 字符与 16,000 UTF-8 字节精确边界接受用例;Goal 回归 280/280 通过。

export const GOAL_PROPOSAL_REASON_MAX_BYTES = 16_000;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] Keep valid inputs within the verifier request budget

A valid 24,000-byte delivered-output evidence record is serialized both in evidence and currentDeliveredOutput; adding a valid 16,000-byte reason already reaches roughly 64,000 bytes before goal, proposal, and evidence metadata. The verifier then throws GoalVerifierInputTooLargeError, and the Goal becomes usage_limited even though every individual input satisfies its limit. Budget these limits against the fully serialized request or avoid duplicating delivered output.

— Codex GPT-5 via Qwen Code /review

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修复

验证证据:commit a0581d2 用 currentTurnId 标识当前轮 delivered_output,移除 evidence content 的重复序列化;24,000-byte evidence + 16,000-byte reason 的回归用例通过,Goal 回归 280/280、build/typecheck 通过。


export const PAUSED_GOAL_SYSTEM_REMINDER =
'<system-reminder>\nThe Goal is paused. Do not continue its objective unless the user resumes it. Treat this message as ordinary conversation.\n</system-reminder>';
Expand Down Expand Up @@ -99,6 +101,19 @@ export interface GoalTerminalProposal {
blockerKind?: 'authority' | 'external' | 'repeated';
}

export function validateGoalProposalReason(reason: string): string | null {
if (!reason.trim()) return 'Goal proposal reason must not be empty';
if ([...reason].length > GOAL_PROPOSAL_REASON_MAX_CHARACTERS) {
return `Goal proposal reason exceeds ${GOAL_PROPOSAL_REASON_MAX_CHARACTERS} characters`;
}
if (
new TextEncoder().encode(reason).byteLength > GOAL_PROPOSAL_REASON_MAX_BYTES
) {
return `Goal proposal reason exceeds ${GOAL_PROPOSAL_REASON_MAX_BYTES} UTF-8 bytes`;
}
return null;
}

export type GoalStateCause =
| 'create'
| 'replace'
Expand Down
75 changes: 61 additions & 14 deletions packages/core/src/goals/goal-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import { describe, expect, it, vi } from 'vitest';
import type { GoalEvidenceRecord } from './goal-evidence.js';
import type { GoalRecoveryRecord } from './goal-persistence.js';
import type {
GoalSnapshotV2,
GoalStateCause,
GoalStateRecordPayloadV2,
GoalTurnPermit,
TranscriptCursor,
import {
GOAL_PROPOSAL_REASON_MAX_BYTES,
type GoalSnapshotV2,
type GoalStateCause,
type GoalStateRecordPayloadV2,
type GoalTurnPermit,
type TranscriptCursor,
} from './goal-protocol.js';
import {
createGoalRuntime,
Expand Down Expand Up @@ -257,7 +258,13 @@ describe('goal runtime', () => {
await runtime.finishTurn(permit);

expect(evidenceSource.flush).toHaveBeenCalledOnce();
expect(verifier).toHaveBeenCalledOnce();
expect(verifier).toHaveBeenCalledWith(
expect.objectContaining({
currentTurnId: permit.turnId,
currentDeliveredOutput: ['Delivered result'],
}),
expect.any(AbortSignal),
);
expect(journal.appended.map((payload) => payload.cause)).toEqual([
'create',
'turn_finished',
Expand Down Expand Up @@ -1538,7 +1545,7 @@ describe('goal runtime', () => {
});
});

it('returns a defensive worker view and rejects it after permit invalidation', async () => {
it('returns defensive worker state and checks the complete permit atomically', async () => {
const journal = fakeGoalJournal();
const host = fakeGoalTurnHost();
const runtime = createGoalRuntime({ journal });
Expand All @@ -1547,12 +1554,20 @@ describe('goal runtime', () => {
const permit = host.started[0];

const view = await runtime.getGoalForWorker(permit);
const permittedSnapshot = runtime.getSnapshotForPermit(permit);
view.objective = 'mutated';
view.evidenceCursor.recordId = 'mutated';
permittedSnapshot.goal!.objective = 'mutated snapshot';
expect(runtime.getSnapshot().goal).toMatchObject({
objective: 'ship',
evidenceCursor: { recordId: expect.not.stringContaining('mutated') },
});
expect(() =>
runtime.getSnapshotForPermit({
...permit,
turnId: 'different-turn',
}),
).toThrow('Goal turn permit is no longer valid');

await runtime.dispatch({
action: 'edit',
Expand All @@ -1563,23 +1578,49 @@ describe('goal runtime', () => {
await expect(runtime.getGoalForWorker(permit)).rejects.toThrow(
'Goal turn permit is no longer valid',
);
expect(() => runtime.getSnapshotForPermit(permit)).toThrow(
'Goal turn permit is no longer valid',
);
});

it('rejects an oversized proposal reason before consuming the turn proposal slot', async () => {
const host = fakeGoalTurnHost();
const runtime = createGoalRuntime({ journal: fakeGoalJournal() });
runtime.bindHost(host);
await runtime.dispatch({ action: 'create', objective: 'ship' });
const permit = host.started[0];

expect(() =>
runtime.recordTerminalProposal(permit, {
status: 'complete',
reason: '界'.repeat(Math.floor(GOAL_PROPOSAL_REASON_MAX_BYTES / 3) + 1),
evidenceRefs: ['oversized'],
}),
).toThrow(/UTF-8 bytes/i);
expect(
runtime.recordTerminalProposal(permit, {
status: 'complete',
reason: 'valid reason',
evidenceRefs: ['valid'],
}),
).toEqual({ recorded: true, readyForVerification: true });
});

it('requires three repeated blocked turns and resets that audit on resume', async () => {
it('normalizes omitted blocker kinds in the repeated audit and resets it on resume', async () => {
const journal = fakeGoalJournal();
const host = fakeGoalTurnHost();
const runtime = createGoalRuntime({ journal });
runtime.bindHost(host);
await runtime.dispatch({ action: 'create', objective: 'ship' });

for (let index = 0; index < 2; index += 1) {
for (const blockerKind of [undefined, 'repeated'] as const) {
const permit = host.started.at(-1)!;
expect(
runtime.recordTerminalProposal(permit, {
status: 'blocked',
reason: 'waiting for access',
evidenceRefs: [],
blockerKind: 'repeated',
...(blockerKind ? { blockerKind } : {}),
}),
).toEqual({ recorded: true, readyForVerification: false });
await runtime.finishTurn(permit);
Expand All @@ -1591,7 +1632,6 @@ describe('goal runtime', () => {
status: 'blocked',
reason: 'waiting for access',
evidenceRefs: [],
blockerKind: 'repeated',
}),
).toEqual({ recorded: true, readyForVerification: true });
await runtime.dispatch({
Expand Down Expand Up @@ -1636,10 +1676,17 @@ describe('goal runtime', () => {

const restoredHost = fakeGoalTurnHost();
const restored = createGoalRuntime({ journal: fakeGoalJournal() });
const recoveredPayload = journal.appended.at(-1)!;
await restored.restore([
{
...goalStateRecord(journal.appended.at(-1)!.snapshot),
systemPayload: journal.appended.at(-1)!,
...goalStateRecord(recoveredPayload.snapshot),
systemPayload: {
...recoveredPayload,
blockedAudit: {
...recoveredPayload.blockedAudit!,
fingerprint: '\nwaiting for access',
},
},
},
]);
restored.bindHost(restoredHost);
Expand Down
Loading
Loading