@flyingrobots/graft 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +218 -0
  2. package/LICENSE +190 -0
  3. package/NOTICE +4 -0
  4. package/README.md +119 -0
  5. package/bin/graft.js +11 -0
  6. package/docs/GUIDE.md +374 -0
  7. package/package.json +76 -0
  8. package/src/adapters/canonical-json.ts +56 -0
  9. package/src/adapters/node-fs.ts +39 -0
  10. package/src/git/diff.ts +96 -0
  11. package/src/guards/stream-boundary.ts +110 -0
  12. package/src/hooks/posttooluse-read.ts +107 -0
  13. package/src/hooks/pretooluse-read.ts +88 -0
  14. package/src/hooks/shared.ts +168 -0
  15. package/src/mcp/cache.ts +94 -0
  16. package/src/mcp/cached-file.ts +38 -0
  17. package/src/mcp/context.ts +52 -0
  18. package/src/mcp/metrics.ts +53 -0
  19. package/src/mcp/receipt.ts +83 -0
  20. package/src/mcp/server.ts +166 -0
  21. package/src/mcp/stdio.ts +6 -0
  22. package/src/mcp/tools/budget.ts +20 -0
  23. package/src/mcp/tools/changed-since.ts +68 -0
  24. package/src/mcp/tools/doctor.ts +20 -0
  25. package/src/mcp/tools/explain.ts +80 -0
  26. package/src/mcp/tools/file-outline.ts +57 -0
  27. package/src/mcp/tools/graft-diff.ts +24 -0
  28. package/src/mcp/tools/read-range.ts +21 -0
  29. package/src/mcp/tools/run-capture.ts +67 -0
  30. package/src/mcp/tools/safe-read.ts +135 -0
  31. package/src/mcp/tools/state.ts +30 -0
  32. package/src/mcp/tools/stats.ts +20 -0
  33. package/src/metrics/logger.ts +69 -0
  34. package/src/metrics/types.ts +12 -0
  35. package/src/operations/file-outline.ts +38 -0
  36. package/src/operations/graft-diff.ts +117 -0
  37. package/src/operations/read-range.ts +65 -0
  38. package/src/operations/safe-read.ts +96 -0
  39. package/src/operations/state.ts +33 -0
  40. package/src/parser/diff.ts +142 -0
  41. package/src/parser/lang.ts +12 -0
  42. package/src/parser/outline.ts +327 -0
  43. package/src/parser/types.ts +67 -0
  44. package/src/policy/evaluate.ts +178 -0
  45. package/src/policy/graftignore.ts +6 -0
  46. package/src/policy/types.ts +86 -0
  47. package/src/ports/codec.ts +13 -0
  48. package/src/ports/filesystem.ts +17 -0
  49. package/src/session/tracker.ts +114 -0
  50. package/src/session/types.ts +20 -0
