Repository navigation
feat(core): add Goal v3 state protocol #7517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
13a7f74
feat(core): add Goal v3 state protocol
qqqys e012045
fix(core): address Goal protocol review feedback
qqqys 953c848
fix(core): recover paused legacy Goals
qqqys 333276c
Merge branch 'main' into agent/goal-v3-state-protocol
wenshao a171c49
Merge branch 'main' into agent/goal-v3-state-protocol
qqqys 82e2f36
fix(core): reject unsupported Goal controls
qqqys ccc68ee
fix(core): cover recoverable Goal states
qqqys de4cd2a
fix(core): recover older valid Goal state
qqqys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Qwen Team | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
| import type { | ||
| GoalRecord, | ||
| GoalStateCause, | ||
| GoalStateRecordPayloadV2, | ||
| } from './goal-protocol.js'; | ||
| import { projectGoalStateToLegacy } from './goal-legacy-projection.js'; | ||
|
|
||
| const GOAL: GoalRecord = { | ||
| goalId: 'goal-1', | ||
| revision: 2, | ||
| objective: 'ship it', | ||
| status: 'active', | ||
| evidenceCursor: { recordId: 'state-1' }, | ||
| turnCount: 4, | ||
| activeTimeMs: 2000, | ||
| createdAt: 100, | ||
| updatedAt: 200, | ||
| lastReason: 'continuing', | ||
| }; | ||
|
|
||
| function payload( | ||
| cause: GoalStateCause, | ||
| status: GoalRecord['status'] = 'active', | ||
| goal: GoalRecord | null = { ...GOAL, status }, | ||
| ): GoalStateRecordPayloadV2 { | ||
| return { | ||
| v: 2, | ||
| cause, | ||
| snapshot: { v: 2, activity: 'idle', goal }, | ||
| }; | ||
| } | ||
|
|
||
| describe('projectGoalStateToLegacy', () => { | ||
| it.each(['create', 'replace', 'edit', 'resume', 'migrated'] as const)( | ||
| 'projects %s as legacy set with an active projection', | ||
| (cause) => { | ||
| const projected = projectGoalStateToLegacy(payload(cause)); | ||
|
|
||
| expect(projected.goalStatus.kind).toBe('set'); | ||
| expect(projected.activeGoal).toMatchObject({ | ||
| condition: 'ship it', | ||
| iterations: 4, | ||
| setAt: 100, | ||
| lastReason: 'continuing', | ||
| }); | ||
| expect(projected.goalTerminal).toBeNull(); | ||
| }, | ||
| ); | ||
|
|
||
| it('projects completion as achieved and stops active_goal', () => { | ||
| const projected = projectGoalStateToLegacy(payload('complete', 'complete')); | ||
|
|
||
| expect(projected.goalStatus.kind).toBe('achieved'); | ||
| expect(projected.activeGoal).toBeNull(); | ||
| expect(projected.goalTerminal).toMatchObject({ | ||
| kind: 'achieved', | ||
| condition: 'ship it', | ||
| iterations: 4, | ||
| durationMs: 2000, | ||
| }); | ||
| }); | ||
|
|
||
| it('projects clear as cleared using the prior goal objective', () => { | ||
| const projected = projectGoalStateToLegacy( | ||
| payload('clear', 'active', null), | ||
| GOAL, | ||
| ); | ||
|
|
||
| expect(projected.goalStatus).toMatchObject({ | ||
| kind: 'cleared', | ||
| condition: 'ship it', | ||
| }); | ||
| expect(projected.activeGoal).toBeNull(); | ||
| expect(projected.goalTerminal).toBeNull(); | ||
| }); | ||
|
|
||
| it('projects pause as a non-terminal legacy paused state', () => { | ||
| const projected = projectGoalStateToLegacy(payload('pause', 'paused')); | ||
|
|
||
| expect(projected.goalStatus.kind).toBe('paused'); | ||
| expect(projected.activeGoal).toBeNull(); | ||
| expect(projected.goalTerminal).toBeNull(); | ||
| }); | ||
|
|
||
| it.each(['blocked', 'usage_limited'] as const)( | ||
| 'projects %s as a legacy stopped state', | ||
| (status) => { | ||
| const projected = projectGoalStateToLegacy(payload(status, status)); | ||
|
|
||
| expect(projected.goalStatus.kind).toBe('aborted'); | ||
| expect(projected.activeGoal).toBeNull(); | ||
| expect(projected.goalTerminal).toMatchObject({ | ||
| kind: 'aborted', | ||
| condition: 'ship it', | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| it('uses checking for active runtime progress without widening the union', () => { | ||
| const projected = projectGoalStateToLegacy( | ||
| payload('turn_finished', 'active'), | ||
| ); | ||
|
|
||
| expect(projected.goalStatus.kind).toBe('checking'); | ||
| expect(projected.activeGoal).not.toBeNull(); | ||
| expect(projected.goalTerminal).toBeNull(); | ||
| }); | ||
|
|
||
| it('does not repeat an aborted terminal after a paused turn finishes', () => { | ||
| const projected = projectGoalStateToLegacy( | ||
| payload('turn_finished', 'paused'), | ||
| ); | ||
|
|
||
| expect(projected.goalStatus.kind).toBe('checking'); | ||
| expect(projected.activeGoal).toBeNull(); | ||
| expect(projected.goalTerminal).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Qwen Team | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { GoalRecord, GoalStateRecordPayloadV2 } from './goal-protocol.js'; | ||
|
|
||
| export type LegacyGoalStatusKind = | ||
| | 'set' | ||
| | 'achieved' | ||
| | 'cleared' | ||
| | 'failed' | ||
| | 'aborted' | ||
| | 'paused' | ||
| | 'checking'; | ||
|
|
||
| export interface LegacyGoalStatus { | ||
| type: 'goal_status'; | ||
| kind: LegacyGoalStatusKind; | ||
| condition: string; | ||
| iterations?: number; | ||
| setAt?: number; | ||
| durationMs?: number; | ||
| lastReason?: string; | ||
| } | ||
|
|
||
| export interface LegacyActiveGoal { | ||
| readonly condition: string; | ||
| readonly iterations: number; | ||
| readonly setAt: number; | ||
| readonly tokensAtStart?: number; | ||
| readonly hookId?: string; | ||
| readonly lastReason?: string; | ||
| } | ||
|
|
||
| export interface LegacyGoalTerminal { | ||
| kind: 'achieved' | 'failed' | 'aborted'; | ||
| condition: string; | ||
| iterations: number; | ||
| durationMs: number; | ||
| lastReason?: string; | ||
| } | ||
|
|
||
| export interface LegacyGoalProjection { | ||
| activeGoal: LegacyActiveGoal | null; | ||
| goalStatus: LegacyGoalStatus; | ||
| goalTerminal: LegacyGoalTerminal | null; | ||
| } | ||
|
|
||
| export function projectGoalStateToLegacy( | ||
| payload: GoalStateRecordPayloadV2, | ||
| previousGoal: GoalRecord | null = null, | ||
| ): LegacyGoalProjection { | ||
| const snapshotGoal = payload.snapshot.goal; | ||
| const displayGoal = snapshotGoal ?? previousGoal; | ||
| const kind = legacyStatusKind(payload); | ||
| const goalStatus: LegacyGoalStatus = { | ||
| type: 'goal_status', | ||
| kind, | ||
| condition: displayGoal?.objective ?? '', | ||
| ...(displayGoal ? { iterations: displayGoal.turnCount } : {}), | ||
| ...(displayGoal ? { setAt: displayGoal.createdAt } : {}), | ||
| ...(displayGoal ? { durationMs: displayGoal.activeTimeMs } : {}), | ||
| ...(displayGoal?.lastReason === undefined | ||
| ? {} | ||
| : { lastReason: displayGoal.lastReason }), | ||
| }; | ||
| const terminalKind = | ||
| kind === 'achieved' || kind === 'failed' || kind === 'aborted' | ||
| ? kind | ||
| : undefined; | ||
|
|
||
| return { | ||
| activeGoal: | ||
| snapshotGoal?.status === 'active' | ||
| ? { | ||
| condition: snapshotGoal.objective, | ||
| iterations: snapshotGoal.turnCount, | ||
| setAt: snapshotGoal.createdAt, | ||
| ...(snapshotGoal.lastReason === undefined | ||
| ? {} | ||
| : { lastReason: snapshotGoal.lastReason }), | ||
| } | ||
| : null, | ||
| goalStatus, | ||
| goalTerminal: | ||
| terminalKind && displayGoal | ||
| ? { | ||
| kind: terminalKind, | ||
| condition: displayGoal.objective, | ||
| iterations: displayGoal.turnCount, | ||
| durationMs: displayGoal.activeTimeMs, | ||
| ...(displayGoal.lastReason === undefined | ||
| ? {} | ||
| : { lastReason: displayGoal.lastReason }), | ||
| } | ||
| : null, | ||
| }; | ||
| } | ||
|
|
||
| function legacyStatusKind( | ||
| payload: GoalStateRecordPayloadV2, | ||
| ): LegacyGoalStatusKind { | ||
| switch (payload.cause) { | ||
| case 'create': | ||
| case 'replace': | ||
| case 'edit': | ||
| case 'resume': | ||
| case 'migrated': | ||
| return 'set'; | ||
| case 'complete': | ||
| return 'achieved'; | ||
| case 'clear': | ||
| return 'cleared'; | ||
| case 'pause': | ||
| return 'paused'; | ||
| case 'blocked': | ||
| case 'usage_limited': | ||
| return 'aborted'; | ||
| case 'turn_finished': | ||
| case 'verifier_accept': | ||
| case 'verifier_reject': | ||
| return payload.snapshot.goal?.status === 'complete' | ||
| ? 'achieved' | ||
| : payload.snapshot.goal?.status === 'blocked' || | ||
| payload.snapshot.goal?.status === 'usage_limited' | ||
| ? 'aborted' | ||
| : 'checking'; | ||
| default: | ||
| return assertNever(payload.cause); | ||
| } | ||
| } | ||
|
|
||
| function assertNever(value: never): never { | ||
| throw new Error(`Unsupported Goal state cause: ${String(value)}`); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Suggestion] The
turn_finished/verifier_accept/verifier_rejectcase group's inner ternary returns'achieved'forcomplete,'aborted'forblocked/usage_limited, and'checking'otherwise — but only the'checking'branch is exercised by tests (viaturn_finishedwith'active'and'paused'goals). — Failure scenario: if the ternary logic were inverted or a status comparison used the wrong string literal, no test would catch it. A regression in verifier result projection ships undetected.— qwen3.7-max via Qwen Code /review