Repository navigation
fix(native-chat): match glue against each pending send's own boundary #14927
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
Closed
Closed
Changes from all commits
Commits
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
111 changes: 111 additions & 0 deletions
111
src/renderer/src/components/native-chat/native-chat-command-marker.ts
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,111 @@ | ||
| // Locally-recorded slash commands (e.g. `/clear`). They dispatch into the | ||
| // agent's TUI rather than the conversation, so native chat renders its own | ||
| // marker line and applies their transcript boundaries — kept out of the | ||
| // optimistic-send pruning in native-chat-pending.ts, which is a separate rule. | ||
|
|
||
| import type { NativeChatMessage } from '../../../../shared/native-chat-types' | ||
| import { setBoundedScopeCacheEntry } from './native-chat-composer-scope-cache' | ||
|
|
||
| /** A locally-recorded slash command (e.g. `/clear`). Slash commands dispatch to | ||
| * the agent's TUI and are not chat turns, so we surface a small system line as | ||
| * feedback that the command ran rather than echoing a user bubble. */ | ||
| export type NativeChatCommandMarker = { | ||
| id: string | ||
| /** The command as typed, e.g. `/clear`. */ | ||
| command: string | ||
| sentAt: number | ||
| } | ||
|
|
||
| export type NativeChatCommandMarkerScope = { | ||
| paneKey: string | ||
| agent: string | ||
| sessionId: string | null | ||
| } | ||
|
|
||
| const COMMAND_MARKER_LIMIT = 8 | ||
| const commandMarkerCache = new Map<string, NativeChatCommandMarker[]>() | ||
| let commandMarkerCounter = 0 | ||
|
|
||
| function commandMarkerScopeKey(scope: NativeChatCommandMarkerScope): string { | ||
| return `${scope.paneKey}\0${scope.agent}\0${scope.sessionId ?? ''}` | ||
| } | ||
|
|
||
| export function readCommandMarkerCache( | ||
| scope: NativeChatCommandMarkerScope | ||
| ): NativeChatCommandMarker[] { | ||
| return [...(commandMarkerCache.get(commandMarkerScopeKey(scope)) ?? [])] | ||
| } | ||
|
|
||
| export function appendCommandMarkerCache( | ||
| scope: NativeChatCommandMarkerScope, | ||
| command: string, | ||
| sentAt = Date.now() | ||
| ): NativeChatCommandMarker[] { | ||
| commandMarkerCounter += 1 | ||
| const key = commandMarkerScopeKey(scope) | ||
| // Why: native/TUI view switches remount the chat surface, but slash commands | ||
| // are not transcript turns, so their local feedback needs a pane-scoped cache. | ||
| const next = [ | ||
| ...(commandMarkerCache.get(key) ?? []), | ||
| { id: `${sentAt}-${commandMarkerCounter}`, command, sentAt } | ||
| ].slice(-COMMAND_MARKER_LIMIT) | ||
| // Why: the per-key array is capped at 8, but the KEY (paneKey\0agent\0sessionId, | ||
| // sessionId changes on every /clear) is ephemeral and was never evicted, so it | ||
| // grew one entry per (pane, session) for the renderer's whole life. LRU-bound | ||
| // the key count (mirrors the #7566 draft/attachment caches in this folder). | ||
| setBoundedScopeCacheEntry(commandMarkerCache, key, next) | ||
| return [...next] | ||
| } | ||
|
|
||
| export function clearCommandMarkerCacheForTests(): void { | ||
| commandMarkerCache.clear() | ||
| commandMarkerCounter = 0 | ||
| } | ||
|
|
||
| function isClearCommand(command: string): boolean { | ||
| return command.trim().toLowerCase().split(/\s+/)[0] === '/clear' | ||
| } | ||
|
|
||
| function latestClearSentAt(markers: readonly NativeChatCommandMarker[]): number | null { | ||
| let latest: number | null = null | ||
| for (const marker of markers) { | ||
| if (isClearCommand(marker.command) && (latest === null || marker.sentAt > latest)) { | ||
| latest = marker.sentAt | ||
| } | ||
| } | ||
| return latest | ||
| } | ||
|
|
||
| export function applyCommandMarkerBoundaries( | ||
| messages: readonly NativeChatMessage[], | ||
| markers: readonly NativeChatCommandMarker[] | ||
| ): NativeChatMessage[] { | ||
| const clearSentAt = latestClearSentAt(markers) | ||
| if (clearSentAt === null) { | ||
| return messages as NativeChatMessage[] | ||
| } | ||
| // Why: `/clear` mutates the TUI/transcript asynchronously. Hide the current | ||
| // transcript immediately so native chat reflects the command before the agent | ||
| // writes a replacement session or truncates the file. | ||
| return messages.filter((message) => message.timestamp !== null && message.timestamp > clearSentAt) | ||
| } | ||
|
|
||
| /** Render command markers as compact `system` messages. The `system` role draws | ||
| * as a muted aside (not a user bubble); the text avoids the harness noise | ||
| * prefixes so stripNoiseMessages keeps it. */ | ||
| export function commandMarkersAsMessages( | ||
| markers: readonly NativeChatCommandMarker[] | ||
| ): NativeChatMessage[] { | ||
| return markers.map((marker) => ({ | ||
| id: `command:${marker.id}`, | ||
| role: 'system' as const, | ||
| blocks: [{ type: 'text' as const, text: `Ran ${marker.command}` }], | ||
| timestamp: marker.sentAt, | ||
| source: 'scrape' as const | ||
| })) | ||
| } | ||
|
|
||
| /** True when a message id was minted for a slash-command marker. */ | ||
| export function isCommandMarkerId(id: string): boolean { | ||
| return id.startsWith('command:') | ||
| } | ||
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
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Localize the command marker text.
Ran ${marker.command}is new user-visible text. Register and use a renderer locale key for the template instead of rendering English directly.Based on learnings: renderer locale keys use the first 10 SHA-1 characters of
<filePath>:<text>.Source: Learnings