@turns/dev 0.0.2 → 0.0.3

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.
@@ -682,8 +682,13 @@ class AcpTransport extends LineTransport {
682
682
  await this.request('session/set_mode', { sessionId: this.sessionId, modeId: this.profile.mode }, 30_000);
683
683
  }
684
684
  const model = this.anchor.model && this.anchor.model !== 'auto' ? this.anchor.model : this.profile.model;
685
- if (model)
686
- await this.request('session/set_model', { sessionId: this.sessionId, modelId: model }, 30_000).catch(() => { });
685
+ if (model) {
686
+ const method = this.profile.modelOption ? 'session/set_config_option' : 'session/set_model';
687
+ const params = this.profile.modelOption
688
+ ? { sessionId: this.sessionId, configId: 'model', value: model }
689
+ : { sessionId: this.sessionId, modelId: model };
690
+ await this.request(method, params, 30_000).catch(() => { });
691
+ }
687
692
  this.ready = true;
688
693
  }
689
694
  prompt(text, timeoutMs) {
@@ -1,3 +1,4 @@
1
+ import crypto from 'node:crypto';
1
2
  import fs from 'node:fs';
2
3
  import os from 'node:os';
3
4
  import path from 'node:path';
@@ -7,16 +8,14 @@ import { uploadFileBytes, uploadFileStream } from "./path-resolution.js";
7
8
  * Answering "what is in that directory?" from the machine that knows.
8
9
  *
9
10
  * A reader's browser cannot reach this machine, and this machine will not open
10
- * a port to let it try. So a browse is served the way an embedded image already
11
- * is: the daemon reads the path, puts what it found in the account's own
12
- * prefix, and the server hands the reader a signed URL for that object. Regular
13
- * files stream from disk to that object; nothing stays open once the one browse
14
- * upload finishes.
11
+ * a port to let it try. Directories are small structured answers, so the daemon
12
+ * sends their listing on the control socket immediately and stores the same
13
+ * bytes in the background for offline browsing. Regular files still stream to
14
+ * object storage; nothing stays open once the one browse upload finishes.
15
15
  *
16
- * A directory has no bytes, so what is uploaded for one is a listing of it,
17
- * stored at `_index.json` beneath the directory. Every path is canonicalised
18
- * before the allowlist is consulted, so a symlink pointing out of an allowed
19
- * tree is not a way to read the rest of the disk.
16
+ * A directory's fallback is stored at `_index.json` beneath it. Every path is
17
+ * canonicalised before the allowlist is consulted, so a symlink pointing out
18
+ * of an allowed tree is not a way to read the rest of the disk.
20
19
  */
21
20
  /** Kept in step with `MAX_DIRECTORY_ENTRIES` in the Worker's shared listing. */
22
21
  export const MAX_DIRECTORY_ENTRIES = 2_000;
@@ -26,62 +25,9 @@ export const DIRECTORY_INDEX_NAME = '_index.json';
26
25
  export const MAX_BROWSE_FILE_BYTES = 199 * 1024 * 1024;
27
26
  /** The derived label used in reader-facing errors. */
28
27
  export const BROWSE_FILE_LIMIT_LABEL = `${Math.round(MAX_BROWSE_FILE_BYTES / (1024 * 1024))} MB`;
29
- /** Extensions a reader can be shown as text, whatever produced them. */
30
- const TEXT_EXTENSIONS = new Set([
31
- '.bash',
32
- '.c',
33
- '.cc',
34
- '.cfg',
35
- '.conf',
36
- '.cpp',
37
- '.cs',
38
- '.css',
39
- '.csv',
40
- '.diff',
41
- '.env',
42
- '.go',
43
- '.gradle',
44
- '.h',
45
- '.hpp',
46
- '.htm',
47
- '.html',
48
- '.ini',
49
- '.java',
50
- '.jsonl',
51
- '.jsx',
52
- '.kt',
53
- '.less',
54
- '.lock',
55
- '.log',
56
- '.lua',
57
- '.m',
58
- '.md',
59
- '.mjs',
60
- '.cjs',
61
- '.mts',
62
- '.patch',
63
- '.php',
64
- '.pl',
65
- '.properties',
66
- '.py',
67
- '.r',
68
- '.rb',
69
- '.rs',
70
- '.scss',
71
- '.sh',
72
- '.sql',
73
- '.swift',
74
- '.tf',
75
- '.toml',
76
- '.ts',
77
- '.tsx',
78
- '.txt',
79
- '.vue',
80
- '.xml',
81
- '.yaml',
82
- '.yml',
83
- '.zsh',
84
- ]);
28
+ /** Enough data to distinguish source and prose from arbitrary binary bytes
29
+ * without reading a large artifact into the daemon's memory. */
30
+ export const FILE_TYPE_SAMPLE_BYTES = 64 * 1024;
85
31
  /** Extensions carrying their own type, where guessing text would be wrong. */
86
32
  const BINARY_CONTENT_TYPES = new Map([
87
33
  ['.apng', 'image/apng'],
@@ -104,23 +50,55 @@ const BINARY_CONTENT_TYPES = new Map([
104
50
  ['.zip', 'application/zip'],
105
51
  ]);
106
52
  /**
107
- * How one browsed file should be served back.
108
- *
109
- * A name with no extension at all — `README`, `Makefile`, `Dockerfile` — is
110
- * nearly always text, and is the one guess worth making. Everything unrecognised
111
- * is served as bytes: the Worker sends it with `nosniff` under a sandboxing
112
- * policy, so an unhelpful type is only ever unhelpful.
53
+ * Whether a bounded sample can safely be decoded in the browser's text editor.
54
+ * NUL and uncommon ASCII control bytes are strong binary signals; strict UTF-8
55
+ * keeps arbitrary binary from being rendered as replacement-character noise.
56
+ * An incomplete final code point is allowed only when the sample is a prefix.
113
57
  */
114
- export function browseContentType(file) {
58
+ export function fileSampleIsText(sample, complete) {
59
+ let controls = 0;
60
+ for (const byte of sample) {
61
+ if (byte === 0)
62
+ return false;
63
+ if (byte < 0x20 && byte !== 0x09 && byte !== 0x0a && byte !== 0x0c && byte !== 0x0d && byte !== 0x1b) {
64
+ controls += 1;
65
+ }
66
+ }
67
+ if (controls / Math.max(1, sample.byteLength) > 0.01)
68
+ return false;
69
+ try {
70
+ new TextDecoder('utf-8', { fatal: true }).decode(sample, { stream: !complete });
71
+ return true;
72
+ }
73
+ catch {
74
+ return false;
75
+ }
76
+ }
77
+ /** How one browsed file should be served back. Explicit media containers keep
78
+ * their MIME type; source, prose, and unknown extensions are decided by bytes.
79
+ * The extension only refines textual JSON after the sample has passed. */
80
+ export function browseContentType(file, sample, complete) {
115
81
  const extension = path.extname(file).toLowerCase();
116
82
  const known = BINARY_CONTENT_TYPES.get(extension);
117
83
  if (known)
118
84
  return known;
119
- if (extension === '.json')
120
- return 'application/json';
121
- if (!extension || TEXT_EXTENSIONS.has(extension))
122
- return 'text/plain; charset=utf-8';
123
- return 'application/octet-stream';
85
+ if (!fileSampleIsText(sample, complete))
86
+ return 'application/octet-stream';
87
+ return extension === '.json' ? 'application/json' : 'text/plain; charset=utf-8';
88
+ }
89
+ function readFileTypeSample(file, bytes) {
90
+ const length = Math.min(bytes, FILE_TYPE_SAMPLE_BYTES);
91
+ if (!length)
92
+ return new Uint8Array();
93
+ const sample = Buffer.allocUnsafe(length);
94
+ const handle = fs.openSync(file, 'r');
95
+ try {
96
+ const read = fs.readSync(handle, sample, 0, length, 0);
97
+ return sample.subarray(0, read);
98
+ }
99
+ finally {
100
+ fs.closeSync(handle);
101
+ }
124
102
  }
125
103
  function isoTime(value) {
126
104
  return Number.isFinite(value) ? new Date(value).toISOString() : '';
@@ -193,39 +171,42 @@ export function readDirectory(directory) {
193
171
  export function directoryIndexPath(directory) {
194
172
  return `${String(directory || '').replace(/\/+$/u, '')}/${DIRECTORY_INDEX_NAME}`;
195
173
  }
196
- /**
197
- * Store one path for a reader, and say what was stored.
198
- *
199
- * An empty file is a legitimate thing to open and has no bytes to upload, so it
200
- * is reported rather than sent — the reader is told the file is empty instead of
201
- * being handed a URL for an object that was never written. A file past the
202
- * upload ceiling is refused here, where the size is known, rather than by the
203
- * Worker after the bytes have crossed the network.
204
- */
205
174
  export async function browseMachinePath({ requested, machine, baseUrl, options, workingDirectory = '', fetchImpl = fetch, }) {
206
175
  const resolved = resolveBrowsePath(requested, {
207
176
  workingDirectory,
208
177
  allowedDirectories: options.allowedDirectories || [],
209
178
  });
210
179
  if (resolved.stat.isDirectory()) {
211
- const body = new TextEncoder().encode(JSON.stringify(readDirectory(resolved.path)));
180
+ const listing = readDirectory(resolved.path);
181
+ const body = new TextEncoder().encode(JSON.stringify(listing));
212
182
  if (body.byteLength > MAX_BROWSE_FILE_BYTES)
213
183
  throw new Error(`Directory listing is too large: ${resolved.path}`);
214
- await uploadFileBytes({
184
+ const etag = crypto.createHash('sha256').update(body).digest('hex');
185
+ const cache = uploadFileBytes({
215
186
  baseUrl,
216
187
  options,
217
188
  machine,
218
189
  absolutePath: directoryIndexPath(resolved.path),
219
190
  contentType: 'application/json',
191
+ contentEtag: etag,
220
192
  body,
221
193
  fetchImpl,
222
194
  });
223
- return { path: resolved.path, kind: 'directory', bytes: body.byteLength, contentType: 'application/json' };
195
+ return {
196
+ path: resolved.path,
197
+ kind: 'directory',
198
+ bytes: body.byteLength,
199
+ contentType: 'application/json',
200
+ listing,
201
+ etag,
202
+ cache,
203
+ };
224
204
  }
225
- const contentType = browseContentType(resolved.path);
226
205
  if (resolved.stat.size > MAX_BROWSE_FILE_BYTES) {
227
206
  throw new Error(`File is larger than the ${BROWSE_FILE_LIMIT_LABEL} browse limit.`);
228
207
  }
208
+ const sample = readFileTypeSample(resolved.path, resolved.stat.size);
209
+ const contentType = browseContentType(resolved.path, sample, sample.byteLength === resolved.stat.size);
229
210
  if (resolved.stat.size === 0)
230
211
  return { path: resolved.path, kind: 'file', bytes: 0, contentType };
231
212
  await uploadFileStream({
@@ -144,6 +144,17 @@ export const ACP_PROFILES = {
144
144
  mcp: true,
145
145
  },
146
146
  vibe: { command: 'vibe-acp', args: [], mcp: true },
147
+ fx: {
148
+ command: 'fx',
149
+ args: ['acp'],
150
+ // Fx reads the process-wide policy when the ACP server starts. Setting a
151
+ // session mode afterwards would replace `yolo` with code mode's `auto`, so
152
+ // bypass deliberately has no session/set_mode call.
153
+ env: (permissionMode) => ({ FX_PERMISSION_MODE: permissionMode === 'bypassPermissions' ? 'yolo' : 'ask' }),
154
+ mode: (permissionMode) => (permissionMode === 'bypassPermissions' ? undefined : 'ask'),
155
+ modelOption: true,
156
+ mcp: true,
157
+ },
147
158
  };
148
159
  // These harnesses keep one bidirectional process attached to the anchor. They
149
160
  // are listed separately from process harnesses so queued turns cannot
@@ -87,6 +87,10 @@ const EXTRA_FILE_STORES = {
87
87
  root: path.join(HOME, '.local', 'state', 'poolside', 'trajectories'),
88
88
  include: (file) => /^trajectory-[^/]+\.ndjson$/i.test(path.basename(file)),
89
89
  },
90
+ fx: {
91
+ root: path.join(HOME, '.fx', 'sessions'),
92
+ include: (file) => path.basename(file) === 'events.jsonl',
93
+ },
90
94
  };
91
95
  function safeStat(file) {
92
96
  try {
@@ -248,6 +252,7 @@ function nativeSessionIdFromPath(harness, sourcePath) {
248
252
  qwen: /\/chats\/([^/]+)\.jsonl$/iu,
249
253
  cline: /\/([^/]+)\.messages\.json$/iu,
250
254
  poolside: /trajectory-(?:standalone_)?([0-9a-f-]{20,})\.ndjson$/iu,
255
+ fx: /\/sessions\/([^/]+)\/events\.jsonl$/iu,
251
256
  };
252
257
  return patterns[harness]?.exec(normalized)?.[1] || '';
253
258
  }
@@ -269,6 +274,28 @@ function fileDescriptor(harness, file) {
269
274
  createdAt: (stat.birthtimeMs > 0 ? stat.birthtime : stat.mtime).toISOString(),
270
275
  };
271
276
  }
277
+ /** Fx's ledger path identifies the session; its adjacent projections provide
278
+ * the workspace and display title without requiring a conversation scan. */
279
+ function fxDescriptor(file) {
280
+ const descriptor = fileDescriptor('fx', file);
281
+ if (!descriptor)
282
+ return null;
283
+ const directory = path.dirname(file);
284
+ const session = jsonFile(path.join(directory, 'session.json'));
285
+ const display = jsonFile(path.join(directory, 'display.json'));
286
+ const createdAtMs = Number(session?.created_at_ms);
287
+ const createdAt = Number.isFinite(createdAtMs) ? new Date(createdAtMs).toISOString() : descriptor.createdAt;
288
+ const title = typeof display?.title === 'string' ? display.title.trim() : '';
289
+ return {
290
+ ...descriptor,
291
+ createdAt,
292
+ nativeSessionId: String(session?.id || path.basename(directory)).trim(),
293
+ ...(absolutePathCandidate([session?.workspace_root, session?.origin_workspace_root])
294
+ ? { workingDirectory: absolutePathCandidate([session?.workspace_root, session?.origin_workspace_root]) }
295
+ : {}),
296
+ ...(title ? { nativeTitle: title } : {}),
297
+ };
298
+ }
272
299
  /** Discover top-level and subagent DeepSeek trajectories with header metadata. */
273
300
  export function deepseekTrajectoryDescriptors(root = DEEPSEEK_SESSIONS_ROOT) {
274
301
  const descriptors = [];
@@ -1070,6 +1097,10 @@ export function nativeDescriptorWorkingDirectory(descriptor) {
1070
1097
  cursor: cursorMetadataWorkingDirectory,
1071
1098
  gemini: geminiMetadataWorkingDirectory,
1072
1099
  kimi: kimiMetadataWorkingDirectory,
1100
+ fx: (candidate) => absolutePathCandidate([
1101
+ sidecarJson(candidate, 'session.json')?.workspace_root,
1102
+ sidecarJson(candidate, 'session.json')?.origin_workspace_root,
1103
+ ]),
1073
1104
  };
1074
1105
  const metadataCandidate = metadataResolvers[descriptor.harness]?.(descriptor) || '';
1075
1106
  if (metadataCandidate)
@@ -1287,6 +1318,7 @@ const HARNESS_PROCESS_NAMES = {
1287
1318
  prime: ['prime', 'prime-agent'],
1288
1319
  qwen: ['qwen'],
1289
1320
  vibe: ['vibe', 'vibe-acp'],
1321
+ fx: ['fx'],
1290
1322
  };
1291
1323
  const SCRIPT_RUNTIMES = new Set(['node', 'nodejs', 'bun', 'deno', 'python', 'python3']);
1292
1324
  function executableStem(value) {
@@ -1392,7 +1424,7 @@ function discoverLocalSessionInventory(only = '') {
1392
1424
  walk(store.root, (file) => {
1393
1425
  if (!store.include(file))
1394
1426
  return;
1395
- const descriptor = fileDescriptor(harness, file);
1427
+ const descriptor = harness === 'fx' ? fxDescriptor(file) : fileDescriptor(harness, file);
1396
1428
  if (descriptor)
1397
1429
  descriptors.push(descriptor);
1398
1430
  });
@@ -173,7 +173,7 @@ function uploadHeaders(options) {
173
173
  * addressed by machine and path alone — the session that happened to prompt the
174
174
  * upload is not part of what was stored, and nothing records it.
175
175
  */
176
- export async function uploadFileBytes({ baseUrl, options, machine, absolutePath, contentType, body, fetchImpl = fetch, }) {
176
+ export async function uploadFileBytes({ baseUrl, options, machine, absolutePath, contentType, contentEtag, body, fetchImpl = fetch, }) {
177
177
  const url = new URL(`/api/file/${encodeURIComponent(machine)}/${encodeURIComponent(absolutePath)}`, String(baseUrl));
178
178
  const response = await fetchImpl(url, {
179
179
  method: 'PUT',
@@ -181,6 +181,7 @@ export async function uploadFileBytes({ baseUrl, options, machine, absolutePath,
181
181
  ...uploadHeaders(options),
182
182
  'content-type': contentType,
183
183
  'content-length': String(body.byteLength),
184
+ ...(contentEtag ? { 'x-turns-content-etag': contentEtag } : {}),
184
185
  },
185
186
  body,
186
187
  });
@@ -952,22 +952,24 @@ export async function watchQueuedPrompts({ rpc, options, machine, getDescriptors
952
952
  return recorded || path.dirname(descriptor.file);
953
953
  };
954
954
  /**
955
- * Store one path this machine holds, so a reader can open it.
955
+ * Read one path this machine holds, so a reader can open it.
956
956
  *
957
957
  * Unlike the rest of these handlers this touches no harness: browsing is a
958
958
  * property of the machine, so it answers for a stopped session as readily as
959
- * for a running one.
959
+ * for a running one. Directory bytes answer directly; their offline copy is
960
+ * stored behind that reply.
960
961
  */
961
962
  const handleFileBrowse = async (message, replyRpc) => {
962
963
  const sessionId = String(message.sessionId || '');
963
964
  try {
964
- const stored = await browseMachinePath({
965
+ const browsed = await browseMachinePath({
965
966
  requested: String(message.path || ''),
966
967
  machine,
967
968
  baseUrl,
968
969
  options,
969
970
  workingDirectory: await browseWorkingDirectory(sessionId),
970
971
  });
972
+ const { cache, ...stored } = browsed.kind === 'directory' ? browsed : { ...browsed, cache: undefined };
971
973
  replyRpc.notify({
972
974
  type: 'session.file.browse.result',
973
975
  requestId: message.requestId,
@@ -975,6 +977,11 @@ export async function watchQueuedPrompts({ rpc, options, machine, getDescriptors
975
977
  ok: true,
976
978
  ...stored,
977
979
  });
980
+ // The direct listing is already useful. Its R2 copy is only the fallback
981
+ // for a later offline browse, so storage latency must not hold this reply.
982
+ void cache?.catch((error) => {
983
+ console.warn(`Directory browse fallback upload failed: ${redactor.redactText(error?.message || String(error))}`);
984
+ });
978
985
  }
979
986
  catch (error) {
980
987
  replyRpc.notify({
@@ -53,6 +53,7 @@ const TIME_FIELDS = [
53
53
  'time_created',
54
54
  'recorded_at',
55
55
  'occurred_at_ms',
56
+ 'timestamp_ms',
56
57
  ];
57
58
  const TIME_CONTAINERS = ['data', 'payload', 'message', 'chat_message', 'node'];
58
59
  function epochToIso(value) {
@@ -845,9 +846,11 @@ async function uploadFile(rpc, machine, root, capture, redactor = DEFAULT_REDACT
845
846
  if (opened.type === 'session_tombstoned')
846
847
  return { rows: 0, sent: 0, skipped: true, tombstoned: true };
847
848
  let nextRow = Number(opened.nextRow) || 0;
849
+ let initialUpload = nextRow === 0;
848
850
  if (Number(opened.redactionVersion || 0) !== REDACTION_VERSION) {
849
851
  await rpc.request({ type: 'session.reset', sessionId, redactionVersion: REDACTION_VERSION });
850
852
  nextRow = 0;
853
+ initialUpload = true;
851
854
  }
852
855
  if (nextRow > rows.length)
853
856
  throw new Error(`${capture.relative}: remote has ${nextRow} rows, local file has ${rows.length}`);
@@ -857,7 +860,12 @@ async function uploadFile(rpc, machine, root, capture, redactor = DEFAULT_REDACT
857
860
  for (const batch of batches(rows, raw, nextRow, redactor)) {
858
861
  let acknowledgement;
859
862
  try {
860
- acknowledgement = await rpc.request({ type: 'ingest', sessionId, ...batch });
863
+ acknowledgement = await rpc.request({
864
+ type: 'ingest',
865
+ sessionId,
866
+ ...batch,
867
+ ...(initialUpload ? { deferReduction: true } : {}),
868
+ });
861
869
  }
862
870
  catch (error) {
863
871
  const offset = /^ROW_OFFSET:(\d+)$/u.exec(String(error.code || ''));
@@ -946,12 +954,16 @@ export async function uploadNativeSession(rpc, machine, descriptor, cursor = {},
946
954
  }
947
955
  const uploadSessionId = String(opened.sessionId || sessionId);
948
956
  let nextRow = Number(opened.nextRow) || 0;
957
+ let initialUpload = nextRow === 0 || cursor.initialUpload === true;
949
958
  if (Number(opened.redactionVersion || 0) !== REDACTION_VERSION) {
950
959
  await rpc.request({ type: 'session.reset', sessionId: uploadSessionId, redactionVersion: REDACTION_VERSION });
951
960
  nextRow = 0;
952
961
  for (const key of Object.keys(cursor))
953
962
  delete cursor[key];
963
+ initialUpload = true;
954
964
  }
965
+ if (initialUpload)
966
+ cursor.initialUpload = true;
955
967
  const resumable = canResumeNativeCursor(descriptor, cursor, nextRow);
956
968
  const scanCursor = resumable
957
969
  ? { offset: Number(cursor.offset), rowCount: Number(cursor.rowCount), lineNumber: Number(cursor.lineNumber) || 0 }
@@ -981,6 +993,7 @@ export async function uploadNativeSession(rpc, machine, descriptor, cursor = {},
981
993
  sessionId: uploadSessionId,
982
994
  startRow: rows[0].index,
983
995
  rows,
996
+ ...(initialUpload ? { deferReduction: true } : {}),
984
997
  });
985
998
  nextRow = Number(acknowledgement.nextRow);
986
999
  sent += rows.length;
@@ -1072,8 +1085,10 @@ export async function uploadNativeSession(rpc, machine, descriptor, cursor = {},
1072
1085
  });
1073
1086
  }
1074
1087
  if (!sawRows && scanCursor.rowCount === 0) {
1088
+ delete cursor.initialUpload;
1075
1089
  return { rows: 0, sent: 0, skipped: true, empty: true };
1076
1090
  }
1091
+ delete cursor.initialUpload;
1077
1092
  return { rows: scanCursor.rowCount, sent, skipped: sent === 0 };
1078
1093
  }
1079
1094
  export function nativeFailureRetryDelay(attempts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turns/dev",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Upload local AI agent sessions to turns.dev",
5
5
  "license": "MIT",
6
6
  "type": "module",