alink-cli 0.8.7 → 0.8.8

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/bin.mjs CHANGED
@@ -51996,12 +51996,20 @@ const makeWsRpcLayer = (currentSession) => WsRpcGroup.toLayer(gen(function* () {
51996
51996
  return startup.enqueueCommand(dispatchEffect).pipe(mapError((cause) => toDispatchCommandError(cause, "Failed to dispatch orchestration command")));
51997
51997
  };
51998
51998
  const loadServerConfig = gen(function* () {
51999
+ console.error("[DEBUG-provider-checking-20260820] getConfig start");
51999
52000
  const keybindingsConfig = yield* keybindings.loadConfigState;
52001
+ console.error("[DEBUG-provider-checking-20260820] getConfig keybindings");
52000
52002
  const providers = yield* providerRegistry.getProviders;
52003
+ console.error("[DEBUG-provider-checking-20260820] getConfig providers");
52001
52004
  const settings = redactServerSettingsForClient(yield* serverSettings.getSettings);
52005
+ console.error("[DEBUG-provider-checking-20260820] getConfig settings");
52006
+ const environment = yield* serverEnvironment.getDescriptor;
52007
+ console.error("[DEBUG-provider-checking-20260820] getConfig environment");
52008
+ const auth = yield* serverAuth.getDescriptor();
52009
+ console.error("[DEBUG-provider-checking-20260820] getConfig auth");
52002
52010
  return {
52003
- environment: yield* serverEnvironment.getDescriptor,
52004
- auth: yield* serverAuth.getDescriptor(),
52011
+ environment,
52012
+ auth,
52005
52013
  cwd: config.cwd,
52006
52014
  keybindingsConfigPath: config.keybindingsConfigPath,
52007
52015
  keybindings: keybindingsConfig.keybindings,
@@ -52350,6 +52358,36 @@ function isEnvelope(value) {
52350
52358
  //#endregion
52351
52359
  //#region src/tunnel/bridge.ts
52352
52360
  const TUNNEL_SCOPES = [AuthOrchestrationReadScope, AuthOrchestrationOperateScope];
52361
+ function machineIdFromToken(token) {
52362
+ const parts = token.split(".");
52363
+ if (parts.length !== 3 || parts[0] !== "al1" || !parts[1]) return null;
52364
+ try {
52365
+ const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
52366
+ return typeof payload.mid === "string" && payload.mid.length > 0 ? payload.mid : null;
52367
+ } catch {
52368
+ return null;
52369
+ }
52370
+ }
52371
+ function hubHttpUrl(hub) {
52372
+ const url = new URL(hub);
52373
+ url.protocol = url.protocol === "wss:" ? "https:" : "http:";
52374
+ url.pathname = "";
52375
+ url.search = "";
52376
+ url.hash = "";
52377
+ return url.toString().replace(/\/$/, "");
52378
+ }
52379
+ const uploadHubEnckey = (config) => gen(function* () {
52380
+ const machineId = machineIdFromToken(config.machineToken);
52381
+ if (!machineId) return yield* fail(/* @__PURE__ */ new Error("machine id is missing from the machine token"));
52382
+ const httpClient = yield* HttpClient;
52383
+ const request = yield* post$1(`${hubHttpUrl(config.hubUrl)}/api/machines/${encodeURIComponent(machineId)}/enckey`).pipe(setHeaders({
52384
+ authorization: `Bearer ${config.machineToken}`,
52385
+ "content-type": "application/json",
52386
+ "x-requested-with": "agentlink"
52387
+ }), bodyJson({ enckey: config.enckey }));
52388
+ yield* filterStatusOk(yield* httpClient.execute(request));
52389
+ return machineId;
52390
+ });
52353
52391
  const hubFrameDecoder = new TextDecoder();
52354
52392
  const ResponseDefectEncoded = (cause) => ({
52355
52393
  _tag: "Defect",
@@ -52387,17 +52425,30 @@ const makeTunnelSocketProtocol = gen(function* () {
52387
52425
  };
52388
52426
  clients.set(id, { write });
52389
52427
  clientIds.add(id);
52428
+ console.error("[DEBUG-provider-checking-20260820] rpc socket ready", { clientId: id });
52390
52429
  yield* socket.runRaw((data) => {
52391
52430
  try {
52431
+ console.error("[DEBUG-provider-checking-20260820] rpc input", { byteLength: typeof data === "string" ? data.length : data.byteLength });
52392
52432
  const decoded = parser.decode(data);
52433
+ console.error("[DEBUG-provider-checking-20260820] rpc decoded", {
52434
+ count: decoded.length,
52435
+ messages: decoded.map((item) => typeof item === "object" && item !== null ? {
52436
+ tag: "tag" in item ? String(item.tag) : void 0,
52437
+ id: "id" in item ? String(item.id) : void 0
52438
+ } : { type: typeof item })
52439
+ });
52393
52440
  if (decoded.length === 0) return void_$1;
52394
52441
  let i = 0;
52395
52442
  return whileLoop({
52396
52443
  while: () => i < decoded.length,
52397
- body: () => writeRequest(id, decoded[i++]),
52444
+ body: () => {
52445
+ console.error("[DEBUG-provider-checking-20260820] rpc dispatch", { clientId: id });
52446
+ return writeRequest(id, decoded[i++]);
52447
+ },
52398
52448
  step: () => void 0
52399
52449
  });
52400
52450
  } catch (cause) {
52451
+ console.error("[DEBUG-provider-checking-20260820] rpc decode error", { message: cause instanceof Error ? cause.message : String(cause) });
52401
52452
  if (isTagged(cause, "MaxBufferSizeExceeded")) return orDie(writeRaw(new CloseEvent(1009, String(cause))));
52402
52453
  const defect = parser.encode(ResponseDefectEncoded(cause));
52403
52454
  return defect === void 0 ? void_$1 : orDie(writeRaw(defect));
@@ -52456,18 +52507,33 @@ const makeTunnelSocket = (input) => {
52456
52507
  return make$63({
52457
52508
  runRaw,
52458
52509
  writer: sync(() => (chunk) => sync(() => {
52459
- if (typeof chunk === "string") input.sendEncrypted(JSON.stringify(utf8ToEnvelope(input.enckey, chunk)));
52510
+ if (typeof chunk === "string") {
52511
+ console.error("[DEBUG-provider-checking-20260820] rpc outbound", { byteLength: chunk.length });
52512
+ input.sendEncrypted(JSON.stringify(utf8ToEnvelope(input.enckey, chunk)));
52513
+ }
52460
52514
  }))
52461
52515
  });
52462
52516
  };
52463
52517
  const runHubTunnel = (config) => gen(function* () {
52464
52518
  const serverAuth = yield* EnvironmentAuth;
52465
52519
  const sessionsStore = yield* SessionStore;
52520
+ const serverConfig = yield* ServerConfig;
52521
+ const httpClient = yield* HttpClient;
52466
52522
  yield* retry(gen(function* () {
52467
52523
  yield* logInfo("hub tunnel: connection effect entered");
52468
52524
  const url = `${config.hubUrl.replace(/\/+$/, "")}/daemon-tunnel?token=${encodeURIComponent(config.machineToken)}&v=1`;
52469
52525
  yield* logInfo("hub tunnel: connecting", { hubUrl: config.hubUrl });
52470
52526
  const socket = yield* makeWebSocket(url).pipe(provide$1(layerWebSocketConstructor));
52527
+ const upload = gen(function* () {
52528
+ const machineId = machineIdFromToken(config.machineToken);
52529
+ if (!machineId) {
52530
+ yield* logWarning$1("[daemon] enckey upload skipped: machine id missing from token");
52531
+ return;
52532
+ }
52533
+ yield* logInfo(`[daemon] uploading enckey for machine ${machineId}...`);
52534
+ yield* uploadHubEnckey(config);
52535
+ yield* logInfo("[daemon] enckey uploaded to hub for cross-device recovery");
52536
+ }).pipe(provideService(HttpClient, httpClient), catch_((error) => logError(`[daemon] enckey upload failed: ${String(error)}`)), asVoid);
52471
52537
  yield* addFinalizer((exit) => logInfo("hub tunnel: connection scope closing", { exit: String(exit) }));
52472
52538
  const tunnels = /* @__PURE__ */ new Map();
52473
52539
  const dropTunnel = (tunnelId) => {
@@ -52511,12 +52577,14 @@ const runHubTunnel = (config) => gen(function* () {
52511
52577
  }
52512
52578
  });
52513
52579
  const { protocol, onSocket } = yield* makeTunnelSocketProtocol.pipe(provide$1(layerJson));
52514
- yield* make$13(WsRpcGroup, { disableTracing: true }).pipe(provideService(Protocol, protocol), provide$1(makeWsRpcLayer({
52580
+ console.error("[DEBUG-provider-checking-20260820] rpc server starting");
52581
+ yield* forkScoped(make$13(WsRpcGroup, { disableTracing: true }).pipe(provideService(Protocol, protocol), provide$1(makeWsRpcLayer({
52515
52582
  sessionId: issued.sessionId,
52516
52583
  subject: "hub-tunnel",
52517
52584
  method: "bearer-access-token",
52518
52585
  scopes: [...TUNNEL_SCOPES]
52519
- }).pipe(provide(layer$8))), forkScoped);
52586
+ }).pipe(provideMerge(layerJson), provide(layer$8))), tapCause((cause) => logError("hub tunnel: RPC server failed", { cause: String(cause) }))));
52587
+ console.error("[DEBUG-provider-checking-20260820] rpc server started");
52520
52588
  yield* forkScoped(onSocket(socketAdapter));
52521
52589
  if (!writeHub(JSON.stringify({
52522
52590
  type: "tunnel_open",
@@ -52549,8 +52617,6 @@ const runHubTunnel = (config) => gen(function* () {
52549
52617
  });
52550
52618
  yield* addFinalizer(() => sessionsStore.markDisconnected(issued.sessionId).pipe(andThen(serverAuth.revokeSession(issued.sessionId)), ignore$1));
52551
52619
  yield* sessionsStore.markConnected(issued.sessionId);
52552
- const serverConfig = yield* ServerConfig;
52553
- const httpClient = yield* HttpClient;
52554
52620
  const localBaseUrl = `http://127.0.0.1:${serverConfig.port}`;
52555
52621
  const sendEncrypted = (frame) => {
52556
52622
  try {
@@ -52630,11 +52696,20 @@ const runHubTunnel = (config) => gen(function* () {
52630
52696
  purpose: live.purpose
52631
52697
  });
52632
52698
  yield* _await(live.closed);
52633
- }).pipe(catch_((error) => logWarning$1("hub tunnel: attach failed", {
52699
+ }).pipe(provideService(HttpClient, httpClient), catch_((error) => logWarning$1("hub tunnel: attach failed", {
52634
52700
  tunnelId,
52635
52701
  error
52636
52702
  })));
52703
+ let debugProviderCheckingFrameCount = 0;
52637
52704
  yield* socket.runRaw((rawData) => {
52705
+ if (debugProviderCheckingFrameCount < 20) {
52706
+ debugProviderCheckingFrameCount += 1;
52707
+ console.error("[DEBUG-provider-checking-20260820] hub frame", {
52708
+ count: debugProviderCheckingFrameCount,
52709
+ type: typeof rawData,
52710
+ byteLength: typeof rawData === "string" ? rawData.length : rawData instanceof Uint8Array ? rawData.byteLength : -1
52711
+ });
52712
+ }
52638
52713
  const data = typeof rawData === "string" ? rawData : rawData instanceof Uint8Array ? hubFrameDecoder.decode(rawData) : null;
52639
52714
  if (data === null) return;
52640
52715
  if (data.startsWith("{")) {
@@ -52657,7 +52732,8 @@ const runHubTunnel = (config) => gen(function* () {
52657
52732
  onDecryptedFrame: null
52658
52733
  };
52659
52734
  tunnels.set(tunnelId, live);
52660
- yield* forkScoped(scoped((purpose === "http" ? attachHttpTunnel : attachRpcTunnel)(tunnelId, live)));
52735
+ if (purpose === "http") runFork(scoped(attachHttpTunnel(tunnelId, live)));
52736
+ else runFork(scoped(attachRpcTunnel(tunnelId, live)));
52661
52737
  });
52662
52738
  }
52663
52739
  if (control.type === "tunnel_close" && typeof control.tunnelId === "string") dropTunnel(control.tunnelId);
@@ -52680,12 +52756,17 @@ const runHubTunnel = (config) => gen(function* () {
52680
52756
  } catch {
52681
52757
  return dropTunnel(framed.tunnelId);
52682
52758
  }
52759
+ console.error("[DEBUG-provider-checking-20260820] frame decrypted", {
52760
+ tunnelId: framed.tunnelId,
52761
+ byteLength: frame.length,
52762
+ delivery: live.onDecryptedFrame ? "deliver" : "pending"
52763
+ });
52683
52764
  if (live.onDecryptedFrame) live.onDecryptedFrame(frame);
52684
52765
  else if (live.pendingFrames.length < 16) live.pendingFrames.push(frame);
52685
52766
  else dropTunnel(framed.tunnelId);
52686
52767
  return;
52687
52768
  }
52688
- }, { onOpen: logInfo("hub tunnel: connected to hub") }).pipe(tapDefect((cause) => logWarning$1("hub tunnel: hub socket defect", { cause: String(cause) })));
52769
+ }, { onOpen: logInfo("hub tunnel: connected to hub").pipe(tap(() => sync(() => void runFork(upload)))) }).pipe(tapDefect((cause) => logWarning$1("hub tunnel: hub socket defect", { cause: String(cause) })));
52689
52770
  yield* logWarning$1("hub tunnel: hub connection closed");
52690
52771
  }).pipe(scoped, tapError((error) => logWarning$1("hub tunnel: connection error", { error }))), {
52691
52772
  schedule: spaced("2 seconds"),