Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d894f104f | |||
| fc3866924a | |||
| 368254d75d |
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "paperclip-adapter-claude-k8s",
|
"name": "paperclip-adapter-claude-k8s",
|
||||||
"version": "0.1.53",
|
"version": "0.1.54",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "paperclip-adapter-claude-k8s",
|
"name": "paperclip-adapter-claude-k8s",
|
||||||
"version": "0.1.53",
|
"version": "0.1.54",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@kubernetes/client-node": "^1.0.0",
|
"@kubernetes/client-node": "^1.0.0",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "paperclip-adapter-claude-k8s",
|
"name": "paperclip-adapter-claude-k8s",
|
||||||
"version": "0.1.53",
|
"version": "0.1.55",
|
||||||
"description": "Paperclip adapter plugin that runs Claude Code agents as Kubernetes Jobs",
|
"description": "Paperclip adapter plugin that runs Claude Code agents as Kubernetes Jobs",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
+2
-1
@@ -1,7 +1,8 @@
|
|||||||
export const type = "claude_k8s";
|
export const type = "claude_k8s";
|
||||||
export const label = "Claude (Kubernetes)";
|
export const label = "Claude (Kubernetes)";
|
||||||
|
|
||||||
export const models: undefined = undefined;
|
import { DIRECT_MODELS, BEDROCK_MODELS, isBedrockEnv } from "./server/models.js";
|
||||||
|
export const models = isBedrockEnv() ? BEDROCK_MODELS : DIRECT_MODELS;
|
||||||
|
|
||||||
export const agentConfigurationDoc = `# claude_k8s agent configuration
|
export const agentConfigurationDoc = `# claude_k8s agent configuration
|
||||||
|
|
||||||
|
|||||||
+40
-11
@@ -401,6 +401,7 @@ export async function streamPodLogsOnce(
|
|||||||
sinceSeconds?: number,
|
sinceSeconds?: number,
|
||||||
dedup?: LogLineDedupFilter,
|
dedup?: LogLineDedupFilter,
|
||||||
stopSignal?: { stopped: boolean },
|
stopSignal?: { stopped: boolean },
|
||||||
|
activity?: { lastActiveAt: number },
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const logApi = getLogApi(kubeconfigPath);
|
const logApi = getLogApi(kubeconfigPath);
|
||||||
const chunks: string[] = [];
|
const chunks: string[] = [];
|
||||||
@@ -409,6 +410,13 @@ export async function streamPodLogsOnce(
|
|||||||
write(chunk: Buffer, _encoding, callback) {
|
write(chunk: Buffer, _encoding, callback) {
|
||||||
const text = chunk.toString("utf-8");
|
const text = chunk.toString("utf-8");
|
||||||
chunks.push(text);
|
chunks.push(text);
|
||||||
|
// Refresh stream liveness on every chunk received from the container.
|
||||||
|
// This MUST happen here (not just after streamPodLogsOnce returns) —
|
||||||
|
// a streaming attempt that never disconnects can produce output for
|
||||||
|
// hours, and the grace timer in execute() will fire 30s after the
|
||||||
|
// FIRST disconnect even if a new long-running attempt is currently
|
||||||
|
// streaming, unless we keep this timestamp fresh per-chunk (FAR-107).
|
||||||
|
if (activity) activity.lastActiveAt = Date.now();
|
||||||
const emitted = dedup ? dedup.filter(text) : text;
|
const emitted = dedup ? dedup.filter(text) : text;
|
||||||
if (!emitted) {
|
if (!emitted) {
|
||||||
callback();
|
callback();
|
||||||
@@ -531,7 +539,7 @@ async function streamPodLogs(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const preStreamTs = Math.floor(Date.now() / 1000);
|
const preStreamTs = Math.floor(Date.now() / 1000);
|
||||||
const result = await streamPodLogsOnce(namespace, podName, onLog, kubeconfigPath, sinceSeconds, dedup, stopSignal);
|
const result = await streamPodLogsOnce(namespace, podName, onLog, kubeconfigPath, sinceSeconds, dedup, stopSignal, activity);
|
||||||
if (activity) activity.streamHasExited = true;
|
if (activity) activity.streamHasExited = true;
|
||||||
if (result) {
|
if (result) {
|
||||||
allChunks.push(result);
|
allChunks.push(result);
|
||||||
@@ -1387,6 +1395,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
reject(err);
|
reject(err);
|
||||||
};
|
};
|
||||||
waitForJobCompletion(namespace, jobName, completionTimeoutMs, kubeconfigPath, jobObserver).then(settleOk).catch(settleErr);
|
waitForJobCompletion(namespace, jobName, completionTimeoutMs, kubeconfigPath, jobObserver).then(settleOk).catch(settleErr);
|
||||||
|
let graceCheckInFlight = false;
|
||||||
gracePoller = setInterval(() => {
|
gracePoller = setInterval(() => {
|
||||||
// Only consider grace once the stream has exited at least once.
|
// Only consider grace once the stream has exited at least once.
|
||||||
// Until then we are still in the warm-up window and
|
// Until then we are still in the warm-up window and
|
||||||
@@ -1395,21 +1404,41 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
// measured against the last received chunk — output that resumes
|
// measured against the last received chunk — output that resumes
|
||||||
// through a reconnect resets the clock so transient drops do not
|
// through a reconnect resets the clock so transient drops do not
|
||||||
// truncate live runs (FAR-107).
|
// truncate live runs (FAR-107).
|
||||||
|
if (graceCheckInFlight) return;
|
||||||
if (
|
if (
|
||||||
streamActivity.streamHasExited &&
|
streamActivity.streamHasExited &&
|
||||||
Date.now() - streamActivity.lastActiveAt >= LOG_EXIT_COMPLETION_GRACE_MS
|
Date.now() - streamActivity.lastActiveAt >= LOG_EXIT_COMPLETION_GRACE_MS
|
||||||
) {
|
) {
|
||||||
// Stop the grace poller immediately so we don't double-fire while the
|
graceCheckInFlight = true;
|
||||||
// verification read below is in flight.
|
|
||||||
if (gracePoller) { clearInterval(gracePoller); gracePoller = null; }
|
|
||||||
// The log stream exiting only means the container stopped producing
|
|
||||||
// output — it does NOT prove the Job was deleted. Verify Job
|
|
||||||
// presence with a one-shot read so we can distinguish:
|
|
||||||
// (a) Job 404 → truly gone (TTL or external deletion)
|
|
||||||
// (b) Job still present → K8s condition propagation lag (FAR-23)
|
|
||||||
// Without this check we mis-classify (b) as "deleted externally" and
|
|
||||||
// emit a false-positive k8s_job_deleted_externally error (FAR-107).
|
|
||||||
void (async () => {
|
void (async () => {
|
||||||
|
try {
|
||||||
|
// Pod-phase gate (FAR-107): if the pod is still Running/Pending
|
||||||
|
// the container is alive — Claude can be silent for >30s during
|
||||||
|
// long tool calls (web fetches, slow upstream APIs). Refresh
|
||||||
|
// the stream-activity timer, leave the poller armed, and let
|
||||||
|
// waitForJobCompletion remain the authoritative signal. Only
|
||||||
|
// proceed with the grace settlement when the pod has actually
|
||||||
|
// reached a terminal phase or is gone.
|
||||||
|
const podLookup = await lookupPodState(namespace, jobName, kubeconfigPath);
|
||||||
|
if (!podLookup.podMissing && (podLookup.phase === "Running" || podLookup.phase === "Pending")) {
|
||||||
|
streamActivity.lastActiveAt = Date.now();
|
||||||
|
graceCheckInFlight = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
await onLog("stderr", `[paperclip] grace gate: pod state lookup failed (${err instanceof Error ? err.message : String(err)}) — falling through to Job-presence check\n`).catch(() => {});
|
||||||
|
}
|
||||||
|
// Pod is no longer Running — proceed with Job-presence verification.
|
||||||
|
// Stop the grace poller immediately so we don't double-fire while the
|
||||||
|
// verification read below is in flight.
|
||||||
|
if (gracePoller) { clearInterval(gracePoller); gracePoller = null; }
|
||||||
|
// The log stream exiting only means the container stopped producing
|
||||||
|
// output — it does NOT prove the Job was deleted. Verify Job
|
||||||
|
// presence with a one-shot read so we can distinguish:
|
||||||
|
// (a) Job 404 → truly gone (TTL or external deletion)
|
||||||
|
// (b) Job still present → K8s condition propagation lag (FAR-23)
|
||||||
|
// Without this check we mis-classify (b) as "deleted externally" and
|
||||||
|
// emit a false-positive k8s_job_deleted_externally error (FAR-107).
|
||||||
try {
|
try {
|
||||||
await getBatchApi(kubeconfigPath).readNamespacedJob({ name: jobName, namespace });
|
await getBatchApi(kubeconfigPath).readNamespacedJob({ name: jobName, namespace });
|
||||||
await onLog("stdout", `[paperclip] Log stream exited ${LOG_EXIT_COMPLETION_GRACE_MS / 1000}s ago without K8s Job condition update; Job ${jobName} still present — proceeding with captured output (FAR-23)\n`).catch(() => {});
|
await onLog("stdout", `[paperclip] Log stream exited ${LOG_EXIT_COMPLETION_GRACE_MS / 1000}s ago without K8s Job condition update; Job ${jobName} still present — proceeding with captured output (FAR-23)\n`).catch(() => {});
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||||
import { listK8sModels } from "./models.js";
|
import { listK8sModels, DIRECT_MODELS, BEDROCK_MODELS } from "./models.js";
|
||||||
|
|
||||||
describe("listK8sModels", () => {
|
describe("listK8sModels", () => {
|
||||||
const savedEnv: Record<string, string | undefined> = {};
|
const savedEnv: Record<string, string | undefined> = {};
|
||||||
@@ -50,3 +50,22 @@ describe("listK8sModels", () => {
|
|||||||
expect(models.some((m) => m.id === "claude-opus-4-7")).toBe(true);
|
expect(models.some((m) => m.id === "claude-opus-4-7")).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("static model lists", () => {
|
||||||
|
it("DIRECT_MODELS is non-empty and has valid ids", () => {
|
||||||
|
expect(DIRECT_MODELS.length).toBeGreaterThan(0);
|
||||||
|
for (const m of DIRECT_MODELS) {
|
||||||
|
expect(typeof m.id).toBe("string");
|
||||||
|
expect(m.id.length).toBeGreaterThan(0);
|
||||||
|
expect(typeof m.label).toBe("string");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("BEDROCK_MODELS is non-empty and all ids contain 'anthropic.'", () => {
|
||||||
|
expect(BEDROCK_MODELS.length).toBeGreaterThan(0);
|
||||||
|
for (const m of BEDROCK_MODELS) {
|
||||||
|
expect(m.id).toContain("anthropic.");
|
||||||
|
expect(typeof m.label).toBe("string");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { AdapterModel } from "@paperclipai/adapter-utils";
|
import type { AdapterModel } from "@paperclipai/adapter-utils";
|
||||||
|
|
||||||
const DIRECT_MODELS: AdapterModel[] = [
|
export const DIRECT_MODELS: AdapterModel[] = [
|
||||||
{ id: "claude-opus-4-7", label: "Claude Opus 4.7" },
|
{ id: "claude-opus-4-7", label: "Claude Opus 4.7" },
|
||||||
{ id: "claude-opus-4-6", label: "Claude Opus 4.6" },
|
{ id: "claude-opus-4-6", label: "Claude Opus 4.6" },
|
||||||
{ id: "claude-sonnet-4-6", label: "Claude Sonnet 4.6" },
|
{ id: "claude-sonnet-4-6", label: "Claude Sonnet 4.6" },
|
||||||
@@ -9,7 +9,7 @@ const DIRECT_MODELS: AdapterModel[] = [
|
|||||||
{ id: "claude-haiku-4-5-20251001", label: "Claude Haiku 4.5" },
|
{ id: "claude-haiku-4-5-20251001", label: "Claude Haiku 4.5" },
|
||||||
];
|
];
|
||||||
|
|
||||||
const BEDROCK_MODELS: AdapterModel[] = [
|
export const BEDROCK_MODELS: AdapterModel[] = [
|
||||||
{ id: "us.anthropic.claude-opus-4-7", label: "Bedrock Opus 4.7" },
|
{ id: "us.anthropic.claude-opus-4-7", label: "Bedrock Opus 4.7" },
|
||||||
{ id: "us.anthropic.claude-opus-4-6-v1", label: "Bedrock Opus 4.6" },
|
{ id: "us.anthropic.claude-opus-4-6-v1", label: "Bedrock Opus 4.6" },
|
||||||
{ id: "us.anthropic.claude-sonnet-4-6", label: "Bedrock Sonnet 4.6" },
|
{ id: "us.anthropic.claude-sonnet-4-6", label: "Bedrock Sonnet 4.6" },
|
||||||
@@ -17,7 +17,7 @@ const BEDROCK_MODELS: AdapterModel[] = [
|
|||||||
{ id: "us.anthropic.claude-haiku-4-5-20251001-v1:0", label: "Bedrock Haiku 4.5" },
|
{ id: "us.anthropic.claude-haiku-4-5-20251001-v1:0", label: "Bedrock Haiku 4.5" },
|
||||||
];
|
];
|
||||||
|
|
||||||
function isBedrockEnv(): boolean {
|
export function isBedrockEnv(): boolean {
|
||||||
return (
|
return (
|
||||||
process.env.CLAUDE_CODE_USE_BEDROCK === "1" ||
|
process.env.CLAUDE_CODE_USE_BEDROCK === "1" ||
|
||||||
process.env.CLAUDE_CODE_USE_BEDROCK === "true" ||
|
process.env.CLAUDE_CODE_USE_BEDROCK === "true" ||
|
||||||
|
|||||||
Reference in New Issue
Block a user