neuralos 3.4.0 → 3.4.2

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/README.md ADDED
@@ -0,0 +1,366 @@
1
+ # <img src="./demo_imgs/icon.png" width="40" height="40" align="center" style="margin-right: 10px;"> RTerm
2
+
3
+ > **The AI-Native Terminal that thinks, executes, and collaborates with you.**
4
+ > **Built for Forward Deployed Engineers (FDEs), SREs, and platform teams** who live inside customer estates: reach any host (SSH / WinRM / serial), execute across fleets, change production safely, and leave behind automation — from one window, under your control.
5
+ > **AIOps · AI SRE · Agentic AI for operations**: closed-loop remediation, self-healing infrastructure, runbook automation, and ChatOps — with an AI agent that executes under policy and leaves tamper-evident evidence.
6
+
7
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
8
+ [![Platform](https://img.shields.io/badge/Platform-Windows%20%7C%20macOS%20%7C%20Linux-blue)](#platforms)
9
+ [![Shell](https://img.shields.io/badge/Shell-Zsh%20%7C%20Bash%20%7C%20PowerShell-orange)](#key-capabilities)
10
+
11
+ English README | [中文 README](./README.zh-CN.md)
12
+ Latest release notes: [`changelogs/v1.6.0.md`](./changelogs/v1.6.0.md)
13
+
14
+ If you have any suggestions or questions, please feel free to submit them in [GitHub Discussions](https://github.com/MrOrangeJJ/RTerm/discussions).
15
+
16
+ Usage guides:
17
+ [`docs/fde.md` — RTerm for Forward Deployed Engineers](./docs/fde.md) ·
18
+ [`docs/mobile-web-usage.md`](./docs/mobile-web-usage.md) ·
19
+ [`docs/tui-usage.md`](./docs/tui-usage.md) ·
20
+ [`docs/gybackend-usage.md`](./docs/gybackend-usage.md)
21
+
22
+ > [!WARNING]
23
+ > **Active Development**: RTerm evolves quickly. If a version introduces history compatibility breaks, it will be called out explicitly in release notes.
24
+
25
+ > [!NOTE]
26
+ > **v1.4.0 upgrade note**: the first launch after upgrading from a pre-1.4.0 version may briefly block while RTerm migrates legacy JSON history into SQLite and writes timestamped backup files. v1.4.3 has no additional migration step.
27
+
28
+ <p align="center">
29
+ <img src="./demo_imgs/v1.6.0_dark.png" width="100%" alt="GyShell dark theme demo">
30
+ </p>
31
+ <p align="center">
32
+ <img src="./demo_imgs/v1.6.0_light.png" width="100%" alt="GyShell light theme demo">
33
+ </p>
34
+ <p align="center">
35
+ <video controls width="100%" src="https://github.com/user-attachments/assets/f9daf884-bda0-4a58-8a6d-934db0eddeb5"></video>
36
+ </p>
37
+
38
+ ---
39
+
40
+ ## Why RTerm Is Different
41
+
42
+ Most AI terminal tools either generate one-shot scripts, or run in isolated sandboxes detached from real shell workflows.
43
+
44
+ RTerm is built for **persistent execution in your real terminal runtime**:
45
+
46
+ - **Persistent execution loop**: observe output -> reason -> continue.
47
+ - **Human-in-the-loop by design**: intervene anytime without breaking flow.
48
+ - **Multi-tab orchestration**: compile, inspect logs, and run fixes in parallel tabs.
49
+ - **Global tab inventory**: scan, reopen, drag, close, and create terminal/chat tabs from a dedicated list panel.
50
+ - **Workspace persistence**: terminal tabs, panel layout, and saved layout slots can survive restarts and restore quickly.
51
+ - **Detachable multi-window workspace**: peel panels into sub-windows and move tabs or whole panels across windows.
52
+ - **Adaptive panel tab display**: keep full tab strips or switch to a compact selector for narrow panel headers.
53
+ - **Reusable Agent setting profiles**: save and reapply complete operating profiles for models, tools, policies, memory, and workflow flags.
54
+ - **Cross-chat context handoff**: reference previous conversations from the composer with `Pass Chat` mentions instead of manually copying history.
55
+ - **Integrated file management**: browse, edit, copy, and transfer files across local and SSH sessions without leaving the workspace.
56
+ - **Live resource visibility**: inspect CPU, memory, disks, network, processes, sockets, and GPU from local or SSH sessions.
57
+ - **OpenClawd-style remote conversation control**: keep the runtime core on your own computer and steer it from anywhere through chat.
58
+ - **Built-in mobile-web delivery**: desktop can publish the mobile-web companion directly over your LAN with copyable access links.
59
+ - **Cross-surface runtime model**: desktop, TUI, and mobile-web share one gateway semantics.
60
+ - **Profile lock safety**: busy sessions pin active model profile for consistency.
61
+ - **Long-horizon context quality**: memory.md + compaction summaries + visible boundaries + deterministic fallback recovery keep long sessions understandable.
62
+ - **Tooling-native workflow**: skills, MCP servers, and built-in tools are runtime primitives.
63
+ - **Plugin system**: anyone can develop a custom plugin (agent tools, event triggers, dashboard panels) and have it auto-integrate on startup — 6 official plugins ship out of the box.
64
+ - **SRE observability pillar**: metrics ledger, golden signals, SLO/error budgets, uptime watchdogs, incident ledger with RCA + postmortems, anomaly detection, capacity forecasting, and a unified live dashboard.
65
+ - **APM + DEM + k8s/cloud infra**: OTLP distributed-trace store, Core Web Vitals (RUM), cluster health, and Windows ETW diagnostics.
66
+ - **Governance & audit**: hash-chained tamper-evident audit ledger with Merkle-tree evidence sealing, an AGT-style YAML policy engine (allow/deny/escalate), and a maker/checker review model that independently verifies the agent's output for correctness, completeness, safety, compliance, and accuracy.
67
+
68
+ ### At a Glance
69
+
70
+ - **For Forward Deployed Engineers (FDEs)**: reach any customer estate (SSH / WinRM / serial / Cisco), execute across fleets, land production changes under MOP approval with automatic rollback, and leave behind playbooks, triggers, and audit evidence — see [`docs/fde.md`](./docs/fde.md).
71
+ - **For shipping work**: not just planning, but iterative execution and correction.
72
+ - **For long-running tasks**: preserves session continuity and state across steps.
73
+ - **For real infrastructure**: shell, SSH, forwarding, file management, and multi-tab interactive terminal control.
74
+ - **For multi-device flow**: desktop + TUI + mobile-web with shared gateway semantics.
75
+ - **For multimodal workflows**: text and image inputs can be combined in one execution turn.
76
+
77
+ ## Where RTerm Fits — AIOps, AI SRE & Agentic Ops
78
+
79
+ Depending on your lens, RTerm is an **AIOps platform**, an **AI SRE teammate**, an **agentic-AI operations runtime**, a **runbook automation engine**, or **ChatOps for production** — all running on your own machine, against your real estate, under your policy.
80
+
81
+ | If you're looking for… | RTerm delivers |
82
+ |---|---|
83
+ | **AIOps / closed-loop remediation** | Detect (metrics, anomaly detection, early-warning forecasts, triggers) → Decide (AI agent under AGT policy) → Act (playbooks, MOP-gated changes) → Prove (hash-chained audit ledger + Merkle evidence sealing) |
84
+ | **AI SRE / SRE agents** | Golden signals, SLOs with error budgets + burn-rate alerting, uptime watchdogs, incident ledger with AI RCA + postmortems, capacity forecasting, on-call paging — executed by an agent with guardrails, not another dashboard you stare at |
85
+ | **Agentic AI for infrastructure** | The missing execution layer: a persistent observe→reason→act loop over SSH/WinRM/serial fleets, 100+ built-in tools, MCP support, skills, plugins, and a maker/checker review model that double-checks every consequential action |
86
+ | **Runbook automation** | Orchestrated DAG playbooks (incl. dagu YAML), validation steps with automatic rollback, Jinja templates, cron scheduling, GitOps drift detection |
87
+ | **Self-healing infrastructure** | Event-driven triggers (terminal pattern / metric threshold / webhook / schedule) fire playbooks or propose approved changes — auto-remediation with cooldowns and concurrency caps |
88
+ | **ChatOps for prod** | Steer operations conversationally from the desktop app or your phone's browser (mobile-web companion); approve blocked commands from anywhere; alerts fan out to Slack/Teams/Telegram/SMTP |
89
+ | **Copilot for ops/on-call** | Ask "why is db-02 slow?" — the agent pulls facts across hosts, correlates metrics + traces + logs, proposes a fix under approval, writes the postmortem |
90
+
91
+ The common thread: **RTerm doesn't just watch or suggest — it executes, safely, and leaves evidence.**
92
+
93
+ ## Latest Highlights
94
+
95
+ **v2.7.x — Governance, plugins & the maker/checker model:**
96
+ - **Review model (maker/checker)** with a visible Settings UI — a second model independently verifies the action model's output (correctness, completeness, safety, compliance, accuracy); skipped when not configured for fast output.
97
+ - **AGT policy engine** — YAML policies (allow/deny/escalate) evaluated before every consequential action, with a built-in safe default policy.
98
+ - **Hash-chained audit ledger + Merkle evidence sealing** for tamper-evident, independently-verifiable audit trails.
99
+ - **Monitor diagnostics** — one-call answer to "why aren't stats displaying?" per terminal.
100
+
101
+ **v2.5–v2.6 — Plugin system + official plugin suite + APerf:**
102
+ - **Plugin system** — custom plugins (agent tools, triggers, dashboard panels) auto-integrate on startup; 6 official plugins ship out of the box (patch-manager, request-router, sop-assistant, iam-connector, fraudops, netdata-rterm).
103
+ - **AWS APerf deep-dive** — deploy aperf to any Linux host for deep performance profiling with agent RCA on the findings.
104
+
105
+ **v2.0–v2.4 — The SRE pillar + advanced automation:**
106
+ - **Full observability** — metrics ledger, golden signals, SLO/error budgets, uptime watchdogs, incident ledger (RCA + postmortems), anomaly detection, capacity forecasting, unified live dashboard.
107
+ - **APM/DEM/infra/ETW** — OTLP traces, Core Web Vitals, k8s/cloud health, Windows ETW diagnostics.
108
+ - **Advanced automation** — event-driven triggers (NATS mesh), DAG playbooks, parameterized runbooks, dagu workflows, MOP change management with automatic rollback.
109
+
110
+ **v1.6.0 — Workspace foundation:**
111
+
112
+ - **Global Tab List panel**
113
+ - a new `TAB LIST` panel shows terminal and chat tabs as a vertical workspace inventory, with counts, status dots, latest-first ordering, drag/drop support, close actions, and quick creation for chat, local terminal, and saved-SSH terminal tabs
114
+ - **Default workspace refresh**
115
+ - new main layouts start with the list panel on the left, chat in the center, and terminal on the right, making tab-heavy sessions easier to scan immediately
116
+ - **More predictable background terminal tabs**
117
+ - local and SSH tabs created from the list panel can start in the background, stay visible in the global terminal inventory, bind to terminal panels when appropriate, and no longer unexpectedly take over linked filesystem or monitor panels
118
+ - **Visible compaction boundaries**
119
+ - long chats now persist and render a `[CTX COMPACTED]` marker at the actual retained-history cutoff across desktop, mobile-web, and TUI clients
120
+ - **Deterministic compaction fallback**
121
+ - when the compaction model fails or returns an empty summary, GyShell can recover with a local deterministic digest while preserving the protected tail and exporting exact older history for on-demand inspection when available
122
+ - **Safer stream recovery**
123
+ - empty non-tool provider stream finishes now retry through the normal path instead of silently ending a run with no answer, while valid empty tool-call finishes remain routable
124
+ - **Terminal inventory stability**
125
+ - terminal titles stay unique and stable across duplicate backend snapshots, concurrent terminal creation, explicit numeric suffixes, and detached-window terminal transfers
126
+ - **Mobile-web runtime refresh**
127
+ - Electron-packaged mobile-web assets were regenerated so desktop builds serve the updated client without requiring a separate mobile-web development server
128
+
129
+ ---
130
+
131
+ ## Key Capabilities
132
+
133
+ ### AI-Native Runtime
134
+
135
+ - Thinking-oriented execution for complex tasks.
136
+ - Context-aware responses from terminal state and selected resources.
137
+ - Per-profile model routing for `Global`, `Thinking`, `Action`, and `Compaction` roles.
138
+ - Reusable Agent Setting profiles for model profile, security policy, tools, skills, memory, recursion, and experimental workflow flags.
139
+ - Long-session context quality with dedicated compaction models, dynamic summaries, visible `[CTX COMPACTED]` boundary markers, and deterministic fallback recovery when model compaction is unavailable.
140
+ - SQLite-backed conversation history with automatic one-time migration from legacy JSON storage.
141
+ - AI-assisted terminal command drafting from recent tab context, with paste-before-run control.
142
+ - Background (nowait) commands automatically notify the agent on completion, so the agent can close the loop without polling.
143
+ - Terminal-targeting agent tools report runtime status and refuse stale operations on disconnected tabs until reconnect succeeds.
144
+ - Reference previous conversations with `Pass Chat` mentions; GyShell exports the selected chat as private local Markdown and tells the agent how to read it only when needed.
145
+ - Classic or Seamless chat activity display, depending on how much inline tool detail you want.
146
+ - Persistent memory injection via `memory.md`, scoped to the active Agent Setting profile when one is applied.
147
+ - Multimodal user input pipeline (text + images) for compatible models.
148
+ - OpenAI-compatible model endpoint support, with automatic recovery from malformed empty tool-call stream finishes.
149
+ - Optional experimental agent tools, including asynchronous cross-machine file transfer between terminal tabs with progress polling.
150
+
151
+ ### Terminal + SSH + File Management
152
+
153
+ - Shell support: Zsh, Bash, PowerShell.
154
+ - Older Windows PowerShell environments now use more reliable sidecar-based command completion tracking for local and SSH sessions.
155
+ - SSH support: password/key auth, proxy chaining, bastion workflows.
156
+ - SSH sessions use protocol keepalive to reduce silent idle disconnects.
157
+ - Port forwarding: local, remote, and dynamic SOCKS.
158
+ - Agent can coordinate **multiple SSH/local terminal tabs** in parallel during one task.
159
+ - Control-character operations for interactive terminal apps.
160
+ - Draft a command for the current terminal tab from recent visible output, then paste it back without auto-running it.
161
+ - Search within the active terminal buffer without leaving the panel.
162
+ - Terminal tab restoration after backend restart, plus lossless output catch-up for renderer remount/reconnect within the same backend runtime.
163
+ - Local terminal tabs auto-respawn their shell if it exits, so a local tab stays usable instead of going dead.
164
+ - Disconnected SSH tabs can be reconnected in place from the tab right-click menu using their saved connection config.
165
+ - **Integrated file browser panel**: browse, create, rename, delete, preview, sort, filter, and search files across local and SSH sessions.
166
+ - **Cross-session file transfer** (copy/move) with real-time progress, cancellation, and adaptive SFTP tuning.
167
+ - **Built-in file editor panel** for editing text files, plus inline preview of images (`png/jpg/gif/webp/bmp/ico/svg/avif`) and PDFs (with page navigation and zoom), all directly in the workspace.
168
+ - **File row right-click menu** with Copy / Cut / Paste / Rename / Delete and **Copy Full Path(s)** to the system clipboard.
169
+ - **Paste conflict resolution**: choose between **Overwrite** and **Keep Both** (auto-numbered names) when pasting into a folder with same-named items.
170
+
171
+ ### Workspace + Monitoring
172
+
173
+ - Detach panels into dedicated sub-windows and move tabs or whole panels across windows.
174
+ - Use the global Tab List panel to scan terminal/chat inventory, restore unhosted tabs, drag tabs across layout targets, close tabs, and create new chat/local/SSH tabs without forcing a terminal panel to appear.
175
+ - Save up to three workspace layout slots and restore them from the rail.
176
+ - Optionally keep the computer awake while any chat session is running, with the system-sleep block released automatically when runs finish.
177
+ - Chat tabs show a running indicator while a session is busy, mirroring terminal tab runtime-state dots.
178
+ - Choose `Auto`, `Expanded`, or `Select` panel tab display modes based on how much header space your workspace has.
179
+ - `Ctrl/Cmd+F` opens a panel-local find bar in terminal, current chat, file browser, and file editor.
180
+ - Open a resource monitor panel for local and SSH terminals from the workspace rail.
181
+ - Monitor panel surfaces CPU, memory, disk, network, process, socket, and GPU telemetry when available.
182
+ - Monitor collection is shared across tabs that point at the same local or SSH target, with failover if the original source tab exits.
183
+ - Monitor polling can be paused or resumed per local/SSH source, with the preference kept across restarts.
184
+ - Compact monitor layouts now give GPU telemetry its own card with clearer VRAM usage details.
185
+
186
+ ### Skills + MCP + Tools
187
+
188
+ - Folder-based skills workflow compatible with agentskills-style structure.
189
+ - Dynamic MCP server integration.
190
+ - Precision editing tools for safe, targeted file updates.
191
+ - Runtime tool toggles and summaries exposed to clients.
192
+
193
+ ### Plugin System (v2.5+)
194
+
195
+ - **Custom plugins** auto-integrate on startup: drop a folder with `plugin.json` + `index.mjs` into `~/.gybackend-data/plugins/` — the agent gets your tools, triggers, and dashboard panels immediately.
196
+ - **6 official plugins ship out of the box** (21 tools, 10 triggers, 6 panels):
197
+ - **patch-manager** — autonomous patch management (discover patches via yum/apt/Windows Update, build deployment plans, execute with MOP approval, fleet-wide compliance dashboard).
198
+ - **request-router** — automated request handling (submit/approve/list requests with risk classification → auto-approve/queue/MOP routing).
199
+ - **sop-assistant** — SOP retrieval + step-by-step guided execution; 8 built-in SOPs (restart-service, disk-cleanup, database-failover, incident-response, …) + IAM policy lookup.
200
+ - **iam-connector** — IAM integration (user/group info, privileged access identification, access reviews, disable users with approval) on Linux + Windows.
201
+ - **fraudops** — FraudOps operational layer (Flink/NATS/Kafka pipeline health, STR workflow with deadlines, decision summaries).
202
+ - **netdata-rterm** — Netdata Cloud alert webhook ingestion + correlation with RTerm metrics/incidents for RCA and auto-remediation.
203
+
204
+ ### SRE Observability (v2.0+)
205
+
206
+ - **Metrics ledger** with per-second snapshots per host (cpu/mem/disk/net/load/gpu) and trend forecasting ("disk full in N days").
207
+ - **Golden signals** per host (saturation/traffic/latency/errors) + capacity forecast.
208
+ - **Uptime watchdogs** (tcp/ssh/http/command liveness), up/degraded/down with alerting.
209
+ - **SLO/SLI** with error budget + burn rate + fast-burn alerting.
210
+ - **Incident ledger** with timelines, AI root-cause analysis, and postmortems.
211
+ - **Anomaly detection** (z-score + robust z-score) + predictive early warnings with optional MOP auto-remediation.
212
+ - **APM** — OTLP distributed-trace store (per-service p50/95/99, error rate, bottleneck services).
213
+ - **DEM/RUM** — Core Web Vitals (LCP/INP/CLS/TTFB) per page + error rate.
214
+ - **k8s/cloud infra** — cluster health (pods, restarts, node readiness, cpu/mem % of limit).
215
+ - **Windows ETW diagnostics** — built-in ETW providers (network/file/registry/process), agentless.
216
+ - **UEBA behavior ledger** — agent run baselines + deviations (run-spike, token-blowout, error-spike, unusual-model).
217
+ - **Embedded eval harness** — measures the agent's accuracy, tool selection, safety/policy compliance, and determinism.
218
+ - **Unified live dashboard** + browser-renderable HTML dashboard.
219
+ - **AWS APerf deep-dive** (v2.6+) — deploy aperf to any Linux host for deep CPU/PMU/process profiling with parsed findings feeding the agent's RCA.
220
+
221
+ ### Governance, Audit & the Maker/Checker Model (v2.7+)
222
+
223
+ - **Hash-chained audit ledger** — every agent action, command evaluation, approval, MOP change, and playbook step is recorded with the SHA-256 hash of the previous record (tamper-evident), plus **Merkle-tree evidence sealing** for independently-verifiable audit bundles.
224
+ - **AGT policy engine** — YAML policies evaluated before every consequential action (allow/deny/escalate); glob action patterns, target wildcards (`prod-*`), agent identity + sponsoring principal for zero-trust.
225
+ - **Review model (maker/checker)** — a second model independently verifies the action model's output on 5 dimensions: correctness, completeness, safety, compliance, and accuracy. Three modes (strict/advisory/auto-approve); skipped entirely when no review model is configured (fast output mode).
226
+ - **Monitor diagnostics** — one-call answer to "why aren't stats displaying?" per terminal (publisher wired? session exists? collection stuck? connected? last-collect time?).
227
+
228
+ ### Automation & Change Management
229
+
230
+ - **Playbooks** with validation + automatic rollback; **DAG/orchestrated playbooks** with parallel waves.
231
+ - **Event-driven triggers** (pattern/threshold/webhook/schedule) firing playbooks or proposing MOP changes, with cooldown + concurrency caps.
232
+ - **MOP change management** — plan → approve → run → status with a durable change ledger and automatic rollback on validation failure.
233
+ - **Scheduled tasks** (5-field cron) running headless inside the daemon.
234
+ - **dagu workflows** — run declarative dagu YAML DAGs natively on the orchestrated playbook engine, no dagu server required.
235
+ - **Parameterized runbooks** with `{{param}}` substitution + secret masking; idempotent `desiredState` steps; cross-host `captureVar`.
236
+ - **NATS event mesh** — fleet-wide trigger fan-out across multiple RTerm backends.
237
+
238
+ ### Mobile-Web Companion
239
+
240
+ - Mobile-first remote client for active session tracking and steering.
241
+ - Desktop can serve the mobile-web companion directly and expose copyable access links from settings.
242
+ - OpenClawd-style conversational control from anywhere while your core runtime stays on your own machine.
243
+ - Session list with search and status hints.
244
+ - Pending approval badge with jump-to-blocked-session behavior, plus task-completion toasts.
245
+ - Conversation rollback and branch-from-message controls from mobile.
246
+ - Swipe-to-delete session flow for faster mobile cleanup.
247
+ - Read-only terminal output tails with unread indicators, local/saved-SSH terminal creation, and SSH reconnect.
248
+ - Detailed turn event inspection from phone browser.
249
+ - Tool, skill, Agent Setting profile, terminal, and settings access through gateway RPC.
250
+ - Long chat timelines avoid full-list rerenders during composer input, keeping history-heavy mobile sessions responsive.
251
+ - Gateway exposure can now be limited to localhost, LAN-only, custom CIDR ranges, or all interfaces.
252
+
253
+ ---
254
+
255
+ ## Platforms
256
+
257
+ 1. **Electron desktop app** (`apps/electron`)
258
+ 2. **Standalone backend runtime** (`apps/gybackend`)
259
+ 3. **Deprecated TUI runtime** (`apps/tui` wrapper + `packages/tui` core)
260
+ 4. **Mobile-web runtime** (`apps/mobile-web` wrapper + `packages/mobile-web` core)
261
+
262
+ ### Which Surface Should You Use?
263
+
264
+ - **Desktop app**: primary full-featured experience for daily development.
265
+ - **TUI (`gyll`)**: deprecated and unsupported. Desktop packages no longer bundle or install `gyll`.
266
+ - **Mobile-web**: OpenClawd-style remote conversational control from phone/browser.
267
+
268
+ ---
269
+
270
+ ## Quick Start
271
+
272
+ ### Prerequisites
273
+
274
+ - Node.js 18+
275
+ - npm
276
+
277
+ ### Development
278
+
279
+ ```bash
280
+ git clone https://github.com/MrOrangeJJ/RTerm.git
281
+ cd RTerm
282
+ npm install
283
+ npm run dev
284
+ ```
285
+
286
+ ### One-line Mental Model
287
+
288
+ `RTerm = persistent AI runtime + real terminal control + human override at any time.`
289
+
290
+ ### Mobile-web development
291
+
292
+ ```bash
293
+ npm run dev:mobile-web
294
+ ```
295
+
296
+ ---
297
+
298
+ ## Deprecated CLI (`gyll`)
299
+
300
+ After installing and launching RTerm desktop once, `gyll` is available from the desktop runtime setup.
301
+
302
+ When an existing user updates from a version that installed desktop-managed `gyll` launchers, the updated app removes those legacy launchers on startup while leaving any shell profile PATH block untouched.
303
+
304
+ ---
305
+
306
+ ## Architecture Notes
307
+
308
+ RTerm follows strict layering:
309
+
310
+ - `packages/*`: implementation logic.
311
+ - `apps/*`: composition/bootstrap/build wrappers.
312
+ - Frontend logic does not belong in `packages/backend`.
313
+
314
+ Core runtime chain (simplified):
315
+
316
+ 1. `startElectronMain` (desktop composition root)
317
+ 2. `GatewayService` (session runtime + transport-agnostic orchestration)
318
+ 3. `WebSocketGatewayControlService` (policy-based ws gateway control)
319
+ 4. `WebSocketGatewayAdapter` / `ElectronWindowTransport` (transport implementations)
320
+ 5. Client controllers in TUI and mobile-web
321
+
322
+ See:
323
+
324
+ - `docs/monorepo-architecture.md`
325
+ - `docs/build-commands.md`
326
+
327
+ ## Privacy and Update Policy
328
+
329
+ - Version checks query only this repository's GitHub `version.json`.
330
+ - No third-party auto-update endpoint is used.
331
+ - Version check is the only automatic background network request.
332
+
333
+ ## Read More
334
+
335
+ - Release notes: `changelogs/v1.6.0.md`
336
+ - Build matrix and packaging: `docs/build-commands.md`
337
+ - Monorepo boundaries and runtime flow: `docs/monorepo-architecture.md`
338
+
339
+ ---
340
+
341
+ ## Build and Packaging
342
+
343
+ - `npm run build`
344
+ - `npm run build:backend`
345
+ - `npm run build:tui`
346
+ - `npm run build:mobile-web`
347
+ - `npm run dist`
348
+ - `npm run dist:mac`
349
+ - `npm run dist:win`
350
+ - `npm run dist:linux`
351
+ - `npm run dist:linux-arm64`
352
+ - `./build.sh --help`
353
+
354
+ For the full command matrix and packaging notes, see `docs/build-commands.md`.
355
+
356
+ ---
357
+
358
+ ## License
359
+
360
+ This project is licensed under the **Apache License, Version 2.0** ([LICENSE](./LICENSE)).
361
+
362
+ Special acknowledgment: inspirations and references from [Tabby](https://github.com/Eugeny/tabby) (MIT).
363
+
364
+ ---
365
+
366
+ **RTerm** - _The shell that thinks with you._
package/bin/gybackend.cjs CHANGED
@@ -258528,6 +258528,92 @@ var init_HistorySqliteStore = __esm({
258528
258528
  });
258529
258529
  })();
258530
258530
  }
258531
+ /**
258532
+ * v3.4.1: append only the NEW messages for a session instead of rewriting
258533
+ * the whole session. saveUiSessions() deletes every row and re-inserts the
258534
+ * entire message list — on a long session that is a large synchronous
258535
+ * better-sqlite3 transaction on the main event loop, which is the
258536
+ * spinning-wheel freeze. This method appends from a given position in one
258537
+ * small transaction, so a debounced flush costs O(new messages), not
258538
+ * O(all messages).
258539
+ *
258540
+ * v3.4.3: this is a TRUNCATE-AND-APPEND from fromPosition — rows at
258541
+ * position >= fromPosition are deleted before the slice is inserted. The
258542
+ * caller always passes fromPosition such that messages[fromPosition..] is
258543
+ * the authoritative content, so this is what makes a rollback (or any
258544
+ * shrink) expressible on the incremental path: without the delete, the
258545
+ * old tail rows would survive the flush and resurrect on reload.
258546
+ *
258547
+ * Returns the number of rows appended.
258548
+ */
258549
+ appendUiSessionMessages(sessionId, messages, fromPosition, summary) {
258550
+ if (messages.length === 0 || fromPosition >= messages.length) {
258551
+ return 0;
258552
+ }
258553
+ const upsertSession = this.db.prepare(
258554
+ `INSERT INTO ui_sessions (
258555
+ id, title, updated_at, messages_count, last_message_preview
258556
+ ) VALUES (
258557
+ @id, @title, @updatedAt, @messagesCount, @lastMessagePreview
258558
+ )
258559
+ ON CONFLICT(id) DO UPDATE SET
258560
+ title = excluded.title,
258561
+ updated_at = excluded.updated_at,
258562
+ messages_count = excluded.messages_count,
258563
+ last_message_preview = excluded.last_message_preview`
258564
+ );
258565
+ const insertMessage = this.db.prepare(
258566
+ `INSERT OR REPLACE INTO ui_session_messages (
258567
+ session_id, position, ui_message_id, backend_message_id, role, message_type, content, metadata_json, timestamp, streaming
258568
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
258569
+ );
258570
+ const truncateFrom = this.db.prepare(
258571
+ "DELETE FROM ui_session_messages WHERE session_id = ? AND position >= ?"
258572
+ );
258573
+ const slice = messages.slice(fromPosition);
258574
+ this.db.transaction(() => {
258575
+ if (summary) {
258576
+ upsertSession.run({
258577
+ id: sessionId,
258578
+ title: summary.title,
258579
+ updatedAt: summary.updatedAt,
258580
+ messagesCount: summary.messagesCount,
258581
+ lastMessagePreview: summary.lastMessagePreview
258582
+ });
258583
+ } else {
258584
+ const existing = this.db.prepare("SELECT title, updated_at FROM ui_sessions WHERE id = ?").get(sessionId);
258585
+ upsertSession.run({
258586
+ id: sessionId,
258587
+ title: existing?.title ?? "New Chat",
258588
+ updatedAt: Date.now(),
258589
+ messagesCount: messages.length,
258590
+ lastMessagePreview: slice[slice.length - 1]?.content?.slice(0, 200) ?? ""
258591
+ });
258592
+ }
258593
+ truncateFrom.run(sessionId, fromPosition);
258594
+ for (let i = 0; i < slice.length; i++) {
258595
+ const message = slice[i];
258596
+ insertMessage.run(
258597
+ sessionId,
258598
+ fromPosition + i,
258599
+ message.id,
258600
+ message.backendMessageId ?? null,
258601
+ message.role,
258602
+ message.type,
258603
+ message.content,
258604
+ message.metadata ? JSON.stringify(message.metadata) : null,
258605
+ message.timestamp,
258606
+ message.streaming ? 1 : 0
258607
+ );
258608
+ }
258609
+ })();
258610
+ return slice.length;
258611
+ }
258612
+ /** v3.4.1: how many messages are already persisted for a session. */
258613
+ countUiSessionMessages(sessionId) {
258614
+ const row = this.db.prepare("SELECT COUNT(*) AS n FROM ui_session_messages WHERE session_id = ?").get(sessionId);
258615
+ return row?.n ?? 0;
258616
+ }
258531
258617
  deleteUiSessions(sessionIds) {
258532
258618
  const ids = Array.from(
258533
258619
  new Set(sessionIds.filter((id) => id.trim().length > 0))
@@ -302287,6 +302373,15 @@ ${promptPrefix}`;
302287
302373
  getAllTerminals() {
302288
302374
  return Array.from(this.terminals.values()).filter((t) => !t.isInitializing && t.runtimeState === "ready");
302289
302375
  }
302376
+ /**
302377
+ * v3.4.2: the saved connection config for a terminal (host/port/path/type…).
302378
+ * Read-only view used by the fleet tools to key per-host concurrency —
302379
+ * two tabs on the same SSH box must share one concurrency bucket.
302380
+ * Returns null for unknown terminals; never throws.
302381
+ */
302382
+ getTerminalConfig(terminalId) {
302383
+ return this.terminalConfigs.get(terminalId) ?? null;
302384
+ }
302290
302385
  getTransferMachineIdentity(terminalId) {
302291
302386
  const terminal = this.terminals.get(terminalId);
302292
302387
  const config2 = this.terminalConfigs.get(terminalId);
@@ -353399,6 +353494,239 @@ ${usage}`;
353399
353494
 
353400
353495
  // ../../packages/backend/src/services/AgentHelper/tools/fleet_tools.ts
353401
353496
  init_zod();
353497
+
353498
+ // ../../packages/backend/src/services/AgentHelper/tools/hostConcurrency.ts
353499
+ function hostKeyForTab(tab, config2) {
353500
+ const cfg = config2 ?? {};
353501
+ const host = cfg.host ?? cfg.address;
353502
+ if (host && String(host).trim()) return String(host).trim().toLowerCase();
353503
+ if (cfg.path && String(cfg.path).trim()) return `serial:${String(cfg.path).trim()}`;
353504
+ const type2 = (cfg?.type ?? tab.type ?? "").toLowerCase();
353505
+ if (type2 === "local") return "local";
353506
+ return `tab:${tab.id}`;
353507
+ }
353508
+ var HostConcurrency = class {
353509
+ perHost;
353510
+ global;
353511
+ queueTimeoutMs;
353512
+ runningByHost = /* @__PURE__ */ new Map();
353513
+ queueByHost = /* @__PURE__ */ new Map();
353514
+ globalRunning = 0;
353515
+ globalQueue = [];
353516
+ seq = 0;
353517
+ constructor(options) {
353518
+ this.perHost = Math.max(1, options?.perHost ?? 3);
353519
+ this.global = Math.max(1, options?.global ?? 12);
353520
+ this.queueTimeoutMs = options?.queueTimeoutMs ?? 0;
353521
+ }
353522
+ /** Run `fn` under the host cap. FIFO per host; global cap across hosts. */
353523
+ async run(host, fn, label = "", options) {
353524
+ const key = String(host || "unknown").toLowerCase();
353525
+ const id = ++this.seq;
353526
+ const desc = label || `op#${id}`;
353527
+ const waitedFrom = Date.now();
353528
+ if (this.canStart(key) && !this.hasQueuedWaiters()) {
353529
+ if (options?.signal?.aborted) {
353530
+ const err = new Error("Aborted before acquiring a host slot");
353531
+ err.name = "AbortError";
353532
+ return Promise.reject(err);
353533
+ }
353534
+ this.acquire(key);
353535
+ this.notifyWaited(waitedFrom, options);
353536
+ try {
353537
+ return await fn();
353538
+ } finally {
353539
+ this.release(key);
353540
+ }
353541
+ }
353542
+ await this.waitForSlot(key, desc, options);
353543
+ this.notifyWaited(waitedFrom, options);
353544
+ try {
353545
+ return await fn();
353546
+ } finally {
353547
+ this.release(key);
353548
+ }
353549
+ }
353550
+ notifyWaited(from, options) {
353551
+ if (options?.onWaitedMs) {
353552
+ try {
353553
+ options.onWaitedMs(Date.now() - from);
353554
+ } catch {
353555
+ }
353556
+ }
353557
+ }
353558
+ hasQueuedWaiters() {
353559
+ if (this.globalQueue.length > 0) return true;
353560
+ for (const q of this.queueByHost.values()) {
353561
+ if (q.length > 0) return true;
353562
+ }
353563
+ return false;
353564
+ }
353565
+ canStart(host) {
353566
+ return (this.runningByHost.get(host) ?? 0) < this.perHost && this.globalRunning < this.global;
353567
+ }
353568
+ acquire(host) {
353569
+ this.runningByHost.set(host, (this.runningByHost.get(host) ?? 0) + 1);
353570
+ this.globalRunning += 1;
353571
+ }
353572
+ release(host) {
353573
+ const n2 = (this.runningByHost.get(host) ?? 1) - 1;
353574
+ if (n2 <= 0) this.runningByHost.delete(host);
353575
+ else this.runningByHost.set(host, n2);
353576
+ this.globalRunning = Math.max(0, this.globalRunning - 1);
353577
+ this.pump();
353578
+ }
353579
+ waitForSlot(host, label, options) {
353580
+ return new Promise((resolve2, reject) => {
353581
+ const waiter = {
353582
+ resolve: resolve2,
353583
+ reject,
353584
+ label,
353585
+ enqueuedAt: Date.now(),
353586
+ settled: false,
353587
+ timer: null,
353588
+ onAbort: null,
353589
+ signal: options?.signal ?? null
353590
+ };
353591
+ const settle = (err) => {
353592
+ if (waiter.settled) return;
353593
+ waiter.settled = true;
353594
+ if (waiter.timer) {
353595
+ clearTimeout(waiter.timer);
353596
+ waiter.timer = null;
353597
+ }
353598
+ if (waiter.signal && waiter.onAbort) {
353599
+ waiter.signal.removeEventListener("abort", waiter.onAbort);
353600
+ waiter.onAbort = null;
353601
+ }
353602
+ this.removeWaiter(host, waiter);
353603
+ if (err) reject(err);
353604
+ else resolve2();
353605
+ };
353606
+ waiter.onAbort = () => {
353607
+ const err = new Error("Aborted while waiting for a host slot");
353608
+ err.name = "AbortError";
353609
+ settle(err);
353610
+ };
353611
+ const q = this.queueByHost.get(host) ?? [];
353612
+ q.push(waiter);
353613
+ this.queueByHost.set(host, q);
353614
+ this.globalQueue.push(waiter);
353615
+ if (waiter.signal) {
353616
+ if (waiter.signal.aborted) {
353617
+ waiter.onAbort();
353618
+ return;
353619
+ }
353620
+ waiter.signal.addEventListener("abort", waiter.onAbort, { once: true });
353621
+ }
353622
+ if (this.queueTimeoutMs > 0) {
353623
+ const t = setTimeout(() => {
353624
+ settle(new Error(`host queue timeout after ${this.queueTimeoutMs}ms waiting for ${host}`));
353625
+ }, this.queueTimeoutMs);
353626
+ if (typeof t.unref === "function") t.unref();
353627
+ waiter.timer = t;
353628
+ }
353629
+ });
353630
+ }
353631
+ removeWaiter(host, waiter) {
353632
+ const q = this.queueByHost.get(host);
353633
+ if (q) {
353634
+ const i = q.indexOf(waiter);
353635
+ if (i >= 0) q.splice(i, 1);
353636
+ if (q.length === 0) this.queueByHost.delete(host);
353637
+ }
353638
+ const gi = this.globalQueue.indexOf(waiter);
353639
+ if (gi >= 0) this.globalQueue.splice(gi, 1);
353640
+ }
353641
+ /** Grant slots to queued waiters, per-host FIFO first, then global FIFO. */
353642
+ pump() {
353643
+ for (const [host, q] of Array.from(this.queueByHost.entries())) {
353644
+ while (q.length > 0 && (this.runningByHost.get(host) ?? 0) < this.perHost && this.globalRunning < this.global) {
353645
+ const w = q.shift();
353646
+ if (q.length === 0) this.queueByHost.delete(host);
353647
+ const gi = this.globalQueue.indexOf(w);
353648
+ if (gi >= 0) this.globalQueue.splice(gi, 1);
353649
+ this.runningByHost.set(host, (this.runningByHost.get(host) ?? 0) + 1);
353650
+ this.globalRunning += 1;
353651
+ this.settleWaiter(w);
353652
+ }
353653
+ }
353654
+ while (this.globalQueue.length > 0 && this.globalRunning < this.global) {
353655
+ const w = this.globalQueue[0];
353656
+ const host = this.findWaiterHost(w);
353657
+ if (!host) {
353658
+ this.globalQueue.shift();
353659
+ continue;
353660
+ }
353661
+ if ((this.runningByHost.get(host) ?? 0) >= this.perHost) {
353662
+ break;
353663
+ }
353664
+ this.globalQueue.shift();
353665
+ const q = this.queueByHost.get(host);
353666
+ if (q) {
353667
+ const i = q.indexOf(w);
353668
+ if (i >= 0) q.splice(i, 1);
353669
+ if (q.length === 0) this.queueByHost.delete(host);
353670
+ }
353671
+ this.runningByHost.set(host, (this.runningByHost.get(host) ?? 0) + 1);
353672
+ this.globalRunning += 1;
353673
+ this.settleWaiter(w);
353674
+ }
353675
+ }
353676
+ findWaiterHost(w) {
353677
+ for (const [host, q] of this.queueByHost.entries()) {
353678
+ if (q.includes(w)) return host;
353679
+ }
353680
+ return null;
353681
+ }
353682
+ /** Grant path: mark settled and resolve. The waiting run() continuation
353683
+ * executes fn and releases in its finally. */
353684
+ settleWaiter(waiter) {
353685
+ if (waiter.settled) return;
353686
+ waiter.settled = true;
353687
+ if (waiter.timer) {
353688
+ clearTimeout(waiter.timer);
353689
+ waiter.timer = null;
353690
+ }
353691
+ if (waiter.signal && waiter.onAbort) {
353692
+ waiter.signal.removeEventListener("abort", waiter.onAbort);
353693
+ waiter.onAbort = null;
353694
+ }
353695
+ waiter.resolve();
353696
+ }
353697
+ /** Observable state — surfaced to the agent so a wait is not a mystery. */
353698
+ stats() {
353699
+ const out = [];
353700
+ const hosts = /* @__PURE__ */ new Set([
353701
+ ...this.runningByHost.keys(),
353702
+ ...this.queueByHost.keys()
353703
+ ]);
353704
+ for (const host of hosts) {
353705
+ out.push({
353706
+ host,
353707
+ running: this.runningByHost.get(host) ?? 0,
353708
+ queued: (this.queueByHost.get(host) ?? []).length,
353709
+ limit: this.perHost
353710
+ });
353711
+ }
353712
+ return out;
353713
+ }
353714
+ get globalRunningCount() {
353715
+ return this.globalRunning;
353716
+ }
353717
+ get globalQueuedCount() {
353718
+ return this.globalQueue.length;
353719
+ }
353720
+ };
353721
+ var defaultInstance = null;
353722
+ function getDefaultHostConcurrency() {
353723
+ if (!defaultInstance) {
353724
+ defaultInstance = new HostConcurrency();
353725
+ }
353726
+ return defaultInstance;
353727
+ }
353728
+
353729
+ // ../../packages/backend/src/services/AgentHelper/tools/fleet_tools.ts
353402
353730
  var MAX_FLEET_TARGETS = 25;
353403
353731
  var PROBE_READY_TIMEOUT_MS = 3e4;
353404
353732
  var PROBE_READY_POLL_MS = 500;
@@ -353412,10 +353740,19 @@ async function runOnOneTab(tab, command, context2) {
353412
353740
  const label = tab.title || tab.id;
353413
353741
  try {
353414
353742
  abortIfNeeded(context2.signal);
353415
- const result = await context2.terminalService.runCommandAndWait(
353416
- tab.id,
353417
- command,
353418
- { signal: context2.signal, interruptOnAbort: false }
353743
+ const host = hostKeyForTab(
353744
+ tab,
353745
+ context2.terminalService?.getTerminalConfig?.(tab.id) ?? null
353746
+ );
353747
+ const limiter = getDefaultHostConcurrency();
353748
+ const result = await limiter.run(
353749
+ host,
353750
+ async () => context2.terminalService.runCommandAndWait(tab.id, command, {
353751
+ signal: context2.signal,
353752
+ interruptOnAbort: false
353753
+ }),
353754
+ `fleet:${label}`,
353755
+ { signal: context2.signal }
353419
353756
  );
353420
353757
  return {
353421
353758
  target: label,
@@ -353503,7 +353840,9 @@ ${body}`
353503
353840
  }
353504
353841
  });
353505
353842
  const okCount = allResults.filter((r) => r.ok).length;
353506
- const summary = `Fleet command finished on ${allResults.length} target(s): ${okCount} ok, ${allResults.length - okCount} failed/errored.`;
353843
+ const limiter = getDefaultHostConcurrency();
353844
+ const queued = limiter.globalQueuedCount;
353845
+ const summary = `Fleet command finished on ${allResults.length} target(s): ${okCount} ok, ${allResults.length - okCount} failed/errored.${queued > 0 ? ` (${queued} operation(s) were still queued when this completed \u2014 per-host cap ${limiter.stats()[0]?.limit ?? 3}.)` : ""}`;
353507
353846
  return finish(`${summary}
353508
353847
  ${serializeResults(allResults)}`);
353509
353848
  }
@@ -353615,10 +353954,17 @@ async function collectFacts(args, context2) {
353615
353954
  continue;
353616
353955
  }
353617
353956
  try {
353618
- const res = await terminalService.runCommandAndWait(
353619
- tab.id,
353620
- cmd,
353621
- { signal: context2.signal, interruptOnAbort: false }
353957
+ const res = await getDefaultHostConcurrency().run(
353958
+ hostKeyForTab(
353959
+ tab,
353960
+ terminalService?.getTerminalConfig?.(tab.id) ?? null
353961
+ ),
353962
+ () => terminalService.runCommandAndWait(tab.id, cmd, {
353963
+ signal: context2.signal,
353964
+ interruptOnAbort: false
353965
+ }),
353966
+ `facts:${tab.title || tab.id}`,
353967
+ { signal: context2.signal }
353622
353968
  );
353623
353969
  facts[`cmd${i}`] = truncateForFleet(res.stdoutDelta || "", 800);
353624
353970
  } catch (e) {
@@ -369354,6 +369700,13 @@ function canRunInParallel(toolCalls) {
369354
369700
  }
369355
369701
  return true;
369356
369702
  }
369703
+ function countFleetToolsInBatch(toolCalls) {
369704
+ let n2 = 0;
369705
+ for (const tc of toolCalls) {
369706
+ if (tc?.name === "run_fleet_command" || tc?.name === "collect_facts") n2++;
369707
+ }
369708
+ return n2;
369709
+ }
369357
369710
  function reconcileToolCalls(toolCalls) {
369358
369711
  if (!Array.isArray(toolCalls) || toolCalls.length === 0) return [];
369359
369712
  const seen = /* @__PURE__ */ new Set();
@@ -370539,7 +370892,7 @@ var AgentService_v2 = class {
370539
370892
  if (!sessionId) throw new Error("No session ID in state");
370540
370893
  const sessionBinding = this.getSessionModelBinding(sessionId);
370541
370894
  const queue2 = Array.isArray(state.pendingToolCalls) ? state.pendingToolCalls : [];
370542
- if (canRunInParallel(queue2)) {
370895
+ if (canRunInParallel(queue2) && countFleetToolsInBatch(queue2) <= 1) {
370543
370896
  const parallelResults = await Promise.all(
370544
370897
  queue2.map(async (tc) => {
370545
370898
  const tm = this.createToolMessage(tc);
@@ -373378,6 +373731,7 @@ function normalizeCompactionBoundaryMarkers(messages) {
373378
373731
  return normalized;
373379
373732
  }
373380
373733
  function getLastVisiblePreview(messages) {
373734
+ const MAX_PREVIEW_CHARS = 200;
373381
373735
  for (let index2 = messages.length - 1; index2 >= 0; index2 -= 1) {
373382
373736
  const message = messages[index2];
373383
373737
  if (message.type === "tokens_count") continue;
@@ -373387,9 +373741,8 @@ function getLastVisiblePreview(messages) {
373387
373741
  message.content || message.metadata?.output || imagePreview || ""
373388
373742
  );
373389
373743
  if (preview) {
373390
- return preview;
373744
+ return preview.length > MAX_PREVIEW_CHARS ? `${preview.slice(0, MAX_PREVIEW_CHARS)}\u2026` : preview;
373391
373745
  }
373392
- return "";
373393
373746
  }
373394
373747
  return "";
373395
373748
  }
@@ -373404,16 +373757,118 @@ function buildUiSessionSummary(session) {
373404
373757
  }
373405
373758
 
373406
373759
  // ../../packages/backend/src/services/UIHistoryService.ts
373407
- var UIHistoryService = class {
373760
+ var UIHistoryService = class _UIHistoryService {
373408
373761
  store;
373409
373762
  sessionsCache = {};
373410
373763
  sessionSummaryCache = {};
373411
373764
  dirtySessions = /* @__PURE__ */ new Set();
373765
+ /**
373766
+ * v3.4.1: debounced auto-flush. Previously recordEvent() marked a session
373767
+ * dirty but NEVER flushed — messages reached SQLite only on rename /
373768
+ * rollback / branch or a graceful app close. Kill the process mid-run
373769
+ * (freeze, crash, force-quit) and every message since the last flush was
373770
+ * lost. That is the "work done, no record" bug.
373771
+ *
373772
+ * Now: flush within FLUSH_DEBOUNCE_MS of the last event, plus a synchronous
373773
+ * flush on beforeExit / SIGINT / SIGTERM.
373774
+ */
373775
+ flushTimer = null;
373776
+ shutdownHooksInstalled = false;
373777
+ /** v3.4.1: messages already written to SQLite per session, so flush()
373778
+ * appends only the new ones instead of rewriting the whole session. */
373779
+ persistedMessageCount = /* @__PURE__ */ new Map();
373780
+ /**
373781
+ * v3.4.3: per session, the IDs of messages that were written to disk
373782
+ * WHILE STILL STREAMING. The append path can only add rows, so a message
373783
+ * persisted mid-stream has truncated content on disk. While it is still
373784
+ * streaming the `streaming` scan in flush() catches it — but the moment
373785
+ * it FINISHES (streaming flips to false) that scan goes blind and the
373786
+ * final content would never land. This set is the historical record that
373787
+ * keeps the rewrite alive across that transition.
373788
+ */
373789
+ persistedStreamingMessageIds = /* @__PURE__ */ new Map();
373790
+ /** Flush debounce window — short enough to survive a crash, long enough
373791
+ * to batch a streaming burst into one write. */
373792
+ static FLUSH_DEBOUNCE_MS = 1500;
373412
373793
  constructor(options) {
373413
373794
  this.store = options?.store || new HistorySqliteStore();
373414
373795
  this.sessionSummaryCache = this.buildSessionSummaryCache(
373415
373796
  this.store.listUiSessionSummaries()
373416
373797
  );
373798
+ this.installShutdownHooks();
373799
+ }
373800
+ installShutdownHooks() {
373801
+ if (this.shutdownHooksInstalled) return;
373802
+ this.shutdownHooksInstalled = true;
373803
+ if (!_UIHistoryService.beforeExitInstalled) {
373804
+ _UIHistoryService.beforeExitInstalled = true;
373805
+ process.once("beforeExit", () => {
373806
+ for (const svc of _UIHistoryService.instances) {
373807
+ try {
373808
+ svc.flush();
373809
+ } catch {
373810
+ }
373811
+ }
373812
+ });
373813
+ }
373814
+ for (const sig of ["SIGINT", "SIGTERM"]) {
373815
+ if (!_UIHistoryService.activeFlushOnSignal.has(sig)) {
373816
+ _UIHistoryService.activeFlushOnSignal.add(sig);
373817
+ const hadListeners = process.listenerCount(sig) > 0;
373818
+ process.once(sig, () => {
373819
+ for (const svc of _UIHistoryService.instances) {
373820
+ try {
373821
+ svc.flush();
373822
+ } catch {
373823
+ }
373824
+ }
373825
+ _UIHistoryService.activeFlushOnSignal.delete(sig);
373826
+ if (!hadListeners) {
373827
+ process.kill(process.pid, sig);
373828
+ }
373829
+ });
373830
+ }
373831
+ }
373832
+ _UIHistoryService.instances.add(this);
373833
+ }
373834
+ /**
373835
+ * v3.4.3: remove this instance from the shared shutdown-flush registry.
373836
+ * Without this, every UIHistoryService ever constructed stays in the
373837
+ * static `instances` set forever (tests, multi-window, hot reload) — the
373838
+ * shared signal handler then flushes (and holds) dead services on every
373839
+ * SIGINT/SIGTERM, and the set itself grows without bound.
373840
+ */
373841
+ dispose() {
373842
+ try {
373843
+ this.flush();
373844
+ } catch {
373845
+ }
373846
+ _UIHistoryService.instances.delete(this);
373847
+ if (this.flushTimer) {
373848
+ clearTimeout(this.flushTimer);
373849
+ this.flushTimer = null;
373850
+ }
373851
+ }
373852
+ /** Live instances — the shared signal handler flushes all of them. */
373853
+ static instances = /* @__PURE__ */ new Set();
373854
+ static activeFlushOnSignal = /* @__PURE__ */ new Set();
373855
+ /** v3.4.2: beforeExit is installed once for the whole class. */
373856
+ static beforeExitInstalled = false;
373857
+ scheduleFlush() {
373858
+ if (this.flushTimer) {
373859
+ clearTimeout(this.flushTimer);
373860
+ }
373861
+ this.flushTimer = setTimeout(() => {
373862
+ this.flushTimer = null;
373863
+ try {
373864
+ this.flush();
373865
+ } catch (error40) {
373866
+ console.error("[UIHistory] auto-flush failed:", error40);
373867
+ }
373868
+ }, _UIHistoryService.FLUSH_DEBOUNCE_MS);
373869
+ if (typeof this.flushTimer.unref === "function") {
373870
+ this.flushTimer.unref();
373871
+ }
373417
373872
  }
373418
373873
  buildSessionSummaryCache(summaries) {
373419
373874
  const cache2 = {};
@@ -373433,6 +373888,18 @@ var UIHistoryService = class {
373433
373888
  }
373434
373889
  const sanitized = sanitizeUiSession(loaded);
373435
373890
  this.sessionsCache[sessionId] = sanitized;
373891
+ if (!this.persistedMessageCount.has(sessionId)) {
373892
+ this.persistedMessageCount.set(sessionId, sanitized.messages.length);
373893
+ }
373894
+ const stillStreaming = /* @__PURE__ */ new Set();
373895
+ for (const m2 of sanitized.messages) {
373896
+ if (m2.streaming) stillStreaming.add(m2.id);
373897
+ }
373898
+ if (stillStreaming.size > 0) {
373899
+ this.persistedStreamingMessageIds.set(sessionId, stillStreaming);
373900
+ } else {
373901
+ this.persistedStreamingMessageIds.delete(sessionId);
373902
+ }
373436
373903
  this.syncSessionSummary(sessionId);
373437
373904
  return sanitized;
373438
373905
  }
@@ -373459,6 +373926,7 @@ var UIHistoryService = class {
373459
373926
  const actions = this.processEvent(session, event, sessionId);
373460
373927
  this.syncSessionSummary(sessionId);
373461
373928
  this.dirtySessions.add(sessionId);
373929
+ this.scheduleFlush();
373462
373930
  return actions;
373463
373931
  }
373464
373932
  flush(sessionId) {
@@ -373467,6 +373935,7 @@ var UIHistoryService = class {
373467
373935
  return;
373468
373936
  }
373469
373937
  const entries = [];
373938
+ const appendFrom = {};
373470
373939
  sessionIds.forEach((id) => {
373471
373940
  const session = this.sessionsCache[id];
373472
373941
  if (!session) {
@@ -373478,9 +373947,56 @@ var UIHistoryService = class {
373478
373947
  const summary = buildUiSessionSummary(sanitized);
373479
373948
  this.sessionSummaryCache[id] = summary;
373480
373949
  entries.push({ session: sanitized, summary });
373950
+ const already = this.persistedMessageCount.get(id);
373951
+ let from = typeof already === "number" && already <= sanitized.messages.length ? already : 0;
373952
+ if (from > 0) {
373953
+ const staleIds = this.persistedStreamingMessageIds.get(id);
373954
+ for (let i = 0; i < from; i++) {
373955
+ const m2 = sanitized.messages[i];
373956
+ if (m2?.streaming || staleIds && m2 && staleIds.has(m2.id)) {
373957
+ from = i;
373958
+ break;
373959
+ }
373960
+ }
373961
+ }
373962
+ appendFrom[id] = from;
373481
373963
  });
373482
373964
  if (entries.length > 0) {
373483
- this.store.saveUiSessions(entries);
373965
+ let usedIncremental = false;
373966
+ try {
373967
+ let appended = 0;
373968
+ for (const { session, summary } of entries) {
373969
+ const from = appendFrom[session.id] ?? 0;
373970
+ const n2 = this.store.appendUiSessionMessages(
373971
+ session.id,
373972
+ session.messages,
373973
+ from,
373974
+ summary
373975
+ );
373976
+ appended += n2;
373977
+ this.persistedMessageCount.set(session.id, session.messages.length);
373978
+ const stale = /* @__PURE__ */ new Set();
373979
+ for (let i = from; i < session.messages.length; i++) {
373980
+ const m2 = session.messages[i];
373981
+ if (m2?.streaming) stale.add(m2.id);
373982
+ }
373983
+ if (stale.size > 0) {
373984
+ this.persistedStreamingMessageIds.set(session.id, stale);
373985
+ } else {
373986
+ this.persistedStreamingMessageIds.delete(session.id);
373987
+ }
373988
+ }
373989
+ usedIncremental = true;
373990
+ } catch {
373991
+ usedIncremental = false;
373992
+ }
373993
+ if (!usedIncremental) {
373994
+ this.store.saveUiSessions(entries);
373995
+ entries.forEach(({ session }) => {
373996
+ this.persistedMessageCount.set(session.id, session.messages.length);
373997
+ this.persistedStreamingMessageIds.delete(session.id);
373998
+ });
373999
+ }
373484
374000
  }
373485
374001
  sessionIds.forEach((id) => this.dirtySessions.delete(id));
373486
374002
  }
@@ -373789,6 +374305,8 @@ Error: ${event.message}` : ""),
373789
374305
  sessionId,
373790
374306
  messageId: message.id
373791
374307
  });
374308
+ this.persistedMessageCount.delete(sessionId);
374309
+ this.persistedStreamingMessageIds.delete(sessionId);
373792
374310
  }
373793
374311
  } else if (type2 === "done") {
373794
374312
  const stopAction = this.stopLatestStreaming(session, sessionId);
@@ -373858,6 +374376,8 @@ Error: ${event.message}` : ""),
373858
374376
  session.messages.slice(0, index2)
373859
374377
  );
373860
374378
  this.dirtySessions.add(sessionId);
374379
+ this.persistedMessageCount.delete(sessionId);
374380
+ this.persistedStreamingMessageIds.delete(sessionId);
373861
374381
  actions.push({
373862
374382
  type: "ROLLBACK",
373863
374383
  sessionId,
@@ -373993,6 +374513,8 @@ Error: ${event.message}` : ""),
373993
374513
  delete this.sessionsCache[id];
373994
374514
  delete this.sessionSummaryCache[id];
373995
374515
  this.dirtySessions.delete(id);
374516
+ this.persistedMessageCount.delete(id);
374517
+ this.persistedStreamingMessageIds.delete(id);
373996
374518
  });
373997
374519
  this.store.deleteUiSessions(ids);
373998
374520
  }
@@ -374031,6 +374553,8 @@ Error: ${event.message}` : ""),
374031
374553
  );
374032
374554
  this.syncSessionSummary(sessionId);
374033
374555
  this.dirtySessions.add(sessionId);
374556
+ this.persistedMessageCount.delete(sessionId);
374557
+ this.persistedStreamingMessageIds.delete(sessionId);
374034
374558
  this.flush(sessionId);
374035
374559
  return removedCount;
374036
374560
  }
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "neuralos",
3
- "version": "3.4.0",
4
- "description": "AI-native terminal & agentic-AI operations platform for Forward Deployed Engineers & SREs: AIOps closed-loop remediation, AI SRE, self-healing infrastructure, runbook automation, ChatOps; executes over SSH/WinRM/serial under policy with tamper-evident audit.",
3
+ "version": "3.4.2",
4
+ "description": "Standalone neuralOS backend (rterm-backend): AI-native terminal & agentic-AI operations platform for Forward Deployed Engineers & SREs.",
5
5
  "keywords": [
6
6
  "forward-deployed-engineer",
7
7
  "fde",
Binary file