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

address review (codex): normalize overlapping API origins longest-first (#3359)

[._.]/ Adam Eivydfa31e27e18b
diff --git a/server/services/taskPromptDefaults.test.js b/server/services/taskPromptDefaults.test.js
index 3c82876e61..b1db0b68bd 100644
--- a/server/services/taskPromptDefaults.test.js
+++ b/server/services/taskPromptDefaults.test.js
@@ -50,22 +50,30 @@ describe('taskPromptDefaults integrity snapshot', () => {
// PORTOS_HOST, or merely a shell with PORT set, as inside a CoS agent — five
// untouched bodies hashed differently and this suite failed while nothing had
// drifted (issue #3359).
- it('reproduces the snapshot on an install with a non-default API origin', async () => {
+ it.each([
+ // PORTOS_API_URL cleared so the origin is derived from host/port — and so
+ // the expectation can't inherit whatever the ambient environment sets,
+ // which is the very bug under test.
+ [
+ { PORTOS_API_URL: undefined, PORTOS_HOST: 'portos.example.test', PORT: '5558' },
+ 'http://portos.example.test:5558',
+ ],
+ // An origin that is a PREFIX of the legacy literal (port 80). Normalizing
+ // the shorter one first would rewrite `http://localhost:5555` into
+ // `{{PORTOS_API_URL}}:5555`, which the legacy pass can no longer match.
+ [{ PORTOS_API_URL: 'http://localhost' }, 'http://localhost'],
+ [{ PORTOS_API_URL: 'https://portos.example.test:8443' }, 'https://portos.example.test:8443'],
+ ])('reproduces the snapshot on an install whose API origin is %j', async (env, expectedOrigin) => {
vi.resetModules();
- // Cleared so the origin is derived from host/port rather than inherited
- // from whatever the ambient environment sets — otherwise the assertion
- // below depends on the machine running the suite, the very bug under test.
- vi.stubEnv('PORTOS_API_URL', undefined);
- vi.stubEnv('PORTOS_HOST', 'portos.example.test');
- vi.stubEnv('PORT', '5558');
+ Object.entries(env).forEach(([key, value]) => vi.stubEnv(key, value));
const [freshDefaults, { PORTOS_API_URL }] = await Promise.all([
import('./taskPromptDefaults.js'),
import('../lib/ports.js'),
]);
- // Guard the guard: if the stub stopped taking effect this test would pass
+ // Guard the guard: if the stub stopped taking effect this case would pass
// vacuously by re-running the ambient-environment assertions above.
- expect(PORTOS_API_URL).toBe('http://portos.example.test:5558');
+ expect(PORTOS_API_URL).toBe(expectedOrigin);
expect(buildPromptIntegritySnapshot(freshDefaults, PORTOS_API_URL)).toEqual(SNAPSHOT);
});
diff --git a/server/services/taskPromptDefaults/integrityHash.js b/server/services/taskPromptDefaults/integrityHash.js
index 029cb421a2..ba93d888b5 100644
--- a/server/services/taskPromptDefaults/integrityHash.js
+++ b/server/services/taskPromptDefaults/integrityHash.js
@@ -30,12 +30,16 @@ const API_URL_PLACEHOLDER = '{{PORTOS_API_URL}}';
// integrity test failed while nothing had actually drifted (issue #3359).
const LEGACY_API_ORIGIN = 'http://localhost:5555';
-const replaceAll = (value, needle) => (
- needle ? value.split(needle).join(API_URL_PLACEHOLDER) : value
-);
-
+// Longest first: the two origins can overlap — `PORTOS_API_URL=http://localhost`
+// (port 80) is a prefix of the legacy literal, and replacing it first would turn
+// `http://localhost:5555` into `{{PORTOS_API_URL}}:5555`, which the legacy pass
+// can then no longer match. Replacing the longer candidate first leaves only
+// standalone occurrences of the shorter one.
export const normalizePromptForHash = (body, apiUrl = PORTOS_API_URL) => (
- replaceAll(replaceAll(String(body), apiUrl), LEGACY_API_ORIGIN)
+ [apiUrl, LEGACY_API_ORIGIN]
+ .filter(Boolean)
+ .sort((a, b) => b.length - a.length)
+ .reduce((out, origin) => out.split(origin).join(API_URL_PLACEHOLDER), String(body))
);
export const hashPromptBody = (body, apiUrl = PORTOS_API_URL) => createHash('md5')