Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix(cli): replace the whole allowed_origins value in the engine config (#1509)

Dmitrii Zhukov9bd73e20c55d
diff --git a/src/cli/engine-config.ts b/src/cli/engine-config.ts
index 37f1f7ac..1831261b 100644
--- a/src/cli/engine-config.ts
+++ b/src/cli/engine-config.ts
@@ -282,6 +282,25 @@ function replaceKvAdapterWithRedis(
);
}
+function indentWidth(line: string): number {
+ return line.length - line.trimStart().length;
+}
+
+function mappingValueEnd(lines: string[], keyIndex: number, limit: number): number {
+ const keyIndent = indentWidth(lines[keyIndex]!);
+ let end = keyIndex + 1;
+ for (let i = keyIndex + 1; i < limit; i++) {
+ const text = lines[i]!.trim();
+ if (text === "" || text.startsWith("#")) continue;
+ const indent = indentWidth(lines[i]!);
+ const continuesValue =
+ indent > keyIndent || (indent === keyIndent && /^(-(\s|$)|[\]}])/.test(text));
+ if (!continuesValue) break;
+ end = i + 1;
+ }
+ return end;
+}
+
function setManagedCorsOrigins(
lines: string[],
restPort: number,
@@ -297,6 +316,8 @@ function setManagedCorsOrigins(
);
if (originsIndex === -1) return;
const indent = lines[originsIndex]!.match(/^\s*/)?.[0] ?? " ";
+ const valueEnd = mappingValueEnd(lines, originsIndex, block.end);
+ lines.splice(originsIndex + 1, valueEnd - originsIndex - 1);
lines[originsIndex] =
`${indent}allowed_origins: [` +
`"http://localhost:${restPort}", ` +
diff --git a/test/engine-config.test.ts b/test/engine-config.test.ts
index 6f0213d4..c8cf20a0 100644
--- a/test/engine-config.test.ts
+++ b/test/engine-config.test.ts
@@ -241,6 +241,93 @@ describe("renderEngineConfig bind hosts", () => {
});
});
+describe("renderEngineConfig CORS origins", () => {
+ const ports = { restPort: 3211, streamPort: 3212, viewerPort: 3213, enginePort: 49234 };
+ const managedOrigins =
+ ' allowed_origins: ["http://localhost:3211", "http://localhost:3213", "http://127.0.0.1:3211", "http://127.0.0.1:3213"]';
+ const httpWorker = (corsLines: string[]) =>
+ [
+ "workers:",
+ " - name: iii-worker-manager",
+ " config:",
+ " port: 49234",
+ " host: 127.0.0.1",
+ " - name: iii-http",
+ " config:",
+ " port: 3211",
+ " cors:",
+ ...corsLines,
+ " allowed_methods: [GET, POST, PUT, DELETE, OPTIONS]",
+ " - name: iii-state",
+ " config: {}",
+ ].join("\n");
+
+ it.each([
+ {
+ shape: "an indented block list",
+ origins: [
+ " allowed_origins:",
+ ' - "http://localhost:3111"',
+ ' - "http://localhost:3113"',
+ ],
+ },
+ {
+ shape: "a compact block list",
+ origins: [
+ " allowed_origins:",
+ ' - "http://localhost:3111"',
+ ' - "http://localhost:3113"',
+ ],
+ },
+ {
+ shape: "a commented block list",
+ origins: [
+ " allowed_origins: # local only",
+ ' - "http://localhost:3111"',
+ "",
+ " # viewer",
+ ' - "http://localhost:3113"',
+ ],
+ },
+ {
+ shape: "a one-line flow list",
+ origins: [' allowed_origins: ["http://localhost:3111", "http://localhost:3113"]'],
+ },
+ {
+ shape: "a multi-line flow list",
+ origins: [
+ " allowed_origins: [",
+ ' "http://localhost:3111",',
+ ' "http://localhost:3113",',
+ " ]",
+ ],
+ },
+ {
+ shape: "a scalar",
+ origins: [' allowed_origins: "*"'],
+ },
+ ])("replaces $shape with the managed origins", ({ origins }) => {
+ const rendered = renderEngineConfig(httpWorker(origins), { dataDir: "/tmp/am", ports });
+
+ expect(rendered).toBe(httpWorker([managedOrigins]));
+ });
+
+ it("keeps the comment that introduces the next key", () => {
+ const source = httpWorker([
+ " allowed_origins:",
+ ' - "http://localhost:3111"',
+ "",
+ " # the viewer needs OPTIONS",
+ ]);
+
+ const rendered = renderEngineConfig(source, { dataDir: "/tmp/am", ports });
+
+ expect(rendered).toBe(
+ httpWorker([managedOrigins, "", " # the viewer needs OPTIONS"]),
+ );
+ });
+});
+
describe("clearPersistedBuiltinConfig", () => {
const configPath = join("/srv", "state", "iii-config.runtime.yaml");