Skip to content

fix(go-agent): persist canvas session state - #16970

Closed
Harry-Hz-Zhang wants to merge 7 commits into
infiniflow:mainfrom
Harry-Hz-Zhang:fix/agent-list-sys-history
Closed

Harry-Hz-Zhang wants to merge 7 commits into
infiniflow:mainfrom
Harry-Hz-Zhang:fix/agent-list-sys-history

Conversation

@Harry-Hz-Zhang

@Harry-Hz-Zhang Harry-Hz-Zhang commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Persist Canvas history, memory, and sys.history across agent session runs.
  • Align Go multi-turn agent behavior with the Python Canvas runtime.
  • Add regression coverage for history, tool memory, and interrupted runs.

Testing

  • bash build.sh --test ./internal/agent/runtime ./internal/agent/canvas ./internal/agent/component ./internal/service
image

@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2f469a93-9ae8-4eaf-a03b-39adcbd74bb4

📥 Commits

Reviewing files that changed from the base of the PR and between b1482f0 and 8e6676b.

📒 Files selected for processing (2)
  • internal/agent/component/agent.go
  • internal/service/document_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/agent/component/agent.go
  • internal/service/document_test.go

📝 Walkthrough

Walkthrough

The change adds DSL history and memory support, separates tool-call memory from conversation history, synchronizes runtime state across workflows, persists session history and partial outputs, and standardizes JSON-style display rendering.

Changes

History and memory flow

