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
70 changes: 70 additions & 0 deletions kiloclaw/controller/src/config-writer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,76 @@ describe('generateBaseConfig', () => {
expect(config.models.providers.kilocode.models).toEqual([{ id: 'kept/model', name: 'Kept' }]);
});

it('sets X-KiloCode-OrganizationId header when KILOCODE_ORGANIZATION_ID is set', () => {
const { deps } = fakeDeps();
const env = { ...minimalEnv(), KILOCODE_ORGANIZATION_ID: 'org_abc123' };
const config = generateBaseConfig(env, '/tmp/openclaw.json', deps);

expect(config.models.providers.kilocode.headers['X-KiloCode-OrganizationId']).toBe(
'org_abc123'
);
expect(config.models.providers.kilocode.models).toEqual([]);
});

it('does not set org header when KILOCODE_ORGANIZATION_ID is not set', () => {
const { deps } = fakeDeps();
const config = generateBaseConfig(minimalEnv(), '/tmp/openclaw.json', deps);

// No kilocode provider entry created when neither baseUrl nor orgId is set
expect(config.models).toBeUndefined();
});

it('preserves existing kilocode config when adding org header', () => {
const existing = JSON.stringify({
models: {
providers: {
kilocode: {
baseUrl: 'https://tunnel.example.com/',
headers: { 'X-Custom': 'value' },
models: [{ id: 'kept/model', name: 'Kept' }],
},
},
},
});
const { deps } = fakeDeps(existing);
const env = { ...minimalEnv(), KILOCODE_ORGANIZATION_ID: 'org_xyz789' };
const config = generateBaseConfig(env, '/tmp/openclaw.json', deps);

expect(config.models.providers.kilocode.headers['X-KiloCode-OrganizationId']).toBe(
'org_xyz789'
);
expect(config.models.providers.kilocode.headers['X-Custom']).toBe('value');
expect(config.models.providers.kilocode.baseUrl).toBe('https://tunnel.example.com/');
expect(config.models.providers.kilocode.models).toEqual([{ id: 'kept/model', name: 'Kept' }]);
});

it('removes stale org header when KILOCODE_ORGANIZATION_ID is no longer set', () => {
const existing = JSON.stringify({
models: {
providers: {
kilocode: {
baseUrl: 'https://tunnel.example.com/',
headers: {
'X-KiloCode-OrganizationId': 'org_old_stale',
'X-Custom': 'preserved',
},
models: [{ id: 'kept/model', name: 'Kept' }],
},
},
},
});
const { deps } = fakeDeps(existing);
// No KILOCODE_ORGANIZATION_ID in env
const config = generateBaseConfig(minimalEnv(), '/tmp/openclaw.json', deps);

// Stale org header removed
expect(config.models.providers.kilocode.headers['X-KiloCode-OrganizationId']).toBeUndefined();
// Other headers and config preserved
expect(config.models.providers.kilocode.headers['X-Custom']).toBe('preserved');
expect(config.models.providers.kilocode.baseUrl).toBe('https://tunnel.example.com/');
expect(config.models.providers.kilocode.models).toEqual([{ id: 'kept/model', name: 'Kept' }]);
});

it('removes agents.defaults.models allowlist left by openclaw onboard', () => {
const existing = JSON.stringify({
agents: {
Expand Down
18 changes: 18 additions & 0 deletions kiloclaw/controller/src/config-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ export function generateBaseConfig(
console.log(`Overriding kilocode base URL: ${env.KILOCODE_API_BASE_URL}`);
}

// Pass org scope to KiloCode provider as request header when available.
// This is used by OpenClaw provider requests (not Kilo CLI).
// Header name matches ORGANIZATION_ID_HEADER in src/lib/constants.ts.
if (env.KILOCODE_ORGANIZATION_ID) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Stale org header is never removed

If this instance already has X-KiloCode-OrganizationId in openclaw.json and later boots without KILOCODE_ORGANIZATION_ID, this code preserves the old header because it only ever adds to headers. That would keep attributing requests to the previous organization. The patch should explicitly delete this header when the env var is absent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a good catch, i bet we have this bug with some of our other entries as well

config.models = config.models ?? {};
config.models.providers = config.models.providers ?? {};
config.models.providers.kilocode = config.models.providers.kilocode ?? {};
config.models.providers.kilocode.headers = config.models.providers.kilocode.headers ?? {};
config.models.providers.kilocode.headers['X-KiloCode-OrganizationId'] =
env.KILOCODE_ORGANIZATION_ID;
config.models.providers.kilocode.models = config.models.providers.kilocode.models ?? [];
console.log('Configured KiloCode organization header from KILOCODE_ORGANIZATION_ID');
} else {
// Remove stale org header from previous boots (e.g., instance was transferred
// from org to personal, or org was deleted).
delete config.models?.providers?.kilocode?.headers?.['X-KiloCode-OrganizationId'];
}

// User-selected default model override.
if (env.KILOCODE_DEFAULT_MODEL) {
config.agents = config.agents ?? {};
Expand Down
Loading