crysk 0.0.0 → 0.3.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 +176 -0
- package/README.md +609 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/cli.js +42292 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +122 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +496 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +923 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +901 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +847 -0
- package/src/pr/ci-admission-gate.ts +665 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +787 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
// The `.cryskignore` (legacy `.prheroignore`) dialect: gitignore syntax, translated to Bun.Glob and
|
|
2
|
+
// matched with per-RULE last-match-wins. Pure and offline-testable — this
|
|
3
|
+
// module never reads a file or shells out; callers hand it text.
|
|
4
|
+
//
|
|
5
|
+
// Every deviation from git's own gitignore(5) is deliberate and named at the
|
|
6
|
+
// point it happens, never silent: this repo matches a FINITE DIFF FILE LIST,
|
|
7
|
+
// not a tree walk, so some of git's own tree-walk optimizations (and their
|
|
8
|
+
// side effects) do not apply here and are not reproduced.
|
|
9
|
+
//
|
|
10
|
+
// Case sensitivity is DELIBERATE, not an accident of what Bun.Glob happens to
|
|
11
|
+
// do: matching is case-SENSITIVE, matching git's own behavior (git tracks
|
|
12
|
+
// paths case-sensitively regardless of the filesystem), and DIVERGING on
|
|
13
|
+
// purpose from the `ignore` npm package's case-INSENSITIVE default — the
|
|
14
|
+
// thing most JS users' intuitions come from, and which that package's own
|
|
15
|
+
// README documents as its deviation from git. See the pin in
|
|
16
|
+
// test/ignore-file.test.ts.
|
|
17
|
+
|
|
18
|
+
import { IGNORE_FILE_NAME } from "./brand-paths";
|
|
19
|
+
|
|
20
|
+
// The name an error carries when the caller does not say which file it parsed.
|
|
21
|
+
// A caller that read the legacy .prheroignore passes that name instead.
|
|
22
|
+
const FILE_NAME = IGNORE_FILE_NAME;
|
|
23
|
+
|
|
24
|
+
export interface IgnoreRule {
|
|
25
|
+
// The source line, after leading `!`/negation removal and trailing
|
|
26
|
+
// insignificant-whitespace stripping — but otherwise VERBATIM, escape
|
|
27
|
+
// backslashes included. Provenance prints THIS, never the emitted globs:
|
|
28
|
+
// a user re-negating a builtin (`!bun.lock`) then textually mirrors the
|
|
29
|
+
// builtin line it negates (`bun.lock`), which is only true if neither
|
|
30
|
+
// side has been further transformed.
|
|
31
|
+
pattern: string;
|
|
32
|
+
negated: boolean;
|
|
33
|
+
// Bun.Glob-ready translations of `pattern`. The rule fires if ANY of
|
|
34
|
+
// these match — that "any" is an OR WITHIN one rule's own dual emission
|
|
35
|
+
// (e.g. the bare form and the directory-contents form), never the
|
|
36
|
+
// last-match-wins decision ACROSS rules. See compileIgnoreRules.
|
|
37
|
+
globs: string[];
|
|
38
|
+
source: "builtin" | "user";
|
|
39
|
+
// 1-based. Builtins have no line — there is no file for them to be a
|
|
40
|
+
// line OF.
|
|
41
|
+
line?: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Thrown by parseIgnoreFile before any matching or spend happens. The file
|
|
45
|
+
// name defaults to ".cryskignore" and a caller reading the legacy
|
|
46
|
+
// ".prheroignore" passes that name; a caller that resolves a
|
|
47
|
+
// different physical path (e.g. a base-ref blob) is expected to catch and
|
|
48
|
+
// re-wrap with that context.
|
|
49
|
+
export class IgnoreFileError extends Error {
|
|
50
|
+
readonly file: string;
|
|
51
|
+
readonly line: number;
|
|
52
|
+
readonly text: string;
|
|
53
|
+
|
|
54
|
+
constructor(file: string, line: number, text: string, reason: string) {
|
|
55
|
+
super(
|
|
56
|
+
`${file}:${line}: ${reason} — offending line: ${JSON.stringify(text)}`,
|
|
57
|
+
);
|
|
58
|
+
this.name = "IgnoreFileError";
|
|
59
|
+
this.file = file;
|
|
60
|
+
this.line = line;
|
|
61
|
+
this.text = text;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// The 9 shipped exclusions, as gitignore LINES — not hand-written globs.
|
|
66
|
+
// Feeding these through the SAME parser a user's own file goes through
|
|
67
|
+
// means a user's `!bun.lock` textually mirrors the builtin line it negates,
|
|
68
|
+
// and it means the dual-emission behavior (see translatePattern) applies to
|
|
69
|
+
// the defaults too: `bun.lock` now ALSO matches a directory literally named
|
|
70
|
+
// `bun.lock`, a benign superset of the old hand-written `**/bun.lock`.
|
|
71
|
+
export const BUILTIN_IGNORE_LINES: string[] = [
|
|
72
|
+
"bun.lock",
|
|
73
|
+
"package-lock.json",
|
|
74
|
+
"yarn.lock",
|
|
75
|
+
"pnpm-lock.yaml",
|
|
76
|
+
"Cargo.lock",
|
|
77
|
+
"go.sum",
|
|
78
|
+
"*.min.js",
|
|
79
|
+
"*.min.css",
|
|
80
|
+
"*.snap",
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
export const BUILTIN_IGNORE_RULES: IgnoreRule[] = parseIgnoreFile(
|
|
84
|
+
BUILTIN_IGNORE_LINES.join("\n"),
|
|
85
|
+
"builtin",
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
export function parseIgnoreFile(
|
|
89
|
+
text: string,
|
|
90
|
+
source: IgnoreRule["source"],
|
|
91
|
+
fileName: string = FILE_NAME,
|
|
92
|
+
): IgnoreRule[] {
|
|
93
|
+
const rules: IgnoreRule[] = [];
|
|
94
|
+
const rawLines = text.split("\n");
|
|
95
|
+
for (let i = 0; i < rawLines.length; i++) {
|
|
96
|
+
const lineNo = i + 1;
|
|
97
|
+
const line = rawLines[i] ?? "";
|
|
98
|
+
const raw = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
99
|
+
if (raw.length === 0) continue; // blank line: no rule, not an error
|
|
100
|
+
if (raw[0] === "#") continue; // unescaped comment marker: whole line skipped
|
|
101
|
+
|
|
102
|
+
let negated = false;
|
|
103
|
+
let working: string;
|
|
104
|
+
if (raw[0] === "!") {
|
|
105
|
+
negated = true;
|
|
106
|
+
working = raw.slice(1);
|
|
107
|
+
} else {
|
|
108
|
+
// `\!` and `\#` (and every other backslash pair) pass through
|
|
109
|
+
// UNTOUCHED here — Bun.Glob's own escape handling resolves them at
|
|
110
|
+
// match time. Verified directly: Bun.Glob("\\!foo").match("!foo")
|
|
111
|
+
// and Bun.Glob("\\#foo").match("#foo") are both true, so there is
|
|
112
|
+
// nothing for this parser to unescape on their behalf.
|
|
113
|
+
working = raw;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
working = stripTrailingWhitespace(working);
|
|
117
|
+
|
|
118
|
+
if (negated && working.length === 0) {
|
|
119
|
+
throw new IgnoreFileError(
|
|
120
|
+
fileName,
|
|
121
|
+
lineNo,
|
|
122
|
+
raw,
|
|
123
|
+
"bare ! (undocumented by gitignore(5)): a negation marker needs a pattern to negate",
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
if (working.length === 0) continue; // an all-whitespace line reduces to blank
|
|
127
|
+
|
|
128
|
+
if (working === "/") {
|
|
129
|
+
throw new IgnoreFileError(
|
|
130
|
+
fileName,
|
|
131
|
+
lineNo,
|
|
132
|
+
raw,
|
|
133
|
+
"bare / (undocumented by gitignore(5)): nothing to anchor",
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (hasTrailingUnescapedBackslash(working)) {
|
|
138
|
+
throw new IgnoreFileError(
|
|
139
|
+
fileName,
|
|
140
|
+
lineNo,
|
|
141
|
+
raw,
|
|
142
|
+
"trailing unescaped backslash (deliberately narrower than gitignore(5), " +
|
|
143
|
+
"which leaves this case undefined; Bun.Glob itself does not throw for " +
|
|
144
|
+
"one either — it silently matches nothing — so Crysk throws instead " +
|
|
145
|
+
"of letting a misconfigured rule become a silent no-op)",
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
rules.push({
|
|
150
|
+
pattern: working,
|
|
151
|
+
negated,
|
|
152
|
+
globs: translatePattern(working),
|
|
153
|
+
source,
|
|
154
|
+
line: source === "user" ? lineNo : undefined,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
return rules;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Removes trailing spaces/tabs, UNLESS the last one is backslash-escaped —
|
|
161
|
+
// in which case only the ESCAPING backslash is dropped and stripping stops;
|
|
162
|
+
// the whitespace character itself survives as a real, literal trailing
|
|
163
|
+
// character (no further unescaping needed: a bare trailing space is not a
|
|
164
|
+
// special glob character, so Bun.Glob matches it literally either way).
|
|
165
|
+
function stripTrailingWhitespace(line: string): string {
|
|
166
|
+
let end = line.length;
|
|
167
|
+
while (end > 0 && (line[end - 1] === " " || line[end - 1] === "\t")) {
|
|
168
|
+
let backslashes = 0;
|
|
169
|
+
let i = end - 2;
|
|
170
|
+
while (i >= 0 && line[i] === "\\") {
|
|
171
|
+
backslashes++;
|
|
172
|
+
i--;
|
|
173
|
+
}
|
|
174
|
+
if (backslashes % 2 === 1) {
|
|
175
|
+
return line.slice(0, end - 2) + line.slice(end - 1);
|
|
176
|
+
}
|
|
177
|
+
end--;
|
|
178
|
+
}
|
|
179
|
+
return line.slice(0, end);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function hasTrailingUnescapedBackslash(line: string): boolean {
|
|
183
|
+
if (!line.endsWith("\\")) return false;
|
|
184
|
+
let count = 0;
|
|
185
|
+
let i = line.length - 1;
|
|
186
|
+
while (i >= 0 && line[i] === "\\") {
|
|
187
|
+
count++;
|
|
188
|
+
i--;
|
|
189
|
+
}
|
|
190
|
+
return count % 2 === 1;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// The anchoring/trailing-slash matrix. A pattern is anchored IFF a leading
|
|
194
|
+
// OR interior slash remains once the TRAILING slash — a directory-only
|
|
195
|
+
// marker, not an anchor — has been set aside. This is git's own distinction
|
|
196
|
+
// (gitignore(5)): `frotz/` matches a directory named `frotz` anywhere,
|
|
197
|
+
// while `doc/frotz/` matches only `doc/frotz`, because the interior slash
|
|
198
|
+
// in the second pattern anchors it to the repo root and the trailing slash
|
|
199
|
+
// in NEITHER pattern does that job.
|
|
200
|
+
//
|
|
201
|
+
// The "ONLY" rows are load-bearing, not a stylistic choice: a trailing
|
|
202
|
+
// slash means directory-only, so ALSO emitting the bare form would wrongly
|
|
203
|
+
// match a same-named FILE (verified: `build/` must not match the file
|
|
204
|
+
// `src/build`). Conversely the no-trailing-slash rows dual-emit on purpose:
|
|
205
|
+
// `foo/bar` alone does NOT cover the directory's own contents (verified:
|
|
206
|
+
// Bun.Glob("foo/bar").match("foo/bar/x.ts") is false), so the `/**` form is
|
|
207
|
+
// a real gap, not redundant.
|
|
208
|
+
//
|
|
209
|
+
// KNOWN DIVERGENCE from real git, found by the differential test
|
|
210
|
+
// (test/ignore-file-differential.test.ts) and left as-is rather than
|
|
211
|
+
// "fixed": a trailing-slash rule's directory-only emission requires content
|
|
212
|
+
// AFTER the directory name (`**/frotz/**`), so a BARE path string equal to
|
|
213
|
+
// the directory's own name never matches here — there is no way to tell
|
|
214
|
+
// from a string alone whether "frotz" names a file or a directory. Real
|
|
215
|
+
// git, given an actual directory on disk, knows and matches it. This is
|
|
216
|
+
// architecturally forced: a string alone cannot say what it names.
|
|
217
|
+
//
|
|
218
|
+
// This comment previously called that case UNREACHABLE, reasoning that a
|
|
219
|
+
// diff's changed-path list only ever contains leaves. That was WRONG, and
|
|
220
|
+
// the counter-example is ordinary: a SUBMODULE BUMP is a bare
|
|
221
|
+
// directory-shaped entry. Measured — `git diff --numstat` emits `1 1 sub`
|
|
222
|
+
// and the patch header is `diff --git a/sub b/sub` with mode 160000 — so a
|
|
223
|
+
// gitlink flows through parseNumstatFiles and diffRecordPath like any other
|
|
224
|
+
// path. A repo whose author writes `sub/` to ignore submodule bumps ignores
|
|
225
|
+
// NOTHING.
|
|
226
|
+
//
|
|
227
|
+
// The workaround is exact and already works: write `sub`, with no trailing
|
|
228
|
+
// slash. That dual-emits `**/sub` AND `**/sub/**`, and `**/sub` matches the
|
|
229
|
+
// bare gitlink. Pinned by "a trailing-slash rule does not match a bare
|
|
230
|
+
// gitlink path" in test/ignore-file.test.ts, because a claim of
|
|
231
|
+
// unreachability is exactly what stops the next person from testing it.
|
|
232
|
+
// Note the differential suite cannot catch this on its own: its scratch
|
|
233
|
+
// repo contains files and directories, never a submodule.
|
|
234
|
+
//
|
|
235
|
+
// Closing it here is NOT possible and must not be attempted: emitting the
|
|
236
|
+
// bare form for a trailing-slash rule would make `build/` match a FILE
|
|
237
|
+
// named `build`, which is the exact wrong the "ONLY" rows above exist to
|
|
238
|
+
// prevent. A string cannot carry what it names. The door, if anyone wants
|
|
239
|
+
// it, is one layer up — the patch header already carries mode 160000, so a
|
|
240
|
+
// caller that knows an entry is a gitlink could decide it separately.
|
|
241
|
+
function translatePattern(pattern: string): string[] {
|
|
242
|
+
const trailingSlash = pattern.endsWith("/");
|
|
243
|
+
const body = trailingSlash ? pattern.slice(0, -1) : pattern;
|
|
244
|
+
const anchored = body.includes("/");
|
|
245
|
+
const leadingSlash = body.startsWith("/");
|
|
246
|
+
const core = escapeBraces(leadingSlash ? body.slice(1) : body);
|
|
247
|
+
if (anchored) {
|
|
248
|
+
return trailingSlash ? [`${core}/**`] : [core, `${core}/**`];
|
|
249
|
+
}
|
|
250
|
+
return trailingSlash ? [`**/${core}/**`] : [`**/${core}`, `**/${core}/**`];
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// gitignore has no brace syntax — `{`/`}` are ordinary literal characters
|
|
254
|
+
// there. Bun.Glob, however, reads `{a,b}` as ALTERNATION (verified directly:
|
|
255
|
+
// Bun.Glob("{a,b}.md").match("{a,b}.md") is false and
|
|
256
|
+
// Bun.Glob("{a,b}.md").match("a.md") is true — the opposite of a faithful
|
|
257
|
+
// gitignore translation). Any brace not already preceded by the user's own
|
|
258
|
+
// backslash gets one inserted here, or a `.prheroignore` line containing a
|
|
259
|
+
// real brace would silently change which files are ignored the moment it
|
|
260
|
+
// reached Bun.Glob.
|
|
261
|
+
function escapeBraces(pattern: string): string {
|
|
262
|
+
let out = "";
|
|
263
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
264
|
+
const c = pattern[i];
|
|
265
|
+
if (c === "\\" && i + 1 < pattern.length) {
|
|
266
|
+
out += c + pattern[i + 1];
|
|
267
|
+
i++;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
out += c === "{" || c === "}" ? `\\${c}` : c;
|
|
271
|
+
}
|
|
272
|
+
return out;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export interface IgnoreMatcher {
|
|
276
|
+
// undefined means KEEP (nothing matched, or the last match was negated).
|
|
277
|
+
match(path: string): IgnoreRule | undefined;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Compiles rules into a matcher that decides per PATH by walking every rule
|
|
281
|
+
// IN ORDER and remembering only the LAST one that fired. This is the reason
|
|
282
|
+
// the three duplicated `map(new Bun.Glob) -> .some()` loops this module's
|
|
283
|
+
// consumer used to run had to be consolidated rather than merely renamed:
|
|
284
|
+
// `.some()` over a flat glob list has no notion of order or polarity, so it
|
|
285
|
+
// cannot express "the last matching RULE wins" — it can only express "did
|
|
286
|
+
// ANY glob match", which gives the same answer regardless of rule order and
|
|
287
|
+
// therefore cannot support negation correctly (see the order-swap test in
|
|
288
|
+
// test/ignore-file.test.ts, which is the only test that actually
|
|
289
|
+
// distinguishes this from a `.some()`-based implementation).
|
|
290
|
+
//
|
|
291
|
+
// A rule's OWN glob list is still matched with an inner "any" — that is a
|
|
292
|
+
// different axis (one rule's dual emission, e.g. bare-form OR
|
|
293
|
+
// contents-form), not the cross-rule ordering this function exists to get
|
|
294
|
+
// right.
|
|
295
|
+
export function compileIgnoreRules(
|
|
296
|
+
rules: readonly IgnoreRule[],
|
|
297
|
+
): IgnoreMatcher {
|
|
298
|
+
const compiled = rules.map((rule) => ({
|
|
299
|
+
rule,
|
|
300
|
+
globs: rule.globs.map((pattern) => new Bun.Glob(pattern)),
|
|
301
|
+
}));
|
|
302
|
+
return {
|
|
303
|
+
match(path: string): IgnoreRule | undefined {
|
|
304
|
+
let winner: IgnoreRule | undefined;
|
|
305
|
+
for (const { rule, globs } of compiled) {
|
|
306
|
+
if (globs.some((glob) => glob.match(path))) {
|
|
307
|
+
winner = rule.negated ? undefined : rule;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return winner;
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// `git ls-tree --full-tree <sha> -- <ignore file>`, parsed. Callers use this
|
|
316
|
+
// to discriminate "absent at this ref" (normal, defaults apply) from
|
|
317
|
+
// "present but not a plain file" (a hazard: a root SYMLINK commits as mode
|
|
318
|
+
// `120000 blob <sha>`, and `cat-file blob` on it returns the link TARGET —
|
|
319
|
+
// e.g. `/etc/passwd` — which would then be parsed as ignore patterns if the
|
|
320
|
+
// mode were not checked). Only `100644`/`100755` are accepted; `120000`
|
|
321
|
+
// (symlink), `040000` (tree/submodule-as-directory) and `160000` (gitlink)
|
|
322
|
+
// are all rejected.
|
|
323
|
+
export function parseIgnoreLsTree(
|
|
324
|
+
stdout: string,
|
|
325
|
+
):
|
|
326
|
+
| { kind: "absent" }
|
|
327
|
+
| { kind: "blob"; sha: string }
|
|
328
|
+
| { kind: "reject"; mode: string } {
|
|
329
|
+
const trimmed = stdout.trim();
|
|
330
|
+
if (trimmed.length === 0) return { kind: "absent" };
|
|
331
|
+
const tabIndex = trimmed.indexOf("\t");
|
|
332
|
+
const meta = tabIndex === -1 ? trimmed : trimmed.slice(0, tabIndex);
|
|
333
|
+
const [mode = "", , sha = ""] = meta.split(/\s+/);
|
|
334
|
+
if (mode === "100644" || mode === "100755") return { kind: "blob", sha };
|
|
335
|
+
return { kind: "reject", mode };
|
|
336
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// The ignore file I/O shell (`.cryskignore`, else the legacy `.prheroignore`). `ignore-file.ts` beside it is the PURE
|
|
2
|
+
// half — dialect, translation, matching — and this module is the part that
|
|
3
|
+
// touches a filesystem.
|
|
4
|
+
//
|
|
5
|
+
// It exists as its OWN module rather than living in cli.ts because
|
|
6
|
+
// `pr-hero watch` needs the same read, and importing it from cli.ts made
|
|
7
|
+
// cli.ts and watch/watch.ts import EACH OTHER (cli.ts already imports
|
|
8
|
+
// `watchCommand` from watch/watch.ts for dispatch). That cycle happened to work,
|
|
9
|
+
// but only because the binding was an `export async function` DECLARATION:
|
|
10
|
+
// function declarations are hoisted and bound at link time, so watch/watch.ts
|
|
11
|
+
// could reference it at its own top level before cli.ts's body ran. Convert
|
|
12
|
+
// it to `export const … = async () => …` and the same code crashes at
|
|
13
|
+
// import time with a ReferenceError, in production only — watch/watch.ts is the
|
|
14
|
+
// module required first in the real dispatch path, and test import order
|
|
15
|
+
// need not reproduce that.
|
|
16
|
+
//
|
|
17
|
+
// A load-bearing invariant that no test enforces and only a comment
|
|
18
|
+
// protects is the kind that gets refactored away by someone doing something
|
|
19
|
+
// else entirely. Breaking the cycle costs one small module and removes the
|
|
20
|
+
// invariant instead of documenting it.
|
|
21
|
+
//
|
|
22
|
+
// The CI counterpart (`readBaseRefIgnoreRules`) lives in `#git/git.ts`: it
|
|
23
|
+
// needs that module's own `GitRunner` plumbing, and the git domain owns the
|
|
24
|
+
// CI flow's base-ref read. Both readers share the dialect and the error
|
|
25
|
+
// re-contextualizing below, which is the part that must not drift.
|
|
26
|
+
|
|
27
|
+
import { stat } from "node:fs/promises";
|
|
28
|
+
import path from "node:path";
|
|
29
|
+
import {
|
|
30
|
+
IGNORE_FILE_NAME,
|
|
31
|
+
LEGACY_IGNORE_FILE_NAME,
|
|
32
|
+
resolveIgnoreFileName,
|
|
33
|
+
} from "./brand-paths";
|
|
34
|
+
import { CliError } from "./errors";
|
|
35
|
+
import {
|
|
36
|
+
IgnoreFileError,
|
|
37
|
+
type IgnoreRule,
|
|
38
|
+
parseIgnoreFile,
|
|
39
|
+
} from "./ignore-file";
|
|
40
|
+
|
|
41
|
+
export interface IgnoreFileReadResult {
|
|
42
|
+
rules: IgnoreRule[];
|
|
43
|
+
found: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// `IgnoreFileError` carries a default file name (see ignore-file.ts) because
|
|
47
|
+
// that pure parser never sees which PHYSICAL path it was asked to read — a working-tree file and a base-ref blob share
|
|
48
|
+
// the same dialect and the same parser. Every caller here re-contextualizes
|
|
49
|
+
// the message with whichever path it actually resolved, so the abort names
|
|
50
|
+
// something a human can go look at (local: a real filesystem path; CI: a
|
|
51
|
+
// `<sha>:<ignore file>` locator, since there is no working-tree path to
|
|
52
|
+
// name).
|
|
53
|
+
export function reContextualizeIgnoreError(
|
|
54
|
+
error: IgnoreFileError,
|
|
55
|
+
resolvedLocation: string,
|
|
56
|
+
): string {
|
|
57
|
+
return error.message.replace(
|
|
58
|
+
new RegExp(`^(?:${IGNORE_FILE_NAME}|${LEGACY_IGNORE_FILE_NAME}):`),
|
|
59
|
+
`${resolvedLocation}:`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Local working-tree read — local review, and PR review without --ci (O-8:
|
|
64
|
+
// always `operatorRoot`, never `worktreePath`; see design D3/O-8 and
|
|
65
|
+
// isCiEnvironment's own WHY at cli.ts's isCi read sites).
|
|
66
|
+
//
|
|
67
|
+
// Absent is NOT an error (`.exists()`-style check would suffice for that
|
|
68
|
+
// alone), but `Bun.file(...).exists()` ALSO reports false for a directory —
|
|
69
|
+
// verified directly — so an absent-vs-directory distinction needs `stat`,
|
|
70
|
+
// not `.exists()`. A directory named like the ignore file, or one this process
|
|
71
|
+
// cannot read, must abort loudly and must NEVER be treated as "absent,
|
|
72
|
+
// defaults apply": that silent equivalence is exactly the kind of quiet
|
|
73
|
+
// partial review this whole design exists to prevent.
|
|
74
|
+
export async function readLocalIgnoreRules(
|
|
75
|
+
root: string,
|
|
76
|
+
): Promise<IgnoreFileReadResult> {
|
|
77
|
+
// .cryskignore first, then the legacy .prheroignore. A .cryskignore that
|
|
78
|
+
// exists but is unusable aborts; it never falls through to the old file.
|
|
79
|
+
const ignoreName = resolveIgnoreFileName(root);
|
|
80
|
+
const ignorePath = path.join(root, ignoreName);
|
|
81
|
+
let fileStat: Awaited<ReturnType<typeof stat>>;
|
|
82
|
+
try {
|
|
83
|
+
fileStat = await stat(ignorePath);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
86
|
+
return { rules: [], found: false };
|
|
87
|
+
}
|
|
88
|
+
throw new CliError(
|
|
89
|
+
`${ignoreName} at ${ignorePath} could not be accessed: ` +
|
|
90
|
+
`${(error as Error).message}`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
if (!fileStat.isFile()) {
|
|
94
|
+
throw new CliError(
|
|
95
|
+
`${ignoreName} at ${ignorePath} is not a regular file (found a ` +
|
|
96
|
+
"directory or special file); refusing to read it as ignore rules",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
let text: string;
|
|
100
|
+
try {
|
|
101
|
+
text = await Bun.file(ignorePath).text();
|
|
102
|
+
} catch (error) {
|
|
103
|
+
// The `stat` above already proved it is a regular file, so a read
|
|
104
|
+
// failure here is something else — most likely permission denied.
|
|
105
|
+
throw new CliError(
|
|
106
|
+
`${ignoreName} at ${ignorePath} could not be read: ` +
|
|
107
|
+
`${(error as Error).message}`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
return { rules: parseIgnoreFile(text, "user", ignoreName), found: true };
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (error instanceof IgnoreFileError) {
|
|
114
|
+
throw new CliError(reContextualizeIgnoreError(error, ignorePath));
|
|
115
|
+
}
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// pr-hero — a multi-agent PR-review engine (hunters → dedupe → refuter → tier).
|
|
2
|
+
// Public API surface; the deep-review lab consumes exactly these exports.
|
|
3
|
+
|
|
4
|
+
export const ENGINE_NAME = "pr-hero";
|
|
5
|
+
export const ENGINE_VERSION = "0.3.0";
|
|
6
|
+
|
|
7
|
+
// errors and git/refs used to reach this surface only through re-exports in
|
|
8
|
+
// review/preflight.ts; they are exported directly now that those re-exports
|
|
9
|
+
// are gone, so the published symbol set is unchanged.
|
|
10
|
+
export * from "./errors";
|
|
11
|
+
// attemptLogPath/settlementReceiptPath/attemptEvidencePath/FORMAT_RETRY_REMINDER,
|
|
12
|
+
// classifyFailure/isTransientSessionFailure/isTerminalSessionFailure/FailureClass,
|
|
13
|
+
// and SpawnedProcess/ACTIVE_CHILD_PROCS/killAllChildProcesses moved out of
|
|
14
|
+
// review/step-runner.ts into these three leaves (architecture guard C2); exported
|
|
15
|
+
// directly now that step-runner.ts no longer re-exports them, so the published
|
|
16
|
+
// symbol set is unchanged.
|
|
17
|
+
export * from "./execution/failure-classification";
|
|
18
|
+
export * from "./execution/spawned-process";
|
|
19
|
+
export * from "./execution/step-artifacts";
|
|
20
|
+
export * from "./git/refs";
|
|
21
|
+
export * from "./model/routing";
|
|
22
|
+
export * from "./review/dedupe";
|
|
23
|
+
export * from "./review/drafts";
|
|
24
|
+
export * from "./review/findings";
|
|
25
|
+
export * from "./review/findings-conformance";
|
|
26
|
+
export * from "./review/pipeline";
|
|
27
|
+
export * from "./review/preflight";
|
|
28
|
+
export * from "./review/prompt-set";
|
|
29
|
+
export * from "./review/report";
|
|
30
|
+
export * from "./review/root-cause";
|
|
31
|
+
export * from "./review/spec";
|
|
32
|
+
export * from "./review/step-runner";
|
|
33
|
+
export * from "./transport-registry";
|
|
34
|
+
export * from "./usage";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// The pr-hero → Crysk rename renamed the launchd labels (io.prhero.* →
|
|
2
|
+
// io.crysk.*). Installing the new agent must not leave the old one running a
|
|
3
|
+
// second copy, and uninstalling must not leave it behind, so both call this.
|
|
4
|
+
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { rm } from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
|
|
9
|
+
export const LEGACY_WATCH_LAUNCHD_LABEL = "io.prhero.watch";
|
|
10
|
+
export const LEGACY_GC_LAUNCHD_LABEL = "io.prhero.gc";
|
|
11
|
+
|
|
12
|
+
export function legacyPlistPath(home: string, label: string): string {
|
|
13
|
+
return path.join(home, "Library", "LaunchAgents", `${label}.plist`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Unloads (best effort: it may not be loaded) and deletes the old plist.
|
|
17
|
+
// Silent when the old agent was never installed.
|
|
18
|
+
export async function retireLegacyAgent(input: {
|
|
19
|
+
home: string;
|
|
20
|
+
legacyLabel: string;
|
|
21
|
+
run: (cmd: string[]) => Promise<{ ok: boolean; stderr: string }>;
|
|
22
|
+
log: (line: string) => void;
|
|
23
|
+
}): Promise<void> {
|
|
24
|
+
const plist = legacyPlistPath(input.home, input.legacyLabel);
|
|
25
|
+
if (!existsSync(plist)) return;
|
|
26
|
+
await input.run(["launchctl", "unload", "-w", plist]);
|
|
27
|
+
await rm(plist, { force: true });
|
|
28
|
+
input.log(`removed legacy ${input.legacyLabel} agent (${plist})`);
|
|
29
|
+
}
|
package/src/mcp/mcp.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// Read-only MCP server (Fundamentals #6 / observability-canonical-store.md),
|
|
2
|
+
// impure half: stdio line reader, JSON-RPC event loop, and store client binding.
|
|
3
|
+
//
|
|
4
|
+
// Invariant: The MCP server is strictly read-only. All data access routes through
|
|
5
|
+
// ProductStoreClient.
|
|
6
|
+
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import os from "node:os";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import readline from "node:readline";
|
|
11
|
+
import { ProductStoreClient } from "#server/client";
|
|
12
|
+
import {
|
|
13
|
+
type StoreServerHandle,
|
|
14
|
+
startProductStoreServer,
|
|
15
|
+
} from "#server/server";
|
|
16
|
+
import { prheroLayout } from "../home-preflight";
|
|
17
|
+
import { type JsonRpcRequest, processMcpMessage } from "./preflight";
|
|
18
|
+
|
|
19
|
+
export interface McpServerOptions {
|
|
20
|
+
socketPath?: string;
|
|
21
|
+
dbPath?: string;
|
|
22
|
+
client?: ProductStoreClient;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function runMcpServer(
|
|
26
|
+
options: McpServerOptions = {},
|
|
27
|
+
): Promise<void> {
|
|
28
|
+
let client = options.client;
|
|
29
|
+
let embeddedServer: StoreServerHandle | null = null;
|
|
30
|
+
|
|
31
|
+
if (!client) {
|
|
32
|
+
if (options.socketPath) {
|
|
33
|
+
// Test if an existing store server is already alive on this socket
|
|
34
|
+
const probeClient = new ProductStoreClient({
|
|
35
|
+
socketPath: options.socketPath,
|
|
36
|
+
});
|
|
37
|
+
try {
|
|
38
|
+
await probeClient.health();
|
|
39
|
+
// Socket is alive! Reuse existing server without starting an embedded one or hijacking
|
|
40
|
+
client = probeClient;
|
|
41
|
+
} catch {
|
|
42
|
+
const lockPath = `${options.socketPath}.lock`;
|
|
43
|
+
try {
|
|
44
|
+
fs.openSync(lockPath, "wx");
|
|
45
|
+
} catch {
|
|
46
|
+
// Another process may be starting the server; poll for health
|
|
47
|
+
let healthy = false;
|
|
48
|
+
for (let i = 0; i < 20; i++) {
|
|
49
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
50
|
+
try {
|
|
51
|
+
await probeClient.health();
|
|
52
|
+
healthy = true;
|
|
53
|
+
break;
|
|
54
|
+
} catch {}
|
|
55
|
+
}
|
|
56
|
+
if (healthy) {
|
|
57
|
+
client = probeClient;
|
|
58
|
+
} else {
|
|
59
|
+
// Lock was stale or process died; reclaim lock
|
|
60
|
+
try {
|
|
61
|
+
fs.unlinkSync(lockPath);
|
|
62
|
+
} catch {}
|
|
63
|
+
try {
|
|
64
|
+
fs.openSync(lockPath, "wx");
|
|
65
|
+
} catch {}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!client) {
|
|
70
|
+
// Socket is absent or stale; start embedded server on this socket path
|
|
71
|
+
const layout = prheroLayout(os.homedir());
|
|
72
|
+
const dbPath = options.dbPath ?? layout.prheroDbPath;
|
|
73
|
+
embeddedServer = startProductStoreServer({
|
|
74
|
+
dbPath,
|
|
75
|
+
socketPath: options.socketPath,
|
|
76
|
+
});
|
|
77
|
+
client = probeClient;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
// Default: create a dedicated ephemeral socket for this MCP process
|
|
82
|
+
const layout = prheroLayout(os.homedir());
|
|
83
|
+
const socketPath = path.join(
|
|
84
|
+
os.tmpdir(),
|
|
85
|
+
`prhero-mcp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.sock`,
|
|
86
|
+
);
|
|
87
|
+
const dbPath = options.dbPath ?? layout.prheroDbPath;
|
|
88
|
+
embeddedServer = startProductStoreServer({
|
|
89
|
+
dbPath,
|
|
90
|
+
socketPath,
|
|
91
|
+
});
|
|
92
|
+
client = new ProductStoreClient({ socketPath });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const rl = readline.createInterface({
|
|
97
|
+
input: process.stdin,
|
|
98
|
+
output: process.stdout,
|
|
99
|
+
terminal: false,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const cleanup = () => {
|
|
103
|
+
rl.close();
|
|
104
|
+
if (embeddedServer) {
|
|
105
|
+
try {
|
|
106
|
+
embeddedServer.stop();
|
|
107
|
+
} catch {}
|
|
108
|
+
if (options.socketPath) {
|
|
109
|
+
try {
|
|
110
|
+
fs.unlinkSync(`${options.socketPath}.lock`);
|
|
111
|
+
} catch {}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
process.on("SIGINT", () => {
|
|
117
|
+
cleanup();
|
|
118
|
+
process.exit(0);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
process.on("SIGTERM", () => {
|
|
122
|
+
cleanup();
|
|
123
|
+
process.exit(0);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
for await (const line of rl) {
|
|
127
|
+
const trimmed = line.trim();
|
|
128
|
+
if (!trimmed) continue;
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
const request = JSON.parse(trimmed) as JsonRpcRequest;
|
|
132
|
+
const response = await processMcpMessage(client, request);
|
|
133
|
+
if (response !== null) {
|
|
134
|
+
process.stdout.write(`${JSON.stringify(response)}\n`);
|
|
135
|
+
}
|
|
136
|
+
} catch (err) {
|
|
137
|
+
const errorResponse = {
|
|
138
|
+
jsonrpc: "2.0",
|
|
139
|
+
id: null,
|
|
140
|
+
error: {
|
|
141
|
+
code: -32700,
|
|
142
|
+
message: `Parse error: ${(err as Error).message}`,
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
process.stdout.write(`${JSON.stringify(errorResponse)}\n`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
cleanup();
|
|
150
|
+
}
|