@gobing-ai/knowledge-kit 0.1.16 → 0.1.17

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 (49) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/dist/index.js +162 -39
  3. package/package.json +1 -1
  4. package/plugins/kk/plugin.json +1 -1
  5. package/plugins/kk/publish-presets/daily-ai-voice.json +8 -8
  6. package/plugins/kk/publish-presets/itc-main.json +42 -0
  7. package/plugins/kk/scripts/cover-compositor.ts +54 -8
  8. package/plugins/kk/scripts/itc-stages.ts +3 -2
  9. package/plugins/kk/scripts/voice3-publish-window.ts +5 -1
  10. package/plugins/kk/workflows/kk-daily-publish3.yaml +10 -3
  11. package/plugins/publishings/bilibili-pub/dist/index.js +89 -16
  12. package/plugins/publishings/bilibili-pub/dist/profile-check.js +90 -16
  13. package/plugins/publishings/cnblogs-pub/dist/index.js +89 -16
  14. package/plugins/publishings/cnblogs-pub/dist/profile-check.js +90 -16
  15. package/plugins/publishings/csdn-pub/dist/index.js +89 -16
  16. package/plugins/publishings/csdn-pub/dist/profile-check.js +90 -16
  17. package/plugins/publishings/infoq-pub/dist/index.js +119 -18
  18. package/plugins/publishings/infoq-pub/dist/profile-check.js +94 -17
  19. package/plugins/publishings/infoq-pub/src/index.ts +40 -1
  20. package/plugins/publishings/jike-pub/dist/index.js +89 -16
  21. package/plugins/publishings/jike-pub/dist/profile-check.js +90 -16
  22. package/plugins/publishings/juejin-pub/dist/index.js +116 -19
  23. package/plugins/publishings/juejin-pub/dist/profile-check.js +93 -17
  24. package/plugins/publishings/juejin-pub/src/index.ts +41 -2
  25. package/plugins/publishings/linkedin-pub/dist/index.js +89 -16
  26. package/plugins/publishings/linkedin-pub/dist/profile-check.js +90 -16
  27. package/plugins/publishings/shipinhao-pub/dist/index.js +89 -16
  28. package/plugins/publishings/shipinhao-pub/dist/profile-check.js +90 -16
  29. package/plugins/publishings/sspai-pub/dist/index.js +89 -16
  30. package/plugins/publishings/sspai-pub/dist/profile-check.js +90 -16
  31. package/plugins/publishings/substack-pub/dist/index.js +89 -16
  32. package/plugins/publishings/substack-pub/dist/profile-check.js +90 -16
  33. package/plugins/publishings/surfdash-pub/dist/index.js +9 -1
  34. package/plugins/publishings/surfdash-pub/src/index.ts +17 -2
  35. package/plugins/publishings/wechatmp-pub/dist/index.js +176 -32
  36. package/plugins/publishings/wechatmp-pub/dist/profile-check.js +95 -18
  37. package/plugins/publishings/wechatmp-pub/src/cgi-draft.ts +14 -2
  38. package/plugins/publishings/wechatmp-pub/src/flow.ts +155 -21
  39. package/plugins/publishings/wechatmp-pub/src/profile-check.ts +8 -1
  40. package/plugins/publishings/weibo-pub/dist/index.js +89 -16
  41. package/plugins/publishings/weibo-pub/dist/profile-check.js +90 -16
  42. package/plugins/publishings/x-pub/dist/index.js +89 -16
  43. package/plugins/publishings/x-pub/dist/profile-check.js +90 -16
  44. package/plugins/publishings/xhs-pub/dist/index.js +89 -16
  45. package/plugins/publishings/xhs-pub/dist/profile-check.js +89 -16
  46. package/plugins/publishings/xiaoyuzhou-pub/dist/index.js +89 -16
  47. package/plugins/publishings/xiaoyuzhou-pub/dist/profile-check.js +90 -16
  48. package/plugins/publishings/zenn-pub/dist/index.js +93 -1
  49. package/plugins/publishings/zenn-pub/src/index.ts +134 -1
