Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: preserve concurrent twin policy changes on remote saves (#9040)

[._.]/ Adam Eivyfd386e833e34
diff --git a/server/routes/digital-twin/settings.js b/server/routes/digital-twin/settings.js
index f197d79dc7..0307a73f87 100644
--- a/server/routes/digital-twin/settings.js
+++ b/server/routes/digital-twin/settings.js
@@ -35,6 +35,11 @@ router.put('/settings', asyncHandler(async (req, res) => {
let authorized = false;
requireHostControl(req, res, () => { authorized = true; });
if (!authorized) return;
+ } else {
+ // A remote whole-form save may resend these unchanged values. Do not
+ // write them back: an operator can change one while updateSettings awaits
+ // its current meta, and this request has no authority to undo that change.
+ for (const key of named) delete data[key];
}
}
const settings = await digitalTwinService.updateSettings(data);
diff --git a/server/routes/hostControlFamiliesPromptStores.test.js b/server/routes/hostControlFamiliesPromptStores.test.js
index 79aa84e371..93c871e135 100644
--- a/server/routes/hostControlFamiliesPromptStores.test.js
+++ b/server/routes/hostControlFamiliesPromptStores.test.js
@@ -90,6 +90,17 @@ describe('prompt store operator authority (#9040)', () => {
}
expect(persisted).toHaveLength(writes.length * 2);
});
+ it('does not overwrite a concurrent operator change when a remote form resends unchanged protected values', async () => {
+ twin.updateSettings.mockImplementationOnce(async data => {
+ // Operator update lands after the route compared the form snapshot.
+ twin.current.includePrivacyContext = true;
+ return Object.assign(twin.current, data);
+ });
+ const response = await call('192.0.2.10', ['put', '/api/digital-twin/settings'],
+ { ...twin.current, maxContextTokens: 5000 });
+ expect(response.status).toBe(200);
+ expect(twin.current).toMatchObject({ includePrivacyContext: true, maxContextTokens: 5000 });
+ });
it('gates changed twin instruction settings, including the alternate active-persona path', async () => {
const original = { ...twin.current };
for (const data of [
diff --git a/server/services/memoryRetriever.js b/server/services/memoryRetriever.js
index fe0742fc48..39c7a49669 100644
--- a/server/services/memoryRetriever.js
+++ b/server/services/memoryRetriever.js
@@ -214,7 +214,13 @@ export function formatForPrompt(memories, maxTokens = DEFAULT_MEMORY_CONFIG.maxC
lines.push('');
}
- return `${UNTRUSTED_REFERENCE_NOTICE}\n\n${fenceBlock('Memory reference data', lines.join('\n'), maxTokens * 4)}`;
+ const label = 'Memory reference data';
+ // Include the notice, delimiters and possible truncation marker in the same
+ // budget used for retrieval; an oversized preference cannot crowd them out.
+ const overhead = UNTRUSTED_REFERENCE_NOTICE.length + 2
+ + fenceBlock(label, 'x', 1).length - 1 + '\n… [truncated]'.length;
+ const block = fenceBlock(label, lines.join('\n'), Math.max(0, maxTokens * 4 - overhead));
+ return block ? `${UNTRUSTED_REFERENCE_NOTICE}\n\n${block}` : '';
}
/**
diff --git a/server/services/memoryRetriever.test.js b/server/services/memoryRetriever.test.js
index cb6ddcbb77..da29734a9c 100644
--- a/server/services/memoryRetriever.test.js
+++ b/server/services/memoryRetriever.test.js
@@ -9,10 +9,19 @@ vi.mock('./memoryEmbeddings.js', () => ({
estimateTokens: text => Math.ceil(text.length / 4),
generateQueryEmbedding: vi.fn(), truncateToTokens: vi.fn(),
}));
+import { getMemory } from './memoryBackend.js';
import { getMemorySection } from './memoryRetriever.js';
import { UNTRUSTED_REFERENCE_NOTICE } from '../lib/promptFencing.js';
describe('memory prompt reference boundary (#9040)', () => {
+ it('keeps the notice and complete fence inside the configured budget for a boundary-sized memory', async () => {
+ vi.mocked(getMemory).mockResolvedValueOnce({ type: 'preference', content: 'x'.repeat(7990), importance: 1 });
+ const text = await getMemorySection({}, { maxTokens: 2000 });
+ expect(text.length).toBeLessThanOrEqual(8000);
+ expect(text).toContain(UNTRUSTED_REFERENCE_NOTICE);
+ expect(text).toContain('[truncated]');
+ expect(text.endsWith('\`\`\`')).toBe(true);
+ });
it('fences stored preferences and neutralizes a forged closing delimiter', async () => {
const text = await getMemorySection({});
expect(text).toContain(UNTRUSTED_REFERENCE_NOTICE);