screw-bugs 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +906 -2
- package/bin/screw-bugs.js +29 -0
- package/dist/browser-playwright/annotate.d.ts +9 -0
- package/dist/browser-playwright/annotate.js +86 -0
- package/dist/browser-playwright/capture.d.ts +232 -0
- package/dist/browser-playwright/capture.js +1049 -0
- package/dist/browser-playwright/frames.d.ts +77 -0
- package/dist/browser-playwright/frames.js +191 -0
- package/dist/browser-playwright/index.d.ts +2 -0
- package/dist/browser-playwright/index.js +2 -0
- package/dist/browser-playwright/input.d.ts +44 -0
- package/dist/browser-playwright/input.js +116 -0
- package/dist/browser-playwright/launch.d.ts +21 -0
- package/dist/browser-playwright/launch.js +86 -0
- package/dist/browser-playwright/page.d.ts +387 -0
- package/dist/browser-playwright/page.js +1930 -0
- package/dist/build-info.json +5 -0
- package/dist/cli/app.d.ts +19 -0
- package/dist/cli/app.js +70 -0
- package/dist/cli/build-info.d.ts +52 -0
- package/dist/cli/build-info.js +82 -0
- package/dist/cli/commands.d.ts +14 -0
- package/dist/cli/commands.js +308 -0
- package/dist/cli/config.d.ts +16 -0
- package/dist/cli/config.js +71 -0
- package/dist/cli/doctor.d.ts +19 -0
- package/dist/cli/doctor.js +113 -0
- package/dist/cli/environment.d.ts +20 -0
- package/dist/cli/environment.js +34 -0
- package/dist/cli/exit-codes.d.ts +29 -0
- package/dist/cli/exit-codes.js +28 -0
- package/dist/cli/failure.d.ts +20 -0
- package/dist/cli/failure.js +15 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.js +6 -0
- package/dist/cli/install-method.d.ts +96 -0
- package/dist/cli/install-method.js +148 -0
- package/dist/cli/main.d.ts +1 -0
- package/dist/cli/main.js +10 -0
- package/dist/cli/mcp.d.ts +30 -0
- package/dist/cli/mcp.js +309 -0
- package/dist/cli/node-version.d.ts +3 -0
- package/dist/cli/node-version.js +22 -0
- package/dist/cli/operations.d.ts +127 -0
- package/dist/cli/operations.js +337 -0
- package/dist/cli/package-info.d.ts +22 -0
- package/dist/cli/package-info.js +25 -0
- package/dist/cli/report-server.d.ts +170 -0
- package/dist/cli/report-server.js +387 -0
- package/dist/cli/signals.d.ts +37 -0
- package/dist/cli/signals.js +61 -0
- package/dist/cli/stamp-build.d.ts +1 -0
- package/dist/cli/stamp-build.js +9 -0
- package/dist/cli/update-check.d.ts +140 -0
- package/dist/cli/update-check.js +316 -0
- package/dist/cli/uploads.d.ts +16 -0
- package/dist/cli/uploads.js +80 -0
- package/dist/core/css.d.ts +95 -0
- package/dist/core/css.js +436 -0
- package/dist/core/engine.d.ts +65 -0
- package/dist/core/engine.js +436 -0
- package/dist/core/errors.d.ts +66 -0
- package/dist/core/errors.js +30 -0
- package/dist/core/evidence.d.ts +14 -0
- package/dist/core/evidence.js +127 -0
- package/dist/core/index.d.ts +12 -0
- package/dist/core/index.js +12 -0
- package/dist/core/labels.d.ts +47 -0
- package/dist/core/labels.js +199 -0
- package/dist/core/liveness.d.ts +45 -0
- package/dist/core/liveness.js +64 -0
- package/dist/core/marks.d.ts +63 -0
- package/dist/core/marks.js +410 -0
- package/dist/core/services.d.ts +444 -0
- package/dist/core/services.js +22 -0
- package/dist/core/steps.d.ts +96 -0
- package/dist/core/steps.js +656 -0
- package/dist/core/uploads.d.ts +49 -0
- package/dist/core/uploads.js +133 -0
- package/dist/core/video.d.ts +16 -0
- package/dist/core/video.js +101 -0
- package/dist/core/viewport.d.ts +28 -0
- package/dist/core/viewport.js +50 -0
- package/dist/report/html.d.ts +37 -0
- package/dist/report/html.js +762 -0
- package/dist/report/index.d.ts +5 -0
- package/dist/report/index.js +5 -0
- package/dist/report/markup.d.ts +27 -0
- package/dist/report/markup.js +75 -0
- package/dist/report/pr-summary.d.ts +11 -0
- package/dist/report/pr-summary.js +110 -0
- package/dist/report/sha256.d.ts +9 -0
- package/dist/report/sha256.js +97 -0
- package/dist/report/styles.d.ts +1 -0
- package/dist/report/styles.js +328 -0
- package/dist/report/view.d.ts +141 -0
- package/dist/report/view.js +368 -0
- package/dist/report/walkthrough.d.ts +75 -0
- package/dist/report/walkthrough.js +458 -0
- package/dist/schema/config.d.ts +7 -0
- package/dist/schema/config.js +6 -0
- package/dist/schema/doctor.d.ts +46 -0
- package/dist/schema/doctor.js +23 -0
- package/dist/schema/evidence.d.ts +994 -0
- package/dist/schema/evidence.js +521 -0
- package/dist/schema/findings.d.ts +159 -0
- package/dist/schema/findings.js +80 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.js +7 -0
- package/dist/schema/job.d.ts +1724 -0
- package/dist/schema/job.js +505 -0
- package/dist/schema/ledger.d.ts +951 -0
- package/dist/schema/ledger.js +193 -0
- package/dist/schema/status.d.ts +7 -0
- package/dist/schema/status.js +11 -0
- package/dist/store-local/accounts.d.ts +7 -0
- package/dist/store-local/accounts.js +9 -0
- package/dist/store-local/index.d.ts +3 -0
- package/dist/store-local/index.js +3 -0
- package/dist/store-local/paths.d.ts +10 -0
- package/dist/store-local/paths.js +17 -0
- package/dist/store-local/storage.d.ts +20 -0
- package/dist/store-local/storage.js +170 -0
- package/package.json +73 -4
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { Config, Console, Context, Duration, Effect, Fiber, FileSystem, Layer, Option, Path, Random, Result, Schema, SemVer, Stdio, } from 'effect';
|
|
2
|
+
import { HttpClient, HttpClientResponse, HttpIncomingMessage } from 'effect/http';
|
|
3
|
+
import { buildProblem, RunningBuild } from './build-info.js';
|
|
4
|
+
import { updateCheckEnabled } from './environment.js';
|
|
5
|
+
import { detectInstall, inFolder, updateCommandFor, } from './install-method.js';
|
|
6
|
+
import { PackageInfo } from './package-info.js';
|
|
7
|
+
/** A check is due when the last one that got an answer is this old. */
|
|
8
|
+
export const CHECK_EVERY = Duration.hours(24);
|
|
9
|
+
/** A running MCP server tries again this long after a check that got no answer. */
|
|
10
|
+
export const RETRY_AFTER = Duration.hours(1);
|
|
11
|
+
/** How long a check waits for the registry. */
|
|
12
|
+
export const CHECK_TIMEOUT = Duration.seconds(3);
|
|
13
|
+
/**
|
|
14
|
+
* How long a CLI command waits, after its output, for a check it started because one was due,
|
|
15
|
+
* counted from when the check started.
|
|
16
|
+
*/
|
|
17
|
+
export const CLI_WAIT = Duration.seconds(3);
|
|
18
|
+
/** A running MCP server never waits less than this between checks, whatever the cache says. */
|
|
19
|
+
export const MIN_WAIT = Duration.minutes(1);
|
|
20
|
+
/** The CLI's line about a newer version is printed at most this often. */
|
|
21
|
+
export const NOTICE_EVERY = Duration.hours(24);
|
|
22
|
+
/**
|
|
23
|
+
* What the last checks found, shared by every screw-bugs process of the user (times in ms since
|
|
24
|
+
* the epoch). A check that got no answer only moves `lastAttemptMs`.
|
|
25
|
+
*/
|
|
26
|
+
export const UpdateCache = Schema.Struct({
|
|
27
|
+
lastAttemptMs: Schema.optionalKey(Schema.Number),
|
|
28
|
+
lastSuccessMs: Schema.optionalKey(Schema.Number),
|
|
29
|
+
/** The registry's latest version at the last success; missing when it knows none (404). */
|
|
30
|
+
latest: Schema.optionalKey(Schema.String),
|
|
31
|
+
/** When the CLI last printed its line about a newer version. */
|
|
32
|
+
notifiedMs: Schema.optionalKey(Schema.Number),
|
|
33
|
+
});
|
|
34
|
+
export const CACHE_FILE = 'update-check.json';
|
|
35
|
+
const ms = Duration.toMillis;
|
|
36
|
+
/** Due when no check has got an answer in the last 24 hours: what a process checks on start. */
|
|
37
|
+
export const isDue = (cache, now) => cache.lastSuccessMs === undefined || now - cache.lastSuccessMs >= ms(CHECK_EVERY);
|
|
38
|
+
/** In a running server, a failed check is also not tried again for an hour. */
|
|
39
|
+
export const isDueInServer = (cache, now) => isDue(cache, now) &&
|
|
40
|
+
(cache.lastAttemptMs === undefined || now - cache.lastAttemptMs >= ms(RETRY_AFTER));
|
|
41
|
+
/**
|
|
42
|
+
* How long a running server waits before it checks again: at least `MIN_WAIT`, so that no cache
|
|
43
|
+
* (one that was never written, or holds odd times) can make the server check in a busy loop.
|
|
44
|
+
*/
|
|
45
|
+
export const nextCheckIn = (cache, now) => {
|
|
46
|
+
const at = isDue(cache, now)
|
|
47
|
+
? (cache.lastAttemptMs ?? -Infinity) + ms(RETRY_AFTER)
|
|
48
|
+
: (cache.lastSuccessMs ?? 0) + ms(CHECK_EVERY);
|
|
49
|
+
const wait = at - now;
|
|
50
|
+
return Duration.millis(Number.isFinite(wait) ? Math.max(ms(MIN_WAIT), wait) : ms(MIN_WAIT));
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* The user's cache folder, as each system has it: `$XDG_CACHE_HOME` or `~/.cache` on Linux and
|
|
54
|
+
* the like, `~/Library/Caches` on macOS, `%LOCALAPPDATA%` on Windows. None when there is no home.
|
|
55
|
+
*/
|
|
56
|
+
export const cacheDirFor = (platform) => Effect.gen(function* () {
|
|
57
|
+
const path = yield* Path.Path;
|
|
58
|
+
const env = (name) => Config.String(name).pipe(Config.map((value) => value.trim()), Config.option, Effect.map(Option.filter((value) => value !== '')), Effect.map(Option.getOrUndefined), Effect.orElseSucceed(() => undefined));
|
|
59
|
+
const home = (yield* env('HOME')) ?? (yield* env('USERPROFILE'));
|
|
60
|
+
if (platform === 'win32') {
|
|
61
|
+
const local = yield* env('LOCALAPPDATA');
|
|
62
|
+
return local ?? (home ? path.join(home, 'AppData', 'Local') : undefined);
|
|
63
|
+
}
|
|
64
|
+
if (platform === 'darwin')
|
|
65
|
+
return home ? path.join(home, 'Library', 'Caches') : undefined;
|
|
66
|
+
const xdg = yield* env('XDG_CACHE_HOME');
|
|
67
|
+
// The spec says to ignore a relative XDG_CACHE_HOME.
|
|
68
|
+
if (xdg && path.isAbsolute(xdg))
|
|
69
|
+
return xdg;
|
|
70
|
+
return home ? path.join(home, '.cache') : undefined;
|
|
71
|
+
});
|
|
72
|
+
/** The cache file, `<cache folder>/screw-bugs/update-check.json`. */
|
|
73
|
+
export const cacheFileFor = (platform) => cacheDirFor(platform).pipe(Effect.flatMap((dir) => Path.Path.useSync((path) => (dir ? path.join(dir, 'screw-bugs', CACHE_FILE) : undefined))));
|
|
74
|
+
/** The cache as saved; a missing or broken file is an empty cache. */
|
|
75
|
+
export const readCache = (file) => FileSystem.FileSystem.use((fs) => fs.readFileString(file)).pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.fromJsonString(UpdateCache))), Effect.orElseSucceed(() => ({})));
|
|
76
|
+
/** A temporary file of the cache this old is left from a write that was cut short: removed. */
|
|
77
|
+
export const STALE_TEMPORARY = Duration.hours(1);
|
|
78
|
+
/**
|
|
79
|
+
* Saves the cache through a temporary file and a rename, so a process reading it never sees half
|
|
80
|
+
* a file. Several processes may write at once (each with its own temporary file); the last one
|
|
81
|
+
* wins. Never fails: a cache that cannot be written only means the next start checks again.
|
|
82
|
+
*
|
|
83
|
+
* Interruptible throughout, so a filesystem that stalls (a network home that went away) never
|
|
84
|
+
* holds up the CLI's capped wait, the MCP server's shutdown or the process's exit. A write cut
|
|
85
|
+
* short may leave its temporary file; nothing reads those, and the next write removes any older
|
|
86
|
+
* than `STALE_TEMPORARY`.
|
|
87
|
+
*/
|
|
88
|
+
export const writeCache = (file, cache) => Effect.gen(function* () {
|
|
89
|
+
const fs = yield* FileSystem.FileSystem;
|
|
90
|
+
const path = yield* Path.Path;
|
|
91
|
+
const dir = path.dirname(file);
|
|
92
|
+
yield* fs.makeDirectory(dir, { recursive: true });
|
|
93
|
+
const suffix = (yield* Random.nextIntBetween(0, 2 ** 32 - 1)).toString(16);
|
|
94
|
+
const temporary = `${file}.${suffix}.tmp`;
|
|
95
|
+
yield* fs.writeFileString(temporary, `${JSON.stringify(cache)}\n`);
|
|
96
|
+
yield* fs
|
|
97
|
+
.rename(temporary, file)
|
|
98
|
+
.pipe(Effect.tapError(() => fs.remove(temporary).pipe(Effect.ignore)));
|
|
99
|
+
yield* removeStaleTemporaries(file);
|
|
100
|
+
}).pipe(Effect.ignore);
|
|
101
|
+
/** Removes the temporary files of `file` older than `STALE_TEMPORARY`. Never fails. */
|
|
102
|
+
const removeStaleTemporaries = (file) => Effect.gen(function* () {
|
|
103
|
+
const fs = yield* FileSystem.FileSystem;
|
|
104
|
+
const path = yield* Path.Path;
|
|
105
|
+
const dir = path.dirname(file);
|
|
106
|
+
const prefix = `${path.basename(file)}.`;
|
|
107
|
+
const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis);
|
|
108
|
+
for (const name of yield* fs.readDirectory(dir)) {
|
|
109
|
+
if (!name.startsWith(prefix) || !name.endsWith('.tmp'))
|
|
110
|
+
continue;
|
|
111
|
+
const temporary = path.join(dir, name);
|
|
112
|
+
const info = yield* fs.stat(temporary).pipe(Effect.option);
|
|
113
|
+
const modified = Option.flatMap(info, (found) => found.mtime);
|
|
114
|
+
if (Option.isNone(modified))
|
|
115
|
+
continue;
|
|
116
|
+
if (now - modified.value.getTime() < ms(STALE_TEMPORARY))
|
|
117
|
+
continue;
|
|
118
|
+
yield* fs.remove(temporary).pipe(Effect.ignore);
|
|
119
|
+
}
|
|
120
|
+
}).pipe(Effect.ignore);
|
|
121
|
+
const Latest = Schema.Struct({ version: Schema.String });
|
|
122
|
+
/** The npm registry's URL for the latest version of `name` (a scoped name keeps its `@`). */
|
|
123
|
+
export const registryUrl = (name) => `https://registry.npmjs.org/${name.replace('/', '%2F')}/latest`;
|
|
124
|
+
/**
|
|
125
|
+
* The latest version on npm: `Some(version)`, or `None` when the registry does not know the
|
|
126
|
+
* package (404: not published yet). Fails when there is no answer (offline, timeout, another
|
|
127
|
+
* status, a body that is not a version).
|
|
128
|
+
*/
|
|
129
|
+
export const fetchLatest = (name) => HttpClient.get(registryUrl(name), { acceptJson: true }).pipe(Effect.flatMap((response) => response.status === 404
|
|
130
|
+
? Effect.succeed(Option.none())
|
|
131
|
+
: HttpClientResponse.filterStatusOk(response).pipe(Effect.flatMap(HttpIncomingMessage.schemaBodyJson(Latest)), Effect.map((body) => Option.some(body.version)))), Effect.scoped, Effect.timeout(CHECK_TIMEOUT));
|
|
132
|
+
/** `latest` when it is a newer version than `current`. */
|
|
133
|
+
export const newerThan = (latest, current) => {
|
|
134
|
+
if (latest === undefined)
|
|
135
|
+
return undefined;
|
|
136
|
+
const a = SemVer.parse(latest);
|
|
137
|
+
const b = SemVer.parse(current);
|
|
138
|
+
if (Result.isFailure(a) || Result.isFailure(b))
|
|
139
|
+
return undefined;
|
|
140
|
+
return SemVer.isGreaterThan(a.success, b.success) ? latest : undefined;
|
|
141
|
+
};
|
|
142
|
+
/** The sentence telling the agent a newer version exists and exactly how to get it. */
|
|
143
|
+
export const updateSentence = (latest, current, update, options = {}) => {
|
|
144
|
+
const name = options.name ?? 'screw-bugs';
|
|
145
|
+
const restart = options.restart ?? 'the MCP server';
|
|
146
|
+
const where = update.cwd === undefined ? '' : ` in the folder ${update.cwd}`;
|
|
147
|
+
const how = update.runs === 'config'
|
|
148
|
+
? `make the ${name} MCP server's command \`${update.command}\` in your MCP config (ask the user first)`
|
|
149
|
+
: `run \`${update.command}\`${where} (ask the user first)`;
|
|
150
|
+
return `${name} ${latest} is available (you have ${current}). To update, ${how}, then restart ${restart}.`;
|
|
151
|
+
};
|
|
152
|
+
const RESTART_HOW = 'in Claude Code: run /mcp, pick screw-bugs, then Reconnect';
|
|
153
|
+
/** The doctor check for a build problem (see `buildProblem`). */
|
|
154
|
+
export const checkBuild = (problem, name = 'screw-bugs') => {
|
|
155
|
+
if (problem.kind === 'rebuild') {
|
|
156
|
+
const rebuild = inFolder(problem.root, 'pnpm build');
|
|
157
|
+
const where = rebuild.cwd === undefined ? '' : ` in the folder ${rebuild.cwd}`;
|
|
158
|
+
return {
|
|
159
|
+
id: 'update',
|
|
160
|
+
ok: false,
|
|
161
|
+
severity: 'warning',
|
|
162
|
+
detail: `dist/ was built from commit ${problem.built}, but the checkout in ${problem.root} is at ${problem.head}.`,
|
|
163
|
+
fix: {
|
|
164
|
+
action: `Rebuild screw-bugs${where} (ask the user first), then restart the MCP server.`,
|
|
165
|
+
command: rebuild.command,
|
|
166
|
+
why: 'The CLI and the MCP server run dist/, not the source; ask the user first, then restart the MCP server',
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
id: 'update',
|
|
172
|
+
ok: false,
|
|
173
|
+
severity: 'warning',
|
|
174
|
+
detail: `This ${name} process runs an older build (${problem.loaded}) than the one installed now (${problem.installed}).`,
|
|
175
|
+
fix: {
|
|
176
|
+
action: `Restart the ${name} MCP server (${RESTART_HOW}).`,
|
|
177
|
+
why: 'It keeps running the code it started with until it restarts.',
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
/** The doctor check for a newer version, from what the cache knows. */
|
|
182
|
+
export const checkVersion = (current, cache, install, options) => {
|
|
183
|
+
const name = options.name ?? 'screw-bugs';
|
|
184
|
+
const newer = newerThan(cache.latest, current);
|
|
185
|
+
if (newer === undefined) {
|
|
186
|
+
const known = !options.enabled
|
|
187
|
+
? ' (update check off)'
|
|
188
|
+
: cache.latest !== undefined && cache.lastSuccessMs !== undefined
|
|
189
|
+
? ' (latest)'
|
|
190
|
+
: '';
|
|
191
|
+
return { id: 'update', ok: true, detail: `${name} ${current}${known}` };
|
|
192
|
+
}
|
|
193
|
+
const update = updateCommandFor(install, name);
|
|
194
|
+
return {
|
|
195
|
+
id: 'update',
|
|
196
|
+
ok: false,
|
|
197
|
+
severity: 'warning',
|
|
198
|
+
detail: `${name} ${newer} is available (you have ${current}).`,
|
|
199
|
+
fix: update.runs === 'config'
|
|
200
|
+
? {
|
|
201
|
+
action: `Make the ${name} MCP server's command \`${update.command}\` in your MCP config (ask the user first), then restart the MCP server`,
|
|
202
|
+
why: 'npx keeps running the version it cached until the command asks for the latest.',
|
|
203
|
+
}
|
|
204
|
+
: {
|
|
205
|
+
action: `Update with the command${update.cwd === undefined ? '' : ` in the folder ${update.cwd}`} (ask the user first), then restart the MCP server.`,
|
|
206
|
+
command: update.command,
|
|
207
|
+
why: 'Ask the user first, then restart the MCP server, which runs the old version until then',
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* The check for a newer screw-bugs on npm, once a day, in the background. screw-bugs only says
|
|
213
|
+
* so (doctor, the MCP server's instructions, one CLI line a day); it never installs anything.
|
|
214
|
+
*/
|
|
215
|
+
export class UpdateCheck extends Context.Service()('screw-bugs/UpdateCheck') {
|
|
216
|
+
/** `platform` is the operating system's name (`process.platform`), for the cache folder. */
|
|
217
|
+
static layer = (platform) => Layer.effect(UpdateCheck, Effect.gen(function* () {
|
|
218
|
+
const context = yield* Effect.context();
|
|
219
|
+
const { name, version, root } = yield* PackageInfo;
|
|
220
|
+
const enabled = yield* updateCheckEnabled;
|
|
221
|
+
const file = enabled ? yield* cacheFileFor(platform) : undefined;
|
|
222
|
+
const install = yield* detectInstall(root, name);
|
|
223
|
+
const provide = (effect) => Effect.provide(effect, context);
|
|
224
|
+
const read = file === undefined ? Effect.succeed({}) : provide(readCache(file));
|
|
225
|
+
const checkIfDue = (rule) => Effect.gen(function* () {
|
|
226
|
+
if (file === undefined)
|
|
227
|
+
return {};
|
|
228
|
+
const before = yield* readCache(file);
|
|
229
|
+
const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis);
|
|
230
|
+
if (!(rule === 'start' ? isDue : isDueInServer)(before, now))
|
|
231
|
+
return before;
|
|
232
|
+
const found = yield* fetchLatest(name).pipe(Effect.option);
|
|
233
|
+
// Read again: another process may have written while this one waited.
|
|
234
|
+
const current = yield* readCache(file);
|
|
235
|
+
const next = Option.match(found, {
|
|
236
|
+
onNone: () => ({ ...current, lastAttemptMs: now }),
|
|
237
|
+
onSome: (latest) => {
|
|
238
|
+
const { latest: _old, ...rest } = current;
|
|
239
|
+
return {
|
|
240
|
+
...rest,
|
|
241
|
+
lastAttemptMs: now,
|
|
242
|
+
lastSuccessMs: now,
|
|
243
|
+
...(Option.isSome(latest) ? { latest: latest.value } : {}),
|
|
244
|
+
};
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
yield* writeCache(file, next);
|
|
248
|
+
return next;
|
|
249
|
+
}).pipe(provide);
|
|
250
|
+
const serverLoop = file === undefined
|
|
251
|
+
? Effect.void
|
|
252
|
+
: Effect.gen(function* () {
|
|
253
|
+
let cache = yield* checkIfDue('start');
|
|
254
|
+
while (true) {
|
|
255
|
+
const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis);
|
|
256
|
+
yield* Effect.sleep(nextCheckIn(cache, now));
|
|
257
|
+
cache = yield* checkIfDue('server');
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
// Nothing here touches the filesystem before the fork: a stalled one cannot hold up the
|
|
261
|
+
// command, and the wait is capped however it behaves (all of it is interruptible).
|
|
262
|
+
const startCliCheck = Effect.gen(function* () {
|
|
263
|
+
if (file === undefined)
|
|
264
|
+
return Effect.void;
|
|
265
|
+
// The cap counts from here, so the wait after the command is what is left of it.
|
|
266
|
+
const check = yield* Effect.forkChild(Effect.gen(function* () {
|
|
267
|
+
const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis);
|
|
268
|
+
if (isDue(yield* readCache(file), now))
|
|
269
|
+
yield* checkIfDue('start');
|
|
270
|
+
}).pipe(Effect.timeout(CLI_WAIT), Effect.ignore));
|
|
271
|
+
return Fiber.await(check).pipe(Effect.asVoid);
|
|
272
|
+
}).pipe(provide);
|
|
273
|
+
const newer = read.pipe(Effect.map((cache) => newerThan(cache.latest, version)));
|
|
274
|
+
const notice = newer.pipe(Effect.map((latest) => latest === undefined
|
|
275
|
+
? undefined
|
|
276
|
+
: updateSentence(latest, version, updateCommandFor(install, name), { name })));
|
|
277
|
+
const doctorCheck = Effect.gen(function* () {
|
|
278
|
+
const problem = yield* buildProblem(install);
|
|
279
|
+
if (problem)
|
|
280
|
+
return checkBuild(problem, name);
|
|
281
|
+
return checkVersion(version, yield* read, install, { enabled, name });
|
|
282
|
+
}).pipe(provide);
|
|
283
|
+
const cliNotice = (args) => Effect.gen(function* () {
|
|
284
|
+
if (file === undefined || args.includes('--json') || args[0] === 'mcp')
|
|
285
|
+
return;
|
|
286
|
+
if (!(yield* Stdio.Stdio.use((stdio) => stdio.stderrIsTerminal)))
|
|
287
|
+
return;
|
|
288
|
+
const cache = yield* readCache(file);
|
|
289
|
+
const latest = newerThan(cache.latest, version);
|
|
290
|
+
const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis);
|
|
291
|
+
if (latest === undefined)
|
|
292
|
+
return;
|
|
293
|
+
if (cache.notifiedMs !== undefined && now - cache.notifiedMs < ms(NOTICE_EVERY))
|
|
294
|
+
return;
|
|
295
|
+
yield* Console.error(updateSentence(latest, version, updateCommandFor(install, name), {
|
|
296
|
+
name,
|
|
297
|
+
restart: 'any running MCP server',
|
|
298
|
+
}));
|
|
299
|
+
yield* writeCache(file, { ...cache, notifiedMs: now });
|
|
300
|
+
}).pipe(
|
|
301
|
+
// A stalled filesystem only costs the line (or, once printed, its once-a-day mark).
|
|
302
|
+
Effect.timeout(CLI_WAIT), Effect.ignore, provide);
|
|
303
|
+
return UpdateCheck.of({
|
|
304
|
+
enabled,
|
|
305
|
+
active: file !== undefined,
|
|
306
|
+
install,
|
|
307
|
+
read,
|
|
308
|
+
checkIfDue,
|
|
309
|
+
serverLoop,
|
|
310
|
+
startCliCheck,
|
|
311
|
+
notice,
|
|
312
|
+
doctorCheck,
|
|
313
|
+
cliNotice,
|
|
314
|
+
});
|
|
315
|
+
}));
|
|
316
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Effect, FileSystem, Path } from 'effect';
|
|
2
|
+
import { type FileNeeded, type UploadSource } from '../core/index.js';
|
|
3
|
+
import { type JobSpec } from '../schema/index.js';
|
|
4
|
+
import { Failure } from './failure.js';
|
|
5
|
+
/**
|
|
6
|
+
* Checks every file the job's upload steps give against the project folder: it must be a file
|
|
7
|
+
* inside the project (links resolved), neither empty nor over `UPLOAD_MAX_BYTES`, with content
|
|
8
|
+
* that fits its extension (`contentMatches`) unless the job allows a mismatch for it. Nothing is copied or kept but names, sizes and
|
|
9
|
+
* where the files really are.
|
|
10
|
+
*/
|
|
11
|
+
export declare const checkUploads: (projectRoot: string, spec: JobSpec) => Effect.Effect<{
|
|
12
|
+
sources: UploadSource[];
|
|
13
|
+
needed: FileNeeded[];
|
|
14
|
+
}, never, FileSystem.FileSystem | Path.Path>;
|
|
15
|
+
/** Nothing started: the files a job needs, for the caller's agent to create (`formatFilesNeeded`). */
|
|
16
|
+
export declare const filesNeeded: (projectRoot: string, files: ReadonlyArray<FileNeeded>) => Failure;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Effect, FileSystem, Option, Path } from 'effect';
|
|
2
|
+
import { contentMatches, fileProblemText, formatFilesNeeded, UPLOAD_HEAD_BYTES, UPLOAD_TAIL_BYTES, uploadTypeOf, } from '../core/index.js';
|
|
3
|
+
import { casesOf, UPLOAD_MAX_BYTES, uploadFilesOf } from '../schema/index.js';
|
|
4
|
+
import { ExitCode } from './exit-codes.js';
|
|
5
|
+
import { Failure } from './failure.js';
|
|
6
|
+
/** The first and last bytes of an open file, as `contentMatches` reads them. */
|
|
7
|
+
const sample = (file, size) => Effect.gen(function* () {
|
|
8
|
+
const read = (length, position) => length <= 0
|
|
9
|
+
? Effect.succeed(new Uint8Array())
|
|
10
|
+
: file
|
|
11
|
+
.readAlloc(length, { position: BigInt(position) })
|
|
12
|
+
.pipe(Effect.map(Option.getOrElse(() => new Uint8Array())));
|
|
13
|
+
const head = yield* read(Math.min(size, UPLOAD_HEAD_BYTES), 0);
|
|
14
|
+
const tailLength = Math.min(size, UPLOAD_TAIL_BYTES);
|
|
15
|
+
const tail = yield* read(tailLength, size - tailLength);
|
|
16
|
+
return { head, tail, size };
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* Checks every file the job's upload steps give against the project folder: it must be a file
|
|
20
|
+
* inside the project (links resolved), neither empty nor over `UPLOAD_MAX_BYTES`, with content
|
|
21
|
+
* that fits its extension (`contentMatches`) unless the job allows a mismatch for it. Nothing is copied or kept but names, sizes and
|
|
22
|
+
* where the files really are.
|
|
23
|
+
*/
|
|
24
|
+
export const checkUploads = (projectRoot, spec) => Effect.gen(function* () {
|
|
25
|
+
const fs = yield* FileSystem.FileSystem;
|
|
26
|
+
const path = yield* Path.Path;
|
|
27
|
+
const root = yield* fs.realPath(projectRoot).pipe(Effect.orElseSucceed(() => projectRoot));
|
|
28
|
+
const sources = [];
|
|
29
|
+
const needed = [];
|
|
30
|
+
for (const { path: relative, about, allowMismatch } of uploadFilesOf(casesOf(spec))) {
|
|
31
|
+
const type = uploadTypeOf(relative);
|
|
32
|
+
const problem = (kind, details = {}) => needed.push({
|
|
33
|
+
path: relative,
|
|
34
|
+
type,
|
|
35
|
+
...(about?.trim() ? { about } : {}),
|
|
36
|
+
...(allowMismatch === true ? { allowMismatch } : {}),
|
|
37
|
+
problem: kind,
|
|
38
|
+
reason: fileProblemText(kind, { type, ...details }),
|
|
39
|
+
});
|
|
40
|
+
const real = yield* fs.realPath(path.resolve(root, relative)).pipe(Effect.option);
|
|
41
|
+
if (Option.isNone(real)) {
|
|
42
|
+
problem('missing');
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const inside = path.relative(root, real.value);
|
|
46
|
+
if (inside === '' || inside.startsWith('..') || path.isAbsolute(inside)) {
|
|
47
|
+
problem('outside_project');
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const checked = yield* Effect.scoped(Effect.gen(function* () {
|
|
51
|
+
const info = yield* fs.stat(real.value);
|
|
52
|
+
if (info.type !== 'File')
|
|
53
|
+
return { problem: 'not_a_file' };
|
|
54
|
+
const size = Number(info.size);
|
|
55
|
+
if (size === 0)
|
|
56
|
+
return { problem: 'empty' };
|
|
57
|
+
if (size > UPLOAD_MAX_BYTES)
|
|
58
|
+
return { problem: 'too_big', size };
|
|
59
|
+
if (allowMismatch === true)
|
|
60
|
+
return { size };
|
|
61
|
+
const file = yield* fs.open(real.value, { flag: 'r' });
|
|
62
|
+
const matches = contentMatches(relative, yield* sample(file, size));
|
|
63
|
+
return matches ? { size } : { problem: 'wrong_type' };
|
|
64
|
+
})).pipe(Effect.catch((error) => Effect.succeed({ problem: 'unreadable', error: error.message })));
|
|
65
|
+
if ('problem' in checked) {
|
|
66
|
+
problem(checked.problem, checked);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
sources.push({ path: relative, absolute: real.value, size: checked.size });
|
|
70
|
+
}
|
|
71
|
+
return { sources, needed };
|
|
72
|
+
});
|
|
73
|
+
/** Nothing started: the files a job needs, for the caller's agent to create (`formatFilesNeeded`). */
|
|
74
|
+
export const filesNeeded = (projectRoot, files) => new Failure({
|
|
75
|
+
code: ExitCode.FilesNeeded,
|
|
76
|
+
reason: 'files_needed',
|
|
77
|
+
message: formatFilesNeeded(files),
|
|
78
|
+
paths: [projectRoot],
|
|
79
|
+
details: { projectRoot, files },
|
|
80
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** An attribute selector, such as `[data-test="preview"]` or `[href*=linkedin i]`. */
|
|
2
|
+
export interface AttributeTest {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
/** Undefined for `[name]`: the attribute is there, whatever its value. */
|
|
5
|
+
readonly op?: '=' | '~=' | '|=' | '^=' | '$=' | '*=';
|
|
6
|
+
readonly value?: string;
|
|
7
|
+
/** `i` compares ignoring ASCII case, `s` with it; without one, as HTML says for the attribute. */
|
|
8
|
+
readonly flag?: 'i' | 's';
|
|
9
|
+
}
|
|
10
|
+
/** One compound selector: everything about one element, such as `iframe.preview[title=Hi]`. */
|
|
11
|
+
export interface Compound {
|
|
12
|
+
/** Lower case; undefined for `*` or none. */
|
|
13
|
+
readonly tag?: string;
|
|
14
|
+
readonly ids: ReadonlyArray<string>;
|
|
15
|
+
readonly classes: ReadonlyArray<string>;
|
|
16
|
+
readonly attributes: ReadonlyArray<AttributeTest>;
|
|
17
|
+
readonly not: ReadonlyArray<Compound>;
|
|
18
|
+
}
|
|
19
|
+
/** A compound and how it relates to the one before it: inside it anywhere, or right inside it. */
|
|
20
|
+
export interface Part {
|
|
21
|
+
readonly compound: Compound;
|
|
22
|
+
/** How this part relates to the one before it; undefined for the first. */
|
|
23
|
+
readonly combinator?: 'descendant' | 'child';
|
|
24
|
+
}
|
|
25
|
+
/** A selector list: an element matches when it matches any of them, judged by its last part. */
|
|
26
|
+
export type Selector = ReadonlyArray<ReadonlyArray<Part>>;
|
|
27
|
+
/** A selector, or why it cannot be matched against saved page state, in words. */
|
|
28
|
+
export type Parsed = {
|
|
29
|
+
readonly _tag: 'selector';
|
|
30
|
+
readonly selector: Selector;
|
|
31
|
+
} | {
|
|
32
|
+
readonly _tag: 'unsupported';
|
|
33
|
+
readonly reason: string;
|
|
34
|
+
};
|
|
35
|
+
/** What a highlight's CSS can check, besides the element itself and the frame it is in. */
|
|
36
|
+
export declare const SUPPORTED_CSS: string;
|
|
37
|
+
/**
|
|
38
|
+
* Parses `css` into the selectors `matches` takes, or says in words what of it cannot be matched
|
|
39
|
+
* against saved page state (see `SUPPORTED_CSS`).
|
|
40
|
+
*/
|
|
41
|
+
export declare const parseSelector: (css: string) => Parsed;
|
|
42
|
+
/**
|
|
43
|
+
* An attribute's value as saved: `cut` when only its start was kept, so it is longer than `value`.
|
|
44
|
+
*/
|
|
45
|
+
export interface AttributeValue {
|
|
46
|
+
readonly value: string;
|
|
47
|
+
readonly cut?: boolean | undefined;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* An element as a selector sees it: its own attributes, and the element it is in. What was not
|
|
51
|
+
* saved whole is said, so a selector that depends on it is not taken as matching or not.
|
|
52
|
+
*/
|
|
53
|
+
export interface CssElement {
|
|
54
|
+
readonly tag: string;
|
|
55
|
+
readonly id?: string | undefined;
|
|
56
|
+
readonly classes?: ReadonlyArray<string> | undefined;
|
|
57
|
+
/** More classes than `classes` holds. */
|
|
58
|
+
readonly classesCut?: boolean | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* An attribute's value, undefined when it does not have it (as far as was saved), or `unsaved`
|
|
61
|
+
* when it has it but its value was not saved.
|
|
62
|
+
*/
|
|
63
|
+
readonly attribute: (name: string) => AttributeValue | 'unsaved' | undefined;
|
|
64
|
+
/** More attributes than were saved, so one `attribute` does not give may still be there. */
|
|
65
|
+
readonly attributesCut?: boolean | undefined;
|
|
66
|
+
/** Not an HTML element (such as an `<svg>`), whose attribute values HTML never folds. */
|
|
67
|
+
readonly foreign?: boolean | undefined;
|
|
68
|
+
/** The element it is in; undefined at the top of its document, or when that was not saved. */
|
|
69
|
+
readonly parent: () => CssElement | undefined;
|
|
70
|
+
/** It is in an element that was not saved, so `parent` stops short of the top. */
|
|
71
|
+
readonly parentCut?: boolean | undefined;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Whether something holds: true, false, or undefined when it depends on what was not saved (an
|
|
75
|
+
* attribute value cut short, attributes, classes or ancestors left out).
|
|
76
|
+
*/
|
|
77
|
+
export type Maybe = boolean | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Attributes whose values HTML compares ignoring ASCII case in selectors, on HTML elements, unless
|
|
80
|
+
* the selector says `s` (HTML's list of case-insensitive attribute values).
|
|
81
|
+
*/
|
|
82
|
+
export declare const CASE_INSENSITIVE_ATTRIBUTES: ReadonlySet<string>;
|
|
83
|
+
/** Whether `el` matches any selector of the list; undefined when that depends on what was not saved. */
|
|
84
|
+
export declare const selectorMatches: (selector: Selector, el: CssElement) => Maybe;
|
|
85
|
+
/** Whether the selector looks at an element's ancestors at all. */
|
|
86
|
+
export declare const usesAncestors: (selector: Selector) => boolean;
|
|
87
|
+
/**
|
|
88
|
+
* The ID or test ID that every element matching `selector` exactly has, when it is one compound
|
|
89
|
+
* with one: what `Truncated.unsaved` can count.
|
|
90
|
+
*/
|
|
91
|
+
export declare const selectorKey: (selector: Selector) => {
|
|
92
|
+
readonly id: string;
|
|
93
|
+
} | {
|
|
94
|
+
readonly testId: string;
|
|
95
|
+
} | undefined;
|