Selaa lähdekoodia

fix(tui): hide editor context from transcript (#36264)

Kit Langton 1 kuukausi sitten
vanhempi
sitoutus
41d0a9f010
2 muutettua tiedostoa jossa 22 lisäystä ja 19 poistoa
  1. 1 0
      AGENTS.md
  2. 21 19
      packages/tui/src/component/prompt/index.tsx

+ 1 - 0
AGENTS.md

@@ -27,6 +27,7 @@ Never bypass Git hooks. Do not use `--no-verify` or otherwise disable, skip, or
 
 - Keep things in one function unless composable or reusable
 - Do not extract single-use helpers preemptively. Inline the logic at the call site unless the helper is reused, hides a genuinely complex boundary, or has a clear independent name that improves the caller.
+- Before adding complexity for a speculative or vanishingly unlikely race or security edge case, explain the concrete failure mode, likelihood, and complexity cost to the user and get their buy-in. Do not silently expand scope for theoretical robustness.
 - Avoid `try`/`catch` where possible
 - Avoid using the `any` type
 - Use Bun APIs when possible, like `Bun.file()`

+ 21 - 19
packages/tui/src/component/prompt/index.tsx

@@ -1044,22 +1044,7 @@ export function Prompt(props: PromptProps) {
     // Capture mode before it gets reset
     const currentMode = store.mode
     const editorSelection = editorContext()
-    const editorParts =
-      editorSelection && editor.labelState() === "pending"
-        ? [
-            {
-              type: "text" as const,
-              text: formatEditorContext(editorSelection),
-              synthetic: true,
-              metadata: {
-                kind: "editor_context",
-                source: editorSelection.source ?? "editor",
-                filePath: editorSelection.filePath,
-                ranges: editorSelection.ranges,
-              },
-            },
-          ]
-        : []
+    const pendingEditorSelection = editorSelection && editor.labelState() === "pending" ? editorSelection : undefined
 
     if (store.mode === "shell") {
       move.startSubmit()
@@ -1135,10 +1120,27 @@ export function Prompt(props: PromptProps) {
           return false
         }
       }
+      if (pendingEditorSelection) {
+        // Keep editor context hidden while admitting it before the corresponding user prompt.
+        const error = await sdk.api.session
+          .synthetic({
+            sessionID,
+            text: formatEditorContext(pendingEditorSelection),
+            resume: false,
+          })
+          .then(
+            () => undefined,
+            (error) => error,
+          )
+        if (error) {
+          toast.show({ title: "Failed to send editor context", message: errorMessage(error), variant: "error" })
+          return false
+        }
+      }
       const error = await sdk.api.session
         .prompt({
           sessionID,
-          text: [...editorParts.map((part) => part.text), inputText].filter(Boolean).join("\n\n"),
+          text: inputText,
           files: store.prompt.files,
           agents: store.prompt.agents,
         })
@@ -1150,7 +1152,7 @@ export function Prompt(props: PromptProps) {
         toast.show({ title: "Failed to send prompt", message: errorMessage(error), variant: "error" })
         return false
       }
-      if (editorParts.length > 0) editor.markSelectionSent()
+      if (pendingEditorSelection) editor.markSelectionSent()
     }
     history.append({
       ...store.prompt,
@@ -1163,7 +1165,7 @@ export function Prompt(props: PromptProps) {
 
     // temporary hack to make sure the message is sent
     if (!props.sessionID) {
-      if (editorParts.length > 0) editor.preserveSelectionFromNewSession()
+      if (pendingEditorSelection) editor.preserveSelectionFromNewSession()
       setTimeout(() => {
         route.navigate({
           type: "session",