From 1279006fe0b5acf9263bbdef00fa9fbef41e3729 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 28 Nov 2026 21:09:24 -0500 Subject: [PATCH] fix(shiso): preserve global conversation during recipe selection --- src/App.tsx | 9 +- .../shisoAgentFoundationAppOnDisk.test.tsx | 205 ++++++++++++++++++ 2 files changed, 212 insertions(+), 2 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 30b1bdd..83475a4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1021,9 +1021,14 @@ export default function App({ // context restores the same transcript instead of silently losing it. const shisoConversationContextKey = shiso.source === 'create' ? 'create' - : recipeIdentityOf(selectedRecipe) === '' + : shiso.source === 'global' + // A global conversation stays global when select_vault_recipe binds a + // recipe inside the same turn. The selection is an operation in that + // conversation, not a conversation-identity change. ? 'global' - : `recipe:${recipeIdentityOf(selectedRecipe)}`; + : recipeIdentityOf(selectedRecipe) === '' + ? 'global' + : `recipe:${recipeIdentityOf(selectedRecipe)}`; const activeOverrideMatchesContext = activeConversationKeyOverride !== null && conversationKeyBelongsToContext(activeConversationKeyOverride, shisoConversationContextKey); const activeConversationKeyForContext = activeOverrideMatchesContext ? activeConversationKeyOverride : null; diff --git a/tests/integration/shisoAgentFoundationAppOnDisk.test.tsx b/tests/integration/shisoAgentFoundationAppOnDisk.test.tsx index 4947ad3..c4434a0 100644 --- a/tests/integration/shisoAgentFoundationAppOnDisk.test.tsx +++ b/tests/integration/shisoAgentFoundationAppOnDisk.test.tsx @@ -23,6 +23,7 @@ import type { RuntimeBundleResult } from '../../src/core/nutritionV2/runtime'; import { USDA_ATTRIBUTION } from '../../src/core/nutritionV2/usda/types'; import { NUTRITION_PRODUCT_ACCESS_VERSION } from '../../src/core/nutritionV2/nutritionProductAccess'; import { buildMatchingBundle } from '../fixtures/usdaMatchingFixtures'; +import { loadShisoConversation } from '../../src/application/shisoConversationPersistence'; const nutritionRecipeMarkdown = `--- title: "Nutrition Fixture" @@ -174,6 +175,130 @@ function capturingNetwork( }; } +function namedGlobalSelectionNetwork(): { + readonly network: NetworkAdapter; + readonly turns: Array>; +} { + const turns: Array> = []; + let step = 0; + let selectedRecipeId = ''; + const respond = async (path: string, body?: unknown) => { + if (path !== '/api/shiso/turn') { + return { + status: 200, + ok: true, + data: path === '/api/nutrition/product-access' + ? { ok: true, version: NUTRITION_PRODUCT_ACCESS_VERSION, tier: 'ai_advanced' } + : path === '/api/providers' + ? { providers: [] } + : { ok: true, access: 'entitled', available: true }, + }; + } + const request = (body ?? {}) as Record; + turns.push(request); + const observation = request['observation'] as Record | undefined; + if (step === 0) { + step += 1; + return { + status: 200, + ok: true, + data: { + ok: true, + kind: 'tool_request', + tool: 'search_vault_recipes', + args: { tool: 'search_vault_recipes', query: 'Braised', field: 'title', limit: 10 }, + model_invocations_used: 1, + tool_calls_used: 1, + }, + }; + } + if (step === 1) { + const results = ((observation?.['data'] as Record | undefined)?.['results'] ?? []) as Array>; + const selected = results.find((entry) => entry['title'] === 'Braised Beef Short Ribs'); + selectedRecipeId = typeof selected?.['id'] === 'string' ? selected['id'] : ''; + if (selectedRecipeId.length === 0) throw new Error('the selected recipe was not found in the search observation'); + step += 1; + return { + status: 200, + ok: true, + data: { + ok: true, + kind: 'tool_request', + tool: 'select_vault_recipe', + args: { tool: 'select_vault_recipe', recipe_id: selectedRecipeId }, + model_invocations_used: 2, + tool_calls_used: 2, + }, + }; + } + if (step === 2) { + expect(observation?.['tool']).toBe('select_vault_recipe'); + expect(observation?.['ok']).toBe(true); + expect(String(observation?.['summary'] ?? '')).toContain('Braised Beef Short Ribs'); + step += 1; + return { + status: 200, + ok: true, + data: { + ok: true, + kind: 'tool_request', + tool: 'get_vault_recipe', + args: { tool: 'get_vault_recipe', recipe_id: selectedRecipeId }, + model_invocations_used: 3, + tool_calls_used: 3, + }, + }; + } + if (step === 3) { + expect(observation?.['tool']).toBe('get_vault_recipe'); + const ingredients = ((observation?.['data'] as Record | undefined)?.['ingredients'] ?? []) as Array>; + const oil = ingredients.find((entry) => String(entry['text'] ?? '').includes('olive oil')); + const lineRef = typeof oil?.['line_ref'] === 'string' ? oil['line_ref'] : ''; + if (lineRef.length === 0) throw new Error('the olive oil ingredient was not present in the selected recipe observation'); + step += 1; + return { + status: 200, + ok: true, + data: { + ok: true, + kind: 'tool_request', + tool: 'apply_ingredient_edits', + args: { + tool: 'apply_ingredient_edits', + edits: [{ line_ref: lineRef, new_amount: 3 }], + }, + model_invocations_used: 4, + tool_calls_used: 4, + }, + }; + } + expect(observation?.['tool']).toBe('apply_ingredient_edits'); + expect(observation?.['ok']).toBe(true); + expect((observation?.['data'] as Record | undefined)?.['applied_count']).toBe(1); + step += 1; + return { + status: 200, + ok: true, + data: { + ok: true, + kind: 'reply', + text: 'The selected recipe’s unsaved working copy now uses 3 tbsp olive oil. Nothing was saved to the Vault.', + proposals: [], + model_invocations_used: 5, + tool_calls_used: 4, + }, + }; + }; + return { + network: { + post: respond as NetworkAdapter['post'], + get: respond as NetworkAdapter['get'], + request: async (request) => respond(request.path, request.body) as never, + }, + turns, + }; +} + function OpenGlobal() { const { open } = useShiso(); useEffect(() => open('global'), [open]); @@ -968,4 +1093,84 @@ describe('SHISO agent foundation real App save closure', () => { expect(await readFile(sidePath, 'utf8')).toBe(sideMarkdown); }, 120000); + // Regression: selecting a recipe inside a named global conversation must not + // switch the conversation key to unscoped "global" and abort the turn. + it('REGRESSION: keeps a named global conversation active when select_vault_recipe binds the recipe', async () => { + await hydrateAiSelections(settings); + const rootPath = await mkdtemp(join('/tmp', 'tkc-named-global-select-')); + roots.push(rootPath); + const recipePath = join(rootPath, 'Recipes', 'Main', 'Braised Beef Short Ribs.md'); + await mkdir(dirname(recipePath), { recursive: true }); + const markdown = [ + '---', 'title: "Braised Beef Short Ribs"', 'tags:', ' - food/recipes', ' - main', + 'category: Main Course', 'cuisine: French', 'servings: 4', + 'prepTime: 20 mins', 'cookTime: 150 mins', 'totalTime: 170 mins', '---', + '# Braised Beef Short Ribs', '## Ingredients', + '- 4 beef short ribs (about 3 lb)', '- 2 tbsp olive oil', '- 1 onion, diced', + '- 2 carrots, diced', '- 2 cups red wine', '- 2 cups beef stock', '- 2 sprigs thyme', + '## Instructions', + '1. Season the ribs and brown them in olive oil.', + '2. Sauté the onion and carrots until softened.', + '3. Add wine and reduce by half.', + '4. Add stock and thyme, cover, and braise at 325°F for 2 1/2 hours.', + '5. Skim the fat and serve the ribs with the braising liquid.', '', + ].join('\n'); + await writeFile(recipePath, markdown, 'utf8'); + const root = new DiskDirectoryHandle('Disposable Named Global Vault', rootPath); + (window as unknown as { showDirectoryPicker: () => Promise }).showDirectoryPicker = async () => root; + + const captured = namedGlobalSelectionNetwork(); + render( + + + , + ); + + fireEvent.click(await screen.findByRole('button', { name: 'Connect Vault' }, { timeout: 15000 })); + fireEvent.click(await screen.findByRole('tab', { name: 'Live Directory Sync' }, { timeout: 15000 })); + fireEvent.click(await screen.findByRole('button', { name: 'Choose Local Obsidian Vault Folder' }, { timeout: 15000 })); + await screen.findByText(/Connected live to vault "Disposable Named Global Vault"/, {}, { timeout: 15000 }); + fireEvent.click(screen.getByRole('button', { name: 'Close Vault settings' })); + fireEvent.click(await screen.findByRole('button', { name: 'Chat with SHISO' }, { timeout: 15000 })); + + fireEvent.click(await screen.findByTestId('shiso-history-new', {}, { timeout: 15000 })); + await waitFor(() => expect(settingsValues.get('tkc_shiso_active_conversation_v1')).toMatch(/^global:/)); + const activeKey = settingsValues.get('tkc_shiso_active_conversation_v1') as string; + await waitFor(() => expect(screen.queryByTestId('shiso-conversation-restoring')).toBeNull()); + + const composer = await screen.findByPlaceholderText('Ask Shiso…', {}, { timeout: 15000 }) as HTMLTextAreaElement; + fireEvent.change(composer, { + target: { value: 'Search my Vault for Braised Beef Short Ribs, select it, read it, and change the olive oil to 3 tbsp in the unsaved working copy. Do not save.' }, + }); + fireEvent.click(screen.getByRole('button', { name: 'Send' })); + await waitFor( + () => expect(document.body.textContent).toContain('unsaved working copy now uses 3 tbsp olive oil'), + { timeout: 20000 }, + ); + expect(document.body.textContent).toContain('Search my Vault for Braised Beef Short Ribs'); + + expect(captured.turns).toHaveLength(5); + expect( + captured.turns + .map((turn) => (turn['observation'] as Record | undefined)?.['tool']) + .filter(Boolean), + ).toEqual(['search_vault_recipes', 'select_vault_recipe', 'get_vault_recipe', 'apply_ingredient_edits']); + expect(captured.turns[2]?.['observation']).toMatchObject({ tool: 'select_vault_recipe', ok: true }); + expect(captured.turns[3]?.['observation']).toMatchObject({ tool: 'get_vault_recipe', ok: true }); + expect(captured.turns[4]?.['observation']).toMatchObject({ tool: 'apply_ingredient_edits', ok: true }); + expect(JSON.stringify(captured.turns[4]?.['observation'])).toContain('3 tbsp olive oil'); + expect(settingsValues.get('tkc_shiso_active_conversation_v1')).toBe(activeKey); + + const stored = await loadShisoConversation(settings, activeKey); + expect(stored?.messages.length).toBeGreaterThanOrEqual(2); + expect(stored?.entries.some((entry) => entry.role === 'assistant')).toBe(true); + + const working = await screen.findByTestId('shiso-working-recipe', {}, { timeout: 15000 }); + expect(working.textContent).toContain('1 change'); + expect(document.body.textContent).toContain('3 tbsp olive oil'); + expect(await readFile(recipePath, 'utf8')).toBe(markdown); + const persisted = await loadVaultContent(createBrowserVaultAdapter(root)); + expect(persisted.recipes.find((recipe) => recipe.title === 'Braised Beef Short Ribs')?.codexNutrition).toBeUndefined(); + }, 90000); + });