@harrylabsj/kiwi 0.12.7 → 0.12.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/a2a/node.js +17 -1
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/server/server.js +4 -1
- package/dist/a2a/server/server.js.map +1 -1
- package/dist/agent/merchant/merchant-client.js +2 -1
- package/dist/agent/merchant/merchant-client.js.map +1 -1
- package/dist/cli.js +218 -8
- package/dist/cli.js.map +1 -1
- package/dist/cloud/binding/enrollment-heartbeat.js +46 -4
- package/dist/cloud/binding/enrollment-heartbeat.js.map +1 -1
- package/dist/config/credentials.d.ts +22 -0
- package/dist/config/credentials.js +16 -0
- package/dist/config/credentials.js.map +1 -0
- package/dist/merchant-credential-rotation.d.ts +48 -0
- package/dist/merchant-credential-rotation.js +382 -0
- package/dist/merchant-credential-rotation.js.map +1 -0
- package/dist/merchant-runtime/credential-consumer-proof.d.ts +22 -0
- package/dist/merchant-runtime/credential-consumer-proof.js +73 -0
- package/dist/merchant-runtime/credential-consumer-proof.js.map +1 -0
- package/dist/merchant-runtime/credential-doctor.d.ts +23 -0
- package/dist/merchant-runtime/credential-doctor.js +227 -0
- package/dist/merchant-runtime/credential-doctor.js.map +1 -0
- package/dist/merchant-runtime/credential-rotation-scheduler.d.ts +26 -0
- package/dist/merchant-runtime/credential-rotation-scheduler.js +40 -0
- package/dist/merchant-runtime/credential-rotation-scheduler.js.map +1 -0
- package/dist/merchant-runtime/manager.d.ts +32 -3
- package/dist/merchant-runtime/manager.js +101 -14
- package/dist/merchant-runtime/manager.js.map +1 -1
- package/dist/merchant-runtime/services.d.ts +5 -0
- package/dist/merchant-runtime/services.js +4 -0
- package/dist/merchant-runtime/services.js.map +1 -1
- package/dist/product-cli.js +4 -0
- package/dist/product-cli.js.map +1 -1
- package/dist/product-init.d.ts +19 -2
- package/dist/product-init.js +53 -5
- package/dist/product-init.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
import { spawn, spawnSync } from "node:child_process";
|
|
31
31
|
import { DatabaseSync } from "node:sqlite";
|
|
32
32
|
import { isIP } from "node:net";
|
|
33
|
-
import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync, } from "node:fs";
|
|
33
|
+
import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync, } from "node:fs";
|
|
34
34
|
import { homedir, userInfo } from "node:os";
|
|
35
35
|
import path from "node:path";
|
|
36
36
|
import { fileURLToPath } from "node:url";
|
|
@@ -40,6 +40,7 @@ import { runChatTui } from "./agent/chat-tui.js";
|
|
|
40
40
|
import { buildChatKernel, buildClient, defaultChatProfile } from "./agent/kernel-builder.js";
|
|
41
41
|
import { AgentKernel } from "./agent/kernel.js";
|
|
42
42
|
import { loadProfile, ProfileError } from "./config/profile.js";
|
|
43
|
+
import { catalogOwnerTokenEnv, DEFAULT_CATALOG_OWNER_TOKEN_ENV, shoppingCredentialIsSeparate, shoppingTokenEnv, } from "./config/credentials.js";
|
|
43
44
|
import { CommerceError } from "./commerce/types.js";
|
|
44
45
|
import { runDoctor } from "./doctor.js";
|
|
45
46
|
import { EXIT } from "./exit-codes.js";
|
|
@@ -69,6 +70,9 @@ import { MerchantJobs, standardMerchantJobs } from "./merchant-runtime/jobs.js";
|
|
|
69
70
|
import { runBackup } from "./merchant-runtime/backup.js";
|
|
70
71
|
import { connectMerchant } from "./cloud/merchant-connect.js";
|
|
71
72
|
import { rotateEnrollmentSigningKey } from "./cloud/binding/enrollment-key-recovery.js";
|
|
73
|
+
import { credentialFingerprint, rotateMerchantCredential } from "./merchant-credential-rotation.js";
|
|
74
|
+
import { runMerchantCredentialDoctor } from "./merchant-runtime/credential-doctor.js";
|
|
75
|
+
import { runCredentialRotationSchedule, writeCredentialRotationScheduleStatus } from "./merchant-runtime/credential-rotation-scheduler.js";
|
|
72
76
|
function readRegistrationStatus(dataDir) {
|
|
73
77
|
try {
|
|
74
78
|
const value = JSON.parse(readFileSync(path.join(dataDir, "registration.json"), "utf8"));
|
|
@@ -98,6 +102,20 @@ function countPendingMerchantCommands(dataDir) {
|
|
|
98
102
|
return undefined;
|
|
99
103
|
}
|
|
100
104
|
}
|
|
105
|
+
function merchantCredentialRotationSettings(env = process.env) {
|
|
106
|
+
const parse = (name, fallback, minimum, maximum) => {
|
|
107
|
+
const raw = env[name]?.trim();
|
|
108
|
+
if (!raw)
|
|
109
|
+
return fallback;
|
|
110
|
+
const value = Number(raw);
|
|
111
|
+
return Number.isSafeInteger(value) && value >= minimum && value <= maximum ? value : undefined;
|
|
112
|
+
};
|
|
113
|
+
const pollIntervalSeconds = parse("KIWI_MERCHANT_TOKEN_ROTATION_POLL_SECONDS", 300, 60, 300);
|
|
114
|
+
const rotateBeforeSeconds = parse("KIWI_MERCHANT_TOKEN_ROTATE_BEFORE_SECONDS", 7 * 86400, 3600, 30 * 86400);
|
|
115
|
+
if (pollIntervalSeconds === undefined || rotateBeforeSeconds === undefined)
|
|
116
|
+
return { error: "invalid_rotation_schedule_env" };
|
|
117
|
+
return { pollIntervalSeconds, rotateBeforeSeconds };
|
|
118
|
+
}
|
|
101
119
|
import { agentDirName, DEFAULT_AGENTS_ROOT } from "./agent/agent-db.js";
|
|
102
120
|
import { writeFileAtomic } from "./fs/atomic-write.js";
|
|
103
121
|
import { ProfileCredentialBroker } from "./agent/merchant/credential-broker.js";
|
|
@@ -107,7 +125,7 @@ import { parseLogLines, runLogs } from "./supervisor/logs.js";
|
|
|
107
125
|
import { StackConfigError } from "./supervisor/stack-config.js";
|
|
108
126
|
import { cmdProductDoctor, productHelp, PRODUCT_VERSION, DEFAULT_CATALOG_URL, DEFAULT_PROFILE_PATH, } from "./product-cli.js";
|
|
109
127
|
import { cmdWeixin, weixinUsage } from "./weixin/cli-weixin.js";
|
|
110
|
-
import { DEFAULT_SHOPPING_DB_PATH, loadMerchantCredentials, merchantInit, slugifyMerchantId, } from "./product-init.js";
|
|
128
|
+
import { DEFAULT_SHOPPING_DB_PATH, DEFAULT_CREDENTIALS_ENV_PATH, loadMerchantCredentials, reloadMerchantCredential, inspectMerchantCredentialStorage, merchantInit, slugifyMerchantId, } from "./product-init.js";
|
|
111
129
|
import readline from "node:readline";
|
|
112
130
|
import { buyerInit, buyerSearch, buyerTasks } from "./product-buyer.js";
|
|
113
131
|
import { supplierList, supplierMetrics, supplierPause, supplierPrefer, supplierRemove, supplierSave, supplierWatch, } from "./product-supplier.js";
|
|
@@ -1204,7 +1222,7 @@ async function cmdBuyerTasks(args) {
|
|
|
1204
1222
|
/**
|
|
1205
1223
|
* 产品层 merchant 命令树(rev1.1 §2.3/§19 D1-D3)。
|
|
1206
1224
|
* - start = agent serve 别名(Merchant A2A server + 注册 Kiwi Network);
|
|
1207
|
-
* - init/publish/listings/status
|
|
1225
|
+
* - init/publish/listings/status = 骨架;merchant doctor 与凭据恢复已实现。
|
|
1208
1226
|
*/
|
|
1209
1227
|
/**
|
|
1210
1228
|
* A199:本地 owner 会话 CLI(用户批准件范围)——仅本地受信进程内调用。
|
|
@@ -1373,6 +1391,8 @@ async function routeMerchant(sub, args) {
|
|
|
1373
1391
|
return await cmdMerchantRuntime(args);
|
|
1374
1392
|
if (sub === "connect")
|
|
1375
1393
|
return await cmdMerchantConnect(args);
|
|
1394
|
+
if (sub === "credentials")
|
|
1395
|
+
return await cmdMerchantCredentials(args);
|
|
1376
1396
|
if (sub === "init")
|
|
1377
1397
|
return await cmdMerchantInit(args);
|
|
1378
1398
|
if (sub === "publish")
|
|
@@ -1388,10 +1408,138 @@ async function routeMerchant(sub, args) {
|
|
|
1388
1408
|
if (sub === "status")
|
|
1389
1409
|
return notImplementedProduct("kiwi merchant status", "D1");
|
|
1390
1410
|
if (sub === "doctor")
|
|
1391
|
-
return
|
|
1411
|
+
return await cmdMerchantDoctor(args);
|
|
1392
1412
|
process.stderr.write(`unknown merchant command: ${sub}\n`);
|
|
1393
1413
|
return EXIT.CONFIG;
|
|
1394
1414
|
}
|
|
1415
|
+
async function cmdMerchantDoctor(args) {
|
|
1416
|
+
try {
|
|
1417
|
+
const profile = requireProfileOrDefault(args);
|
|
1418
|
+
if (profile.role !== "merchant")
|
|
1419
|
+
throw new Error("kiwi merchant doctor requires a merchant profile");
|
|
1420
|
+
const dirs = resolveMerchantMcpDirs({
|
|
1421
|
+
...(configuredDataDir(args, profile) !== undefined ? { dataDir: configuredDataDir(args, profile) } : {}),
|
|
1422
|
+
agentId: profile.agent_id,
|
|
1423
|
+
});
|
|
1424
|
+
const cliEntry = fileURLToPath(import.meta.url);
|
|
1425
|
+
const profileArgs = args.profile !== undefined ? ["--profile", args.profile] : [];
|
|
1426
|
+
const manager = new MerchantRuntimeManager({
|
|
1427
|
+
dir: dirs.merchantDataDir,
|
|
1428
|
+
readOnly: true,
|
|
1429
|
+
services: buildRuntimeServiceSpecs({
|
|
1430
|
+
dirs, cliEntry, profileArgs,
|
|
1431
|
+
credentialEnvFile: { path: DEFAULT_CREDENTIALS_ENV_PATH, names: [profile.commerce.token_env] },
|
|
1432
|
+
credentialSwitchMarker: path.join(dirs.merchantDataDir, "runtime", "credential-rotation.lock"),
|
|
1433
|
+
}),
|
|
1434
|
+
});
|
|
1435
|
+
const report = await runMerchantCredentialDoctor({ profile, dataDir: dirs.merchantDataDir, services: manager.status() });
|
|
1436
|
+
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
1437
|
+
return report.status === "ok" ? EXIT.OK : report.status === "unknown" ? EXIT.TRANSIENT : EXIT.CONFIG;
|
|
1438
|
+
}
|
|
1439
|
+
catch (err) {
|
|
1440
|
+
process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
|
|
1441
|
+
return EXIT.CONFIG;
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
function merchantCredentialRotationOptions(profile, dirs, manager) {
|
|
1445
|
+
const markerPath = path.join(dirs.merchantDataDir, "runtime", "credential-rotation.lock");
|
|
1446
|
+
const proofPath = path.join(dirs.merchantDataDir, "runtime", "merchant-credential-consumer.json");
|
|
1447
|
+
return {
|
|
1448
|
+
baseUrl: process.env.KIWI_COMMERCE_URL ?? profile.commerce.base_url,
|
|
1449
|
+
merchantId: profile.agent_id,
|
|
1450
|
+
tokenEnv: profile.commerce.token_env,
|
|
1451
|
+
credentialsPath: DEFAULT_CREDENTIALS_ENV_PATH,
|
|
1452
|
+
pendingPath: path.join(homedir(), ".kiwi", "credential-rotation.json"),
|
|
1453
|
+
operationLockPath: path.join(homedir(), ".kiwi", "credential-rotation-operation.lock"),
|
|
1454
|
+
runningServices: () => manager.status().filter((entry) => entry.running).map((entry) => entry.name),
|
|
1455
|
+
beginRuntimeSwitch: () => writeFileAtomic(markerPath, `${JSON.stringify({ merchant_id: profile.agent_id, started_at: new Date().toISOString() })}\n`, { mode: 0o600 }),
|
|
1456
|
+
endRuntimeSwitch: () => { rmSync(markerPath, { force: true }); },
|
|
1457
|
+
waitForRuntimeCredential: async (token) => {
|
|
1458
|
+
const expected = credentialFingerprint(token);
|
|
1459
|
+
const deadline = Date.now() + 95_000;
|
|
1460
|
+
let sawA2a = false;
|
|
1461
|
+
while (Date.now() < deadline) {
|
|
1462
|
+
try {
|
|
1463
|
+
const a2a = manager.status().find((entry) => entry.name === "a2a" && entry.running);
|
|
1464
|
+
if (a2a?.pid !== undefined)
|
|
1465
|
+
sawA2a = true;
|
|
1466
|
+
const observed = JSON.parse(readFileSync(proofPath, "utf8"));
|
|
1467
|
+
if (a2a?.pid !== undefined && observed.merchant_id === profile.agent_id && observed.token_env === profile.commerce.token_env &&
|
|
1468
|
+
observed.credential_fingerprint === expected && observed.pid === a2a.pid)
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
1471
|
+
catch { /* wait for the A2A child to record its protected API proof */ }
|
|
1472
|
+
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
1473
|
+
}
|
|
1474
|
+
throw new Error(sawA2a
|
|
1475
|
+
? "A2A child did not prove the new credential against the protected merchant endpoint; confirmation is withheld and recovery state is retained"
|
|
1476
|
+
: "managed A2A service is not running, so no process can attest the new credential; start it and re-run `kiwi merchant credentials recover`");
|
|
1477
|
+
},
|
|
1478
|
+
stopServices: async (services) => {
|
|
1479
|
+
for (const name of [...services].reverse())
|
|
1480
|
+
await manager.stop(name);
|
|
1481
|
+
const deadline = Date.now() + 5_000;
|
|
1482
|
+
while (Date.now() < deadline && manager.status().some((entry) => services.includes(entry.name) && entry.running)) {
|
|
1483
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
1484
|
+
}
|
|
1485
|
+
if (manager.status().some((entry) => services.includes(entry.name) && entry.running)) {
|
|
1486
|
+
throw new Error("managed services have not stopped; rotation remains pending and recoverable");
|
|
1487
|
+
}
|
|
1488
|
+
if (services.length > 0)
|
|
1489
|
+
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
1490
|
+
},
|
|
1491
|
+
startRuntime: async (services) => {
|
|
1492
|
+
for (const name of services)
|
|
1493
|
+
await manager.start(name);
|
|
1494
|
+
if (services.length > 0) {
|
|
1495
|
+
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
1496
|
+
const status = new Map(manager.status().map((entry) => [entry.name, entry.running]));
|
|
1497
|
+
if (services.some((name) => status.get(name) !== true)) {
|
|
1498
|
+
throw new Error("managed service did not remain running with the new credential; rotation remains recoverable");
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
},
|
|
1502
|
+
};
|
|
1503
|
+
}
|
|
1504
|
+
async function cmdMerchantCredentials(args) {
|
|
1505
|
+
const action = args.command[2];
|
|
1506
|
+
if (action !== "rotate" && action !== "recover") {
|
|
1507
|
+
process.stderr.write("usage: kiwi merchant credentials <rotate|recover> [--profile <file>]\n");
|
|
1508
|
+
return EXIT.CONFIG;
|
|
1509
|
+
}
|
|
1510
|
+
try {
|
|
1511
|
+
const profile = requireProfileOrDefault(args);
|
|
1512
|
+
if (profile.role !== "merchant")
|
|
1513
|
+
throw new Error("kiwi merchant credentials requires a merchant profile");
|
|
1514
|
+
if (profile.commerce.credentials?.catalog?.token_env === profile.commerce.token_env) {
|
|
1515
|
+
throw new Error(`shopping-cli token env (${profile.commerce.token_env}) is also configured as the Catalog credential; separate commerce.token_env to SHOPPING_MERCHANT_TOKEN before rotation`);
|
|
1516
|
+
}
|
|
1517
|
+
const pendingPath = path.join(homedir(), ".kiwi", "credential-rotation.json");
|
|
1518
|
+
if (action === "recover" && !existsSync(pendingPath))
|
|
1519
|
+
throw new Error("there is no pending merchant credential rotation to recover");
|
|
1520
|
+
const dirs = resolveMerchantMcpDirs({
|
|
1521
|
+
...(configuredDataDir(args, profile) !== undefined ? { dataDir: configuredDataDir(args, profile) } : {}),
|
|
1522
|
+
agentId: profile.agent_id,
|
|
1523
|
+
});
|
|
1524
|
+
const cliEntry = fileURLToPath(import.meta.url);
|
|
1525
|
+
const profileArgs = args.profile !== undefined ? ["--profile", args.profile] : [];
|
|
1526
|
+
const manager = new MerchantRuntimeManager({
|
|
1527
|
+
dir: dirs.merchantDataDir,
|
|
1528
|
+
services: buildRuntimeServiceSpecs({
|
|
1529
|
+
dirs, cliEntry, profileArgs,
|
|
1530
|
+
credentialEnvFile: { path: DEFAULT_CREDENTIALS_ENV_PATH, names: [profile.commerce.token_env] },
|
|
1531
|
+
credentialSwitchMarker: path.join(dirs.merchantDataDir, "runtime", "credential-rotation.lock"),
|
|
1532
|
+
}),
|
|
1533
|
+
});
|
|
1534
|
+
const result = await rotateMerchantCredential(merchantCredentialRotationOptions(profile, dirs, manager));
|
|
1535
|
+
process.stdout.write(`${result.recovered ? "待完成的" : "新的"}商家凭据轮换已确认。受管服务已切换并重启:${result.restarted.join(", ") || "当前无运行中的服务"}。\n`);
|
|
1536
|
+
return EXIT.OK;
|
|
1537
|
+
}
|
|
1538
|
+
catch (err) {
|
|
1539
|
+
process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
|
|
1540
|
+
return EXIT.CONFIG;
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1395
1543
|
/** 本机执行设备授权;实际 Runtime 持钥留在本机文件,远端控制器不伪造其身份。 */
|
|
1396
1544
|
async function cmdMerchantConnect(args) {
|
|
1397
1545
|
try {
|
|
@@ -1481,7 +1629,11 @@ async function cmdMerchantRuntime(args) {
|
|
|
1481
1629
|
const manager = new MerchantRuntimeManager({
|
|
1482
1630
|
dir: dirs.merchantDataDir,
|
|
1483
1631
|
// BUG-04:子进程显式携带统一 --data-dir 与确定 cwd(三槽位解析结果)。
|
|
1484
|
-
services: buildRuntimeServiceSpecs({
|
|
1632
|
+
services: buildRuntimeServiceSpecs({
|
|
1633
|
+
dirs, cliEntry, profileArgs,
|
|
1634
|
+
credentialEnvFile: { path: DEFAULT_CREDENTIALS_ENV_PATH, names: [profile.commerce.token_env] },
|
|
1635
|
+
credentialSwitchMarker: path.join(dirs.merchantDataDir, "runtime", "credential-rotation.lock"),
|
|
1636
|
+
}),
|
|
1485
1637
|
});
|
|
1486
1638
|
if (action === "status") {
|
|
1487
1639
|
process.stdout.write(`${JSON.stringify(manager.status(), null, 2)}\n`);
|
|
@@ -1518,6 +1670,9 @@ async function cmdMerchantRuntime(args) {
|
|
|
1518
1670
|
for (const job of standardMerchantJobs({
|
|
1519
1671
|
healthPoll: async () => {
|
|
1520
1672
|
if (probeClient !== undefined) {
|
|
1673
|
+
const shoppingToken = reloadMerchantCredential(profile.commerce.token_env) ?? process.env[profile.commerce.token_env] ?? "";
|
|
1674
|
+
if (shoppingToken === "")
|
|
1675
|
+
throw new Error(`${profile.commerce.token_env} is not configured`);
|
|
1521
1676
|
await probeClient.probeCapabilities({ persistPath: probePath });
|
|
1522
1677
|
}
|
|
1523
1678
|
else {
|
|
@@ -1545,7 +1700,53 @@ async function cmdMerchantRuntime(args) {
|
|
|
1545
1700
|
})) {
|
|
1546
1701
|
jobs.register(job);
|
|
1547
1702
|
}
|
|
1703
|
+
const rotationSettings = merchantCredentialRotationSettings();
|
|
1704
|
+
const rotationStatusPath = path.join(dirs.merchantDataDir, "runtime", "credential-rotation-schedule.json");
|
|
1705
|
+
const credentialStorage = inspectMerchantCredentialStorage(profile.commerce.token_env);
|
|
1706
|
+
const credentialScopeSeparate = shoppingCredentialIsSeparate(profile);
|
|
1707
|
+
let rotationScheduleStatus = {
|
|
1708
|
+
version: 1,
|
|
1709
|
+
merchant_id: profile.agent_id,
|
|
1710
|
+
enabled: rotationSettings !== undefined && "pollIntervalSeconds" in rotationSettings && credentialScopeSeparate && credentialStorage.status === "ready",
|
|
1711
|
+
poll_interval_seconds: rotationSettings !== undefined && "pollIntervalSeconds" in rotationSettings ? rotationSettings.pollIntervalSeconds : 300,
|
|
1712
|
+
rotate_before_seconds: rotationSettings !== undefined && "rotateBeforeSeconds" in rotationSettings ? rotationSettings.rotateBeforeSeconds : 7 * 86400,
|
|
1713
|
+
supervisor_pid: process.pid,
|
|
1714
|
+
started_at: new Date().toISOString(),
|
|
1715
|
+
...("error" in rotationSettings ? { disabled_reason: "invalid_schedule_config" } : {}),
|
|
1716
|
+
...(!credentialScopeSeparate ? { disabled_reason: "catalog_token_ref_conflict" } : {}),
|
|
1717
|
+
...(credentialStorage.status !== "ready" ? { disabled_reason: "credential_file_unavailable" } : {}),
|
|
1718
|
+
};
|
|
1719
|
+
writeCredentialRotationScheduleStatus(rotationStatusPath, rotationScheduleStatus);
|
|
1720
|
+
let firstRotationCheck;
|
|
1721
|
+
if (rotationScheduleStatus.enabled && rotationSettings !== undefined && "pollIntervalSeconds" in rotationSettings) {
|
|
1722
|
+
jobs.register({
|
|
1723
|
+
name: "merchant-credential-rotation",
|
|
1724
|
+
intervalMs: rotationSettings.pollIntervalSeconds * 1000,
|
|
1725
|
+
run: async () => {
|
|
1726
|
+
rotationScheduleStatus = await runCredentialRotationSchedule({
|
|
1727
|
+
statusPath: rotationStatusPath,
|
|
1728
|
+
status: rotationScheduleStatus,
|
|
1729
|
+
rotation: {
|
|
1730
|
+
...merchantCredentialRotationOptions(profile, dirs, manager),
|
|
1731
|
+
rotateBeforeSeconds: rotationSettings.rotateBeforeSeconds,
|
|
1732
|
+
beforeRotation: () => {
|
|
1733
|
+
if (!manager.status().some((service) => service.name === "a2a" && service.running)) {
|
|
1734
|
+
throw new Error("managed A2A service is not running; automatic token rotation is deferred until its protected credential proof can be checked");
|
|
1735
|
+
}
|
|
1736
|
+
},
|
|
1737
|
+
},
|
|
1738
|
+
}).catch((error) => {
|
|
1739
|
+
process.stderr.write("[kiwi] automatic merchant token rotation failed; inspect `kiwi merchant doctor` and recover before expiry if needed.\n");
|
|
1740
|
+
throw error;
|
|
1741
|
+
});
|
|
1742
|
+
},
|
|
1743
|
+
});
|
|
1744
|
+
}
|
|
1548
1745
|
jobs.start();
|
|
1746
|
+
if (rotationScheduleStatus.enabled) {
|
|
1747
|
+
firstRotationCheck = setTimeout(() => { void jobs.runOnce("merchant-credential-rotation"); }, 120_000);
|
|
1748
|
+
firstRotationCheck.unref();
|
|
1749
|
+
}
|
|
1549
1750
|
// Node.js 在没有活动 I/O 句柄时会退出;runtime manager 的受管子进程
|
|
1550
1751
|
// 使用 detached + unref,单靠下面的 never-resolving Promise 不能维持
|
|
1551
1752
|
// systemd 主进程。显式保活,确保 7×24 监控/备份/自动重启持续运行。
|
|
@@ -1554,6 +1755,8 @@ async function cmdMerchantRuntime(args) {
|
|
|
1554
1755
|
await manager.supervise();
|
|
1555
1756
|
const shutdown = async () => {
|
|
1556
1757
|
clearInterval(keepAlive);
|
|
1758
|
+
if (firstRotationCheck !== undefined)
|
|
1759
|
+
clearTimeout(firstRotationCheck);
|
|
1557
1760
|
jobs.stop();
|
|
1558
1761
|
await manager.shutdown();
|
|
1559
1762
|
process.exit(0);
|
|
@@ -1800,9 +2003,11 @@ async function cmdMerchantInit(args) {
|
|
|
1800
2003
|
let merchantId = args.merchantId ?? process.env.KIWI_MERCHANT_ID ?? "";
|
|
1801
2004
|
let name = args.merchantName ?? process.env.KIWI_MERCHANT_NAME ?? "";
|
|
1802
2005
|
const outputPath = args.output ?? args.profile ?? process.env.KIWI_DEFAULT_PROFILE ?? DEFAULT_PROFILE_PATH;
|
|
2006
|
+
let existingProfile;
|
|
1803
2007
|
if (existsSync(outputPath) && !args.force && merchantId === "" && name === "") {
|
|
1804
2008
|
try {
|
|
1805
2009
|
const existing = loadProfile(outputPath);
|
|
2010
|
+
existingProfile = existing;
|
|
1806
2011
|
if (existing.role === "merchant") {
|
|
1807
2012
|
merchantId = existing.agent_id;
|
|
1808
2013
|
name = existing.name ?? existing.owner_id;
|
|
@@ -1842,7 +2047,12 @@ async function cmdMerchantInit(args) {
|
|
|
1842
2047
|
}
|
|
1843
2048
|
if (merchantToken === "") {
|
|
1844
2049
|
loadMerchantCredentials();
|
|
1845
|
-
|
|
2050
|
+
// 这里要的是 **shopping-cli** 令牌;`KIWI_MERCHANT_TOKEN` 是 Kiwi Catalog 的
|
|
2051
|
+
// owner token,把它填进 SHOPPING_MERCHANT_TOKEN 会让数据引擎全部 401,还把
|
|
2052
|
+
// catalog 凭据复制到另一个槽位。
|
|
2053
|
+
const shoppingEnv = existingProfile !== undefined ? shoppingTokenEnv(existingProfile) : "SHOPPING_MERCHANT_TOKEN";
|
|
2054
|
+
const catalogEnv = existingProfile !== undefined ? catalogOwnerTokenEnv(existingProfile) : DEFAULT_CATALOG_OWNER_TOKEN_ENV;
|
|
2055
|
+
merchantToken = shoppingEnv === catalogEnv ? "" : (process.env[shoppingEnv] ?? "");
|
|
1846
2056
|
if (merchantToken === "")
|
|
1847
2057
|
merchantToken = await promptSecret("shopping-cli 商品服务令牌(回车稍后配置;输入不回显): ");
|
|
1848
2058
|
}
|
|
@@ -1879,7 +2089,7 @@ async function cmdMerchantInit(args) {
|
|
|
1879
2089
|
autoInstallShoppingCli: !args.noInstall,
|
|
1880
2090
|
});
|
|
1881
2091
|
if (merchantToken !== "")
|
|
1882
|
-
process.env.
|
|
2092
|
+
process.env.SHOPPING_MERCHANT_TOKEN = merchantToken;
|
|
1883
2093
|
else
|
|
1884
2094
|
loadMerchantCredentials();
|
|
1885
2095
|
let sourceCapabilities = report.steps.product_source_capabilities;
|
|
@@ -1937,7 +2147,7 @@ async function cmdMerchantInit(args) {
|
|
|
1937
2147
|
process.stderr.write("尚未提供公网 Runtime 地址;Catalog 账号授权和绑定未完成。先让 Agent Card 可经 HTTPS 公网访问,再设置 --public-url 重跑 init 继续。\n");
|
|
1938
2148
|
}
|
|
1939
2149
|
if (args.merchantToken !== undefined && report.ok) {
|
|
1940
|
-
process.stderr.write("shopping-cli
|
|
2150
|
+
process.stderr.write("shopping-cli 商家令牌已写入 ~/.kiwi/credentials.env(0600);Kiwi Catalog 凭据保持独立。\n");
|
|
1941
2151
|
}
|
|
1942
2152
|
return report.ok ? EXIT.OK : EXIT.CONFIG;
|
|
1943
2153
|
}
|