diff --git a/src/index.ts b/src/index.ts index 4af773d..0dfe600 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,6 +38,11 @@ Kubernetes fields: - ttlSecondsAfterFinished (number, optional): auto-cleanup delay; default 300 - retainJobs (boolean, optional): skip cleanup on completion for debugging +RTK fields (token optimization): +- enableRtk (boolean, optional): enable RTK to reduce token usage by filtering CLI output. Configures Claude Code PreToolUse/PostToolUse hooks automatically via project-level settings. Adds an init container to download the RTK binary. +- rtkVersion (string, optional): RTK version to install; defaults to "latest" +- rtkImage (string, optional): container image for the RTK download init container; defaults to "curlimages/curl:8.12.1" + Operational fields: - timeoutSec (number, optional): run timeout in seconds; 0 means no timeout - graceSec (number, optional): additional grace before adapter gives up after Job deadline diff --git a/src/server/config-schema.ts b/src/server/config-schema.ts index 98265bf..8654f46 100644 --- a/src/server/config-schema.ts +++ b/src/server/config-schema.ts @@ -108,6 +108,26 @@ export function getConfigSchema(): AdapterConfigSchema { label: "Memory Limit", hint: "Memory limit for Job pods (e.g. 128Mi, 512Mi, 1Gi).", }, + // RTK (token optimization) + { + type: "toggle", + key: "enableRtk", + label: "Enable RTK", + hint: "Install and enable RTK (rtk-ai/rtk) to reduce token usage by filtering CLI output through PreToolUse/PostToolUse hooks. Adds an init container to download the RTK binary.", + default: false, + }, + { + type: "text", + key: "rtkVersion", + label: "RTK Version", + hint: "RTK version to install (e.g. '0.5.0'). Defaults to 'latest'.", + }, + { + type: "text", + key: "rtkImage", + label: "RTK Installer Image", + hint: "Container image for the RTK download init container. Defaults to curlimages/curl:8.12.1.", + }, // Scheduling { type: "textarea", diff --git a/src/server/job-manifest.test.ts b/src/server/job-manifest.test.ts index adc18a6..c199ba2 100644 --- a/src/server/job-manifest.test.ts +++ b/src/server/job-manifest.test.ts @@ -497,6 +497,116 @@ describe("buildJobManifest", () => { }); }); + describe("RTK integration", () => { + it("does not add RTK init container by default", () => { + const { job } = buildJobManifest({ ctx, selfPod }); + const inits = job.spec?.template?.spec?.initContainers ?? []; + expect(inits).toHaveLength(1); + expect(inits[0]?.name).toBe("write-prompt"); + }); + + it("adds install-rtk init container when enableRtk is true", () => { + ctx.config = { enableRtk: true }; + const { job } = buildJobManifest({ ctx, selfPod }); + const inits = job.spec?.template?.spec?.initContainers ?? []; + expect(inits).toHaveLength(2); + expect(inits[1]?.name).toBe("install-rtk"); + expect(inits[1]?.image).toBe("curlimages/curl:8.12.1"); + }); + + it("uses custom rtkImage for init container", () => { + ctx.config = { enableRtk: true, rtkImage: "my-registry/rtk-installer:v1" }; + const { job } = buildJobManifest({ ctx, selfPod }); + const inits = job.spec?.template?.spec?.initContainers ?? []; + const rtkInit = inits.find((c) => c.name === "install-rtk"); + expect(rtkInit?.image).toBe("my-registry/rtk-installer:v1"); + }); + + it("adds rtk-bin emptyDir volume when enableRtk is true", () => { + ctx.config = { enableRtk: true }; + const { job } = buildJobManifest({ ctx, selfPod }); + const rtkVol = job.spec?.template?.spec?.volumes?.find((v) => v.name === "rtk-bin"); + expect(rtkVol?.emptyDir).toEqual({}); + }); + + it("mounts rtk-bin in main container when enableRtk is true", () => { + ctx.config = { enableRtk: true }; + const { job } = buildJobManifest({ ctx, selfPod }); + const rtkMount = job.spec?.template?.spec?.containers[0]?.volumeMounts?.find( + (vm) => vm.name === "rtk-bin", + ); + expect(rtkMount?.mountPath).toBe("/tmp/rtk-bin"); + }); + + it("prepends rtk setup to main command with project-level settings isolation", () => { + ctx.config = { enableRtk: true }; + const { job } = buildJobManifest({ ctx, selfPod }); + const command = job.spec?.template?.spec?.containers[0]?.command; + expect(command?.[2]).toContain('export PATH="/tmp/rtk-bin:$PATH"'); + expect(command?.[2]).toContain("rtk install claude-code"); + expect(command?.[2]).toContain("settings.local.json"); + expect(command?.[2]).toContain("export HOME=/paperclip"); + expect(command?.[2]).toContain("cat /tmp/prompt/prompt.txt | claude"); + }); + + it("does not prepend rtk setup when enableRtk is false", () => { + ctx.config = { enableRtk: false }; + const { job } = buildJobManifest({ ctx, selfPod }); + const command = job.spec?.template?.spec?.containers[0]?.command; + expect(command?.[2]).not.toContain("rtk"); + expect(command?.[2]).toMatch(/^cat \/tmp\/prompt\/prompt\.txt/); + }); + + it("does not add rtk-bin volume when enableRtk is false", () => { + ctx.config = { enableRtk: false }; + const { job } = buildJobManifest({ ctx, selfPod }); + expect(job.spec?.template?.spec?.volumes?.find((v) => v.name === "rtk-bin")).toBeUndefined(); + }); + + it("sets RTK_NO_TELEMETRY env var when enableRtk is true", () => { + ctx.config = { enableRtk: true }; + const { job } = buildJobManifest({ ctx, selfPod }); + const rtkTelemetry = job.spec?.template?.spec?.containers[0]?.env?.find( + (e) => e.name === "RTK_NO_TELEMETRY", + ); + expect(rtkTelemetry?.value).toBe("1"); + }); + + it("does not set RTK_NO_TELEMETRY when enableRtk is false", () => { + const { job } = buildJobManifest({ ctx, selfPod }); + const rtkTelemetry = job.spec?.template?.spec?.containers[0]?.env?.find( + (e) => e.name === "RTK_NO_TELEMETRY", + ); + expect(rtkTelemetry).toBeUndefined(); + }); + + it("uses custom rtkVersion in install command", () => { + ctx.config = { enableRtk: true, rtkVersion: "0.5.0" }; + const { job } = buildJobManifest({ ctx, selfPod }); + const inits = job.spec?.template?.spec?.initContainers ?? []; + const rtkInit = inits.find((c) => c.name === "install-rtk"); + expect(rtkInit?.command?.[2]).toContain("RTK_VERSION=0.5.0"); + }); + + it("mounts rtk-bin in install-rtk init container", () => { + ctx.config = { enableRtk: true }; + const { job } = buildJobManifest({ ctx, selfPod }); + const inits = job.spec?.template?.spec?.initContainers ?? []; + const rtkInit = inits.find((c) => c.name === "install-rtk"); + expect(rtkInit?.volumeMounts).toContainEqual({ name: "rtk-bin", mountPath: "/tmp/rtk-bin" }); + }); + + it("writes hooks to workspace .claude/settings.local.json not global settings", () => { + ctx.context = { paperclipWorkspace: { cwd: "/paperclip/workspaces/agent-abc" } }; + ctx.config = { enableRtk: true }; + const { job } = buildJobManifest({ ctx, selfPod }); + const command = job.spec?.template?.spec?.containers[0]?.command; + expect(command?.[2]).toContain("/paperclip/workspaces/agent-abc/.claude"); + expect(command?.[2]).toContain("settings.local.json"); + expect(command?.[2]).not.toMatch(/HOME="\/paperclip".*rtk install/); + }); + }); + describe("return value", () => { it("returns job, jobName, namespace, prompt, claudeArgs, promptMetrics", () => { const result = buildJobManifest({ ctx, selfPod }); diff --git a/src/server/job-manifest.ts b/src/server/job-manifest.ts index 0243cfb..ef07db6 100644 --- a/src/server/job-manifest.ts +++ b/src/server/job-manifest.ts @@ -148,6 +148,10 @@ function buildEnvVars( // HOME must be /paperclip to match PVC mount and enable session resume merged.HOME = "/paperclip"; + if (asBoolean(config.enableRtk, false)) { + merged.RTK_NO_TELEMETRY = "1"; + } + // Convert to V1EnvVar array const envVars: k8s.V1EnvVar[] = Object.entries(merged).map(([name, value]) => ({ name, @@ -171,6 +175,9 @@ export function buildJobManifest(input: JobBuildInput): JobBuildResult { // K8s Job pods are always unattended — no one to approve permission prompts const dangerouslySkipPermissions = asBoolean(config.dangerouslySkipPermissions, true); const extraArgs = asStringArray(config.extraArgs); + const enableRtk = asBoolean(config.enableRtk, false); + const rtkVersion = asString(config.rtkVersion, "latest"); + const rtkImage = asString(config.rtkImage, "curlimages/curl:8.12.1"); const timeoutSec = asNumber(config.timeoutSec, 0); const ttlSeconds = asNumber(config.ttlSecondsAfterFinished, 300); const resources = parseObject(config.resources); @@ -282,6 +289,11 @@ export function buildJobManifest(input: JobBuildInput): JobBuildResult { }, ]; + if (enableRtk) { + volumes.push({ name: "rtk-bin", emptyDir: {} }); + volumeMounts.push({ name: "rtk-bin", mountPath: "/tmp/rtk-bin" }); + } + // Mount shared PVC for /paperclip (session state, workspaces, data) if (selfPod.pvcClaimName) { volumes.push({ @@ -326,7 +338,10 @@ export function buildJobManifest(input: JobBuildInput): JobBuildResult { // Build the claude command string for the main container const claudeArgsEscaped = claudeArgs.map((a) => `'${a.replace(/'/g, "'\\''")}'`).join(" "); - const mainCommand = `cat /tmp/prompt/prompt.txt | claude ${claudeArgsEscaped}`; + const claudeCommand = `cat /tmp/prompt/prompt.txt | claude ${claudeArgsEscaped}`; + const mainCommand = enableRtk + ? `export PATH="/tmp/rtk-bin:$PATH" && _RTK_HOME=$(mktemp -d) && HOME="$_RTK_HOME" rtk install claude-code 2>/dev/null && mkdir -p '${workingDir}/.claude' && mv "$_RTK_HOME/.claude/settings.json" '${workingDir}/.claude/settings.local.json' 2>/dev/null; rm -rf "$_RTK_HOME"; export HOME=/paperclip && ${claudeCommand}` + : claudeCommand; const job: k8s.V1Job = { apiVersion: "batch/v1", @@ -368,6 +383,28 @@ export function buildJobManifest(input: JobBuildInput): JobBuildResult { limits: { cpu: "100m", memory: "64Mi" }, }, }, + ...(enableRtk + ? [ + { + name: "install-rtk", + image: rtkImage, + imagePullPolicy: "IfNotPresent" as const, + command: [ + "sh", + "-c", + rtkVersion === "latest" + ? "curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/refs/heads/master/install.sh | RTK_INSTALL_DIR=/tmp/rtk-bin sh" + : `curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/refs/heads/master/install.sh | RTK_INSTALL_DIR=/tmp/rtk-bin RTK_VERSION=${rtkVersion} sh`, + ], + volumeMounts: [{ name: "rtk-bin", mountPath: "/tmp/rtk-bin" }], + securityContext, + resources: { + requests: { cpu: "10m", memory: "32Mi" }, + limits: { cpu: "200m", memory: "128Mi" }, + }, + }, + ] + : []), ], containers: [ {