mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-14 02:29:33 +01:00
fix(edit-history): remote reads get time for their size, follow CLAUDE_CONFIG_DIR, keep the conversation after /clear
Review findings on the remote edit history: - A forward read may carry 13 MiB and "Load older" 29 MiB; the flat 30 s read timeout cut those off below ~1 MiB/s. The timeout now adds the transfer time at 256 KiB/s (remoteEditSourcesTimeoutMs). It ends a hung read, it does not cut a slow one. - The transcript lookup looked only under $HOME/.claude. It now uses an absolute CLAUDE_CONFIG_DIR of the ssh command's environment, else $HOME/.claude, for both the projects and the live sessions/<pid>.json. A value set only in .zshrc stays invisible (the lookup is a plain sh); documented, and the panel's remote no-transcript note says so instead of promising the next prompt (a remote host sends no hooks). - After a remote /clear claudeSessionId stays the launch id, so once the remote claude exited the lookup fell back to the cleared conversation and the panel flipped back. The sync now hands the lookup the stored conversation, tried right after the live one (remoteTranscriptIds). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c4ba14235f
commit
0a6a877415
10 files changed
+169
-36
No files matched your search
+44
-13
@@ -435,6 +435,22 @@ const EDIT_SOURCE_META_BUDGET_BYTES = 4 * 1024 * 1024;
|
||||
/** Room for the record headers (one per transcript file) on top of budget and metas. */
|
||||
const EDIT_SOURCE_HEADER_SLACK_BYTES = 1024 * 1024;
|
||||
|
||||
/**
|
||||
* The slowest link an edit-history read is given time for. Such a read can carry megabytes
|
||||
* (a forward pass its budget plus the metas, "Load older" up to three budgets), which a flat
|
||||
* {@link REMOTE_READ_TIMEOUT_MS} would cut off below ~1 MiB/s. 256 KiB/s ≈ 2 Mbit/s.
|
||||
*/
|
||||
const EDIT_SOURCE_MIN_BYTES_PER_SEC = 256 * 1024;
|
||||
|
||||
/**
|
||||
* Timeout of an edit-history read that may return up to `maxBytes`: the flat read timeout plus
|
||||
* the transfer time at {@link EDIT_SOURCE_MIN_BYTES_PER_SEC}. It ends a hung read (a host that
|
||||
* fell asleep mid-transfer; no keepalives, see the wake invariants), it does not cut a slow one.
|
||||
*/
|
||||
export function remoteEditSourcesTimeoutMs(maxBytes: number): number {
|
||||
return REMOTE_READ_TIMEOUT_MS + Math.ceil((Math.max(0, maxBytes) / EDIT_SOURCE_MIN_BYTES_PER_SEC) * 1000);
|
||||
}
|
||||
|
||||
const TMUX_NAME_PATTERN = /^[A-Za-z0-9._-]+$/;
|
||||
|
||||
/** Where a remote session's agent runs: the remote tmux socket and session (`remoteTmuxLocation`). */
|
||||
@@ -458,15 +474,27 @@ export function isRemoteClaudeTranscriptPath(path: string): boolean {
|
||||
return CLAUDE_TRANSCRIPT_ID_PATTERN.test(path.slice(path.lastIndexOf('/') + 1, -'.jsonl'.length));
|
||||
}
|
||||
|
||||
/**
|
||||
* Shell lines that set `cfg` to claude's config dir on the remote host: an absolute
|
||||
* `CLAUDE_CONFIG_DIR` from the ssh command's environment, else `$HOME/.claude` — claude's own
|
||||
* rule. ⚠️ The remote claude runs under `$SHELL -i -l -c` (`remoteLoginShellCommand`), this
|
||||
* script under a plain non-interactive `sh`: a `CLAUDE_CONFIG_DIR` set only in an interactive
|
||||
* rc file (`.zshrc`) is not seen here; one from `.zshenv`, sshd `SetEnv` or the system
|
||||
* environment is.
|
||||
*/
|
||||
function claudeConfigDirLines(): string[] {
|
||||
return ['cfg=$HOME/.claude', 'case ${CLAUDE_CONFIG_DIR:-} in /*) cfg=$CLAUDE_CONFIG_DIR ;; esac'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Shell lines that set `live` to the conversation the claude in `pane` is writing NOW,
|
||||
* or leave it empty. Codeman launches a remote claude with `--session-id <id>`, but a
|
||||
* `/clear` (or a resume picked inside claude) starts a new conversation, and with no
|
||||
* hooks from a remote host Codeman never hears of it. claude itself keeps
|
||||
* `~/.claude/sessions/<pid>.json` with the current `sessionId`, so: the pane's pid, its
|
||||
* children and grandchildren (a wrapper shell may sit in between), the first of those
|
||||
* with such a file. Only `[A-Za-z0-9._-]` survives the `sed`, the id pattern, so `$live`
|
||||
* is safe unquoted. `ps -A -o pid= -o ppid=` is POSIX (no GNU `--ppid`).
|
||||
* or leave it empty; they need `cfg` ({@link claudeConfigDirLines}). Codeman launches a
|
||||
* remote claude with `--session-id <id>`, but a `/clear` (or a resume picked inside claude)
|
||||
* starts a new conversation, and with no hooks from a remote host Codeman never hears of it.
|
||||
* claude itself keeps `<config dir>/sessions/<pid>.json` with the current `sessionId`, so: the
|
||||
* pane's pid, its children and grandchildren (a wrapper shell may sit in between), the first
|
||||
* of those with such a file. Only `[A-Za-z0-9._-]` survives the `sed`, the id pattern, so
|
||||
* `$live` is safe unquoted. `ps -A -o pid= -o ppid=` is POSIX (no GNU `--ppid`).
|
||||
*/
|
||||
function liveClaudeIdLines(pane: RemoteClaudePane | undefined): string[] {
|
||||
if (!pane || !TMUX_NAME_PATTERN.test(pane.socket) || !TMUX_NAME_PATTERN.test(pane.sessionName)) return [];
|
||||
@@ -479,7 +507,7 @@ function liveClaudeIdLines(pane: RemoteClaudePane | undefined): string[] {
|
||||
' kids=$(printf \'%s\\n\' "$tree" | awk -v p="$pp" \'$2 == p { print $1 }\')',
|
||||
' grand=$(for k in $kids; do printf \'%s\\n\' "$tree" | awk -v p="$k" \'$2 == p { print $1 }\'; done)',
|
||||
' for c in $pp $kids $grand; do',
|
||||
' f="$HOME/.claude/sessions/$c.json"',
|
||||
' f="$cfg/sessions/$c.json"',
|
||||
' [ -f "$f" ] || continue',
|
||||
' live=$(sed -n \'s/.*"sessionId":"\\([A-Za-z0-9._-]*\\)".*/\\1/p\' "$f" | head -n 1)',
|
||||
' [ -n "$live" ] && break',
|
||||
@@ -492,8 +520,9 @@ function liveClaudeIdLines(pane: RemoteClaudePane | undefined): string[] {
|
||||
* Script that finds a remote claude session's main transcript and prints `\0<path>\0`;
|
||||
* exit {@link TRANSCRIPT_NOT_FOUND_EXIT} when there is none. First the conversation the
|
||||
* claude in `pane` writes now ({@link liveClaudeIdLines}), then each of `ids`; per id the
|
||||
* newest `$HOME/.claude/projects/*\/<id>.jsonl`. `$HOME` is expanded by the REMOTE shell:
|
||||
* the transcript lives in the remote user's home, which the server cannot know.
|
||||
* newest `<config dir>/projects/*\/<id>.jsonl` ({@link claudeConfigDirLines}). `$HOME` is
|
||||
* expanded by the REMOTE shell: the transcript lives in the remote user's home, which the
|
||||
* server cannot know.
|
||||
*
|
||||
* ⚠️ Runs under an explicit `sh` with `LC_ALL=C`: sshd runs a command in the remote user's
|
||||
* LOGIN shell, and on a zsh host `ls` can be an alias and an unmatched glob aborts the whole
|
||||
@@ -508,9 +537,10 @@ export function buildRemoteClaudeTranscriptLocateCommand(ids: readonly string[],
|
||||
'LC_ALL=C',
|
||||
'export LC_ALL',
|
||||
'best=',
|
||||
...claudeConfigDirLines(),
|
||||
...liveLines,
|
||||
`for id in ${liveLines.length ? '$live ' : ''}${safeIds.map((id) => shellescape(id)).join(' ')}; do`,
|
||||
' for f in "$HOME"/.claude/projects/*/"$id".jsonl; do',
|
||||
' for f in "$cfg"/projects/*/"$id".jsonl; do',
|
||||
' [ -f "$f" ] || continue',
|
||||
' if [ -z "$best" ] || [ "$f" -nt "$best" ]; then best=$f; fi',
|
||||
' done',
|
||||
@@ -752,12 +782,13 @@ export async function remoteReadClaudeEditSources(
|
||||
assertNotUnderTest();
|
||||
const budget = Number.isFinite(budgetBytes) ? Math.max(0, Math.floor(budgetBytes)) : 0;
|
||||
const command = buildRemoteFileCommand(remote, buildRemoteClaudeEditSourcesCommand(mainPath, requests, budget, opts));
|
||||
const maxBytes = budget + EDIT_SOURCE_META_BUDGET_BYTES + EDIT_SOURCE_HEADER_SLACK_BYTES;
|
||||
let stdout: Buffer;
|
||||
try {
|
||||
const result = await runWithRemoteSshLimit(() =>
|
||||
execAsync(command, {
|
||||
timeout: REMOTE_READ_TIMEOUT_MS,
|
||||
maxBuffer: budget + EDIT_SOURCE_META_BUDGET_BYTES + EDIT_SOURCE_HEADER_SLACK_BYTES,
|
||||
timeout: remoteEditSourcesTimeoutMs(maxBytes),
|
||||
maxBuffer: maxBytes,
|
||||
encoding: 'buffer',
|
||||
})
|
||||
);
|
||||
|
||||
@@ -84,8 +84,12 @@ export interface EditHistorySession {
|
||||
}
|
||||
|
||||
export interface EditHistorySyncDeps {
|
||||
/** The session's main transcript path, or null when it has none yet. */
|
||||
transcriptPath(session: EditHistorySession): Promise<string | null>;
|
||||
/**
|
||||
* The session's main transcript path, or null when it has none yet. `storedTranscriptId` is
|
||||
* the conversation the stored history holds (null before the first read); see
|
||||
* {@link remoteTranscriptIds}.
|
||||
*/
|
||||
transcriptPath(session: EditHistorySession, storedTranscriptId: string | null): Promise<string | null>;
|
||||
/** The session's transcripts (a remote session's on its host). */
|
||||
readSources(
|
||||
session: EditHistorySession,
|
||||
@@ -108,6 +112,18 @@ export interface SyncOptions {
|
||||
older?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* The conversation ids a REMOTE session's transcript lookup tries, in order, after the one its
|
||||
* claude writes now. No hook tells Codeman of a remote `/clear`, so `claudeSessionId` stays the
|
||||
* launch id; tried first, it would flip the panel back to the cleared conversation as soon as
|
||||
* the remote claude exits. The stored history's conversation is the newest the panel has read.
|
||||
*/
|
||||
export function remoteTranscriptIds(session: EditHistorySession, storedTranscriptId: string | null): string[] {
|
||||
return [...new Set([storedTranscriptId, session.claudeSessionId || session.id, session.id])].filter(
|
||||
(id): id is string => !!id
|
||||
);
|
||||
}
|
||||
|
||||
/** Why a session has no edit history, or undefined when it can have one. */
|
||||
export function unsupportedReason(session: EditHistorySession): string | undefined {
|
||||
if (session.docker) return 'docker';
|
||||
@@ -329,7 +345,7 @@ export class EditHistorySync {
|
||||
try {
|
||||
// This slot's store: a run still in flight after `remove()` cannot reach a later one.
|
||||
const store = await this.storeOf(session.id, slot);
|
||||
const path = slot.gone ? null : await this.deps.transcriptPath(session);
|
||||
const path = slot.gone ? null : await this.deps.transcriptPath(session, store.getTranscriptId());
|
||||
let found: boolean;
|
||||
if (!path) {
|
||||
found = false;
|
||||
|
||||
@@ -363,17 +363,18 @@ Object.assign(CodemanApp.prototype, {
|
||||
return;
|
||||
}
|
||||
if (data.state === 'unavailable') {
|
||||
body.append(
|
||||
el(
|
||||
'div',
|
||||
'edit-history-note edit-history-note--warn',
|
||||
data.reason === 'no-transcript'
|
||||
// A remote host sends no hooks: its transcript is only ever found by the lookup over ssh.
|
||||
const note =
|
||||
data.reason !== 'no-transcript'
|
||||
? t('Showing saved entries only: {reason}', { reason: data.reason || '' })
|
||||
: this.sessions?.get?.(h.sessionId)?.remote
|
||||
? t(
|
||||
'No transcript found for this session yet. It is picked up with the next prompt; until then a CLAUDE_CONFIG_DIR set outside Codeman cannot be found.'
|
||||
'No transcript found on the remote host yet (Claude writes it with the first prompt). A CLAUDE_CONFIG_DIR set only in an interactive shell profile (.zshrc) cannot be found.'
|
||||
)
|
||||
: t('Showing saved entries only: {reason}', { reason: data.reason || '' })
|
||||
)
|
||||
);
|
||||
: t(
|
||||
'No transcript found for this session yet. It is picked up with the next prompt; until then a CLAUDE_CONFIG_DIR set outside Codeman cannot be found.'
|
||||
);
|
||||
body.append(el('div', 'edit-history-note edit-history-note--warn', note));
|
||||
}
|
||||
|
||||
const all = Array.isArray(data.entries) ? data.entries : [];
|
||||
|
||||
@@ -570,6 +570,8 @@
|
||||
'Showing saved entries only: {reason}': '仅显示已保存的条目:{reason}',
|
||||
'No transcript found for this session yet. It is picked up with the next prompt; until then a CLAUDE_CONFIG_DIR set outside Codeman cannot be found.':
|
||||
'尚未找到此会话的记录。它会在下一次提示时被识别;在此之前,在 Codeman 之外设置的 CLAUDE_CONFIG_DIR 无法被找到。',
|
||||
'No transcript found on the remote host yet (Claude writes it with the first prompt). A CLAUDE_CONFIG_DIR set only in an interactive shell profile (.zshrc) cannot be found.':
|
||||
'远程主机上尚未找到记录(Claude 会在第一次提示时写入)。仅在交互式 shell 配置文件(.zshrc)中设置的 CLAUDE_CONFIG_DIR 无法被找到。',
|
||||
'No edits yet.': '暂无编辑。',
|
||||
'No edits by the main agent.': '主智能体没有编辑。',
|
||||
'Load older': '加载更早的编辑',
|
||||
|
||||
+15
-8
@@ -137,7 +137,7 @@ import { probePathKind } from '../utils/bounded-path-probe.js';
|
||||
import { defaultDockerWorkspaces } from './routes/git-status-routes.js';
|
||||
import { composerIsEmpty, plainFrame } from '../session-submit-verifier.js';
|
||||
import { detectUsageLimitPause } from '../usage-limit-patterns.js';
|
||||
import { EditHistorySync } from './edit-history-sync.js';
|
||||
import { EditHistorySync, remoteTranscriptIds } from './edit-history-sync.js';
|
||||
import { EditHistoryStore } from '../edit-history-store.js';
|
||||
import { probeLocalPaths, readClaudeEditSources, resolveClaudeTranscript } from '../edit-history-local-source.js';
|
||||
import { remoteLocateClaudeTranscript, remoteProbePaths, remoteReadClaudeEditSources } from '../remote-files.js';
|
||||
@@ -362,15 +362,22 @@ export class WebServer extends EventEmitter {
|
||||
* A local session's main transcript is the one the transcript watcher follows (hook-reported
|
||||
* path), or, before the first hook after a restart, a lookup under the session's claude
|
||||
* config dir. A remote session sends no hooks: its host is asked over ssh for the transcript
|
||||
* the pane's claude writes (remote-files.ts), and is never woken for it.
|
||||
* the pane's claude writes, else the stored conversation's (`remoteTranscriptIds`), and is
|
||||
* never woken for it (remote-files.ts).
|
||||
*/
|
||||
private readonly editHistorySync = new EditHistorySync({
|
||||
transcriptPath: (session) => {
|
||||
const ids = [session.claudeSessionId || session.id, session.id];
|
||||
return session.remote
|
||||
? remoteLocateClaudeTranscript(session.remote, ids, remoteTmuxLocation(session.remote, session.id))
|
||||
: resolveClaudeTranscript(this.transcriptWatchers.get(session.id)?.getPath(), ids, session.claudeConfigDir);
|
||||
},
|
||||
transcriptPath: (session, storedTranscriptId) =>
|
||||
session.remote
|
||||
? remoteLocateClaudeTranscript(
|
||||
session.remote,
|
||||
remoteTranscriptIds(session, storedTranscriptId),
|
||||
remoteTmuxLocation(session.remote, session.id)
|
||||
)
|
||||
: resolveClaudeTranscript(
|
||||
this.transcriptWatchers.get(session.id)?.getPath(),
|
||||
[session.claudeSessionId || session.id, session.id],
|
||||
session.claudeConfigDir
|
||||
),
|
||||
readSources: (session, mainPath, requests, budget, opts) =>
|
||||
session.remote
|
||||
? remoteReadClaudeEditSources(session.remote, mainPath, requests, budget, opts)
|
||||
|
||||
Reference in new issue
Block a user