@gobing-ai/knowledge-kit 0.1.29 → 0.1.30

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.
Files changed (114) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/dist/board/api.js +8724 -3861
  3. package/dist/board/collect/index.js +69 -1
  4. package/dist/board/daily-voice/index.js +474 -265
  5. package/dist/board/itc/index.js +69 -1
  6. package/dist/board/wiki/index.css +1305 -0
  7. package/dist/board/wiki/index.js +1621 -0
  8. package/dist/index.js +34689 -28532
  9. package/package.json +1 -1
  10. package/plugins/generations/content-gen/dist/index.js +14 -1
  11. package/plugins/generations/core-facts-gen/dist/index.js +14 -1
  12. package/plugins/generations/daily-article-gen/dist/index.js +14 -1
  13. package/plugins/generations/dailynews-gen/dist/index.js +14 -1
  14. package/plugins/generations/episode-plan-gen/dist/index.js +32 -2
  15. package/plugins/generations/episode-plan-gen/src/index.ts +19 -1
  16. package/plugins/generations/gemini-tts/dist/index.js +14 -1
  17. package/plugins/generations/image-gen/dist/index.js +14 -1
  18. package/plugins/generations/music-gen/dist/index.js +15 -2
  19. package/plugins/generations/news-report-gen/dist/index.js +14 -1
  20. package/plugins/generations/vibe-voice-tts/dist/index.js +14 -1
  21. package/plugins/generations/video-gen/README.md +245 -8
  22. package/plugins/generations/video-gen/dist/index.js +22447 -4763
  23. package/plugins/generations/video-gen/package.json +1 -0
  24. package/plugins/generations/video-gen/presets/formats/avatar.yaml +5 -0
  25. package/plugins/generations/video-gen/presets/formats/editorial.yaml +6 -0
  26. package/plugins/generations/video-gen/presets/formats/hybrid-pip.yaml +5 -0
  27. package/plugins/generations/video-gen/presets/styles/daily-ai.yaml +5 -0
  28. package/plugins/generations/video-gen/presets/styles/neutral.yaml +3 -0
  29. package/plugins/generations/video-gen/python/colab_comfyui_h3.py +322 -0
  30. package/plugins/generations/video-gen/python/comfyui-h3-i2v-api.json +47 -0
  31. package/plugins/generations/video-gen/python/comfyui-h3-models.json +32 -0
  32. package/plugins/generations/video-gen/python/comfyui_model_cache.py +388 -0
  33. package/plugins/generations/video-gen/python/drive_client.py +187 -0
  34. package/plugins/generations/video-gen/src/brief-cli.ts +65 -0
  35. package/plugins/generations/video-gen/src/brief.ts +464 -0
  36. package/plugins/generations/video-gen/src/ccu-ledger.ts +206 -3
  37. package/plugins/generations/video-gen/src/colab-runner.ts +346 -35
  38. package/plugins/generations/video-gen/src/index.ts +141 -109
  39. package/plugins/generations/video-gen/src/inputs.ts +162 -4
  40. package/plugins/generations/video-gen/src/layout.ts +33 -0
  41. package/plugins/generations/video-gen/src/props.ts +27 -3
  42. package/plugins/generations/video-gen/src/remotion/AvatarReel.tsx +17 -1
  43. package/plugins/generations/video-gen/src/remotion/EditorialReel.tsx +17 -1
  44. package/plugins/generations/video-gen/src/remotion/Root.tsx +2 -0
  45. package/plugins/generations/video-gen/src/remotion/engine.ts +3 -7
  46. package/plugins/generations/video-gen/src/render.ts +25 -0
  47. package/plugins/generations/video-gen/src/request.ts +150 -20
  48. package/plugins/generations/video-gen/src/visual-provider.ts +116 -3
  49. package/plugins/generations/voice-gen/dist/index.js +14 -1
  50. package/plugins/generations/voxcpm-tts/dist/index.js +14 -1
  51. package/plugins/ingestions/aihot-ingest/dist/index.js +14 -1
  52. package/plugins/ingestions/horizon-ingest/dist/index.js +14 -1
  53. package/plugins/ingestions/last30days-ingest/dist/index.js +14 -1
  54. package/plugins/ingestions/web-search/dist/index.js +14 -1
  55. package/plugins/kk/config.example.yaml +1 -1
  56. package/plugins/kk/mcp-tools.yaml +47 -0
  57. package/plugins/kk/plugin.json +1 -1
  58. package/plugins/kk/publish-presets/itc-surfdash-locales.json +42 -0
  59. package/plugins/kk/scripts/daily-stages.ts +87 -6
  60. package/plugins/kk/scripts/kk-workflow-stages.ts +64 -9
  61. package/plugins/kk/scripts/video-stages.ts +332 -0
  62. package/plugins/kk/scripts/wiki-normalize-audio.ts +354 -0
  63. package/plugins/kk/scripts/wiki-normalize-document.ts +495 -0
  64. package/plugins/kk/scripts/wiki-normalize-stages.ts +96 -0
  65. package/plugins/kk/scripts/wiki-stage-runtime.ts +39 -0
  66. package/plugins/kk/skills/news-editorial/SKILL.md +2 -1
  67. package/plugins/kk/workflows/kk-collect.yaml +10 -0
  68. package/plugins/kk/workflows/kk-news-generate.yaml +70 -8
  69. package/plugins/kk/workflows/kk-smart-seo.yaml +8 -0
  70. package/plugins/kk/workflows/kk-storm-research.yaml +10 -0
  71. package/plugins/kk/workflows/kk-video-generate.yaml +169 -0
  72. package/plugins/kk/workflows/kk-wiki-digest.yaml +49 -0
  73. package/plugins/publishings/bilibili-pub/dist/index.js +281 -268
  74. package/plugins/publishings/bilibili-pub/dist/profile-check.js +202 -0
  75. package/plugins/publishings/cnblogs-pub/dist/index.js +281 -268
  76. package/plugins/publishings/cnblogs-pub/dist/profile-check.js +202 -0
  77. package/plugins/publishings/csdn-pub/dist/index.js +281 -268
  78. package/plugins/publishings/csdn-pub/dist/profile-check.js +216 -1
  79. package/plugins/publishings/emdash-pub/dist/index.js +33 -3
  80. package/plugins/publishings/infoq-pub/dist/index.js +281 -268
  81. package/plugins/publishings/infoq-pub/dist/profile-check.js +216 -1
  82. package/plugins/publishings/jike-pub/dist/index.js +281 -268
  83. package/plugins/publishings/jike-pub/dist/profile-check.js +216 -1
  84. package/plugins/publishings/juejin-pub/dist/index.js +281 -268
  85. package/plugins/publishings/juejin-pub/dist/profile-check.js +216 -1
  86. package/plugins/publishings/linkedin-pub/dist/index.js +281 -268
  87. package/plugins/publishings/linkedin-pub/dist/profile-check.js +216 -1
  88. package/plugins/publishings/medium-pub/dist/index.js +281 -268
  89. package/plugins/publishings/medium-pub/dist/profile-check.js +216 -1
  90. package/plugins/publishings/podcast-pub/dist/index.js +39 -9
  91. package/plugins/publishings/qiita-pub/dist/index.js +33 -3
  92. package/plugins/publishings/shipinhao-pub/dist/index.js +281 -268
  93. package/plugins/publishings/shipinhao-pub/dist/profile-check.js +175 -0
  94. package/plugins/publishings/sspai-pub/dist/index.js +281 -268
  95. package/plugins/publishings/sspai-pub/dist/profile-check.js +202 -0
  96. package/plugins/publishings/substack-pub/dist/index.js +281 -268
  97. package/plugins/publishings/substack-pub/dist/profile-check.js +216 -1
  98. package/plugins/publishings/surfdash-pub/dist/index.js +99 -39
  99. package/plugins/publishings/surfdash-pub/plugin.json +54 -0
  100. package/plugins/publishings/surfdash-pub/src/index.ts +36 -5
  101. package/plugins/publishings/threads-pub/dist/index.js +281 -268
  102. package/plugins/publishings/threads-pub/dist/profile-check.js +216 -1
  103. package/plugins/publishings/wechatmp-pub/dist/index.js +665 -451
  104. package/plugins/publishings/wechatmp-pub/dist/profile-check.js +215 -3
  105. package/plugins/publishings/weibo-pub/dist/index.js +281 -268
  106. package/plugins/publishings/weibo-pub/dist/profile-check.js +189 -1
  107. package/plugins/publishings/x-pub/dist/index.js +281 -268
  108. package/plugins/publishings/x-pub/dist/profile-check.js +216 -1
  109. package/plugins/publishings/xhs-pub/dist/index.js +281 -268
  110. package/plugins/publishings/xhs-pub/dist/profile-check.js +119 -1
  111. package/plugins/publishings/xiaoyuzhou-pub/dist/index.js +281 -268
  112. package/plugins/publishings/xiaoyuzhou-pub/dist/profile-check.js +202 -0
  113. package/plugins/publishings/youtube-pub/dist/index.js +14 -1
  114. package/plugins/publishings/zenn-pub/dist/index.js +37 -7
