@yunhozz/arc 0.2.7 → 0.3.0
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/MANIFEST.sha256 +115 -68
- package/README.md +21 -5
- package/claude/agents/arc-implementer.md +3 -3
- package/claude/agents/arc-planner.md +3 -3
- package/claude/agents/arc-qa.md +3 -3
- package/claude/agents/arc-reviewer.md +3 -3
- package/claude/agents/arc-scouter.md +3 -3
- package/claude/skill/SKILL.md +5 -5
- package/claude/skill/models.toml +7 -48
- package/claude/skill/references/commit.md +7 -7
- package/claude/skill/references/coordination.md +39 -3
- package/claude/skill/references/git-recovery.md +1 -1
- package/claude/skill/references/handoff-prep.md +6 -6
- package/claude/skill/references/handoff.md +8 -3
- package/claude/skill/references/limits.md +2 -0
- package/claude/skill/references/planning.md +8 -8
- package/claude/skill/references/verification.md +9 -9
- package/claude/skill/runtime/adapters/claude.mjs +4 -0
- package/claude/skill/runtime/adapters/codex.mjs +3 -0
- package/claude/skill/runtime/bootstrap.mjs +4 -2
- package/claude/skill/runtime/command.mjs +21 -4
- package/claude/skill/runtime/local-cli.mjs +248 -0
- package/claude/skill/runtime/role-bridge-args.mjs +148 -0
- package/claude/skill/runtime/role-bridge.mjs +227 -0
- package/claude/skill/runtime/role-dispatch-policy.mjs +110 -0
- package/claude/skill/runtime/role-dispatch-store.mjs +134 -0
- package/claude/skill/runtime/role-observe.mjs +299 -0
- package/claude/skill/runtime/role-policy.json +12 -10
- package/claude/skill/runtime/role-routing-config.mjs +19 -0
- package/claude/skill/runtime/role-routing.mjs +233 -0
- package/claude/skill/runtime/role-supervisor.mjs +356 -0
- package/claude/skill/scripts/model_config.py +23 -5
- package/codex/agents/arc-implementer.toml +3 -3
- package/codex/agents/arc-planner.toml +3 -3
- package/codex/agents/arc-qa.toml +3 -3
- package/codex/agents/arc-reviewer.toml +3 -3
- package/codex/agents/arc-scouter.toml +3 -3
- package/codex/skill/SKILL.md +5 -5
- package/codex/skill/models.toml +7 -48
- package/codex/skill/references/commit.md +7 -7
- package/codex/skill/references/coordination.md +38 -2
- package/codex/skill/references/handoff-prep.md +6 -6
- package/codex/skill/references/handoff-worktree.md +3 -3
- package/codex/skill/references/handoff.md +8 -3
- package/codex/skill/references/limits.md +2 -0
- package/codex/skill/references/planning.md +8 -8
- package/codex/skill/references/verification.md +9 -9
- package/codex/skill/runtime/adapters/claude.mjs +4 -0
- package/codex/skill/runtime/adapters/codex.mjs +3 -0
- package/codex/skill/runtime/bootstrap.mjs +4 -2
- package/codex/skill/runtime/command.mjs +21 -4
- package/codex/skill/runtime/local-cli.mjs +248 -0
- package/codex/skill/runtime/role-bridge-args.mjs +148 -0
- package/codex/skill/runtime/role-bridge.mjs +227 -0
- package/codex/skill/runtime/role-dispatch-policy.mjs +110 -0
- package/codex/skill/runtime/role-dispatch-store.mjs +134 -0
- package/codex/skill/runtime/role-observe.mjs +299 -0
- package/codex/skill/runtime/role-policy.json +12 -10
- package/codex/skill/runtime/role-routing-config.mjs +19 -0
- package/codex/skill/runtime/role-routing.mjs +233 -0
- package/codex/skill/runtime/role-supervisor.mjs +356 -0
- package/codex/skill/scripts/model_config.py +17 -22
- package/lib/cli/root.mjs +3 -2
- package/lib/installer/install-mode.mjs +34 -0
- package/lib/installer/install.mjs +1 -1
- package/lib/installer/plan.mjs +16 -7
- package/lib/installer/preset.mjs +18 -11
- package/lib/installer/role-routing.mjs +175 -0
- package/lib/installer/runtime-assets.mjs +9 -0
- package/lib/installer/targets.mjs +2 -7
- package/lib/installer/transaction.mjs +1 -1
- package/lib/installer/uninstall.mjs +10 -5
- package/lib/installer/upgrade-state.mjs +9 -6
- package/lib/installer/upgrade.mjs +2 -2
- package/lib/management/operations.mjs +94 -5
- package/lib/management/status.mjs +12 -6
- package/lib/management/update.mjs +3 -1
- package/lib/models/catalog.mjs +3 -3
- package/lib/models/claude-models.json +297 -0
- package/lib/models/codex-catalog.mjs +9 -6
- package/lib/models/codex-models.json +189 -0
- package/lib/models/config.mjs +25 -20
- package/lib/models/index.mjs +1 -1
- package/lib/models/static-catalog.mjs +42 -0
- package/lib/policy/role-policy.json +12 -10
- package/lib/runtime/adapters/claude.mjs +4 -0
- package/lib/runtime/adapters/codex.mjs +3 -0
- package/lib/runtime/bootstrap.mjs +4 -2
- package/lib/runtime/command.mjs +21 -4
- package/lib/runtime/local-cli.mjs +248 -0
- package/lib/runtime/role-bridge-args.mjs +148 -0
- package/lib/runtime/role-bridge.mjs +227 -0
- package/lib/runtime/role-dispatch-policy.mjs +110 -0
- package/lib/runtime/role-dispatch-store.mjs +134 -0
- package/lib/runtime/role-observe.mjs +299 -0
- package/lib/runtime/role-routing-config.mjs +19 -0
- package/lib/runtime/role-routing.mjs +233 -0
- package/lib/runtime/role-supervisor.mjs +356 -0
- package/lib/tui/app.mjs +32 -23
- package/lib/tui/custom-preset.mjs +138 -66
- package/lib/tui/render.mjs +4 -3
- package/lib/tui/setup.mjs +96 -51
- package/lib/watch/args.mjs +43 -0
- package/lib/watch/discover-claude.mjs +63 -0
- package/lib/watch/discover-codex.mjs +45 -0
- package/lib/watch/dispatch.mjs +133 -0
- package/lib/watch/git.mjs +29 -0
- package/lib/watch/index.mjs +112 -0
- package/lib/watch/labels.mjs +24 -0
- package/lib/watch/model.mjs +48 -0
- package/lib/watch/normalize-claude.mjs +99 -0
- package/lib/watch/normalize-codex.mjs +96 -0
- package/lib/watch/policy.mjs +52 -0
- package/lib/watch/render.mjs +86 -0
- package/lib/watch/scope.mjs +64 -0
- package/lib/watch/tail.mjs +75 -0
- package/lib/watch/watcher.mjs +337 -0
- package/package.json +2 -2
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {WATCH_LIMITS} from './model.mjs';
|
|
2
|
+
|
|
3
|
+
const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
4
|
+
const string = value => typeof value === 'string';
|
|
5
|
+
const strings = values => values.filter(string);
|
|
6
|
+
const optional = fields => Object.fromEntries(Object.entries(fields).filter(([,value]) => value !== undefined));
|
|
7
|
+
|
|
8
|
+
export function parseCodexMeta(obj) {
|
|
9
|
+
try {
|
|
10
|
+
if (obj?.type !== 'session_meta' || !object(obj.payload)) return null;
|
|
11
|
+
const p = obj.payload;
|
|
12
|
+
if (![p.id, p.originator, p.cwd].every(string)) return null;
|
|
13
|
+
const spawn = p.source?.subagent?.thread_spawn;
|
|
14
|
+
const fields = {threadId:p.id, originator:p.originator, cwd:p.cwd};
|
|
15
|
+
for (const [key, value] of Object.entries({
|
|
16
|
+
cliVersion:p.cli_version, timestamp:p.timestamp,
|
|
17
|
+
parentId:p.parent_thread_id ?? spawn?.parent_thread_id,
|
|
18
|
+
agentRole:p.agent_role ?? spawn?.agent_role,
|
|
19
|
+
agentNickname:p.agent_nickname ?? spawn?.agent_nickname,
|
|
20
|
+
agentPath:p.agent_path ?? spawn?.agent_path,
|
|
21
|
+
})) if (string(value)) fields[key] = value;
|
|
22
|
+
const depth = p.depth ?? spawn?.depth;
|
|
23
|
+
if (Number.isFinite(depth)) fields.depth = depth;
|
|
24
|
+
return fields;
|
|
25
|
+
} catch { return null; }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function commandText(command) {
|
|
29
|
+
if (string(command)) return command;
|
|
30
|
+
if (!Array.isArray(command)) return undefined;
|
|
31
|
+
if (command.length === 3 && command[1] === '-lc' && string(command[2])) return command[2];
|
|
32
|
+
return strings(command).join(' ');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function changePaths(changes) {
|
|
36
|
+
const paths = [];
|
|
37
|
+
if (Array.isArray(changes)) {
|
|
38
|
+
for (const change of changes) paths.push(change?.path, change?.file);
|
|
39
|
+
} else if (object(changes)) {
|
|
40
|
+
for (const [path, change] of Object.entries(changes)) paths.push(path, change?.move_path);
|
|
41
|
+
}
|
|
42
|
+
return [...new Set(strings(paths))].slice(0, WATCH_LIMITS.pathsMax);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Normalize one parsed rollout line; no I/O or transcript state. */
|
|
46
|
+
export function normalizeCodexLine(obj) {
|
|
47
|
+
try {
|
|
48
|
+
if (!object(obj)) return [];
|
|
49
|
+
const emit = (kind, fields={}) => [optional({ts:string(obj.timestamp) ? obj.timestamp : undefined, kind, ...fields})];
|
|
50
|
+
const text = value => string(value) ? value : undefined;
|
|
51
|
+
if (obj.type === 'session_meta') return parseCodexMeta(obj) ? emit('session') : [];
|
|
52
|
+
const p = obj.payload;
|
|
53
|
+
if (!object(p)) return [];
|
|
54
|
+
if (obj.type === 'response_item') {
|
|
55
|
+
if (!['function_call','custom_tool_call'].includes(p.type) || !string(p.name) || !p.name.includes('spawn_agent')) return [];
|
|
56
|
+
let args;
|
|
57
|
+
try { args = JSON.parse(p.arguments); } catch { /* Unparseable calls still identify the tool. */ }
|
|
58
|
+
const agent = text(args?.agent_type) ?? '';
|
|
59
|
+
const task = text(args?.task_name);
|
|
60
|
+
return emit('spawn', {tool:p.name, text:agent && task ? `${agent}: ${task}` : agent});
|
|
61
|
+
}
|
|
62
|
+
if (obj.type !== 'event_msg') return [];
|
|
63
|
+
switch (p.type) {
|
|
64
|
+
case 'thread_settings_applied': {
|
|
65
|
+
const settings = p.thread_settings;
|
|
66
|
+
return emit('settings', {model:text(settings?.model), effort:text(settings?.reasoning_effort), approval:text(settings?.approval_policy)});
|
|
67
|
+
}
|
|
68
|
+
case 'task_started': return emit('turn_start');
|
|
69
|
+
case 'task_complete': return p.error ? emit('turn_failed', {text:text(p.error.message)}) : emit('turn_end');
|
|
70
|
+
case 'turn_aborted': return emit('turn_end', {text:`aborted: ${text(p.reason) ?? ''}`});
|
|
71
|
+
case 'exec_approval_request': return emit('approval', {text:commandText(p.command)});
|
|
72
|
+
case 'apply_patch_approval_request': return emit('approval', {paths:changePaths(p.changes)});
|
|
73
|
+
case 'error': return emit('error', {text:text(p.message)});
|
|
74
|
+
case 'item_completed': break;
|
|
75
|
+
default: return [];
|
|
76
|
+
}
|
|
77
|
+
const item = p.item;
|
|
78
|
+
if (!object(item) || !string(item.type)) return [];
|
|
79
|
+
switch (item.type) {
|
|
80
|
+
case 'UserMessage':
|
|
81
|
+
case 'AgentMessage':
|
|
82
|
+
return emit(item.type === 'UserMessage' ? 'user' : 'agent', {text:Array.isArray(item.content) ? strings(item.content.map(part => part?.text)).join('') : ''});
|
|
83
|
+
case 'Reasoning': {
|
|
84
|
+
const summary = Array.isArray(item.summary_text) ? strings(item.summary_text).join('') : '';
|
|
85
|
+
return summary ? emit('reasoning', {text:summary}) : [];
|
|
86
|
+
}
|
|
87
|
+
case 'CommandExecution': return emit('command', {text:commandText(item.command), exitCode:Number.isFinite(item.exit_code) ? item.exit_code : undefined});
|
|
88
|
+
case 'FileChange':
|
|
89
|
+
case 'PatchApply': return emit('file_change', {paths:changePaths(item.changes)});
|
|
90
|
+
case 'McpToolCall': return emit('tool', {tool:strings([item.server,item.tool]).join('.')});
|
|
91
|
+
case 'Extension':
|
|
92
|
+
return item.kind === 'web.search' ? emit('tool', {tool:'web.search', text:text(item.query)}) : emit('other', {text:`Extension:${text(item.kind) ?? ''}`});
|
|
93
|
+
default: return emit('other', {text:item.type});
|
|
94
|
+
}
|
|
95
|
+
} catch { return []; }
|
|
96
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {VERBOSE_KINDS} from './model.mjs';
|
|
2
|
+
|
|
3
|
+
export function maskSecrets(text) {
|
|
4
|
+
const secretName = /(?:password|passwd|pass|secret|token|api[_-]?key|access_key|private_key|auth)$/i;
|
|
5
|
+
return text
|
|
6
|
+
.replace(/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*/g, '***')
|
|
7
|
+
.replace(/:\/\/[^\s/@:]+:[^\s/@]*@/g, '://***@')
|
|
8
|
+
.replace(/\bAuthorization[^\S\r\n]*:[^\S\r\n]*(?:[A-Za-z]+[^\S\r\n]+)?[^\s"']+/gi, 'Bearer ***')
|
|
9
|
+
.replace(/\bBearer[^\S\r\n]+[^\s"']+/gi, 'Bearer ***')
|
|
10
|
+
.replace(/\b(?:sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|xox[abpr]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{35})/g, '***')
|
|
11
|
+
.replace(/(["'])([A-Za-z_][\w-]*)\1([^\S\r\n]*:[^\S\r\n]*)("[^"\r\n]*(?:"|(?=[\r\n]|$))|'[^'\r\n]*(?:'|(?=[\r\n]|$))|[^\s"',;}]+)/g,
|
|
12
|
+
(match, quote, key, separator) => secretName.test(key) ? `${quote}${key}${quote}${separator}***` : match)
|
|
13
|
+
.replace(/(^|[^\w-])([A-Za-z_][\w-]*)([^\S\r\n]*[=:][^\S\r\n]*)("[^"\r\n]*(?:"|(?=[\r\n]|$))|'[^'\r\n]*(?:'|(?=[\r\n]|$))|[^\s"',;]+)/g,
|
|
14
|
+
(match, prefix, key, separator) => secretName.test(key) ? `${prefix}${key}${separator}***` : match)
|
|
15
|
+
.replace(/(?<![\w-])--([A-Za-z_][\w-]{0,63})([ \t]+|=)("[^"\r\n]*(?:"|(?=[\r\n]|$))|'[^'\r\n]*(?:'|(?=[\r\n]|$))|[^\s"',;]+)/g,
|
|
16
|
+
(match, key, separator) => secretName.test(key) ? `--${key}${separator}***` : match);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function sanitizeText(value) {
|
|
20
|
+
if (typeof value !== 'string') return '';
|
|
21
|
+
const plain = value
|
|
22
|
+
.replace(/(?:\x1b\]|\u009d)[\s\S]*?(?:\x07|\x1b\\|\u009c|$)/g, '')
|
|
23
|
+
.replace(/(?:\x1b\[|\u009b)[0-?]*[ -/]*[@-~]/g, '')
|
|
24
|
+
.replace(/[\t\n\r\v\f]/g, ' ')
|
|
25
|
+
.replace(/[\x00-\x1f\x7f-\x9f]/g, '')
|
|
26
|
+
.replace(/\s+/gu, ' ').trim();
|
|
27
|
+
return maskSecrets(plain);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function sanitizeMessage(value) {
|
|
31
|
+
if (typeof value !== 'string') return '';
|
|
32
|
+
const plain = value
|
|
33
|
+
.replace(/(?:\x1b\]|\u009d)[\s\S]*?(?:\x07|\x1b\\|\u009c|$)/g, '')
|
|
34
|
+
.replace(/(?:\x1b\[|\u009b)[0-?]*[ -/]*[@-~]/g, '')
|
|
35
|
+
.replace(/\r\n|\r|\u2028|\u2029/g, '\n')
|
|
36
|
+
.replace(/\t/g, ' ')
|
|
37
|
+
.replace(/[\x00-\x09\x0b-\x1f\x7f-\x9f]/g, '')
|
|
38
|
+
.split('\n').map(line => line.trimEnd()).join('\n')
|
|
39
|
+
.replace(/^\n+|\n+$/g, '');
|
|
40
|
+
return maskSecrets(plain);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function clipCodePoints(text, max) {
|
|
44
|
+
const points = [...text];
|
|
45
|
+
const limit = Math.max(0, Math.floor(max));
|
|
46
|
+
if (points.length <= limit) return {text, truncated:false};
|
|
47
|
+
return {text:limit === 0 ? '' : points.slice(0, limit - 1).join('') + '…', truncated:true};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isVisible(kind, verbose) {
|
|
51
|
+
return Boolean(verbose) || !VERBOSE_KINDS.includes(kind);
|
|
52
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {WATCH_LIMITS, MESSAGE_KINDS} from './model.mjs';
|
|
2
|
+
import {sanitizeText, sanitizeMessage, clipCodePoints} from './policy.mjs';
|
|
3
|
+
|
|
4
|
+
export const PLATFORM_COLORS = Object.freeze({codex:'36', claude:'38;5;208', other:'2'});
|
|
5
|
+
|
|
6
|
+
export function tagColor(event) {
|
|
7
|
+
if (event.kind === 'command') return event.exitCode == null || event.exitCode === 0 ? '38;5;242' : '31';
|
|
8
|
+
return ({user:'1;35', agent:'32', file_change:'33', tool:'38;5;250', spawn:'38;5;141',
|
|
9
|
+
dispatch:'38;5;137', approval:'1;4', turn_failed:'31', error:'31', tool_failed:'31'})[event.kind] ?? '2';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function kindTag(event) {
|
|
13
|
+
if (event.kind === 'agent') {
|
|
14
|
+
const name = event.role || (event.source === 'subagent' && event.label?.includes('/') ? event.label.split('/').at(-1) : '');
|
|
15
|
+
const safeName = clipCodePoints(sanitizeText(name), WATCH_LIMITS.agentNameMax).text;
|
|
16
|
+
return safeName ? `AGENT(${safeName})` : 'AGENT';
|
|
17
|
+
}
|
|
18
|
+
if (event.kind === 'command') return event.exitCode == null ? 'CMD' : `CMD(${event.exitCode})`;
|
|
19
|
+
return ({session:'SESSION', settings:'SETTINGS', turn_start:'TURN', turn_end:'END',
|
|
20
|
+
turn_failed:'FAIL', user:'USER', agent:'AGENT', reasoning:'THINK', tool:'TOOL',
|
|
21
|
+
tool_failed:'TOOL!', file_change:'EDIT', spawn:'SPAWN', approval:'APPROVE',
|
|
22
|
+
dispatch:'DISPATCH', error:'ERROR', other:'ITEM', notice:'NOTICE'})[event.kind] ?? 'ITEM';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function eventText(event, verbose) {
|
|
26
|
+
if (event.kind === 'tool' || event.kind === 'tool_failed') {
|
|
27
|
+
return [event.tool, event.text].map(sanitizeText).filter(Boolean).join(' ');
|
|
28
|
+
}
|
|
29
|
+
if (event.kind === 'file_change') {
|
|
30
|
+
return [event.text, ...(event.paths ?? [])].map(sanitizeText).filter(Boolean).join(' ');
|
|
31
|
+
}
|
|
32
|
+
if (event.kind !== 'dispatch') return sanitizeText(event.text);
|
|
33
|
+
return sanitizeText([event.state, event.route, event.workflowId,
|
|
34
|
+
event.reasons?.join(','), verbose && event.hostPlatform ? `host=${event.hostPlatform}` : undefined,
|
|
35
|
+
].filter(Boolean).join(' '));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function renderHuman(event, {width, color, verbose}) {
|
|
39
|
+
let date = new Date(event.ts);
|
|
40
|
+
if (Number.isNaN(date.getTime())) date = new Date(event.observedAt);
|
|
41
|
+
const time = Number.isNaN(date.getTime()) ? '00:00:00' : date.toLocaleTimeString('en-GB', {hour12:false});
|
|
42
|
+
const platform = sanitizeText(event.source === 'watch' ? 'watch' : event.platform ?? 'arc').slice(0,6).padEnd(6);
|
|
43
|
+
const tag = sanitizeText(kindTag(event)).padEnd(8);
|
|
44
|
+
const prefix = `${time} ${platform} ${tag} `;
|
|
45
|
+
const message = MESSAGE_KINDS.includes(event.kind);
|
|
46
|
+
const text = message ? sanitizeMessage(event.text) : eventText(event, verbose);
|
|
47
|
+
const messageBody = text.split('\n').map((line, i) => !line ? '' : (i ? ' '.repeat([...prefix].length) : '') + line).join('\n');
|
|
48
|
+
const plain = message ? (text ? prefix + messageBody : prefix.trimEnd()) : clipCodePoints(prefix + text, width).text;
|
|
49
|
+
if (!color || (!message && [...prefix].length > width)) return plain;
|
|
50
|
+
const failure = ['turn_failed','tool_failed','error'].includes(event.kind) || (event.kind === 'command' && event.exitCode != null && event.exitCode !== 0);
|
|
51
|
+
const paint = (value, code) => `\x1b[${code}m${value}\x1b[0m`;
|
|
52
|
+
const platformColor = event.source === 'watch' ? PLATFORM_COLORS.other : PLATFORM_COLORS[event.platform] ?? PLATFORM_COLORS.other;
|
|
53
|
+
const paintedTag = paint(tag.trimEnd(), tagColor(event));
|
|
54
|
+
const padding = tag.slice(tag.trimEnd().length);
|
|
55
|
+
const clippedText = message ? messageBody : [...plain].slice([...prefix].length).join('');
|
|
56
|
+
if (message && !text) return `${time} ${paint(platform, platformColor)} ${paintedTag}`;
|
|
57
|
+
return `${time} ${paint(platform, platformColor)} ${paintedTag}${padding} ${failure && clippedText ? paint(clippedText, 31) : clippedText}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function renderJson(event) {
|
|
61
|
+
const fields = ['ts','observedAt','platform','source','origin','sessionId','parentId','depth',
|
|
62
|
+
'label','role','dispatchId','workflowId','kind','text','truncated','exitCode','tool','paths',
|
|
63
|
+
'model','effort','approval','state','reasons','route','hostPlatform','cwd'];
|
|
64
|
+
const output = {v:1};
|
|
65
|
+
for (const key of fields) {
|
|
66
|
+
const value = event[key];
|
|
67
|
+
if (value == null) continue;
|
|
68
|
+
if (key === 'platform' && event.source === 'watch') continue;
|
|
69
|
+
if (key === 'truncated' && MESSAGE_KINDS.includes(event.kind)) continue;
|
|
70
|
+
if (key === 'text' && MESSAGE_KINDS.includes(event.kind)) {
|
|
71
|
+
output.text = sanitizeMessage(value);
|
|
72
|
+
} else if (key === 'text') {
|
|
73
|
+
const clipped = clipCodePoints(sanitizeText(value), WATCH_LIMITS.jsonTextMax);
|
|
74
|
+
output.text = clipped.text;
|
|
75
|
+
if (clipped.truncated) output.truncated = true;
|
|
76
|
+
} else if (key === 'truncated') {
|
|
77
|
+
if (value || output.truncated) output.truncated = true;
|
|
78
|
+
} else if (Array.isArray(value)) {
|
|
79
|
+
output[key] = (key === 'paths' ? value.slice(0, WATCH_LIMITS.pathsMax) : value)
|
|
80
|
+
.filter(item => item != null).map(item => sanitizeText(item));
|
|
81
|
+
} else {
|
|
82
|
+
output[key] = typeof value === 'string' ? sanitizeText(value) : value;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return JSON.stringify(output);
|
|
86
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {GIT_ALLOWED} from './git.mjs';
|
|
4
|
+
|
|
5
|
+
const scopeCaches = new WeakMap();
|
|
6
|
+
|
|
7
|
+
// Resolve the existing ancestor too: a transcript cwd can name a deleted child.
|
|
8
|
+
function realPath(file) {
|
|
9
|
+
let current = path.resolve(file);
|
|
10
|
+
const suffix = [];
|
|
11
|
+
for (;;) {
|
|
12
|
+
try { return path.join(fs.realpathSync(current), ...suffix); }
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (!['ENOENT','ENOTDIR'].includes(error.code)) return path.resolve(file);
|
|
15
|
+
const parent = path.dirname(current);
|
|
16
|
+
if (parent === current) return path.resolve(file);
|
|
17
|
+
suffix.unshift(path.basename(current)); current = parent;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function worktreeRoots(output) {
|
|
23
|
+
return [...new Set(output.split('\0').filter(field => field.startsWith('worktree ')).map(field => realPath(field.slice(9))))];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function resolveScope({mode, dir, session, git}) {
|
|
27
|
+
if (mode === 'all') return {kind:'all', roots:[], gitCommonDir:null, description:'All sessions'};
|
|
28
|
+
if (mode === 'session') return {kind:'session', roots:[], gitCommonDir:null, description:`Session ${session}`};
|
|
29
|
+
const root = realPath(dir), top = await git(root, GIT_ALLOWED[0]);
|
|
30
|
+
if (top !== null) {
|
|
31
|
+
const common = await git(root, GIT_ALLOWED[1]), trees = await git(root, GIT_ALLOWED[2]);
|
|
32
|
+
if (common !== null && trees !== null) {
|
|
33
|
+
const roots = worktreeRoots(trees);
|
|
34
|
+
return {kind:'repo', roots:roots.length ? roots : [realPath(top.trim())], gitCommonDir:realPath(common.trim()), description:`Repository ${top.trim()}`};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return {kind:'dir', roots:[root], gitCommonDir:null, description:`Directory ${root}`};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function refreshScope(scope, git) {
|
|
41
|
+
if (scope.kind !== 'repo') return scope;
|
|
42
|
+
const output = await git(scope.roots[0], GIT_ALLOWED[2]);
|
|
43
|
+
if (output === null) return scope;
|
|
44
|
+
const roots = worktreeRoots(output);
|
|
45
|
+
return roots.length ? {...scope, roots} : scope;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function inScope(scope, cwd) {
|
|
49
|
+
if (scope.kind === 'all' || scope.kind === 'session') return true;
|
|
50
|
+
let cache = scopeCaches.get(scope);
|
|
51
|
+
if (!cache) { cache = new Map(); scopeCaches.set(scope, cache); }
|
|
52
|
+
if (cache.has(cwd)) return cache.get(cwd);
|
|
53
|
+
const candidates = [path.resolve(cwd), realPath(cwd)];
|
|
54
|
+
const inside = scope.roots.some(root => [path.resolve(root), realPath(root)].some(base => candidates.some(candidate => candidate === base || candidate.startsWith(base.endsWith(path.sep) ? base : base + path.sep))));
|
|
55
|
+
cache.set(cwd, inside);
|
|
56
|
+
return inside;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const normalizeSlug = value => value.replace(/[^A-Za-z0-9]/g, '-');
|
|
60
|
+
|
|
61
|
+
export function claudeDirPrefixes(scope) {
|
|
62
|
+
if (scope.kind === 'all' || scope.kind === 'session') return null;
|
|
63
|
+
return [...new Set(scope.roots.flatMap(root => [normalizeSlug(root), normalizeSlug(realPath(root))]))];
|
|
64
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import fsp from 'node:fs/promises';
|
|
2
|
+
import {createLineParser} from '../runtime/role-observe.mjs';
|
|
3
|
+
import {WATCH_LIMITS} from './model.mjs';
|
|
4
|
+
|
|
5
|
+
const parserFor = onLine => createLineParser(onLine, {maxLineBytes:WATCH_LIMITS.maxLineBytes});
|
|
6
|
+
|
|
7
|
+
export function createTail(file, {offset = 0} = {}) {
|
|
8
|
+
let position = offset, lines = [], parser = parserFor(line => lines.push(line));
|
|
9
|
+
return {
|
|
10
|
+
get offset() { return position; },
|
|
11
|
+
async poll() {
|
|
12
|
+
lines = [];
|
|
13
|
+
let handle, reset = false;
|
|
14
|
+
try {
|
|
15
|
+
handle = await fsp.open(file, 'r');
|
|
16
|
+
const {size} = await handle.stat();
|
|
17
|
+
if (size < position) {
|
|
18
|
+
position = 0; reset = true;
|
|
19
|
+
parser = parserFor(line => lines.push(line));
|
|
20
|
+
}
|
|
21
|
+
const buffer = Buffer.alloc(Math.min(WATCH_LIMITS.maxReadBytesPerTick, size - position));
|
|
22
|
+
const {bytesRead} = buffer.length ? await handle.read(buffer, 0, buffer.length, position) : {bytesRead:0};
|
|
23
|
+
position += bytesRead;
|
|
24
|
+
parser.push(buffer.subarray(0, bytesRead));
|
|
25
|
+
return {lines, reset, gone:false, overflowed:parser.overflow};
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (error.code !== 'ENOENT') throw error;
|
|
28
|
+
return {lines, reset, gone:true, overflowed:false};
|
|
29
|
+
} finally { await handle?.close(); }
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function readTail(file, {maxBytes = WATCH_LIMITS.historyBytes} = {}) {
|
|
35
|
+
const handle = await fsp.open(file, 'r');
|
|
36
|
+
try {
|
|
37
|
+
const {size} = await handle.stat(), start = Math.max(0, size - maxBytes);
|
|
38
|
+
const buffer = Buffer.alloc(size - start);
|
|
39
|
+
const {bytesRead} = await handle.read(buffer, 0, buffer.length, start);
|
|
40
|
+
let chunk = buffer.subarray(0, bytesRead), parseStart = start;
|
|
41
|
+
if (start > 0) {
|
|
42
|
+
const newline = chunk.indexOf(10);
|
|
43
|
+
if (newline < 0) return {lines:[], endOffset:start};
|
|
44
|
+
parseStart += newline + 1;
|
|
45
|
+
chunk = chunk.subarray(newline + 1);
|
|
46
|
+
}
|
|
47
|
+
// Leave incomplete bytes for createTail, whose parser survives live polls.
|
|
48
|
+
const consumed = chunk.lastIndexOf(10) + 1;
|
|
49
|
+
const lines = [], parser = parserFor(line => lines.push(line));
|
|
50
|
+
parser.push(chunk.subarray(0, consumed));
|
|
51
|
+
return {lines, endOffset:parseStart + consumed};
|
|
52
|
+
} finally { await handle.close(); }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function readHead(file, {maxBytes, accept}) {
|
|
56
|
+
let handle;
|
|
57
|
+
try {
|
|
58
|
+
handle = await fsp.open(file, 'r');
|
|
59
|
+
const {size} = await handle.stat();
|
|
60
|
+
let found = null, position = 0;
|
|
61
|
+
const parser = parserFor(line => { if (found === null && accept(line)) found = line; });
|
|
62
|
+
while (position < Math.min(size, maxBytes) && found === null) {
|
|
63
|
+
const buffer = Buffer.alloc(Math.min(65536, size - position, maxBytes - position));
|
|
64
|
+
const {bytesRead} = await handle.read(buffer, 0, buffer.length, position);
|
|
65
|
+
if (!bytesRead) break;
|
|
66
|
+
position += bytesRead;
|
|
67
|
+
parser.push(buffer.subarray(0, bytesRead));
|
|
68
|
+
}
|
|
69
|
+
if (position === size) parser.end();
|
|
70
|
+
return found;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (!['ENOENT','EACCES'].includes(error.code)) throw error;
|
|
73
|
+
return null;
|
|
74
|
+
} finally { await handle?.close(); }
|
|
75
|
+
}
|