Repository navigation
feat(goal): edit the current goal objective in place - #4568
Conversation
Add IAgentGoalService.editGoalObjective and PATCH /sessions/{id}/goal.
The edit keeps goalId, status, stats and budget, and emits goal.updated.
A running turn continues; its UpdateGoal complete or blocked calls are
ignored so a goal cannot finish before the agent sees the new objective.
|
There was a problem hiding this comment.
🟡 Changes recommended
Missing execution-time objective-edit checks can let queued updates complete or block an edited goal, while stale edit markers can incorrectly veto updates to same-turn replacements.
2 open issues
What changed in this PR
Adds in-place goal objective editing through the server API and Node SDK, preserving goal identity, status, progress, and budget.
Changes:
- Adds a PATCH endpoint and SDK method with validation and snapshot notifications.
- Persists objective edits across replay and guards against stale status updates.
- Adds tests and bilingual API documentation.
| File | Description |
|---|---|
packages/transcript/test/layers.test.ts |
Tests edited objectives in transcript metadata. |
packages/transcript/src/history/foldFacts.ts |
Applies objective edits during history folding. |
packages/node-sdk/test/session-goal.test.ts |
Tests SDK edit argument forwarding. |
packages/node-sdk/src/types.ts |
Adds the objective-edit input type. |
packages/node-sdk/src/session.ts |
Exposes Session.editGoalObjective. |
packages/node-sdk/src/sdk-rpc-client-v2.ts |
Connects SDK edits to the goal service. |
packages/node-sdk/src/rpc.ts |
Adds the RPC edit contract. |
packages/kap-server/test/sessions.test.ts |
Tests route behavior, events, and errors. |
packages/kap-server/test/__snapshots__/apiSurface.snapshot.test.ts.snap |
Registers the new endpoint in the API snapshot. |
packages/kap-server/src/routes/sessions.ts |
Adds the goal PATCH route. |
packages/kap-server/src/protocol/rest-session.ts |
Defines edit request and response schemas. |
packages/agent-core-v2/test/features/goal/injection/goalInjection.test.ts |
Tests next-turn objective reminders. |
packages/agent-core-v2/test/features/goal/goal.test.ts |
Tests edit validation, state preservation, and vetoes. |
packages/agent-core-v2/test/agent/replayBuilder/fold.test.ts |
Tests edits without additional transcript entries. |
packages/agent-core-v2/src/features/goal/types.ts |
Defines the core edit input. |
packages/agent-core-v2/src/features/goal/goalService.ts |
Implements editing and stale-status protection. |
packages/agent-core-v2/src/features/goal/goalOps.ts |
Adds objective text to durable update events. |
packages/agent-core-v2/src/app/sessionLegacy/sessionLegacyService.ts |
Delegates session edits to the main agent. |
packages/agent-core-v2/src/app/sessionLegacy/sessionLegacy.ts |
Extends the session service interface. |
packages/agent-core-v2/src/agent/replayBuilder/fold.ts |
Carries edited objectives through replay state. |
packages/agent-core-v2/docs/wire-manifest.d.ts |
Updates the durable wire contract. |
docs/zh/reference/server-api.md |
Documents the endpoint in Chinese. |
docs/en/reference/server-api.md |
Documents the endpoint in English. |
apps/vis/server/src/lib/context-projector.ts |
Reflects objective edits in visualization state. |
Review effort: Balanced
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 48ef901539
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The before-execute veto runs before pending approvals and hooks settle, so an objective edited during that wait still let a stale complete or blocked call finish the goal. Recheck the turn's edit marker at execution.
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
A marker left by an edit outlived the goal it applied to, so after a same-turn CreateGoal replace the new goal's complete and blocked updates were ignored.
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
commit: |
|
@codex review |
…r-turn set Bump an objective revision on each edit and record it when a turn launches. UpdateGoal complete/blocked is refused at execution time when the turn's recorded revision is stale. The duplicate before-tool veto is removed; the execution-time check also covers edits made while the call awaits approval.
|
@codex review |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 510542de14
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Port of upstream MoonshotAI/kimi-code 0772396 (MoonshotAI#4568). IAgentGoalService.editGoalObjective changes only the objective of the current goal: goalId, status, stats and budget are untouched, and goal.updated notifies clients. A running turn is not interrupted, and the tool cannot finish the goal before the agent sees the new objective: an edit bumps an objective revision, each turn records the revision it started under, and UpdateGoal's complete/blocked path refuses a stale revision at execution time, which also covers an edit made while the call waited for approval. CreateGoal replace clears the recorded revisions, so a same-turn replacement goal stays finishable. Surfaces: PATCH /api/v1/sessions/{session_id}/goal, Session#editGoalObjective with the SDKRpcClientBase/V2 pair, and the vis context projector folding the new goal.update objective field. Local-form adaptations, because this fork's goal state differs from upstream: - GoalState carries updatedAt and the transition tracks a changed flag; an objective edit sets it so the snapshot's updatedAt moves. Upstream's transition has neither. - The fork rejects non-strict request bodies at route definition, so editSessionGoalRequestSchema is .strict(). - The route reuses the local ensureMainAgent helper, which returns the agent handle and localizes the not-found message, instead of upstream's ensureMainAgent + handleOf(AGENT_NOT_FOUND) pair. - The complete-status test dispatches GoalUpdate directly because the fork's markComplete clears the goal and cannot leave one in complete status. Also updates the api-surface snapshot, which was still missing POST /api/v1/workspace/fs:list from MoonshotAI#4511; that batch never ran this gate. Verified: agent-core-v2 goal 126 pass, fold 18, goal injection 25; kap-server goal routes 7 and the api-surface snapshot; node-sdk 305; agent-core-v2, kap-server, apps/kimi-code and vis server typechecks clean; agent-core-v2 bun run build and check:no-comments clean. Reverse checks: short-circuiting the execution-time check reddens the two stale-edit cases, and dropping the transition's objective assignment reddens four cases including this route's. No changeset: the CLI does not expose the edit yet, so users of the published package cannot perceive it.


Requirement or Bug
Server clients can create, pause, resume, or cancel a goal, but cannot change its text.
/goal replaceclears the goal and restarts its progress. This PR adds a true edit: the objective changes while the goal keeps its id, status, stats, and budget. Desktop will consume it in a later change after a submodule bump.Bug Reproduction Steps
N/A
Root Cause
N/A
Code Changes
The edit is a new optional
objectivefield on the durablegoal.updaterecord, so it replays through the same reducers as other goal updates.IAgentGoalService.editGoalObjectivetrims and validates the text (non-empty, at most 4000 characters), dispatches the record, and emitsgoal.updatedwith a fresh snapshot and nochange, the same shape as a budget edit, so clients refresh without showing a transcript entry. kap-server exposes it asPATCH /api/v1/sessions/{session_id}/goal; node-sdk getsSession.editGoalObjective.The route resumes the session and resolves the main agent itself; a missing session or agent returns
40401. The running turn is not cancelled. The reminder only injects on a new turn and already treats a changed objective as a reason for the full text, so the new objective arrives on the next turn.The turn veto uses an in-memory objective revision. Each edit bumps a counter, and each turn records the revision when it launches (or when it adopts a replacement goal mid-turn, if none was recorded).
UpdateGoalcomplete/blockedis refused when the turn's revision differs from the current one;active(resume) is still allowed. The single check isisObjectiveEditedSinceTurnStart, called from the UpdateGoal tool, so it also covers an edit made while UpdateGoal waits for approval. The revision is not persisted and replay is unaffected.complete/blockedfrom a turn that began before the editpauseGoalgoal_updatedreplay recordchangeand renders a transcript card; resume reads goal state from the live agentReading order:
goalService.ts(editGoalObjective,isObjectiveEditedSinceTurnStart),updateGoalTool.ts, thenroutes/sessions.ts. The remaining changes are the SDK pass-through, reducers (fold.ts, viscontext-projector.ts), the regenerated manifest and API snapshot, and docs.Behavior Changes and Affected Users
PATCH /sessions/{id}/goalGoalSnapshot; errors40914no goal,40915complete status,40917empty,40918too long,40001missing field,40401unknown session or agentgoal.updatewire recordobjectiveobjective, written only by an editgoal.updatedafter an editchangechangecomplete/blockedin a turn that began before an editExternal contracts changed:
wire-manifest.d.ts, the API surface snapshot, andSessionin node-sdk; each change is additive. There are no prompt file edits. The veto adds one new tool result string, shown only to a turn overlapping an edit.Coverage: goal service tests (active, paused, blocked, unchanged, error cases, running turn untouched, veto before launch and during approval, replacement goal, next turn), the next-turn reminder, replay fold, and the route (success,
goal.updated, typed error codes). Not verified: a real model turn, or a live client receiving the event over WebSocket; event publication is tested on the agent event bus.No changeset: this is a server and SDK capability that CLI users cannot perceive until a client uses it.
Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.