Skip to content

feat(subagents): cache-aware resident file sub-agents for refactors (closes #529)#660

Closed
merchloubna70-dot wants to merge 4 commits into
Hmbown:mainfrom
merchloubna70-dot:feat/cache-resident-529
Closed

feat(subagents): cache-aware resident file sub-agents for refactors (closes #529)#660
merchloubna70-dot wants to merge 4 commits into
Hmbown:mainfrom
merchloubna70-dot:feat/cache-resident-529

Conversation

@merchloubna70-dot

Copy link
Copy Markdown

Adds resident_file parameter to agent_spawn. Child prompt is prefixed with file contents for byte-stable prefix caching. Ownership table prevents two agents leasing the same file simultaneously. Closes #529


wangfengcsu@qq.com

macworkers added 2 commits May 4, 2026 12:39
All system prompts were English-only, causing DeepSeek V4 to reason
and respond in English even when users wrote in Chinese or other
languages.

Add a Language Mirror section to base.md and base.txt that instructs
the model to detect the user's primary language and use it for both
reasoning (thinking tokens) and the final reply.
base.txt is not referenced via include_str! in prompts.rs.
Only base.md is loaded (BASE_PROMPT). Remove the redundant change
to base.txt as noted by Gemini Code Assist review.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@ILoveScratch2

Copy link
Copy Markdown

谁家PR机器人

@Hmbown

Hmbown commented May 5, 2026

Copy link
Copy Markdown
Owner

Review notes from v0.8.12 integration

Reviewed the cache-aware resident file sub-agents as part of merging into v0.8.12. One finding:

Lease table has no cleanup

The global ownership table (HashMap<PathBuf, String> mapping file → agent_id) prevents two agents from holding a resident lease on the same file simultaneously. The lease is acquired on spawn:

if let Some(owner) = guard.get(file_path) {
    Some(format!("Warning: agent {owner} already holds a resident lease on {file_path}"))
} else {
    guard.insert(file_path.to_string(), agent_id.clone());
    None
}

But the lease is never released. Not on agent completion, not on crash, not on cancellation. The only way to clear a lease is to restart the TUI.

This means: spawn an agent on src/main.rs, it finishes its work, try to spawn another on the same file → you get a conflict warning even though the first agent is long gone. It's a soft failure (the spawn still succeeds, just with a warning in the result), but it defeats the purpose of resident mode for repeated work on the same file.

Recommendation

Hook into the agent lifecycle to release leases on terminal states (completed, failed, cancelled). The sub-agent manager's cleanup path is the right place — when an agent transitions to a terminal status, walk its leases and remove them.

@merchloubna70-dot

Copy link
Copy Markdown
Author

@Hmbown Correct — the lease is never released, which makes resident mode unusable for repeated work on the same file. This is a real bug.

Fix: hook into the agent's terminal state transition to remove the lease. The right place is wherever the sub-agent manager marks an agent as completed/failed/cancelled.

I'll push the fix to the PR branch now. The approach:

  1. Store the resident_file path alongside the agent in the manager
  2. When transitioning to a terminal state (completed/failed/cancelled), acquire the lease table lock and remove the entry

wangfengcsu@qq.com

@Hmbown

Hmbown commented May 5, 2026

Copy link
Copy Markdown
Owner

v0.8.12 fix applied

The lease cleanup leak has been fixed on feat/v0.8.12:

  • Moved RESIDENT_LEASES from block-scoped static to module level so it's accessible from all lifecycle hooks
  • Added release_resident_leases_for(agent_id) function
  • Called at all three terminal transitions:
    • Cancelledcancel_agent path
    • Failedupdate_failed path
    • Completed — before returning SubAgentResult

Previously, leases were acquired on spawn but never released, causing false conflict warnings when respawning on the same file. Now the file is immediately available after an agent finishes.

Commit: 2ee826924

Remaining issue: The lease acquisition inserts "pending" as a placeholder instead of the real agent id. The conflict warning will show "agent pending already holds a lease" rather than the actual agent nickname. This should be a follow-up fix to update the lease with the real id after agent creation.

…Hmbown#660)

After spawning a resident-file sub-agent, the lease table held "pending"
as the owner placeholder. Conflict warnings for the same file on the
next spawn showed "agent pending already holds a lease" instead of the
actual agent nickname. Update the placeholder to the real agent_id
immediately after spawn_background_with_assignment_options succeeds.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@merchloubna70-dot merchloubna70-dot force-pushed the feat/cache-resident-529 branch from 60e4fc9 to 4bad077 Compare May 5, 2026 06:39
@merchloubna70-dot

Copy link
Copy Markdown
Author

Fixed the remaining issue — after spawn_background_with_assignment_options returns the real agent_id, the lease entry is now updated from "pending" to the actual id. Conflict warnings will now show the correct agent nickname.

Hmbown added a commit that referenced this pull request May 5, 2026
47 fmt drifts had accumulated from the squash-merged community PRs on
this branch (#653, #654, #655, #645, #658, #668, #659, #661, #660,
#667, #656). Pure formatting — no behavioural changes — applied via
`cargo fmt --all` to satisfy CI's `cargo fmt --all -- --check` gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Hmbown added a commit that referenced this pull request May 5, 2026
#660)

Resident-file leases were stamped as "pending" at spawn time because the
agent id is only assigned by the manager later. The release function
introduced in 2ee926924 matches by agent id, so it could never find
those entries and leases would persist for the lifetime of the process.

After spawn returns the real agent id, replace any "pending" entry with
it so the existing release-on-terminal-state path actually fires.

Resolves the documented v0.8.12 caveat noted in the CHANGELOG. Closes
the loop with PR #694, which proposed a release-by-file-path API but
did not address the placeholder problem itself.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
kibuniverse pushed a commit to kibuniverse/DeepSeek-TUI that referenced this pull request May 5, 2026
kibuniverse pushed a commit to kibuniverse/DeepSeek-TUI that referenced this pull request May 5, 2026
…own#660)

Moved RESIDENT_LEASES from block-scoped static to module level so the
release function can access it. Added release_resident_leases_for()
called at all three terminal transitions:
- Cancelled (cancel_agent)
- Failed (update_failed)
- Completed (run_subagent_turn result construction)

Previously leases were acquired but never released, causing false
conflict warnings on subsequent spawns targeting the same file.
MMMarcinho pushed a commit to MMMarcinho/DeepSeek-TUI that referenced this pull request May 6, 2026
MMMarcinho pushed a commit to MMMarcinho/DeepSeek-TUI that referenced this pull request May 6, 2026
…own#660)

Moved RESIDENT_LEASES from block-scoped static to module level so the
release function can access it. Added release_resident_leases_for()
called at all three terminal transitions:
- Cancelled (cancel_agent)
- Failed (update_failed)
- Completed (run_subagent_turn result construction)

Previously leases were acquired but never released, causing false
conflict warnings on subsequent spawns targeting the same file.
MMMarcinho pushed a commit to MMMarcinho/DeepSeek-TUI that referenced this pull request May 6, 2026
47 fmt drifts had accumulated from the squash-merged community PRs on
this branch (Hmbown#653, Hmbown#654, Hmbown#655, Hmbown#645, Hmbown#658, Hmbown#668, Hmbown#659, Hmbown#661, Hmbown#660,
Hmbown#667, Hmbown#656). Pure formatting — no behavioural changes — applied via
`cargo fmt --all` to satisfy CI's `cargo fmt --all -- --check` gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
MMMarcinho pushed a commit to MMMarcinho/DeepSeek-TUI that referenced this pull request May 6, 2026
Hmbown#660)

Resident-file leases were stamped as "pending" at spawn time because the
agent id is only assigned by the manager later. The release function
introduced in 2ee926924 matches by agent id, so it could never find
those entries and leases would persist for the lifetime of the process.

After spawn returns the real agent id, replace any "pending" entry with
it so the existing release-on-terminal-state path actually fires.

Resolves the documented v0.8.12 caveat noted in the CHANGELOG. Closes
the loop with PR Hmbown#694, which proposed a release-by-file-path API but
did not address the placeholder problem itself.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Hmbown

Hmbown commented May 23, 2026

Copy link
Copy Markdown
Owner

This PR was opened before the v0.8.41 rebrand and is now stale. Feel free to rebase onto current main and reopen. 鲸鱼兄弟们等你 🐋

@Hmbown Hmbown closed this May 23, 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.

Cache-aware resident file sub-agents for refactors

3 participants