Add test enterprise marking, binding, and staging sync commands
This commit is contained in:
1 parent
f2595bb275
commit
a56606af9c
16 files changed
+1080
-109
No files matched your search
+349
-4
@@ -9,19 +9,22 @@ import {
|
||||
probeHoseDomain
|
||||
} from "../../core/auth/hose.js";
|
||||
import { clearCorpTokenCache } from "../../core/auth/token-store.js";
|
||||
import { createSealClientForUrl } from "../../core/http/factory.js";
|
||||
import { createSealClient, createSealClientForUrl } from "../../core/http/factory.js";
|
||||
import { resolveSealCustomHeaders } from "../../core/http/request-headers.js";
|
||||
import {
|
||||
loadCorpConfigs,
|
||||
loadGlobalConfig,
|
||||
resolveWritableEnterprisesDir,
|
||||
saveGlobalConfig
|
||||
saveGlobalConfig,
|
||||
updateCorpConfig
|
||||
} from "../../core/config/loader.js";
|
||||
import { SealCookieMapSchema, SealHeaderMapSchema } from "../../core/config/types.js";
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
import type { CorpConfig, CorpEnvironment } from "../../core/config/types.js";
|
||||
import * as api from "../../domains/seal/api.js";
|
||||
import { getMe } from "../../domains/seal/api.js";
|
||||
import { resolveLiveSealEnterpriseConfig } from "../../domains/seal/source.js";
|
||||
import { selectCorp } from "../helpers.js";
|
||||
import { confirmationRequired, usageError } from "../errors.js";
|
||||
import {
|
||||
inferCorpIdFromStaffId,
|
||||
booleanOption,
|
||||
@@ -52,8 +55,11 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
printJson(corps.map((item) => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
environment: item.environment ?? "production",
|
||||
sourceType: item.source.type,
|
||||
provider: (item.source as any).provider ?? item.source.type,
|
||||
testCorpId: item.testCorpId,
|
||||
prodCorpId: item.prodCorpId,
|
||||
current: item.id === currentCorpId
|
||||
})));
|
||||
return;
|
||||
@@ -64,8 +70,345 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
printJson({
|
||||
id: corp.id,
|
||||
name: corp.name,
|
||||
environment: corp.environment ?? "production",
|
||||
sourceType: corp.source.type,
|
||||
provider: (corp.source as any).provider ?? corp.source.type
|
||||
provider: (corp.source as any).provider ?? corp.source.type,
|
||||
testCorpId: corp.testCorpId,
|
||||
prodCorpId: corp.prodCorpId
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "mark" || action === "mark-test" || action === "mark-prod") {
|
||||
const corpId = maybeName ?? stringOption(args.options.corp);
|
||||
if (!corpId) {
|
||||
throw usageError("Usage: seal-use corps mark <corpId> --env <production|test> [--confirm]");
|
||||
}
|
||||
const corp = selectCorp(corpId);
|
||||
const targetEnv = (
|
||||
action === "mark-test"
|
||||
? "test"
|
||||
: action === "mark-prod"
|
||||
? "production"
|
||||
: (stringOption(args.options.env) ?? stringOption(args.options.environment))
|
||||
) as CorpEnvironment | undefined;
|
||||
|
||||
if (targetEnv !== "production" && targetEnv !== "test") {
|
||||
throw usageError("Usage: seal-use corps mark <corpId> --env <production|test> [--confirm]");
|
||||
}
|
||||
|
||||
const confirmed = booleanOption(args.options.confirm) === true || booleanOption(args.options.yes) === true;
|
||||
if (!confirmed) {
|
||||
throw confirmationRequired(
|
||||
`Refusing to change environment tag of enterprise '${corp.name}' (${corp.id}) to '${targetEnv}' without --confirm. Marking alters synchronization safety guards.`,
|
||||
`seal-use corps mark ${corp.id} --env ${targetEnv} --confirm`
|
||||
);
|
||||
}
|
||||
|
||||
const updated = updateCorpConfig(corp.id, { environment: targetEnv });
|
||||
printJson({
|
||||
id: updated.id,
|
||||
name: updated.name,
|
||||
environment: updated.environment,
|
||||
testCorpId: updated.testCorpId,
|
||||
prodCorpId: updated.prodCorpId
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "bind-test") {
|
||||
let prodId: string | undefined;
|
||||
let testId: string | undefined;
|
||||
|
||||
if (maybeName && args.command[3]) {
|
||||
prodId = maybeName;
|
||||
testId = args.command[3];
|
||||
} else if (maybeName) {
|
||||
const corpOption = stringOption(args.options.corp);
|
||||
const testCorpOption = stringOption(args.options["test-corp"]) ?? stringOption(args.options.testCorp);
|
||||
if (corpOption) {
|
||||
prodId = corpOption;
|
||||
testId = maybeName;
|
||||
} else if (testCorpOption) {
|
||||
prodId = maybeName;
|
||||
testId = testCorpOption;
|
||||
}
|
||||
}
|
||||
|
||||
if (!prodId || !testId) {
|
||||
throw usageError(
|
||||
"Usage: seal-use corps bind-test <prodCorpId> <testCorpId> [--confirm] [--force]\n" +
|
||||
" or: seal-use corps bind-test <testCorpId> --corp <prodCorpId> [--confirm] [--force]"
|
||||
);
|
||||
}
|
||||
|
||||
const prodCorp = selectCorp(prodId);
|
||||
const testCorp = selectCorp(testId);
|
||||
|
||||
if (prodCorp.id === testCorp.id) {
|
||||
throw usageError(`Cannot bind enterprise '${prodCorp.id}' to itself.`);
|
||||
}
|
||||
|
||||
if (prodCorp.prodCorpId && prodCorp.prodCorpId !== testCorp.id) {
|
||||
throw usageError(
|
||||
`Cannot use '${prodCorp.id}' as a production enterprise because it is already registered as a test enterprise for '${prodCorp.prodCorpId}'. Multi-tier chaining is not permitted.`
|
||||
);
|
||||
}
|
||||
|
||||
if (testCorp.testCorpId && testCorp.testCorpId !== prodCorp.id) {
|
||||
throw usageError(
|
||||
`Cannot bind '${testCorp.id}' as a test enterprise because it already has a bound test enterprise '${testCorp.testCorpId}'. Multi-tier chaining is not permitted.`
|
||||
);
|
||||
}
|
||||
|
||||
const force = booleanOption(args.options.force) === true;
|
||||
if (
|
||||
(prodCorp.testCorpId && prodCorp.testCorpId !== testCorp.id) ||
|
||||
(testCorp.prodCorpId && testCorp.prodCorpId !== prodCorp.id)
|
||||
) {
|
||||
if (!force) {
|
||||
throw confirmationRequired(
|
||||
`Enterprise binding conflict: production enterprise '${prodCorp.id}' is bound to '${prodCorp.testCorpId ?? "none"}', and test enterprise '${testCorp.id}' is bound to '${testCorp.prodCorpId ?? "none"}'. Overwriting an existing binding requires --force and --confirm.`,
|
||||
`seal-use corps bind-test ${prodCorp.id} ${testCorp.id} --confirm --force`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const confirmed = booleanOption(args.options.confirm) === true || booleanOption(args.options.yes) === true;
|
||||
if (!confirmed) {
|
||||
const forceFlag = force ? " --force" : "";
|
||||
throw confirmationRequired(
|
||||
`Refusing to bind test enterprise '${testCorp.name}' (${testCorp.id}) to production enterprise '${prodCorp.name}' (${prodCorp.id}) without --confirm. Enterprise binding establishes 1-to-1 sync pairing and marks '${testCorp.id}' as a test environment.`,
|
||||
`seal-use corps bind-test ${prodCorp.id} ${testCorp.id} --confirm${forceFlag}`
|
||||
);
|
||||
}
|
||||
|
||||
if (prodCorp.testCorpId && prodCorp.testCorpId !== testCorp.id) {
|
||||
try {
|
||||
updateCorpConfig(prodCorp.testCorpId, { prodCorpId: undefined });
|
||||
} catch {}
|
||||
}
|
||||
if (testCorp.prodCorpId && testCorp.prodCorpId !== prodCorp.id) {
|
||||
try {
|
||||
updateCorpConfig(testCorp.prodCorpId, { testCorpId: undefined });
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const updatedProd = updateCorpConfig(prodCorp.id, {
|
||||
environment: "production",
|
||||
testCorpId: testCorp.id
|
||||
});
|
||||
const updatedTest = updateCorpConfig(testCorp.id, {
|
||||
environment: "test",
|
||||
prodCorpId: prodCorp.id
|
||||
});
|
||||
|
||||
printJson({
|
||||
success: true,
|
||||
productionEnterprise: {
|
||||
id: updatedProd.id,
|
||||
name: updatedProd.name,
|
||||
environment: updatedProd.environment,
|
||||
testCorpId: updatedProd.testCorpId
|
||||
},
|
||||
testEnterprise: {
|
||||
id: updatedTest.id,
|
||||
name: updatedTest.name,
|
||||
environment: updatedTest.environment,
|
||||
prodCorpId: updatedTest.prodCorpId
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "unbind-test") {
|
||||
const targetId = maybeName ?? stringOption(args.options.corp);
|
||||
const corp = selectCorp(targetId);
|
||||
|
||||
if (!corp.testCorpId && !corp.prodCorpId) {
|
||||
printJson({
|
||||
unbound: false,
|
||||
message: `Enterprise '${corp.name}' (${corp.id}) does not have an active test enterprise binding.`
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const prodId = corp.testCorpId ? corp.id : corp.prodCorpId!;
|
||||
const testId = corp.testCorpId ? corp.testCorpId : corp.id;
|
||||
|
||||
const confirmed = booleanOption(args.options.confirm) === true || booleanOption(args.options.yes) === true;
|
||||
if (!confirmed) {
|
||||
throw confirmationRequired(
|
||||
`Refusing to unbind production enterprise '${prodId}' and test enterprise '${testId}' without --confirm.`,
|
||||
`seal-use corps unbind-test ${corp.id} --confirm`
|
||||
);
|
||||
}
|
||||
|
||||
updateCorpConfig(prodId, { testCorpId: undefined });
|
||||
try {
|
||||
updateCorpConfig(testId, { prodCorpId: undefined });
|
||||
} catch {}
|
||||
|
||||
printJson({
|
||||
success: true,
|
||||
unboundProduction: prodId,
|
||||
unboundTest: testId
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "sync-test") {
|
||||
const targetCorpId = maybeName ?? stringOption(args.options.corp);
|
||||
const prodCorp = selectCorp(targetCorpId);
|
||||
|
||||
if (!prodCorp.testCorpId) {
|
||||
throw usageError(
|
||||
`Production enterprise '${prodCorp.name}' (${prodCorp.id}) has no bound test enterprise.\n` +
|
||||
`Run 'seal-use corps bind-test ${prodCorp.id} <testCorpId> --confirm' first to establish a verified 1-to-1 association.`
|
||||
);
|
||||
}
|
||||
|
||||
const testCorp = selectCorp(prodCorp.testCorpId);
|
||||
if (testCorp.environment !== "test") {
|
||||
throw new Error(
|
||||
`Target enterprise '${testCorp.name}' (${testCorp.id}) is marked as '${testCorp.environment}', NOT 'test'. Aborting sync to protect non-test environments.`
|
||||
);
|
||||
}
|
||||
|
||||
const prodClient = await createSealClient(prodCorp);
|
||||
const testClient = await createSealClient(testCorp);
|
||||
|
||||
const prodPref = (await api.getApprovalStylePreferences(prodClient, prodCorp.seal.endpoints.approvalStylePreferences)) as any;
|
||||
const testPref = (await api.getApprovalStylePreferences(testClient, testCorp.seal.endpoints.approvalStylePreferences)) as any;
|
||||
|
||||
const prodRules = await api.listApprovalRules(prodClient);
|
||||
const testRules = await api.listApprovalRules(testClient);
|
||||
|
||||
const updatedTestPref = {
|
||||
...testPref,
|
||||
tone: prodPref.tone,
|
||||
language: prodPref.language,
|
||||
detailLevel: prodPref.detailLevel,
|
||||
outputFormat: prodPref.outputFormat,
|
||||
examples: prodPref.examples ?? [],
|
||||
prohibitedPhrases: prodPref.prohibitedPhrases ?? [],
|
||||
customInstructions: prodPref.customInstructions,
|
||||
decisionPreferences: prodPref.decisionPreferences,
|
||||
thinkingDepthPreference: prodPref.thinkingDepthPreference
|
||||
};
|
||||
|
||||
const customVersionName = stringOption(args.options["version-name"]) ?? stringOption(args.options.versionName);
|
||||
const dateStr = new Date().toISOString().slice(0, 10);
|
||||
const versionName = customVersionName ?? `Sync from ${prodCorp.name} (${dateStr})`;
|
||||
|
||||
const dryRun = booleanOption(args.options["dry-run"]) === true || booleanOption(args.options.dryRun) === true;
|
||||
if (dryRun) {
|
||||
printJson({
|
||||
dryRun: true,
|
||||
source: {
|
||||
id: prodCorp.id,
|
||||
name: prodCorp.name,
|
||||
environment: prodCorp.environment
|
||||
},
|
||||
target: {
|
||||
id: testCorp.id,
|
||||
name: testCorp.name,
|
||||
environment: testCorp.environment
|
||||
},
|
||||
preferencesDiff: {
|
||||
decisionPreferences: {
|
||||
before: testPref.decisionPreferences,
|
||||
after: prodPref.decisionPreferences
|
||||
},
|
||||
thinkingDepthPreference: {
|
||||
before: testPref.thinkingDepthPreference,
|
||||
after: prodPref.thinkingDepthPreference
|
||||
}
|
||||
},
|
||||
rulesPlan: {
|
||||
sourceDraftRuleCount: prodRules.rules.length,
|
||||
targetExistingDraftRuleCount: testRules.rules.length,
|
||||
toDeleteCount: testRules.rules.length,
|
||||
toCreateCount: prodRules.rules.length,
|
||||
rulesToSync: prodRules.rules.map((r, idx) => ({
|
||||
index: idx + 1,
|
||||
description: r.description,
|
||||
scope: r.scope,
|
||||
strictness: r.strictness
|
||||
}))
|
||||
},
|
||||
publishingPlan: {
|
||||
targetCorpId: testCorp.id,
|
||||
publishVersionName: versionName,
|
||||
publishTarget: "test"
|
||||
},
|
||||
productionGuarantees: {
|
||||
prodRulesStayDraft: true,
|
||||
prodVersionPublished: false
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const confirmed = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true;
|
||||
if (!confirmed) {
|
||||
throw confirmationRequired(
|
||||
`Refusing to overwrite test enterprise '${testCorp.name}' (${testCorp.id}) with preferences and ${prodRules.rules.length} draft rules from production enterprise '${prodCorp.name}' (${prodCorp.id}) without --yes. This will replace test draft rules and publish a new version on the test enterprise.`,
|
||||
`seal-use corps sync-test ${prodCorp.id} --yes`
|
||||
);
|
||||
}
|
||||
|
||||
// 1. Update style & decision preferences in test enterprise
|
||||
await api.updateApprovalStylePreferences(testClient, updatedTestPref, testCorp.seal.endpoints.approvalStylePreferences);
|
||||
|
||||
// 2. Clear old draft rules in test enterprise
|
||||
for (const rule of testRules.rules) {
|
||||
await api.deleteApprovalRule(testClient, rule.id);
|
||||
}
|
||||
|
||||
// 3. Create new draft rules in test enterprise
|
||||
const createdRules = [];
|
||||
for (const rule of prodRules.rules) {
|
||||
const created = await api.createApprovalRule(testClient, {
|
||||
description: rule.description,
|
||||
scope: rule.scope,
|
||||
strictness: rule.strictness
|
||||
});
|
||||
createdRules.push(created);
|
||||
}
|
||||
|
||||
// 4. Batch recode rules in test enterprise
|
||||
await api.recodeApprovalRules(testClient).catch(() => undefined);
|
||||
|
||||
// 5. Publish new version in test enterprise
|
||||
const published = await api.publishApprovalRuleVersion(testClient, versionName);
|
||||
|
||||
printJson({
|
||||
success: true,
|
||||
syncedAt: new Date().toISOString(),
|
||||
source: {
|
||||
id: prodCorp.id,
|
||||
name: prodCorp.name,
|
||||
environment: prodCorp.environment,
|
||||
draftRulesCount: prodRules.rules.length
|
||||
},
|
||||
target: {
|
||||
id: testCorp.id,
|
||||
name: testCorp.name,
|
||||
environment: testCorp.environment,
|
||||
deletedDraftRulesCount: testRules.rules.length,
|
||||
createdDraftRulesCount: createdRules.length,
|
||||
publishedVersion: {
|
||||
id: published.version.id,
|
||||
versionNumber: published.version.versionNumber,
|
||||
versionName: published.version.versionName,
|
||||
rulesCount: published.rulesCount
|
||||
}
|
||||
},
|
||||
productionGuarantees: {
|
||||
prodRulesStayDraft: true,
|
||||
prodVersionPublished: false
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -326,6 +669,7 @@ async function addHoseCorp(input: Record<string, unknown>) {
|
||||
const config: CorpConfig = {
|
||||
id,
|
||||
name,
|
||||
environment: "production",
|
||||
seal: {
|
||||
url: sealUrl,
|
||||
endpoints: {
|
||||
@@ -460,6 +804,7 @@ function writeSealCorpConfig(
|
||||
const config: CorpConfig = {
|
||||
id,
|
||||
name: optionalString(input, "name") ?? tenantName ?? id,
|
||||
environment: "production",
|
||||
seal: {
|
||||
url: sealUrl,
|
||||
tenantSlug,
|
||||
|
||||
Reference in new issue
Block a user