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,337 @@
|
|
|
1
|
+
import { Cause, Clock, Context, Duration, Effect, FileSystem, Layer, Option, Path, Predicate, Schedule, Schema, } from 'effect';
|
|
2
|
+
import { Browser, Engine, evidencePackOf, REPORT_STALE_MS, staleFor, Storage, } from '../core/index.js';
|
|
3
|
+
import { Findings, JobId, NewJobSpec, ProjectConfig, runFiles, } from '../schema/index.js';
|
|
4
|
+
import { CONFIG_FILE, loadConfig, missingConfig } from './config.js';
|
|
5
|
+
import { runDoctor } from './doctor.js';
|
|
6
|
+
import { ExitCode } from './exit-codes.js';
|
|
7
|
+
import { Failure, fromNotImplemented } from './failure.js';
|
|
8
|
+
import { PackageInfo } from './package-info.js';
|
|
9
|
+
import { UpdateCheck } from './update-check.js';
|
|
10
|
+
import { checkUploads, filesNeeded } from './uploads.js';
|
|
11
|
+
/** How often a status call that waits rereads the ledger. */
|
|
12
|
+
export const STATUS_POLL_MS = 500;
|
|
13
|
+
/** The longest a status call may wait for a run to end. */
|
|
14
|
+
export const MAX_WAIT_SECONDS = 60;
|
|
15
|
+
/** Whether a run is past queued and running: it ended, or it waits for an answer. */
|
|
16
|
+
const hasEnded = (ledger) => ledger.state !== 'queued' && ledger.state !== 'running';
|
|
17
|
+
const isOver = (ledger) => ledger.state === 'finished' || ledger.state === 'errored';
|
|
18
|
+
/**
|
|
19
|
+
* Everything the CLI and the MCP server can do. Both are thin front ends over these, so the MCP
|
|
20
|
+
* server has no logic of its own.
|
|
21
|
+
*/
|
|
22
|
+
export class Operations extends Context.Service()('screw-bugs/Operations') {
|
|
23
|
+
static layer = (projectRoot) => Layer.effect(Operations, Effect.gen(function* () {
|
|
24
|
+
const engine = yield* Engine;
|
|
25
|
+
const storage = yield* Storage;
|
|
26
|
+
const browser = yield* Browser;
|
|
27
|
+
const fs = yield* FileSystem.FileSystem;
|
|
28
|
+
const path = yield* Path.Path;
|
|
29
|
+
const packageInfo = yield* PackageInfo;
|
|
30
|
+
const updateCheck = yield* Effect.serviceOption(UpdateCheck);
|
|
31
|
+
/** What the operations below read and write the project with. */
|
|
32
|
+
const platform = (effect) => effect.pipe(Effect.provideService(FileSystem.FileSystem, fs), Effect.provideService(Path.Path, path), Effect.provideService(PackageInfo, packageInfo));
|
|
33
|
+
const decodeJobId = (raw) => Schema.decodeUnknownEffect(JobId)(raw).pipe(Effect.mapError(() => new Failure({
|
|
34
|
+
code: ExitCode.Usage,
|
|
35
|
+
reason: 'invalid_job_id',
|
|
36
|
+
message: `"${raw}" is not a job ID.`,
|
|
37
|
+
})));
|
|
38
|
+
const runNotFound = (jobId) => new Failure({
|
|
39
|
+
code: ExitCode.Usage,
|
|
40
|
+
reason: 'run_not_found',
|
|
41
|
+
message: `No run with job ID ${jobId} in ${projectRoot}.`,
|
|
42
|
+
});
|
|
43
|
+
const orRunNotFound = (jobId) => (effect) => effect.pipe(Effect.catchTags({
|
|
44
|
+
RunNotFound: () => Effect.fail(runNotFound(jobId)),
|
|
45
|
+
StorageFailed: (e) => Effect.fail(storageFailure(e)),
|
|
46
|
+
}));
|
|
47
|
+
/**
|
|
48
|
+
* The ledger once the run is no longer queued or running, or as it is after
|
|
49
|
+
* `waitSeconds`. Polls storage rather than waiting on the engine, so a run started by
|
|
50
|
+
* another process counts too (and one whose process died ends when it goes stale).
|
|
51
|
+
*/
|
|
52
|
+
const waitForEnd = (jobId, waitSeconds) => engine
|
|
53
|
+
.status(jobId)
|
|
54
|
+
.pipe(Effect.repeat({ until: hasEnded, schedule: Schedule.spaced(STATUS_POLL_MS) }), Effect.timeoutOption(Duration.seconds(waitSeconds)), Effect.flatMap(Option.match({ onNone: () => engine.status(jobId), onSome: Effect.succeed })));
|
|
55
|
+
/** Runs whose report a status read failed to write; reads in this process don't retry. */
|
|
56
|
+
const reportFailed = new Set();
|
|
57
|
+
const ownReport = (jobId) => path.join(storage.runDir(jobId), runFiles.html);
|
|
58
|
+
/**
|
|
59
|
+
* The run's `walkthrough.html`, when its report is its own `report.html` and the
|
|
60
|
+
* walkthrough is there. Only read: whoever writes the report writes it.
|
|
61
|
+
*/
|
|
62
|
+
const walkthroughOf = (jobId, reportPath) => reportPath !== ownReport(jobId)
|
|
63
|
+
? Effect.succeed(undefined)
|
|
64
|
+
: storage
|
|
65
|
+
.probe(jobId, runFiles.walkthrough, 0)
|
|
66
|
+
.pipe(Effect.map((file) => file === undefined ? undefined : storage.filePath(jobId, runFiles.walkthrough)));
|
|
67
|
+
/** The run's own `report.html`, with its bundle from the ledger as it is now. */
|
|
68
|
+
const ownReportNow = (jobId) => engine.bundle(jobId).pipe(Effect.map((bundle) => ({ path: ownReport(jobId), bundle })));
|
|
69
|
+
/**
|
|
70
|
+
* How long ago the process of a run reported errored stopped beating (see `staleFor`),
|
|
71
|
+
* and whether that is past `REPORT_STALE_MS`, so its `report-interrupted.html` may be
|
|
72
|
+
* written. Before that it may only be paused, and come back to write its own report.
|
|
73
|
+
*/
|
|
74
|
+
const goneFor = (ledger) => Clock.currentTimeMillis.pipe(Effect.map((now) => {
|
|
75
|
+
const stale = staleFor(ledger, now);
|
|
76
|
+
return { stale, due: stale !== undefined && stale > REPORT_STALE_MS };
|
|
77
|
+
}));
|
|
78
|
+
/**
|
|
79
|
+
* Writes `report-interrupted.html` from the ledger as it is now, then returns the report
|
|
80
|
+
* that takes precedence on disk (see `Storage.findReport`) and its bundle: the run's own
|
|
81
|
+
* `report.html` may have landed meanwhile, and deleted the interrupted one.
|
|
82
|
+
*/
|
|
83
|
+
const writeInterrupted = (jobId) => Effect.gen(function* () {
|
|
84
|
+
const written = yield* engine.writeReport(jobId, { interrupted: true });
|
|
85
|
+
const found = yield* storage.findReport(jobId);
|
|
86
|
+
if (found === written.path)
|
|
87
|
+
return written;
|
|
88
|
+
return found === ownReport(jobId) ? yield* ownReportNow(jobId) : undefined;
|
|
89
|
+
});
|
|
90
|
+
/**
|
|
91
|
+
* The report of an ended run (see `Storage.findReport`). `report.html` is the run's own:
|
|
92
|
+
* only its process writes it. A run that `goneFor` says is due and that has no report gets
|
|
93
|
+
* `report-interrupted.html` written now, unless `write` is false. Best effort, once per
|
|
94
|
+
* process.
|
|
95
|
+
*/
|
|
96
|
+
const reportOf = (jobId, ledger, write) => Effect.gen(function* () {
|
|
97
|
+
const found = yield* storage.findReport(jobId);
|
|
98
|
+
if (found !== undefined || !write || reportFailed.has(jobId))
|
|
99
|
+
return found;
|
|
100
|
+
if (!(yield* goneFor(ledger)).due)
|
|
101
|
+
return undefined;
|
|
102
|
+
return yield* writeInterrupted(jobId).pipe(Effect.map((report) => report?.path), Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause)
|
|
103
|
+
? Effect.interrupt
|
|
104
|
+
: Effect.sync(() => {
|
|
105
|
+
reportFailed.add(jobId);
|
|
106
|
+
return undefined;
|
|
107
|
+
})));
|
|
108
|
+
});
|
|
109
|
+
/**
|
|
110
|
+
* The ledger read by `read`, and the report path of an ended run. Unless `writeReport` is
|
|
111
|
+
* false, an interrupted run's report may be written now (see `reportOf`).
|
|
112
|
+
*/
|
|
113
|
+
const ledgerOf = (jobId, read, writeReport = true) => Effect.gen(function* () {
|
|
114
|
+
const ledger = yield* read(jobId).pipe(orRunNotFound(jobId));
|
|
115
|
+
const reportPath = isOver(ledger)
|
|
116
|
+
? yield* reportOf(jobId, ledger, writeReport)
|
|
117
|
+
: undefined;
|
|
118
|
+
const walkthroughPath = yield* walkthroughOf(jobId, reportPath);
|
|
119
|
+
return {
|
|
120
|
+
ledger,
|
|
121
|
+
runDir: storage.runDir(jobId),
|
|
122
|
+
reportPath,
|
|
123
|
+
walkthroughPath,
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
/**
|
|
127
|
+
* (Re)writes a run's report. The same ownership as status reads: `report.html` only for a
|
|
128
|
+
* run that ended by itself, `report-interrupted.html` for one whose process is gone (see
|
|
129
|
+
* `goneFor`).
|
|
130
|
+
*/
|
|
131
|
+
const rewriteReport = (jobId) => Effect.gen(function* () {
|
|
132
|
+
const ledger = yield* engine.status(jobId).pipe(orRunNotFound(jobId));
|
|
133
|
+
const { stale, due } = yield* goneFor(ledger);
|
|
134
|
+
if (stale === undefined) {
|
|
135
|
+
if (!isOver(ledger))
|
|
136
|
+
return yield* new Failure({
|
|
137
|
+
code: ExitCode.Usage,
|
|
138
|
+
reason: 'run_not_ended',
|
|
139
|
+
message: `Run ${jobId} has not ended yet; it writes its report.html when it does.`,
|
|
140
|
+
});
|
|
141
|
+
return yield* engine.writeReport(jobId).pipe(orRunNotFound(jobId));
|
|
142
|
+
}
|
|
143
|
+
// The run's own report, written once it came back, is never replaced.
|
|
144
|
+
if ((yield* storage.findReport(jobId)) === ownReport(jobId))
|
|
145
|
+
return yield* ownReportNow(jobId).pipe(orRunNotFound(jobId));
|
|
146
|
+
if (!due)
|
|
147
|
+
return yield* new Failure({
|
|
148
|
+
code: ExitCode.Usage,
|
|
149
|
+
reason: 'run_not_responding',
|
|
150
|
+
message: `Run ${jobId} stopped responding ${Math.round(stale / 1000)} s ago; ` +
|
|
151
|
+
`an interrupted report is written after ${REPORT_STALE_MS / 60_000} minutes.`,
|
|
152
|
+
});
|
|
153
|
+
const report = yield* writeInterrupted(jobId).pipe(orRunNotFound(jobId));
|
|
154
|
+
if (report)
|
|
155
|
+
return report;
|
|
156
|
+
return yield* new Failure({
|
|
157
|
+
code: ExitCode.Internal,
|
|
158
|
+
reason: 'report_missing',
|
|
159
|
+
message: `The report of run ${jobId} was removed while it was being written.`,
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
const invalidFindings = (jobId, problems) => new Failure({
|
|
163
|
+
code: ExitCode.InvalidFindings,
|
|
164
|
+
reason: 'invalid_findings',
|
|
165
|
+
message: [
|
|
166
|
+
`The findings were not saved; fix ${problems.length === 1 ? 'this' : 'these'} and submit them again for job ${jobId}:`,
|
|
167
|
+
...problems.map((problem) => `- ${problem}`),
|
|
168
|
+
].join('\n'),
|
|
169
|
+
});
|
|
170
|
+
return Operations.of({
|
|
171
|
+
doctor: runDoctor(projectRoot, undefined, Option.getOrUndefined(Option.map(updateCheck, (check) => check.doctorCheck))).pipe(Effect.provideService(Browser, browser), platform),
|
|
172
|
+
init: (input) => Effect.gen(function* () {
|
|
173
|
+
const file = path.join(projectRoot, CONFIG_FILE);
|
|
174
|
+
const exists = yield* fs.exists(file).pipe(Effect.orElseSucceed(() => false));
|
|
175
|
+
if (exists && !input.force) {
|
|
176
|
+
return yield* new Failure({
|
|
177
|
+
code: ExitCode.Usage,
|
|
178
|
+
reason: 'config_exists',
|
|
179
|
+
message: `${file} already exists. Pass --force to overwrite it.`,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
const config = yield* Schema.decodeUnknownEffect(ProjectConfig)({
|
|
183
|
+
version: 1,
|
|
184
|
+
...(input.baseUrl ? { baseUrl: input.baseUrl } : {}),
|
|
185
|
+
}).pipe(Effect.mapError(invalidInput));
|
|
186
|
+
yield* fs.writeFileString(file, `${JSON.stringify(config, null, 2)}\n`).pipe(Effect.mapError(() => new Failure({
|
|
187
|
+
code: ExitCode.Config,
|
|
188
|
+
reason: 'config_not_written',
|
|
189
|
+
message: `Could not write ${file}.`,
|
|
190
|
+
})));
|
|
191
|
+
return { path: file };
|
|
192
|
+
}),
|
|
193
|
+
startRun: (input, options) => Effect.gen(function* () {
|
|
194
|
+
const config = yield* platform(loadConfig(projectRoot));
|
|
195
|
+
if (!config && options.requireConfig)
|
|
196
|
+
return yield* missingConfig(projectRoot);
|
|
197
|
+
const fromSpec = Predicate.isObject(input.spec) ? input.spec : {};
|
|
198
|
+
if (input.spec !== undefined && !Predicate.isObject(input.spec)) {
|
|
199
|
+
return yield* invalidInput({ message: 'The job spec must be a JSON object.' });
|
|
200
|
+
}
|
|
201
|
+
const url = input.url ??
|
|
202
|
+
(Predicate.hasProperty(fromSpec, 'url') ? fromSpec.url : undefined) ??
|
|
203
|
+
config?.baseUrl;
|
|
204
|
+
if (!url) {
|
|
205
|
+
return yield* new Failure({
|
|
206
|
+
code: ExitCode.Usage,
|
|
207
|
+
reason: 'missing_url',
|
|
208
|
+
message: 'No URL to test. Pass a URL, or set baseUrl in screw-bugs.json.',
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const spec = yield* Schema.decodeUnknownEffect(NewJobSpec)({
|
|
212
|
+
version: 1,
|
|
213
|
+
...fromSpec,
|
|
214
|
+
url,
|
|
215
|
+
...(input.device ? { device: input.device } : {}),
|
|
216
|
+
...(input.instructions !== undefined ? { instructions: input.instructions } : {}),
|
|
217
|
+
...(input.summary !== undefined ? { summary: input.summary } : {}),
|
|
218
|
+
}).pipe(Effect.mapError(invalidInput));
|
|
219
|
+
// Upload files are the caller's to make; nothing starts until every one will do.
|
|
220
|
+
const uploads = yield* platform(checkUploads(projectRoot, spec));
|
|
221
|
+
if (uploads.needed.length > 0)
|
|
222
|
+
return yield* filesNeeded(projectRoot, uploads.needed);
|
|
223
|
+
const jobId = yield* engine.startRun(spec, { uploads: uploads.sources }).pipe(Effect.catchTags({
|
|
224
|
+
NotImplemented: (e) => Effect.fail(fromNotImplemented(e)),
|
|
225
|
+
StorageFailed: (e) => Effect.fail(storageFailure(e)),
|
|
226
|
+
}));
|
|
227
|
+
return { jobId, runDir: storage.runDir(jobId) };
|
|
228
|
+
}),
|
|
229
|
+
awaitRun: (jobId) => ledgerOf(jobId, engine.awaitRun),
|
|
230
|
+
// The run has just tried to write its report; when that failed, trying again here
|
|
231
|
+
// would only hold up Ctrl+C.
|
|
232
|
+
stopRun: (jobId) => ledgerOf(jobId, (id) => engine.stopRun(id).pipe(Effect.andThen(engine.status(id))), false),
|
|
233
|
+
runStatus: (raw, options) => Effect.gen(function* () {
|
|
234
|
+
const jobId = yield* decodeJobId(raw);
|
|
235
|
+
const waitSeconds = options?.waitSeconds ?? 0;
|
|
236
|
+
const result = yield* ledgerOf(jobId, (id) => waitSeconds > 0 ? waitForEnd(id, waitSeconds) : engine.status(id));
|
|
237
|
+
const job = yield* storage.readJob(jobId).pipe(orRunNotFound(jobId));
|
|
238
|
+
const evidenceFile = storage.filePath(jobId, runFiles.evidence);
|
|
239
|
+
const evidencePath = isOver(result.ledger)
|
|
240
|
+
? (yield* storage.probe(jobId, runFiles.evidence, 0)) === undefined
|
|
241
|
+
? undefined
|
|
242
|
+
: evidenceFile
|
|
243
|
+
: undefined;
|
|
244
|
+
return {
|
|
245
|
+
...result,
|
|
246
|
+
job,
|
|
247
|
+
evidence: evidencePackOf(job, result.ledger),
|
|
248
|
+
evidencePath,
|
|
249
|
+
};
|
|
250
|
+
}),
|
|
251
|
+
submitFindings: (raw, input) => Effect.gen(function* () {
|
|
252
|
+
const jobId = yield* decodeJobId(raw);
|
|
253
|
+
const findings = yield* Schema.decodeUnknownEffect(Findings)(input, {
|
|
254
|
+
errors: 'all',
|
|
255
|
+
}).pipe(Effect.mapError((error) => invalidFindings(jobId, schemaProblems(error.message))));
|
|
256
|
+
const saved = yield* engine.submitFindings(jobId, findings).pipe(Effect.catchTags({
|
|
257
|
+
RunNotFound: () => Effect.fail(runNotFound(jobId)),
|
|
258
|
+
StorageFailed: (e) => Effect.fail(storageFailure(e)),
|
|
259
|
+
RunNotEnded: () => Effect.fail(new Failure({
|
|
260
|
+
code: ExitCode.Usage,
|
|
261
|
+
reason: 'run_not_ended',
|
|
262
|
+
message: `Run ${jobId} has not ended yet; wait for it with run_status, then submit findings.`,
|
|
263
|
+
})),
|
|
264
|
+
FindingsRejected: (e) => Effect.fail(invalidFindings(jobId, e.problems)),
|
|
265
|
+
}));
|
|
266
|
+
// Saved findings stay saved; a report that may not be rewritten yet shows them later.
|
|
267
|
+
const report = yield* rewriteReport(jobId).pipe(Effect.map((written) => written.path), Effect.catchTag('Failure', () => Effect.succeed(undefined)));
|
|
268
|
+
return {
|
|
269
|
+
jobId,
|
|
270
|
+
...saved,
|
|
271
|
+
reportPath: report,
|
|
272
|
+
walkthroughPath: yield* walkthroughOf(jobId, report),
|
|
273
|
+
};
|
|
274
|
+
}),
|
|
275
|
+
report: (raw) => Effect.gen(function* () {
|
|
276
|
+
const jobId = yield* decodeJobId(raw);
|
|
277
|
+
const written = yield* rewriteReport(jobId);
|
|
278
|
+
return {
|
|
279
|
+
path: written.path,
|
|
280
|
+
bundle: written.bundle,
|
|
281
|
+
walkthroughPath: yield* walkthroughOf(jobId, written.path),
|
|
282
|
+
};
|
|
283
|
+
}),
|
|
284
|
+
walkthrough: (raw) => Effect.gen(function* () {
|
|
285
|
+
const jobId = yield* decodeJobId(raw);
|
|
286
|
+
const written = yield* rewriteReport(jobId);
|
|
287
|
+
const walkthrough = yield* walkthroughOf(jobId, written.path);
|
|
288
|
+
if (walkthrough !== undefined)
|
|
289
|
+
return { path: walkthrough, reportPath: written.path };
|
|
290
|
+
return yield* written.path === ownReport(jobId)
|
|
291
|
+
? new Failure({
|
|
292
|
+
code: ExitCode.Internal,
|
|
293
|
+
reason: 'walkthrough_not_written',
|
|
294
|
+
message: `The walkthrough of run ${jobId} could not be written; its report is ${written.path}.`,
|
|
295
|
+
})
|
|
296
|
+
: new Failure({
|
|
297
|
+
code: ExitCode.Usage,
|
|
298
|
+
reason: 'run_interrupted',
|
|
299
|
+
message: `Run ${jobId} was interrupted, so it has no walkthrough; ` +
|
|
300
|
+
`its report is ${written.path}.`,
|
|
301
|
+
});
|
|
302
|
+
}),
|
|
303
|
+
listRuns: (limit) => storage.listRuns(limit).pipe(Effect.mapError(storageFailure)),
|
|
304
|
+
answer: (input) => Effect.gen(function* () {
|
|
305
|
+
const jobId = yield* decodeJobId(input.jobId);
|
|
306
|
+
yield* engine.answer({ jobId, questionId: input.questionId, text: input.text }).pipe(Effect.catchTags({
|
|
307
|
+
NotImplemented: (e) => Effect.fail(fromNotImplemented(e)),
|
|
308
|
+
RunNotFound: () => Effect.fail(runNotFound(jobId)),
|
|
309
|
+
}));
|
|
310
|
+
}),
|
|
311
|
+
});
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* A Schema error message as one line per problem, in the form `checkFindings` uses:
|
|
316
|
+
* `fixes[0].severity: Expected "high" | "medium" | "low"`.
|
|
317
|
+
*/
|
|
318
|
+
export const schemaProblems = (message) => {
|
|
319
|
+
const problems = [];
|
|
320
|
+
let pending = [];
|
|
321
|
+
for (const line of message.split('\n')) {
|
|
322
|
+
const at = /^\s+at (.*)$/.exec(line);
|
|
323
|
+
if (at === null) {
|
|
324
|
+
if (line.trim())
|
|
325
|
+
pending.push(line.trim());
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
const path = at[1].replace(/\["([^"]*)"\]/g, '.$1').replace(/^\./, '');
|
|
329
|
+
problems.push(`${path || 'findings'}: ${pending.join(' ')}`);
|
|
330
|
+
pending = [];
|
|
331
|
+
}
|
|
332
|
+
if (pending.length > 0)
|
|
333
|
+
problems.push(pending.join(' '));
|
|
334
|
+
return problems;
|
|
335
|
+
};
|
|
336
|
+
const invalidInput = (error) => new Failure({ code: ExitCode.Usage, reason: 'invalid_job', message: error.message });
|
|
337
|
+
const storageFailure = (error) => new Failure({ code: ExitCode.Internal, reason: 'storage_failed', message: error.message });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Context, FileSystem, Layer, Path } from 'effect';
|
|
2
|
+
declare const PackageInfo_base: Context.ServiceClass<PackageInfo, "screw-bugs/PackageInfo", {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly version: string;
|
|
5
|
+
readonly engines: {
|
|
6
|
+
readonly node: string;
|
|
7
|
+
};
|
|
8
|
+
} & {
|
|
9
|
+
readonly root: string;
|
|
10
|
+
}>;
|
|
11
|
+
/**
|
|
12
|
+
* Our own package.json: the name and version we report, and the Node versions we support, and
|
|
13
|
+
* `root`, the folder it is in (where screw-bugs is installed or checked out).
|
|
14
|
+
*/
|
|
15
|
+
export declare class PackageInfo extends PackageInfo_base {
|
|
16
|
+
/**
|
|
17
|
+
* Read at runtime so `engines.node` stays the single source of the minimum Node version. A
|
|
18
|
+
* missing or broken package.json is a broken install, so it is a defect.
|
|
19
|
+
*/
|
|
20
|
+
static readonly layer: Layer.Layer<PackageInfo, never, FileSystem.FileSystem | Path.Path>;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Context, Effect, FileSystem, Layer, Path, Schema } from 'effect';
|
|
2
|
+
const PackageJson = Schema.Struct({
|
|
3
|
+
name: Schema.String,
|
|
4
|
+
version: Schema.String,
|
|
5
|
+
engines: Schema.Struct({ node: Schema.String }),
|
|
6
|
+
});
|
|
7
|
+
/**
|
|
8
|
+
* Our own package.json: the name and version we report, and the Node versions we support, and
|
|
9
|
+
* `root`, the folder it is in (where screw-bugs is installed or checked out).
|
|
10
|
+
*/
|
|
11
|
+
export class PackageInfo extends Context.Service()('screw-bugs/PackageInfo') {
|
|
12
|
+
/**
|
|
13
|
+
* Read at runtime so `engines.node` stays the single source of the minimum Node version. A
|
|
14
|
+
* missing or broken package.json is a broken install, so it is a defect.
|
|
15
|
+
*/
|
|
16
|
+
static layer = Layer.effect(PackageInfo, Effect.gen(function* () {
|
|
17
|
+
const fs = yield* FileSystem.FileSystem;
|
|
18
|
+
const path = yield* Path.Path;
|
|
19
|
+
// `src/cli/` in development and `dist/cli/` when installed: package.json is two levels up.
|
|
20
|
+
const file = yield* path.fromFileUrl(new URL('../../package.json', import.meta.url));
|
|
21
|
+
const text = yield* fs.readFileString(file);
|
|
22
|
+
const json = yield* Schema.decodeUnknownEffect(Schema.fromJsonString(PackageJson))(text);
|
|
23
|
+
return { ...json, root: path.dirname(file) };
|
|
24
|
+
}).pipe(Effect.orDie));
|
|
25
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { Context, Duration, Effect, FileSystem, Layer, Option, Path, Schema, Scope } from 'effect';
|
|
2
|
+
import { HttpServerRequest, HttpServerResponse, type HttpServerError } from 'effect/http';
|
|
3
|
+
import { Failure } from './failure.js';
|
|
4
|
+
/**
|
|
5
|
+
* A local HTTP server for reports, for browsers that cannot open `file://` (the ones built into
|
|
6
|
+
* coding tools). It binds 127.0.0.1 only and serves the runs folder read-only: only files with
|
|
7
|
+
* the extensions a report uses, never a directory listing, a dotfile, or anything outside the
|
|
8
|
+
* folder (also through symlinks). Range requests (206, 416) come from `HttpStaticServer`, so
|
|
9
|
+
* videos seek and play.
|
|
10
|
+
*/
|
|
11
|
+
export declare const REPORT_HOST = "127.0.0.1";
|
|
12
|
+
/**
|
|
13
|
+
* Without `--port`, a project's report server takes a port from this range, the same one every
|
|
14
|
+
* time: a hash of the runs folder's real path picks it, so report links keep working across
|
|
15
|
+
* processes and sessions.
|
|
16
|
+
*/
|
|
17
|
+
export declare const STABLE_PORTS: {
|
|
18
|
+
readonly first: 41000;
|
|
19
|
+
readonly count: 1000;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Proves that a report server serves a given runs folder, so another process for the same folder
|
|
23
|
+
* can use it instead of starting its own. A challenge: the caller sends a fresh random `nonce`
|
|
24
|
+
* (64 hex digits), and the server answers with an HMAC of it, the folder's hash and the port it
|
|
25
|
+
* listens on, under the folder's secret (see `SECRET_FILE`). Only a process that can read the
|
|
26
|
+
* secret (the same user) can answer, and an answer relayed from that user's server on another port
|
|
27
|
+
* does not fit. Answered after the same Host and method checks as files; 404 without a secret.
|
|
28
|
+
*/
|
|
29
|
+
export declare const IDENTITY_PATH = "/.screw-bugs/identity";
|
|
30
|
+
/** What `IDENTITY_PATH` answers: a fixed marker, the folder's hash (never its path) and the proof. */
|
|
31
|
+
export declare const Identity: Schema.Struct<{
|
|
32
|
+
readonly app: Schema.Literal<"screw-bugs-report-server">;
|
|
33
|
+
readonly root: Schema.String;
|
|
34
|
+
readonly proof: Schema.String;
|
|
35
|
+
}>;
|
|
36
|
+
/**
|
|
37
|
+
* The secret behind identity proofs, beside the runs folder (in `.screw-bugs/`): 32 random bytes
|
|
38
|
+
* as hex, made once per folder, readable by its owner only (mode 0600). On Windows, where modes do
|
|
39
|
+
* not apply, it gets whatever access `FileSystem` gives new files there.
|
|
40
|
+
*/
|
|
41
|
+
export declare const SECRET_FILE = "report-server.secret";
|
|
42
|
+
/**
|
|
43
|
+
* Whether proofs are off on this platform: on Windows, where mode 0600 does not make a file
|
|
44
|
+
* private (a folder's inherited ACL may let other users read it), so a secret there could be read
|
|
45
|
+
* and proofs forged. There, no secret is made, no server is reused, and a taken stable port means
|
|
46
|
+
* a free port of our own. Read from the path separator; tests give it instead.
|
|
47
|
+
*/
|
|
48
|
+
export declare const noProofs: (options?: {
|
|
49
|
+
readonly windows?: boolean | undefined;
|
|
50
|
+
}) => Effect.Effect<boolean, never, Path.Path>;
|
|
51
|
+
/**
|
|
52
|
+
* The secret for the runs folder at `realRoot`, made on first use: written to a temporary file
|
|
53
|
+
* with mode 0600, then linked into place, which fails if another process got there first (its
|
|
54
|
+
* secret is then the one used). None when it cannot be made or read, or when others could read it,
|
|
55
|
+
* and always on Windows (see `noProofs`), where it is not made at all.
|
|
56
|
+
*/
|
|
57
|
+
export declare const loadSecret: (realRoot: string, options?: {
|
|
58
|
+
readonly windows?: boolean;
|
|
59
|
+
}) => Effect.Effect<Option.Option<Uint8Array<ArrayBufferLike>>, never, FileSystem.FileSystem | Path.Path>;
|
|
60
|
+
/** The proof a server for `rootId` on `port` gives for `nonce`. */
|
|
61
|
+
export declare const proofOf: (secret: Uint8Array, nonce: string, rootId: string, port: number) => string;
|
|
62
|
+
/** The hash that identifies a served folder (its real absolute path), as hex. */
|
|
63
|
+
export declare const rootIdOf: (realRoot: string) => string;
|
|
64
|
+
/** The stable port for a served folder, from its `rootIdOf` hash. */
|
|
65
|
+
export declare const stablePortOf: (rootId: string) => number;
|
|
66
|
+
/** What a report links to; anything else is a 404. */
|
|
67
|
+
export declare const SERVED_TYPES: Readonly<Record<string, string>>;
|
|
68
|
+
/**
|
|
69
|
+
* The request handler for `root`, answering only requests addressed to `port` on this machine by
|
|
70
|
+
* name (a Host check, so a web page cannot reach it through DNS rebinding). `port` may be a
|
|
71
|
+
* function, read per request, so the handler can be made before the server knows its port.
|
|
72
|
+
*/
|
|
73
|
+
export declare const makeReportApp: (root: string, port: number | (() => number), secret?: Uint8Array<ArrayBufferLike> | undefined) => Effect.Effect<Effect.Effect<HttpServerResponse.HttpServerResponse, never, HttpServerRequest.HttpServerRequest>, import("effect/PlatformError").PlatformError, FileSystem.FileSystem | import("effect/http/HttpPlatform").HttpPlatform | Path.Path>;
|
|
74
|
+
/** The URL of `file` (an absolute path inside `root`) on a server at `base`, or undefined. */
|
|
75
|
+
export declare const fileUrl: (path: Path.Path, base: string, root: string, file: string) => string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Listens for `root` on `port` (0 picks a free one) while the scope is open; the base URL.
|
|
78
|
+
* `beforeServe` runs between listening and attaching the handler, for tests of that moment.
|
|
79
|
+
*/
|
|
80
|
+
export declare const listen: (root: string, port: number, secret?: Uint8Array, options?: {
|
|
81
|
+
readonly beforeServe?: (state: {
|
|
82
|
+
readonly port: number;
|
|
83
|
+
readonly held: () => number;
|
|
84
|
+
}) => Effect.Effect<void>;
|
|
85
|
+
}) => Effect.Effect<string, import("effect/PlatformError").PlatformError | HttpServerError.ServeError, Scope.Scope>;
|
|
86
|
+
/** How long to wait for whatever holds a port to answer the identity challenge. */
|
|
87
|
+
export declare const IDENTITY_TIMEOUT: Duration.Duration;
|
|
88
|
+
/**
|
|
89
|
+
* Whether the report server of this folder (the one that can read `secret`) answers on `port`.
|
|
90
|
+
* Anything else there (another program, a server for another folder or user, a wrong or late
|
|
91
|
+
* answer) is false. Nothing stops a process from taking the port after this check; links would
|
|
92
|
+
* then reach it until this process takes the port back (see `ReportServer`).
|
|
93
|
+
*/
|
|
94
|
+
export declare const servesSameRoot: (port: number, rootId: string, secret: Uint8Array) => Effect.Effect<boolean, never, never>;
|
|
95
|
+
/** How often to try the preferred port again after its holder failed the challenge. */
|
|
96
|
+
export declare const BIND_RETRIES = 2;
|
|
97
|
+
export interface Served {
|
|
98
|
+
/** Such as `http://127.0.0.1:41234`. */
|
|
99
|
+
readonly base: string;
|
|
100
|
+
/**
|
|
101
|
+
* `preferred`: this process serves on the preferred port. `reused`: this folder's server in
|
|
102
|
+
* another process does, and this one holds nothing. `fallback`: this process serves on another
|
|
103
|
+
* port, because something else holds the preferred one.
|
|
104
|
+
*/
|
|
105
|
+
readonly on: 'preferred' | 'reused' | 'fallback';
|
|
106
|
+
}
|
|
107
|
+
/** What `serveOnPreferredPort` needs to know about the folder. */
|
|
108
|
+
export interface Folder {
|
|
109
|
+
readonly rootId: string;
|
|
110
|
+
readonly secret: Option.Option<Uint8Array>;
|
|
111
|
+
}
|
|
112
|
+
/** The hash and secret of the runs folder at `root` (no secret on Windows, see `noProofs`). */
|
|
113
|
+
export declare const folderOf: (root: string, options?: {
|
|
114
|
+
readonly windows?: boolean;
|
|
115
|
+
}) => Effect.Effect<{
|
|
116
|
+
rootId: string;
|
|
117
|
+
secret: Option.Option<Uint8Array<ArrayBufferLike>>;
|
|
118
|
+
}, import("effect/PlatformError").PlatformError, FileSystem.FileSystem | Path.Path>;
|
|
119
|
+
/**
|
|
120
|
+
* Serves `root` on `preferred` if it is free; else uses this folder's server already there, if it
|
|
121
|
+
* proves it is one; else tries the port again (`BIND_RETRIES`; its holder may have been shutting
|
|
122
|
+
* down); else serves with `fallback` (by default on a free port). `probe` is for tests.
|
|
123
|
+
*/
|
|
124
|
+
export declare const serveOnPreferredPort: (root: string, preferred: number, folder: Folder, options?: {
|
|
125
|
+
readonly fallback?: Effect.Effect<string, HttpServerError.ServeError, Scope.Scope | FileSystem.FileSystem | Path.Path>;
|
|
126
|
+
readonly probe?: Effect.Effect<boolean>;
|
|
127
|
+
}) => Effect.Effect<{
|
|
128
|
+
base: string;
|
|
129
|
+
on: "preferred";
|
|
130
|
+
} | {
|
|
131
|
+
base: string;
|
|
132
|
+
on: "reused";
|
|
133
|
+
} | {
|
|
134
|
+
base: string;
|
|
135
|
+
on: "fallback";
|
|
136
|
+
}, import("effect/PlatformError").PlatformError | HttpServerError.ServeError, FileSystem.FileSystem | Path.Path | Scope.Scope>;
|
|
137
|
+
/**
|
|
138
|
+
* Serves `root` while the scope is open. With `port`, exactly there (0 picks a free one), failing
|
|
139
|
+
* if it is taken. Without, on `preferred` (by default the folder's stable port, see
|
|
140
|
+
* `STABLE_PORTS`) as `serveOnPreferredPort` does.
|
|
141
|
+
*/
|
|
142
|
+
export declare const startReportServer: (root: string, options?: {
|
|
143
|
+
readonly port?: number | undefined;
|
|
144
|
+
readonly preferred?: number | undefined;
|
|
145
|
+
/** For tests: the platform, instead of reading it (see `noProofs`). */
|
|
146
|
+
readonly windows?: boolean;
|
|
147
|
+
}) => Effect.Effect<Served, Failure, FileSystem.FileSystem | Path.Path | Scope.Scope>;
|
|
148
|
+
/** `SCREW_BUGS_SERVE=0` (or false, no, off) turns the MCP server's report URLs off. */
|
|
149
|
+
export declare const serveEnabled: Effect.Effect<boolean, never, never>;
|
|
150
|
+
declare const ReportServer_base: Context.ServiceClass<ReportServer, "screw-bugs/ReportServer", {
|
|
151
|
+
/** The URL of a file in the runs folder, starting the server if needed. */
|
|
152
|
+
readonly urlOf: (file: string) => Effect.Effect<string | undefined>;
|
|
153
|
+
}>;
|
|
154
|
+
/**
|
|
155
|
+
* Report URLs for a long-lived process (the MCP server). The server starts on first need, on the
|
|
156
|
+
* project's stable port, and lives as long as the process. While this folder's server in another
|
|
157
|
+
* process holds that port, its URL is given instead; while anything else holds it, this process
|
|
158
|
+
* serves on a free port. Either way each later call tries the stable port again, and once it is
|
|
159
|
+
* free this process serves there too and gives that URL from then on; a fallback server stays up,
|
|
160
|
+
* so links already given keep working. Without a server, or when none can start, there is no URL
|
|
161
|
+
* and callers give the file path alone. On Windows it never uses another process's server (see
|
|
162
|
+
* `noProofs`). `preferredPort` and `windows` are for tests.
|
|
163
|
+
*/
|
|
164
|
+
export declare class ReportServer extends ReportServer_base {
|
|
165
|
+
static readonly layer: (projectRoot: string, options?: {
|
|
166
|
+
readonly preferredPort?: number | undefined;
|
|
167
|
+
readonly windows?: boolean;
|
|
168
|
+
}) => Layer.Layer<ReportServer, never, FileSystem.FileSystem | Path.Path>;
|
|
169
|
+
}
|
|
170
|
+
export {};
|