mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 13:55:50 +00:00
Squashed merge of: 1. fix/mcp-tsc-errors — 修复上游 MCP 重构后的 tsc 错误和测试失败 2. feat/pipe-mute-disconnect — Pipe IPC 逻辑断开、/lang 命令、mute 状态机 3. feat/stub-recovery-all — 实现全部 stub 恢复 (task 001-012) 4. feat/kairos-activation — KAIROS 激活解除阻塞 + 工具实现 5. codex/openclaw-autonomy-pr — 自治权限系统、运行记录、managed flows Additional: 6. daemon/job 命令层级化重构 (subcommand 架构) 7. 跨平台后台引擎抽象 (detached/tmux engines) 8. 修复 src/ 中 43 个预存在的 TypeScript 类型错误 9. 修复 langfuse isolated test mock 完整性 10. 修复 CodeRabbit 审查的 Critical/Major/Minor 问题 11. remote-control-server logger 抽象 (测试 stderr 静默化) 12. /simplify 审查修复 (代码复用、质量、效率)
100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
import { log, error as logError } from "../logger";
|
|
import {
|
|
storeCreateWorkItem,
|
|
storeGetWorkItem,
|
|
storeGetPendingWorkItem,
|
|
storeUpdateWorkItem,
|
|
storeListSessionsByEnvironment,
|
|
storeGetEnvironment,
|
|
} from "../store";
|
|
import { config } from "../config";
|
|
import { getBaseUrl } from "../config";
|
|
import type { WorkResponse } from "../types/api";
|
|
|
|
/** Encode work secret as base64 JSON (no JWT — just API key as token) */
|
|
function encodeWorkSecret(): string {
|
|
const payload = {
|
|
version: 1,
|
|
session_ingress_token: config.apiKeys[0] || "",
|
|
api_base_url: getBaseUrl(),
|
|
sources: [] as string[],
|
|
auth: [] as string[],
|
|
use_code_sessions: false,
|
|
};
|
|
return Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
}
|
|
|
|
export async function createWorkItem(environmentId: string, sessionId: string): Promise<string> {
|
|
// Validate environment exists and is active
|
|
const env = storeGetEnvironment(environmentId);
|
|
if (!env) {
|
|
throw new Error(`Environment ${environmentId} not found`);
|
|
}
|
|
if (env.status !== "active") {
|
|
throw new Error(`Environment ${environmentId} is not active (status: ${env.status})`);
|
|
}
|
|
|
|
const secret = encodeWorkSecret();
|
|
const record = storeCreateWorkItem({ environmentId, sessionId, secret });
|
|
log(`[RCS] Work item created: ${record.id} for env=${environmentId} session=${sessionId}`);
|
|
return record.id;
|
|
}
|
|
|
|
/** Long-poll for work — blocks until work is available or timeout.
|
|
* Returns null when no work is available, matching the CLI bridge client protocol. */
|
|
export async function pollWork(environmentId: string, timeoutSeconds = config.pollTimeout): Promise<WorkResponse | null> {
|
|
const deadline = Date.now() + timeoutSeconds * 1000;
|
|
|
|
while (Date.now() < deadline) {
|
|
const item = storeGetPendingWorkItem(environmentId);
|
|
|
|
if (item) {
|
|
storeUpdateWorkItem(item.id, { state: "dispatched" });
|
|
|
|
return {
|
|
id: item.id,
|
|
type: "work",
|
|
environment_id: environmentId,
|
|
state: "dispatched",
|
|
data: {
|
|
type: "session",
|
|
id: item.sessionId,
|
|
},
|
|
secret: item.secret,
|
|
created_at: item.createdAt.toISOString(),
|
|
};
|
|
}
|
|
|
|
await new Promise((r) => setTimeout(r, 500));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export function ackWork(workId: string) {
|
|
storeUpdateWorkItem(workId, { state: "acked" });
|
|
}
|
|
|
|
export function stopWork(workId: string) {
|
|
storeUpdateWorkItem(workId, { state: "completed" });
|
|
}
|
|
|
|
export function heartbeatWork(workId: string): { lease_extended: boolean; state: string; last_heartbeat: string; ttl_seconds: number } {
|
|
storeUpdateWorkItem(workId, {} as any); // just bump updatedAt
|
|
const item = storeGetWorkItem(workId);
|
|
const now = new Date();
|
|
return {
|
|
lease_extended: true,
|
|
state: item?.state ?? "acked",
|
|
last_heartbeat: now.toISOString(),
|
|
ttl_seconds: config.heartbeatInterval * 2,
|
|
};
|
|
}
|
|
|
|
/** Reconnect: re-queue sessions associated with an environment */
|
|
export function reconnectWorkForEnvironment(envId: string) {
|
|
const activeSessions = storeListSessionsByEnvironment(envId).filter((s) => s.status === "idle");
|
|
const promises = activeSessions.map((s) => createWorkItem(envId, s.id));
|
|
return Promise.all(promises);
|
|
}
|