Merge remote-tracking branch 'origin/master' into feat/sub2api-fault-levels

This commit is contained in:
Codex
2026-07-14 12:40:26 +02:00
63 changed files with 4159 additions and 73 deletions
+2
View File
@@ -871,6 +871,8 @@ function platformInfraHelpSummary(): unknown {
usage: [
"bun scripts/cli.ts platform-infra sub2api plan",
"bun scripts/cli.ts platform-infra sub2api status [--full|--raw]",
"bun scripts/cli.ts platform-infra sub2api ops diagnosis [--platform <name>] [--group <id-or-name>] [--id <diagnosis-id>] [--json]",
"bun scripts/cli.ts platform-infra sub2api ops channels [--platform <name>] [--channel <id-or-name>] [--window 7d|15d|30d] [--page-token <record-id>|--record <record-id>] [--json]",
"bun scripts/cli.ts platform-infra sub2api codex-pool validate",
"bun scripts/cli.ts platform-infra sub2api codex-pool trace --request-id <requestId>",
"bun scripts/cli.ts platform-infra sub2api codex-pool sentinel-image status",
+413 -10
View File
@@ -12,6 +12,7 @@ import { compactText, fingerprintEnvValues as fingerprintValues, parseEnvFile, p
const defaultConfigPath = "config/platform-db/postgres-pk01.yaml";
const managedHbaStart = "# BEGIN unidesk managed pk01-platform-postgres";
const managedHbaEnd = "# END unidesk managed pk01-platform-postgres";
const compactActionablePreviewLimit = 4;
interface PlatformDbOptions {
configPath: string;
@@ -302,10 +303,10 @@ export function platformDbHelp(): unknown {
command: "platform-db postgres plan|status|apply|export-secrets",
output: "json",
usage: [
`bun scripts/cli.ts platform-db postgres plan --config ${defaultConfigPath}`,
`bun scripts/cli.ts platform-db postgres plan --config ${defaultConfigPath} [--full|--raw]`,
`bun scripts/cli.ts platform-db postgres status --config ${defaultConfigPath} [--full|--raw]`,
`bun scripts/cli.ts platform-db postgres export-secrets --config ${defaultConfigPath} --dry-run`,
`bun scripts/cli.ts platform-db postgres export-secrets --config ${defaultConfigPath} --confirm`,
`bun scripts/cli.ts platform-db postgres export-secrets --config ${defaultConfigPath} --dry-run [--full|--raw]`,
`bun scripts/cli.ts platform-db postgres export-secrets --config ${defaultConfigPath} --confirm [--full|--raw]`,
`bun scripts/cli.ts platform-db postgres apply --config ${defaultConfigPath} --dry-run`,
`bun scripts/cli.ts platform-db postgres apply --config ${defaultConfigPath} --confirm`,
`bun scripts/cli.ts platform-db postgres apply --config ${defaultConfigPath} --confirm --wait`,
@@ -318,6 +319,7 @@ export function platformDbHelp(): unknown {
configTruth: defaultConfigPath,
defaultMutation: false,
exportSecrets: "export-secrets only materializes YAML-declared local Secret source/export files; it does not touch the remote PostgreSQL service.",
disclosure: "plan, status and export-secrets use bounded summaries by default; --full and --raw explicitly disclose the complete structured result without printing Secret values.",
apply: "apply --confirm returns an async UniDesk job; the job uses short trans calls and a remote PK01 job instead of keeping one long SSH session open.",
monitorReset: "monitor reset is restricted by owning YAML to the dedicated web_probe_monitor database and requires --confirm; it cannot accept a database name or arbitrary SQL.",
redaction: "Passwords and full DATABASE_URL values are never printed; output is limited to key names, presence, fingerprints and state.",
@@ -439,14 +441,15 @@ async function plan(config: UniDeskConfig, options: PlatformDbOptions): Promise<
const remote = await remoteFacts(config, pg, null);
const facts = remote.parsed;
const checks = facts === null ? [] : desiredChecks(pg, facts, secrets);
return {
const desired = desiredSummary(pg, secrets, facts);
const result = {
ok: remote.capture.exitCode === 0 && facts !== null && secrets.ok,
action: "platform-db-postgres-plan",
mutation: false,
config: configSummary(pg),
secrets: secretSummary(secrets),
remoteFacts: facts,
desired: desiredSummary(pg, secrets, facts),
desired,
checks,
next: {
apply: `bun scripts/cli.ts platform-db postgres apply --config ${pg.configPath} --confirm`,
@@ -456,6 +459,8 @@ async function plan(config: UniDeskConfig, options: PlatformDbOptions): Promise<
remote: compactCapture(remote.capture, { full: options.full || remote.capture.exitCode !== 0 }),
...(options.raw ? { raw: remote.capture } : {}),
};
if (options.full) return result;
return compactPlan(pg, secrets, facts, checks, remote.capture, result.ok);
}
async function status(config: UniDeskConfig, options: PlatformDbOptions): Promise<Record<string, unknown>> {
@@ -485,7 +490,8 @@ async function status(config: UniDeskConfig, options: PlatformDbOptions): Promis
...(secrets.ok ? [] : ["secrets-unhealthy"]),
...(endpointHealthy ? [] : [controllerConnection.attempted ? "connection-host-probe-failed" : "connection-host-probe-unavailable"]),
];
return {
const exportTargets = inspectExportTargets(pg);
const result = {
ok: remote.capture.exitCode === 0 && facts !== null && deploymentHealthy && secrets.ok,
action: "platform-db-postgres-status",
mutation: false,
@@ -522,9 +528,12 @@ async function status(config: UniDeskConfig, options: PlatformDbOptions): Promis
},
remoteFacts: facts,
secrets: secretSummary(secrets),
exports: exportTargets,
remote: compactCapture(remote.capture, { full: options.full || remote.capture.exitCode !== 0 }),
...(options.raw ? { raw: remote.capture } : {}),
};
if (options.full) return result;
return compactStatus(pg, secrets, exportTargets, facts, controllerConnection, remote.capture, result.summary, result.ok);
}
async function apply(config: UniDeskConfig, options: PlatformDbOptions): Promise<Record<string, unknown>> {
@@ -600,7 +609,7 @@ async function exportSecrets(options: PlatformDbOptions): Promise<Record<string,
const pg = readPostgresHostConfig(options.configPath);
if (!options.confirm || options.dryRun) {
const localState = buildLocalSecretState(pg, false);
return {
const result = {
ok: localState.inspection.ok,
action: "platform-db-postgres-export-secrets",
mode: "dry-run",
@@ -613,9 +622,11 @@ async function exportSecrets(options: PlatformDbOptions): Promise<Record<string,
},
valuesPrinted: false,
};
if (options.full) return result;
return compactExportSecrets(pg, localState, "dry-run", false, result.ok);
}
const localState = ensureLocalSecretState(pg);
return {
const result = {
ok: true,
action: "platform-db-postgres-export-secrets",
mode: "confirmed",
@@ -628,6 +639,8 @@ async function exportSecrets(options: PlatformDbOptions): Promise<Record<string,
},
valuesPrinted: false,
};
if (options.full) return result;
return compactExportSecrets(pg, localState, "confirmed", true, result.ok);
}
function readPostgresHostConfig(pathArg: string): PostgresHostConfig {
@@ -1071,6 +1084,360 @@ function configSummary(pg: PostgresHostConfig): Record<string, unknown> {
};
}
function compactPlan(
pg: PostgresHostConfig,
secrets: SecretInspection,
facts: RemoteFacts | null,
checks: Array<Record<string, unknown>>,
capture: SshCaptureResult,
ok: boolean,
): Record<string, unknown> {
const observedRoles = facts?.postgres.roles ?? [];
const observedDatabases = facts?.postgres.databases ?? [];
return {
ok,
action: "platform-db-postgres-plan",
mutation: false,
target: {
node: pg.node.id,
route: pg.node.route,
mode: pg.node.mode,
},
config: {
path: pg.configPath,
id: pg.metadata.id,
pgVersion: pg.postgres.package.version,
connectionHost: pg.postgres.network.connectionHost,
publicDns: pg.postgres.network.publicDns,
port: pg.postgres.network.port,
transport: pg.postgres.network.transport,
sslmode: pg.postgres.network.sslmode,
},
roles: pg.objects.roles.map((role) => ({
name: role.name,
exists: observedRoles.find((item) => item.name === role.name)?.exists ?? null,
action: observedRoles.some((item) => item.name === role.name && item.exists) ? "none" : "create-or-update",
})),
databases: pg.objects.databases.map((database) => ({
name: database.name,
owner: database.owner,
exists: observedDatabases.find((item) => item.name === database.name)?.exists ?? null,
action: observedDatabases.some((item) => item.name === database.name && item.exists) ? "none" : "create",
})),
secrets: {
ok: secrets.ok,
root: secrets.root,
entries: secrets.entries.map((entry) => ({
sourceRef: entry.sourceRef,
missingKeys: entry.missingKeys,
action: entry.action,
})),
valuesPrinted: false,
},
exports: pg.exports.connectionStrings.map((item) => ({
name: item.name,
sourceRef: item.sourceSecretRef,
targetRef: item.writeToSecretSource.sourceRef,
key: item.writeToSecretSource.key,
consumerScopes: item.consumers.map((consumer) => consumer.scope),
})),
checks: {
ok: checks.every((check) => check.ok === true),
passed: checks.filter((check) => check.ok === true).length,
total: checks.length,
items: checks.map((check) => ({
name: check.name,
ok: check.ok,
...(check.ok === true ? {} : { detail: compactText(JSON.stringify(check)).slice(0, 480) }),
})),
},
remoteFacts: compactRemoteFacts(facts),
remote: compactPlanCapture(capture),
next: {
apply: `bun scripts/cli.ts platform-db postgres apply --config ${pg.configPath} --confirm`,
status: `bun scripts/cli.ts platform-db postgres status --config ${pg.configPath}`,
full: `bun scripts/cli.ts platform-db postgres plan --config ${pg.configPath} --full`,
raw: `bun scripts/cli.ts platform-db postgres plan --config ${pg.configPath} --raw`,
issues: pg.metadata.relatedIssues.map((issue) => `https://github.com/pikasTech/unidesk/issues/${issue}`),
},
disclosure: {
compact: true,
omitted: ["package", "host policy", "TLS paths", "backup", "full remote facts"],
fullAvailable: true,
rawAvailable: true,
},
valuesPrinted: false,
};
}
function compactStatus(
pg: PostgresHostConfig,
secrets: SecretInspection,
exports: Array<Record<string, unknown>>,
facts: RemoteFacts | null,
controllerConnection: ControllerConnectionProbe,
capture: SshCaptureResult,
summary: Record<string, unknown> | null,
ok: boolean,
): Record<string, unknown> {
const blockers = summary === null
? ["remote-facts-unavailable"]
: Array.isArray(summary.cutoverBlockers)
? summary.cutoverBlockers
: [];
const roles = pg.objects.roles.map((role) => ({
name: role.name,
exists: facts?.postgres.roles.find((item) => item.name === role.name)?.exists ?? null,
}));
const databases = pg.objects.databases.map((database) => ({
name: database.name,
owner: database.owner,
exists: facts?.postgres.databases.find((item) => item.name === database.name)?.exists ?? null,
}));
const appConnections = facts?.postgres.appConnections ?? [];
return {
ok,
action: "platform-db-postgres-status",
mutation: false,
failure: ok ? null : {
type: capture.exitCode !== 0 || facts === null
? "remote-facts-unavailable"
: secrets.ok
? "deployment-unhealthy"
: "secrets-unhealthy",
blockers,
},
target: {
node: pg.node.id,
route: pg.node.route,
mode: pg.node.mode,
},
config: {
path: pg.configPath,
id: pg.metadata.id,
pgVersion: pg.postgres.package.version,
connectionHost: pg.postgres.network.connectionHost,
port: pg.postgres.network.port,
sslmode: pg.postgres.network.sslmode,
},
summary: summary === null ? null : {
healthy: summary.healthy,
deploymentHealthy: summary.deploymentHealthy,
cutoverReady: summary.cutoverReady,
cutoverBlockers: summary.cutoverBlockers,
packageInstalled: summary.packageInstalled,
serviceActive: summary.serviceActive,
serviceEnabled: summary.serviceEnabled,
sslOn: summary.sslOn,
port5432Listening: summary.port5432Listening,
dnsResolves: summary.dnsResolves,
dnsDisposition: summary.dnsDisposition,
secretsOk: summary.secretsOk,
connectionHostProbe: {
attempted: controllerConnection.attempted,
ok: controllerConnection.ok,
ssl: controllerConnection.ssl,
user: controllerConnection.user,
database: controllerConnection.database,
host: controllerConnection.host,
port: controllerConnection.port,
error: controllerConnection.error,
},
},
roles: compactActionableSummary(roles, {
passed: (item) => item.exists === true,
missing: (item) => item.exists === false,
actionable: (item) => item.exists !== true,
project: (item) => item,
}),
databases: compactActionableSummary(databases, {
passed: (item) => item.exists === true,
missing: (item) => item.exists === false,
actionable: (item) => item.exists !== true,
project: (item) => item,
}),
appConnections: compactActionableSummary(appConnections, {
passed: (item) => item.ok === true && item.ssl === true,
missing: (item) => item.ok !== true || item.ssl !== true,
actionable: (item) => item.ok !== true || item.ssl !== true,
project: (item) => ({ user: item.user, database: item.database, ok: item.ok, ssl: item.ssl, error: item.error }),
}),
secrets: compactSecretInspection(secrets),
exports: compactActionableSummary(exports, {
passed: (item) => item.present === true,
missing: (item) => item.present !== true,
actionable: (item) => item.present !== true,
project: (item) => ({
targetRef: item.targetRef,
key: item.key,
exists: item.exists,
present: item.present,
fingerprint: item.fingerprint,
}),
}),
remote: compactPlanCapture(capture),
next: {
plan: `bun scripts/cli.ts platform-db postgres plan --config ${pg.configPath}`,
full: `bun scripts/cli.ts platform-db postgres status --config ${pg.configPath} --full`,
raw: `bun scripts/cli.ts platform-db postgres status --config ${pg.configPath} --raw`,
},
disclosure: {
compact: true,
omitted: ["host facts", "package repository", "TLS paths", "raw remote capture"],
fullAvailable: true,
rawAvailable: true,
},
valuesPrinted: false,
};
}
function compactExportSecrets(
pg: PostgresHostConfig,
localState: { inspection: SecretInspection; summary: Record<string, unknown>; exports: Array<Record<string, unknown>> },
mode: "dry-run" | "confirmed",
mutation: boolean,
ok: boolean,
): Record<string, unknown> {
const suffix = mode === "dry-run" ? " --dry-run" : " --confirm";
const exports = localState.exports.map((item) => {
const before = asRecord(item.before, "export before");
const after = asRecord(item.after, "export after");
return {
name: item.name,
sourceRef: item.sourceRef,
targetRef: item.targetRef,
key: item.key,
before,
after,
action: item.action,
desiredFingerprint: item.desiredFingerprint,
};
});
return {
ok,
action: "platform-db-postgres-export-secrets",
mode,
mutation,
failure: ok ? null : {
type: "secrets-unhealthy",
blockers: localState.inspection.entries.filter((entry) => entry.missingKeys.length > 0).map((entry) => entry.sourceRef),
},
config: {
path: pg.configPath,
id: pg.metadata.id,
secretRoot: localState.inspection.root,
},
secrets: compactSecretInspection(localState.inspection),
exports: compactActionableSummary(exports, {
passed: (item) => item.action === "none" && item.after.present === true,
missing: (item) => item.after.present !== true,
actionable: (item) => item.action !== "none" || item.after.present !== true,
project: (item) => item,
}),
next: {
...(mode === "dry-run" ? { confirm: `bun scripts/cli.ts platform-db postgres export-secrets --config ${pg.configPath} --confirm` } : {}),
full: `bun scripts/cli.ts platform-db postgres export-secrets --config ${pg.configPath}${suffix} --full`,
raw: `bun scripts/cli.ts platform-db postgres export-secrets --config ${pg.configPath}${suffix} --raw`,
syncConsumers: "run the consumer-specific secret sync command after confirmed export",
},
disclosure: {
compact: true,
omitted: ["source paths", "required key lists", "consumer Secret details"],
fullAvailable: true,
rawAvailable: true,
},
valuesPrinted: false,
};
}
function compactSecretInspection(secrets: SecretInspection): Record<string, unknown> {
const entries = secrets.entries.map((entry) => ({
sourceRef: entry.sourceRef,
exists: entry.exists,
presentKeys: entry.presentKeys,
missingKeys: entry.missingKeys,
action: entry.action,
fingerprint: entry.fingerprint,
}));
return {
ok: secrets.ok,
root: secrets.root,
...compactActionableSummary(entries, {
passed: (item) => item.exists && item.missingKeys.length === 0 && item.action === "none",
missing: (item) => !item.exists || item.missingKeys.length > 0,
actionable: (item) => item.action !== "none" || !item.exists || item.missingKeys.length > 0,
project: (item) => item,
}),
valuesPrinted: false,
};
}
function compactActionableSummary<T>(
items: T[],
selectors: {
passed: (item: T) => boolean;
missing: (item: T) => boolean;
actionable: (item: T) => boolean;
project: (item: T) => unknown;
},
): Record<string, unknown> {
const actionableItems = items.filter(selectors.actionable);
const visibleItems = actionableItems.slice(0, compactActionablePreviewLimit);
return {
total: items.length,
passed: items.filter(selectors.passed).length,
missing: items.filter(selectors.missing).length,
actionable: actionableItems.length,
listed: visibleItems.length,
omitted: items.length - visibleItems.length,
actionableOmitted: actionableItems.length - visibleItems.length,
items: visibleItems.map(selectors.project),
};
}
function compactRemoteFacts(facts: RemoteFacts | null): Record<string, unknown> | null {
if (facts === null) return null;
return {
ok: facts.ok,
observedAt: facts.observedAt,
host: {
hostname: facts.host.hostname,
cpuCount: facts.host.cpuCount,
memoryTotalMiB: facts.host.memoryTotalMiB,
swapTotalMiB: facts.host.swapTotalMiB,
rootAvailGiB: facts.host.rootAvailGiB,
targetDataAvailGiB: facts.host.targetDataAvailGiB,
},
network: {
port5432Listening: facts.network.port5432Listening,
dns: facts.network.dns,
},
repo: {
reachable: facts.repo.reachable,
releaseUrl: facts.repo.releaseUrl,
},
postgres: {
packageInstalled: facts.postgres.packageInstalled,
serviceActive: facts.postgres.serviceActive,
serviceEnabled: facts.postgres.serviceEnabled,
roleCount: facts.postgres.roles.length,
databaseCount: facts.postgres.databases.length,
serverVersion: facts.postgres.serverVersion,
sslOn: facts.postgres.sslOn,
},
};
}
function compactPlanCapture(result: SshCaptureResult): Record<string, unknown> {
return {
exitCode: result.exitCode,
stdoutBytes: Buffer.byteLength(result.stdout, "utf8"),
stderrBytes: Buffer.byteLength(result.stderr, "utf8"),
stdoutTail: result.exitCode === 0 ? "" : result.stdout.slice(-1200),
stderrTail: result.exitCode === 0 ? "" : result.stderr.slice(-1200),
};
}
function requireMonitorResetConfig(pg: PostgresHostConfig): MonitorResetConfig {
const target = pg.managedOperations?.monitorReset;
if (target === undefined) throw new Error(`${pg.configPath}.managedOperations.monitorReset is required`);
@@ -1252,8 +1619,12 @@ function connectionStringExportState(pg: PostgresHostConfig, inspection: SecretI
const rendered = renderConnectionString(item, source.values);
const root = secretRoot(pg);
const targetPath = join(root, item.writeToSecretSource.sourceRef);
const existing = existsSync(targetPath) ? parseEnvFile(readFileSync(targetPath, "utf8")) : {};
const exists = existsSync(targetPath);
const existing = exists ? parseEnvFile(readFileSync(targetPath, "utf8")) : {};
const before = existing[item.writeToSecretSource.key];
const beforePresent = before !== undefined && before.length > 0;
const beforeFingerprint = beforePresent ? fingerprintValues({ [item.writeToSecretSource.key]: before }, [item.writeToSecretSource.key]) : null;
const desiredFingerprint = fingerprintValues({ [item.writeToSecretSource.key]: rendered }, [item.writeToSecretSource.key]);
const next = { ...existing, [item.writeToSecretSource.key]: rendered };
if (materialize) writeEnvFile(targetPath, next);
return {
@@ -1261,13 +1632,45 @@ function connectionStringExportState(pg: PostgresHostConfig, inspection: SecretI
sourceRef: item.sourceSecretRef,
targetRef: item.writeToSecretSource.sourceRef,
key: item.writeToSecretSource.key,
before: {
exists,
present: beforePresent,
fingerprint: beforeFingerprint,
},
after: {
exists: materialize ? true : exists,
present: materialize ? true : beforePresent,
fingerprint: materialize ? desiredFingerprint : beforeFingerprint,
},
action: before === rendered ? "none" : before === undefined ? "create" : "update",
fingerprint: fingerprintValues({ [item.writeToSecretSource.key]: rendered }, [item.writeToSecretSource.key]),
desiredFingerprint,
consumers: item.consumers,
valuesPrinted: false,
};
}
function inspectExportTargets(pg: PostgresHostConfig): Array<Record<string, unknown>> {
const root = secretRoot(pg);
return pg.exports.connectionStrings.map((item) => {
const targetPath = join(root, item.writeToSecretSource.sourceRef);
const exists = existsSync(targetPath);
const values = exists ? parseEnvFile(readFileSync(targetPath, "utf8")) : {};
const value = values[item.writeToSecretSource.key];
const present = value !== undefined && value.length > 0;
return {
name: item.name,
sourceRef: item.sourceSecretRef,
targetRef: item.writeToSecretSource.sourceRef,
key: item.writeToSecretSource.key,
exists,
present,
fingerprint: present ? fingerprintValues({ [item.writeToSecretSource.key]: value }, [item.writeToSecretSource.key]) : null,
consumerScopes: item.consumers.map((consumer) => consumer.scope),
valuesPrinted: false,
};
});
}
function renderConnectionString(item: ConnectionStringExportConfig, values: Record<string, string>): string {
let output = item.render.format;
const merged = { ...values, ...(item.render.variables ?? {}) };
@@ -4,7 +4,7 @@ import {
readGiteaConfig,
resolveTarget,
} from "./platform-infra-gitea-config";
import { buildMirrorWebhookCredentialBlockedResult, renderManifest } from "./platform-infra-gitea";
import { buildGiteaMirrorBootstrapCredentialPreflight, buildMirrorWebhookCredentialBlockedResult, renderManifest } from "./platform-infra-gitea";
import { renderMirrorWebhookCredentialBlocked } from "./platform-infra-gitea-render";
function syntheticMissingSelfmediaCredential(): Record<string, unknown> {
@@ -65,6 +65,28 @@ describe("platform-infra Gitea repository GitHub credentials", () => {
expect(nc01Overrides.map((credential) => credential.sourceRef)).toContain("pikainc-selfmedia-gh-token.txt");
});
test("bootstrap preflight only requires the selected repository effective credential", () => {
const config = readGiteaConfig();
const repository = config.sourceAuthority.repositories.find((repo) => repo.key === "selfmedia-nc01");
expect(repository).toBeDefined();
const result = buildGiteaMirrorBootstrapCredentialPreflight(config, [repository!], (credential) => (
credential.sourceRef === "pikainc-selfmedia-gh-token.txt"
? { [credential.sourceKey]: "fixture-token" }
: null
));
expect(result.blockers).toEqual([]);
expect(result.credentials).toHaveLength(1);
expect(result.credentials[0]).toMatchObject({
id: "github-upstream:selfmedia-nc01",
sourceRef: "pikainc-selfmedia-gh-token.txt",
present: true,
requiredKeysPresent: true,
});
expect(JSON.stringify(result)).not.toContain(config.sourceAuthority.credentials.admin.sourceRef);
expect(JSON.stringify(result)).not.toContain(config.sourceAuthority.credentials.github.sourceRef);
});
test("does not inject the NC01 selfmedia token into JD01 bridge containers", () => {
const config = readGiteaConfig();
const selfmediaTokenEnv = githubTokenEnvNameForSecretKey("github-token-pikainc-selfmedia");
@@ -221,11 +221,45 @@ payload = {
},
"valuesPrinted": False,
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
sys.exit(0 if payload["ok"] else 1)
PY
}
run_mirror_preflight() {
python3 - "$tmp/repos.json" <<'PY'
import base64, json, os, subprocess, sys
repos = json.load(open(sys.argv[1], encoding="utf-8"))
rows = []
timeout = int(os.environ.get("UNIDESK_GITEA_WAIT_TIMEOUT_SECONDS", "20"))
for repo in repos:
credential = repo.get("githubCredential") or {}
token_env = credential.get("tokenEnv") or os.environ.get("UNIDESK_GITEA_GITHUB_DEFAULT_TOKEN_ENV", "")
token = os.environ.get(token_env, "")
clone_url = repo["upstream"]["cloneUrl"]
branch = repo["upstream"]["branch"]
if not token:
rows.append({"key": repo["key"], "repository": repo["upstream"]["repository"], "branch": branch, "ok": False, "code": "github-credential-unavailable", "valuesPrinted": False})
continue
basic = base64.b64encode(("x-access-token:" + token).encode()).decode()
try:
completed = subprocess.run(
["git", "-c", "http.extraHeader=Authorization: Basic " + basic, "ls-remote", "--exit-code", clone_url, "refs/heads/" + branch],
capture_output=True,
text=True,
timeout=timeout,
)
error = " ".join(completed.stderr.split())[-800:]
rows.append({"key": repo["key"], "repository": repo["upstream"]["repository"], "branch": branch, "ok": completed.returncode == 0, "code": "github-upstream-available" if completed.returncode == 0 else "github-repository-not-found-or-no-access", "exitCode": completed.returncode, "errorTail": error, "valuesPrinted": False})
except subprocess.TimeoutExpired:
rows.append({"key": repo["key"], "repository": repo["upstream"]["repository"], "branch": branch, "ok": False, "code": "github-upstream-timeout", "valuesPrinted": False})
payload = {"ok": bool(rows) and all(row["ok"] for row in rows), "mutation": False, "repositories": rows, "valuesPrinted": False}
print(json.dumps(payload, ensure_ascii=False))
sys.exit(0 if payload["ok"] else 1)
PY
}
run_status() {
selector="app.kubernetes.io/name=$UNIDESK_GITEA_APP_NAME,app.kubernetes.io/component=gitea"
capture_json namespace kubectl get namespace "$UNIDESK_GITEA_NAMESPACE"
@@ -1125,6 +1159,7 @@ case "$UNIDESK_GITEA_ACTION" in
apply) run_apply ;;
status) run_status ;;
validate) run_validate ;;
mirror-preflight) run_mirror_preflight ;;
mirror-bootstrap) run_mirror_bootstrap ;;
mirror-sync) run_mirror_sync ;;
mirror-status) run_mirror_status ;;
+64 -1
View File
@@ -123,6 +123,69 @@ export async function runPlatformInfraGiteaCommand(config: UniDeskConfig, args:
};
}
export async function runGiteaMirrorBootstrapPreflight(
config: UniDeskConfig,
targetId: string,
repoKey: string,
): Promise<Record<string, unknown>> {
const gitea = readGiteaConfig();
const target = resolveCommandTarget(gitea, targetId);
const repos = selectedRepositories(gitea, target, repoKey);
const { credentials, blockers } = buildGiteaMirrorBootstrapCredentialPreflight(gitea, repos);
if (blockers.length > 0) {
return { ok: false, mutation: false, code: "github-credential-unavailable", blockers, repositories: repos.map((repo) => repositorySummary(gitea, repo)), valuesPrinted: false };
}
const secrets = mirrorPreflightSecrets(gitea, repos);
const result = await capture(config, target.route, ["sh"], remoteScript("mirror-preflight", gitea, target, "", { targetId, repoKey, confirm: false, dryRun: true, wait: false, full: false, raw: false }, { repos, secrets }).script);
const parsed = parseJsonOutput(result.stdout);
return parsed ?? { ok: false, mutation: false, code: "github-upstream-preflight-failed", remote: compactCapture(result, { full: true }), valuesPrinted: false };
}
export function buildGiteaMirrorBootstrapCredentialPreflight(
gitea: GiteaConfig,
repositories: GiteaMirrorRepository[],
readCredential: (credential: GiteaGithubCredential) => Record<string, string> | null = (credential) => readGithubCredential(gitea, credential),
): { credentials: Array<Record<string, unknown>>; blockers: string[] } {
const credentials = repositories.map((repo) => {
const github = githubCredentialForRepo(gitea, repo);
const values = readCredential(github);
const present = values !== null;
const requiredKeysPresent = Boolean(values?.[github.sourceKey]);
return {
id: `github-upstream:${repo.key}`,
repository: repo.upstream.repository,
sourceRef: github.sourceRef,
present,
requiredKeysPresent,
fingerprint: present ? fingerprintKeys(values, [github.sourceKey]) : null,
permissionResult: present && requiredKeysPresent
? { status: "not-probed", permissions: github.permissions, error: null }
: { status: "blocked-missing-credential", permissions: github.permissions, error: "credential material is absent" },
valuesPrinted: false,
};
});
return { credentials, blockers: credentialBlockers(credentials) };
}
function readGithubCredential(gitea: GiteaConfig, github: GiteaGithubCredential): Record<string, string> | null {
const sourcePath = credentialPath(gitea, github.sourceRef);
return existsSync(sourcePath) ? parseGithubCredentialFile(sourcePath, github) : null;
}
function mirrorPreflightSecrets(gitea: GiteaConfig, repositories: GiteaMirrorRepository[]): MirrorSecrets {
const githubTokens: Record<string, string> = {};
for (const repo of repositories) {
const github = githubCredentialForRepo(gitea, repo);
const githubPath = credentialPath(gitea, github.sourceRef);
if (!existsSync(githubPath)) throw new Error(`${github.sourceRef} is missing; cannot probe GitHub upstream`);
const githubEnv = parseGithubCredentialFile(githubPath, github);
const githubToken = githubEnv[github.sourceKey];
if (!githubToken) throw new Error(`${github.sourceRef} must contain ${github.sourceKey}`);
githubTokens[github.gitFetchCredential.secretRef.key] = githubToken;
}
return { adminUsername: "", adminPassword: "", githubTokens, webhookSecret: "" };
}
export function giteaHelp(scope?: string): Record<string, unknown> {
if (scope === "platform-bootstrap") {
return {
@@ -1116,7 +1179,7 @@ function envVars(gitea: GiteaConfig, target: GiteaTarget): string {
value: ${yamlQuote(value)}`).join("\n");
}
function remoteScript(action: "apply" | "status" | "validate" | "mirror-bootstrap" | "mirror-sync" | "mirror-status" | "mirror-webhook-apply" | "mirror-webhook-status", gitea: GiteaConfig, target: GiteaTarget, manifest: string, options: ApplyOptions | MirrorOptions, params: MirrorRemoteParams = {}): { script: string; payloadSummary: Record<string, unknown> } {
function remoteScript(action: "apply" | "status" | "validate" | "mirror-preflight" | "mirror-bootstrap" | "mirror-sync" | "mirror-status" | "mirror-webhook-apply" | "mirror-webhook-status", gitea: GiteaConfig, target: GiteaTarget, manifest: string, options: ApplyOptions | MirrorOptions, params: MirrorRemoteParams = {}): { script: string; payloadSummary: Record<string, unknown> } {
const frpcExposure = targetFrpcExposure(gitea, target);
const sync = targetWebhookSync(gitea, target);
const env: Record<string, string> = {
@@ -3,7 +3,12 @@ import { spawnSync } from "node:child_process";
import { rootPath } from "./config";
import { isRenderedCliResult } from "./output";
import { runPlatformObservabilityCommand } from "./platform-infra-observability";
import { resolveTarget } from "./platform-infra-observability/actions";
import { isKubernetesLabelValue, isKubernetesQualifiedName, readObservabilityConfig } from "./platform-infra-observability/config";
import { renderObservabilityPlanFailure } from "./platform-infra-observability/plan-render";
import { prometheusMetaLabel, re2Literal } from "./platform-infra-observability/prometheus";
import { statusScript } from "./platform-infra-observability/search-script";
import { renderManifest } from "./platform-infra-observability/trace-script";
describe("platform-infra observability progressive disclosure", () => {
test("default plan is a bounded table while full and raw preserve the complete plan", async () => {
@@ -13,6 +18,8 @@ describe("platform-infra observability progressive disclosure", () => {
expect(Buffer.byteLength(compact.renderedText, "utf8")).toBeLessThan(10_240);
expect(compact.renderedText).toContain("serviceConnections=8");
expect(compact.renderedText).toContain("configRefs=6/6 missing=0");
expect(compact.renderedText).toContain("prometheus=enabled service=prometheus targets=NC01");
expect(compact.renderedText).toContain("scrapeSelector=unidesk.ai/metrics=true");
expect(compact.renderedText).toContain("--full");
expect(compact.renderedText).toContain("--raw");
@@ -25,6 +32,7 @@ describe("platform-infra observability progressive disclosure", () => {
renderPlan: { target: { id: "NC01", route: "NC01:k3s", namespace: "platform-infra" } },
});
expect((expanded as Record<string, any>).renderPlan.instrumentation).toHaveLength(8);
expect((expanded as Record<string, any>).config.metricsBackend).toMatchObject({ type: "prometheus", enabled: true });
}
});
@@ -38,8 +46,66 @@ describe("platform-infra observability progressive disclosure", () => {
expect(result.stdout).not.toContain("/tmp/unidesk-cli-output");
});
test("search, trace and diagnose-code-agent expose only scoped help", () => {
for (const operation of ["search", "trace", "diagnose-code-agent"]) {
test("Prometheus manifests and runtime access only apply to YAML-selected targets", async () => {
const observability = readObservabilityConfig();
const nc01 = resolveTarget(observability, "NC01");
const d518 = resolveTarget(observability, "D518");
const jd01 = resolveTarget(observability, "JD01");
const nc01Manifest = renderManifest(observability, nc01);
for (const target of [d518, jd01]) {
const manifest = renderManifest(observability, target);
expect(manifest).not.toContain("kind: ClusterRole\nmetadata:\n name: platform-infra-prometheus");
expect(manifest).not.toContain("kind: PersistentVolumeClaim\nmetadata:\n name: prometheus-data");
expect(manifest).not.toContain("app.kubernetes.io/component: metrics-backend");
const plan = await runPlatformObservabilityCommand({} as never, ["plan", "--target", target.id]);
expect(isRenderedCliResult(plan)).toBe(true);
if (!isRenderedCliResult(plan)) throw new Error("expected rendered plan");
expect(plan.renderedText).toContain("prometheus=disabled-for-target");
expect(plan.renderedText).toContain("objects=8");
const script = statusScript(observability, target, false);
expect(script).not.toContain("capture_json metrics_deployments");
expect(script).not.toContain("prometheus-ready");
const query = await runPlatformObservabilityCommand({} as never, ["metrics-query", "--target", target.id, "--query", "up", "--full"]);
expect(query).toMatchObject({ metrics: { enabled: false, disposition: "disabled-for-target", targetIds: ["NC01"] } });
}
expect(nc01Manifest).toContain("kind: ClusterRole\nmetadata:\n name: platform-infra-prometheus");
expect(nc01Manifest).toContain("kind: PersistentVolumeClaim\nmetadata:\n name: prometheus-data");
});
test("Prometheus relabel rules prefer an explicit path and only default when absent", () => {
const observability = readObservabilityConfig();
const manifest = renderManifest(observability, resolveTarget(observability, "NC01"));
const explicitPath = "source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]\n action: replace\n target_label: __metrics_path__\n regex: (/.+)";
const defaultPath = "source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]\n action: replace\n target_label: __metrics_path__\n regex: ^$\n replacement: /metrics";
expect(manifest).toContain("source_labels: [__meta_kubernetes_pod_label_unidesk_ai_metrics]");
expect(manifest).toContain("regex: '^true$'");
expect(manifest).toContain(explicitPath);
expect(manifest).toContain(defaultPath);
expect(manifest.indexOf(explicitPath)).toBeLessThan(manifest.indexOf(defaultPath));
});
test("Prometheus selector rendering uses qualified names, stable meta labels and literal RE2", () => {
const observability = readObservabilityConfig();
observability.metricsBackend.discovery.labelSelector = {
key: "metrics.example.com/release-track",
value: "v1.2-beta",
};
observability.metricsBackend.discovery.annotationKeys.scrape = "prometheus.io/scrape-enabled";
const manifest = renderManifest(observability, resolveTarget(observability, "NC01"));
expect(manifest).toContain("__meta_kubernetes_pod_label_metrics_example_com_release_track");
expect(manifest).toContain("__meta_kubernetes_pod_annotation_prometheus_io_scrape_enabled");
expect(manifest).toContain("regex: '^v1\\.2-beta$'");
expect(prometheusMetaLabel("a.b/c-d_e")).toBe("a_b_c_d_e");
expect(re2Literal("v1.2-beta")).toBe("^v1\\.2-beta$");
expect(isKubernetesQualifiedName("metrics.example.com/release-track")).toBe(true);
expect(isKubernetesQualifiedName("metrics.example.com/release/track")).toBe(false);
expect(isKubernetesQualifiedName("Metrics.example.com/release-track")).toBe(false);
expect(isKubernetesQualifiedName(`${"a".repeat(64)}.example/release-track`)).toBe(false);
expect(isKubernetesLabelValue("v1.2-beta")).toBe(true);
});
test("query commands expose only scoped help", () => {
for (const operation of ["search", "trace", "diagnose-code-agent", "metrics-query"]) {
const result = cli(["platform-infra", "observability", operation, "--help"]);
expect(result.status).toBe(0);
const payload = JSON.parse(result.stdout) as Record<string, any>;
@@ -57,7 +123,7 @@ describe("platform-infra observability progressive disclosure", () => {
expect(result.status).toBe(0);
const payload = JSON.parse(result.stdout) as Record<string, any>;
expect(payload.data.operations.map((item: Record<string, unknown>) => item.name)).toEqual([
"plan", "apply", "status", "validate", "trace", "search", "diagnose-code-agent",
"plan", "apply", "status", "validate", "trace", "search", "diagnose-code-agent", "metrics-query",
]);
expect(payload.data.scopedHelp).toContain("<operation> --help");
expect(payload.data.usage).toBeUndefined();
@@ -27,6 +27,7 @@ import { compactStatus, configSummary, manifestObjectSummary, policyChecks, stat
import { renderManifest } from "./trace-script";
import { formatTable, shortenEnd, textValue } from "./manifest";
import { apiPathField, configLabel, kubernetesNameField, stringField } from "./types";
import { metricsEnabledForTarget, metricsTargetDisposition } from "./metrics-target";
export function parseStatusEndpoint(record: Record<string, unknown>, index: number): StatusEndpoint {
const path = `probes.statusEndpoints[${index}]`;
@@ -57,6 +58,7 @@ export function plan(options: CommonOptions): Record<string, unknown> {
const target = resolveTarget(observability, options.targetId);
const yaml = renderManifest(observability, target);
const policy = policyChecks(yaml, target);
const metricsDisposition = metricsTargetDisposition(observability, target);
return {
ok: policy.every((check) => check.ok),
action: "platform-infra-observability-plan",
@@ -65,10 +67,16 @@ export function plan(options: CommonOptions): Record<string, unknown> {
renderPlan: {
target: targetSummary(target),
objects: manifestObjectSummary(yaml),
metrics: {
disposition: metricsDisposition,
targetIds: observability.metricsBackend.targetIds,
},
otlp: {
collectorGrpcEndpoint: `${observability.collector.serviceName}.${target.namespace}.svc.cluster.local:${observability.collector.otlp.grpcPort}`,
collectorHttpEndpoint: `http://${observability.collector.serviceName}.${target.namespace}.svc.cluster.local:${observability.collector.otlp.httpPort}`,
backendGrpcEndpoint: `${observability.traceBackend.serviceName}.${target.namespace}.svc.cluster.local:${observability.traceBackend.otlp.grpcPort}`,
prometheusHttpEndpoint: metricsEnabledForTarget(observability, target) ? `http://${observability.metricsBackend.serviceName}.${target.namespace}.svc.cluster.local:${observability.metricsBackend.httpPort}` : null,
scrapeDiscovery: observability.metricsBackend.discovery,
},
instrumentation: observability.instrumentation.serviceConnections,
resourceAttributes: observability.resourceAttributes,
@@ -80,6 +88,7 @@ export function plan(options: CommonOptions): Record<string, unknown> {
status: `bun scripts/cli.ts platform-infra observability status --target ${target.id}`,
validate: `bun scripts/cli.ts platform-infra observability validate --target ${target.id}`,
trace: `bun scripts/cli.ts platform-infra observability trace --target ${target.id} --trace-id <traceId>`,
metricsQuery: `bun scripts/cli.ts platform-infra observability metrics-query --target ${target.id} --query <promql>`,
},
};
}
@@ -39,6 +39,12 @@ export function readObservabilityConfig(): ObservabilityConfig {
const traceBackend = objectField(root, "traceBackend", "");
const traceBackendOtlp = objectField(traceBackend, "otlp", "traceBackend");
const traceBackendStorage = objectField(traceBackend, "storage", "traceBackend");
const metricsBackend = objectField(root, "metricsBackend", "");
const metricsStorage = objectField(metricsBackend, "storage", "metricsBackend");
const metricsDiscovery = objectField(metricsBackend, "discovery", "metricsBackend");
const metricsLabelSelector = objectField(metricsDiscovery, "labelSelector", "metricsBackend.discovery");
const metricsAnnotations = objectField(metricsDiscovery, "annotationKeys", "metricsBackend.discovery");
const metricsQuery = objectField(metricsBackend, "query", "metricsBackend");
const sampling = objectField(root, "sampling", "");
const instrumentation = objectField(root, "instrumentation", "");
const resourceAttributes = objectField(root, "resourceAttributes", "");
@@ -56,6 +62,7 @@ export function readObservabilityConfig(): ObservabilityConfig {
images: {
collector: imageSpec(objectField(images, "collector", "images"), "images.collector"),
tempo: imageSpec(objectField(images, "tempo", "images"), "images.tempo"),
prometheus: imageSpec(objectField(images, "prometheus", "images"), "images.prometheus"),
},
targets: arrayOfRecords(root.targets, "targets").map(parseTarget),
collector: {
@@ -79,6 +86,46 @@ export function readObservabilityConfig(): ObservabilityConfig {
retention: stringField(traceBackendStorage, "retention", "traceBackend.storage"),
},
},
metricsBackend: {
type: enumField(metricsBackend, "type", "metricsBackend", ["prometheus"] as const),
enabled: booleanField(metricsBackend, "enabled", "metricsBackend"),
targetIds: stringArrayField(metricsBackend, "targetIds", "metricsBackend"),
deploymentName: kubernetesNameField(metricsBackend, "deploymentName", "metricsBackend"),
serviceName: kubernetesNameField(metricsBackend, "serviceName", "metricsBackend"),
configMapName: kubernetesNameField(metricsBackend, "configMapName", "metricsBackend"),
serviceAccountName: kubernetesNameField(metricsBackend, "serviceAccountName", "metricsBackend"),
clusterRoleName: kubernetesNameField(metricsBackend, "clusterRoleName", "metricsBackend"),
clusterRoleBindingName: kubernetesNameField(metricsBackend, "clusterRoleBindingName", "metricsBackend"),
persistentVolumeClaimName: kubernetesNameField(metricsBackend, "persistentVolumeClaimName", "metricsBackend"),
replicas: integerField(metricsBackend, "replicas", "metricsBackend"),
httpPort: portField(metricsBackend, "httpPort", "metricsBackend"),
storage: {
mode: enumField(metricsStorage, "mode", "metricsBackend.storage", ["persistentVolumeClaim"] as const),
size: stringField(metricsStorage, "size", "metricsBackend.storage"),
retention: stringField(metricsStorage, "retention", "metricsBackend.storage"),
},
discovery: {
namespaces: stringArrayField(metricsDiscovery, "namespaces", "metricsBackend.discovery"),
labelSelector: {
key: stringField(metricsLabelSelector, "key", "metricsBackend.discovery.labelSelector"),
value: stringField(metricsLabelSelector, "value", "metricsBackend.discovery.labelSelector"),
},
annotationKeys: {
scrape: stringField(metricsAnnotations, "scrape", "metricsBackend.discovery.annotationKeys"),
path: stringField(metricsAnnotations, "path", "metricsBackend.discovery.annotationKeys"),
port: stringField(metricsAnnotations, "port", "metricsBackend.discovery.annotationKeys"),
},
defaultPath: apiPathField(metricsDiscovery, "defaultPath", "metricsBackend.discovery"),
scrapeInterval: stringField(metricsDiscovery, "scrapeInterval", "metricsBackend.discovery"),
scrapeTimeout: stringField(metricsDiscovery, "scrapeTimeout", "metricsBackend.discovery"),
},
query: {
path: apiPathField(metricsQuery, "path", "metricsBackend.query"),
timeoutSeconds: integerField(metricsQuery, "timeoutSeconds", "metricsBackend.query"),
maxSeries: integerField(metricsQuery, "maxSeries", "metricsBackend.query"),
maxResponseBytes: integerField(metricsQuery, "maxResponseBytes", "metricsBackend.query"),
},
},
sampling: {
mode: enumField(sampling, "mode", "sampling", ["parentbased_traceidratio"] as const),
ratio: numberField(sampling, "ratio", "sampling"),
@@ -99,12 +146,37 @@ export function readObservabilityConfig(): ObservabilityConfig {
};
if (config.targets.length === 0) throw new Error(`${configLabel}.targets must not be empty`);
assertKnownEnabledTarget(config.targets, config.defaults.targetId, "defaults.targetId");
if (config.collector.replicas < 0 || config.traceBackend.replicas < 0) throw new Error(`${configLabel} replicas must be >= 0`);
for (const targetId of config.metricsBackend.targetIds) assertKnownEnabledTarget(config.targets, targetId, "metricsBackend.targetIds");
if (config.metricsBackend.enabled && config.metricsBackend.targetIds.length === 0) throw new Error(`${configLabel}.metricsBackend.targetIds must not be empty when metricsBackend.enabled is true`);
if (new Set(config.metricsBackend.targetIds.map((targetId) => targetId.toLowerCase())).size !== config.metricsBackend.targetIds.length) throw new Error(`${configLabel}.metricsBackend.targetIds must not contain duplicates`);
if (!isKubernetesQualifiedName(config.metricsBackend.discovery.labelSelector.key)) throw new Error(`${configLabel}.metricsBackend.discovery.labelSelector.key must be a Kubernetes qualified name`);
if (!isKubernetesLabelValue(config.metricsBackend.discovery.labelSelector.value)) throw new Error(`${configLabel}.metricsBackend.discovery.labelSelector.value must be a Kubernetes label value`);
if (config.collector.replicas < 0 || config.traceBackend.replicas < 0 || config.metricsBackend.replicas < 0) throw new Error(`${configLabel} replicas must be >= 0`);
if (config.metricsBackend.query.timeoutSeconds < 1 || config.metricsBackend.query.maxSeries < 1 || config.metricsBackend.query.maxResponseBytes < 1024) throw new Error(`${configLabel}.metricsBackend.query budgets must be positive and maxResponseBytes must be >= 1024`);
if (config.sampling.ratio < 0 || config.sampling.ratio > 1) throw new Error(`${configLabel}.sampling.ratio must be between 0 and 1`);
if (!config.probes.traceQueryPathTemplate.includes("{{traceId}}")) throw new Error(`${configLabel}.probes.traceQueryPathTemplate must include {{traceId}}`);
return config;
}
export function isKubernetesQualifiedName(value: string): boolean {
const slash = value.indexOf("/");
if (slash !== value.lastIndexOf("/")) return false;
const prefix = slash < 0 ? null : value.slice(0, slash);
const name = slash < 0 ? value : value.slice(slash + 1);
if (!isKubernetesNamePart(name)) return false;
if (prefix === null) return true;
if (prefix.length === 0 || prefix.length > 253) return false;
return prefix.split(".").every((segment) => segment.length <= 63 && /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/u.test(segment));
}
export function isKubernetesLabelValue(value: string): boolean {
return value.length <= 63 && (value === "" || isKubernetesNamePart(value));
}
function isKubernetesNamePart(value: string): boolean {
return value.length > 0 && value.length <= 63 && /^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?$/u.test(value);
}
export function imageSpec(record: Record<string, unknown>, path: string): ImageSpec {
const image = {
repository: stringField(record, "repository", path),
@@ -0,0 +1,12 @@
import type { ObservabilityConfig, ObservabilityTarget } from "./types";
export type MetricsTargetDisposition = "enabled" | "disabled" | "disabled-for-target";
export function metricsTargetDisposition(observability: ObservabilityConfig, target: ObservabilityTarget): MetricsTargetDisposition {
if (!observability.metricsBackend.enabled) return "disabled";
return observability.metricsBackend.targetIds.some((targetId) => targetId.toLowerCase() === target.id.toLowerCase()) ? "enabled" : "disabled-for-target";
}
export function metricsEnabledForTarget(observability: ObservabilityConfig, target: ObservabilityTarget): boolean {
return metricsTargetDisposition(observability, target) === "enabled";
}
@@ -19,18 +19,19 @@ import {
capture,
} from "../platform-infra-ops-library";
import type { ApplyOptions, CommonOptions, DiagnoseCodeAgentOptions, SearchOptions, TraceOptions } from "./types";
import type { ApplyOptions, CommonOptions, DiagnoseCodeAgentOptions, MetricsQueryOptions, SearchOptions, TraceOptions } from "./types";
import { apply, plan, status, validate } from "./actions";
import { diagnoseCodeAgent, search, trace } from "./render";
import { renderObservabilityPlan, renderObservabilityPlanFailure } from "./plan-render";
import { metricsQuery } from "./prometheus";
const observabilityOperations = ["plan", "apply", "status", "validate", "trace", "search", "diagnose-code-agent"] as const;
const observabilityOperations = ["plan", "apply", "status", "validate", "trace", "search", "diagnose-code-agent", "metrics-query"] as const;
export function observabilityHelp(scope: string | null = null): Record<string, unknown> {
const scoped = scope === null ? null : observabilityOperationHelp(scope);
if (scoped !== null) return scoped;
return {
command: "platform-infra observability plan|apply|status|validate|trace|search|diagnose-code-agent",
command: "platform-infra observability plan|apply|status|validate|trace|search|diagnose-code-agent|metrics-query",
output: "默认输出为有界摘要;--full/--raw 显式披露完整结构",
configTruth: "config/platform-infra/observability.yaml",
spec: "PJ2026-01060501 OTel追踪 draft-2026-06-19-p0",
@@ -39,7 +40,7 @@ export function observabilityHelp(scope: string | null = null): Record<string, u
help: `bun scripts/cli.ts platform-infra observability ${name} --help`,
})),
scopedHelp: "bun scripts/cli.ts platform-infra observability <operation> --help",
boundary: "Prometheus 仍是指标来源;本命令只负责 platform-infra OTel CollectorTempo readiness 与 trace 查询。",
boundary: "Collector + Tempo 是 trace authorityPrometheus 仅承担 metrics,故障和漂移保持非阻塞 warning。",
};
}
@@ -62,6 +63,7 @@ export async function runPlatformObservabilityCommand(config: UniDeskConfig, arg
if (action === "trace") return await trace(config, parseTraceOptions(args.slice(1)));
if (action === "search") return await search(config, parseSearchOptions(args.slice(1)));
if (action === "diagnose-code-agent") return await diagnoseCodeAgent(config, parseDiagnoseCodeAgentOptions(args.slice(1)));
if (action === "metrics-query") return await metricsQuery(config, parseMetricsQueryOptions(args.slice(1)));
return { ok: false, error: "unsupported-platform-infra-observability-command", args, help: observabilityHelp() };
}
@@ -105,6 +107,12 @@ function observabilityOperationHelp(scope: string): Record<string, unknown> | nu
],
options: ["--trace-id <32-hex>", "--target <NODE>", "--grep <text>", "--limit <1..500>", "--full", "--raw"],
};
if (scope === "metrics-query") return {
...common,
command: "platform-infra observability metrics-query",
usage: ["bun scripts/cli.ts platform-infra observability metrics-query --target <NODE> --query <promql> [--full|--raw]"],
options: ["--target <NODE>", "--query <promql>", "--full", "--raw"],
};
if (scope === "search") return {
...common,
command: "platform-infra observability search",
@@ -126,6 +134,27 @@ function observabilityOperationHelp(scope: string): Record<string, unknown> | nu
return null;
}
export function parseMetricsQueryOptions(args: string[]): MetricsQueryOptions {
const commonArgs: string[] = [];
let query: string | null = null;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === "--query") {
const value = args[index + 1];
if (value === undefined || value.startsWith("--")) throw new Error("--query requires a value");
query = value;
index += 1;
} else {
commonArgs.push(arg);
if (arg === "--target") {
commonArgs.push(args[index + 1] ?? "");
index += 1;
}
}
}
return { ...parseCommonOptions(commonArgs), query };
}
export function parseCommonOptions(args: string[]): CommonOptions {
let targetId: string | null = null;
let full = false;
@@ -8,7 +8,9 @@ export function renderObservabilityPlan(result: Record<string, unknown>): Render
const images = record(config.images);
const collector = record(config.collector);
const traceBackend = record(config.traceBackend);
const metricsBackend = record(config.metricsBackend);
const renderPlan = record(result.renderPlan);
const metricsPlan = record(renderPlan.metrics);
const otlp = record(renderPlan.otlp);
const connections = records(config.serviceConnections);
const objects = records(renderPlan.objects);
@@ -39,6 +41,8 @@ export function renderObservabilityPlan(result: Record<string, unknown>): Render
`target=${textValue(target.id)} route=${textValue(target.route)} namespace=${textValue(target.namespace)} role=${textValue(target.role)}`,
`collector=${textValue(collector.serviceName)} replicas=${textValue(collector.replicas)} image=${textValue(images.collector)}`,
`tempo=${textValue(traceBackend.serviceName)} replicas=${textValue(traceBackend.replicas)} retention=${textValue(record(traceBackend.storage).retention)} image=${textValue(images.traceBackend)}`,
`prometheus=${textValue(metricsPlan.disposition)} service=${textValue(metricsBackend.serviceName)} targets=${values(metricsPlan.targetIds).join(",")} replicas=${textValue(metricsBackend.replicas)} retention=${textValue(record(metricsBackend.storage).retention)} image=${textValue(images.metricsBackend)}`,
`scrapeSelector=${textValue(record(record(metricsBackend.discovery).labelSelector).key)}=${textValue(record(record(metricsBackend.discovery).labelSelector).value)} namespaces=${values(record(metricsBackend.discovery).namespaces).length === 0 ? "all" : values(record(metricsBackend.discovery).namespaces).join(",")} queryBudget=${textValue(record(metricsBackend.query).timeoutSeconds)}s/${textValue(record(metricsBackend.query).maxSeries)}series/${textValue(record(metricsBackend.query).maxResponseBytes)}bytes`,
`serviceConnections=${connections.length} services=${serviceNames.size} nodes=${nodes.size} configRefs=${presentConfigRefs.length}/${configRefs.length} missing=${missingConfigRefs.length} objects=${objects.length}`,
"",
"Service connections:",
@@ -77,6 +81,7 @@ export function renderObservabilityPlan(result: Record<string, unknown>): Render
` collector-grpc: ${textValue(otlp.collectorGrpcEndpoint)}`,
` collector-http: ${textValue(otlp.collectorHttpEndpoint)}`,
` tempo-grpc: ${textValue(otlp.backendGrpcEndpoint)}`,
` prometheus-http: ${textValue(otlp.prometheusHttpEndpoint)}`,
"",
"Next:",
` bun scripts/cli.ts platform-infra observability plan --target ${textValue(target.id)} --full`,
@@ -0,0 +1,53 @@
import type { UniDeskConfig } from "../config";
import type { RenderedCliResult } from "../output";
import { capture, compactCapture, parseJsonOutput, redactSensitiveUnknown, shQuote } from "../platform-infra-ops-library";
import { resolveTarget } from "./actions";
import { readObservabilityConfig } from "./config";
import { formatTable, shortenEnd, textValue } from "./manifest";
import { imageReference, indent, targetSummary } from "./summary";
import type { MetricsQueryOptions, ObservabilityConfig, ObservabilityTarget } from "./types";
import { metricsEnabledForTarget, metricsTargetDisposition } from "./metrics-target";
export function prometheusManifests(observability: ObservabilityConfig, target: ObservabilityTarget): string[] {
const prometheus = observability.metricsBackend;
if (!metricsEnabledForTarget(observability, target)) return [];
const namespaceNames = prometheus.discovery.namespaces.length > 0
? `\n namespaces:\n names:\n${prometheus.discovery.namespaces.map((name) => ` - ${name}`).join("\n")}`
: "";
const config = `global:\n scrape_interval: ${prometheus.discovery.scrapeInterval}\n scrape_timeout: ${prometheus.discovery.scrapeTimeout}\nscrape_configs:\n - job_name: kubernetes-pods\n kubernetes_sd_configs:\n - role: pod${namespaceNames}\n relabel_configs:\n - source_labels: [__meta_kubernetes_pod_annotation_${prometheusMetaLabel(prometheus.discovery.annotationKeys.scrape)}]\n action: keep\n regex: \"true\"\n - source_labels: [__meta_kubernetes_pod_label_${prometheusMetaLabel(prometheus.discovery.labelSelector.key)}]\n action: keep\n regex: '${re2Literal(prometheus.discovery.labelSelector.value)}'\n - source_labels: [__meta_kubernetes_pod_annotation_${prometheusMetaLabel(prometheus.discovery.annotationKeys.path)}]\n action: replace\n target_label: __metrics_path__\n regex: (/.+)\n - source_labels: [__address__, __meta_kubernetes_pod_annotation_${prometheusMetaLabel(prometheus.discovery.annotationKeys.port)}]\n action: replace\n regex: ([^:]+)(?::\\d+)?;(\\d+)\n replacement: \$1:\$2\n target_label: __address__\n - source_labels: [__meta_kubernetes_pod_annotation_${prometheusMetaLabel(prometheus.discovery.annotationKeys.path)}]\n action: replace\n target_label: __metrics_path__\n regex: ^$\n replacement: ${prometheus.discovery.defaultPath}\n`;
const labels = ` app.kubernetes.io/name: ${prometheus.deploymentName}\n app.kubernetes.io/component: metrics-backend\n app.kubernetes.io/part-of: platform-infra\n app.kubernetes.io/managed-by: unidesk`;
return [
`apiVersion: v1\nkind: ServiceAccount\nmetadata:\n name: ${prometheus.serviceAccountName}\n namespace: ${target.namespace}\n labels:\n${labels}\n`,
`apiVersion: rbac.authorization.k8s.io/v1\nkind: ClusterRole\nmetadata:\n name: ${prometheus.clusterRoleName}\nrules:\n - apiGroups: [\"\"]\n resources: [\"nodes\", \"nodes/proxy\", \"services\", \"endpoints\", \"pods\"]\n verbs: [\"get\", \"list\", \"watch\"]\n`,
`apiVersion: rbac.authorization.k8s.io/v1\nkind: ClusterRoleBinding\nmetadata:\n name: ${prometheus.clusterRoleBindingName}\nroleRef:\n apiGroup: rbac.authorization.k8s.io\n kind: ClusterRole\n name: ${prometheus.clusterRoleName}\nsubjects:\n - kind: ServiceAccount\n name: ${prometheus.serviceAccountName}\n namespace: ${target.namespace}\n`,
`apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: ${prometheus.configMapName}\n namespace: ${target.namespace}\n labels:\n${labels}\ndata:\n prometheus.yml: |\n${indent(config, 4)}\n`,
`apiVersion: v1\nkind: PersistentVolumeClaim\nmetadata:\n name: ${prometheus.persistentVolumeClaimName}\n namespace: ${target.namespace}\n labels:\n${labels}\nspec:\n accessModes: [ReadWriteOnce]\n resources:\n requests:\n storage: ${prometheus.storage.size}\n`,
`apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: ${prometheus.deploymentName}\n namespace: ${target.namespace}\n labels:\n${labels}\nspec:\n replicas: ${prometheus.replicas}\n selector:\n matchLabels:\n app.kubernetes.io/name: ${prometheus.deploymentName}\n app.kubernetes.io/component: metrics-backend\n template:\n metadata:\n labels:\n app.kubernetes.io/name: ${prometheus.deploymentName}\n app.kubernetes.io/component: metrics-backend\n app.kubernetes.io/part-of: platform-infra\n spec:\n serviceAccountName: ${prometheus.serviceAccountName}\n containers:\n - name: prometheus\n image: ${imageReference(observability.images.prometheus)}\n imagePullPolicy: ${observability.images.prometheus.pullPolicy}\n args:\n - --config.file=/etc/prometheus/prometheus.yml\n - --storage.tsdb.path=/prometheus\n - --storage.tsdb.retention.time=${prometheus.storage.retention}\n ports:\n - name: http\n containerPort: ${prometheus.httpPort}\n readinessProbe:\n httpGet:\n path: /-/ready\n port: http\n volumeMounts:\n - name: config\n mountPath: /etc/prometheus/prometheus.yml\n subPath: prometheus.yml\n readOnly: true\n - name: data\n mountPath: /prometheus\n volumes:\n - name: config\n configMap:\n name: ${prometheus.configMapName}\n - name: data\n persistentVolumeClaim:\n claimName: ${prometheus.persistentVolumeClaimName}\n`,
`apiVersion: v1\nkind: Service\nmetadata:\n name: ${prometheus.serviceName}\n namespace: ${target.namespace}\n labels:\n${labels}\nspec:\n type: ClusterIP\n selector:\n app.kubernetes.io/name: ${prometheus.deploymentName}\n app.kubernetes.io/component: metrics-backend\n ports:\n - name: http\n port: ${prometheus.httpPort}\n targetPort: http\n`,
];
}
export async function metricsQuery(config: UniDeskConfig, options: MetricsQueryOptions): Promise<Record<string, unknown> | RenderedCliResult> {
if (options.query === null || options.query.trim() === "") throw new Error("observability metrics-query requires --query <promql>");
const observability = readObservabilityConfig();
const target = resolveTarget(observability, options.targetId);
const prometheus = observability.metricsBackend;
const disposition = metricsTargetDisposition(observability, target);
if (disposition !== "enabled") return { ok: false, action: "platform-infra-observability-metrics-query", mutation: false, target: targetSummary(target), metrics: { enabled: false, disposition, targetIds: prometheus.targetIds }, warnings: [`Prometheus is ${disposition} in config/platform-infra/observability.yaml`] };
const path = `${prometheus.query.path}?query=${encodeURIComponent(options.query)}`;
const script = `set -u\npython3 - <<'PY'\nimport json, subprocess\npath = ${JSON.stringify(`/api/v1/namespaces/${target.namespace}/services/http:${prometheus.serviceName}:http/proxy${path}`)}\nproc = subprocess.run([\"kubectl\", \"get\", \"--raw\", path], text=True, capture_output=True, timeout=${prometheus.query.timeoutSeconds})\nbody = proc.stdout[:${prometheus.query.maxResponseBytes}]\ntry:\n parsed = json.loads(body) if body else None\nexcept Exception:\n parsed = None\nresult = parsed.get(\"data\", {}).get(\"result\", []) if isinstance(parsed, dict) else []\nprint(json.dumps({\"ok\": proc.returncode == 0 and isinstance(parsed, dict) and parsed.get(\"status\") == \"success\", \"exitCode\": proc.returncode, \"truncated\": len(proc.stdout) > ${prometheus.query.maxResponseBytes} or len(result) > ${prometheus.query.maxSeries}, \"resultType\": parsed.get(\"data\", {}).get(\"resultType\") if isinstance(parsed, dict) else None, \"result\": result[:${prometheus.query.maxSeries}], \"stderrTail\": proc.stderr[-2000:]}, ensure_ascii=False))\nPY`;
const result = await capture(config, target.route, ["sh"], script);
const parsed = parseJsonOutput(result.stdout);
const ok = result.exitCode === 0 && parsed?.ok === true;
if (options.full || options.raw) return { ok, action: "platform-infra-observability-metrics-query", mutation: false, target: targetSummary(target), query: options.query, budgets: prometheus.query, result: parsed === null ? compactCapture(result, { full: true }) : redactSensitiveUnknown(parsed) };
const rows = Array.isArray(parsed?.result) ? parsed.result.slice(0, 20).map((item: unknown) => [shortenEnd(JSON.stringify(item), 160)]) : [];
return { ok, command: "platform-infra observability metrics-query", contentType: "text/plain", renderedText: [`platform-infra observability metrics-query (${ok ? "ok" : "not-ok"})`, "", `target=${target.id} prometheus=${prometheus.serviceName}.${target.namespace}.svc.cluster.local:${prometheus.httpPort}`, `query=${options.query}`, `budget=timeout:${prometheus.query.timeoutSeconds}s series:${prometheus.query.maxSeries} bytes:${prometheus.query.maxResponseBytes} truncated=${textValue(parsed?.truncated)}`, "", formatTable(["RESULT"], rows.length > 0 ? rows : [["-"]]), "", `Next: bun scripts/cli.ts platform-infra observability metrics-query --target ${target.id} --query ${shQuote(options.query)} --full`].join("\n") };
}
export function prometheusMetaLabel(value: string): string {
return value.replace(/[^A-Za-z0-9_]/gu, "_");
}
export function re2Literal(value: string): string {
return `^${value.replace(/[\\.^$|?*+()[\]{}]/gu, "\\$&")}$`;
}
@@ -21,6 +21,7 @@ import {
import type { ObservabilityConfig, ObservabilityTarget } from "./types";
import { fieldManager } from "./types";
import { metricsEnabledForTarget, metricsTargetDisposition } from "./metrics-target";
export function tempoService(observability: ObservabilityConfig, target: ObservabilityTarget): string {
return `apiVersion: v1
@@ -102,7 +103,15 @@ PY
}
export function statusScript(observability: ObservabilityConfig, target: ObservabilityTarget, full: boolean, generateValidationTrace = false): string {
const endpointsJson = JSON.stringify(observability.probes.statusEndpoints);
const metricsEnabled = metricsEnabledForTarget(observability, target);
const metricsDisposition = metricsTargetDisposition(observability, target);
const enabledEndpoints = metricsEnabled
? observability.probes.statusEndpoints
: observability.probes.statusEndpoints.filter((endpoint) => endpoint.service !== observability.metricsBackend.serviceName);
const endpointsJson = JSON.stringify(enabledEndpoints);
const metricsCaptures = metricsEnabled
? `capture_json metrics_deployments kubectl -n ${shQuote(target.namespace)} get deployment ${shQuote(observability.metricsBackend.deploymentName)} -o json\ncapture_json metrics_services kubectl -n ${shQuote(target.namespace)} get service ${shQuote(observability.metricsBackend.serviceName)} -o json\ncapture_json metrics_pods kubectl -n ${shQuote(target.namespace)} get pods -l ${shQuote(`app.kubernetes.io/name=${observability.metricsBackend.deploymentName}`)} -o json`
: "";
return `
set -u
tmp="$(mktemp -d)"
@@ -123,6 +132,7 @@ capture_json namespace kubectl get namespace ${shQuote(target.namespace)} -o jso
capture_json deployments kubectl -n ${shQuote(target.namespace)} get deployment ${shQuote(observability.collector.deploymentName)} ${shQuote(observability.traceBackend.deploymentName)} -o json
capture_json services kubectl -n ${shQuote(target.namespace)} get service ${shQuote(observability.collector.serviceName)} ${shQuote(observability.traceBackend.serviceName)} -o json
capture_json pods kubectl -n ${shQuote(target.namespace)} get pods -l ${shQuote(`app.kubernetes.io/name in (${observability.collector.deploymentName},${observability.traceBackend.deploymentName})`)} -o json
${metricsCaptures}
capture_json events kubectl -n ${shQuote(target.namespace)} get events -o json
python3 - "$tmp" '${endpointsJson.replaceAll("'", "'\"'\"'")}' <<'PY'
import json, random, socket, subprocess, sys, time, urllib.error, urllib.request
@@ -304,7 +314,9 @@ def generate_test_trace():
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=3)
runtime_ready = rc("namespace") == 0 and rc("deployments") == 0 and rc("services") == 0 and all(item["ok"] for item in probe_results)
blocking_probes = [item for item in probe_results if item.get("service") != "${observability.metricsBackend.serviceName}"]
metrics_probes = [item for item in probe_results if item.get("service") == "${observability.metricsBackend.serviceName}"]
runtime_ready = rc("namespace") == 0 and rc("deployments") == 0 and rc("services") == 0 and all(item["ok"] for item in blocking_probes)
validation_trace = generate_test_trace() if (${generateValidationTrace ? "True" : "False"} and runtime_ready) else None
payload = {
"ok": runtime_ready and (validation_trace is None or validation_trace.get("ok") is True),
@@ -316,8 +328,12 @@ payload = {
"services": compact_section("services"),
"pods": compact_section("pods"),
"events": compact_section("events"),
"metricsDeployments": compact_section("metrics_deployments") if ${metricsEnabled ? "True" : "False"} else None,
"metricsServices": compact_section("metrics_services") if ${metricsEnabled ? "True" : "False"} else None,
"metricsPods": compact_section("metrics_pods") if ${metricsEnabled ? "True" : "False"} else None,
},
"probes": probe_results,
"warnings": ([{"code": "prometheus-not-ready", "detail": item} for item in metrics_probes if item.get("ok") is not True] if ${metricsEnabled ? "True" : "False"} else [{"code": "prometheus-${metricsDisposition}"}]),
"validationTrace": validation_trace,
}
print(json.dumps(payload, ensure_ascii=False, indent=2 if ${full ? "True" : "False"} else None))
@@ -32,9 +32,11 @@ export function configSummary(observability: ObservabilityConfig, target: Observ
images: {
collector: imageReference(observability.images.collector),
traceBackend: imageReference(observability.images.tempo),
metricsBackend: imageReference(observability.images.prometheus),
},
collector: observability.collector,
traceBackend: observability.traceBackend,
metricsBackend: observability.metricsBackend,
sampling: observability.sampling,
serviceConnections: observability.instrumentation.serviceConnections.map((item) => ({
serviceName: item.serviceName,
@@ -62,8 +64,8 @@ export function targetSummary(target: ObservabilityTarget): Record<string, unkno
export function policyChecks(yaml: string, target: ObservabilityTarget): Array<Record<string, unknown>> {
return [
{ name: "yaml-source-of-truth", ok: true, detail: "All concrete images, routes, namespace, ports, retention and sampling values are read from config/platform-infra/observability.yaml." },
{ name: "clusterip-only", ok: !/^\s*type:\s*(NodePort|LoadBalancer)\s*$/mu.test(yaml), detail: "Collector and trace backend stay ClusterIP-only." },
{ name: "yaml-source-of-truth", ok: true, detail: "All concrete images, routes, namespace, ports, retention, storage, scrape discovery and query budgets are read from config/platform-infra/observability.yaml." },
{ name: "clusterip-only", ok: !/^\s*type:\s*(NodePort|LoadBalancer)\s*$/mu.test(yaml), detail: "Collector, Tempo and Prometheus stay ClusterIP-only." },
{ name: "no-ingress", ok: !/^\s*kind:\s*Ingress\s*$/mu.test(yaml), detail: "No public ingress is rendered for the first tracing backend." },
{ name: "no-host-network", ok: !/^\s*hostNetwork:\s*true\s*$/mu.test(yaml), detail: "Pods must not use host network." },
{ name: "allow-all-network-policy", ok: yaml.includes("kind: NetworkPolicy") && yaml.includes("name: allow-all") && yaml.includes(`namespace: ${target.namespace}`), detail: `NetworkPolicy/allow-all is rendered in ${target.namespace}.` },
@@ -76,6 +78,9 @@ export function statusSummary(payload: Record<string, unknown>): Record<string,
const services = objectList(sectionJson(sections, "services"));
const pods = objectList(sectionJson(sections, "pods"));
const events = objectList(sectionJson(sections, "events"));
const metricsDeployments = objectList(optionalSectionJson(sections, "metricsDeployments"));
const metricsServices = objectList(optionalSectionJson(sections, "metricsServices"));
const metricsPods = objectList(optionalSectionJson(sections, "metricsPods"));
const podEvents = latestPodEvents(events);
const probes = Array.isArray(payload.probes) ? payload.probes as Array<Record<string, unknown>> : [];
const readyDeployments = deployments.map((item) => deploymentSummary(item));
@@ -92,6 +97,12 @@ export function statusSummary(payload: Record<string, unknown>): Record<string,
path: item.path,
stderrTail: item.ok === true ? "" : item.stderrTail,
})),
warnings: Array.isArray(payload.warnings) ? payload.warnings : [],
metrics: {
deployments: metricsDeployments.map((item) => deploymentSummary(item)),
services: metricsServices.map((item) => metadataName(item)),
pods: metricsPods.map((item) => podSummary(item, podEvents)),
},
};
}
@@ -106,6 +117,12 @@ export function sectionJson(sections: Record<string, unknown>, name: string): un
return section.json;
}
function optionalSectionJson(sections: Record<string, unknown>, name: string): unknown {
const value = sections[name];
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
return (value as Record<string, unknown>).json;
}
export function objectList(value: unknown): Record<string, unknown>[] {
if (typeof value !== "object" || value === null) return [];
const items = (value as Record<string, unknown>).items;
@@ -22,6 +22,7 @@ import {
import type { ObservabilityConfig, ObservabilityTarget } from "./types";
import { tempoService } from "./search-script";
import { imageReference, indent } from "./summary";
import { prometheusManifests } from "./prometheus";
export function asArray(value: unknown): unknown[] {
return Array.isArray(value) ? value : [];
@@ -43,6 +44,7 @@ export function renderManifest(observability: ObservabilityConfig, target: Obser
tempoConfigMap(observability, target),
tempoDeployment(observability, target, tempoImage),
tempoService(observability, target),
...prometheusManifests(observability, target),
].filter((item) => item.trim().length > 0).join("\n---\n");
}
@@ -51,6 +51,7 @@ export interface ObservabilityConfig {
images: {
collector: ImageSpec;
tempo: ImageSpec;
prometheus: ImageSpec;
};
targets: ObservabilityTarget[];
collector: {
@@ -71,6 +72,7 @@ export interface ObservabilityConfig {
otlp: OtlpPorts;
storage: { mode: "emptyDir"; retention: string };
};
metricsBackend: PrometheusConfig;
sampling: { mode: "parentbased_traceidratio"; ratio: number };
instrumentation: {
contextPropagation: string[];
@@ -87,6 +89,31 @@ export interface ObservabilityConfig {
};
}
export interface PrometheusConfig {
type: "prometheus";
enabled: boolean;
targetIds: string[];
deploymentName: string;
serviceName: string;
configMapName: string;
serviceAccountName: string;
clusterRoleName: string;
clusterRoleBindingName: string;
persistentVolumeClaimName: string;
replicas: number;
httpPort: number;
storage: { mode: "persistentVolumeClaim"; size: string; retention: string };
discovery: {
namespaces: string[];
labelSelector: { key: string; value: string };
annotationKeys: { scrape: string; path: string; port: string };
defaultPath: string;
scrapeInterval: string;
scrapeTimeout: string;
};
query: { path: string; timeoutSeconds: number; maxSeries: number; maxResponseBytes: number };
}
export interface ImageSpec {
repository: string;
tag: string;
@@ -165,3 +192,7 @@ export interface DiagnoseCodeAgentOptions extends CommonOptions {
candidateLimit: number;
lookbackMinutes: number;
}
export interface MetricsQueryOptions extends CommonOptions {
query: string | null;
}
@@ -0,0 +1,108 @@
import { resolveToken } from "./gh/auth-and-safety";
import { githubRequest, isGitHubError } from "./gh/client";
export interface PacDeliveryTimingBinding {
target: Record<string, unknown>;
consumer: {
id: string;
node: string;
lane: string;
repository: string;
branch: string;
pipeline: string;
};
}
interface PacDeliveryTimingObservers {
history: () => Promise<Record<string, unknown>>;
status: () => Promise<Record<string, unknown>>;
}
function record(value: unknown): Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value) ? value as Record<string, unknown> : {};
}
function records(value: unknown): Record<string, unknown>[] {
return Array.isArray(value) ? value.map(record) : [];
}
function durationBetween(start: unknown, end: unknown): number | null {
if (typeof start !== "string" || typeof end !== "string") return null;
const a = Date.parse(start);
const b = Date.parse(end);
if (!Number.isFinite(a) || !Number.isFinite(b)) return null;
return Math.max(0, Math.round((b - a) / 1000));
}
export async function observePacDeliveryTiming(
binding: PacDeliveryTimingBinding,
observers: PacDeliveryTimingObservers,
): Promise<Record<string, unknown>> {
const [sourceOwner, sourceRepo] = binding.consumer.repository.split("/");
if (!sourceOwner || !sourceRepo) throw new Error(`invalid owning YAML source repository ${binding.consumer.repository}`);
const [historyResult, statusResult] = await Promise.all([observers.history(), observers.status()]);
const row = records(historyResult.rows)[0] ?? {};
const statusSummary = record(statusResult.summary);
const latest = record(statusSummary.latestPipelineRun);
const commit = typeof row.commit === "string" ? row.commit : typeof latest.sourceCommit === "string" ? latest.sourceCommit : null;
const evidenceGaps: string[] = [];
let pullRequest: Record<string, unknown> | null = null;
let githubEvidence: Record<string, unknown> = { repository: binding.consumer.repository, source: "github-commit-pulls", mutation: false };
if (commit === null) {
evidenceGaps.push("source-commit-missing");
} else {
const tokenInfo = resolveToken(binding.consumer.repository, false);
if (tokenInfo.token === null) {
evidenceGaps.push("github-token-unavailable");
githubEvidence = { ...githubEvidence, ok: false, token: tokenInfo.probe };
} else {
const response = await githubRequest<unknown>(tokenInfo.token, "GET", `/repos/${sourceOwner}/${sourceRepo}/commits/${commit}/pulls`);
if (isGitHubError(response)) {
evidenceGaps.push("github-pr-lookup-failed");
githubEvidence = { ...githubEvidence, ok: false, error: response };
} else {
const prs = Array.isArray(response) ? response : [];
const merged = prs.find((item) => record(item).merged_at !== null && record(item).merged_at !== undefined) ?? prs[0];
if (merged === undefined) {
evidenceGaps.push("github-pr-not-found");
} else {
const item = record(merged);
pullRequest = { number: item.number ?? null, title: item.title ?? null, url: item.html_url ?? null, mergedAt: item.merged_at ?? null, mergeCommit: commit };
if (typeof item.merged_at !== "string") evidenceGaps.push("github-mergedAt-missing");
}
githubEvidence = { ...githubEvidence, ok: true, count: prs.length, token: tokenInfo.probe };
}
}
}
const mergedAt = pullRequest?.mergedAt ?? null;
const startTime = row.startTime ?? null;
const completionTime = row.completionTime ?? null;
const argo = record(statusSummary.argo);
const runtime = record(statusSummary.runtime);
const provenance = record(statusSummary.provenance);
if (mergedAt === null) evidenceGaps.push("mergedAt-missing");
if (startTime === null || completionTime === null) evidenceGaps.push("pipeline-timestamps-missing");
if (argo.sync === undefined || argo.health === undefined) evidenceGaps.push("argo-closeout-missing");
if (runtime.readyReplicas === undefined) evidenceGaps.push("runtime-health-missing");
if (provenance.relation === "unknown") evidenceGaps.push("runtime-provenance-unknown");
return {
ok: commit !== null && startTime !== null && completionTime !== null,
action: "platform-infra-pipelines-as-code-delivery-timing",
mutation: false,
state: evidenceGaps.length === 0 ? "complete" : "partial",
target: binding.target,
consumer: binding.consumer,
source: { commit, pullRequest, github: githubEvidence },
timing: {
mergedAt,
pipelineStart: startTime,
pipelineCompletion: completionTime,
triggerWaitSeconds: durationBetween(mergedAt, startTime),
pipelineDurationSeconds: row.durationSeconds ?? durationBetween(startTime, completionTime),
endToEndSeconds: durationBetween(mergedAt, completionTime),
},
pipeline: { id: row.id ?? latest.name ?? null, status: row.status ?? latest.status ?? null, taskRuns: row.taskRuns ?? statusSummary.taskRuns ?? null },
runtime: { argo, health: runtime, provenance },
evidenceGaps: [...new Set(evidenceGaps)],
};
}
@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import type { GiteaConfig, GiteaMirrorRepository } from "./platform-infra-gitea-config";
import { renderMirrorBootstrap } from "./platform-infra-gitea-render";
import { resolvePacBootstrapMirrorRepository } from "./platform-infra-pipelines-as-code-bootstrap";
import { pacBootstrapYamlMatchFailure, projectPacBootstrapResult, renderPacBootstrap, resolvePacBootstrapMirrorRepository } from "./platform-infra-pipelines-as-code-bootstrap";
import { runPlatformInfraPipelinesAsCodeCommand } from "./platform-infra-pipelines-as-code";
const mirror: GiteaMirrorRepository = {
@@ -78,3 +78,64 @@ test("platform bootstrap help advertises the composite entry before low-level ap
expect(usage[0]).toContain("pipelines-as-code bootstrap");
expect(usage).toContain("bun scripts/cli.ts platform-infra pipelines-as-code bootstrap --target <NODE> --consumer <consumer> --confirm");
});
test("PaC bootstrap projects typed stages and compact JSON", () => {
const result = {
ok: true,
action: "platform-infra-pipelines-as-code-bootstrap",
mutation: false,
mode: "dry-run",
target: { id: "NC01" },
consumer: { id: "widgets", node: "NC01", lane: "v01" },
repository: { id: "widgets", namespace: "ci" },
mirrorRepository: { key: mirror.key, upstreamRepository: mirror.upstream.repository, upstreamBranch: mirror.upstream.branch, owner: mirror.gitea.owner, repo: mirror.gitea.name },
githubPreflight: { ok: true, mutation: false, repositories: [{ key: mirror.key, code: "github-upstream-available", ok: true }] },
giteaBootstrap: { ok: true, mutation: false, mode: "dry-run", blockers: [] },
pipelinesAsCodeApply: { ok: true, mutation: false, mode: "dry-run", remote: { ok: true, preflight: { repositoryExists: true } } },
};
const projection = projectPacBootstrapResult(result);
expect(projection.ok).toBe(true);
expect(JSON.stringify(projection)).not.toContain("pipelinesAsCodeApply");
expect((projection.stages as Record<string, unknown>[]).map((item) => item.id)).toEqual(["gitea-init", "pac-controller", "tekton-consumer", "gitops-argo"]);
const rendered = renderPacBootstrap(result).renderedText;
expect(rendered).toContain("PRECONDITIONS");
expect(rendered).toContain("yaml-repository-exact-match");
expect(rendered).toContain("confirm:");
});
test("PaC bootstrap returns fix-yaml next for zero YAML matches", () => {
const result = pacBootstrapYamlMatchFailure("NC01", "widgets", new Error("cannot resolve repository: no exact match"));
expect(projectPacBootstrapResult(result)).toBe(result);
expect((result.blockers as Record<string, unknown>[])[0]?.code).toBe("yaml-repository-no-match");
expect(result.next).toEqual({
kind: "fix-yaml",
command: "检查 config/platform-infra/gitea.yaml 与 config/platform-infra/pipelines-as-code.yaml 的 target、owner/name 和 read URL 精确匹配",
});
});
test("PaC bootstrap returns fix-yaml next for multiple YAML matches", () => {
const result = pacBootstrapYamlMatchFailure("NC01", "widgets", new Error("cannot resolve repository: 2 exact matches"));
expect(result.mutation).toBe(false);
expect((result.blockers as Record<string, unknown>[])[0]?.code).toBe("yaml-repository-multiple-matches");
expect((result.next as Record<string, unknown>).kind).toBe("fix-yaml");
});
test("PaC bootstrap distinguishes GitHub access failure from Gitea and PaC stages", () => {
const projection = projectPacBootstrapResult({
ok: false,
action: "platform-infra-pipelines-as-code-bootstrap",
mutation: false,
mode: "dry-run",
target: { id: "NC01" },
consumer: { id: "widgets", node: "NC01", lane: "v01" },
repository: { id: "widgets", namespace: "ci" },
mirrorRepository: { key: mirror.key, upstreamRepository: mirror.upstream.repository, upstreamBranch: mirror.upstream.branch, owner: mirror.gitea.owner, repo: mirror.gitea.name },
githubPreflight: { ok: false, mutation: false, repositories: [{ key: mirror.key, code: "github-repository-not-found-or-no-access", errorTail: "repository unavailable" }] },
giteaBootstrap: { ok: false, mutation: false, mode: "skipped", githubPreflight: { ok: false, repositories: [{ code: "github-repository-not-found-or-no-access", errorTail: "repository unavailable" }] } },
pipelinesAsCodeApply: { ok: false, mutation: false, mode: "skipped" },
});
expect((projection.blockers as Record<string, unknown>[])).toEqual([{ code: "github-repository-not-found-or-no-access", stage: "github-upstream", reason: "repository unavailable" }]);
expect((projection.preconditions as Record<string, unknown>[])[2]).toMatchObject({ ok: null, code: "gitea-bootstrap-skipped" });
expect((projection.stages as Record<string, unknown>[]).map((item) => item.state)).toEqual(["skipped", "skipped", "skipped", "skipped"]);
expect((projection.next as Record<string, unknown>).kind).toBe("read-only-status");
});
@@ -1,4 +1,5 @@
import type { GiteaConfig, GiteaMirrorRepository } from "./platform-infra-gitea-config";
import type { RenderedCliResult } from "./output";
export interface PacBootstrapRepositoryIdentity {
readonly id: string;
@@ -28,6 +29,181 @@ export function resolvePacBootstrapMirrorRepository(
return candidates[0];
}
export function pacBootstrapYamlMatchFailure(
targetId: string,
consumerId: string,
error: unknown,
): Record<string, unknown> {
const reason = error instanceof Error ? error.message : String(error);
const matchCount = reason.includes("no exact match") ? 0 : Number.parseInt(reason.match(/(\d+) exact matches/u)?.[1] ?? "-1", 10);
return {
ok: false,
action: "platform-infra-pipelines-as-code-bootstrap",
mutation: false,
mode: "precondition-failed",
target: { id: targetId },
consumer: { id: consumerId },
preconditions: [
{ id: "yaml-exact-match", ok: false, code: matchCount === 0 ? "yaml-repository-no-match" : "yaml-repository-multiple-matches", matchCount, detail: reason },
],
stages: [],
blockers: [{ code: matchCount === 0 ? "yaml-repository-no-match" : "yaml-repository-multiple-matches", stage: "yaml-exact-match", reason }],
next: {
kind: "fix-yaml",
command: "检查 config/platform-infra/gitea.yaml 与 config/platform-infra/pipelines-as-code.yaml 的 target、owner/name 和 read URL 精确匹配",
},
valuesPrinted: false,
};
}
export function projectPacBootstrapResult(result: Record<string, unknown>): Record<string, unknown> {
if (Array.isArray(result.preconditions) && Array.isArray(result.stages) && Array.isArray(result.blockers)) return result;
const target = record(result.target);
const consumer = record(result.consumer);
const repository = record(result.repository);
const mirror = record(result.mirrorRepository);
const githubPreflight = record(result.githubPreflight);
const githubRepository = records(githubPreflight.repositories)[0] ?? {};
const gitea = record(result.giteaBootstrap);
const apply = record(result.pipelinesAsCodeApply);
const remote = record(apply.remote);
const blockers = bootstrapBlockers(githubPreflight, gitea, apply, remote);
const dryRun = result.mode === "dry-run";
const repositoryMissing = remote.error === "gitea-repository-missing";
const githubReady = githubPreflight.ok === true;
const giteaReady = gitea.ok === true;
const giteaSkipped = gitea.mode === "skipped";
const applySkipped = apply.mode === "skipped";
const applyReady = apply.ok === true || (dryRun && repositoryMissing);
const next = bootstrapNext(result, blockers);
return {
ok: blockers.length === 0 && giteaReady && applyReady,
action: result.action,
mutation: result.mutation === true,
mode: result.mode,
target: { id: target.id },
consumer: { id: consumer.id, node: consumer.node, lane: consumer.lane },
sourceAuthority: {
upstreamRepository: mirror.upstreamRepository,
upstreamBranch: mirror.upstreamBranch,
github: { ok: githubReady, code: stringValue(githubRepository.code, githubReady ? "github-upstream-available" : "github-upstream-preflight-failed") },
giteaRepository: `${stringValue(mirror.owner)}/${stringValue(mirror.repo)}`,
giteaKey: mirror.key,
valuesPrinted: false,
},
preconditions: [
{ id: "yaml-exact-match", ok: true, code: "yaml-repository-exact-match", matchCount: 1 },
{ id: "github-upstream", ok: githubReady, code: stringValue(githubRepository.code, githubReady ? "github-upstream-available" : "github-upstream-preflight-failed") },
{ id: "gitea-repository", ok: giteaSkipped ? null : giteaReady, code: giteaSkipped ? "gitea-bootstrap-skipped" : giteaReady ? (dryRun ? "gitea-bootstrap-planned" : "gitea-bootstrap-ready") : "gitea-bootstrap-failed" },
],
stages: [
{ id: "gitea-init", ok: giteaSkipped ? null : giteaReady, state: giteaSkipped ? "skipped" : giteaReady ? (dryRun ? "planned" : "ready") : "failed", mutation: gitea.mutation === true },
{ id: "pac-controller", ok: applySkipped ? null : applyReady, state: applySkipped ? "skipped" : repositoryMissing ? "waiting-for-gitea-confirm" : applyReady ? (dryRun ? "planned" : "ready") : "failed", mutation: apply.mutation === true },
{ id: "tekton-consumer", ok: applySkipped ? null : applyReady, state: applySkipped ? "skipped" : repositoryMissing ? "waiting-for-gitea-confirm" : applyReady ? (dryRun ? "planned" : "ready") : "failed", mutation: apply.mutation === true },
{ id: "gitops-argo", ok: applySkipped ? null : applyReady, state: applySkipped ? "skipped" : repositoryMissing ? "waiting-for-gitea-confirm" : applyReady ? (dryRun ? "planned" : "ready") : "failed", mutation: apply.mutation === true },
],
repository: { id: repository.id, namespace: repository.namespace },
blockers,
next,
valuesPrinted: false,
};
}
export function renderPacBootstrap(result: Record<string, unknown>): RenderedCliResult {
const projection = projectPacBootstrapResult(result);
const target = record(projection.target);
const consumer = record(projection.consumer);
const source = record(projection.sourceAuthority);
const preconditions = records(projection.preconditions);
const stages = records(projection.stages);
const blockers = records(projection.blockers);
const next = record(projection.next);
const lines = [
"PLATFORM-INFRA PAC / TEKTON / GITOPS BOOTSTRAP",
...table(["TARGET", "CONSUMER", "MODE", "MUTATION", "OK"], [[stringValue(target.id), stringValue(consumer.id), stringValue(projection.mode), boolText(projection.mutation), boolText(projection.ok)]]),
"",
"SOURCE AUTHORITY",
...table(["UPSTREAM", "BRANCH", "GITEA_KEY", "GITEA_REPOSITORY"], [[stringValue(source.upstreamRepository), stringValue(source.upstreamBranch), stringValue(source.giteaKey), stringValue(source.giteaRepository)]]),
"",
"PRECONDITIONS",
...table(["PRECONDITION", "OK", "CODE"], preconditions.map((item) => [stringValue(item.id), boolText(item.ok), stringValue(item.code)])),
"",
"STAGES",
...table(["STAGE", "OK", "STATE", "MUTATION"], stages.map((item) => [stringValue(item.id), boolText(item.ok), stringValue(item.state), boolText(item.mutation)])),
...(blockers.length === 0 ? [] : ["", "BLOCKERS", ...blockers.map((item) => ` ${stringValue(item.code)}: ${compactLine(stringValue(item.reason))}`)]),
"",
"NEXT",
` ${stringValue(next.kind)}: ${stringValue(next.command)}`,
];
return { ok: projection.ok === true, command: "platform-infra pipelines-as-code bootstrap", renderedText: lines.join("\n"), contentType: "text/plain", projection };
}
function bootstrapBlockers(githubPreflight: Record<string, unknown>, gitea: Record<string, unknown>, apply: Record<string, unknown>, remote: Record<string, unknown>): Record<string, unknown>[] {
const blockers: Record<string, unknown>[] = [];
const githubRepository = records(githubPreflight.repositories)[0] ?? {};
if (githubPreflight.ok === false) {
blockers.push({ code: stringValue(githubRepository.code, stringValue(githubPreflight.code, "github-upstream-preflight-failed")), stage: "github-upstream", reason: stringValue(githubRepository.errorTail, errorText(githubPreflight)) });
return blockers;
}
for (const code of Array.isArray(gitea.blockers) ? gitea.blockers.map(String) : []) {
blockers.push({ code: code.includes("credential") ? "source-credential-unavailable" : "github-upstream-unavailable", stage: "github-upstream", reason: code });
}
if (gitea.ok !== true && blockers.length === 0) blockers.push({ code: "gitea-bootstrap-failed", stage: "gitea-init", reason: errorText(gitea) });
if (apply.ok !== true && remote.error !== "gitea-repository-missing") {
blockers.push({ code: remote.error === "gitea-credential-unavailable" ? "gitea-credential-unavailable" : "pac-apply-failed", stage: "pac-controller", reason: errorText(remote, apply) });
}
return blockers;
}
function bootstrapNext(result: Record<string, unknown>, blockers: Record<string, unknown>[]): Record<string, unknown> {
const target = record(result.target);
const consumer = record(result.consumer);
const source = record(result.mirrorRepository);
if (blockers.length > 0) {
const yamlFailure = blockers.some((item) => String(item.code).startsWith("yaml-"));
return yamlFailure
? { kind: "fix-yaml", command: "检查 config/platform-infra/gitea.yaml 与 config/platform-infra/pipelines-as-code.yaml 的 target、owner/name 和 read URL 精确匹配" }
: { kind: "read-only-status", command: `bun scripts/cli.ts platform-infra pipelines-as-code status --target ${target.id} --consumer ${consumer.id}` };
}
if (result.mode === "dry-run") return { kind: "confirm", command: `bun scripts/cli.ts platform-infra pipelines-as-code bootstrap --target ${target.id} --consumer ${consumer.id} --confirm` };
return { kind: "source-pr-merge", command: `合并 ${source.upstreamRepository}@${source.upstreamBranch} 的 GitHub PR,由自动链继续交付` };
}
function errorText(...values: Record<string, unknown>[]): string {
for (const value of values) {
for (const candidate of [value.error, value.stderrTail, record(value.remote).error, record(value.remote).stderrTail]) {
if (typeof candidate === "string" && candidate.length > 0) return candidate;
}
}
return "unknown-bootstrap-failure";
}
function record(value: unknown): Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value) ? value as Record<string, unknown> : {};
}
function records(value: unknown): Record<string, unknown>[] {
return Array.isArray(value) ? value.map(record) : [];
}
function stringValue(value: unknown, fallback = "-"): string {
return typeof value === "string" && value.length > 0 ? value : value === undefined || value === null ? fallback : String(value);
}
function boolText(value: unknown): string {
return value === true ? "true" : value === false ? "false" : "-";
}
function compactLine(value: string): string {
return value.replace(/\s+/gu, " ").trim().slice(0, 240) || "-";
}
function table(headers: string[], rows: string[][]): string[] {
const widths = headers.map((header, index) => Math.max(header.length, ...rows.map((row) => (row[index] ?? "").length)));
const format = (row: string[]) => row.map((cell, index) => cell.padEnd(widths[index])).join(" ").trimEnd();
return [format(headers), format(widths.map((width) => "-".repeat(width))), ...rows.map(format)];
}
function repositoryUrlIdentity(value: string): string {
const parsed = new URL(value);
const path = parsed.pathname.replace(/\/+$/u, "");
+73 -44
View File
@@ -28,10 +28,16 @@ import {
} from "./platform-infra-pipelines-as-code-source-artifact";
import { pacAdmissionDesiredIdentity } from "./platform-infra-pac-provenance";
import { pacConsumerRbacDesiredIdentity, renderPacConsumerRbacDesiredFragment } from "./platform-infra-pac-consumer-rbac";
import { runPlatformInfraGiteaCommand } from "./platform-infra-gitea";
import { runGiteaMirrorBootstrapPreflight, runPlatformInfraGiteaCommand } from "./platform-infra-gitea";
import { readGiteaConfig } from "./platform-infra-gitea-config";
import { resolvePacBootstrapMirrorRepository } from "./platform-infra-pipelines-as-code-bootstrap";
import {
pacBootstrapYamlMatchFailure,
projectPacBootstrapResult,
renderPacBootstrap,
resolvePacBootstrapMirrorRepository,
} from "./platform-infra-pipelines-as-code-bootstrap";
import { featureConfigSchemaHistoryFields, renderFeatureConfigSchemaLine } from "./platform-infra-pac-feature-config-projection";
import { observePacDeliveryTiming } from "./platform-infra-pac-delivery-timing";
const configFile = rootPath("config", "platform-infra", "pipelines-as-code.yaml");
const configLabel = "config/platform-infra/pipelines-as-code.yaml";
@@ -256,7 +262,7 @@ export async function runPlatformInfraPipelinesAsCodeCommand(config: UniDeskConf
if (action === "bootstrap") {
const options = parseApplyOptions(args.slice(1));
const result = await bootstrap(config, options);
return options.json || options.full || options.raw ? result : renderBootstrap(result);
return options.full || options.raw ? result : options.json ? projectPacBootstrapResult(result) : renderPacBootstrap(result);
}
if (action === "status") {
const options = parseCommonOptions(args.slice(1));
@@ -273,6 +279,11 @@ export async function runPlatformInfraPipelinesAsCodeCommand(config: UniDeskConf
const result = await history(config, options);
return options.raw ? result : options.full ? compactHistoryJson(result, true) : options.json ? compactHistoryJson(result) : renderHistory(result);
}
if (action === "delivery-timing" || action === "timing") {
const options = parseCommonOptions(args.slice(1));
const result = await deliveryTiming(config, options);
return options.full || options.raw || options.json ? result : renderDeliveryTiming(result);
}
if (action === "debug-step") {
const options = parseHistoryOptions(args.slice(1));
const result = await debugStep(config, options);
@@ -395,12 +406,13 @@ function help(scope: string | null): Record<string, unknown> {
};
}
return {
command: "platform-infra pipelines-as-code plan|status|history|debug-step|source-artifact",
command: "platform-infra pipelines-as-code plan|status|history|delivery-timing|debug-step|source-artifact",
configTruth: configLabel,
usage: [
"bun scripts/cli.ts platform-infra pipelines-as-code plan --target JD01",
"bun scripts/cli.ts platform-infra pipelines-as-code status --target JD01 [--json|--full|--raw]",
"bun scripts/cli.ts platform-infra pipelines-as-code history --target JD01 [--consumer hwlab-jd01-v03] [--limit 10]",
"bun scripts/cli.ts platform-infra pipelines-as-code delivery-timing --target NC01 --consumer selfmedia-nc01 [--json]",
"bun scripts/cli.ts platform-infra pipelines-as-code history --target JD01 --id <pipelinerun>",
"bun scripts/cli.ts platform-infra pipelines-as-code debug-step --target JD01 [--consumer <consumer>] [--id <pipelinerun>] [--json]",
"bun scripts/cli.ts platform-infra pipelines-as-code source-artifact check --target NC01 --consumer agentrun-nc01-v02 --source-worktree /abs/worktree",
@@ -975,11 +987,20 @@ async function bootstrap(config: UniDeskConfig, options: ApplyOptions): Promise<
throw new Error(`Pipelines-as-Code consumer ${consumer.id} belongs to ${consumer.node}, not target ${target.id}`);
}
const repository = resolveRepository(pac, consumer.repositoryRef);
const mirror = resolvePacBootstrapMirrorRepository(readGiteaConfig(), target.id, repository);
let mirror: ReturnType<typeof resolvePacBootstrapMirrorRepository>;
try {
mirror = resolvePacBootstrapMirrorRepository(readGiteaConfig(), target.id, repository);
} catch (error) {
return pacBootstrapYamlMatchFailure(target.id, consumer.id, error);
}
const githubPreflight = record(await runGiteaMirrorBootstrapPreflight(config, target.id, mirror.key));
if (githubPreflight.ok !== true) {
return bootstrapResult(target, consumer, repository, mirror, options, githubPreflight, { ok: false, mutation: false, mode: "skipped", githubPreflight, valuesPrinted: false }, { ok: false, mutation: false, mode: "skipped", valuesPrinted: false });
}
const giteaArgs = ["mirror", "bootstrap", "--target", target.id, "--repo", mirror.key, ...(options.confirm ? ["--confirm"] : []), "--full"];
const giteaBootstrap = record(await runPlatformInfraGiteaCommand(config, giteaArgs));
if (options.confirm && giteaBootstrap.ok !== true) {
return bootstrapResult(target, consumer, repository, mirror, options, giteaBootstrap, {
return bootstrapResult(target, consumer, repository, mirror, options, githubPreflight, giteaBootstrap, {
ok: false,
mutation: false,
mode: "skipped",
@@ -988,7 +1009,7 @@ async function bootstrap(config: UniDeskConfig, options: ApplyOptions): Promise<
});
}
const pacApply = await apply(config, options);
return bootstrapResult(target, consumer, repository, mirror, options, giteaBootstrap, pacApply);
return bootstrapResult(target, consumer, repository, mirror, options, githubPreflight, giteaBootstrap, pacApply);
}
function bootstrapResult(
@@ -997,6 +1018,7 @@ function bootstrapResult(
repository: PacRepository,
mirror: ReturnType<typeof resolvePacBootstrapMirrorRepository>,
options: ApplyOptions,
githubPreflight: Record<string, unknown>,
giteaBootstrap: Record<string, unknown>,
pacApply: Record<string, unknown>,
): Record<string, unknown> {
@@ -1021,6 +1043,7 @@ function bootstrapResult(
repo: mirror.gitea.name,
valuesPrinted: false,
},
githubPreflight,
steps: [
{ id: "gitea-repository", ok: giteaBootstrap.ok === true, mutation: giteaBootstrap.mutation === true, mode: giteaBootstrap.mode, valuesPrinted: false },
{ id: "pac-tekton-gitops", ok: pacReady, mutation: pacApply.mutation === true, mode: pacApply.mode, repositoryMissing, valuesPrinted: false },
@@ -2208,43 +2231,6 @@ function renderApply(result: Record<string, unknown>): RenderedCliResult {
return rendered(result, "platform-infra pipelines-as-code apply", lines);
}
function renderBootstrap(result: Record<string, unknown>): RenderedCliResult {
const target = record(result.target);
const consumer = record(result.consumer);
const mirror = record(result.mirrorRepository);
const steps = arrayRecords(result.steps);
const pacApply = record(result.pipelinesAsCodeApply);
const pacRemote = record(pacApply.remote);
const giteaBootstrap = record(result.giteaBootstrap);
const next = record(result.next);
const errorValue = [pacRemote.error, giteaBootstrap.error, pacApply.error]
.find((value): value is string => typeof value === "string" && value.length > 0) ?? "";
const error = errorValue.length === 0 ? "" : compactLine(errorValue);
const lines = [
"PLATFORM-INFRA PAC / TEKTON / GITOPS BOOTSTRAP",
...table(["TARGET", "CONSUMER", "MODE", "MUTATION", "OK"], [[stringValue(target.id), stringValue(consumer.id), stringValue(result.mode), boolText(result.mutation), boolText(result.ok)]]),
"",
"SOURCE AUTHORITY",
...table(["GITEA_KEY", "GITEA_REPOSITORY", "UPSTREAM", "BRANCH"], [[stringValue(mirror.key), `${stringValue(mirror.owner)}/${stringValue(mirror.repo)}`, stringValue(mirror.upstreamRepository), stringValue(mirror.upstreamBranch)]]),
"",
"STEPS",
...table(["STEP", "OK", "MODE", "MUTATION", "DETAIL"], steps.map((step) => [
stringValue(step.id),
boolText(step.ok),
stringValue(step.mode),
boolText(step.mutation),
step.repositoryMissing === true ? "repository-will-be-created-on-confirm" : "ready",
])),
...(error.length === 0 ? [] : ["", `ERROR ${error}`]),
"",
"NEXT",
...(result.mode === "dry-run"
? [` confirm: ${stringValue(next.confirm)}`, ` boundary: ${stringValue(next.boundary)}`]
: [` delivery: ${stringValue(next.deliveryTrigger)}`, ` investigate: ${stringValue(next.investigate)}`]),
];
return rendered(result, "platform-infra pipelines-as-code bootstrap", lines);
}
export function renderStatus(result: Record<string, unknown>): RenderedCliResult {
const summary = record(result.summary);
const consumer = record(result.consumer);
@@ -2423,6 +2409,49 @@ function renderCloseout(result: Record<string, unknown>): RenderedCliResult {
return rendered(result, "platform-infra pipelines-as-code closeout", lines);
}
async function deliveryTiming(config: UniDeskConfig, options: CommonOptions): Promise<Record<string, unknown>> {
const pac = readPacConfig();
const target = resolveTarget(pac, options.targetId);
const consumer = resolveConsumer(pac, options.consumerId);
if (consumer.node.toLowerCase() !== target.id.toLowerCase()) throw new Error(`Pipelines-as-Code consumer ${consumer.id} belongs to ${consumer.node}, not target ${target.id}`);
const authority = resolveCicdDeliveryAuthority({ consumerId: consumer.id, node: consumer.node, lane: consumer.lane });
if (authority.kind !== "pac-pr-merge") throw new Error(`delivery authority is ${authority.kind}: ${authority.reason}`);
const historyOptions: HistoryOptions = { ...options, limit: 1, detailId: null };
return await observePacDeliveryTiming({
target: targetSummary(target),
consumer: {
id: consumer.id,
node: consumer.node,
lane: consumer.lane,
repository: authority.consumer.sourceRepository,
branch: authority.consumer.sourceBranch,
pipeline: consumer.pipeline,
},
}, {
history: async () => await history(config, historyOptions),
status: async () => await status(config, options),
});
}
function renderDeliveryTiming(result: Record<string, unknown>): RenderedCliResult {
const timing = record(result.timing);
const source = record(result.source);
const pr = record(source.pullRequest);
const runtime = record(result.runtime);
const argo = record(runtime.argo);
const health = record(runtime.health);
const lines = [
"PLATFORM-INFRA DELIVERY TIMING",
`STATE: ${stringValue(result.state)} MUTATION: ${boolText(result.mutation)}`,
`SOURCE: ${stringValue(record(result.consumer).repository)}@${stringValue(source.commit)} PR#${stringValue(pr.number)}`,
`MERGED_AT: ${stringValue(timing.mergedAt)} PIPELINE_START: ${stringValue(timing.pipelineStart)} PIPELINE_DONE: ${stringValue(timing.pipelineCompletion)}`,
`TRIGGER_WAIT_S: ${stringValue(timing.triggerWaitSeconds)} PIPELINE_S: ${stringValue(timing.pipelineDurationSeconds)} END_TO_END_S: ${stringValue(timing.endToEndSeconds)}`,
`ARGO: ${stringValue(argo.sync)}/${stringValue(argo.health)} RUNTIME_READY: ${stringValue(health.readyReplicas)}/${stringValue(health.replicas)}`,
`EVIDENCE_GAPS: ${(Array.isArray(result.evidenceGaps) ? result.evidenceGaps.map(String) : []).join(", ") || "-"}`,
];
return rendered(result, "platform-infra pipelines-as-code delivery-timing", lines);
}
export function renderHistory(result: Record<string, unknown>): RenderedCliResult {
const rows = arrayRecords(result.rows);
const config = record(result.config);
@@ -0,0 +1,75 @@
import type { UniDeskConfig } from "../config";
import type { RenderedCliResult } from "../output";
import { defaultSub2ApiTargetId } from "../platform-infra/config";
import { projectSub2ApiOps } from "./projection";
import { querySub2ApiOps } from "./query";
import { renderSub2ApiOps } from "./render";
import type { Sub2ApiOpsOptions } from "./types";
export async function runSub2ApiOpsCommand(config: UniDeskConfig, args: string[]): Promise<Record<string, unknown> | RenderedCliResult> {
const options = parseSub2ApiOpsOptions(args);
const raw = await querySub2ApiOps(config, options);
const projected = projectSub2ApiOps(raw, options);
return options.json ? projected : renderSub2ApiOps(projected);
}
export function parseSub2ApiOpsOptions(args: string[]): Sub2ApiOpsOptions {
const [actionRaw, ...rest] = args;
if (actionRaw !== "diagnosis" && actionRaw !== "channels") {
throw new Error("sub2api ops usage: diagnosis|channels [--target <id>] [--platform <name>] [--json]");
}
let targetId = defaultSub2ApiTargetId();
let json = false;
let platform: string | null = null;
let group: string | null = null;
let diagnosisId: string | null = null;
let timeRange = "1h" as Sub2ApiOpsOptions["timeRange"];
let channel: string | null = null;
let model: string | null = null;
let window = "7d" as Sub2ApiOpsOptions["window"];
let pageToken: string | null = null;
let recordId: string | null = null;
for (let index = 0; index < rest.length; index += 1) {
const arg = rest[index]!;
const readValue = (name: string): string => {
const value = rest[index + 1];
if (value === undefined || value.startsWith("--")) throw new Error(`${name} requires a value`);
index += 1;
return value.trim();
};
if (arg === "--json") json = true;
else if (arg === "--target") targetId = readValue(arg);
else if (arg === "--platform") platform = readValue(arg);
else if (arg === "--group") group = readValue(arg);
else if (arg === "--id") diagnosisId = readValue(arg);
else if (arg === "--time-range") timeRange = parseTimeRange(readValue(arg));
else if (arg === "--channel") channel = readValue(arg);
else if (arg === "--model") model = readValue(arg);
else if (arg === "--window") window = parseWindow(readValue(arg));
else if (arg === "--page-token") pageToken = parseRecordId(readValue(arg), arg);
else if (arg === "--record") recordId = parseRecordId(readValue(arg), arg);
else throw new Error(`unknown sub2api ops option: ${arg}`);
}
if (actionRaw === "diagnosis" && (channel !== null || model !== null || window !== "7d" || pageToken !== null || recordId !== null)) throw new Error("ops diagnosis does not accept channel history options");
if (actionRaw === "channels" && (group !== null || diagnosisId !== null || timeRange !== "1h")) throw new Error("ops channels does not accept --group, --id, or --time-range");
if (actionRaw === "channels" && model !== null && channel === null) throw new Error("--model requires --channel");
if (actionRaw === "channels" && (pageToken !== null || recordId !== null) && channel === null) throw new Error("--page-token and --record require --channel");
if (pageToken !== null && recordId !== null) throw new Error("use only one of --page-token or --record");
return { action: actionRaw, targetId, json, platform, group, diagnosisId, timeRange, channel, model, window, pageToken, recordId };
}
function parseTimeRange(value: string): Sub2ApiOpsOptions["timeRange"] {
if (value === "5m" || value === "30m" || value === "1h" || value === "6h" || value === "24h") return value;
throw new Error("--time-range must be one of 5m, 30m, 1h, 6h, 24h");
}
function parseWindow(value: string): Sub2ApiOpsOptions["window"] {
if (value === "7d" || value === "15d" || value === "30d") return value;
throw new Error("--window must be one of 7d, 15d, 30d");
}
function parseRecordId(value: string, option: string): string {
if (/^[1-9][0-9]*$/u.test(value)) return value;
throw new Error(`${option} must be a positive native history record ID`);
}
@@ -0,0 +1,337 @@
import type { Sub2ApiOpsOptions } from "./types";
const upstreamVersion = "v0.1.155";
const diagnosisRuleSource = "frontend/src/views/admin/ops/components/OpsDashboardHeader.vue";
const diagnosisTextSource = "frontend/src/i18n/locales/zh/admin/ops.ts";
const channelHistoryPageSize = 20;
export function projectSub2ApiOps(raw: Record<string, unknown>, options: Sub2ApiOpsOptions): Record<string, unknown> {
const target = record(raw.target);
if (raw.ok !== true) {
return {
ok: false,
action: `platform-infra-sub2api-ops-${options.action}`,
target,
query: querySummary(options),
source: sourceSummary(options.action, "unavailable"),
error: stringValue(raw.error, "Sub2API native source unavailable"),
valuesPrinted: false,
};
}
const data = record(raw.data);
return options.action === "diagnosis"
? projectDiagnosis(target, data, options)
: projectChannels(target, data, options);
}
function projectDiagnosis(target: Record<string, unknown>, data: Record<string, unknown>, options: Sub2ApiOpsOptions): Record<string, unknown> {
const groups = arrayRecords(data.groups).map((entry) => {
const group = record(entry.group);
const overview = record(entry.overview);
const scope = group.id === null || group.id === undefined ? "all" : `g${String(group.id)}`;
return {
id: scope,
groupId: group.id ?? null,
groupName: stringValue(group.name, "all"),
platform: stringValue(group.platform, "all"),
window: {
kind: "native-ops-time-range",
value: options.timeRange,
startAt: overview.start_time ?? null,
endAt: overview.end_time ?? null,
},
sourceStatus: "available",
diagnoses: diagnosisItems(scope, overview),
metrics: compactOverview(overview),
};
});
const selectedDiagnosis = options.diagnosisId === null
? null
: groups.flatMap((group) => arrayRecords(group.diagnoses)).find((item) => item.id === options.diagnosisId) ?? null;
return {
ok: options.diagnosisId === null || selectedDiagnosis !== null,
action: "platform-infra-sub2api-ops-diagnosis",
target,
query: querySummary(options),
source: sourceSummary("diagnosis", "available"),
groups,
selectedDiagnosis,
evidence: options.diagnosisId === null ? null : projectEvidence(record(data.evidence)),
error: options.diagnosisId !== null && selectedDiagnosis === null ? `diagnosis id not found: ${options.diagnosisId}` : null,
valuesPrinted: false,
};
}
function diagnosisItems(scope: string, overview: Record<string, unknown>): Record<string, unknown>[] {
const items: Record<string, unknown>[] = [];
const qps = numberAt(record(overview.qps).current, 0);
const errorRate = numberAt(overview.error_rate, 0);
if (qps === 0 && errorRate === 0) {
return [diagnosisItem(scope, "idle", "info", "system_activity", 0, "requests/s", "系统当前处于待机状态", "无活跃流量", null, overview)];
}
const systemMetrics = record(overview.system_metrics);
if (systemMetrics.db_ok === false) items.push(diagnosisItem(scope, "db-down", "critical", "db_ok", false, "boolean", "数据库连接失败", "所有数据库操作将失败", "检查数据库服务状态、网络连接和连接配置", overview));
if (systemMetrics.redis_ok === false) items.push(diagnosisItem(scope, "redis-down", "warning", "redis_ok", false, "boolean", "Redis连接失败", "缓存功能降级,性能可能下降", "检查Redis服务状态和网络连接", overview));
const cpu = numberAt(systemMetrics.cpu_usage_percent, 0);
if (cpu > 90) items.push(diagnosisItem(scope, "cpu-critical", "critical", "cpu_usage_percent", cpu, "%", `CPU使用率严重过高 (${cpu.toFixed(1)}%)`, "系统响应变慢,可能影响所有请求", "检查CPU密集型任务,考虑扩容或优化代码", overview));
else if (cpu > 80) items.push(diagnosisItem(scope, "cpu-high", "warning", "cpu_usage_percent", cpu, "%", `CPU使用率偏高 (${cpu.toFixed(1)}%)`, "系统负载较高,需要关注", "监控CPU趋势,准备扩容方案", overview));
const memory = numberAt(systemMetrics.memory_usage_percent, 0);
if (memory > 90) items.push(diagnosisItem(scope, "memory-critical", "critical", "memory_usage_percent", memory, "%", `内存使用率严重过高 (${memory.toFixed(1)}%)`, "可能触发OOM,系统稳定性受威胁", "检查内存泄漏,考虑增加内存或优化内存使用", overview));
else if (memory > 85) items.push(diagnosisItem(scope, "memory-high", "warning", "memory_usage_percent", memory, "%", `内存使用率偏高 (${memory.toFixed(1)}%)`, "内存压力较大,需要关注", "监控内存趋势,检查是否有内存泄漏", overview));
const ttft = numberAt(record(overview.ttft).p99_ms, 0);
if (ttft > 500) items.push(diagnosisItem(scope, "ttft-high", "warning", "ttft.p99_ms", ttft, "ms", `首 Token 时间偏高 (${ttft.toFixed(0)}ms)`, "用户感知时长增加", "优化请求处理流程,减少前置逻辑耗时", overview));
const upstreamRate = numberAt(overview.upstream_error_rate, 0) * 100;
if (upstreamRate > 5) items.push(diagnosisItem(scope, "upstream-critical", "critical", "upstream_error_rate", upstreamRate, "%", `上游错误率严重偏高 (${upstreamRate.toFixed(2)}%)`, "可能影响大量用户请求", "检查上游服务健康状态,启用降级策略", overview));
else if (upstreamRate > 2) items.push(diagnosisItem(scope, "upstream-high", "warning", "upstream_error_rate", upstreamRate, "%", `上游错误率偏高 (${upstreamRate.toFixed(2)}%)`, "建议检查上游服务状态", "联系上游服务团队,准备降级方案", overview));
const clientRate = errorRate * 100;
if (clientRate > 3) items.push(diagnosisItem(scope, "error-high", "critical", "error_rate", clientRate, "%", `错误率过高 (${clientRate.toFixed(2)}%)`, "大量请求失败", "查看错误日志,定位错误根因,紧急修复", overview));
else if (clientRate > 0.5) items.push(diagnosisItem(scope, "error-elevated", "warning", "error_rate", clientRate, "%", `错误率偏高 (${clientRate.toFixed(2)}%)`, "建议检查错误日志", "分析错误类型和分布,制定修复计划", overview));
const sla = numberAt(overview.sla, 0) * 100;
if (sla < 90) items.push(diagnosisItem(scope, "sla-critical", "critical", "sla", sla, "%", `SLA 严重低于目标 (${sla.toFixed(2)}%)`, "用户体验严重受损", "紧急排查错误原因,必要时采取限流保护", overview));
else if (sla < 98) items.push(diagnosisItem(scope, "sla-low", "warning", "sla", sla, "%", `SLA 低于目标 (${sla.toFixed(2)}%)`, "需要关注服务质量", "分析SLA下降原因,优化系统性能", overview));
const healthScore = finiteNumber(overview.health_score);
if (healthScore !== null && healthScore < 60) items.push(diagnosisItem(scope, "health-critical", "critical", "health_score", healthScore, "score", `综合健康评分过低 (${healthScore})`, "多个指标可能同时异常,建议优先排查错误与资源使用情况", "全面检查系统状态,优先处理critical级别问题", overview));
else if (healthScore !== null && healthScore < 90) items.push(diagnosisItem(scope, "health-low", "warning", "health_score", healthScore, "score", `综合健康评分偏低 (${healthScore})`, "可能存在轻度波动,建议关注 SLA 与错误率", "监控指标趋势,预防问题恶化", overview));
if (items.length === 0) items.push(diagnosisItem(scope, "healthy", "info", "health", "normal", "state", "所有系统指标正常", "服务运行稳定", null, overview));
return items;
}
function diagnosisItem(scope: string, category: string, severity: string, metric: string, value: unknown, unit: string, message: string, impact: string, advice: string | null, overview: Record<string, unknown>): Record<string, unknown> {
return {
id: `${scope}:${category}`,
category,
severity,
message,
metric: { name: metric, value, unit },
impact,
advice,
adviceKind: advice === null ? null : "native-frontend-advice",
factRef: `${scope}.metrics`,
inferenceStatus: "not-verified",
};
}
function compactOverview(overview: Record<string, unknown>): Record<string, unknown> {
return {
requestCount: overview.request_count_total ?? null,
successCount: overview.success_count ?? null,
errorCount: overview.error_count_total ?? null,
errorRate: overview.error_rate ?? null,
upstreamErrorRate: overview.upstream_error_rate ?? null,
sla: overview.sla ?? null,
healthScore: overview.health_score ?? null,
qps: record(overview.qps).current ?? null,
ttftP99Ms: record(overview.ttft).p99_ms ?? null,
collectedAt: overview.end_time ?? null,
};
}
function projectEvidence(evidence: Record<string, unknown>): Record<string, unknown> | null {
if (Object.keys(evidence).length === 0) return null;
const records = arrayRecords(evidence.records).map((item) => ({
requestId: item.request_id ?? null,
createdAt: item.created_at ?? null,
platform: item.platform ?? null,
model: item.model ?? null,
accountId: item.account_id ?? null,
accountName: item.account_name ?? null,
statusCode: item.status_code ?? item.upstream_status_code ?? null,
durationMs: item.duration_ms ?? null,
severity: item.severity ?? null,
message: item.message ?? null,
}));
return { diagnosisId: evidence.diagnosisId ?? null, recordCount: records.length, records };
}
function projectChannels(target: Record<string, unknown>, data: Record<string, unknown>, options: Sub2ApiOpsOptions): Record<string, unknown> {
const channels = arrayRecords(data.channels).map((entry) => projectChannel(entry, options.window));
const operational = channels.every((channel) => channel.status === "OPERATIONAL");
const history = arrayRecords(data.history)
.map((item) => ({
id: item.id ?? null,
model: item.model ?? null,
status: item.status ?? null,
latencyMs: item.latency_ms ?? null,
pingLatencyMs: item.ping_latency_ms ?? null,
message: item.message ?? null,
checkedAt: item.checked_at ?? null,
}))
.sort((left, right) => String(left.checkedAt).localeCompare(String(right.checkedAt)));
const historyProjection = options.channel === null ? null : paginateHistory(history, options);
const historyError = historyProjection === null ? null : historyProjection.error;
return {
ok: historyError === null,
action: "platform-infra-sub2api-ops-channels",
target,
query: querySummary(options),
source: sourceSummary("channels", "available"),
overallStatus: operational ? "OPERATIONAL" : "DEGRADED",
channels,
history: historyProjection,
error: historyError,
valuesPrinted: false,
};
}
function paginateHistory(history: Record<string, unknown>[], options: Sub2ApiOpsOptions): Record<string, unknown> {
if (options.recordId !== null) {
const selectedRecord = history.find((item) => String(item.id) === options.recordId) ?? null;
return {
order: "PAST_TO_NOW",
pageSize: channelHistoryPageSize,
sourceRecordCount: history.length,
sourceCapReached: history.length === 1000,
records: [],
moreAvailable: false,
nextPageToken: null,
selectedRecord,
error: selectedRecord === null ? `channel history record not found: ${options.recordId}` : null,
};
}
let end = history.length;
if (options.pageToken !== null) {
const cursorIndex = history.findIndex((item) => String(item.id) === options.pageToken);
if (cursorIndex < 0) {
return {
order: "PAST_TO_NOW",
pageSize: channelHistoryPageSize,
sourceRecordCount: history.length,
sourceCapReached: history.length === 1000,
records: [],
moreAvailable: false,
nextPageToken: null,
selectedRecord: null,
error: `channel history page token not found: ${options.pageToken}`,
};
}
end = cursorIndex;
}
const start = Math.max(0, end - channelHistoryPageSize);
const records = history.slice(start, end);
return {
order: "PAST_TO_NOW",
pageSize: channelHistoryPageSize,
sourceRecordCount: history.length,
sourceCapReached: history.length === 1000,
records,
moreAvailable: start > 0,
nextPageToken: start > 0 && records.length > 0 ? records[0]?.id ?? null : null,
selectedRecord: null,
error: null,
};
}
function projectChannel(entry: Record<string, unknown>, window: "7d" | "15d" | "30d"): Record<string, unknown> {
const summary = record(entry.summary);
const detail = record(entry.detail);
const models = arrayRecords(detail.models).map((model) => ({
model: model.model ?? null,
status: upperStatus(model.latest_status),
latencyMs: model.latest_latency_ms ?? null,
availability: availabilityFor(model, window),
availabilityWindow: window,
avgLatency7dMs: model.avg_latency_7d_ms ?? null,
}));
const timeline = arrayRecords(summary.timeline)
.map((point) => ({
status: upperStatus(point.status),
latencyMs: point.latency_ms ?? null,
pingLatencyMs: point.ping_latency_ms ?? null,
checkedAt: point.checked_at ?? null,
}))
.sort((left, right) => String(left.checkedAt).localeCompare(String(right.checkedAt)));
const status = [summary.primary_status, ...arrayRecords(summary.extra_models).map((item) => item.status)]
.every((value) => String(value ?? "").toLowerCase() === "operational") ? "OPERATIONAL" : "DEGRADED";
return {
id: summary.id ?? detail.id ?? null,
name: summary.name ?? detail.name ?? null,
groupName: summary.group_name ?? detail.group_name ?? null,
provider: summary.provider ?? detail.provider ?? null,
status,
primary: {
model: summary.primary_model ?? null,
status: upperStatus(summary.primary_status),
latencyMs: summary.primary_latency_ms ?? null,
pingLatencyMs: summary.primary_ping_latency_ms ?? null,
availability: availabilityFor(models.find((item) => item.model === summary.primary_model) ?? {}, window) ?? summary.availability_7d ?? null,
availabilityWindow: window,
},
models,
recentRecordCount: timeline.length,
collectedAt: timeline.length === 0 ? null : timeline[timeline.length - 1]?.checkedAt ?? null,
refreshRemainingSeconds: null,
refreshSourceStatus: "unsupported-by-native-response",
timeline: { order: "PAST_TO_NOW", records: timeline },
};
}
function availabilityFor(model: Record<string, unknown>, window: "7d" | "15d" | "30d"): unknown {
return model[`availability_${window}`] ?? model.availability ?? null;
}
function sourceSummary(action: "diagnosis" | "channels", status: string): Record<string, unknown> {
if (action === "diagnosis") {
return {
status,
upstreamVersion,
metricsEndpoint: "/api/v1/admin/ops/dashboard/overview",
nativeDiagnosisEndpoint: { status: "unsupported", reason: "v0.1.155 has no diagnosis/advice response schema" },
projectionKind: "native-frontend-projection",
ruleSource: diagnosisRuleSource,
textSource: diagnosisTextSource,
evidenceBoundary: "metrics are native API facts; severity, impact and advice reproduce the official v0.1.155 frontend projection and are not server-returned root-cause proof",
};
}
return {
status,
upstreamVersion,
listEndpoint: "/api/v1/channel-monitors",
detailEndpoint: "/api/v1/channel-monitors/:id/status",
historyEndpoint: "/api/v1/admin/channel-monitors/:id/history",
windows: ["7d", "15d", "30d"],
overallStatusKind: "native-frontend-projection",
refreshRemaining: { status: "unsupported", reason: "native response does not include a refresh countdown" },
};
}
function querySummary(options: Sub2ApiOpsOptions): Record<string, unknown> {
return {
target: options.targetId,
platform: options.platform,
group: options.group,
diagnosisId: options.diagnosisId,
timeRange: options.action === "diagnosis" ? options.timeRange : undefined,
channel: options.channel,
model: options.model,
window: options.action === "channels" ? options.window : undefined,
pageToken: options.pageToken,
recordId: options.recordId,
snapshot: true,
foregroundRefresh: false,
};
}
function upperStatus(value: unknown): string {
const text = String(value ?? "unknown").trim();
return text.length === 0 ? "UNKNOWN" : text.toUpperCase();
}
function record(value: unknown): Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
}
function arrayRecords(value: unknown): Record<string, unknown>[] {
return Array.isArray(value) ? value.map(record) : [];
}
function stringValue(value: unknown, fallback: string): string {
return typeof value === "string" && value.length > 0 ? value : fallback;
}
function finiteNumber(value: unknown): number | null {
return typeof value === "number" && Number.isFinite(value) ? value : null;
}
function numberAt(value: unknown, fallback: number): number {
return finiteNumber(value) ?? fallback;
}
@@ -0,0 +1,267 @@
import type { UniDeskConfig } from "../config";
import { parseJsonOutput } from "../platform-infra-ops-library";
import { runSshCommandCapture } from "../ssh";
import { readSub2ApiConfig } from "../platform-infra/config";
import { resolveTarget } from "../platform-infra/manifest";
import type { Sub2ApiOpsOptions, Sub2ApiOpsTarget } from "./types";
export async function querySub2ApiOps(config: UniDeskConfig, options: Sub2ApiOpsOptions): Promise<Record<string, unknown>> {
const target = resolveOpsTarget(options.targetId);
const result = await runSshCommandCapture(config, target.route, ["sh"], remoteQueryScript(target, options));
const parsed = parseJsonOutput(result.stdout);
if (result.exitCode !== 0 || parsed === null) {
const stderr = result.stderr.trim().slice(-1200);
const stdout = result.stdout.trim().slice(-1200);
return {
ok: false,
sourceStatus: "unavailable",
error: stderr || stdout || "remote Sub2API ops query returned no JSON",
remote: { exitCode: result.exitCode, stdoutTail: stdout, stderrTail: stderr },
target,
valuesPrinted: false,
};
}
return { ...parsed, target, valuesPrinted: false };
}
function resolveOpsTarget(targetId: string): Sub2ApiOpsTarget {
const config = readSub2ApiConfig();
const target = resolveTarget(config, targetId);
return {
id: target.id,
route: target.route,
namespace: target.namespace,
runtimeMode: target.runtimeMode,
hostDockerAppPort: target.hostDocker?.appPort ?? null,
hostDockerEnvPath: target.hostDocker?.envPath ?? null,
appSecretName: config.runtime.database.secretName,
};
}
function pyJson(value: unknown): string {
return `json.loads(${JSON.stringify(JSON.stringify(value))})`;
}
function remoteQueryScript(target: Sub2ApiOpsTarget, options: Sub2ApiOpsOptions): string {
return `
set -u
python3 - <<'PY'
import base64
import json
import subprocess
import sys
from urllib.parse import urlencode
TARGET = ${pyJson(target)}
OPTIONS = ${pyJson(options)}
def run(command, input_bytes=None):
return subprocess.run(command, input=input_bytes, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def tail_text(value, limit=800):
if isinstance(value, bytes):
value = value.decode("utf-8", errors="replace")
return str(value)[-limit:]
def kube_json(args, label):
proc = run(["kubectl", *args, "-o", "json"])
if proc.returncode != 0:
raise RuntimeError(f"{label} failed: {tail_text(proc.stderr)}")
return json.loads(proc.stdout.decode("utf-8"))
def read_host_env():
path = TARGET.get("hostDockerEnvPath")
try:
with open(path, "r", encoding="utf-8") as handle:
lines = handle.read().splitlines()
except PermissionError:
proc = run(["sudo", "-n", "cat", path])
if proc.returncode != 0:
raise RuntimeError("read host env failed: " + tail_text(proc.stderr))
lines = proc.stdout.decode("utf-8", errors="replace").splitlines()
values = {}
for line in lines:
text = line.strip()
if not text or text.startswith("#") or "=" not in text:
continue
key, value = text.split("=", 1)
value = value.strip()
if len(value) >= 2 and value[0] == value[-1] and value[0] in ("'", '"'):
value = value[1:-1]
values[key.strip()] = value
return values
def select_app_pod():
if TARGET["runtimeMode"] == "host-docker":
return "sub2api-app"
pods = kube_json(["-n", TARGET["namespace"], "get", "pods", "-l", "app.kubernetes.io/name=sub2api"], "sub2api pods").get("items") or []
running = [item for item in pods if (item.get("status") or {}).get("phase") == "Running"]
if not running:
raise RuntimeError("sub2api app pod not found")
return running[0]["metadata"]["name"]
APP_POD = select_app_pod()
def config_value(key):
if TARGET["runtimeMode"] == "host-docker":
return read_host_env().get(key)
data = kube_json(["-n", TARGET["namespace"], "get", "configmap", "sub2api-config"], "configmap/sub2api-config").get("data") or {}
return data.get(key)
def secret_value(key):
if TARGET["runtimeMode"] == "host-docker":
return read_host_env().get(key)
data = kube_json(["-n", TARGET["namespace"], "get", "secret", TARGET["appSecretName"]], "sub2api secret").get("data") or {}
return base64.b64decode(data[key]).decode("utf-8") if key in data else None
def parse_curl(proc):
output = proc.stdout.decode("utf-8", errors="replace")
marker = "\\n__HTTP_CODE__:"
position = output.rfind(marker)
if position < 0:
return {"ok": False, "httpStatus": 0, "json": None, "body": output, "stderr": tail_text(proc.stderr)}
body = output[:position]
try:
status = int(output[position + len(marker):].strip()[-3:])
except ValueError:
status = 0
try:
parsed = json.loads(body) if body.strip() else None
except json.JSONDecodeError:
parsed = None
return {"ok": proc.returncode == 0 and 200 <= status < 300, "httpStatus": status, "json": parsed, "body": body, "stderr": tail_text(proc.stderr)}
def curl_api(method, path, bearer=None, payload=None):
body = b"" if payload is None else json.dumps(payload, separators=(",", ":")).encode("utf-8")
script = r'''
set -eu
method="$1"
url="$2"
token="\${3:-}"
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
cat > "$tmp"
if [ -n "$token" ] && [ "$method" = GET ] && [ ! -s "$tmp" ]; then
curl -sS --max-time 20 -w '\n__HTTP_CODE__:%{http_code}' -X "$method" -H "Authorization: Bearer $token" "$url"
elif [ -n "$token" ]; then
curl -sS --max-time 20 -w '\n__HTTP_CODE__:%{http_code}' -X "$method" -H 'Content-Type: application/json' -H "Authorization: Bearer $token" --data-binary @"$tmp" "$url"
elif [ "$method" = GET ] && [ ! -s "$tmp" ]; then
curl -sS --max-time 20 -w '\n__HTTP_CODE__:%{http_code}' -X "$method" "$url"
else
curl -sS --max-time 20 -w '\n__HTTP_CODE__:%{http_code}' -X "$method" -H 'Content-Type: application/json' --data-binary @"$tmp" "$url"
fi
'''
if TARGET["runtimeMode"] == "host-docker":
command = ["sh", "-c", script, "sh", method, f"http://127.0.0.1:{TARGET['hostDockerAppPort']}{path}", bearer or ""]
else:
command = ["kubectl", "-n", TARGET["namespace"], "exec", "-i", APP_POD, "--", "sh", "-c", script, "sh", method, f"http://127.0.0.1:8080{path}", bearer or ""]
return parse_curl(run(command, body))
def data_of(response, label):
parsed = response.get("json")
code = parsed.get("code") if isinstance(parsed, dict) else None
if response.get("ok") is not True or (code is not None and code != 0):
message = parsed.get("message") if isinstance(parsed, dict) else tail_text(response.get("body"), 400)
raise RuntimeError(f"{label} failed: http={response.get('httpStatus')} message={message}")
return parsed.get("data") if isinstance(parsed, dict) and "data" in parsed else parsed
def items_of(data):
if isinstance(data, list):
return data
if isinstance(data, dict) and isinstance(data.get("items"), list):
return data["items"]
return []
def get(token, path, params=None, label="GET"):
suffix = "?" + urlencode(params) if params else ""
return data_of(curl_api("GET", path + suffix, bearer=token), label)
def login():
email = config_value("ADMIN_EMAIL")
password = secret_value("ADMIN_PASSWORD")
if not email or not password:
raise RuntimeError("Sub2API admin credentials are unavailable")
data = data_of(curl_api("POST", "/api/v1/auth/login", payload={"email": email, "password": password}), "admin login")
token = data.get("access_token") or data.get("token") if isinstance(data, dict) else None
if not token:
raise RuntimeError("admin login response has no token")
return token
def find_named(items, selector, label):
if selector is None:
return items
text = str(selector).strip().lower()
numeric = int(text) if text.isdigit() else None
matches = [item for item in items if (numeric is not None and item.get("id") == numeric) or str(item.get("id", "")).strip().lower() == text or str(item.get("name", "")).strip().lower() == text]
if not matches:
available = [f"{item.get('id')}:{item.get('name')}" for item in items]
raise RuntimeError(f"unknown {label} {selector}; available={available}")
return matches
def diagnosis(token):
group_params = {"platform": OPTIONS["platform"]} if OPTIONS.get("platform") else None
groups = items_of(get(token, "/api/v1/admin/groups/all", group_params, "list groups"))
groups = find_named(groups, OPTIONS.get("group"), "group")
snapshots = []
selected = groups if groups else [{"id": None, "name": "all", "platform": OPTIONS.get("platform") or "all"}]
for group in selected:
params = {"time_range": OPTIONS["timeRange"]}
platform = OPTIONS.get("platform") or group.get("platform")
if platform and platform != "all":
params["platform"] = platform
if group.get("id"):
params["group_id"] = group["id"]
overview = get(token, "/api/v1/admin/ops/dashboard/overview", params, "ops dashboard overview")
snapshots.append({"group": {"id": group.get("id"), "name": group.get("name"), "platform": platform or "all"}, "overview": overview})
evidence = None
diagnosis_id = OPTIONS.get("diagnosisId")
if diagnosis_id:
scope, _, category = diagnosis_id.partition(":")
group = next((item["group"] for item in snapshots if ("g" + str(item["group"].get("id"))) == scope or scope == "all"), None)
if group is None:
raise RuntimeError(f"diagnosis id scope not found: {diagnosis_id}")
params = {"time_range": OPTIONS["timeRange"], "page": 1, "page_size": 20}
if group.get("id"):
params["group_id"] = group["id"]
if group.get("platform") and group.get("platform") != "all":
params["platform"] = group["platform"]
if category.startswith("upstream-"):
endpoint = "/api/v1/admin/ops/upstream-errors"
elif category.startswith("error-") or category.startswith("sla-"):
endpoint = "/api/v1/admin/ops/request-errors"
else:
endpoint = "/api/v1/admin/ops/requests"
params["kind"] = "all"
params["sort"] = "duration_desc"
evidence = {"diagnosisId": diagnosis_id, "records": items_of(get(token, endpoint, params, "diagnosis evidence"))}
return {"groups": snapshots, "evidence": evidence}
def channels(token):
monitors = items_of(get(token, "/api/v1/channel-monitors", None, "list channel monitors"))
if OPTIONS.get("platform"):
monitors = [item for item in monitors if str(item.get("provider", "")).lower() == OPTIONS["platform"].lower()]
monitors = find_named(monitors, OPTIONS.get("channel"), "channel")
output = []
for monitor in monitors:
detail = get(token, f"/api/v1/channel-monitors/{monitor['id']}/status", None, "channel monitor status")
output.append({"summary": monitor, "detail": detail})
history = None
if OPTIONS.get("channel") and output:
monitor_id = output[0]["summary"]["id"]
params = {"limit": 1000}
if OPTIONS.get("model"):
params["model"] = OPTIONS["model"]
history = items_of(get(token, f"/api/v1/admin/channel-monitors/{monitor_id}/history", params, "channel monitor history"))
return {"channels": output, "history": history}
try:
token = login()
data = diagnosis(token) if OPTIONS["action"] == "diagnosis" else channels(token)
print(json.dumps({"ok": True, "sourceStatus": "available", "data": data}, ensure_ascii=False, separators=(",", ":")))
except Exception as error:
print(json.dumps({"ok": False, "sourceStatus": "unavailable", "error": str(error), "data": None}, ensure_ascii=False, separators=(",", ":")))
sys.exit(1)
PY
`;
}
@@ -0,0 +1,184 @@
import type { RenderedCliResult } from "../output";
export function renderSub2ApiOps(result: Record<string, unknown>): RenderedCliResult {
const action = String(result.action ?? "platform-infra-sub2api-ops");
const lines = action.endsWith("diagnosis") ? renderDiagnosis(result) : renderChannels(result);
return {
ok: result.ok !== false,
command: action.replaceAll("-", " "),
renderedText: lines.join("\n"),
contentType: "text/plain",
};
}
function renderDiagnosis(result: Record<string, unknown>): string[] {
const source = record(result.source);
const groups = arrayRecords(result.groups);
const rows = groups.flatMap((group) => arrayRecords(group.diagnoses).map((item) => {
const metric = record(item.metric);
return [
stringValue(item.id),
stringValue(item.severity).toUpperCase(),
`${stringValue(metric.value)} ${stringValue(metric.unit)}`.trim(),
stringValue(item.message),
`${stringValue(group.groupName)} / ${stringValue(group.platform)}`,
];
}));
const lines = [
"PLATFORM-INFRA SUB2API OPS DIAGNOSIS",
...table(["ID", "SEVERITY", "VALUE", "DIAGNOSIS", "SCOPE"], rows),
"",
"SOURCE",
...table(["FIELD", "VALUE"], [
["status", stringValue(source.status)],
["upstreamVersion", stringValue(source.upstreamVersion)],
["metrics", stringValue(source.metricsEndpoint)],
["advice", stringValue(source.projectionKind)],
["nativeDiagnosisEndpoint", stringValue(record(source.nativeDiagnosisEndpoint).status)],
]),
];
const selected = record(result.selectedDiagnosis);
if (Object.keys(selected).length > 0) {
const metric = record(selected.metric);
lines.push(
"",
"DETAIL",
...table(["FIELD", "VALUE"], [
["id", stringValue(selected.id)],
["category", stringValue(selected.category)],
["severity", stringValue(selected.severity)],
["metric", `${stringValue(metric.name)}=${stringValue(metric.value)} ${stringValue(metric.unit)}`.trim()],
["impact", stringValue(selected.impact)],
["nativeAdvice", stringValue(selected.advice, "-")],
["inference", stringValue(selected.inferenceStatus)],
]),
);
const evidence = record(result.evidence);
const evidenceRows = arrayRecords(evidence.records).map((item) => [
stringValue(item.requestId, "-"),
stringValue(item.createdAt, "-"),
stringValue(item.accountName, stringValue(item.accountId, "-")),
stringValue(item.statusCode, "-"),
stringValue(item.message, "-").replace(/\s+/gu, " ").slice(0, 80),
]);
lines.push("", "RELATED EVIDENCE", ...(evidenceRows.length === 0 ? ["-"] : table(["REQUEST_ID", "AT", "ACCOUNT", "STATUS", "SUMMARY"], evidenceRows)));
}
lines.push(
"",
"NEXT",
" detail: bun scripts/cli.ts platform-infra sub2api ops diagnosis --id <diagnosis-id>",
" json: bun scripts/cli.ts platform-infra sub2api ops diagnosis --json",
"",
`Evidence boundary: ${stringValue(source.evidenceBoundary)}`,
"Disclosure: one-shot read-only snapshot; no foreground refresh, Secret, token, or full log output.",
);
if (result.error) lines.push(`Error: ${stringValue(result.error)}`);
return lines;
}
function renderChannels(result: Record<string, unknown>): string[] {
const source = record(result.source);
const channels = arrayRecords(result.channels);
const rows = channels.map((channel) => {
const primary = record(channel.primary);
return [
stringValue(channel.id),
stringValue(channel.name),
stringValue(channel.provider),
stringValue(primary.model),
stringValue(channel.status),
stringValue(primary.latencyMs, "-"),
stringValue(primary.pingLatencyMs, "-"),
formatAvailability(primary.availability),
stringValue(channel.recentRecordCount, "0"),
stringValue(channel.collectedAt, "-"),
];
});
const lines = [
"PLATFORM-INFRA SUB2API OPS CHANNELS",
...table(["ID", "CHANNEL", "PLATFORM", "MODEL", "STATUS", "LATENCY_MS", "PING_MS", "AVAIL", "RECORDS", "COLLECTED_AT"], rows),
"",
"SUMMARY",
...table(["FIELD", "VALUE"], [
["overallStatus", stringValue(result.overallStatus)],
["window", stringValue(record(result.query).window)],
["sourceStatus", stringValue(source.status)],
["refreshRemaining", stringValue(record(source.refreshRemaining).status)],
]),
];
const history = record(result.history);
const historyRows = arrayRecords(history.records).map((item) => [
stringValue(item.id),
stringValue(item.model),
stringValue(item.status),
stringValue(item.latencyMs, "-"),
stringValue(item.pingLatencyMs, "-"),
stringValue(item.checkedAt, "-"),
]);
if (historyRows.length > 0) {
lines.push(
"",
"PAST → NOW",
...table(["ID", "MODEL", "STATUS", "LATENCY_MS", "PING_MS", "CHECKED_AT"], historyRows),
`pageSize=${stringValue(history.pageSize)} moreAvailable=${stringValue(history.moreAvailable)} nextPageToken=${stringValue(history.nextPageToken)}`,
);
}
const selectedRecord = record(history.selectedRecord);
if (Object.keys(selectedRecord).length > 0) {
lines.push(
"",
"RECORD",
...table(["FIELD", "VALUE"], [
["id", stringValue(selectedRecord.id)],
["model", stringValue(selectedRecord.model)],
["status", stringValue(selectedRecord.status)],
["latencyMs", stringValue(selectedRecord.latencyMs)],
["pingLatencyMs", stringValue(selectedRecord.pingLatencyMs)],
["checkedAt", stringValue(selectedRecord.checkedAt)],
["message", stringValue(selectedRecord.message)],
]),
);
}
lines.push(
"",
"NEXT",
" channel: bun scripts/cli.ts platform-infra sub2api ops channels --channel <id-or-name>",
" window: bun scripts/cli.ts platform-infra sub2api ops channels --window 7d|15d|30d",
" older page: bun scripts/cli.ts platform-infra sub2api ops channels --channel <id-or-name> --page-token <record-id>",
" record: bun scripts/cli.ts platform-infra sub2api ops channels --channel <id-or-name> --record <record-id>",
" json: bun scripts/cli.ts platform-infra sub2api ops channels --json",
"",
"Disclosure: one-shot read-only snapshot; native response has no refresh countdown, and the CLI does not simulate one.",
);
if (result.error) lines.push(`Error: ${stringValue(result.error)}`);
return lines;
}
function formatAvailability(value: unknown): string {
return typeof value === "number" && Number.isFinite(value) ? `${value.toFixed(2)}%` : stringValue(value, "-");
}
function table(headers: string[], rows: string[][]): string[] {
if (rows.length === 0) return ["-"];
const widths = headers.map((header, index) => Math.max(header.length, ...rows.map((row) => visibleLength(row[index] ?? ""))));
const format = (row: string[]): string => row.map((cell, index) => `${cell}${" ".repeat(Math.max(0, widths[index]! - visibleLength(cell)))}`).join(" ").trimEnd();
return [format(headers), format(widths.map((width) => "-".repeat(width))), ...rows.map(format)];
}
function visibleLength(value: string): number {
return [...value].length;
}
function record(value: unknown): Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
}
function arrayRecords(value: unknown): Record<string, unknown>[] {
return Array.isArray(value) ? value.map(record) : [];
}
function stringValue(value: unknown, fallback = "-"): string {
if (typeof value === "string") return value.length > 0 ? value : fallback;
if (typeof value === "number" || typeof value === "boolean") return String(value);
return fallback;
}
@@ -0,0 +1,26 @@
export type Sub2ApiOpsAction = "diagnosis" | "channels";
export interface Sub2ApiOpsOptions {
action: Sub2ApiOpsAction;
targetId: string;
json: boolean;
platform: string | null;
group: string | null;
diagnosisId: string | null;
timeRange: "5m" | "30m" | "1h" | "6h" | "24h";
channel: string | null;
model: string | null;
window: "7d" | "15d" | "30d";
pageToken: string | null;
recordId: string | null;
}
export interface Sub2ApiOpsTarget {
id: string;
route: string;
namespace: string;
runtimeMode: "k3s" | "host-docker";
hostDockerAppPort: number | null;
hostDockerEnvPath: string | null;
appSecretName: string;
}
+2
View File
@@ -417,6 +417,8 @@ export function platformInfraHelp(): unknown {
"bun scripts/cli.ts platform-infra sub2api apply [--target G14|D601] --confirm",
"bun scripts/cli.ts platform-infra sub2api status [--target G14|D601] [--full|--raw]",
"bun scripts/cli.ts platform-infra sub2api validate [--target G14|D601] [--full|--raw]",
"bun scripts/cli.ts platform-infra sub2api ops diagnosis [--target PK01] [--platform <name>] [--group <id-or-name>] [--time-range 5m|30m|1h|6h|24h] [--id <diagnosis-id>] [--json]",
"bun scripts/cli.ts platform-infra sub2api ops channels [--target PK01] [--platform <name>] [--channel <id-or-name>] [--model <name>] [--window 7d|15d|30d] [--page-token <record-id>|--record <record-id>] [--json]",
"bun scripts/cli.ts platform-infra sub2api codex-pool plan",
"bun scripts/cli.ts platform-infra sub2api codex-pool sync --confirm",
"bun scripts/cli.ts platform-infra sub2api codex-pool validate",
+4
View File
@@ -108,6 +108,10 @@ export async function runPlatformInfraCommand(config: UniDeskConfig, args: strin
return options.full || options.raw ? result : renderSub2ApiStatus(result);
}
if (action === "validate") return await validate(config, parseDisclosureOptions(args.slice(2)));
if (action === "ops") {
const { runSub2ApiOpsCommand } = await import("../platform-infra-sub2api-ops");
return await runSub2ApiOpsCommand(config, args.slice(2));
}
if (action === "codex-pool") {
const { runCodexPoolCommand } = await import("../platform-infra-sub2api-codex");
return await runCodexPoolCommand(config, args.slice(2));