Skip to content

feat(goal): edit the current goal objective in place - #4568

Merged
chengluyu merged 10 commits into
mainfrom
foreman/2026-wk45-goal-edit
Nov 3, 2026
Merged

chengluyu merged 10 commits into
mainfrom
foreman/2026-wk45-goal-edit

Conversation

@chengluyu

@chengluyu chengluyu commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Server clients can create, pause, resume, or cancel a goal, but cannot change its text. /goal replace clears 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 objective field on the durable goal.update record, so it replays through the same reducers as other goal updates. IAgentGoalService.editGoalObjective trims and validates the text (non-empty, at most 4000 characters), dispatches the record, and emits goal.updated with a fresh snapshot and no change, the same shape as a budget edit, so clients refresh without showing a transcript entry. kap-server exposes it as PATCH /api/v1/sessions/{session_id}/goal; node-sdk gets Session.editGoalObjective.

PATCH /sessions/{id}/goal  { objective }
  -> resume session, resolve main agent
  -> IAgentGoalService.editGoalObjective
       -> goal.update { objective }   durable, replayed on resume
       -> objective revision += 1     in memory
       -> goal.updated { snapshot }   existing client refresh event
next new turn
  -> goal reminder sees a changed objective and sends the full reminder

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). UpdateGoal complete / blocked is refused when the turn's revision differs from the current one; active (resume) is still allowed. The single check is isObjectiveEditedSinceTurnStart, 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.

Decision Choice Not chosen Reason
UpdateGoal complete / blocked from a turn that began before the edit Refuse with a tool result telling the model to end the turn Allow it The model has not seen the new objective, and allowing it could finish the edited goal without the agent ever receiving it
Unchanged text after trimming Return the snapshot with no record or event Write a no-op record Matches the idempotent pauseGoal
Replay Update fold state, no new goal_updated replay record Add a replay record That record requires a change and renders a transcript card; resume reads goal state from the live agent

Reading order: goalService.ts (editGoalObjective, isObjectiveEditedSinceTurnStart), updateGoalTool.ts, then routes/sessions.ts. The remaining changes are the SDK pass-through, reducers (fold.ts, vis context-projector.ts), the regenerated manifest and API snapshot, and docs.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
PATCH /sessions/{id}/goal Route absent Edits the objective and returns the GoalSnapshot; errors 40914 no goal, 40915 complete status, 40917 empty, 40918 too long, 40001 missing field, 40401 unknown session or agent None, new route n/a
goal.update wire record No objective Optional objective, written only by an edit Sessions from older releases have no such field and replay unchanged. An older release resuming a session that was edited strips the field and shows the old objective none
goal.updated after an edit Never emitted for text changes Emitted with the new snapshot and no change TUI, desktop, web, and the transcript map already handle a snapshot with no change n/a
UpdateGoal complete / blocked in a turn that began before an edit Not reachable Ignored with a tool result; the goal stays as it was and the next turn gets the new objective Only sessions that use the new edit n/a

External contracts changed: wire-manifest.d.ts, the API surface snapshot, and Session in 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

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

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.
Copilot AI balanced review requested due to automatic review settings October 31, 2026 06:51
@changeset-bot

changeset-bot Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 6cef9e0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T05:54:31.848815Z 6cef9e0 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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.
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: d8bd48eda6

ℹ️ 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".

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.
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 3c0c714d91

ℹ️ 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".

@chengluyu
chengluyu marked this pull request as ready for review October 31, 2026 07:44
@pkg-pr-new

pkg-pr-new Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@6cef9e0
npx https://pkg.pr.new/@moonshot-ai/kimi-code@6cef9e0

commit: 6cef9e0

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@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.
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: b3630b430a

ℹ️ 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".

wbxl2000
wbxl2000 previously approved these changes Nov 3, 2026
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 6cef9e0bcd

ℹ️ 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".

@chengluyu
chengluyu merged commit 0772396 into main Nov 3, 2026
17 checks passed
@chengluyu
chengluyu deleted the foreman/2026-wk45-goal-edit branch November 3, 2026 06:14
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
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.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants