@tiny-fish/cli 0.18.1-next.172 → 0.18.1-next.173

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.
@@ -1,7 +1,7 @@
1
1
  import { spawn } from "child_process";
2
2
  import * as readline from "readline";
3
3
  import { Option } from "commander";
4
- import { clearConfig, getDashboardUrl, loadConfig, maskKey, saveConfig, validateKeyFormat, } from "../lib/auth.js";
4
+ import { CONNECT_ATTEMPT_ENV, clearConfig, getDashboardUrl, loadConfig, maskKey, savePendingConnectAttempt, saveConfig, validateKeyFormat, } from "../lib/auth.js";
5
5
  import { err, errLine, out, outLine } from "../lib/output.js";
6
6
  /**
7
7
  * Read a key from stdin without echoing it to the terminal.
@@ -66,6 +66,13 @@ async function readKeyFromStdinOrPrompt() {
66
66
  }
67
67
  return readKeyHidden("Paste your API key: ");
68
68
  }
69
+ // Sign-in and the connect that follows are separate copied commands, so a page-minted id
70
+ // handed to this process has to outlive it.
71
+ function parkSeedAttempt() {
72
+ const seed = process.env[CONNECT_ATTEMPT_ENV];
73
+ if (seed)
74
+ savePendingConnectAttempt(seed);
75
+ }
69
76
  export function registerAuth(program) {
70
77
  const auth = program.command("auth").description("Manage your TinyFish API key");
71
78
  auth
@@ -110,6 +117,7 @@ export function registerAuth(program) {
110
117
  process.exit(1);
111
118
  }
112
119
  saveConfig(key);
120
+ parkSeedAttempt();
113
121
  out({ status: "ok", message: "API key saved", key_preview: maskKey(key) });
114
122
  });
115
123
  auth
@@ -127,6 +135,7 @@ export function registerAuth(program) {
127
135
  process.exit(1);
128
136
  }
129
137
  saveConfig(key);
138
+ parkSeedAttempt();
130
139
  out({ status: "ok", message: "API key saved", key_preview: maskKey(key) });
131
140
  });
132
141
  auth
@@ -1,7 +1,7 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import * as path from "node:path";
3
3
  import spawn from "cross-spawn";
4
- import { CONNECT_SOURCE, loadConfig, persistApiKeyToEnvironment, resolvedApiKey, saveConnectContext, validateKeyFormat, validatedApiKey, writeConfig, } from "../lib/auth.js";
4
+ import { CONNECT_ATTEMPT_ENV, CONNECT_SOURCE, isAttemptId, loadConfig, persistApiKeyToEnvironment, resolvedApiKey, saveConnectContext, takePendingConnectAttempt, validateKeyFormat, validatedApiKey, writeConfig, } from "../lib/auth.js";
5
5
  import { CLI_VERSION, TINYFISH_CLI_PACKAGE } from "../lib/constants.js";
6
6
  import { cursorInstallDeeplink, cursorMcpPath, writeCursorMcpConfig, } from "../lib/cursor-config.js";
7
7
  import { runConnectAll } from "../lib/connect-all.js";
@@ -287,9 +287,24 @@ const OPENCLAW = {
287
287
  unavailableMessage: OPENCLAW_SKILL_UNAVAILABLE_MESSAGE,
288
288
  },
289
289
  };
290
+ /**
291
+ * One page-minted id seeds one install attempt, so `tinyfish onboard` connecting three agents
292
+ * does not report three installs under it. Later connects in the run fall back to a random id.
293
+ */
294
+ function takeSeedAttemptId() {
295
+ const fromEnv = process.env[CONNECT_ATTEMPT_ENV];
296
+ // Both sources are read-and-clear, and the delete lands before any subprocess inherits it.
297
+ delete process.env[CONNECT_ATTEMPT_ENV];
298
+ const parked = takePendingConnectAttempt();
299
+ return fromEnv && isAttemptId(fromEnv) ? fromEnv : parked;
300
+ }
290
301
  function createConnectTelemetry(mcpUrl, client, opts) {
291
- // A setup-page id (via --url) joins "copied the command" to this install attempt.
292
- const attemptId = opts?.attemptId ?? randomUUID();
302
+ // Consumed even when --url already supplied an id: leaving it behind would let a stale
303
+ // seed reach the agent this connect launches, or the next connect on this machine.
304
+ const seeded = takeSeedAttemptId();
305
+ // A setup-page id (via --url, the env var, or one parked by login) joins "copied the
306
+ // command" to this install attempt.
307
+ const attemptId = opts?.attemptId ?? seeded ?? randomUUID();
293
308
  const endpoint = new URL("/api/cli/connect-event", mcpUrl).toString();
294
309
  const pending = [];
295
310
  async function deliver(body) {
@@ -2,14 +2,28 @@ export { DASHBOARD_URL, getDashboardUrl } from "./constants.js";
2
2
  export declare function configDir(): string;
3
3
  /** Install channel baked onto connect context; the connect flow is always CLI-driven. */
4
4
  export declare const CONNECT_SOURCE = "tinyfish_cli";
5
+ export declare const CONNECT_ATTEMPT_ENV = "TINYFISH_CONNECT_ATTEMPT_ID";
5
6
  interface ConnectEntry {
6
7
  attempt_id: string;
7
8
  }
9
+ interface PendingConnectAttempt {
10
+ id: string;
11
+ ts: number;
12
+ }
8
13
  interface TinyfishConfig {
9
14
  api_key?: string;
10
15
  connect?: Record<string, ConnectEntry>;
16
+ pending_connect_attempt?: PendingConnectAttempt;
11
17
  }
12
18
  export declare function loadConfig(): TinyfishConfig;
19
+ export declare function isAttemptId(value: string): boolean;
20
+ /**
21
+ * Park a page-minted attempt id for the connect that follows in a later process:
22
+ * `auth login` and the connect it leads to are separate copied commands.
23
+ */
24
+ export declare function savePendingConnectAttempt(attemptId: string): void;
25
+ /** Read-and-clear: one parked id seeds one install attempt, expired or not. */
26
+ export declare function takePendingConnectAttempt(): string | undefined;
13
27
  /** Throwing variant for callers with their own cleanup (connect's telemetry flush). */
14
28
  export declare function writeConfig(apiKey: string): void;
15
29
  export declare function saveConfig(apiKey: string): void;
package/dist/lib/auth.js CHANGED
@@ -15,6 +15,9 @@ function configFile() {
15
15
  }
16
16
  /** Install channel baked onto connect context; the connect flow is always CLI-driven. */
17
17
  export const CONNECT_SOURCE = "tinyfish_cli";
18
+ // An env var, not a flag: released CLIs ignore an unknown one, and the installer's PATH
19
+ // prepend means a copied command can still run a CLI far older than the page that wrote it.
20
+ export const CONNECT_ATTEMPT_ENV = "TINYFISH_CONNECT_ATTEMPT_ID";
18
21
  export function loadConfig() {
19
22
  try {
20
23
  const raw = JSON.parse(fs.readFileSync(configFile(), "utf8"));
@@ -27,6 +30,9 @@ export function loadConfig() {
27
30
  const connect = parseConnectMap(obj["connect"]);
28
31
  if (connect)
29
32
  config.connect = connect;
33
+ const pending = parsePendingAttempt(obj["pending_connect_attempt"]);
34
+ if (pending)
35
+ config.pending_connect_attempt = pending;
30
36
  return config;
31
37
  }
32
38
  catch {
@@ -44,16 +50,63 @@ function parseConnectMap(value) {
44
50
  }
45
51
  return Object.keys(out).length > 0 ? out : undefined;
46
52
  }
47
- /** Throwing variant for callers with their own cleanup (connect's telemetry flush). */
48
- export function writeConfig(apiKey) {
49
- const dir = configDir();
53
+ function parsePendingAttempt(value) {
54
+ const entry = value;
55
+ if (typeof entry?.id !== "string" || typeof entry.ts !== "number")
56
+ return undefined;
57
+ return isAttemptId(entry.id) ? { id: entry.id, ts: entry.ts } : undefined;
58
+ }
59
+ export function isAttemptId(value) {
60
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
61
+ }
62
+ /**
63
+ * The one read-modify-write of the config file. Every caller goes through it so the
64
+ * loadConfig whitelist stays the single definition of what survives a write. Throws;
65
+ * callers own their failure policy, which differs per command.
66
+ */
67
+ function updateConfig(change) {
50
68
  // 0o700: only owner can read/write/execute the directory
51
- fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
52
- const existing = loadConfig();
69
+ fs.mkdirSync(configDir(), { recursive: true, mode: 0o700 });
53
70
  // 0o600: only owner can read/write the config file (key is sensitive)
54
- fs.writeFileSync(configFile(), JSON.stringify({ ...existing, api_key: apiKey }), {
55
- mode: 0o600,
56
- });
71
+ fs.writeFileSync(configFile(), JSON.stringify(change(loadConfig())), { mode: 0o600 });
72
+ }
73
+ /**
74
+ * Park a page-minted attempt id for the connect that follows in a later process:
75
+ * `auth login` and the connect it leads to are separate copied commands.
76
+ */
77
+ export function savePendingConnectAttempt(attemptId) {
78
+ if (!isAttemptId(attemptId))
79
+ return;
80
+ try {
81
+ updateConfig((config) => ({
82
+ ...config,
83
+ pending_connect_attempt: { id: attemptId, ts: Date.now() },
84
+ }));
85
+ }
86
+ catch {
87
+ // Attribution is never worth failing a sign-in for.
88
+ }
89
+ }
90
+ // Older than this and the id belongs to some abandoned setup, not the connect running now.
91
+ const PENDING_ATTEMPT_MAX_AGE_MS = 24 * 60 * 60 * 1000;
92
+ /** Read-and-clear: one parked id seeds one install attempt, expired or not. */
93
+ export function takePendingConnectAttempt() {
94
+ const pending = loadConfig().pending_connect_attempt;
95
+ if (!pending)
96
+ return undefined;
97
+ try {
98
+ updateConfig(({ pending_connect_attempt: _cleared, ...rest }) => rest);
99
+ }
100
+ catch {
101
+ // An id still on disk would be claimed twice, and a double-counted install is worse
102
+ // than a missed join.
103
+ return undefined;
104
+ }
105
+ return Date.now() - pending.ts <= PENDING_ATTEMPT_MAX_AGE_MS ? pending.id : undefined;
106
+ }
107
+ /** Throwing variant for callers with their own cleanup (connect's telemetry flush). */
108
+ export function writeConfig(apiKey) {
109
+ updateConfig((config) => ({ ...config, api_key: apiKey }));
57
110
  }
58
111
  export function saveConfig(apiKey) {
59
112
  try {
@@ -65,12 +118,11 @@ export function saveConfig(apiKey) {
65
118
  }
66
119
  }
67
120
  export function saveConnectContext(client, attemptId) {
68
- const dir = configDir();
69
121
  try {
70
- fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
71
- const existing = loadConfig();
72
- const connect = { ...existing.connect, [client]: { attempt_id: attemptId } };
73
- fs.writeFileSync(configFile(), JSON.stringify({ ...existing, connect }), { mode: 0o600 });
122
+ updateConfig((config) => ({
123
+ ...config,
124
+ connect: { ...config.connect, [client]: { attempt_id: attemptId } },
125
+ }));
74
126
  }
75
127
  catch (e) {
76
128
  // Best-effort attribution marker: a write failure must not fail a connect that has
@@ -119,12 +171,12 @@ export function persistApiKeyToEnvironment(apiKey, options = {}) {
119
171
  fs.chmodSync(profile, 0o600);
120
172
  }
121
173
  export function clearConfig() {
122
- const config = loadConfig();
123
- if (!config.api_key)
174
+ if (!loadConfig().api_key)
124
175
  return false;
125
- delete config.api_key;
126
176
  try {
127
- fs.writeFileSync(configFile(), JSON.stringify(config), { mode: 0o600 });
177
+ // The parked id goes too: the next person to sign in on this machine is not the one
178
+ // whose copy minted it.
179
+ updateConfig(({ api_key: _key, pending_connect_attempt: _attempt, ...rest }) => rest);
128
180
  return true;
129
181
  }
130
182
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiny-fish/cli",
3
- "version": "0.18.1-next.172",
3
+ "version": "0.18.1-next.173",
4
4
  "description": "TinyFish CLI — run web automations from your terminal",
5
5
  "type": "module",
6
6
  "bin": {