@@ -0,0 +1,178 @@
1
+ import picomatch from "picomatch";
2
+ import type { PolicyInput, PolicyOptions, ReasonCode, SessionDepth } from "./types.js";
3
+ import { ContentResult, OutlineResult, RefusedResult } from "./types.js";
4
+ import type { PolicyResult } from "./types.js";
5
+
6
+ export const STATIC_THRESHOLDS = { lines: 150, bytes: 12288 } as const;
7
+
8
+ const SESSION_BYTE_CAPS: Record<string, number> = {
9
+ early: 20480,
10
+ mid: 10240,
11
+ late: 4096,
12
+ };
13
+
14
+ const BINARY_EXTENSIONS = new Set([
15
+ ".png", ".jpg", ".jpeg", ".gif", ".pdf", ".zip",
16
+ ".wasm", ".bin", ".sqlite", ".mp4", ".mov", ".ico",
17
+ ]);
18
+
19
+ const LOCKFILE_NAMES = new Set([
20
+ "package-lock.json", "pnpm-lock.yaml", "yarn.lock",
21
+ "Gemfile.lock", "poetry.lock", "Cargo.lock",
22
+ "composer.lock", "Pipfile.lock",
23
+ ]);
24
+
25
+ const BUILD_OUTPUT_PREFIXES = ["dist/", "build/", ".next/", "out/", "target/"];
26
+
27
+ const SECRET_PATTERNS: ((name: string) => boolean)[] = [
28
+ (n) => n === ".env" || (n.startsWith(".env.") && !n.endsWith(".example")),
29
+ (n) => n.endsWith(".pem"),
30
+ (n) => n.endsWith(".key"),
31
+ (n) => n.startsWith("credentials."),
32
+ ];
33
+
34
+ function basename(path: string): string {
35
+ const i = path.lastIndexOf("/");
36
+ return i === -1 ? path : path.slice(i + 1);
37
+ }
38
+
39
+ function extname(path: string): string {
40
+ const name = basename(path);
41
+ const i = name.lastIndexOf(".");
42
+ return i <= 0 ? "" : name.slice(i);
43
+ }
44
+
45
+ function checkBan(path: string): { reason: ReasonCode; reasonDetail: string; next: string[] } | undefined {
46
+ const name = basename(path);
47
+ const ext = extname(path).toLowerCase();
48
+
49
+ if (BINARY_EXTENSIONS.has(ext)) {
50
+ return {
51
+ reason: "BINARY",
52
+ reasonDetail: `Binary file (${ext}) cannot be usefully read`,
53
+ next: ["Use file_outline to see metadata", "Check for a text alternative"],
54
+ };
55
+ }
56
+
57
+ if (LOCKFILE_NAMES.has(name)) {
58
+ return {
59
+ reason: "LOCKFILE",
60
+ reasonDetail: `Lockfile ${name} is machine-generated and not useful to read`,
61
+ next: ["Read package.json for dependency info instead"],
62
+ };
63
+ }
64
+
65
+ if (name.endsWith(".min.js") || name.endsWith(".min.css")) {
66
+ return {
67
+ reason: "MINIFIED",
68
+ reasonDetail: `Minified file ${name} is not human-readable`,
69
+ next: ["Look for the unminified source"],
70
+ };
71
+ }
72
+
73
+ for (const prefix of BUILD_OUTPUT_PREFIXES) {
74
+ if (path.includes("/" + prefix) || path.startsWith(prefix)) {
75
+ return {
76
+ reason: "BUILD_OUTPUT",
77
+ reasonDetail: `Build output path ${prefix} contains generated files`,
78
+ next: ["Try reading src/ instead of dist/"],
79
+ };
80
+ }
81
+ }
82
+
83
+ for (const check of SECRET_PATTERNS) {
84
+ if (check(name)) {
85
+ return {
86
+ reason: "SECRET",
87
+ reasonDetail: `${name} may contain secrets and should not be read`,
88
+ next: ["Check for a .example or template version"],
89
+ };
90
+ }
91
+ }
92
+
93
+ return undefined;
94
+ }
95
+
96
+ export function evaluatePolicy(input: PolicyInput, options?: PolicyOptions): PolicyResult {
97
+ const { path, lines, bytes } = input;
98
+ const actual = { lines, bytes };
99
+ const thresholds = { ...STATIC_THRESHOLDS };
100
+ const sessionDepth: SessionDepth | undefined = options?.sessionDepth;
101
+
102
+ // 1. Bans first
103
+ const ban = checkBan(path);
104
+ if (ban !== undefined) {
105
+ return new RefusedResult({
106
+ reason: ban.reason,
107
+ reasonDetail: ban.reasonDetail,
108
+ next: ban.next,
109
+ thresholds,
110
+ actual,
111
+ ...(sessionDepth !== undefined ? { sessionDepth } : {}),
112
+ });
113
+ }
114
+
115
+ // 2. Graftignore
116
+ const patterns = options?.graftignorePatterns;
117
+ if (patterns !== undefined && patterns.length > 0) {
118
+ const isMatch = picomatch(patterns);
119
+ if (isMatch(path)) {
120
+ return new RefusedResult({
121
+ reason: "GRAFTIGNORE",
122
+ reasonDetail: "File matches .graftignore pattern",
123
+ next: ["Check .graftignore if this file should be readable"],
124
+ thresholds,
125
+ actual,
126
+ ...(sessionDepth !== undefined ? { sessionDepth } : {}),
127
+ });
128
+ }
129
+ }
130
+
131
+ // 3. Determine effective byte cap
132
+ // When session depth is known (not "unknown"), the session cap replaces the static byte threshold.
133
+ // Static line threshold always applies.
134
+ const hasSessionCap = sessionDepth !== undefined && sessionDepth !== "unknown";
135
+ let effectiveByteCap = hasSessionCap
136
+ ? SESSION_BYTE_CAPS[sessionDepth] ?? STATIC_THRESHOLDS.bytes
137
+ : STATIC_THRESHOLDS.bytes;
138
+
139
+ // 4. Budget cap — no single read should exceed 5% of remaining budget
140
+ const MAX_READ_FRACTION = 0.05;
141
+ const budgetRemaining = options?.budgetRemaining;
142
+ const hasBudgetCap = budgetRemaining !== undefined && budgetRemaining >= 0;
143
+ let budgetTriggered = false;
144
+ if (hasBudgetCap) {
145
+ const budgetCap = Math.floor(budgetRemaining * MAX_READ_FRACTION);
146
+ if (budgetCap < effectiveByteCap) {
147
+ effectiveByteCap = budgetCap;
148
+ budgetTriggered = true;
149
+ }
150
+ }
151
+
152
+ const exceedsLines = lines > STATIC_THRESHOLDS.lines;
153
+ const exceedsBytes = bytes > effectiveByteCap;
154
+
155
+ if (!exceedsLines && !exceedsBytes) {
156
+ return new ContentResult({
157
+ thresholds,
158
+ actual,
159
+ ...(sessionDepth !== undefined ? { sessionDepth } : {}),
160
+ });
161
+ }
162
+
163
+ // Determine reason: BUDGET_CAP > SESSION_CAP > OUTLINE
164
+ const finalReason: "OUTLINE" | "SESSION_CAP" | "BUDGET_CAP" = exceedsLines && !exceedsBytes
165
+ ? "OUTLINE"
166
+ : exceedsBytes && budgetTriggered
167
+ ? "BUDGET_CAP"
168
+ : exceedsBytes && hasSessionCap
169
+ ? "SESSION_CAP"
170
+ : "OUTLINE";
171
+
172
+ return new OutlineResult({
173
+ reason: finalReason,
174
+ thresholds,
175
+ actual,
176
+ ...(sessionDepth !== undefined ? { sessionDepth } : {}),
177
+ });
178
+ }
@@ -0,0 +1,6 @@
1
+ export function loadGraftignore(content: string): string[] {
2
+ return content
3
+ .split("\n")
4
+ .map((line) => line.trim())
5
+ .filter((line) => line.length > 0 && !line.startsWith("#"));
6
+ }
@@ -0,0 +1,86 @@
1
+ export type ReasonCode =
2
+ | "CONTENT"
3
+ | "OUTLINE"
4
+ | "SESSION_CAP"
5
+ | "BINARY"
6
+ | "LOCKFILE"
7
+ | "MINIFIED"
8
+ | "BUILD_OUTPUT"
9
+ | "SECRET"
10
+ | "GRAFTIGNORE"
11
+ | "BUDGET_CAP";
12
+
13
+ export type SessionDepth = "early" | "mid" | "late" | "unknown";
14
+
15
+ export interface PolicyInput {
16
+ path: string;
17
+ lines: number;
18
+ bytes: number;
19
+ }
20
+
21
+ export interface PolicyOptions {
22
+ graftignorePatterns?: string[] | undefined;
23
+ sessionDepth?: SessionDepth | undefined;
24
+ budgetRemaining?: number | undefined;
25
+ }
26
+
27
+ // Shared fields for all results
28
+ interface PolicyResultBase {
29
+ readonly thresholds: { readonly lines: number; readonly bytes: number };
30
+ readonly actual: { readonly lines: number; readonly bytes: number };
31
+ readonly sessionDepth?: SessionDepth | undefined;
32
+ }
33
+
34
+ export class ContentResult implements PolicyResultBase {
35
+ readonly projection = "content" as const;
36
+ readonly reason = "CONTENT" as const;
37
+ readonly thresholds: { readonly lines: number; readonly bytes: number };
38
+ readonly actual: { readonly lines: number; readonly bytes: number };
39
+ readonly sessionDepth?: SessionDepth | undefined;
40
+
41
+ constructor(opts: { thresholds: { lines: number; bytes: number }; actual: { lines: number; bytes: number }; sessionDepth?: SessionDepth | undefined }) {
42
+ this.thresholds = Object.freeze({ ...opts.thresholds });
43
+ this.actual = Object.freeze({ ...opts.actual });
44
+ if (opts.sessionDepth !== undefined) this.sessionDepth = opts.sessionDepth;
45
+ Object.freeze(this);
46
+ }
47
+ }
48
+
49
+ export class OutlineResult implements PolicyResultBase {
50
+ readonly projection = "outline" as const;
51
+ readonly reason: "OUTLINE" | "SESSION_CAP" | "BUDGET_CAP";
52
+ readonly thresholds: { readonly lines: number; readonly bytes: number };
53
+ readonly actual: { readonly lines: number; readonly bytes: number };
54
+ readonly sessionDepth?: SessionDepth | undefined;
55
+
56
+ constructor(opts: { reason: "OUTLINE" | "SESSION_CAP" | "BUDGET_CAP"; thresholds: { lines: number; bytes: number }; actual: { lines: number; bytes: number }; sessionDepth?: SessionDepth | undefined }) {
57
+ this.reason = opts.reason;
58
+ this.thresholds = Object.freeze({ ...opts.thresholds });
59
+ this.actual = Object.freeze({ ...opts.actual });
60
+ if (opts.sessionDepth !== undefined) this.sessionDepth = opts.sessionDepth;
61
+ Object.freeze(this);
62
+ }
63
+ }
64
+
65
+ export class RefusedResult implements PolicyResultBase {
66
+ readonly projection = "refused" as const;
67
+ readonly reason: ReasonCode;
68
+ readonly reasonDetail: string;
69
+ readonly next: readonly string[];
70
+ readonly thresholds: { readonly lines: number; readonly bytes: number };
71
+ readonly actual: { readonly lines: number; readonly bytes: number };
72
+ readonly sessionDepth?: SessionDepth | undefined;
73
+
74
+ constructor(opts: { reason: ReasonCode; reasonDetail: string; next: string[]; thresholds: { lines: number; bytes: number }; actual: { lines: number; bytes: number }; sessionDepth?: SessionDepth | undefined }) {
75
+ this.reason = opts.reason;
76
+ this.reasonDetail = opts.reasonDetail;
77
+ this.next = Object.freeze([...opts.next]);
78
+ this.thresholds = Object.freeze({ ...opts.thresholds });
79
+ this.actual = Object.freeze({ ...opts.actual });
80
+ if (opts.sessionDepth !== undefined) this.sessionDepth = opts.sessionDepth;
81
+ Object.freeze(this);
82
+ }
83
+ }
84
+
85
+ // Union type for callers that need the general type
86
+ export type PolicyResult = ContentResult | OutlineResult | RefusedResult;
@@ -0,0 +1,13 @@
1
+ // ---------------------------------------------------------------------------
2
+ // JsonCodec port — hexagonal boundary for JSON serialization
3
+ // ---------------------------------------------------------------------------
4
+
5
+ /**
6
+ * Portable JSON codec interface. Core logic imports this port, not
7
+ * JSON.stringify/JSON.parse directly. Implementations control key
8
+ * ordering, whitespace, and determinism.
9
+ */
10
+ export interface JsonCodec {
11
+ encode(value: unknown): string;
12
+ decode(data: string): unknown;
13
+ }
@@ -0,0 +1,17 @@
1
+ // ---------------------------------------------------------------------------
2
+ // FileSystem port — hexagonal boundary for file I/O
3
+ // ---------------------------------------------------------------------------
4
+
5
+ /**
6
+ * Portable filesystem interface. Core logic imports this port, not node:fs.
7
+ * Node adapter implements it; tests can substitute a mock.
8
+ */
9
+ export interface FileSystem {
10
+ readFile(path: string, encoding: "utf-8"): Promise<string>;
11
+ readFile(path: string): Promise<Buffer>;
12
+ writeFile(path: string, data: string, encoding: "utf-8"): Promise<void>;
13
+ appendFile(path: string, data: string, encoding: "utf-8"): Promise<void>;
14
+ mkdir(path: string, options: { recursive: true }): Promise<void>;
15
+ stat(path: string): Promise<{ size: number }>;
16
+ readFileSync(path: string, encoding: "utf-8"): string;
17
+ }
@@ -0,0 +1,114 @@
1
+ import { Tripwire } from "./types.js";
2
+ import type { SessionDepth } from "./types.js";
3
+
4
+ const EDIT_BASH_TOOLS = new Set(["Edit", "Bash"]);
5
+ const LATE_READ_BYTE_THRESHOLD = 20480;
6
+ const LATE_READ_MESSAGE_THRESHOLD = 300;
7
+
8
+ export class SessionTracker {
9
+ private totalMessages = 0;
10
+ private toolCallsSinceUser = 0;
11
+ private editBashTransitions = 0;
12
+ private lastEditBashTool: string | null = null;
13
+ private budgetBytes: number | null = null;
14
+ private consumedBytes = 0;
15
+
16
+ getMessageCount(): number {
17
+ return this.totalMessages;
18
+ }
19
+
20
+ recordMessage(): void {
21
+ this.totalMessages++;
22
+ }
23
+
24
+ recordToolCall(toolName: string): void {
25
+ this.toolCallsSinceUser++;
26
+
27
+ if (EDIT_BASH_TOOLS.has(toolName)) {
28
+ // Count full Edit→Bash cycles, not individual alternations.
29
+ // Each Edit followed by Bash is one cycle.
30
+ if (this.lastEditBashTool === "Edit" && toolName === "Bash") {
31
+ this.editBashTransitions++;
32
+ }
33
+ this.lastEditBashTool = toolName;
34
+ }
35
+ }
36
+
37
+ recordUserMessage(): void {
38
+ this.toolCallsSinceUser = 0;
39
+ }
40
+
41
+ checkTripwires(): Tripwire[] {
42
+ const wires: Tripwire[] = [];
43
+
44
+ if (this.totalMessages > 500) {
45
+ wires.push(new Tripwire({
46
+ signal: "SESSION_LONG",
47
+ recommendation:
48
+ "Session exceeds 500 messages. Use state_save to persist context and start a new session.",
49
+ }));
50
+ }
51
+
52
+ if (this.editBashTransitions > 30) {
53
+ wires.push(new Tripwire({
54
+ signal: "EDIT_BASH_LOOP",
55
+ recommendation:
56
+ "Detected repeated edit/bash cycling. Step back and rethink the approach.",
57
+ }));
58
+ }
59
+
60
+ if (this.toolCallsSinceUser > 80) {
61
+ wires.push(new Tripwire({
62
+ signal: "RUNAWAY_TOOLS",
63
+ recommendation:
64
+ "Over 80 tool calls without user input. Pause and check in with the user.",
65
+ }));
66
+ }
67
+
68
+ return wires;
69
+ }
70
+
71
+ checkLateRead(outputBytes: number): Tripwire | null {
72
+ if (
73
+ outputBytes > LATE_READ_BYTE_THRESHOLD &&
74
+ this.totalMessages > LATE_READ_MESSAGE_THRESHOLD
75
+ ) {
76
+ return new Tripwire({
77
+ signal: "LATE_LARGE_READ",
78
+ recommendation:
79
+ "Large read late in session. Use file_outline or read_range instead.",
80
+ });
81
+ }
82
+ return null;
83
+ }
84
+
85
+ setBudget(bytes: number): void {
86
+ if (bytes <= 0) throw new Error("Budget must be positive");
87
+ this.budgetBytes = bytes;
88
+ }
89
+
90
+ recordBytesConsumed(bytes: number): void {
91
+ this.consumedBytes += bytes;
92
+ }
93
+
94
+ getBudget(): { total: number; consumed: number; remaining: number; fraction: number } | null {
95
+ if (this.budgetBytes === null) return null;
96
+ const remaining = Math.max(0, this.budgetBytes - this.consumedBytes);
97
+ return {
98
+ total: this.budgetBytes,
99
+ consumed: this.consumedBytes,
100
+ remaining,
101
+ fraction: Math.round((this.consumedBytes / this.budgetBytes) * 1000) / 1000,
102
+ };
103
+ }
104
+
105
+ getSessionDepth(): SessionDepth {
106
+ if (this.totalMessages < 100) {
107
+ return "early";
108
+ }
109
+ if (this.totalMessages <= 500) {
110
+ return "mid";
111
+ }
112
+ return "late";
113
+ }
114
+ }
@@ -0,0 +1,20 @@
1
+ export class Tripwire {
2
+ /** @internal */
3
+ private readonly _brand = "Tripwire" as const;
4
+ readonly signal: string;
5
+ readonly recommendation: string;
6
+
7
+ constructor(opts: { signal: string; recommendation: string }) {
8
+ if (opts.signal.trim().length === 0) {
9
+ throw new Error("Tripwire: signal must be non-empty");
10
+ }
11
+ if (opts.recommendation.trim().length === 0) {
12
+ throw new Error("Tripwire: recommendation must be non-empty");
13
+ }
14
+ this.signal = opts.signal.trim();
15
+ this.recommendation = opts.recommendation.trim();
16
+ Object.freeze(this);
17
+ }
18
+ }
19
+
20
+ export type SessionDepth = "early" | "mid" | "late";