Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
488 changes: 468 additions & 20 deletions src/App.tsx

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/application/adapters/VaultAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export interface VaultAdapter {
readText(path: string): Promise<string>;
/** Writes raw Markdown text to a vault file, creating or overwriting it. */
writeText(path: string, content: string): Promise<void>;
/**
* Optional guarded write for an existing file. `beforeCommit` runs at each
* application-controlled reversible checkpoint (before a staging stream is
* opened and immediately before the platform commit). If it throws, no
* commit is performed and staged resources are aborted where supported.
* Callers that require a final authority check must fail closed when this
* method is unavailable.
*/
writeTextGuarded?(path: string, content: string, beforeCommit: () => void): Promise<void>;
/**
* Optional conditional write. Commits `content` only when the current file
* content exactly equals `expected`. Implementations that cannot provide a
Expand Down
22 changes: 21 additions & 1 deletion src/application/vaultRecipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,39 @@ export async function scanVaultMarkdown(vault: VaultAdapter): Promise<VaultScanR
return { recipes, notes, mealPlan, shoppingList };
}

/**
* A guarded recipe write was requested but the selected destination does not
* implement the guarded commit contract. This is a bounded fail-closed error;
* ordinary `writeText` is never used as a substitute.
*/
export class GuardedVaultWriteUnavailableError extends Error {
constructor() {
super('This Vault destination does not support guarded recipe writes. Nothing was saved.');
this.name = 'GuardedVaultWriteUnavailableError';
}
}

/**
* Writes a recipe back to the vault through the adapter. The logical location is
* `resolveRecipeVaultPath(recipe)` (vault-relative `filePath`), so a nested
* recipe always writes back to its nested folder — never a root basename.
*/
export async function saveRecipeWithVaultAdapter(
vault: VaultAdapter,
recipe: ObsidianRecipe
recipe: ObsidianRecipe,
options?: { readonly beforeCommit?: () => void },
): Promise<void> {
const path = resolveRecipeVaultPath(recipe);
// Preserve owner-authored body content; only supported edited sections and
// managed frontmatter fields are updated.
const markdown = serializeRecipePreservingContent(recipe);
if (options?.beforeCommit !== undefined) {
if (typeof vault.writeTextGuarded !== 'function') {
throw new GuardedVaultWriteUnavailableError();
}
await vault.writeTextGuarded(path, markdown, options.beforeCommit);
return;
}
await vault.writeText(path, markdown);
}

Expand Down
11 changes: 9 additions & 2 deletions src/components/AdvancedNutritionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2820,9 +2820,16 @@ onReviewRecipeContextWithAi,
if (standingTransactionSeq.current !== seq) return;
const message = saved.ok && saved.saved
? `SHISO applied the verified nutrition review and saved it to the active Vault. ${saved.message}`
: `SHISO applied the verified nutrition review, but it was not saved: ${saved.message}`;
: saved.reason_code === 'mixed_working_copy'
? 'Nutrition updated. Other recipe changes are pending. Review and save your recipe.'
: saved.reason_code === 'stale_nutrition_operation'
? saved.message
: `SHISO applied the verified nutrition review, but it was not saved: ${saved.message}`;
setShisoNutritionState({ kind: saved.ok && saved.saved ? 'success' : 'error', message });
setGenerateAiFeedback({ kind: saved.ok && saved.saved ? 'success' : 'error', message });
setGenerateAiFeedback({
kind: saved.ok && saved.saved ? 'success' : saved.reason_code === 'mixed_working_copy' ? 'partial' : 'error',
message,
});
})().catch(() => {
if (standingTransactionSeq.current !== seq) return;
const message = 'SHISO applied no persistent nutrition change because the authorized save failed. Nothing was saved.';
Expand Down
4 changes: 2 additions & 2 deletions src/components/ShisoModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2465,7 +2465,7 @@ export function ShisoModal({
))}
</ul>
<p className="text-[10px] text-gray-500">
Your saved recipe is untouched until you save. The nutrition review recalculates from this working copy.
Saving writes the complete working recipe, including every change listed above. Your saved recipe is untouched until then.
</p>
<div className="flex flex-wrap items-center gap-2">
<button
Expand All @@ -2475,7 +2475,7 @@ export function ShisoModal({
onClick={() => void saveWorking()}
className="text-[11px] px-2 py-1 rounded-lg bg-teal-500/20 text-teal-200 border border-teal-500/40 hover:bg-teal-500/30 disabled:opacity-40"
>
{saveBusy ? 'Saving…' : 'Save to vault'}
{saveBusy ? 'Saving…' : 'Save all recipe changes'}
</button>
<button
type="button"
Expand Down
14 changes: 14 additions & 0 deletions src/core/shiso/nutritionAuthority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { canonicalStringify, sha256Hex } from '../nutritionV2/usda/digest';

export const SHISO_NUTRITION_AUTHORITY_SETTING = 'shiso_nutrition_authority_v1';

/**
* The one pure SHA-256 bridge from SHISO authority code to the audited Phase 1
* digest helper. Keeping it here prevents other SHISO modules from importing
* the USDA namespace directly.
*/
export function nutritionAuthorityTextDigest(text: string): string {
return sha256Hex(text);
}

/** The audited canonical serializer, exposed only to SHISO authority modules. */
export function nutritionAuthorityCanonicalStringify(value: unknown): string {
return canonicalStringify(value);
}

export interface ShisoNutritionAuthorityRecord {
readonly version: 1;
readonly enabled: true;
Expand Down
Loading