@@ -20489,8 +20489,11 @@ import {
20489
20489
  mkdirSync as mkdirSync2,
20490
20490
  openSync,
20491
20491
  readdirSync as readdirSync2,
20492
+ readlinkSync,
20492
20493
  realpathSync as realpathSync2,
20493
- rmdirSync
20494
+ renameSync as renameSync2,
20495
+ rmdirSync,
20496
+ unlinkSync
20494
20497
  } from "fs";
20495
20498
  function lstat(path) {
20496
20499
  try {
@@ -20525,6 +20528,33 @@ function rmdir(path) {
20525
20528
  throw error;
20526
20529
  }
20527
20530
  }
20531
+ function readlink(path) {
20532
+ try {
20533
+ return readlinkSync(path);
20534
+ } catch {
20535
+ return null;
20536
+ }
20537
+ }
20538
+ function unlink(path) {
20539
+ try {
20540
+ unlinkSync(path);
20541
+ return true;
20542
+ } catch (error) {
20543
+ if (error.code === "ENOENT")
20544
+ return false;
20545
+ throw error;
20546
+ }
20547
+ }
20548
+ function rename(from, to) {
20549
+ try {
20550
+ renameSync2(from, to);
20551
+ return true;
20552
+ } catch (error) {
20553
+ if (error.code === "ENOENT")
20554
+ return false;
20555
+ throw error;
20556
+ }
20557
+ }
20528
20558
  function chmod(path, mode) {
20529
20559
  chmodSync(path, mode);
20530
20560
  }
@@ -20536,14 +20566,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join2(homedir2(), ".config", "kk", "browser-t
20536
20566
 
20537
20567
  // ../../packages/publish-harness/src/profile-lease.ts
20538
20568
  import { createHash, randomBytes } from "crypto";
20539
- import { homedir as homedir3 } from "os";
20569
+ import { homedir as homedir3, hostname } from "os";
20540
20570
  import { basename, dirname as dirname3, join as join3, resolve as resolve3 } from "path";
20541
20571
  var DEFAULT_BOT_LEASE_ROOT = join3(homedir3(), ".config", "kk", "browser-leases");
20542
20572
  var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
20543
20573
  var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
20544
20574
  var OWNER_FILE = "owner.json";
20545
20575
  var HOLDER_PREFIX = "holder-";
20546
- var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
20576
+ var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
20547
20577
  async function acquireProfileLease(profileDir, options = {}) {
20548
20578
  const fileSystem = options.fileSystem ?? createNodeFileSystem();
20549
20579
  const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
@@ -20711,6 +20741,8 @@ function isProcessAlive(pid) {
20711
20741
  }
20712
20742
  }
20713
20743
  function refuseNativeLock(profileDir) {
20744
+ if (reclaimStaleNativeLock(profileDir))
20745
+ return;
20714
20746
  for (const name of NATIVE_LOCK_FILES) {
20715
20747
  const path = join3(profileDir, name);
20716
20748
  if (lstat(path) !== null) {
@@ -20718,6 +20750,24 @@ function refuseNativeLock(profileDir) {
20718
20750
  }
20719
20751
  }
20720
20752
  }
20753
+ function reclaimStaleNativeLock(profileDir) {
20754
+ const lockPath = join3(profileDir, "SingletonLock");
20755
+ const target = readlink(lockPath);
20756
+ if (target === null)
20757
+ return false;
20758
+ const match = /^(.*)-(\d+)$/.exec(target);
20759
+ if (match === null)
20760
+ return false;
20761
+ const [, host, pidText] = match;
20762
+ if (host !== hostname())
20763
+ return false;
20764
+ const pid = Number(pidText);
20765
+ if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
20766
+ return false;
20767
+ for (const name of NATIVE_LOCK_FILES)
20768
+ unlink(join3(profileDir, name));
20769
+ return true;
20770
+ }
20721
20771
  function sha256Hex(value) {
20722
20772
  return createHash("sha256").update(value).digest("hex");
20723
20773
  }
@@ -20754,6 +20804,27 @@ var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20754
20804
  var DEFAULT_STEP_TIMEOUT_MS = 30000;
20755
20805
  var DEFAULT_POLL_MS = 500;
20756
20806
  var ATTACH_POLL_MS = 100;
20807
+ function isInstantBrowserDeath(message) {
20808
+ return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20809
+ }
20810
+ function classifyLaunchError(error, profileDir, channel) {
20811
+ const message = errorMessage2(error);
20812
+ if (/singleton|already in use|in use by another/i.test(message))
20813
+ return new BrowserProfileBusyError(profileDir, error);
20814
+ if (/playwright install|executable doesn't exist/i.test(message)) {
20815
+ return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
20816
+ }
20817
+ if (isInstantBrowserDeath(message)) {
20818
+ return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
20819
+ }
20820
+ return error instanceof Error ? error : new Error(message);
20821
+ }
20822
+ function quarantineProfileSyncData(profileDir) {
20823
+ const syncData = join5(profileDir, "Default", "Sync Data");
20824
+ if (lstat(syncData) === null)
20825
+ return false;
20826
+ return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
20827
+ }
20757
20828
  function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
20758
20829
  return {
20759
20830
  async launch(request) {
@@ -20782,23 +20853,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
20782
20853
  throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
20783
20854
  }
20784
20855
  const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
20856
+ const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
20857
+ headless,
20858
+ channel,
20859
+ args: ["--disable-blink-features=AutomationControlled"],
20860
+ ignoreDefaultArgs: ["--enable-automation"]
20861
+ });
20785
20862
  let context;
20786
20863
  try {
20787
- context = await playwright.chromium.launchPersistentContext(profileDir, {
20788
- headless,
20789
- channel,
20790
- args: ["--disable-blink-features=AutomationControlled"],
20791
- ignoreDefaultArgs: ["--enable-automation"]
20792
- });
20864
+ context = await launch();
20793
20865
  } catch (error) {
20794
- const message = errorMessage2(error);
20795
- if (/singleton|already in use|in use by another/i.test(message)) {
20796
- throw new BrowserProfileBusyError(profileDir, error);
20797
- }
20798
- if (/playwright install|executable doesn't exist/i.test(message)) {
20799
- throw new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
20866
+ if (!isInstantBrowserDeath(errorMessage2(error)))
20867
+ throw classifyLaunchError(error, profileDir, channel);
20868
+ if (!quarantineProfileSyncData(profileDir))
20869
+ throw classifyLaunchError(error, profileDir, channel);
20870
+ try {
20871
+ context = await launch();
20872
+ } catch (retryError) {
20873
+ throw classifyLaunchError(retryError, profileDir, channel);
20800
20874
  }
20801
- throw error;
20802
20875
  }
20803
20876
  try {
20804
20877
  await context.addInitScript(() => {
@@ -20125,8 +20125,11 @@ import {
20125
20125
  mkdirSync as mkdirSync2,
20126
20126
  openSync,
20127
20127
  readdirSync as readdirSync2,
20128
+ readlinkSync,
20128
20129
  realpathSync as realpathSync2,
20129
- rmdirSync
20130
+ renameSync as renameSync2,
20131
+ rmdirSync,
20132
+ unlinkSync
20130
20133
  } from "fs";
20131
20134
  function lstat(path) {
20132
20135
  try {
@@ -20161,6 +20164,33 @@ function rmdir(path) {
20161
20164
  throw error;
20162
20165
  }
20163
20166
  }
20167
+ function readlink(path) {
20168
+ try {
20169
+ return readlinkSync(path);
20170
+ } catch {
20171
+ return null;
20172
+ }
20173
+ }
20174
+ function unlink(path) {
20175
+ try {
20176
+ unlinkSync(path);
20177
+ return true;
20178
+ } catch (error) {
20179
+ if (error.code === "ENOENT")
20180
+ return false;
20181
+ throw error;
20182
+ }
20183
+ }
20184
+ function rename(from, to) {
20185
+ try {
20186
+ renameSync2(from, to);
20187
+ return true;
20188
+ } catch (error) {
20189
+ if (error.code === "ENOENT")
20190
+ return false;
20191
+ throw error;
20192
+ }
20193
+ }
20164
20194
  function chmod(path, mode) {
20165
20195
  chmodSync(path, mode);
20166
20196
  }
@@ -20172,14 +20202,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join(homedir(), ".config", "kk", "browser-tra
20172
20202
 
20173
20203
  // ../../packages/publish-harness/src/profile-lease.ts
20174
20204
  import { createHash, randomBytes } from "crypto";
20175
- import { homedir as homedir2 } from "os";
20205
+ import { homedir as homedir2, hostname } from "os";
20176
20206
  import { basename, dirname as dirname3, join as join2, resolve as resolve2 } from "path";
20177
20207
  var DEFAULT_BOT_LEASE_ROOT = join2(homedir2(), ".config", "kk", "browser-leases");
20178
20208
  var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
20179
20209
  var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
20180
20210
  var OWNER_FILE = "owner.json";
20181
20211
  var HOLDER_PREFIX = "holder-";
20182
- var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
20212
+ var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
20183
20213
  async function acquireProfileLease(profileDir, options = {}) {
20184
20214
  const fileSystem = options.fileSystem ?? createNodeFileSystem();
20185
20215
  const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
@@ -20347,6 +20377,8 @@ function isProcessAlive(pid) {
20347
20377
  }
20348
20378
  }
20349
20379
  function refuseNativeLock(profileDir) {
20380
+ if (reclaimStaleNativeLock(profileDir))
20381
+ return;
20350
20382
  for (const name of NATIVE_LOCK_FILES) {
20351
20383
  const path = join2(profileDir, name);
20352
20384
  if (lstat(path) !== null) {
@@ -20354,6 +20386,24 @@ function refuseNativeLock(profileDir) {
20354
20386
  }
20355
20387
  }
20356
20388
  }
20389
+ function reclaimStaleNativeLock(profileDir) {
20390
+ const lockPath = join2(profileDir, "SingletonLock");
20391
+ const target = readlink(lockPath);
20392
+ if (target === null)
20393
+ return false;
20394
+ const match = /^(.*)-(\d+)$/.exec(target);
20395
+ if (match === null)
20396
+ return false;
20397
+ const [, host, pidText] = match;
20398
+ if (host !== hostname())
20399
+ return false;
20400
+ const pid = Number(pidText);
20401
+ if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
20402
+ return false;
20403
+ for (const name of NATIVE_LOCK_FILES)
20404
+ unlink(join2(profileDir, name));
20405
+ return true;
20406
+ }
20357
20407
  function sha256Hex(value) {
20358
20408
  return createHash("sha256").update(value).digest("hex");
20359
20409
  }
@@ -20432,6 +20482,27 @@ var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
20432
20482
  var DEFAULT_LOGIN_TIMEOUT_MS = 180000;
20433
20483
  var DEFAULT_OPEN_NAV_TIMEOUT_MS = 30000;
20434
20484
  var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20485
+ function isInstantBrowserDeath(message) {
20486
+ return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20487
+ }
20488
+ function classifyLaunchError(error, profileDir, channel) {
20489
+ const message = errorMessage2(error);
20490
+ if (/singleton|already in use|in use by another/i.test(message))
20491
+ return new BrowserProfileBusyError(profileDir, error);
20492
+ if (/playwright install|executable doesn't exist/i.test(message)) {
20493
+ return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
20494
+ }
20495
+ if (isInstantBrowserDeath(message)) {
20496
+ return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
20497
+ }
20498
+ return error instanceof Error ? error : new Error(message);
20499
+ }
20500
+ function quarantineProfileSyncData(profileDir) {
20501
+ const syncData = join5(profileDir, "Default", "Sync Data");
20502
+ if (lstat(syncData) === null)
20503
+ return false;
20504
+ return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
20505
+ }
20435
20506
  function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
20436
20507
  return {
20437
20508
  async launch(request) {
@@ -20460,23 +20531,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
20460
20531
  throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
20461
20532
  }
20462
20533
  const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
20534
+ const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
20535
+ headless,
20536
+ channel,
20537
+ args: ["--disable-blink-features=AutomationControlled"],
20538
+ ignoreDefaultArgs: ["--enable-automation"]
20539
+ });
20463
20540
  let context;
20464
20541
  try {
20465
- context = await playwright.chromium.launchPersistentContext(profileDir, {
20466
- headless,
20467
- channel,
20468
- args: ["--disable-blink-features=AutomationControlled"],
20469
- ignoreDefaultArgs: ["--enable-automation"]
20470
- });
20542
+ context = await launch();
20471
20543
  } catch (error) {
20472
- const message = errorMessage2(error);
20473
- if (/singleton|already in use|in use by another/i.test(message)) {
20474
- throw new BrowserProfileBusyError(profileDir, error);
20475
- }
20476
- if (/playwright install|executable doesn't exist/i.test(message)) {
20477
- throw new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
20544
+ if (!isInstantBrowserDeath(errorMessage2(error)))
20545
+ throw classifyLaunchError(error, profileDir, channel);
20546
+ if (!quarantineProfileSyncData(profileDir))
20547
+ throw classifyLaunchError(error, profileDir, channel);
20548
+ try {
20549
+ context = await launch();
20550
+ } catch (retryError) {
20551
+ throw classifyLaunchError(retryError, profileDir, channel);
20478
20552
  }
20479
- throw error;
20480
20553
  }
20481
20554
  try {
20482
20555
  await context.addInitScript(() => {
@@ -20633,6 +20706,7 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
20633
20706
  if (command === "login") {
20634
20707
  const loginTimeoutMs = timeoutMs ?? config.loginTimeoutMs ?? DEFAULT_LOGIN_TIMEOUT_MS;
20635
20708
  const launcher = deps.launcher ?? kkBrowserLauncher;
20709
+ stdout(`${config.channel} profile login: a browser window is opening \u2014 sign in there; waiting up to ${Math.round(loginTimeoutMs / 1000)}s for the signed-in state`);
20636
20710
  try {
20637
20711
  if (url === undefined) {
20638
20712
  await loginWithProfile({
@@ -20464,8 +20464,11 @@ import {
20464
20464
  mkdirSync as mkdirSync2,
20465
20465
  openSync,
20466
20466
  readdirSync as readdirSync2,
20467
+ readlinkSync,
20467
20468
  realpathSync as realpathSync2,
20468
- rmdirSync
20469
+ renameSync as renameSync2,
20470
+ rmdirSync,
20471
+ unlinkSync
20469
20472
  } from "fs";
20470
20473
  function lstat(path) {
20471
20474
  try {
@@ -20500,6 +20503,33 @@ function rmdir(path) {
20500
20503
  throw error;
20501
20504
  }
20502
20505
  }
20506
+ function readlink(path) {
20507
+ try {
20508
+ return readlinkSync(path);
20509
+ } catch {
20510
+ return null;
20511
+ }
20512
+ }
20513
+ function unlink(path) {
20514
+ try {
20515
+ unlinkSync(path);
20516
+ return true;
20517
+ } catch (error) {
20518
+ if (error.code === "ENOENT")
20519
+ return false;
20520
+ throw error;
20521
+ }
20522
+ }
20523
+ function rename(from, to) {
20524
+ try {
20525
+ renameSync2(from, to);
20526
+ return true;
20527
+ } catch (error) {
20528
+ if (error.code === "ENOENT")
20529
+ return false;
20530
+ throw error;
20531
+ }
20532
+ }
20503
20533
  function chmod(path, mode) {
20504
20534
  chmodSync(path, mode);
20505
20535
  }
@@ -20511,14 +20541,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join2(homedir2(), ".config", "kk", "browser-t
20511
20541
 
20512
20542
  // ../../packages/publish-harness/src/profile-lease.ts
20513
20543
  import { createHash, randomBytes } from "crypto";
20514
- import { homedir as homedir3 } from "os";
20544
+ import { homedir as homedir3, hostname } from "os";
20515
20545
  import { basename, dirname as dirname3, join as join3, resolve as resolve3 } from "path";
20516
20546
  var DEFAULT_BOT_LEASE_ROOT = join3(homedir3(), ".config", "kk", "browser-leases");
20517
20547
  var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
20518
20548
  var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
20519
20549
  var OWNER_FILE = "owner.json";
20520
20550
  var HOLDER_PREFIX = "holder-";
20521
- var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
20551
+ var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
20522
20552
  async function acquireProfileLease(profileDir, options = {}) {
20523
20553
  const fileSystem = options.fileSystem ?? createNodeFileSystem();
20524
20554
  const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
@@ -20686,6 +20716,8 @@ function isProcessAlive(pid) {
20686
20716
  }
20687
20717
  }
20688
20718
  function refuseNativeLock(profileDir) {
20719
+ if (reclaimStaleNativeLock(profileDir))
20720
+ return;
20689
20721
  for (const name of NATIVE_LOCK_FILES) {
20690
20722
  const path = join3(profileDir, name);
20691
20723
  if (lstat(path) !== null) {
@@ -20693,6 +20725,24 @@ function refuseNativeLock(profileDir) {
20693
20725
  }
20694
20726
  }
20695
20727
  }
20728
+ function reclaimStaleNativeLock(profileDir) {
20729
+ const lockPath = join3(profileDir, "SingletonLock");
20730
+ const target = readlink(lockPath);
20731
+ if (target === null)
20732
+ return false;
20733
+ const match = /^(.*)-(\d+)$/.exec(target);
20734
+ if (match === null)
20735
+ return false;
20736
+ const [, host, pidText] = match;
20737
+ if (host !== hostname())
20738
+ return false;
20739
+ const pid = Number(pidText);
20740
+ if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
20741
+ return false;
20742
+ for (const name of NATIVE_LOCK_FILES)
20743
+ unlink(join3(profileDir, name));
20744
+ return true;
20745
+ }
20696
20746
  function sha256Hex(value) {
20697
20747
  return createHash("sha256").update(value).digest("hex");
20698
20748
  }
@@ -20729,6 +20779,27 @@ var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20729
20779
  var DEFAULT_STEP_TIMEOUT_MS = 30000;
20730
20780
  var DEFAULT_POLL_MS = 500;
20731
20781
  var ATTACH_POLL_MS = 100;
20782
+ function isInstantBrowserDeath(message) {
20783
+ return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20784
+ }
20785
+ function classifyLaunchError(error, profileDir, channel) {
20786
+ const message = errorMessage2(error);
20787
+ if (/singleton|already in use|in use by another/i.test(message))
20788
+ return new BrowserProfileBusyError(profileDir, error);
20789
+ if (/playwright install|executable doesn't exist/i.test(message)) {
20790
+ return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
20791
+ }
20792
+ if (isInstantBrowserDeath(message)) {
20793
+ return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
20794
+ }
20795
+ return error instanceof Error ? error : new Error(message);
20796
+ }
20797
+ function quarantineProfileSyncData(profileDir) {
20798
+ const syncData = join5(profileDir, "Default", "Sync Data");
20799
+ if (lstat(syncData) === null)
20800
+ return false;
20801
+ return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
20802
+ }
20732
20803
  function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
20733
20804
  return {
20734
20805
  async launch(request) {
@@ -20757,23 +20828,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
20757
20828
  throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
20758
20829
  }
20759
20830
  const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
20831
+ const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
20832
+ headless,
20833
+ channel,
20834
+ args: ["--disable-blink-features=AutomationControlled"],
20835
+ ignoreDefaultArgs: ["--enable-automation"]
20836
+ });
20760
20837
  let context;
20761
20838
  try {
20762
- context = await playwright.chromium.launchPersistentContext(profileDir, {
20763
- headless,
20764
- channel,
20765
- args: ["--disable-blink-features=AutomationControlled"],
20766
- ignoreDefaultArgs: ["--enable-automation"]
20767
- });
20839
+ context = await launch();
20768
20840
  } catch (error) {
20769
- const message = errorMessage2(error);
20770
- if (/singleton|already in use|in use by another/i.test(message)) {
20771
- throw new BrowserProfileBusyError(profileDir, error);
20772
- }
20773
- if (/playwright install|executable doesn't exist/i.test(message)) {
20774
- throw new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
20841
+ if (!isInstantBrowserDeath(errorMessage2(error)))
20842
+ throw classifyLaunchError(error, profileDir, channel);
20843
+ if (!quarantineProfileSyncData(profileDir))
20844
+ throw classifyLaunchError(error, profileDir, channel);
20845
+ try {
20846
+ context = await launch();
20847
+ } catch (retryError) {
20848
+ throw classifyLaunchError(retryError, profileDir, channel);
20775
20849
  }
20776
- throw error;
20777
20850
  }
20778
20851
  try {
20779
20852
  await context.addInitScript(() => {
@@ -20133,8 +20133,11 @@ import {
20133
20133
  mkdirSync as mkdirSync2,
20134
20134
  openSync,
20135
20135
  readdirSync as readdirSync2,
20136
+ readlinkSync,
20136
20137
  realpathSync as realpathSync2,
20137
- rmdirSync
20138
+ renameSync as renameSync2,
20139
+ rmdirSync,
20140
+ unlinkSync
20138
20141
  } from "fs";
20139
20142
  function lstat(path) {
20140
20143
  try {
@@ -20169,6 +20172,33 @@ function rmdir(path) {
20169
20172
  throw error;
20170
20173
  }
20171
20174
  }
20175
+ function readlink(path) {
20176
+ try {
20177
+ return readlinkSync(path);
20178
+ } catch {
20179
+ return null;
20180
+ }
20181
+ }
20182
+ function unlink(path) {
20183
+ try {
20184
+ unlinkSync(path);
20185
+ return true;
20186
+ } catch (error) {
20187
+ if (error.code === "ENOENT")
20188
+ return false;
20189
+ throw error;
20190
+ }
20191
+ }
20192
+ function rename(from, to) {
20193
+ try {
20194
+ renameSync2(from, to);
20195
+ return true;
20196
+ } catch (error) {
20197
+ if (error.code === "ENOENT")
20198
+ return false;
20199
+ throw error;
20200
+ }
20201
+ }
20172
20202
  function chmod(path, mode) {
20173
20203
  chmodSync(path, mode);
20174
20204
  }
@@ -20180,14 +20210,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join(homedir(), ".config", "kk", "browser-tra
20180
20210
 
20181
20211
  // ../../packages/publish-harness/src/profile-lease.ts
20182
20212
  import { createHash, randomBytes } from "crypto";
20183
- import { homedir as homedir2 } from "os";
20213
+ import { homedir as homedir2, hostname } from "os";
20184
20214
  import { basename, dirname as dirname3, join as join2, resolve as resolve2 } from "path";
20185
20215
  var DEFAULT_BOT_LEASE_ROOT = join2(homedir2(), ".config", "kk", "browser-leases");
20186
20216
  var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
20187
20217
  var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
20188
20218
  var OWNER_FILE = "owner.json";
20189
20219
  var HOLDER_PREFIX = "holder-";
20190
- var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
20220
+ var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
20191
20221
  async function acquireProfileLease(profileDir, options = {}) {
20192
20222
  const fileSystem = options.fileSystem ?? createNodeFileSystem();
20193
20223
  const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
@@ -20355,6 +20385,8 @@ function isProcessAlive(pid) {
20355
20385
  }
20356
20386
  }
20357
20387
  function refuseNativeLock(profileDir) {
20388
+ if (reclaimStaleNativeLock(profileDir))
20389
+ return;
20358
20390
  for (const name of NATIVE_LOCK_FILES) {
20359
20391
  const path = join2(profileDir, name);
20360
20392
  if (lstat(path) !== null) {
@@ -20362,6 +20394,24 @@ function refuseNativeLock(profileDir) {
20362
20394
  }
20363
20395
  }
20364
20396
  }
20397
+ function reclaimStaleNativeLock(profileDir) {
20398
+ const lockPath = join2(profileDir, "SingletonLock");
20399
+ const target = readlink(lockPath);
20400
+ if (target === null)
20401
+ return false;
20402
+ const match = /^(.*)-(\d+)$/.exec(target);
20403
+ if (match === null)
20404
+ return false;
20405
+ const [, host, pidText] = match;
20406
+ if (host !== hostname())
20407
+ return false;
20408
+ const pid = Number(pidText);
20409
+ if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
20410
+ return false;
20411
+ for (const name of NATIVE_LOCK_FILES)
20412
+ unlink(join2(profileDir, name));
20413
+ return true;
20414
+ }
20365
20415
  function sha256Hex(value) {
20366
20416
  return createHash("sha256").update(value).digest("hex");
20367
20417
  }
@@ -20430,6 +20480,27 @@ async function ensureProfileDir2(profileDir, headless) {
20430
20480
  var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
20431
20481
  // ../../packages/publish-harness/src/browser.ts
20432
20482
  var DEFAULT_CHECK_TIMEOUT_MS = 30000;
20483
+ function isInstantBrowserDeath(message) {
20484
+ return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
20485
+ }
20486
+ function classifyLaunchError(error, profileDir, channel) {
20487
+ const message = errorMessage2(error);
20488
+ if (/singleton|already in use|in use by another/i.test(message))
20489
+ return new BrowserProfileBusyError(profileDir, error);
20490
+ if (/playwright install|executable doesn't exist/i.test(message)) {
20491
+ return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
20492
+ }
20493
+ if (isInstantBrowserDeath(message)) {
20494
+ return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
20495
+ }
20496
+ return error instanceof Error ? error : new Error(message);
20497
+ }
20498
+ function quarantineProfileSyncData(profileDir) {
20499
+ const syncData = join5(profileDir, "Default", "Sync Data");
20500
+ if (lstat(syncData) === null)
20501
+ return false;
20502
+ return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
20503
+ }
20433
20504
  function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
20434
20505
  return {
20435
20506
  async launch(request) {
@@ -20458,23 +20529,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
20458
20529
  throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
20459
20530
  }
20460
20531
  const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
20532
+ const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
20533
+ headless,
20534
+ channel,
20535
+ args: ["--disable-blink-features=AutomationControlled"],
20536
+ ignoreDefaultArgs: ["--enable-automation"]
20537
+ });
20461
20538
  let context;
20462
20539
  try {
20463
- context = await playwright.chromium.launchPersistentContext(profileDir, {
20464
- headless,
20465
- channel,
20466
- args: ["--disable-blink-features=AutomationControlled"],
20467
- ignoreDefaultArgs: ["--enable-automation"]
20468
- });
20540
+ context = await launch();
20469
20541
  } catch (error) {
20470
- const message = errorMessage2(error);
20471
- if (/singleton|already in use|in use by another/i.test(message)) {
20472
- throw new BrowserProfileBusyError(profileDir, error);
20473
- }
20474
- if (/playwright install|executable doesn't exist/i.test(message)) {
20475
- throw new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
20542
+ if (!isInstantBrowserDeath(errorMessage2(error)))
20543
+ throw classifyLaunchError(error, profileDir, channel);
20544
+ if (!quarantineProfileSyncData(profileDir))
20545
+ throw classifyLaunchError(error, profileDir, channel);
20546
+ try {
20547
+ context = await launch();
20548
+ } catch (retryError) {
20549
+ throw classifyLaunchError(retryError, profileDir, channel);
20476
20550
  }
20477
- throw error;
20478
20551
  }
20479
20552
  try {
20480
20553
  await context.addInitScript(() => {