Skip to content
Closed
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
5 changes: 4 additions & 1 deletion src/grok/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,17 @@ function canonicalDottedKey(raw: string): string[] {
* TOML admits equivalent header spellings for BOTH segments (`["model"."ocx-mine"]`,
* `['model'.ocx-mine]`, `[ model . ocx-mine ]`); all of them redefine the same table, so each
* form is canonicalized before it is reserved.
* Only exactly two-segment headers collide: `[[model.x]]` makes `model.x` an array that a
* generated `[model.x]` table cannot redefine, while a sub-table like `[model.x.extra]` does
* not collide at all — TOML permits its implicitly created parent to be defined explicitly later.
*/
function userModelAliases(content: string, region: ManagedRegion | null): Set<string> {
const outsideManagedRegion = region
? content.slice(0, region.start) + content.slice(region.end)
: content;
const aliases = new Set<string>();
for (const header of analyzeTomlStructure(outsideManagedRegion).headers) {
if (header.segments[0] !== "model" || header.segments.length < 2) continue;
if (header.segments[0] !== "model" || header.segments.length !== 2) continue;
aliases.add(header.segments[1]!);
}
return aliases;
Expand Down
16 changes: 15 additions & 1 deletion tests/providers/xai/grok-config-inject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ describe("Grok config injection", () => {
["mixed quoting with whitespace", `[ "model" . 'ocx-mine' ]`],
["bare (baseline)", "[model.ocx-mine]"],
["array of tables", "[[model.ocx-mine]]"],
["sub-table", "[model.ocx-mine.extra]"],
["trailing comment", '[model."ocx-mine"] # mine'],
];

Expand All @@ -211,6 +210,21 @@ describe("Grok config injection", () => {
});
}

test("does not reserve an alias from a user sub-table", () => {
// [model.ocx-mine.extra] implicitly creates model.ocx-mine, and TOML permits that parent
// to be defined explicitly later — reserving "ocx-mine" here would needlessly suffix the
// generated alias.
writeFileSync(configPath(), "[model.ocx-mine.extra]\nx = 1\n", "utf8");

injectGrokConfig(10100, [{ id: "mine" }], { grokHome });

const written = readFileSync(configPath(), "utf8");
const generated = written.slice(written.indexOf(BEGIN_MARKER));
expect(written).toContain("[model.ocx-mine.extra]");
expect(generated).toContain("[model.ocx-mine]\n");
expect(generated).not.toContain("[model.ocx-mine-2]");
});

test("does not reserve aliases from unrelated tables", () => {
// [models.*] and [model_providers.*] are different tables entirely — reserving from them
// would needlessly suffix our aliases.
Expand Down
Loading