ctxtop 0.0.0-stage → 4.1.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/.antigravity-plugin/plugin.json +36 -0
- package/.claude-plugin/plugin.json +18 -0
- package/CHECKSUMS.sha256 +166 -0
- package/LICENSE +27 -0
- package/README.md +350 -2
- package/SECURITY.md +22 -0
- package/agents/ah-flash-scout.md +25 -0
- package/assets/advanced_view_loaded_420px.png +0 -0
- package/assets/hero-demo.svg +188 -0
- package/assets/logo.svg +19 -0
- package/assets/simple_view_lean_420px.png +0 -0
- package/assets/simple_view_loaded_420px.png +0 -0
- package/assets/social-preview.svg +72 -0
- package/assets/zone_c_step3_shield_full.png +0 -0
- package/bin/ah-statusline.mjs +175 -0
- package/bin/cli.mjs +3 -0
- package/bin/ctxtop.mjs +1799 -0
- package/commands/cost-radar.md +11 -0
- package/commands/evolve.md +10 -0
- package/commands/handoff.md +10 -0
- package/commands/horizon.md +10 -0
- package/commands/inspect.md +10 -0
- package/commands/precheck.md +10 -0
- package/config/default_flightcheck.json +133 -0
- package/config/modelCatalog.json +785 -0
- package/config/platformQuirks.json +19 -0
- package/hooks/handlers/compact-lifecycle.mjs +577 -0
- package/hooks/handlers/config-change.mjs +402 -0
- package/hooks/handlers/hookIo.mjs +311 -0
- package/hooks/handlers/hookIpcClient.mjs +83 -0
- package/hooks/handlers/post-model-switch.mjs +111 -0
- package/hooks/handlers/post-tool-use.mjs +366 -0
- package/hooks/handlers/pre-model-switch.mjs +1048 -0
- package/hooks/handlers/pre-tool-use.mjs +632 -0
- package/hooks/handlers/stop-gate.mjs +526 -0
- package/hooks/handlers/user-prompt-submit.mjs +367 -0
- package/hooks/hooks.json +131 -0
- package/hooks/post_invocation.mjs +13 -0
- package/hooks/post_tool_use.mjs +25 -0
- package/hooks/pre_invocation.mjs +14 -0
- package/hooks/pre_tool_use.mjs +97 -0
- package/hooks/register.js +726 -0
- package/hooks/session_start.mjs +24 -0
- package/hooks/stop.mjs +23 -0
- package/hooks.json +56 -0
- package/package.json +110 -4
- package/packages/antigravity/package.json +14 -0
- package/packages/antigravity/src/index.mjs +1 -0
- package/packages/claude-code/package.json +14 -0
- package/packages/claude-code/src/index.mjs +2 -0
- package/packages/core/package.json +14 -0
- package/packages/core/src/index.mjs +1 -0
- package/packages/engine-node/package.json +14 -0
- package/packages/engine-node/src/index.mjs +1 -0
- package/packages/mcp/package.json +17 -0
- package/packages/mcp/src/server.mjs +396 -0
- package/packages/pro/LICENSE-COMMERCIAL.md +19 -0
- package/packages/pro/README-SIGNING.md +30 -0
- package/packages/pro/package.json +16 -0
- package/packages/pro/src/index.mjs +3 -0
- package/packages/pro/src/team_finops_pack.mjs +241 -0
- package/packages/pro/workers/polar_webhook_worker.mjs +215 -0
- package/plugin.json +36 -0
- package/rules/AGENTS.md +10 -0
- package/sidecars/horizon-daemon/ipcServer.mjs +24 -0
- package/sidecars/horizon-daemon/server.mjs +2038 -0
- package/sidecars/horizon-daemon/sidecar.json +19 -0
- package/skills/cost-radar/SKILL.md +21 -0
- package/skills/evolve/SKILL.md +21 -0
- package/skills/handoff/SKILL.md +50 -0
- package/skills/horizon/SKILL.md +17 -0
- package/skills/inspect/SKILL.md +18 -0
- package/skills/precheck/SKILL.md +44 -0
- package/src/adapters/antigravity/index.mjs +109 -0
- package/src/adapters/claude-code/index.mjs +194 -0
- package/src/cli/cost_radar.mjs +70 -0
- package/src/cli/doctor.mjs +351 -0
- package/src/cli/horizon_hud.mjs +888 -0
- package/src/cli/init.mjs +425 -0
- package/src/cli/postmortem.mjs +515 -0
- package/src/coach/cacheGravityRouter.mjs +12 -0
- package/src/config/modelCatalog.json +704 -0
- package/src/config/platformQuirks.json +19 -0
- package/src/core/action_queue.mjs +264 -0
- package/src/core/ag_bridge.mjs +346 -0
- package/src/core/atomic_io.mjs +124 -0
- package/src/core/baseline.mjs +607 -0
- package/src/core/blindspot_sensors.mjs +399 -0
- package/src/core/bucket_b_runtime.mjs +1200 -0
- package/src/core/classifier.mjs +1757 -0
- package/src/core/coach/circuit_breakers.mjs +650 -0
- package/src/core/coach/contradiction_auditor.mjs +239 -0
- package/src/core/coach/effort_advisor.mjs +143 -0
- package/src/core/coach/handoff_hydrator.mjs +483 -0
- package/src/core/coach/progressive_checkpointer.mjs +551 -0
- package/src/core/coach/prompt_decomposer_coach.mjs +141 -0
- package/src/core/coach/reflector_queue.mjs +505 -0
- package/src/core/coach/saved_quota_ledger.mjs +615 -0
- package/src/core/coach/subagent_ipi_sanitizer.mjs +200 -0
- package/src/core/coach/subagent_phantom_auditor.mjs +261 -0
- package/src/core/coach/task_quota_router.mjs +955 -0
- package/src/core/coach/turn_zero_gate.mjs +574 -0
- package/src/core/compaction_coach.mjs +1126 -0
- package/src/core/config.mjs +214 -0
- package/src/core/constants.mjs +1987 -0
- package/src/core/detectors_det01_to_16.mjs +1523 -0
- package/src/core/doc_indexer.mjs +338 -0
- package/src/core/episodic_memory.mjs +135 -0
- package/src/core/fts5_output_compressor.mjs +359 -0
- package/src/core/git_ledger.mjs +189 -0
- package/src/core/handoff_audit.mjs +558 -0
- package/src/core/history.mjs +221 -0
- package/src/core/hook_diagnostics.mjs +158 -0
- package/src/core/horizon_classifier.mjs +326 -0
- package/src/core/horizon_config.mjs +802 -0
- package/src/core/host_watchdog.mjs +200 -0
- package/src/core/layer5_hygiene.mjs +1084 -0
- package/src/core/log_resolver.mjs +231 -0
- package/src/core/mcp_auditor.mjs +138 -0
- package/src/core/micro_skills_store.mjs +91 -0
- package/src/core/offenders.mjs +161 -0
- package/src/core/parser.mjs +1997 -0
- package/src/core/path_canonicalizer.mjs +73 -0
- package/src/core/paths.mjs +311 -0
- package/src/core/prefix_cache_shield.mjs +386 -0
- package/src/core/pro_license.mjs +385 -0
- package/src/core/prompt_rubric.mjs +518 -0
- package/src/core/pruner.mjs +433 -0
- package/src/core/quota.mjs +1860 -0
- package/src/core/rsi_instinct_engine.mjs +1197 -0
- package/src/core/rsl_clash_auditor.mjs +423 -0
- package/src/core/rsl_distiller.mjs +372 -0
- package/src/core/rsl_ledger.mjs +969 -0
- package/src/core/rsl_triage.mjs +909 -0
- package/src/core/sanitizer.mjs +121 -0
- package/src/core/session_auditor.mjs +658 -0
- package/src/core/session_forensics.mjs +303 -0
- package/src/core/session_timeline.mjs +358 -0
- package/src/core/settings.mjs +99 -0
- package/src/core/shared/detectors.mjs +1342 -0
- package/src/core/shared/fnv1a64.mjs +85 -0
- package/src/core/shared/handoff_schema.mjs +123 -0
- package/src/core/shared/index.mjs +5 -0
- package/src/core/shared/ipi_sanitizer.mjs +165 -0
- package/src/core/shared/micro_skills.mjs +1 -0
- package/src/core/shared/plugin_validator.mjs +202 -0
- package/src/core/shared/prompt_hygiene_governor.mjs +1 -0
- package/src/core/shield_manager.mjs +1417 -0
- package/src/core/sidecar_store.mjs +302 -0
- package/src/core/sprint_metrics.mjs +366 -0
- package/src/core/sqlite_client.mjs +283 -0
- package/src/core/storage_doctor.mjs +358 -0
- package/src/core/subagents.mjs +609 -0
- package/src/core/swarm_topology.mjs +360 -0
- package/src/core/task_quota_router.mjs +1656 -0
- package/src/core/telemetry/cache_ttl_watchdog.mjs +284 -0
- package/src/core/telemetry/scope_lock_registry.mjs +578 -0
- package/src/core/telemetry/shadow_file_detector.mjs +291 -0
- package/src/core/telemetry/trajectory_watcher.mjs +259 -0
- package/src/core/timeline_svg.mjs +236 -0
- package/src/core/tool_surgeon.mjs +305 -0
- package/src/core/trajectory_stitcher.mjs +645 -0
- package/src/core/u_shaped_optimizer.mjs +789 -0
- package/src/core/wal_hygiene.mjs +233 -0
- package/src/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
- package/src/core-isomorphic/catalog/index.mjs +1 -0
- package/src/core-isomorphic/coach/cacheGravityRouter.mjs +1547 -0
- package/src/core-isomorphic/coach/microSkills.mjs +1 -0
- package/src/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
- package/src/core-isomorphic/coach/repoCalibration.mjs +254 -0
- package/src/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
- package/src/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
- package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +305 -0
- package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +464 -0
- package/src/core-isomorphic/config/anthropicModelCatalog.json +606 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +1268 -0
- package/src/core-isomorphic/config/horizonConfig.mjs +1055 -0
- package/src/core-isomorphic/control/callTree.mjs +338 -0
- package/src/core-isomorphic/control/coachLatches.mjs +126 -0
- package/src/core-isomorphic/control/controlStateMachine.mjs +205 -0
- package/src/core-isomorphic/control/index.mjs +49 -0
- package/src/core-isomorphic/control/swarmWorktree.mjs +109 -0
- package/src/core-isomorphic/control/toolSuspension.mjs +197 -0
- package/src/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
- package/src/core-isomorphic/diagnostics/index.mjs +9 -0
- package/src/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
- package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +888 -0
- package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +369 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +256 -0
- package/src/core-isomorphic/economics/driftLatency.mjs +433 -0
- package/src/core-isomorphic/economics/dualTtl.mjs +198 -0
- package/src/core-isomorphic/economics/index.mjs +47 -0
- package/src/core-isomorphic/economics/retentionRadar.mjs +348 -0
- package/src/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
- package/src/core-isomorphic/guards/bucketBGamma.mjs +442 -0
- package/src/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
- package/src/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1184 -0
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +572 -0
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +913 -0
- package/src/core-isomorphic/guards/docIndexCore.mjs +446 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +317 -0
- package/src/core-isomorphic/guards/slopsquatGate.mjs +551 -0
- package/src/core-isomorphic/index.js +27 -0
- package/src/core-isomorphic/telemetry/otelExporter.mjs +187 -0
- package/src/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
- package/src/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
- package/src/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
- package/src/core-isomorphic/telemetry/telemetryBridge.mjs +1156 -0
- package/src/core-isomorphic/ui/brailleSparkline.mjs +142 -0
- package/src/core-isomorphic/ui/costRadarSvg.mjs +276 -0
- package/src/core-isomorphic/ui/horizonGrid.mjs +475 -0
- package/src/core-isomorphic/ui/horizonPanelHtml.mjs +1237 -0
- package/src/core-isomorphic/ui/modViews.mjs +1504 -0
- package/src/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
- package/src/core-isomorphic/ustf/index.mjs +7 -0
- package/src/core-isomorphic/ustf/normalizer.mjs +512 -0
- package/src/core-isomorphic/ustf/schema.mjs +186 -0
- package/src/core-isomorphic/utils/runtime.mjs +962 -0
- package/src/demo/synthetic_states.mjs +661 -0
- package/src/diagnostics/bugReportGenerator.mjs +253 -0
- package/src/diagnostics/claudeHookCapabilities.mjs +396 -0
- package/src/diagnostics/doctorEngine.mjs +295 -0
- package/src/engine-node/db/index.mjs +58 -0
- package/src/engine-node/db/jsonl_fallback.mjs +184 -0
- package/src/engine-node/db/probe.mjs +342 -0
- package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
- package/src/engine-node/index.mjs +12 -0
- package/src/engine-node/ipc/client.mjs +447 -0
- package/src/engine-node/ipc/index.mjs +22 -0
- package/src/engine-node/ipc/protocol.mjs +177 -0
- package/src/engine-node/ipc/server.mjs +445 -0
- package/src/engine-node/offloader/index.mjs +6 -0
- package/src/engine-node/offloader/tool_offloader.mjs +241 -0
- package/src/engine-node/sensors/blindspots.mjs +376 -0
- package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
- package/src/engine-node/sensors/index.mjs +7 -0
- package/src/hooks/antigravity_rsl_hook.mjs +250 -0
- package/src/hooks/claude_rsl_hook.mjs +288 -0
- package/src/hooks/portfolio_runtime.mjs +858 -0
- package/src/hooks/post_invocation.mjs +611 -0
- package/src/hooks/pre_invocation.mjs +1725 -0
- package/src/hooks/session_start.mjs +199 -0
- package/src/shims/agy_cli_compressor.mjs +705 -0
- package/src/shims/agy_git_guard.mjs +749 -0
- package/src/shims/ah_log_query.mjs +39 -0
- package/src/ui/costRadarSvg.mjs +6 -0
- package/src/ui/horizonGrid.mjs +1 -0
- package/src/ui/horizonPanelHtml.mjs +1 -0
- package/src/ui/horizon_panel.css +167 -0
- package/src/ui/horizon_panel.js +212 -0
- package/src/ui/modViews.mjs +1 -0
- package/src/utils/modelSwitchTelemetry.mjs +308 -0
- package/src/utils/nodeIo.mjs +59 -0
- package/src/utils/runtime.mjs +1 -0
- package/src/utils/sessionCalibration.mjs +657 -0
- package/src/webview/advanced_view.html +1808 -0
- package/src/webview/horizon_panel.html +12397 -0
- package/src/webview/registry.mjs +80 -0
- package/src/webview/simple_view.html +1370 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { scopesOverlap } from './telemetry/scope_lock_registry.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Sprint 2.1 — Subagent `/swarm` & Git Worktree Topology.
|
|
7
|
+
*
|
|
8
|
+
* Pure roster reconstruction from transcript tool calls (`swarm`, `invoke_subagent`,
|
|
9
|
+
* `manage_subagents`, `send_message`) joined with a read-only `git worktree list`
|
|
10
|
+
* probe and the scope-lock registry (DET-20 overlap visibility).
|
|
11
|
+
*
|
|
12
|
+
* Honesty rules:
|
|
13
|
+
* - Worker state is only reported as `working` when there is recent activity evidence;
|
|
14
|
+
* otherwise it is `waiting` (parked, $0 while asleep). Explicit `swarm status` output
|
|
15
|
+
* in the transcript overrides inference.
|
|
16
|
+
* - Warm-prefix tokens are never fabricated; unknown stays 0 and `warmKnown=false`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export const SWARM_TOPOLOGY_CONSTANTS = Object.freeze({
|
|
20
|
+
WORKING_WINDOW_MS: 120000, // activity within 2 min => working
|
|
21
|
+
CACHE_TTL_MS: 300000, // 5 min prefix cache window for parked workers
|
|
22
|
+
WORKTREE_CACHE_MS: 15000,
|
|
23
|
+
GIT_TIMEOUT_MS: 1200,
|
|
24
|
+
STATUS_LOOKAHEAD_STEPS: 3
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const VALID_STATES = new Set(['working', 'waiting', 'stopped', 'killed']);
|
|
28
|
+
|
|
29
|
+
function stepTimestampMs(step = {}) {
|
|
30
|
+
if (typeof step.timestampMs === 'number' && step.timestampMs > 0) return step.timestampMs;
|
|
31
|
+
const raw = step.createdAt || step.timestamp || null;
|
|
32
|
+
const parsed = raw ? Date.parse(raw) : NaN;
|
|
33
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function argOf(args, ...keys) {
|
|
37
|
+
for (const k of keys) {
|
|
38
|
+
if (args && args[k] !== undefined && args[k] !== null && args[k] !== '') return args[k];
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function expandRosterNames(name, n, tasks) {
|
|
44
|
+
const base = String(name || '').trim();
|
|
45
|
+
if (!base) return [];
|
|
46
|
+
if (Array.isArray(tasks) && tasks.length > 0) {
|
|
47
|
+
return tasks.map((_, idx) => `${base}-${idx + 1}`);
|
|
48
|
+
}
|
|
49
|
+
const count = Number(n);
|
|
50
|
+
if (Number.isFinite(count) && count > 1) {
|
|
51
|
+
return Array.from({ length: Math.floor(count) }, (_, idx) => `${base}-${idx + 1}`);
|
|
52
|
+
}
|
|
53
|
+
return [base];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function escapeRegex(s) {
|
|
57
|
+
return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Reconstructs the swarm / subagent roster from transcript steps.
|
|
62
|
+
*/
|
|
63
|
+
export function parseSwarmRoster(steps = [], {
|
|
64
|
+
nowMs = Date.now(),
|
|
65
|
+
workingWindowMs = SWARM_TOPOLOGY_CONSTANTS.WORKING_WINDOW_MS,
|
|
66
|
+
cacheTtlMs = SWARM_TOPOLOGY_CONSTANTS.CACHE_TTL_MS
|
|
67
|
+
} = {}) {
|
|
68
|
+
const roster = new Map();
|
|
69
|
+
const statusCalls = [];
|
|
70
|
+
let title = null;
|
|
71
|
+
|
|
72
|
+
const touch = (name, ts, patch = {}) => {
|
|
73
|
+
const key = String(name || '').trim();
|
|
74
|
+
if (!key) return null;
|
|
75
|
+
const existing = roster.get(key) || {
|
|
76
|
+
name: key,
|
|
77
|
+
kind: 'swarm',
|
|
78
|
+
model: 'inherit',
|
|
79
|
+
workspace: 'inherit',
|
|
80
|
+
fork: null,
|
|
81
|
+
addedAtMs: ts || 0,
|
|
82
|
+
lastActiveMs: ts || 0,
|
|
83
|
+
killed: false,
|
|
84
|
+
explicitState: null,
|
|
85
|
+
messages: 0
|
|
86
|
+
};
|
|
87
|
+
const next = { ...existing, ...patch };
|
|
88
|
+
if (ts && ts > next.lastActiveMs) next.lastActiveMs = ts;
|
|
89
|
+
roster.set(key, next);
|
|
90
|
+
return next;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
for (let i = 0; i < steps.length; i++) {
|
|
94
|
+
const step = steps[i] || {};
|
|
95
|
+
const ts = stepTimestampMs(step);
|
|
96
|
+
const calls = Array.isArray(step.toolCalls) ? step.toolCalls : [];
|
|
97
|
+
for (const call of calls) {
|
|
98
|
+
const toolName = String(call?.name || call?.tool || '').toLowerCase();
|
|
99
|
+
const args = call?.args || call?.parameters || {};
|
|
100
|
+
|
|
101
|
+
if (toolName === 'swarm') {
|
|
102
|
+
const action = String(argOf(args, 'action', 'Action') || '').toLowerCase();
|
|
103
|
+
const name = argOf(args, 'name', 'Name');
|
|
104
|
+
if (action === 'add') {
|
|
105
|
+
const names = expandRosterNames(name, argOf(args, 'n'), argOf(args, 'tasks'));
|
|
106
|
+
for (const nm of names) {
|
|
107
|
+
touch(nm, ts, {
|
|
108
|
+
kind: 'swarm',
|
|
109
|
+
model: String(argOf(args, 'model', 'Model') || 'inherit'),
|
|
110
|
+
workspace: String(argOf(args, 'workspace', 'Workspace') || 'inherit'),
|
|
111
|
+
fork: argOf(args, 'fork') ? String(argOf(args, 'fork')) : null,
|
|
112
|
+
killed: false,
|
|
113
|
+
explicitState: 'working'
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
} else if (action === 'kill' && name) {
|
|
117
|
+
touch(name, ts, { killed: true, explicitState: 'killed' });
|
|
118
|
+
} else if (action === 'model' && name) {
|
|
119
|
+
touch(name, ts, { model: String(argOf(args, 'model', 'Model') || 'inherit') });
|
|
120
|
+
} else if (action === 'status') {
|
|
121
|
+
statusCalls.push(i);
|
|
122
|
+
} else if (action === 'title') {
|
|
123
|
+
title = String(argOf(args, 'title') || '') || title;
|
|
124
|
+
}
|
|
125
|
+
} else if (toolName === 'invoke_subagent') {
|
|
126
|
+
const role = String(argOf(args, 'Role', 'role') || 'Subagent');
|
|
127
|
+
const convId = call?.result?.conversationId || call?.conversationId || null;
|
|
128
|
+
touch(convId ? `${role}#${String(convId).slice(0, 8)}` : role, ts, {
|
|
129
|
+
kind: 'subagent',
|
|
130
|
+
model: String(argOf(args, 'Model', 'model') || 'inherit'),
|
|
131
|
+
workspace: String(argOf(args, 'Workspace', 'workspace') || 'inherit'),
|
|
132
|
+
conversationId: convId,
|
|
133
|
+
killed: false,
|
|
134
|
+
explicitState: 'working'
|
|
135
|
+
});
|
|
136
|
+
} else if (toolName === 'manage_subagents') {
|
|
137
|
+
const action = String(argOf(args, 'Action', 'action') || '').toLowerCase();
|
|
138
|
+
const ids = Array.isArray(args.ConversationIds) ? args.ConversationIds.map(String) : [];
|
|
139
|
+
for (const w of roster.values()) {
|
|
140
|
+
if (w.kind !== 'subagent') continue;
|
|
141
|
+
const matches = action === 'kill_all' || (action === 'kill' && w.conversationId && ids.includes(String(w.conversationId)));
|
|
142
|
+
if (matches) touch(w.name, ts, { killed: true, explicitState: 'killed' });
|
|
143
|
+
}
|
|
144
|
+
} else if (toolName === 'send_message') {
|
|
145
|
+
const recipient = String(argOf(args, 'Recipient', 'recipient') || '');
|
|
146
|
+
for (const w of roster.values()) {
|
|
147
|
+
if (w.name === recipient || (w.conversationId && String(w.conversationId) === recipient)) {
|
|
148
|
+
touch(w.name, ts, { messages: (w.messages || 0) + 1, explicitState: w.killed ? 'killed' : 'working' });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Explicit `swarm status` output overrides inferred state.
|
|
156
|
+
for (const idx of statusCalls) {
|
|
157
|
+
const end = Math.min(steps.length, idx + 1 + SWARM_TOPOLOGY_CONSTANTS.STATUS_LOOKAHEAD_STEPS);
|
|
158
|
+
for (let j = idx; j < end; j++) {
|
|
159
|
+
const text = String(steps[j]?.content || '');
|
|
160
|
+
if (!text) continue;
|
|
161
|
+
for (const w of roster.values()) {
|
|
162
|
+
const re = new RegExp(`\\b${escapeRegex(w.name)}\\b[^\\n]{0,120}?\\b(working|waiting|stopped|killed)\\b`, 'i');
|
|
163
|
+
const m = text.match(re);
|
|
164
|
+
if (m) {
|
|
165
|
+
const state = m[1].toLowerCase();
|
|
166
|
+
roster.set(w.name, { ...w, explicitState: state, killed: state === 'killed', statusAtMs: stepTimestampMs(steps[j]) || w.lastActiveMs });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const workers = Array.from(roster.values()).map((w) => {
|
|
173
|
+
const idleMs = w.lastActiveMs > 0 ? Math.max(0, nowMs - w.lastActiveMs) : null;
|
|
174
|
+
let state;
|
|
175
|
+
if (w.killed) state = 'killed';
|
|
176
|
+
else if (w.explicitState === 'stopped') state = 'stopped';
|
|
177
|
+
else if (w.statusAtMs && VALID_STATES.has(w.explicitState) && w.statusAtMs >= w.lastActiveMs) state = w.explicitState;
|
|
178
|
+
else if (idleMs !== null && idleMs <= workingWindowMs) state = 'working';
|
|
179
|
+
else state = 'waiting';
|
|
180
|
+
|
|
181
|
+
const ttlRemainingSec = (state === 'killed' || state === 'stopped' || idleMs === null)
|
|
182
|
+
? 0
|
|
183
|
+
: Math.max(0, Math.ceil((cacheTtlMs - idleMs) / 1000));
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
name: w.name,
|
|
187
|
+
kind: w.kind,
|
|
188
|
+
model: w.model,
|
|
189
|
+
workspace: w.workspace,
|
|
190
|
+
fork: w.fork,
|
|
191
|
+
conversationId: w.conversationId || null,
|
|
192
|
+
state,
|
|
193
|
+
parked: state === 'waiting',
|
|
194
|
+
idleSec: idleMs === null ? null : Math.round(idleMs / 1000),
|
|
195
|
+
ttlRemainingSec,
|
|
196
|
+
cacheWarm: ttlRemainingSec > 0,
|
|
197
|
+
warmPrefixTokens: 0,
|
|
198
|
+
warmKnown: false,
|
|
199
|
+
messages: w.messages || 0
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
return { title, workers };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Parses `git worktree list --porcelain` output.
|
|
208
|
+
*/
|
|
209
|
+
export function parseWorktreePorcelain(text = '') {
|
|
210
|
+
const out = [];
|
|
211
|
+
let cur = null;
|
|
212
|
+
for (const rawLine of String(text).split(/\r?\n/)) {
|
|
213
|
+
const line = rawLine.trimEnd();
|
|
214
|
+
if (!line) {
|
|
215
|
+
if (cur) out.push(cur);
|
|
216
|
+
cur = null;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const sp = line.indexOf(' ');
|
|
220
|
+
const key = sp === -1 ? line : line.slice(0, sp);
|
|
221
|
+
const val = sp === -1 ? '' : line.slice(sp + 1);
|
|
222
|
+
if (key === 'worktree') {
|
|
223
|
+
if (cur) out.push(cur);
|
|
224
|
+
cur = { path: val, head: null, branch: null, bare: false, detached: false, locked: false, prunable: false };
|
|
225
|
+
} else if (cur) {
|
|
226
|
+
if (key === 'HEAD') cur.head = val.slice(0, 12);
|
|
227
|
+
else if (key === 'branch') cur.branch = val.replace(/^refs\/heads\//, '');
|
|
228
|
+
else if (key === 'bare') cur.bare = true;
|
|
229
|
+
else if (key === 'detached') cur.detached = true;
|
|
230
|
+
else if (key === 'locked') cur.locked = true;
|
|
231
|
+
else if (key === 'prunable') cur.prunable = true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (cur) out.push(cur);
|
|
235
|
+
return out.map((w, idx) => ({ ...w, isMain: idx === 0, name: path.basename(w.path || '') || w.path }));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const worktreeCache = new Map(); // workspaceDir -> { atMs, result }
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Read-only `git worktree list --porcelain` probe (1.2s timeout, 15s memo).
|
|
242
|
+
*/
|
|
243
|
+
export function inspectGitWorktrees(workspaceDir, { nowMs = Date.now(), useCache = true, runner = null } = {}) {
|
|
244
|
+
if (!workspaceDir) return { available: false, worktrees: [], error: 'no-workspace' };
|
|
245
|
+
const cached = worktreeCache.get(workspaceDir);
|
|
246
|
+
if (useCache && cached && nowMs - cached.atMs < SWARM_TOPOLOGY_CONSTANTS.WORKTREE_CACHE_MS) {
|
|
247
|
+
return cached.result;
|
|
248
|
+
}
|
|
249
|
+
let result;
|
|
250
|
+
try {
|
|
251
|
+
const exec = runner || ((args) => execFileSync('git', args, {
|
|
252
|
+
cwd: workspaceDir,
|
|
253
|
+
timeout: SWARM_TOPOLOGY_CONSTANTS.GIT_TIMEOUT_MS,
|
|
254
|
+
encoding: 'utf8',
|
|
255
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
256
|
+
windowsHide: true
|
|
257
|
+
}));
|
|
258
|
+
const text = exec(['worktree', 'list', '--porcelain']);
|
|
259
|
+
result = { available: true, worktrees: parseWorktreePorcelain(text), error: null };
|
|
260
|
+
} catch (err) {
|
|
261
|
+
if (process.env.HORIZON_DEBUG) console.error('[swarm_topology] git worktree probe failed:', err?.message || err);
|
|
262
|
+
result = { available: false, worktrees: [], error: String(err?.code || err?.message || 'git-error') };
|
|
263
|
+
}
|
|
264
|
+
worktreeCache.set(workspaceDir, { atMs: nowMs, result });
|
|
265
|
+
return result;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Pairwise overlap between active scope leases (DET-20 visibility in the topology view).
|
|
270
|
+
*/
|
|
271
|
+
export function computeLeaseOverlaps(leases = []) {
|
|
272
|
+
const list = Array.isArray(leases) ? leases : [];
|
|
273
|
+
const collisions = [];
|
|
274
|
+
for (let a = 0; a < list.length; a++) {
|
|
275
|
+
for (let b = a + 1; b < list.length; b++) {
|
|
276
|
+
const A = list[a] || {};
|
|
277
|
+
const B = list[b] || {};
|
|
278
|
+
if (!A.trajectoryId || A.trajectoryId === B.trajectoryId) continue;
|
|
279
|
+
const overlapping = [];
|
|
280
|
+
for (const pa of (A.claimedPaths || [])) {
|
|
281
|
+
for (const pb of (B.claimedPaths || [])) {
|
|
282
|
+
if (scopesOverlap(pa, pb)) overlapping.push(pa === pb ? pa : `${pa} ~ ${pb}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (overlapping.length > 0) {
|
|
286
|
+
collisions.push({
|
|
287
|
+
detectorId: 'DET-20',
|
|
288
|
+
trajectories: [A.trajectoryId, B.trajectoryId],
|
|
289
|
+
tools: [A.tool || 'antigravity', B.tool || 'antigravity'],
|
|
290
|
+
overlappingPaths: Array.from(new Set(overlapping)).slice(0, 6)
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return collisions;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Joins roster + worktrees + scope leases into a single topology payload.
|
|
300
|
+
*/
|
|
301
|
+
export function computeSwarmTopology(steps = [], {
|
|
302
|
+
nowMs = Date.now(),
|
|
303
|
+
workspaceDir = null,
|
|
304
|
+
worktrees = null,
|
|
305
|
+
leases = [],
|
|
306
|
+
probeGit = true
|
|
307
|
+
} = {}) {
|
|
308
|
+
const { title, workers } = parseSwarmRoster(steps, { nowMs });
|
|
309
|
+
const wt = Array.isArray(worktrees)
|
|
310
|
+
? { available: true, worktrees, error: null }
|
|
311
|
+
: (probeGit && workspaceDir ? inspectGitWorktrees(workspaceDir, { nowMs }) : { available: false, worktrees: [], error: 'not-probed' });
|
|
312
|
+
|
|
313
|
+
const counts = { working: 0, waiting: 0, stopped: 0, killed: 0 };
|
|
314
|
+
for (const w of workers) counts[w.state] = (counts[w.state] || 0) + 1;
|
|
315
|
+
const live = counts.working + counts.waiting + counts.stopped;
|
|
316
|
+
const parkedWarm = workers.filter((w) => w.state === 'waiting' && w.cacheWarm).length;
|
|
317
|
+
const nearCliff = workers.filter((w) => w.state === 'waiting' && w.ttlRemainingSec > 0 && w.ttlRemainingSec <= 60).length;
|
|
318
|
+
|
|
319
|
+
const leaseList = Array.isArray(leases) ? leases : [];
|
|
320
|
+
const collisions = computeLeaseOverlaps(leaseList);
|
|
321
|
+
const worktreeCount = wt.worktrees.length;
|
|
322
|
+
|
|
323
|
+
const autoExpand = live > 0 || worktreeCount > 1 || collisions.length > 0;
|
|
324
|
+
const mode = live > 0 ? 'swarm' : (worktreeCount > 1 ? 'worktrees' : 'solo');
|
|
325
|
+
|
|
326
|
+
const parts = [];
|
|
327
|
+
if (live > 0) parts.push(`${live} worker${live === 1 ? '' : 's'} (${counts.working} working · ${counts.waiting} parked)`);
|
|
328
|
+
else parts.push('Solo agent');
|
|
329
|
+
if (worktreeCount > 1) parts.push(`${worktreeCount} worktrees`);
|
|
330
|
+
if (collisions.length > 0) parts.push(`${collisions.length} DET-20 overlap${collisions.length === 1 ? '' : 's'}`);
|
|
331
|
+
|
|
332
|
+
return {
|
|
333
|
+
mode,
|
|
334
|
+
title,
|
|
335
|
+
autoExpand,
|
|
336
|
+
label: parts.join(' · '),
|
|
337
|
+
counts: { ...counts, live, total: workers.length, parkedWarm, nearCliff },
|
|
338
|
+
workers,
|
|
339
|
+
worktrees: wt.worktrees,
|
|
340
|
+
worktreeCount,
|
|
341
|
+
worktreesAvailable: wt.available,
|
|
342
|
+
scopeLocks: {
|
|
343
|
+
leaseCount: leaseList.length,
|
|
344
|
+
collisions
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Compact label for status surfaces: `Swarm 3W` / `WT 2` / ''.
|
|
351
|
+
*/
|
|
352
|
+
export function formatSwarmLabel(topology) {
|
|
353
|
+
if (!topology || typeof topology !== 'object') return '';
|
|
354
|
+
const live = topology.counts?.live || 0;
|
|
355
|
+
const parts = [];
|
|
356
|
+
if (live > 0) parts.push(`Swarm ${live}W`);
|
|
357
|
+
if ((topology.worktreeCount || 0) > 1) parts.push(`WT ${topology.worktreeCount}`);
|
|
358
|
+
if ((topology.scopeLocks?.collisions || []).length > 0) parts.push('!DET-20');
|
|
359
|
+
return parts.join(' ');
|
|
360
|
+
}
|