@@ -25287,7 +25287,19 @@ var omniVoiceGenContentSchema = ContentSchema.extend({
25287
25287
  })
25288
25288
  });
25289
25289
  var VideoVisualModeSchema = _enum(["editorial", "avatar", "hybrid-pip"]);
25290
- var VideoRenderProviderSchema = _enum(["local-ken-burns", "wan21-i2v", "musetalk", "echomimic-v2"]);
25290
+ var VideoRenderProviderSchema = _enum([
25291
+ "local-ken-burns",
25292
+ "wan21-i2v",
25293
+ "comfyui-minimax-h3",
25294
+ "musetalk",
25295
+ "echomimic-v2"
25296
+ ]);
25297
+ var RenderProvenanceSchema = object({
25298
+ cache: _enum(["drive", "local"]),
25299
+ comfyuiCommit: string2().min(1).optional(),
25300
+ comfyuiRuntimeCommit: string2().min(1).nullable().optional(),
25301
+ modelCache: object({ verified: boolean2(), restored: number2().int().nonnegative() }).optional()
25302
+ });
25291
25303
  var videoGenContentSchema = ContentSchema.extend({
25292
25304
  format: literal("video"),
25293
25305
  metadata: looseObject({
@@ -25300,6 +25312,7 @@ var videoGenContentSchema = ContentSchema.extend({
25300
25312
  durationSec: number2().positive(),
25301
25313
  visualMode: VideoVisualModeSchema,
25302
25314
  renderProvider: VideoRenderProviderSchema,
25315
+ renderProvenance: RenderProvenanceSchema.optional(),
25303
25316
  fallbackUsed: boolean2().optional(),
25304
25317
  warnings: array(string2()).optional(),
25305
25318
  ccuEstimated: number2().nonnegative().optional(),
@@ -25494,8 +25507,12 @@ var CJK_TERMINATORS = new Set(["\u3002", "\uFF01", "\uFF1F"]);
25494
25507
  var LATIN_TERMINATORS = new Set([".", "!", "?"]);
25495
25508
  var ATTACHED_CLOSERS = new Set(["\u201D", "\u2019", "\u300D", "\u300F", "\uFF09", ")", '"', "'"]);
25496
25509
  // ../../packages/publish-harness/src/index.ts
25497
- import { homedir } from "os";
25498
- import { join, resolve } from "path";
25510
+ import { homedir as homedir4 } from "os";
25511
+ import { join as join4, resolve as resolve4 } from "path";
25512
+
25513
+ // ../../packages/publish-harness/src/bot.ts
25514
+ import { homedir as homedir3 } from "os";
25515
+ import { isAbsolute, join as join3, relative, resolve as resolve3, sep } from "path";
25499
25516
 
25500
25517
  // ../../packages/publish-harness/src/bot-fs.ts
25501
25518
  import {
@@ -25576,6 +25593,264 @@ function chmod(path, mode) {
25576
25593
  chmodSync(path, mode);
25577
25594
  }
25578
25595
 
25596
+ // ../../packages/publish-harness/src/bot-transaction.ts
25597
+ import { homedir } from "os";
25598
+ import { dirname as dirname2, join, resolve } from "path";
25599
+ var DEFAULT_BOT_TRANSACTION_ROOT = join(homedir(), ".config", "kk", "browser-transactions");
25600
+
25601
+ // ../../packages/publish-harness/src/profile-lease.ts
25602
+ import { createHash, randomBytes } from "crypto";
25603
+ import { homedir as homedir2, hostname } from "os";
25604
+ import { basename, dirname as dirname3, join as join2, resolve as resolve2 } from "path";
25605
+ var DEFAULT_BOT_LEASE_ROOT = join2(homedir2(), ".config", "kk", "browser-leases");
25606
+ var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
25607
+ var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
25608
+ var OWNER_FILE = "owner.json";
25609
+ var HOLDER_PREFIX = "holder-";
25610
+ var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
25611
+ async function acquireProfileLease(profileDir, options = {}) {
25612
+ const fileSystem = options.fileSystem ?? createNodeFileSystem();
25613
+ const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
25614
+ const logicalPath = canonicalize(profileDir);
25615
+ refuseNativeLock(logicalPath);
25616
+ const id = sha256Hex(logicalPath);
25617
+ const leaseDir = join2(leaseRoot, id);
25618
+ await fileSystem.ensureDir(leaseRoot);
25619
+ if (!mkdirExclusive(leaseDir, 448)) {
25620
+ if (!await reclaimDeadOwnerLease(fileSystem, leaseDir)) {
25621
+ throw new BotError("busy", await busyGuidance(fileSystem, leaseDir));
25622
+ }
25623
+ if (!mkdirExclusive(leaseDir, 448)) {
25624
+ throw new BotError("busy", await busyGuidance(fileSystem, leaseDir));
25625
+ }
25626
+ }
25627
+ const token = randomBytes(16).toString("hex");
25628
+ const owner = {
25629
+ schemaVersion: 1,
25630
+ id,
25631
+ token,
25632
+ logicalPath,
25633
+ physicalPath: null,
25634
+ pid: process.pid,
25635
+ createdAt: new Date().toISOString()
25636
+ };
25637
+ await writeOwner(fileSystem, leaseDir, owner);
25638
+ return {
25639
+ id,
25640
+ token,
25641
+ profileDir: logicalPath,
25642
+ leaseDir,
25643
+ async release() {
25644
+ await releaseOwnedLease(fileSystem, leaseDir, owner);
25645
+ },
25646
+ async delegatedEnv(physicalProfileDir) {
25647
+ await writeOwner(fileSystem, leaseDir, { ...owner, physicalPath: canonicalize(physicalProfileDir) });
25648
+ return { [KK_BOT_LEASE_PATH]: leaseDir, [KK_BOT_LEASE_TOKEN]: token };
25649
+ }
25650
+ };
25651
+ }
25652
+ async function enterDelegatedLease(env, physicalProfileDir, options = {}) {
25653
+ const fileSystem = options.fileSystem ?? createNodeFileSystem();
25654
+ const leaseDir = env[KK_BOT_LEASE_PATH];
25655
+ const token = env[KK_BOT_LEASE_TOKEN];
25656
+ if (leaseDir === undefined || leaseDir === "" || token === undefined || token === "") {
25657
+ throw new BotError("usage", `no delegated lease present (${KK_BOT_LEASE_PATH}/${KK_BOT_LEASE_TOKEN})`);
25658
+ }
25659
+ const owner = await readOwner(fileSystem, leaseDir);
25660
+ if (owner === null)
25661
+ throw new BotError("busy", `delegated lease ${leaseDir} has no owner record`);
25662
+ if (owner.token !== token)
25663
+ throw new BotError("busy", `delegated lease ${leaseDir} token does not match its owner`);
25664
+ const physicalPath = canonicalize(physicalProfileDir);
25665
+ if (owner.physicalPath !== null && owner.physicalPath !== physicalPath) {
25666
+ throw new BotError("busy", `delegated lease ${leaseDir} is bound to ${owner.physicalPath}, not ${physicalPath}`);
25667
+ }
25668
+ const holderPath = join2(leaseDir, `${HOLDER_PREFIX}${process.pid}.json`);
25669
+ await fileSystem.writeFile(holderPath, `${JSON.stringify({ pid: process.pid, physicalPath, enteredAt: new Date().toISOString() }, null, 2)}
25670
+ `);
25671
+ chmod(holderPath, 384);
25672
+ return {
25673
+ pid: process.pid,
25674
+ leaseDir,
25675
+ async release() {
25676
+ await fileSystem.deleteFile(holderPath);
25677
+ }
25678
+ };
25679
+ }
25680
+ async function verifyLeaseHeld(lease, options = {}) {
25681
+ const fileSystem = options.fileSystem ?? createNodeFileSystem();
25682
+ const owner = await readOwner(fileSystem, lease.leaseDir);
25683
+ if (owner === null || owner.token !== lease.token || owner.id !== lease.id) {
25684
+ throw new BotError("busy", `browser lease ${lease.leaseDir} is not held by this invocation any more \u2014 refusing to borrow it`);
25685
+ }
25686
+ }
25687
+ function canonicalize(profileDir) {
25688
+ let probe = resolve2(profileDir);
25689
+ const tail = [];
25690
+ for (;; ) {
25691
+ if (lstat(probe) !== null) {
25692
+ let canonical;
25693
+ try {
25694
+ canonical = realpath(probe);
25695
+ } catch {
25696
+ canonical = probe;
25697
+ }
25698
+ return tail.length === 0 ? canonical : join2(canonical, ...tail);
25699
+ }
25700
+ const parent = dirname3(probe);
25701
+ if (parent === probe)
25702
+ return join2(probe, ...tail);
25703
+ tail.unshift(basename(probe));
25704
+ probe = parent;
25705
+ }
25706
+ }
25707
+ async function releaseOwnedLease(fileSystem, leaseDir, expected) {
25708
+ const current = await readOwner(fileSystem, leaseDir);
25709
+ if (current === null || current.token !== expected.token || current.id !== expected.id) {
25710
+ throw new BotError("busy", `lease ${leaseDir} is not owned by this invocation \u2014 refusing to release`);
25711
+ }
25712
+ const live = await liveChildHolders(fileSystem, leaseDir);
25713
+ if (live.length > 0) {
25714
+ throw new BotError("busy", `lease ${leaseDir} still has live child holders (pids ${live.join(", ")}) \u2014 stop them before releasing`);
25715
+ }
25716
+ await fileSystem.deleteFile(join2(leaseDir, OWNER_FILE));
25717
+ rmdir(leaseDir);
25718
+ }
25719
+ async function readOwner(fileSystem, leaseDir) {
25720
+ try {
25721
+ return JSON.parse(await fileSystem.readFile(join2(leaseDir, OWNER_FILE)));
25722
+ } catch {
25723
+ return null;
25724
+ }
25725
+ }
25726
+ async function writeOwner(fileSystem, leaseDir, owner) {
25727
+ const path = join2(leaseDir, OWNER_FILE);
25728
+ await fileSystem.writeFile(path, `${JSON.stringify(owner, null, 2)}
25729
+ `);
25730
+ chmod(path, 384);
25731
+ }
25732
+ async function busyGuidance(fileSystem, leaseDir) {
25733
+ const owner = await readOwner(fileSystem, leaseDir);
25734
+ if (owner === null) {
25735
+ const residue = await holderResidue(fileSystem, leaseDir);
25736
+ if (residue !== null) {
25737
+ return `browser lease ${leaseDir} has no owner but ${residue} child holder record(s) remain \u2014 ` + "a previous parent released after its children stopped; confirm no kk browser still uses this " + `profile, then remove ${leaseDir} manually`;
25738
+ }
25739
+ return `browser lease ${leaseDir} already exists with no owner record \u2014 a previous run may have crashed mid-acquire; confirm no kk browser still uses this profile, then remove ${leaseDir} manually`;
25740
+ }
25741
+ const live = isProcessAlive(owner.pid) ? [] : await liveChildHolders(fileSystem, leaseDir);
25742
+ const suffix = live.length > 0 ? `pid ${owner.pid} is gone but child holders ${live.join(", ")} are still running \u2014 stop those processes before retrying` : isProcessAlive(owner.pid) ? "confirm that process and any browser for this profile have stopped; stale ownership is never auto-removed" : `pid ${owner.pid} is no longer running and the lease could not be reclassified \u2014 confirm no browser for this profile is open, then remove the stale lease with: rm -rf ${leaseDir}`;
25743
+ return `browser lease ${leaseDir} is held by pid ${owner.pid} (logical ${owner.logicalPath}) \u2014 ${suffix}`;
25744
+ }
25745
+ async function reclaimDeadOwnerLease(fileSystem, leaseDir) {
25746
+ const owner = await readOwner(fileSystem, leaseDir);
25747
+ if (owner === null || isProcessAlive(owner.pid))
25748
+ return false;
25749
+ if ((await liveChildHolders(fileSystem, leaseDir)).length > 0)
25750
+ return false;
25751
+ const target = staleLeaseDir(leaseDir);
25752
+ try {
25753
+ rename(leaseDir, target);
25754
+ } catch {
25755
+ return false;
25756
+ }
25757
+ const moved = await readOwner(fileSystem, target);
25758
+ if (moved !== null && moved.token !== owner.token) {
25759
+ try {
25760
+ rename(target, leaseDir);
25761
+ } catch {}
25762
+ return false;
25763
+ }
25764
+ return true;
25765
+ }
25766
+ function staleLeaseDir(leaseDir) {
25767
+ const stamp = new Date().toISOString().replace(/[:.]/g, "").slice(0, 15);
25768
+ return `${leaseDir}.stale-${stamp}-${process.pid}-${randomBytes(3).toString("hex")}`;
25769
+ }
25770
+ async function holderResidue(fileSystem, leaseDir) {
25771
+ try {
25772
+ const entries = await fileSystem.readDir(leaseDir);
25773
+ const holders = entries.filter((name) => name.startsWith(HOLDER_PREFIX) && name.endsWith(".json"));
25774
+ return holders.length > 0 ? holders.length : null;
25775
+ } catch {
25776
+ return null;
25777
+ }
25778
+ }
25779
+ async function liveChildHolders(fileSystem, leaseDir) {
25780
+ let entries;
25781
+ try {
25782
+ entries = await fileSystem.readDir(leaseDir);
25783
+ } catch {
25784
+ return [];
25785
+ }
25786
+ const pids = [];
25787
+ for (const name of entries) {
25788
+ if (!name.startsWith(HOLDER_PREFIX) || !name.endsWith(".json"))
25789
+ continue;
25790
+ let record;
25791
+ try {
25792
+ record = JSON.parse(await fileSystem.readFile(join2(leaseDir, name)));
25793
+ } catch {
25794
+ continue;
25795
+ }
25796
+ if (typeof record.pid === "number" && isProcessAlive(record.pid))
25797
+ pids.push(record.pid);
25798
+ }
25799
+ return pids;
25800
+ }
25801
+ function isProcessAlive(pid) {
25802
+ try {
25803
+ process.kill(pid, 0);
25804
+ return true;
25805
+ } catch (error) {
25806
+ return error.code === "EPERM";
25807
+ }
25808
+ }
25809
+ function refuseNativeLock(profileDir) {
25810
+ if (reclaimStaleNativeLock(profileDir))
25811
+ return;
25812
+ for (const name of NATIVE_LOCK_FILES) {
25813
+ const path = join2(profileDir, name);
25814
+ if (lstat(path) !== null) {
25815
+ throw new BotError("busy", `native Chromium ownership present at ${path} \u2014 confirm no browser is using ${profileDir} before retrying`);
25816
+ }
25817
+ }
25818
+ }
25819
+ function reclaimStaleNativeLock(profileDir) {
25820
+ const lockPath = join2(profileDir, "SingletonLock");
25821
+ const target = readlink(lockPath);
25822
+ if (target === null)
25823
+ return false;
25824
+ const match = /^(.*)-(\d+)$/.exec(target);
25825
+ if (match === null)
25826
+ return false;
25827
+ const [, host, pidText] = match;
25828
+ if (host !== hostname())
25829
+ return false;
25830
+ const pid = Number(pidText);
25831
+ if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
25832
+ return false;
25833
+ for (const name of NATIVE_LOCK_FILES)
25834
+ unlink(join2(profileDir, name));
25835
+ return true;
25836
+ }
25837
+ function sha256Hex(value) {
25838
+ return createHash("sha256").update(value).digest("hex");
25839
+ }
25840
+
25841
+ // ../../packages/publish-harness/src/bot.ts
25842
+ var DEFAULT_BOT_PROFILE_ROOT = join3(homedir3(), ".config", "kk", "browser-profiles");
25843
+ class BotError extends Error {
25844
+ code;
25845
+ details;
25846
+ constructor(code, message, details = {}) {
25847
+ super(message);
25848
+ this.name = "BotError";
25849
+ this.code = code;
25850
+ this.details = details;
25851
+ }
25852
+ }
25853
+
25579
25854
  // ../../packages/publish-harness/src/index.ts
25580
25855
  class FakeTransportCore {
25581
25856
  callLogs = [];
@@ -25653,11 +25928,11 @@ function resolveBrowserProfileDir(channel, override) {
25653
25928
  if (override && override.trim().length > 0) {
25654
25929
  const trimmed = override.trim();
25655
25930
  if (trimmed.startsWith("~/")) {
25656
- return join(homedir(), trimmed.slice(2));
25931
+ return join4(homedir4(), trimmed.slice(2));
25657
25932
  }
25658
- return resolve(trimmed);
25933
+ return resolve4(trimmed);
25659
25934
  }
25660
- return join(homedir(), ".config", "kk", "browser-profiles", channel);
25935
+ return join4(homedir4(), ".config", "kk", "browser-profiles", channel);
25661
25936
  }
25662
25937
  async function trySelectors(page, selectors, options = {}) {
25663
25938
  const timeout = options.timeout ?? 2000;
@@ -25880,268 +26155,6 @@ function sleep(ms) {
25880
26155
  }
25881
26156
  // ../../packages/publish-harness/src/browser.ts
25882
26157
  init_dist();
25883
-
25884
- // ../../packages/publish-harness/src/bot.ts
25885
- import { homedir as homedir4 } from "os";
25886
- import { isAbsolute, join as join4, relative, resolve as resolve4, sep } from "path";
25887
-
25888
- // ../../packages/publish-harness/src/bot-transaction.ts
25889
- import { homedir as homedir2 } from "os";
25890
- import { dirname as dirname2, join as join2, resolve as resolve2 } from "path";
25891
- var DEFAULT_BOT_TRANSACTION_ROOT = join2(homedir2(), ".config", "kk", "browser-transactions");
25892
-
25893
- // ../../packages/publish-harness/src/profile-lease.ts
25894
- import { createHash, randomBytes } from "crypto";
25895
- import { homedir as homedir3, hostname } from "os";
25896
- import { basename, dirname as dirname3, join as join3, resolve as resolve3 } from "path";
25897
- var DEFAULT_BOT_LEASE_ROOT = join3(homedir3(), ".config", "kk", "browser-leases");
25898
- var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
25899
- var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
25900
- var OWNER_FILE = "owner.json";
25901
- var HOLDER_PREFIX = "holder-";
25902
- var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
25903
- async function acquireProfileLease(profileDir, options = {}) {
25904
- const fileSystem = options.fileSystem ?? createNodeFileSystem();
25905
- const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
25906
- const logicalPath = canonicalize(profileDir);
25907
- refuseNativeLock(logicalPath);
25908
- const id = sha256Hex(logicalPath);
25909
- const leaseDir = join3(leaseRoot, id);
25910
- await fileSystem.ensureDir(leaseRoot);
25911
- if (!mkdirExclusive(leaseDir, 448)) {
25912
- if (!await reclaimDeadOwnerLease(fileSystem, leaseDir)) {
25913
- throw new BotError("busy", await busyGuidance(fileSystem, leaseDir));
25914
- }
25915
- if (!mkdirExclusive(leaseDir, 448)) {
25916
- throw new BotError("busy", await busyGuidance(fileSystem, leaseDir));
25917
- }
25918
- }
25919
- const token = randomBytes(16).toString("hex");
25920
- const owner = {
25921
- schemaVersion: 1,
25922
- id,
25923
- token,
25924
- logicalPath,
25925
- physicalPath: null,
25926
- pid: process.pid,
25927
- createdAt: new Date().toISOString()
25928
- };
25929
- await writeOwner(fileSystem, leaseDir, owner);
25930
- return {
25931
- id,
25932
- token,
25933
- profileDir: logicalPath,
25934
- leaseDir,
25935
- async release() {
25936
- await releaseOwnedLease(fileSystem, leaseDir, owner);
25937
- },
25938
- async delegatedEnv(physicalProfileDir) {
25939
- await writeOwner(fileSystem, leaseDir, { ...owner, physicalPath: canonicalize(physicalProfileDir) });
25940
- return { [KK_BOT_LEASE_PATH]: leaseDir, [KK_BOT_LEASE_TOKEN]: token };
25941
- }
25942
- };
25943
- }
25944
- async function enterDelegatedLease(env, physicalProfileDir, options = {}) {
25945
- const fileSystem = options.fileSystem ?? createNodeFileSystem();
25946
- const leaseDir = env[KK_BOT_LEASE_PATH];
25947
- const token = env[KK_BOT_LEASE_TOKEN];
25948
- if (leaseDir === undefined || leaseDir === "" || token === undefined || token === "") {
25949
- throw new BotError("usage", `no delegated lease present (${KK_BOT_LEASE_PATH}/${KK_BOT_LEASE_TOKEN})`);
25950
- }
25951
- const owner = await readOwner(fileSystem, leaseDir);
25952
- if (owner === null)
25953
- throw new BotError("busy", `delegated lease ${leaseDir} has no owner record`);
25954
- if (owner.token !== token)
25955
- throw new BotError("busy", `delegated lease ${leaseDir} token does not match its owner`);
25956
- const physicalPath = canonicalize(physicalProfileDir);
25957
- if (owner.physicalPath !== null && owner.physicalPath !== physicalPath) {
25958
- throw new BotError("busy", `delegated lease ${leaseDir} is bound to ${owner.physicalPath}, not ${physicalPath}`);
25959
- }
25960
- const holderPath = join3(leaseDir, `${HOLDER_PREFIX}${process.pid}.json`);
25961
- await fileSystem.writeFile(holderPath, `${JSON.stringify({ pid: process.pid, physicalPath, enteredAt: new Date().toISOString() }, null, 2)}
25962
- `);
25963
- chmod(holderPath, 384);
25964
- return {
25965
- pid: process.pid,
25966
- leaseDir,
25967
- async release() {
25968
- await fileSystem.deleteFile(holderPath);
25969
- }
25970
- };
25971
- }
25972
- async function verifyLeaseHeld(lease, options = {}) {
25973
- const fileSystem = options.fileSystem ?? createNodeFileSystem();
25974
- const owner = await readOwner(fileSystem, lease.leaseDir);
25975
- if (owner === null || owner.token !== lease.token || owner.id !== lease.id) {
25976
- throw new BotError("busy", `browser lease ${lease.leaseDir} is not held by this invocation any more \u2014 refusing to borrow it`);
25977
- }
25978
- }
25979
- function canonicalize(profileDir) {
25980
- let probe = resolve3(profileDir);
25981
- const tail = [];
25982
- for (;; ) {
25983
- if (lstat(probe) !== null) {
25984
- let canonical;
25985
- try {
25986
- canonical = realpath(probe);
25987
- } catch {
25988
- canonical = probe;
25989
- }
25990
- return tail.length === 0 ? canonical : join3(canonical, ...tail);
25991
- }
25992
- const parent = dirname3(probe);
25993
- if (parent === probe)
25994
- return join3(probe, ...tail);
25995
- tail.unshift(basename(probe));
25996
- probe = parent;
25997
- }
25998
- }
25999
- async function releaseOwnedLease(fileSystem, leaseDir, expected) {
26000
- const current = await readOwner(fileSystem, leaseDir);
26001
- if (current === null || current.token !== expected.token || current.id !== expected.id) {
26002
- throw new BotError("busy", `lease ${leaseDir} is not owned by this invocation \u2014 refusing to release`);
26003
- }
26004
- const live = await liveChildHolders(fileSystem, leaseDir);
26005
- if (live.length > 0) {
26006
- throw new BotError("busy", `lease ${leaseDir} still has live child holders (pids ${live.join(", ")}) \u2014 stop them before releasing`);
26007
- }
26008
- await fileSystem.deleteFile(join3(leaseDir, OWNER_FILE));
26009
- rmdir(leaseDir);
26010
- }
26011
- async function readOwner(fileSystem, leaseDir) {
26012
- try {
26013
- return JSON.parse(await fileSystem.readFile(join3(leaseDir, OWNER_FILE)));
26014
- } catch {
26015
- return null;
26016
- }
26017
- }
26018
- async function writeOwner(fileSystem, leaseDir, owner) {
26019
- const path = join3(leaseDir, OWNER_FILE);
26020
- await fileSystem.writeFile(path, `${JSON.stringify(owner, null, 2)}
26021
- `);
26022
- chmod(path, 384);
26023
- }
26024
- async function busyGuidance(fileSystem, leaseDir) {
26025
- const owner = await readOwner(fileSystem, leaseDir);
26026
- if (owner === null) {
26027
- const residue = await holderResidue(fileSystem, leaseDir);
26028
- if (residue !== null) {
26029
- return `browser lease ${leaseDir} has no owner but ${residue} child holder record(s) remain \u2014 ` + "a previous parent released after its children stopped; confirm no kk browser still uses this " + `profile, then remove ${leaseDir} manually`;
26030
- }
26031
- return `browser lease ${leaseDir} already exists with no owner record \u2014 a previous run may have crashed mid-acquire; confirm no kk browser still uses this profile, then remove ${leaseDir} manually`;
26032
- }
26033
- const live = isProcessAlive(owner.pid) ? [] : await liveChildHolders(fileSystem, leaseDir);
26034
- const suffix = live.length > 0 ? `pid ${owner.pid} is gone but child holders ${live.join(", ")} are still running \u2014 stop those processes before retrying` : isProcessAlive(owner.pid) ? "confirm that process and any browser for this profile have stopped; stale ownership is never auto-removed" : `pid ${owner.pid} is no longer running and the lease could not be reclassified \u2014 confirm no browser for this profile is open, then remove the stale lease with: rm -rf ${leaseDir}`;
26035
- return `browser lease ${leaseDir} is held by pid ${owner.pid} (logical ${owner.logicalPath}) \u2014 ${suffix}`;
26036
- }
26037
- async function reclaimDeadOwnerLease(fileSystem, leaseDir) {
26038
- const owner = await readOwner(fileSystem, leaseDir);
26039
- if (owner === null || isProcessAlive(owner.pid))
26040
- return false;
26041
- if ((await liveChildHolders(fileSystem, leaseDir)).length > 0)
26042
- return false;
26043
- const target = staleLeaseDir(leaseDir);
26044
- try {
26045
- rename(leaseDir, target);
26046
- } catch {
26047
- return false;
26048
- }
26049
- const moved = await readOwner(fileSystem, target);
26050
- if (moved !== null && moved.token !== owner.token) {
26051
- try {
26052
- rename(target, leaseDir);
26053
- } catch {}
26054
- return false;
26055
- }
26056
- return true;
26057
- }
26058
- function staleLeaseDir(leaseDir) {
26059
- const stamp = new Date().toISOString().replace(/[:.]/g, "").slice(0, 15);
26060
- return `${leaseDir}.stale-${stamp}-${process.pid}-${randomBytes(3).toString("hex")}`;
26061
- }
26062
- async function holderResidue(fileSystem, leaseDir) {
26063
- try {
26064
- const entries = await fileSystem.readDir(leaseDir);
26065
- const holders = entries.filter((name) => name.startsWith(HOLDER_PREFIX) && name.endsWith(".json"));
26066
- return holders.length > 0 ? holders.length : null;
26067
- } catch {
26068
- return null;
26069
- }
26070
- }
26071
- async function liveChildHolders(fileSystem, leaseDir) {
26072
- let entries;
26073
- try {
26074
- entries = await fileSystem.readDir(leaseDir);
26075
- } catch {
26076
- return [];
26077
- }
26078
- const pids = [];
26079
- for (const name of entries) {
26080
- if (!name.startsWith(HOLDER_PREFIX) || !name.endsWith(".json"))
26081
- continue;
26082
- let record;
26083
- try {
26084
- record = JSON.parse(await fileSystem.readFile(join3(leaseDir, name)));
26085
- } catch {
26086
- continue;
26087
- }
26088
- if (typeof record.pid === "number" && isProcessAlive(record.pid))
26089
- pids.push(record.pid);
26090
- }
26091
- return pids;
26092
- }
26093
- function isProcessAlive(pid) {
26094
- try {
26095
- process.kill(pid, 0);
26096
- return true;
26097
- } catch (error) {
26098
- return error.code === "EPERM";
26099
- }
26100
- }
26101
- function refuseNativeLock(profileDir) {
26102
- if (reclaimStaleNativeLock(profileDir))
26103
- return;
26104
- for (const name of NATIVE_LOCK_FILES) {
26105
- const path = join3(profileDir, name);
26106
- if (lstat(path) !== null) {
26107
- throw new BotError("busy", `native Chromium ownership present at ${path} \u2014 confirm no browser is using ${profileDir} before retrying`);
26108
- }
26109
- }
26110
- }
26111
- function reclaimStaleNativeLock(profileDir) {
26112
- const lockPath = join3(profileDir, "SingletonLock");
26113
- const target = readlink(lockPath);
26114
- if (target === null)
26115
- return false;
26116
- const match = /^(.*)-(\d+)$/.exec(target);
26117
- if (match === null)
26118
- return false;
26119
- const [, host, pidText] = match;
26120
- if (host !== hostname())
26121
- return false;
26122
- const pid = Number(pidText);
26123
- if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
26124
- return false;
26125
- for (const name of NATIVE_LOCK_FILES)
26126
- unlink(join3(profileDir, name));
26127
- return true;
26128
- }
26129
- function sha256Hex(value) {
26130
- return createHash("sha256").update(value).digest("hex");
26131
- }
26132
-
26133
- // ../../packages/publish-harness/src/bot.ts
26134
- var DEFAULT_BOT_PROFILE_ROOT = join4(homedir4(), ".config", "kk", "browser-profiles");
26135
- class BotError extends Error {
26136
- code;
26137
- details;
26138
- constructor(code, message, details = {}) {
26139
- super(message);
26140
- this.name = "BotError";
26141
- this.code = code;
26142
- this.details = details;
26143
- }
26144
- }
26145
26158
  // ../../packages/publish-harness/src/profile-dir.ts
26146
26159
  import { mkdir as mkdir2, stat } from "fs/promises";
26147
26160
  async function ensureProfileDir2(profileDir, headless) {