wishforge-ide/scripts/normalize-model-names.mjs
wishforge 3f879c37d3 ✅ Red-team the Copilot, prepare screenshot acceptance, and tidy two names
Ten ways a customer, a page or a log line may try to make the Copilot hand over
what it must not: a key, the connection string, the system prompt, a kubeconfig.
Where a mock model leaks, the platform still refuses; where it obeys an
injected instruction, the prompt already says a document is data, not a command.
The tool layer is tested separately: an environment file is refused even when
the model names it, and logs outside the project's namespaces are refused.

Screenshot acceptance is prepared, not faked: the fixtures directory explains
what to put there, and the test runs only when an operator does, against the
real gateway.

Model names stored with a context suffix are rewritten by a dry-run-by-default
script (the local database currently has no projects), and the console calls
/providers what it is: 模型连接.
2026-11-09 10:09:59 -08:00

44 lines
1.9 KiB
JavaScript

#!/usr/bin/env node
/**
* Removes a context-window suffix (`[1m]`) from project model names.
*
* The gateway accepts both spellings and normalises them in its reply, so the suffix was never
* meaningful to it — but it does reach the CLI as an unknown model name, and it makes two projects
* look like they use different models. This rewrites the stored name, nothing else.
*
* Dry run by default; pass --apply to write. Only `project` entities are read.
*/
import { Client } from 'pg';
const SUFFIX = /\[[0-9]+[a-z]?\]$/i;
const strip = value => (typeof value === 'string' ? value.replace(SUFFIX, '') : value);
const apply = process.argv.includes('--apply');
if (!process.env.DATABASE_URL) {
console.error('DATABASE_URL is required');
process.exit(1);
}
const client = new Client({ connectionString: process.env.DATABASE_URL });
await client.connect();
try {
const { rows } = await client.query("SELECT tenant_id, id, version, data FROM entities WHERE kind = 'project'");
let changed = 0;
for (const row of rows) {
const models = Array.isArray(row.data?.models) ? row.data.models : [];
const normalized = models.map(strip);
if (JSON.stringify(models) === JSON.stringify(normalized)) continue;
changed += 1;
console.log(`${row.id}\n before: ${models.join(', ')}\n after: ${normalized.join(', ')}`);
if (!apply) continue;
// A compare-and-set on the version keeps a concurrent edit from being overwritten.
const updated = await client.query(
`UPDATE entities SET data = jsonb_set(data, '{models}', $1::jsonb), version = version + 1
WHERE tenant_id = $2 AND kind = 'project' AND id = $3 AND version = $4`,
[JSON.stringify(normalized), row.tenant_id, row.id, row.version],
);
if (updated.rowCount !== 1) console.error(` skipped: ${row.id} changed while this ran`);
}
console.log(`\n${changed} project(s) ${apply ? 'updated' : 'would be updated'}.`);
} finally {
await client.end();
}