Layer / File(s) Summary
DSL history and memory contracts
internal/agent/canvas/*
Canvas decoding accepts multiple history and memory shapes, while encoders restore Python DSL list forms and skip malformed entries.
Runtime state namespaces
internal/agent/runtime/state.go, internal/agent/runtime/state_test.go
CanvasState stores, serializes, snapshots, deep-copies, and separately manages history, memory, and system history.
Workflow state synchronization
internal/agent/canvas/scheduler.go, internal/agent/component/*
Workflow state synchronization and prompt construction use prior history, while tool-call summaries are written to memory.
Agent session persistence
internal/service/agent.go, internal/service/agent_run_e2e_test.go, internal/service/document_test.go
Agent runs load or create sessions, seed state, derive terminal assistant output, enforce session ownership, and persist encoded history and memory across completion and interruption paths.
Display output serialization
internal/agent/runtime/template.go, internal/agent/component/message.go, related tests
Resolved non-string values render as JSON-like text, and message outputs always include a non-nil downloads field.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AgentService
  participant ConversationSession
  participant CanvasState
  participant Workflow
  AgentService->>ConversationSession: load session DSL
  AgentService->>CanvasState: seed history and memory
  AgentService->>Workflow: invoke workflow
  Workflow->>CanvasState: update history and memory
  AgentService->>ConversationSession: persist encoded state
Loading

Possibly related PRs

Suggested labels: 💞 feature

Suggested reviewers: yuzhichang

Poem

I’m a rabbit with history tucked neat,
Memory hops where tool calls meet.
JSON carrots line up in a row,
Sessions remember each turn as they grow.
Hop, hop, review complete!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.74% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: persisting canvas session state across runs.
Description check ✅ Passed The description includes a clear Summary and Testing section, which is sufficient for this repository template.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Harry-Hz-Zhang Harry-Hz-Zhang added ci Continue Integration agent labels Jul 16, 2026
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 03:28

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/agent/canvas/scheduler.go (1)

154-190: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Replace length-based state conflict resolution.

A longer slice is not necessarily newer, and equal-length divergent states always discard the local version. Checkpoint restoration or parallel updates can therefore overwrite valid history, memory, or sys.history. Establish one authoritative direction per lifecycle phase or merge append-only entries using a verified common prefix/version.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/agent/canvas/scheduler.go` around lines 154 - 190, Replace the
length-based conflict resolution in the state synchronization block with
lifecycle-aware authoritative selection or append-only merging based on a
verified common prefix/version. Apply the same strategy consistently to history,
memory, and sys.history, preserving valid local and context updates instead of
selecting solely by slice length or discarding divergent equal-length states.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/agent/runtime/state.go`:
- Around line 461-474: Update cloneMapSlice to recursively deep-copy each map
value, including nested map[string]any and []any values, while preserving scalar
values and nils. Ensure payload and other nested JSON-compatible structures no
longer share mutable references with the original state.
- Around line 353-366: Update CanvasState.SnapshotPriorHistory so it excludes
the current in-flight turn only when its identity matches the active
prompt/turn, not merely when the latest history entry has role "user". Pass the
current prompt/turn identity or track it separately, while preserving persisted
user turns whose content or identity differs.

In `@internal/service/agent.go`:
- Around line 799-805: Update the session-loading logic in RunAgent around
api4ConversationDAO.GetBySessionID to enforce ownership by comparing the loaded
session.UserID with the authenticated userID, or by using a user-scoped DAO
query. Treat mismatches as not found and do not load or overwrite the other
user’s session data.
- Around line 1319-1334: The persistence logic around the state-handling block
must retain the complete DSL used for the run instead of rebuilding it from an
empty session DSL. Pass that run-time DSL into the relevant function, copy it as
the base, then overlay globals, history, and memory; preserve existing globals
while updating sys.history. Add a regression test covering an API4Conversation
with nil DSL and verify the next run decodes successfully.
- Around line 1167-1169: Update the interrupt handling branch in the run flow
around markRunFailed and persistAgentRunSession to call appendAssistantHistory
with the partial answer before persisting the interrupted session. Ensure this
occurs when answer is non-empty so the assistant response is included in
persisted DSL history and retained for the next run.

---

Outside diff comments:
In `@internal/agent/canvas/scheduler.go`:
- Around line 154-190: Replace the length-based conflict resolution in the state
synchronization block with lifecycle-aware authoritative selection or
append-only merging based on a verified common prefix/version. Apply the same
strategy consistently to history, memory, and sys.history, preserving valid
local and context updates instead of selecting solely by slice length or
discarding divergent equal-length states.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6adcbde0-c470-4d3c-b96b-3caaa7ddc6d0

📥 Commits

Reviewing files that changed from the base of the PR and between 8f87e19 and d5ecbbb.

📒 Files selected for processing (14)
  • internal/agent/canvas/canvas.go
  • internal/agent/canvas/decode.go
  • internal/agent/canvas/decode_history_test.go
  • internal/agent/canvas/scheduler.go
  • internal/agent/component/agent.go
  • internal/agent/component/history_window_test.go
  • internal/agent/component/llm.go
  • internal/agent/component/tool_call_memory_test.go
  • internal/agent/runtime/state.go
  • internal/agent/runtime/state_test.go
  • internal/service/agent.go
  • internal/service/agent_run_e2e_test.go
  • internal/service/agent_test.go
  • internal/service/document_test.go

Comment thread internal/agent/runtime/state.go Resolved
Comment thread internal/agent/runtime/state.go Resolved
Comment thread internal/service/agent.go Resolved
Comment thread internal/service/agent.go Resolved
Comment thread internal/service/agent.go Resolved
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 06:09

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
internal/service/agent_run_e2e_test.go (1)

1244-1245: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Describe the Go rendering contract directly.

Avoid coupling this regression test’s rationale to the Python implementation.

Proposed update
-	// Python Message._stringify_message_value renders non-string template
-	// values as JSON, so list references keep commas and quotes.
+	// Non-string template values use JSON rendering, so list references
+	// retain commas and quotes.

As per coding guidelines, “keep package and documentation comments aligned with the current runtime path rather than migration history” and “do not add compatibility wording to comments or documentation.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/service/agent_run_e2e_test.go` around lines 1244 - 1245, Update the
comment in the regression test to describe the Go renderer’s contract directly:
non-string template values are rendered as JSON, preserving commas and quotes in
list references. Remove the reference to Python’s
Message._stringify_message_value and any migration or compatibility wording.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/agent/component/message_test.go`:
- Around line 50-53: Update the downloads assertion in the test around the
out["downloads"] type check to explicitly verify that the []DownloadInfo slice
is non-nil, while retaining the existing empty-length assertion.

In `@internal/agent/runtime/template.go`:
- Around line 140-143: Remove migration and Python compatibility references from
the comments at internal/agent/runtime/template.go:140-143 and
internal/agent/component/message.go:227-229. In stringifyDisplayValue, describe
the current Go stringification behavior directly; in the message comment,
explain retaining the key using the current architecture, with no direct code
changes required.

---

Nitpick comments:
In `@internal/service/agent_run_e2e_test.go`:
- Around line 1244-1245: Update the comment in the regression test to describe
the Go renderer’s contract directly: non-string template values are rendered as
JSON, preserving commas and quotes in list references. Remove the reference to
Python’s Message._stringify_message_value and any migration or compatibility
wording.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8875de37-e2da-4c52-a463-f81ad42b6c3a

📥 Commits

Reviewing files that changed from the base of the PR and between d5ecbbb and b1482f0.

📒 Files selected for processing (8)
  • internal/agent/component/history_window_test.go
  • internal/agent/component/message.go
  • internal/agent/component/message_test.go
  • internal/agent/runtime/state.go
  • internal/agent/runtime/state_test.go
  • internal/agent/runtime/template.go
  • internal/service/agent.go
  • internal/service/agent_run_e2e_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/agent/runtime/state.go
  • internal/service/agent.go

Comment thread internal/agent/runtime/template.go Resolved
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 06:31
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 06:31
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 06:40
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 06:40
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 07:35
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 07:36
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 08:12
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 08:12
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 08:43
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 08:43
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 10:00
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 10:00
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 11:02
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 11:02
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 11:03
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 11:03
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as draft July 16, 2026 11:04
@Harry-Hz-Zhang
Harry-Hz-Zhang marked this pull request as ready for review July 16, 2026 11:04
@Harry-Hz-Zhang Harry-Hz-Zhang added ci Continue Integration and removed ci Continue Integration labels Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent 🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant