fascicle 0.17.0 → 0.17.1
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/CHANGELOG.md +8 -0
- package/dist/AGENTS.md +7 -7
- package/dist/acp.d.ts +1 -1
- package/dist/acp.js +1 -1
- package/dist/agents.d.ts +1 -1
- package/dist/{engine-BMdRqWL2.js → engine-Bde18P7H.js} +176 -19
- package/dist/engine-Bde18P7H.js.map +1 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp.d.ts +1 -1
- package/dist/{search-CpCJYMUm.js → search-DjhdMsJ8.js} +4 -1
- package/dist/search-DjhdMsJ8.js.map +1 -0
- package/dist/search_worker.js +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +1 -1
- package/dist/{types-BNgAUQP1.d.ts → types-C7pO0jLq.d.ts} +51 -7
- package/dist/viewer.d.ts +27 -0
- package/dist/viewer.js +164 -18
- package/dist/viewer.js.map +1 -1
- package/package.json +1 -1
- package/dist/engine-BMdRqWL2.js.map +0 -1
- package/dist/search-CpCJYMUm.js.map +0 -1
package/dist/mcp.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { c as Step, h as TrajectoryLogger } from "./types-CPqLZOEr.js";
|
|
2
2
|
import { r as ToolSchema } from "./types-BK0UW28o.js";
|
|
3
|
-
import { M as Tool } from "./types-
|
|
3
|
+
import { M as Tool } from "./types-C7pO0jLq.js";
|
|
4
4
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
5
5
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
6
|
import { z } from "zod";
|
|
@@ -286,6 +286,7 @@ function read_file_tool(session, read_limit_lines) {
|
|
|
286
286
|
required: ["path"],
|
|
287
287
|
additionalProperties: false
|
|
288
288
|
}),
|
|
289
|
+
read_only: true,
|
|
289
290
|
execute: async (input) => {
|
|
290
291
|
const request = read_request(input, read_limit_lines);
|
|
291
292
|
const { real } = await contain(session, request.path);
|
|
@@ -386,6 +387,7 @@ function list_files_tool(session, ignore, limit) {
|
|
|
386
387
|
},
|
|
387
388
|
additionalProperties: false
|
|
388
389
|
}),
|
|
390
|
+
read_only: true,
|
|
389
391
|
execute: async (input, ctx) => {
|
|
390
392
|
const request = list_request(input);
|
|
391
393
|
const files = await matching_files(session, request, ignored, ctx.abort);
|
|
@@ -603,6 +605,7 @@ function search_tool(session, ignore, budget_ms) {
|
|
|
603
605
|
required: ["pattern"],
|
|
604
606
|
additionalProperties: false
|
|
605
607
|
}),
|
|
608
|
+
read_only: true,
|
|
606
609
|
execute: async (input, ctx) => {
|
|
607
610
|
const request = search_request(input);
|
|
608
611
|
const base = await contain(session, request.path);
|
|
@@ -623,4 +626,4 @@ function search_tool(session, ignore, budget_ms) {
|
|
|
623
626
|
//#endregion
|
|
624
627
|
export { read_file_tool as a, resolve_roots as c, is_binary as i, shown_path as l, search_tool as n, contain_writable as o, list_files_tool as r, error_code as s, search_files as t };
|
|
625
628
|
|
|
626
|
-
//# sourceMappingURL=search-
|
|
629
|
+
//# sourceMappingURL=search-DjhdMsJ8.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search-DjhdMsJ8.js","names":["LINE_WIDTH","ALL_FILES"],"sources":["../src/acp/tools/containment.ts","../src/acp/tools/read_file.ts","../src/acp/tools/list_files.ts","../src/acp/tools/search.ts"],"sourcesContent":["/**\n * Containment: the boundary the workspace file tools keep. A path, absolute\n * or relative to the session's `cwd`, is allowed only when its real path\n * lands under one of the session's roots, which are real paths themselves,\n * resolved once at `session/new`.\n *\n * Real paths on both sides are the point. A symlink inside the workspace\n * that points out of it is the classic escape, and checking the path as\n * written would let it through. A `cwd` behind a symlink (macOS's `/tmp` and\n * `/var` both are) would refuse every file in the project if the roots\n * stayed as the client sent them. A path that doesn't exist yet resolves\n * through its nearest existing ancestor, and a dangling symlink through its\n * target, so a path outside the roots is refused the same way whether or\n * not it exists.\n *\n * The writes keep a stricter boundary inside the roots: nothing under a\n * `.git` or a `node_modules`. Git runs what its directory holds (a hook,\n * `core.fsmonitor`), a package manager runs what `node_modules` holds\n * (`.bin`, an install script), and a change in either never shows in\n * `git diff`, so an edit there would be code that runs unseen. No mode lifts\n * it, since Accept edits and Auto write without asking.\n */\n\nimport { readlink, realpath, stat } from 'node:fs/promises'\nimport { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'\nimport type { AcpSession } from '../types.js'\n\n/** A path a tool may touch, both as the client names it and as the file system resolves it. */\nexport type ContainedPath = {\n /** Absolute, under the `cwd` the client sent: what the client and the model are shown. */\n readonly path: string\n /** Symlinks resolved: what the tool opens. */\n readonly real: string\n}\n\n/** The error codes that mean a path, or a directory on the way to it, doesn't exist. */\nconst MISSING: ReadonlySet<unknown> = new Set(['ENOENT', 'ENOTDIR'])\n\n/**\n * How many symlinks a missing path may pass through before resolving it\n * gives up. `path.resolve` folds `missing/..` away where the kernel would\n * fail on it, so a link such as `a -> missing/../a` would otherwise loop.\n */\nconst MAX_LINK_HOPS = 40\n\n/**\n * The names a write never reaches through, wherever under a root they sit:\n * a nested repository's `.git` counts, and so does a worktree's, which is a\n * file pointing git at its real directory.\n */\nconst GUARDED_NAMES: ReadonlySet<string> = new Set(['.git', 'node_modules'])\n\n/**\n * The code a Node file system error carries, such as `ENOENT`. Every\n * caller hands in an error from `node:fs`, which always has one.\n */\nexport function error_code(err: unknown): unknown {\n return Reflect.get(Object(err), 'code')\n}\n\n/**\n * Resolve one root the client sent to its real path, refusing a relative\n * path (which would resolve against wherever the agent was launched), a path\n * that doesn't exist, and a file.\n */\nasync function resolve_root(root: string): Promise<string> {\n if (!isAbsolute(root)) {\n throw new Error(`the workspace root ${root} is a relative path; send session/new with absolute paths`)\n }\n const real = await realpath(root).catch((err: unknown) => {\n throw new Error(\n `the workspace root ${root} can't be opened (${String(error_code(err))}); send session/new with a directory that exists`,\n )\n })\n if (!(await stat(real)).isDirectory()) {\n throw new Error(`the workspace root ${root} is a file; send session/new with a directory`)\n }\n return real\n}\n\n/**\n * Resolve a session's roots (its `cwd`, then each additional directory) to\n * their real paths, once, when the session starts. The error names the\n * first root that isn't an absolute path to an existing directory.\n */\nexport async function resolve_roots(roots: ReadonlyArray<string>): Promise<string[]> {\n return Promise.all(roots.map(resolve_root))\n}\n\n/**\n * Whether `target` is `root` or lies beneath it. Both are compared as they\n * are, so the caller hands in real paths for a containment check.\n */\nfunction is_within(root: string, target: string): boolean {\n return target === root || target.startsWith(root.endsWith(sep) ? root : `${root}${sep}`)\n}\n\n/**\n * A symlink's target as written, or `undefined` when the path isn't a\n * symlink or isn't there.\n */\nasync function link_target(path: string): Promise<string | undefined> {\n return readlink(path).catch(() => undefined)\n}\n\n/**\n * The real path of `path`, existing or not. A path that exists is the file\n * system's own answer. A missing one is its parent's real path plus its\n * name, unless it's a dangling symlink, whose real path is its target's:\n * writing through it would land there.\n */\nasync function real_path(path: string, hops_left: number): Promise<string> {\n try {\n return await realpath(path)\n } catch (err: unknown) {\n if (!MISSING.has(error_code(err))) throw err\n }\n const parent = await real_path(dirname(path), hops_left)\n const target = await link_target(path)\n if (target === undefined) return join(parent, basename(path))\n if (hops_left === 0) throw new Error(`${path} passes through too many symbolic links to resolve`)\n return real_path(resolve(parent, target), hops_left - 1)\n}\n\n/**\n * Check that a path a tool was handed lands inside the session's workspace,\n * and return it both ways. The refusal is an ordinary error naming the roots\n * in the client's terms, since the model reads it as a tool result and can\n * try a path that fits.\n */\nexport async function contain(session: AcpSession, requested: string): Promise<ContainedPath> {\n const path = resolve(session.cwd, requested)\n const real = await real_path(path, MAX_LINK_HOPS)\n if (session.roots.some((root) => is_within(root, real))) return { path, real }\n const allowed = [session.cwd, ...session.additional_directories].join(', ')\n throw new Error(`${requested} is outside the workspace; the file tools only reach paths under ${allowed}`)\n}\n\n/**\n * A path segment as a file system that ignores case, or Windows, which also\n * drops trailing dots and spaces, compares it, so `.GIT` and `.git.` name\n * the same directory as `.git` wherever they do.\n */\nfunction folded(segment: string): string {\n return segment.toLowerCase().replace(/[. ]+$/u, '')\n}\n\n/**\n * The first of GUARDED_NAMES on the way down to `path` from any root it lies\n * under, or `undefined` when there is none.\n */\nfunction guarded_segment(roots: ReadonlyArray<string>, path: string): string | undefined {\n for (const root of roots) {\n if (!is_within(root, path)) continue\n const found = relative(root, path).split(sep).map(folded).find((name) => GUARDED_NAMES.has(name))\n if (found !== undefined) return found\n }\n return undefined\n}\n\n/**\n * Check a path a write was handed the way `contain` does, then refuse one\n * that reaches `.git` or `node_modules` under a root. The path is checked as\n * written, against the roots in the client's terms, and as its symlinks\n * resolve, against the real ones, so a `.git` that is a symlink and a\n * symlink into `.git` are both refused. `tool` names the tool asking, for\n * the refusal, which points the model at `run_command`, the tool that asks\n * before it acts in Manual and in Accept edits.\n */\nexport async function contain_writable(session: AcpSession, requested: string, tool: string): Promise<ContainedPath> {\n const target = await contain(session, requested)\n const roots = [...session.roots, ...[session.cwd, ...session.additional_directories].map((dir) => resolve(dir))]\n const segment = guarded_segment(roots, target.path) ?? guarded_segment(roots, target.real)\n if (segment === undefined) return target\n throw new Error(\n `${requested} reaches ${segment}, where ${tool} never writes, since what's there can run on the user's next command without showing in git diff; if it has to change, use run_command`,\n )\n}\n\n/**\n * A path as a tool shows it to the model: relative to `cwd` when it's under\n * it, and absolute otherwise, as for a file in an additional directory.\n * Either form reads back through `contain`.\n */\nexport function shown_path(session: AcpSession, path: string): string {\n return is_within(session.cwd, path) ? relative(session.cwd, path) || '.' : path\n}\n","/**\n * read_file: read a text file in the workspace with its lines numbered, a\n * window at a time.\n *\n * A window stops at `read_limit_lines` and says where the next one starts,\n * and a line past LINE_WIDTH characters is cut with a marker, because a\n * minified bundle or a source map can be one line of megabytes and nothing\n * else keeps it out of the model's context. A file with a NUL byte in its\n * first 8 KB is refused as binary. The file streams line by line, so a\n * window near the top of a huge file reads only as far as it shows.\n */\n\nimport { open, stat } from 'node:fs/promises'\nimport type { FileHandle } from 'node:fs/promises'\nimport type { Tool } from '#engine'\nimport { as_record, json_schema_to_standard } from '#schema'\nimport type { AcpSession } from '../types.js'\nimport { contain, error_code } from './containment.js'\n\n/** The longest line `read_file` returns whole, the width Claude Code's Read cuts at. */\nconst LINE_WIDTH = 2000\n\n/** How much of a file's head is checked for a NUL byte, the sign of a binary file. */\nconst BINARY_SNIFF_BYTES = 8192\n\n/** One read: the path as the model gave it, and the lines it covers. */\ntype ReadRequest = {\n readonly path: string\n readonly offset: number\n readonly limit: number\n}\n\n/**\n * Whether a value counts lines: a whole number of at least 1.\n */\nfunction is_count(value: unknown): value is number {\n return Number.isInteger(value) && Number(value) >= 1\n}\n\n/**\n * Read the call's window out of its input, with `limit` held to the cap.\n * The tool loop has already checked the types against the schema, but a\n * flow that wraps the tool can call `execute` directly, and the schema's\n * `minimum` is advice to the model rather than something the loop enforces.\n */\nfunction read_request(input: unknown, read_limit_lines: number): ReadRequest {\n const fields: Record<string, unknown> = as_record(input) ?? {}\n const { path, offset = 1, limit = read_limit_lines } = fields\n if (typeof path !== 'string') throw new Error('read_file needs a path, as a string')\n if (!is_count(offset) || !is_count(limit)) {\n throw new Error('read_file counts offset and limit in lines from 1, so each has to be a whole number of at least 1')\n }\n return { path, offset, limit: Math.min(limit, read_limit_lines) }\n}\n\n/**\n * Whether the file's first BINARY_SNIFF_BYTES hold a NUL byte. `search`\n * skips a file this calls binary, so the two tools agree on what is text.\n */\nexport async function is_binary(handle: FileHandle): Promise<boolean> {\n const head = Buffer.alloc(BINARY_SNIFF_BYTES)\n const { bytesRead } = await handle.read(head, 0, BINARY_SNIFF_BYTES, 0)\n return head.subarray(0, bytesRead).includes(0)\n}\n\n/**\n * One line as the model sees it: its number, a tab, and its text, cut at\n * LINE_WIDTH with a marker that says how long it was.\n */\nfunction numbered(line_number: number, line: string): string {\n const text =\n line.length > LINE_WIDTH ? `${line.slice(0, LINE_WIDTH)} [line cut at ${LINE_WIDTH} of ${line.length} characters]` : line\n return `${String(line_number).padStart(6)}\\t${text}`\n}\n\n/**\n * Number the window's lines, reading one line past it to learn whether the\n * file goes on, and say where to start the next read when it does.\n */\nasync function read_lines(handle: FileHandle, request: ReadRequest): Promise<string> {\n const last = request.offset + request.limit - 1\n const shown: string[] = []\n let count = 0\n let more = false\n // The binary check reads at an explicit position, which leaves the\n // handle's own position at the top, where these lines start.\n for await (const line of handle.readLines()) {\n count += 1\n if (count > last) {\n more = true\n break\n }\n if (count >= request.offset) shown.push(numbered(count, line))\n }\n if (count === 0) return `${request.path} is empty`\n if (shown.length === 0) {\n throw new Error(`offset ${request.offset} is past the end of ${request.path}, which has ${count} lines`)\n }\n const body = shown.join('\\n')\n if (!more) return body\n return `${body}\\n\\n[lines ${request.offset} to ${last} shown; the file goes on, so read on with offset ${last + 1}]`\n}\n\n/**\n * Read a contained file's window, refusing what isn't a regular text file.\n * The type is checked before the file opens, since opening a FIFO would\n * wait for a writer that never comes.\n */\nasync function read_contained(real: string, request: ReadRequest): Promise<string> {\n const info = await stat(real).catch((err: unknown) => {\n throw error_code(err) === 'ENOENT' ? new Error(`${request.path} doesn't exist`) : err\n })\n if (info.isDirectory()) throw new Error(`${request.path} is a directory; list_files shows what's in it`)\n if (!info.isFile()) throw new Error(`${request.path} isn't a regular file, so read_file can't read it`)\n const handle = await open(real)\n try {\n if (await is_binary(handle)) {\n throw new Error(`${request.path} is a binary file (it has a NUL byte in its first 8 KB), so read_file won't read it`)\n }\n return await read_lines(handle, request)\n } finally {\n await handle.close()\n }\n}\n\n/**\n * The `read_file` tool for one session: numbered lines from a file inside\n * its workspace, at most `read_limit_lines` per call.\n */\nexport function read_file_tool(session: AcpSession, read_limit_lines: number): Tool {\n return {\n name: 'read_file',\n description:\n `Read a text file in the workspace. Returns its lines numbered from 1, at most ${read_limit_lines} per call; ` +\n `pass offset (the first line to read) and limit to read part of a longer file. ` +\n `Lines over ${LINE_WIDTH} characters are cut. The path is absolute or relative to the workspace root.`,\n input_schema: json_schema_to_standard({\n type: 'object',\n properties: {\n path: { type: 'string', description: 'The file to read, absolute or relative to the workspace root' },\n offset: { type: 'integer', minimum: 1, description: 'The first line to read, counting from 1; default: 1' },\n limit: {\n type: 'integer',\n minimum: 1,\n description: `How many lines to read; default and most: ${read_limit_lines}`,\n },\n },\n required: ['path'],\n additionalProperties: false,\n }),\n read_only: true,\n execute: async (input) => {\n const request = read_request(input, read_limit_lines)\n const { real } = await contain(session, request.path)\n return read_contained(real, request)\n },\n }\n}\n","/**\n * list_files: list the workspace's files that match a glob.\n *\n * It walks the directory itself and matches each file with `path`'s\n * `matchesGlob`, rather than handing the walk to `fs/promises`' `glob`.\n * That `glob` steps through a symlinked directory whenever a pattern names\n * it, passes over its `exclude` callback for a pattern that names its\n * segments, swallows a directory it can't read, and, with an `exclude`\n * callback under Stryker's runner, can drop a whole directory it has already\n * listed. The walk here never follows a symlink and never enters an ignored\n * name, so every file it lists lies under the searched directory, which\n * `contain` has already placed inside a root, and its matches follow the\n * same glob rules as `glob`'s.\n */\n\nimport type { Dirent } from 'node:fs'\nimport { readdir, stat } from 'node:fs/promises'\nimport { isAbsolute, join, matchesGlob } from 'node:path'\nimport type { Tool } from '#engine'\nimport { as_record, json_schema_to_standard } from '#schema'\nimport type { AcpSession } from '../types.js'\nimport { contain, shown_path } from './containment.js'\n\n/** What `list_files` matches when the call names no pattern. */\nconst ALL_FILES = '**/*'\n\n/** One listing: the glob, and the directory it runs in, as the model gave them. */\ntype ListRequest = {\n readonly pattern: string\n readonly path: string\n}\n\n/** What one walk needs besides the directory it's in. */\nexport type Walk = {\n readonly pattern: string\n readonly ignored: ReadonlySet<string>\n readonly abort: AbortSignal\n readonly found: string[]\n}\n\n/**\n * Read the call's pattern and directory out of its input, refusing a\n * pattern that reaches past the directory: it could never match, and the\n * model is better told to pass the directory as `path`.\n */\nfunction list_request(input: unknown): ListRequest {\n const fields: Record<string, unknown> = as_record(input) ?? {}\n const { pattern = ALL_FILES, path = '.' } = fields\n if (typeof pattern !== 'string' || typeof path !== 'string') {\n throw new Error('list_files takes its pattern and path as strings')\n }\n if (isAbsolute(pattern) || pattern.split(/[\\\\/]/).includes('..')) {\n throw new Error(\n `list_files matches its pattern under path, so ${pattern} can't be absolute or climb with ..; pass the directory as path instead`,\n )\n }\n return { pattern, path }\n}\n\n/**\n * Walk one directory, depth first: descend into each subdirectory and keep\n * each regular file that matches, skipping ignored names. A symlink is\n * neither a directory nor a file to a `readdir` entry, so the walk never\n * follows one. `search` walks with it too, so both tools see the same files.\n */\nexport async function walk(directory: string, below: string, work: Walk): Promise<void> {\n work.abort.throwIfAborted()\n const entries: Dirent[] = await readdir(directory, { withFileTypes: true })\n for (const entry of entries) {\n if (work.ignored.has(entry.name)) continue\n const relative = join(below, entry.name)\n if (entry.isDirectory()) await walk(join(directory, entry.name), relative, work)\n else if (entry.isFile() && matchesGlob(relative, work.pattern)) work.found.push(relative)\n }\n}\n\n/**\n * The files under the request's directory that match its pattern, in the\n * model's terms and sorted.\n */\nasync function matching_files(\n session: AcpSession,\n request: ListRequest,\n ignored: ReadonlySet<string>,\n abort: AbortSignal,\n): Promise<string[]> {\n const base = await contain(session, request.path)\n if (!(await stat(base.real)).isDirectory()) {\n throw new Error(`${request.path} is a file; list_files lists a directory, and read_file reads a file`)\n }\n const found: string[] = []\n await walk(base.real, '', { pattern: request.pattern, ignored, abort, found })\n return found.map((relative) => shown_path(session, join(base.path, relative))).toSorted()\n}\n\n/**\n * The files as the model reads them, one to a line, and past `limit` the\n * first `limit` of them with a note giving the total. The walk has already\n * found every match to sort them, so the total costs nothing to count.\n */\nfunction listing(files: ReadonlyArray<string>, limit: number): string {\n if (files.length <= limit) return files.join('\\n')\n const note = `[the first ${limit} of ${files.length} files; narrow pattern or path to see the rest]`\n return [...files.slice(0, limit), '', note].join('\\n')\n}\n\n/**\n * The `list_files` tool for one session: the files under a directory in\n * its workspace that match a glob, skipping the `ignore` names, at most\n * `limit` per call.\n */\nexport function list_files_tool(session: AcpSession, ignore: ReadonlyArray<string>, limit: number): Tool {\n const ignored = new Set(ignore)\n return {\n name: 'list_files',\n description: [\n `List the files in the workspace that match a glob pattern, one per line and at most ${limit} per call; a path under the workspace root is shown relative to it.`,\n `The pattern matches under path (default: the workspace root) and defaults to ${ALL_FILES}.`,\n 'As in a shell, * and ** skip names that start with a dot, so name the dot to list hidden files (.github/**/*).',\n ...(ignore.length === 0 ? [] : [`Skips ${ignore.join(', ')}.`]),\n ].join(' '),\n input_schema: json_schema_to_standard({\n type: 'object',\n properties: {\n pattern: { type: 'string', description: `A glob such as src/**/*.ts; default: ${ALL_FILES}` },\n path: {\n type: 'string',\n description: 'The directory to search, absolute or relative to the workspace root; default: the root',\n },\n },\n additionalProperties: false,\n }),\n read_only: true,\n execute: async (input, ctx) => {\n const request = list_request(input)\n const files = await matching_files(session, request, ignored, ctx.abort)\n return files.length === 0 ? `no files match ${request.pattern} under ${request.path}` : listing(files, limit)\n },\n }\n}\n","/**\n * search: find the lines in the workspace's files that match a regular\n * expression.\n *\n * The pattern is the model's, and JavaScript's `RegExp` backtracks, so a\n * pattern such as `(a+)+$` against one long line can run for minutes. No\n * abort interrupts synchronous code, and one agent process serves every\n * session, so a search on the main thread could leave even `session/cancel`\n * unanswered. Each search runs in a worker thread instead, which the turn's\n * abort and a time budget can terminate mid-match. The worker is a thin\n * shell, `search_worker.ts`: what it runs is `search_files`, here, where\n * the tests call it on the main thread.\n *\n * The search walks the files itself, with `list_files`' walk, rather than\n * handing the work to `rg`, because the module keeps child processes to\n * `run_command`.\n */\n\nimport { open, stat } from 'node:fs/promises'\nimport { extname, join } from 'node:path'\nimport { Worker } from 'node:worker_threads'\nimport type { Tool } from '#engine'\nimport { as_record, json_schema_to_standard } from '#schema'\nimport type { AcpSession } from '../types.js'\nimport { contain, shown_path } from './containment.js'\nimport { walk } from './list_files.js'\nimport { is_binary } from './read_file.js'\n\n/** What `search` matches when the call names no glob. */\nconst ALL_FILES = '**/*'\n\n/** The most matches one call returns. */\nconst MATCH_LIMIT = 100\n\n/**\n * The longest matching line shown whole. A longer one is shown as a window\n * this wide around its match, since a minified bundle is one line of\n * megabytes and every match in it would otherwise carry the whole line.\n */\nconst LINE_WIDTH = 500\n\n/** How much of a long line its window shows before the match. */\nconst LINE_LEAD = 100\n\n/**\n * The search worker, beside this module with its extension: `.ts` in the\n * source tree, where vitest and Stryker run it, and `.js` once built.\n */\nconst SEARCH_WORKER = new URL(`./search_worker${extname(import.meta.url)}`, import.meta.url)\n\n/** One search as the model asked for it. */\ntype SearchRequest = {\n readonly pattern: string\n readonly path: string\n readonly glob: string\n}\n\n/** One search as the worker runs it: plain data, since it crosses to another thread. */\ntype SearchJob = {\n /** The real path of the directory to walk, or of the one file to search. */\n readonly base: string\n readonly directory: boolean\n readonly pattern: string\n /** The glob as the walk matches it, relative to `base`. */\n readonly glob: string\n readonly ignore: ReadonlyArray<string>\n readonly limit: number\n}\n\n/**\n * One matching line: its file relative to the job's base (empty when the\n * base is that file), its number counting from 1, and its text.\n */\ntype SearchMatch = {\n readonly file: string\n readonly line: number\n readonly text: string\n}\n\n/** The matches, and whether the search stopped at its limit with more to find. */\ntype SearchResult = {\n readonly matches: ReadonlyArray<SearchMatch>\n readonly more: boolean\n}\n\n/**\n * Read the call's pattern, path, and glob out of its input. The pattern\n * compiles in the worker, which reports one that isn't a regular expression\n * with the engine's own message.\n */\nfunction search_request(input: unknown): SearchRequest {\n const fields: Record<string, unknown> = as_record(input) ?? {}\n const { pattern, path = '.', glob = ALL_FILES } = fields\n if (typeof pattern !== 'string' || typeof path !== 'string' || typeof glob !== 'string') {\n throw new Error('search needs a pattern, and takes its pattern, path, and glob as strings')\n }\n return { pattern, path, glob }\n}\n\n/**\n * The glob as the walk matches it. Like `rg --glob`, a glob without a slash\n * names files at any depth, so `*.ts` reaches `src/a.ts`, while one with a\n * slash matches the path under the searched directory.\n */\nfunction walk_glob(glob: string): string {\n return glob.includes('/') ? glob : `**/${glob}`\n}\n\n/**\n * The files a job searches, relative to its base: the one file, or every\n * file the walk keeps, sorted so the same search stops at the same match\n * every time.\n */\nasync function files_of(job: SearchJob): Promise<string[]> {\n if (!job.directory) return ['']\n const found: string[] = []\n // Nothing inside the worker cancels the walk: the turn's abort and the\n // budget terminate the whole thread instead.\n const abort = new AbortController().signal\n await walk(job.base, '', { pattern: job.glob, ignored: new Set(job.ignore), abort, found })\n return found.toSorted()\n}\n\n/**\n * A file's text, or nothing for a binary file, which then has no lines to\n * search.\n */\nasync function file_text(path: string): Promise<string> {\n const handle = await open(path)\n try {\n // The binary check reads at an explicit position, which leaves the\n // handle's own position at the top, where readFile starts.\n return (await is_binary(handle)) ? '' : await handle.readFile('utf8')\n } finally {\n await handle.close()\n }\n}\n\n/**\n * A text's lines without their endings, and without the empty one after a\n * final newline, which isn't a line of the file.\n */\nfunction lines_of(text: string): string[] {\n const lines = text.split(/\\r?\\n/)\n if (lines.at(-1) === '') lines.pop()\n return lines\n}\n\n/**\n * A matching line as `search` shows it: whole up to LINE_WIDTH characters,\n * and past that, a window that wide starting LINE_LEAD characters before\n * the match, kept inside the line, with the columns it covers.\n */\nfunction excerpt(line: string, at: number): string {\n if (line.length <= LINE_WIDTH) return line\n const start = Math.max(0, Math.min(at - LINE_LEAD, line.length - LINE_WIDTH))\n return `${line.slice(start, start + LINE_WIDTH)} [columns ${start + 1} to ${start + LINE_WIDTH} of ${line.length}]`\n}\n\n/**\n * Search a job's files, in order, for the lines its pattern matches,\n * stopping at the first match past the limit, which says there are more.\n * The search worker runs this, and the tests call it on the main thread,\n * where Stryker can switch its mutants on.\n */\nexport async function search_files(job: SearchJob): Promise<SearchResult> {\n const regex = new RegExp(job.pattern)\n const matches: SearchMatch[] = []\n for (const file of await files_of(job)) {\n for (const [index, line] of lines_of(await file_text(join(job.base, file))).entries()) {\n const found = regex.exec(line)\n if (found === null) continue\n if (matches.length === job.limit) return { matches, more: true }\n matches.push({ file, line: index + 1, text: excerpt(line, found.index) })\n }\n }\n return { matches, more: false }\n}\n\n/**\n * Run one job in a search worker. The turn's abort and the budget each\n * terminate the thread, mid-match if need be, which nothing could do to a\n * pattern running on the main thread. However the search ends, the abort\n * listener and the budget's timer go with it.\n */\nasync function in_worker(job: SearchJob, budget_ms: number, abort: AbortSignal): Promise<SearchResult> {\n abort.throwIfAborted()\n const worker = new Worker(SEARCH_WORKER, { workerData: job })\n const { promise, resolve, reject } = Promise.withResolvers<SearchResult>()\n const stop = (reason: unknown): void => {\n void worker.terminate()\n reject(reason)\n }\n const on_abort = (): void => {\n stop(abort.reason)\n }\n const over_budget = new Error(\n `search stopped after ${budget_ms / 1000} s without finishing: the pattern may backtrack without end, ` +\n 'as nested quantifiers such as (a+)+ can, or there may be too much to search; ' +\n 'simplify the pattern, or narrow path or glob',\n )\n const budget = setTimeout(stop, budget_ms, over_budget)\n abort.addEventListener('abort', on_abort)\n worker.once('message', resolve)\n worker.once('error', reject)\n try {\n return await promise\n } finally {\n clearTimeout(budget)\n abort.removeEventListener('abort', on_abort)\n }\n}\n\n/**\n * The matches as the model reads them, one `path:line: text` to a line, and\n * a note when the search stopped at its limit.\n */\nfunction report(session: AcpSession, base: string, request: SearchRequest, result: SearchResult): string {\n if (result.matches.length === 0) {\n return `no lines match ${request.pattern} under ${request.path} in files matching ${request.glob}`\n }\n const lines = result.matches.map(\n (match) => `${shown_path(session, join(base, match.file))}:${match.line}: ${match.text}`,\n )\n if (result.more) {\n lines.push('', `[the first ${result.matches.length} matches; narrow the pattern, path, or glob to see the rest]`)\n }\n return lines.join('\\n')\n}\n\n/**\n * The `search` tool for one session: the lines under a path in its\n * workspace that match a regular expression, skipping binary files and the\n * `ignore` names, at most MATCH_LIMIT per call, each search in a worker\n * that the turn's abort and `budget_ms` can terminate.\n */\nexport function search_tool(session: AcpSession, ignore: ReadonlyArray<string>, budget_ms: number): Tool {\n return {\n name: 'search',\n description: [\n `Search the workspace's files for lines that match a regular expression, at most ${MATCH_LIMIT} per call,`,\n 'each shown as path:line: text; a path under the workspace root is shown relative to it.',\n 'The pattern is a JavaScript regular expression matched against one line at a time; write (?i:...) for a part that ignores case.',\n 'path (default: the workspace root) is the directory to search, or a single file.',\n 'glob picks the files: without a slash it matches a file name at any depth (*.ts, *.{ts,tsx}),',\n 'and with one it matches the path under path (src/**/*.ts).',\n 'As in a shell, * and ** skip names that start with a dot, so name the dot to search hidden files (.github/**/*).',\n ignore.length === 0 ? 'Skips binary files.' : `Skips binary files and ${ignore.join(', ')}.`,\n ].join(' '),\n input_schema: json_schema_to_standard({\n type: 'object',\n properties: {\n pattern: { type: 'string', description: 'A JavaScript regular expression, such as function\\\\s+\\\\w+' },\n path: {\n type: 'string',\n description: 'The directory or file to search, absolute or relative to the workspace root; default: the root',\n },\n glob: { type: 'string', description: `Which files to search, such as *.ts or src/**/*.ts; default: ${ALL_FILES}` },\n },\n required: ['pattern'],\n additionalProperties: false,\n }),\n read_only: true,\n execute: async (input, ctx) => {\n const request = search_request(input)\n const base = await contain(session, request.path)\n const info = await stat(base.real)\n if (!info.isDirectory() && !info.isFile()) {\n throw new Error(`${request.path} isn't a directory or a regular file, so search can't read it`)\n }\n const job: SearchJob = {\n base: base.real,\n directory: info.isDirectory(),\n pattern: request.pattern,\n glob: walk_glob(request.glob),\n ignore,\n limit: MATCH_LIMIT,\n }\n return report(session, base.path, request, await in_worker(job, budget_ms, ctx.abort))\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,MAAM,0BAAgC,IAAI,IAAI,CAAC,UAAU,SAAS,CAAC;;;;;;AAOnE,MAAM,gBAAgB;;;;;;AAOtB,MAAM,gCAAqC,IAAI,IAAI,CAAC,QAAQ,cAAc,CAAC;;;;;AAM3E,SAAgB,WAAW,KAAuB;CAChD,OAAO,QAAQ,IAAI,OAAO,GAAG,GAAG,MAAM;AACxC;;;;;;AAOA,eAAe,aAAa,MAA+B;CACzD,IAAI,CAAC,WAAW,IAAI,GAClB,MAAM,IAAI,MAAM,sBAAsB,KAAK,0DAA0D;CAEvG,M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/testing.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { n as CheckpointStore } from "./types-CPqLZOEr.js";
|
|
2
|
-
import { F as ToolCallRecord,
|
|
2
|
+
import { B as UsageTotals, F as ToolCallRecord, G as NativeProviderAdapter, R as TurnRequest, h as GenerateResult, m as GenerateOptions, n as AssistantContentPart, p as FinishReason, u as Engine } from "./types-C7pO0jLq.js";
|
|
3
3
|
//#region src/testing/assert_prefix_stable.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Check that every turn of every call in `requests` sends the turn before it
|
package/dist/testing.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { T as validate_schema, k as aborted_error, w as format_schema_issues } from "./core-BOFcG3tp.js";
|
|
2
|
-
import { n as to_json_schema, t as create_engine } from "./engine-
|
|
2
|
+
import { n as to_json_schema, t as create_engine } from "./engine-Bde18P7H.js";
|
|
3
3
|
import { k as schema_validation_error } from "./leading_system-DmNxhvzL.js";
|
|
4
4
|
//#region src/testing/assert_prefix_stable.ts
|
|
5
5
|
const ONE_AT_A_TIME = "the check reads calls that ran one at a time, so a parallel flow takes one double per branch";
|
|
@@ -176,6 +176,14 @@ type CacheSetting = 'off' | '5m' | '1h';
|
|
|
176
176
|
* See `docs/concepts.md`'s What Gets Recorded.
|
|
177
177
|
*/
|
|
178
178
|
type CaptureLevel = 'off' | 'turns' | 'wire';
|
|
179
|
+
/**
|
|
180
|
+
* What a tool loop does with a turn that reached its output limit (finish
|
|
181
|
+
* reason `'length'`) before it made a tool call: `'stop'` ends the loop with
|
|
182
|
+
* that turn, and `'continue'` sends back the turn's text with a message that
|
|
183
|
+
* says it was cut off, and asks for another turn, which counts toward
|
|
184
|
+
* `max_steps`. See `docs/configuration.md`'s Local-Runtime Tool Reliability.
|
|
185
|
+
*/
|
|
186
|
+
type TruncatedTurnPolicy = 'stop' | 'continue';
|
|
179
187
|
/**
|
|
180
188
|
* One captured payload, as `EngineConfig.redact_capture` receives it before
|
|
181
189
|
* the trajectory does: `'request'` is what a turn's `request_sent` would
|
|
@@ -326,6 +334,13 @@ type StepRecord = {
|
|
|
326
334
|
* here, since the call-level field can only carry one.
|
|
327
335
|
*/
|
|
328
336
|
provider_reported?: Record<string, unknown>;
|
|
337
|
+
/**
|
|
338
|
+
* Present on a turn that reached its output limit before it made a tool
|
|
339
|
+
* call, and that the loop went on past (`truncated_turn_policy:
|
|
340
|
+
* 'continue'`). Its `finish_reason` is `'length'`. A turn the loop ended
|
|
341
|
+
* on carries no such mark, so a call that ends on one says `'length'`.
|
|
342
|
+
*/
|
|
343
|
+
truncated?: true;
|
|
329
344
|
};
|
|
330
345
|
/**
|
|
331
346
|
* Provider-specific data on a content part, keyed by provider name: the AI
|
|
@@ -424,6 +439,14 @@ type Tool<i = unknown, o = unknown> = {
|
|
|
424
439
|
* end the loop. undefined (the default) never ends the loop early.
|
|
425
440
|
*/
|
|
426
441
|
ends_turn?: boolean;
|
|
442
|
+
/**
|
|
443
|
+
* True for a tool whose calls change nothing, such as a file read or a
|
|
444
|
+
* search. A call to it doesn't count toward `max_tool_calls_per_step`,
|
|
445
|
+
* which is there to stop a turn from making calls that depend on each
|
|
446
|
+
* other's effects before the model has seen a result, and a call that
|
|
447
|
+
* changes nothing can't. undefined (the default) counts like any call.
|
|
448
|
+
*/
|
|
449
|
+
read_only?: boolean;
|
|
427
450
|
};
|
|
428
451
|
type ToolApprovalRequest = {
|
|
429
452
|
tool_name: string;
|
|
@@ -637,12 +660,26 @@ type GenerateOptions<t = string> = {
|
|
|
637
660
|
*/
|
|
638
661
|
tool_call_repair_attempts?: number;
|
|
639
662
|
/**
|
|
640
|
-
* Cap on tool calls executed per step.
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
*
|
|
663
|
+
* Cap on tool calls executed per step. A call to a tool flagged
|
|
664
|
+
* `read_only` doesn't count. The first call past the cap, and every call
|
|
665
|
+
* after it, are dropped for that step and surfaced via ToolCallRecord
|
|
666
|
+
* errors and a tool_calls_dropped trajectory event. When the loop goes on,
|
|
667
|
+
* one user message after the step's tool results names them, so the model
|
|
668
|
+
* can make them again. undefined (the default) leaves the count unlimited.
|
|
644
669
|
*/
|
|
645
670
|
max_tool_calls_per_step?: number;
|
|
671
|
+
/**
|
|
672
|
+
* What the loop does with a turn that reached its output limit before it
|
|
673
|
+
* made a tool call. `'stop'` (the default) ends the loop with that turn.
|
|
674
|
+
* `'continue'`, on a call with tools and with a step left, sends back the
|
|
675
|
+
* turn's visible text (its reasoning is left out) and a user message that
|
|
676
|
+
* says the turn was cut off, then asks for another turn; the turn's
|
|
677
|
+
* StepRecord says `truncated: true`, and a turn_truncated trajectory event
|
|
678
|
+
* records it. That makes `max_tokens` a per-turn cap that a thinking model
|
|
679
|
+
* can run into without ending its session. Any other value throws
|
|
680
|
+
* `engine_config_error`.
|
|
681
|
+
*/
|
|
682
|
+
truncated_turn_policy?: TruncatedTurnPolicy;
|
|
646
683
|
on_tool_approval?: ToolApprovalHandler;
|
|
647
684
|
/**
|
|
648
685
|
* Per-turn hook to reshape the messages sent to the model without mutating
|
|
@@ -783,7 +820,7 @@ type AiSdkTelemetrySettings = {
|
|
|
783
820
|
* | --------------------------------------------------------------------- | ----------------------------------------- |
|
|
784
821
|
* | `model` | per-call wins; else default; else model_required_error is thrown |
|
|
785
822
|
* | `provider` | per-call wins; else default; else sole configured provider; else provider_required_error is thrown |
|
|
786
|
-
* | `system`, `effort`, `cache`, `capture`, `max_steps`, `turn_timeout_ms`, `tool_error_policy`, `schema_repair_attempts`, `tool_call_repair_attempts`, `max_tool_calls_per_step` | per-call wins (nullish coalesce) |
|
|
823
|
+
* | `system`, `effort`, `cache`, `capture`, `max_steps`, `turn_timeout_ms`, `tool_error_policy`, `schema_repair_attempts`, `tool_call_repair_attempts`, `max_tool_calls_per_step`, `truncated_turn_policy` | per-call wins (nullish coalesce) |
|
|
787
824
|
* | `temperature`, `max_tokens`, `top_p` | per-call wins (nullish coalesce) |
|
|
788
825
|
* | `retry_policy` | per-call replaces wholesale |
|
|
789
826
|
* | `provider_options` | two-level: per-provider-key shallow merge |
|
|
@@ -815,6 +852,13 @@ type EngineDefaults = {
|
|
|
815
852
|
readonly schema_repair_attempts?: number;
|
|
816
853
|
readonly tool_call_repair_attempts?: number;
|
|
817
854
|
readonly max_tool_calls_per_step?: number;
|
|
855
|
+
/**
|
|
856
|
+
* What every call that sets none does with a turn that reached its output
|
|
857
|
+
* limit before it made a tool call. An unknown value throws
|
|
858
|
+
* `engine_config_error` at `create_engine`. See
|
|
859
|
+
* `GenerateOptions.truncated_turn_policy`.
|
|
860
|
+
*/
|
|
861
|
+
readonly truncated_turn_policy?: TruncatedTurnPolicy;
|
|
818
862
|
readonly provider_options?: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
|
|
819
863
|
/**
|
|
820
864
|
* Opt-in OpenTelemetry for the `ai_sdk` transport (Layer 2). Enabling it
|
|
@@ -873,5 +917,5 @@ type Engine = {
|
|
|
873
917
|
dispose: () => Promise<void>;
|
|
874
918
|
};
|
|
875
919
|
//#endregion
|
|
876
|
-
export { StepTiming as A,
|
|
877
|
-
//# sourceMappingURL=types-
|
|
920
|
+
export { StepTiming as A, UsageTotals as B, ProviderConfigMap as C, RetryFailureKind as D, ResolvedModel as E, ToolCallRecord as F, NativeProviderAdapter as G, AiSdkProviderAdapter as H, ToolExecContext as I, ProviderFactory as J, ProviderAdapter as K, TruncatedTurnPolicy as L, Tool as M, ToolApprovalHandler as N, RetryPolicy as O, ToolApprovalRequest as P, ClaudeCliProviderReported as Q, TurnRequest as R, PricingTable as S, ProviderOptions as T, EffortTranslation as U, UserContentPart as V, ExternalAgentAdapter as W, RawProviderUsage as X, ProviderTransport as Y, default_normalize_usage as Z, Message as _, CacheSetting as a, PrepareStepResult as b, CostBreakdown as c, EngineConfig as d, EngineDefaults as f, GenerateTiming as g, GenerateResult as h, BedrockCredentials as i, StreamChunk as j, StepRecord as k, EffortLevel as l, GenerateOptions as m, AssistantContentPart as n, CaptureLevel as o, FinishReason as p, ProviderCapability as q, BedrockCredentialProvider as r, CapturePayload as s, AiSdkTelemetrySettings as t, Engine as u, PrepareStepContext as v, ProviderInit as w, Pricing as x, PrepareStepHook as y, TurnResult as z };
|
|
921
|
+
//# sourceMappingURL=types-C7pO0jLq.d.ts.map
|
package/dist/viewer.d.ts
CHANGED
|
@@ -15,6 +15,14 @@ type StartViewerOptions = {
|
|
|
15
15
|
readonly buffer?: number;
|
|
16
16
|
/** Whether the server takes `POST /api/ingest`, which `http_logger` posts to; default true. */
|
|
17
17
|
readonly ingest?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Whether the tail and `/api/trajectory` follow a link at the last name of
|
|
20
|
+
* `path`; default true. False reads only a regular file with no other
|
|
21
|
+
* name, opened with `O_NOFOLLOW` and checked on its handle, for a host that
|
|
22
|
+
* checks the path itself before it starts the viewer. A refusal goes to
|
|
23
|
+
* `on_io_error`, and the route serves the ring, as it does for no file.
|
|
24
|
+
*/
|
|
25
|
+
readonly follow_links?: boolean;
|
|
18
26
|
readonly on_parse_error?: (err: unknown, line: string) => void;
|
|
19
27
|
readonly on_io_error?: (err: unknown) => void;
|
|
20
28
|
readonly on_subscriber_error?: (err: unknown) => void;
|
|
@@ -91,6 +99,15 @@ type ViewerHandlerConfig = {
|
|
|
91
99
|
* canvas resolves its assets and its API requests against the page.
|
|
92
100
|
*/
|
|
93
101
|
readonly base_path?: string;
|
|
102
|
+
/**
|
|
103
|
+
* Whether `/api/trajectory` follows a link at the last name of
|
|
104
|
+
* `trajectory_path`; default true. False opens the file with `O_NOFOLLOW`
|
|
105
|
+
* and serves only a regular file with no other name, checked on the handle
|
|
106
|
+
* it opened, so a link swapped in after a host checked the path is never
|
|
107
|
+
* served. Anything else is served as no file, from the ring. Throws on a
|
|
108
|
+
* platform with no `O_NOFOLLOW`.
|
|
109
|
+
*/
|
|
110
|
+
readonly follow_links?: boolean | undefined;
|
|
94
111
|
readonly on_parse_error?: (err: unknown, line: string) => void;
|
|
95
112
|
};
|
|
96
113
|
/**
|
|
@@ -138,6 +155,16 @@ type TailConfig = {
|
|
|
138
155
|
readonly on_event: (event: ParsedTrajectoryEvent) => void;
|
|
139
156
|
readonly on_parse_error?: (err: unknown, line: string) => void;
|
|
140
157
|
readonly on_io_error?: (err: unknown) => void;
|
|
158
|
+
/**
|
|
159
|
+
* Whether a link at the path's last name is followed; default true. False
|
|
160
|
+
* opens the file with `O_NOFOLLOW` and reads only a regular file with no
|
|
161
|
+
* other name, checked on the handle it opened, so a link swapped in after
|
|
162
|
+
* a host checked the path is never read. What it refuses goes to
|
|
163
|
+
* `on_io_error`, and the tail waits for a file it can read, as it does for
|
|
164
|
+
* a missing one. Directories above the file are still followed. Throws on a
|
|
165
|
+
* platform with no `O_NOFOLLOW`.
|
|
166
|
+
*/
|
|
167
|
+
readonly follow_links?: boolean | undefined;
|
|
141
168
|
};
|
|
142
169
|
type Tail = {
|
|
143
170
|
readonly stop: () => void;
|
package/dist/viewer.js
CHANGED
|
@@ -2,7 +2,7 @@ import "./core-BOFcG3tp.js";
|
|
|
2
2
|
import { l as parse_trajectory_event } from "./trajectory-DeURWIhj.js";
|
|
3
3
|
import { open, stat } from "node:fs/promises";
|
|
4
4
|
import { basename, dirname, extname, relative, resolve, sep } from "node:path";
|
|
5
|
-
import { createReadStream, existsSync, statSync, watch } from "node:fs";
|
|
5
|
+
import { constants, createReadStream, existsSync, statSync, watch } from "node:fs";
|
|
6
6
|
import { spawn } from "node:child_process";
|
|
7
7
|
import { pipeline } from "node:stream";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
@@ -57,6 +57,85 @@ function create_broadcaster(config) {
|
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
59
|
//#endregion
|
|
60
|
+
//#region src/viewer/no_follow.ts
|
|
61
|
+
/**
|
|
62
|
+
* Opening the trajectory without following a link, for `follow_links: false`.
|
|
63
|
+
*
|
|
64
|
+
* A host can check the trajectory's path before it starts a viewer, but the
|
|
65
|
+
* check can't hold for long, because the tail and `/api/trajectory` open the
|
|
66
|
+
* path again on every read. A link swapped in after the check would point the
|
|
67
|
+
* viewer at any file the user can read and serve it over HTTP. So with links
|
|
68
|
+
* not followed, every open of the trajectory goes through `open_unfollowed`,
|
|
69
|
+
* which opens it with `O_NOFOLLOW`, where a link at the last name fails rather
|
|
70
|
+
* than being followed, and then checks the handle it opened rather than the
|
|
71
|
+
* path, so what it checked is what gets read.
|
|
72
|
+
*
|
|
73
|
+
* Only the last name is covered. A directory above the file is still followed,
|
|
74
|
+
* since Node has no `openat` to walk a path one directory handle at a time, so
|
|
75
|
+
* a host keeps checking those itself.
|
|
76
|
+
*/
|
|
77
|
+
/**
|
|
78
|
+
* The flags that open the trajectory for reading without following a link at
|
|
79
|
+
* its last name. `O_NONBLOCK` keeps a FIFO planted at the path from holding
|
|
80
|
+
* the open until some writer turns up, and changes nothing for a regular file.
|
|
81
|
+
*
|
|
82
|
+
* Throws where the platform has no `O_NOFOLLOW` (Windows), so that asking not
|
|
83
|
+
* to follow links never quietly follows one.
|
|
84
|
+
*/
|
|
85
|
+
function no_follow_flags() {
|
|
86
|
+
const no_follow = Reflect.get(constants, "O_NOFOLLOW");
|
|
87
|
+
if (typeof no_follow !== "number") throw new Error("follow_links: false needs O_NOFOLLOW, which this platform doesn't have");
|
|
88
|
+
return constants.O_RDONLY | no_follow | constants.O_NONBLOCK;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Why the viewer won't read what it opened, or undefined when it may.
|
|
92
|
+
*
|
|
93
|
+
* Only a regular file with no other name is read. A hard link can stand at the
|
|
94
|
+
* path as well as a symbolic one, and `O_NOFOLLOW` can't tell it from the file
|
|
95
|
+
* it names, so a file with a second name is refused too.
|
|
96
|
+
*/
|
|
97
|
+
function refusal(stats) {
|
|
98
|
+
if (!stats.isFile()) return "it isn't a regular file";
|
|
99
|
+
if (stats.nlink !== 1) return `it's one of ${String(stats.nlink)} hard links to the same file`;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Opens `path` with `flags` (see `no_follow_flags`) and checks the handle it
|
|
103
|
+
* opened. Never throws: whatever it couldn't open or wouldn't keep comes back
|
|
104
|
+
* as the error, with the handle already closed.
|
|
105
|
+
*/
|
|
106
|
+
async function open_unfollowed(path, flags) {
|
|
107
|
+
let handle;
|
|
108
|
+
try {
|
|
109
|
+
handle = await open(path, flags);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
return {
|
|
112
|
+
kind: Reflect.get(Object(error), "code") === "ELOOP" ? "refused" : "failed",
|
|
113
|
+
error
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
let stats;
|
|
117
|
+
try {
|
|
118
|
+
stats = await handle.stat();
|
|
119
|
+
} catch (error) {
|
|
120
|
+
await handle.close();
|
|
121
|
+
return {
|
|
122
|
+
kind: "failed",
|
|
123
|
+
error
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const reason = refusal(stats);
|
|
127
|
+
if (reason === void 0) return {
|
|
128
|
+
kind: "opened",
|
|
129
|
+
handle,
|
|
130
|
+
stats
|
|
131
|
+
};
|
|
132
|
+
await handle.close();
|
|
133
|
+
return {
|
|
134
|
+
kind: "refused",
|
|
135
|
+
error: /* @__PURE__ */ new Error(`follow_links is false, so the viewer won't read ${path}: ${reason}`)
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
60
139
|
//#region src/viewer/server.ts
|
|
61
140
|
/**
|
|
62
141
|
* Tiny HTTP server: 5 API routes, SSE fan-out, NDJSON ingest, plus the
|
|
@@ -79,7 +158,9 @@ function create_broadcaster(config) {
|
|
|
79
158
|
* A server bound to a loopback name answers only requests whose `Host` names
|
|
80
159
|
* loopback too, so a web page can't reach it by rebinding a domain of its own
|
|
81
160
|
* to 127.0.0.1. Ingest is mounted unless the config turns it off, reads only
|
|
82
|
-
* `application/x-ndjson`, and refuses a body past 8 MB.
|
|
161
|
+
* `application/x-ndjson`, and refuses a body past 8 MB. With `follow_links`
|
|
162
|
+
* false, `/api/trajectory` serves the file only through a handle opened
|
|
163
|
+
* without following a link at its last name (see `no_follow.ts`).
|
|
83
164
|
*
|
|
84
165
|
* The routes live in `create_viewer_handler`, which a host can mount under a
|
|
85
166
|
* path of its own on a server of its own. `start_server` is that handler on a
|
|
@@ -146,6 +227,7 @@ const ROUTE_BASE = "http://localhost";
|
|
|
146
227
|
function create_viewer_handler(config) {
|
|
147
228
|
const { broadcaster, app_dir, trajectory_path, ingest = true, on_parse_error } = config;
|
|
148
229
|
const base_path = normalize_base_path(config.base_path);
|
|
230
|
+
const no_follow = config.follow_links === false ? no_follow_flags() : void 0;
|
|
149
231
|
return (req, res) => {
|
|
150
232
|
const method = req.method ?? "GET";
|
|
151
233
|
const target = new URL(req.url ?? "/", ROUTE_BASE);
|
|
@@ -166,7 +248,7 @@ function create_viewer_handler(config) {
|
|
|
166
248
|
return true;
|
|
167
249
|
}
|
|
168
250
|
if (route === "GET /api/trajectory") {
|
|
169
|
-
serve_trajectory(res, broadcaster, trajectory_path);
|
|
251
|
+
serve_trajectory(res, broadcaster, trajectory_path, no_follow);
|
|
170
252
|
return true;
|
|
171
253
|
}
|
|
172
254
|
if (route === "GET /api/events") {
|
|
@@ -358,20 +440,49 @@ function send_not_found(res, route) {
|
|
|
358
440
|
* broadcaster's newest id, the seam the client resumes SSE from; the client
|
|
359
441
|
* still takes the larger of that and its own folded count, so a file that ran
|
|
360
442
|
* ahead of the broadcaster is not re-folded.
|
|
443
|
+
*
|
|
444
|
+
* With `no_follow` set (`follow_links: false`), the file is served through
|
|
445
|
+
* `serve_unfollowed` instead, after the headers and the ring are taken here.
|
|
361
446
|
*/
|
|
362
|
-
function serve_trajectory(res, broadcaster, trajectory_path) {
|
|
447
|
+
function serve_trajectory(res, broadcaster, trajectory_path, no_follow) {
|
|
363
448
|
const ring = broadcaster.snapshot();
|
|
364
449
|
res.statusCode = 200;
|
|
365
450
|
res.setHeader("content-type", "application/x-ndjson; charset=utf-8");
|
|
366
451
|
res.setHeader("cache-control", "no-store");
|
|
367
452
|
res.setHeader(CURSOR_HEADER, String(ring.at(-1)?.id ?? 0));
|
|
453
|
+
if (trajectory_path !== void 0 && no_follow !== void 0) {
|
|
454
|
+
serve_unfollowed(res, trajectory_path, no_follow, ring);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
368
457
|
if (trajectory_path !== void 0) {
|
|
369
458
|
if (stat_file(trajectory_path) !== null) {
|
|
370
459
|
stream_file(res, trajectory_path);
|
|
371
460
|
return;
|
|
372
461
|
}
|
|
373
462
|
}
|
|
374
|
-
res.end(ring
|
|
463
|
+
res.end(ring_ndjson(ring));
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* The ring's events as NDJSON, one per line, which is the whole history a
|
|
467
|
+
* server holds when there's no file it may serve.
|
|
468
|
+
*/
|
|
469
|
+
function ring_ndjson(ring) {
|
|
470
|
+
return ring.map((entry) => `${JSON.stringify(entry.event)}\n`).join("");
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Serves the trajectory from a handle opened without following a link at the
|
|
474
|
+
* path's last name and checked before a byte is read (see `open_unfollowed`),
|
|
475
|
+
* so the file that passed the check is the file that's streamed. A path that
|
|
476
|
+
* can't be opened that way, or that holds anything but a regular file with no
|
|
477
|
+
* other name, is served as no file, from the ring. Never rejects.
|
|
478
|
+
*/
|
|
479
|
+
async function serve_unfollowed(res, path, flags, ring) {
|
|
480
|
+
const opened = await open_unfollowed(path, flags);
|
|
481
|
+
if (opened.kind !== "opened") {
|
|
482
|
+
res.end(ring_ndjson(ring));
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
pipeline(opened.handle.createReadStream(), res, () => {});
|
|
375
486
|
}
|
|
376
487
|
/**
|
|
377
488
|
* Where the compiled canvas can be, relative to this module.
|
|
@@ -679,6 +790,14 @@ function handle_ingest(req, res, broadcaster, on_parse_error) {
|
|
|
679
790
|
* which also covers platforms where fs.watch misses some append
|
|
680
791
|
* notifications: the next successful change event catches the file up
|
|
681
792
|
* regardless.
|
|
793
|
+
*
|
|
794
|
+
* With `follow_links: false`, a pass opens the path first, without following
|
|
795
|
+
* a link at its last name, and takes the inode and size from the handle it
|
|
796
|
+
* opened, then reads through that same handle (see `no_follow.ts`). What it
|
|
797
|
+
* won't read, a link or anything but a regular file with no other name, goes
|
|
798
|
+
* to `on_io_error`, and the tail waits as it does for a missing file. The
|
|
799
|
+
* watches still follow a link, but a watch only starts a pass, and every pass
|
|
800
|
+
* opens the path again without following one, so nothing is read through it.
|
|
682
801
|
*/
|
|
683
802
|
/**
|
|
684
803
|
* True for the errors a stat or an open throws when no file is at the path,
|
|
@@ -748,6 +867,7 @@ function next_name(dir, path) {
|
|
|
748
867
|
*/
|
|
749
868
|
function start_tail(config) {
|
|
750
869
|
const { path, on_event, on_parse_error, on_io_error } = config;
|
|
870
|
+
const no_follow = config.follow_links === false ? no_follow_flags() : void 0;
|
|
751
871
|
let inode = null;
|
|
752
872
|
let offset = 0;
|
|
753
873
|
let buffer = "";
|
|
@@ -814,33 +934,57 @@ function start_tail(config) {
|
|
|
814
934
|
file_watcher = watch$1(path, schedule);
|
|
815
935
|
watch_nearest();
|
|
816
936
|
};
|
|
937
|
+
const forget = () => {
|
|
938
|
+
inode = null;
|
|
939
|
+
await_file();
|
|
940
|
+
};
|
|
941
|
+
const track = (st) => {
|
|
942
|
+
if (st.ino !== inode) adopt(st.ino);
|
|
943
|
+
else if (st.size < offset) {
|
|
944
|
+
offset = 0;
|
|
945
|
+
buffer = "";
|
|
946
|
+
}
|
|
947
|
+
return st.size;
|
|
948
|
+
};
|
|
817
949
|
const locate = async () => {
|
|
818
950
|
let st;
|
|
819
951
|
try {
|
|
820
952
|
st = await stat(path);
|
|
821
953
|
} catch (err) {
|
|
822
|
-
if (is_missing(err))
|
|
823
|
-
inode = null;
|
|
824
|
-
await_file();
|
|
825
|
-
}
|
|
954
|
+
if (is_missing(err)) forget();
|
|
826
955
|
report(err);
|
|
827
956
|
return null;
|
|
828
957
|
}
|
|
829
|
-
|
|
830
|
-
else if (st.size < offset) {
|
|
831
|
-
offset = 0;
|
|
832
|
-
buffer = "";
|
|
833
|
-
}
|
|
834
|
-
return st.size;
|
|
958
|
+
return track(st);
|
|
835
959
|
};
|
|
836
|
-
const
|
|
960
|
+
const open_followed = async () => {
|
|
837
961
|
const size = await locate();
|
|
838
|
-
if (size === null || size === offset) return;
|
|
962
|
+
if (size === null || size === offset) return null;
|
|
839
963
|
const fh = await open(path, "r").catch((err) => {
|
|
840
964
|
report(err);
|
|
841
965
|
return null;
|
|
842
966
|
});
|
|
843
|
-
|
|
967
|
+
return fh === null ? null : {
|
|
968
|
+
fh,
|
|
969
|
+
size
|
|
970
|
+
};
|
|
971
|
+
};
|
|
972
|
+
const open_checked = async (flags) => {
|
|
973
|
+
const opened = await open_unfollowed(path, flags);
|
|
974
|
+
if (opened.kind !== "opened") {
|
|
975
|
+
if (opened.kind === "refused" || is_missing(opened.error)) forget();
|
|
976
|
+
report(opened.error);
|
|
977
|
+
return null;
|
|
978
|
+
}
|
|
979
|
+
return {
|
|
980
|
+
fh: opened.handle,
|
|
981
|
+
size: track(opened.stats)
|
|
982
|
+
};
|
|
983
|
+
};
|
|
984
|
+
const read_more = async () => {
|
|
985
|
+
const opened = await (no_follow === void 0 ? open_followed() : open_checked(no_follow));
|
|
986
|
+
if (opened === null) return;
|
|
987
|
+
const { fh, size } = opened;
|
|
844
988
|
try {
|
|
845
989
|
const length = size - offset;
|
|
846
990
|
const buf = Buffer.alloc(length);
|
|
@@ -927,12 +1071,14 @@ async function start_viewer(options = {}) {
|
|
|
927
1071
|
host,
|
|
928
1072
|
port,
|
|
929
1073
|
ingest: options.ingest,
|
|
1074
|
+
follow_links: options.follow_links,
|
|
930
1075
|
...options.path !== void 0 ? { trajectory_path: options.path } : {},
|
|
931
1076
|
...options.on_parse_error ? { on_parse_error: options.on_parse_error } : {}
|
|
932
1077
|
});
|
|
933
1078
|
let tail = null;
|
|
934
1079
|
if (options.path !== void 0) tail = start_tail({
|
|
935
1080
|
path: options.path,
|
|
1081
|
+
follow_links: options.follow_links,
|
|
936
1082
|
on_event: (event) => {
|
|
937
1083
|
broadcaster.emit(event);
|
|
938
1084
|
},
|