Skip to content

Commit 884a63d

Browse files
committed
Use RTK automatically when rtk is on PATH
Flip resolveRtkPrefix from opt-in to auto-detect: an installed rtk on PATH is used by default. POSTHOG_RTK now only overrides — "0"/"false" opts out even when rtk is installed, and any other value is still an explicit binary path. When rtk is not on PATH the resolver returns undefined and the hook is never registered, so there is no behavior change on machines without rtk. Generated-By: PostHog Code Task-Id: 2cfc5453-0307-4c67-a6eb-6456bc96cf1a
1 parent 555dbd3 commit 884a63d

2 files changed

Lines changed: 43 additions & 31 deletions

File tree

packages/agent/src/adapters/claude/session/rtk.test.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,30 @@ describe("resolveRtkPrefix", () => {
8686
});
8787

8888
test.each([
89-
["undefined", undefined],
89+
["unset", undefined],
9090
["empty", ""],
91-
["zero", "0"],
92-
["false", "false"],
93-
["FALSE", "FALSE"],
94-
])("is disabled when POSTHOG_RTK is %s", (_label, value) => {
95-
expect(resolveRtkPrefix({ POSTHOG_RTK: value })).toBeUndefined();
91+
["1", "1"],
92+
["true", "true"],
93+
])("auto-detects rtk on PATH when POSTHOG_RTK is %s", (_label, value) => {
94+
expect(resolveRtkPrefix({ POSTHOG_RTK: value, PATH: dir })).toBe(binary);
9695
});
9796

98-
test("auto-detects rtk on PATH when enabled", () => {
99-
expect(resolveRtkPrefix({ POSTHOG_RTK: "1", PATH: dir })).toBe(binary);
97+
test("returns undefined when rtk is not on PATH", () => {
98+
expect(resolveRtkPrefix({ PATH: "/nonexistent" })).toBeUndefined();
10099
});
101100

102-
test("returns undefined when enabled but rtk is not on PATH", () => {
103-
expect(
104-
resolveRtkPrefix({ POSTHOG_RTK: "true", PATH: "/nonexistent" }),
105-
).toBeUndefined();
106-
});
101+
test.each([
102+
["zero", "0"],
103+
["false", "false"],
104+
["FALSE", "FALSE"],
105+
])(
106+
"opts out when POSTHOG_RTK is %s, even with rtk on PATH",
107+
(_label, value) => {
108+
expect(
109+
resolveRtkPrefix({ POSTHOG_RTK: value, PATH: dir }),
110+
).toBeUndefined();
111+
},
112+
);
107113

108114
test("uses an explicit path that exists", () => {
109115
expect(resolveRtkPrefix({ POSTHOG_RTK: binary })).toBe(binary);

packages/agent/src/adapters/claude/session/rtk.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { gitSubcommand } from "../git-command";
1010
* available we rewrite eligible `Bash` calls to run through it, so the savings
1111
* happen at the source — the verbose output is never generated into context.
1212
*
13-
* Opt-in and dormant by default: nothing changes unless `POSTHOG_RTK` is set.
13+
* Used automatically when `rtk` is on PATH; set `POSTHOG_RTK=0` to opt out.
1414
*/
1515

1616
// Commands RTK compresses faithfully and that have no side effects, so wrapping
@@ -100,27 +100,33 @@ function findOnPath(bin: string, env: NodeJS.ProcessEnv): string | undefined {
100100
}
101101

102102
/**
103-
* Resolves the RTK binary to route shell output through, from `POSTHOG_RTK`:
104-
* unset / "" / "0" / "false" → disabled (undefined)
105-
* "1" / "true" → auto-detect `rtk` on PATH
106-
* any other value → an explicit path to the binary
107-
*
108-
* Opt-in by design: a user who merely has `rtk` installed sees no behavior
109-
* change until they ask for it, since rewriting alters how their commands run.
103+
* Resolves the RTK binary to route shell output through. Auto-detects `rtk` on
104+
* PATH by default, so an installed `rtk` is used automatically. `POSTHOG_RTK`
105+
* overrides:
106+
* unset / "" / "1" / "true" → auto-detect `rtk` on PATH
107+
* "0" / "false" → disabled (opt out)
108+
* any other value → an explicit path to the binary
110109
*/
111110
export function resolveRtkPrefix(env: NodeJS.ProcessEnv): string | undefined {
112111
const raw = env.POSTHOG_RTK?.trim();
113-
if (!raw || raw === "0" || raw.toLowerCase() === "false") return undefined;
114-
if (raw === "1" || raw.toLowerCase() === "true") {
115-
return findOnPath("rtk", env);
116-
}
117-
try {
118-
if (fs.statSync(raw).isFile()) return raw;
119-
} catch {
120-
// Explicit path doesn't exist — treat as disabled rather than emit a
121-
// command that would fail with "rtk: not found".
112+
const lowered = raw?.toLowerCase();
113+
114+
// Explicit opt-out, even when rtk is installed.
115+
if (lowered === "0" || lowered === "false") return undefined;
116+
117+
// An explicit binary-path override (anything other than a bare enable flag).
118+
if (raw && lowered !== "1" && lowered !== "true") {
119+
try {
120+
if (fs.statSync(raw).isFile()) return raw;
121+
} catch {
122+
// Explicit path doesn't exist — treat as disabled rather than emit a
123+
// command that would fail with "rtk: not found".
124+
}
125+
return undefined;
122126
}
123-
return undefined;
127+
128+
// Default (unset) or explicit enable: use rtk if it is on PATH.
129+
return findOnPath("rtk", env);
124130
}
125131

126132
export const createRtkRewriteHook =

0 commit comments

Comments
 (0)