@humwick/runner 0.130.2 → 0.130.4
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.
- package/package.json +4 -4
- package/plugins/claude/.claude-plugin/plugin.json +1 -1
- package/src/adapters/claude-api.ts +1 -1
- package/src/adapters/claude-headless.ts +1 -1
- package/src/adapters/claude-launch-exit.ts +26 -0
- package/src/adapters/claude-session-probe.ts +1 -1
- package/src/adapters/claude-tmux.ts +38 -10
- package/src/adapters/claude.ts +28 -75
- package/src/adapters/provider-quota-harvest.ts +20 -71
- package/src/agent-runner/lifecycle-milestones.ts +2 -2
- package/src/agent-runner/liveness-exchange.ts +104 -0
- package/src/claude-system-prompt.ts +49 -0
- package/src/launch-assembly.ts +14 -10
- package/src/profile-home.ts +1 -1
- package/src/project-instructions.ts +2 -2
- package/src/provider-command.ts +18 -0
- package/src/provider-setup.ts +3 -2
- package/src/runner-core.ts +14 -15
- package/src/runner-helpers.ts +0 -8
- package/src/session-scratch.ts +6 -2
- package/src/agent-runner/http-liveness.ts +0 -85
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@humwick/runner",
|
|
3
|
-
"version": "0.130.
|
|
3
|
+
"version": "0.130.4",
|
|
4
4
|
"description": "Unified provider lifecycle runner for Humwick",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"directory": "runner"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@humwick/offload": "0.130.
|
|
33
|
-
"@humwick/providers": "0.104.
|
|
34
|
-
"@humwick/sdk": "0.2.
|
|
32
|
+
"@humwick/offload": "0.130.4",
|
|
33
|
+
"@humwick/providers": "0.104.10",
|
|
34
|
+
"@humwick/sdk": "0.2.139",
|
|
35
35
|
"callmux": "0.25.0",
|
|
36
36
|
"unbash": "6.0.0"
|
|
37
37
|
},
|
|
@@ -49,7 +49,7 @@ export class ClaudeApiAdapter extends ClaudeHeadlessAdapter {
|
|
|
49
49
|
env: resolveProviderApiAuth(this.provider, built.env, {
|
|
50
50
|
ANTHROPIC_API_KEY_FILE: providerConfig.env.ANTHROPIC_API_KEY_FILE ?? process.env.ANTHROPIC_API_KEY_FILE
|
|
51
51
|
?? "~/.config/agent-relay/secrets/anthropic-api.key",
|
|
52
|
-
}) };
|
|
52
|
+
}, config.env) };
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
override async spawn(config: RunnerSpawnConfig): Promise<ManagedProcess> {
|
|
@@ -137,7 +137,7 @@ export class ClaudeHeadlessAdapter implements ProviderAdapter {
|
|
|
137
137
|
if (!config.headless) throw new Error("Claude stream-json transport requires --headless");
|
|
138
138
|
// Use the existing provisioning, permission policy and credential-isolation seam.
|
|
139
139
|
// Prompts enter through stdin only; no positional/one-shot prompt at launch.
|
|
140
|
-
const args = this.launch.buildSpawnArgs({ ...config, prompt: undefined, rig: undefined },
|
|
140
|
+
const args = this.launch.buildSpawnArgs({ ...config, prompt: undefined, rig: undefined }, providerConfig);
|
|
141
141
|
const transportArgs: string[] = [];
|
|
142
142
|
for (let i = 0; i < args.args.length; i++) {
|
|
143
143
|
const arg = args.args[i]!;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { readIncrementalFileRange, statIncrementalFile } from "./incremental-file";
|
|
2
|
+
import type { ProviderStatusUpdate } from "../adapter";
|
|
3
|
+
import { readOneShotExitCode } from "../session-scratch";
|
|
4
|
+
|
|
5
|
+
/** Read only the diagnostic tail, even after a long provider session. */
|
|
6
|
+
async function stderrTail(path: string): Promise<string> {
|
|
7
|
+
try {
|
|
8
|
+
const { size } = await statIncrementalFile(path);
|
|
9
|
+
return new TextDecoder().decode(await readIncrementalFileRange(path, Math.max(0, size - 4096), size)).trim();
|
|
10
|
+
} catch { return ""; }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// #3830 — a vanished pane is not evidence of a successful provider launch.
|
|
14
|
+
export async function claudeLaunchExitStatus(exitCodeFile: string | undefined, ready: boolean, oneShot: boolean, paneTail = ""): Promise<ProviderStatusUpdate> {
|
|
15
|
+
const code = exitCodeFile ? await readOneShotExitCode(exitCodeFile) : undefined;
|
|
16
|
+
// A successful interactive exec replaces the shell and its EXIT trap.
|
|
17
|
+
if ((code === undefined && ready && !oneShot) || (code === "0" && (ready || oneShot))) return "offline";
|
|
18
|
+
const tail = exitCodeFile ? await stderrTail(`${exitCodeFile}.stderr`) : "";
|
|
19
|
+
const reason = ready || oneShot ? "provider-exited" : "spawn_failed";
|
|
20
|
+
const message = `Claude ${ready || oneShot ? "provider exited" : "launch failed before ready"} (${code === undefined ? "exit code unavailable" : `exit code ${code}`})${tail || paneTail ? `: ${tail || paneTail.slice(-4096)}` : ""}`;
|
|
21
|
+
return {
|
|
22
|
+
status: "error",
|
|
23
|
+
providerState: { state: "failed", reason, message, source: "provider", terminal: true },
|
|
24
|
+
metadata: { terminalFailureReason: reason, terminalFailureMessage: message },
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
// This is the single home (DRY) for reading + version-gating + mapping that probe.
|
|
11
11
|
// `probeActivity`, the BusyReconciler backstop, and the monitorless turn-watch all
|
|
12
12
|
// delegate here; the legacy pane-scrape stays only as the graceful fallback for
|
|
13
|
-
// pre-2.1.119 / probe-missing sessions (e.g. a
|
|
13
|
+
// pre-2.1.119 / probe-missing sessions (e.g. a host whose real config
|
|
14
14
|
// home the runner can't resolve). Schema is reverse-engineered (pbauermeister/
|
|
15
15
|
// claude-busy-monitor), so we gate on `version`/presence and degrade gracefully if
|
|
16
16
|
// Anthropic ever drifts it.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { relayEnv } from "@humwick/sdk/env";
|
|
1
|
+
import { relayEnv, writeChildEnv } from "@humwick/sdk/env";
|
|
2
2
|
import { envPipeSource, prepareEnvPipe, providerSecretEnvKeys } from "@humwick/sdk/child-env";
|
|
3
3
|
import { PROVIDER_SCRATCH_ENV_KEYS } from "@humwick/sdk/provider-scratch";
|
|
4
4
|
// Claude tmux transport plumbing (extracted from ./claude for the file-size
|
|
@@ -15,12 +15,12 @@ import { TMUX_SESSION_OWNER_OPTION, managedTmuxSocketName, renderTmuxSessionTarg
|
|
|
15
15
|
import { claudeClearCommitted, claudeConversationAnchor, claudePaneIsBusy, claudePaneLooksReady } from "./claude-status-detectors";
|
|
16
16
|
import { ClaudeSubmitFence, ClaudeSubmitTranscriptProbe, claudeSubmitRecoveryConfirmed, submitClaudePromptConfirmed } from "./claude-submit-confirmation";
|
|
17
17
|
import type { ClaudePromptHookExpectation, ClaudePromptHookLedger } from "./claude-prompt-hook";
|
|
18
|
-
import type { ProviderInputObserver } from "../adapter";
|
|
18
|
+
import type { ProviderInputObserver, RunnerSpawnConfig, SpawnArgs } from "../adapter";
|
|
19
19
|
import { logger } from "../logger";
|
|
20
20
|
import { claudeClearSessionVerdict, type ClaudeClearSessionReading } from "./claude-session-probe";
|
|
21
21
|
import { tmuxOwnerIdFromEnv } from "../config";
|
|
22
22
|
import { spawnSyncBounded } from "../bounded-spawn-sync";
|
|
23
|
-
import { launcherGatePathForSession, launcherScriptPathForSession
|
|
23
|
+
import { launcherGatePathForSession, launcherScriptPathForSession } from "../session-scratch";
|
|
24
24
|
import { captureTmuxPaneAsync, captureTmuxVisiblePaneAsync, loadTmuxBufferFromStdin, runTmuxInput, tmuxSessionPresence, type TmuxReadOpts, type TmuxSessionPresence } from "./tmux-offload";
|
|
25
25
|
|
|
26
26
|
export const CLAUDE_TMUX_SUBMIT_DELAY_MS = 250;
|
|
@@ -50,11 +50,6 @@ export function seedClaudeInitialPrompt(args: string[], prompt: string, options:
|
|
|
50
50
|
return { stdinFile: promptFile, exitCodeFile, stdinContents: prompt };
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
/** A missing sentinel means the one-shot process was killed before its shell could report success. */
|
|
54
|
-
export async function claudeOneShotExitStatus(exitCodeFile?: string): Promise<"offline" | "error"> {
|
|
55
|
-
if (!exitCodeFile) return "offline";
|
|
56
|
-
return (await readOneShotExitCode(exitCodeFile)) === "0" ? "offline" : "error";
|
|
57
|
-
}
|
|
58
53
|
|
|
59
54
|
export function tmuxSessionName(prefix: string, instanceId: string, label?: string): string {
|
|
60
55
|
if (label) return `${prefix}-${sanitizeFsName(label, { replacement: "-", collapse: false, lowercase: true })}`;
|
|
@@ -487,8 +482,12 @@ export function writeLauncherScript(sessionName: string, env: Record<string, str
|
|
|
487
482
|
const unsets = unsetKeys.map((key) => `unset ${shellEnvKey(key)}`).join("\n");
|
|
488
483
|
const gate = gateFifo ? `read -r _ < ${shellQuote(gateFifo)} || true\n` : "";
|
|
489
484
|
const stdin = stdinFile ? `exec < ${shellQuote(stdinFile)}\n` : "";
|
|
490
|
-
const
|
|
491
|
-
|
|
485
|
+
const codeFile = exitCodeFile ?? `${scriptPath}.exit-code`;
|
|
486
|
+
// Interactive Claude must own pane_pid for its session probe. execfail lets a
|
|
487
|
+
// failed exec return to EXIT; print mode keeps a parent to record completion (#3830).
|
|
488
|
+
const recordExit = `trap ${shellQuote(`exit_code=$?; printf '%s' "$exit_code" > ${shellQuote(codeFile)}; exit "$exit_code"`)} EXIT`;
|
|
489
|
+
const command = exitCodeFile ? `( ${shellCmd} )` : `exec ${shellCmd}`;
|
|
490
|
+
writeFileSync(scriptPath, `#!/usr/bin/env bash\numask 077\nshopt -s execfail\nrm -f -- ${shellQuote(codeFile)} ${shellQuote(`${codeFile}.stderr`)}\n${recordExit}\nexec 2> >(tee ${shellQuote(`${codeFile}.stderr`)} >&2)\n${exports ? `${exports}\n` : ""}${Object.keys(env).length ? `${envPipeSource()}\n` : ""}${unsets ? `${unsets}\n` : ""}rm -f -- "$0"\n${gate}${stdin}${command}\n`, { mode: 0o700 });
|
|
492
491
|
return scriptPath;
|
|
493
492
|
}
|
|
494
493
|
|
|
@@ -546,3 +545,32 @@ export function tmuxEnvKeys(env: Record<string, string>, providerEnv: Record<str
|
|
|
546
545
|
}
|
|
547
546
|
return [...keys].sort();
|
|
548
547
|
}
|
|
548
|
+
|
|
549
|
+
export function buildClaudeTmuxArgs(config: RunnerSpawnConfig, spawnArgs: SpawnArgs, provider: string): { sessionName: string; socketName: string; args: string[]; launcherScript: string; gateFifo?: string; printMode: boolean } {
|
|
550
|
+
const requestedSessionName = config.tmuxSession || tmuxSessionName(config.providerConfig.headless.tmuxPrefix, config.instanceId, config.label);
|
|
551
|
+
// Keep the socket derived from the orchestrator's stable identity, but carry
|
|
552
|
+
// tmux's rendered session name everywhere we later target the pane. Without
|
|
553
|
+
// this, `opus-4.8` is created as `opus-4_8` and every watcher/restart lookup
|
|
554
|
+
// immediately misses it.
|
|
555
|
+
const socketName = tmuxSocketName(requestedSessionName); const sessionName = tmuxSessionTargetName(requestedSessionName);
|
|
556
|
+
const shellCmd = [spawnArgs.command, ...spawnArgs.args].map(shellQuote).join(" ");
|
|
557
|
+
const tmuxArgs = ["new-session", "-d", "-s", sessionName, "-x", "200", "-y", "50"];
|
|
558
|
+
|
|
559
|
+
const envKeys = tmuxEnvKeys(spawnArgs.env, config.providerConfig.env, provider);
|
|
560
|
+
const env: Record<string, string> = {};
|
|
561
|
+
for (const key of envKeys) if (relayEnv(spawnArgs.env, key) !== undefined) writeChildEnv(env, { [key]: relayEnv(spawnArgs.env, key)! });
|
|
562
|
+
|
|
563
|
+
// Only gate the launch behind pipe-pane attachment when there's a log file to
|
|
564
|
+
// protect (#1317) — no point serializing dev/interactive spawns that never
|
|
565
|
+
// read AGENT_RELAY_LOG_FILE.
|
|
566
|
+
const gateFifo = relayEnv(spawnArgs.env, "AGENT_RELAY_LOG_FILE") ? launcherGatePathForSession(sessionName) : undefined;
|
|
567
|
+
|
|
568
|
+
// tmux new-session has a hard command-length limit that includes every argv item,
|
|
569
|
+
// including `-e KEY=value`. The script holds the command; its private FIFO
|
|
570
|
+
// carries env values without writing credentials to disk or argv.
|
|
571
|
+
const printMode = spawnArgs.args.some(arg => arg === "-p" || arg === "--print");
|
|
572
|
+
const exitCodeFile = spawnArgs.exitCodeFile ?? (printMode ? `${launcherScriptPathForSession(sessionName)}.exit-code` : undefined);
|
|
573
|
+
const launcherScript = writeLauncherScript(sessionName, env, shellCmd, gateFifo, spawnArgs.stdinFile, exitCodeFile, spawnArgs.envUnset);
|
|
574
|
+
tmuxArgs.push("-c", spawnArgs.cwd, `bash ${shellQuote(launcherScript)}`);
|
|
575
|
+
return { sessionName, socketName, args: tmuxArgs, launcherScript, gateFifo, printMode };
|
|
576
|
+
}
|
package/src/adapters/claude.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { claudeSubagentEnv, claudeSubagentPolicy, configureClaudeSubagentLaunch,
|
|
|
3
3
|
import { submitClaudeContextCommand } from "./claude-context-command";
|
|
4
4
|
import { scopeClaudeCredentialEnv } from "./claude-credentials";
|
|
5
5
|
import { claudeEffortLaunch, claudeEffortSwitchMeta, switchClaudeEffort } from "./claude-effort";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { claudeLaunchExitStatus } from "./claude-launch-exit";
|
|
7
|
+
import { claudeCallerAppends } from "../claude-system-prompt";
|
|
8
|
+
import { resolveLaunchCommand } from "../provider-command";
|
|
9
9
|
import { fireAndForget, sanitizeLaunchBoundary, sanitizeLaunchText, type LivenessSignal, type Message } from "@humwick/sdk";
|
|
10
10
|
import { shellEscape as shellQuote } from "@humwick/sdk/shell-utils";
|
|
11
11
|
import { tmuxCommand, tmuxHasSession } from "@humwick/sdk/tmux-utils";
|
|
@@ -25,13 +25,13 @@ import { claudeActivityFromProbeAndPane, claudeBypassDialogDisplayGate, claudeBy
|
|
|
25
25
|
import { ClaudeSessionCapture, collectClaudeTranscriptArchiveSegment, collectClaudeTranscriptSessionEvents } from "./claude-session-capture";
|
|
26
26
|
import { claudeToolHookPaths, extractLatestContextUsage } from "./claude-transcript";
|
|
27
27
|
import { ClaudeTranscriptTokenUsageCapture } from "./claude-token-usage-capture";
|
|
28
|
-
import { CLAUDE_CLEAR_COMMIT_CAPTURE, CLAUDE_TMUX_READY_TIMEOUT_MS,
|
|
28
|
+
import { CLAUDE_CLEAR_COMMIT_CAPTURE, CLAUDE_TMUX_READY_TIMEOUT_MS, buildClaudeTmuxArgs, confirmClaudeSubmitRecovery, confirmTmuxSessionGone, createLauncherGate, createTmuxSessionWithEnv, releaseClaudeSubmitFence, releaseLauncherGate, seedClaudeInitialPrompt, submitTextToTmux, waitForClaudeClearCommitted, waitForClaudeInputReady } from "./claude-tmux";
|
|
29
29
|
import { captureTmuxPaneAsync, captureTmuxVisiblePaneAsync, resolveClaudePanePidAsync, tmuxSessionPresence } from "./tmux-offload";
|
|
30
30
|
import { claudeShutdownGraceMs, shutdownClaudeTmux, type ClaudeShutdownDeps } from "./claude-shutdown";
|
|
31
31
|
import { claudePermissionPromptHandler } from "./claude-permission";
|
|
32
32
|
import { claudeHarnessTrim, claudeProfileDisallowedToolsArgs } from "./claude-tool-policy";
|
|
33
33
|
import { claudeProjectShellEnv } from "./project-shell";
|
|
34
|
-
import {
|
|
34
|
+
import { stageOneShotPrompt } from "../session-scratch";
|
|
35
35
|
|
|
36
36
|
type MonitorlessTurnWatchDeps = {
|
|
37
37
|
resolvePanePid: typeof resolveClaudePanePidAsync;
|
|
@@ -635,32 +635,22 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
635
635
|
// #557 — ONE assembler owns the provisioning/vanilla/instruction launch surface
|
|
636
636
|
// (--setting-sources, --plugin-dir, --mcp-config, status-line, --append-system-prompt);
|
|
637
637
|
// profile-projection describes the SAME assembled result, so report == launch. The
|
|
638
|
-
// adapter only adds the bits the assembler does not own:
|
|
638
|
+
// adapter only adds the bits the assembler does not own: launch command resolution,
|
|
639
639
|
// headless permission flags, --model, and the positional prompt. materializeLaunchAssembly
|
|
640
640
|
// writes the provisioned skills/plugins the assembled args point at.
|
|
641
641
|
const assembled = assembleLaunch(this.provider, config, providerConfig);
|
|
642
|
-
materializeLaunchAssembly(this.provider, config);
|
|
642
|
+
materializeLaunchAssembly(this.provider, config, assembled);
|
|
643
643
|
const defaultArgs = assembled.hostDefaultArgs;
|
|
644
|
-
const
|
|
645
|
-
|
|
646
|
-
// claude-rig — claude-rig resolves host rig config and would redirect the assembled home.
|
|
647
|
-
// Only a profile-less (user-wrapped) session may use claude-rig.
|
|
648
|
-
const usesClaudeRig = isClaudeRig && !assembled.configHome;
|
|
649
|
-
const bypassRigDefaults = config.headless && usesClaudeRig && config.approvalMode !== "open";
|
|
650
|
-
const rigPrefix = usesClaudeRig && !bypassRigDefaults && config.rig ? ["launch", config.rig] : [];
|
|
651
|
-
const command = !usesClaudeRig || bypassRigDefaults || (!config.rig && !findClaudeRigRC(config.cwd))
|
|
652
|
-
? "claude"
|
|
653
|
-
: providerConfig.command;
|
|
654
|
-
const providerArgs = config.headless
|
|
644
|
+
const command = resolveLaunchCommand(providerConfig.command);
|
|
645
|
+
const providerArgs = claudeCallerAppends(config.headless
|
|
655
646
|
? claudeLaunchArgs(defaultArgs, config.providerArgs, config.approvalMode)
|
|
656
|
-
: [...defaultArgs, ...config.providerArgs];
|
|
647
|
+
: [...defaultArgs, ...config.providerArgs]).args;
|
|
657
648
|
// #1096 — when the assembler merged the caller's own `--settings` into relay's (so the
|
|
658
649
|
// baseline hooks/projectSettings/statusLine survive), drop the caller's now-redundant
|
|
659
650
|
// `--settings` here so exactly ONE reaches the launch (relay's merged copy in assembled.args).
|
|
660
651
|
// #2453 — the coding/review profiles' --disallowedTools trim unions in at this single
|
|
661
652
|
// claude-family launch seam (helpers + per-profile config in ./claude-tool-policy).
|
|
662
653
|
const args = claudeProfileDisallowedToolsArgs([
|
|
663
|
-
...rigPrefix,
|
|
664
654
|
...assembled.args,
|
|
665
655
|
...(assembled.callerSettingsConsumed ? stripSettingsArgs(providerArgs) : providerArgs),
|
|
666
656
|
], config);
|
|
@@ -673,12 +663,10 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
673
663
|
...config.env,
|
|
674
664
|
// #557 — the assembler owns the isolated config-home env (CLAUDE_CONFIG_DIR).
|
|
675
665
|
...assembled.env,
|
|
676
|
-
//
|
|
677
|
-
//
|
|
678
|
-
//
|
|
679
|
-
//
|
|
680
|
-
// inject their own token, so only thread it for the plain-claude path, and
|
|
681
|
-
// never override an explicit per-launch value.
|
|
666
|
+
// Bare `claude` runs its own CLAUDE_CONFIG_DIR and so never inherits the host's long-lived
|
|
667
|
+
// CLAUDE_CODE_OAUTH_TOKEN; without it claude falls back to the (often stale) file-based
|
|
668
|
+
// .credentials.json and bails to /login (401). A custom wrapper command manages its own
|
|
669
|
+
// token, and an explicit per-launch value always wins.
|
|
682
670
|
...(command === "claude" && process.env.CLAUDE_CODE_OAUTH_TOKEN && !config.env?.CLAUDE_CODE_OAUTH_TOKEN
|
|
683
671
|
? { CLAUDE_CODE_OAUTH_TOKEN: process.env.CLAUDE_CODE_OAUTH_TOKEN }
|
|
684
672
|
: {}),
|
|
@@ -758,36 +746,13 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
758
746
|
});
|
|
759
747
|
}
|
|
760
748
|
|
|
761
|
-
buildTmuxArgs(config: RunnerSpawnConfig, spawnArgs: SpawnArgs):
|
|
762
|
-
|
|
763
|
-
// Keep the socket derived from the orchestrator's stable identity, but carry
|
|
764
|
-
// tmux's rendered session name everywhere we later target the pane. Without
|
|
765
|
-
// this, `opus-4.8` is created as `opus-4_8` and every watcher/restart lookup
|
|
766
|
-
// immediately misses it.
|
|
767
|
-
const socketName = tmuxSocketName(requestedSessionName); const sessionName = tmuxSessionTargetName(requestedSessionName);
|
|
768
|
-
const shellCmd = [spawnArgs.command, ...spawnArgs.args].map(shellQuote).join(" ");
|
|
769
|
-
const tmuxArgs = ["new-session", "-d", "-s", sessionName, "-x", "200", "-y", "50"];
|
|
770
|
-
|
|
771
|
-
const envKeys = tmuxEnvKeys(spawnArgs.env, config.providerConfig.env, this.provider);
|
|
772
|
-
const env: Record<string, string> = {};
|
|
773
|
-
for (const key of envKeys) if (relayEnv(spawnArgs.env, key) !== undefined) writeChildEnv(env, { [key]: relayEnv(spawnArgs.env, key)! });
|
|
774
|
-
|
|
775
|
-
// Only gate the launch behind pipe-pane attachment when there's a log file to
|
|
776
|
-
// protect (#1317) — no point serializing dev/interactive spawns that never
|
|
777
|
-
// read AGENT_RELAY_LOG_FILE.
|
|
778
|
-
const gateFifo = relayEnv(spawnArgs.env, "AGENT_RELAY_LOG_FILE") ? launcherGatePathForSession(sessionName) : undefined;
|
|
779
|
-
|
|
780
|
-
// tmux new-session has a hard command-length limit that includes every argv item,
|
|
781
|
-
// including `-e KEY=value`. The script holds the command; its private FIFO
|
|
782
|
-
// carries env values without writing credentials to disk or argv.
|
|
783
|
-
const launcherScript = writeLauncherScript(sessionName, env, shellCmd, gateFifo, spawnArgs.stdinFile, spawnArgs.exitCodeFile, spawnArgs.envUnset);
|
|
784
|
-
tmuxArgs.push("-c", spawnArgs.cwd, `bash ${shellQuote(launcherScript)}`);
|
|
785
|
-
return { sessionName, socketName, args: tmuxArgs, launcherScript, gateFifo };
|
|
749
|
+
buildTmuxArgs(config: RunnerSpawnConfig, spawnArgs: SpawnArgs): ReturnType<typeof buildClaudeTmuxArgs> {
|
|
750
|
+
return buildClaudeTmuxArgs(config, spawnArgs, this.provider);
|
|
786
751
|
}
|
|
787
752
|
|
|
788
753
|
private async spawnHeadless(config: RunnerSpawnConfig, spawnArgs: SpawnArgs): Promise<ManagedProcess> {
|
|
789
754
|
if (spawnArgs.stdinFile && spawnArgs.exitCodeFile && spawnArgs.stdinContents !== undefined) await stageOneShotPrompt(spawnArgs.stdinFile, spawnArgs.exitCodeFile, spawnArgs.stdinContents);
|
|
790
|
-
const { sessionName, socketName, args: tmuxArgs, launcherScript, gateFifo } = this.buildTmuxArgs(config, spawnArgs);
|
|
755
|
+
const { sessionName, socketName, args: tmuxArgs, launcherScript, gateFifo, printMode } = this.buildTmuxArgs(config, spawnArgs);
|
|
791
756
|
const providerEpoch = ++this.providerEpoch;
|
|
792
757
|
this.modelUnavailableObservationState = initialClaudeModelUnavailableObservationState();
|
|
793
758
|
this.connectionRetryActive = false;
|
|
@@ -802,7 +767,8 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
802
767
|
|
|
803
768
|
await createTmuxSessionWithEnv(sessionName, socketName, tmuxArgs, launcherScript, spawnArgs.env, config.providerConfig.env, gateFifo, this.provider);
|
|
804
769
|
|
|
805
|
-
|
|
770
|
+
const exitCodeFile = spawnArgs.exitCodeFile ?? `${launcherScript}.exit-code`;
|
|
771
|
+
this.watchTmuxSession(sessionName, socketName, exitCodeFile, printMode);
|
|
806
772
|
// #1838 review MED — resolve the pane pid OFF the loop now that the session exists and cache
|
|
807
773
|
// it, so the synchronous resolveBusyPid() never has to shell out on a hot busy edge. Fenced
|
|
808
774
|
// on providerEpoch so a resolve that lands after a restart can't stamp a stale pid.
|
|
@@ -832,14 +798,15 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
832
798
|
tmuxSession: sessionName,
|
|
833
799
|
tmuxSocket: socketName,
|
|
834
800
|
launcherScript,
|
|
835
|
-
|
|
836
|
-
...(
|
|
801
|
+
exitCodeFile,
|
|
802
|
+
...claudeEffortSwitchMeta(this.provider, config, printMode),
|
|
803
|
+
printMode,
|
|
837
804
|
providerEpoch,
|
|
838
805
|
},
|
|
839
806
|
};
|
|
840
807
|
}
|
|
841
808
|
|
|
842
|
-
private watchTmuxSession(sessionName: string, socketName?: string,
|
|
809
|
+
private watchTmuxSession(sessionName: string, socketName?: string, exitCodeFile?: string, oneShot = false): void {
|
|
843
810
|
if (this.tmuxWatcher) clearInterval(this.tmuxWatcher);
|
|
844
811
|
// #1838 row 12 — the highest-frequency blocking site in the runner (every 2s for the
|
|
845
812
|
// agent's whole lifetime). Its two tmux subprocesses (has-session + capture-pane) now run
|
|
@@ -849,6 +816,7 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
849
816
|
// probe on the same pane. `this.tmuxWatcher !== watcher` still fences a predecessor tick
|
|
850
817
|
// whose session was replaced by a respawn.
|
|
851
818
|
let tickInFlight = false;
|
|
819
|
+
let lastPane = "";
|
|
852
820
|
const watcher = setInterval(() => {
|
|
853
821
|
if (this.tmuxWatcher !== watcher || tickInFlight) return;
|
|
854
822
|
tickInFlight = true;
|
|
@@ -866,7 +834,7 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
866
834
|
// confirm itself only returns true when BOTH reads are `absent` (#1838 HIGH-2).
|
|
867
835
|
const gone = await confirmTmuxSessionGone(sessionName, socketName).catch(() => false);
|
|
868
836
|
if (this.tmuxWatcher !== watcher || !gone) return;
|
|
869
|
-
const exitStatus = await
|
|
837
|
+
const exitStatus = await claudeLaunchExitStatus(exitCodeFile, this.startupReadyObserved, oneShot, lastPane);
|
|
870
838
|
if (this.tmuxWatcher !== watcher) return;
|
|
871
839
|
clearInterval(watcher); this.tmuxWatcher = undefined; this.publishConfirmedTmuxExit(exitStatus);
|
|
872
840
|
return;
|
|
@@ -879,6 +847,7 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
879
847
|
}
|
|
880
848
|
// #1838 HIGH-1: fence again after the capture await, before any observation/status emit.
|
|
881
849
|
if (this.tmuxWatcher !== watcher) return;
|
|
850
|
+
lastPane = pane;
|
|
882
851
|
// A reused tmux pane can leave an old ready footer in scrollback. Until the
|
|
883
852
|
// current launch is visibly ready, derive both readiness and modal state
|
|
884
853
|
// from the visible grid. After that, avoid an extra capture every tick.
|
|
@@ -938,7 +907,7 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
938
907
|
this.tmuxWatcher = watcher;
|
|
939
908
|
}
|
|
940
909
|
|
|
941
|
-
private publishConfirmedTmuxExit(status:
|
|
910
|
+
private publishConfirmedTmuxExit(status: ProviderStatusUpdate): void {
|
|
942
911
|
// The pane and turn pollers are independent. Invalidate the turn generation BEFORE publishing
|
|
943
912
|
// the terminal edge so an in-flight incarnation/activity read cannot land a later idle and
|
|
944
913
|
// resurrect the row with its shutting-down lifecycle pill still latched (#2505).
|
|
@@ -962,7 +931,7 @@ export class ClaudeAdapter implements ProviderAdapter {
|
|
|
962
931
|
const socketName = process.meta?.tmuxSocket as string | undefined;
|
|
963
932
|
const turnWatchResume = this.turnWatchResume?.process === process ? this.turnWatchResume : undefined;
|
|
964
933
|
this.statusWatchersStopped = false;
|
|
965
|
-
if (sessionName) this.watchTmuxSession(sessionName, socketName, process.meta?.
|
|
934
|
+
if (sessionName) this.watchTmuxSession(sessionName, socketName, process.meta?.exitCodeFile as string | undefined, process.meta?.printMode === true);
|
|
966
935
|
if (turnWatchResume) await this.beginMonitorlessTurnWatch(process, turnWatchResume.sessionName, turnWatchResume.socketName, false);
|
|
967
936
|
}
|
|
968
937
|
|
|
@@ -1058,22 +1027,6 @@ export { claudeActivityFromProbeAndPane, claudePaneLooksReady, claudePaneIsBusy,
|
|
|
1058
1027
|
// Shared shell-quoting; re-exported so `./claude` consumers + tests resolve it.
|
|
1059
1028
|
export { shellQuote };
|
|
1060
1029
|
|
|
1061
|
-
export function findClaudeRigRC(cwd: string): string | null {
|
|
1062
|
-
const home = homedir();
|
|
1063
|
-
let dir = resolve(cwd);
|
|
1064
|
-
while (true) {
|
|
1065
|
-
// Stop at $HOME without inspecting it: ~/.claude-rig is the claude-rig tool's
|
|
1066
|
-
// own config home, not a per-project rc marker. Matching it would make every
|
|
1067
|
-
// project under $HOME look like a claude-rig project.
|
|
1068
|
-
if (dir === home) return null;
|
|
1069
|
-
const rc = join(dir, ".claude-rig");
|
|
1070
|
-
if (existsSync(rc)) return rc;
|
|
1071
|
-
const parent = resolve(dir, "..");
|
|
1072
|
-
if (parent === dir) return null;
|
|
1073
|
-
dir = parent;
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
1030
|
async function terminateSingleProcess(process: ManagedProcess, opts: { graceful: boolean; timeoutMs: number }): Promise<ShutdownOutcome> {
|
|
1078
1031
|
const proc = process.process;
|
|
1079
1032
|
if (!proc) return "killed"; // nothing to kill — vacuously terminal
|
|
@@ -1,61 +1,47 @@
|
|
|
1
1
|
import { writeChildEnv } from "@humwick/sdk/env";
|
|
2
|
-
import type { QuotaState,
|
|
2
|
+
import type { QuotaState, HeartbeatReport, HeartbeatResult } from "@humwick/sdk";
|
|
3
3
|
import { getManifest } from "@humwick/providers";
|
|
4
4
|
import { quotaStateFromProbeMetrics } from "@humwick/sdk/context-probe";
|
|
5
5
|
import { claudeQuotaGeneration, claudeQuotaUnavailableWindows } from "@humwick/sdk/provider-quota-order";
|
|
6
6
|
import { readRunnerContextProbeState } from "../context-probe-state";
|
|
7
7
|
import { providerHostConfigHome } from "../profile-home";
|
|
8
|
-
import {
|
|
8
|
+
import { resolveClaudeStatuslineQuotaIdentity, type ProviderQuotaIdentity } from "../quota";
|
|
9
9
|
|
|
10
|
-
const PASSIVE_QUOTA_CONFIG_REFRESH_MS = 60_000;
|
|
11
10
|
const PASSIVE_QUOTA_SAMPLE_STALE_MS = 10 * 60_000;
|
|
12
|
-
const PASSIVE_QUOTA_LEASE_MAX_TTL_MS = 10 * 60_000;
|
|
13
|
-
const PASSIVE_QUOTA_LEASE_PADDING_MS = 60_000;
|
|
14
11
|
|
|
15
12
|
interface ProviderQuotaHarvestOptions {
|
|
16
13
|
agentId: string;
|
|
17
14
|
provider: string;
|
|
18
15
|
/** The runner's per-provider config env (central provider-config values), read lazily each publish. */
|
|
19
16
|
providerConfigEnv: () => Record<string, string>;
|
|
20
|
-
http: () => Pick<HumwickHttpClient, "acquireProviderQuotaPublisherLease" | "getProviderQuotaConfig" | "upsertProviderQuota">;
|
|
21
17
|
}
|
|
22
18
|
|
|
23
19
|
export class ProviderQuotaHarvest {
|
|
24
|
-
private config = { ...DEFAULT_PROVIDER_QUOTA_CONFIG };
|
|
25
|
-
private configRefreshAt = 0;
|
|
26
|
-
private lastReportAt?: number;
|
|
27
20
|
private lastReportedObservedAt?: number;
|
|
28
|
-
private
|
|
29
|
-
private
|
|
21
|
+
private nextAttemptAt = 0;
|
|
22
|
+
private lease?: { accountKey: string; leaseToken: string };
|
|
30
23
|
|
|
31
24
|
constructor(private readonly options: ProviderQuotaHarvestOptions) {}
|
|
32
25
|
|
|
33
|
-
async
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (getManifest(this.options.provider)?.shim?.kind !== "claude-relay") return;
|
|
38
|
-
const probeMetrics = readRunnerContextProbeState(this.options.agentId);
|
|
39
|
-
const now = Date.now();
|
|
40
|
-
const quota = probeMetrics ? freshQuotaFromProbeMetrics(probeMetrics, now) : undefined;
|
|
41
|
-
// An idle session re-sends nothing new; skipping lets its lease lapse to an active session (#3082).
|
|
26
|
+
async sample(): Promise<Pick<HeartbeatReport, "providerQuota" | "quotaLeaseToken"> | undefined> {
|
|
27
|
+
if (getManifest(this.options.provider)?.shim?.kind !== "claude-relay" || Date.now() < this.nextAttemptAt) return;
|
|
28
|
+
const metrics = readRunnerContextProbeState(this.options.agentId);
|
|
29
|
+
const quota = metrics ? freshQuotaFromProbeMetrics(metrics, Date.now()) : undefined;
|
|
42
30
|
if (!quota || (this.lastReportedObservedAt !== undefined && quota.updatedAt <= this.lastReportedObservedAt)) return;
|
|
43
|
-
const config = await this.refreshConfig();
|
|
44
|
-
if (!config.enabled) return;
|
|
45
|
-
if (this.lastReportAt !== undefined && now - this.lastReportAt < config.pollIntervalMs) return;
|
|
46
31
|
const identity = await this.resolveIdentity();
|
|
47
32
|
if (!identity) return;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
accountKey
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.
|
|
58
|
-
this.lastReportedObservedAt = quota
|
|
33
|
+
return {
|
|
34
|
+
providerQuota: { provider: this.options.provider, accountKey: identity.accountKey, quota,
|
|
35
|
+
unavailableWindows: claudeQuotaUnavailableWindows(quota), lastAttemptAt: Date.now(), sourceAgentId: this.options.agentId },
|
|
36
|
+
...(this.lease?.accountKey === identity.accountKey ? { quotaLeaseToken: this.lease.leaseToken } : {}),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
accept(report: HeartbeatReport, result: HeartbeatResult["providerQuota"]): void {
|
|
41
|
+
if (!result || !report.providerQuota) return;
|
|
42
|
+
this.nextAttemptAt = Date.now() + result.retryAfterMs;
|
|
43
|
+
if (result.reported) this.lastReportedObservedAt = report.providerQuota.quota?.updatedAt;
|
|
44
|
+
if (result.leaseToken) this.lease = { accountKey: report.providerQuota.accountKey, leaseToken: result.leaseToken };
|
|
59
45
|
}
|
|
60
46
|
|
|
61
47
|
// #2514 — quota identity resolves exactly as the LAUNCH authenticates. The primary claude
|
|
@@ -77,43 +63,6 @@ export class ProviderQuotaHarvest {
|
|
|
77
63
|
});
|
|
78
64
|
}
|
|
79
65
|
|
|
80
|
-
private async refreshConfig() {
|
|
81
|
-
const now = Date.now();
|
|
82
|
-
if (now < this.configRefreshAt) return this.config;
|
|
83
|
-
this.configRefreshAt = now + PASSIVE_QUOTA_CONFIG_REFRESH_MS;
|
|
84
|
-
try {
|
|
85
|
-
const map = await this.options.http().getProviderQuotaConfig();
|
|
86
|
-
const providerCfg = map[this.options.provider];
|
|
87
|
-
this.config = providerCfg
|
|
88
|
-
? normalizeProviderQuotaConfig(providerCfg)
|
|
89
|
-
: { ...DEFAULT_PROVIDER_QUOTA_CONFIG };
|
|
90
|
-
} catch {
|
|
91
|
-
// Keep the previous config; defaults preserve collection if the relay is older
|
|
92
|
-
// or temporarily unreachable during a liveness tick.
|
|
93
|
-
}
|
|
94
|
-
return this.config;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private async ensurePublisherLease(accountKey: string, pollIntervalMs: number, now: number): Promise<boolean> {
|
|
98
|
-
if (this.lease?.accountKey === accountKey && this.lease.expiresAt > now) return true;
|
|
99
|
-
if (this.nextLeaseAttemptAt !== undefined && now < this.nextLeaseAttemptAt) return false;
|
|
100
|
-
const leaseToken = this.lease?.accountKey === accountKey ? this.lease.leaseToken : undefined;
|
|
101
|
-
const result = await this.options.http().acquireProviderQuotaPublisherLease({
|
|
102
|
-
provider: this.options.provider,
|
|
103
|
-
accountKey,
|
|
104
|
-
sourceAgentId: this.options.agentId,
|
|
105
|
-
...(leaseToken ? { leaseToken } : {}),
|
|
106
|
-
ttlMs: Math.min(PASSIVE_QUOTA_LEASE_MAX_TTL_MS, pollIntervalMs + PASSIVE_QUOTA_LEASE_PADDING_MS),
|
|
107
|
-
});
|
|
108
|
-
if (!result.acquired || !result.lease) {
|
|
109
|
-
this.lease = undefined;
|
|
110
|
-
this.nextLeaseAttemptAt = now + Math.min(result.retryAfterMs ?? pollIntervalMs, pollIntervalMs);
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
this.lease = { accountKey, leaseToken: result.lease.leaseToken, expiresAt: result.lease.expiresAt };
|
|
114
|
-
this.nextLeaseAttemptAt = undefined;
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
66
|
}
|
|
118
67
|
|
|
119
68
|
function freshQuotaFromProbeMetrics(metrics: NonNullable<ReturnType<typeof readRunnerContextProbeState>>, now: number): QuotaState | undefined {
|
|
@@ -24,7 +24,7 @@ interface LifecycleMilestoneDeps {
|
|
|
24
24
|
stopDrainTimer(): void;
|
|
25
25
|
stopSubmitRecoveryPoll(): void;
|
|
26
26
|
stopTaskClaims(): void;
|
|
27
|
-
|
|
27
|
+
stopLiveness(): void;
|
|
28
28
|
stopCredentials(): void;
|
|
29
29
|
disarmBusyLivenessReconcile(): void;
|
|
30
30
|
disarmBusyReconciler(): void;
|
|
@@ -140,7 +140,7 @@ export class LifecycleMilestones {
|
|
|
140
140
|
this.deps.stopDrainTimer();
|
|
141
141
|
this.deps.stopSubmitRecoveryPoll();
|
|
142
142
|
this.deps.stopTaskClaims();
|
|
143
|
-
this.deps.
|
|
143
|
+
this.deps.stopLiveness();
|
|
144
144
|
this.deps.stopCredentials();
|
|
145
145
|
this.deps.disarmBusyLivenessReconcile();
|
|
146
146
|
this.deps.disarmBusyReconciler();
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { errMessage, type HumwickBusClient, type HumwickHttpClient, type HeartbeatReport, type HeartbeatResult, type Message } from "@humwick/sdk";
|
|
2
|
+
import { httpErrorKey, isHttpAuthError, runnerAgentStatus } from "../runner-helpers";
|
|
3
|
+
import type { ClaimTracker } from "../claim-tracker";
|
|
4
|
+
|
|
5
|
+
const LIVENESS_INTERVAL_MS = 20_000;
|
|
6
|
+
// Shorter than the interval, so a wedged bus exchange falls back to HTTP before the next tick.
|
|
7
|
+
const BUS_LIVENESS_TIMEOUT_MS = 15_000;
|
|
8
|
+
|
|
9
|
+
interface LivenessExchangeDeps {
|
|
10
|
+
http: Pick<HumwickHttpClient, "heartbeat">;
|
|
11
|
+
bus(): Pick<HumwickBusClient, "livenessAvailable" | "reportLiveness">;
|
|
12
|
+
agentId: string;
|
|
13
|
+
instanceId: string;
|
|
14
|
+
logIntervalMs: number;
|
|
15
|
+
isStopped(): boolean;
|
|
16
|
+
isTerminalStatusLatched(): boolean;
|
|
17
|
+
currentStatus: ClaimTracker["currentStatus"];
|
|
18
|
+
sampleQuota(): Promise<Pick<HeartbeatReport, "providerQuota" | "quotaLeaseToken"> | undefined>;
|
|
19
|
+
acceptQuota(report: HeartbeatReport, result: HeartbeatResult["providerQuota"]): void;
|
|
20
|
+
enqueueMessage(message: Message): void;
|
|
21
|
+
triggerEffort(): void;
|
|
22
|
+
recoverRuntimeTokenAfterAuthFailure(source: string): void;
|
|
23
|
+
logRunnerDiagnostic(message: string): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The runner's periodic liveness exchange (#3608). It rides the registered bus socket and falls
|
|
27
|
+
* back to HTTP while the socket is down or the exchange fails on it (#3618). */
|
|
28
|
+
export class LivenessExchange {
|
|
29
|
+
private timer?: Timer;
|
|
30
|
+
private inFlight = false;
|
|
31
|
+
private authFailed = false;
|
|
32
|
+
private lastLog?: { key: string; at: number };
|
|
33
|
+
|
|
34
|
+
constructor(private readonly deps: LivenessExchangeDeps) {}
|
|
35
|
+
|
|
36
|
+
stop(): void {
|
|
37
|
+
if (this.timer) clearInterval(this.timer);
|
|
38
|
+
this.timer = undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
resetAuthFailure(): void { this.authFailed = false; }
|
|
42
|
+
|
|
43
|
+
start(): void {
|
|
44
|
+
if (this.timer) clearInterval(this.timer);
|
|
45
|
+
void this.publish();
|
|
46
|
+
this.timer = setInterval(() => {
|
|
47
|
+
void this.publish();
|
|
48
|
+
}, LIVENESS_INTERVAL_MS);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private async publish(): Promise<void> {
|
|
52
|
+
if (this.deps.isStopped() || this.deps.isTerminalStatusLatched() || this.inFlight || this.authFailed) return;
|
|
53
|
+
this.inFlight = true;
|
|
54
|
+
const status = this.deps.currentStatus();
|
|
55
|
+
const agentStatus = runnerAgentStatus(status);
|
|
56
|
+
try {
|
|
57
|
+
const quota = await this.deps.sampleQuota().catch((error) => {
|
|
58
|
+
this.deps.logRunnerDiagnostic(`passive provider quota sample failed: ${errMessage(error)}`);
|
|
59
|
+
});
|
|
60
|
+
// #3608 — inbox recovery and fresh quota samples ride the single liveness exchange.
|
|
61
|
+
const report: HeartbeatReport = { status: agentStatus, ready: agentStatus !== "offline", unread: true, ...quota };
|
|
62
|
+
const result = await this.exchange(report);
|
|
63
|
+
this.deps.acceptQuota(report, result?.providerQuota);
|
|
64
|
+
for (const message of result?.messages ?? []) this.deps.enqueueMessage(message);
|
|
65
|
+
this.deps.triggerEffort();
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (!this.deps.isStopped()) this.handleHttpFailure(error);
|
|
68
|
+
} finally {
|
|
69
|
+
this.inFlight = false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private async exchange(report: HeartbeatReport): Promise<HeartbeatResult> {
|
|
74
|
+
const bus = this.deps.bus();
|
|
75
|
+
if (bus.livenessAvailable) {
|
|
76
|
+
try {
|
|
77
|
+
return await bus.reportLiveness(report, BUS_LIVENESS_TIMEOUT_MS);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
// The report is idempotent (status, readiness, unread by id), so a retry over HTTP is safe.
|
|
80
|
+
this.logFailure(`bus:${errMessage(error)}`, `[runner] bus liveness exchange failed, using HTTP: ${errMessage(error)}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return await this.deps.http.heartbeat(this.deps.agentId, this.deps.instanceId, report);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private handleHttpFailure(error: unknown): void {
|
|
87
|
+
const authFailed = isHttpAuthError(error);
|
|
88
|
+
const suffix = authFailed ? "auth failed; stopping HTTP liveness retries until restart" : String(error);
|
|
89
|
+
this.logFailure(httpErrorKey(error), `[runner] HTTP liveness update failed: ${suffix}`);
|
|
90
|
+
if (!authFailed) return;
|
|
91
|
+
this.authFailed = true;
|
|
92
|
+
this.stop();
|
|
93
|
+
// A 401/403 here is the only timely signal that the token died — stopping the
|
|
94
|
+
// liveness timer means there is no second chance, so recover from THIS failure.
|
|
95
|
+
this.deps.recoverRuntimeTokenAfterAuthFailure("http-liveness");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private logFailure(key: string, message: string): void {
|
|
99
|
+
const now = Date.now();
|
|
100
|
+
if (this.lastLog?.key === key && now - this.lastLog.at < this.deps.logIntervalMs) return;
|
|
101
|
+
this.lastLog = { key, at: now };
|
|
102
|
+
this.deps.logRunnerDiagnostic(message);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { dirname, join, resolve } from "node:path";
|
|
2
|
+
import { HARNESS_SUBAGENTS_DISABLED, harnessSubagentsEnabled } from "@humwick/sdk";
|
|
3
|
+
import type { RunnerSpawnConfig } from "./adapter";
|
|
4
|
+
import { bundledIntegrationHome } from "./provider-integration";
|
|
5
|
+
import { composeSystemPromptAppend } from "./project-instructions";
|
|
6
|
+
import { CLAUDE_NO_ATTRIBUTION_RULE } from "./claude-settings";
|
|
7
|
+
import { claudeWebToolsSteer } from "./web-tools";
|
|
8
|
+
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
|
|
10
|
+
const APPEND_FLAGS = ["--append-system-prompt", "--append-system-prompt-file"];
|
|
11
|
+
|
|
12
|
+
// #3830 — caller/default appends share the same file channel as Relay context.
|
|
13
|
+
export function claudeCallerAppends(args: string[]): { args: string[]; appends: Array<{ value: string; file: boolean }> } {
|
|
14
|
+
const remaining: string[] = [];
|
|
15
|
+
const appends: Array<{ value: string; file: boolean }> = [];
|
|
16
|
+
for (let i = 0; i < args.length; i++) {
|
|
17
|
+
const arg = args[i]!;
|
|
18
|
+
if (arg === "--") { remaining.push(...args.slice(i)); break; }
|
|
19
|
+
const flag = APPEND_FLAGS.find(flag => arg === flag || arg.startsWith(`${flag}=`));
|
|
20
|
+
if (!flag) { remaining.push(arg); continue; }
|
|
21
|
+
const value = arg === flag ? args[++i] : arg.slice(flag.length + 1);
|
|
22
|
+
if (value === undefined) throw new Error(`${flag} requires a value`);
|
|
23
|
+
appends.push({ value, file: flag.endsWith("-file") });
|
|
24
|
+
}
|
|
25
|
+
return { args: remaining, appends };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function resolveClaudeSystemPrompt(config: RunnerSpawnConfig, defaultArgs: string[], assetHome?: string) {
|
|
29
|
+
const composed = composeSystemPromptAppend(config);
|
|
30
|
+
const parts: Array<{ text: string } | { file: string }> = [{ text: composed.text }];
|
|
31
|
+
for (const append of claudeCallerAppends([...defaultArgs, ...config.providerArgs]).appends) {
|
|
32
|
+
parts.push(append.file ? { file: resolve(config.cwd, append.value) } : { text: append.value });
|
|
33
|
+
}
|
|
34
|
+
parts.push(...[...(harnessSubagentsEnabled(config) ? [] : [HARNESS_SUBAGENTS_DISABLED]), claudeWebToolsSteer(config), CLAUDE_NO_ATTRIBUTION_RULE].map(text => ({ text })));
|
|
35
|
+
return {
|
|
36
|
+
path: join(assetHome ?? bundledIntegrationHome(config), "system-prompt-append.md"),
|
|
37
|
+
reinjected: composed.reinjected,
|
|
38
|
+
parts,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function materializeClaudeSystemPrompt(file?: ReturnType<typeof resolveClaudeSystemPrompt>): void {
|
|
43
|
+
if (!file) return;
|
|
44
|
+
// Report-only assembly never opens caller files; launch materialization owns that failure.
|
|
45
|
+
const text = file.parts.map(part => "text" in part ? part.text : readFileSync(part.file, "utf8")).filter(Boolean).join("\n\n");
|
|
46
|
+
mkdirSync(dirname(file.path), { recursive: true, mode: 0o700 });
|
|
47
|
+
writeFileSync(file.path, text, { mode: 0o600 });
|
|
48
|
+
chmodSync(file.path, 0o600);
|
|
49
|
+
}
|
package/src/launch-assembly.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { bundledIntegrationAssets, bundledIntegrationHome, suppressOwnedClaudePlugins, selectClaudePluginDirs } from "./provider-integration";
|
|
2
2
|
import { relayEnv, writeChildEnv } from "@humwick/sdk/env";
|
|
3
3
|
import { codexRetiredMcpConfigArgs } from "./adapters/codex-effective-config";
|
|
4
|
-
import { activeSharedMcpServers
|
|
4
|
+
import { activeSharedMcpServers } from "@humwick/sdk";
|
|
5
5
|
import { claudeSubagentHooks, claudeSubagentSettings } from "./adapters/claude-subagents";
|
|
6
6
|
import { serviceAccessLaunchEnv, serviceAccessMcpServers } from "./adapters/service-access";
|
|
7
|
-
import { claudeSettingsArgs,
|
|
7
|
+
import { claudeSettingsArgs, extractCallerSettings, stripSettingsArgs, mergeHookSets, hasSettingsArg, sessionStatusLineSettingsArgs } from "./claude-settings";
|
|
8
8
|
export { stripSettingsArgs, sessionStatusLineSettingsArgs } from "./claude-settings";
|
|
9
9
|
import { rmSync, writeFileSync } from "node:fs";
|
|
10
10
|
import { basename, relative, resolve, join } from "node:path";
|
|
@@ -45,8 +45,8 @@ import type { ResolvedProjectClaudeSettings } from "./provisioning";
|
|
|
45
45
|
import { resolveMcpSources, type McpSourceSelection } from "./mcp-source-selection";
|
|
46
46
|
import { resolveProjectHooks } from "./project-hooks";
|
|
47
47
|
import { agentRedactEnv, agentRedactOn, isAgentRedactInstall, resolveAgentRedact, type AgentRedactState } from "./agent-redact";
|
|
48
|
-
import {
|
|
49
|
-
import {
|
|
48
|
+
import { resolveClaudeSystemPrompt, materializeClaudeSystemPrompt } from "./claude-system-prompt";
|
|
49
|
+
import { disabledSharedWebTools, webToolsEnv, withDisabledWebTools } from "./web-tools";
|
|
50
50
|
import {
|
|
51
51
|
claudeMcpConfigPayload,
|
|
52
52
|
claudeMcpLaunchArgs,
|
|
@@ -160,6 +160,7 @@ export interface AssembledMcp {
|
|
|
160
160
|
|
|
161
161
|
export interface AssembledLaunch {
|
|
162
162
|
provider: SpawnProvider;
|
|
163
|
+
systemPromptFile?: ReturnType<typeof resolveClaudeSystemPrompt>;
|
|
163
164
|
/** The resolved profile this launch was assembled from; undefined for a profile-less
|
|
164
165
|
* (legacy default) spawn, which behaves as host base with relay features default-on. */
|
|
165
166
|
profile?: AgentProfile;
|
|
@@ -546,8 +547,8 @@ function assembleClaude(config: RunnerSpawnConfig, providerConfig: ProviderConfi
|
|
|
546
547
|
// #2086 — when P.hooks forces the project scope off but P.instructions is on, re-inject the repo
|
|
547
548
|
// CLAUDE.md/AGENTS.md through the controlled append-system-prompt channel so the docs are honored
|
|
548
549
|
// rather than silently dropped (see composeSystemPromptAppend / resolveProjectInstructions).
|
|
549
|
-
const
|
|
550
|
-
const
|
|
550
|
+
const promptFile = resolveClaudeSystemPrompt(config, hostDefaultArgs, assetHome);
|
|
551
|
+
const repoInstructionsReinjected = promptFile.reinjected;
|
|
551
552
|
const instructions = assembleInstructions(config, {
|
|
552
553
|
projectScopeDropped,
|
|
553
554
|
isolatedHome: Boolean(configHome),
|
|
@@ -570,11 +571,12 @@ function assembleClaude(config: RunnerSpawnConfig, providerConfig: ProviderConfi
|
|
|
570
571
|
...(mcpConfigFilePath ? { configFilePath: mcpConfigFilePath } : {}),
|
|
571
572
|
}),
|
|
572
573
|
...settings.args,
|
|
573
|
-
|
|
574
|
+
"--append-system-prompt-file", promptFile.path,
|
|
574
575
|
];
|
|
575
576
|
|
|
576
577
|
return {
|
|
577
578
|
provider,
|
|
579
|
+
systemPromptFile: promptFile,
|
|
578
580
|
...(config.agentProfile ? { profile: config.agentProfile } : {}),
|
|
579
581
|
base: config.agentProfile?.base ?? "host",
|
|
580
582
|
vanilla,
|
|
@@ -938,10 +940,12 @@ export function assembleLaunch(provider: SpawnProvider, config: RunnerSpawnConfi
|
|
|
938
940
|
/**
|
|
939
941
|
* Write the provisioned assets the assembly describes (skills/plugins/hooks and Codex
|
|
940
942
|
* hooks.json). Driven by the SAME resolvers `assembleLaunch` uses, so disk == launch ==
|
|
941
|
-
* report. Idempotent; the isolated home must already exist (prepare*ProfileHome).
|
|
942
|
-
*
|
|
943
|
+
* report. Idempotent; the isolated home must already exist (prepare*ProfileHome). Claude
|
|
944
|
+
* system context also uses a private file for host-base and profile-less launches.
|
|
943
945
|
*/
|
|
944
|
-
export function materializeLaunchAssembly(provider: SpawnProvider, config: RunnerSpawnConfig): void {
|
|
946
|
+
export function materializeLaunchAssembly(provider: SpawnProvider, config: RunnerSpawnConfig, assembled?: AssembledLaunch): void {
|
|
947
|
+
// #3830 — every Claude path, including host-base/profile-less, uses a private file.
|
|
948
|
+
if (getManifest(provider)?.shim?.kind === "claude-relay") materializeClaudeSystemPrompt((assembled ?? assembleLaunch(provider, config, config.providerConfig)).systemPromptFile);
|
|
945
949
|
if (!config.agentProfile && runnerMcpGeneration(config.env) === "humwick") {
|
|
946
950
|
const feature = getManifest(provider)?.provisioning?.bundledSkills ? "skills" : "plugins";
|
|
947
951
|
if (profileAllowsRelayFeature(config, feature)) {
|
package/src/profile-home.ts
CHANGED
|
@@ -78,7 +78,7 @@ export function providerHomePathFor(provider: SpawnProvider, config: RunnerSpawn
|
|
|
78
78
|
// the repo's CLAUDE.md / AGENTS.md as behavioral instructions, NOT "don't
|
|
79
79
|
// operate here" — an isolated agent must still read/edit/execute in its own cwd.
|
|
80
80
|
//
|
|
81
|
-
// #2254 —
|
|
81
|
+
// #2254 — host-base launches skip the onboarding/theme bootstrap (an
|
|
82
82
|
// onboarded host config already answers those), but they do NOT dodge folder trust:
|
|
83
83
|
// --dangerously-skip-permissions does not bypass the trust gate, and a cwd the host
|
|
84
84
|
// config has never seen still renders the dialog and wedges the spawn. Trust is
|
|
@@ -11,8 +11,8 @@ import { projectSettingSourcesOn } from "./profile-home";
|
|
|
11
11
|
// the Claude re-injection channel and lists both names the cwd may carry.
|
|
12
12
|
const REPO_INSTRUCTION_FILES = ["CLAUDE.md", "AGENTS.md"] as const;
|
|
13
13
|
|
|
14
|
-
// Bound each re-injected file so a pathological repo doc
|
|
15
|
-
//
|
|
14
|
+
// Bound each re-injected file so a pathological repo doc cannot balloon the context.
|
|
15
|
+
// The composed append travels by private file (#3830). Mirrors Codex's
|
|
16
16
|
// project_doc_max_bytes default; oversized content is truncated with a loud marker
|
|
17
17
|
// rather than silently dropped.
|
|
18
18
|
const REPO_INSTRUCTIONS_MAX_BYTES = 64 * 1024;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
import { logger } from "./logger";
|
|
3
|
+
|
|
4
|
+
const warned = new Set<string>();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The ONE place a provider's launch command is resolved. The launch and the pre-spawn login check
|
|
8
|
+
* both call it, so the check can never probe a different binary than the launch runs.
|
|
9
|
+
* A stored `claude-rig` command (the retired wrapper) is treated as plain `claude`, with one warning.
|
|
10
|
+
*/
|
|
11
|
+
export function resolveLaunchCommand(command: string): string {
|
|
12
|
+
if (basename(command) !== "claude-rig") return command;
|
|
13
|
+
if (!warned.has(command)) {
|
|
14
|
+
warned.add(command);
|
|
15
|
+
logger.warn("provider-command", `stored provider command "${command}" is the retired claude-rig wrapper; launching plain "claude" instead. Set the provider command to "claude" in the dashboard.`);
|
|
16
|
+
}
|
|
17
|
+
return "claude";
|
|
18
|
+
}
|
package/src/provider-setup.ts
CHANGED
|
@@ -4,13 +4,14 @@ import { selectProviderConfigEnv } from "@humwick/sdk/child-env";
|
|
|
4
4
|
import type { ProviderAdapter, ProviderStatusEvent } from "./adapter";
|
|
5
5
|
import { logger } from "./logger";
|
|
6
6
|
import { hostSourcesFor } from "./host-asset-plan";
|
|
7
|
+
import { resolveLaunchCommand } from "./provider-command";
|
|
7
8
|
|
|
8
9
|
/** All real adapters share the managed-spawn login check, including replacements (#3751). */
|
|
9
10
|
export function withProviderSetup(adapter: ProviderAdapter): ProviderAdapter {
|
|
10
11
|
const spawn = adapter.spawn.bind(adapter);
|
|
11
12
|
adapter.spawn = async (config) => {
|
|
12
13
|
const env = selectProviderConfigEnv(config.providerConfig.env, hostSourcesFor(config).settings);
|
|
13
|
-
const auth = await checkProviderAuth(config.provider, undefined, env, config.providerConfig.command);
|
|
14
|
+
const auth = await checkProviderAuth(config.provider, undefined, env, resolveLaunchCommand(config.providerConfig.command), config.env);
|
|
14
15
|
if (!auth.ok) throw new Error(auth.reason);
|
|
15
16
|
return spawn(config);
|
|
16
17
|
};
|
|
@@ -21,7 +22,7 @@ export function providerStartupFailure(error: unknown): ProviderStatusEvent {
|
|
|
21
22
|
const message = `Provider startup failed: ${errMessage(error)}`;
|
|
22
23
|
return { status: "error", reason: "provider-turn", providerState: {
|
|
23
24
|
state: "blocked", terminal: true, reason: "providerSetupRequired", label: "Provider setup required", message,
|
|
24
|
-
recommendedAction: "
|
|
25
|
+
recommendedAction: "Resolve the provider startup failure shown above, then restart the runner.",
|
|
25
26
|
} };
|
|
26
27
|
}
|
|
27
28
|
|
package/src/runner-core.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { PromptCommands, promptOrigin } from "./agent-runner/commands/prompt";
|
|
|
11
11
|
import type { NativeSelfResumeHandoff } from "./native-self-resume";
|
|
12
12
|
import { LifecyclePhase } from "./agent-runner/lifecycle-phase";
|
|
13
13
|
import { startRunnerMcpProxy, runnerWebToolLog, runnerWebSearch, runnerMcpHiddenTools } from "./runner-mcp-proxy";
|
|
14
|
-
import {
|
|
14
|
+
import { LivenessExchange } from "./agent-runner/liveness-exchange";
|
|
15
15
|
import { TaskClaims } from "./agent-runner/task-claims";
|
|
16
16
|
import { TurnReport } from "./agent-runner/turn-report";
|
|
17
17
|
import { SessionMirror } from "./agent-runner/session-mirror";
|
|
@@ -22,7 +22,7 @@ import { hostname } from "node:os";
|
|
|
22
22
|
import { resolveEnvLayers, selectProviderConfigEnv } from "@humwick/sdk/child-env";
|
|
23
23
|
import { createHash } from "node:crypto";
|
|
24
24
|
import { join } from "node:path";
|
|
25
|
-
import { type ContextState, type Message, type ProviderState, HELD_FOR_PAYLOAD_KEY, PROVIDER_SUBMIT_UNCONFIRMED, PROXY_AUTH_HOLD_KIND, errMessage, frameSuppressedFromTurn, stringValue } from "@humwick/sdk";
|
|
25
|
+
import { type ContextState, type Message, type ProviderState, HELD_FOR_PAYLOAD_KEY, PROVIDER_SUBMIT_UNCONFIRMED, PROXY_AUTH_HOLD_KIND, errMessage, frameSuppressedFromTurn, relayBusUrl, stringValue } from "@humwick/sdk";
|
|
26
26
|
import { exitRunner } from "./runner-exit";
|
|
27
27
|
import { isResumeWake, ProviderHoldEpisodes } from "./provider-hold";
|
|
28
28
|
import { contextStateFromProbeMetrics, quotaStateFromProbeMetrics } from "@humwick/sdk/context-probe";
|
|
@@ -88,7 +88,6 @@ import {
|
|
|
88
88
|
providerMonitorUnavailableMessage,
|
|
89
89
|
providerStateFromActiveWork,
|
|
90
90
|
registerWithinDeadline,
|
|
91
|
-
relayBusUrl,
|
|
92
91
|
runnerAgentStatus,
|
|
93
92
|
runnerBusErrorAction,
|
|
94
93
|
runnerMessageMatches,
|
|
@@ -218,7 +217,7 @@ export class AgentRunner {
|
|
|
218
217
|
private terminalProviderExit?: Record<string, unknown>;
|
|
219
218
|
private nativeSelfResume = new NativeSelfResumeTracker();
|
|
220
219
|
private readonly registration: Registration;
|
|
221
|
-
private readonly
|
|
220
|
+
private readonly liveness: LivenessExchange;
|
|
222
221
|
private readonly taskClaims: TaskClaims;
|
|
223
222
|
private delivering = false;
|
|
224
223
|
private drainTimer?: Timer;
|
|
@@ -367,7 +366,7 @@ export class AgentRunner {
|
|
|
367
366
|
disposeEmptyTurns: () => this.emptyTurns.dispose(), resetEmptyTurns: () => this.emptyTurns.reset(), resetLateClearRecovery: () => { this.lateClearRecovery.reset(); this.continuationFence.reset(); },
|
|
368
367
|
clearLifecycleAction: () => this.lifecycle.setLifecycleAction(undefined), setOffline: () => this.claims.setTerminalStatus("offline"), publishStatus: () => this.publishStatus(),
|
|
369
368
|
stopDrainTimer: () => { if (this.drainTimer) clearTimeout(this.drainTimer); this.drainTimer = undefined; }, stopSubmitRecoveryPoll: () => this.stopSubmitRecoveryPoll(),
|
|
370
|
-
stopTaskClaims: () => this.taskClaims.stop(),
|
|
369
|
+
stopTaskClaims: () => this.taskClaims.stop(), stopLiveness: () => this.liveness.stop(), stopCredentials: () => this.credentials.stop(),
|
|
371
370
|
disarmBusyLivenessReconcile: () => this.disarmBusyLivenessReconcile(), disarmBusyReconciler: () => this.busyReconciler.disarm(),
|
|
372
371
|
disposeProviderInput: () => this.providerInput.dispose(), disposeParkPosts: () => this.parkPosts.dispose(),
|
|
373
372
|
stopReasoningTail: () => this.sessionMirror.stopReasoningTail(), clearNativeSessionCapture: () => this.sessionMirror.clearNativeSessionCapture(), stopObligationCache: () => this.obligationCache.stop(),
|
|
@@ -433,11 +432,11 @@ export class AgentRunner {
|
|
|
433
432
|
void this.proxy?.refreshTools().catch(() => {});
|
|
434
433
|
},
|
|
435
434
|
resumeLivenessAndPublishRotation: (status, jti) => {
|
|
436
|
-
this.
|
|
435
|
+
this.liveness.resetAuthFailure();
|
|
437
436
|
// An earlier auth failure may have stopped the liveness loop; restart it so the
|
|
438
|
-
// agent reports live again on the fresh token.
|
|
437
|
+
// agent reports live again on the fresh token. startLiveness clears any
|
|
439
438
|
// existing timer first, so this is safe on the normal (proactive) renew path too.
|
|
440
|
-
this.
|
|
439
|
+
this.startLiveness();
|
|
441
440
|
this.pendingTimelineEvent = { status, id: jti, timestamp: Date.now() };
|
|
442
441
|
this.bus.reconnectTransport(status === "runtime-token-reminted" ? "runtime token re-minted" : "runtime token renewed");
|
|
443
442
|
this.publishStatus();
|
|
@@ -469,14 +468,14 @@ export class AgentRunner {
|
|
|
469
468
|
// #2514 — the per-provider config env carries a dedicated-home provider's (claude-b's) own
|
|
470
469
|
// credential, which the quota harvest needs to key its reports under the B subscription.
|
|
471
470
|
providerConfigEnv: () => this.options.providerConfig.env,
|
|
472
|
-
http: () => this.http,
|
|
473
471
|
});
|
|
474
|
-
this.
|
|
475
|
-
http: this.http, agentId: this.agentId, instanceId: options.instanceId, logIntervalMs: HTTP_LIVENESS_LOG_INTERVAL_MS,
|
|
472
|
+
this.liveness = new LivenessExchange({
|
|
473
|
+
http: this.http, bus: () => this.bus, agentId: this.agentId, instanceId: options.instanceId, logIntervalMs: HTTP_LIVENESS_LOG_INTERVAL_MS,
|
|
476
474
|
isStopped: () => this.lifecycle.stopped, isTerminalStatusLatched: () => this.lifecycle.terminalStatusLatched,
|
|
477
475
|
currentStatus: () => this.claims.currentStatus(),
|
|
478
|
-
|
|
479
|
-
|
|
476
|
+
sampleQuota: () => this.providerQuotaHarvest.sample(),
|
|
477
|
+
acceptQuota: (report, result) => this.providerQuotaHarvest.accept(report, result),
|
|
478
|
+
enqueueMessage: (message) => this.enqueueMessage(message), triggerEffort: () => this.effort.trigger(),
|
|
480
479
|
recoverRuntimeTokenAfterAuthFailure: (source) => this.credentials.recoverRuntimeTokenAfterAuthFailure(source),
|
|
481
480
|
logRunnerDiagnostic: (message) => this.logRunnerDiagnostic(message),
|
|
482
481
|
});
|
|
@@ -741,7 +740,7 @@ export class AgentRunner {
|
|
|
741
740
|
}, (status) => this.setProviderStatus(status));
|
|
742
741
|
await this.bootstrapUnreadMessages();
|
|
743
742
|
this.taskClaims.start();
|
|
744
|
-
this.
|
|
743
|
+
this.startLiveness();
|
|
745
744
|
this.testRunDedupSweep.start();
|
|
746
745
|
this.credentials.scheduleRuntimeTokenRenewal();
|
|
747
746
|
this.scheduleDrain();
|
|
@@ -2861,7 +2860,7 @@ export class AgentRunner {
|
|
|
2861
2860
|
if (published && this.pendingTimelineEvent === timelineEvent) this.pendingTimelineEvent = undefined;
|
|
2862
2861
|
}
|
|
2863
2862
|
|
|
2864
|
-
private
|
|
2863
|
+
private startLiveness(): void { this.liveness.start(); }
|
|
2865
2864
|
|
|
2866
2865
|
private logRunnerDiagnostic(message: string): void { this.context.logRunnerDiagnostic(message); }
|
|
2867
2866
|
private sessionLog(message: string): void { this.context.sessionLog(message); }
|
package/src/runner-helpers.ts
CHANGED
|
@@ -133,14 +133,6 @@ export function csvTags(raw: string | undefined): string[] {
|
|
|
133
133
|
return (raw || "").split(",").map((tag) => tag.trim()).filter(Boolean);
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
export function relayBusUrl(relayUrl: string): string {
|
|
137
|
-
const url = new URL(relayUrl);
|
|
138
|
-
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
139
|
-
url.pathname = `${url.pathname.replace(/\/+$/, "")}/bus`;
|
|
140
|
-
url.search = "";
|
|
141
|
-
return url.toString();
|
|
142
|
-
}
|
|
143
|
-
|
|
144
136
|
export function runnerAgentStatus(status: "idle" | "busy" | "offline" | "error"): "idle" | "busy" | "offline" {
|
|
145
137
|
return status === "error" ? "offline" : status;
|
|
146
138
|
}
|
package/src/session-scratch.ts
CHANGED
|
@@ -194,9 +194,13 @@ export function reapSessionScratch(target: SessionScratchTarget): void {
|
|
|
194
194
|
// scratch dir reap to avoid leaking one file per spawn.
|
|
195
195
|
export function reapSessionLauncher(target: SessionLauncherTarget): string[] {
|
|
196
196
|
const removed: string[] = [];
|
|
197
|
-
|
|
197
|
+
const launchers = dedupePaths([
|
|
198
198
|
target.launcherScript,
|
|
199
199
|
target.tmuxSession ? launcherScriptPathForSession(target.tmuxSession, target.launcherDir) : undefined,
|
|
200
|
+
]);
|
|
201
|
+
for (const path of dedupePaths([
|
|
202
|
+
...launchers,
|
|
203
|
+
...launchers.flatMap(path => [`${path}.exit-code`, `${path}.exit-code.stderr`]),
|
|
200
204
|
target.tmuxSession ? launcherGatePathForSession(target.tmuxSession, target.launcherDir) : undefined,
|
|
201
205
|
])) {
|
|
202
206
|
try {
|
|
@@ -286,7 +290,7 @@ export async function sweepStaleLaunchersAsync(opts: LauncherSweepOptions): Prom
|
|
|
286
290
|
try {
|
|
287
291
|
const current = await stat(scriptPath);
|
|
288
292
|
if (!current.isFile() || opts.now - current.mtimeMs < minAge) continue;
|
|
289
|
-
const agentId = await launcherAgentId(scriptPath);
|
|
293
|
+
const agentId = await launcherAgentId(scriptPath.replace(/\.exit-code(?:\.stderr)?$/, ""));
|
|
290
294
|
if (agentId && opts.keepAgentIds.has(agentId)) continue;
|
|
291
295
|
await rm(scriptPath, { force: true });
|
|
292
296
|
removed.push(scriptPath);
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { errMessage, type HumwickHttpClient } from "@humwick/sdk";
|
|
2
|
-
import { httpErrorKey, isHttpAuthError, runnerAgentStatus } from "../runner-helpers";
|
|
3
|
-
import type { ClaimTracker } from "../claim-tracker";
|
|
4
|
-
|
|
5
|
-
const HTTP_LIVENESS_INTERVAL_MS = 20_000;
|
|
6
|
-
|
|
7
|
-
interface HttpLivenessDeps {
|
|
8
|
-
http: Pick<HumwickHttpClient, "heartbeat">;
|
|
9
|
-
agentId: string;
|
|
10
|
-
instanceId: string;
|
|
11
|
-
logIntervalMs: number;
|
|
12
|
-
isStopped(): boolean;
|
|
13
|
-
isTerminalStatusLatched(): boolean;
|
|
14
|
-
currentStatus: ClaimTracker["currentStatus"];
|
|
15
|
-
publishQuota(): Promise<void>;
|
|
16
|
-
bootstrapUnreadMessages(): Promise<void>;
|
|
17
|
-
triggerEffort(): void;
|
|
18
|
-
recoverRuntimeTokenAfterAuthFailure(source: string): void;
|
|
19
|
-
logRunnerDiagnostic(message: string): void;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class HttpLiveness {
|
|
23
|
-
private httpLivenessTimer?: Timer;
|
|
24
|
-
private httpLivenessInFlight = false;
|
|
25
|
-
private httpLivenessAuthFailed = false;
|
|
26
|
-
private httpLivenessLastLog?: { key: string; at: number };
|
|
27
|
-
|
|
28
|
-
constructor(private readonly deps: HttpLivenessDeps) {}
|
|
29
|
-
|
|
30
|
-
stop(): void {
|
|
31
|
-
if (this.httpLivenessTimer) clearInterval(this.httpLivenessTimer);
|
|
32
|
-
this.httpLivenessTimer = undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
resetAuthFailure(): void { this.httpLivenessAuthFailed = false; }
|
|
36
|
-
|
|
37
|
-
start(): void {
|
|
38
|
-
if (this.httpLivenessTimer) clearInterval(this.httpLivenessTimer);
|
|
39
|
-
void this.publishHttpLiveness();
|
|
40
|
-
this.httpLivenessTimer = setInterval(() => {
|
|
41
|
-
void this.publishHttpLiveness();
|
|
42
|
-
}, HTTP_LIVENESS_INTERVAL_MS);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private async publishHttpLiveness(): Promise<void> {
|
|
46
|
-
if (this.deps.isStopped() || this.deps.isTerminalStatusLatched() || this.httpLivenessInFlight || this.httpLivenessAuthFailed) return;
|
|
47
|
-
this.httpLivenessInFlight = true;
|
|
48
|
-
const status = this.deps.currentStatus();
|
|
49
|
-
const agentStatus = runnerAgentStatus(status);
|
|
50
|
-
try {
|
|
51
|
-
// #3606 — one call; the hub writes and emits only when status or readiness actually changed.
|
|
52
|
-
await this.deps.http.heartbeat(this.deps.agentId, this.deps.instanceId, { status: agentStatus, ready: agentStatus !== "offline" });
|
|
53
|
-
await this.deps.publishQuota().catch((error) => this.deps.logRunnerDiagnostic(`passive provider quota publish failed: ${errMessage(error)}`));
|
|
54
|
-
await this.deps.bootstrapUnreadMessages(); this.deps.triggerEffort();
|
|
55
|
-
} catch (error) {
|
|
56
|
-
if (!this.deps.isStopped()) this.handleHttpLivenessFailure(error);
|
|
57
|
-
} finally {
|
|
58
|
-
this.httpLivenessInFlight = false;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
private handleHttpLivenessFailure(error: unknown): void {
|
|
63
|
-
const authFailed = isHttpAuthError(error);
|
|
64
|
-
this.logHttpLivenessFailure(error, authFailed);
|
|
65
|
-
if (!authFailed) return;
|
|
66
|
-
this.httpLivenessAuthFailed = true;
|
|
67
|
-
if (this.httpLivenessTimer) clearInterval(this.httpLivenessTimer);
|
|
68
|
-
this.httpLivenessTimer = undefined;
|
|
69
|
-
// A 401/403 here is the only timely signal that the token died — stopping the
|
|
70
|
-
// liveness timer means there is no second chance, so recover from THIS failure.
|
|
71
|
-
this.deps.recoverRuntimeTokenAfterAuthFailure("http-liveness");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private logHttpLivenessFailure(error: unknown, authFailed: boolean): void {
|
|
75
|
-
const key = httpErrorKey(error);
|
|
76
|
-
const now = Date.now();
|
|
77
|
-
if (
|
|
78
|
-
this.httpLivenessLastLog?.key === key &&
|
|
79
|
-
now - this.httpLivenessLastLog.at < this.deps.logIntervalMs
|
|
80
|
-
) return;
|
|
81
|
-
this.httpLivenessLastLog = { key, at: now };
|
|
82
|
-
const suffix = authFailed ? "auth failed; stopping HTTP liveness retries until restart" : String(error);
|
|
83
|
-
this.deps.logRunnerDiagnostic(`[runner] HTTP liveness update failed: ${suffix}`);
|
|
84
|
-
}
|
|
85
|
-
}
|