Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.
JavaScript agent-collaborationagent-harnessagent-orchestrationagent-teamagent-workflowai-assistantai-automationai-employeeai-productivityai-teamai-workflowautonomous-agentsclaude-code-guiclaude-coworkdesktop-agentlocal-firstmulti-agentmulti-agent-systemself-evolving-agentstask-automation
Sync shared release 1.7.2 updates
wyt838ca9a664e2
diff --git a/bin/builtin-resource-gate.cjs b/bin/builtin-resource-gate.cjsindex 0d57ac0b..2f7a899f 100644--- a/bin/builtin-resource-gate.cjs+++ b/bin/builtin-resource-gate.cjs@@ -38,6 +38,7 @@ const REQUIRED_BUILTIN_INVENTORY = Object.freeze({'web-app-sdk',]),marketplace_agents: Object.freeze([+ '1040b336306f','173d4235a431','5f890bd72ac4','78900d8758bc',diff --git a/resources/builtin/_manifest.json b/resources/builtin/_manifest.jsonindex a0ace303..77dd5bcc 100644--- a/resources/builtin/_manifest.json+++ b/resources/builtin/_manifest.json@@ -1,7 +1,67 @@{"schema": 1,- "content_tree_sha256": "46e079bbd12d3ad84066830ecbcf6b21af99530cdede68d63f78bade516b5749",+ "content_tree_sha256": "1a54db9542e5d6e42bf27a8710783b3e51b55ec8df4d15b50c1b916a56395cb0","files": [+ {+ "path": "marketplace/agents/1040b336306f/agent.json",+ "bytes": 5184,+ "sha256": "e8a9a677acd8de2b23917ba123df2b778e36deae4a5a8420824eac02b441a756"+ },+ {+ "path": "marketplace/agents/1040b336306f/README.md",+ "bytes": 2347,+ "sha256": "c3d00d6220ec5689400667fe39d210cc88b68f25d9473b48fc122f24e5d5a777"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/investment-report/SKILL.md",+ "bytes": 3190,+ "sha256": "bc519b7b3c7181a27ef4b0083e66d46a360a9f979b6472159fb0421ba7d75767"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/market-data/requirements.history.txt",+ "bytes": 17,+ "sha256": "279c6e16b07af3bfc395318be3a41935b1e9a225112b82d18299ffc93422dd16"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/market-data/scripts/history.py",+ "bytes": 10192,+ "sha256": "ad4bced7369d8ec5bee5c31d46f609478c61cb2832444114e555ee561a72e249"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/market-data/scripts/market.py",+ "bytes": 13604,+ "sha256": "969ddd7a625cd78004c68aa602e8a5f58d39bed3ed18ec00478f5f14804e38f2"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/market-data/scripts/stock.py",+ "bytes": 58704,+ "sha256": "b178ccf98e6fa92b2d0f04d51b9760ff19f1ed0af48c2c21d1be0face633a29f"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/market-data/SKILL.md",+ "bytes": 4392,+ "sha256": "b1a65408f2e0738817e5a4bc6f737d009e4750c7733b90648ef82fdc735c611c"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/portfolio-risk/SKILL.md",+ "bytes": 2722,+ "sha256": "d878a3d82373ba5f0af9f3a0b7cde88922b56974e8c84da88e2f70ac6e7737c8"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/quant-lab/SKILL.md",+ "bytes": 2556,+ "sha256": "bd9d4b813bc822ca214ca3e43b4a6a8dc1181eec27aaa824ac85d41113723f35"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/security-research/SKILL.md",+ "bytes": 2421,+ "sha256": "0becdc19a761db731b726dfbec895d39e33ca45f1a346dcd06b4153176cf7be2"+ },+ {+ "path": "marketplace/agents/1040b336306f/skills/trade-control/SKILL.md",+ "bytes": 2325,+ "sha256": "4bca4732057f9797fb6b8247aec5b16561a40b84f3605de69ccbaa4df664fcd2"+ },{"path": "marketplace/agents/173d4235a431/_meta.json","bytes": 57,@@ -1495,7 +1555,7 @@{"path": "system/skills/_system.json","bytes": 273,- "sha256": "172acfe9a83f1c0e85393db6dcadcf1c397b3b38907835980ea8b794ae15e6ed"+ "sha256": "fa87ccfc5f1efb3fe44fd71966f52948a3846ed8bae69aa72941d842fb612afb"},{"path": "system/skills/agent-creator/references/cli-and-prose.md",@@ -1549,8 +1609,8 @@},{"path": "system/skills/web-app-sdk/assets/starter/index.html",- "bytes": 13768,- "sha256": "48139adfe7661239c5d5483aec57a76686a0e9e4959ea511c64501e11a0ce0c2"+ "bytes": 13305,+ "sha256": "e3624924ff995993472486591c38bd49551826d78e93c1bcfec7f4c20c2a0262"},{"path": "system/skills/web-app-sdk/assets/starter/orkas-app.json",@@ -1559,13 +1619,13 @@},{"path": "system/skills/web-app-sdk/references/api.json",- "bytes": 18164,- "sha256": "e75dff61c315244e499d3afd42f5a19db6815117c7b11a6729a97e5ed4622827"+ "bytes": 17541,+ "sha256": "6f7605ad0dfaf979378dbe70f35dd462e38be579d50d29f43504d3376dac9b1c"},{"path": "system/skills/web-app-sdk/SKILL.md",- "bytes": 7096,- "sha256": "70cd08e9d6c080a9a4bc4ca985b3292190a18351415b44ffb226cf6089b621bc"+ "bytes": 7657,+ "sha256": "2f2ffcf01f462bf2bc32c97f2e117f58136066cc3b962e7bc0e76291e511de0b"}],"inventory": {@@ -1584,10 +1644,34 @@},{"id": "web-app-sdk",- "update_at": 1789511205555+ "update_at": 1789990251573}],"marketplace_agents": [+ {+ "id": "1040b336306f",+ "name": "StockAnalyser",+ "version": "1.1.13",+ "icon": "chart",+ "color": "mint",+ "updated_at": "2026-09-21T08:21:00.000Z",+ "skill_list": [+ "investment-report",+ "market-data",+ "portfolio-risk",+ "quant-lab",+ "security-research",+ "trade-control"+ ],+ "embedded_skills": [+ "investment-report",+ "market-data",+ "portfolio-risk",+ "quant-lab",+ "security-research",+ "trade-control"+ ]+ },{"id": "173d4235a431","name": "ContentWriter",diff --git a/resources/builtin/marketplace/agents/1040b336306f/README.md b/resources/builtin/marketplace/agents/1040b336306f/README.mdnew file mode 100644index 00000000..0cab854a--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/README.md@@ -0,0 +1,44 @@+# StockAnalyser — repository source++`StockAnalyser` is a platform built-in Agent under+`PC/resources/builtin/marketplace/agents/1040b336306f`. PC startup seeds this+package through the same built-in marketplace path as the other shipped+Agents. Its six private Skills remain owned by StockAnalyser, and its model+regressions continue to participate in the default production-runtime cohort.+This built-in placement is independent of marketplace production publication.++## Lean package++- `market-data`: A/HK/US identifiers, free current-quote APIs, pinned AKShare+ history, provenance, CSV/JSON contracts, and the shared deterministic core.+- `security-research`: evidence-led company research and cross-market screens.+- `quant-lab`: cost-aware, look-ahead-safe strategy experiments.+- `portfolio-risk`: concentration, drawdown, tail-risk, and scenario analysis.+- `trade-control`: order drafts, paper mode, and live pre-trade safety.+- `investment-report`: concise dashboard and durable report contracts.++The six Skills are user-task boundaries, not workflow stages. Only the relevant+Skill plus `market-data` should be loaded for a request. All numeric operations+share `market-data/scripts/stock.py`, which uses the Python standard library.+Current quotes use free Tencent/Sina HTTP APIs. The history adapter pins AKShare+inside the Skill runner's isolated dependency cache, never as an Orkas-wide+dependency. Connected broker data stays behind the connector boundary.++## Open-source design lineage++The package adopts provider normalization from OpenBB, A/HK/US coverage from+AKShare and Longbridge, factor/research discipline from Qlib, backtest semantics+from backtrader/vectorbt/Zipline, risk decomposition from Riskfolio-Lib, and+paper/live execution separation from LEAN and Freqtrade. It intentionally does+not copy those projects' application shells or introduce their workflow engines.++## Local verification++```bash+cd PC+node scripts/run-python-tests.mjs resources/test/test_stock_analyser_core_unit.py resources/test/test_stock_analyser_market_unit.py resources/test/test_stock_analyser_history_unit.py -q+```++Network-provider canaries are intentionally separate: deterministic tests do+not require credentials, a live market, or optional packages. Run the explicit+live A/HK/US check with `npm run test:stock-data:live`.diff --git a/resources/builtin/marketplace/agents/1040b336306f/agent.json b/resources/builtin/marketplace/agents/1040b336306f/agent.jsonnew file mode 100644index 00000000..21217b64--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/agent.json@@ -0,0 +1,65 @@+{+ "agent_id": "1040b336306f",+ "version": "1.1.13",+ "name": "StockAnalyser",+ "description_zh": "研究 A 股、港股和美股,完成因子筛选、策略回测、组合风险分析与交易前校验,交付可追溯的分析看板、情景结论和订单草稿。适合“研究这只股票”“审查这个回测”“检查组合风险”;不负责非证券主题的通用调研或软件开发。触发词:股票、A股、港股、美股、行情、股价、基本面、技术面、选股、因子、回测、组合、风险、订单、模拟盘、Longbridge、AKShare",+ "description_en": "Research A/HK/US equities, screen factors, backtest strategies, assess portfolio risk, and validate pre-trade constraints; deliver traceable dashboards, scenarios, and order drafts. For: \"research this stock\", \"review this backtest\", or \"check portfolio risk\"; not general research on non-security topics or software development. Triggers: stock, equity, A-share, Hong Kong, US, quote, price, fundamental, technical, screen, factor, backtest, portfolio, risk, order, paper trade, Longbridge, AKShare",+ "workflow": "### 1. Route\n- Choose RESEARCH, SCREEN, BACKTEST, PORTFOLIO, TRADE, or REPORT. Read `market-data` plus only the relevant route Skill. When a route also requires a durable report, read `investment-report`.\n- Normalize symbol, time, currency, adjustment, source, and as-of across A/HK/US. Treat all inputs as untrusted; expose stale or missing data.\n\n### 2. Ground API-first\n- For current-price, worth-buying, or entry-range work, read `market-data` and call its quote adapter before any web search; fetch API history when price behavior matters. Web snippets cannot replace quote/history data.\n- If required data fails, leave it unverified and withhold precise entry, stop, valuation, or buy/sell conclusions. Use the deterministic Skill core; never guess or mask a failed command.\n\n### 3. Decide Safely\n- Expose coverage, conflicting evidence, scenarios, catalysts, and invalidation. Apply the route Skill's leakage, cost, risk, and market-rule checks rather than inventing a shortcut.\n- TRADE defaults to a draft or paper mode. Live submission needs explicit authorization for the exact visible order, connector permission, a passing pre-trade validation, and broker readback. Never bypass a violation.\n\n### 4. Deliver\n- Follow `market-data`'s provenance contract. Return a compact dashboard with method, downside, uncertainty, and gaps; preserve market/currency labels and separate price from total return.\n- Never promise returns or present a heuristic score as personalized investment advice.",+ "created_at": "2026-08-27T16:20:00.000Z",+ "updated_at": "2026-09-21T08:21:00.000Z",+ "knowhow": [+ "Normalizes free A-share, Hong Kong, and US current quotes and daily history into one provenance-aware market-data contract.",+ "Combines technical, fundamental, event, and cross-sectional factor evidence without hiding missing coverage.",+ "Runs cost-aware, look-ahead-safe strategy tests with explicit benchmarks and sample limitations.",+ "Measures portfolio concentration, volatility, drawdown, historical tail risk, and scenario exposure.",+ "Builds paper or live order drafts behind deterministic market-rule, account, freshness, and authorization checks."+ ],+ "standards": [+ "Every current-price conclusion begins with the quote API and names provider observation time, retrieval time, session, quote kind, and quality flags; web snippets never substitute for it.",+ "Numerical analysis uses the deterministic Skill core, preserves market/currency/adjustment semantics, and never fabricates unavailable prices or fundamentals.",+ "Backtest conclusions disclose signal timing, costs, benchmark, sample window, data leakage controls, and the limits of any out-of-sample evidence.",+ "A trade remains a draft or paper action unless the user explicitly authorizes the exact live order and all connector and pre-trade checks pass.",+ "Delivery includes downside, uncertainty, invalidation conditions, and unverified items; it never guarantees performance or disguises a heuristic score as advice."+ ],+ "category": "data",+ "icon": "chart",+ "color": "mint",+ "runtime": {+ "kind": "in_process"+ },+ "interactive": false,+ "output_format": "dashboard",+ "tool_list": [+ "workspace.read",+ "workspace.write.output",+ "workspace.execute.command",+ "web",+ "connectors"+ ],+ "skill_list": [+ "market-data",+ "security-research",+ "quant-lab",+ "portfolio-risk",+ "trade-control",+ "investment-report"+ ],+ "inputs": [+ {+ "id": "request",+ "label": "Analysis request",+ "type": "textarea",+ "required": true,+ "default": "",+ "description": "Security, market, portfolio, strategy, or order-draft question. Include the intended market and horizon when known."+ },+ {+ "id": "data_file",+ "label": "Market or portfolio data (optional)",+ "type": "file",+ "required": false,+ "default": "",+ "description": "Optional CSV or JSON input using the market-data Skill contract."+ }+ ]+}diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/investment-report/SKILL.md b/resources/builtin/marketplace/agents/1040b336306f/skills/investment-report/SKILL.mdnew file mode 100644index 00000000..54857d3c--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/investment-report/SKILL.md@@ -0,0 +1,58 @@+---+ownerAgent: 1040b336306f+name: investment-report+description_zh: 把研究、筛选、回测、组合或订单校验结果压缩为带来源、时点、质量标记、情景、风险和失效条件的看板;按需生成可归档的 Markdown 报告。+description_en: Compress research, screen, backtest, portfolio, or order-validation results into a dashboard with provenance, as-of, quality, scenarios, risks, and invalidation conditions, with optional archival Markdown.+---++# Investment Report++Read when the user requests a synthesis, comparison, handoff, or durable report.+Do not rerun unrelated analysis merely to fill a template.++## Chat dashboard++Lead with the decision question and evidence time. Then show:++1. scope: symbol/listing, market, horizon, currency, and adjustment;+2. evidence: sources, as-of times, quality flags, and missing coverage;+3. result: the smallest useful metric table and plain-language interpretation;+4. scenarios: connect each base/bull/bear or declared stress case to its+ supporting evidence, catalysts, and evidence that would weaken or reverse it;+ a shared condition may cover several cases when its scope is clear;+5. downside: risks and conflicting evidence;+6. method: calculation/backtest assumptions and limitations;+7. action: identify the next verification priority and how it could change the+ interpretation; several ordered or jointly necessary sources are valid. For+ trading, retain the constraint-aware option or validated order draft.++Keep calculated, provider-reported, user-provided, estimated, and model-inferred+items visibly distinct. If evidence is missing, state what cannot yet be+determined and what would resolve it; do not invent facts or numerical+thresholds to complete a scenario. Preserve units and never compare currencies+without an explicit FX basis.++## Durable report++Only when requested, assemble a JSON dossier and render stable Markdown:++```bash+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data stock -- render-report --input dossier.json --evidence-input source.json --output STOCK-ANALYSIS.md+```++Pass each original source or core-result file with repeated `--evidence-input`.+The renderer carries its declared source, observation/export and retrieval+times into a snapshot section with a content digest; it does not rely on copied+dates in the dossier. This applies equally to quotes, histories, calculations+and exported order receipts. Qualify status and cash claims at the record's+time, and leave later state unknown without a fresh read. Missing dates stay+unknown. Keep source/core files separate from the interpretive dossier.++The dossier may contain `title`, `scope`, `evidence`, `summary`, `metrics`,+`scenarios`, `risks`, `limitations`, and `next_steps`. The renderer escapes+tables and preserves source/as-of labels; it does not invent content. Preserve+the scenario-to-evidence links and verification priorities above in the dossier,+so they reach the report rather than appearing only in the chat handoff.++Name the created artifact and checks run. For an order, keep the exact visible+draft and validation decision in chat even when a report is also written.diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/SKILL.md b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/SKILL.mdnew file mode 100644index 00000000..b8f791d4--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/SKILL.md@@ -0,0 +1,84 @@+---+ownerAgent: 1040b336306f+name: market-data+description_zh: 统一 A 股、港股、美股的证券代码、实时报价、OHLCV、币种、时区、复权口径、来源和时效;优先调用免费行情 API,也可读取 CSV/JSON 或已连接的券商数据。+description_en: Normalize A/HK/US symbols, current quotes, OHLCV, currency, timezone, adjustment, provenance, and freshness; prefer free market APIs, while also supporting CSV/JSON and connected broker data.+---++# Market Data++Read for every substantive StockAnalyser route. It owns the shared input and+evidence contract; the other Skills own interpretation.++## API-first source order++1. Use supplied CSV/JSON when the user explicitly asks to analyze it.+2. Any question about the current/latest price, whether a stock is worth buying,+ or an entry range must call `market quote` before any web search. Tencent is+ the free primary quote API and Sina is the automatic fallback.+3. Use `history fetch` for free A/HK/US daily bars. Its pinned AKShare dependency+ is installed automatically in the Skill runner's isolated cache; never run a+ manual package install or package-presence preflight.+4. Use a connected broker only for broker-grade corroboration, account state,+ security rules, or orders. Keep credentials inside the connector boundary.+5. Use targeted exchange, filing, and issuer pages for company facts or events+ missing from providers. Web search snippets never substitute for a quote or+ history API response.++Do not begin web research while a required quote call is still pending. If all+quote providers fail, say that the current price is unverified and do not issue+an exact valuation, entry band, stop, or buy/sell conclusion. A prior close or+auction indication must retain its `quote_kind`, provider time, session, age,+and quality flags.++Never silently mix vendors, currencies, raw and adjusted prices, intraday and+daily bars, or price and total returns.++## Canonical contract++CSV columns: `date,open,high,low,close,volume`; `amount` and `adjusted_close`+are optional. JSON may be the same row array or `{ "bars": [...], "meta": {} }`.+Dates use ISO-8601. Numbers are raw numeric values, not comma/percent strings.+Attach `symbol`, `market`, `currency`, `timezone`, `adjustment`, `source`, and+`as_of` when known. The core sorts dates, rejects duplicates or impossible OHLC,+and emits freshness and coverage flags.++For a screen, use:++```json+{"securities":[{"symbol":"600519.SH","bars":[...],"fundamentals":{"roe":0.31,"pe":24.2}}],"filters":{},"weights":{}}+```++`analyze --symbol <code>` also accepts this universe and selects exactly one+matching security, retaining its `meta` and `fundamentals`. Analysis uses the+record's `source` (or `meta.source`) and `meta.as_of` unless explicitly overridden+by `--source` or `--as-of`; absent sources default to `user-file`.++## Shared core++Run only through the private Skill runner, with one runner command per tool+call:++```bash+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data market -- quote --symbol 600519.SH+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data history -- fetch --symbol 600519.SH --start 2025-01-01 --end 2026-08-28 --adjust qfq --output history.json+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data stock -- normalize-symbol --symbol 600519+```++`market quote` returns normalized price fields plus source, provider symbol,+provider observation time, retrieval time, market session, quote kind, age, and+quality flags. `history fetch` reports the selected upstream and every fallback+attempt. These free endpoints are best-effort and not exchange-certified feeds;+retain their delay and calendar limitations.++In the final answer, explicitly name the quote source and the selected history+upstream (for example, Tencent and AKShare/Sina), including the history provider+version and adjustment mode. Do not collapse them into a generic “API data”+label: this provenance is part of the result, even when no web research is+needed.++Use absolute input/output paths. A command writes only when `--output` is+explicit. Never join runner commands with `;`, `||`, or an unrelated command+that could mask a nonzero exit. On any validation or provider error, stop that+numeric conclusion and surface the error instead of repairing financial data by+guessing or ad hoc calculation.diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/requirements.history.txt b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/requirements.history.txtnew file mode 100644index 00000000..b210a102--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/requirements.history.txt@@ -0,0 +1 @@+akshare==1.18.94diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/history.py b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/history.pynew file mode 100644index 00000000..1ab77f5a--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/history.py@@ -0,0 +1,272 @@+#!/usr/bin/env python3+"""Free daily history adapter for A-share, Hong Kong, and US equities."""++from __future__ import annotations++import argparse+import datetime as dt+import importlib.metadata+import json+import re+import socket+import sys+from typing import Any, Mapping, Sequence++from stock import StockError, _emit, normalize_bars, normalize_symbol+++PROVIDERS = ("akshare-sina", "akshare-eastmoney")+++def _records(frame: Any) -> list[dict[str, Any]]:+ try:+ records = frame.to_dict(orient="records")+ except (AttributeError, TypeError, ValueError) as exc:+ raise StockError("history provider returned an unsupported table") from exc+ if not isinstance(records, list):+ raise StockError("history provider returned an unsupported table")+ return records+++def _date_from_row(row: Mapping[str, Any]) -> dt.date | None:+ lower = {str(key).strip().lower(): value for key, value in row.items()}+ value = lower.get("date", lower.get("日期", lower.get("datetime", lower.get("时间"))))+ try:+ return dt.date.fromisoformat(str(value)[:10])+ except (TypeError, ValueError):+ return None+++def _filter_dates(+ rows: list[dict[str, Any]],+ start: dt.date,+ end: dt.date,+) -> list[dict[str, Any]]:+ return [row for row in rows if (value := _date_from_row(row)) is not None and start <= value <= end]+++def _sina_symbol(instrument: Mapping[str, str]) -> str:+ if instrument["market"] == "A":+ suffix = instrument["symbol"].rsplit(".", 1)[-1].lower()+ return f"{suffix}{instrument['code']}"+ if instrument["market"] == "HK":+ return instrument["code"].zfill(5)+ return instrument["code"]+++def _fetch_sina(+ ak: Any,+ instrument: Mapping[str, str],+ start: dt.date,+ end: dt.date,+ adjust: str,+) -> tuple[list[dict[str, Any]], str]:+ provider_code = _sina_symbol(instrument)+ compact_start, compact_end = start.strftime("%Y%m%d"), end.strftime("%Y%m%d")+ if instrument["market"] == "A":+ frame = ak.stock_zh_a_daily(+ symbol=provider_code,+ start_date=compact_start,+ end_date=compact_end,+ adjust=adjust,+ )+ elif instrument["market"] == "HK":+ frame = ak.stock_hk_daily(symbol=provider_code, adjust=adjust)+ else:+ frame = ak.stock_us_daily(symbol=provider_code, adjust=adjust)+ return _filter_dates(_records(frame), start, end), provider_code+++def _fetch_eastmoney(+ ak: Any,+ instrument: Mapping[str, str],+ start: dt.date,+ end: dt.date,+ adjust: str,+ provider_symbol: str | None,+) -> tuple[list[dict[str, Any]], str]:+ compact_start, compact_end = start.strftime("%Y%m%d"), end.strftime("%Y%m%d")+ code = instrument["code"]+ if instrument["market"] == "A":+ frame = ak.stock_zh_a_hist(+ symbol=code, period="daily", start_date=compact_start,+ end_date=compact_end, adjust=adjust,+ )+ used_symbol = code+ elif instrument["market"] == "HK":+ used_symbol = code.zfill(5)+ frame = ak.stock_hk_hist(+ symbol=used_symbol, period="daily", start_date=compact_start,+ end_date=compact_end, adjust=adjust,+ )+ else:+ if not provider_symbol:+ raise StockError("Eastmoney US fallback requires --provider-symbol, for example 105.AAPL")+ used_symbol = provider_symbol+ frame = ak.stock_us_hist(+ symbol=used_symbol, period="daily", start_date=compact_start,+ end_date=compact_end, adjust=adjust,+ )+ return _filter_dates(_records(frame), start, end), used_symbol+++def _safe_error(exc: Exception) -> str:+ text = re.sub(r"https?://\S+", "provider endpoint", str(exc)).strip()+ return (text or exc.__class__.__name__)[:240]+++def fetch(+ symbol: str,+ start: str,+ end: str,+ market: str = "auto",+ provider: str = "auto",+ adjust: str = "qfq",+ provider_symbol: str | None = None,+ timeout: float = 15,+ max_bars: int = 5000,+ *,+ ak_module: Any | None = None,+ retrieved_at: dt.datetime | None = None,+) -> dict[str, Any]:+ instrument = normalize_symbol(symbol, market)+ try:+ first, last = dt.date.fromisoformat(start), dt.date.fromisoformat(end)+ except ValueError as exc:+ raise StockError("start and end must use YYYY-MM-DD") from exc+ if first > last:+ raise StockError("start must not be later than end")+ if provider not in {"auto", *PROVIDERS}:+ raise StockError("provider must be auto, akshare-sina, or akshare-eastmoney")+ if adjust not in {"raw", "qfq", "hfq"}:+ raise StockError("adjust must be raw, qfq, or hfq")+ if timeout <= 0 or timeout > 60:+ raise StockError("timeout must be greater than 0 and at most 60 seconds")+ if max_bars < 2 or max_bars > 20_000:+ raise StockError("max-bars must be between 2 and 20000")+ if ak_module is None:+ try:+ import akshare as ak_module # type: ignore+ except ImportError as exc:+ raise StockError("AKShare dependency is unavailable; run this adapter through run-skill.cjs") from exc++ candidates = PROVIDERS if provider == "auto" else (provider,)+ attempts: list[dict[str, str]] = []+ rows: list[dict[str, Any]] | None = None+ selected = ""+ used_symbol = ""+ old_timeout = socket.getdefaulttimeout()+ socket.setdefaulttimeout(timeout)+ try:+ for candidate in candidates:+ try:+ if candidate == "akshare-sina":+ candidate_rows, candidate_symbol = _fetch_sina(+ ak_module, instrument, first, last, "" if adjust == "raw" else adjust,+ )+ else:+ candidate_rows, candidate_symbol = _fetch_eastmoney(+ ak_module, instrument, first, last, "" if adjust == "raw" else adjust,+ provider_symbol,+ )+ if len(candidate_rows) < 2:+ raise StockError("provider returned fewer than two bars in the requested range")+ rows, selected, used_symbol = candidate_rows, candidate, candidate_symbol+ attempts.append({"provider": candidate, "status": "ok"})+ break+ except Exception as exc: # bounded provider evidence; auto may continue+ attempts.append({"provider": candidate, "status": "error", "detail": _safe_error(exc)})+ finally:+ socket.setdefaulttimeout(old_timeout)+ if rows is None:+ raise StockError("all history providers failed: " + json.dumps(attempts, ensure_ascii=False))+ if len(rows) > max_bars:+ raise StockError(f"provider returned {len(rows)} bars, exceeding max-bars={max_bars}; narrow the date range")++ retrieved = retrieved_at or dt.datetime.now(dt.timezone.utc)+ if retrieved.tzinfo is None:+ raise StockError("retrieved_at must include a timezone offset")+ version = getattr(ak_module, "__version__", None)+ if not version:+ try:+ version = importlib.metadata.version("akshare")+ except importlib.metadata.PackageNotFoundError:+ version = "test-double"+ normalized = normalize_bars({+ "bars": rows,+ "meta": {+ **instrument,+ "source": selected.replace("-", "_"),+ "provider_symbol": used_symbol,+ "provider_package": "akshare",+ "provider_version": version,+ "adjustment": adjust,+ "requested_start": first.isoformat(),+ "requested_end": last.isoformat(),+ "retrieved_at": retrieved.astimezone(dt.timezone.utc).isoformat(),+ "delay": "best_effort_not_guaranteed",+ "attempts": attempts,+ },+ }, as_of=last.isoformat())+ return {"contract_version": 1, **normalized}+++def status() -> dict[str, Any]:+ try:+ version = importlib.metadata.version("akshare")+ available = True+ except importlib.metadata.PackageNotFoundError:+ version, available = None, False+ return {+ "contract_version": 1,+ "providers": {+ "akshare_sina": {"markets": ["A", "HK", "US"], "cost": "free", "role": "primary_history"},+ "akshare_eastmoney": {"markets": ["A", "HK", "US"], "cost": "free", "role": "fallback_history"},+ },+ "dependency": {"name": "akshare", "available": available, "version": version},+ "network_checked": False,+ "credentials_required": False,+ }+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(description=__doc__)+ commands = parser.add_subparsers(dest="command", required=True)+ commands.add_parser("status")+ command = commands.add_parser("fetch")+ command.add_argument("--symbol", required=True)+ command.add_argument("--market", default="auto")+ command.add_argument("--provider", choices=["auto", *PROVIDERS], default="auto")+ command.add_argument("--provider-symbol")+ command.add_argument("--start", required=True)+ command.add_argument("--end", required=True)+ command.add_argument("--adjust", choices=["raw", "qfq", "hfq"], default="qfq")+ command.add_argument("--timeout", type=float, default=15)+ command.add_argument("--max-bars", type=int, default=5000)+ command.add_argument("--output")+ return parser+++def main(argv: Sequence[str] | None = None) -> int:+ args = build_parser().parse_args(argv)+ try:+ result = status() if args.command == "status" else fetch(+ symbol=args.symbol,+ market=args.market,+ provider=args.provider,+ provider_symbol=args.provider_symbol,+ start=args.start,+ end=args.end,+ adjust=args.adjust,+ timeout=args.timeout,+ max_bars=args.max_bars,+ )+ _emit(result, getattr(args, "output", None))+ return 0+ except (StockError, OSError, ValueError) as exc:+ sys.stderr.write(f"error: {_safe_error(exc)}\n")+ return 2+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/market.py b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/market.pynew file mode 100644index 00000000..647c49ef--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/market.py@@ -0,0 +1,342 @@+#!/usr/bin/env python3+"""Free, API-first current quotes for A-share, Hong Kong, and US equities."""++from __future__ import annotations++import argparse+import datetime as dt+import json+import re+import sys+import urllib.request+from pathlib import Path+from typing import Any, Callable, Mapping, Sequence+from zoneinfo import ZoneInfo++from stock import StockError, _emit, normalize_symbol+++TENCENT_URL = "https://qt.gtimg.cn/q={symbol}"+SINA_URL = "https://hq.sinajs.cn/list={symbol}"+USER_AGENT = "Mozilla/5.0 (compatible; Orkas-StockAnalyser/1.0)"+PROVIDERS = ("tencent", "sina")+++def provider_symbol(instrument: Mapping[str, str], provider: str) -> str:+ market, code = instrument["market"], instrument["code"]+ if market == "A":+ suffix = instrument["symbol"].rsplit(".", 1)[-1].lower()+ return f"{suffix}{code}"+ if market == "HK":+ prefix = "r_hk" if provider == "tencent" else "rt_hk"+ return f"{prefix}{code.zfill(5)}"+ prefix = "us" if provider == "tencent" else "gb_"+ return f"{prefix}{code}"+++def _number(value: Any, *, positive: bool = False) -> float | None:+ try:+ number = float(str(value).strip())+ except (TypeError, ValueError):+ return None+ if positive and number <= 0:+ return None+ return number+++def _download(+ url: str,+ timeout: float,+ opener: Callable[..., Any] | None = None,+) -> str:+ referer = (+ "https://finance.sina.com.cn/"+ if "sinajs.cn" in url+ else "https://finance.qq.com/"+ )+ request = urllib.request.Request(+ url,+ headers={"User-Agent": USER_AGENT, "Referer": referer},+ )+ open_url = opener or urllib.request.urlopen+ with open_url(request, timeout=timeout) as response:+ return response.read().decode("gb18030", errors="replace")+++def _provider_time(value: str, market: str) -> dt.datetime | None:+ text = value.strip()+ timezone = ZoneInfo({+ "A": "Asia/Shanghai",+ "HK": "Asia/Hong_Kong",+ "US": "America/New_York",+ }[market])+ formats = ["%Y%m%d%H%M%S", "%Y/%m/%d %H:%M:%S", "%Y-%m-%d %H:%M:%S"]+ for fmt in formats:+ try:+ return dt.datetime.strptime(text, fmt).replace(tzinfo=timezone)+ except ValueError:+ continue+ match = re.fullmatch(+ r"([A-Z][a-z]{2})\s+(\d{1,2})\s+(\d{1,2}:\d{2}[AP]M)\s+(?:EDT|EST)",+ text,+ )+ if match:+ try:+ parsed = dt.datetime.strptime(+ f"{dt.datetime.now(timezone).year} {match.group(1)} {match.group(2)} {match.group(3)}",+ "%Y %b %d %I:%M%p",+ )+ return parsed.replace(tzinfo=timezone)+ except ValueError:+ return None+ return None+++def _session(market: str, now: dt.datetime) -> str:+ local = now.astimezone(ZoneInfo({+ "A": "Asia/Shanghai",+ "HK": "Asia/Hong_Kong",+ "US": "America/New_York",+ }[market]))+ if local.weekday() >= 5:+ return "closed"+ current = local.time().replace(tzinfo=None)+ if market == "A":+ if dt.time(9, 15) <= current < dt.time(9, 30):+ return "opening_auction"+ if dt.time(9, 30) <= current <= dt.time(11, 30) or dt.time(13) <= current <= dt.time(15):+ return "regular"+ if dt.time(11, 30) < current < dt.time(13):+ return "lunch_break"+ return "closed"+ if market == "HK":+ if dt.time(9) <= current < dt.time(9, 30):+ return "opening_auction"+ if dt.time(9, 30) <= current <= dt.time(12) or dt.time(13) <= current <= dt.time(16):+ return "regular"+ if dt.time(12) < current < dt.time(13):+ return "lunch_break"+ if dt.time(16) < current <= dt.time(16, 10):+ return "closing_auction"+ return "closed"+ if dt.time(4) <= current < dt.time(9, 30):+ return "pre_market"+ if dt.time(9, 30) <= current <= dt.time(16):+ return "regular"+ if dt.time(16) < current <= dt.time(20):+ return "post_market"+ return "closed"+++def _parse_tencent(text: str, instrument: Mapping[str, str]) -> dict[str, Any]:+ match = re.search(r'=\s*"(.*?)"\s*;', text, flags=re.DOTALL)+ if not match:+ raise StockError("Tencent quote response did not contain a quote record")+ fields = match.group(1).split("~")+ if len(fields) < 35 or not fields[1].strip():+ raise StockError("Tencent quote record was empty or incomplete")+ market = instrument["market"]+ volume = _number(fields[6], positive=True)+ amount = _number(fields[37], positive=True) if len(fields) > 37 else None+ if market == "A":+ volume = volume * 100 if volume is not None else None+ amount = amount * 10_000 if amount is not None else None+ return {+ "name": fields[1].strip(),+ "last": _number(fields[3], positive=True),+ "previous_close": _number(fields[4], positive=True),+ "open": _number(fields[5], positive=True),+ "volume": volume,+ "bid": _number(fields[9], positive=True),+ "ask": _number(fields[19], positive=True),+ "provider_time": fields[30].strip(),+ "change": _number(fields[31]),+ "change_percent": _number(fields[32]),+ "high": _number(fields[33], positive=True),+ "low": _number(fields[34], positive=True),+ "amount": amount,+ }+++def _parse_sina(text: str, instrument: Mapping[str, str]) -> dict[str, Any]:+ match = re.search(r'=\s*"(.*?)"\s*;', text, flags=re.DOTALL)+ if not match:+ raise StockError("Sina quote response did not contain a quote record")+ fields = match.group(1).split(",")+ market = instrument["market"]+ if market == "A" and len(fields) >= 32:+ bid, ask = _number(fields[6], positive=True), _number(fields[7], positive=True)+ last = _number(fields[3], positive=True) or bid or ask+ return {+ "name": fields[0].strip(), "last": last,+ "previous_close": _number(fields[2], positive=True),+ "open": _number(fields[1], positive=True), "high": _number(fields[4], positive=True),+ "low": _number(fields[5], positive=True), "bid": bid, "ask": ask,+ "volume": _number(fields[8], positive=True), "amount": _number(fields[9], positive=True),+ "provider_time": f"{fields[30]} {fields[31]}",+ }+ if market == "HK" and len(fields) >= 19:+ return {+ "name": fields[1].strip() or fields[0].strip(),+ "last": _number(fields[6], positive=True),+ "previous_close": _number(fields[3], positive=True),+ "open": _number(fields[2], positive=True), "high": _number(fields[4], positive=True),+ "low": _number(fields[5], positive=True), "bid": _number(fields[9], positive=True),+ "ask": _number(fields[10], positive=True), "volume": _number(fields[12], positive=True),+ "amount": _number(fields[11], positive=True), "change": _number(fields[7]),+ "change_percent": _number(fields[8]), "provider_time": f"{fields[17]} {fields[18]}",+ }+ if market == "US" and len(fields) >= 31:+ return {+ "name": fields[0].strip(), "last": _number(fields[1], positive=True),+ "previous_close": _number(fields[26], positive=True),+ "open": _number(fields[5], positive=True), "high": _number(fields[6], positive=True),+ "low": _number(fields[7], positive=True), "bid": _number(fields[21], positive=True),+ "ask": None, "volume": _number(fields[10], positive=True),+ "amount": _number(fields[30], positive=True), "change": _number(fields[4]),+ "change_percent": _number(fields[2]), "provider_time": fields[25],+ }+ raise StockError("Sina quote record was empty or incomplete")+++def _safe_error(exc: Exception) -> str:+ text = re.sub(r"https?://\S+", "provider endpoint", str(exc)).strip()+ return (text or exc.__class__.__name__)[:240]+++def quote(+ symbol: str,+ market: str = "auto",+ provider: str = "auto",+ timeout: float = 10,+ *,+ opener: Callable[..., Any] | None = None,+ now: dt.datetime | None = None,+) -> dict[str, Any]:+ instrument = normalize_symbol(symbol, market)+ if provider not in {"auto", *PROVIDERS}:+ raise StockError("provider must be auto, tencent, or sina")+ if timeout <= 0 or timeout > 30:+ raise StockError("timeout must be greater than 0 and at most 30 seconds")+ retrieved_at = now or dt.datetime.now(dt.timezone.utc)+ if retrieved_at.tzinfo is None:+ raise StockError("now must include a timezone offset")+ session = _session(instrument["market"], retrieved_at)+ stale_limit = 900 if session in {"regular", "opening_auction", "closing_auction"} else 345_600+ providers = PROVIDERS if provider == "auto" else (provider,)+ attempts: list[dict[str, str]] = []+ record: dict[str, Any] | None = None+ selected = ""+ selected_symbol = ""+ for candidate in providers:+ candidate_symbol = provider_symbol(instrument, candidate)+ url = (TENCENT_URL if candidate == "tencent" else SINA_URL).format(symbol=candidate_symbol)+ try:+ body = _download(url, timeout, opener)+ record = (_parse_tencent if candidate == "tencent" else _parse_sina)(body, instrument)+ selected, selected_symbol = candidate, candidate_symbol+ attempts.append({"provider": candidate, "status": "ok"})+ break+ except Exception as exc: # provider failures are returned as bounded evidence+ attempts.append({"provider": candidate, "status": "error", "detail": _safe_error(exc)})+ if record is None:+ raise StockError("all quote providers failed: " + json.dumps(attempts, ensure_ascii=False))++ observed_at = _provider_time(str(record.pop("provider_time", "")), instrument["market"])+ flags = ["provider_delay_not_guaranteed", "exchange_calendar_not_verified"]+ if session != "regular":+ flags.append("outside_regular_session")+ quote_kind = "last_trade"+ if session in {"opening_auction", "closing_auction"}:+ quote_kind = "indicative"+ flags.append("indicative_quote")+ elif session != "regular":+ quote_kind = "last_market_snapshot"+ if record.get("last") is None and record.get("previous_close") is not None:+ record["last"] = record["previous_close"]+ flags.append("no_current_trade_used_previous_close")+ quote_kind = "previous_close"+ if record.get("last") is None:+ raise StockError(f"{selected} returned no usable current or previous price")+ age_seconds = None+ if observed_at is None:+ flags.append("provider_timestamp_unavailable")+ else:+ observed_age = (+ retrieved_at.astimezone(dt.timezone.utc)+ - observed_at.astimezone(dt.timezone.utc)+ ).total_seconds()+ age_seconds = max(0.0, observed_age)+ if observed_age < 0:+ flags.append("provider_timestamp_in_future")+ if age_seconds > stale_limit:+ flags.append("stale_quote")++ quote_fields = {+ key: record.get(key)+ for key in (+ "last", "previous_close", "open", "high", "low", "bid", "ask",+ "volume", "amount", "change", "change_percent",+ )+ }+ return {+ "contract_version": 1,+ "instrument": {**instrument, "name": record.get("name") or None},+ "quote": quote_fields,+ "provenance": {+ "source": f"{selected}_quote_api",+ "provider_symbol": selected_symbol,+ "observed_at": observed_at.isoformat() if observed_at else None,+ "retrieved_at": retrieved_at.astimezone(dt.timezone.utc).isoformat(),+ "delay": "best_effort_not_guaranteed",+ },+ "quality": {+ "market_session": session,+ "quote_kind": quote_kind,+ "age_seconds": round(age_seconds, 3) if age_seconds is not None else None,+ "flags": sorted(set(flags)),+ "attempts": attempts,+ },+ }+++def status() -> dict[str, Any]:+ return {+ "contract_version": 1,+ "providers": {+ "tencent": {"markets": ["A", "HK", "US"], "cost": "free", "role": "primary_quote"},+ "sina": {"markets": ["A", "HK", "US"], "cost": "free", "role": "fallback_quote"},+ },+ "network_checked": False,+ "credentials_required": False,+ }+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(description=__doc__)+ commands = parser.add_subparsers(dest="command", required=True)+ commands.add_parser("status")+ current = commands.add_parser("quote")+ current.add_argument("--symbol", required=True)+ current.add_argument("--market", default="auto")+ current.add_argument("--provider", choices=["auto", *PROVIDERS], default="auto")+ current.add_argument("--timeout", type=float, default=10)+ current.add_argument("--output")+ return parser+++def main(argv: Sequence[str] | None = None) -> int:+ args = build_parser().parse_args(argv)+ try:+ result = status() if args.command == "status" else quote(+ args.symbol, args.market, args.provider, args.timeout,+ )+ _emit(result, getattr(args, "output", None))+ return 0+ except (StockError, OSError, ValueError) as exc:+ sys.stderr.write(f"error: {_safe_error(exc)}\n")+ return 2+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/stock.py b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/stock.pynew file mode 100644index 00000000..b6dd256e--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/stock.py@@ -0,0 +1,1229 @@+#!/usr/bin/env python3+"""Deterministic, dependency-free financial calculations for StockAnalyser.++This module never submits orders and never reads or prints credentials.+Provider access lives in the dedicated market.py and history.py adapters.+"""++from __future__ import annotations++import argparse+import csv+import datetime as dt+import hashlib+import json+import math+import statistics+import sys+from pathlib import Path+from typing import Any, Iterable, Mapping, Sequence+++TRADING_DAYS = 252+SUPPORTED_MARKETS = {"A", "HK", "US"}+MARKET_META = {+ "A": {"currency": "CNY", "timezone": "Asia/Shanghai"},+ "HK": {"currency": "HKD", "timezone": "Asia/Hong_Kong"},+ "US": {"currency": "USD", "timezone": "America/New_York"},+}+++class StockError(ValueError):+ """A user-actionable data or analysis contract error."""+++def _finite(value: Any, field: str, *, optional: bool = False) -> float | None:+ if value is None or value == "":+ if optional:+ return None+ raise StockError(f"missing numeric field: {field}")+ if isinstance(value, bool):+ raise StockError(f"invalid numeric field: {field}")+ try:+ number = float(value)+ except (TypeError, ValueError) as exc:+ raise StockError(f"invalid numeric field: {field}") from exc+ if not math.isfinite(number):+ raise StockError(f"non-finite numeric field: {field}")+ return number+++def _rounded(value: float | None, digits: int = 8) -> float | None:+ if value is None or not math.isfinite(value):+ return None+ return round(value, digits)+++def _read(path: str) -> Any:+ source = Path(path)+ if not source.is_file():+ raise StockError(f"input file not found: {source}")+ if source.suffix.lower() == ".csv":+ with source.open("r", encoding="utf-8-sig", newline="") as handle:+ return list(csv.DictReader(handle))+ try:+ return json.loads(source.read_text(encoding="utf-8"))+ except json.JSONDecodeError as exc:+ raise StockError(f"invalid JSON at line {exc.lineno}, column {exc.colno}") from exc+++def _emit(payload: Any, output: str | None = None) -> None:+ text = json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n"+ if output:+ target = Path(output)+ if not target.parent.is_dir():+ raise StockError(f"output directory does not exist: {target.parent}")+ target.write_text(text, encoding="utf-8")+ else:+ sys.stdout.write(text)+++def _parse_date(value: Any, field: str = "date") -> dt.date:+ text = str(value or "").strip()+ if not text:+ raise StockError(f"missing {field}")+ try:+ return dt.date.fromisoformat(text[:10])+ except ValueError as exc:+ raise StockError(f"{field} must be ISO-8601: {text}") from exc+++def _parse_timestamp(value: Any, field: str) -> dt.datetime:+ text = str(value or "").strip().replace("Z", "+00:00")+ if not text:+ raise StockError(f"missing {field}")+ try:+ parsed = dt.datetime.fromisoformat(text)+ except ValueError as exc:+ raise StockError(f"{field} must be ISO-8601: {text}") from exc+ if parsed.tzinfo is None:+ raise StockError(f"{field} must include a timezone offset")+ return parsed+++def normalize_symbol(symbol: str, market: str = "auto") -> dict[str, str]:+ raw = str(symbol or "").strip().upper().replace(" ", "")+ if not raw:+ raise StockError("symbol is required")+ requested = str(market or "auto").strip().upper()+ if requested not in SUPPORTED_MARKETS | {"AUTO"}:+ raise StockError("market must be auto, A, HK, or US")++ for old, new in ((".XSHG", ".SH"), (".XSHE", ".SZ"), (".XHKG", ".HK")):+ if raw.endswith(old):+ raw = raw[: -len(old)] + new+ if raw.startswith("SH") and raw[2:].isdigit():+ raw = raw[2:] + ".SH"+ elif raw.startswith("SZ") and raw[2:].isdigit():+ raw = raw[2:] + ".SZ"+ elif raw.startswith("BJ") and raw[2:].isdigit():+ raw = raw[2:] + ".BJ"+ elif raw.startswith("HK") and raw[2:].isdigit():+ raw = str(int(raw[2:])) + ".HK"++ suffix_market = None+ if raw.endswith((".SH", ".SZ", ".BJ")):+ suffix_market = "A"+ elif raw.endswith(".HK"):+ suffix_market = "HK"+ elif raw.endswith(".US"):+ suffix_market = "US"+ if suffix_market:+ if requested != "AUTO" and requested != suffix_market:+ raise StockError(f"symbol suffix conflicts with market {requested}")+ if suffix_market == "HK":+ raw = str(int(raw[:-3])) + ".HK"+ code = raw.rsplit(".", 1)[0]+ return {"symbol": raw, "code": code, "market": suffix_market, **MARKET_META[suffix_market]}++ resolved = requested+ if resolved == "AUTO":+ if raw.isdigit() and len(raw) == 6:+ resolved = "A"+ elif raw.isdigit() and len(raw) <= 5:+ resolved = "HK"+ else:+ resolved = "US"++ if resolved == "A":+ if not (raw.isdigit() and len(raw) == 6):+ raise StockError("A-share symbols must contain six digits")+ if raw[0] in {"6", "9"}:+ suffix = "SH"+ elif raw[0] in {"0", "2", "3"}:+ suffix = "SZ"+ elif raw[0] in {"4", "8"}:+ suffix = "BJ"+ else:+ raise StockError("cannot infer A-share exchange from symbol")+ canonical = f"{raw}.{suffix}"+ elif resolved == "HK":+ if not (raw.isdigit() and 1 <= len(raw) <= 5):+ raise StockError("Hong Kong symbols must contain one to five digits")+ canonical = f"{int(raw)}.HK"+ else:+ if not all(char.isalnum() or char in {".", "-"} for char in raw):+ raise StockError("invalid US ticker")+ canonical = f"{raw}.US"+ return {"symbol": canonical, "code": canonical.rsplit(".", 1)[0], "market": resolved, **MARKET_META[resolved]}+++ALIASES = {+ "date": ("date", "datetime", "日期", "时间"),+ "open": ("open", "开盘"),+ "high": ("high", "最高"),+ "low": ("low", "最低"),+ "close": ("close", "收盘"),+ "volume": ("volume", "vol", "成交量"),+ "amount": ("amount", "turnover", "成交额"),+ "adjusted_close": ("adjusted_close", "adj_close", "复权收盘"),+}+++def _lookup(row: Mapping[str, Any], field: str) -> Any:+ lower = {str(key).strip().lower(): value for key, value in row.items()}+ for alias in ALIASES[field]:+ if alias.lower() in lower:+ return lower[alias.lower()]+ return None+++def normalize_bars(payload: Any, *, as_of: str | None = None) -> dict[str, Any]:+ if isinstance(payload, list):+ rows, meta = payload, {}+ elif isinstance(payload, Mapping) and isinstance(payload.get("bars"), list):+ rows, meta = payload["bars"], dict(payload.get("meta") or {})+ else:+ raise StockError("history input must be a row array or an object with bars")+ if len(rows) < 2:+ raise StockError("at least two price bars are required")++ clean = []+ for index, row in enumerate(rows):+ if not isinstance(row, Mapping):+ raise StockError(f"bar {index} must be an object")+ date = _parse_date(_lookup(row, "date"), f"bar {index} date")+ item = {"date": date.isoformat()}+ for field in ("open", "high", "low", "close", "volume"):+ item[field] = _finite(_lookup(row, field), f"bar {index} {field}")+ for field in ("amount", "adjusted_close"):+ value = _finite(_lookup(row, field), f"bar {index} {field}", optional=True)+ if value is not None:+ item[field] = value+ if min(item["open"], item["high"], item["low"], item["close"]) <= 0:+ raise StockError(f"bar {index} prices must be positive")+ if item["high"] < max(item["open"], item["low"], item["close"]):+ raise StockError(f"bar {index} high is below another OHLC value")+ if item["low"] > min(item["open"], item["high"], item["close"]):+ raise StockError(f"bar {index} low is above another OHLC value")+ if item["volume"] < 0:+ raise StockError(f"bar {index} volume must be non-negative")+ clean.append(item)++ clean.sort(key=lambda row: row["date"])+ dates = [row["date"] for row in clean]+ if len(set(dates)) != len(dates):+ raise StockError("duplicate bar dates are not allowed")+ adjusted = ["adjusted_close" in row for row in clean]+ if any(adjusted) and not all(adjusted):+ raise StockError("adjusted_close must cover every bar or none")++ reference = _parse_date(as_of, "as_of") if as_of else dt.datetime.now(dt.timezone.utc).date()+ last = _parse_date(dates[-1])+ age = (reference - last).days+ if age < 0:+ raise StockError("last bar is later than as_of")+ gaps = sum(+ 1 for left, right in zip(clean, clean[1:])+ if (_parse_date(right["date"]) - _parse_date(left["date"])).days > 10+ )+ flags = []+ if age > 7:+ flags.append("stale_daily_history")+ if gaps:+ flags.append("calendar_gaps_over_10_days")+ if not all(row["volume"] > 0 for row in clean):+ flags.append("zero_volume_bars")+ return {+ "bars": clean,+ "meta": meta,+ "quality": {+ "bar_count": len(clean),+ "first_date": dates[0],+ "last_date": dates[-1],+ "age_days": age,+ "gap_count": gaps,+ "price_field": "adjusted_close" if all(adjusted) else "close",+ "flags": flags,+ },+ }+++def select_history_cutoff(payload: Any, cutoff: str) -> tuple[Any, list[dict[str, Any]]]:+ """Explicit price-history selection, separate from strict as-of validation.++ Preserve input metadata and never manufacture point-in-time fundamentals.+ The same selection applies to analysis, screening and backtesting.+ """+ date = _parse_date(cutoff, "cutoff").isoformat()+ if isinstance(payload, Mapping) and "securities" in payload:+ if not isinstance(payload["securities"], list):+ raise StockError("securities must be an array")+ selected, audit = [], []+ for security in payload["securities"]:+ item, records = select_history_cutoff(security, date)+ selected.append(item)+ audit.extend(records)+ return {**payload, "securities": selected}, audit+ rows = payload if isinstance(payload, list) else payload.get("bars") if isinstance(payload, Mapping) else None+ if not isinstance(rows, list):+ raise StockError("cutoff selection requires price bars")+ dates = []+ for row in rows:+ if not isinstance(row, Mapping):+ raise StockError("cutoff bar must be an object")+ dates.append(_parse_date(_lookup(row, "date")).isoformat())+ if len(set(dates)) != len(dates):+ raise StockError("duplicate bar dates are not allowed")+ retained = [dict(row) for row, observed in zip(rows, dates) if observed <= date]+ if len(retained) < 2:+ raise StockError("cutoff leaves fewer than two price bars")+ record = {"symbol": payload.get("symbol") if isinstance(payload, Mapping) else None,+ "cutoff": date, "input_bars": len(rows), "retained_bars": len(retained),+ "excluded_bars": len(rows) - len(retained)}+ return (retained if isinstance(payload, list) else {**payload, "bars": retained}), [record]+++def _returns(values: Sequence[float]) -> list[float]:+ return [values[index] / values[index - 1] - 1 for index in range(1, len(values))]+++def _sma(values: Sequence[float], window: int, end: int | None = None) -> float | None:+ stop = len(values) if end is None else end + 1+ if stop < window:+ return None+ return statistics.fmean(values[stop - window:stop])+++def _ema(values: Sequence[float], window: int) -> list[float]:+ alpha = 2 / (window + 1)+ output = [values[0]]+ for value in values[1:]:+ output.append(alpha * value + (1 - alpha) * output[-1])+ return output+++def _rsi_series(values: Sequence[float], window: int = 14) -> list[float | None]:+ output: list[float | None] = [None] * len(values)+ if len(values) <= window:+ return output+ changes = [values[index] - values[index - 1] for index in range(1, len(values))]+ gains = [max(change, 0) for change in changes]+ losses = [max(-change, 0) for change in changes]+ avg_gain = statistics.fmean(gains[:window])+ avg_loss = statistics.fmean(losses[:window])+ output[window] = 100.0 if avg_loss == 0 else 100 - 100 / (1 + avg_gain / avg_loss)+ for index in range(window + 1, len(values)):+ avg_gain = (avg_gain * (window - 1) + gains[index - 1]) / window+ avg_loss = (avg_loss * (window - 1) + losses[index - 1]) / window+ output[index] = 100.0 if avg_loss == 0 else 100 - 100 / (1 + avg_gain / avg_loss)+ return output+++def _max_drawdown(returns: Sequence[float]) -> float:+ equity = peak = 1.0+ worst = 0.0+ for value in returns:+ equity *= 1 + value+ peak = max(peak, equity)+ worst = min(worst, equity / peak - 1)+ return worst+++def _annual_volatility(returns: Sequence[float]) -> float | None:+ if len(returns) < 2:+ return None+ return statistics.stdev(returns) * math.sqrt(TRADING_DAYS)+++def _ratio(value: Any) -> float | None:+ number = _finite(value, "fundamental", optional=True)+ if number is None:+ return None+ return number / 100 if abs(number) > 2 else number+++def _scale(value: float | None, low: float, high: float, *, inverse: bool = False) -> float | None:+ if value is None:+ return None+ score = max(0.0, min(1.0, (value - low) / (high - low))) * 100+ return 100 - score if inverse else score+++def _mean_present(values: Iterable[float | None]) -> float | None:+ present = [value for value in values if value is not None]+ return statistics.fmean(present) if present else None+++def _fundamental_profile(raw: Mapping[str, Any]) -> dict[str, Any]:+ values = {+ "roe": _ratio(raw.get("roe")),+ "net_margin": _ratio(raw.get("net_margin")),+ "free_cash_flow_margin": _ratio(raw.get("free_cash_flow_margin")),+ "revenue_growth": _ratio(raw.get("revenue_growth")),+ "net_income_growth": _ratio(raw.get("net_income_growth")),+ "debt_to_equity": _finite(raw.get("debt_to_equity"), "debt_to_equity", optional=True),+ "current_ratio": _finite(raw.get("current_ratio"), "current_ratio", optional=True),+ "pe": _finite(raw.get("pe"), "pe", optional=True),+ "pb": _finite(raw.get("pb"), "pb", optional=True),+ }+ profitability = _mean_present([+ _scale(values["roe"], 0, 0.25),+ _scale(values["net_margin"], 0, 0.25),+ _scale(values["free_cash_flow_margin"], 0, 0.20),+ ])+ growth = _mean_present([+ _scale(values["revenue_growth"], -0.20, 0.30),+ _scale(values["net_income_growth"], -0.30, 0.40),+ ])+ balance = _mean_present([+ _scale(values["debt_to_equity"], 0, 2, inverse=True),+ _scale(values["current_ratio"], 0.5, 2.0),+ ])+ pe_score = None if values["pe"] is None or values["pe"] <= 0 else _scale(values["pe"], 5, 50, inverse=True)+ pb_score = None if values["pb"] is None or values["pb"] <= 0 else _scale(values["pb"], 0.5, 10, inverse=True)+ valuation = _mean_present([pe_score, pb_score])+ available = sum(value is not None for value in values.values())+ return {+ "values": {key: _rounded(value) for key, value in values.items()},+ "dimensions": {+ "profitability": _rounded(profitability, 4),+ "growth": _rounded(growth, 4),+ "balance_sheet": _rounded(balance, 4),+ "valuation_heuristic": _rounded(valuation, 4),+ },+ "coverage": {"available": available, "possible": len(values)},+ "caveat": "Heuristic dimensions are for evidence organization, not a universal rating.",+ }+++def analyze_payload(payload: Any, *, symbol: str | None, source: str | None, as_of: str | None) -> dict[str, Any]:+ if isinstance(payload, Mapping) and "securities" in payload:+ securities = payload["securities"]+ if not isinstance(securities, list):+ raise StockError("analysis universe requires a securities array")+ if not symbol:+ raise StockError("analysis universe requires --symbol to select one security")+ target = normalize_symbol(symbol)["symbol"]+ matches = []+ for index, security in enumerate(securities):+ if not isinstance(security, Mapping):+ raise StockError(f"security {index} must be an object")+ if normalize_symbol(security.get("symbol"))["symbol"] == target:+ matches.append(security)+ if not matches:+ raise StockError(f"analysis symbol not found in securities: {target}")+ if len(matches) != 1:+ raise StockError(f"analysis symbol is ambiguous in securities: {target}")+ # Keep the selected record intact; extracting only bars loses the+ # declared adjustment, cutoff, source and fundamental coverage.+ payload = matches[0]+ selected_meta = payload.get("meta") or {}+ if not isinstance(selected_meta, Mapping):+ raise StockError("security meta must be an object")+ if selected_meta.get("symbol") and normalize_symbol(selected_meta["symbol"])["symbol"] != target:+ raise StockError("security symbol conflicts with meta.symbol")+ meta = (payload.get("meta") or {}) if isinstance(payload, Mapping) else {}+ if not isinstance(meta, Mapping):+ raise StockError("history meta must be an object")+ as_of = as_of or meta.get("as_of")+ normalized = normalize_bars(payload, as_of=as_of)+ bars = normalized["bars"]+ price_field = normalized["quality"]["price_field"]+ prices = [float(row[price_field]) for row in bars]+ daily = _returns(prices)+ ema12, ema26 = _ema(prices, 12), _ema(prices, 26)+ macd_line = [left - right for left, right in zip(ema12, ema26)]+ signal = _ema(macd_line, 9)+ true_ranges = []+ for index, row in enumerate(bars):+ previous = prices[index - 1] if index else float(row["close"])+ true_ranges.append(max(row["high"] - row["low"], abs(row["high"] - previous), abs(row["low"] - previous)))+ rsi = _rsi_series(prices)++ returns_by_window = {}+ for window in (20, 60, 252):+ returns_by_window[str(window)] = _rounded(prices[-1] / prices[-1 - window] - 1) if len(prices) > window else None+ average_turnover = statistics.fmean(+ row.get("amount", row["close"] * row["volume"]) for row in bars[-20:]+ )+ meta = normalized["meta"]+ inferred_symbol = symbol or meta.get("symbol")+ instrument = normalize_symbol(inferred_symbol) if inferred_symbol else None+ fundamentals = payload.get("fundamentals", {}) if isinstance(payload, Mapping) else {}+ return {+ "contract_version": 1,+ "instrument": instrument,+ "provenance": {+ "source": source or (payload.get("source") if isinstance(payload, Mapping) else None)+ or meta.get("source") or "user-file",+ "as_of": as_of or normalized["quality"]["last_date"],+ "adjustment": meta.get("adjustment", "unspecified"),+ "evidence_type": "calculated_from_price_history",+ },+ "quality": normalized["quality"],+ "technical": {+ "last_price": _rounded(prices[-1]),+ "return_20": returns_by_window["20"],+ "return_60": returns_by_window["60"],+ "return_252": returns_by_window["252"],+ "sma_20": _rounded(_sma(prices, 20)),+ "sma_60": _rounded(_sma(prices, 60)),+ "rsi_14": _rounded(rsi[-1], 4),+ "macd": _rounded(macd_line[-1]),+ "macd_signal": _rounded(signal[-1]),+ "atr_14": _rounded(statistics.fmean(true_ranges[-14:]) if len(true_ranges) >= 14 else None),+ "annualized_volatility": _rounded(_annual_volatility(daily)),+ "max_drawdown": _rounded(_max_drawdown(daily)),+ "average_turnover_20": _rounded(average_turnover, 2),+ },+ "fundamental": _fundamental_profile(fundamentals if isinstance(fundamentals, Mapping) else {}),+ }+++def _percentile(values: Mapping[str, float], key: str) -> float | None:+ if key not in values:+ return None+ ordered = sorted(values.values())+ if len(ordered) == 1:+ return 50.0+ rank = sum(item < values[key] for item in ordered) + 0.5 * sum(item == values[key] for item in ordered) - 0.5+ return 100 * rank / (len(ordered) - 1)+++def screen_payload(payload: Any, *, as_of: str | None) -> dict[str, Any]:+ if not isinstance(payload, Mapping) or not isinstance(payload.get("securities"), list):+ raise StockError("screen input requires a securities array")+ # All peers share the screen cutoff, even when their records carry older+ # observation dates. A historical record must not refresh itself.+ as_of = as_of or dt.datetime.now(dt.timezone.utc).date().isoformat()+ filters = payload.get("filters") or {}+ weights = payload.get("weights") or {+ "momentum": 0.30, "quality": 0.30, "valuation": 0.15,+ "low_volatility": 0.15, "liquidity": 0.10,+ }+ if not isinstance(filters, Mapping) or not isinstance(weights, Mapping):+ raise StockError("filters and weights must be objects")+ allowed_factors = {"momentum", "quality", "valuation", "low_volatility", "liquidity"}+ factor_weights = {}+ for name, value in weights.items():+ if name not in allowed_factors:+ raise StockError(f"unsupported factor weight: {name}")+ number = _finite(value, f"weight {name}")+ if number <= 0:+ raise StockError("factor weights must be positive")+ factor_weights[name] = number++ rows = []+ for index, security in enumerate(payload["securities"]):+ if not isinstance(security, Mapping):+ raise StockError(f"security {index} must be an object")+ symbol = str(security.get("symbol") or "")+ analysis = analyze_payload(+ {"bars": security.get("bars"), "meta": security.get("meta", {}), "fundamentals": security.get("fundamentals", {})},+ symbol=symbol, source=str(security.get("source") or "screen-input"), as_of=as_of,+ )+ tech = analysis["technical"]+ fund = analysis["fundamental"]+ fvalues = fund["values"]+ quality = _mean_present([fund["dimensions"][name] for name in ("profitability", "growth", "balance_sheet")])+ raw_factors = {+ "momentum": tech["return_60"] if tech["return_60"] is not None else tech["return_20"],+ "quality": quality,+ "valuation": fund["dimensions"]["valuation_heuristic"],+ "low_volatility": -tech["annualized_volatility"] if tech["annualized_volatility"] is not None else None,+ "liquidity": math.log1p(tech["average_turnover_20"]) if tech["average_turnover_20"] is not None else None,+ }+ reasons = []+ checks = {+ "min_roe": fvalues["roe"],+ "max_pe": fvalues["pe"],+ "min_momentum_60": tech["return_60"],+ "max_volatility": tech["annualized_volatility"],+ "min_avg_turnover": tech["average_turnover_20"],+ }+ for name, threshold in filters.items():+ if name not in checks:+ raise StockError(f"unsupported screen filter: {name}")+ actual = checks[name]+ limit = _finite(threshold, name)+ if actual is None:+ reasons.append(f"missing:{name}")+ elif name.startswith("min_") and actual < limit:+ reasons.append(f"below:{name}")+ elif name.startswith("max_") and actual > limit:+ reasons.append(f"above:{name}")+ rows.append({+ "symbol": analysis["instrument"]["symbol"],+ "eligible": not reasons,+ "exclusion_reasons": reasons,+ "quality_flags": analysis["quality"]["flags"],+ "factor_raw": {key: _rounded(value) for key, value in raw_factors.items()},+ "factor_coverage": sum(value is not None for value in raw_factors.values()),+ })++ eligible = [row for row in rows if row["eligible"]]+ universes = {+ factor: {row["symbol"]: row["factor_raw"][factor] for row in eligible if row["factor_raw"][factor] is not None}+ for factor in allowed_factors+ }+ for row in eligible:+ components = {factor: _percentile(universes[factor], row["symbol"]) for factor in factor_weights}+ present_weight = sum(factor_weights[name] for name, value in components.items() if value is not None)+ row["factor_percentiles"] = {key: _rounded(value, 4) for key, value in components.items()}+ row["score"] = _rounded(sum(factor_weights[name] * value for name, value in components.items() if value is not None) / present_weight, 4) if present_weight else None+ row["score_weight_coverage"] = _rounded(present_weight / sum(factor_weights.values()), 4)+ eligible.sort(key=lambda row: (row.get("score") is not None, row.get("score") or -1), reverse=True)+ for rank, row in enumerate(eligible, 1):+ row["rank"] = rank+ excluded = [row for row in rows if not row["eligible"]]+ return {+ "contract_version": 1,+ "as_of": as_of,+ "eligible_count": len(eligible),+ "excluded_count": len(excluded),+ "ranking": eligible,+ "excluded": excluded,+ "caveats": [+ "Scores are cross-sectional research priorities, not investment instructions.",+ "Missing factors are excluded and lower score_weight_coverage.",+ ],+ }+++def _performance(returns: Sequence[float]) -> dict[str, Any]:+ equity = math.prod(1 + value for value in returns)+ annual = equity ** (TRADING_DAYS / len(returns)) - 1 if returns and equity > 0 else None+ volatility = _annual_volatility(returns)+ sharpe = None+ if len(returns) >= 2 and statistics.stdev(returns) > 0:+ sharpe = statistics.fmean(returns) / statistics.stdev(returns) * math.sqrt(TRADING_DAYS)+ return {+ "total_return": _rounded(equity - 1),+ "annualized_return": _rounded(annual),+ "annualized_volatility": _rounded(volatility),+ "sharpe_zero_rate": _rounded(sharpe),+ "max_drawdown": _rounded(_max_drawdown(returns)),+ }+++def backtest_payload(payload: Any, args: argparse.Namespace) -> dict[str, Any]:+ normalized = normalize_bars(payload, as_of=args.as_of)+ bars = normalized["bars"]+ field = normalized["quality"]["price_field"]+ prices = [row[field] for row in bars]+ if len(prices) < 10:+ raise StockError("backtest requires at least ten bars")+ signals = [0.0] * len(prices)+ parameters: dict[str, Any]+ if args.strategy == "sma_cross":+ if args.fast < 2 or args.slow <= args.fast:+ raise StockError("sma_cross requires 2 <= fast < slow")+ for index in range(len(prices)):+ fast, slow = _sma(prices, args.fast, index), _sma(prices, args.slow, index)+ signals[index] = 1.0 if fast is not None and slow is not None and fast > slow else 0.0+ parameters = {"fast": args.fast, "slow": args.slow}+ elif args.strategy == "momentum":+ if args.lookback < 2:+ raise StockError("momentum lookback must be at least two")+ for index in range(args.lookback, len(prices)):+ signals[index] = 1.0 if prices[index] / prices[index - args.lookback] - 1 > args.threshold else 0.0+ parameters = {"lookback": args.lookback, "threshold": args.threshold}+ else:+ if not (0 < args.rsi_entry < args.rsi_exit < 100):+ raise StockError("rsi_reversion requires 0 < entry < exit < 100")+ rsi = _rsi_series(prices)+ held = 0.0+ for index, value in enumerate(rsi):+ if value is not None and value < args.rsi_entry:+ held = 1.0+ elif value is not None and value > args.rsi_exit:+ held = 0.0+ signals[index] = held+ parameters = {"entry": args.rsi_entry, "exit": args.rsi_exit}++ rate = (args.cost_bps + args.slippage_bps) / 10_000+ net_returns, benchmark_returns, applied_positions, trades = [], [], [], []+ previous_position = 0.0+ equity = benchmark_equity = 1.0+ series = []+ for index in range(1, len(prices)):+ position = signals[index - 1]+ gross = prices[index] / prices[index - 1] - 1+ turnover = abs(position - previous_position)+ net = position * gross - turnover * rate+ net_returns.append(net)+ benchmark_returns.append(gross)+ applied_positions.append(position)+ trades.append(turnover)+ equity *= 1 + net+ benchmark_equity *= 1 + gross+ if args.include_series:+ series.append({+ "date": bars[index]["date"], "equity": _rounded(equity),+ "benchmark_equity": _rounded(benchmark_equity), "position": position,+ "signal_date": bars[index - 1]["date"],+ "previous_position": previous_position, "turnover_units": turnover,+ "asset_return": _rounded(gross), "gross_return": _rounded(position * gross),+ "cost_return": _rounded(turnover * rate), "net_return": _rounded(net),+ })+ previous_position = position++ result = {+ "contract_version": 1,+ "strategy": args.strategy,+ "parameters": parameters,+ "sample": {"start": bars[0]["date"], "end": bars[-1]["date"], "bars": len(bars)},+ "data_quality": normalized["quality"],+ "execution": {+ "cost_bps": args.cost_bps,+ "slippage_bps": args.slippage_bps,+ "lookahead_guard": "signal_at_close_t_is_applied_to_return_t_plus_1",+ "dividends_taxes_borrow_market_impact": "not_modeled",+ },+ "performance": {+ **_performance(net_returns),+ "turnover_units": _rounded(sum(trades)),+ "trade_entries": sum(1 for left, right in zip([0.0] + applied_positions, applied_positions) if left == 0 and right > 0),+ "exposure": _rounded(statistics.fmean(applied_positions)),+ "active_period_hit_rate": _rounded(statistics.fmean([value > 0 for value, position in zip(net_returns, applied_positions) if position > 0])) if any(applied_positions) else None,+ },+ "benchmark": {"name": "buy_and_hold_same_price_series", **_performance(benchmark_returns)},+ "limitations": ["No out-of-sample claim is created by this command.", "Corporate-action accuracy follows the supplied adjustment metadata."],+ }+ if args.include_series:+ result["series"] = series+ return result+++def portfolio_payload(payload: Any, confidence: float, *, mode: str = "historical") -> dict[str, Any]:+ if not isinstance(payload, Mapping) or not isinstance(payload.get("positions"), list):+ raise StockError("portfolio input requires a positions array")+ if not 0.5 < confidence < 1:+ raise StockError("confidence must be between 0.5 and 1")+ if mode not in ("historical", "scenario"):+ raise StockError("portfolio mode must be historical or scenario")+ positions = payload["positions"]+ if not positions:+ raise StockError("portfolio requires at least one position")+ clean = []+ lengths = set()+ for index, item in enumerate(positions):+ if not isinstance(item, Mapping):+ raise StockError(f"position {index} must be an object")+ weight = _finite(item.get("weight"), f"position {index} weight")+ if weight < 0:+ raise StockError("long-only portfolio weights must be non-negative")+ raw_returns = item.get("returns")+ if mode == "historical" and (not isinstance(raw_returns, list) or len(raw_returns) < 2):+ raise StockError(f"position {index} requires at least two returns")+ returns = [_finite(value, f"position {index} return") for value in raw_returns] if mode == "historical" else []+ lengths.add(len(returns))+ clean.append({+ "symbol": normalize_symbol(str(item.get("symbol") or ""))["symbol"],+ "weight": weight,+ "currency": str(item.get("currency") or "UNKNOWN").upper(),+ "sector": str(item.get("sector") or "UNKNOWN"),+ "returns": returns,+ })+ if len(lengths) != 1:+ raise StockError("position return arrays must be aligned and equal length")+ if len({item["symbol"] for item in clean}) != len(clean):+ raise StockError("portfolio contains duplicate securities")+ weight_sum = sum(item["weight"] for item in clean)+ if not 0.99 <= weight_sum <= 1.01:+ raise StockError(f"portfolio weights must sum to one, got {weight_sum:.6f}")+ for item in clean:+ item["weight"] /= weight_sum+ count = lengths.pop()+ daily = [sum(item["weight"] * item["returns"][index] for item in clean) for index in range(count)]+ losses = sorted(-value for value in daily)+ quantile_index = min(len(losses) - 1, max(0, math.ceil(confidence * len(losses)) - 1))+ var = max(0.0, losses[quantile_index]) if losses else None+ tail = [loss for loss in losses if loss >= var] if losses else []++ def exposure(field: str) -> dict[str, float]:+ values: dict[str, float] = {}+ for item in clean:+ values[item[field]] = values.get(item[field], 0) + item["weight"]+ return {key: _rounded(value) for key, value in sorted(values.items())}++ scenario_results = {}+ scenarios = payload.get("scenarios") or {}+ if not isinstance(scenarios, Mapping):+ raise StockError("scenarios must be an object")+ for name, spec in scenarios.items():+ if not isinstance(spec, Mapping):+ raise StockError(f"scenario {name} must be an object")+ if "local" in spec or "fx" in spec:+ # Explicit compound scenario protocol; legacy linear scenarios below+ # retain their existing semantics. Weights must be in base NAV.+ base = payload.get("base_currency")+ if not isinstance(base, str) or not base or base != base.upper():+ raise StockError("compound scenarios require uppercase base_currency and base-NAV weights")+ local, fx = spec.get("local", {}), spec.get("fx", {})+ if not isinstance(local, Mapping) or not isinstance(fx, Mapping):+ raise StockError("scenario local and fx must be objects")+ if set(spec) - {"local", "fx"}:+ raise StockError("compound scenario supports only local and fx")+ symbols = {item["symbol"] for item in clean}+ currencies = {item["currency"] for item in clean}+ if set(local) - symbols or set(fx) - currencies or "UNKNOWN" in currencies:+ raise StockError("scenario shocks must reference declared securities and currencies")+ if base in fx and _finite(fx[base], "base currency shock") != 0:+ raise StockError("base currency cannot have an FX shock against itself")+ contributions, local_total, fx_total = {}, 0.0, 0.0+ for item in clean:+ price_shock = _finite(local.get(item["symbol"], 0), "local shock")+ fx_shock = _finite(fx.get(item["currency"], 0), "FX shock")+ if price_shock < -1 or fx_shock < -1:+ raise StockError("scenario price and FX returns cannot be below -100 percent")+ local_total += item["weight"] * price_shock+ fx_total += item["weight"] * fx_shock+ contributions[item["symbol"]] = _rounded(item["weight"] * ((1 + price_shock) * (1 + fx_shock) - 1))+ scenario_results[str(name)] = {"portfolio_return": _rounded(sum(contributions.values())),+ "local_only_return": _rounded(local_total), "fx_only_return": _rounded(fx_total),+ "contributions": contributions, "base_currency": base,+ "method": "base_nav_weight_times_compound_local_and_fx_return"}+ continue+ result = 0.0+ contributions = {}+ for item in clean:+ shock = None+ for bucket in ("symbol", "sector", "currency"):+ nested = spec.get(bucket)+ if isinstance(nested, Mapping) and item[bucket] in nested:+ shock = _finite(nested[item[bucket]], f"scenario {name} {bucket}")+ break+ if shock is None:+ for key in (item["symbol"], item["sector"], item["currency"]):+ if key in spec:+ shock = _finite(spec[key], f"scenario {name} shock")+ break+ shock = shock if shock is not None else 0.0+ contribution = item["weight"] * shock+ contributions[item["symbol"]] = _rounded(contribution)+ result += contribution+ scenario_results[str(name)] = {"portfolio_return": _rounded(result), "contributions": contributions}++ sorted_positions = sorted(clean, key=lambda item: item["weight"], reverse=True)+ return {+ "contract_version": 1,+ "analysis_mode": mode,+ "observations": count,+ "performance": _performance(daily) if daily else None,+ "tail_risk": {+ "method": "historical_daily",+ "confidence": confidence,+ "var": _rounded(var),+ "cvar": _rounded(statistics.fmean(tail) if tail else var),+ "caveat": "Historical VaR/CVaR are sample statistics, not maximum-loss estimates.",+ } if daily else None,+ "concentration": {+ "hhi": _rounded(sum(item["weight"] ** 2 for item in clean)),+ "largest_position": {"symbol": sorted_positions[0]["symbol"], "weight": _rounded(sorted_positions[0]["weight"])},+ "top_5_weight": _rounded(sum(item["weight"] for item in sorted_positions[:5])),+ },+ "exposure": {"sector": exposure("sector"), "currency": exposure("currency")},+ "scenarios": scenario_results,+ "limitations": ["Returns must be pre-aligned by the caller." if daily else "No historical risk statistics are estimated in scenario mode.",+ "Unspecified scenario shocks are zero assumptions, not forecasts.",+ "Derivatives, leverage, liquidity, taxes, and nonlinear payoffs are not inferred."],+ }+++def _order_quote(raw: Mapping[str, Any], instrument: Mapping[str, str]) -> dict[str, Any]:+ """Adapt the shipped quote envelope or a flat quote without inventing facts."""+ if "quote" in raw:+ if raw.get("contract_version") != 1 or not all(+ isinstance(raw.get(key), Mapping) for key in ("quote", "provenance", "quality", "instrument")+ ):+ raise StockError("unsupported quote contract")+ quote = dict(raw["quote"])+ quote["observed_at"] = raw["provenance"].get("observed_at")+ quote["market_session"] = raw["quality"].get("market_session")+ quote["symbol"] = raw["instrument"].get("symbol")+ quote["currency"] = raw["instrument"].get("currency")+ else:+ quote = dict(raw)+ if quote.get("symbol") and normalize_symbol(str(quote["symbol"]))["symbol"] != instrument["symbol"]:+ raise StockError("quote symbol does not match order")+ if quote.get("currency") and quote["currency"] != instrument["currency"]:+ raise StockError("quote currency does not match order")+ # Retrieval time never refreshes a stale observation. Accept equivalent+ # timezone representations but reject conflicting provider observations.+ if quote.get("as_of") is not None and quote.get("observed_at") is not None:+ if _parse_timestamp(quote["as_of"], "quote.as_of") != _parse_timestamp(quote["observed_at"], "quote.observed_at"):+ raise StockError("conflicting quote timestamps")+ elif quote.get("as_of") is None:+ quote["as_of"] = quote.get("observed_at")+ return quote+++def validate_order_payload(payload: Any, *, as_of: str | None) -> dict[str, Any]:+ if not isinstance(payload, Mapping):+ raise StockError("order input must be an object")+ order = payload.get("order") or {}+ quote = payload.get("quote") or {}+ account = payload.get("account") or {}+ policy = payload.get("policy") or {}+ if not all(isinstance(item, Mapping) for item in (order, quote, account, policy)):+ raise StockError("order, quote, account, and policy must be objects")+ instrument = normalize_symbol(str(order.get("symbol") or ""), str(order.get("market") or "auto"))+ quote = _order_quote(quote, instrument)+ unknown_policy = set(policy) - {"allow_live", "stale_after_seconds", "estimated_fees_bps", "max_order_value", "max_position_weight"}+ if unknown_policy:+ raise StockError("unsupported policy fields: " + ", ".join(sorted(unknown_policy)))+ side = str(order.get("side") or "").upper()+ order_type = str(order.get("order_type") or "MARKET").upper()+ mode = str(order.get("mode") or "paper").lower()+ violations, warnings = [], []+ if side not in {"BUY", "SELL"}:+ violations.append("side_must_be_buy_or_sell")+ if order_type not in {"MARKET", "LIMIT"}:+ violations.append("unsupported_order_type")+ try:+ quantity_number = _finite(order.get("quantity"), "quantity")+ quantity = int(quantity_number)+ if quantity_number != quantity or quantity <= 0:+ violations.append("quantity_must_be_positive_integer")+ except StockError:+ quantity = 0+ violations.append("quantity_must_be_positive_integer")+ last = _finite(quote.get("last"), "quote.last", optional=True)+ limit_price = _finite(order.get("limit_price"), "limit_price", optional=True)+ if order_type == "LIMIT" and (limit_price is None or limit_price <= 0):+ violations.append("positive_limit_price_required")+ price = limit_price if order_type == "LIMIT" else last+ if price is None or price <= 0:+ violations.append("usable_order_price_required")++ now = _parse_timestamp(as_of, "as_of") if as_of else dt.datetime.now(dt.timezone.utc)+ try:+ quote_time = _parse_timestamp(quote.get("as_of"), "quote.as_of")+ age_seconds = (now.astimezone(dt.timezone.utc) - quote_time.astimezone(dt.timezone.utc)).total_seconds()+ if age_seconds < 0:+ violations.append("quote_timestamp_is_in_the_future")+ stale_after = _finite(policy.get("stale_after_seconds", 120), "stale_after_seconds")+ if age_seconds > stale_after:+ violations.append("stale_quote")+ except StockError as exc:+ quote_time, age_seconds = None, None+ violations.append(str(exc).replace(" ", "_"))++ instrument_data = payload.get("instrument") or {}+ lot_size = order.get("lot_size", instrument_data.get("lot_size") if isinstance(instrument_data, Mapping) else None)+ if instrument["market"] == "A" and side == "BUY":+ lot_size = int(_finite(lot_size if lot_size is not None else 100, "lot_size"))+ elif instrument["market"] == "HK" and side == "BUY" and lot_size is None:+ violations.append("hong_kong_buy_requires_current_instrument_lot_size")+ else:+ lot_size = int(_finite(lot_size if lot_size is not None else 1, "lot_size"))+ if lot_size and quantity and quantity % lot_size:+ violations.append("quantity_not_multiple_of_lot_size")++ positions = account.get("positions") or []+ if isinstance(positions, Mapping):+ positions = [{"symbol": key, **(value if isinstance(value, Mapping) else {"quantity": value})} for key, value in positions.items()]+ if not isinstance(positions, list):+ raise StockError("account.positions must be an array or object")+ held = sellable = current_value = 0.0+ sellable_known = False+ for position in positions:+ if not isinstance(position, Mapping) or not position.get("symbol"):+ continue+ try:+ matches = normalize_symbol(str(position["symbol"]))["symbol"] == instrument["symbol"]+ except StockError:+ matches = False+ if matches:+ held = _finite(position.get("quantity", 0), "position.quantity")+ current_value = _finite(position.get("market_value", 0), "position.market_value")+ if position.get("sellable_quantity") is not None:+ sellable = _finite(position.get("sellable_quantity"), "position.sellable_quantity")+ sellable_known = True+ elif instrument["market"] != "A":+ sellable, sellable_known = held, True+ break+ if side == "SELL":+ if instrument["market"] == "A" and not sellable_known:+ violations.append("a_share_sell_requires_sellable_quantity_for_t_plus_1")+ elif quantity > sellable:+ violations.append("insufficient_sellable_quantity")++ fees_bps = _finite(policy.get("estimated_fees_bps", 0), "estimated_fees_bps")+ order_value = quantity * price if quantity and price else None+ estimated_cash = order_value * (1 + fees_bps / 10_000) if order_value is not None else None+ cash = _finite(account.get("cash"), "account.cash", optional=True)+ if side == "BUY" and estimated_cash is not None:+ if cash is None:+ violations.append("cash_balance_required_for_buy")+ elif estimated_cash > cash:+ violations.append("insufficient_cash")+ max_order_value = _finite(policy.get("max_order_value"), "max_order_value", optional=True)+ if max_order_value is not None and order_value is not None and order_value > max_order_value:+ violations.append("max_order_value_exceeded")+ nav = _finite(account.get("net_asset_value"), "net_asset_value", optional=True)+ max_position_weight = _finite(policy.get("max_position_weight"), "max_position_weight", optional=True)+ projected_weight = None+ if max_position_weight is not None:+ if nav is None or nav <= 0 or order_value is None:+ violations.append("net_asset_value_required_for_position_limit")+ else:+ projected_value = current_value + order_value if side == "BUY" else max(0, current_value - order_value)+ projected_weight = projected_value / nav+ if projected_weight > max_position_weight:+ violations.append("max_position_weight_exceeded")++ if mode not in {"paper", "live"}:+ violations.append("mode_must_be_paper_or_live")+ if mode == "live":+ if policy.get("allow_live") is not True:+ violations.append("policy_does_not_allow_live_orders")+ if payload.get("explicit_user_authorization") is not True:+ violations.append("exact_live_order_not_explicitly_authorized")+ if not order.get("time_in_force"):+ warnings.append("time_in_force_not_declared")+ if not quote.get("market_session"):+ warnings.append("market_session_not_verified")++ return {+ "contract_version": 1,+ "allowed": not violations,+ "violations": sorted(set(violations)),+ "warnings": sorted(set(warnings)),+ "normalized_order": {+ "symbol": instrument["symbol"], "market": instrument["market"],+ "currency": instrument["currency"], "side": side, "quantity": quantity,+ "order_type": order_type, "limit_price": _rounded(limit_price),+ "time_in_force": order.get("time_in_force"), "mode": mode, "lot_size": lot_size,+ },+ "evidence": {+ "quote_as_of": quote_time.isoformat() if quote_time else None,+ "quote_age_seconds": _rounded(age_seconds, 2),+ "last_price": _rounded(last),+ },+ "estimates": {+ "order_value": _rounded(order_value, 2),+ "cash_required_with_estimated_fees": _rounded(estimated_cash, 2),+ "projected_position_weight": _rounded(projected_weight),+ },+ "execution": "validation_only_no_order_was_submitted",+ }+++def _markdown_cell(value: Any) -> str:+ if isinstance(value, (dict, list)):+ text = json.dumps(value, ensure_ascii=False, sort_keys=True)+ elif value is None:+ text = "—"+ else:+ text = str(value)+ return text.replace("|", "\\|").replace("\n", "<br>")+++def evidence_record(filename: str) -> dict[str, Any]:+ """Bind report provenance to source bytes rather than a model's transcription.++ Keep declared structured metadata verbatim. No free-text interpretation or+ replacement of observation dates with file/retrieval time occurs here.+ """+ source = Path(filename)+ if not source.is_file():+ raise StockError("evidence file not found")+ raw = source.read_bytes()+ try:+ payload = {} if source.suffix.lower() == ".csv" else json.loads(raw)+ except (json.JSONDecodeError, UnicodeDecodeError) as exc:+ raise StockError("evidence must be valid JSON or CSV") from exc+ fields = ("symbol", "order_id", "mode", "source", "timestamp", "observed_at", "retrieved_at",+ "as_of", "currency", "base_currency", "meta", "instrument", "provenance", "sample", "input_selection")+ def metadata(value: Any) -> dict[str, Any]:+ if not isinstance(value, Mapping):+ return {}+ result = {key: value[key] for key in fields if key in value}+ for key in ("quote", "order", "data"):+ if isinstance(value.get(key), Mapping):+ result[key] = metadata(value[key])+ if isinstance(value.get("securities"), list):+ result["securities"] = [metadata(item) for item in value["securities"]]+ return result+ return {"file": source.name, "sha256": hashlib.sha256(raw).hexdigest(),+ "declared_metadata": metadata(payload)}+++def render_report(payload: Any, *, sources: Sequence[Mapping[str, Any]] = ()) -> str:+ if not isinstance(payload, Mapping):+ raise StockError("report dossier must be an object")+ lines = [f"# {_markdown_cell(payload.get('title') or 'Stock Analysis')}", ""]+ if sources:+ lines.extend(["## Source records", "", "These are file snapshots; record times below are not a claim of current state.", "",+ "| File | SHA-256 | Declared metadata |", "| --- | --- | --- |"])+ lines.extend(f"| {_markdown_cell(item['file'])} | {_markdown_cell(item['sha256'])} | {_markdown_cell(item['declared_metadata'])} |" for item in sources)+ lines.append("")+ for name in ("scope", "evidence", "summary"):+ value = payload.get(name)+ if value is None:+ continue+ lines.extend([f"## {name.replace('_', ' ').title()}", ""])+ if isinstance(value, Mapping):+ lines.extend(["| Field | Value |", "| --- | --- |"])+ lines.extend(f"| {_markdown_cell(key)} | {_markdown_cell(item)} |" for key, item in value.items())+ elif isinstance(value, list):+ lines.extend(f"- {_markdown_cell(item)}" for item in value)+ else:+ lines.append(_markdown_cell(value))+ lines.append("")+ metrics = payload.get("metrics")+ if isinstance(metrics, Mapping):+ lines.extend(["## Metrics", "", "| Metric | Value |", "| --- | --- |"])+ lines.extend(f"| {_markdown_cell(key)} | {_markdown_cell(value)} |" for key, value in metrics.items())+ lines.append("")+ elif isinstance(metrics, list) and all(isinstance(row, Mapping) for row in metrics):+ columns = list(dict.fromkeys(key for row in metrics for key in row))+ if columns:+ lines.extend(["## Metrics", "", "| " + " | ".join(_markdown_cell(key) for key in columns) + " |",+ "| " + " | ".join("---" for _ in columns) + " |"])+ lines.extend("| " + " | ".join(_markdown_cell(row.get(key)) for key in columns) + " |" for row in metrics)+ lines.append("")+ elif metrics is not None:+ raise StockError("report metrics must be an object or an array of objects")+ for name in ("scenarios", "risks", "limitations", "next_steps"):+ value = payload.get(name)+ if value is None:+ continue+ lines.extend([f"## {name.replace('_', ' ').title()}", ""])+ if isinstance(value, Mapping):+ lines.extend(f"- **{_markdown_cell(key)}:** {_markdown_cell(item)}" for key, item in value.items())+ elif isinstance(value, list):+ lines.extend(f"- {_markdown_cell(item)}" for item in value)+ else:+ lines.append(_markdown_cell(value))+ lines.append("")+ lines.extend(["---", "Generated from the supplied StockAnalyser dossier; absent fields were not inferred.", ""])+ return "\n".join(lines)+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(description=__doc__)+ commands = parser.add_subparsers(dest="command", required=True)+ normalize = commands.add_parser("normalize-symbol")+ normalize.add_argument("--symbol", required=True)+ normalize.add_argument("--market", default="auto")++ analyze = commands.add_parser("analyze")+ analyze.add_argument("--input", required=True)+ analyze.add_argument("--symbol")+ analyze.add_argument("--source")+ analyze.add_argument("--as-of")+ analyze.add_argument("--cutoff")+ analyze.add_argument("--output")++ screen = commands.add_parser("screen")+ screen.add_argument("--input", required=True)+ screen.add_argument("--as-of")+ screen.add_argument("--cutoff")+ screen.add_argument("--output")++ backtest = commands.add_parser("backtest")+ backtest.add_argument("--input", required=True)+ backtest.add_argument("--strategy", choices=["sma_cross", "momentum", "rsi_reversion"], required=True)+ backtest.add_argument("--fast", type=int, default=20)+ backtest.add_argument("--slow", type=int, default=60)+ backtest.add_argument("--lookback", type=int, default=60)+ backtest.add_argument("--threshold", type=float, default=0.0)+ backtest.add_argument("--rsi-entry", type=float, default=30.0)+ backtest.add_argument("--rsi-exit", type=float, default=55.0)+ backtest.add_argument("--cost-bps", type=float, default=0.0)+ backtest.add_argument("--slippage-bps", type=float, default=0.0)+ backtest.add_argument("--as-of")+ backtest.add_argument("--cutoff")+ backtest.add_argument("--include-series", action="store_true")+ backtest.add_argument("--output")++ portfolio = commands.add_parser("portfolio-risk")+ portfolio.add_argument("--input", required=True)+ portfolio.add_argument("--confidence", type=float, default=0.95)+ portfolio.add_argument("--mode", choices=["historical", "scenario"], default="historical")+ portfolio.add_argument("--output")++ order = commands.add_parser("validate-order")+ order.add_argument("--input", required=True)+ order.add_argument("--as-of")+ order.add_argument("--output")++ report = commands.add_parser("render-report")+ report.add_argument("--input", required=True)+ report.add_argument("--output")+ report.add_argument("--evidence-input", action="append", default=[])+ return parser+++def main(argv: Sequence[str] | None = None) -> int:+ args = build_parser().parse_args(argv)+ try:+ input_path = getattr(args, "input", None)+ evidence_paths = getattr(args, "evidence_input", [])+ output = getattr(args, "output", None)+ # Protect every analysis input, including symlink/hardlink aliases, at+ # the shared write boundary. Existing separate output files still work.+ if output:+ target = Path(output)+ for filename in ([input_path] if input_path else []) + evidence_paths:+ source = Path(filename)+ if target.resolve() == source.resolve() or (target.exists() and source.exists() and target.samefile(source)):+ raise StockError("output must not overwrite an input or evidence file")+ payload = _read(input_path) if input_path else None+ selection = None+ cutoff = getattr(args, "cutoff", None)+ if cutoff:+ if args.as_of and _parse_date(args.as_of) != _parse_date(cutoff):+ raise StockError("cutoff and as-of must agree when both are provided")+ payload, selection = select_history_cutoff(payload, cutoff)+ args.as_of = _parse_date(cutoff).isoformat()+ if args.command == "normalize-symbol":+ result = normalize_symbol(args.symbol, args.market)+ elif args.command == "analyze":+ result = analyze_payload(payload, symbol=args.symbol, source=args.source, as_of=args.as_of)+ elif args.command == "screen":+ result = screen_payload(payload, as_of=args.as_of)+ elif args.command == "backtest":+ if args.cost_bps < 0 or args.slippage_bps < 0:+ raise StockError("cost and slippage must be non-negative")+ result = backtest_payload(payload, args)+ elif args.command == "portfolio-risk":+ result = portfolio_payload(payload, args.confidence, mode=args.mode)+ elif args.command == "validate-order":+ result = validate_order_payload(payload, as_of=args.as_of)+ else:+ rendered = render_report(payload, sources=[evidence_record(filename) for filename in evidence_paths])+ if args.output:+ target = Path(args.output)+ if not target.parent.is_dir():+ raise StockError(f"output directory does not exist: {target.parent}")+ target.write_text(rendered, encoding="utf-8")+ else:+ sys.stdout.write(rendered)+ return 0+ if selection is not None:+ result["input_selection"] = {"method": "price_bars_on_or_before_cutoff", "histories": selection,+ "limitation": "Price selection does not establish point-in-time availability of fundamentals or universe membership."}+ _emit(result, output)+ return 0+ except StockError as exc:+ sys.stderr.write(json.dumps({"ok": False, "error": str(exc)}, ensure_ascii=False) + "\n")+ return 2+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/portfolio-risk/SKILL.md b/resources/builtin/marketplace/agents/1040b336306f/skills/portfolio-risk/SKILL.mdnew file mode 100644index 00000000..6b26a19c--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/portfolio-risk/SKILL.md@@ -0,0 +1,53 @@+---+ownerAgent: 1040b336306f+name: portfolio-risk+description_zh: 评估持仓权重、集中度、波动、回撤、历史 VaR/CVaR、行业与币种暴露及情景冲击,并把风险来源转化为可审查的约束与再平衡建议。+description_en: Assess weights, concentration, volatility, drawdown, historical VaR/CVaR, sector and currency exposure, and scenario shocks, then turn risk sources into reviewable constraints and rebalance options.+---++# Portfolio Risk++Read with `market-data` for a portfolio review, stress test, or rebalance+question. Keep account values and holdings private and use only what is needed.++Choose the calculation from the supplied data: `--mode historical` (default)+requires aligned daily decimal returns; `--mode scenario` uses weights and+declared shocks, returns no historical statistics, and needs no return series.+Do not manufacture a series to make a scenario request fit historical mode.++Historical input uses weights that sum to one and aligned daily decimal returns:++```json+{"positions":[{"symbol":"700.HK","weight":0.4,"currency":"HKD","sector":"Technology","returns":[0.01,-0.02]}],"scenarios":{"tech_down":{"Technology":-0.15}}}+```++Run:++```bash+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data stock -- portfolio-risk --input portfolio.json --confidence 0.95+```++The core calculates portfolio return series from aligned observations, annual+volatility, max drawdown, historical VaR/CVaR, HHI and top-name concentration,+sector/currency exposure, and declared linear scenario losses. VaR is a sample+statistic, not a loss ceiling.++For weight-only concentration or declared stress, use the same command with+`--mode scenario`. For simultaneous local-price and FX shocks, supply weights+already measured in `base_currency` NAV and a compound scenario:++```json+{"base_currency":"USD","positions":[{"symbol":"700.HK","weight":1,"currency":"HKD"}],"scenarios":{"stress":{"local":{"700.HK":-0.1},"fx":{"HKD":-0.05}}}}+```++The core multiplies local and FX returns and also reports their separate+contributions. Unspecified shocks are zero assumptions; the base currency+cannot move against itself. Prepare this structured input from supplied facts+without changing the source or inventing currency conversions. If the weight+basis is unknown, resolve it before a cross-currency calculation.++Review missing positions, stale prices, derivatives, leverage, FX translation,+liquidity, tax lots, and correlation regime change before recommending action.+Frame rebalance ideas as constraint-aware options with trade-offs. Do not+silently normalize invalid weights, fabricate correlations, or assume an+investor's suitability, tax status, or loss capacity.diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/quant-lab/SKILL.md b/resources/builtin/marketplace/agents/1040b336306f/skills/quant-lab/SKILL.mdnew file mode 100644index 00000000..275976f9--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/quant-lab/SKILL.md@@ -0,0 +1,48 @@+---+ownerAgent: 1040b336306f+name: quant-lab+description_zh: 把投资想法变成可复现的策略实验,执行带基准、手续费、滑点、信号延迟和样本边界的回测,并审查未来函数、过拟合与稳健性。+description_en: Turn an investment idea into a reproducible strategy experiment with benchmark, fees, slippage, signal delay, sample boundaries, and explicit leakage, overfitting, and robustness review.+---++# Quant Lab++Read with `market-data` for strategy design, backtesting, or experiment review.++## Experiment card++Before calculation, state universe, data/adjustment, signal, rebalance timing,+execution price assumption, sizing, cash treatment, costs, benchmark, sample,+and invalidation rule. A close-derived signal may affect only the next return.++The compact core supports `sma_cross`, `momentum`, and `rsi_reversion` as+transparent baselines:++```bash+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data stock -- backtest --input history.json --strategy sma_cross --fast 20 --slow 60 --cost-bps 8 --slippage-bps 5 --include-series --output result.json+```++Use the baselines to test a claim, not to stretch the engine into an unsupported+strategy. For a custom strategy, produce a precise experiment specification and+use a user-authorized project framework when available.++## Review gate++- Confirm point-in-time membership and fundamentals; disclose survivorship or+ restatement exposure when they cannot be reconstructed.+- Include transaction costs and a relevant passive benchmark. State whether+ dividends, borrow, market impact, taxes, and FX are absent.+- Prefer chronological train/validation/test or walk-forward evidence. Keep the+ final test untouched while tuning.+- Inspect sensitivity to neighboring parameters, later entry, higher cost, and+ subperiods. A fragile result is a finding, not something to optimize away.+- Report CAGR/return, volatility, Sharpe convention, max drawdown, turnover,+ exposure, trade count, and the exact signal-delay guard.++Never describe a backtest as expected future performance. Preserve the core's+result JSON; put supplementary interpretation in a separate dossier or file.+`--include-series` supplies signal date, applied position, turnover and gross,+cost and net returns from the same calculation as the summary. Use that ledger+for trade explanations rather than rebuilding it with another timing model.+`--cutoff YYYY-MM-DD` selects a requested historical price window in memory;+the result records exclusions without changing the input.diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/security-research/SKILL.md b/resources/builtin/marketplace/agents/1040b336306f/skills/security-research/SKILL.mdnew file mode 100644index 00000000..106df61b--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/security-research/SKILL.md@@ -0,0 +1,49 @@+---+ownerAgent: 1040b336306f+name: security-research+description_zh: 对 A 股、港股、美股做可追溯的个股研究与横截面筛选,整合价格技术面、基本面、估值、事件、催化剂和反证,不把缺失数据包装成结论。+description_en: Perform traceable A/HK/US security research and cross-sectional screening across technicals, fundamentals, valuation, events, catalysts, and contrary evidence without masking missing data.+---++# Security Research++Read with `market-data` for a company thesis, comparison, factor screen, or+watchlist. The goal is a decision-ready evidence map, not a forced rating.++## Research contract++- Lock security, listing, market, horizon, currency, and comparison set.+- Prefer filings, exchange notices, and issuer releases for current business+ facts. Distinguish filing period from retrieval date.+- Separate business quality, growth, balance sheet, cash generation, valuation,+ price/volume behavior, ownership/liquidity, and event risk.+- Reconcile conflicts rather than averaging them away. Missing a field lowers+ coverage; it is not neutral or zero.+- End with base/bull/bear cases, catalysts, invalidation conditions, and the+ next evidence that would change the view.++Run deterministic time-series and supplied-fundamental analysis:++```bash+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data stock -- analyze --input history.json --symbol 600519.SH --source user-file+```++Run a cross-sectional screen:++```bash+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data stock -- screen --input universe.json+```++For a requested historical price cutoff, use `--cutoff YYYY-MM-DD` on `analyze`+or `screen`. The core selects bars in memory, preserves the source file and+reports excluded-bar counts. `--as-of` alone remains strict validation and+rejects later bars. Neither option proves point-in-time availability of+fundamentals or universe membership; keep those evidence gaps explicit.++The screen uses only covered factors, reports eligibility and coverage, and+produces a relative research priority—not a buy/sell instruction. Explain each+rank through its factor components and name survivorship, point-in-time, FX,+and sector-comparability limits when applicable.++For event/news questions, retrieve fresh evidence; do not infer today's event+state from historical bars. Rumors remain labeled and never override filings.diff --git a/resources/builtin/marketplace/agents/1040b336306f/skills/trade-control/SKILL.md b/resources/builtin/marketplace/agents/1040b336306f/skills/trade-control/SKILL.mdnew file mode 100644index 00000000..7f843f50--- /dev/null+++ b/resources/builtin/marketplace/agents/1040b336306f/skills/trade-control/SKILL.md@@ -0,0 +1,45 @@+---+ownerAgent: 1040b336306f+name: trade-control+description_zh: 生成并校验 A 股、港股、美股订单草稿,覆盖行情时效、现金与持仓、手数、T+1、限额、集中度和 live 授权;默认分析或模拟,不在校验脚本中下单。+description_en: Build and validate A/HK/US order drafts for quote freshness, cash, holdings, lot size, T+1, limits, concentration, and live authorization; default to analysis or paper mode and never submit from the validator.+---++# Trade Control++Read with `market-data` whenever an order, paper trade, broker account, or live+execution is mentioned. Analysis permission is not trading permission.++## Draft and validate++Prepare JSON with `order`, `quote`, `account`, and `policy`. Quote timestamps+must be ISO-8601 with timezone. `mode` defaults to `paper`; a live draft also+needs `explicit_user_authorization: true` and `policy.allow_live: true`.++```bash+"$ORKAS_NODE" "$ORKAS_PC_DIR/bin/run-skill.cjs" market-data stock -- validate-order --input order.json+```++The deterministic validator checks positive integer quantity, order price,+quote staleness, buying power or sellable shares, configured order/position+limits, A-share buy lots and T+1 sellable quantity, and explicit live mode. Hong+Kong lot size is security-specific and must come from current instrument data;+absence blocks a buy rather than assuming 100.++An `allowed` result means only that the declared checks passed. Reconfirm the+symbol/listing, side, quantity, order type, limit, time-in-force, market session,+fees, currency, mode, and connector account in the visible order summary.++## Execution boundary++- Default to an order draft or the broker's paper environment.+- Submit live only after the user explicitly authorizes that exact visible+ order in the current task and the connected broker exposes the permission.+- Never put credentials in chat, files, commands, or reports.+- Never bypass a violation, convert an order type, split an order, or retry a+ rejected live order without new authorization.+- After any submission, read back broker order ID, state, filled/remaining+ quantity, average price, timestamp, and rejection reason. Report uncertainty+ instead of claiming success when readback fails.++The Skill does not implement unattended/autonomous live trading.diff --git a/resources/builtin/system/skills/_system.json b/resources/builtin/system/skills/_system.jsonindex 1e3c0fae..1374017a 100644--- a/resources/builtin/system/skills/_system.json+++ b/resources/builtin/system/skills/_system.json@@ -13,6 +13,6 @@},{"id": "web-app-sdk",- "update_at": 1789511205555+ "update_at": 1789990251573}]diff --git a/resources/builtin/system/skills/web-app-sdk/SKILL.md b/resources/builtin/system/skills/web-app-sdk/SKILL.mdindex 593903b8..18afe79f 100644--- a/resources/builtin/system/skills/web-app-sdk/SKILL.md+++ b/resources/builtin/system/skills/web-app-sdk/SKILL.md@@ -26,7 +26,8 @@ eligible existing executors; never invent SDK methods from host tool names.1. Put `orkas-app.json` at the app bundle root. Set `sdkVersion: 1` and declareonly needed families from `storage`, `appFiles`, `files`, `ai`, `library`, `connectors`.2. Load `<script src="/__orkas/sdk.js"></script>` before app code. Use- `window.orkasApp`. Bundle every other script, style and asset locally.+ `window.orkasApp`. Local bundles and external HTTP(S) scripts, styles and assets+ are supported in running apps and previews.3. Call `capabilities.list()` to check support, declaration, availability andauthorization separately. Use `capabilities.describe({method})` for thecurrent schema. Never treat a declaration or discovery result as consent.@@ -69,8 +70,16 @@ eligible existing executors; never invent SDK methods from host tool names.inherit it. External writes and fees are possible; inspect business errors.Connector installation is not exposed.+Use browser `fetch`/XHR, EventSource or WebSocket for external APIs; no SDK network+method or manifest capability is needed. Browser CORS, mixed-content and endpoint+authentication still apply. External scripts run with the app's declared SDK+abilities. Handle failures and cancellation without replaying uncertain writes.+Automated `html_preview` allows Web+resources but isolates storage and disables account services; verify business+actions in the running app.+Read `unsupported` in the reference for excluded families and reasons. Shell,-arbitrary network/CDNs, task/Agent execution, Skill execution, project/history/+task/Agent execution, Skill execution, project/history/memory access, media generation and editing selected original files have no v1 adapter.## Failure and lifecycle@@ -80,17 +89,16 @@ must leave the UI usable for an explicit retry. Never automatically replay AI ortool calls after uncertain failure: spending or a remote write may already haveoccurred. Aborting does not undo a completed external action.-Close, navigation, account switch, source change and `permissions.revoke()`+Close, navigation away from the app, account switch, source change and `permissions.revoke()`invalidate the instance. Reopen for fresh grants. Do not persist file handles.When the host/model is unavailable, explain how to reopen in Orkas or configurea model; do not substitute fabricated results or direct provider requests.-Keep requests bounded: four concurrent calls, two host-wide model/tool calls,-1 MiB request/result/store, 256 storage keys per scope, sandbox files up to-512 KiB each (256 files / 16 MiB per scope), 16 selected files up to 512 KiB,-32,000-character AI prompts and at most 4096 output tokens. Large tool results-fail without automatic replay. A missing host times out after 10 seconds;-acknowledged operations have a five-minute host deadline.+File and JSON-store byte limits follow PC's 200 MiB ceiling. The SDK adds no+quotas on file count, aggregate file bytes, storage-key count, instance count,+cumulative requests or concurrent calls. Owning services retain their limits; AI output defaults to the configured model. Tool results are application data and do not use+model-context token budgets. Initial host discovery times out after 10 seconds;+after connecting, operations use the owning service's timeout and cancellation.## Language and interface qualitydiff --git a/resources/builtin/system/skills/web-app-sdk/assets/starter/index.html b/resources/builtin/system/skills/web-app-sdk/assets/starter/index.htmlindex a4dd9ca8..07e8cd5d 100644--- a/resources/builtin/system/skills/web-app-sdk/assets/starter/index.html+++ b/resources/builtin/system/skills/web-app-sdk/assets/starter/index.html@@ -15,7 +15,7 @@<body><h1 data-label="title">Text notebook</h1><label data-label="draft" for="draft">Text to summarize</label>- <textarea id="draft" maxlength="30000"></textarea>+ <textarea id="draft"></textarea><div><button id="open" disabled>Open text file</button><button id="save" disabled>Save draft</button>@@ -87,7 +87,6 @@"Configure a model in Orkas, then reopen this app.": "请在 Orkas 中配置模型,然后重新打开应用。","Action failed. Check the input or choose a new export filename before trying again.": "操作失败。请检查输入或选择新的导出文件名后重试。","File selection cancelled.": "已取消文件选择。",- "Choose text with at most 30,000 characters.": "请选择不超过 30,000 个字符的文本。","File loaded. The original is unchanged.": "文件已加载,原文件未修改。","Draft saved.": "草稿已保存。","Enter some text first.": "请先输入文本。",@@ -108,7 +107,6 @@"Configure a model in Orkas, then reopen this app.": "Orkas でモデルを設定してからアプリを開き直してください。","Action failed. Check the input or choose a new export filename before trying again.": "操作に失敗しました。入力を確認するか、新しい出力ファイル名で再試行してください。","File selection cancelled.": "ファイル選択をキャンセルしました。",- "Choose text with at most 30,000 characters.": "30,000 文字以内のテキストを選んでください。","File loaded. The original is unchanged.": "ファイルを読み込みました。元ファイルは変更していません。","Draft saved.": "下書きを保存しました。","Enter some text first.": "先にテキストを入力してください。",@@ -129,7 +127,6 @@"Configure a model in Orkas, then reopen this app.": "Configure um modelo no Orkas e reabra este app.","Action failed. Check the input or choose a new export filename before trying again.": "Falha na ação. Confira a entrada ou escolha um novo nome de exportação e tente novamente.","File selection cancelled.": "Seleção de arquivo cancelada.",- "Choose text with at most 30,000 characters.": "Escolha um texto com até 30.000 caracteres.","File loaded. The original is unchanged.": "Arquivo carregado. O original não foi alterado.","Draft saved.": "Rascunho salvo.","Enter some text first.": "Digite algum texto primeiro.",@@ -188,7 +185,6 @@if (!file) { status.textContent = t("File selection cancelled."); return; }try {const {text} = await api.files.readText({handle:file.handle});- if (text.length > 30000) { status.textContent = t("Choose text with at most 30,000 characters."); return; }draft.value = text; status.textContent = t("File loaded. The original is unchanged.");} finally { await api.files.release({handle:file.handle}); }});diff --git a/resources/builtin/system/skills/web-app-sdk/references/api.json b/resources/builtin/system/skills/web-app-sdk/references/api.jsonindex e53df269..ce6e8a1b 100644--- a/resources/builtin/system/skills/web-app-sdk/references/api.json+++ b/resources/builtin/system/skills/web-app-sdk/references/api.json@@ -23,8 +23,7 @@"properties": {"method": {"type": "string",- "minLength": 1,- "maxLength": 128+ "minLength": 1}},"required": [@@ -73,8 +72,7 @@"properties": {"key": {"type": "string",- "minLength": 1,- "maxLength": 128+ "minLength": 1},"scope": {"type": "string",@@ -99,14 +97,13 @@{"name": "storage.set","capability": "storage",- "description": "Atomically save an app-private JSON value; total store limit 1 MiB.",+ "description": "Atomically save an app-private JSON value; total store limit 200 MiB.","inputSchema": {"type": "object","properties": {"key": {"type": "string",- "minLength": 1,- "maxLength": 128+ "minLength": 1},"scope": {"type": "string",@@ -140,8 +137,7 @@"properties": {"key": {"type": "string",- "minLength": 1,- "maxLength": 128+ "minLength": 1},"scope": {"type": "string",@@ -166,7 +162,7 @@{"name": "storage.keys","capability": "storage",- "description": "List up to 256 keys in the selected app store.",+ "description": "List keys in the selected app store.","inputSchema": {"type": "object","properties": {@@ -188,7 +184,7 @@{"name": "appFiles.list","capability": "appFiles",- "description": "List sandbox files recursively. cloud (default) syncs with the account; local is for caches. At most 256 files and 16 MiB per scope.",+ "description": "List sandbox files recursively. cloud (default) syncs with the account; local is for caches.","inputSchema": {"type": "object","properties": {@@ -212,15 +208,14 @@{"name": "appFiles.readText","capability": "appFiles",- "description": "Read a sandbox UTF-8 file up to 512 KiB; missing files return null.",+ "description": "Read a sandbox UTF-8 file up to 200 MiB; missing files return null.","inputSchema": {"type": "object","properties": {"path": {"type": "string","minLength": 1,- "maxLength": 240,- "description": "Relative path, at most 8 segments. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."+ "description": "Relative path. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."},"scope": {"type": "string",@@ -245,15 +240,14 @@{"name": "appFiles.readBase64","capability": "appFiles",- "description": "Read a sandbox binary file up to 512 KiB; missing files return null.",+ "description": "Read a sandbox binary file up to 200 MiB; missing files return null.","inputSchema": {"type": "object","properties": {"path": {"type": "string","minLength": 1,- "maxLength": 240,- "description": "Relative path, at most 8 segments. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."+ "description": "Relative path. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."},"scope": {"type": "string",@@ -278,15 +272,14 @@{"name": "appFiles.writeText","capability": "appFiles",- "description": "Atomically replace an app-owned UTF-8 file (512 KiB). Creates parent directories. Saved means local persistence, not completed upload.",+ "description": "Atomically replace an app-owned UTF-8 file (200 MiB). Creates parent directories. Saved means local persistence, not completed upload.","inputSchema": {"type": "object","properties": {"path": {"type": "string","minLength": 1,- "maxLength": 240,- "description": "Relative path, at most 8 segments. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."+ "description": "Relative path. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."},"scope": {"type": "string",@@ -298,7 +291,7 @@},"text": {"type": "string",- "maxLength": 524288+ "maxLength": 209715200}},"required": [@@ -317,15 +310,14 @@{"name": "appFiles.writeBase64","capability": "appFiles",- "description": "Atomically replace an app-owned binary file (512 KiB). Requires canonical base64.",+ "description": "Atomically replace an app-owned binary file (200 MiB). Requires canonical base64.","inputSchema": {"type": "object","properties": {"path": {"type": "string","minLength": 1,- "maxLength": 240,- "description": "Relative path, at most 8 segments. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."+ "description": "Relative path. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."},"scope": {"type": "string",@@ -337,7 +329,7 @@},"base64": {"type": "string",- "maxLength": 700000+ "maxLength": 279620268}},"required": [@@ -363,8 +355,7 @@"path": {"type": "string","minLength": 1,- "maxLength": 240,- "description": "Relative path, at most 8 segments. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."+ "description": "Relative path. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini."},"scope": {"type": "string",@@ -402,7 +393,7 @@{"name": "files.readText","capability": "files",- "description": "Read a selected UTF-8 file, at most 512 KiB.",+ "description": "Read a selected UTF-8 file, at most 200 MiB.","inputSchema": {"type": "object","properties": {@@ -425,7 +416,7 @@{"name": "files.readBase64","capability": "files",- "description": "Read a selected file as base64, at most 512 KiB.",+ "description": "Read a selected file as base64, at most 200 MiB.","inputSchema": {"type": "object","properties": {@@ -455,12 +446,11 @@"name": {"type": "string","minLength": 1,- "maxLength": 120,"pattern": "^[^/\\\\\\x00-\\x1f]+$"},"text": {"type": "string",- "maxLength": 524288+ "maxLength": 209715200}},"required": [@@ -508,14 +498,13 @@"properties": {"prompt": {"type": "string",- "minLength": 1,- "maxLength": 32000+ "minLength": 1},"maxTokens": {"type": "integer",- "minimum": 1,- "maximum": 4096,- "default": 1024+ "exclusiveMinimum": 0,+ "minimum": -9007199254740991,+ "maximum": 9007199254740991}},"required": [@@ -552,8 +541,7 @@"properties": {"name": {"type": "string",- "minLength": 1,- "maxLength": 128+ "minLength": 1},"arguments": {"type": "object",@@ -610,10 +598,6 @@{"capability": "media.generate","reason": "Deferred: owning studio plan, output and spending gates must be adapted first."- },- {- "capability": "network",- "reason": "Deferred: private app data must not reach arbitrary remote endpoints; bundled resources only."}]}diff --git a/resources/test/test_stock_analyser_core_unit.py b/resources/test/test_stock_analyser_core_unit.pynew file mode 100644index 00000000..68b54c28--- /dev/null+++ b/resources/test/test_stock_analyser_core_unit.py@@ -0,0 +1,634 @@+import argparse+import datetime as dt+import copy+import contextlib+import io+import importlib.util+import json+import tempfile+import unittest+from pathlib import Path+++SCRIPT = Path(__file__).resolve().parents[1] / "builtin/marketplace/agents/1040b336306f/skills/market-data/scripts/stock.py"+SPEC = importlib.util.spec_from_file_location("stock_analyser_core", SCRIPT)+assert SPEC and SPEC.loader+stock = importlib.util.module_from_spec(SPEC)+SPEC.loader.exec_module(stock)+++def bars(closes, start="2026-01-01"):+ first = dt.date.fromisoformat(start)+ output = []+ for index, close in enumerate(closes):+ date = first + dt.timedelta(days=index)+ output.append({+ "date": date.isoformat(),+ "open": close,+ "high": close * 1.01,+ "low": close * 0.99,+ "close": close,+ "volume": 1_000 + index,+ "amount": close * (1_000 + index),+ })+ return output+++def backtest_args(**overrides):+ values = {+ "strategy": "sma_cross",+ "fast": 2,+ "slow": 4,+ "lookback": 3,+ "threshold": 0.0,+ "rsi_entry": 30.0,+ "rsi_exit": 55.0,+ "cost_bps": 0.0,+ "slippage_bps": 0.0,+ "as_of": "2026-06-01",+ "include_series": False,+ }+ values.update(overrides)+ return argparse.Namespace(**values)+++class SymbolAndDataTests(unittest.TestCase):+ def test_normalizes_all_three_markets(self):+ self.assertEqual(stock.normalize_symbol("600519")["symbol"], "600519.SH")+ self.assertEqual(stock.normalize_symbol("00700", "HK")["symbol"], "700.HK")+ self.assertEqual(stock.normalize_symbol("MSFT")["symbol"], "MSFT.US")+ self.assertEqual(stock.normalize_symbol("430047")["symbol"], "430047.BJ")++ def test_rejects_market_suffix_conflict(self):+ with self.assertRaisesRegex(stock.StockError, "conflicts"):+ stock.normalize_symbol("700.HK", "US")++ def test_analysis_reports_provenance_quality_and_fundamental_coverage(self):+ history = bars([100 + index * 0.4 for index in range(90)])+ payload = {+ "bars": history,+ "meta": {"adjustment": "qfq"},+ "fundamentals": {"roe": 0.25, "net_margin": 0.18, "pe": 20, "pb": 4},+ }+ result = stock.analyze_payload(+ payload, symbol="600519", source="fixture", as_of=history[-1]["date"]+ )+ self.assertEqual(result["instrument"]["symbol"], "600519.SH")+ self.assertEqual(result["provenance"]["source"], "fixture")+ self.assertEqual(result["quality"]["flags"], [])+ self.assertIsNotNone(result["technical"]["return_60"])+ self.assertEqual(result["fundamental"]["coverage"], {"available": 4, "possible": 9})++ def test_stale_data_is_flagged_and_impossible_ohlc_is_rejected(self):+ history = bars([10, 11, 12])+ result = stock.normalize_bars(history, as_of="2026-02-01")+ self.assertIn("stale_daily_history", result["quality"]["flags"])+ broken = bars([10, 11])+ broken[1]["high"] = 9+ with self.assertRaisesRegex(stock.StockError, "high"):+ stock.normalize_bars(broken, as_of="2026-01-02")++ def test_analyze_universe_selects_canonical_symbol_without_losing_evidence(self):+ universe = {"securities": [+ {"symbol": "600519.SH", "bars": bars([900, 901])},+ {"symbol": "00700.HK", "source": "dated-export",+ "meta": {"adjustment": "qfq", "as_of": "2026-01-02", "currency": "HKD"},+ "bars": bars([100, 110]), "fundamentals": {"roe": 0.2}},+ ]}+ original = copy.deepcopy(universe)+ result = stock.analyze_payload(universe, symbol="HK700", source=None, as_of=None)+ self.assertEqual(result["instrument"]["symbol"], "700.HK")+ self.assertEqual(result["instrument"]["currency"], "HKD")+ self.assertEqual(result["technical"]["last_price"], 110)+ self.assertEqual(result["fundamental"]["values"]["roe"], 0.2)+ self.assertEqual(result["provenance"], {+ "source": "dated-export", "as_of": "2026-01-02", "adjustment": "qfq",+ "evidence_type": "calculated_from_price_history",+ })+ self.assertEqual(result["quality"]["age_days"], 0)+ self.assertEqual(universe, original)++ def test_analyze_cli_keeps_metadata_defaults_and_explicit_overrides(self):+ with tempfile.TemporaryDirectory() as directory:+ source = Path(directory) / "history.json"+ output = Path(directory) / "analysis.json"+ payload = {"bars": bars([100, 110]), "meta": {+ "symbol": "600519.SH", "source": "history-provider", "adjustment": "qfq",+ "as_of": "2026-01-02",+ }}+ source.write_text(json.dumps(payload), encoding="utf-8")+ command = ["analyze", "--input", str(source), "--output", str(output)]+ self.assertEqual(stock.main(command), 0)+ result = json.loads(output.read_text(encoding="utf-8"))+ self.assertEqual(result["provenance"]["source"], "history-provider")+ self.assertEqual(result["provenance"]["as_of"], "2026-01-02")+ self.assertEqual(result["instrument"]["symbol"], "600519.SH")+ self.assertEqual(stock.main(command + ["--source", "reviewed-export", "--as-of", "2026-02-01"]), 0)+ result = json.loads(output.read_text(encoding="utf-8"))+ self.assertEqual(result["provenance"]["source"], "reviewed-export")+ self.assertEqual(result["provenance"]["as_of"], "2026-02-01")+ self.assertIn("stale_daily_history", result["quality"]["flags"])+ # Legacy row-array files still work without a declared instrument.+ source.write_text(json.dumps(payload["bars"]), encoding="utf-8")+ self.assertEqual(stock.main(command + ["--as-of", "2026-01-02"]), 0)+ result = json.loads(output.read_text(encoding="utf-8"))+ self.assertIsNone(result["instrument"])+ self.assertEqual(result["provenance"]["source"], "user-file")++ def test_analyze_rejects_ambiguous_or_invalid_selection_without_overwrite(self):+ security = {"symbol": "600519.SH", "bars": bars([100, 110]),+ "meta": {"as_of": "2026-01-02", "adjustment": "qfq"}}+ future = copy.deepcopy(security)+ future["meta"]["as_of"] = "2026-01-01"+ conflicting = copy.deepcopy(security)+ conflicting["meta"]["symbol"] = "700.HK"+ for payload, symbol, error in [+ ({"securities": [security]}, None, "requires --symbol"),+ ({"securities": [security]}, "700.HK", "not found"),+ ({"securities": [security, {**security, "symbol": "SH600519"}]}, "600519", "ambiguous"),+ ({"securities": [future]}, "600519", "later than as_of"),+ ({"securities": [conflicting]}, "600519", "conflicts"),+ ({"securities": "invalid"}, "600519", "securities array"),+ ]:+ with self.subTest(error=error), tempfile.TemporaryDirectory() as directory:+ source, output = Path(directory) / "input.json", Path(directory) / "saved.json"+ source.write_text(json.dumps(payload), encoding="utf-8")+ output.write_text("previous analysis", encoding="utf-8")+ command = ["analyze", "--input", str(source), "--output", str(output)]+ if symbol:+ command += ["--symbol", symbol]+ stdout, stderr = io.StringIO(), io.StringIO()+ with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):+ self.assertEqual(stock.main(command), 2)+ self.assertEqual(stdout.getvalue(), "")+ self.assertIn(error, json.loads(stderr.getvalue())["error"])+ self.assertEqual(output.read_text(encoding="utf-8"), "previous analysis")++class ResearchAndQuantTests(unittest.TestCase):+ def test_screen_keeps_one_current_cutoff_when_records_have_historical_metadata(self):+ # A dated record must not make an old screen look fresh today.+ universe = {"securities": [{"symbol": "600519.SH",+ "bars": bars([100, 110], start="2020-01-01"),+ "meta": {"as_of": "2020-01-02"}}]}+ today = dt.datetime.now(dt.timezone.utc).date().isoformat()+ result = stock.screen_payload(universe, as_of=None)+ self.assertEqual(result["as_of"], today)+ self.assertIn("stale_daily_history", result["ranking"][0]["quality_flags"])++ def test_screen_ranks_eligible_names_and_exposes_factor_coverage(self):+ universe = {"securities": []}+ for symbol, slope, roe, pe in [+ ("600519", 0.5, 0.30, 20),+ ("700.HK", 0.25, 0.22, 26),+ ("MSFT", 0.35, 0.28, 32),+ ]:+ universe["securities"].append({+ "symbol": symbol,+ "bars": bars([100 + index * slope for index in range(75)]),+ "fundamentals": {"roe": roe, "net_margin": roe / 2, "pe": pe, "pb": 5},+ })+ result = stock.screen_payload(universe, as_of="2026-03-16")+ self.assertEqual(result["eligible_count"], 3)+ self.assertEqual([row["rank"] for row in result["ranking"]], [1, 2, 3])+ self.assertTrue(all(row["score_weight_coverage"] > 0 for row in result["ranking"]))++ def test_screen_missing_fundamentals_reduce_coverage_and_fail_required_filter(self):+ # Independent ranks: monotone 60%, 30%, 0% returns rank 100, 50, 0.+ universe = {"weights": {"momentum": 0.5, "quality": 0.5}, "securities": [+ {"symbol": symbol, "bars": bars([100 + i * slope for i in range(61)])}+ for symbol, slope in [("600519", 1), ("700.HK", 0.5), ("AAPL", 0)]+ ]}+ original = copy.deepcopy(universe)+ result = stock.screen_payload(universe, as_of="2026-03-02")+ self.assertEqual([(r["symbol"], r["score"], r["score_weight_coverage"])+ for r in result["ranking"]],+ [("600519.SH", 100, 0.5), ("700.HK", 50, 0.5), ("AAPL.US", 0, 0.5)])+ self.assertTrue(all(r["factor_raw"]["quality"] is None for r in result["ranking"]))+ self.assertEqual(universe, original)+ universe["filters"] = {"min_roe": 0.1}+ excluded = stock.screen_payload(universe, as_of="2026-03-02")+ self.assertEqual(excluded["ranking"], [])+ self.assertEqual(excluded["excluded_count"], 3)+ self.assertTrue(all(r["exclusion_reasons"] == ["missing:min_roe"] for r in excluded["excluded"]))++ def test_future_prices_cannot_change_prior_positions_or_equity(self):+ prefix = [100, 100, 110, 100, 90, 100, 120, 120, 110, 100, 110, 120]+ args = backtest_args(include_series=True, cost_bps=8, slippage_bps=5)+ before = stock.backtest_payload({"bars": bars(prefix)}, args)+ after = stock.backtest_payload({"bars": bars(prefix + [1, 1000, 1, 1000])}, args)+ self.assertEqual(after["series"][:len(before["series"])], before["series"])++ def test_single_delayed_entry_charges_fee_and_slippage_exactly_once(self):+ # The jump creates a signal; the next flat bar earns no gain and pays+ # 13bp once. Charging on the signal bar or every held bar is incorrect.+ result = stock.backtest_payload({"bars": bars([100] * 10 + [150, 150, 150])},+ backtest_args(strategy="momentum", lookback=2, cost_bps=8, slippage_bps=5, include_series=True))+ self.assertAlmostEqual(result["performance"]["total_return"], -0.0013)+ self.assertEqual(result["performance"]["trade_entries"], 1)+ self.assertEqual(result["benchmark"]["total_return"], 0.5)+ self.assertEqual(result["series"][9]["position"], 0)+ self.assertEqual(result["series"][10]["position"], 1)++ def test_transaction_cost_cannot_improve_same_backtest(self):+ closes = [100 + (index % 6) * 3 + index * 0.1 for index in range(80)]+ payload = {"bars": bars(closes)}+ free = stock.backtest_payload(payload, backtest_args())+ costly = stock.backtest_payload(payload, backtest_args(cost_bps=20, slippage_bps=10))+ self.assertLess(costly["performance"]["total_return"], free["performance"]["total_return"])+ self.assertEqual(+ costly["execution"]["lookahead_guard"],+ "signal_at_close_t_is_applied_to_return_t_plus_1",+ )++ def test_last_bar_jump_is_not_captured_by_close_derived_signal(self):+ payload = {"bars": bars([100] * 11 + [150])}+ result = stock.backtest_payload(+ payload,+ backtest_args(strategy="momentum", lookback=2, threshold=0.0),+ )+ self.assertEqual(result["performance"]["total_return"], 0.0)+ self.assertGreater(result["benchmark"]["total_return"], 0)+++class PortfolioAndTradeTests(unittest.TestCase):+ def test_portfolio_risk_calculates_concentration_tail_and_scenario(self):+ payload = {+ "positions": [+ {"symbol": "600519", "weight": 0.6, "currency": "CNY", "sector": "Consumer", "returns": [0.01, -0.02, 0.01, -0.03]},+ {"symbol": "700.HK", "weight": 0.4, "currency": "HKD", "sector": "Technology", "returns": [0.02, -0.01, 0.00, -0.04]},+ ],+ "scenarios": {"risk_off": {"Consumer": -0.10, "Technology": -0.20}},+ }+ result = stock.portfolio_payload(payload, 0.95)+ self.assertAlmostEqual(result["concentration"]["hhi"], 0.52)+ self.assertEqual(result["scenarios"]["risk_off"]["portfolio_return"], -0.14)+ self.assertGreaterEqual(result["tail_risk"]["cvar"], result["tail_risk"]["var"])++ def test_multimarket_exposure_and_declared_currency_stress_preserve_units(self):+ payload = {"positions": [+ {"symbol": s, "weight": w, "currency": c, "returns": [0, 0]}+ for s, w, c in [("600519", 0.4, "CNY"), ("700.HK", 0.35, "HKD"), ("AAPL", 0.25, "USD")]+ ], "scenarios": {"usd_down": {"currency": {"USD": -0.1}}}}+ result = stock.portfolio_payload(payload, 0.95)+ self.assertEqual(result["exposure"]["currency"], {"CNY": 0.4, "HKD": 0.35, "USD": 0.25})+ self.assertEqual(result["concentration"]["hhi"], 0.345)+ self.assertEqual(result["scenarios"]["usd_down"]["portfolio_return"], -0.025)+ self.assertEqual(result["scenarios"]["usd_down"]["contributions"],+ {"600519.SH": 0, "700.HK": 0, "AAPL.US": -0.025})+ # No dates or FX series are invented to align incomplete observations.+ payload["positions"][1]["returns"] = [0]+ with self.assertRaisesRegex(stock.StockError, "at least two returns"):+ stock.portfolio_payload(payload, 0.95)++ def test_live_authorization_and_policy_are_independent_and_never_submit(self):+ base = {"order": {"symbol": "700.HK", "side": "BUY", "quantity": 100, "mode": "live", "time_in_force": "DAY"},+ "instrument": {"lot_size": 100},+ "quote": {"last": 10, "as_of": "2026-08-28T09:30:00+08:00", "market_session": "open"},+ "account": {"cash": 2000}, "policy": {}}+ for allow, authorize in [(False, False), (True, False), (False, True), (True, True)]:+ with self.subTest(allow=allow, authorize=authorize):+ payload = copy.deepcopy(base)+ payload["policy"]["allow_live"] = allow+ payload["explicit_user_authorization"] = authorize+ before = copy.deepcopy(payload)+ result = stock.validate_order_payload(payload, as_of="2026-08-28T09:30:30+08:00")+ self.assertEqual(result["allowed"], allow and authorize)+ self.assertEqual(result["execution"], "validation_only_no_order_was_submitted")+ self.assertEqual(payload, before)+ # Exact authorization never overrides stale quotes or instrument rules.+ payload["order"]["quantity"] = 150+ result = stock.validate_order_payload(payload, as_of="2026-08-28T10:30:00+08:00")+ self.assertFalse(result["allowed"])+ self.assertIn("stale_quote", result["violations"])+ self.assertIn("quantity_not_multiple_of_lot_size", result["violations"])+ self.assertEqual(result["normalized_order"]["quantity"], 150)++ def test_a_share_t_plus_one_does_not_treat_held_shares_as_sellable(self):+ base = {"order": {"symbol": "600519", "side": "SELL", "quantity": 100},+ "quote": {"last": 10, "as_of": "2026-08-28T09:30:00+08:00"},+ "account": {"positions": [{"symbol": "600519", "quantity": 100}]}}+ blocked = stock.validate_order_payload(base, as_of="2026-08-28T09:30:30+08:00")+ self.assertIn("a_share_sell_requires_sellable_quantity_for_t_plus_1", blocked["violations"])+ base["account"]["positions"][0]["sellable_quantity"] = 100+ allowed = stock.validate_order_payload(base, as_of="2026-08-28T09:30:30+08:00")+ self.assertTrue(allowed["allowed"])+ self.assertEqual(allowed["normalized_order"]["mode"], "paper")+ self.assertEqual(allowed["execution"], "validation_only_no_order_was_submitted")++ def test_invalid_portfolio_weights_are_not_silently_normalized(self):+ payload = {+ "positions": [+ {"symbol": "MSFT", "weight": 0.7, "returns": [0.01, 0.02]},+ {"symbol": "AAPL", "weight": 0.5, "returns": [0.00, 0.01]},+ ]+ }+ with self.assertRaisesRegex(stock.StockError, "sum to one"):+ stock.portfolio_payload(payload, 0.95)++ def test_order_validator_blocks_bad_lot_cash_and_unauthorized_live_mode(self):+ payload = {+ "order": {"symbol": "600519", "side": "BUY", "quantity": 150, "mode": "live"},+ "quote": {"last": 100, "as_of": "2026-08-28T09:30:00+08:00"},+ "account": {"cash": 5_000, "positions": []},+ "policy": {"allow_live": True, "stale_after_seconds": 120},+ }+ result = stock.validate_order_payload(payload, as_of="2026-08-28T09:30:30+08:00")+ self.assertFalse(result["allowed"])+ self.assertIn("quantity_not_multiple_of_lot_size", result["violations"])+ self.assertIn("insufficient_cash", result["violations"])+ self.assertIn("exact_live_order_not_explicitly_authorized", result["violations"])+ self.assertEqual(result["execution"], "validation_only_no_order_was_submitted")++ def test_valid_paper_order_and_hong_kong_unknown_lot(self):+ base = {+ "order": {"symbol": "600519", "side": "BUY", "quantity": 100, "mode": "paper", "time_in_force": "DAY"},+ "quote": {"last": 10, "as_of": "2026-08-28T09:30:00+08:00", "market_session": "open"},+ "account": {"cash": 2_000, "positions": []},+ "policy": {"stale_after_seconds": 120},+ }+ result = stock.validate_order_payload(base, as_of="2026-08-28T09:30:30+08:00")+ self.assertTrue(result["allowed"])+ hk = json.loads(json.dumps(base))+ hk["order"].update({"symbol": "700.HK", "quantity": 100})+ blocked = stock.validate_order_payload(hk, as_of="2026-08-28T09:30:30+08:00")+ self.assertIn("hong_kong_buy_requires_current_instrument_lot_size", blocked["violations"])++ def test_quote_observation_time_reaches_order_validation_without_becoming_retrieval_time(self):+ payload = {"order": {"symbol": "700.HK", "side": "BUY", "quantity": 100},+ "instrument": {"lot_size": 100}, "account": {"cash": 2000},+ "quote": {"symbol": "700.HK", "currency": "HKD", "last": 10,+ "observed_at": "2026-08-28T09:30:00+08:00",+ "retrieved_at": "2026-08-28T09:40:00+08:00"}}+ before = copy.deepcopy(payload)+ result = stock.validate_order_payload(payload, as_of="2026-08-28T09:40:30+08:00")+ self.assertEqual(result["evidence"]["quote_age_seconds"], 630)+ self.assertIn("stale_quote", result["violations"])+ self.assertNotIn("missing_quote.as_of", result["violations"])+ self.assertEqual(payload, before)+ payload["quote"]["as_of"] = "2026-08-28T01:30:00+00:00"+ self.assertEqual(stock.validate_order_payload(payload, as_of="2026-08-28T09:30:30+08:00")["evidence"]["quote_age_seconds"], 30)+ payload["quote"]["as_of"] = "2026-08-28T09:40:00+08:00"+ with self.assertRaisesRegex(stock.StockError, "conflicting quote timestamps"):+ stock.validate_order_payload(payload, as_of="2026-08-28T09:40:30+08:00")++ def test_misnamed_risk_policy_is_rejected_instead_of_silently_dropping_limits(self):+ base = {"order": {"symbol": "AAPL", "side": "BUY", "quantity": 1},+ "quote": {"last": 100, "as_of": "2026-08-28T09:30:00+08:00"},+ "account": {"cash": 200}, "policy": {"max_position_value": 50}}+ with self.assertRaisesRegex(stock.StockError, "unsupported policy fields: max_position_value"):+ stock.validate_order_payload(base, as_of="2026-08-28T09:30:30+08:00")++ def test_quote_listing_currency_and_misplaced_authorization_fail_closed(self):+ base = {"order": {"symbol": "AAPL", "side": "BUY", "quantity": 1, "mode": "live",+ "explicit_user_authorization": True},+ "quote": {"symbol": "AAPL.US", "currency": "USD", "last": 100,+ "observed_at": "2026-08-28T09:30:00+08:00"},+ "account": {"cash": 200}, "policy": {"allow_live": True}}+ result = stock.validate_order_payload(base, as_of="2026-08-28T09:30:30+08:00")+ self.assertIn("exact_live_order_not_explicitly_authorized", result["violations"])+ self.assertFalse(result["allowed"])+ self.assertEqual(result["execution"], "validation_only_no_order_was_submitted")+ for field, value in [("symbol", "MSFT.US"), ("currency", "HKD")]:+ with self.subTest(field=field):+ changed = copy.deepcopy(base)+ changed["quote"][field] = value+ with self.assertRaisesRegex(stock.StockError, "quote .* does not match order"):+ stock.validate_order_payload(changed, as_of="2026-08-28T09:30:30+08:00")+++class ReportingTests(unittest.TestCase):+ def test_report_preserves_metric_records_and_missing_values_in_the_delivered_table(self):+ dossier = {"metrics": [+ {"symbol": "600519.SH", "score": 100, "coverage": 0.5, "currency": "CNY", "note": "source|as-of\n2024-12-31"},+ {"symbol": "700.HK", "score": 50, "coverage": 0.5, "currency": "HKD"},+ {"symbol": "AAPL.US", "score": 0, "coverage": 0.5, "currency": "USD", "quality": None},+ ]}+ before = copy.deepcopy(dossier)+ report = stock.render_report(dossier)+ self.assertIn("## Metrics\n\n| symbol | score | coverage | currency | note | quality |", report)+ self.assertIn("| 600519.SH | 100 | 0.5 | CNY | source\\|as-of<br>2024-12-31 | — |", report)+ self.assertIn("| 700.HK | 50 | 0.5 | HKD | — | — |", report)+ self.assertIn("| AAPL.US | 0 | 0.5 | USD | — | — |", report)+ self.assertEqual(dossier, before)++ def test_invalid_report_metrics_fail_without_overwriting_the_previous_delivery(self):+ with tempfile.TemporaryDirectory() as directory:+ source, output = Path(directory) / "dossier.json", Path(directory) / "report.md"+ output.write_text("previous user report", encoding="utf-8")+ for metrics in [[{"score": 100}, "not a metric record"], "unsupported shape"]:+ with self.subTest(metrics=metrics):+ source.write_text(json.dumps({"metrics": metrics}), encoding="utf-8")+ stdout, stderr = io.StringIO(), io.StringIO()+ with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):+ result = stock.main(["render-report", "--input", str(source), "--output", str(output)])+ self.assertEqual(result, 2)+ self.assertEqual(stdout.getvalue(), "")+ self.assertEqual(json.loads(stderr.getvalue())["error"], "report metrics must be an object or an array of objects")+ self.assertEqual(output.read_text(encoding="utf-8"), "previous user report")++ def test_historical_screen_cli_preserves_cutoff_and_rejects_future_bars_without_overwrite(self):+ with tempfile.TemporaryDirectory() as directory:+ source = Path(directory) / "universe.json"+ output = Path(directory) / "screen-result.json"+ source.write_text(json.dumps({"weights": {"momentum": 0.5, "quality": 0.5}, "securities": [+ {"symbol": "600519.SH", "bars": bars(range(100, 161), start="2024-11-01")},+ ]}), encoding="utf-8")+ command = ["screen", "--input", str(source), "--output", str(output), "--as-of"]+ with contextlib.redirect_stdout(io.StringIO()):+ self.assertEqual(stock.main(command + ["2024-12-31"]), 0)+ saved = output.read_text(encoding="utf-8")+ result = json.loads(saved)+ self.assertEqual(result["as_of"], "2024-12-31")+ self.assertEqual(result["ranking"][0]["quality_flags"], [])+ self.assertEqual(result["ranking"][0]["score_weight_coverage"], 0.5)+ stdout, stderr = io.StringIO(), io.StringIO()+ with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):+ self.assertEqual(stock.main(command + ["2024-12-30"]), 2)+ self.assertEqual(stdout.getvalue(), "")+ self.assertEqual(json.loads(stderr.getvalue()), {"ok": False, "error": "last bar is later than as_of"})+ self.assertEqual(output.read_text(encoding="utf-8"), saved)++ def test_report_preserves_evidence_and_escapes_table_cells(self):+ dossier = {+ "title": "MSFT Review",+ "evidence": {"source": "filing|quote", "as_of": "2026-08-27"},+ "metrics": {"return": "12%"},+ "risks": ["valuation"],+ }+ report = stock.render_report(dossier)+ self.assertIn("# MSFT Review", report)+ self.assertIn("filing\\|quote", report)+ self.assertIn("## Risks", report)+ self.assertIn("absent fields were not inferred", report)++ def test_report_preserves_scenario_counterevidence_and_parallel_verification_priorities(self):+ dossier = {+ "scenarios": [{"case": "base", "support": "cash supports earnings",+ "reverse_if": "cash gain is one-off", "catalyst": "audited release"},+ {"case": "upside", "threshold": None, "gap": "no cash-flow evidence"}],+ "next_steps": [{"priority": 1, "source": "cash-flow notes", "changes": "earnings interpretation"},+ {"priority": 1, "source": "inventory reconciliation", "changes": "channel pressure"}],+ }+ original = copy.deepcopy(dossier)+ report = stock.render_report(dossier)+ for value in ("cash supports earnings", "cash gain is one-off", "audited release",+ "no cash-flow evidence", "cash-flow notes", "earnings interpretation",+ "inventory reconciliation", "channel pressure"):+ self.assertIn(value, report)+ self.assertEqual(dossier, original)+ self.assertIn('"threshold": null', report)+ self.assertEqual(report.count('"priority": 1'), 2)++ def test_cli_failed_calculation_preserves_existing_report_and_has_no_success_payload(self):+ with tempfile.TemporaryDirectory() as directory:+ source = Path(directory) / "invalid.json"+ output = Path(directory) / "report.json"+ source.write_text(json.dumps({"bars": bars([100, 101])}), encoding="utf-8")+ output.write_text("user-owned result", encoding="utf-8")+ stdout, stderr = io.StringIO(), io.StringIO()+ with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):+ result = stock.main(["backtest", "--input", str(source), "--strategy", "sma_cross", "--output", str(output)])+ self.assertEqual(result, 2)+ self.assertEqual(stdout.getvalue(), "")+ self.assertEqual(json.loads(stderr.getvalue()), {"ok": False, "error": "backtest requires at least ten bars"})+ self.assertEqual(output.read_text(encoding="utf-8"), "user-owned result")++ def test_explicit_output_is_the_only_write_path(self):+ with tempfile.TemporaryDirectory() as directory:+ source = Path(directory) / "dossier.json"+ output = Path(directory) / "report.md"+ source.write_text(json.dumps({"title": "Fixture"}), encoding="utf-8")+ result = stock.main(["render-report", "--input", str(source), "--output", str(output)])+ self.assertEqual(result, 0)+ self.assertEqual(output.read_text(encoding="utf-8"), "# Fixture\n\n---\nGenerated from the supplied StockAnalyser dossier; absent fields were not inferred.\n")+++class EvidenceFlowTests(unittest.TestCase):+ def test_explicit_cutoff_is_shared_without_changing_strict_as_of_or_source(self):+ history = {"bars": bars([100] * 10 + [110, 10000]),+ "meta": {"symbol": "600519.SH", "source": "dated-export", "as_of": "2026-01-12"}}+ original = copy.deepcopy(history)+ selected, audit = stock.select_history_cutoff(history, "2026-01-11")+ self.assertEqual(history, original)+ self.assertEqual(selected["meta"], original["meta"])+ self.assertEqual(len(selected["bars"]), 11)+ self.assertEqual(audit[0]["excluded_bars"], 1)+ with self.assertRaisesRegex(stock.StockError, "later than as_of"):+ stock.normalize_bars(history, as_of="2026-01-11")+ with tempfile.TemporaryDirectory() as directory:+ source, output = Path(directory) / "input.json", Path(directory) / "result.json"+ for command in ("analyze", "screen", "backtest"):+ value = {"securities": [{"symbol": "600519.SH", **history}]} if command == "screen" else history+ source.write_text(json.dumps(value))+ before = source.read_bytes()+ args = [command, "--input", str(source), "--output", str(output), "--cutoff", "2026-01-11"]+ if command == "backtest": args += ["--strategy", "sma_cross", "--fast", "2", "--slow", "3"]+ self.assertEqual(stock.main(args), 0)+ result = json.loads(output.read_text())+ self.assertEqual(result["input_selection"]["histories"][0]["excluded_bars"], 1)+ self.assertEqual(source.read_bytes(), before)+ if command == "analyze": self.assertEqual(result["technical"]["last_price"], 110)+ if command == "backtest": self.assertEqual(result["performance"]["total_return"], 0)++ def test_cutoff_rejects_ambiguous_dates_or_insufficient_history(self):+ for rows in (bars([10, 11]) + [bars([12])[0]], [{"close": 1}], bars([10, 11], "2027-01-01")):+ with self.subTest(rows=rows), self.assertRaises(stock.StockError):+ stock.select_history_cutoff(rows, "2026-01-02")++ def test_conflicting_selection_and_broken_evidence_preserve_previous_delivery(self):+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory); source, evidence, output = [root / name for name in ("source.json", "evidence.json", "result.md")]+ source.write_text(json.dumps({"bars": bars([10, 11, 12])}))+ output.write_text("previous reviewed result")+ with contextlib.redirect_stderr(io.StringIO()):+ self.assertEqual(stock.main(["analyze", "--input", str(source), "--cutoff", "2026-01-02", "--as-of", "2026-01-03", "--output", str(output)]), 2)+ self.assertEqual(output.read_text(), "previous reviewed result")+ evidence.write_text("{broken")+ with contextlib.redirect_stderr(io.StringIO()):+ self.assertEqual(stock.main(["render-report", "--input", str(source), "--evidence-input", str(evidence), "--output", str(output)]), 2)+ self.assertEqual(output.read_text(), "previous reviewed result")++ def test_backtest_detail_uses_the_same_delayed_cost_ledger(self):+ args = backtest_args(slow=3, cost_bps=12, slippage_bps=8, include_series=True)+ result = stock.backtest_payload({"bars": bars([100] * 20 + [120] * 20)}, args)+ trades = [row for row in result["series"] if row["turnover_units"]]+ self.assertEqual([(row["signal_date"], row["date"]) for row in trades],+ [("2026-01-21", "2026-01-22"), ("2026-01-23", "2026-01-24")])+ self.assertEqual([row["cost_return"] for row in trades], [0.002, 0.002])+ self.assertEqual(result["series"][19]["gross_return"], 0)+ self.assertAlmostEqual(result["series"][-1]["equity"], 0.996004)+ args.include_series = False+ plain = stock.backtest_payload({"bars": bars([100] * 20 + [120] * 20)}, args)+ self.assertEqual({k: v for k, v in result.items() if k != "series"}, plain)++ def test_scenario_mode_compounds_fx_without_fabricating_historical_risk(self):+ payload = {"base_currency": "CNY", "positions": [+ {"symbol": "601318.SH", "currency": "CNY", "weight": 0.3},+ {"symbol": "9988.HK", "currency": "HKD", "weight": 0.2},+ {"symbol": "MSFT.US", "currency": "USD", "weight": 0.5}],+ "scenarios": {"joint": {"local": {"601318.SH": -0.2, "9988.HK": -0.05, "MSFT.US": -0.1},+ "fx": {"HKD": -0.02, "USD": -0.03}}}}+ original = copy.deepcopy(payload)+ result = stock.portfolio_payload(payload, 0.95, mode="scenario")+ self.assertEqual(payload, original)+ self.assertEqual(result["concentration"]["hhi"], 0.38)+ self.assertEqual(result["scenarios"]["joint"]["portfolio_return"], -0.1373)+ self.assertEqual(result["scenarios"]["joint"]["local_only_return"], -0.12)+ self.assertEqual(result["scenarios"]["joint"]["fx_only_return"], -0.019)+ self.assertIsNone(result["tail_risk"])+ self.assertIsNone(result["performance"])+ self.assertEqual(result["observations"], 0)+ with self.assertRaisesRegex(stock.StockError, "requires at least two returns"):+ stock.portfolio_payload(payload, 0.95)+ # A different base currency and positive shock exercise composition,+ # rather than only the regression fixture's additive-loss near miss.+ payload = {"base_currency": "USD", "positions": [{"symbol": "700.HK", "currency": "HKD", "weight": 1}],+ "scenarios": {"up": {"local": {"700.HK": 0.1}, "fx": {"HKD": 0.2}}}}+ self.assertEqual(stock.portfolio_payload(payload, 0.95, mode="scenario")["scenarios"]["up"]["portfolio_return"], 0.32)+ for mutation in ({"fx": {"EUR": 0.1}}, {"local": {"700.HK": -1.1}}, {"fx": []}):+ invalid = copy.deepcopy(payload); invalid["scenarios"]["up"].update(mutation)+ with self.subTest(mutation=mutation), self.assertRaises(stock.StockError):+ stock.portfolio_payload(invalid, 0.95, mode="scenario")++ def test_render_report_carries_original_observation_and_retrieval_times(self):+ import hashlib+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory); dossier, receipt, quote, output = [root / name for name in ("dossier.json", "receipt.json", "quote.json", "review.md")]+ dossier.write_text(json.dumps({"summary": "Model interpretation without copied timestamps."}))+ receipt.write_text(json.dumps({"source": "export", "order_id": "PAPER-X", "timestamp": "2020-02-03T10:00:00+08:00", "mode": "paper"}))+ quote.write_text(json.dumps({"quote": {"observed_at": "2020-02-02T10:00:00+08:00", "retrieved_at": "2020-02-04T10:00:00+08:00"}}))+ before = receipt.read_bytes()+ self.assertEqual(stock.main(["render-report", "--input", str(dossier), "--evidence-input", str(receipt), "--evidence-input", str(quote), "--output", str(output)]), 0)+ text = output.read_text()+ for value in ("2020-02-03T10:00:00+08:00", "2020-02-02T10:00:00+08:00", "2020-02-04T10:00:00+08:00", hashlib.sha256(before).hexdigest(), "not a claim of current state"):+ self.assertIn(value, text)+ self.assertEqual(receipt.read_bytes(), before)+ # No timestamp is inferred for a genuinely undated export.+ receipt.write_text('{"source":"undated"}')+ self.assertEqual(stock.evidence_record(str(receipt))["declared_metadata"], {"source": "undated"})++ def test_duplicate_security_aliases_cannot_hide_contributions(self):+ payload = {"positions": [{"symbol": symbol, "weight": 0.5, "returns": [0, 0]} for symbol in ("00700.HK", "700.HK")]}+ for mode in ("historical", "scenario"):+ with self.subTest(mode=mode), self.assertRaisesRegex(stock.StockError, "duplicate securities"):+ stock.portfolio_payload(payload, 0.95, mode=mode)++ def test_all_cli_outputs_preserve_input_and_evidence_even_through_aliases(self):+ import os+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory); source = root / "input.json"; source.write_text('{"title":"keep"}')+ aliases = [source, root / "symlink.json", root / "hardlink.json"]+ aliases[1].symlink_to(source); os.link(source, aliases[2])+ for alias in aliases:+ for command in ("render-report", "screen", "portfolio-risk", "validate-order"):+ with self.subTest(command=command, alias=alias.name), contextlib.redirect_stderr(io.StringIO()):+ self.assertEqual(stock.main([command, "--input", str(source), "--output", str(alias)]), 2)+ self.assertEqual(source.read_text(), '{"title":"keep"}')+ dossier = root / "dossier.json"; dossier.write_text('{}')+ with contextlib.redirect_stderr(io.StringIO()):+ self.assertEqual(stock.main(["render-report", "--input", str(dossier), "--evidence-input", str(source), "--output", str(source)]), 2)+ self.assertEqual(source.read_text(), '{"title":"keep"}')+++if __name__ == "__main__":+ unittest.main()diff --git a/resources/test/test_stock_analyser_history_unit.py b/resources/test/test_stock_analyser_history_unit.pynew file mode 100644index 00000000..19080bee--- /dev/null+++ b/resources/test/test_stock_analyser_history_unit.py@@ -0,0 +1,262 @@+import datetime as dt+import importlib.util+import socket+import sys+import unittest+from pathlib import Path+++SKILL_DIR = Path(__file__).resolve().parents[1] / "builtin/marketplace/agents/1040b336306f/skills/market-data"+SCRIPT_DIR = SKILL_DIR / "scripts"+sys.path.insert(0, str(SCRIPT_DIR))+SPEC = importlib.util.spec_from_file_location("stock_analyser_history", SCRIPT_DIR / "history.py")+assert SPEC and SPEC.loader+history = importlib.util.module_from_spec(SPEC)+SPEC.loader.exec_module(history)+++class Frame:+ def __init__(self, rows):+ self.rows = rows++ def to_dict(self, orient):+ if orient != "records":+ raise AssertionError(orient)+ return self.rows+++def rows(*dates):+ return [+ {+ "date": date,+ "open": 10 + index,+ "high": 11 + index,+ "low": 9 + index,+ "close": 10.5 + index,+ "volume": 1_000 + index,+ "amount": 10_500 + index,+ }+ for index, date in enumerate(dates)+ ]+++class FakeAk:+ __version__ = "1.18.94"++ def __init__(self):+ self.calls = []++ def stock_zh_a_daily(self, **kwargs):+ self.calls.append(("a-sina", kwargs))+ return Frame(rows("2026-08-25", "2026-08-26", "2026-08-27"))++ def stock_hk_daily(self, **kwargs):+ self.calls.append(("hk-sina", kwargs))+ return Frame(rows("2026-08-24", "2026-08-26", "2026-08-27"))++ def stock_us_daily(self, **kwargs):+ self.calls.append(("us-sina", kwargs))+ return Frame(rows("2026-08-25", "2026-08-26", "2026-08-27"))+++class HistoryTests(unittest.TestCase):+ def test_primary_sina_history_covers_a_hk_and_us(self):+ cases = [+ ("601872.SH", "auto", "a-sina", "sh601872", 3),+ ("00700", "HK", "hk-sina", "00700", 2),+ ("AAPL", "auto", "us-sina", "AAPL", 3),+ ]+ for symbol, market_name, call_name, provider_symbol, expected_count in cases:+ with self.subTest(symbol=symbol):+ ak = FakeAk()+ result = history.fetch(+ symbol, "2026-08-25", "2026-08-27", market=market_name,+ ak_module=ak,+ retrieved_at=dt.datetime(2026, 8, 28, tzinfo=dt.timezone.utc),+ )+ self.assertEqual(ak.calls[0][0], call_name)+ self.assertEqual(result["meta"]["source"], "akshare_sina")+ self.assertEqual(result["meta"]["provider_symbol"], provider_symbol)+ self.assertEqual(result["meta"]["provider_version"], "1.18.94")+ self.assertEqual(result["meta"]["adjustment"], "qfq")+ self.assertEqual(result["quality"]["bar_count"], expected_count)+ self.assertTrue(all(+ "2026-08-25" <= bar["date"] <= "2026-08-27"+ for bar in result["bars"]+ ))+ self.assertEqual(result["quality"]["last_date"], "2026-08-27")++ def test_auto_uses_eastmoney_fallback_after_sina_failure(self):+ class FallbackAk:+ __version__ = "1.18.94"++ def stock_zh_a_daily(self, **_kwargs):+ raise RuntimeError("sina unavailable")++ def stock_zh_a_hist(self, **kwargs):+ self.kwargs = kwargs+ return Frame([+ {"日期": "2026-08-26", "开盘": 10, "最高": 11, "最低": 9, "收盘": 10.5, "成交量": 100},+ {"日期": "2026-08-27", "开盘": 11, "最高": 12, "最低": 10, "收盘": 11.5, "成交量": 120},+ ])++ ak = FallbackAk()+ result = history.fetch("601872", "2026-08-26", "2026-08-27", ak_module=ak)+ self.assertEqual(result["meta"]["source"], "akshare_eastmoney")+ self.assertEqual(result["meta"]["attempts"][0]["status"], "error")+ self.assertEqual(result["meta"]["attempts"][1]["status"], "ok")+ self.assertEqual(ak.kwargs["symbol"], "601872")++ def test_eastmoney_fallback_maps_hk_and_us_provider_symbols(self):+ class FallbackAk:+ __version__ = "1.18.94"++ def __init__(self):+ self.kwargs = None++ def stock_hk_daily(self, **_kwargs):+ raise RuntimeError("sina unavailable")++ def stock_us_daily(self, **_kwargs):+ raise RuntimeError("sina unavailable")++ def stock_hk_hist(self, **kwargs):+ self.kwargs = kwargs+ return Frame(rows("2026-08-26", "2026-08-27"))++ def stock_us_hist(self, **kwargs):+ self.kwargs = kwargs+ return Frame(rows("2026-08-26", "2026-08-27"))++ cases = [+ ("00700.HK", None, "00700"),+ ("AAPL.US", "105.AAPL", "105.AAPL"),+ ]+ for symbol, explicit_provider_symbol, expected_provider_symbol in cases:+ with self.subTest(symbol=symbol):+ ak = FallbackAk()+ result = history.fetch(+ symbol, "2026-08-26", "2026-08-27",+ provider_symbol=explicit_provider_symbol,+ ak_module=ak,+ )+ self.assertEqual(result["meta"]["source"], "akshare_eastmoney")+ self.assertEqual(result["meta"]["provider_symbol"], expected_provider_symbol)+ self.assertEqual(ak.kwargs["symbol"], expected_provider_symbol)+ self.assertEqual(ak.kwargs["adjust"], "qfq")++ def test_raw_history_preserves_raw_metadata_and_provider_argument(self):+ ak = FakeAk()+ result = history.fetch(+ "601872.SH", "2026-08-25", "2026-08-27",+ adjust="raw", ak_module=ak,+ )+ self.assertEqual(ak.calls[0][1]["adjust"], "")+ self.assertEqual(result["meta"]["adjustment"], "raw")++ def test_explicit_provider_failure_does_not_silently_switch_vendor(self):+ class BrokenSina:+ __version__ = "1.18.94"++ def __init__(self):+ self.calls = []++ def stock_zh_a_daily(self, **_kwargs):+ self.calls.append("sina")+ raise RuntimeError("sina unavailable")++ def stock_zh_a_hist(self, **_kwargs):+ self.calls.append("eastmoney")+ return Frame(rows("2026-08-26", "2026-08-27"))++ ak = BrokenSina()+ with self.assertRaisesRegex(history.StockError, "all history providers failed"):+ history.fetch(+ "601872.SH", "2026-08-26", "2026-08-27",+ provider="akshare-sina", ak_module=ak,+ )+ self.assertEqual(ak.calls, ["sina"])++ def test_provider_failure_is_sanitized_and_restores_process_timeout(self):+ class BrokenProviders:+ __version__ = "1.18.94"++ def stock_zh_a_daily(self, **_kwargs):+ raise RuntimeError("failed at https://secret.invalid/sina")++ def stock_zh_a_hist(self, **_kwargs):+ raise RuntimeError("failed at https://secret.invalid/eastmoney")++ original = socket.getdefaulttimeout()+ socket.setdefaulttimeout(7.25)+ try:+ with self.assertRaisesRegex(+ history.StockError, "all history providers failed",+ ) as raised:+ history.fetch(+ "601872.SH", "2026-08-26", "2026-08-27",+ timeout=1, ak_module=BrokenProviders(),+ )+ self.assertNotIn("secret.invalid", str(raised.exception))+ self.assertIn("provider endpoint", str(raised.exception))+ self.assertEqual(socket.getdefaulttimeout(), 7.25)+ finally:+ socket.setdefaulttimeout(original)++ def test_partial_provider_results_do_not_report_false_success(self):+ class PartialProviders:+ __version__ = "1.18.94"++ def stock_zh_a_daily(self, **_kwargs):+ return Frame(rows("2026-08-27"))++ def stock_zh_a_hist(self, **_kwargs):+ return Frame(rows("2026-08-27"))++ with self.assertRaisesRegex(+ history.StockError, "all history providers failed",+ ) as raised:+ history.fetch(+ "601872.SH", "2026-08-26", "2026-08-27",+ ak_module=PartialProviders(),+ )+ self.assertIn("fewer than two bars", str(raised.exception))+ self.assertIn("akshare-sina", str(raised.exception))+ self.assertIn("akshare-eastmoney", str(raised.exception))++ def test_does_not_silently_truncate_provider_rows(self):+ ak = FakeAk()+ with self.assertRaisesRegex(history.StockError, "exceeding max-bars"):+ history.fetch(+ "601872", "2026-08-25", "2026-08-27", max_bars=2, ak_module=ak,+ )++ def test_invalid_options_fail_before_any_provider_call(self):+ invalid = [+ {"start": "2026-08-28", "end": "2026-08-27"},+ {"start": "2026/08/26", "end": "2026-08-27"},+ {"provider": "unknown"},+ {"adjust": "split-only"},+ {"timeout": 0},+ {"timeout": 61},+ {"max_bars": 1},+ {"max_bars": 20_001},+ ]+ for kwargs in invalid:+ with self.subTest(kwargs=kwargs):+ ak = FakeAk()+ defaults = {"start": "2026-08-26", "end": "2026-08-27"}+ defaults.update(kwargs)+ with self.assertRaises(history.StockError):+ history.fetch("601872.SH", ak_module=ak, **defaults)+ self.assertEqual(ak.calls, [])++ def test_dependency_is_exactly_pinned(self):+ self.assertEqual(+ (SKILL_DIR / "requirements.history.txt").read_text(encoding="utf-8").strip(),+ "akshare==1.18.94",+ )+++if __name__ == "__main__":+ unittest.main()diff --git a/resources/test/test_stock_analyser_market_unit.py b/resources/test/test_stock_analyser_market_unit.pynew file mode 100644index 00000000..27325372--- /dev/null+++ b/resources/test/test_stock_analyser_market_unit.py@@ -0,0 +1,255 @@+import datetime as dt+import contextlib+import importlib.util+import io+import json+import sys+import unittest+import tempfile+from unittest import mock+import urllib.error+from pathlib import Path+++SCRIPT_DIR = Path(__file__).resolve().parents[1] / "builtin/marketplace/agents/1040b336306f/skills/market-data/scripts"+sys.path.insert(0, str(SCRIPT_DIR))+SPEC = importlib.util.spec_from_file_location("stock_analyser_market", SCRIPT_DIR / "market.py")+assert SPEC and SPEC.loader+market = importlib.util.module_from_spec(SPEC)+SPEC.loader.exec_module(market)+++class Response(io.BytesIO):+ def __enter__(self):+ return self++ def __exit__(self, *_args):+ self.close()+++def response(text):+ return Response(text.encode("gb18030"))+++TENCENT_A = (+ 'v_sh601872="1~招商轮船~601872~18.99~18.62~0.00~0~0~0~18.99~2342~'+ '0~0~0~0~0~0~0~0~18.99~2342~0~0~0~0~0~0~0~0~~20260828092302~'+ '0.37~1.99~0.00~0.00~18.99/0/0~0~0~0.00";\n'+)++TENCENT_HK = (+ 'v_hk00700="100~腾讯控股~00700~455.600~447.800~444.000~10201355.0~0~0~'+ '455.600~0~0~0~0~0~0~0~0~0~455.600~0~0~0~0~0~0~0~0~0~10201355.0~'+ '2026/08/28 10:38:39~7.800~1.74~459.800~443.400~455.600~10201355.0~'+ '4630281113.124~0~16.66~~0~0~3.66";\n'+)++TENCENT_US = (+ 'v_usAAPL="200~苹果~AAPL.OQ~314.58~313.45~310.55~32419233~0~0~314.80~'+ '80~0~0~0~0~0~0~0~0~314.94~80~0~0~0~0~0~0~0~0~~2026-08-27 16:00:01~'+ '1.13~0.36~315.40~309.40~USD~32419233~10163243816~0.22~36.08";\n'+)++SINA_A = (+ 'var hq_str_sh601872="招商轮船,0.000,18.620,0.000,0.000,0.000,18.990,'+ '18.990,0,0.000,241936,18.990,13564,0.000,0,0.000,0,0.000,0,0.000,'+ '241936,18.990,0,0.000,0,0.000,0,0.000,0,0.000,2026-08-28,09:23:01,00,";\n'+)++class QuoteTests(unittest.TestCase):+ def test_actual_quote_adapter_result_can_be_validated_without_field_reconstruction(self):+ result = market.quote("700.HK", opener=lambda *_args, **_kwargs: response(TENCENT_HK),+ now=dt.datetime(2026, 8, 28, 2, 39, tzinfo=dt.timezone.utc))+ from stock import validate_order_payload+ payload = {"order": {"symbol": "00700.HK", "side": "BUY", "quantity": 100},+ "quote": result, "instrument": {"lot_size": 100}, "account": {"cash": 50000}}+ validation = validate_order_payload(payload, as_of="2026-08-28T10:39:00+08:00")+ self.assertTrue(validation["allowed"])+ self.assertEqual(validation["evidence"]["quote_age_seconds"], 21)+ self.assertEqual(validation["evidence"]["last_price"], 455.6)+ self.assertEqual(validation["estimates"]["order_value"], 45560)+ self.assertNotIn("market_session_not_verified", validation["warnings"])+ self.assertEqual(validation["execution"], "validation_only_no_order_was_submitted")++ def test_tencent_a_quote_is_api_sourced_and_auction_aware(self):+ result = market.quote(+ "601872.SH",+ opener=lambda *_args, **_kwargs: response(TENCENT_A),+ now=dt.datetime(2026, 8, 28, 1, 23, 2, tzinfo=dt.timezone.utc),+ )+ self.assertEqual(result["instrument"]["symbol"], "601872.SH")+ self.assertEqual(result["instrument"]["name"], "招商轮船")+ self.assertEqual(result["quote"]["last"], 18.99)+ self.assertEqual(result["provenance"]["source"], "tencent_quote_api")+ self.assertEqual(result["provenance"]["observed_at"], "2026-08-28T09:23:02+08:00")+ self.assertEqual(result["quality"]["market_session"], "opening_auction")+ self.assertEqual(result["quality"]["quote_kind"], "indicative")+ self.assertIn("indicative_quote", result["quality"]["flags"])++ def test_real_cross_market_shapes_preserve_currency_time_and_snapshot_semantics(self):+ cases = [+ (+ "00700.HK", TENCENT_HK,+ dt.datetime(2026, 8, 28, 2, 39, tzinfo=dt.timezone.utc),+ "700.HK", "HKD", 455.6, "2026-08-28T10:38:39+08:00",+ "regular", "last_trade", 4_630_281_113.124,+ ),+ (+ "AAPL.US", TENCENT_US,+ dt.datetime(2026, 8, 27, 20, 1, tzinfo=dt.timezone.utc),+ "AAPL.US", "USD", 314.58, "2026-08-27T16:00:01-04:00",+ "post_market", "last_market_snapshot", 10_163_243_816,+ ),+ ]+ for (+ symbol, body, now, canonical, currency, last, observed_at,+ session, quote_kind, amount,+ ) in cases:+ with self.subTest(symbol=symbol):+ result = market.quote(+ symbol,+ opener=lambda *_args, body=body, **_kwargs: response(body),+ now=now,+ )+ self.assertEqual(result["instrument"]["symbol"], canonical)+ self.assertEqual(result["instrument"]["currency"], currency)+ self.assertEqual(result["quote"]["last"], last)+ self.assertEqual(result["quote"]["amount"], amount)+ self.assertEqual(result["provenance"]["observed_at"], observed_at)+ self.assertEqual(result["quality"]["market_session"], session)+ self.assertEqual(result["quality"]["quote_kind"], quote_kind)++ def test_future_provider_time_is_disclosed_instead_of_looking_fresh(self):+ result = market.quote(+ "601872.SH",+ opener=lambda *_args, **_kwargs: response(TENCENT_A),+ now=dt.datetime(2026, 8, 28, 1, 22, tzinfo=dt.timezone.utc),+ )+ self.assertEqual(result["quality"]["age_seconds"], 0)+ self.assertIn("provider_timestamp_in_future", result["quality"]["flags"])++ def test_missing_current_trade_uses_previous_close_with_explicit_flag(self):+ no_last = TENCENT_A.replace(+ "~601872~18.99~18.62~", "~601872~~18.62~", 1,+ )+ result = market.quote(+ "601872.SH",+ opener=lambda *_args, **_kwargs: response(no_last),+ now=dt.datetime(2026, 8, 28, 1, 23, 2, tzinfo=dt.timezone.utc),+ )+ self.assertEqual(result["quote"]["last"], 18.62)+ self.assertEqual(result["quality"]["quote_kind"], "previous_close")+ self.assertIn("no_current_trade_used_previous_close", result["quality"]["flags"])++ def test_auto_falls_back_to_sina_and_uses_indicative_bid(self):+ requests = []++ def opener(request, **_kwargs):+ requests.append(request)+ if "qt.gtimg.cn" in request.full_url:+ raise urllib.error.URLError("primary unavailable")+ return response(SINA_A)++ result = market.quote(+ "601872",+ opener=opener,+ now=dt.datetime(2026, 8, 28, 1, 23, 2, tzinfo=dt.timezone.utc),+ )+ self.assertEqual(result["quote"]["last"], 18.99)+ self.assertEqual(result["provenance"]["source"], "sina_quote_api")+ self.assertEqual(result["quality"]["attempts"][0]["status"], "error")+ self.assertEqual(result["quality"]["attempts"][1], {"provider": "sina", "status": "ok"})+ self.assertEqual(requests[1].get_header("Referer"), "https://finance.sina.com.cn/")++ def test_successful_stale_primary_is_disclosed_without_switching_provider(self):+ requests = []++ def opener(request, **_kwargs):+ requests.append(request)+ return response(TENCENT_HK)++ result = market.quote(+ "00700.HK",+ opener=opener,+ now=dt.datetime(2026, 9, 17, 2, 42, 30, tzinfo=dt.timezone.utc),+ )+ self.assertEqual(result["provenance"]["source"], "tencent_quote_api")+ self.assertIn("stale_quote", result["quality"]["flags"])+ self.assertEqual(result["quality"]["attempts"], [{"provider": "tencent", "status": "ok"}])+ self.assertEqual(len(requests), 1)+ self.assertIn("qt.gtimg.cn", requests[0].full_url)++ def test_provider_symbol_mapping_covers_three_markets(self):+ self.assertEqual(market.provider_symbol(market.normalize_symbol("600519"), "tencent"), "sh600519")+ self.assertEqual(market.provider_symbol(market.normalize_symbol("00700", "HK"), "tencent"), "r_hk00700")+ self.assertEqual(market.provider_symbol(market.normalize_symbol("AAPL"), "tencent"), "usAAPL")+ self.assertEqual(market.provider_symbol(market.normalize_symbol("AAPL"), "sina"), "gb_AAPL")++ def test_status_is_free_and_does_not_inspect_credentials(self):+ result = market.status()+ serialized = json.dumps(result).lower()+ self.assertFalse(result["network_checked"])+ self.assertFalse(result["credentials_required"])+ self.assertIn('"cost": "free"', serialized)+ self.assertNotIn("token", serialized)++ def test_all_provider_failures_are_bounded_and_actionable(self):+ attempts = []++ def opener(request, **kwargs):+ attempts.append((request.full_url, kwargs["timeout"]))+ raise urllib.error.URLError("downstream unavailable at https://private.invalid/path")++ with self.assertRaisesRegex(market.StockError, "all quote providers failed") as raised:+ market.quote("AAPL", opener=opener, timeout=3)+ self.assertNotIn("private.invalid", str(raised.exception))+ self.assertEqual(len(attempts), 2)+ self.assertIn("qt.gtimg.cn", attempts[0][0])+ self.assertIn("sinajs.cn", attempts[1][0])+ self.assertEqual([timeout for _, timeout in attempts], [3, 3])++ def test_explicit_provider_failure_does_not_silently_change_source(self):+ attempts = []++ def opener(request, **kwargs):+ attempts.append(request.full_url)+ raise urllib.error.URLError("unavailable")++ with self.assertRaises(market.StockError):+ market.quote("601872.SH", provider="tencent", opener=opener)+ self.assertEqual(len(attempts), 1)+ self.assertIn("qt.gtimg.cn", attempts[0])++ def test_quote_failure_returns_error_without_fabricating_or_overwriting_output(self):+ with tempfile.TemporaryDirectory() as root:+ output = Path(root) / "quote.json"+ output.write_text("previous verified snapshot", encoding="utf-8")+ stdout, stderr = io.StringIO(), io.StringIO()+ with mock.patch.object(market, "quote", side_effect=market.StockError("all quote providers failed")), \+ contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):+ code = market.main(["quote", "--symbol", "601872.SH", "--output", str(output)])+ self.assertEqual(code, 2)+ self.assertEqual(stdout.getvalue(), "")+ self.assertEqual(stderr.getvalue(), "error: all quote providers failed\n")+ self.assertEqual(output.read_text(encoding="utf-8"), "previous verified snapshot")++ def test_invalid_options_fail_before_any_network_attempt(self):+ attempts = []++ def opener(*args, **kwargs):+ attempts.append((args, kwargs))+ return response(TENCENT_A)++ invalid = [+ {"provider": "unknown"},+ {"timeout": 0},+ {"timeout": 31},+ ]+ for kwargs in invalid:+ with self.subTest(kwargs=kwargs), self.assertRaises(market.StockError):+ market.quote("601872.SH", opener=opener, **kwargs)+ self.assertEqual(attempts, [])+++if __name__ == "__main__":+ unittest.main()diff --git a/src/core-agent/src/agent/runner.ts b/src/core-agent/src/agent/runner.tsindex d7309bba..bef035c7 100644--- a/src/core-agent/src/agent/runner.ts+++ b/src/core-agent/src/agent/runner.ts@@ -1,5 +1,6 @@import { completedRepeatKey } from "./repeat-evidence.js";import { fileFailureForLog } from "../tools/file-diagnostics.js";+import { toolInputDiagnostic } from "../providers/tool-input-diagnostics.js";import { createHash, randomBytes } from "node:crypto";import type {Message,@@ -617,6 +618,7 @@ function logTextRef(value: unknown): { text_hash: string; text_chars: number } {function toolFailureForLog(call: ToolUseCall, result: ToolResult, sessionId?: string, correlation?: { runner_ref: string; batch_sequence: number; execution_mode: "single" | "parallel" | "programmatic" }): Record<string, unknown> {const execution = result.observations?.execution;const fileFailure = fileFailureForLog(result.observations?.fileFailure);+ const inputDiagnostic = toolInputDiagnostic(call.input);const count = (value: unknown) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0? value : undefined;const flag = (value: unknown) => typeof value === "boolean" ? value : undefined;@@ -624,6 +626,7 @@ function toolFailureForLog(call: ToolUseCall, result: ToolResult, sessionId?: sttool: call.name,...correlation,...(fileFailure ? { file_failure: fileFailure } : {}),+ ...(inputDiagnostic ? { input_parse: inputDiagnostic } : {}),...(sessionId ? { session_hash: logTextRef(sessionId).text_hash } : {}),call_hash: logTextRef(call.id).text_hash,input_digest: stableToolInputDigest(call),@@ -850,7 +853,7 @@ function requestMetadataForModelCall(planStepCount: number;noProgressRounds: number;},-): Record<string, unknown> | undefined {+): Record<string, unknown> {const metadata: Record<string, unknown> = {...(base || {}),// The main agent turn gets its output limit from the model catalog (or the@@ -1864,6 +1867,16 @@ export class AgentRunner {servedModelId,);const callUsableInputTokens = this.resolveUsableInputTokens(modelId, servedModelId);+ // In-memory scalar evidence only; the provider emits it on failure.+ // Reuse compaction's existing estimates rather than rescan healthy requests.+ const contextBudgetDiagnostics: Record<string, number | string> = {+ version: 1,+ budget_context_window: historyModel?.contextWindow ?? 200_000,+ output_reservation: historyModel?.maxOutputTokens ?? 8_192,+ usable_input_tokens: callUsableInputTokens,+ input_ceiling_tokens: Math.floor(callUsableInputTokens * CONTEXT_COMPACTION_TRIGGER_RATIO),+ window_source: historyModel?.contextWindow ? 'catalog' : 'fallback',+ };const prepareContextStartedAt = Date.now();const progressContextEpoch = this.session.contentEpoch();@@ -1881,6 +1894,7 @@ export class AgentRunner {{usableInputTokens: callUsableInputTokens,fixedOverheadTokens: estimateFixedOverheadTokens(systemPrompt, toolDefs, budgetEphemeral || undefined),+ diagnostics: contextBudgetDiagnostics,},);// Layered compaction has had its turn. If the request is still over@@ -1893,6 +1907,7 @@ export class AgentRunner {callUsableInputTokens,compactionControl,requestTokenAnchor,+ contextBudgetDiagnostics,);} finally {if (this.session.contentEpoch() !== progressContextEpoch) resetProgressEvidence();@@ -1909,6 +1924,9 @@ export class AgentRunner {planStepCount: this.session.getExecutionPlan()?.steps.length || 0,noProgressRounds: guards.consecutiveNoProgressRounds,});+ contextBudgetDiagnostics.compaction_attempts = compactionControl.attempts;+ contextBudgetDiagnostics.compaction_failures = compactionControl.failures;+ modelRequestMetadata.contextBudgetDiagnostics = contextBudgetDiagnostics;// Consume the provider stream token-by-token so callers (UI) can// paint partial text as it arrives. We still assemble a full@@ -3518,6 +3536,7 @@ export class AgentRunner {usableInputTokens: number,control: CompactionControl,anchor: RequestTokenAnchor | null = null,+ diagnostics?: Record<string, number | string>,): AsyncIterable<AgentRunEvent> {// Anchored to the previous call's real usage when the anchor is still// valid; a fold earlier in this prepare phase bumps the content epoch, so@@ -3530,10 +3549,15 @@ export class AgentRunner {this.session.getEstimatorCalibration(),);const before = requestTokens();+ if (diagnostics) Object.assign(diagnostics, {+ emergency_before_tokens: before.tokens, emergency_after_tokens: before.tokens,+ estimated_input_tokens: before.tokens, estimate_source: before.source, emergency_result: 'not_needed',+ });if (before.tokens <= usableInputTokens * CONTEXT_COMPACTION_TRIGGER_RATIO) return;const { foldedGroups, archivedTurns } = this.applyDeterministicEmergencyFold();if (foldedGroups === 0 && archivedTurns === 0) {+ if (diagnostics) diagnostics.emergency_result = 'nothing_to_drop';// Nothing left that this pass is allowed to drop: what remains is the// system prompt, tool schemas, the user message, injected ledgers and// prior summaries and the protected latest tool round. Report it and let@@ -3554,6 +3578,10 @@ export class AgentRunner {}const after = requestTokens();+ if (diagnostics) Object.assign(diagnostics, {+ emergency_after_tokens: after.tokens, estimated_input_tokens: after.tokens,+ estimate_source: after.source, emergency_result: 'applied',+ });control.readCursor = this.session.workspaceObservationCursor();log.error("emergency context reduction applied", {sessionId: this.session.getSessionId(),@@ -3669,7 +3697,7 @@ export class AgentRunner {signal?: AbortSignal,retryContext?: CompletionParams["retryContext"],budget: ContextBudget = DEFAULT_CONTEXT_BUDGET,- costContext?: { usableInputTokens: number; fixedOverheadTokens: number },+ costContext?: { usableInputTokens: number; fixedOverheadTokens: number; diagnostics?: Record<string, number | string> },): AsyncIterable<AgentRunEvent> {throwIfAborted(signal);const compactionControl = control ?? {@@ -3710,6 +3738,7 @@ export class AgentRunner {if (activeCandidate && this.claimCompactionCandidate(compactionControl, activeFingerprint)) {const activeCompactionStartedAt = Date.now();const modelViewTokensBefore = this.session.estimateModelTokens();+ if (costContext?.diagnostics) costContext.diagnostics.compaction_before_tokens = modelViewTokensBefore;const activeCostFields = costContext? compactionCostFields(this.session, compactionControl, budget, costContext.usableInputTokens, costContext.fixedOverheadTokens): {};@@ -3783,6 +3812,7 @@ export class AgentRunner {budget.summaryHardTokens,);const appliedCheckpointTokens = estimateTextTokens(appliedSummary);+ if (costContext?.diagnostics) costContext.diagnostics.compaction_after_tokens = tokensAfter;const durationMs = Math.max(0, Date.now() - activeCompactionStartedAt);compactionControl.consecutiveFailures = 0;compactionControl.readCursor = this.session.workspaceObservationCursor();diff --git a/src/core-agent/src/providers/pi-provider.ts b/src/core-agent/src/providers/pi-provider.tsindex 6eeff969..3d41a396 100644--- a/src/core-agent/src/providers/pi-provider.ts+++ b/src/core-agent/src/providers/pi-provider.ts@@ -52,6 +52,7 @@ import { providerCredentialFailure } from "../shared/provider-error-facts.js";import type { CompletionParams, CompletionResult, LLMProvider, ToolDefinition } from "./base.js";import { createRequestDiagnostics, type ProviderFailureSource } from "./request-diagnostics.js";+import { ToolInputDiagnostics } from "./tool-input-diagnostics.js";const log = createLogger("pi-provider");@@ -1028,7 +1029,7 @@ export function createPiProvider(config: {const suppliedHeaders = requestHeaders(params);const turnRouting = codexTurnRouting(model, params, config.apiKey, suppliedHeaders);const headers = turnRouting?.headers ?? suppliedHeaders;- const requestDiagnostics = createRequestDiagnostics(params.signal, params.onRequestFailure);+ const requestDiagnostics = createRequestDiagnostics(params.signal, params.onRequestFailure, undefined, params.requestMetadata?.contextBudgetDiagnostics);const onPayload = (useOfficialReasoningDefaults || useProviderOutputDefault || config.onPayload || context.hostMessageIndexes.length)? ((payload: unknown, hookModel: Model<Api>) => requestDiagnostics.observePayload(() => {payload = restoreHostMessageRoles(payload, context, hookModel);@@ -1155,7 +1156,7 @@ export function createPiProvider(config: {const turnRouting = codexTurnRouting(model, params, config.apiKey, suppliedHeaders);const headers = turnRouting?.headers ?? suppliedHeaders;let responseErrorStatus: number | undefined;- const requestDiagnostics = createRequestDiagnostics(params.signal, params.onRequestFailure);+ const requestDiagnostics = createRequestDiagnostics(params.signal, params.onRequestFailure, undefined, params.requestMetadata?.contextBudgetDiagnostics);const onResponse = (response: ProviderHttpResponse) => {turnRouting?.observe(response);responseErrorStatus = response.status >= 400 ? response.status : undefined;@@ -1250,6 +1251,7 @@ export function createPiProvider(config: {onPayload: onPayload as any,});+ const toolInputs = new ToolInputDiagnostics(model.api === 'openai-completions');for await (const event of eventStream) {requestDiagnostics.observeEvent(event.type);switch (event.type) {@@ -1286,6 +1288,7 @@ export function createPiProvider(config: {yield { type: "thinking_end" };break;case "toolcall_start":+ toolInputs.start(event.contentIndex);{const text = leadingThinkFilter?.finish() || "";yield* takeLeadingThinkEvents();@@ -1302,9 +1305,11 @@ export function createPiProvider(config: {};break;case "toolcall_delta":+ toolInputs.delta(event.contentIndex, event.delta);yield { type: "tool_use_delta", id: "", input: event.delta };break;case "toolcall_end":+ toolInputs.end(event.contentIndex, event.toolCall.arguments);yield { type: "tool_use_end", id: event.toolCall.id };break;case "done":diff --git a/src/core-agent/src/providers/request-diagnostics.ts b/src/core-agent/src/providers/request-diagnostics.tsindex 48303856..8ebe2e81 100644--- a/src/core-agent/src/providers/request-diagnostics.ts+++ b/src/core-agent/src/providers/request-diagnostics.ts@@ -13,6 +13,29 @@ export interface ProviderRequestFailure {contextWindow?: number;outputLimit?: number;budgetObserved?: boolean;+ contextBudget?: Record<string, number | string>;+}++/** Existing runner observations, never a second scan of request content. */+export type FailureContextBudget = Record<string, number | string>;+const BUDGET_NUMBERS = ['budget_context_window', 'output_reservation', 'usable_input_tokens',+ 'input_ceiling_tokens', 'estimated_input_tokens', 'emergency_before_tokens', 'emergency_after_tokens',+ 'compaction_before_tokens', 'compaction_after_tokens', 'compaction_attempts', 'compaction_failures'];++function safeContextBudget(value: unknown): FailureContextBudget | undefined {+ if (!value || typeof value !== 'object') return;+ const raw = value as Record<string, unknown>;+ if (raw.version !== 1) return;+ const out: FailureContextBudget = { version: 1 };+ for (const key of BUDGET_NUMBERS) {+ const v = raw[key];+ if (typeof v === 'number' && Number.isSafeInteger(v) && v >= 0) out[key] = Math.min(1_000_000_000, v);+ }+ for (const [key, values] of Object.entries({+ estimate_source: ['estimated', 'anchored'], window_source: ['catalog', 'fallback'],+ emergency_result: ['not_needed', 'applied', 'nothing_to_drop'],+ })) if (typeof raw[key] === 'string' && values.includes(raw[key] as string)) out[key] = raw[key] as string;+ return out;}export type ProviderFailureSource = 'unknown' | 'payload' | 'transport' | 'http' | 'sdk_error' | 'exception';@@ -51,6 +74,7 @@ export function createRequestDiagnostics(signal?: AbortSignal,onFailure?: (failure: ProviderRequestFailure) => void,requestRef?: unknown,+ contextBudget?: unknown,) {const started = performance.now();const requestSequence = onFailure ? Math.min(1_000_000_000, (requestSequences.get(onFailure) ?? 0) + 1) : undefined;@@ -131,6 +155,10 @@ export function createRequestDiagnostics(elapsedMs: Math.min(1_000_000_000, Math.max(0, Math.round(performance.now() - started))),aborted: aborted || signal?.aborted === true,};+ // Only a failed request copies/sanitizes this small metadata record.+ if (!failure.aborted) {+ try { failure.contextBudget = safeContextBudget(contextBudget); } catch { /* Best effort. */ }+ }try { if (onFailure) { onFailure({ ...failure }); observed = true; } } catch { /* Best-effort observer. */ }return failure;},diff --git a/src/core-agent/src/providers/tool-input-diagnostics.ts b/src/core-agent/src/providers/tool-input-diagnostics.tsnew file mode 100644index 00000000..528c66ea--- /dev/null+++ b/src/core-agent/src/providers/tool-input-diagnostics.ts@@ -0,0 +1,63 @@+import { isDeepStrictEqual } from 'node:util';++type InputDiagnostic = {+ stream_json: 'strict' | 'non_strict' | 'unobserved' | 'limit';+ tolerant_parse: 'used' | 'not_used' | 'unknown';+ parsed_input_matches?: boolean;+};++// Facts follow the executed input by identity. They never become model input,+// durable history, raw logs, or an alternative parser/execution decision.+const facts = new WeakMap<object, Readonly<InputDiagnostic>>();+export function toolInputDiagnostic(input: unknown): Readonly<InputDiagnostic> | undefined {+ return input && typeof input === 'object' ? facts.get(input) : undefined;+}++/** Observe only bounded SDK deltas, once per completed call. Completions uses+ * their concatenation as its final JSON input. Other transports may replace it+ * in a terminal snapshot, so their parser provenance remains unknown. */+export class ToolInputDiagnostics {+ private readonly pending = new Map<number, string | null>();+ private chars = 0;+ private calls = 0;+ constructor(private readonly concatenatedFinalInput: boolean) {}++ start(index: number): void {+ if (this.calls++ < 32) this.pending.set(index, '');+ }++ delta(index: number, delta: string): void {+ const current = this.pending.get(index);+ if (typeof current !== 'string') return;+ // Cumulative limits also bound CPU for a storm of completed/oversized calls.+ if (current.length + delta.length > 32_768 || this.chars + delta.length > 131_072) {+ this.pending.set(index, null);+ return;+ }+ this.chars += delta.length;+ this.pending.set(index, current + delta);+ }++ end(index: number, input: unknown): void {+ const raw = this.pending.get(index);+ this.pending.delete(index);+ if (!input || typeof input !== 'object') return;+ const diagnostic: InputDiagnostic = {+ stream_json: raw === null ? 'limit' : 'unobserved', tolerant_parse: 'unknown',+ };+ if (typeof raw === 'string' && raw.trim().length) {+ try {+ const parsed = JSON.parse(raw);+ diagnostic.stream_json = 'strict';+ diagnostic.parsed_input_matches = isDeepStrictEqual(parsed, input);+ if (this.concatenatedFinalInput && diagnostic.parsed_input_matches) {+ diagnostic.tolerant_parse = 'not_used';+ }+ } catch {+ diagnostic.stream_json = 'non_strict';+ if (this.concatenatedFinalInput) diagnostic.tolerant_parse = 'used';+ }+ }+ facts.set(input, Object.freeze(diagnostic));+ }+}diff --git a/src/core-agent/src/sandbox/executor.ts b/src/core-agent/src/sandbox/executor.tsindex 36a6798c..b40c9c35 100644--- a/src/core-agent/src/sandbox/executor.ts+++ b/src/core-agent/src/sandbox/executor.ts@@ -17,6 +17,7 @@ import { createLogger } from "../shared/logger.js";import { errorCodeForLog } from "../shared/errors.js";import { blockedCommand } from "./command-policy.js";import { windowsTreeCleanupCommand } from "./windows-process-tree.js";+import { compilePowerShellChain, parsePowerShellChain } from "./powershell-chain.js";import {ProcessOutputCapture,type StreamedToolOutput,@@ -239,11 +240,14 @@ export function buildShellInvocation(const kind = inferShellKind(shell, platform);if (platform === "win32" && kind === "powershell") {+ const chain = shellBaseName(shell).replace(/\.exe$/i, "") === "powershell"+ ? parsePowerShellChain(command) : { kind: "none" as const };+ const executableCommand = chain.kind === "chain" ? compilePowerShellChain(chain) : command;// powershell.exe otherwise normalizes a failed native child's exit code to// 1. Preserve $LASTEXITCODE so process tools can report the real code.const powershellCommand = ["& {",- command,+ executableCommand,"$orkasCommandSucceeded = $?","$orkasNativeExitCode = $LASTEXITCODE","if ($null -ne $orkasNativeExitCode) { exit $orkasNativeExitCode }",diff --git a/src/core-agent/src/sandbox/powershell-chain.ts b/src/core-agent/src/sandbox/powershell-chain.tsnew file mode 100644index 00000000..0faae1c3--- /dev/null+++ b/src/core-agent/src/sandbox/powershell-chain.ts@@ -0,0 +1,110 @@+import { randomUUID } from "node:crypto";++export type PowerShellChain =+ | { kind: "none" }+ | { kind: "unsupported" }+ | { kind: "chain"; commands: string[]; operators: Array<"&&" | "||"> };++const MAX_CHAIN_CHARS = 64 * 1024;+const MAX_CHAIN_COMMANDS = 32;+// Grammar keywords cannot be treated as ordinary command invocations.+const STATEMENT_KEYWORDS = new Set([+ "if", "elseif", "else", "switch", "foreach", "for", "while", "do",+ "until", "try", "catch", "finally", "trap", "function", "filter",+ "class", "enum", "param", "return", "break", "continue", "exit",+ "throw", "data", "using", "begin", "process", "end", "clean",+]);++/** Recognize only complete, single-line chains of simple PowerShell commands.+ * This is not a general shell translator. Preserve operand bytes and reject+ * expressions, pipelines, redirections and statements rather than guessing.+ */+export function parsePowerShellChain(command: string): PowerShellChain {+ if (!command.includes("&&") && !command.includes("||")) return { kind: "none" };+ if (command.length > MAX_CHAIN_CHARS) return { kind: "unsupported" };+ const commands: string[] = [];+ const operators: Array<"&&" | "||"> = [];+ let quote = "";+ let start = 0;+ let unsupported = false;+ let commandHasContent = false;+ const environmentVariable = /\$env:[a-z_][a-z0-9_]*/iy;+ for (let i = 0; i < command.length; i++) {+ const ch = command[i];+ const atCommandStart = !commandHasContent;+ if (/\S/.test(ch)) commandHasContent = true;+ if (/[\r\n\0\u0085\u2028\u2029\u2018\u2019\u201c\u201d]/.test(ch)) unsupported = true;+ if (quote === "'") {+ if (ch === "'" && command[i + 1] === "'") i++;+ else if (ch === "'") quote = "";+ continue;+ }+ if (ch === "`") {+ // PowerShell escapes with backticks, not backslashes. Escapes outside+ // strings remain outside the supported grammar, but cannot split a chain.+ if (!quote || i + 1 >= command.length || /[\r\n]/.test(command[i + 1])) unsupported = true;+ i++;+ continue;+ }+ if (ch === "$") {+ environmentVariable.lastIndex = i;+ if (!environmentVariable.test(command)) unsupported = true;+ }+ if (quote === '"') {+ if (ch === '"' && command[i + 1] === '"') i++;+ else if (ch === '"') quote = "";+ continue;+ }+ if (ch === "'" || ch === '"') { quote = ch; continue; }+ if (ch === "#") { unsupported = true; break; }+ // Stop-parsing changes the meaning of all following operators. Smart+ // quotes above are also PowerShell delimiters outside this small grammar.+ if (command.startsWith("--%", i)) unsupported = true;+ if ((ch === "&" || ch === "|") && command[i + 1] === ch) {+ commands.push(command.slice(start, i).trim());+ operators.push(ch === "&" ? "&&" : "||");+ start = ++i + 1;+ commandHasContent = false;+ if (operators.length >= MAX_CHAIN_COMMANDS) return { kind: "unsupported" };+ continue;+ }+ if (";|<>(){}[]@".includes(ch)) unsupported = true;+ // A leading call operator supports quoted executable paths. A trailing+ // single ampersand would instead change the execution lifecycle.+ if (ch === "&" && !atCommandStart) unsupported = true;+ }+ if (!operators.length) return { kind: "none" };+ commands.push(command.slice(start).trim());+ if (quote || unsupported || commands.some(part => {+ const first = part.split(/\s/, 1)[0].toLowerCase();+ return !part || STATEMENT_KEYWORDS.has(first)+ || /^["'$@]/.test(part) || first === "." || part === "&";+ })) return { kind: "unsupported" };+ return { kind: "chain", commands, operators };+}++/** Lower a recognized chain inside the existing single PowerShell process.+ * Capture success immediately, retaining it when a branch is skipped. Clear+ * stale native status before each command so a successful cmdlet can recover+ * from a failed executable. Only the final selected command determines exit.+ */+export function compilePowerShellChain(chain: Extract<PowerShellChain, { kind: "chain" }>): string {+ const id = randomUUID().replace(/-/g, "");+ const succeeded = `$orkasChainSucceeded_${id}`;+ const exitCode = `$orkasChainExitCode_${id}`;+ const stage = (command: string) => [+ // NativeCommandProcessor writes the global automatic variable. A local+ // assignment here would shadow it inside the shell's existing & { } scope.+ "$global:LASTEXITCODE = 0",+ command,+ `${succeeded} = $?`,+ `${exitCode} = if (${succeeded}) { 0 } elseif ($global:LASTEXITCODE -ne 0) { $global:LASTEXITCODE } else { 1 }`,+ ].join("\n");+ const script = [stage(chain.commands[0])];+ for (let i = 1; i < chain.commands.length; i++) {+ const condition = chain.operators[i - 1] === "&&" ? succeeded : `-not ${succeeded}`;+ script.push(`if (${condition}) {\n${stage(chain.commands[i])}\n}`);+ }+ script.push(`exit ${exitCode}`);+ return script.join("\n");+}diff --git a/src/core-agent/src/tools/file-diagnostics.ts b/src/core-agent/src/tools/file-diagnostics.tsindex 63a12c3c..2cab4a3e 100644--- a/src/core-agent/src/tools/file-diagnostics.ts+++ b/src/core-agent/src/tools/file-diagnostics.ts@@ -6,6 +6,7 @@ const CODES = ["E_BAD_INPUT", "E_PATCH_FORMAT", "E_PATCH_TOO_LARGE", "E_PATCH_NO_MATCH", "E_PATCH_AMBIGUOUS","E_NO_MATCH", "E_MULTIPLE_MATCHES", "E_NOT_READ", "E_STALE", "E_REVISION_UNKNOWN","E_REVISION_PATH_MISMATCH", "E_NOT_DIRECTORY", "E_NOT_FOUND",+ "E_SKILL_REF_INVALID", "E_SKILL_NOT_AVAILABLE",] as const;const REASONS = ["patch_input", "patch_size", "patch_envelope", "file_limit", "file_header", "file_path",@@ -15,6 +16,7 @@ const REASONS = ["range_order", "append_precondition", "revision_unknown", "revision_path", "revision_changed","size_changed", "not_read", "file_changed", "expected_hash_changed", "target_type", "target_missing","target_stat",+ "skill_ref_format", "skill_ref_unbound",] as const;const COUNTS = ["line", "file_index", "hunk_index", "added_lines", "removed_lines", "context_lines", "item_index",@@ -24,6 +26,7 @@ const FLAGS = ["begin_marker", "end_marker", "start_present", "end_present", "start_integer", "end_integer","start_in_bounds", "ordered", "revision_provided", "revision_found", "revision_matches","size_provided", "size_valid", "baseline_present", "baseline_changed", "expected_hash_provided",+ "skill_ref_valid", "skill_binding_found",] as const;const ENUMS = {stage: ["input", "parse", "stat", "freshness", "match"],diff --git a/src/core-agent/src/tools/github-repository-fetch.ts b/src/core-agent/src/tools/github-repository-fetch.tsindex 43c13069..dffe872a 100644--- a/src/core-agent/src/tools/github-repository-fetch.ts+++ b/src/core-agent/src/tools/github-repository-fetch.ts@@ -190,11 +190,19 @@ export function compactGitHubReadme(readme: string): string {/** Fetch one deterministic repository snapshot with two parallel HTTP requests. */export async function fetchGitHubRepositorySnapshot(resource: GitHubRepositoryResource,+ signal?: AbortSignal,): Promise<ToolResult> {+ const cancelled = (): ToolResult => ({+ content: `Fetch of ${resource.canonicalUrl} was cancelled`,+ isError: true,+ });+ if (signal?.aborted) return cancelled();const metadataUrl =`https://api.github.com/repos/${encodeURIComponent(resource.owner)}/${encodeURIComponent(resource.repo)}`;const readmeUrl = `${metadataUrl}/readme`;const controller = new AbortController();+ const onAbort = () => controller.abort();+ signal?.addEventListener("abort", onAbort, { once: true });const timer = setTimeout(() => controller.abort(), DEFAULT_WEB_FETCH_TIMEOUT_MS);try {const [metadataResponse, readmeResponse] = await Promise.all([@@ -215,6 +223,7 @@ export async function fetchGitHubRepositorySnapshot(readWebFetchResponse(metadataResponse),readWebFetchResponse(readmeResponse),]);+ if (signal?.aborted) return cancelled();if ("error" in metadataBody && "error" in readmeBody) {return {@@ -297,6 +306,7 @@ export async function fetchGitHubRepositorySnapshot(].join("\n"),};} catch (error) {+ if (signal?.aborted) return cancelled();const message = error instanceof Error ? error.message : String(error);return {content: message.includes("abort")@@ -306,6 +316,9 @@ export async function fetchGitHubRepositorySnapshot(};} finally {clearTimeout(timer);+ signal?.removeEventListener("abort", onAbort);+ // A failed parallel request must not leave its sibling running after return.+ controller.abort();}}diff --git a/src/core-agent/src/tools/web-fetch.ts b/src/core-agent/src/tools/web-fetch.tsindex 62992af5..c76de6f8 100644--- a/src/core-agent/src/tools/web-fetch.ts+++ b/src/core-agent/src/tools/web-fetch.ts@@ -650,6 +650,9 @@ export const webFetchTool: AgentTool = defineTool({? fetchCache.get(`github:${githubResource.key}`): undefined;if (githubResource && githubResource.kind !== "repository" && priorGitHubSnapshot) {+ // A pending or failed root fetch is not successful source evidence.+ const result = await priorGitHubSnapshot.result;+ if (result.isError) return applyExplicitCharacterLimit(result, maxChars);log.info("github repository alias cache hit; skipped network request", {repository: githubResource.key,requestedKind: githubResource.kind,@@ -668,7 +671,7 @@ export const webFetchTool: AgentTool = defineTool({}const request = githubResource?.kind === "repository"- ? fetchGitHubRepositorySnapshot(githubResource)+ ? fetchGitHubRepositorySnapshot(githubResource, ctx.signal): fetchGeneralUrl(url, ctx.signal);const cacheEntry = { epoch, result: request };fetchCache.set(requestKey, cacheEntry);diff --git a/src/core-agent/test/agent-runner.test.ts b/src/core-agent/test/agent-runner.test.tsindex 4321bcc3..d15c3699 100644--- a/src/core-agent/test/agent-runner.test.ts+++ b/src/core-agent/test/agent-runner.test.ts@@ -94,6 +94,28 @@ describe("runner error metadata", () => {});describe("model history continuity", () => {+ it("carries real context-budget observations as metadata without adding them to model input", async () => {+ const requests: CompletionParams[] = [];+ const registry = new ProviderRegistry();+ registry.registerFactory("mock", () => createMockProvider([{+ model: 'mock-model', stopReason: 'end_turn', content: [{ type: 'text', text: 'Done' }],+ usage: { inputTokens: 10, outputTokens: 1, totalTokens: 11 },+ }], request => requests.push(request)));+ const runner = new AgentRunner({ providers: registry, session: new Session(), tools: [],+ config: createConfig({ agent: { defaultProvider: 'mock', defaultModel: 'mock-model' },+ models: { catalog: { 'mock-model': { provider: 'mock', model: 'mock-model', contextWindow: 272000, maxOutputTokens: 32000 } } },+ evolution: { enabled: false } }),+ });+ expect((await runner.run({ message: 'Hello' })).text).toBe('Done');+ expect(requests).toHaveLength(1);+ const budget = requests[0].requestMetadata?.contextBudgetDiagnostics;+ expect(budget).toMatchObject({ version: 1, budget_context_window: 272000, output_reservation: 32000,+ usable_input_tokens: 240000, input_ceiling_tokens: 196800, window_source: 'catalog',+ estimate_source: 'estimated', emergency_result: 'not_needed', compaction_attempts: 0, compaction_failures: 0 });+ expect((budget as any).estimated_input_tokens).toBeGreaterThan(0);+ expect((budget as any).emergency_after_tokens).toBe((budget as any).estimated_input_tokens);+ expect(JSON.stringify(requests[0].messages)).not.toContain('estimated_input_tokens');+ });it.each(["sequential", "parallel"] as const)("carries authoring references into later provider requests through %s tool execution", async executionMode => {const requests: CompletionParams[] = [];const usage = { inputTokens: 1, outputTokens: 1, totalTokens: 2 };@@ -3587,6 +3609,7 @@ describe("AgentRunner", () => {let summaryCalls = 0;let streamCalls = 0;const capturedRequests: Message[][] = [];+ const budgets: any[] = [];const provider: LLMProvider = {id: "mock",name: "Mock",@@ -3601,6 +3624,7 @@ describe("AgentRunner", () => {}const call = streamCalls++;capturedRequests.push([...params.messages]);+ budgets.push(params.requestMetadata?.contextBudgetDiagnostics);// Report usage consistent with the request actually received, like a// real provider: request-level decisions anchor on this real usage,// so a fixed tiny count would (correctly) read as a small request and@@ -3665,6 +3689,11 @@ describe("AgentRunner", () => {expect(reductions.length).toBeGreaterThan(0);const applied = reductions.filter((e) => e.data?.result === "applied");expect(applied.length).toBeGreaterThan(0);+ const reducedBudget = budgets.find(budget => budget?.emergency_result === 'applied');+ expect(reducedBudget).toBeDefined();+ expect(reducedBudget.emergency_after_tokens).toBeLessThan(reducedBudget.emergency_before_tokens);+ expect(reducedBudget.compaction_failures).toBeGreaterThan(0);+ expect(reducedBudget.estimated_input_tokens).toBe(reducedBudget.emergency_after_tokens);expect(Number(applied[0].data?.requestTokensAfter)).toBeLessThan(Number(applied[0].data?.requestTokensBefore));@@ -5311,6 +5340,7 @@ describe("AgentRunner", () => {it("checkpoints oversized active-turn tool process before the next model call", async () => {let completeCalls = 0;let streamCalls = 0;+ const budgets: any[] = [];let finalStreamMessages: Message[] = [];let checkpointPrompt = "";let checkpointSystemPrompt = "";@@ -5346,6 +5376,7 @@ describe("AgentRunner", () => {return;}const n = streamCalls++;+ budgets.push(params.requestMetadata?.contextBudgetDiagnostics);if (n > 0) {const latest = params.messages.flatMap(m => m.content).find(c => c.type === "tool_result" && c.toolUseId === `call-${n - 1}`);@@ -5398,6 +5429,11 @@ describe("AgentRunner", () => {for await (const ev of runner.runStream({ message: "large active process", systemPrompt: mainAgentPrompt })) events.push(ev);expect(completeCalls).toBe(1);+ const compacted = budgets.find(budget => budget?.compaction_after_tokens !== undefined);+ expect(compacted).toBeDefined();+ expect(compacted.compaction_after_tokens).toBeLessThan(compacted.compaction_before_tokens);+ expect(compacted.compaction_attempts).toBe(1);+ expect(compacted.compaction_failures).toBe(0);expect(checkpointSystemPrompt).toBe(CONTEXT_COMPACTION_SYSTEM_PROMPT);expect(checkpointSystemPrompt).not.toContain("MAIN_AGENT_ONLY");expect(checkpointParams?.reasoning).toBe("off");diff --git a/src/core-agent/test/github-repository-fetch.test.ts b/src/core-agent/test/github-repository-fetch.test.tsindex e2988611..1e2c1396 100644--- a/src/core-agent/test/github-repository-fetch.test.ts+++ b/src/core-agent/test/github-repository-fetch.test.ts@@ -47,9 +47,97 @@ function repositoryResource() {afterEach(() => {vi.unstubAllGlobals();+ vi.useRealTimers();});describe("GitHub repository web_fetch provider", () => {+ it.each([false, true])("does not claim cached source evidence after a denied repository fetch (in flight: %s)", async inFlight => {+ let release!: () => void;+ const gate = new Promise<void>(resolve => { release = resolve; });+ const fetchMock = vi.fn(async () => {+ if (inFlight) await gate;+ return textResponse("Forbidden", 403);+ });+ vi.stubGlobal("fetch", fetchMock);+ const webFetch = getBuiltinTools().find(tool => tool.name === "web_fetch")!;+ const context = { state: { runScopedLedger: new Map() } };+ const root = webFetch.execute({ url: REPOSITORY_URL }, context);+ if (!inFlight) await root;+ const alias = webFetch.execute({ url: README_URL }, context);+ release();+ expect((await root).isError).toBe(true);+ for (const result of [await alias, await webFetch.execute({ url: METADATA_URL }, context)]) {+ expect(result.isError).toBe(true);+ expect(result.content).toContain("HTTP 403");+ expect(result.content).not.toContain("GITHUB_REPOSITORY_SNAPSHOT_CACHE_HIT");+ }+ expect(fetchMock).toHaveBeenCalledTimes(2);+ });++ it("does not start repository requests after the task was cancelled", async () => {+ const fetchMock = vi.fn(async () => jsonResponse(metadata));+ vi.stubGlobal("fetch", fetchMock);+ const controller = new AbortController();+ controller.abort();+ const webFetch = getBuiltinTools().find(tool => tool.name === "web_fetch")!;+ const result = await webFetch.execute({ url: REPOSITORY_URL }, { state: {}, signal: controller.signal });+ expect(result.isError).toBe(true);+ expect(result.content).toContain("cancelled");+ expect(fetchMock).not.toHaveBeenCalled();+ });++ it("releases the sibling request when one parallel request fails", async () => {+ let siblingSignal: AbortSignal | undefined;+ vi.stubGlobal("fetch", vi.fn((input: string | URL | Request, init: RequestInit) => {+ if (requestUrl(input) === METADATA_URL) return Promise.reject(new Error("fetch failed"));+ siblingSignal = init.signal!;+ return new Promise<Response>((_resolve, reject) => {+ siblingSignal!.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")), { once: true });+ });+ }));+ const result = await fetchGitHubRepositorySnapshot(repositoryResource());+ expect(result.isError).toBe(true);+ expect(siblingSignal?.aborted).toBe(true);+ });++ it.each(["headers", "body"])("cancels both repository requests while waiting for %s", async stage => {+ vi.useFakeTimers();+ const signals: AbortSignal[] = [];+ const fetchMock = vi.fn((_input: unknown, init: RequestInit) => {+ const signal = init.signal!;+ signals.push(signal);+ if (stage === "headers") {+ return new Promise<Response>((_resolve, reject) => {+ signal.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")), { once: true });+ });+ }+ return Promise.resolve(new Response(new ReadableStream({+ start(stream) {+ signal.addEventListener("abort", () => stream.error(new DOMException("aborted", "AbortError")), { once: true });+ },+ })));+ });+ vi.stubGlobal("fetch", fetchMock);+ const controller = new AbortController();+ const webFetch = getBuiltinTools().find(tool => tool.name === "web_fetch")!;+ const pending = webFetch.execute({ url: REPOSITORY_URL }, { state: {}, signal: controller.signal });+ await vi.advanceTimersByTimeAsync(0);+ controller.abort();+ try {+ expect(signals).toHaveLength(2);+ expect(signals.every(signal => signal.aborted)).toBe(true);+ const result = await pending;+ expect(result.isError).toBe(true);+ expect(result.content).toContain("cancelled");+ expect(result.content).not.toContain("Timeout");+ expect(vi.getTimerCount()).toBe(0);+ } finally {+ // Also release the pre-fix implementation through its existing deadline.+ await vi.advanceTimersByTimeAsync(60_000);+ await pending;+ }+ });+it("keeps metadata when the README request fails", async () => {const fetchMock = vi.fn(async (input: string | URL | Request) => {const url = requestUrl(input);diff --git a/src/core-agent/test/powershell-chain.test.ts b/src/core-agent/test/powershell-chain.test.tsnew file mode 100644index 00000000..7ce3c482--- /dev/null+++ b/src/core-agent/test/powershell-chain.test.ts@@ -0,0 +1,145 @@+import { describe, expect, it } from "vitest";+import { parsePowerShellChain } from "../src/sandbox/powershell-chain.js";+import { buildShellInvocation, SandboxExecutor } from "../src/sandbox/executor.js";+import fs from "node:fs/promises";+import os from "node:os";+import path from "node:path";++describe("Windows simple command chains", () => {+ it("accepts the bounded 32-command limit", () => {+ const chain = parsePowerShellChain(Array(32).fill('Write-Output ok').join(' && '));+ expect(chain.kind).toBe('chain');+ if (chain.kind === 'chain') expect(chain.commands).toHaveLength(32);+ });+ it.each([+ ['npm install && npm test', ['npm install', 'npm test'], ['&&']],+ ['first || second && third', ['first', 'second', 'third'], ['||', '&&']],+ [`Write-Output 'a && b' && Write-Output 'it''s || literal'`,+ [`Write-Output 'a && b'`, `Write-Output 'it''s || literal'`], ['&&']],+ ['& "C:\\tool path\\node.exe" -v && Write-Output "C:\\data\\"',+ ['& "C:\\tool path\\node.exe" -v', 'Write-Output "C:\\data\\"'], ['&&']],+ ['Write-Output "a `" && b" || Write-Output "$env:TEMP"',+ ['Write-Output "a `" && b"', 'Write-Output "$env:TEMP"'], ['||']],+ ])("preserves command bytes and operator order: %s", (source, commands, operators) => {+ expect(parsePowerShellChain(source)).toEqual({ kind: "chain", commands, operators });+ });++ it.each([+ "Write-Output '&& ||'", 'Write-Output "&& ||"',+ "Write-Output 'a'' && b'", "Write-Output done # && is a comment",+ ])("leaves literal operators alone: %s", source => {+ expect(parsePowerShellChain(source)).toEqual({ kind: "none" });+ expect(buildShellInvocation("powershell.exe", source, "win32").args.at(-1)).toContain(`\n${source}\n`);+ });++ it.each([+ 'first &&', '&& second', 'first || || second', 'first && "unterminated',+ 'first && (second)', 'first && $result', 'first && Write-Output "$(second)"',+ 'first && Write-Output "$LASTEXITCODE"', 'first && second; third',+ 'first && second\nthird', 'first && second | third', 'first && second > out.txt',+ 'first && second &', 'first && . script.ps1', 'first && exit 0',+ 'first && if($true) { second }', 'first && second # comment',+ 'first && @"\ntext\n"@', 'first && second`\nthird',+ 'first && Write-Output @(1)', 'first && [Console]::WriteLine(1)',+ 'first && Write-Output "nul\0"', 'first && ' + 'x'.repeat(65_536),+ 'Write-Output --% literal && second', 'Write-Output “literal && data”',+ 'first && Write-Output "smart” || hidden “quote"',+ Array(33).fill('first').join(' && '),+ ])("rejects incomplete, compound or oversized chains", source => {+ expect(parsePowerShellChain(source)).toEqual({ kind: "unsupported" });+ });++ it("lowers only the legacy Windows PowerShell invocation", () => {+ const source = "first && second || third";+ for (const [shell, platform] of [+ ["pwsh.exe", "win32"], ["cmd.exe", "win32"], ["bash.exe", "win32"], ["/bin/sh", "darwin"],+ ] as const) {+ expect(buildShellInvocation(shell, source, platform).args.at(-1)).toContain(source);+ }+ const script = buildShellInvocation("powershell.exe", source, "win32").args.at(-1)!;+ const status = script.match(/\$(orkasChainSucceeded_[a-f0-9]{32})/)!;+ expect(status).not.toBeNull();+ expect(script).toContain(`first\n$${status[1]} = $?`);+ expect(script).toContain(`if ($${status[1]}) {\n$global:LASTEXITCODE = 0\nsecond`);+ expect(script).toContain(`if (-not $${status[1]}) {\n$global:LASTEXITCODE = 0\nthird`);+ expect(script).not.toContain('\n$LASTEXITCODE =');+ expect(script).toContain('elseif ($global:LASTEXITCODE -ne 0) { $global:LASTEXITCODE } else { 1 }');+ expect(script.match(/^first$|^second$|^third$/gm)).toEqual(['first', 'second', 'third']);+ expect(script).toMatch(/exit \$orkasChainExitCode_[a-f0-9]{32}/);+ });+});++// These cases need Windows PowerShell 5.1, not a POSIX emulator or pwsh 7.+// Observe real output/exit codes to catch skipped-if status resets and stale+// native failure codes; pure parsing checks cannot certify those semantics.+describe.runIf(process.platform === "win32")("native Windows PowerShell chain outcomes", () => {+ const quote = (value: string) => `'${value.replace(/'/g, "''")}'`;+ const node = process.env.ORKAS_TEST_NODE || process.execPath;+ const command = (label: string, code: number) =>+ `& ${quote(node)} -e ${quote(`console.log('${label}');process.exit(${code})`)}`;++ it.each([+ [0, '&&', 0, '&&', 0, 'A B C', 0],+ [7, '&&', 0, '&&', 0, 'A', 7],+ [0, '||', 9, '||', 8, 'A', 0],+ [7, '||', 0, '||', 8, 'A B', 0],+ [7, '&&', 9, '||', 0, 'A C', 0],+ [0, '||', 9, '&&', 0, 'A C', 0],+ [0, '&&', 8, '||', 9, 'A B C', 9],+ [7, '||', 8, '&&', 0, 'A B', 8],+ ])("preserves short circuit and final status (%s %s %s %s %s)", async (a, op1, b, op2, c, output, code) => {+ const sandbox = new SandboxExecutor({ workingDir: os.tmpdir(), shell: "powershell.exe" });+ const result = await sandbox.execute(`${command('A', a as number)} ${op1} ${command('B', b as number)} ${op2} ${command('C', c as number)}`);+ expect(result.stdout.trim().split(/\s+/).join(' ')).toBe(output);+ expect(result.stderr).toBe('');+ expect(result.exitCode).toBe(code);+ }, 25_000);++ it("clears a recovered native error and preserves literal operators", async () => {+ const sandbox = new SandboxExecutor({ workingDir: os.tmpdir(), shell: "powershell.exe" });+ const result = await sandbox.execute(`${command('A', 7)} || Write-Output 'recovered && ||'`);+ expect(result.stdout.trim().split(/\r?\n/)).toEqual(['A', 'recovered && ||']);+ expect(result.exitCode).toBe(0);+ expect(result.stderr).toBe('');+ }, 25_000);++ it.each([+ ['&&', '', 1], ['||', 'recovered', 0],+ ])("uses cmdlet failure for %s short circuit", async (operator, output, exitCode) => {+ const sandbox = new SandboxExecutor({ workingDir: os.tmpdir(), shell: "powershell.exe" });+ const result = await sandbox.execute(`Write-Error 'chain-test-error' ${operator} Write-Output recovered`);+ expect(result.stdout.trim()).toBe(output);+ expect(result.stderr).toContain('chain-test-error');+ expect(result.exitCode).toBe(exitCode);+ }, 25_000);++ it("cancels a running chain without starting its remaining command", async () => {+ const controller = new AbortController();+ const sandbox = new SandboxExecutor({+ workingDir: os.tmpdir(), shell: "powershell.exe", signal: controller.signal, timeoutMs: 10_000,+ });+ const source = `& ${quote(node)} -e ${quote("console.log('READY');setInterval(()=>{},1000)")} && Write-Output MUST-NOT-RUN`;+ const result = await sandbox.execute(source, {+ onStart() {},+ onOutput(bytes, stream) {+ if (stream === 'stdout' && bytes.toString().includes('READY')) controller.abort();+ },+ });+ expect(result.stdout).toContain('READY');+ expect(result.stdout).not.toContain('MUST-NOT-RUN');+ expect(result.aborted).toBe(true);+ expect(result.timedOut).toBe(false);+ }, 25_000);++ it("retains cwd and environment in the same process", async () => {+ const cwd = await fs.mkdtemp(path.join(os.tmpdir(), 'orkas-chain-'));+ try {+ const sandbox = new SandboxExecutor({ workingDir: os.tmpdir(), shell: "powershell.exe" });+ const source = `Set-Location ${quote(cwd)} && Set-Item Env:ORKAS_CHAIN_TEST kept && & ${quote(node)} -e ${quote('console.log(JSON.stringify([process.cwd(),process.env.ORKAS_CHAIN_TEST]))')}`;+ const result = await sandbox.execute(source);+ expect(result.exitCode).toBe(0);+ expect(result.stderr).toBe('');+ expect(JSON.parse(result.stdout.trim())).toEqual([cwd, 'kept']);+ } finally { await fs.rm(cwd, { recursive: true, force: true }); }+ }, 25_000);+});diff --git a/src/core-agent/test/tool-input-diagnostics.test.ts b/src/core-agent/test/tool-input-diagnostics.test.tsnew file mode 100644index 00000000..053f28ce--- /dev/null+++ b/src/core-agent/test/tool-input-diagnostics.test.ts@@ -0,0 +1,101 @@+import { afterEach, expect, it, vi } from 'vitest';+import { ToolInputDiagnostics, toolInputDiagnostic } from '../src/providers/tool-input-diagnostics.js';+import { createPiProvider } from '../src/providers/pi-provider.js';+import { Session } from '../src/agent/session.js';+import { AgentRunner } from '../src/agent/runner.js';+import { ProviderRegistry } from '../src/providers/registry.js';+import { createConfig } from '../src/config/loader.js';+import { defineTool } from '../src/tools/base.js';+import { fileFailureForLog } from '../src/tools/file-diagnostics.js';++afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); });++function completions(argumentsText?: string): Response {+ const chunks = argumentsText === undefined+ ? [{ choices: [{ index: 0, delta: { content: 'Inspected.' }, finish_reason: 'stop' }] }]+ : [+ { choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: 'fixture-call', type: 'function', function: { name: 'read_files', arguments: argumentsText.slice(0, 17) } }] }, finish_reason: null }] },+ { choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: argumentsText.slice(17) } }] }, finish_reason: 'tool_calls' }] },+ ];+ return new Response(chunks.map(chunk => `data: ${JSON.stringify({ id: 'fixture', model: 'fixture', ...chunk })}\n\n`).join('') + 'data: [DONE]\n\n', { headers: { 'content-type': 'text/event-stream' } });+}++it.each([false, true])('keeps strict/partial input evidence through the real SDK and runner (partial=%s)', async partial => {+ const input = { paths: [{ path: '@skill/skill-creator},{' }] };+ const json = JSON.stringify(input);+ const wire = partial ? json.slice(0, -1) : json;+ let requests = 0;+ vi.stubGlobal('fetch', vi.fn(async () => completions(requests++ === 0 ? wire : undefined)));+ const warning = vi.spyOn(console, 'warn').mockImplementation(() => {});+ const provider = createPiProvider({ provider: 'openai', apiKey: 'fixture', customModel: {+ id: 'fixture', name: 'fixture', api: 'openai-completions', provider: 'openai', baseUrl: 'https://fixture.invalid/v1',+ reasoning: false, input: ['text'], contextWindow: 128_000, maxTokens: 4096,+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },+ } });+ const registry = new ProviderRegistry();+ registry.registerFactory('fixture', () => provider);+ const execute = vi.fn(async (received: Record<string, unknown>) => {+ expect(received).toEqual(input);+ expect(toolInputDiagnostic(received)).toMatchObject({+ stream_json: partial ? 'non_strict' : 'strict', tolerant_parse: partial ? 'used' : 'not_used',+ });+ return { content: 'E_SKILL_REF_INVALID: Use the advertised reference.', isError: true,+ observations: { fileFailure: { code: 'E_SKILL_REF_INVALID' as const, reason: 'skill_ref_format' as const, skill_ref_valid: false } } };+ });+ const session = new Session();+ const runner = new AgentRunner({ session, config: createConfig({ agent: { defaultProvider: 'fixture', defaultModel: 'fixture' } }), providers: registry,+ tools: [defineTool({ name: 'read_files', description: 'Read files.', inputSchema: { type: 'object' }, execute })] });+ await runner.run({ message: 'Inspect the protocol.' });+ expect(execute).toHaveBeenCalledOnce();+ expect(requests).toBe(2);+ const errors = warning.mock.calls.filter(call => call[1] === 'Tool returned error');+ expect(errors).toHaveLength(1);+ expect(errors[0][2]).toMatchObject({ input_parse: { stream_json: partial ? 'non_strict' : 'strict', tolerant_parse: partial ? 'used' : 'not_used' }, file_failure: { code: 'E_SKILL_REF_INVALID', skill_ref_valid: false } });+ expect(JSON.stringify(warning.mock.calls)).not.toContain('skill-creator');+ expect(JSON.stringify(session.getMessagesForModel())).not.toContain('tolerant_parse');+});++it('keeps snapshot transports and absent input unknown instead of claiming no repair', () => {+ const observer = new ToolInputDiagnostics(false);+ observer.start(0);+ observer.delta(0, '{"value":1}');+ const replaced = { value: 2 };+ observer.end(0, replaced);+ expect(toolInputDiagnostic(replaced)).toEqual({ stream_json: 'strict', tolerant_parse: 'unknown', parsed_input_matches: false });+ const unobserved = {};+ observer.end(1, unobserved);+ expect(toolInputDiagnostic(unobserved)).toEqual({ stream_json: 'unobserved', tolerant_parse: 'unknown' });+ const partial = {};+ observer.start(2); observer.delta(2, '{'); observer.end(2, partial);+ expect(toolInputDiagnostic(partial)).toEqual({ stream_json: 'non_strict', tolerant_parse: 'unknown' });+ // The SDK treats whitespace as absent input, without entering JSON repair.+ const completionsObserver = new ToolInputDiagnostics(true);+ completionsObserver.start(0); completionsObserver.delta(0, ' \n ');+ const empty = {}; completionsObserver.end(0, empty);+ expect(toolInputDiagnostic(empty)).toEqual({ stream_json: 'unobserved', tolerant_parse: 'unknown' });+});++it('isolates interleaved inputs and bounds large inputs, total volume and call storms', () => {+ const observer = new ToolInputDiagnostics(true);+ observer.start(0); observer.start(1);+ observer.delta(0, '{"value":'); observer.delta(1, '{"other":2}'); observer.delta(0, '1}');+ const first = { value: 1 }, second = { other: 2 };+ observer.end(1, second); observer.end(0, first);+ expect(toolInputDiagnostic(first)).toMatchObject({ stream_json: 'strict', parsed_input_matches: true });+ expect(toolInputDiagnostic(second)).toMatchObject({ stream_json: 'strict', parsed_input_matches: true });+ observer.start(2); observer.delta(2, 'x'.repeat(32_769));+ const large = {}; observer.end(2, large);+ expect(toolInputDiagnostic(large)).toEqual({ stream_json: 'limit', tolerant_parse: 'unknown' });+ for (let index = 3; index < 8; index++) {+ observer.start(index); observer.delta(index, JSON.stringify({ body: 'x'.repeat(32_700) }));+ const input = { body: 'x'.repeat(32_700) }; observer.end(index, input);+ if (index === 7) expect(toolInputDiagnostic(input)).toEqual({ stream_json: 'limit', tolerant_parse: 'unknown' });+ }+ for (let index = 8; index < 40; index++) observer.start(index);+ observer.delta(39, '{}'); const overflow = {}; observer.end(39, overflow);+ expect(toolInputDiagnostic(overflow)).toEqual({ stream_json: 'unobserved', tolerant_parse: 'unknown' });+});++it('allows only closed diagnostic facts into logs', () => {+ expect(fileFailureForLog({ code: 'E_SKILL_REF_INVALID', reason: 'skill_ref_format', stage: 'input', skill_ref_valid: false, skill_binding_found: 'secret', requested_path: '/private/secret' })).toEqual({ code: 'E_SKILL_REF_INVALID', reason: 'skill_ref_format', stage: 'input', skill_ref_valid: false });+});diff --git a/src/main/features/chat_artifacts.ts b/src/main/features/chat_artifacts.tsindex a5ec9d0c..c7ddb544 100644--- a/src/main/features/chat_artifacts.ts+++ b/src/main/features/chat_artifacts.ts@@ -29,12 +29,11 @@* `<cid>/<artifactId>/` (belt-and-braces anti-traversal)* - file must exist and be a regular file*- * If binary assets larger than the per-file cap are ever needed, the right- * extension is base64-encoded content (already supported below) — never widen- * the served allowlist to executable types.*/import * as fs from 'node:fs';+import * as fsp from 'node:fs/promises';+import { setImmediate as yieldToHost } from 'node:timers/promises';import * as path from 'node:path';import * as crypto from 'node:crypto';@@ -49,25 +48,21 @@ import { isPathAllowed } from '../util/path-sandbox';const log = createLogger('chat_artifacts');-// ── Caps & extension allowlists ──────────────────────────────────────────--export const MAX_ARTIFACT_FILES = 20;-export const MAX_BYTES_PER_FILE = 256 * 1024; // 256 KB-export const MAX_BYTES_TOTAL = 1 * 1024 * 1024; // 1 MB-const MAX_RELPATH_LEN = 200;+// ── Paths & extension allowlists ──────────────────────────────────────────// What the `chat-app://` handler is willing to serve. Web assets only — no// executables, no archives.-const SERVED_EXTS: ReadonlySet<string> = new Set([+export const SERVED_EXTS: ReadonlySet<string> = new Set(['.html', '.htm', '.js', '.mjs', '.css', '.json', '.map', '.svg', '.xml', '.txt','.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.bmp','.wasm', '.woff', '.woff2', '.ttf', '.otf', '.csv',+ '.avif', '.webmanifest', '.mp3', '.wav', '.ogg', '.mp4', '.webm', '.glb', '.gltf', '.md',]);// Extensions whose content is text (so `create_artifact` can validate UTF-8// for utf8-encoded files, and which is the natural shape for hand-written app// code). Anything else must be supplied as base64.-const TEXT_EXTS: ReadonlySet<string> = new Set([- '.html', '.htm', '.js', '.mjs', '.css', '.json', '.map', '.svg', '.xml', '.txt', '.csv',+export const TEXT_EXTS: ReadonlySet<string> = new Set([+ '.html', '.htm', '.js', '.mjs', '.css', '.json', '.map', '.svg', '.xml', '.txt', '.csv', '.webmanifest', '.gltf', '.md',]);const META_FILENAME = '__orkas-meta.json';@@ -152,7 +147,6 @@ function safeRelPath(rel: unknown): string {let s = rel.trim();if (s.startsWith('/')) s = s.slice(1);if (!s) throw new Error('empty relpath');- if (s.length > MAX_RELPATH_LEN) throw new Error('relpath too long');if (s.includes('\x00') || s.includes('\\')) throw new Error('invalid relpath');const segs = s.split('/');for (const seg of segs) {@@ -201,6 +195,16 @@ export function mimeFor(name: string): string {case '.js': case '.mjs': return 'text/javascript; charset=utf-8';case '.css': return 'text/css; charset=utf-8';case '.json': case '.map': return 'application/json; charset=utf-8';+ case '.webmanifest': return 'application/manifest+json';+ case '.avif': return 'image/avif';+ case '.mp3': return 'audio/mpeg';+ case '.wav': return 'audio/wav';+ case '.ogg': return 'audio/ogg';+ case '.mp4': return 'video/mp4';+ case '.webm': return 'video/webm';+ case '.glb': return 'model/gltf-binary';+ case '.gltf': return 'model/gltf+json';+ case '.md': return 'text/markdown; charset=utf-8';case '.svg': return 'image/svg+xml';case '.xml': return 'application/xml; charset=utf-8';case '.txt': case '.csv': return 'text/plain; charset=utf-8';@@ -293,17 +297,16 @@ export function ensureBridgeScript(html: string): string {/*** Write a new artifact bundle for (uid, cid, agentId). Validates the file set- * (must include exactly one top-level `index.html`; per-file + total + count- * caps; extension allowlist; UTF-8 for utf8-encoded text files; relpath+ * (must include exactly one top-level `index.html`; extension allowlist; UTF-8 for utf8-encoded text files; relpath* safety; no `__orkas/` or `__orkas-meta.json` clobber), writes atomically* (temp dir → rename), and stamps `__orkas-meta.json`.*/-export function createArtifact(+export async function createArtifact(userId: string,cid: string,agentId: string,input: { title?: unknown; files?: unknown },-): Result<{ artifactId: string; title: string }> {+): Promise<Result<{ artifactId: string; title: string }>> {let safeConvId: string;try { safeConvId = safeCid(cid); }catch (err) { return { ok: false, error: (err as Error).message }; }@@ -315,13 +318,9 @@ export function createArtifact(if (!Array.isArray(input?.files) || input.files.length === 0) {return { ok: false, error: 'files: required, must be a non-empty array of { path, content }' };}- if (input.files.length > MAX_ARTIFACT_FILES) {- return { ok: false, error: `too many files (max ${MAX_ARTIFACT_FILES})` };- }-- // Validate + materialise every file in memory first; only touch disk once- // the whole set checks out.- const prepared: Array<{ rel: string; buf: Buffer }> = [];+ // Validate names before staging. Retain source strings, not a decoded copy+ // of every asset; decode and write one file at a time below.+ const prepared: Array<{ rel: string; content: string; encoding: 'base64' | 'utf8' }> = [];const seen = new Set<string>();let hasIndex = false;let totalBytes = 0;@@ -346,28 +345,12 @@ export function createArtifact(if (encoding === 'utf8' && !TEXT_EXTS.has(ext)) {return { ok: false, error: `file "${rel}": ${ext} content must be base64-encoded (set "encoding":"base64")` };}- let buf: Buffer;- if (encoding === 'base64') {- if (!/^[A-Za-z0-9+/_\-=\s]*$/.test(f.content)) {- return { ok: false, error: `file "${rel}": invalid base64 content` };- }- buf = Buffer.from(f.content, 'base64'); // tolerant of url-safe alphabet + whitespace- } else {- buf = Buffer.from(f.content, 'utf8');- if (buf.toString('utf8') !== f.content) return { ok: false, error: `file "${rel}": content is not valid UTF-8` };- // Before the size accounting below, so the caps still describe what- // actually lands on disk.- if (rel === 'index.html') buf = Buffer.from(ensureBridgeScript(f.content), 'utf8');- }- if (buf.length > MAX_BYTES_PER_FILE) {- return { ok: false, error: `file "${rel}": exceeds ${Math.round(MAX_BYTES_PER_FILE / 1024)}KB per-file cap` };- }- totalBytes += buf.length;- if (totalBytes > MAX_BYTES_TOTAL) {- return { ok: false, error: `bundle exceeds ${Math.round(MAX_BYTES_TOTAL / 1024)}KB total cap` };+ if (encoding === 'base64' && !/^[A-Za-z0-9+/_\-=\s]*$/.test(f.content)) {+ return { ok: false, error: `file "${rel}": invalid base64 content` };}if (rel === 'index.html') hasIndex = true;- prepared.push({ rel, buf });+ prepared.push({ rel, content: f.content, encoding });+ if (prepared.length % 256 === 0) await yieldToHost();}if (!hasIndex) return { ok: false, error: 'files must include a top-level "index.html"' };@@ -391,17 +374,23 @@ export function createArtifact(const tmpDir = `${finalDir}.tmp-${crypto.randomBytes(4).toString('hex')}`;try {- for (const { rel, buf } of prepared) {+ for (const { rel, content, encoding } of prepared) {+ let buf = Buffer.from(content, encoding);+ if (encoding === 'utf8') {+ if (buf.toString('utf8') !== content) throw new Error(`file "${rel}": content is not valid UTF-8`);+ if (rel === 'index.html') buf = Buffer.from(ensureBridgeScript(content), 'utf8');+ }const dst = path.join(tmpDir, rel);- fs.mkdirSync(path.dirname(dst), { recursive: true });- fs.writeFileSync(dst, buf);+ await fsp.mkdir(path.dirname(dst), { recursive: true });+ await fsp.writeFile(dst, buf);+ totalBytes += buf.length;}- fs.writeFileSync(path.join(tmpDir, META_FILENAME), metaBuf);- fs.mkdirSync(path.dirname(finalDir), { recursive: true });- fs.renameSync(tmpDir, finalDir);+ await fsp.writeFile(path.join(tmpDir, META_FILENAME), metaBuf);+ await fsp.mkdir(path.dirname(finalDir), { recursive: true });+ await fsp.rename(tmpDir, finalDir);} catch (err) {- try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ }- try { fs.rmSync(finalDir, { recursive: true, force: true }); } catch { /* best-effort */ }+ try { await fsp.rm(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ }+ try { await fsp.rm(finalDir, { recursive: true, force: true }); } catch { /* best-effort */ }log.warn(`createArtifact failed user=${userId} cid=${safeConvId}: ${(err as Error).message}`);return { ok: false, error: `failed to write artifact: ${(err as Error).message}` };}@@ -537,8 +526,7 @@ export interface ArtifactSummary {* a `create_artifact` app could not be found again.** Best-effort: an unreadable or malformed entry is skipped rather than failing- * the whole listing, and the caps here are the same ones `createArtifact`- * enforces on write.+ * the whole listing.*/export function listArtifacts(userId: string, cid: string): ArtifactSummary[] {let safeConvId: string;diff --git a/src/main/features/chat_attachments.ts b/src/main/features/chat_attachments.tsindex c3d3497d..cdf7d8d5 100644--- a/src/main/features/chat_attachments.ts+++ b/src/main/features/chat_attachments.ts@@ -21,7 +21,7 @@* (`main_chat`, `projchat-*`) are local-only until* adoptDraftAttachments moves them into a real cid.* PDF/DOCX/- * XLSX/PPTX extract + image grayscale happen lazily on read via+ * XLSX/PPTX extraction and color image compression happen lazily on read via* features/file_indexer (which caches under* <uid>/local/file_cache/<hash>/). Video is kept raw* and never cached.@@ -71,16 +71,22 @@ const log = createLogger('chat_attachments');// ── Whitelists & caps (aligned with features/contexts) ────────────────────const TEXT_EXTS: ReadonlySet<string> = new Set([- '.md', '.markdown', '.txt', '.csv', '.tsv',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt','.json', '.yaml', '.yml', '.log',+ '.html', '.htm', '.xml', '.toml', '.ini', '.conf',+ '.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs',+ '.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt',+ '.c', '.cpp', '.cc', '.h', '.hpp', '.css', '.scss', '.less',+ '.sql', '.graphql', '.gql',]);const IMAGE_EXTS: ReadonlySet<string> = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif']);-// SVG is intentionally display-only. Keep it out of IMAGE_EXTS so attachment+// Additional browser formats are display-only. Keep them out of IMAGE_EXTS so attachment// upload, model vision input, image transforms, and per-cid attachment serving// retain their existing raster-only contract.-const LOCAL_DISPLAY_IMAGE_EXTS: ReadonlySet<string> = new Set([...IMAGE_EXTS, '.svg']);+const LOCAL_DISPLAY_IMAGE_EXTS: ReadonlySet<string> = new Set([...IMAGE_EXTS, '.apng', '.jpe', '.jfif', '.avif', '.bmp', '.ico', '.svg']);const VIDEO_EXTS: ReadonlySet<string> = new Set(['.mp4', '.webm', '.mov', '.m4v', '.ogv']);const AUDIO_EXTS: ReadonlySet<string> = new Set(['.mp3', '.wav', '.ogg', '.opus', '.m4a', '.aac', '.flac']);+const LOCAL_DISPLAY_AUDIO_EXTS: ReadonlySet<string> = new Set([...AUDIO_EXTS, '.oga', '.weba']);const PDF_EXT = '.pdf';const DOCX_EXTS: ReadonlySet<string> = new Set(['.docx', '.docm']);const SPREADSHEET_EXTS: ReadonlySet<string> = new Set(['.xlsx', '.xlsm', '.xls']);@@ -854,14 +860,14 @@ export async function diagnoseMediaFile(} catch { return { diagnosis: 'bad_input' }; }}const ext = path.extname(abs).toLowerCase();- if (local && !LOCAL_DISPLAY_IMAGE_EXTS.has(ext) && !VIDEO_EXTS.has(ext) && !AUDIO_EXTS.has(ext)) {+ if (local && !LOCAL_DISPLAY_IMAGE_EXTS.has(ext) && !VIDEO_EXTS.has(ext) && !LOCAL_DISPLAY_AUDIO_EXTS.has(ext)) {return { diagnosis: 'bad_input' };}- const media_kind = LOCAL_DISPLAY_IMAGE_EXTS.has(ext) ? 'image' : kindOf(ext);+ const media_kind = LOCAL_DISPLAY_IMAGE_EXTS.has(ext) ? 'image' : (LOCAL_DISPLAY_AUDIO_EXTS.has(ext) ? 'audio' : kindOf(ext));try {const stat = await fs.promises.stat(abs);if (!stat.isFile()) return { diagnosis: 'not_file', media_kind };- const cap = LOCAL_DISPLAY_IMAGE_EXTS.has(ext) ? MAX_BYTES_IMAGE : maxBytesFor(ext);+ const cap = LOCAL_DISPLAY_IMAGE_EXTS.has(ext) ? MAX_BYTES_IMAGE : (LOCAL_DISPLAY_AUDIO_EXTS.has(ext) ? MAX_BYTES_AUDIO : maxBytesFor(ext));if (local && stat.size > cap) return { diagnosis: 'too_large', media_kind };return { diagnosis: 'stat_available', media_kind };} catch (error) {@@ -936,14 +942,14 @@ export function resolveLocalMediaPath(}const normalized = path.resolve(absPath);const ext = path.extname(normalized).toLowerCase();- if (!LOCAL_DISPLAY_IMAGE_EXTS.has(ext) && !VIDEO_EXTS.has(ext) && !AUDIO_EXTS.has(ext)) {+ if (!LOCAL_DISPLAY_IMAGE_EXTS.has(ext) && !VIDEO_EXTS.has(ext) && !LOCAL_DISPLAY_AUDIO_EXTS.has(ext)) {return { ok: false, code: 'bad_input', error: `unsupported extension: ${ext || '(none)'}` };}let stat: fs.Stats;try { stat = fs.statSync(normalized); }catch (error) { return { ok: false, code: 'not_found', error: 'not found', diagnosticCode: mediaFileFailureCode(error) }; }if (!stat.isFile()) return { ok: false, code: 'not_found', error: 'not a file', diagnosticCode: 'not_file' };- const cap = LOCAL_DISPLAY_IMAGE_EXTS.has(ext) ? MAX_BYTES_IMAGE : maxBytesFor(ext);+ const cap = LOCAL_DISPLAY_IMAGE_EXTS.has(ext) ? MAX_BYTES_IMAGE : (LOCAL_DISPLAY_AUDIO_EXTS.has(ext) ? MAX_BYTES_AUDIO : maxBytesFor(ext));if (stat.size > cap) {const mb = Math.round(cap / 1024 / 1024);return { ok: false, code: 'too_large', error: `file exceeds ${mb}MB cap` };@@ -970,7 +976,7 @@ export function resolveLocalMediaPath(return { ok: false, code: 'bad_input', error: 'unsafe SVG content' };}}- const kind = VIDEO_EXTS.has(ext) ? 'video' : (AUDIO_EXTS.has(ext) ? 'audio' : 'image');+ const kind = VIDEO_EXTS.has(ext) ? 'video' : (LOCAL_DISPLAY_AUDIO_EXTS.has(ext) ? 'audio' : 'image');return { ok: true, absPath: normalized, kind };}@@ -1112,10 +1118,16 @@ export function mediaMimeFor(name: string): string {return 'application/octet-stream';}-/** MIME lookup for `chat-media://local`. SVG support is deliberately kept+/** MIME lookup for `chat-media://local`. Browser display aliases are kept* separate from `mediaMimeFor`, which is also used by per-cid attachments. */export function localMediaMimeFor(name: string): string {- if (path.extname(name).toLowerCase() === '.svg') return 'image/svg+xml';+ const aliases: Record<string, string> = {+ '.apng': 'image/apng', '.jpe': 'image/jpeg', '.jfif': 'image/jpeg',+ '.avif': 'image/avif', '.bmp': 'image/bmp', '.ico': 'image/x-icon',+ '.svg': 'image/svg+xml', '.oga': 'audio/ogg', '.weba': 'audio/webm',+ };+ const alias = aliases[path.extname(name).toLowerCase()];+ if (alias) return alias;return mediaMimeFor(name);}@@ -1244,7 +1256,7 @@ export interface AttachmentManifest {* attachments are attached. The model uses this as a directory listing* and calls the on-demand file tools for content. */manifest: string;- /** Compressed gray JPEG images formatted for `ChatOptions.images` (pi-ai+ /** Compressed color JPEG images formatted for `ChatOptions.images` (pi-ai* ImageContent). Produced in real time on each call — no cache. */images: Array<{ data: string; mediaType: ImageMimeType }>;/** Names of attachments we couldn't use (missing / too big / load error). */@@ -1284,7 +1296,7 @@ export interface BuildConversationAttachmentIndexOpts {* Otherwise `total_chars` is omitted and the model must* let read_files prepare extraction. Never eagerly extract* here — upload stays zero-cost.- * - image → compressed grayscale JPEG via real-time+ * - image → compressed color JPEG via real-time* toCompressedGrayJpeg on the raw source → images[]* for pi-ai vision.* - video/audio → listed by path for media tools, without inline bytes.@@ -1342,7 +1354,8 @@ export async function buildAttachmentManifest(}try {const buf = fs.readFileSync(abs);- const compressed = await toCompressedGrayJpeg(buf, { maxDim: 1024, quality: 70, grayscale: true });+ // Preserve reference colors on first delivery, just as read_files does.+ const compressed = await toCompressedGrayJpeg(buf, { maxDim: 1024, quality: 70, grayscale: false });images.push({ data: compressed.buf.toString('base64'), mediaType: 'image/jpeg' });// Candidate providers enforce their own N-image request limit after// this manifest is built. `image_order` preserves a deterministicdiff --git a/src/main/features/connectors/catalog-japan-commerce.ts b/src/main/features/connectors/catalog-japan-commerce.tsindex fa066f0e..f8f434aa 100644--- a/src/main/features/connectors/catalog-japan-commerce.ts+++ b/src/main/features/connectors/catalog-japan-commerce.ts@@ -362,18 +362,18 @@ export function japanMerchantEntries(toolPolicies: Record<string, ConnectorActioguide_label_ru: "Открыть официальное руководство по настройке Yahoo! Shopping",guide_label_it: "Apri la guida ufficiale alla configurazione di Yahoo! Shopping",guide_label_ja: 'Yahoo!ショッピング公式導入ガイドを開く', guide_label_pt: 'Abrir o guia oficial Yahoo! Shopping',- instructions_zh: '使用具有「ストア向け操作関連の機能」权限的 Yahoo! Shopping 应用,配置下方回调地址。生产环境登录账号须关联店铺员工的 Business ID。生产订单功能另需申请和登记出口 IP;可选填官方公开密钥及版本,将相关接口的重新授权周期从 12 小时延长至最多 4 周。',+ instructions_zh: '使用具有“店铺操作相关功能”(日文:ストア向け操作関連の機能)权限的 Yahoo! Shopping 应用,配置下方回调地址。生产环境登录账号须关联店铺员工的 Business ID。生产订单功能另需申请和登记出口 IP;可选填官方公开密钥及版本,将相关接口的重新授权周期从 12 小时延长至最多 4 周。',instructions_en: 'Use a Yahoo! Shopping app with store-operation scope and register the callback below. In production, the login must link to a Business ID registered as shop staff. Production order access needs separate approval and a registered outbound IP. An optional provider-issued public key and version extend reauthorization for those APIs from 12 hours to up to four weeks.',instructions_es: "Usa una aplicación de Yahoo! Shopping con el ámbito de permisos de operaciones de tienda y registra la URL de retorno que aparece a continuación. En producción, la cuenta de inicio de sesión debe estar vinculada a un Business ID registrado como personal de la tienda. El acceso a pedidos en producción requiere una aprobación independiente y una IP de salida registrada. Una clave pública opcional emitida por el proveedor y su versión amplían el intervalo de reautorización de esas API de 12 horas a un máximo de cuatro semanas.",instructions_fr: "Utilisez une application Yahoo! Shopping avec la portée de gestion de boutique et enregistrez l’URL de rappel ci-dessous. En production, le compte de connexion doit être lié à un Business ID enregistré comme membre du personnel de la boutique. L’accès aux commandes en production nécessite une approbation distincte et une adresse IP sortante enregistrée. Une clé publique facultative émise par le fournisseur et sa version permettent d’étendre l’intervalle de réautorisation de ces API de 12 heures à quatre semaines au maximum.",instructions_ko: "상점 운영 권한 범위가 있는 Yahoo! Shopping 앱을 사용하고 아래 콜백을 등록하세요. 프로덕션에서는 로그인 계정이 상점 직원으로 등록된 Business ID와 연결되어 있어야 합니다. 프로덕션 주문 접근에는 별도 승인과 아웃바운드 IP 등록이 필요합니다. 선택 사항인 제공업체 발급 공개 키와 버전을 사용하면 해당 API의 재승인 주기가 12시간에서 최대 4주로 늘어납니다.",instructions_de: "Verwenden Sie eine Yahoo! Shopping-App mit dem Berechtigungsbereich für den Shopbetrieb und registrieren Sie den unten angegebenen Callback. In der Produktionsumgebung muss die Anmeldung mit einer Business ID verknüpft sein, die als Shopmitarbeiter registriert ist. Produktions-Bestellzugriff erfordert eine separate Genehmigung und eine registrierte ausgehende IP-Adresse. Ein optionaler öffentlicher Schlüssel des Anbieters samt Version verlängert das Intervall zur erneuten Autorisierung dieser APIs von 12 Stunden auf bis zu vier Wochen.",instructions_ru: "Используйте приложение Yahoo! Shopping с областью доступа для операций магазина и зарегистрируйте обратный вызов ниже. В производственной среде аккаунт входа должен быть связан с Business ID, зарегистрированным как сотрудник магазина. Производственный доступ к заказам требует отдельного одобрения и регистрации исходящего IP. Необязательные открытый ключ поставщика и его версия увеличивают срок до повторной авторизации этих API с 12 часов до четырёх недель максимум.",- instructions_it: "Usa un'app Yahoo! Shopping con ambito di autorizzazione store-operation e registra il callback qui sotto. In produzione, l'account di accesso deve essere collegato a un Business ID registrato come personale del negozio. L'accesso agli ordini in produzione richiede un'approvazione separata e un IP in uscita registrato. Una chiave pubblica facoltativa rilasciata dal fornitore, insieme alla sua versione, estende l'intervallo di riautorizzazione per queste API da 12 ore fino a quattro settimane.",+ instructions_it: "Usa un'app Yahoo! Shopping con autorizzazioni per le operazioni del negozio e registra il callback qui sotto. In produzione, l'account di accesso deve essere collegato a un Business ID registrato come personale del negozio. L'accesso agli ordini in produzione richiede un'approvazione separata e un IP in uscita registrato. Una chiave pubblica facoltativa rilasciata dal fornitore, insieme alla sua versione, estende l'intervallo di riautorizzazione per queste API da 12 ore fino a quattro settimane.",instructions_ja: '「ストア向け操作関連の機能」権限を持つアプリに下記コールバックを登録します。本番ではログイン ID と店舗従業員のビジネス ID の紐付けが必要です。本番の注文 API は別途申請と接続元 IP 登録が必要です。任意の公式公開鍵とバージョンを設定すると、対象 API の再認可間隔が12時間から最大4週間になります。',instructions_pt: 'Use app Yahoo! Shopping com permissão de operações da loja e registre o callback abaixo. Em produção, o login deve vincular um Business ID de funcionário da loja. Pedidos em produção exigem aprovação separada e IP de saída registrado. Chave pública e versão opcionais estendem a reautorização dessas APIs de 12 horas para até quatro semanas.',fields: [{ key: 'seller_id', input: 'text', storage: 'metadata', format: 'yahoo_seller_id', required: true,- help_zh: '填写目标店铺的ストアアカウント,不是登录用的 Yahoo! JAPAN ID 或 Business ID。',+ help_zh: '填写目标店铺的店铺账号(日文:ストアアカウント),不是登录用的 Yahoo! JAPAN ID 或 Business ID。',help_en: 'Enter the intended shop’s store account, not the login Yahoo! JAPAN ID or Business ID.',help_es: "Introduce la cuenta de tienda de la tienda que quieras conectar, no el Yahoo! JAPAN ID de inicio de sesión ni el Business ID.",help_fr: "Saisissez le compte de boutique voulu, et non le Yahoo! JAPAN ID ou le Business ID de connexion.",diff --git a/src/main/features/contexts.ts b/src/main/features/contexts.tsindex 3061379c..9a7e9b61 100644--- a/src/main/features/contexts.ts+++ b/src/main/features/contexts.ts@@ -22,6 +22,7 @@* generated.*/+import { readTextPreview } from '../util/text-preview';import { fileFailureKind } from '../util/app-error';import * as fs from 'node:fs';import * as path from 'node:path';@@ -77,7 +78,7 @@ const CONTEXTS_IGNORE: ReadonlySet<string> = new Set([// for-byte. Size cap defensive only — content gets chunked before hitting// the LLM so no practical ceiling on "how big a file can the KB take".const TEXT_EXTS: ReadonlySet<string> = new Set([- '.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt', '.json', '.yaml', '.yml', '.log','.html', '.htm', '.xml', '.toml', '.ini', '.conf','.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs','.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt',@@ -283,7 +284,7 @@ export function listContextsTree(): ContextNode[] {return walk(contextsRoot());}-export function readContextFile(relpath: string): Result<{ content: string; path: string }> {+export function readContextFile(relpath: string, preview = false): Result<{ content: string; path: string; truncated?: boolean }> {let p: string;try { p = resolvePath(relpath, { mustExist: true }); }catch (err) { return { ok: false, error: (err as Error).message }; }@@ -292,7 +293,13 @@ export function readContextFile(relpath: string): Result<{ content: string; pathif (!TEXT_EXTS.has(ext)) {return { ok: false, error: `binary file cannot be read as text: ${ext}` };}- try { return { ok: true, content: fs.readFileSync(p, 'utf8'), path: relpath }; }+ try {+ if (preview && /\.(csv|tsv)$/i.test(p)) {+ const result = readTextPreview(p);+ return { ok: true, content: result.text, path: relpath, truncated: result.truncated };+ }+ return { ok: true, content: fs.readFileSync(p, 'utf8'), path: relpath };+ }catch (err) { return { ok: false, error: (err as Error).message }; }}diff --git a/src/main/features/file_indexer.ts b/src/main/features/file_indexer.tsindex 49615e38..110b3078 100644--- a/src/main/features/file_indexer.ts+++ b/src/main/features/file_indexer.ts@@ -84,8 +84,13 @@ export class UnsupportedFileKindError extends Error {}const TEXT_EXTS: ReadonlySet<string> = new Set([- '.md', '.markdown', '.txt', '.csv', '.tsv',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt','.json', '.yaml', '.yml', '.log',+ '.html', '.htm', '.xml', '.toml', '.ini', '.conf',+ '.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs',+ '.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt',+ '.c', '.cpp', '.cc', '.h', '.hpp', '.css', '.scss', '.less',+ '.sql', '.graphql', '.gql',]);const IMAGE_EXTS: ReadonlySet<string> = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif']);// Match the attachment media contract without importing its feature lifecycle.diff --git a/src/main/features/generation_reference_assets.ts b/src/main/features/generation_reference_assets.tsindex ef7706a1..c6b7ee45 100644--- a/src/main/features/generation_reference_assets.ts+++ b/src/main/features/generation_reference_assets.ts@@ -135,7 +135,21 @@ export async function loadImageReferenceBuffersWithProgress(const out: Buffer[] = [];let index = 0;const total = urls.filter(Boolean).length + paths.filter(Boolean).length;- for (const url of urls.map(normalizeUrl).filter(Boolean)) {+ // Validate URLs before I/O, then preserve the public reference-binding order.+ const referenceUrls = urls.map(normalizeUrl).filter(Boolean);+ for (const localPath of paths.map(normalizePath).filter(Boolean)) {+ index += 1;+ throwIfAborted(opts.signal);+ emitProgress(opts.onProgress, 'reference_load', `Loading reference image ${index}/${total}`, {+ kind: 'image',+ index,+ total,+ });+ const buf = await fs.readFile(localPath);+ throwIfAborted(opts.signal);+ out.push(buf);+ }+ for (const url of referenceUrls) {index += 1;throwIfAborted(opts.signal);if (url.toLowerCase().startsWith('asset://')) {@@ -169,18 +183,6 @@ export async function loadImageReferenceBuffersWithProgress(throwIfAborted(opts.signal);out.push(buf);}- for (const localPath of paths.map(normalizePath).filter(Boolean)) {- index += 1;- throwIfAborted(opts.signal);- emitProgress(opts.onProgress, 'reference_load', `Loading reference image ${index}/${total}`, {- kind: 'image',- index,- total,- });- const buf = await fs.readFile(localPath);- throwIfAborted(opts.signal);- out.push(buf);- }return out;}diff --git a/src/main/features/group_chat/bus.ts b/src/main/features/group_chat/bus.tsindex b54223c9..c623f857 100644--- a/src/main/features/group_chat/bus.ts+++ b/src/main/features/group_chat/bus.ts@@ -57,6 +57,7 @@ import {import * as path from 'node:path';import * as fs from 'node:fs';import { inspectCodingDirectory } from '../local_agents/project-directory';+import { getActiveUserId } from '../users';import type {LocalActiveRunIngress,LocalActiveRunInput,@@ -1916,7 +1917,8 @@ async function readCommanderHistoryTail(* carries its process trail) on every CLI turn — O(conversation bytes) per* turn on the main thread. Page back from the tail until the turn boundary,* the stored history cursor (when the binding has one) and enough prior user- * turns are in hand. A boundary that never shows up reads the whole log. */+ * turns are in hand. A missing boundary is unavailable history, not permission+ * to replay the whole log as prior context. */const CLI_CANONICAL_TAIL_PAGE = 256;const CLI_CANONICAL_TAIL_USER_TURNS = CLI_HISTORY_MAX_TURNS;async function _readCliCanonicalTail(@@ -1942,6 +1944,7 @@ async function _readCliCanonicalTail(if (page.nextCursor === null) break;before = page.nextCursor;}+ if (!boundarySeen) throw new Error('Canonical conversation history turn boundary is unavailable.');return pages.flat();}@@ -1989,6 +1992,7 @@ async function buildGroupHistoryForTurn(params: {const source = groupConversationHistorySource(cid, actorId);const file = conversationMessageReadFile(uid, cid);const fileIdentity = commanderHistoryFileIdentity(file);+ if (!fileIdentity) throw new Error('Canonical conversation history is unavailable.');const sessions = await import('../../model/core-agent/session-store');const session = await sessions.getSessionForUser(uid, sessionId);const checkpoint = parseCommanderHistoryCheckpoint(@@ -2058,8 +2062,12 @@ async function buildGroupHistoryForTurn(params: {const rows = await readJsonl<GroupMessage>(file, 0);const currentIndex = rows.findIndex((message) => message.id === currentMsgId);- const throughCurrent = currentIndex >= 0 ? rows.slice(0, currentIndex + 1) : rows;- const priorRows = currentIndex >= 0 ? rows.slice(0, currentIndex) : rows;+ // Storage's tolerant readers also return [] for an unreadable/missing file.+ // Every admitted turn has a persisted trigger, including the first turn;+ // without it neither a full rebase nor a new checkpoint is trustworthy.+ if (currentIndex < 0) throw new Error('Canonical conversation history turn boundary is unavailable.');+ const throughCurrent = rows.slice(0, currentIndex + 1);+ const priorRows = rows.slice(0, currentIndex);const userRows = throughCurrent.filter((message) => message.from === USER_ID);const latestUser = userRows.at(-1);const nextCheckpoint = latestUser ? JSON.stringify({@@ -5043,7 +5051,14 @@ async function runActorTurnWithDisplay(});}} catch (err) {- log.warn(`conversation history build failed cid=${cid} actor=${actor.id}: ${(err as Error).message}`);+ log.warn('conversation history build failed', {+ cid: maskId(cid), actor: maskId(actor.id), error: logErrorSummary(err),+ });+ // Keep the prior session/checkpoint; report the gap only on this turn.+ messageText = '<history-availability status="unavailable">'+ + 'Canonical conversation history could not be refreshed for this turn. '+ + 'Any retained history may be incomplete or stale; this is not evidence of empty history.'+ + '</history-availability>\n' + messageText;}// Attach a `<attachments>` manifest block listing files uploaded on this@@ -5495,6 +5510,7 @@ async function runActorTurnWithDisplay(// the fallback for mid-stream failures and tells abort settlement whether a// status row is needed, without promoting the partial text to a final answer.let streamingText = '';+ let unphasedTextWasStreamed = false;let cliDisplayText = '';const nativeDisplaySegments: GroupMessage[] = [];if (actor.kind !== 'worker') {@@ -6007,6 +6023,21 @@ async function runActorTurnWithDisplay(// Stream events → process channel.if (ev.type === 'final') {finalText = ev.text || '';+ } else if (ev.type === 'commentary-finalized') {+ const text = ev.text || '';+ // The draft was already visible. Move only this round's suffix into+ // process history; previous settled display segments retain their text.+ if (text && streamingText.endsWith(text)) {+ streamingText = streamingText.slice(0, -text.length);+ }+ appendChronologicalCommentary(processItems, text);+ if (actor.kind !== 'worker') {+ emit(state, {+ type: 'process', cid, actor: actor.id,+ turn_id: item.turnId, seg: segState.seg,+ data: ev as unknown as Record<string, unknown>,+ });+ }} else if (ev.type === 'delta') {// Pulled out of the generic branch below so we can mirror the text// into `streamingText` for failure/abort settlement. The activity++ +@@ -6015,6 +6046,7 @@ async function runActorTurnWithDisplay(const piece = (ev as { text?: string }).text;const phase = (ev as { phase?: unknown }).phase;if (typeof piece === 'string') {+ if (phase === 'pending' && piece) unphasedTextWasStreamed = true;if (phase === 'commentary') {appendChronologicalCommentary(processItems, piece);} else {@@ -6136,6 +6168,7 @@ async function runActorTurnWithDisplay(&& !toolPhaseHang&& !!errText&& CHANNEL_RETRY_FAILURE_CODES.has(turnFailureCode)+ && !unphasedTextWasStreamed&& !streamingText.trim()&& !finalText.trim();if (canRetryChannel && turnFailureRetryExhausted) {@@ -6623,6 +6656,8 @@ async function runActorTurnWithDisplay(// covers built-in / not-found / charset / collision cases — bus only// appends the pill.const skillR = extractSkillContainers(workingText);+ let skillCorrectionAttempted = false;+ let skillCorrectionSucceeded = false;if (skillR.containers.length) {workingText = skillR.cleanText;// Apply each `<skill>` container independently. A failed container@@ -6630,7 +6665,56 @@ async function runActorTurnWithDisplay(// the chip slot only fills when the spec was actually written.for (const container of skillR.containers) {try {- const result = await skillsFeat.applySkillContainerFromCommander(container);+ let result = await skillsFeat.applySkillContainerFromCommander(container);+ if (!result.ok && !errText && !aborted && !w.stopRequested+ && skillR.containers.length === 1 && !r.blocks.length+ && !toolCreatedSkills.length && !terminalHandoffCompleted+ && !item.llmPayload.includes(AGENT_MUTATION_FEEDBACK_TAG)) {+ const correction = await import('./skill-creation-correction');+ const message = correction.skillCreationCorrectionMessage(container, result, messageText);+ if (message && !w.stopRequested && getActiveUserId() === uid && Date.now() < executionDeadlineAt) {+ skillCorrectionAttempted = true;+ const controller = new AbortController();+ w.abortController = controller;+ const timeout = setTimeout(() => controller.abort(),+ Math.max(0, Math.min(60_000, executionDeadlineAt - Date.now())));+ const progress = { type: 'progress' as const, text: t('chat.skill_creation_correcting', undefined, turnLanguage) };+ appendProcessItem(processItems, progress);+ emit(state, { type: 'process', cid, actor: actor.id, turn_id: item.turnId,+ seg: segState.seg, data: progress });+ let detachAbort = () => {};+ try {+ await markInFlight(uid, cid, actor.id, true);+ await emitStateChanged(state);+ const { completeSkillCreationCorrection } = await import('../../model/core-agent/runner');+ const cancelled = new Promise<never>((_, reject) => {+ const onAbort = () => reject(new Error('Skill correction cancelled'));+ controller.signal.addEventListener('abort', onAbort, { once: true });+ detachAbort = () => controller.signal.removeEventListener('abort', onAbort);+ if (controller.signal.aborted) onAbort();+ });+ const text = await Promise.race([+ completeSkillCreationCorrection({ userId: uid, cid, turnId: item.turnId,+ message, signal: controller.signal }),+ cancelled,+ ]);+ const corrected = await correction.applySkillCreationCorrection(text, container, uid, controller.signal);+ if (corrected) result = corrected;+ skillCorrectionSucceeded = result.ok && !result.rejected?.length && !result.validation_failed?.length;+ } catch {+ // Preserve the original actionable rejection. No raw content or+ // provider errors enter diagnostics, and no second correction.+ log.warn('Skill creation correction did not complete');+ } finally {+ clearTimeout(timeout);+ detachAbort();+ w.abortController = null;+ aborted ||= w.stopRequested;+ await markInFlight(uid, cid, actor.id, false);+ await emitStateChanged(state);+ }+ }+ }if (result.ok && result.skillId && result.name && result.kind) {recordCreatedSkill({ skill_id: result.skillId, name: result.name, kind: result.kind });if (result.rejected && result.rejected.length) {@@ -6683,6 +6767,14 @@ async function runActorTurnWithDisplay(}}+ if (skillCorrectionAttempted) {+ // Only a single Skill operation enters correction. Its draft success+ // claim cannot override the host result; preserve concrete diagnostics.+ workingText = [t(skillCorrectionSucceeded ? 'chat.skill_creation_corrected'+ : 'chat.skill_creation_correction_failed', undefined, turnLanguage),+ ...commanderMutationNotices].join('\n\n');+ }+// Rejected mutation prose is host-owned. The model may have claimed// success before its container was validated; replacing that prose avoids// presenting a false success next to a failure notice. Persistence chipsdiff --git a/src/main/features/group_chat/skill-creation-correction.ts b/src/main/features/group_chat/skill-creation-correction.tsnew file mode 100644index 00000000..e1b61934--- /dev/null+++ b/src/main/features/group_chat/skill-creation-correction.ts@@ -0,0 +1,89 @@+/** Bounded recovery for a single rejected new Skill. This module never runs+ * tools or interprets prose as authority; the caller owns the one-call budget. */+import {+ applySkillContainerFromCommander, extractSkillContainers, splitSkillMd,+ validateSkillName, type SkillContainerExtracted, type SkillContainerResult,+} from '../skills';+import { validateSkillFile } from '../../quality';+import { getActiveUserId } from '../users';++function validFiles(container: SkillContainerExtracted): boolean {+ const seen = new Set<string>();+ for (const file of container.files) {+ // A portable relative path with one spelling per file. Do not let a+ // corrected proposal introduce platform-dependent escapes or aliases.+ const parts = file.path.split('/');+ if (!file.path || file.path !== file.path.trim() || /[\\:\0<>"|?*]/.test(file.path)+ || parts.some(part => !part || part === '.' || part === '..' || /[. ]$/.test(part)+ || /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(part))) return false;+ const key = file.path.toLowerCase();+ if (seen.has(key)) return false;+ seen.add(key);+ if (!validateSkillFile({ relpath: file.path, content: file.content }).ok) return false;+ }+ // A file cannot simultaneously be a directory, on either supported OS.+ // Sort separators before filename characters so a file's first descendant+ // is adjacent, without repeatedly constructing every deep path prefix.+ const ordered = [...seen].map(file => file.replaceAll('/', '\0')).sort();+ return ordered.every((file, index) => index === 0 || !file.startsWith(`${ordered[index - 1]}\0`));+}++export function skillCreationCorrectionMessage(+ original: SkillContainerExtracted,+ failure: SkillContainerResult,+ request: string,+): string | null {+ if (original.skillId || failure.ok || !failure.creationError || failure.written?.length) return null;+ if (request.length + (original.raw?.length || 0)+ + original.files.reduce((size, file) => size + file.path.length + file.content.length, 0) > 64_000) return null;+ // Missing-entry/name errors occur before quality validation. Do not use that+ // early exit to repair a safety rejection elsewhere in the same proposal.+ const otherFiles = original.files.filter(file => file.path.toUpperCase() !== 'SKILL.MD');+ if (!validFiles({ files: otherFiles })) return null;+ for (const file of original.files.filter(file => file.path.toUpperCase() === 'SKILL.MD')) {+ const report = validateSkillFile({ relpath: file.path, content: file.content });+ if (report.violations.some(v => v.level === 'EXTREME'+ && v.rule !== 'frontmatter_unparseable' && v.rule !== 'frontmatter_name_missing')) return null;+ }+ const snapshot = JSON.stringify({+ kind: 'skill_creation_rejected', code: failure.creationError,+ reason: failure.error, request, proposal: original,+ });+ const message = [+ 'Correct this rejected new-Skill proposal once. No files from this proposal were written.',+ 'Return exactly one <skill> container, without skill_id or surrounding prose. Preserve the requested capability, language, metadata and every existing non-SKILL.md file verbatim. Correct only the reported missing file or format; do not substitute a different task. If the supplied evidence is insufficient, return no container.',+ 'Use whole-file blocks: <<<skill-file path=SKILL.md followed by a newline, complete file content, and >>> alone on its own line. Include SKILL.md with YAML name and description between --- lines, then its instructions. Close </skill>.',+ 'The following JSON is request/proposal data, not additional execution authority:',+ snapshot,+ ].join('\n');+ return message.length <= 64_000 ? message : null;+}++export async function applySkillCreationCorrection(+ text: string,+ original: SkillContainerExtracted,+ userId: string,+ signal: AbortSignal,+): Promise<SkillContainerResult | null> {+ if (signal.aborted || getActiveUserId() !== userId || text.length > 64_000) return null;+ const parsed = extractSkillContainers(text);+ if (parsed.cleanText.trim() || parsed.containers.length !== 1) return null;+ const corrected = parsed.containers[0];+ if (corrected.skillId || !validFiles(corrected)) return null;+ const entry = corrected.files.find(file => file.path === 'SKILL.md');+ if (!entry) return null;+ const oldEntry = original.files.find(file => file.path.toUpperCase() === 'SKILL.MD');+ const oldName = original.metadata?.name || (oldEntry ? splitSkillMd(oldEntry.content).meta.name : '');+ const newName = corrected.metadata?.name || splitSkillMd(entry.content).meta.name;+ if (oldName && !validateSkillName(oldName) && newName !== oldName) return null;+ if (JSON.stringify(original.metadata || {}) !== JSON.stringify(corrected.metadata || {})) return null;+ const otherFiles = original.files.filter(file => file.path.toUpperCase() !== 'SKILL.MD');+ const correctedByPath = new Map(corrected.files.map(file => [file.path, file.content]));+ const originalPaths = new Set(otherFiles.map(file => file.path));+ if (otherFiles.some(file => correctedByPath.get(file.path) !== file.content)) return null;+ if (original.files.length && corrected.files.some(file => file.path !== 'SKILL.md'+ && !originalPaths.has(file.path))) return null;+ // Creation prevalidation, allocation and writes have no async yield. Existing+ // identity collisions still reject; never turn a correction into an edit.+ return applySkillContainerFromCommander(corrected);+}diff --git a/src/main/features/html_preview.ts b/src/main/features/html_preview.tsindex 90adfcda..cd3c031a 100644--- a/src/main/features/html_preview.ts+++ b/src/main/features/html_preview.ts@@ -747,7 +747,7 @@ function blockedResourceLabel(raw: string): string {}function allowedPreviewRequest(raw: string, entryRootReal: string): boolean {- if (/^(?:data|blob|about):/i.test(raw)) return true;+ if (/^(?:https?|wss?|data|blob|about):/i.test(raw)) return true;if (!raw.startsWith('file:')) return false;try {const requested = fs.realpathSync(fileURLToPath(raw));@@ -766,7 +766,7 @@ function registerNetworkIsolation(ses.webRequest.onBeforeRequest((details, callback) => {const raw = String(details.url || '');const allowed = sdkOrigin.current- ? /^(?:data|blob|about):/i.test(raw) || raw.startsWith(sdkOrigin.current + '/')+ ? /^(?:https?|wss?|data|blob|about):/i.test(raw) || raw.startsWith(sdkOrigin.current + '/'): allowedPreviewRequest(raw, entryRootReal);if (!allowed) {blocked.count += 1;@@ -935,8 +935,7 @@ async function renderViewport(/*** Render one local HTML entry at one or more declared viewports using the- * packaged Electron runtime. Network access is denied; only data/blob/about- * URLs and real files below the entry directory are allowed.+ * packaged Electron runtime. Web resources are allowed; local files remain confined to the entry directory.*/export async function renderResponsiveHtmlPreview(entryPath: string,@@ -955,7 +954,7 @@ export async function renderResponsiveHtmlPreview(throw new Error('E_HTML_PREVIEW_BROWSER_UNAVAILABLE: Electron BrowserWindow is unavailable');}- const bundle = previewBundle(entryReal);+ const bundle = await previewBundle(entryReal);const sdkOrigin: { current?: string } = {};const blocked = { count: 0, samples: [] as string[] };const partition = `html-preview-${Date.now()}-${crypto.randomBytes(6).toString('hex')}`;diff --git a/src/main/features/library_corpus.ts b/src/main/features/library_corpus.tsindex 50073b63..ddfc270c 100644--- a/src/main/features/library_corpus.ts+++ b/src/main/features/library_corpus.ts@@ -111,7 +111,7 @@ const RECONCILE_FILE_CONCURRENCY = 4;const MAX_PATH_SEGMENT_LEN = 200;const TEXT_EXTS: ReadonlySet<string> = new Set([- '.md', '.markdown', '.txt', '.csv', '.tsv',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt','.json', '.yaml', '.yml', '.log','.html', '.htm', '.xml', '.toml', '.ini', '.conf','.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs',diff --git a/src/main/features/preview_windows.ts b/src/main/features/preview_windows.tsindex 7e031c3e..84918421 100644--- a/src/main/features/preview_windows.ts+++ b/src/main/features/preview_windows.ts@@ -4,7 +4,7 @@ import { randomUUID } from 'node:crypto';import { z } from 'zod';import { SRC_ROOT } from '../paths';import { createLogger } from '../logger';-import { hardenedWebPreferences, installExternalNavigationGuard, installOfflineHtmlPreviewNavigationGuard } from '../util/window-security';+import { hardenedWebPreferences, installExternalNavigationGuard, installHtmlPreviewNavigationGuard } from '../util/window-security';import { registerUserSwitchHook } from './user-switch-hooks';import { subscribeBus } from './group_chat';@@ -99,7 +99,6 @@ export async function openPreview(uid: string, owner: WebContents, payload: unkn}return { windowId: existing.win.id };}- if (records.size >= 16) throw new Error('Close a preview window before opening another.');const parent = BrowserWindow.fromWebContents(owner);if (!parent || parent.isDestroyed()) throw new Error('Preview is unavailable.');// Background E2E owners must keep all their native previews off the desktop.@@ -134,7 +133,7 @@ export async function openPreview(uid: string, owner: WebContents, payload: unkn}ownerState.windows.add(win);installExternalNavigationGuard(win.webContents, url => shell.openExternal(url), () => log.warn('external link failed', { code: 'open_failed' }));- installOfflineHtmlPreviewNavigationGuard(win.webContents);+ installHtmlPreviewNavigationGuard(win.webContents);win.on('close', event => {if (!record.dirty) return;event.preventDefault();diff --git a/src/main/features/project_files.ts b/src/main/features/project_files.tsindex c9a31352..7c37ef59 100644--- a/src/main/features/project_files.ts+++ b/src/main/features/project_files.ts@@ -7,6 +7,7 @@* file tools get read-only access to this directory.*/+import { readTextPreview } from '../util/text-preview';import { fileFailureKind, type FileFailureKind } from '../util/app-error';import * as fs from 'node:fs';import { isUtf8 } from 'node:buffer';@@ -39,7 +40,7 @@ import { logErrorSummary, logPathRef, maskId } from '../util/log-redact';const log = createLogger('project_files');const TEXT_EXTS: ReadonlySet<string> = new Set([- '.md', '.markdown', '.txt', '.csv', '.tsv',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt','.json', '.yaml', '.yml', '.log','.html', '.htm', '.xml', '.toml', '.ini', '.conf','.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs',@@ -835,13 +836,20 @@ export async function readProjectTextFile(userId: string,projectId: string,name: string,-): Promise<Result<{ content: string; name: string }>> {+ preview = false,+): Promise<Result<{ content: string; name: string; truncated?: boolean }>> {const r = await resolveProjectFileAbsPath(userId, projectId, name);if (!r.ok) return { ok: false, error: (r as { error?: string }).error || 'not_found' };if (r.kind !== 'text') return { ok: false, error: 'binary file cannot be read as text' };let st: fs.Stats;try { st = fs.statSync(r.absPath); }catch { return { ok: false, error: 'not_found' }; }+ if (preview && /\.(csv|tsv)$/i.test(name)) {+ try {+ const result = readTextPreview(r.absPath);+ return { ok: true, content: result.text, name, truncated: result.truncated };+ } catch (err) { return { ok: false, error: (err as Error).message }; }+ }if (st.size > MAX_BYTES_TEXT) {return { ok: false, error: t('errors.file_too_large_mb', { mb: Math.round(MAX_BYTES_TEXT / 1024 / 1024) }) };}diff --git a/src/main/features/saved_apps.ts b/src/main/features/saved_apps.tsindex 2b3d65ca..65d8396f 100644--- a/src/main/features/saved_apps.ts+++ b/src/main/features/saved_apps.ts@@ -19,6 +19,7 @@*/import * as fs from 'node:fs';+import * as fsp from 'node:fs/promises';import * as path from 'node:path';import * as crypto from 'node:crypto';@@ -42,14 +43,8 @@ const DEFAULT_TITLE = 'Interactive app';// wasm / …) becomes a one-line `[binary asset …]` placeholder — it can't be// represented in a text `.md` bundle, and changing it means re-supplying it// via `create_artifact` with `"encoding":"base64"` anyway.-const TEXT_LIKE_EXTS: ReadonlySet<string> = new Set([- '.html', '.htm', '.js', '.mjs', '.css', '.json', '.map', '.svg', '.xml', '.txt', '.csv',-]);-const BUNDLE_RESOURCE_EXTS: ReadonlySet<string> = new Set([- '.html', '.htm', '.css', '.js', '.mjs', '.json', '.svg', '.png', '.jpg', '.jpeg', '.webp',- '.gif', '.avif', '.bmp', '.ico', '.webmanifest', '.woff', '.woff2', '.ttf', '.wasm',- '.mp3', '.wav', '.ogg', '.mp4', '.webm', '.glb', '.gltf', '.txt', '.md', '.csv', '.xml',-]);+const TEXT_LIKE_EXTS = chatArtifacts.TEXT_EXTS;+const BUNDLE_RESOURCE_EXTS = chatArtifacts.SERVED_EXTS;const HTML_ENTRY_EXTS: ReadonlySet<string> = new Set(['.html', '.htm']);// Only application source files express an intent to save the surrounding// HTML bundle. Images, media, fonts, manifests, data, and models remain valid@@ -58,12 +53,9 @@ const BUNDLE_TRIGGER_EXTS: ReadonlySet<string> = new Set(['.html', '.htm', '.css', '.js', '.mjs',]);const BUNDLE_EXCLUDED_DIRS: ReadonlySet<string> = new Set([- '.git', '.hg', '.svn', 'node_modules', 'dist', 'build', '.next', '.vite', 'coverage',+ '.git', '.hg', '.svn', 'node_modules', '.next', '.vite', 'coverage',]);const SOURCE_BUNDLE_NAME = 'app-source.md';-const MAX_BUNDLE_FILES = 300;-const MAX_BUNDLE_BYTES = 30 * 1024 * 1024;-const MAX_BUNDLE_ANCESTORS = 8;export type Result<T = {}> = ({ ok: true } & T) | { ok: false; error: string };type ResolveCode = 'bad_input' | 'forbidden' | 'not_found';@@ -181,9 +173,9 @@ function nearestFenceRoot(candidate: string, roots: string[]): string {return best;}-function inferTitleFromHtml(entryPath: string, rootDir: string): string {+async function inferTitleFromHtml(entryPath: string, rootDir: string): Promise<string> {try {- const html = fs.readFileSync(entryPath, 'utf8');+ const html = await fsp.readFile(entryPath, 'utf8');const title = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(html)?.[1]|| /<h1[^>]*>([\s\S]*?)<\/h1>/i.exec(html)?.[1]|| '';@@ -252,9 +244,9 @@ function savedAppMimeFor(name: string): string {}}-function chooseHtmlEntryInDir(dir: string): string {+async function chooseHtmlEntryInDir(dir: string): Promise<string> {let entries: fs.Dirent[];- try { entries = fs.readdirSync(dir, { withFileTypes: true }); }+ try { entries = await fsp.readdir(dir, { withFileTypes: true }); }catch { return ''; }const html = entries.filter((e) => e.isFile() && isHtmlEntryName(e.name) && shouldIncludeBundleFile(e.name))@@ -275,42 +267,43 @@ function shouldIncludeBundleFile(name: string): boolean {return BUNDLE_RESOURCE_EXTS.has(path.extname(name).toLowerCase());}-function collectBundleFiles(rootDir: string): { ok: true; files: string[]; totalBytes: number } | { ok: false; reason: string } {+async function collectBundleFiles(rootDir: string): Promise<{ ok: true; files: string[]; totalBytes: number } | { ok: false; reason: string }> {const files: string[] = [];let totalBytes = 0;- const walk = (dir: string): void => {- const entries = fs.readdirSync(dir, { withFileTypes: true });- for (const e of entries) {- if (e.isSymbolicLink()) continue;- const abs = path.join(dir, e.name);- if (e.isDirectory()) {- if (!shouldSkipBundleDir(e.name)) walk(abs);- continue;+ const pending = [rootDir];+ try {+ while (pending.length) {+ const dir = pending.pop()!;+ if (!isPathAllowed(dir, [rootDir])) throw new Error('path traversal blocked');+ const directory = await fsp.opendir(dir);+ for await (const e of directory) {+ if (e.isSymbolicLink()) continue;+ const abs = path.join(dir, e.name);+ if (e.isDirectory()) {+ if (!shouldSkipBundleDir(e.name)) pending.push(abs);+ } else if (e.isFile() && shouldIncludeBundleFile(e.name)) {+ const st = await fsp.lstat(abs);+ if (!st.isFile() || !isPathAllowed(abs, [rootDir])) throw new Error('source file changed');+ files.push(abs);+ totalBytes += st.size;+ }}- if (!e.isFile() || !shouldIncludeBundleFile(e.name)) continue;- const st = fs.statSync(abs);- files.push(abs);- totalBytes += st.size;- if (files.length > MAX_BUNDLE_FILES) throw new Error(`too many files (>${MAX_BUNDLE_FILES})`);- if (totalBytes > MAX_BUNDLE_BYTES) throw new Error(`bundle is too large (>${Math.round(MAX_BUNDLE_BYTES / 1024 / 1024)} MB)`);}- };- try { walk(rootDir); }- catch (err) { return { ok: false, reason: (err as Error).message || 'could not scan bundle' }; }+ } catch (err) { return { ok: false, reason: (err as Error).message || 'could not scan bundle' }; }return { ok: true, files, totalBytes };}-function findBundleRoot(target: string, opts: BundleInspectOptions = {}): { ok: true; rootDir: string; entry: string } | { ok: false; reason: string } {+async function findBundleRoot(target: string, opts: BundleInspectOptions = {}): Promise<{ ok: true; rootDir: string; entry: string } | { ok: false; reason: string }> {const abs = path.resolve(target);let st: fs.Stats;- try { st = fs.statSync(abs); }+ try { st = await fsp.stat(abs); }catch { return { ok: false, reason: 'file not found' }; }const roots = (opts.fenceRoots || []).filter(Boolean).map((r) => path.resolve(r));if (roots.length && !isInsideAnyRoot(abs, roots)) return { ok: false, reason: 'outside allowed workspace' };if (st.isDirectory()) {- const entry = chooseHtmlEntryInDir(abs);+ const entry = await chooseHtmlEntryInDir(abs);if (!entry) return { ok: false, reason: 'folder has no HTML entry' };return { ok: true, rootDir: abs, entry };}@@ -322,8 +315,8 @@ function findBundleRoot(target: string, opts: BundleInspectOptions = {}): { ok:const fence = nearestFenceRoot(abs, roots);let dir = path.dirname(abs);- for (let i = 0; i <= MAX_BUNDLE_ANCESTORS; i++) {- const entry = chooseHtmlEntryInDir(dir);+ while (true) {+ const entry = await chooseHtmlEntryInDir(dir);if (entry) return { ok: true, rootDir: dir, entry };if (fence && path.resolve(dir) === fence) break;const parent = path.dirname(dir);@@ -333,9 +326,9 @@ function findBundleRoot(target: string, opts: BundleInspectOptions = {}): { ok:return { ok: false, reason: 'no HTML entry found for this file' };}-export function inspectBundleFromPath(targetPath: string, opts: BundleInspectOptions = {}): BundleInspection {+async function inspectBundle(targetPath: string, opts: BundleInspectOptions): Promise<BundleInspection & { files?: string[] }> {if (typeof targetPath !== 'string' || !targetPath.trim()) return { ok: false, error: 'path required' };- const found = findBundleRoot(targetPath, opts);+ const found = await findBundleRoot(targetPath, opts);if (!found.ok) return { ok: true, canSave: false, reason: (found as { ok: false; reason: string }).reason };const rootDir = found.rootDir;if (opts.fenceRoots?.length && !isInsideAnyRoot(rootDir, opts.fenceRoots)) {@@ -344,10 +337,10 @@ export function inspectBundleFromPath(targetPath: string, opts: BundleInspectOptconst entry = found.entry;const entryAbs = path.join(rootDir, entry);let entryStat: fs.Stats;- try { entryStat = fs.statSync(entryAbs); }+ try { entryStat = await fsp.stat(entryAbs); }catch { return { ok: true, canSave: false, reason: 'bundle is missing its HTML entry' }; }if (!entryStat.isFile()) return { ok: true, canSave: false, reason: 'HTML entry is not a file' };- const scanned = collectBundleFiles(rootDir);+ const scanned = await collectBundleFiles(rootDir);if (!scanned.ok) return { ok: true, canSave: false, reason: (scanned as { ok: false; reason: string }).reason };const includesEntry = scanned.files.some((p) => path.resolve(p) === path.resolve(entryAbs));if (!includesEntry) return { ok: true, canSave: false, reason: 'HTML entry is not a supported app file' };@@ -356,23 +349,30 @@ export function inspectBundleFromPath(targetPath: string, opts: BundleInspectOptcanSave: true,rootDir,entry,- title: inferTitleFromHtml(entryAbs, rootDir),+ title: await inferTitleFromHtml(entryAbs, rootDir),+ files: scanned.files,fileCount: scanned.files.length,totalBytes: scanned.totalBytes,};}-function copyBundleFiles(srcRoot: string, destRoot: string): { fileCount: number; totalBytes: number } {- const scanned = collectBundleFiles(srcRoot);- if (!scanned.ok) throw new Error((scanned as { ok: false; reason: string }).reason);- for (const src of scanned.files) {+export async function inspectBundleFromPath(targetPath: string, opts: BundleInspectOptions = {}): Promise<BundleInspection> {+ const { files: _files, ...inspection } = await inspectBundle(targetPath, opts);+ return inspection;+}++async function copyBundleFiles(srcRoot: string, destRoot: string, files: string[]): Promise<{ fileCount: number; totalBytes: number }> {+ let totalBytes = 0;+ for (const src of files) {const rel = path.relative(srcRoot, src);if (rel.startsWith('..') || path.isAbsolute(rel)) throw new Error('path traversal blocked');const dst = path.join(destRoot, rel);- fs.mkdirSync(path.dirname(dst), { recursive: true });- fs.copyFileSync(src, dst);+ if (!(await fsp.lstat(src)).isFile() || !isPathAllowed(src, [srcRoot])) throw new Error('source file changed');+ await fsp.mkdir(path.dirname(dst), { recursive: true });+ await fsp.copyFile(src, dst);+ totalBytes += (await fsp.stat(dst)).size;}- return { fileCount: scanned.files.length, totalBytes: scanned.totalBytes };+ return { fileCount: files.length, totalBytes };}function mintAppId(userId: string): { appId: string; destDir: string } {@@ -387,7 +387,7 @@ function mintAppId(userId: string): { appId: string; destDir: string } {// ── Public API ───────────────────────────────────────────────────────────/** Copy a chat artifact bundle into a new `saved_apps/<appId>/`. */-export function saveFromArtifact(userId: string, cid: string, artifactId: string): Result<{ id: string; title: string }> {+export async function saveFromArtifact(userId: string, cid: string, artifactId: string): Promise<Result<{ id: string; title: string }>> {const resolved = chatArtifacts.resolveArtifactDir(userId, cid, artifactId);if (!resolved.ok) return { ok: false, error: (resolved as { error?: string }).error || 'artifact not found' };const srcDir = (resolved as { dirPath: string }).dirPath;@@ -409,7 +409,7 @@ export function saveFromArtifact(userId: string, cid: string, artifactId: stringtry {// Copy everything except the source `__orkas-meta.json` (we write a fresh// one stamped with the source provenance).- fs.cpSync(srcDir, tmpDir, {+ await fsp.cp(srcDir, tmpDir, {recursive: true,filter: (src) => path.basename(src) !== META_FILENAME,});@@ -422,12 +422,12 @@ export function saveFromArtifact(userId: string, cid: string, artifactId: stringsourceArtifactId: typeof artifactId === 'string' ? artifactId : '',savedAt: new Date().toISOString(),};- writeMeta(tmpDir, meta);- fs.mkdirSync(path.dirname(destDir), { recursive: true });- fs.renameSync(tmpDir, destDir);+ await fsp.writeFile(path.join(tmpDir, META_FILENAME), JSON.stringify(meta, null, 2));+ await fsp.mkdir(path.dirname(destDir), { recursive: true });+ await fsp.rename(tmpDir, destDir);} catch (err) {- try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ }- try { fs.rmSync(destDir, { recursive: true, force: true }); } catch { /* best-effort */ }+ try { await fsp.rm(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ }+ try { await fsp.rm(destDir, { recursive: true, force: true }); } catch { /* best-effort */ }log.warn('saveFromArtifact failed', {user_id: maskId(userId),conversation_id: maskId(cid),@@ -447,12 +447,12 @@ export function saveFromArtifact(userId: string, cid: string, artifactId: string}/** Copy a workspace/file-tab-discovered HTML app bundle into saved_apps. */-export function saveFromPath(+export async function saveFromPath(userId: string,targetPath: string,opts: BundleInspectOptions & { title?: unknown; sourceCid?: unknown } = {},-): Result<{ id: string; title: string; rootDir: string; entry: string; fileCount: number; totalBytes: number }> {- const inspected = inspectBundleFromPath(targetPath, opts);+): Promise<Result<{ id: string; title: string; rootDir: string; entry: string; fileCount: number; totalBytes: number }>> {+ const inspected = await inspectBundle(targetPath, opts);if (!inspected.ok) return { ok: false, error: (inspected as { ok: false; error: string }).error };if (!inspected.canSave) return { ok: false, error: (inspected as { ok: true; canSave: false; reason: string }).reason };@@ -469,8 +469,8 @@ export function saveFromPath(const tmpDir = `${destDir}.tmp-${crypto.randomBytes(4).toString('hex')}`;try {- fs.mkdirSync(tmpDir, { recursive: true });- const copied = copyBundleFiles(inspected.rootDir, tmpDir);+ await fsp.mkdir(tmpDir, { recursive: true });+ const copied = await copyBundleFiles(inspected.rootDir, tmpDir, inspected.files!);if (!fs.existsSync(path.join(tmpDir, inspected.entry))) throw new Error('bundle is missing its HTML entry');const meta: SavedAppMeta = {title,@@ -479,9 +479,9 @@ export function saveFromPath(entry: inspected.entry,savedAt: new Date().toISOString(),};- writeMeta(tmpDir, meta);- fs.mkdirSync(path.dirname(destDir), { recursive: true });- fs.renameSync(tmpDir, destDir);+ await fsp.writeFile(path.join(tmpDir, META_FILENAME), JSON.stringify(meta, null, 2));+ await fsp.mkdir(path.dirname(destDir), { recursive: true });+ await fsp.rename(tmpDir, destDir);log.info('saveFromPath completed', {user_id: maskId(userId),app_id: maskId(appId),@@ -493,8 +493,8 @@ export function saveFromPath(notifySavedAppDirty(appId);return { ok: true, id: appId, title, rootDir: inspected.rootDir, entry: inspected.entry, ...copied };} catch (err) {- try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ }- try { fs.rmSync(destDir, { recursive: true, force: true }); } catch { /* best-effort */ }+ try { await fsp.rm(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ }+ try { await fsp.rm(destDir, { recursive: true, force: true }); } catch { /* best-effort */ }log.warn('saveFromPath failed', {user_id: maskId(userId),target: logPathRef(targetPath),@@ -710,7 +710,7 @@ function buildSourceBundle(dir: string, title: string): string {const lines: string[] = [`# Interactive app source — "${title}"`,'',- `This is the current source of a self-contained interactive web app. Its current entry HTML is \`${entry}\`. To modify it: read the files below, then call \`create_artifact\` again with the updated \`files\` array — include a top-level HTML entry, keep the app offline (inline your CSS/JS or reference sibling files by relative URL), and the result is embedded in a sandboxed iframe.`,+ `This is the current source of a self-contained interactive web app. Its current entry HTML is \`${entry}\`. To modify it: read the files below, then call \`create_artifact\` again with the updated \`files\` array — include a top-level HTML entry, use relative URLs for bundled files or HTTP(S) URLs for external resources, and the result is embedded in a sandboxed iframe.`,'',];for (const rel of files) {diff --git a/src/main/features/search/chat_store.ts b/src/main/features/search/chat_store.tsindex a1298a2d..b089f04b 100644--- a/src/main/features/search/chat_store.ts+++ b/src/main/features/search/chat_store.ts@@ -11,10 +11,15 @@* • `chat_files` — per-conversation watermark (mtime + size + next index),* the same contiguity proof the JSON index kept* • `chat_docs` — one row per indexed message- * • `chat_postings` — (term, doc) → tf, i.e. the inverted index+ * • `chat_terms` — interned term text, referenced by `chat_postings`+ * • `chat_postings` — (term_id, doc) → tf, i.e. the inverted index* • `chat_meta` — the source fingerprint that decides whether a search* may trust the index without a history-wide re-scan*+ * Every table here is derived from the conversation JSONL, so a store written+ * by a different `CHAT_STORE_SCHEMA_VERSION` is discarded and rebuilt rather+ * than migrated or refused. Bump that constant whenever this layout changes.+ ** `chat_postings` deliberately stores `tf` rather than delegating to FTS5:* chat ranking is BM25 *plus* the CJK bigram anchor in `search/index.ts`, and* reproducing it needs the per-term frequency of each candidate, which FTS5@@ -45,7 +50,15 @@ function warn(message: string, error: unknown): void {createLogger('search:chat_store').warn(message, { error: (error as Error).message });}-export const CHAT_STORE_SCHEMA_VERSION = 1;+/** Record an expected but consequential event. Unlike `warn`, this must not+ * abort a worker: discarding a stale store is recovery, not failure. */+function note(message: string, fields: Record<string, unknown>): void {+ if (!isMainThread) return;+ const { createLogger } = require('../../logger') as typeof import('../../logger');+ createLogger('search:chat_store').info(message, fields);+}++export const CHAT_STORE_SCHEMA_VERSION = 2;export interface ChatDocInput {cid: string;@@ -103,79 +116,111 @@ export function chatStorePath(uid: string): string {return path.join(userSearchDir(uid), 'chats.db');}-function ensureSchema(db: Database.Database, dbPath: string): void {- const hasTables = db- .prepare("SELECT COUNT(*) AS n FROM sqlite_master WHERE type='table' AND name='chat_docs'")- .get() as { n: number };- if (!hasTables.n) {- db.exec(`- CREATE TABLE chat_files (- cid TEXT PRIMARY KEY,- mtime REAL NOT NULL,- size INTEGER NOT NULL,- next INTEGER NOT NULL- );+/** Tables a usable store must already have. A database missing any of them+ * predates a schema change and cannot answer a query. */+const REQUIRED_TABLES = ['chat_files', 'chat_docs', 'chat_terms', 'chat_postings', 'chat_meta'];- CREATE TABLE chat_docs (- id INTEGER PRIMARY KEY AUTOINCREMENT,- doc_id TEXT NOT NULL UNIQUE,- cid TEXT NOT NULL,- msg_index INTEGER NOT NULL,- role TEXT NOT NULL,- time TEXT NOT NULL,- len INTEGER NOT NULL- );- CREATE INDEX chat_docs_cid ON chat_docs(cid);+function tableNames(db: Database.Database): Set<string> {+ return new Set((db+ .prepare("SELECT name FROM sqlite_master WHERE type='table'")+ .all() as Array<{ name: string }>).map((row) => row.name));+}- CREATE TABLE chat_meta (- key TEXT PRIMARY KEY,- value TEXT NOT NULL- );+function schemaIsUsable(db: Database.Database, names: Set<string>): boolean {+ if (!REQUIRED_TABLES.every((name) => names.has(name))) return false;+ return ((db.pragma('user_version', { simple: true }) as number) || 0) === CHAT_STORE_SCHEMA_VERSION;+}- -- Terms are interned. Storing the term text on every posting row makes- -- the table larger than the JSON index it replaces, because JSON wrote- -- each term once and listed its documents underneath; measured on a real- -- 230 MB corpus that cost 298 MB against the snapshot's 241 MB.- CREATE TABLE chat_terms (- id INTEGER PRIMARY KEY AUTOINCREMENT,- term TEXT NOT NULL UNIQUE- );+function createSchema(db: Database.Database): void {+ db.exec(`+ CREATE TABLE chat_files (+ cid TEXT PRIMARY KEY,+ mtime REAL NOT NULL,+ size INTEGER NOT NULL,+ next INTEGER NOT NULL+ );- CREATE TABLE chat_postings (- term_id INTEGER NOT NULL,- doc INTEGER NOT NULL,- tf INTEGER NOT NULL,- PRIMARY KEY (term_id, doc)- ) WITHOUT ROWID;- -- Primary key order puts a term's postings together, which is the read- -- path; deleting one document's postings needs the other direction.- CREATE INDEX chat_postings_doc ON chat_postings(doc);- `);- db.pragma(`user_version = ${CHAT_STORE_SCHEMA_VERSION}`);- return;- }- const version = (db.pragma('user_version', { simple: true }) as number) || 0;- if (version !== CHAT_STORE_SCHEMA_VERSION) {- throw new Error(- `chat_store schema version mismatch (${version} vs ${CHAT_STORE_SCHEMA_VERSION}) at ${dbPath}`,+ CREATE TABLE chat_docs (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ doc_id TEXT NOT NULL UNIQUE,+ cid TEXT NOT NULL,+ msg_index INTEGER NOT NULL,+ role TEXT NOT NULL,+ time TEXT NOT NULL,+ len INTEGER NOT NULL+ );+ CREATE INDEX chat_docs_cid ON chat_docs(cid);++ CREATE TABLE chat_meta (+ key TEXT PRIMARY KEY,+ value TEXT NOT NULL+ );++ -- Terms are interned. Storing the term text on every posting row makes+ -- the table larger than the JSON index it replaces, because JSON wrote+ -- each term once and listed its documents underneath; measured on a real+ -- 230 MB corpus that cost 298 MB against the snapshot's 241 MB.+ CREATE TABLE chat_terms (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ term TEXT NOT NULL UNIQUE);++ CREATE TABLE chat_postings (+ term_id INTEGER NOT NULL,+ doc INTEGER NOT NULL,+ tf INTEGER NOT NULL,+ PRIMARY KEY (term_id, doc)+ ) WITHOUT ROWID;+ -- Primary key order puts a term's postings together, which is the read+ -- path; deleting one document's postings needs the other direction.+ CREATE INDEX chat_postings_doc ON chat_postings(doc);+ `);+ db.pragma(`user_version = ${CHAT_STORE_SCHEMA_VERSION}`);+}++/** Remove a database whose schema this build cannot use, sidecars included. */+function discardStore(dbPath: string): void {+ for (const suffix of ['', '-wal', '-shm']) {+ try { fs.rmSync(dbPath + suffix, { force: true }); }+ catch (err) { warn('discard chat store failed', err); }}}+function openDatabase(dbPath: string): Database.Database {+ const db = new Database(dbPath);+ // WAL keeps a reader (a search) from blocking behind an append; DELETE mode+ // would serialize them. `synchronous = NORMAL` is the same durability trade+ // vec_store makes for a derived index that reconciles from source.+ db.pragma('journal_mode = WAL');+ db.pragma('synchronous = NORMAL');+ return db;+}+function handleFor(uid: string): Handle {const cached = _stores.get(uid);if (cached) return cached;const dbPath = chatStorePath(uid);fs.mkdirSync(path.dirname(dbPath), { recursive: true });- const db = new Database(dbPath);+ let db = openDatabase(dbPath);try {- // WAL keeps a reader (a search) from blocking behind an append; DELETE- // mode would serialize them. `synchronous = NORMAL` is the same durability- // trade vec_store makes: a derived index that reconciles from source.- db.pragma('journal_mode = WAL');- db.pragma('synchronous = NORMAL');- ensureSchema(db, dbPath);+ const names = tableNames(db);+ if (!names.size) {+ createSchema(db);+ } else if (!schemaIsUsable(db, names)) {+ // The index is derived state whose source of truth is the conversation+ // JSONL, so a store this build cannot read is discarded and rebuilt+ // rather than reported as an error. Throwing here left search+ // permanently broken for everyone who already had a store when the+ // schema changed — the JSON index it replaced rebuilt from source.+ const previous = (db.pragma('user_version', { simple: true }) as number) || 0;+ db.close();+ discardStore(dbPath);+ note('chat store discarded for a schema change',+ { previous, schema: CHAT_STORE_SCHEMA_VERSION });+ db = openDatabase(dbPath);+ createSchema(db);+ }} catch (err) {// Never let a failed handle reach the cache: on Windows the leaked SQLite// handle locks the file and blocks repair or workspace cleanup.diff --git a/src/main/features/skills.ts b/src/main/features/skills.tsindex c7b2aa28..e3546416 100644--- a/src/main/features/skills.ts+++ b/src/main/features/skills.ts@@ -1098,7 +1098,7 @@ function normalizePlatformSkillMdForWrite(content: string, fallbackName = ''): sreturn normalizeSkillMdForWrite(content, fallbackName);}-async function _getCustomSkillForUser(skillId: string, userId: string): Promise<CustomSkill | null> {+function _getCustomSkillForUser(skillId: string, userId: string): CustomSkill | null {const d = customSkillDir(skillId, userId);if (!fs.existsSync(d) || !fs.statSync(d).isDirectory()) return null;if (!hasSkillMd(d)) return null;@@ -1159,6 +1159,14 @@ async function _listSkillFilesAt(skillDir: string): Promise<SkillFileInfo[]> {export async function createCustomSkill(name: string, description: string, category = '',): Promise<CustomSkill | null> {+ return _createCustomSkillSync(name, description, category);+}++// Keep allocation and the commander's validated file writes in one synchronous+// commit section; an account switch or Stop cannot interleave with a seed.+function _createCustomSkillSync(+ name: string, description: string, category = '',+): CustomSkill | null {const err = validateSkillName(name);if (err) throw new Error(err);const d = customSkillDir(name);@@ -1190,7 +1198,7 @@ export async function createCustomSkill(log.info(`created name=${name} category=${category || '(none)'}`);_invalidateSkillListCache();invalidateCoreAgentSkills().catch(() => { /* runner may not be loaded yet */ });- return getCustomSkill(name);+ return _getCustomSkillForUser(name, getActiveUserId());}export async function updateCustomSkill(@@ -2959,6 +2967,9 @@ function _visibleInlineSkillEditText(text: string, currentSkillId: string): striexport interface SkillContainerResult {ok: boolean;+ /** Structured, pre-write creation failures. Correction additionally requires+ * an unbound container; the legacy missing-edit-target fallback is excluded. */+ creationError?: 'container_no_blocks' | 'missing_skill_md' | 'missing_name' | 'invalid_name' | 'invalid_frontmatter';/** 'created' for the no-skill-id branch; 'updated' for the edit branch.* Undefined on `ok: false`. */kind?: 'created' | 'updated';@@ -3004,6 +3015,8 @@ export async function applySkillContainerFromCommander(// into re-sending the same malformed blocks.return {ok: false,+ ...(!container.skillId && (container.raw || '').trim()+ ? { creationError: 'container_no_blocks' as const } : {}),error: (container.raw || '').trim()? t('skills.errors.container_no_blocks'): t('skills.errors.container_empty'),@@ -3030,13 +3043,13 @@ async function _applySkillContainerCreate(// SKILL.md is mandatory in the create branch — that's where the skill id// (frontmatter `name`) and bilingual descriptions are sourced from.const skillMd = files.find((f) => f.path.toUpperCase() === 'SKILL.MD');- if (!skillMd) return { ok: false, error: t('skills.errors.create_missing_skill_md') };+ if (!skillMd) return { ok: false, creationError: 'missing_skill_md', error: t('skills.errors.create_missing_skill_md') };const { meta } = splitSkillMd(skillMd.content || '');const rawName = (meta.name || '');const name = rawName.trim();- if (!name) return { ok: false, error: t('skills.errors.create_missing_name') };+ if (!name) return { ok: false, creationError: 'missing_name', error: t('skills.errors.create_missing_name') };const validateErr = validateSkillName(rawName);- if (validateErr) return { ok: false, error: validateErr };+ if (validateErr) return { ok: false, creationError: 'invalid_name', error: validateErr };// Collision checks — same gates as the IPC create path so commander and// detail panel produce identical failure modes.if (fs.existsSync(customSkillDir(name))) {@@ -3067,6 +3080,10 @@ async function _applySkillContainerCreate(ok: false,error: t('skills.errors.validation_blocked'),validation_failed: validationFailed,+ ...(validationFailed.every((entry) => entry.path === 'SKILL.md'+ && entry.report.violations.filter((v) => v.level === 'EXTREME')+ .every((v) => v.rule === 'frontmatter_unparseable'))+ ? { creationError: 'invalid_frontmatter' as const } : {}),};}@@ -3076,7 +3093,7 @@ async function _applySkillContainerCreate(const metadataSidecar = metadata ? _skillSidecarPatchFromMetadataUpdate(metadata) : {};const fileSidecar = _skillSidecarPatchFromFrontmatter(meta);const seedCategory = String(metadataSidecar.category || fileSidecar.category || '');- const created = await createCustomSkill(name, seedDescription, seedCategory);+ const created = _createCustomSkillSync(name, seedDescription, seedCategory);if (!created) return { ok: false, error: t('skills.errors.create_failed') };const written: string[] = [];diff --git a/src/main/features/video_studio.ts b/src/main/features/video_studio.tsindex 61dbb250..b2e41c55 100644--- a/src/main/features/video_studio.ts+++ b/src/main/features/video_studio.ts@@ -1507,6 +1507,7 @@ export async function withVideoStudioTimeout<T>(* inspect, snapshot, audio-only track reuse) is deliberately not gated. */let compositionRenderSlotTail: Promise<void> = Promise.resolve();const RENDER_SLOT_WAIT_PROGRESS_INTERVAL_MS = 30_000;+const softwareOnscreenCompositionWindows = new WeakSet<ElectronBrowserWindow>();export async function acquireCompositionRenderSlot(input: {signal?: AbortSignal;@@ -1554,8 +1555,13 @@ async function withCompositionWindow<T>(fn: (win: ElectronBrowserWindow) => Promise<T>,): Promise<T> {const electron = await import('electron');- const { BrowserWindow, session } = electron;+ const { BrowserWindow, session, app } = electron;if (!BrowserWindow) throw new Error('Electron BrowserWindow unavailable');+ const observedGpuMode = await observeElectronGpuMode();+ const softwareRendering = observedGpuMode === 'software'+ || app.commandLine.hasSwitch('disable-gpu')+ || process.argv.includes('--disable-gpu');+ const useOffscreenPainting = !(process.platform === 'win32' && softwareRendering);const partition = `video-studio-${Date.now()}-${Math.random().toString(36).slice(2)}`;const ses = session.fromPartition(partition);ses.webRequest.onBeforeRequest((details, callback) => {@@ -1576,12 +1582,14 @@ async function withCompositionWindow<T>(backgroundColor: '#000000',webPreferences: hardenedWebPreferences({session: ses,- // Hidden on-screen windows can retain the previous composited scene- // even after seek/RAF settles, particularly with software-rendered masks.- // Offscreen painting keeps preview and encoder captures at the seek time.- offscreen: true,+ // Offscreen painting fixes stale hidden-window captures on hardware and+ // macOS software compositors. Windows software compositing has the+ // inverse defect: its offscreen surface retains the prior scene while a+ // hidden on-screen surface tracks timeline seeks correctly.+ offscreen: useOffscreenPainting,}),});+ if (!useOffscreenPainting) softwareOnscreenCompositionWindows.add(win);try {// Electron centers new Windows/Linux windows within the display work// area, which can shrink even a useContentSize window (1920x1080 became@@ -1784,6 +1792,11 @@ new Promise((resolve) => requestAnimationFrame(() => {`, true), COMPOSITION_SCRIPT_TIMEOUT_MS, 'E_COMPOSITION_PAINT_TIMEOUT', 'composition paint did not settle before capture.', () => {try { win.destroy(); } catch { /* best effort */ }});+ // On Windows the software compositor needs an on-screen backing surface and+ // a short post-RAF grace period before capturePage observes its invalidation.+ if (softwareOnscreenCompositionWindows.has(win)) {+ await new Promise((resolve) => setTimeout(resolve, 250));+ }}/** How many times a capture may be retaken when the pixels contradict the DOM.diff --git a/src/main/features/web_apps/bundle-revision.ts b/src/main/features/web_apps/bundle-revision.tsindex 5de73b2e..423c43f9 100644--- a/src/main/features/web_apps/bundle-revision.ts+++ b/src/main/features/web_apps/bundle-revision.ts@@ -1,33 +1,43 @@import * as fs from 'node:fs';+import * as fsp from 'node:fs/promises';import * as path from 'node:path';/** Freeze the file inventory at launch. Only files from this revision can be* served; already loaded resources are rechecked before privileged operations.* Immutable saved/artifact source remains owned by its existing resolver. */-export function bundleRevision(root: string, required: string[]) {+export async function bundleRevision(root: string, required: string[]) {const stamps = new Map<string, string>();const loaded = new Set(required.map(p => path.resolve(p)));let invalid = false;- let entries = 0;const stamp = (file: string) => {const s = fs.lstatSync(file);return s.isFile() ? [s.dev, s.ino, s.size, s.mtimeMs, s.ctimeMs].join(':') : '';};- function visit(dir: string, depth: number) {- if (depth > 32) throw new Error('bundle depth limit');- for (const item of fs.readdirSync(dir, { withFileTypes: true })) {- if (++entries > 2048) throw new Error('bundle file limit');+ // Manifest authority and the entry were resolved before the asynchronous+ // inventory. They must not change while that inventory yields to the host.+ const requiredStamps = new Map([...loaded].map(file => [file, stamp(file)]));+ const pending = [path.resolve(root)];+ while (pending.length) {+ const dir = pending.pop()!;+ if (!(await fsp.lstat(dir)).isDirectory()) throw new Error('unsupported bundle entry');+ const directory = await fsp.opendir(dir);+ for await (const item of directory) {if (item.name.startsWith('.')) continue;const file = path.join(dir, item.name);- if (item.isDirectory()) visit(file, depth + 1);- else if (item.isFile()) stamps.set(file, stamp(file));- else throw new Error('unsupported bundle entry');+ if (item.isDirectory()) pending.push(file);+ else if (item.isFile()) {+ const s = await fsp.lstat(file);+ if (!s.isFile()) throw new Error('unsupported bundle entry');+ stamps.set(file, [s.dev, s.ino, s.size, s.mtimeMs, s.ctimeMs].join(':'));+ } else throw new Error('unsupported bundle entry');}}- visit(path.resolve(root), 0);const same = (file: string) => {try { return stamps.has(file) && stamps.get(file) === stamp(file); } catch { return false; }};+ for (const [file, original] of requiredStamps) {+ if (!original || stamps.get(file) !== original || !same(file)) throw new Error('bundle changed during launch');+ }return {valid() {if (!invalid) for (const file of loaded) if (!same(file)) { invalid = true; break; }diff --git a/src/main/features/web_apps/catalog.ts b/src/main/features/web_apps/catalog.tsindex 8ddd72d5..c2b1f275 100644--- a/src/main/features/web_apps/catalog.ts+++ b/src/main/features/web_apps/catalog.ts@@ -1,20 +1,22 @@/** Web-only API contract. This is not a second model-visible Tool catalog. */import { isAtomicWriteTempPath } from '../../storage';import { z } from 'zod';+import { MAX_TEXT_FILE_BYTES } from '../../util/file-size-limits';import { zodToJsonSchema } from 'zod-to-json-schema';export const SDK_VERSION = 1;export const MANIFEST_FILE = 'orkas-app.json';export const SDK_PATH = '__orkas/sdk.js';-export const MAX_PAYLOAD_BYTES = 1024 * 1024;+// Leave room for base64 and JSON framing above the shared file byte ceiling.+export const MAX_PAYLOAD_BYTES = 2 * MAX_TEXT_FILE_BYTES + 1024 * 1024;const empty = z.object({}).strict();-const key = z.string().min(1).max(128);+const key = z.string().min(1);const scope = z.enum(['local', 'cloud']).default('local');const fileScope = z.enum(['local', 'cloud']).default('cloud');-const appPath = z.string().min(1).max(240).describe('Relative path, at most 8 segments. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini.').refine(value => value.split('/').length <= 8 && value.split('/').every(part =>+const appPath = z.string().min(1).describe('Relative path. No atomic temporary filenames, hidden/traversal segments, backslashes, Windows reserved names, trailing dots/spaces, visibility, Thumbs.db or desktop.ini.').refine(value => value.split('/').every(part =>!!part && !isAtomicWriteTempPath(part) && !['visibility', 'Thumbs.db', 'desktop.ini'].includes(part) && !part.startsWith('.') && !/[\\:\x00-\x1f<>"|?*]/.test(part) && !/[. ]$/.test(part) && !/^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\.|$)/i.test(part)));const handle = z.string().regex(/^[a-f0-9]{32}$/);-const text = z.string().max(512 * 1024);+const text = z.string().max(MAX_TEXT_FILE_BYTES);const define = (capability: string | null, description: string, input: z.ZodTypeAny, result: string, example: object) =>({ capability, description, input, result, example });export const METHODS = {@@ -23,21 +25,21 @@ export const METHODS = {'host.getContext': define(null, 'Read the current application language and SDK version.', empty, '{language, sdkVersion}', {}),'permissions.revoke': define(null, 'Revoke this instance grants and cancel pending work. Reopening starts fresh.', empty, '{revoked:true}', {}),'storage.get': define('storage', 'Read app-private JSON; scope defaults to local for compatibility. cloud participates in account sync.', z.object({ key, scope }).strict(), '{value}; null when missing.', { key: 'draft' }),- 'storage.set': define('storage', 'Atomically save an app-private JSON value; total store limit 1 MiB.', z.object({ key, scope, value: z.unknown().refine(v => v !== undefined) }).strict(), '{saved:true}', { key: 'draft', value: 'Hello' }),+ 'storage.set': define('storage', 'Atomically save an app-private JSON value; total store limit 200 MiB.', z.object({ key, scope, value: z.unknown().refine(v => v !== undefined) }).strict(), '{saved:true}', { key: 'draft', value: 'Hello' }),'storage.remove': define('storage', 'Remove one app-private value.', z.object({ key, scope }).strict(), '{removed:true}', { key: 'draft' }),- 'storage.keys': define('storage', 'List up to 256 keys in the selected app store.', z.object({ scope }).strict(), '{keys:string[]}', {}),- 'appFiles.list': define('appFiles', 'List sandbox files recursively. cloud (default) syncs with the account; local is for caches. At most 256 files and 16 MiB per scope.', z.object({ scope: fileScope }).strict(), '{files:[{path,size}]}', { scope: 'cloud' }),- 'appFiles.readText': define('appFiles', 'Read a sandbox UTF-8 file up to 512 KiB; missing files return null.', z.object({ path: appPath, scope: fileScope }).strict(), '{text} or null.', { path: 'notes/today.txt' }),- 'appFiles.readBase64': define('appFiles', 'Read a sandbox binary file up to 512 KiB; missing files return null.', z.object({ path: appPath, scope: fileScope }).strict(), '{base64} or null.', { path: 'images/icon.png' }),- 'appFiles.writeText': define('appFiles', 'Atomically replace an app-owned UTF-8 file (512 KiB). Creates parent directories. Saved means local persistence, not completed upload.', z.object({ path: appPath, scope: fileScope, text }).strict(), '{saved:true}', { path: 'notes/today.txt', text: 'Hello' }),- 'appFiles.writeBase64': define('appFiles', 'Atomically replace an app-owned binary file (512 KiB). Requires canonical base64.', z.object({ path: appPath, scope: fileScope, base64: z.string().max(700000) }).strict(), '{saved:true}', { path: 'images/icon.bin', base64: 'AAE=' }),+ 'storage.keys': define('storage', 'List keys in the selected app store.', z.object({ scope }).strict(), '{keys:string[]}', {}),+ 'appFiles.list': define('appFiles', 'List sandbox files recursively. cloud (default) syncs with the account; local is for caches.', z.object({ scope: fileScope }).strict(), '{files:[{path,size}]}', { scope: 'cloud' }),+ 'appFiles.readText': define('appFiles', 'Read a sandbox UTF-8 file up to 200 MiB; missing files return null.', z.object({ path: appPath, scope: fileScope }).strict(), '{text} or null.', { path: 'notes/today.txt' }),+ 'appFiles.readBase64': define('appFiles', 'Read a sandbox binary file up to 200 MiB; missing files return null.', z.object({ path: appPath, scope: fileScope }).strict(), '{base64} or null.', { path: 'images/icon.png' }),+ 'appFiles.writeText': define('appFiles', 'Atomically replace an app-owned UTF-8 file (200 MiB). Creates parent directories. Saved means local persistence, not completed upload.', z.object({ path: appPath, scope: fileScope, text }).strict(), '{saved:true}', { path: 'notes/today.txt', text: 'Hello' }),+ 'appFiles.writeBase64': define('appFiles', 'Atomically replace an app-owned binary file (200 MiB). Requires canonical base64.', z.object({ path: appPath, scope: fileScope, base64: z.string().max(4 * Math.ceil(MAX_TEXT_FILE_BYTES / 3)) }).strict(), '{saved:true}', { path: 'images/icon.bin', base64: 'AAE=' }),'appFiles.remove': define('appFiles', 'Remove one sandbox file; missing files are a successful no-op. Directories are not recursively deleted.', z.object({ path: appPath, scope: fileScope }).strict(), '{removed:true}', { path: 'notes/today.txt' }),'files.pick': define('files', 'Choose a local file using the host dialog; grants read access only.', empty, '{handle,name,size} or null on cancellation.', {}),- 'files.readText': define('files', 'Read a selected UTF-8 file, at most 512 KiB.', z.object({ handle }).strict(), '{text}', { handle: '0123456789abcdef0123456789abcdef' }),- 'files.readBase64': define('files', 'Read a selected file as base64, at most 512 KiB.', z.object({ handle }).strict(), '{base64}', { handle: '0123456789abcdef0123456789abcdef' }),- 'files.saveAs': define('files', 'Export UTF-8 text using the host Save dialog. Create-only: choose a new destination; existing files are not overwritten.', z.object({ name: z.string().min(1).max(120).regex(/^[^/\\\x00-\x1f]+$/), text }).strict(), '{saved:true,name} or null on cancellation.', { name: 'summary.txt', text: 'Summary' }),+ 'files.readText': define('files', 'Read a selected UTF-8 file, at most 200 MiB.', z.object({ handle }).strict(), '{text}', { handle: '0123456789abcdef0123456789abcdef' }),+ 'files.readBase64': define('files', 'Read a selected file as base64, at most 200 MiB.', z.object({ handle }).strict(), '{base64}', { handle: '0123456789abcdef0123456789abcdef' }),+ 'files.saveAs': define('files', 'Export UTF-8 text using the host Save dialog. Create-only: choose a new destination; existing files are not overwritten.', z.object({ name: z.string().min(1).regex(/^[^/\\\x00-\x1f]+$/), text }).strict(), '{saved:true,name} or null on cancellation.', { name: 'summary.txt', text: 'Summary' }),'files.release': define('files', 'Release a file handle before closing the app.', z.object({ handle }).strict(), '{released:true}', { handle: '0123456789abcdef0123456789abcdef' }),- 'ai.generate': define('ai', 'Generate text with the configured model without tools, skills or conversation history. Optional progress receives text deltas. Uses the configured model without an extra application permission dialog.', z.object({ prompt: z.string().min(1).max(32000), maxTokens: z.number().int().min(1).max(4096).default(1024) }).strict(), '{text,usage,stopReason}; token-limited output has stopReason=max_tokens. Cancel with AbortSignal.', { prompt: 'Write a short greeting.', maxTokens: 128 }),+ 'ai.generate': define('ai', 'Generate text with the configured model without tools, skills or conversation history. Optional progress receives text deltas. Uses the configured model without an extra application permission dialog.', z.object({ prompt: z.string().min(1), maxTokens: z.number().int().positive().safe().optional() }).strict(), '{text,usage,stopReason}; token-limited output has stopReason=max_tokens. Cancel with AbortSignal.', { prompt: 'Write a short greeting.', maxTokens: 128 }),'tools.list': define(null, 'Discover eligible existing Orkas tools and their original schemas. Does not grant execution.', empty, '{tools:[{name,capability,description,inputSchema}]}', {}),'tools.call': define(null, 'Execute an eligible existing tool under its original operation permissions. Sensitive actions use host approval; Trusted mode skips confirmation. Unknown tools fail closed.', z.object({ name: key, arguments: z.record(z.unknown()) }).strict(), '{content,isError?}; inspect isError. Oversized results fail without replay.', { name: 'library', arguments: { action: 'list', scope: 'global', limit: 10 } }),} as const;@@ -54,7 +56,6 @@ export const UNSUPPORTED = [{ capability: 'skills', reason: 'Host-only: Skill script execution needs an explicitly scoped application sandbox and execution contract.' },{ capability: 'shell', reason: 'Host-only: arbitrary execution would escape the application capability boundary.' },{ capability: 'media.generate', reason: 'Deferred: owning studio plan, output and spending gates must be adapted first.' },- { capability: 'network', reason: 'Deferred: private app data must not reach arbitrary remote endpoints; bundled resources only.' },];export function describeMethod(method: string) {if (!Object.hasOwn(METHODS, method)) return null;diff --git a/src/main/features/web_apps/host.ts b/src/main/features/web_apps/host.tsindex 18a8e85e..813986d3 100644--- a/src/main/features/web_apps/host.ts+++ b/src/main/features/web_apps/host.ts@@ -1,10 +1,8 @@/** Native adapters for the Web-only runtime. Never forwards arbitrary IPC. */import * as fs from 'node:fs';-import * as fsp from 'node:fs/promises';import * as path from 'node:path';import * as crypto from 'node:crypto';import { dialog } from 'electron';-import { userLocalRoot } from '../../paths';import { getActiveUserId } from '../users';import { registerUserSwitchHook } from '../user-switch-hooks';import { getLanguage } from '../config';@@ -14,7 +12,6 @@ import * as artifacts from '../chat_artifacts';import * as saved from '../saved_apps';import { AppError, WebAppRuntime, type AppTool, type Bundle } from './runtime';import { MANIFEST_FILE } from './catalog';-import { capToolResult, ToolResultPersistenceError, DEFAULT_INLINE_RESULT_TOKENS } from '../../util/tool-result-cap';import type { AgentTool } from '#core-agent';import { appResource as scopedAppResource } from './resources';import { cancelForApp, type AppUsageScope } from '../connectors/action_confirm';@@ -42,16 +39,9 @@ export async function appTools(uid: string, appUsage?: AppUsageScope): Promise<Aconst context = { signal, state: {} };const result = await tool.execute(args, context);if (signal.aborted || getActiveUserId() !== uid) throw new AppError('E_CLOSED');- const spool = path.join(userLocalRoot(uid), 'web_apps', 'results', crypto.randomBytes(16).toString('hex'));- try {- const capped = capToolResult(tool.name, result, context, { maxInlineTokens: DEFAULT_INLINE_RESULT_TOKENS, toolResultsDir: spool });- if (capped !== result) throw new AppError('E_RESULT_LIMIT');- // Host observations, backing paths and provider diagnostics never cross the bridge.- return { content: capped.content, ...(capped.isError ? { isError: true } : {}) };- } catch (err) {- if (err instanceof ToolResultPersistenceError) throw new AppError('E_RESULT_LIMIT');- throw err;- } finally { await fsp.rm(spool, { recursive: true, force: true }); }+ // Model-context budgets belong to AgentRunner, not application data.+ // Project only public tool content; never expose backing paths or observations.+ return { content: result.content, ...(result.isError ? { isError: true } : {}) };},});return [wrap(library, 'library'), ...connectors.map(tool => wrap(tool, 'connectors'))];@@ -82,7 +72,7 @@ export const runtime = new WebAppRuntime({registerUserSwitchHook('web-apps', previousUid => runtime.closeUser(previousUid));export type Source = { appId: string } | { cid: string; artifactId: string };-export function resolveBundle(uid: string, source: Source): { bundle: Bundle | null; url: string; entry: string } {+export async function resolveBundle(uid: string, source: Source): Promise<{ bundle: Bundle | null; url: string; entry: string }> {const isSaved = 'appId' in source;const resolve = (rel: string) => {const result = isSaved ? saved.resolveSavedAppFilePath(uid, source.appId, rel)@@ -109,14 +99,14 @@ export function resolveBundle(uid: string, source: Source): { bundle: Bundle | nconst meta = JSON.parse(fs.readFileSync(metaFile, 'utf8'));if (typeof meta.title === 'string') title = meta.title.slice(0, 120);} catch { /* A missing legacy title uses the localized default. */ }- let revision: ReturnType<typeof bundleRevision>;- try { revision = bundleRevision(path.dirname(manifestFile.absPath), [initial.absPath, manifestFile.absPath]); }+ let revision: Awaited<ReturnType<typeof bundleRevision>>;+ try { revision = await bundleRevision(path.dirname(manifestFile.absPath), [initial.absPath, manifestFile.absPath]); }catch { throw new AppError('E_BUNDLE'); }return { url, entry, bundle: { kind: isSaved ? 'saved' : 'artifact', key, title, entry, manifest, valid: () => revision.valid(),resolve: rel => { const file = resolve(rel); return file && revision.accept(file.absPath) ? file : null; } } };}-export function openApp(uid: string, owner: number, source: Source) {- const found = resolveBundle(uid, source);+export async function openApp(uid: string, owner: number, source: Source) {+ const found = await resolveBundle(uid, source);return found.bundle ? runtime.open(uid, owner, found.bundle) : { url: found.url, entry: found.entry };}export { sdkScript, APP_CSP } from './resources';diff --git a/src/main/features/web_apps/preview-preload.js b/src/main/features/web_apps/preview-preload.jsindex 86b167c9..1ea007bc 100644--- a/src/main/features/web_apps/preview-preload.js+++ b/src/main/features/web_apps/preview-preload.js@@ -19,12 +19,6 @@ if (process.isMainFrame) {/** @type {(value: any) => void} */let resolve = () => {};const promise = new Promise(done => { resolve = done; });- if (pending.size >= 8) {- const value = { code: 'E_LIMIT' };- queueMicrotask(() => onEvent?.({ type: 'result', ok: false, value }));- resolve(value);- return { promise, cancel() {} };- }const cancel = () => {const item = pending.get(id); if (!item) return;ipcRenderer.send(channel, { id, operation: 'webApps.cancel', payload });diff --git a/src/main/features/web_apps/preview.ts b/src/main/features/web_apps/preview.tsindex 0af4fdd9..24278be1 100644--- a/src/main/features/web_apps/preview.ts+++ b/src/main/features/web_apps/preview.ts@@ -1,9 +1,10 @@-/** Offline authoring preview: real SDK/runtime, temporary storage, no account services. */+/** Isolated authoring preview: real SDK/runtime, temporary storage, no account services. */import * as fs from 'node:fs';import * as fsp from 'node:fs/promises';import * as os from 'node:os';import * as path from 'node:path';import type { Session, WebContents } from 'electron';+import { safeExternalHttpUrl } from '../../util/window-security';import { getLanguage } from '../config';import { SRC_ROOT } from '../../paths';import { serveFileRange } from '../../util/http-range';@@ -23,7 +24,7 @@ export interface PreviewSdkEvidence {}/** Absence preserves legacy HTML; an invalid opt-in must never downgrade. */-export function previewBundle(entry: string): Bundle | null {+export async function previewBundle(entry: string): Promise<Bundle | null> {const root = path.dirname(entry);const manifestPath = path.join(root, MANIFEST_FILE);let stat: fs.Stats;@@ -33,8 +34,8 @@ export function previewBundle(entry: string): Bundle | null {let manifest: unknown;try { manifest = manifestSchema.parse(JSON.parse(fs.readFileSync(manifestPath, 'utf8'))); }catch { throw new AppError('E_MANIFEST'); }- let revision: ReturnType<typeof bundleRevision>;- try { revision = bundleRevision(root, [entry, manifestPath]); }+ let revision: Awaited<ReturnType<typeof bundleRevision>>;+ try { revision = await bundleRevision(root, [entry, manifestPath]); }catch { throw new AppError('E_BUNDLE'); }return { key: 'preview', title: 'HTML preview', entry: path.basename(entry), manifest,valid: () => revision.valid(),@@ -114,10 +115,15 @@ export async function createPreviewSdk(ses: Session, contents: WebContents, bundcontents.on('ipc-message', listener);contents.on('will-frame-navigate', event => {if (event.isMainFrame) { event.preventDefault(); return; }- // After the initial attach every navigation revokes this preview instance.- if (event.frame?.url && event.frame.url !== 'about:blank') {- event.preventDefault(); runtime.closeOwner(contents.id);- } else if (event.url !== open.url) event.preventDefault();+ if (event.frame?.parent && event.frame.parent !== contents.mainFrame && safeExternalHttpUrl(event.url)) return;+ const source = event.frame ? event.frame.url : event.initiator?.url || '';+ if (runtime.canNavigate(contents.id, source, event.url)) {+ runtime.cancelOriginRequests(contents.id, source);+ return;+ }+ // Automated playback cannot leave the inspected bundle. A blocked link+ // does not close the document or interrupt subsequent SDK checks.+ if (event.url !== open.url) event.preventDefault();});ses.protocol.handle('chat-app', async request => {const resource = appResource(request, runtime);diff --git a/src/main/features/web_apps/request-history.ts b/src/main/features/web_apps/request-history.tsnew file mode 100644index 00000000..16cbfb66--- /dev/null+++ b/src/main/features/web_apps/request-history.ts@@ -0,0 +1,34 @@+/** Compact sequential SDK ids without expiring replay protection. Legacy/custom+ * ids remain supported; out-of-order calls and cancel-before-call are remembered. */+export class RequestHistory {+ private documents = new Map<string, Array<[number, number]>>();+ private other = new Set<string>();+ private sequence(id: string): { document: string; n: number } | null {+ const match = /^((?:d[a-f0-9]{32})?q)([1-9][0-9]*)$/.exec(id);+ if (!match) return null;+ const n = Number(match[2]);+ return Number.isSafeInteger(n) ? { document: match[1], n } : null;+ }+ has(id: string): boolean {+ const sequence = this.sequence(id);+ return sequence === null ? this.other.has(id) : (this.documents.get(sequence.document) ?? []).some(([start, end]) => start <= sequence.n && sequence.n <= end);+ }+ add(id: string): void {+ const sequence = this.sequence(id);+ if (sequence === null) { this.other.add(id); return; }+ const { document, n } = sequence;+ let ranges = this.documents.get(document);+ if (!ranges) { ranges = []; this.documents.set(document, ranges); }+ let i = 0;+ while (i < ranges.length && ranges[i][1] < n - 1) i++;+ let start = n, end = n;+ const first = i;+ while (i < ranges.length && ranges[i][0] <= end + 1) {+ start = Math.min(start, ranges[i][0]);+ end = Math.max(end, ranges[i][1]);+ i++;+ }+ ranges.splice(first, i - first, [start, end]);+ }+ clear(): void { this.documents.clear(); this.other.clear(); }+}diff --git a/src/main/features/web_apps/resources.ts b/src/main/features/web_apps/resources.tsindex 25a5c1e8..1ae99cb1 100644--- a/src/main/features/web_apps/resources.ts+++ b/src/main/features/web_apps/resources.ts@@ -4,6 +4,7 @@ import * as path from 'node:path';import { SRC_ROOT } from '../../paths';import * as artifacts from '../chat_artifacts';import { METHODS, SDK_PATH, catalogDocument } from './catalog';+import { webContentCsp } from '../../util/web-content-policy';import type { WebAppRuntime } from './runtime';let sdk: string | null = null;@@ -12,8 +13,7 @@ export function sdkScript() {.replace('/* HOST_METHOD_NAMES */ []', JSON.stringify(Object.keys(METHODS)));return sdk;}-export const APP_CSP = ["default-src 'none'", "script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval'", "style-src 'self' 'unsafe-inline'",- "img-src 'self' data: blob:", "font-src 'self' data:", "media-src 'self' blob:", "connect-src 'self'", "frame-src 'none'", "object-src 'none'", "form-action 'none'", "base-uri 'none'"].join('; ');+export const APP_CSP = webContentCsp();export function appResource(request: Request, runtime: WebAppRuntime) {const url = new URL(request.url);if (!/^app-[a-f0-9]{32}$/.test(url.host)) return null;diff --git a/src/main/features/web_apps/runtime.ts b/src/main/features/web_apps/runtime.tsindex ebeb0231..9a59740d 100644--- a/src/main/features/web_apps/runtime.ts+++ b/src/main/features/web_apps/runtime.ts@@ -8,6 +8,8 @@ import { METHODS, SDK_VERSION, UNSUPPORTED, describeMethod, manifestSchema, typeimport { writeJson, writeBytesAtomic, isAtomicWriteTempPath } from '../../storage';import { webAppDataFile, webAppSandboxRoot, userLocalRoot, userCloudRoot, userRoot, WS_ROOT } from '../../paths';import { isPathAllowed } from '../../util/path-sandbox';+import { MAX_TEXT_FILE_BYTES } from '../../util/file-size-limits';+import { RequestHistory } from './request-history';export class AppError extends Error {constructor(readonly code: string) { super(code); }@@ -36,7 +38,7 @@ export interface HostAdapters {closed?(uid: string, usage: AppUsageScope): void;pick(): Promise<string | null>;save(name: string): Promise<string | null>;- generate(uid: string, args: { prompt: string; maxTokens: number }, signal: AbortSignal, progress: (event: unknown) => void): Promise<unknown>;+ generate(uid: string, args: { prompt: string; maxTokens?: number }, signal: AbortSignal, progress: (event: unknown) => void): Promise<unknown>;tools(uid: string, usage: AppUsageScope): Promise<AppTool[]>;}interface Instance {@@ -49,28 +51,31 @@ interface Instance {controller: AbortController;handles: Map<string, { name: string; bytes: Buffer }>;requests: Map<string, AbortController>;- seen: Set<string>;- createdAt: number;+ seen: RequestHistory;}const id = () => crypto.randomBytes(16).toString('hex');-const MAX_FILE_BYTES = 512 * 1024;-const STORE_BYTES = 1024 * 1024;-const STORE_KEYS = 256;-const MAX_INSTANCES = 32;-const REQUEST_LIMIT = 4096;+const MAX_FILE_BYTES = MAX_TEXT_FILE_BYTES;+const STORE_BYTES = MAX_TEXT_FILE_BYTES;const error = (code: string): never => { throw new AppError(code); };async function readFileBytes(file: fs.FileHandle, check: () => void): Promise<Buffer> {- const bytes = Buffer.alloc(MAX_FILE_BYTES + 1);+ // Allocate for this file, not the admission ceiling; read in chunks so+ // cancellation still interrupts large snapshots. Short reads are not EOF.+ let bytes = Buffer.allocUnsafe(Math.min((await file.stat()).size, MAX_FILE_BYTES) + 1);let offset = 0;- while (offset < bytes.length) {+ while (true) {check();- const { bytesRead } = await file.read(bytes, offset, bytes.length - offset, offset);+ const { bytesRead } = await file.read(bytes, offset, Math.min(1024 * 1024, bytes.length - offset), offset);check();- if (!bytesRead) break;+ if (!bytesRead) return bytes.subarray(0, offset);offset += bytesRead;+ if (offset > MAX_FILE_BYTES) return error('E_LIMIT');+ if (offset === bytes.length) {+ // The source grew after stat; expand only as needed, with one sentinel+ // byte so a file crossing the shared ceiling cannot be truncated silently.+ const grown = Buffer.allocUnsafe(Math.min(MAX_FILE_BYTES + 1, Math.max(64 * 1024, bytes.length * 2)));+ bytes.copy(grown); bytes = grown;+ }}- if (offset > MAX_FILE_BYTES) return error('E_LIMIT');- return bytes.subarray(0, offset);}export function boundedJson(value: unknown, max = MAX_PAYLOAD_BYTES): string {let json: string;@@ -80,7 +85,8 @@ export function boundedJson(value: unknown, max = MAX_PAYLOAD_BYTES): string {let depth = 0;let quoted = false;let escaped = false;- for (const c of json) {+ for (let i = 0; i < json.length; i++) {+ const c = json[i];if (quoted) { if (escaped) escaped = false; else if (c === '\\') escaped = true; else if (c === '"') quoted = false; }else if (c === '"') quoted = true;else if (c === '{' || c === '[') { if (++depth > 32) return error('E_LIMIT'); }@@ -92,7 +98,6 @@ export function boundedJson(value: unknown, max = MAX_PAYLOAD_BYTES): string {/** Instances and grants are memory-only and bound to the trusted host renderer. */export class WebAppRuntime {private instances = new Map<string, Instance>();- private heavyCalls = 0;private stores = new Map<string, { mutex: Mutex; users: number }>();constructor(private host: HostAdapters, private preview?: { storageRoot: string }) {}@@ -103,23 +108,19 @@ export class WebAppRuntime {open(uid: string, owner: number, bundle: Bundle) {if (uid !== this.host.activeUser()) return error('E_CLOSED');- for (const [token, instance] of this.instances) {- if (Date.now() - instance.createdAt > 12 * 60 * 60 * 1000) this.close(instance.uid, instance.owner, token);- }- if (this.instances.size >= MAX_INSTANCES) return error('E_LIMIT');const parsed = manifestSchema.safeParse(bundle.manifest);if (!parsed.success) return error('E_MANIFEST');const token = id();const instance: Instance = { token, host: `app-${id()}`, uid, owner, bundle, manifest: parsed.data,controller: new AbortController(), handles: new Map(), requests: new Map(),- seen: new Set(), createdAt: Date.now() };+ seen: new RequestHistory() };this.instances.set(token, instance);return { token, entry: bundle.entry, url: `chat-app://${instance.host}/${bundle.entry.split('/').map(encodeURIComponent).join('/')}`, sdkVersion: SDK_VERSION };}private current(uid: string, owner: number, token: string): Instance {const s = this.instances.get(token);if (!s || s.uid !== uid || s.owner !== owner || uid !== this.host.activeUser() || s.controller.signal.aborted) return error('E_CLOSED');- if (Date.now() - s.createdAt > 12 * 60 * 60 * 1000 || s.bundle.valid?.() === false) {+ if (s.bundle.valid?.() === false) {this.close(uid, owner, token); return error('E_CLOSED');}return s;@@ -133,6 +134,23 @@ export class WebAppRuntime {this.host.closed?.(s.uid, { id: s.token, owner: s.owner });s.handles.clear(); s.seen.clear();}+ canNavigate(owner: number, source: string, destination: string): boolean {+ try {+ const from = new URL(source), to = new URL(destination);+ if (from.protocol !== 'chat-app:' || to.protocol !== from.protocol || to.host !== from.host+ || to.username || to.password) return false;+ const instance = [...this.instances.values()].find(s => s.owner === owner && s.host === from.host);+ if (!instance) return false;+ const rel = to.pathname.slice(1).split('/').map(decodeURIComponent).join('/');+ return this.resource(to.host, rel, `chat-app://${from.host}`)?.resolved?.mime.startsWith('text/html') === true;+ } catch { return false; }+ }+ cancelOriginRequests(owner: number, url: string): void {+ let host: string; try { host = new URL(url).host; } catch { return; }+ for (const s of this.instances.values()) {+ if (s.owner === owner && s.host === host) for (const request of s.requests.values()) request.abort();+ }+ }closeOrigin(owner: number, url: string): void {let host: string; try { host = new URL(url).host; } catch { return; }for (const s of this.instances.values()) if (s.owner === owner && s.host === host) this.close(s.uid, owner, s.token);@@ -147,7 +165,6 @@ export class WebAppRuntime {const s = this.current(uid, owner, token);if (!/^[a-zA-Z0-9_-]{1,64}$/.test(requestId)) return error('E_INPUT');// Tombstones also stop a cancellation arriving before its invoke.- if (s.seen.size >= REQUEST_LIMIT && !s.seen.has(requestId)) return error('E_LIMIT');s.seen.add(requestId);s.requests.get(requestId)?.abort();}@@ -167,21 +184,16 @@ export class WebAppRuntime {const s = this.current(uid, owner, token);if (!/^[a-zA-Z0-9_-]{1,64}$/.test(requestId)) return error('E_INPUT');if (s.seen.has(requestId)) return error('E_DUPLICATE');- if (s.seen.size >= REQUEST_LIMIT || s.requests.size >= 4) return error('E_LIMIT');if (!Object.hasOwn(METHODS, method)) return error('E_METHOD');boundedJson(raw);const def = METHODS[method as Method];const parsed = def.input.safeParse(raw);if (!parsed.success) return error('E_INPUT');- const heavy = method === 'ai.generate' || method === 'tools.call';- if (heavy && this.heavyCalls >= 2) return error('E_BUSY');if (!this.available(def.capability) || (this.preview && method === 'tools.call')) return error('E_UNAVAILABLE');- if (heavy) this.heavyCalls++;s.seen.add(requestId);const controller = new AbortController();s.requests.set(requestId, controller);const signal = controller.signal;- const timer = setTimeout(() => controller.abort(), 5 * 60 * 1000);try {if (def.capability) this.requireCapability(s, def.capability);this.check(s, signal);@@ -196,7 +208,7 @@ export class WebAppRuntime {// The request signal, not provider wording, determines SDK cancellation.if (signal.aborted) return error('E_CANCELLED');throw cause;- } finally { clearTimeout(timer); s.requests.delete(requestId); if (heavy) this.heavyCalls--; }+ } finally { s.requests.delete(requestId); }}private check(s: Instance, signal: AbortSignal) {if (signal.aborted) return error('E_CANCELLED');@@ -232,7 +244,6 @@ export class WebAppRuntime {const saved = { version: 1, entries };boundedJson(saved, STORE_BYTES);if (Buffer.byteLength(JSON.stringify(saved, null, 2)) > STORE_BYTES) return error('E_LIMIT');- if (Object.keys(entries).length > STORE_KEYS) return error('E_LIMIT');await writeJson(file, saved, { shouldCommit: () => { this.check(s, signal); return true; } });this.check(s, signal);this.changed(s, input.scope, file);@@ -246,7 +257,7 @@ export class WebAppRuntime {const base = this.preview ? path.join(this.preview.storageRoot, input.scope) : webAppSandboxRoot(s.uid, s.bundle.key, input.scope);const root = path.join(base, 'files');const file = input.path ? path.join(root, ...input.path.split('/')) : root;- // Serialize quota checks and publication across instances of this app.+ // Serialize file operations and publication across instances of this app.let lock = this.stores.get(root);if (!lock) { lock = { mutex: new Mutex(), users: 0 }; this.stores.set(root, lock); }lock.users++;@@ -287,31 +298,28 @@ export class WebAppRuntime {this.changed(s, input.scope, file);return { removed: true };}- const files: Array<{ path: string; size: number }> = [];- let total = 0, visited = 0;- const walk = async (dir: string, depth: number): Promise<void> => {- guard(dir); if (depth > 8) return error('E_LIMIT');- let entries; try { entries = await fs.readdir(dir, { withFileTypes: true }); }- catch (err) { if ((err as NodeJS.ErrnoException).code === 'ENOENT') return; throw err; }- for (const entry of entries) {- if (isAtomicWriteTempPath(entry.name)) continue;- if (++visited > 2048) return error('E_LIMIT');- const abs = path.join(dir, entry.name); guard(abs);- if (entry.isDirectory()) await walk(abs, depth + 1);- else if (entry.isFile()) {- const st = await fs.lstat(abs); total += st.size;- files.push({ path: path.relative(root, abs).split(path.sep).join('/'), size: st.size });- if (files.length > 256 || total > 16 * 1024 * 1024) return error('E_LIMIT');- } else return error('E_FILE');- }- };- await walk(root, 0);- if (action === 'appFiles.list') return { files: files.sort((a, b) => a.path.localeCompare(b.path)) };+ if (action === 'appFiles.list') {+ const files: Array<{ path: string; size: number }> = [];+ const walk = async (dir: string, depth: number): Promise<void> => {+ guard(dir);+ let entries; try { entries = await fs.readdir(dir, { withFileTypes: true }); }+ catch (err) { if ((err as NodeJS.ErrnoException).code === 'ENOENT') return; throw err; }+ for (const entry of entries) {+ if (isAtomicWriteTempPath(entry.name)) continue;+ const abs = path.join(dir, entry.name); guard(abs);+ if (entry.isDirectory()) await walk(abs, depth + 1);+ else if (entry.isFile()) {+ const st = await fs.lstat(abs);+ files.push({ path: path.relative(root, abs).split(path.sep).join('/'), size: st.size });+ } else return error('E_FILE');+ }+ };+ await walk(root, 0);+ return { files: files.sort((a, b) => a.path.localeCompare(b.path)) };+ }const bytes = action === 'appFiles.writeText' ? Buffer.from(input.text, 'utf8') : Buffer.from(input.base64, 'base64');if (action === 'appFiles.writeBase64' && bytes.toString('base64') !== input.base64) return error('E_INPUT');if (bytes.length > MAX_FILE_BYTES) return error('E_LIMIT');- const old = files.find(item => item.path === input.path);- if ((!old && files.length >= 256) || total - (old?.size ?? 0) + bytes.length > 16 * 1024 * 1024) return error('E_LIMIT');guard(file);await writeBytesAtomic(file, bytes, { shouldCommit: () => { guard(file); return true; } });guard(file); this.changed(s, input.scope, file);@@ -334,7 +342,6 @@ export class WebAppRuntime {}), unsupported: UNSUPPORTED };case 'permissions.revoke': this.close(s.uid, s.owner, s.token); return { revoked: true };case 'files.pick': {- if (s.handles.size >= 16) return error('E_LIMIT');const selected = await this.host.pick(); this.check(s, signal);if (!selected) return null;const real = await fs.realpath(selected);@@ -345,7 +352,6 @@ export class WebAppRuntime {if (!stat.isFile() || stat.size > MAX_FILE_BYTES) return error('E_LIMIT');const bytes = await readFileBytes(file, () => this.check(s, signal));this.check(s, signal);- if (s.handles.size >= 16) return error('E_LIMIT');const handle = id(); const name = path.basename(real);s.handles.set(handle, { name, bytes });return { handle, name, size: bytes.length };@@ -359,6 +365,7 @@ export class WebAppRuntime {}case 'files.release': s.handles.delete(a.handle); return { released: true };case 'files.saveAs': {+ if (Buffer.byteLength(a.text, 'utf8') > MAX_FILE_BYTES) return error('E_LIMIT');const selected = await this.host.save(a.name); this.check(s, signal);if (!selected) return null;const parent = await fs.realpath(path.dirname(selected));diff --git a/src/main/features/web_apps/sdk.js b/src/main/features/web_apps/sdk.jsindex 39d26b93..370c441b 100644--- a/src/main/features/web_apps/sdk.js+++ b/src/main/features/web_apps/sdk.js@@ -2,14 +2,15 @@(function () {'use strict';const pending = new Map();+ let hostConnected = false;+ const documentId = Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('');let nextId = 0;function fail(code, message) { return Object.assign(new Error(message || code), { code }); }function call(method, args, options) {const opts = options || {};if (window.parent === window) return Promise.reject(fail('E_HOST_UNAVAILABLE'));if (opts.signal && opts.signal.aborted) return Promise.reject(fail('E_CANCELLED'));- if (pending.size >= 4) return Promise.reject(fail('E_LIMIT'));- const id = 'q' + (++nextId);+ const id = 'd' + documentId + 'q' + (++nextId);return new Promise((resolve, reject) => {const cancel = () => {parent.postMessage({ __orkasApp: 1, type: 'cancel', id }, '*');@@ -21,7 +22,7 @@if (opts.signal) opts.signal.removeEventListener('abort', cancel);if (ok) resolve(value); else reject(fail(value.code || 'E_FAILED', value.message));};- const timer = setTimeout(() => {+ const timer = hostConnected ? undefined : setTimeout(() => {parent.postMessage({ __orkasApp: 1, type: 'cancel', id }, '*');finish(false, { code: 'E_HOST_UNAVAILABLE' });}, 10000);@@ -34,12 +35,12 @@window.addEventListener('message', event => {if (event.source !== parent || !event.data || event.data.__orkasApp !== 1) return;const d = event.data; const p = pending.get(d.id); if (!p) return;+ if (!hostConnected && ['ack', 'progress', 'result'].includes(d.type)) {+ hostConnected = true;+ for (const request of pending.values()) clearTimeout(request.timer);+ }if (d.type === 'ack') {clearTimeout(p.timer);- p.timer = setTimeout(() => {- parent.postMessage({ __orkasApp: 1, type: 'cancel', id: d.id }, '*');- p.finish(false, { code: 'E_CANCELLED' });- }, 310000);} else if (d.type === 'progress') {if (typeof p.progress === 'function') { try { p.progress(d.value); } catch (_) {} }} else if (d.type === 'result') p.finish(d.ok, d.value);diff --git a/src/main/features/web_assist.ts b/src/main/features/web_assist.tsindex 6628f685..9f85728f 100644--- a/src/main/features/web_assist.ts+++ b/src/main/features/web_assist.ts@@ -12,7 +12,6 @@ import * as fs from 'node:fs';import * as path from 'node:path';import {- app,BrowserWindow,session,shell,@@ -485,17 +484,35 @@ export function normalizeWebAssistBounds(return { x, y, width, height };}-function chromeLikeUserAgent(): string {- const base = String(app.userAgentFallback || '');- if (!base) return '';- const appName = String(app.getName() || '');- let value = base.replace(/\sElectron\/\S+/iu, '');- if (appName) {- const escaped = appName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');- value = value.replace(new RegExp(`\\s${escaped}\\/\\S+`, 'iu'), '');- }- return value.replace(/\s{2,}/g, ' ').trim();-}+/*+ * We deliberately do NOT override the user agent here.+ *+ * Stripping the `Electron/<ver>` and app-name tokens looks like it should make+ * a page treat us as ordinary Chrome. It does the opposite, because of a fact+ * the stripping cannot change: Electron never sends the `Sec-CH-UA` client+ * hints, override or not (measured — Chromium builds them from user agent+ * *metadata*, which `setUserAgent()` cannot reach, and Electron ships none).+ *+ * So a UA washed down to plain Chrome contradicts itself: it claims to be+ * Chrome while omitting three headers every real Chrome sends. Leaving the+ * Electron/app token in place makes it an honest non-Chrome client instead,+ * and bot detection treats that far more kindly.+ *+ * Measured against openai.com (Cloudflare) on 2026-09-21, same Electron build,+ * 25s settle window, three runs each — only the UA handling varied:+ *+ * default UA, Electron token kept ......................... 3/3 HTTP 200+ * UA washed to plain Chrome (what this used to do) ........ 1/3 HTTP 403,+ * hard block+ *+ * Confirmed in-app after restart: the page that previously stalled on the+ * Cloudflare interstitial now renders in ~4s.+ *+ * If a future change really needs a custom UA, it must supply the metadata too+ * (`Network.setUserAgentOverride` via the debugger, with `userAgentMetadata`),+ * so the claimed brand and the client hints agree. Do not call `setUserAgent()`+ * on its own.+ *//** Let provider copy buttons work without granting pages clipboard read access. */export function isWebAssistPermissionAllowed(permission: unknown): boolean {@@ -1332,8 +1349,6 @@ function handleLoadFailure(record: WebAssistRecord, tab: WebAssistTabRecord, netfunction configureWebAssistPopup(popup: BrowserWindow, proxy: ReturnType<typeof prepareBrowserProxy>): void {const contents = popup.webContents;proxy.attach(contents);- const userAgent = chromeLikeUserAgent();- if (userAgent) contents.setUserAgent(userAgent);popup.setMenuBarVisibility(false);// Authorization popups may communicate with their opener, but cannot create// an unbounded window tree of their own.@@ -1458,8 +1473,6 @@ function mountTab(proxy.attach(contents);contents.on('before-input-event', () => { tab.lastUsedAt = Date.now(); tab.edited = true; });contents.on('before-mouse-event', () => { tab.lastUsedAt = Date.now(); });- const userAgent = chromeLikeUserAgent();- if (userAgent) contents.setUserAgent(userAgent);contents.setWindowOpenHandler((details) => {const { url, disposition } = details;if (records.get(record.owner.id) !== record || record.tabs.get(tab.id) !== tab) {diff --git a/src/main/index.ts b/src/main/index.tsindex dd935392..0a622eb1 100644--- a/src/main/index.ts+++ b/src/main/index.ts@@ -43,8 +43,8 @@ import { desktopPlatform, osVersion, preferredSystemLanguage } from './system_inimport {hardenedWebPreferences,installExternalNavigationGuard,- installOfflineHtmlPreviewNavigationGuard,- withOfflineHtmlPreviewPolicy,+ installHtmlPreviewNavigationGuard,+ withHtmlPreviewPolicy,} from './util/window-security';import {createRendererChannel,@@ -329,7 +329,7 @@ function createWindow(): BrowserWindow {(url) => shell.openExternal(url),(err) => log.warn('openExternal failed', { error: (err as Error)?.message || String(err) }),);- installOfflineHtmlPreviewNavigationGuard(win.webContents);+ installHtmlPreviewNavigationGuard(win.webContents);// Hijack Cmd/Ctrl+R / F5 uniformly:// - Packaged: refresh disabled (the App doesn't need reload).@@ -1198,7 +1198,7 @@ function registerChatMediaProtocol(): void {'chat_local',);return resolved.kind === 'html'- ? withOfflineHtmlPreviewPolicy(response)+ ? withHtmlPreviewPolicy(response): response;}diff --git a/src/main/ipc/index.ts b/src/main/ipc/index.tsindex 2a5766c0..15c5faee 100644--- a/src/main/ipc/index.ts+++ b/src/main/ipc/index.ts@@ -14,6 +14,7 @@* dropping it into `invokeHandlers` or `streamHandlers`.*/+import { readTextPreview } from '../util/text-preview';import { app, ipcMain, dialog, BrowserWindow, type WebContents } from 'electron';import * as users from '../features/users';@@ -205,7 +206,7 @@ const CHAT_PICK_EXTENSIONS = [...chatAttachments.ALLOWED_EXTENSIONS].map((ext) => ext.replace(/^\./, '')).sort();const CONTEXT_PICK_EXTENSIONS = [- 'md', 'markdown', 'txt', 'csv', 'tsv', 'json', 'yaml', 'yml', 'log',+ 'md', 'markdown', 'txt', 'csv', 'tsv', 'jsonl', 'ndjson', 'rst', 'tex', 'srt', 'vtt', 'json', 'yaml', 'yml', 'log','html', 'htm', 'xml', 'toml', 'ini', 'conf','py', 'pyi', 'ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs','sh', 'bash', 'zsh', 'ps1', 'cmd', 'bat', 'rb', 'go', 'rs', 'java', 'kt',@@ -1518,10 +1519,10 @@ const invokeHandlers: Record<string, InvokeHandler> = {return projectFiles.createProjectTextFile(ctx.userId, projectId, name);},- 'projects.files.readText': async ({ projectId, name }, ctx) => {+ 'projects.files.readText': async ({ projectId, name, preview }, ctx) => {if (!safeId(projectId)) throw new Error('invalid projectId');if (typeof name !== 'string' || !name) throw new Error('invalid name');- return projectFiles.readProjectTextFile(ctx.userId, projectId, name);+ return projectFiles.readProjectTextFile(ctx.userId, projectId, name, preview === true || preview === 'true');},'projects.files.updateText': async ({ projectId, name, content }, ctx) => {@@ -2156,7 +2157,7 @@ const invokeHandlers: Record<string, InvokeHandler> = {// `⋯` → "保存".'conversations.artifacts.save': async ({ cid, artifactId }, ctx) => {if (!safeId(cid)) throw new Error('invalid cid');- const r = savedApps.saveFromArtifact(ctx.userId, String(cid), String(artifactId || ''));+ const r = await savedApps.saveFromArtifact(ctx.userId, String(cid), String(artifactId || ''));if (!r.ok) throw new Error((r as { error?: string }).error || 'failed to save app');return { ok: true, id: (r as { id: string }).id, title: (r as { title: string }).title };},@@ -2180,7 +2181,7 @@ const invokeHandlers: Record<string, InvokeHandler> = {if (!await _isAllowedFileActionPath(ctx.userId, payload, norm)) {throw new Error('path is outside the user workspace');}- const r = savedApps.saveFromPath(ctx.userId, norm, {+ const r = await savedApps.saveFromPath(ctx.userId, norm, {title: payload?.title,sourceCid: payload?.cid,fenceRoots: await _ipcFileSandboxAllowedRoots(ctx.userId, payload),@@ -2668,8 +2669,8 @@ const invokeHandlers: Record<string, InvokeHandler> = {// ── Contexts (user-owned directory tree; vectorized via kb_indexer) ──'contexts.tree': async () => ({ tree: contexts.listContextsTree() }),- 'contexts.read': async ({ path }) => {- return contexts.readContextFile(path || '');+ 'contexts.read': async ({ path, preview }) => {+ return contexts.readContextFile(path || '', preview === true || preview === 'true');},'contexts.index': async () => ({@@ -3403,6 +3404,9 @@ const invokeHandlers: Record<string, InvokeHandler> = {if (!st.isFile()) return { ok: false, error: 'not_found' };const MAX_TEXT_BYTES = 2 * 1024 * 1024;const htmlPreviewLayoutOnly = payload?.htmlPreviewLayoutOnly === true;+ if (payload?.preview === true && /\.(csv|tsv)$/i.test(norm)) {+ return { ok: true, ...readTextPreview(norm), size: st.size };+ }if (!htmlPreviewLayoutOnly && st.size > MAX_TEXT_BYTES) {return { ok: false, error: 'too_large', size: st.size, cap: MAX_TEXT_BYTES };}diff --git a/src/main/ipc/web_apps.ts b/src/main/ipc/web_apps.tsindex a8ceb653..83df3e12 100644--- a/src/main/ipc/web_apps.ts+++ b/src/main/ipc/web_apps.ts@@ -3,6 +3,7 @@ import { z } from 'zod';import { catalogDocument } from '../features/web_apps/catalog';import { runtime, openApp, safeFailure } from '../features/web_apps/host';import { AppError } from '../features/web_apps/runtime';+import { safeExternalHttpUrl } from '../util/window-security';type Context = { userId: string; sender: WebContents };const sourceSchema = z.union([@@ -19,11 +20,16 @@ function bindOwner(sender: WebContents) {if (event.isMainFrame) runtime.closeOwner(sender.id);else {// Electron can omit frame for cross-process/document navigation.- const source = [event.frame?.url, event.initiator?.url]- .find(url => url?.startsWith('chat-app://app-'));- if (source) {- event.preventDefault();- runtime.closeOrigin(sender.id, source);+ const source = event.frame ? event.frame.url : event.initiator?.url;+ if (source?.startsWith('chat-app://app-')) {+ if (runtime.canNavigate(sender.id, source, event.url)) {+ runtime.cancelOriginRequests(sender.id, source);+ return;+ }+ // Rejected navigation leaves the current document usable. Only an+ // actual departure to a website revokes this application's authority.+ if (!safeExternalHttpUrl(event.url)) event.preventDefault();+ else runtime.closeOrigin(sender.id, source);}}});@@ -40,7 +46,12 @@ export const webAppInvokeHandlers = {const parsed = sourceSchema.safeParse(payload?.source);if (!parsed.success) throw new AppError('E_INPUT');bindOwner(ctx.sender);- return openApp(ctx.userId, ctx.sender.id, parsed.data as any);+ const opened = await openApp(ctx.userId, ctx.sender.id, parsed.data as any);+ if (ctx.sender.isDestroyed()) {+ runtime.closeOwner(ctx.sender.id);+ throw new AppError('E_CLOSED');+ }+ return opened;} catch (err) { return failure(err); }},'webApps.close': async (payload: any, ctx: Context) => {diff --git a/src/main/locales/de.json b/src/main/locales/de.jsonindex 8e226267..1f40f39b 100644--- a/src/main/locales/de.json+++ b/src/main/locales/de.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "Die Skill-Erstellung erfordert eine Korrektur. Ein Versuch wird unternommen.",+ "chat.skill_creation_corrected": "Der Skill wurde korrigiert und erstellt.",+ "chat.skill_creation_correction_failed": "Der Skill konnte nach einem Korrekturversuch nicht erstellt werden.","errors.reveal_invalid_path": "Verwenden Sie einen vollständigen lokalen Datei- oder Ordnerpfad.","errors.reveal_unavailable": "Die Datei oder der Ordner ist nicht verfügbar. Prüfen Sie, ob das Element noch existiert und zugänglich ist.","errors.reveal_failed": "Der Ordner konnte nicht geöffnet werden. Versuchen Sie, ihn im Dateimanager zu öffnen.",diff --git a/src/main/locales/en.json b/src/main/locales/en.jsonindex 15bc2d60..889a018b 100644--- a/src/main/locales/en.json+++ b/src/main/locales/en.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "Skill creation needs a correction. Trying once.",+ "chat.skill_creation_corrected": "The Skill was corrected and created.",+ "chat.skill_creation_correction_failed": "The Skill could not be created after one correction.","errors.reveal_invalid_path": "Use a full local file or folder path.","errors.reveal_unavailable": "The file or folder is unavailable. Check that it still exists and is accessible.","errors.reveal_failed": "Could not open the folder. Try opening it in your file manager.",diff --git a/src/main/locales/es.json b/src/main/locales/es.jsonindex 506aa417..0e68be2a 100644--- a/src/main/locales/es.json+++ b/src/main/locales/es.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "La creación del Skill necesita una corrección. Se intentará una vez.",+ "chat.skill_creation_corrected": "El Skill se corrigió y se creó.",+ "chat.skill_creation_correction_failed": "No se pudo crear el Skill tras una corrección.","errors.reveal_invalid_path": "Usa una ruta local completa de archivo o carpeta.","errors.reveal_unavailable": "El archivo o la carpeta no está disponible. Comprueba que siga existiendo y sea accesible.","errors.reveal_failed": "No se pudo abrir la carpeta. Intenta abrirla en tu gestor de archivos.",diff --git a/src/main/locales/fr.json b/src/main/locales/fr.jsonindex bd287249..2e8f2c59 100644--- a/src/main/locales/fr.json+++ b/src/main/locales/fr.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "La création du Skill nécessite une correction. Une tentative est en cours.",+ "chat.skill_creation_corrected": "Le Skill a été corrigé et créé.",+ "chat.skill_creation_correction_failed": "Le Skill n’a pas pu être créé après une correction.","errors.reveal_invalid_path": "Utilisez un chemin local complet de fichier ou de dossier.","errors.reveal_unavailable": "Le fichier ou dossier est indisponible. Vérifiez qu’il existe encore et qu’il est accessible.","errors.reveal_failed": "Impossible d’ouvrir le dossier. Essayez de l’ouvrir dans votre gestionnaire de fichiers.",diff --git a/src/main/locales/it.json b/src/main/locales/it.jsonindex 1e4fea05..6b5a4474 100644--- a/src/main/locales/it.json+++ b/src/main/locales/it.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "La creazione dello Skill richiede una correzione. Verrà effettuato un tentativo.",+ "chat.skill_creation_corrected": "Lo Skill è stato corretto e creato.",+ "chat.skill_creation_correction_failed": "Impossibile creare lo Skill dopo un tentativo di correzione.","errors.reveal_invalid_path": "Usa un percorso completo di un file o una cartella locale.","errors.reveal_unavailable": "Il file o la cartella non è disponibile. Verifica che esista ancora e sia accessibile.","errors.reveal_failed": "Impossibile aprire la cartella. Prova ad aprirla nel gestore file.",diff --git a/src/main/locales/ja.json b/src/main/locales/ja.jsonindex 2c74a21d..a16a420c 100644--- a/src/main/locales/ja.json+++ b/src/main/locales/ja.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "Skill の作成内容を一度だけ修正しています。",+ "chat.skill_creation_corrected": "Skill を修正して作成しました。",+ "chat.skill_creation_correction_failed": "一度修正しましたが、Skill を作成できませんでした。","errors.reveal_invalid_path": "ローカルのファイルまたはフォルダーの絶対パスを指定してください。","errors.reveal_unavailable": "ファイルまたはフォルダーを利用できません。存在し、アクセスできることを確認してください。","errors.reveal_failed": "フォルダーを開けませんでした。システムのファイルマネージャーで開いてみてください。",diff --git a/src/main/locales/ko.json b/src/main/locales/ko.jsonindex d1575796..82664d79 100644--- a/src/main/locales/ko.json+++ b/src/main/locales/ko.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "Skill 생성 내용을 한 번 수정하고 있습니다.",+ "chat.skill_creation_corrected": "Skill을 수정하여 생성했습니다.",+ "chat.skill_creation_correction_failed": "한 번 수정했지만 Skill을 생성하지 못했습니다.","errors.reveal_invalid_path": "로컬 파일 또는 폴더의 전체 경로를 사용해 주세요.","errors.reveal_unavailable": "파일 또는 폴더를 사용할 수 없습니다. 아직 존재하며 접근할 수 있는지 확인해 주세요.","errors.reveal_failed": "폴더를 열 수 없습니다. 파일 관리자에서 열어 보세요.",diff --git a/src/main/locales/pt.json b/src/main/locales/pt.jsonindex 6dad92e5..60b13066 100644--- a/src/main/locales/pt.json+++ b/src/main/locales/pt.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "A criação do Skill precisa de uma correção. Será feita uma tentativa.",+ "chat.skill_creation_corrected": "O Skill foi corrigido e criado.",+ "chat.skill_creation_correction_failed": "Não foi possível criar o Skill após uma correção.","errors.reveal_invalid_path": "Use o caminho completo de um arquivo ou pasta local.","errors.reveal_unavailable": "O arquivo ou a pasta não está disponível. Verifique se ainda existe e se está acessível.","errors.reveal_failed": "Não foi possível abrir a pasta. Tente abri-la no gerenciador de arquivos.",diff --git a/src/main/locales/ru.json b/src/main/locales/ru.jsonindex cced0ef5..7bca9e1c 100644--- a/src/main/locales/ru.json+++ b/src/main/locales/ru.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "Для создания Skill требуется исправление. Выполняется одна попытка.",+ "chat.skill_creation_corrected": "Skill исправлен и создан.",+ "chat.skill_creation_correction_failed": "Не удалось создать Skill после одной попытки исправления.","errors.reveal_invalid_path": "Укажите полный путь к локальному файлу или папке.","errors.reveal_unavailable": "Файл или папка недоступны. Убедитесь, что они существуют и у вас есть к ним доступ.","errors.reveal_failed": "Не удалось открыть папку. Попробуйте открыть её в файловом менеджере.",diff --git a/src/main/locales/zh.json b/src/main/locales/zh.jsonindex 7d0ee509..98217482 100644--- a/src/main/locales/zh.json+++ b/src/main/locales/zh.json@@ -1,4 +1,7 @@{+ "chat.skill_creation_correcting": "Skill 创建内容需要修正,正在尝试一次。",+ "chat.skill_creation_corrected": "已修正并创建 Skill。",+ "chat.skill_creation_correction_failed": "尝试修正一次后,仍未能创建 Skill。","errors.reveal_invalid_path": "请使用本机文件或文件夹的完整路径。","errors.reveal_unavailable": "文件或文件夹不可用,请确认它仍然存在且可以访问。","errors.reveal_failed": "无法打开文件夹,请尝试在系统文件管理器中打开。",diff --git a/src/main/logger-worker.js b/src/main/logger-worker.jsnew file mode 100644index 00000000..5ba1b882--- /dev/null+++ b/src/main/logger-worker.js@@ -0,0 +1,47 @@+'use strict';++// File creation, rotation and retention must never run on Electron's event loop.+const { parentPort, workerData } = require('node:worker_threads');+const fs = require('node:fs');+const path = require('node:path');+const log = require('electron-log/node');++const { dateKey, sweepLogDirectory } = require('./util/log-retention');++fs.mkdirSync(workerData.directory, { recursive: true });+log.transports.file.resolvePathFn = () => path.join(workerData.directory, `${dateKey(new Date())}.log`);+log.transports.file.maxSize = workerData.fileMaxBytes;+log.transports.file.level = workerData.fileLevel;+log.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] [{scope}] {text}';+log.transports.console.level = workerData.consoleLevel;+log.transports.console.format = '[{h}:{i}:{s}.{ms}] [{level}] [{scope}] {text}';+// electron-log can report file errors directly to its console transport. Those+// records contain a local filename and raw exception; keep only a fixed signal.+const consoleTransport = log.transports.console;+let activeData = null;+let writeFailed = false;+const safeConsole = Object.assign(function(message) {+ if (!activeData || message.data.length !== activeData.length+ || message.data.some((value, index) => value !== activeData[index])) {+ writeFailed = true;+ return;+ }+ const stream = message.level === 'error' || message.level === 'warn' ? process.stderr : process.stdout;+ if (workerData.consoleLevel !== false && stream.writableLength < 65536) consoleTransport(message);+}, consoleTransport);+log.transports.console = safeConsole;+Object.assign(log, { processInternalErrorFn: () => { writeFailed = true; } });+for (const stream of [process.stdout, process.stderr]) {+ stream.on('error', () => { safeConsole.level = false; consoleTransport.level = false; });+}+try { sweepLogDirectory(workerData.directory, workerData.retainDays, workerData.totalMaxBytes); } catch (_) { /* Failure to sweep must not disable new logs. */ }++parentPort.on('message', (message) => {+ // The parent caps messages including those already sent to this port.+ // A slow disk can stall this worker without growing its mailbox indefinitely.+ activeData = message.data;+ writeFailed = false;+ try { log.processMessage(message); parentPort.postMessage({ written: !writeFailed }); }+ catch (_) { parentPort.postMessage({ written: false }); }+ finally { activeData = null; }+});diff --git a/src/main/logger.ts b/src/main/logger.tsindex 266e73ad..6aba874d 100644--- a/src/main/logger.ts+++ b/src/main/logger.ts@@ -1,39 +1,17 @@/**- * Central logger — wraps `electron-log/main` with:- *- * - **Daily file rotation** via `resolvePathFn` returning a date-suffixed- * path (`YYYY-MM-DD.log`); electron-log opens a fresh file whenever- * the path changes, which naturally happens at midnight.- * - **Per-file size cap** so a chatty day doesn't produce a 1 GB file —- * once a file hits `FILE_MAX_BYTES`, electron-log archives it to- * `YYYY-MM-DD.old.log` and starts a new one for the same day.- * - **Retention sweep on boot**: drop files older than `RETAIN_DAYS`- * days, then (if the logs dir still exceeds `TOTAL_MAX_BYTES`) drop- * the oldest until under cap. Runs once per process start — no- * timers, no background work.- * - **Sensitive-field redaction** via an electron-log hook: any object- * property whose name matches our secret-looking key set is masked- * before the record is serialized. Stack traces and plain strings- * pass through unchanged.- * - **Scoped loggers** — `createLogger('auth')` yields a logger that- * stamps every record with `[auth]`, so the file reads at-a-glance- * as `[2026-04-20 14:23:45.123] [info] [auth] OAuth flow started`.- *- * The console transport stays on too: in dev you see the same events in- * the DevTools / terminal, with a lighter format.- *- * Renderer-side callers talk to us through the `orkas.log` IPC channel- * (wired in `main/ipc/index.ts`); every renderer record gets a- * `renderer/<module>` scope so it's distinguishable from main-side work.+ * Scoped logger with bounded, sanitized snapshots on the calling thread.+ * A worker owns file creation, daily/size rotation, retention and console I/O.+ * The mailbox reserves capacity for warnings/errors; saturation never waits+ * for diagnostic I/O. Renderer records arrive through the existing log IPC.*/-import * as fs from 'node:fs';-import * as path from 'node:path';+const { sweepLogDirectory } = require('./util/log-retention');import log from 'electron-log/main';import type { LogMessage } from 'electron-log';import { LOGS_DIR } from './paths';+import { createLogDelivery } from './util/log-delivery';import { maskLogId, sanitizeLogTextForUpload } from './util/log-sanitize';// ── Tunables ─────────────────────────────────────────────────────────────@@ -65,25 +43,6 @@ export function writeConsoleSafely(}}-// ── Date helpers ─────────────────────────────────────────────────────────--function dateKey(d: Date = new Date()): string {- const y = d.getFullYear();- const m = String(d.getMonth() + 1).padStart(2, '0');- const dd = String(d.getDate()).padStart(2, '0');- return `${y}-${m}-${dd}`;-}--// File names we manage: either `YYYY-MM-DD.log` (current) or the archive-// variants electron-log creates under size pressure (`...old.log`,-// `...1.log`, etc.). We leave anything else alone.-const LOG_FILE_RE = /^\d{4}-\d{2}-\d{2}.*\.log$/;--function datePrefixOf(name: string): string | null {- const m = name.match(/^(\d{4}-\d{2}-\d{2})/);- return m ? m[1] : null;-}-// ── Sensitive-field redaction ────────────────────────────────────────────// Keys that may carry secrets or PII — redacted regardless of nesting depth.@@ -130,19 +89,31 @@ const PATH_MASK = '***REDACTED_PATH***';* - Errors keep their name/message/stack shape, but message and stack text* are sanitized before they reach file/console transports.*/-export function redact(v: unknown, seen: WeakSet<object> = new WeakSet()): unknown {+export function redact(v: unknown, seen: WeakSet<object> = new WeakSet(), budget = { nodes: 0, chars: 0 }, depth = 0, wire = false): unknown {+ if (++budget.nodes > 128 || depth > 6) return '[truncated]';if (v === null || v === undefined) return v;- if (typeof v === 'string') return sanitizeLogTextForUpload(v);+ if (typeof v === 'string') {+ if (v.length > 4096 || budget.chars + v.length > 16384) return '[oversized text omitted]';+ budget.chars += v.length;+ return sanitizeLogTextForUpload(v);+ }+ if (typeof v === 'function' || typeof v === 'symbol') return '[unsupported]';if (typeof v !== 'object') return v;if (seen.has(v as object)) return '[circular]';seen.add(v as object);if (v instanceof Error) {- const out = new Error(sanitizeLogTextForUpload(v.message || ''));- out.name = sanitizeLogTextForUpload(v.name || 'Error');- if (typeof v.stack === 'string') out.stack = sanitizeLogTextForUpload(v.stack);- for (const [k, val] of Object.entries(v as Error & Record<string, unknown>)) {+ const out = new Error(String(redact(v.message || '', seen, budget, depth + 1, wire)));+ out.name = String(redact(v.name || 'Error', seen, budget, depth + 1, wire));+ if (typeof v.stack === 'string') out.stack = String(redact(v.stack, seen, budget, depth + 1, wire));+ let keys = 0;+ for (const k in v) {+ if (++keys > 32 || budget.nodes >= 128) break;+ if (k.length > 128 || k === '__proto__') continue;+ const descriptor = Object.getOwnPropertyDescriptor(v, k);+ if (!descriptor) continue;+ const val = 'value' in descriptor ? descriptor.value : '[accessor]';if (k === 'name' || k === 'message' || k === 'stack') continue;const key = k.toLowerCase();if (REDACT_KEYS.has(key)) {@@ -150,26 +121,39 @@ export function redact(v: unknown, seen: WeakSet<object> = new WeakSet()): unkno} else if (PRIVATE_FILE_KEYS.has(key) && typeof val === 'string') {(out as Error & Record<string, unknown>)[k] = PATH_MASK;} else if (MASK_ID_KEYS.has(key)) {- (out as Error & Record<string, unknown>)[k] = maskLogId(val);+ (out as Error & Record<string, unknown>)[k] = maskLogId(typeof val === 'string' && val.length <= 4096 ? val : '[omitted]');} else {- (out as Error & Record<string, unknown>)[k] = redact(val, seen);+ (out as Error & Record<string, unknown>)[k] = redact(val, seen, budget, depth + 1, wire);}}- return out;+ return wire ? { ...out, name: out.name, message: out.message, stack: out.stack } : out;}- if (Array.isArray(v)) return v.map((it) => redact(it, seen));+ if (Array.isArray(v)) {+ const items = [];+ for (let i = 0; i < Math.min(v.length, 32); i++) {+ const element = Object.getOwnPropertyDescriptor(v, String(i));+ items.push(element && 'value' in element ? redact(element.value, seen, budget, depth + 1, wire) : '[accessor]');+ }+ return items;+ }const out: Record<string, unknown> = {};- for (const [k, val] of Object.entries(v as Record<string, unknown>)) {+ let keys = 0;+ for (const k in v) {+ if (++keys > 32 || budget.nodes >= 128) { out.truncated = true; break; }+ const descriptor = Object.getOwnPropertyDescriptor(v, k);+ if (!descriptor) continue;+ const val = 'value' in descriptor ? descriptor.value : '[accessor]';+ if (k.length > 128 || k === '__proto__') continue;if (REDACT_KEYS.has(k.toLowerCase())) {out[k] = MASK;} else if (PRIVATE_FILE_KEYS.has(k.toLowerCase()) && typeof val === 'string') {out[k] = PATH_MASK;} else if (MASK_ID_KEYS.has(k.toLowerCase())) {- out[k] = maskLogId(val);+ out[k] = maskLogId(typeof val === 'string' && val.length <= 4096 ? val : '[omitted]');} else {- out[k] = redact(val, seen);+ out[k] = redact(val, seen, budget, depth + 1, wire);}}return out;@@ -177,87 +161,9 @@ export function redact(v: unknown, seen: WeakSet<object> = new WeakSet()): unkno// ── Retention sweep ──────────────────────────────────────────────────────-interface FileStat {- name: string;- full: string;- size: number;- mtimeMs: number;- datePrefix: string;-}--function listLogFiles(): FileStat[] {- let names: string[];- try { names = fs.readdirSync(LOGS_DIR); } catch { return []; }- const out: FileStat[] = [];- for (const name of names) {- if (!LOG_FILE_RE.test(name)) continue;- const full = path.join(LOGS_DIR, name);- let st: fs.Stats;- try { st = fs.statSync(full); } catch { continue; }- if (!st.isFile()) continue;- out.push({- name,- full,- size: st.size,- mtimeMs: st.mtimeMs,- datePrefix: datePrefixOf(name) || '',- });- }- return out;-}--/**- * Delete:- * 1. files whose date prefix is older than `RETAIN_DAYS`, OR- * 2. oldest-first until total size fits under `TOTAL_MAX_BYTES` (if it- * still overshoots after step 1).- *- * Today's live file is skipped — we never delete the file electron-log- * is currently appending to, even if size/age rules would otherwise hit- * it. Exposed for testing.- */-export function sweepLogs(now: Date = new Date()): {- removed: string[];- reason: Record<string, 'age' | 'size'>;-} {- const todayKey = dateKey(now);- const files = listLogFiles();- const ageLimitMs = now.getTime() - RETAIN_DAYS * 24 * 60 * 60 * 1000;-- const removed: string[] = [];- const reason: Record<string, 'age' | 'size'> = {};-- // Phase 1: drop anything older than RETAIN_DAYS. Use the date prefix —- // a mis-stamped mtime shouldn't spare a genuinely old file.- const survivors: FileStat[] = [];- for (const f of files) {- if (f.datePrefix && f.datePrefix < todayKey) {- const fileDate = new Date(`${f.datePrefix}T00:00:00`).getTime();- if (fileDate < ageLimitMs) {- try { fs.unlinkSync(f.full); removed.push(f.name); reason[f.name] = 'age'; continue; }- catch { /* fall through — leave the file, we'll try again next boot */ }- }- }- survivors.push(f);- }-- // Phase 2: if still over total cap, drop oldest (by date prefix, then- // mtime as tiebreak). Never touch today's live file.- let totalSize = survivors.reduce((s, f) => s + f.size, 0);- if (totalSize > TOTAL_MAX_BYTES) {- survivors.sort((a, b) => {- if (a.datePrefix !== b.datePrefix) return a.datePrefix.localeCompare(b.datePrefix);- return a.mtimeMs - b.mtimeMs;- });- for (const f of survivors) {- if (totalSize <= TOTAL_MAX_BYTES) break;- if (f.datePrefix === todayKey && f.name === `${todayKey}.log`) continue;- try { fs.unlinkSync(f.full); removed.push(f.name); reason[f.name] = 'size'; totalSize -= f.size; }- catch { /* keep going */ }- }- }-- return { removed, reason };+/** Retention policy is shared with the file worker; exposed for isolated tests. */+export function sweepLogs(now: Date = new Date()): { removed: string[]; reason: Record<string, 'age' | 'size'> } {+ return sweepLogDirectory(LOGS_DIR, RETAIN_DAYS, TOTAL_MAX_BYTES, now);}// ── Bootstrap ────────────────────────────────────────────────────────────@@ -272,65 +178,35 @@ export function initLogger(): void {if (_initialized) return;_initialized = true;- try { fs.mkdirSync(LOGS_DIR, { recursive: true }); } catch { /* noop */ }-- // Level config — `ORKAS_LOG_LEVEL` overrides in both environments; dev- // gets verbose console by default so you see activity while coding.- const fileLevel = (process.env.ORKAS_LOG_LEVEL as any) || 'info';- const consoleLevel = process.env.ORKAS_DEVTOOLS ? 'debug' : (process.env.ORKAS_LOG_LEVEL as any) || 'info';- const originalConsole = {- error: console.error.bind(console),- warn: console.warn.bind(console),- info: console.info.bind(console),- debug: console.debug.bind(console),- };- const disableConsoleTransport = () => {- log.transports.console.level = false;- };-- log.transports.file.level = fileLevel;- log.transports.console.level = consoleLevel;-- // Daily file path — electron-log reopens the file whenever the resolved- // path changes, so at midnight the next write naturally lands in a new- // `YYYY-MM-DD.log`. Size-based rotation (maxSize) produces `.old.log`- // siblings for the same day.- log.transports.file.resolvePathFn = () =>- path.join(LOGS_DIR, `${dateKey()}.log`);-- log.transports.file.maxSize = FILE_MAX_BYTES;-- log.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] [{scope}] {text}';- log.transports.console.format = '[{h}:{i}:{s}.{ms}] [{level}] [{scope}] {text}';- log.transports.console.writeFn = ({ message }) => {- const write = message.level === 'error'- ? originalConsole.error- : message.level === 'warn'- ? originalConsole.warn- : message.level === 'debug' || message.level === 'silly'- ? originalConsole.debug- : originalConsole.info;- writeConsoleSafely(write, message.data, disableConsoleTransport);- };-- // A closed pipe can surface asynchronously as a stream `error`, outside the- // transport's try/catch. Once that happens, stop console logging and retain- // the file transport; non-EPIPE stream failures keep their normal fatal- // semantics.- const handleOutputError = (error: Error & { code?: string }) => {- if (!isBrokenPipeError(error)) throw error;- disableConsoleTransport();- };- process.stdout?.on('error', handleOutputError);- process.stderr?.on('error', handleOutputError);-- // Redaction hook — runs for every record before formatting. Walk the- // `data` array (the arguments passed to log.info(...) etc.) and mask- // any object property named like a secret.- log.hooks.push((message: LogMessage) => {- message.data = message.data.map((d) => redact(d));- return message;- });+ const levels = ['error', 'warn', 'info', 'verbose', 'debug', 'silly'];+ const configuredLevel = process.env.ORKAS_LOG_LEVEL || 'info';+ const fileLevel = levels.includes(configuredLevel) ? configuredLevel : 'info';+ const consoleLevel = process.env.ORKAS_DEVTOOLS ? 'debug' : fileLevel;+ // Disable direct transports before starting the worker: startup failure must+ // never fall back to synchronous disk I/O in the business process.+ log.transports.file.level = false;+ log.transports.console.level = false;+ // Renderer logging already has its own bounded bridge and console mirror.+ // electron-log's implicit development IPC serializes raw data synchronously.+ if (log.transports.ipc) log.transports.ipc.level = false;+ try {+ const delivery = createLogDelivery({+ directory: LOGS_DIR, fileLevel, consoleLevel,+ retainDays: RETAIN_DAYS, fileMaxBytes: FILE_MAX_BYTES, totalMaxBytes: TOTAL_MAX_BYTES,+ });+ const transport = (message: LogMessage) => {+ if (!delivery.canAccept(message.level)) { delivery.noteDrop(); return; }+ try {+ delivery.send({+ level: message.level, scope: typeof message.scope === 'string' ? sanitizeLogTextForUpload(message.scope.slice(0, 128)) : '',+ date: message.date,+ data: redact(message.data.slice(0, 16), new WeakSet(), { nodes: 0, chars: 0 }, 0, true),+ });+ } catch (_) { /* Logging must not fail the business operation. */ }+ };+ (transport as any).level = levels[Math.max(levels.indexOf(fileLevel), levels.indexOf(consoleLevel))];+ (log.transports as any).background = transport;+ } catch (_) { /* Worker unavailable: retain business availability. */ }// Catch uncaught main-process errors. Must come after transports are// configured so the first error hits the file.@@ -342,7 +218,7 @@ export function initLogger(): void {// `data/logs/`. Without this, core-agent stream errors only print to// stderr and disappear when the dev terminal is closed — making "fetch// failed" impossible to retro-diagnose. The console transport writes each- // bridged record once through the original methods captured above.+ // bridged record once in the worker.try {const consoleScope = log.scope('console');console.info = (...args: any[]) => { try { consoleScope.info(...args); } catch { /* noop */ } };@@ -350,15 +226,8 @@ export function initLogger(): void {console.error = (...args: any[]) => { try { consoleScope.error(...args); } catch { /* noop */ } };} catch { /* noop */ }- // One-shot retention sweep. Log the outcome via the newly-initialized- // logger so the first line in today's file reads self-diagnostic.- const sweep = sweepLogs();- const boot = log.scope('logger');- if (sweep.removed.length > 0) {- boot.info(`retention sweep: removed ${sweep.removed.length} file(s)`, sweep.reason);- } else {- boot.info(`retention sweep: nothing to drop (${RETAIN_DAYS}d / ${Math.round(TOTAL_MAX_BYTES / 1024 / 1024)}MB caps)`);- }+ // The worker owns retention, file creation, rotation and console transport.+}// ── Scoped-logger factory ────────────────────────────────────────────────@@ -380,13 +249,13 @@ export interface Logger {* log.info('OAuth flow started', { provider: 'minimax-portal' });*/export function createLogger(module: string): Logger {- const scope = (module || 'app').trim() || 'app';+ const scope = typeof module === 'string' && module.length <= 128 ? sanitizeLogTextForUpload(module.trim()) || 'app' : 'app';const s = log.scope(scope);return {- error: (msg, ...args) => s.error(msg, ...args),- warn: (msg, ...args) => s.warn (msg, ...args),- info: (msg, ...args) => s.info (msg, ...args),- debug: (msg, ...args) => s.debug(msg, ...args),+ error: (msg, ...args) => { initLogger(); s.error(msg, ...args); },+ warn: (msg, ...args) => { initLogger(); s.warn(msg, ...args); },+ info: (msg, ...args) => { initLogger(); s.info(msg, ...args); },+ debug: (msg, ...args) => { initLogger(); s.debug(msg, ...args); },};}@@ -405,7 +274,7 @@ export function logFromRenderer(payload: {const module = String(p.module || 'app').trim() || 'app';const scoped = createLogger(`renderer/${module}`);const msg = String(p.message ?? '');- const args = Array.isArray(p.data) ? p.data : [];+ const args = Array.isArray(p.data) ? p.data.slice(0, 16) : [];switch ((p.level || 'info').toLowerCase()) {case 'error': return scoped.error(msg, ...args);case 'warn': return scoped.warn (msg, ...args);diff --git a/src/main/model/client.ts b/src/main/model/client.tsindex 2f87fcee..c339b16c 100644--- a/src/main/model/client.ts+++ b/src/main/model/client.ts@@ -25,11 +25,11 @@ import {} from './core-agent/client';export interface StreamEvent {- type: 'progress' | 'event' | 'delta' | 'final' | 'error' | 'done';+ type: 'progress' | 'event' | 'delta' | 'commentary-finalized' | 'final' | 'error' | 'done';text?: string;- /** Assistant text presentation channel. Present on model text deltas after- * provider-native phase preservation or structured tool-round inference. */- phase?: 'commentary' | 'final_answer';+ /** Assistant text presentation channel. Pending drafts are visible before+ * a structured boundary classifies them as commentary or a final answer. */+ phase?: 'commentary' | 'final_answer' | 'pending';event?: Record<string, unknown>;aborted?: boolean;/** Structured source for terminal failures. */diff --git a/src/main/model/core-agent/chat-history-tools.ts b/src/main/model/core-agent/chat-history-tools.tsindex 2cfea2a2..5bc7e26f 100644--- a/src/main/model/core-agent/chat-history-tools.ts+++ b/src/main/model/core-agent/chat-history-tools.ts@@ -301,9 +301,9 @@ function currentBoundaryIndex(function currentVisibleRows(rows: IndexedMessage[],currentMessageId?: string,-): IndexedMessage[] {+): IndexedMessage[] | undefined {const boundary = currentBoundaryIndex(rows, currentMessageId);- if (currentMessageId && boundary === undefined) return [];+ if (currentMessageId && boundary === undefined) return undefined;return rows.filter(({ index, message }) => ((boundary === undefined || index < boundary)&& !message.deleted_at@@ -369,6 +369,17 @@ export function diversifyChatHitsForTest(return out;}+function unavailableHistory(action: 'read' | 'search', reason: 'source_unavailable' | 'boundary_unavailable') {+ return {+ content: `chat_history(${action}): history_status=${reason}. `+ + (reason === 'source_unavailable'+ ? 'Conversation records could not be loaded.'+ : 'The current turn boundary could not be established; earlier records cannot be selected safely.')+ + ' This does not establish that history is empty.',+ isError: true as const,+ };+}+function createChatSearchTool(opts: ChatHistoryToolsOpts): AgentTool {const scopeEnum = [...allowedScopes(opts)];const hasCrossConversationScope = scopeEnum.some((scope) => scope !== 'current');@@ -424,10 +435,13 @@ function createChatSearchTool(opts: ChatHistoryToolsOpts): AgentTool {: !opts.projectId;let boundary: CurrentBoundary | undefined;+ let currentUnavailable: 'source_unavailable' | 'boundary_unavailable' | undefined;if (scope === 'current' || (includeCurrent && opts.currentCid && opts.currentMessageId)) {boundary = await cachedCurrentBoundaryIndex(opts.userId, opts.currentCid!, opts.currentMessageId);- if (scope === 'current' && opts.currentMessageId && !boundary) {- return { content: `No conversation-history results for "${query}".` };+ if (opts.currentMessageId && !boundary) {+ currentUnavailable = currentBoundarySource(opts.userId, opts.currentCid!)+ ? 'boundary_unavailable' : 'source_unavailable';+ if (scope === 'current') return unavailableHistory('search', currentUnavailable);}}const page = await search.searchChatsWithStatus(opts.userId, query, {@@ -452,9 +466,12 @@ function createChatSearchTool(opts: ChatHistoryToolsOpts): AgentTool {k,scope === 'current' ? Number.POSITIVE_INFINITY : MAX_HITS_PER_CONVERSATION,);- const status = page.indexComplete ? 'index_complete=true'+ const complete = page.indexComplete && !currentUnavailable;+ const status = currentUnavailable+ ? `index_complete=false: current_history_status=${currentUnavailable}; current-conversation records were excluded. Other matches do not establish complete history coverage.`+ : page.indexComplete ? 'index_complete=true': 'index_complete=false: history indexing is incomplete; matches may be missing. Retry search after indexing or read known records directly.';- if (!hits.length) return { content: page.indexComplete+ if (!hits.length) return { content: complete? `${status}\nNo conversation-history results for "${query}".`: `${status}\nNo matches in the indexed portion. This does not establish that the records are absent.`+ (scope === 'current' ? '\nRead recent records: {"action":"read","scope":"current","page":{"mode":"latest"},"include_process":true}' : '') };@@ -573,11 +590,13 @@ function createChatReadTool(opts: ChatHistoryToolsOpts): AgentTool {}const historySource = await historyMessageIndex(opts.userId, cid);+ if (!historySource.stamp) return unavailableHistory('read', 'source_unavailable');const indexedMessages = historySource.entries.map((entry) => ({ index: entry.index, message: entry.metadata }));const available = cid === opts.currentCid && opts.currentMessageId? currentVisibleRows(indexedMessages, opts.currentMessageId): indexedMessages.filter(({ message }) => !message.deleted_at && !message.dispatch);- if (!available.length) return { content: `chat_history(read): conversation has no messages — ${cid}` };+ if (!available) return unavailableHistory('read', 'boundary_unavailable');+ if (!available.length) return { content: `chat_history(read): No readable messages in the requested history scope — ${cid}` };let selected: IndexedMessage[];let note: string;diff --git a/src/main/model/core-agent/client.ts b/src/main/model/core-agent/client.tsindex f51effbc..ae7a3ab5 100644--- a/src/main/model/core-agent/client.ts+++ b/src/main/model/core-agent/client.ts@@ -83,7 +83,7 @@ export async function* stopStreamOnAbort<T>(events: AsyncIterable<T>,signal: AbortSignal,label = 'stream',- flushBeforeAbort?: () => T | null | undefined,+ flushBeforeAbort?: () => T | readonly T[] | null | undefined,): AsyncGenerator<T, void, unknown> {const iterator = events[Symbol.asyncIterator]();const aborted = Symbol('aborted');@@ -101,7 +101,10 @@ export async function* stopStreamOnAbort<T>(if (result === aborted) {try {const pending = flushBeforeAbort?.();- if (pending !== null && pending !== undefined) yield pending;+ if (pending !== null && pending !== undefined) {+ if (Array.isArray(pending)) yield* pending;+ else yield pending as T;+ }} catch (err) {log.warn('abortable stream flush failed', { label, error: logErrorSummary(err) });}@@ -2094,8 +2097,10 @@ export async function* streamChatWithModel(opts: ChatOptions): AsyncGenerator<St// terminal final/error synthesis. Only raw protocol events own liveness.let eventCount = 0;let flushReasoningBeforeAbort: (() => StreamEvent | null) | null = null;+ let flushTextBeforeAbort: (() => StreamEvent | null) | null = null;const mappedEvents = mapCoreAgentEvents(captureResult(rawEvents), {userId,+ streamUnphasedText: !!cid,failureTrackingScope,isDev: false,workingDir,@@ -2104,12 +2109,14 @@ export async function* streamChatWithModel(opts: ChatOptions): AsyncGenerator<StagentDisplayNameById,connectorDisplayNameById,registerReasoningAbortFlush: (flush) => { flushReasoningBeforeAbort = flush; },+ registerTextAbortFlush: (flush) => { flushTextBeforeAbort = flush; },});const abortableEvents = stopStreamOnAbort(mappedEvents,controller.signal,turnTag,- () => flushReasoningBeforeAbort?.() ?? null,+ () => [flushReasoningBeforeAbort?.(), flushTextBeforeAbort?.()]+ .filter((event): event is StreamEvent => !!event),);for await (const ev of abortableEvents) {eventCount += 1;diff --git a/src/main/model/core-agent/event-mapper.ts b/src/main/model/core-agent/event-mapper.tsindex 19466cd2..896e2183 100644--- a/src/main/model/core-agent/event-mapper.ts+++ b/src/main/model/core-agent/event-mapper.ts@@ -84,6 +84,13 @@ function commonPrefixLength(left: string, right: string): number {export interface MapCoreAgentEventsOptions {userId?: string;+ /** Task conversations can display an unclassified draft and move it into+ * the process body at a structured commentary boundary. Authoring consumers+ * retain their existing protocol buffering unless they opt in. */+ streamUnphasedText?: boolean;+ /** Preserve an already displayed task draft when cancellation interrupts+ * the source before a structured phase boundary arrives. */+ registerTextAbortFlush?: (flush: (() => StreamEvent | null) | null) => void;/** Opaque identity of the persisted model session that owns this run.* Repeated-failure guidance is isolated by object identity and is released* automatically when the session cache releases the underlying session. */@@ -692,6 +699,14 @@ export async function* mapCoreAgentEvents(if (!pendingUnphasedText.length) return [];const pieces = pendingUnphasedText;pendingUnphasedText = [];+ if (opts.streamUnphasedText) {+ const text = pieces.join('');+ if (phase === 'final_answer') {+ finalText += text;+ return [];+ }+ return [{ type: 'commentary-finalized', text }];+ }return phasedTextEvents(pieces, phase);};@@ -784,6 +799,9 @@ export async function* mapCoreAgentEvents(};opts.registerReasoningAbortFlush?.(() => endThinking());+ opts.registerTextAbortFlush?.(() => opts.streamUnphasedText+ ? flushPendingText('commentary')[0] ?? null+ : null);const eventIterator = events[Symbol.asyncIterator]();let nextEvent: Promise<IteratorResult<AgentRunEvent>> | null = null;@@ -845,9 +863,13 @@ export async function* mapCoreAgentEvents(} else {// Chat Completions, Anthropic Messages, Gemini GenerateContent and// other phase-less protocols all expose structured tool boundaries.- // Hold only this provider round until one of those boundaries tells- // us whether the text is commentary or the terminal answer.+ // Retain this round for classification. Task consumers can display+ // the draft immediately, before a boundary establishes its phase.pendingUnphasedText.push(piece);+ if (opts.streamUnphasedText) {+ hasAssistantText = true;+ yield { type: 'delta', text: piece, phase: 'pending' };+ }}break;}@@ -1253,6 +1275,7 @@ export async function* mapCoreAgentEvents(}} finally {opts.registerReasoningAbortFlush?.(null);+ opts.registerTextAbortFlush?.(null);}// A stream that ended without a provider/done boundary did not establish adiff --git a/src/main/model/core-agent/file-tools.ts b/src/main/model/core-agent/file-tools.tsindex 2a06dc93..320ff4c3 100644--- a/src/main/model/core-agent/file-tools.ts+++ b/src/main/model/core-agent/file-tools.ts@@ -26,6 +26,7 @@* write_file) live in local-tools.ts.*/+import { lookupSkillRuntimeRef } from './skill-runtime-ref';import { fileFailure, type FileFailureDiagnostic } from '../../../core-agent/src/tools/file-diagnostics';import { createHash } from 'node:crypto';import * as fs from 'node:fs';@@ -210,8 +211,8 @@ function resolveAbs(ctx: ToolContext, p: string): string {}type RequestedPathResolution =- | { abs: string; displayPath: string; skillRef?: string; error?: never }- | { abs?: never; displayPath?: never; skillRef?: never; error: string };+ | { abs: string; displayPath: string; skillRef?: string; error?: never; diagnostic?: never }+ | { abs?: never; displayPath?: never; skillRef?: never; error: string; diagnostic?: FileFailureDiagnostic };/** Resolve the virtual Skill namespace before the ordinary cwd resolver. The* final containment check is the same symlink-safe guard used by all file@@ -233,18 +234,9 @@ function resolveRequestedPath(const slash = tail.indexOf('/');const ref = (slash >= 0 ? tail.slice(0, slash) : tail).trim();const relative = slash >= 0 ? tail.slice(slash + 1) : '';- if (!ref || ref === '.' || ref === '..') {- return { error: errText('E_SKILL_REF_INVALID', 'Use @skill/<read-ref> with the exact read ref advertised in Available skills.') };- }- const binding = opts.skillRuntimeBindings?.get(ref);- if (!binding) {- return {- error: errText(- 'E_SKILL_NOT_AVAILABLE',- `@skill/${ref} is not bound for this run. Use an exact read ref from the current Available skills block.`,- ),- };- }+ const lookup = lookupSkillRuntimeRef(ref, opts.skillRuntimeBindings);+ if (lookup.error) return { error: lookup.error, diagnostic: lookup.diagnostic };+ const binding = lookup.binding!;const segments = relative ? relative.split('/') : [];if (segments.some((segment) => !segment || segment === '.' || segment === '..')) {@@ -548,8 +540,8 @@ function taggedReadRangeSchema(): Record<string, unknown> {enum: ['line', 'char'],description: 'Addressing unit: line is 1-based/inclusive; char is 0-based with an exclusive end.',},- start: { type: 'integer', description: 'Start position in the selected unit.' },- end: { type: 'integer', description: 'End position: inclusive for line, exclusive for char.' },+ start: { type: 'integer', minimum: 0, description: 'Start position in the selected unit.' },+ end: { type: 'integer', minimum: 0, description: 'End position: inclusive for line, exclusive for char.' },},required: ['unit', 'start', 'end'],};@@ -643,7 +635,7 @@ function createReadFileTool(type: 'object',additionalProperties: false,properties: {- path: { type: 'string', description: 'Path relative to the working directory, a visible absolute path, or @skill/<read-ref>[/relative-path] for a Skill advertised in this run.' },+ path: { type: 'string', minLength: 1, description: 'Path relative to the working directory, a visible absolute path, or @skill/<read-ref>[/relative-path] for a Skill advertised in this run.' },range: taggedReadRangeSchema(),},required: ['path'],@@ -652,7 +644,7 @@ function createReadFileTool(const raw = String(input.path ?? '');if (!raw) return { content: errText('E_BAD_INPUT', '`path` is required'), isError: true };const resolvedPath = resolveRequestedPath(opts, ctx, raw);- if (resolvedPath.error) return { content: resolvedPath.error, isError: true };+ if (resolvedPath.error) return { content: resolvedPath.error, isError: true, observations: { fileFailure: resolvedPath.diagnostic } };const { abs, displayPath } = resolvedPath;const parsedAddress = parseReadAddress(input);if (parsedAddress.error) {@@ -1094,6 +1086,15 @@ function readFilesInlineTokenBudget(ctx: ToolContext, verbatimDocument = false):return Math.max(0, Math.floor(Math.min(...candidates)));}+/** Retain one bounded, structured failure through both read envelopes. */+function readBatchFailure(results: readonly ToolResult[], inputFailure?: FileFailureDiagnostic): Pick<ToolObservations, 'fileFailure'> {+ if (inputFailure) return { fileFailure: inputFailure };+ const index = results.findIndex(result => result.observations?.fileFailure);+ return index < 0 ? {} : {+ fileFailure: { ...results[index].observations!.fileFailure!, item_index: index },+ };+}+/** Unified one-or-many reader. It deliberately delegates every item to the* same internal reader so scope checks, rich-file handling,* line-number rendering, skill attribution, and OCC stamps stay identical. */@@ -1119,7 +1120,7 @@ function createReadFilesTool(opts: FileToolsOpts): AgentTool {type: 'object',additionalProperties: false,properties: {- path: { type: 'string', description: 'Path relative to the working directory, a visible absolute path, or @skill/<read-ref>[/relative-path] for a Skill advertised in this run.' },+ path: { type: 'string', minLength: 1, description: 'Path relative to the working directory, a visible absolute path, or @skill/<read-ref>[/relative-path] for a Skill advertised in this run.' },range: taggedReadRangeSchema(),},required: ['path'],@@ -1268,7 +1269,7 @@ function createReadFilesTool(opts: FileToolsOpts): AgentTool {content,...(errors === files.length ? { isError: true } : {}),observations: {- ...(inputFailure ? { fileFailure: inputFailure } : {}),+ ...readBatchFailure(results, inputFailure),fileReads: results.flatMap((result) => result.observations?.fileReads ?? []),fileReadBatch: fileReadBatchObservation(files.map((file) => (file.ok === false@@ -1329,7 +1330,7 @@ function createReadFilesTool(opts: FileToolsOpts): AgentTool {...(errors === results.length ? { isError: true } : {}),...(includesVerbatimDocument ? { verbatimDocument: true } : {}),observations: {- ...(inputFailure ? { fileFailure: inputFailure } : {}),+ ...readBatchFailure(results, inputFailure),fileReads: results.flatMap((result) => result.observations?.fileReads ?? []),fileReadBatch: fileReadBatchObservation(results.map((result) => ({ok: !result.isError,@@ -1457,7 +1458,7 @@ function createSearchFilesTool(opts: FileToolsOpts): AgentTool {? resolveRequestedPath(opts, ctx, requestedRootInput, { bareSkillRefTarget: 'root' }): null;if (requestedRootResolution?.error) {- return { content: requestedRootResolution.error, isError: true };+ return { content: requestedRootResolution.error, isError: true, observations: { fileFailure: requestedRootResolution.diagnostic } };}const requestedRoot = requestedRootResolution?.abs || '';const requestedRootDisplay = requestedRootResolution?.displayPath || requestedRoot;@@ -1747,7 +1748,7 @@ function createGrepFilesTool(opts: FileToolsOpts): AgentTool {? resolveRequestedPath(opts, ctx, requestedRootInput, { bareSkillRefTarget: 'root' }): null;if (requestedRootResolution?.error) {- return { content: requestedRootResolution.error, isError: true };+ return { content: requestedRootResolution.error, isError: true, observations: { fileFailure: requestedRootResolution.diagnostic } };}const requestedRoot = requestedRootResolution?.abs || '';const requestedRootDisplay = requestedRootResolution?.displayPath || requestedRoot;@@ -1990,7 +1991,7 @@ function createListFilesTool(opts: FileToolsOpts): AgentTool {const raw = String(input.path ?? '');if (!raw) return { content: errText('E_BAD_INPUT', '`path` is required'), isError: true };const resolvedPath = resolveRequestedPath(opts, ctx, raw, { bareSkillRefTarget: 'root' });- if (resolvedPath.error) return { content: resolvedPath.error, isError: true };+ if (resolvedPath.error) return { content: resolvedPath.error, isError: true, observations: { fileFailure: resolvedPath.diagnostic } };const { abs, displayPath } = resolvedPath;const scopeErr = await gatePathAccess(opts, abs, 'list_files', ctx);diff --git a/src/main/model/core-agent/local-tools.ts b/src/main/model/core-agent/local-tools.tsindex 15840fc6..3743bcee 100644--- a/src/main/model/core-agent/local-tools.ts+++ b/src/main/model/core-agent/local-tools.ts@@ -65,6 +65,7 @@ import {type ApplyPatchCommittedFile,} from '../../../core-agent/src/tools/apply-patch';import { managedCommandsEnabled } from '../../../core-agent/src/tools/command-sessions';+import { parsePowerShellChain } from '../../../core-agent/src/sandbox/powershell-chain';import { getProcessSessionTools } from '../../../core-agent/src/tools/process-session';import type { FileChangeObservation } from '../../../core-agent/src/tools/base';import {@@ -133,7 +134,8 @@ import {waitForConfirmationVisible as waitForDeleteConfirmationVisible,} from './delete-file-confirm';import { fileEditLock } from '../../util/locks';-import { fileFailure } from '../../../core-agent/src/tools/file-diagnostics';+import { fileFailure, type FileFailureDiagnostic } from '../../../core-agent/src/tools/file-diagnostics';+import { lookupSkillRuntimeRef } from './skill-runtime-ref';import { firstMatchOffsets, matchRecoveryContext } from '../../../core-agent/src/tools/match-context';import { checkEditFreshness, forgetRead, getReadState, recordRead } from './read-tracker';import {@@ -1419,7 +1421,7 @@ async function executeCoreBashWithOutputTracking(const outputDir = workingDir;const command = String(input.command ?? '');const skillRuntime = resolveRunSkillRuntimeBinding(opts, command);- if (skillRuntime.error) return { content: skillRuntime.error, isError: true };+ if (skillRuntime.error) return { content: skillRuntime.error, isError: true, observations: { fileFailure: skillRuntime.diagnostic } };const removeEmptyWorkingDirOnCompletion = !fs.existsSync(workingDir);if (removeEmptyWorkingDirOnCompletion) {try { fs.mkdirSync(workingDir, { recursive: true }); }@@ -3547,6 +3549,7 @@ function extractRunSkillRefs(command: string, depth = 0, hostPlatform: NodeJS.Pltype RunSkillRuntimeResolution = {binding?: SkillRuntimeBinding;error?: string;+ diagnostic?: FileFailureDiagnostic;};function resolveRunSkillRuntimeBinding(@@ -3568,15 +3571,9 @@ function resolveRunSkillRuntimeBinding(const roots = new Set<string>();const bindings: SkillRuntimeBinding[] = [];for (const ref of refs) {- const binding = opts.skillRuntimeBindings.get(ref);- if (!binding) {- return {- error: errText(- 'E_SKILL_NOT_AVAILABLE',- `@skill/${ref} is not bound for this run. Read and use the exact read ref from the current Available skills entry.`,- ),- };- }+ const lookup = lookupSkillRuntimeRef(ref, opts.skillRuntimeBindings);+ if (lookup.error) return lookup;+ const binding = lookup.binding!;roots.add(path.resolve(binding.root));bindings.push(binding);}@@ -3904,20 +3901,22 @@ function guardVideoStudioUnmanagedRuntime(opts: LocalToolsOpts, command: string,function unquotedShellSurface(command: string): string {let quote = '';- let escaped = false;let out = '';- for (const ch of command) {- if (escaped) {- out += quote ? ' ' : ch;- escaped = false;- continue;- }- if (ch === '\\' || (quote === '"' && ch === '`')) {- escaped = true;- out += quote ? ' ' : ch;+ for (let i = 0; i < command.length; i++) {+ const ch = command[i];+ // PowerShell backslashes are literal, including immediately before a+ // closing path quote. Backticks escape outside single-quoted strings.+ if (ch === '`' && quote !== "'") {+ out += ' ';+ if (i + 1 < command.length) out += command[++i] === '\n' ? '\n' : ' ';continue;}if (quote) {+ if (ch === quote && command[i + 1] === quote) {+ out += ' ';+ i++;+ continue;+ }if (ch === quote) quote = '';out += ch === '\n' ? '\n' : ' ';continue;@@ -3941,9 +3940,12 @@ export function windowsPowerShellCompatibilityError(platform: NodeJS.Platform = process.platform,): string | null {if (platform !== 'win32') return null;- const surface = unquotedShellSurface(String(command || ''));+ const chain = parsePowerShellChain(String(command || ''));+ const surface = chain.kind === 'chain'+ ? chain.commands.map(unquotedShellSurface).join('\n')+ : unquotedShellSurface(String(command || ''));const findings: string[] = [];- if (/&&|\|\|/.test(surface)) findings.push('POSIX &&/|| chaining');+ if (chain.kind === 'unsupported' || /&&|\|\|/.test(surface)) findings.push('unsupported complex &&/|| chain');if (/<<-?\s*[A-Za-z_][A-Za-z0-9_]*/.test(surface)) findings.push('POSIX heredoc');if (/(?:^|[;\r\n]\s*)(?:source|export)\b/m.test(surface)) findings.push('source/export');if (/(?:^|[;|\r\n]\s*)head(?:\s|$)/m.test(surface)) findings.push('head');@@ -3955,7 +3957,9 @@ export function windowsPowerShellCompatibilityError(return errText('E_SHELL_SYNTAX_MISMATCH',`host shell is Windows PowerShell, but the command contains ${findings.join(', ')}. `- + 'Rewrite it as PowerShell before retrying: use `;` for sequencing, `$env:NAME = value` for environment variables, '+ + 'Simple single-line &&/|| command chains are supported. For complex control flow, use PowerShell: '+ + '`;` runs unconditionally; `if ($?) { ... }` runs after success; `if (-not $?) { ... }` runs after failure. '+ + 'Use `$env:NAME = value` for environment variables, '+ '`$null` for discarded output, `Select-Object -First N` for head, and a `[System.IO.Path]::GetTempFileName()` or '+ '`New-Item` temporary path. Create directories with `New-Item -ItemType Directory -Force -Path ...`. '+ 'For a multi-line script, write a `.ps1` file and invoke it with PowerShell.',@@ -4164,7 +4168,7 @@ function createBashTool(opts: LocalToolsOpts): AgentTool {if (!ctx.workingDir) {ctx = { ...ctx, state: { ...ctx.state } };const skillRuntime = resolveRunSkillRuntimeBinding(opts, command);- if (skillRuntime.error) return { content: skillRuntime.error, isError: true };+ if (skillRuntime.error) return { content: skillRuntime.error, isError: true, observations: { fileFailure: skillRuntime.diagnostic } };const restoreSkillRuntimeEnv = withRunSkillRuntimeEnv(ctx,skillRuntime.binding,@@ -4239,7 +4243,7 @@ async function gateInteractiveCliStart(return { content: unsupportedNoBrowserAuthErr, isError: true };}const skillRuntime = resolveRunSkillRuntimeBinding(opts, command);- if (skillRuntime.error) return { content: skillRuntime.error, isError: true };+ if (skillRuntime.error) return { content: skillRuntime.error, isError: true, observations: { fileFailure: skillRuntime.diagnostic } };if (skillRuntime.binding) {return {content: errText(@@ -5952,20 +5956,20 @@ function createCreateArtifactTool(opts: LocalToolsOpts): AgentTool {return {name: 'create_artifact',description:- 'Create and validate a self-contained offline interactive HTML/CSS/JS app rendered in chat; use a dashboard for static summaries. Remote and out-of-directory URLs are blocked: bundle authorized assets in files or use data/blob URLs. The app must expose an operable control with an observable effect.',+ 'Create an interactive HTML/CSS/JS app rendered in chat, with preview diagnostics. Bundled resources and external HTTP(S) URLs are supported; use a dashboard for static summaries.',inputSchema: {type: 'object',properties: {title: { type: 'string', description: 'Short title shown above the embedded app. Optional; defaults to "Interactive app".' },files: {type: 'array',- description: 'Relative local app files with a top-level index.html. Maximum 20 files, 256KB each and 1MB total. Apps may load __orkas/bridge.js for send and resize.',+ description: 'Relative local app files with a top-level index.html. Apps may load __orkas/bridge.js for send and resize.',items: {type: 'object',properties: {path: { type: 'string', description: 'Forward-slash relative path, e.g. "index.html" or "assets/app.js". No "..", no leading "/", no dotfiles, no "__orkas/...".' },content: { type: 'string', description: 'File contents. UTF-8 text by default; for a binary extension set "encoding":"base64" and pass base64.' },- encoding: { type: 'string', enum: ['utf8', 'base64'], description: 'Default "utf8". Use "base64" for image / font / wasm files.' },+ encoding: { type: 'string', enum: ['utf8', 'base64'], description: 'Default "utf8". Use "base64" for binary resources.' },},required: ['path', 'content'],},@@ -5979,7 +5983,7 @@ function createCreateArtifactTool(opts: LocalToolsOpts): AgentTool {if (!uid || !cid) {return { content: errText('E_NO_CONVERSATION', 'create_artifact is only available inside a conversation.'), isError: true };}- const r = chatArtifacts.createArtifact(uid, cid, opts.agentId || '', {+ const r = await chatArtifacts.createArtifact(uid, cid, opts.agentId || '', {title: (input as { title?: unknown }).title,files: (input as { files?: unknown }).files,});@@ -6037,52 +6041,8 @@ function createCreateArtifactTool(opts: LocalToolsOpts): AgentTool {});smoke = await runSmoke(path.join(resolved.dirPath, 'index.html'));} catch (err) {- const discarded = chatArtifacts.discardArtifact(uid, cid, r.artifactId);- log.warn('create_artifact interaction smoke failed to run', {- user_id: maskId(uid),- cid: maskId(cid),- artifact_id: maskId(r.artifactId),- discarded: discarded.ok,- error: logErrorRef(err),- });- return {- content: errText('E_ARTIFACT_SMOKE_FAILED', (err as Error).message || 'artifact interaction smoke failed to run'),- isError: true,- };- }- if (!smoke.ok) {- // Keep every known blocker. The shared tool-result token policy owns- // oversized-output persistence; truncating here would discard repair- // material before Result Store can preserve it.- const blockers = smoke.blockers.join('; ') || 'no observable interaction completed';- const hasBlockedExternalAsset = smoke.blockers.some((blocker) => (- /(?:external|remote|out[- ]of[- ]directory|network).{0,48}(?:block|denied|unavailable)|(?:image|resource).{0,48}(?:failed|blocked|unavailable)/i- .test(blocker)- ));- const recovery = hasBlockedExternalAsset- ? [- 'Remote and out-of-directory resources cannot load inside the artifact sandbox.',- 'If the user explicitly supplied or required an asset, acquire it through an available authorized tool and bundle it under files (base64 for binary content).',- 'Otherwise replace model-chosen remote assets with local CSS/SVG or data/blob content.',- 'Do not retry the same remote URLs; repair the bundle and call create_artifact again.',- ].join(' ')- : 'Repair the app behavior and call create_artifact again.';- const discarded = chatArtifacts.discardArtifact(uid, cid, r.artifactId);- log.warn('create_artifact interaction smoke rejected artifact', {- user_id: maskId(uid),- cid: maskId(cid),- artifact_id: maskId(r.artifactId),- controls_exercised: smoke.controlsExercised,- observable_effects: smoke.observableEffects,- discarded: discarded.ok,- });- return {- content: errText(- 'E_ARTIFACT_INTERACTION_INCOMPLETE',- `${blockers}. ${recovery}`,- ),- isError: true,- };+ log.warn('create_artifact preview unavailable', { error: logErrorRef(err) });+ smoke = { ok: false, blockers: ['Preview unavailable; verify the application in its window.'] };}if (opts.onArtifactCreated) {try { opts.onArtifactCreated({ id: r.artifactId, title: r.title }); }@@ -6099,10 +6059,10 @@ function createCreateArtifactTool(opts: LocalToolsOpts): AgentTool {// description: this is the text the model actually reads back, and an// artifact that does not know the box it lands in writes a full-screen// shell and ships it clipped.- const smokeWarnings = (smoke.warnings ?? []).slice(0, 3);+ const smokeWarnings = [...smoke.blockers, ...(smoke.warnings ?? [])];return {content:- `Artifact "${r.title}" created (id ${r.artifactId}); interaction smoke passed ` ++ `Artifact "${r.title}" created (id ${r.artifactId}); preview ${smoke.ok ? 'checks passed' : 'reported issues'} ` +`(${smoke.controlsExercised ?? 0} control(s), ${smoke.observableEffects ?? 0} observable effect(s)) ` +`at desktop, chat-embed, and mobile viewports. ` +`It is now shown to the user inside this reply, so do NOT paste its HTML in your message. ` +diff --git a/src/main/model/core-agent/runner.ts b/src/main/model/core-agent/runner.tsindex a08d2dc6..e54b8160 100644--- a/src/main/model/core-agent/runner.ts+++ b/src/main/model/core-agent/runner.ts@@ -1886,7 +1886,7 @@ async function buildRotatingProvider(* and no automatic generation retries. No Agent session or prompt assembly. */export async function generateWebAppText(uid: string,- input: { prompt: string; maxTokens: number },+ input: { prompt: string; maxTokens?: number },signal: AbortSignal,progress: (event: { type: 'delta'; text: string }) => void,): Promise<{ text: string; usage: Record<string, number>; stopReason: string }> {@@ -1895,15 +1895,14 @@ export async function generateWebAppText(if (signal.aborted || getActiveUserId() !== uid) throw new Error('app generation cancelled');};check();- if (!input.prompt || input.prompt.length > 32000 || !Number.isInteger(input.maxTokens)- || input.maxTokens < 1 || input.maxTokens > 4096) throw new Error('invalid app model input');+ if (!input.prompt || (input.maxTokens !== undefined && (!Number.isSafeInteger(input.maxTokens) || input.maxTokens < 1))) throw new Error('invalid app model input');const group = (await pickChatEntryGroup()).slice(0, 1);check();if (!group.length) throw new Error('app model unavailable');const mod = await ca();const primary = group[0];const provider = await buildRotatingProvider(mod, primary.provider, group, undefined, undefined, undefined,- 45_000, uid, { networkRetryAttempts: 0, normalEmptyRetryAttempts: 0 });+ undefined, uid, { networkRetryAttempts: 0, normalEmptyRetryAttempts: 0 });check();let text = '';let stopReason = '';@@ -1918,7 +1917,6 @@ export async function generateWebAppText(}if (event.type === 'text_delta') {text += event.text;- if (text.length > 256 * 1024) throw new Error('app model output limit');// Provider chunks can exceed the bridge event budget; split deterministically.for (let at = 0; at < event.text.length; at += 4000) progress({ type: 'delta', text: event.text.slice(at, at + 4000) });}@@ -1938,6 +1936,49 @@ export async function generateWebAppText(return { text, usage, stopReason };}+/** One tools-free Skill repair request. The feature supplies only the rejected+ * proposal and current request; no task replay, session, roster or discovery. */+export async function completeSkillCreationCorrection(input: {+ userId: string;+ cid: string;+ turnId: string;+ message: string;+ signal: AbortSignal;+}): Promise<string> {+ const { getActiveUserId } = await import('../../features/users');+ const check = () => {+ if (input.signal.aborted || getActiveUserId() !== input.userId) {+ throw new Error('Skill correction cancelled');+ }+ };+ check();+ if (!input.message || input.message.length > 64_000) throw new Error('Skill correction input limit');+ const group = (await pickChatEntryGroup()).slice(0, 1);+ check();+ if (!group.length) throw new Error('Skill correction model unavailable');+ const mod = await ca();+ const primary = group[0];+ const provider = await buildRotatingProvider(mod, primary.provider, group,+ undefined, undefined, undefined, 45_000, input.userId,+ { networkRetryAttempts: 0, normalEmptyRetryAttempts: 0 }, undefined, input.cid);+ check();+ const result = await provider.complete({+ model: primary.model,+ messages: [{ role: 'user', content: [{ type: 'text', text: input.message }] }],+ tools: [], maxTokens: 8192, reasoning: 'off', signal: input.signal,+ requestMetadata: { creditContext: {+ conversationId: input.cid, turnId: input.turnId,+ } },+ });+ check();+ if (result.stopReason !== 'end_turn' || result.content.some(part => part.type !== 'text')) {+ throw new Error('Skill correction incomplete');+ }+ const text = result.content.map(part => (part as { text: string }).text).join('').trim();+ if (!text || text.length > 64_000) throw new Error('Skill correction output limit');+ return text;+}+/** A feature-owned memory transformation, using the same auth/candidate and* rotating-provider construction as task runs. No Agent session, tools,* repository instructions, skills, or other memory scopes enter this call. */diff --git a/src/main/model/core-agent/skill-registry.ts b/src/main/model/core-agent/skill-registry.tsindex 632f6744..54351c4c 100644--- a/src/main/model/core-agent/skill-registry.ts+++ b/src/main/model/core-agent/skill-registry.ts@@ -1,4 +1,5 @@import { estimateBudgetTokens, estimateBudgetTokenQuarters, truncateBudgetText } from '../../util/token-estimate';+import { isPortableRuntimeRef } from './skill-runtime-ref';/*** Skill registry implementation: source loaders, prompt rosters, conflict* resolution, and run-scoped logical bindings. The complete runtime policy is@@ -256,11 +257,6 @@ export function getAgentSkillDependenciesPromptBlock(return lines.join('\n');}-function isPortableRuntimeRef(value: string): boolean {- const ref = String(value || '').trim();- return /^[A-Za-z0-9][A-Za-z0-9._:@+-]*$/u.test(ref);-}-function sameRuntimeBinding(a: SkillRuntimeBinding, b: SkillRuntimeBinding): boolean {return path.resolve(a.root) === path.resolve(b.root)&& path.resolve(a.entry) === path.resolve(b.entry);diff --git a/src/main/model/core-agent/skill-runtime-ref.ts b/src/main/model/core-agent/skill-runtime-ref.tsnew file mode 100644index 00000000..b6165321--- /dev/null+++ b/src/main/model/core-agent/skill-runtime-ref.ts@@ -0,0 +1,27 @@+import type { FileFailureDiagnostic } from '../../../core-agent/src/tools/file-diagnostics';++/** One grammar for host-created aliases and model-supplied reference names.+ * Child paths have their own scope/segment rules and must not use this check. */+export function isPortableRuntimeRef(value: string): boolean {+ return /^[A-Za-z0-9][A-Za-z0-9._:@+-]*$/u.test(String(value || '').trim());+}++export function lookupSkillRuntimeRef<T>(+ ref: string,+ bindings: ReadonlyMap<string, T> | undefined,+): { binding?: T; error?: string; diagnostic?: FileFailureDiagnostic } {+ if (!isPortableRuntimeRef(ref)) {+ return {+ error: 'E_SKILL_REF_INVALID: Use @skill/<read-ref> with the exact read ref advertised in Available skills.',+ diagnostic: { code: 'E_SKILL_REF_INVALID', reason: 'skill_ref_format', stage: 'input', skill_ref_valid: false },+ };+ }+ const binding = bindings?.get(ref);+ if (!binding) {+ return {+ error: `E_SKILL_NOT_AVAILABLE: @skill/${ref} is not bound for this run. Use an exact read ref from the current Available skills block.`,+ diagnostic: { code: 'E_SKILL_NOT_AVAILABLE', reason: 'skill_ref_unbound', stage: 'input', skill_ref_valid: true, skill_binding_found: false },+ };+ }+ return { binding };+}diff --git a/src/main/model/core-agent/tool-catalog-revision.ts b/src/main/model/core-agent/tool-catalog-revision.tsindex 0285d595..8e5b0213 100644--- a/src/main/model/core-agent/tool-catalog-revision.ts+++ b/src/main/model/core-agent/tool-catalog-revision.ts@@ -5,4 +5,4 @@* it as their current-state oracle without pulling the Electron runtime or the* tool catalog's logger/path dependency chain into model-eval processes.*/-export const TOOL_CATALOG_REVISION = '40';+export const TOOL_CATALOG_REVISION = '41';diff --git a/src/main/model/core-agent/web-fetch-render.ts b/src/main/model/core-agent/web-fetch-render.tsindex 98ab612b..a43de3a5 100644--- a/src/main/model/core-agent/web-fetch-render.ts+++ b/src/main/model/core-agent/web-fetch-render.ts@@ -55,24 +55,22 @@ function delay(ms: number): Promise<void> {return new Promise((resolve) => setTimeout(resolve, ms));}-function escapeRegExp(value: string): string {- return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');-}--/**- * Electron's default user agent advertises `Electron/<ver>` and the app name- * beside the real Chromium version. Drop only those two tokens so the claimed- * engine still matches what actually renders — hardcoding a Chrome version- * would drift away from the shipped Chromium at the next upgrade.+/*+ * This path exists to survive challenge pages, so it must not break the very+ * fingerprint those pages check. We deliberately keep Electron's default user+ * agent, `Electron/<ver>` token and all.+ *+ * Electron never sends the `Sec-CH-UA` client hints, with or without a UA+ * override — Chromium builds them from user agent *metadata*, which+ * `setUserAgent()` cannot reach. Washing the UA down to plain Chrome therefore+ * produces a self-contradicting request: it claims to be Chrome yet omits the+ * three headers every real Chrome sends. Keeping the Electron token instead+ * presents an honest non-Chrome client, which challenge pages accept.+ *+ * Measured against a Cloudflare-protected page on 2026-09-21: default UA+ * passed 3/3, the washed UA hard-blocked 1/3 even with a 25s settle window.+ * See the fuller note in features/web_assist.ts.*/-function chromeLikeUserAgent(): string {- const base = String(app.userAgentFallback || '');- if (!base) return '';- const appName = String(app.getName() || '');- let ua = base.replace(/\sElectron\/\S+/i, '');- if (appName) ua = ua.replace(new RegExp(`\\s${escapeRegExp(appName)}\\/\\S+`, 'i'), '');- return ua.replace(/\s{2,}/g, ' ').trim();-}function prepareSession(ses: Session): void {// Electron grants permission requests by default. A rendered third-party page@@ -129,8 +127,6 @@ async function renderOnce(url: string, signal?: AbortSignal): Promise<WebFetchResignal?.addEventListener('abort', stop, { once: true });try {- const userAgent = chromeLikeUserAgent();- if (userAgent) webContents.setUserAgent(userAgent);webContents.setWindowOpenHandler?.(() => ({ action: 'deny' }));// Challenge pages resolve by navigating (redirect or an auto-submitted// form), so navigation must stay allowed — but only to web origins.diff --git a/src/main/util/log-delivery.ts b/src/main/util/log-delivery.tsnew file mode 100644index 00000000..1c1d874a--- /dev/null+++ b/src/main/util/log-delivery.ts@@ -0,0 +1,39 @@+import { Worker } from 'node:worker_threads';+import * as path from 'node:path';++/** A bounded, best-effort mailbox. Saturation must never wait for a disk write. */+export function createLogDelivery(options: Record<string, unknown>, makeWorker = (file: string, config: any) => new Worker(file, config)) {+ let pending = 0;+ let dropped = 0;+ let failed = false;+ let unreportedDrops = 0;+ function noteDrop() { dropped += 1; unreportedDrops += 1; }+ const worker = makeWorker(path.join(__dirname, '..', 'logger-worker.js'), { workerData: options });+ worker.on('error', () => { failed = true; pending = 0; });+ worker.on('exit', () => { failed = true; pending = 0; });+ worker.on('message', (ack) => {+ pending = Math.max(0, pending - 1);+ if (ack?.written === false) { noteDrop(); return; }+ if (unreportedDrops && !failed && pending < 256) {+ const count = unreportedDrops;+ unreportedDrops = 0;+ try {+ worker.postMessage({ level: 'warn', scope: 'logger', date: new Date(),+ data: ['Log records dropped', { dropped: count }] });+ pending += 1;+ } catch (_) { unreportedDrops += count; }+ }+ });+ worker.unref();+ return {+ noteDrop,+ canAccept(level: string) { return !failed && pending < (level === 'error' || level === 'warn' ? 256 : 192); },+ send(message: { level: string; [key: string]: unknown }) {+ if (!this.canAccept(message.level)) { noteDrop(); return; }+ try { worker.postMessage(message); pending += 1; }+ catch (_) { noteDrop(); }+ },+ stats() { return { pending, dropped, failed }; },+ async close() { failed = true; pending = 0; await worker.terminate(); },+ };+}diff --git a/src/main/util/log-retention.js b/src/main/util/log-retention.jsnew file mode 100644index 00000000..d62bfcfd--- /dev/null+++ b/src/main/util/log-retention.js@@ -0,0 +1,35 @@+"use strict";+const fs = require('node:fs');+const path = require('node:path');+function dateKey(date = new Date()) {+ return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;+}+// Synchronous by design: production calls this only in the log worker.+function sweepLogDirectory(directory, retainDays, totalMaxBytes, now = new Date()) {+ const today = dateKey(now);+ const cutoff = now.getTime() - retainDays * 86400000;+ const files = [], removed = [], reason = {};+ let names;+ try { names = fs.readdirSync(directory); } catch (_) { return { removed, reason }; }+ for (const name of names) {+ if (!/^\d{4}-\d{2}-\d{2}.*\.log$/.test(name)) continue;+ const full = path.join(directory, name);+ try {+ const stat = fs.statSync(full);+ if (!stat.isFile()) continue;+ if (name.slice(0, 10) < today && new Date(`${name.slice(0, 10)}T00:00:00`).getTime() < cutoff) {+ try { fs.unlinkSync(full); removed.push(name); reason[name] = 'age'; continue; } catch (_) {}+ }+ files.push({ name, full, size: stat.size, mtime: stat.mtimeMs });+ } catch (_) { /* A locked file can be retried at the next boot. */ }+ }+ let total = files.reduce((sum, file) => sum + file.size, 0);+ files.sort((a, b) => a.name.slice(0, 10).localeCompare(b.name.slice(0, 10)) || a.mtime - b.mtime);+ for (const file of files) {+ if (total <= totalMaxBytes) break;+ if (file.name === `${today}.log`) continue;+ try { fs.unlinkSync(file.full); total -= file.size; removed.push(file.name); reason[file.name] = 'size'; } catch (_) {}+ }+ return { removed, reason };+}+module.exports = { dateKey, sweepLogDirectory };diff --git a/src/main/util/text-preview.ts b/src/main/util/text-preview.tsnew file mode 100644index 00000000..3cd0be98--- /dev/null+++ b/src/main/util/text-preview.ts@@ -0,0 +1,26 @@+import * as fs from 'node:fs';+import { StringDecoder } from 'node:string_decoder';++export const TEXT_PREVIEW_BYTES = 2 * 1024 * 1024;++/** Read a bounded prefix without splitting UTF-8 code points. Callers own path+ * authorization. Partial results must never be offered as editable originals. */+export function readTextPreview(absPath: string): { text: string; truncated: boolean } {+ const fd = fs.openSync(absPath, 'r');+ try {+ if (!fs.fstatSync(fd).isFile()) throw new Error('not a file');+ const buf = Buffer.allocUnsafe(TEXT_PREVIEW_BYTES + 1);+ let size = 0;+ while (size < buf.length) {+ const count = fs.readSync(fd, buf, size, buf.length - size, size);+ if (!count) break;+ size += count;+ }+ const truncated = size > TEXT_PREVIEW_BYTES;+ const decoder = new StringDecoder('utf8');+ let text = decoder.write(buf.subarray(0, Math.min(size, TEXT_PREVIEW_BYTES)));+ if (!truncated) text += decoder.end();+ if (text.charCodeAt(0) === 0xFEFF) text = text.slice(1);+ return { text, truncated };+ } finally { fs.closeSync(fd); }+}diff --git a/src/main/util/web-content-policy.ts b/src/main/util/web-content-policy.tsnew file mode 100644index 00000000..e5ef2c1f--- /dev/null+++ b/src/main/util/web-content-policy.ts@@ -0,0 +1,15 @@+/** Browser resources are independent of host/SDK authorization. */+export function webContentCsp(local = "'self'"): string {+ return ["default-src 'none'",+ `script-src ${local} http: https: 'unsafe-inline' 'unsafe-eval' blob:`,+ `style-src ${local} http: https: 'unsafe-inline'`,+ `img-src ${local} http: https: data: blob:`,+ `font-src ${local} http: https: data: blob:`,+ `media-src ${local} http: https: data: blob:`,+ `connect-src ${local} http: https: ws: wss:`,+ `worker-src ${local} http: https: blob:`,+ `frame-src ${local} http: https:`,+ `form-action ${local} http: https:`,+ `manifest-src ${local} http: https:`,+ "object-src 'none'", "base-uri 'self'"].join('; ');+}diff --git a/src/main/util/window-security.ts b/src/main/util/window-security.tsindex 6bdda9de..5af45298 100644--- a/src/main/util/window-security.ts+++ b/src/main/util/window-security.ts@@ -1,3 +1,4 @@+import { webContentCsp } from './web-content-policy';import type { WebPreferences } from 'electron';/**@@ -152,30 +153,12 @@ interface GuardedWebContents {on(event: 'will-navigate', handler: (event: NavigationEvent, url: string) => void): void;}-export const OFFLINE_HTML_PREVIEW_CSP = [- "default-src 'none'",- "script-src 'unsafe-inline' 'unsafe-eval' blob:",- "style-src 'unsafe-inline'",- 'img-src chat-media://local data: blob:',- 'media-src chat-media://local data: blob:',- 'font-src data: blob:',- "connect-src 'none'",- "frame-src 'none'",- 'worker-src blob:',- "object-src 'none'",- "base-uri 'none'",- "form-action 'none'",- "manifest-src 'none'",-].join('; ');+export const HTML_PREVIEW_CSP = webContentCsp('chat-media://local');-/**- * Preserve the streamed/range response while making local HTML previews- * offline-first. Inline code remains usable inside the existing sandbox, but- * network-loaded code/assets and programmatic connections fail closed.- */-export function withOfflineHtmlPreviewPolicy(response: Response): Response {+/** Preserve streaming and local-file authorization while allowing Web resources. */+export function withHtmlPreviewPolicy(response: Response): Response {const headers = new Headers(response.headers);- headers.set('Content-Security-Policy', OFFLINE_HTML_PREVIEW_CSP);+ headers.set('Content-Security-Policy', HTML_PREVIEW_CSP);headers.set('Referrer-Policy', 'no-referrer');headers.set('X-Content-Type-Options', 'nosniff');return new Response(response.body, {@@ -198,7 +181,7 @@ function parsedLocalChatMediaUrl(raw: unknown): URL | null {}}-export function isOfflineHtmlPreviewUrl(raw: unknown): boolean {+export function isHtmlPreviewUrl(raw: unknown): boolean {const url = parsedLocalChatMediaUrl(raw);if (!url) return false;try {@@ -226,19 +209,15 @@ interface PreviewFrameWebContents {): void;}-/**- * CSP resource directives do not cover every document navigation. Keep an- * already-loaded local HTML preview on the validated local protocol so inline- * code cannot exfiltrate data by assigning an HTTP(S) URL to its own frame.- */-export function installOfflineHtmlPreviewNavigationGuard(+/** Keep local previews away from privileged document schemes. */+export function installHtmlPreviewNavigationGuard(webContents: PreviewFrameWebContents,): void {webContents.on('will-frame-navigate', (event) => {if (event.isMainFrame) return;const sourceUrl = event.frame?.url || event.initiator?.url || '';- if (!isOfflineHtmlPreviewUrl(sourceUrl)) return;- if (parsedLocalChatMediaUrl(event.url)) return;+ if (!isHtmlPreviewUrl(sourceUrl)) return;+ if (parsedLocalChatMediaUrl(event.url) || safeExternalHttpUrl(event.url)) return;event.preventDefault();});}diff --git a/src/renderer/index.html b/src/renderer/index.htmlindex 902badc1..8e877a65 100644--- a/src/renderer/index.html+++ b/src/renderer/index.html@@ -1936,6 +1936,7 @@<script src="./modules/file-operation-policy.js"></script><script src="./modules/chat-file-viewer.js"></script><script src="./modules/preview-windows.js"></script>+ <script src="./modules/delimited-preview.js"></script><script src="./modules/md-view-edit.js"></script><script src="./modules/text-view-edit.js"></script><script src="./modules/context-menu.js"></script>diff --git a/src/renderer/locales/de.json b/src/renderer/locales/de.jsonindex 1996c874..ba6cf5e0 100644--- a/src/renderer/locales/de.json+++ b/src/renderer/locales/de.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "Tabelle",+ "files.delimited.source": "Quelltext",+ "files.delimited.limited": "Begrenzte Vorschau. Öffnen Sie die Datei extern, um alle Daten zu sehen.",+ "files.delimited.invalid": "Unvollständige oder ungültige Felder in Anführungszeichen. Der Quelltext wird angezeigt.","connectors.action_confirm.operation.external_or_financial_change": "Externe Aktion oder finanzielle Änderung","connectors.action_confirm.operation.destructive": "Löschung oder unumkehrbare Aktion","connectors.action_confirm.operation.financial_record_change": "Änderung von Finanzdaten",diff --git a/src/renderer/locales/en.json b/src/renderer/locales/en.jsonindex 9b8a7a28..48f1b1ca 100644--- a/src/renderer/locales/en.json+++ b/src/renderer/locales/en.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "Table",+ "files.delimited.source": "Source",+ "files.delimited.limited": "Showing a limited preview. Open the file externally to view all data.",+ "files.delimited.invalid": "This file contains incomplete or invalid quoted fields. Showing the source.","connectors.action_confirm.operation.external_or_financial_change": "External action or financial change","connectors.action_confirm.operation.destructive": "Deletion or irreversible action","connectors.action_confirm.operation.financial_record_change": "Financial record change",diff --git a/src/renderer/locales/es.json b/src/renderer/locales/es.jsonindex b423d69c..2ed7c726 100644--- a/src/renderer/locales/es.json+++ b/src/renderer/locales/es.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "Tabla",+ "files.delimited.source": "Texto original",+ "files.delimited.limited": "Vista previa limitada. Abra el archivo en otra aplicación para ver todos los datos.",+ "files.delimited.invalid": "Hay campos entre comillas incompletos o inválidos. Se muestra el texto original.","connectors.action_confirm.operation.external_or_financial_change": "Acción externa o cambio financiero","connectors.action_confirm.operation.destructive": "Eliminación o acción irreversible","connectors.action_confirm.operation.financial_record_change": "Cambio en un registro financiero",diff --git a/src/renderer/locales/fr.json b/src/renderer/locales/fr.jsonindex 223380e0..f29b33a5 100644--- a/src/renderer/locales/fr.json+++ b/src/renderer/locales/fr.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "Tableau",+ "files.delimited.source": "Source",+ "files.delimited.limited": "Aperçu limité. Ouvrez le fichier dans une application externe pour voir toutes les données.",+ "files.delimited.invalid": "Champs entre guillemets incomplets ou invalides. Affichage du texte source.","connectors.action_confirm.operation.external_or_financial_change": "Action externe ou modification financière","connectors.action_confirm.operation.destructive": "Suppression ou action irréversible","connectors.action_confirm.operation.financial_record_change": "Modification de données financières",diff --git a/src/renderer/locales/it.json b/src/renderer/locales/it.jsonindex ee64c2e2..05060863 100644--- a/src/renderer/locales/it.json+++ b/src/renderer/locales/it.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "Tabella",+ "files.delimited.source": "Testo originale",+ "files.delimited.limited": "Anteprima limitata. Apri il file in un’app esterna per visualizzare tutti i dati.",+ "files.delimited.invalid": "Campi tra virgolette incompleti o non validi. Viene mostrato il testo originale.","connectors.action_confirm.operation.external_or_financial_change": "Azione esterna o modifica finanziaria","connectors.action_confirm.operation.destructive": "Eliminazione o azione irreversibile","connectors.action_confirm.operation.financial_record_change": "Modifica di un dato finanziario",diff --git a/src/renderer/locales/ja.json b/src/renderer/locales/ja.jsonindex 41ed0960..e56593e6 100644--- a/src/renderer/locales/ja.json+++ b/src/renderer/locales/ja.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "表",+ "files.delimited.source": "原文",+ "files.delimited.limited": "プレビューは一部のみです。すべてのデータは外部アプリで開いてください。",+ "files.delimited.invalid": "引用符で囲まれたフィールドが不完全または無効です。原文を表示します。","connectors.action_confirm.operation.external_or_financial_change": "外部への操作・資金の変動","connectors.action_confirm.operation.destructive": "削除・取り消せない操作","connectors.action_confirm.operation.financial_record_change": "財務記録の変更",diff --git a/src/renderer/locales/ko.json b/src/renderer/locales/ko.jsonindex 89ad8122..b7b9e933 100644--- a/src/renderer/locales/ko.json+++ b/src/renderer/locales/ko.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "표",+ "files.delimited.source": "원문",+ "files.delimited.limited": "일부 미리보기만 표시됩니다. 전체 데이터는 외부 앱에서 파일을 열어 확인하세요.",+ "files.delimited.invalid": "따옴표로 묶인 필드가 불완전하거나 유효하지 않아 원문을 표시합니다.","connectors.action_confirm.operation.external_or_financial_change": "외부 동작 또는 재무 변경","connectors.action_confirm.operation.destructive": "삭제 또는 되돌릴 수 없는 동작","connectors.action_confirm.operation.financial_record_change": "재무 기록 변경",diff --git a/src/renderer/locales/pt.json b/src/renderer/locales/pt.jsonindex 5996b46c..594859cf 100644--- a/src/renderer/locales/pt.json+++ b/src/renderer/locales/pt.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "Tabela",+ "files.delimited.source": "Texto original",+ "files.delimited.limited": "Prévia limitada. Abra o arquivo em outro aplicativo para ver todos os dados.",+ "files.delimited.invalid": "Campos entre aspas incompletos ou inválidos. Exibindo o texto original.","connectors.action_confirm.operation.external_or_financial_change": "Ação externa ou alteração financeira","connectors.action_confirm.operation.destructive": "Exclusão ou ação irreversível","connectors.action_confirm.operation.financial_record_change": "Alteração de registros financeiros",diff --git a/src/renderer/locales/ru.json b/src/renderer/locales/ru.jsonindex b0daf23a..12f9fe7b 100644--- a/src/renderer/locales/ru.json+++ b/src/renderer/locales/ru.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "Таблица",+ "files.delimited.source": "Исходный текст",+ "files.delimited.limited": "Предпросмотр ограничен. Откройте файл во внешнем приложении для просмотра всех данных.",+ "files.delimited.invalid": "Незакрытые или некорректные поля в кавычках. Показан исходный текст.","connectors.action_confirm.operation.external_or_financial_change": "Внешнее действие или финансовое изменение","connectors.action_confirm.operation.destructive": "Удаление или необратимое действие","connectors.action_confirm.operation.financial_record_change": "Изменение финансовой записи",diff --git a/src/renderer/locales/zh.json b/src/renderer/locales/zh.jsonindex 5471c7b5..04c6f107 100644--- a/src/renderer/locales/zh.json+++ b/src/renderer/locales/zh.json@@ -1,4 +1,8 @@{+ "files.delimited.table": "表格",+ "files.delimited.source": "原文",+ "files.delimited.limited": "当前仅显示部分预览。请使用外部应用打开文件查看全部数据。",+ "files.delimited.invalid": "文件含有未闭合或无效的引号字段,已显示原文。","connectors.action_confirm.operation.external_or_financial_change": "对外操作或资金变动","connectors.action_confirm.operation.destructive": "删除或不可逆操作","connectors.action_confirm.operation.financial_record_change": "财务记录变更",diff --git a/src/renderer/modules/artifact-security.js b/src/renderer/modules/artifact-security.jsindex bd92e6fe..da2fd08b 100644--- a/src/renderer/modules/artifact-security.js+++ b/src/renderer/modules/artifact-security.js@@ -11,7 +11,7 @@// Generated apps may run code, submit forms, and use same-origin storage.// They may not create popups or navigate the top-level renderer.- const SANDBOX = 'allow-scripts allow-same-origin allow-forms';+ const SANDBOX = 'allow-scripts allow-same-origin allow-forms allow-downloads allow-popups allow-modals';function safeExternalHttpUrl(raw) {const value = String(raw == null ? '' : raw).trim();diff --git a/src/renderer/modules/auto.js b/src/renderer/modules/auto.jsindex e3f6641f..4956ae96 100644--- a/src/renderer/modules/auto.js+++ b/src/renderer/modules/auto.js@@ -109,7 +109,12 @@ function _autoAttachmentPayload(files, source) {const AUTO_ATTACH_ACCEPT = (typeof CHAT_ATTACH_ACCEPT !== 'undefined' && Array.isArray(CHAT_ATTACH_ACCEPT))? CHAT_ATTACH_ACCEPT: [- '.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt', '.json', '.yaml', '.yml', '.log',+ '.html', '.htm', '.xml', '.toml', '.ini', '.conf',+ '.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs',+ '.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt',+ '.c', '.cpp', '.cc', '.h', '.hpp', '.css', '.scss', '.less',+ '.sql', '.graphql', '.gql','.pdf', '.docx', '.docm', '.xlsx', '.xlsm', '.xls', '.pptx', '.pptm','.png', '.jpg', '.jpeg', '.webp', '.gif','.mp4', '.webm', '.mov', '.m4v', '.ogv',diff --git a/src/renderer/modules/chat-file-viewer.js b/src/renderer/modules/chat-file-viewer.jsindex af87f41e..55aaa374 100644--- a/src/renderer/modules/chat-file-viewer.js+++ b/src/renderer/modules/chat-file-viewer.js@@ -337,7 +337,7 @@ const _MARKDOWN_EXTS = new Set(['.md', '.markdown']);// highlighting in this round; <pre> with white-space:pre-wrap is enough.const _TEXT_EXTS = new Set(['.txt', '.log',- '.csv', '.tsv',+ '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt','.json', '.yaml', '.yml','.xml', '.ini', '.toml', '.conf','.py', '.pyi',@@ -941,13 +941,11 @@ async function _renderHtmlBody(absPath, displayName, cid, projectId) {if (!seq) return;const state = _viewerBeginHtmlPreview(cid, projectId);const url = _chatMediaLocalUrl(absPath);- // sandbox: allow-scripts ONLY. chat-media:// is a distinct origin from+ // sandbox: opaque origin. chat-media:// is a distinct origin from// file://, so SOP blocks parent.* access; we additionally forbid// allow-same-origin (no cookie / localStorage / sibling-fetch reach),- // allow-popups (no window.open), and allow-top-navigation (no top-frame- // redirects). Self-contained LLM-generated HTML still runs its inline- // scripts and styles.- const sandbox = 'allow-scripts';+ // top navigation. Browser actions use existing host handlers.+ const sandbox = 'allow-scripts allow-forms allow-downloads allow-popups allow-modals';const iframe = document.createElement('iframe');iframe.className = 'chat-file-viewer-html';iframe.setAttribute('sandbox', sandbox);@@ -1251,7 +1249,8 @@ async function _renderTextBody(absPath, displayName, cid, projectId) {// back to the "open the folder?" dialog (same UX as the read-only path// before). On success, hand the text to mountTextViewEdit; it owns the// view ↔ edit transitions, save IPC, and dirty tracking from there.- const text = await _readTextFile(absPath, cid, projectId, seq);+ const preview = {};+ const text = await _readTextFile(absPath, cid, projectId, seq, preview);if (text === null || seq !== _viewerRenderSeq || !_isViewerOpen()) return;_viewerFinishLoading();if (typeof mountTextViewEdit !== 'function') {@@ -1265,7 +1264,8 @@ async function _renderTextBody(absPath, displayName, cid, projectId) {source: { absPath, cid: cid || undefined, projectId: projectId || undefined },// Project Library files are read-only by design (the LLM owns project workspace// mutations); workspace / per-conv attachments allow edit + save.- capabilities: projectId ? { edit: false, save: false } : { edit: true, save: true },+ partial: preview.truncated === true,+ capabilities: projectId || preview.truncated ? { edit: false, save: false } : { edit: true, save: true },initialMode: 'view',initialContent: text,actionIconOnly: true,@@ -1279,17 +1279,21 @@ async function _renderTextBody(absPath, displayName, cid, projectId) {// surfaces the fallback dialog (and returns null so the caller knows to// stop). Closes the overlay before showing the dialog so it doesn't stack// on top of a half-built viewer.-async function _readTextFile(absPath, cid, projectId, seq) {+async function _readTextFile(absPath, cid, projectId, seq, preview) {_viewerCurrentPath = absPath;_viewerCurrentCid = cid || null;_viewerCurrentProjectId = projectId || null;try {const payload = { path: absPath };+ if (preview && /\.(csv|tsv)$/i.test(absPath)) payload.preview = true;if (cid) payload.cid = cid;if (projectId) payload.projectId = projectId;const res = await window.orkas.invoke('produced.readText', payload);if (seq && seq !== _viewerRenderSeq) return null;- if (res && res.ok) return String(res.text || '');+ if (res && res.ok) {+ if (preview) preview.truncated = res.truncated === true;+ return String(res.text || '');+ }// Specifically distinguish too_large so the user sees "file is X MB,// open in folder?" instead of a generic failure.const err = (res && res.error) || 'unknown';@@ -1400,7 +1404,7 @@ async function openChatFileViewer(absPath, displayName, opts) {if (!absPath) return;if (window.OrkasPreviewWindows) {if (!(await _ensureViewerFileExists(absPath, opts?.cid, opts?.projectId))) return;- if (_kindOf(displayName || absPath) === 'image') return window.OrkasPreviewWindows.image(_chatMediaLocalUrl(absPath), displayName || absPath.split(/[\\/]/).pop(), { ...opts, absPath });+ if (_kindOf(absPath) === 'image') return window.OrkasPreviewWindows.image(_chatMediaLocalUrl(absPath), displayName || absPath.split(/[\\/]/).pop(), { ...opts, absPath });return window.OrkasPreviewWindows.open({ kind: 'file', path: absPath, title: displayName || absPath.split(/[\\/]/).pop(), options: window.OrkasPreviewWindows.options(opts) });}const cid = (opts && opts.cid) || null;@@ -1408,7 +1412,7 @@ async function openChatFileViewer(absPath, displayName, opts) {const name = displayName || (absPath.split(/[\\/]/).pop() || absPath);const exists = await _ensureViewerFileExists(absPath, cid, projectId);if (!exists) return;- const kind = _kindOf(name);+ const kind = _kindOf(absPath);if (kind === 'image') {// Delegate — the image lightbox already has zoom / pan / keyboard. Wediff --git a/src/renderer/modules/contexts.js b/src/renderer/modules/contexts.jsindex 85375359..e2db1919 100644--- a/src/renderer/modules/contexts.js+++ b/src/renderer/modules/contexts.js@@ -1556,7 +1556,7 @@ function _ctxListChildren(dirPath) {// ── Upload ──const CTX_ALLOWED_EXTS = [- '.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt', '.json', '.yaml', '.yml', '.log','.html', '.htm', '.xml', '.toml', '.ini', '.conf','.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs','.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt',@@ -1876,7 +1876,7 @@ async function handleCtxNativeUpload(targetDir = '') {// delete + "open in system" actions for consistency.const CTX_TEXT_EXTS = new Set([- '.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt', '.json', '.yaml', '.yml', '.log','.html', '.htm', '.xml', '.toml', '.ini', '.conf','.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs','.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt',@@ -1924,11 +1924,11 @@ async function openCtxFile(rel) {const kind = _kindOfPath(rel);try {if (kind === 'text') {- const res = await _ctxFetch(`/api/contexts/read?path=${encodeURIComponent(rel)}`);+ const res = await _ctxFetch(`/api/contexts/read?path=${encodeURIComponent(rel)}&preview=true`);const data = await res.json();if (seq !== _ctxOpenSeq) return;if (!data.ok) { await uiAlert(t('contexts.read_failed')); return; }- _showCtxTextViewer(rel, data.content || '');+ _showCtxTextViewer(rel, data.content || '', data.truncated === true);} else if (kind === 'image') {const res = await _ctxFetch(`/api/contexts/image?path=${encodeURIComponent(rel)}`);const data = await res.json();@@ -2096,7 +2096,7 @@ function _ctxClearDraft(path) {if (key) _ctxDrafts.delete(key);}-function _showCtxTextViewer(rel, content) {+function _showCtxTextViewer(rel, content, partial = false) {const els = _prepCtxViewerShell(rel);if (!els) return;const drafts = _ctxDrafts;@@ -2107,6 +2107,9 @@ function _showCtxTextViewer(rel, content) {return (source?.kind === 'project-file' ? source.name : source?.rel) || rel;};controller = mountMdViewEdit({+ plainText: !/\.(md|markdown)$/i.test(rel),+ partial,+ ...(partial ? { capabilities: { edit: false, save: false, taskCheckbox: false } } : {}),bodyEl: els.bodyEl,actionsEl: els.actionsEl,source: _ctxProjectId@@ -2116,7 +2119,7 @@ function _showCtxTextViewer(rel, content) {initialContent: content,// Restore in-progress edits if the user was previously typing in this// file. mountMdViewEdit forces edit mode when `initialDraft` is present.- initialDraft: drafts.get(rel) || null,+ initialDraft: partial ? null : drafts.get(rel) || null,callbacks: {// Mirror draft state into the per-file Map so it survives switching// to another file in the tree and back.diff --git a/src/renderer/modules/conversation-info.js b/src/renderer/modules/conversation-info.jsindex b4f10801..6de6756c 100644--- a/src/renderer/modules/conversation-info.js+++ b/src/renderer/modules/conversation-info.js@@ -40,7 +40,7 @@ const ConversationInfo = (() => {attachments: [],};const _CI_TEXT_EXTS = new Set([- 'md', 'markdown', 'txt', 'csv', 'tsv', 'json', 'yaml', 'yml', 'log',+ 'md', 'markdown', 'txt', 'csv', 'tsv', 'jsonl', 'ndjson', 'rst', 'tex', 'srt', 'vtt', 'json', 'yaml', 'yml', 'log','html', 'htm', 'xml', 'toml', 'ini', 'conf','py', 'pyi', 'ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs','sh', 'bash', 'zsh', 'ps1', 'cmd', 'bat', 'rb', 'go', 'rs', 'java', 'kt',diff --git a/src/renderer/modules/conversation.js b/src/renderer/modules/conversation.jsindex 7657ff79..b86f02d1 100644--- a/src/renderer/modules/conversation.js+++ b/src/renderer/modules/conversation.js@@ -48,13 +48,6 @@ try {}} catch (_) {}-function _handleModelOutputErrorForUi(cid, msgDiv, rawError, extra) {- void cid;- void msgDiv;- void rawError;- void extra;-}-function _uiIconHtml(name, className) {if (typeof window !== 'undefined' && typeof window.uiIconHtml === 'function') return window.uiIconHtml(name, className || 'ui-icon');return '';@@ -4889,7 +4882,12 @@ const _chatAttachmentSendLocks = new Set(); // cid values currently being prepaconst DRAFT_CID = 'main_chat';const CHAT_ATTACH_ACCEPT = [- '.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt', '.json', '.yaml', '.yml', '.log',+ '.html', '.htm', '.xml', '.toml', '.ini', '.conf',+ '.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs',+ '.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt',+ '.c', '.cpp', '.cc', '.h', '.hpp', '.css', '.scss', '.less',+ '.sql', '.graphql', '.gql','.pdf', '.docx', '.docm', '.xlsx', '.xlsm', '.xls', '.pptx', '.pptm','.zip','.png', '.jpg', '.jpeg', '.webp', '.gif',@@ -7858,11 +7856,12 @@ function _isCurrentHistoryLoad(load) {&& document.getElementById('chat-history') === load.container;}-function _rememberHistoryLoadMessage(cid, message) {+function _rememberHistoryLoadMessage(cid, message, turnEnd = false) {const load = _conversationHistoryLoad;if (!load || !load.collecting || load.cid !== cid || !_isCurrentHistoryLoad(load)|| !message?.id || message.deleted_at || !_isVisibleGroupHistoryRecord(message)) return;load.messages.set(String(message.id), message);+ if (turnEnd && message.turn_id) load.endedTurnIds.add(String(message.turn_id));}async function loadConversationHistory(cid, opts = {}) {@@ -7876,6 +7875,8 @@ async function loadConversationHistory(cid, opts = {}) {? previousLoad.processEvents : [],messages: previousLoad?.cid === cid && previousLoad.collecting? previousLoad.messages : new Map(),+ endedTurnIds: previousLoad?.cid === cid && previousLoad.collecting+ ? previousLoad.endedTurnIds : new Set(),};_conversationHistoryLoad = load;const preserveScroll = opts && opts.preserveScroll === true;@@ -7970,6 +7971,14 @@ async function loadConversationHistory(cid, opts = {}) {convMeta.processing_since ||= new Date().toISOString();}}+ // A terminal event received during this request is newer than its active+ // snapshot. Keep the persisted reply out of live-segment folding; otherwise+ // stream cleanup deletes it as an orphan when the next actor finishes.+ if (Array.isArray(convMeta.active_turns) && load.endedTurnIds.size) {+ convMeta.active_turns = convMeta.active_turns.filter(+ (turn) => !load.endedTurnIds.has(_normaliseTurnId(turn?.turn_id || turn?.turnId)),+ );+ }_rememberServerFloor(cid, convMeta);// History reload: drop ALL per-actor placeholder map entries — the// `container.innerHTML=''` below detaches every placeholder DOM node,@@ -8049,15 +8058,20 @@ async function loadConversationHistory(cid, opts = {}) {// tree scans. Live messages and recovery paths still use the guarded// incremental insertion path below.const historyFragment = document.createDocumentFragment();- history.forEach((msg) => appendChatMessage(msg, false, {- cid,- // Only stamp authoritative source indexes returned by an anchored- // history read. A normal latest-page row has no global index; using- // its local 0..9 offset would let a later search falsely match it.- msgIndex: Number.isSafeInteger(msg?._history_index) ? msg._history_index : undefined,- container: historyFragment,- historyHydration: true,- }));+ history.forEach((msg) => {+ const row = appendChatMessage(msg, false, {+ cid,+ // Only stamp authoritative source indexes returned by an anchored+ // history read. A normal latest-page row has no global index; using+ // its local 0..9 offset would let a later search falsely match it.+ msgIndex: Number.isSafeInteger(msg?._history_index) ? msg._history_index : undefined,+ container: historyFragment,+ historyHydration: true,+ });+ // Retain terminal identity across the DOM rebuild so a stale live+ // display snapshot or deferred process event cannot reopen this turn.+ _stampTurnEndRow(row, { turn_id: msg._turn_id }, load.endedTurnIds.has(msg._turn_id));+ });container.appendChild(historyFragment);}load.painted = true;@@ -13906,12 +13920,6 @@ function createChatController(config) {} else {_streamingSetError(msgEl, err.message || String(err));pending.errored = true;- _handleModelOutputErrorForUi(id, msgEl, err.message || String(err), {- stage: 'stream_request',- error_type: 'stream',- failure_kind: 'runtime',- failure_code: 'stream_request_failed',- });if (hooks.onError) hooks.onError(err.message || String(err), msgEl, id);}} finally {@@ -14094,6 +14102,9 @@ function _handleStreamEvent(cid, msg, ev, { archive = false } = {}) {}_advanceStreamingMessageActivityPosition(msg);_renderAgentEvent(msg, ev.event);+ } else if (ev.type === 'commentary-finalized') {+ _advanceStreamingMessageActivityPosition(msg);+ _streamingFinalizeCommentary(msg, ev.text);} else if (ev.type === 'delta') {const text = ev.text || '';if (text) _advanceStreamingMessageActivityPosition(msg);@@ -15075,12 +15086,6 @@ function _finalizeActorPlaceholder(ph, gm, cid, archive) {if (failedAssistant) {_mountEmptyResponseNotice(ph, gm);_attachFailedAssistantActions(ph, () => _messageTextForActions(ph, text));- _handleModelOutputErrorForUi(cid, ph, _failedAssistantErrorText(ph) || text, {- stage: 'actor_final',- error_type: 'model_output',- failure_kind: String(gm.failure_kind || ''),- failure_code: String(gm.failure_code || ''),- });} else if (interruptedAssistant) {_attachInterruptedAssistantActions(ph, () => _messageTextForActions(ph, text), { archive });}@@ -15264,7 +15269,7 @@ function _handleGroupBusEvent(cid, streamingMsg, evData, { archive = false } = {if (evData.type === 'message') {const gm = evData.msg;if (!gm) return;- _rememberHistoryLoadMessage(cid, gm);+ _rememberHistoryLoadMessage(cid, gm, !!evData.turn_end);// Edit chats may already have an optimistic node; main conversations do// not paint one until this persisted event arrives. The shared helper// claims an existing node when present or creates the admitted bubble.@@ -15960,6 +15965,12 @@ function _streamingAppendFinalDelta(msg, piece, phase = '') {return;}_sealStreamingCommentary(msg);+ if (phase === 'pending' && msg.dataset.streamPhase !== 'pending') {+ // Each unclassified model round has its own later commentary boundary.+ // CLI's existing one-boundary replay guard must not suppress later rounds.+ delete msg.dataset.commentaryFinalized;+ delete msg.dataset.commentaryStreamed;+ }const finalEl = msg.querySelector('[data-role="final"]');if (!finalEl) return;if (phase) msg.dataset.streamPhase = String(phase);@@ -15968,7 +15979,7 @@ function _streamingAppendFinalDelta(msg, piece, phase = '') {// remaining text streams. Async questions can precede more work, so leave// the execution clock running until the authoritative terminal event.const details = msg.querySelector('.stream-process');- if (!prev && details) {+ if (!prev && details && phase !== 'pending') {_setProcessSummaryState(msg, 'complete');details.removeAttribute('open');}diff --git a/src/renderer/modules/delimited-preview.js b/src/renderer/modules/delimited-preview.jsnew file mode 100644index 00000000..4ad5b051--- /dev/null+++ b/src/renderer/modules/delimited-preview.js@@ -0,0 +1,75 @@+// Bounded RFC 4180 field parsing shared by task and Library previews. Values+// stay strings: identifiers, formulas and markup are never coerced or executed.+(function (root) {+ const MAX_ROWS = 200;+ const MAX_COLUMNS = 100;+ const MAX_CELL_CHARS = 4000;++ function parse(text, delimiter, partial = false) {+ const input = String(text || '').replace(/^\uFEFF/, '');+ const rows = [];+ let row = [], cell = '', quoted = false, closed = false;+ let limited = false, fieldStarted = false;+ const append = char => {+ if (cell.length < MAX_CELL_CHARS) cell += char;+ else limited = true;+ };+ const finishCell = () => {+ if (row.length < MAX_COLUMNS) row.push(cell);+ else limited = true;+ cell = ''; closed = false; fieldStarted = false;+ };+ for (let i = 0; i < input.length; i++) {+ const ch = input[i];+ if (quoted) {+ if (ch === '"') {+ if (input[i + 1] === '"') { append('"'); i++; }+ else { quoted = false; closed = true; }+ } else append(ch);+ continue;+ }+ if (ch === delimiter) { finishCell(); continue; }+ if (ch === '\n' || ch === '\r') {+ finishCell(); rows.push(row); row = [];+ if (ch === '\r' && input[i + 1] === '\n') i++;+ if (rows.length === MAX_ROWS) return { rows, limited: limited || partial || i + 1 < input.length, malformed: false };+ continue;+ }+ if (closed) return { rows: [], limited, malformed: true };+ if (ch === '"') {+ if (fieldStarted) return { rows: [], limited, malformed: true };+ quoted = true; fieldStarted = true;+ } else { append(ch); fieldStarted = true; }+ }+ // A byte-limited prefix is not an entire final record. Do not present an+ // incomplete cell/row as complete or enable editing of the prefix.+ if (partial) return { rows, limited: true, malformed: false };+ if (quoted) return { rows: [], limited, malformed: true };+ if (fieldStarted || closed || row.length) { finishCell(); rows.push(row); }+ return { rows, limited, malformed: false };+ }++ function isDelimited(name) { return /\.(csv|tsv)$/i.test(String(name || '')); }++ function mount(body, text, name, partial = false) {+ const result = parse(text, /\.tsv$/i.test(name) ? '\t' : ',', partial);+ const label = key => t(`files.delimited.${key}`);+ let source = result.malformed;+ const paint = () => {+ const notice = result.malformed ? label('invalid') : (result.limited ? label('limited') : '');+ body.innerHTML = `<div class="delimited-preview">+ <div class="delimited-preview-toolbar">+ <button type="button" class="btn btn-sm" data-delimited-toggle>${escapeHtml(label(source ? 'table' : 'source'))}</button>+ ${notice ? `<span role="status">${escapeHtml(notice)}</span>` : ''}+ </div>+ ${source ? `<pre class="chat-file-viewer-text">${escapeHtml(text)}</pre>` : `<div class="delimited-preview-scroll"><table><tbody>${result.rows.map((row, index) => `<tr><th scope="row">${index + 1}</th>${row.map(cell => `<td>${escapeHtml(cell)}</td>`).join('')}</tr>`).join('')}</tbody></table></div>`}+ </div>`;+ const button = body.querySelector('[data-delimited-toggle]');+ button.disabled = result.malformed;+ button.addEventListener('click', () => { source = !source; paint(); });+ };+ paint();+ }+ if (root) root.DelimitedPreview = { isDelimited, mount };+ if (typeof module !== 'undefined' && module.exports) module.exports = { parse, isDelimited };+})(typeof window !== 'undefined' ? window : null);diff --git a/src/renderer/modules/file-operation-policy.js b/src/renderer/modules/file-operation-policy.jsindex fce6a7cf..f8141077 100644--- a/src/renderer/modules/file-operation-policy.js+++ b/src/renderer/modules/file-operation-policy.js@@ -4,16 +4,14 @@// that the matching backend contract will reject.(function initFileOperationPolicy(root) {const textExts = new Set([- '.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt', '.json', '.yaml', '.yml', '.log','.html', '.htm', '.xml', '.toml', '.ini', '.conf','.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs','.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt','.c', '.cpp', '.cc', '.h', '.hpp', '.css', '.scss', '.less','.sql', '.graphql', '.gql',]);- const chatTextExts = new Set([- '.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log',- ]);+ const chatTextExts = textExts;const imageExts = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif']);const videoExts = new Set(['.mp4', '.webm', '.mov', '.m4v', '.ogv']);const audioExts = new Set(['.mp3', '.wav', '.ogg', '.opus', '.m4a', '.aac', '.flac']);diff --git a/src/renderer/modules/ipc-shim.js b/src/renderer/modules/ipc-shim.jsindex ded1ab33..112897d4 100644--- a/src/renderer/modules/ipc-shim.js+++ b/src/renderer/modules/ipc-shim.js@@ -421,7 +421,7 @@ async function apiLibraryFetch(projectId, url, options = {}) {}const routes = {'/api/contexts/tree': ['tree', {}],- '/api/contexts/read': ['readText', { name }],+ '/api/contexts/read': ['readText', { name, ...(new URLSearchParams(query).get('preview') === 'true' ? { preview: true } : {}) }],'/api/contexts/image': ['image', { name }],'/api/contexts/office': ['officeHtml', { name }],'/api/contexts/mkdir': ['mkdir', { path: name }],diff --git a/src/renderer/modules/logger.js b/src/renderer/modules/logger.jsindex 128754a2..c4dbe0e7 100644--- a/src/renderer/modules/logger.js+++ b/src/renderer/modules/logger.js@@ -36,7 +36,9 @@ const createLogger = (function () {const CLOUD_PATH_RE = new RegExp(`\\bcloud\\/[^\\n\\r)'",;]+?${PATH_END}`, 'g');function sanitizeText(value) {- return String(value ?? '')+ const text = typeof value === 'string' ? value : String(value ?? '');+ if (text.length > 4096) return '[oversized text omitted]';+ return text.replace(/Bearer\s+[A-Za-z0-9._\-~+/=]+/gi, 'Bearer ***').replace(/Basic\s+[A-Za-z0-9+/=]+/gi, 'Basic ***').replace(/\beyJ[A-Za-z0-9_\-]+\.eyJ[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+\b/g, '***JWT***')@@ -50,9 +52,15 @@ const createLogger = (function () {.replace(/\b(1[3-9]\d)\d{4}(\d{4})\b/g, '$1****$2');}- function sanitizeValue(value, seen) {+ function sanitizeValue(value, seen, budget, depth = 0) {+ if (++budget.nodes > 128 || depth > 6) return '[truncated]';if (value == null) return value;- if (typeof value === 'string') return sanitizeText(value);+ if (typeof value === 'string') {+ if (budget.chars + value.length > 16384) return '[oversized text omitted]';+ budget.chars += value.length;+ return sanitizeText(value);+ }+ if (typeof value === 'function' || typeof value === 'symbol') return '[unsupported]';if (typeof value !== 'object') return value;if (seen.has(value)) return '[circular]';seen.add(value);@@ -63,50 +71,69 @@ const createLogger = (function () {stack: value.stack ? sanitizeText(value.stack) : undefined,};}- if (Array.isArray(value)) return value.map((it) => sanitizeValue(it, seen));+ if (Array.isArray(value)) {+ const items = [];+ for (let i = 0; i < Math.min(value.length, 32); i++) {+ const element = Object.getOwnPropertyDescriptor(value, String(i));+ items.push(element && 'value' in element ? sanitizeValue(element.value, seen, budget, depth + 1) : '[accessor]');+ }+ return items;+ }const out = {};- Object.entries(value).forEach(([k, v]) => {+ let keys = 0;+ for (const k in value) {+ if (++keys > 32 || budget.nodes >= 128) { out.truncated = true; break; }+ if (k.length > 128 || k === '__proto__') continue;+ const descriptor = Object.getOwnPropertyDescriptor(value, k);+ if (!descriptor) continue;+ const v = 'value' in descriptor ? descriptor.value : '[accessor]';if (SECRET_KEY_RE.test(k)) out[k] = '***REDACTED***';else if (PRIVATE_FILE_KEY_RE.test(k) && typeof v === 'string') out[k] = '***REDACTED_PATH***';- else out[k] = sanitizeValue(v, seen);- });+ else out[k] = sanitizeValue(v, seen, budget, depth + 1);+ }return out;}- function send(level, module, message, args) {- try {- if (window.orkas && typeof window.orkas.log === 'function') {- window.orkas.log({- level,- module,- message: sanitizeText(message),- data: args.map((arg) => sanitizeValue(arg, new WeakSet())),- });- }- } catch (_) { /* never let logging crash the UI */ }+ const pending = [];+ let timer = null;+ let dropped = 0;+ function flush() {+ timer = null;+ const batch = pending.splice(0, 8);+ if (dropped) {+ batch.push({ level: 'warn', module: 'logger', message: 'Log buffer full', data: [{ dropped }] });+ dropped = 0;+ }+ for (const record of batch) {+ try {+ const fn = console[record.level] || console.log;+ fn.call(console, `[${record.module}]`, record.message, ...record.data);+ } catch (_) { /* Console failures cannot prevent file delivery. */ }+ try {+ if (window.orkas && typeof window.orkas.log === 'function') window.orkas.log(record);+ } catch (_) { /* Logging cannot fail the user action. */ }+ }+ if (pending.length) timer = setTimeout(flush, 0);}-- // Console mirror — helps debugging while DevTools are open. Keeps the- // [module] prefix so the two streams read the same way.- function mirror(level, module, message, args) {+ function enqueue(level, module, message, args) {try {- const tag = `[${module}]`;- const safeMessage = sanitizeText(message);- const safeArgs = args.map((arg) => sanitizeValue(arg, new WeakSet()));- // eslint-disable-next-line no-console- const fn = console[level] || console.log;- if (safeArgs.length) fn.call(console, tag, safeMessage, ...safeArgs);- else fn.call(console, tag, safeMessage);- } catch (_) { /* noop */ }+ const limit = level === 'warn' || level === 'error' ? 256 : 192;+ if (pending.length >= limit) { dropped += 1; return; }+ // Snapshot once before crossing the async boundary; later caller changes+ // must not change the diagnostic or introduce private data.+ pending.push({ level, module, message: sanitizeText(message),+ data: sanitizeValue(args.slice(0, 16), new WeakSet(), { nodes: 0, chars: 0 }) });+ if (timer === null) timer = setTimeout(flush, 0);+ } catch (_) { /* Never let logging crash the UI. */ }}return function createLogger(moduleName) {- const m = String(moduleName || 'app').trim() || 'app';+ const m = typeof moduleName === 'string' && moduleName.length <= 128 ? sanitizeText(moduleName.trim()) || 'app' : 'app';return {- error: (msg, ...args) => { mirror('error', m, msg, args); send('error', m, msg, args); },- warn: (msg, ...args) => { mirror('warn', m, msg, args); send('warn', m, msg, args); },- info: (msg, ...args) => { mirror('info', m, msg, args); send('info', m, msg, args); },- debug: (msg, ...args) => { mirror('debug', m, msg, args); send('debug', m, msg, args); },+ error: (msg, ...args) => { enqueue('error', m, msg, args); },+ warn: (msg, ...args) => { enqueue('warn', m, msg, args); },+ info: (msg, ...args) => { enqueue('info', m, msg, args); },+ debug: (msg, ...args) => { enqueue('debug', m, msg, args); },};};})();diff --git a/src/renderer/modules/md-view-edit.js b/src/renderer/modules/md-view-edit.jsindex cbfd255b..40e72f19 100644--- a/src/renderer/modules/md-view-edit.js+++ b/src/renderer/modules/md-view-edit.js@@ -102,6 +102,8 @@ function mountMdViewEdit(opts) {// ── State ─────────────────────────────────────────────────────────────const state = {source,+ plainText: opts.plainText === true,+ partial: opts.partial === true,caps,callbacks,bodyEl,@@ -358,8 +360,14 @@ function _mveRenderView(state) {const bodyEl = state.bodyEl;const actionsEl = state.actionsEl;const content = state.content || '';- bodyEl.innerHTML = `<div class="ctx-viewer-md markdown-body">${renderMarkdown(content)}</div>`;- if (state.caps.taskCheckbox) _mveBindTaskCheckboxes(state, bodyEl);+ if (state.plainText) {+ const name = state.source.name || state.source.rel || state.source.absPath;+ if (window.DelimitedPreview?.isDelimited(name)) window.DelimitedPreview.mount(bodyEl, content, name, !!state.partial);+ else bodyEl.innerHTML = `<pre class="chat-file-viewer-text">${escapeHtml(content)}</pre>`;+ } else {+ bodyEl.innerHTML = `<div class="ctx-viewer-md markdown-body">${renderMarkdown(content)}</div>`;+ if (state.caps.taskCheckbox) _mveBindTaskCheckboxes(state, bodyEl);+ }const actions = [];if (state.caps.edit) actions.push(_mveActionButton(state, 'edit', 'contexts.viewer.edit', 'edit-pencil'));@@ -387,7 +395,7 @@ function _mveEnterEdit(state) {function _mveRenderEditor(state) {const bodyEl = state.bodyEl;const actionsEl = state.actionsEl;- const toolbarHtml = _MVE_EDITOR_TOOLBAR.map(item => {+ const toolbarHtml = (state.plainText ? [] : _MVE_EDITOR_TOOLBAR).map(item => {if (item.kind === 'sep') return `<span class="ctx-editor-toolbar-sep" aria-hidden="true"></span>`;const disabled = state.preview ? 'disabled' : '';const extraCls = item.cls ? ` ${item.cls}` : '';@@ -401,10 +409,10 @@ function _mveRenderEditor(state) {? window.uiIconHtml(state.preview ? 'edit-pencil' : 'eye', 'ui-icon ctx-editor-toggle-icon'): '';const draft = state.draft;- const bodyHtml = state.preview+ const bodyHtml = state.preview && !state.plainText? `<div class="ctx-viewer-md markdown-body ctx-editor-preview">${draft.trim() ? renderMarkdown(draft) : `<div class="ctx-viewer-msg">${escapeHtml(t('contexts.editor.preview_empty'))}</div>`}</div>`: `<textarea class="ctx-viewer-editor" data-mve-textarea spellcheck="false">${escapeHtml(draft)}</textarea>`;- bodyEl.innerHTML = `+ bodyEl.innerHTML = state.plainText ? bodyHtml : `<div class="ctx-editor-toolbar" role="toolbar">${toolbarHtml}<span class="ctx-editor-toolbar-spacer"></span>@@ -772,6 +780,17 @@ function _mveOnKey(state, ta, e) {// IME composition guard (CLAUDE.md §8).if (e.isComposing || e.keyCode === 229) return;+ if (state.plainText) {+ if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 's' && state.caps.save) {+ e.preventDefault(); return _mveSave(state);+ }+ if (e.key === 'Tab') {+ e.preventDefault();+ const start = ta.selectionStart;+ _mveReplaceRange(state, ta, start, ta.selectionEnd, '\t', start + 1, start + 1);+ }+ return;+ }const mod = e.metaKey || e.ctrlKey;if (mod && !e.shiftKey && !e.altKey) {const k = (e.key || '').toLowerCase();diff --git a/src/renderer/modules/project-detail.js b/src/renderer/modules/project-detail.jsindex 9c72353d..a51ab308 100644--- a/src/renderer/modules/project-detail.js+++ b/src/renderer/modules/project-detail.js@@ -14,7 +14,7 @@ const PROJECT_LIBRARY_INTERNAL_DRAG_TYPE = 'application/x-project-library-path';// Keep DOM drag uploads aligned with the native project-file picker and the// main-process allow-list. Project Libraries additionally support video files.const PROJECT_LIBRARY_ALLOWED_EXTS = [- '.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log',+ '.md', '.markdown', '.txt', '.csv', '.tsv', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt', '.json', '.yaml', '.yml', '.log','.html', '.htm', '.xml', '.toml', '.ini', '.conf','.py', '.pyi', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs','.sh', '.bash', '.zsh', '.ps1', '.cmd', '.bat', '.rb', '.go', '.rs', '.java', '.kt',diff --git a/src/renderer/modules/text-view-edit.js b/src/renderer/modules/text-view-edit.jsindex 710b9f8d..b51dcd22 100644--- a/src/renderer/modules/text-view-edit.js+++ b/src/renderer/modules/text-view-edit.js@@ -4,19 +4,9 @@// (`destroy / isDirty / getMode / setMode`) so chat-file-viewer can hold// either MD or text controller in `_viewerEditController` without branching.//-// Why a sibling instead of a `plainText` flag on mountMdViewEdit:-// - Plain text has no markdown toolbar, no preview toggle, no-// task-checkbox interactivity. Threading `plainText: true` through-// md-view-edit would mean three `if (!plainText) …` branches in the-// editor / view paths — duplicating logic at the call site is less-// code than gating each rendering primitive.-// - PC/CLAUDE.md §8 "only md files show the md toolbar" — a physical-// module boundary makes that invariant unforgeable; adding a flag-// leaves the toolbar visible if anyone later forgets the gate.-//-// Backend reuse: same `produced.readText` / `produced.writeText` IPC as-// the workspace branch of md-view-edit. Path-sandbox + 2 MB read cap-// already enforced on the main side, so nothing new to validate here.+// Task files use the produced-file read/write lifecycle. Library files retain+// their own draft/rename lifecycle in md-view-edit's plain-text mode. CSV/TSV+// share a bounded table preview; both editors save the literal source text.//// Source shape: { absPath: string, cid?: string, projectId?: string }// `cid` widens the read/write scope to include this conversation's@@ -56,6 +46,7 @@ function mountTextViewEdit(opts) {const state = {source,+ partial: opts.partial === true,caps,callbacks,bodyEl,@@ -146,7 +137,9 @@ function _tveRender(state) {function _tveRenderView(state) {state.mode = 'view';- state.bodyEl.innerHTML = `<pre class="chat-file-viewer-text">${escapeHtml(state.content)}</pre>`;+ if (typeof window !== 'undefined' && window.DelimitedPreview?.isDelimited(state.source.absPath)) {+ window.DelimitedPreview.mount(state.bodyEl, state.content, state.source.absPath, !!state.partial);+ } else state.bodyEl.innerHTML = `<pre class="chat-file-viewer-text">${escapeHtml(state.content)}</pre>`;const actions = [];if (state.caps.edit) actions.push(_tveActionButton(state, 'edit', 'contexts.viewer.edit', 'edit-pencil'));state.actionsEl.innerHTML = actions.join('');diff --git a/src/renderer/modules/web-app-host.js b/src/renderer/modules/web-app-host.jsindex d87d2a80..1a9476c5 100644--- a/src/renderer/modules/web-app-host.js+++ b/src/renderer/modules/web-app-host.js@@ -6,7 +6,7 @@function release(frame) {opening.delete(frame);const b = bindings.get(frame); if (!b) return;- bindings.delete(frame); frame.removeEventListener('load', b.onLoad);+ bindings.delete(frame);for (const stream of b.streams.values()) stream.cancel();window.orkas.invoke('webApps.close', { token: b.token }).catch(() => {});}@@ -15,13 +15,8 @@frame._orkasUsesSdk = !!info.token;if (!info.token) { frame.src = info.url; return; }const parsed = new URL(info.url);- const b = { token: info.token, origin: parsed.protocol + '//' + parsed.host, streams: new Map(), loaded: false, onLoad: null };- b.onLoad = () => {- if (b.loaded) release(frame); // A second document never inherits authority.- else b.loaded = true;- };+ const b = { token: info.token, origin: parsed.protocol + '//' + parsed.host, streams: new Map() };bindings.set(frame, b);- frame.addEventListener('load', b.onLoad);frame.src = info.url;}async function open(frame, source) {@@ -59,12 +54,9 @@return;}if (d.type !== 'call' || typeof d.method !== 'string') return;- if (b.streams.has(d.id) || b.streams.size >= 4) {+ if (b.streams.has(d.id)) {reply('result', { ok: false, value: { code: 'E_LIMIT' } }); return;}- let bytes;- try { bytes = new TextEncoder().encode(JSON.stringify(d.args)).length; } catch (_) { bytes = Infinity; }- if (bytes > 1024 * 1024) { reply('result', { ok: false, value: { code: 'E_LIMIT' } }); return; }reply('ack', {});let terminal = false;const stream = window.orkas.stream('webApps.call', { token: b.token, requestId: d.id, method: d.method, args: d.args }, ev => {diff --git a/src/renderer/preview.html b/src/renderer/preview.htmlindex da825770..d229dc9f 100644--- a/src/renderer/preview.html+++ b/src/renderer/preview.html@@ -18,6 +18,7 @@<script src="./modules/strip-structural-blocks.js"></script><script src="./modules/utils.js"></script><script src="./modules/file-operation-policy.js"></script>+ <script src="./modules/delimited-preview.js"></script><script src="./modules/md-view-edit.js"></script><script src="./modules/text-view-edit.js"></script><script src="./modules/math.js"></script>diff --git a/src/renderer/style.css b/src/renderer/style.cssindex 5ad37e2b..5bf9f47c 100644--- a/src/renderer/style.css+++ b/src/renderer/style.css@@ -19260,3 +19260,11 @@ body.is-task-side-panel-resizing * { cursor: col-resize !important; }outline: 2px solid var(--primary);outline-offset: -2px;}+/* CSV/TSV previews keep literal cell content in a bounded, scrollable grid. */+.delimited-preview { display: flex; flex-direction: column; flex: 1 1 auto; padding: 16px; min-width: 0; min-height: 0; }+.delimited-preview-toolbar { display: flex; flex: 0 0 auto; align-items: center; gap: 12px; margin-bottom: 12px; font-size: 12px; color: var(--text-2); }+.delimited-preview-scroll { overflow: auto; max-height: 70vh; }+.delimited-preview table { border-collapse: collapse; width: max-content; min-width: 100%; font-size: 13px; }+.delimited-preview td, .delimited-preview th { border: 1px solid var(--border); padding: 6px 10px; white-space: pre-wrap; overflow-wrap: anywhere; min-width: 70px; max-width: 480px; vertical-align: top; text-align: left; }+.delimited-preview th { position: sticky; left: 0; background: var(--surface-2); min-width: 35px; color: var(--text-2); font-weight: normal; }+.delimited-preview .chat-file-viewer-text { padding: 0; min-height: 0; overflow: auto; max-height: 70vh; }diff --git a/test/e2e/files_e2e_formats.spec.ts b/test/e2e/files_e2e_formats.spec.tsnew file mode 100644index 00000000..83d320af--- /dev/null+++ b/test/e2e/files_e2e_formats.spec.ts@@ -0,0 +1,156 @@+import { readFileSync } from 'node:fs';+import path from 'node:path';+import { expect, test } from './fixtures/orkas';++test('a descriptive file-link label does not hide the CSV format', async ({ orkas }) => {+ const file = orkas.createWorkspaceFile('report.csv', 'name,value\nOrkas,12\n');+ const preview = await orkas.openPreview(() => orkas.page!.evaluate(file =>+ (window as any).openChatFileViewer(file, '查看数据'), file));+ await expect(preview.locator('.chat-file-viewer-body')).toContainText('Orkas');+});++test('CSV opens from task attachments and the Library without changing cell contents', async ({ orkas }, info) => {+ const page = orkas.page!;+ const bytes = readFileSync(path.resolve(__dirname, '../fixtures/files/sample.csv'));+ const created: any = await orkas.invoke('conversations.create', { title: 'CSV files' });+ const cid = created.conversation.conversation_id;+ const uploaded: any = await orkas.invoke('conversations.attachments.upload', { cid, name: 'sample.csv', data: bytes.toString('base64') });+ expect(uploaded.ok).toBe(true);+ const resolved: any = await orkas.invoke('attachments.absPath', { cid, name: 'sample.csv' });+ expect(resolved.ok).toBe(true);+ const preview = await orkas.openPreview(() => page.evaluate(args =>+ (window as any).openChatFileViewer(args.path, 'sample.csv', { cid: args.cid }), { path: resolved.path, cid }));+ await expect(preview.locator('.chat-file-viewer-body')).toContainText('中文样本');+ await preview.screenshot({ path: info.outputPath('csv-task.png') });+ await expect(preview.locator('.delimited-preview td').filter({ hasText: '包含,逗号' })).toHaveCount(1);+ await expect(preview.locator('.delimited-preview td').filter({ hasText: '<b>原样文本</b>' })).toHaveCount(1);+ await preview.locator('[data-delimited-toggle]').click();+ await expect(preview.locator('pre')).toContainText('"包含,逗号"');+ await preview.locator('[data-tve-action="edit"]').click();+ const edited = bytes.toString('utf8').replace(/^\uFEFF/, '').replace('普通文本', '已编辑');+ await preview.locator('[data-tve-textarea]').fill(edited);+ await preview.locator('[data-tve-action="save"]').click();+ await expect(preview.locator('.delimited-preview td').filter({ hasText: '已编辑' })).toHaveCount(1);+ expect((await orkas.invoke<any>('produced.readText', { path: resolved.path, cid })).text.replace(/\r\n/g, '\n')).toBe(edited.replace(/\r\n/g, '\n'));+ await orkas.closePreview(preview);++ expect((await orkas.invoke<any>('contexts.upload', { path: 'sample.csv', data: bytes.toString('base64') })).ok).toBe(true);+ await page.locator('#contexts-btn').click();+ await page.locator('.ctx-tree-wrap[data-path="sample.csv"] > .skill-tree-node').click();+ await expect(page.locator('#contexts-viewer-body .delimited-preview td').filter({ hasText: '中文样本' })).toHaveCount(1);+ await page.screenshot({ path: info.outputPath('csv-library.png') });+ await expect.poll(async () => (await orkas.invoke<any>('kb.status')).files.find((f: any) => f.path === 'sample.csv')?.status,+ { timeout: 30_000 }).toBe('ready');+});++test('HTML attachments render offline interactions while the Library preserves editable source', async ({ orkas }, info) => {+ const page = orkas.page!;+ const source = readFileSync(path.resolve(__dirname, '../fixtures/files/sample.html'), 'utf8');+ const created: any = await orkas.invoke('conversations.create', { title: 'HTML files' });+ const cid = created.conversation.conversation_id;+ expect(await orkas.invoke('conversations.attachments.upload', {+ cid, name: 'sample.html', data: Buffer.from(source).toString('base64'),+ })).toMatchObject({ ok: true });+ const resolved: any = await orkas.invoke('attachments.absPath', { cid, name: 'sample.html' });+ expect(resolved.ok).toBe(true);+ const preview = await orkas.openPreview(() => page.evaluate(args =>+ (window as any).openChatFileViewer(args.path, '查看演示页面', { cid: args.cid }), { path: resolved.path, cid }));+ const frame = preview.frameLocator('.chat-file-viewer-html');+ await expect(frame.locator('h1')).toHaveText('中文页面与本地交互');+ await expect(frame.locator('svg rect')).toHaveCount(3);+ await expect(frame.locator('#count')).toHaveText('0');+ for (let i = 0; i < 3; i++) await frame.locator('#increment').click();+ await expect(frame.locator('#count')).toHaveText('3');+ await preview.screenshot({ path: info.outputPath('html-task.png') });+ await orkas.closePreview(preview);++ expect(await orkas.invoke('contexts.upload', { path: 'sample.html', data: Buffer.from(source).toString('base64') }))+ .toMatchObject({ ok: true });+ await page.locator('#contexts-btn').click();+ await page.locator('.ctx-tree-wrap[data-path="sample.html"] > .skill-tree-node').click();+ await expect(page.locator('#contexts-viewer-body pre')).toContainText('<!doctype html>');+ await expect(page.locator('#contexts-viewer-body #increment')).toHaveCount(0);+ await page.locator('[data-mve-action="edit"]').click();+ const edited = source.replace('中文测试 A', '人工编辑 A');+ await page.locator('[data-mve-textarea]').fill(edited);+ await page.locator('[data-mve-action="save"]').click();+ expect((await orkas.invoke<any>('contexts.read', { path: 'sample.html' })).content).toBe(edited);+ await expect.poll(async () => (await orkas.invoke<any>('kb.status')).files.find((f: any) => f.path === 'sample.html')?.status,+ { timeout: 30_000 })+ .toBe('ready');+});++test('large CSV previews remain bounded and cannot overwrite the original with a prefix', async ({ orkas }) => {+ // Large, valid final whitespace field exercises byte limits without turning+ // a preview regression into thousands of unrelated embedding operations.+ const content = 'name,value\n' + '中文,001\n'.repeat(300) + ' '.repeat(6 * 1024 * 1024);+ const file = orkas.createWorkspaceFile('large.csv', content);+ const preview = await orkas.openPreview(() => orkas.page!.evaluate(file =>+ (window as any).openChatFileViewer(file, 'large.csv'), file));+ await expect(preview.locator('.delimited-preview td').first()).toHaveText('name');+ await expect(preview.locator('.delimited-preview [role="status"]')).toContainText('limited preview');+ await expect(preview.locator('[data-tve-action="edit"]')).toHaveCount(0);+ await expect(preview.locator('.delimited-preview tr')).toHaveCount(200);+ expect(readFileSync(file, 'utf8')).toBe(content);+ await orkas.closePreview(preview);+ expect((await orkas.invoke<any>('contexts.upload', { path: 'large.csv', data: Buffer.from(content).toString('base64') })).ok).toBe(true);+ await orkas.page!.locator('#contexts-btn').click();+ await orkas.page!.locator('.ctx-tree-wrap[data-path="large.csv"] > .skill-tree-node').click();+ await expect(orkas.page!.locator('#contexts-viewer-body .delimited-preview tr')).toHaveCount(200);+ await expect(orkas.page!.locator('#contexts-viewer-actions [data-mve-action="edit"]')).toHaveCount(0);++ const created: any = await orkas.invoke('projects.create', { name: 'CSV project' });+ const projectId = created.project.project_id;+ expect((await orkas.invoke<any>('projects.files.upload', { projectId, name: 'large.csv', data: Buffer.from(content).toString('base64') })).ok).toBe(true);+ await orkas.page!.evaluate(async projectId => {+ await (window as any).switchCtxProject(projectId);+ await (window as any).openCtxFile('large.csv');+ }, projectId);+ await expect(orkas.page!.locator('#contexts-viewer-body .delimited-preview tr')).toHaveCount(200);+ await expect(orkas.page!.locator('#contexts-viewer-actions [data-mve-action="edit"]')).toHaveCount(0);+ // Global indexing completes; Project Library retains files beyond its+ // existing 5 MiB processing budget with an explicit indexing-limit result.+ await expect.poll(async () => (await orkas.invoke<any>('kb.status')).files.find((f: any) => f.path === 'large.csv'),+ { timeout: 30_000 }).toMatchObject({ status: 'ready', bytes: Buffer.byteLength(content) });+ await expect.poll(async () => (await orkas.invoke<any>('projects.files.status', { projectId, skipReconcile: true })).files.find((f: any) => f.name === 'large.csv'))+ .toMatchObject({ status: 'failed', bytes: Buffer.byteLength(content), errorCode: 'E_LIBRARY_FILE_TOO_LARGE' });+});++test('Library source files remain literal and keep unsaved drafts across file switches', async ({ orkas }) => {+ const page = orkas.page!;+ for (const name of ['config.jsonl', 'sample.py']) {+ expect((await orkas.invoke<any>('contexts.write', { path: name, content: name === 'config.jsonl' ? '# literal\n<b>source</b>\n- [ ] text' : '# different source' })).ok).toBe(true);+ }+ await page.locator('#contexts-btn').click();+ await page.locator('.ctx-tree-wrap[data-path="config.jsonl"] > .skill-tree-node').click();+ await expect(page.locator('#contexts-viewer-body pre')).toHaveText('# literal\n<b>source</b>\n- [ ] text');+ await expect(page.locator('#contexts-viewer-body input[type="checkbox"]')).toHaveCount(0);+ await page.locator('[data-mve-action="edit"]').click();+ await expect(page.locator('.ctx-editor-toolbar')).toHaveCount(0);+ await page.locator('[data-mve-textarea]').fill('updated literal');+ await page.locator('.ctx-tree-wrap[data-path="sample.py"] > .skill-tree-node').click();+ await page.locator('.ctx-tree-wrap[data-path="config.jsonl"] > .skill-tree-node').click();+ await expect(page.locator('[data-mve-textarea]')).toHaveValue('updated literal');+ await page.locator('[data-mve-action="save"]').click();+ expect((await orkas.invoke<any>('contexts.read', { path: 'config.jsonl' })).content).toBe('updated literal');+});++test('native local image previews decode common raster formats and aliases', async ({ orkas }) => {+ const fixtures: Array<[string, string]> = [['image.bmp', 'bmp'], ['image.avif', 'avif'], ['image.jfif', 'jpeg']];+ for (const [name, format] of fixtures) {+ const file = path.join(orkas.userWorkspaceRoot, name);+ await orkas.electronApp!.evaluate(async (_electron, args) => {+ const req = (process as any).mainModule.require.bind((process as any).mainModule);+ let output;+ if (args.format === 'bmp') {+ const { Jimp } = req(args.jimpModule);+ output = await new Jimp({ width: 1, height: 1, color: 0xff0000ff }).getBuffer('image/bmp');+ } else output = await req(args.sharpModule)({ create: { width: 1, height: 1, channels: 3, background: '#ff0000' } }).toFormat(args.format).toBuffer();+ req('node:fs').writeFileSync(args.file, output);+ }, { file, format, jimpModule: require.resolve('jimp'), sharpModule: require.resolve('sharp') });+ const preview = await orkas.openPreview(() => orkas.page!.evaluate(file =>+ (window as any).openChatFileViewer(file, 'Image'), file));+ await expect.poll(() => preview.locator('.chat-lightbox-img').evaluate((image: HTMLImageElement) => image.naturalWidth)).toBe(1);+ await orkas.closePreview(preview);+ }+});diff --git a/test/e2e/fixtures/orkas.ts b/test/e2e/fixtures/orkas.tsindex bde2d7da..14985155 100644--- a/test/e2e/fixtures/orkas.ts+++ b/test/e2e/fixtures/orkas.ts@@ -101,6 +101,15 @@ type ModelToolScenario =}| { kind: 'connector'; connectorId: string }| { kind: 'agent-authoring'; finalText: string }+ | {+ kind: 'create-pdf';+ sourceType: 'markdown' | 'html';+ content: string;+ outputPath: string;+ finalText: string;+ issued: boolean;+ toolLoadRequested: boolean;+ }| {kind: 'bash-sequence';commands: string[];@@ -800,6 +809,8 @@ export class OrkasTestApp {private activeUserId: string;private readonly configuredModelSeededUsers = new Set<string>();private modelToolScenario: ModelToolScenario | null = null;+ private generatedImageBytes = Buffer.from(E2E_PNG_B64, 'base64');+ private generatedVideoBytes = E2E_MP4_BYTES;private modelToolScenarioRequestStart = 0;private pendingAgentHandoffReply: (() => void) | null = null;private pendingCommentaryReply: (() => void) | null = null;@@ -1152,7 +1163,7 @@ export class OrkasTestApp {}setClientConfigImmediate(immediate: Record<string, unknown>): string {- if (!this.updateStub) throw new Error('The local client-config stub is not enabled for this fixture');+ if (!this.updateStub && !this.modelStub) throw new Error('The local client-config stub is not enabled for this fixture');this.clientConfigGeneration += 1;this.clientConfigImmediate = {'feature.e2e_published': 'generation-a',@@ -1287,6 +1298,22 @@ export class OrkasTestApp {};}+ setCreatePdfScenario(sourceType: 'markdown' | 'html', content: string, outputPath: string, finalText: string): void {+ if (!this.modelStub) throw new Error('The local model stub is not enabled for this fixture');+ if (!content.trim() || !path.isAbsolute(outputPath) || !finalText.trim()) {+ throw new Error('The PDF scenario requires content, an absolute output path, and a final reply');+ }+ this.modelToolScenarioRequestStart = this.modelRequests.length;+ this.modelToolScenario = { kind: 'create-pdf', sourceType, content, outputPath, finalText,+ issued: false, toolLoadRequested: false };+ }++ setGeneratedMediaFixture(kind: 'image' | 'video', bytes: Buffer): void {+ if (!this.modelStub || !bytes.length) throw new Error('Generated media requires a local model stub and nonempty bytes');+ if (kind === 'image') this.generatedImageBytes = Buffer.from(bytes);+ else this.generatedVideoBytes = Buffer.from(bytes);+ }+setDelayedWriteFileScenario(filePath: string,content: string,@@ -1902,7 +1929,7 @@ export class OrkasTestApp {return;}if (request.method === 'GET' && isImageOutput && this.modelStub) {- const image = Buffer.from(E2E_PNG_B64, 'base64');+ const image = this.generatedImageBytes;response.writeHead(200, {'Content-Type': 'image/png','Content-Length': String(image.length),@@ -1928,9 +1955,9 @@ export class OrkasTestApp {if (request.method === 'GET' && isVideoOutput && this.modelStub) {response.writeHead(200, {'Content-Type': 'video/mp4',- 'Content-Length': String(E2E_MP4_BYTES.length),+ 'Content-Length': String(this.generatedVideoBytes.length),});- response.end(E2E_MP4_BYTES);+ response.end(this.generatedVideoBytes);return;}const validGenerationUpload = this.modelStub@@ -2742,6 +2769,21 @@ export class OrkasTestApp {}return;}+ if (scenario?.kind === 'create-pdf') {+ if (!scenario.issued && !advertisedToolNames.has('create_pdf')+ && advertisedToolNames.has('tool_load') && !scenario.toolLoadRequested) {+ scenario.toolLoadRequested = true;+ requestToolGroup('call-e2e-load-create-pdf', 'office.pdf');+ } else if (!scenario.issued) {+ scenario.issued = true;+ finishImmediately(toolCallEvents('call-e2e-create-pdf', 'create_pdf', {+ path: scenario.outputPath, source_type: scenario.sourceType, content: scenario.content,+ }));+ } else {+ finishImmediately(finalTextEvents(scenario.finalText));+ }+ return;+ }if (scenario?.kind === 'write-file') {if (!scenario.writeIssued&& !advertisedToolNames.has('write_file')diff --git a/test/e2e/html_preview_e2e_offline_policy.spec.ts b/test/e2e/html_preview_e2e_offline_policy.spec.tsindex 8d9a5c5f..72ce44a5 100644--- a/test/e2e/html_preview_e2e_offline_policy.spec.ts+++ b/test/e2e/html_preview_e2e_offline_policy.spec.ts@@ -32,7 +32,7 @@ async function holdFirstHtmlResponse(app: ElectronApplication, html: string) {return () => app.evaluate(() => (globalThis as any).__htmlResponseGate.release());}-test.describe('local HTML offline preview', () => {+test.describe('local HTML preview', () => {test('audits filter panels after a dense graphic and retains missing effects as inconclusive', async ({ orkas }) => {const modulePath = path.resolve(__dirname, '../../src/main/features/html_preview.ts');for (const working of [true, false]) {@@ -164,146 +164,27 @@ test.describe('local HTML offline preview', () => {}});- test('runs self-contained code while blocking remote code, assets, connections, and navigation', async ({- appPage,- orkas,- }) => {- const requests: string[] = [];- const server = createServer((request, response) => {- requests.push(request.url || '/');- if (request.url?.startsWith('/external.js')) {- response.writeHead(200, { 'Content-Type': 'text/javascript' });- response.end('window.__offlinePreviewEvidence.externalScriptRan = true;');- return;- }- if (request.url?.startsWith('/pixel.png')) {- response.writeHead(200, { 'Content-Type': 'image/png' });- response.end(PNG_1X1);- return;- }- if (request.url?.startsWith('/external.css')) {- response.writeHead(200, { 'Content-Type': 'text/css' });- response.end('#inline-result { color: rgb(1, 2, 3); }');- return;- }- response.writeHead(200, { 'Content-Type': 'text/html' });- response.end('<!doctype html><title>remote</title>');- });-- await new Promise<void>((resolve, reject) => {- server.once('error', reject);- server.listen(0, '127.0.0.1', () => resolve());- });-- try {- const address = server.address() as AddressInfo;- const remoteBase = `http://127.0.0.1:${address.port}`;- const localImagePath = path.join(orkas.userWorkspaceRoot, 'offline-preview-local.png');- const htmlPath = path.join(orkas.userWorkspaceRoot, 'offline-preview.html');- const localImageUrl = chatMediaLocalUrl(localImagePath);-- writeFileSync(localImagePath, PNG_1X1);- writeFileSync(htmlPath, `<!doctype html>-<html>-<body>- <script>- window.__offlinePreviewEvidence = {- inlineRan: false,- dataImageLoaded: false,- localImageLoaded: false,- externalScriptRan: false,- fetchRejected: false,- navigationAttempted: false,- violations: [],- };- document.addEventListener('securitypolicyviolation', (event) => {- window.__offlinePreviewEvidence.violations.push(event.effectiveDirective);- });- </script>- <link rel="stylesheet" href="${remoteBase}/external.css?private=value">- <link rel="preload" as="font" href="${remoteBase}/external.woff2?private=value">- <div id="inline-result">pending</div>- <img id="data-image"- src="data:image/png;base64,${PNG_1X1.toString('base64')}"- onload="window.__offlinePreviewEvidence.dataImageLoaded = true">- <img id="local-image"- src="${localImageUrl}"- onload="window.__offlinePreviewEvidence.localImageLoaded = true">- <script>- window.__offlinePreviewEvidence.inlineRan = true;- document.getElementById('inline-result').textContent = 'inline-ok';- fetch('${remoteBase}/connect?private=value').catch(() => {- window.__offlinePreviewEvidence.fetchRejected = true;+ test('loads remote code, styles and APIs while keeping the host isolated', async ({ appPage, orkas }) => {+ const requests:string[]=[];+ const server=createServer((req,res)=>{+ requests.push(req.url || '/');res.setHeader('Access-Control-Allow-Origin','*');+ if(req.url==='/app.js'){res.setHeader('Content-Type','text/javascript');res.end('document.querySelector("output").textContent="remote loaded";');}+ else if(req.url==='/app.css'){res.setHeader('Content-Type','text/css');res.end('output{color:rgb(1,2,3)}');}+ else if(req.url==='/api'){res.setHeader('Content-Type','application/json');res.end('{"value":42}');}+ else {res.setHeader('Content-Type','text/html');res.end('<h1>External page</h1>');}});- const xhr = new XMLHttpRequest();- xhr.open('GET', '${remoteBase}/xhr?private=value');- xhr.send();+ await new Promise<void>(resolve=>server.listen(0,'127.0.0.1',resolve));try {- new WebSocket('${remoteBase.replace('http:', 'ws:')}/socket?private=value');- } catch {}- navigator.sendBeacon('${remoteBase}/beacon?private=value', 'private=value');- setTimeout(() => {- window.__offlinePreviewEvidence.navigationAttempted = true;- window.location.href = '${remoteBase}/navigate?private=value';- }, 100);- </script>- <script src="${remoteBase}/external.js?private=value"></script>- <img src="${remoteBase}/pixel.png?private=value">- <video autoplay src="${remoteBase}/external.mp4?private=value"></video>- <iframe src="${remoteBase}/nested?private=value"></iframe>-</body>-</html>`);-- const preview = await orkas.openPreview(() => appPage.evaluate((pathValue) => {- void (window as any).openChatFileViewer(pathValue, 'offline-preview.html');- }, htmlPath));-- const previewElement = preview.locator('.chat-file-viewer-html');- await expect(previewElement).toBeVisible();- const previewSrc = await previewElement.getAttribute('src');- await expect.poll(() => preview.frames().some((frame) => frame.url() === previewSrc)).toBe(true);- const previewFrame = preview.frames().find((frame) => frame.url() === previewSrc);- if (!previewFrame) throw new Error('Local HTML preview frame did not load');-- await expect.poll(async () => previewFrame.evaluate(() => {- const evidence = (window as any).__offlinePreviewEvidence;- return evidence ? {- inlineRan: evidence.inlineRan,- dataImageLoaded: evidence.dataImageLoaded,- localImageLoaded: evidence.localImageLoaded,- externalScriptRan: evidence.externalScriptRan,- fetchRejected: evidence.fetchRejected,- navigationAttempted: evidence.navigationAttempted,- violations: Array.from(new Set(evidence.violations)).sort(),- inlineText: document.getElementById('inline-result')?.textContent || '',- } : null;- })).toMatchObject({- inlineRan: true,- dataImageLoaded: true,- localImageLoaded: true,- externalScriptRan: false,- fetchRejected: true,- navigationAttempted: true,- inlineText: 'inline-ok',- });-- const evidence = await previewFrame.evaluate(() => (window as any).__offlinePreviewEvidence);- expect(evidence.violations).toEqual(expect.arrayContaining([- 'connect-src',- 'font-src',- 'frame-src',- 'img-src',- 'media-src',- 'script-src-elem',- 'style-src-elem',- ]));- expect(previewFrame.url()).toMatch(/^chat-media:\/\/local\//);- expect(requests).toEqual([]);- } finally {- if ('closeAllConnections' in server && typeof server.closeAllConnections === 'function') {- server.closeAllConnections();- }- await new Promise<void>((resolve) => server.close(() => resolve()));- }+ const base=`http://127.0.0.1:${(server.address() as AddressInfo).port}`;+ const entry=orkas.createWorkspaceFile('online-preview.html',`<!doctype html><link rel="stylesheet" href="${base}/app.css"><output></output><script src="${base}/app.js"></script><a href="${base}/page">Open page</a>`);+ const preview=await orkas.openPreview(()=>appPage.evaluate(p=>{void (window as any).openChatFileViewer(p,'online-preview.html');},entry));+ const frame=preview.locator('.chat-file-viewer-html').contentFrame();+ await expect(frame.locator('output')).toHaveText('remote loaded');+ await expect(frame.locator('output')).toHaveCSS('color','rgb(1, 2, 3)');+ expect(await frame.locator('body').evaluate(async(_node,base)=>({api:await (await fetch(base+'/api')).json(),node:typeof (window as any).require,ipc:typeof (window as any).orkas}),base)).toEqual({api:{value:42},node:'undefined',ipc:'undefined'});+ await frame.getByRole('link',{name:'Open page'}).click();+ await expect(frame.getByRole('heading')).toHaveText('External page');+ expect(requests).toEqual(expect.arrayContaining(['/app.js','/app.css','/api','/page']));+ }finally{server.closeAllConnections();await new Promise<void>(resolve=>server.close(()=>resolve()));}});});diff --git a/test/e2e/preview_windows_e2e_lifecycle.spec.ts b/test/e2e/preview_windows_e2e_lifecycle.spec.tsindex 4f391681..bdc1a92e 100644--- a/test/e2e/preview_windows_e2e_lifecycle.spec.ts+++ b/test/e2e/preview_windows_e2e_lifecycle.spec.ts@@ -159,10 +159,10 @@ test('legacy application results return to their original task after main-windowconst page = orkas.page!;const created: any = await orkas.invoke('conversations.create', { title: 'Application result destination' });const cid = created.conversation.conversation_id;- const artifact: any = await orkas.electronApp!.evaluate((_electron, args) => {- return (process as any).mainModule.require(args.module).createArtifact('account-e2e', args.cid, 'commander', {+ const artifact: any = await orkas.electronApp!.evaluate(async (_electron, args) => {+ return (await (process as any).mainModule.require(args.module).createArtifact('account-e2e', args.cid, 'commander', {title: 'Choice', files: [{ path: 'index.html', content: '<!doctype html><button onclick="parent.postMessage({__orkasArtifact:true,type:\'submit\',payload:{choice:7}},\'*\')">Submit choice</button>' }],- });+ }));}, { module: path.resolve(__dirname, '../../src/main/features/chat_artifacts.ts'), cid });expect(artifact.ok).toBe(true);const preview = await orkas.openPreview(() => page.evaluate(async args => {@@ -193,13 +193,13 @@ test('saving from an app preview retains its original bundle after task navigaticonst page = orkas.page!;const created: any = await orkas.invoke('conversations.create', { title: 'App to keep' });const cid = created.conversation.conversation_id;- const artifact: any = await orkas.electronApp!.evaluate((_electron, args) =>- (process as any).mainModule.require(args.module).createArtifact('account-e2e', args.cid, 'commander', {+ const artifact: any = await orkas.electronApp!.evaluate(async (_electron, args) =>+ (await (process as any).mainModule.require(args.module).createArtifact('account-e2e', args.cid, 'commander', {title: 'Keep this app', files: [{ path: 'index.html', content: '<!doctype html><h1>Original app</h1><script src="app.js"></script>' },{ path: 'app.js', content: 'document.querySelector("h1").textContent = "Bundle loaded";' },],- }), { module: path.resolve(__dirname, '../../src/main/features/chat_artifacts.ts'), cid });+ })), { module: path.resolve(__dirname, '../../src/main/features/chat_artifacts.ts'), cid });expect(artifact.ok).toBe(true);const preview = await orkas.openPreview(() => page.evaluate(source =>(window as any).openChatArtifactViewer(source), { cid, artifactId: artifact.artifactId, title: 'Keep this app' }));diff --git a/test/e2e/video_capture_e2e_frames.spec.ts b/test/e2e/video_capture_e2e_frames.spec.tsindex 50f79bf4..4c379e28 100644--- a/test/e2e/video_capture_e2e_frames.spec.ts+++ b/test/e2e/video_capture_e2e_frames.spec.ts@@ -148,7 +148,8 @@ test.describe('scaffold child motion', () => {.replace('// ORKAS-SCENE-MOTION-BEGIN:blue',"tl.from('#scene-blue [data-role=visual]', {y:30, autoAlpha:.55, duration:.2, stagger:.05}, 2.1);");fs.writeFileSync(htmlPath, html);- const results: {time: number; path: string; opacity: number; visibility: string}[] = [];+ const results: {time: number; path: string; opacity: number; visibility: string;+ x: number; y: number; viewportWidth: number; viewportHeight: number}[] = [];for (const times of [[1.5], [0, .5, 1.5, 2.5, .5, 1.5]]) {const win = new BrowserWindow({ show:false, width:1920, height:1080, useContentSize:true,webPreferences: { offscreen:true, nodeIntegration:false, contextIsolation:true, sandbox:true } });@@ -162,8 +163,11 @@ test.describe('scaffold child motion', () => {win.webContents.invalidate();await win.webContents.executeJavaScript('new Promise(r=>requestAnimationFrame(()=>setTimeout(()=>requestAnimationFrame(r),0)))');const state = await win.webContents.executeJavaScript(`(() => {- const style=getComputedStyle(document.querySelector('#scene-${['red','green','blue'][Math.floor(time)]} [data-role="visual"]'));- return {opacity:Number(style.opacity), visibility:style.visibility};+ const node=document.querySelector('#scene-${['red','green','blue'][Math.floor(time)]} [data-role="visual"]');+ const style=getComputedStyle(node), rect=node.getBoundingClientRect();+ return {opacity:Number(style.opacity), visibility:style.visibility,+ x:rect.left+rect.width/2, y:rect.top+rect.height/2,+ viewportWidth:innerWidth, viewportHeight:innerHeight};})()`);const png = path.join(args.dir, `child-${results.length}.png`);fs.writeFileSync(png, (await win.webContents.capturePage()).toPNG());@@ -179,7 +183,11 @@ test.describe('scaffold child motion', () => {const visible = sample.time !== .5;expect(sample.opacity, `child at ${sample.time}s`).toBe(visible ? 1 : 0);expect(sample.visibility === 'hidden').toBe(!visible);- const pixel = await sharp(sample.path).resize(1920, 1080).extract({left:900,top:500,width:1,height:1}).raw().toBuffer();+ const image = sharp(sample.path);+ const metadata = await image.metadata();+ const left = Math.floor(sample.x * Number(metadata.width) / sample.viewportWidth);+ const top = Math.floor(sample.y * Number(metadata.height) / sample.viewportHeight);+ const pixel = await image.extract({left,top,width:1,height:1}).raw().toBuffer();const expected = visible ? [255,255,255] : [25,93,239];for (let c=0;c<3;c++) expect(Math.abs(pixel[c]-expected[c]), sample.path).toBeLessThan(8);}diff --git a/test/fixtures/files/sample.csv b/test/fixtures/files/sample.csvnew file mode 100644index 00000000..38ad6eac--- /dev/null+++ b/test/fixtures/files/sample.csv@@ -0,0 +1,6 @@+"编号","名称","数量","备注"+"001","中文样本","12","普通文本"+"002","包含,逗号","0","包含""双引号"""+"003","多行内容","3","第一行+第二行"+"004","空值测试","","<b>原样文本</b>"diff --git a/test/fixtures/files/sample.html b/test/fixtures/files/sample.htmlnew file mode 100644index 00000000..09addf7e--- /dev/null+++ b/test/fixtures/files/sample.html@@ -0,0 +1,23 @@+<!doctype html>+<html lang="zh-CN">+<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">+<title>Orkas HTML 测试页</title>+<style>+*{box-sizing:border-box}body{margin:0;background:#f2f5fa;color:#172538;font:16px/1.65 system-ui,sans-serif}+main{max-width:880px;margin:40px auto;padding:32px;background:white;border:1px solid #dce3ed;border-radius:16px}+h1{font-size:28px;margin:8px 0 12px}.tag{color:#286452;font-weight:600;font-size:13px}p{color:#526377}+.cards{display:grid;grid-template-columns:1fr 1fr;gap:18px;margin:24px 0}.card{border:1px solid #dce3ed;border-radius:10px;padding:20px}+button{border:0;border-radius:8px;background:#2458d3;color:white;padding:10px 18px;font:inherit;cursor:pointer}+button:focus-visible{outline:3px solid #81b3ff;outline-offset:3px}output{display:block;font-size:30px;font-weight:700;margin:10px 0}+table{width:100%;border-collapse:collapse}th,td{text-align:left;padding:9px;border-bottom:1px solid #dce3ed}th{background:#f4f7fb}+svg{width:100%;height:115px}.note{font-size:13px}@media(max-width:600px){main{margin:16px;padding:20px}.cards{grid-template-columns:1fr}h1{font-size:24px}}+</style></head>+<body><main><div class="tag">ORKAS · 离线 HTML 测试</div><h1>中文页面与本地交互</h1>+<p>应看到完整页面、两张卡片、下方表格。此文件不依赖外部图片、字体或脚本。</p>+<div class="cards"><section class="card"><strong>按钮交互</strong><output id="count" aria-live="polite">0</output>+<button id="increment" type="button">点击加一</button><p class="note">点击 3 次,应显示 3。</p></section>+<section class="card"><strong>内嵌 SVG 图形</strong><svg viewBox="0 0 300 115" role="img" aria-label="三根蓝绿色柱形">+<rect x="25" y="60" width="55" height="40" fill="#2458d3"/><rect x="120" y="35" width="55" height="65" fill="#3478db"/><rect x="215" y="10" width="55" height="90" fill="#209d80"/></svg></section></div>+<table><thead><tr><th>项目</th><th>数量</th></tr></thead><tbody><tr><td>中文测试 A</td><td>12</td></tr><tr><td>中文测试 B</td><td>3</td></tr><tr><td>合计</td><td>15</td></tr></tbody></table>+<p class="note">资料库默认应显示 HTML 源码;独立预览应显示此网页。切换窄窗口时,卡片应上下排列。</p>+</main><script>document.getElementById('increment').addEventListener('click',function(){const counter=document.getElementById('count');counter.textContent=String(Number(counter.textContent)+1);});</script></body></html>diff --git a/test/fixtures/office/text-collision-render.cjs b/test/fixtures/office/text-collision-render.cjsindex cb19b732..9b342dbf 100644--- a/test/fixtures/office/text-collision-render.cjs+++ b/test/fixtures/office/text-collision-render.cjs@@ -7,7 +7,10 @@ require(path.join(root, 'node_modules/tsx/dist/cjs/index.cjs'));const { PPTX_TEXT_COLLISION_SCRIPT } = require(path.join(root, 'src/main/features/office/pptx_text_collision.ts'));const { hardenedWebPreferences } = require(path.join(root, 'src/main/util/window-security.ts'));const shape = (id, x, y, text, css = '') => `<div class="shape" data-path="/slide[1]/shape[@id=${id}]" style="left:${x}px;top:${y}px;${css.replace(/"/g, """)}"><div class="shape-text"><span>${text}</span></div></div>`;-const overlap = shape(1, 10, 10, 'ABCDEFG') + shape(2, 85, 10, 'XYZ');+// Chromium's generic monospace advances differ by platform (10px on Windows,+// roughly 12px on macOS at this font size). Keep the fixture's overlap real in+// either layout engine instead of relying on the wider macOS fallback.+const overlap = shape(1, 10, 10, 'ABCDEFG') + shape(2, 75, 10, 'XYZ');const cases = {collision: overlap,observedCjkPair: shape(100114, 91.2, 494.4, '需要客户确认的信息', 'width:220.8px;font: bold 24px/24px "Microsoft YaHei","PingFang SC","Noto Sans CJK SC",sans-serif')diff --git a/test/helpers/capture-main-log-workers.ts b/test/helpers/capture-main-log-workers.tsnew file mode 100644index 00000000..63eaf9b2--- /dev/null+++ b/test/helpers/capture-main-log-workers.ts@@ -0,0 +1,32 @@+import { expect, vi } from 'vitest';++/** Track the real workers after resetModules so workspace teardown cannot race+ * queued log writes. Wait for delivery before termination to retain evidence. */+export async function captureMainLogWorkers(): Promise<() => Promise<void>> {+ type DeliveryModule = typeof import('../../src/main/util/log-delivery');+ const deliveries: ReturnType<DeliveryModule['createLogDelivery']>[] = [];+ // A persistent module mock, rather than a spy on one module instance, also+ // captures workers created after a case calls resetModules() again.+ vi.doMock('../../src/main/util/log-delivery', async importOriginal => {+ const module = await importOriginal<DeliveryModule>();+ return {+ ...module,+ createLogDelivery: (...args: Parameters<DeliveryModule['createLogDelivery']>) => {+ const delivery = module.createLogDelivery(...args);+ deliveries.push(delivery);+ return delivery;+ },+ };+ });+ return async () => {+ try {+ for (const delivery of deliveries) {+ await vi.waitFor(() => expect(delivery.stats().pending).toBe(0));+ expect(delivery.stats()).toMatchObject({ dropped: 0, failed: false });+ }+ } finally {+ await Promise.all(deliveries.map(delivery => delivery.close()));+ vi.doUnmock('../../src/main/util/log-delivery');+ }+ };+}diff --git a/test/helpers/parse-json-object.ts b/test/helpers/parse-json-object.tsnew file mode 100644index 00000000..095fc1e0--- /dev/null+++ b/test/helpers/parse-json-object.ts@@ -0,0 +1,23 @@+/** Parse one JSON object from a mixed stdout stream without assuming it is the+ * final output. Background diagnostics may be flushed after the scorecard. */+export function parseJsonObjectAt<T>(text: string, start: number): { value: T; end: number } {+ if (start < 0 || text[start] !== '{') throw new Error('JSON object start was not found');+ let depth = 0;+ let inString = false;+ let escaped = false;+ for (let index = start; index < text.length; index += 1) {+ const char = text[index];+ if (inString) {+ if (escaped) escaped = false;+ else if (char === '\\') escaped = true;+ else if (char === '"') inString = false;+ continue;+ }+ if (char === '"') inString = true;+ else if (char === '{') depth += 1;+ else if (char === '}' && --depth === 0) {+ return { value: JSON.parse(text.slice(start, index + 1)) as T, end: index + 1 };+ }+ }+ throw new Error('JSON object was not terminated');+}diff --git a/test/main/avatar-catalogs.test.ts b/test/main/avatar-catalogs.test.tsindex 865360d6..9a355e11 100644--- a/test/main/avatar-catalogs.test.ts+++ b/test/main/avatar-catalogs.test.ts@@ -195,6 +195,7 @@ describe('avatar catalogs', () => {const knownIcons = new Set(catalog.icons.map((icon: any) => icon.id));const knownColors = new Set(catalog.colors.map((color: any) => color.id));const expected = [+ ['resources/builtin/marketplace/agents/1040b336306f/agent.json', 'StockAnalyser', 'chart', 'mint'],['resources/builtin/marketplace/agents/79df9cc89f5f/agent.json', 'VideoStudio', 'film', 'violet'],['resources/builtin/marketplace/agents/5f890bd72ac4/agent.json', 'VoiceStudio', 'music', 'violet'],['resources/builtin/marketplace/agents/814b61b027f0/agent.json', 'ImageStudio', 'image', 'violet'],diff --git a/test/main/core-regression.test.ts b/test/main/core-regression.test.tsindex 9e31ecd4..84e61d02 100644--- a/test/main/core-regression.test.ts+++ b/test/main/core-regression.test.ts@@ -202,13 +202,13 @@ describe('PC core regression unit coverage', () => {const savedApps = await import('../../src/main/features/saved_apps');const cid = 'cid_artifact_regression';- const created = artifacts.createArtifact(TEST_UID, cid, 'RegressionAgent', {+ const created = (await artifacts.createArtifact(TEST_UID, cid, 'RegressionAgent', {title: 'Regression App',files: [{ path: 'index.html', content: '<!doctype html><script src="assets/app.js"></script><h1>Regression</h1>' },{ path: 'assets/app.js', content: 'window.result = 42;' },],- });+ }));expect(created.ok).toBe(true);if (!created.ok) return;@@ -220,7 +220,7 @@ describe('PC core regression unit coverage', () => {}expect(artifacts.resolveArtifactFilePath(TEST_UID, cid, created.artifactId, '../secrets.txt').ok).toBe(false);- const saved = savedApps.saveFromArtifact(TEST_UID, cid, created.artifactId);+ const saved = (await savedApps.saveFromArtifact(TEST_UID, cid, created.artifactId));expect(saved.ok).toBe(true);if (!saved.ok) return;expect(savedApps.listSavedApps(TEST_UID).map((app) => app.title)).toEqual(['Regression App']);diff --git a/test/main/features/builtin_marketplace.test.ts b/test/main/features/builtin_marketplace.test.tsindex 6906ce97..430866e9 100644--- a/test/main/features/builtin_marketplace.test.ts+++ b/test/main/features/builtin_marketplace.test.ts@@ -550,6 +550,76 @@ describe('builtin marketplace seed', () => {});});+ it('takes over an older resource-owned agent when it becomes a platform builtin', async () => {+ writeBuiltinAgent(TEST_AGENT_ID, {+ version: '1.1.13',+ name: 'StockAnalyser',+ description: 'Bundled stock analysis Agent',+ category: 'finance',+ workflow: 'Use the bundled workflow.',+ skill_list: ['market-data'],+ updated_at: '2026-09-21T08:21:00.000Z',+ });+ writeBuiltinAgentSkill(TEST_AGENT_ID, 'market-data', 'market-data');++ const seed = await import('../../../src/main/features/builtin_marketplace');+ const paths = await import('../../../src/main/paths');+ const installs = await import('../../../src/main/features/marketplace_installs');+ const localRoot = paths.userMarketplaceAgentDir('u1', TEST_AGENT_ID);+ fs.mkdirSync(path.join(localRoot, 'skills', 'legacy-stock'), { recursive: true });+ fs.writeFileSync(path.join(localRoot, 'agent.json'), JSON.stringify({+ agent_id: TEST_AGENT_ID,+ version: '1.1.12',+ name: 'StockAnalyser',+ category: 'finance',+ workflow: 'Use the Resource workflow.',+ skill_list: ['legacy-stock'],+ }, null, 2), 'utf8');+ fs.writeFileSync(+ path.join(localRoot, 'skills', 'legacy-stock', 'SKILL.md'),+ '---\nname: legacy-stock\ndescription: legacy\n---\n\nlegacy body\n',+ 'utf8',+ );+ writeResourceSeedManifest(localRoot, 'agent', TEST_AGENT_ID);+ fs.writeFileSync(path.join(localRoot, 'runtime-note.txt'), 'preserve local runtime data\n', 'utf8');+ fs.writeFileSync(path.join(localRoot, '_install.json'), JSON.stringify({+ version: '1.1.12',+ installed_at: 12,+ create_uid: '0',+ default_install: false,+ seed_source: 'resource',+ }, null, 2), 'utf8');++ await expect(seed.seedBuiltinMarketplaceForUser('u1')).resolves.toMatchObject({+ seeded_agents: 1,+ manifest_agents: 1,+ });++ expect(JSON.parse(fs.readFileSync(path.join(localRoot, 'agent.json'), 'utf8'))).toMatchObject({+ version: '1.1.13',+ workflow: 'Use the bundled workflow.',+ skill_list: ['market-data'],+ });+ expect(fs.existsSync(path.join(localRoot, 'skills', 'market-data', 'SKILL.md'))).toBe(true);+ expect(fs.existsSync(path.join(localRoot, 'skills', 'legacy-stock'))).toBe(false);+ expect(fs.existsSync(path.join(localRoot, MARKETPLACE_RESOURCE_MANIFEST_NAME))).toBe(false);+ expect(fs.readFileSync(path.join(localRoot, 'runtime-note.txt'), 'utf8'))+ .toBe('preserve local runtime data\n');+ expect(JSON.parse(fs.readFileSync(path.join(localRoot, '_install.json'), 'utf8'))).toMatchObject({+ version: '1.1.13',+ seed_source: 'builtin',+ default_install: false,+ });+ expect((await installs.readInstalls('u1')).agents).toEqual([+ expect.objectContaining({+ id: TEST_AGENT_ID,+ version: '1.1.13',+ seed_source: 'builtin',+ default_install: true,+ }),+ ]);+ });+it('refreshes builtin skill content without replacing local-only files', async () => {writeBuiltinSkill('ee99fbb42964', 'deep-research');writeBuiltinSkillMeta('ee99fbb42964', {diff --git a/test/main/features/chat_artifacts.test.ts b/test/main/features/chat_artifacts.test.tsindex 6858dc14..12807070 100644--- a/test/main/features/chat_artifacts.test.ts+++ b/test/main/features/chat_artifacts.test.ts@@ -1,3 +1,4 @@+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';@@ -10,16 +11,19 @@ const AGENT = 'helper';let tmpDir: string;let prevWs: string | undefined;+let closeLogWorkers: () => Promise<void>;beforeEach(async () => {tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-chatart-'));prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();const users = await import('../../../src/main/features/users');users.activateUser(UID);});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });});@@ -49,10 +53,10 @@ describe('chat_artifacts › bridge injection', () => {it('gives an ordinary artifact the auto-sizing bridge without being asked', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {title: 'Sized',files: [{ path: 'index.html', content: '<!doctype html><html><head><title>a</title></head><body><button>go</button></body></html>' }],- });+ }));expect(r.ok).toBe(true);if (!r.ok) return;const html = readEntry(path.join(cidDir(), r.artifactId));@@ -66,7 +70,7 @@ describe('chat_artifacts › bridge injection', () => {it('does not inject twice when the artifact already loads the bridge', async () => {const m = await loadMod();const authored = `<!doctype html><html><head>${m.BRIDGE_SCRIPT_TAG}</head><body><button>go</button></body></html>`;- const r = m.createArtifact(UID, CID, AGENT, { title: 'Own bridge', files: [{ path: 'index.html', content: authored }] });+ const r = (await m.createArtifact(UID, CID, AGENT, { title: 'Own bridge', files: [{ path: 'index.html', content: authored }] }));expect(r.ok).toBe(true);if (!r.ok) return;const html = readEntry(path.join(cidDir(), r.artifactId));@@ -79,7 +83,7 @@ describe('chat_artifacts › bridge injection', () => {const authored = '<!doctype html><html><head><script>'+ 'parent.postMessage({ __orkasArtifact: true, type: "resize", height: 300 }, "*");'+ '</script></head><body><button>go</button></body></html>';- const r = m.createArtifact(UID, CID, AGENT, { title: 'Self sizing', files: [{ path: 'index.html', content: authored }] });+ const r = (await m.createArtifact(UID, CID, AGENT, { title: 'Self sizing', files: [{ path: 'index.html', content: authored }] }));expect(r.ok).toBe(true);if (!r.ok) return;expect(readEntry(path.join(cidDir(), r.artifactId))).not.toContain(m.BRIDGE_RELPATH);@@ -87,10 +91,10 @@ describe('chat_artifacts › bridge injection', () => {it('places the tag in a fragment that has no head or body', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {title: 'Fragment',files: [{ path: 'index.html', content: '<h1>hi</h1><button>go</button>' }],- });+ }));expect(r.ok).toBe(true);if (!r.ok) return;const html = readEntry(path.join(cidDir(), r.artifactId));@@ -101,13 +105,13 @@ describe('chat_artifacts › bridge injection', () => {it('only touches the entry file', async () => {const m = await loadMod();const helper = 'export const value = 1;\n';- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {title: 'Multi',files: [{ path: 'index.html', content: '<!doctype html><html><body><button>go</button></body></html>' },{ path: 'app.js', content: helper },],- });+ }));expect(r.ok).toBe(true);if (!r.ok) return;const dir = path.join(cidDir(), r.artifactId);@@ -128,7 +132,7 @@ describe('chat_artifacts › bridge injection', () => {describe('chat_artifacts › createArtifact', () => {it('accepts a minimal one-file app, stamps meta, returns the id', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, { title: 'Tip calc', files: MIN_FILES });+ const r = (await m.createArtifact(UID, CID, AGENT, { title: 'Tip calc', files: MIN_FILES }));expect(r.ok).toBe(true);if (!r.ok) return;expect(r.artifactId).toMatch(/^[A-Za-z0-9_-]{8,}$/);@@ -145,14 +149,14 @@ describe('chat_artifacts › createArtifact', () => {it('accepts a multi-file app with a nested asset', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {title: 'Dash',files: [{ path: 'index.html', content: '<!doctype html><script src="assets/app.js"></script>' },{ path: 'assets/app.js', content: 'console.log(1)' },{ path: 'style.css', content: 'body{margin:0}' },],- });+ }));expect(r.ok).toBe(true);if (!r.ok) return;const dir = path.join(cidDir(), r.artifactId);@@ -162,8 +166,8 @@ describe('chat_artifacts › createArtifact', () => {it('discards one failed candidate without removing sibling artifacts', async () => {const m = await loadMod();- const first = m.createArtifact(UID, CID, AGENT, { title: 'First', files: MIN_FILES });- const second = m.createArtifact(UID, CID, AGENT, { title: 'Second', files: MIN_FILES });+ const first = (await m.createArtifact(UID, CID, AGENT, { title: 'First', files: MIN_FILES }));+ const second = (await m.createArtifact(UID, CID, AGENT, { title: 'Second', files: MIN_FILES }));expect(first.ok).toBe(true);expect(second.ok).toBe(true);if (!first.ok || !second.ok) return;@@ -185,12 +189,12 @@ describe('chat_artifacts › createArtifact', () => {it('accepts base64-encoded binary content for an image asset', async () => {const m = await loadMod();const pngB64 = Buffer.from([0x89, 0x50, 0x4e, 0x47]).toString('base64');- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {files: [{ path: 'index.html', content: '<!doctype html><img src="logo.png">' },{ path: 'logo.png', content: pngB64, encoding: 'base64' },],- });+ }));expect(r.ok).toBe(true);if (!r.ok) return;const buf = fs.readFileSync(path.join(cidDir(), r.artifactId, 'logo.png'));@@ -199,7 +203,7 @@ describe('chat_artifacts › createArtifact', () => {it('rejects: no index.html', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, { files: [{ path: 'main.html', content: 'x' }] });+ const r = (await m.createArtifact(UID, CID, AGENT, { files: [{ path: 'main.html', content: 'x' }] }));expect(r.ok).toBe(false);if (r.ok) return;expect(r.error).toMatch(/index\.html/);@@ -207,82 +211,77 @@ describe('chat_artifacts › createArtifact', () => {it('rejects: empty / non-array files', async () => {const m = await loadMod();- expect((m.createArtifact(UID, CID, AGENT, { files: [] }) as { ok: boolean }).ok).toBe(false);- expect((m.createArtifact(UID, CID, AGENT, { files: 'nope' as unknown as [] }) as { ok: boolean }).ok).toBe(false);+ expect(((await m.createArtifact(UID, CID, AGENT, { files: [] })) as { ok: boolean }).ok).toBe(false);+ expect(((await m.createArtifact(UID, CID, AGENT, { files: 'nope' as unknown as [] })) as { ok: boolean }).ok).toBe(false);});it('rejects: path traversal in a file path', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {files: [{ path: 'index.html', content: 'x' }, { path: '../escape.js', content: 'x' }],- });+ }));expect(r.ok).toBe(false);});it('rejects: absolute file path', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {files: [{ path: 'index.html', content: 'x' }, { path: '/etc/passwd', content: 'x' }],- });+ }));// leading slash is stripped → "etc/passwd" with no extension → unsupported extexpect(r.ok).toBe(false);});it('rejects: reserved __orkas-meta.json / __orkas/ paths', async () => {const m = await loadMod();- expect((m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: 'x' }, { path: '__orkas-meta.json', content: '{}' }] }) as { ok: boolean }).ok).toBe(false);- expect((m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: 'x' }, { path: '__orkas/bridge.js', content: 'x' }] }) as { ok: boolean }).ok).toBe(false);+ expect(((await m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: 'x' }, { path: '__orkas-meta.json', content: '{}' }] })) as { ok: boolean }).ok).toBe(false);+ expect(((await m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: 'x' }, { path: '__orkas/bridge.js', content: 'x' }] })) as { ok: boolean }).ok).toBe(false);});it('rejects: disallowed extension', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {files: [{ path: 'index.html', content: 'x' }, { path: 'evil.exe', content: 'AA==', encoding: 'base64' }],- });- expect(r.ok).toBe(false);- });-- it('rejects: too many files', async () => {- const m = await loadMod();- const files = [{ path: 'index.html', content: 'x' }];- for (let i = 0; i < 25; i++) files.push({ path: `f${i}.js`, content: 'x' });- const r = m.createArtifact(UID, CID, AGENT, { files });+ }));expect(r.ok).toBe(false);- if (r.ok) return;- expect(r.error).toMatch(/too many files/);});- it('rejects: a single file over the per-file cap', async () => {+ it('publishes a large multi-file app completely without blocking host progress', async () => {const m = await loadMod();- const big = 'a'.repeat(300 * 1024);- const r = m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: '<x>' }, { path: 'big.js', content: big }] });- expect(r.ok).toBe(false);- if (r.ok) return;- expect(r.error).toMatch(/per-file cap/);+ const files = [{ path: 'index.html', content: '<h1>Large app</h1>' },+ ...Array.from({ length: 25 }, (_, i) => ({ path: `assets/f${i}.js`, content: String(i) + 'x'.repeat(300 * 1024) }))];+ let ticks = 0;+ const timer = setInterval(() => { ticks++; }, 0);+ const r = await m.createArtifact(UID, CID, AGENT, { files });+ clearInterval(timer);+ expect(r.ok).toBe(true);+ if (!r.ok) throw new Error(r.error);+ expect(ticks).toBeGreaterThan(0);+ for (const file of files.slice(1)) {+ const served = m.resolveArtifactFilePath(UID, CID, r.artifactId, file.path);+ expect(served.ok).toBe(true);+ expect(fs.readFileSync(path.join(cidDir(), r.artifactId, file.path), 'utf8')).toBe(file.content);+ }+ expect(fs.readdirSync(cidDir())).toEqual([r.artifactId]);});- it('rejects: bundle over the total cap', async () => {+ it('cleans staged files on an incomplete write and allows a fresh creation', async () => {const m = await loadMod();- const chunk = 'a'.repeat(200 * 1024);- const r = m.createArtifact(UID, CID, AGENT, {- files: [- { path: 'index.html', content: '<x>' },- { path: 'a.js', content: chunk },- { path: 'b.js', content: chunk },- { path: 'c.js', content: chunk },- { path: 'd.js', content: chunk },- { path: 'e.js', content: chunk },- { path: 'f.js', content: chunk },- ],- });- expect(r.ok).toBe(false);- if (r.ok) return;- expect(r.error).toMatch(/total cap/);+ // A file/directory collision happens after index.html has been staged.+ const failed = await m.createArtifact(UID, CID, AGENT, { files: [+ ...MIN_FILES, { path: 'asset.js', content: 'file' }, { path: 'asset.js/child.js', content: 'cannot write' },+ ] });+ expect(failed.ok).toBe(false);+ expect(fs.readdirSync(cidDir())).toEqual([]);+ const retry = await m.createArtifact(UID, CID, AGENT, { files: MIN_FILES });+ expect(retry.ok).toBe(true);+ if (!retry.ok) throw new Error(retry.error);+ expect(fs.readdirSync(cidDir())).toEqual([retry.artifactId]);});it('rejects: utf8-encoded content for a binary extension', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: '<x>' }, { path: 'logo.png', content: 'not base64 binary' }] });+ const r = (await m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: '<x>' }, { path: 'logo.png', content: 'not base64 binary' }] }));expect(r.ok).toBe(false);if (r.ok) return;expect(r.error).toMatch(/base64/);@@ -290,7 +289,7 @@ describe('chat_artifacts › createArtifact', () => {it('rejects: duplicate file paths (case-insensitive)', async () => {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: 'x' }, { path: 'Index.HTML', content: 'y' }] });+ const r = (await m.createArtifact(UID, CID, AGENT, { files: [{ path: 'index.html', content: 'x' }, { path: 'Index.HTML', content: 'y' }] }));expect(r.ok).toBe(false);});});@@ -298,12 +297,12 @@ describe('chat_artifacts › createArtifact', () => {describe('chat_artifacts › resolveArtifactFilePath', () => {async function seed() {const m = await loadMod();- const r = m.createArtifact(UID, CID, AGENT, {+ const r = (await m.createArtifact(UID, CID, AGENT, {files: [{ path: 'index.html', content: '<!doctype html><h1>hi</h1>' },{ path: 'assets/app.js', content: 'console.log(2)' },],- });+ }));if (!r.ok) throw new Error('seed failed: ' + r.error);return { m, artifactId: r.artifactId };}@@ -395,8 +394,8 @@ describe('chat_artifacts › resolveArtifactFilePath', () => {describe('chat_artifacts › purgeByCid', () => {it('removes the whole chat_artifacts/<cid>/ tree', async () => {const m = await loadMod();- expect(m.createArtifact(UID, CID, AGENT, { files: MIN_FILES }).ok).toBe(true);- expect(m.createArtifact(UID, CID, AGENT, { files: MIN_FILES }).ok).toBe(true);+ expect((await m.createArtifact(UID, CID, AGENT, { files: MIN_FILES })).ok).toBe(true);+ expect((await m.createArtifact(UID, CID, AGENT, { files: MIN_FILES })).ok).toBe(true);expect(fs.existsSync(cidDir())).toBe(true);const n = await m.purgeByCid(UID, CID);expect(n).toBe(2);@@ -405,3 +404,15 @@ describe('chat_artifacts › purgeByCid', () => {expect(await m.purgeByCid(UID, CID)).toBe(0);});});++it('creates and serves the same media/model/font resources accepted by saved apps', async () => {+ const mod=await loadMod();+ const resources=[['sound.mp3','audio/mpeg'],['movie.mp4','video/mp4'],['scene.glb','model/gltf-binary'],['font.otf','font/otf'],['image.avif','image/avif']];+ const created=await mod.createArtifact(UID,CID,AGENT,{files:[...MIN_FILES,...resources.map(([name])=>({path:name,content:'AAE=',encoding:'base64'}))]});+ expect(created.ok).toBe(true);if(!created.ok)throw new Error(created.error);+ for(const [name,mime] of resources){+ const result=mod.resolveArtifactFilePath(UID,CID,created.artifactId,name);+ expect(result).toMatchObject({ok:true,mime});+ if(result.ok)expect(fs.readFileSync(result.absPath)).toEqual(Buffer.from([0,1]));+ }+});diff --git a/test/main/features/chat_attachments.test.ts b/test/main/features/chat_attachments.test.tsindex cea73970..c52098d3 100644--- a/test/main/features/chat_attachments.test.ts+++ b/test/main/features/chat_attachments.test.ts@@ -77,6 +77,28 @@ describe('chat_attachments › legacy spreadsheet input', () => {});});+describe('chat_attachments › common text inputs', () => {+ it.each(['csv', 'tsv', 'html', 'xml', 'jsonl', 'ndjson', 'toml', 'py', 'js', 'css', 'rst', 'tex', 'srt', 'vtt'])(+ 'imports and reads %s as literal UTF-8 without modifying the source', async (ext) => {+ const m = await loadMod();+ const name = `input.${ext}`;+ const text = '<b>中文 & literal</b>\n001,"a,b",0\n';+ const source = path.join(tmpDir, name);+ fs.writeFileSync(source, text);+ expect(await m.importAttachmentFromPath(UID, CID, source)).toMatchObject({ ok: true, info: { kind: 'text' } });+ const indexer = await import('../../../src/main/features/file_indexer');+ expect((await indexer.readRange(UID, path.join(attDir(), name))).content).toBe(text);+ expect(fs.readFileSync(source, 'utf8')).toBe(text);+ expect(m.resolveAttachmentAbsPath(UID, CID, name).ok).toBe(true);+ },+ );+ it('rejects a binary file disguised as a newly supported text format without storing it', async () => {+ const m = await loadMod();+ expect(await m.uploadAttachment(UID, CID, 'input.jsonl', Buffer.from([0xff, 0xfe, 0]))).toMatchObject({ ok: false });+ expect(m.listAttachments(UID, CID)).toEqual([]);+ });+});+describe('chat_attachments › diagnostic privacy', () => {async function captureDiagnostics(): Promise<unknown[][]> {const logger = await import('../../../src/main/logger');@@ -939,7 +961,7 @@ describe('chat_attachments › resolveAttachmentAbsPath', () => {it('refuses non-whitelisted extensions', async () => {const m = await loadMod();- const r = m.resolveAttachmentAbsPath(UID, CID, 'script.sh');+ const r = m.resolveAttachmentAbsPath(UID, CID, 'program.exe');expect(r.ok).toBe(false);if (r.ok) return;expect(r.code).toBe('bad_input');@@ -1036,6 +1058,24 @@ describe('chat_attachments › resolveLocalMediaPath', () => {fs.rmSync(sandbox, { recursive: true, force: true });});+ it.each([+ ['apng', 'image', 'image/apng'], ['jpe', 'image', 'image/jpeg'],+ ['jfif', 'image', 'image/jpeg'], ['avif', 'image', 'image/avif'],+ ['bmp', 'image', 'image/bmp'], ['ico', 'image', 'image/x-icon'],+ ['oga', 'audio', 'audio/ogg'], ['weba', 'audio', 'audio/webm'],+ ])('serves local .%s previews with the correct kind, MIME and size limit', async (ext, kind, mime) => {+ const { mod, sandbox } = await setup();+ try {+ const p = path.join(sandbox, `preview.${ext}`);+ fs.writeFileSync(p, 'fixture');+ expect(mod.resolveLocalMediaPath(p)).toMatchObject({ ok: true, kind });+ expect(mod.localMediaMimeFor(p)).toBe(mime);+ // Sparse files exercise admission without allocating large payloads.+ fs.truncateSync(p, (kind === 'image' ? 20 : 50) * 1024 * 1024 + 1);+ expect(mod.resolveLocalMediaPath(p).ok).toBe(false);+ } finally { fs.rmSync(sandbox, { recursive: true, force: true }); }+ });+it('inlines relative raster references when serving a legacy local SVG', async () => {const { mod, sandbox } = await setup();const frame = path.join(sandbox, '01-frame.png');@@ -1441,6 +1481,47 @@ describe('chat_attachments › buildAttachmentManifest', () => {expect(r.metadata).toEqual({ hasAttachments: true, attachmentTypes: ['image'] });});+ it.each([+ { label: 'small opaque', width: 240, height: 120, transparent: false },+ { label: 'large transparent', width: 1600, height: 800, transparent: true },+ ])('preserves reference colors on first delivery and reread for $label images', async ({ width, height, transparent }) => {+ const { Jimp } = await import('jimp' as any);+ const source: any = new Jimp({ width, height, color: transparent ? 0x00000000 : 0xFFFFFFFF });+ // The source palette, not a second production transform, is the oracle.+ const palette = [0xDC2020FF, 0x20B040FF, 0x2040DCFF];+ palette.forEach((color, index) => {+ const patch: any = new Jimp({ width: width / 5, height: height / 2, color });+ source.composite(patch, Math.floor(width * (index + 0.5) / 4), height / 4);+ });+ const png: Buffer = await source.getBuffer('image/png');+ const m = await loadMod();+ expect(await m.uploadAttachment(UID, CID, 'reference.png', png)).toMatchObject({ ok: true });+ const delivered = await m.buildAttachmentManifest(UID, CID, ['reference.png']);+ expect(delivered.skipped).toEqual([]);+ expect(delivered.images).toHaveLength(1);+ const indexer = await import('../../../src/main/features/file_indexer');+ const reread = await indexer.readImageAsJpeg(UID, path.join(attDir(), 'reference.png'));+ for (const data of [delivered.images[0].data, reread.base64]) {+ const decoded: any = await Jimp.read(Buffer.from(data, 'base64'));+ expect(decoded.bitmap.width).toBe(Math.min(width, 1024));+ expect(decoded.bitmap.height).toBe(Math.min(height, 512));+ for (const [index, color] of palette.entries()) {+ const actual = decoded.getPixelColor(+ Math.floor(decoded.bitmap.width * ((index + 0.5) / 4 + 0.1)),+ decoded.bitmap.height / 2,+ );+ for (const shift of [24, 16, 8]) {+ expect(Math.abs(((actual >>> shift) & 0xFF) - ((color >>> shift) & 0xFF))).toBeLessThan(15);+ }+ }+ // Transparent backgrounds must still flatten onto white for JPEG.+ const background = decoded.getPixelColor(5, 5);+ for (const shift of [24, 16, 8]) expect((background >>> shift) & 0xFF).toBeGreaterThan(240);+ }+ expect(fs.readFileSync(path.join(attDir(), 'reference.png'))).toEqual(png);+ expect(fs.readdirSync(attDir())).toEqual(['reference.png']);+ });+it('skips missing files gracefully', async () => {const m = await loadMod();const r = await m.buildAttachmentManifest(UID, CID, ['ghost.txt']);diff --git a/test/main/features/connector-setup-context.test.ts b/test/main/features/connector-setup-context.test.tsindex 14de2902..2b30293e 100644--- a/test/main/features/connector-setup-context.test.ts+++ b/test/main/features/connector-setup-context.test.ts@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { drainMainRuntimeForTest } from '../../helpers/drain-main-runtime';vi.mock('../../../src/main/logger', () => ({createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),@@ -15,7 +16,8 @@ beforeEach(() => {process.env.ORKAS_WORKSPACE_ROOT = root;vi.resetModules();});-afterEach(() => {+afterEach(async () => {+ await drainMainRuntimeForTest();if (previousRoot === undefined) delete process.env.ORKAS_WORKSPACE_ROOT;else process.env.ORKAS_WORKSPACE_ROOT = previousRoot;fs.rmSync(root, { recursive: true, force: true });diff --git a/test/main/features/connectors/manager.test.ts b/test/main/features/connectors/manager.test.tsindex 2970b6d0..67b34952 100644--- a/test/main/features/connectors/manager.test.ts+++ b/test/main/features/connectors/manager.test.ts@@ -2,11 +2,13 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { captureMainLogWorkers } from '../../../helpers/capture-main-log-workers';const TEST_UID = 'u-connectors-manager';let tmpDir: string;let prevWs: string | undefined;+let closeLogWorkers: () => Promise<void>;// ── Mock binding: ONE hoisted controller, mocked ONCE at top level ──────────// Previously each test re-mocked mcp-client / oauth / oauth-dcr with per-test@@ -436,12 +438,14 @@ beforeEach(async () => {prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();vi.clearAllMocks();resetMockBehaviors();await writeGoogleConnectorsConfig({ google: 'enabled', gmail: 'enabled' });});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();if (prevWs === undefined) delete process.env.ORKAS_WORKSPACE_ROOT;else process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });@@ -457,9 +461,9 @@ describe('features/connectors/manager authorization recovery', () => {const { default: electronLog } = await import('electron-log/main');const records: Array<{ level: string; data: unknown[] }> = [];const capture = (message: any, transport: unknown) => {- // electron-log runs hooks separately for file/console/IPC. Observe one- // actual output transport, not three copies of the same log invocation.- if (transport === electronLog.transports.console && message.data[0] === 'connector request completed') {+ // Direct sinks are disabled: observe the single background-worker+ // transport instead of the obsolete console transport.+ if (transport === (electronLog.transports as any).background && message.data[0] === 'connector request completed') {records.push({ level: message.level, data: message.data });}return message;diff --git a/test/main/features/contexts.test.ts b/test/main/features/contexts.test.tsindex 47cccf02..5d22177c 100644--- a/test/main/features/contexts.test.ts+++ b/test/main/features/contexts.test.ts@@ -142,7 +142,7 @@ describe('contexts › writeContextFile', () => {it('accepts every text extension in the whitelist', async () => {const c = await loadContexts();- const exts = ['.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log'];+ const exts = ['.md', '.markdown', '.txt', '.csv', '.tsv', '.json', '.yaml', '.yml', '.log', '.html', '.xml', '.jsonl', '.ndjson', '.rst', '.tex', '.srt', '.vtt'];for (const ext of exts) {const r = c.writeContextFile(`f${ext}`, 'content');expect(r.ok, ext).toBe(true);@@ -554,6 +554,17 @@ describe('contexts › createContextDir', () => {});describe('contexts › readContextFile', () => {+ it('offers a bounded CSV preview while preserving the full stored source', async () => {+ const c = await loadContexts();+ const body = 'id,value\n' + '001,中文\n'.repeat(300_000);+ writeFile('large.csv', body);+ const result = c.readContextFile('large.csv', true);+ expect(result).toMatchObject({ ok: true, truncated: true });+ expect((result as any).content.startsWith('id,value\n001,中文')).toBe(true);+ expect(Buffer.byteLength((result as any).content)).toBeLessThanOrEqual(2 * 1024 * 1024);+ expect(fs.readFileSync(path.join(ctxRoot(), 'large.csv'), 'utf8')).toBe(body);+ expect(c.readContextFile('../outside.csv', true).ok).toBe(false);+ });it('returns content for an existing text file', async () => {writeFile('a.md', '# hello');const c = await loadContexts();diff --git a/test/main/features/conversation-assistance-context.test.ts b/test/main/features/conversation-assistance-context.test.tsindex fbdb92f5..85f9adaf 100644--- a/test/main/features/conversation-assistance-context.test.ts+++ b/test/main/features/conversation-assistance-context.test.ts@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { drainMainRuntimeForTest } from '../../helpers/drain-main-runtime';vi.mock('../../../src/main/logger', () => ({createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),@@ -15,7 +16,8 @@ beforeEach(() => {process.env.ORKAS_WORKSPACE_ROOT = root;vi.resetModules();});-afterEach(() => {+afterEach(async () => {+ await drainMainRuntimeForTest();if (previousRoot === undefined) delete process.env.ORKAS_WORKSPACE_ROOT;else process.env.ORKAS_WORKSPACE_ROOT = previousRoot;fs.rmSync(root, { recursive: true, force: true });diff --git a/test/main/features/generation_reference_assets.test.ts b/test/main/features/generation_reference_assets.test.tsindex b84ee82d..d33ee2e6 100644--- a/test/main/features/generation_reference_assets.test.ts+++ b/test/main/features/generation_reference_assets.test.ts@@ -42,6 +42,58 @@ afterEach(async () => {});describe('generation reference assets', () => {+ it.each(['empty', 'local', 'remote', 'mixed'])('preserves reference identity and progress for %s image inputs', async (mode) => {+ const localBytes = [Buffer.from('local identity'), Buffer.from('local composition')];+ const remoteBytes = [Buffer.from('remote style'), Buffer.from('remote content')];+ const paths = localBytes.map((_, i) => path.join(tmpDir, `reference-${i}.png`));+ await Promise.all(paths.map((file, i) => fs.writeFile(file, localBytes[i])));+ const urls = ['https://cdn.example/style.png', 'https://cdn.example/content.png'];+ const fetchMock = vi.fn(async (url: string) => {+ const index = urls.indexOf(String(url));+ if (index < 0) throw new Error('unexpected reference download');+ return new Response(remoteBytes[index], { status: 200 });+ });+ vi.stubGlobal('fetch', fetchMock);+ const referenceAssets = await import('../../../src/main/features/generation_reference_assets');+ const hasLocal = mode === 'local' || mode === 'mixed';+ const hasRemote = mode === 'remote' || mode === 'mixed';+ const progress: Array<{ phase: string; data?: Record<string, unknown> }> = [];+ const buffers = await referenceAssets.loadImageReferenceBuffersWithProgress(+ hasRemote ? urls : [], hasLocal ? paths : [], { onProgress: event => progress.push(event) },+ );++ const expected = [...(hasLocal ? localBytes : []), ...(hasRemote ? remoteBytes : [])];+ expect(buffers).toEqual(expected);+ expect(fetchMock).toHaveBeenCalledTimes(hasRemote ? 2 : 0);+ expect(progress.map(event => event.data)).toEqual(expected.map((_, i) => ({+ kind: 'image', index: i + 1, total: expected.length,+ })));+ expect(progress.map(event => event.phase)).toEqual([+ ...(hasLocal ? ['reference_load', 'reference_load'] : []),+ ...(hasRemote ? ['reference_download', 'reference_download'] : []),+ ]);+ });++ it.each(['cancel', 'missing', 'download'])('rejects %s during mixed reference preparation without returning a partial list', async (failure) => {+ const localPath = path.join(tmpDir, 'reference.png');+ if (failure !== 'missing') await fs.writeFile(localPath, Buffer.from('local identity'));+ const fetchMock = vi.fn(async () => new Response('reference unavailable', { status: 403 }));+ vi.stubGlobal('fetch', fetchMock);+ const referenceAssets = await import('../../../src/main/features/generation_reference_assets');+ const controller = new AbortController();+ const loading = referenceAssets.loadImageReferenceBuffersWithProgress(+ ['https://cdn.example/style.png'], [localPath], {+ signal: controller.signal,+ onProgress: event => {+ if (failure === 'cancel' && event.phase === 'reference_load') controller.abort();+ },+ },+ );+ if (failure === 'missing') await expect(loading).rejects.toMatchObject({ code: 'ENOENT' });+ else await expect(loading).rejects.toThrow(failure === 'cancel' ? /aborted/i : /403/);+ expect(fetchMock).toHaveBeenCalledTimes(failure === 'download' ? 1 : 0);+ });+it('reuses a generated public URL for the unchanged local output', async () => {const localPath = await writeTinyMp4();const fetchMock = vi.fn();diff --git a/test/main/features/group_chat/agents-index.test.ts b/test/main/features/group_chat/agents-index.test.tsindex 111defa0..be3c93fd 100644--- a/test/main/features/group_chat/agents-index.test.ts+++ b/test/main/features/group_chat/agents-index.test.ts@@ -28,6 +28,17 @@ import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+// Module resets create a workspace per case. Console diagnostics keep this+// roster suite observable without file-log workers racing workspace deletion.+vi.mock('../../../../src/main/logger', () => ({+ createLogger: (scope: string) => ({+ debug: (...args: unknown[]) => console.debug(`[${scope}]`, ...args),+ info: (...args: unknown[]) => console.info(`[${scope}]`, ...args),+ warn: (...args: unknown[]) => console.warn(`[${scope}]`, ...args),+ error: (...args: unknown[]) => console.error(`[${scope}]`, ...args),+ }),+}));+let tmpDir: string;let prevWs: string | undefined;const TEST_UID = 'u-agents-idx';@@ -79,7 +90,13 @@ describe('agents_index block — header + per-entry shape', () => {const file = path.join(root, id, 'agent.json');return fs.existsSync(file) ? [JSON.parse(fs.readFileSync(file, 'utf8'))] : [];});- expect(agents).toHaveLength(10);+ const manifest = JSON.parse(fs.readFileSync(path.join(root, '../../_manifest.json'), 'utf8'));+ const shippedIds = manifest.files.flatMap((entry: { path: string }) => {+ const match = /^marketplace\/agents\/([^/]+)\/agent\.json$/.exec(entry.path);+ return match ? [match[1]] : [];+ });+ expect(shippedIds.length).toBeGreaterThan(0);+ expect(agents.map(agent => agent.agent_id).sort()).toEqual(shippedIds.sort());for (const agent of agents) {writeAgent(builtinAgentsDir(), agent.agent_id, agent, { seed_source: 'builtin' });}diff --git a/test/main/features/group_chat/bus-abort-hygiene.test.ts b/test/main/features/group_chat/bus-abort-hygiene.test.tsindex 5878e366..f1a42876 100644--- a/test/main/features/group_chat/bus-abort-hygiene.test.ts+++ b/test/main/features/group_chat/bus-abort-hygiene.test.ts@@ -2,6 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { drainMainRuntimeForTest } from '../../../helpers/drain-main-runtime';/*** Post-abort work hygiene: a user Stop is the single stop path — no salvaged@@ -109,6 +110,7 @@ afterEach(async () => {}} catch { /* ignore */ }await new Promise((r) => setTimeout(r, 30));+ await drainMainRuntimeForTest();process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });});diff --git a/test/main/features/group_chat/bus-integration.test.ts b/test/main/features/group_chat/bus-integration.test.tsindex c26c64f5..a143ec87 100644--- a/test/main/features/group_chat/bus-integration.test.ts+++ b/test/main/features/group_chat/bus-integration.test.ts@@ -6,6 +6,10 @@ import AdmZip from 'adm-zip';import { drainMainRuntimeForTest } from '../../../helpers/drain-main-runtime';const retryInfoLog = vi.hoisted(() => vi.fn());+const correctionModel = vi.hoisted(() => vi.fn(async (_input: any): Promise<string> => ''));+vi.mock('../../../../src/main/model/core-agent/runner', async (importOriginal) => ({+ ...(await importOriginal<any>()), completeSkillCreationCorrection: correctionModel,+}));vi.mock('../../../../src/main/logger', () => ({createLogger: () => ({ debug: vi.fn(), info: retryInfoLog, warn: vi.fn(), error: vi.fn() }),}));@@ -212,6 +216,7 @@ function newCid(): string {}beforeEach(async () => {+ correctionModel.mockReset().mockResolvedValue('');retryInfoLog.mockClear();tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-int-'));prevWs = process.env.ORKAS_WORKSPACE_ROOT;@@ -267,6 +272,11 @@ afterEach(async () => {bashPermissions._resetForTest();} catch { /* ignore */ }await drainMainRuntimeForTest();+ // A Stop can land between a failed channel attempt and its queued retry.+ // Let that cancellation microtask settle, then close any runtime store it+ // touched after the first drain before Windows removes the temp workspace.+ await new Promise((resolve) => setTimeout(resolve, 50));+ await drainMainRuntimeForTest();process.env.ORKAS_WORKSPACE_ROOT = prevWs;if (prevTestGlobalSkillsRoot === undefined) delete process.env.ORKAS_TEST_GLOBAL_SKILLS_ROOT;else process.env.ORKAS_TEST_GLOBAL_SKILLS_ROOT = prevTestGlobalSkillsRoot;@@ -4754,6 +4764,9 @@ describe('group_chat bus integration › G8d in-process dispatch (run_worker / dexpect(message?.form.fields).toHaveLength(3);expect(message.form.fields.every((f: any) => !Object.hasOwn(f, 'default'))).toBe(true);expect(message.text || '').not.toContain('<agent-input-form>');+ await vi.waitFor(async () => {+ expect((await state.readState(TEST_UID, cid)).status).toBe('idle');+ });const stateBefore = await state.readState(TEST_UID, cid);const callsBefore = _recordedCalls.length;const submit = (values: Record<string, unknown>) => groupChat.markFormSubmittedAndDispatch({@@ -8386,3 +8399,145 @@ describe('group_chat bus integration › D9 same-agent multi-segment (on-deviceexpect(calls[1]).toContain('EPSILON_SECOND');}, 15_000);});+describe('group_chat bus integration › Skill creation correction', () => {+ const rejected = '<skill>\n<<<skill-file path=notes.txt\nKeep source notes.\n>>>\n</skill>';+ const corrected = '<skill>\n<<<skill-file path=SKILL.md\n---\nname: recovered-notes\ndescription: Summarize supplied notes\n---\n\n# Instructions\nRead and summarize supplied notes.\n>>>\n<<<skill-file path=notes.txt\nKeep source notes.\n>>>\n</skill>';++ it('corrects only the rejected proposal without replaying the Commander or its tools', async () => {+ const cid = newCid();+ const state = await import('../../../../src/main/features/group_chat/state');+ const bus = await import('../../../../src/main/features/group_chat/bus');+ const sid = state.buildGconvSessionId(cid);+ _setScript(sid, [{ type: 'final', text: rejected }]);+ correctionModel.mockResolvedValue(corrected);+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: 'Create a notes Skill' });+ // The first correction in a Windows Vitest worker cold-loads the dynamic+ // runner/quality modules; preserve the quiescence assertion without using+ // the 2s steady-state polling budget as a module-transform benchmark.+ await waitForQuiescent(TEST_UID, cid, 8000);+ expect(_recordedCalls.filter(call => call.sid === sid)).toHaveLength(1);+ expect(correctionModel).toHaveBeenCalledTimes(1);+ expect(correctionModel.mock.calls[0][0]).toMatchObject({ userId: TEST_UID, cid, message: expect.stringContaining('missing_skill_md') });+ const messages = await (await import('../../../../src/main/features/group_chat')).readMessages(TEST_UID, cid);+ expect(messages.flatMap(message => message.created_skills || []))+ .toEqual([expect.objectContaining({ skill_id: 'recovered-notes', name: 'recovered-notes' })]);+ expect(messages.at(-1)?.text).toContain('corrected and created');+ const paths = await import('../../../../src/main/paths');+ expect(fs.readFileSync(path.join(paths.userSkillsDir(TEST_UID), 'recovered-notes', 'notes.txt'), 'utf8')).toBe('Keep source notes.');+ });++ it('stops after one failed correction and replaces the premature success claim', async () => {+ const cid = newCid();+ const state = await import('../../../../src/main/features/group_chat/state');+ const bus = await import('../../../../src/main/features/group_chat/bus');+ const sid = state.buildGconvSessionId(cid);+ _setScript(sid, [{ type: 'final', text: 'Already created and ready!\n' + rejected }]);+ correctionModel.mockResolvedValue(rejected);+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: 'Create a notes Skill' });+ await waitForQuiescent(TEST_UID, cid);+ expect(_recordedCalls.filter(call => call.sid === sid)).toHaveLength(1);+ expect(correctionModel).toHaveBeenCalledTimes(1);+ const messages = await (await import('../../../../src/main/features/group_chat')).readMessages(TEST_UID, cid);+ expect(messages.flatMap(message => message.created_skills || [])).toEqual([]);+ expect(messages.at(-1)?.text).toContain('could not be created');+ expect(messages.at(-1)?.text).not.toContain('Already created');+ expect(messages.at(-1)?.failure_kind).toBe('validation');+ });++ it('preserves the successful sibling of a mixed batch without launching correction', async () => {+ const cid = newCid();+ const state = await import('../../../../src/main/features/group_chat/state');+ const bus = await import('../../../../src/main/features/group_chat/bus');+ _setScript(state.buildGconvSessionId(cid), [{ type: 'final', text: corrected + rejected }]);+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: 'Create two Skills' });+ await waitForQuiescent(TEST_UID, cid);+ expect(correctionModel).not.toHaveBeenCalled();+ const messages = await (await import('../../../../src/main/features/group_chat')).readMessages(TEST_UID, cid);+ expect(messages.flatMap(message => message.created_skills || []))+ .toEqual([expect.objectContaining({ skill_id: 'recovered-notes' })]);+ });++ it('times out one correction and ignores a late result without replay', async () => {+ const cid = newCid();+ const state = await import('../../../../src/main/features/group_chat/state');+ const bus = await import('../../../../src/main/features/group_chat/bus');+ _setScript(state.buildGconvSessionId(cid), [{ type: 'final', text: rejected }]);+ let finish!: (value: string) => void;+ correctionModel.mockImplementationOnce(() => new Promise<string>(resolve => { finish = resolve; }));+ // Accelerate only the declared correction wall bound; scheduler timers keep+ // real time. The callback still goes through the production abort path.+ const nativeTimeout = globalThis.setTimeout;+ const timer = vi.spyOn(globalThis, 'setTimeout').mockImplementation(((fn: any, ms: any, ...args: any[]) =>+ nativeTimeout(fn, ms === 60_000 ? 10 : ms, ...args)) as typeof setTimeout);+ try {+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: 'Create a notes Skill' });+ await waitForQuiescent(TEST_UID, cid);+ expect(correctionModel).toHaveBeenCalledTimes(1);+ expect(correctionModel.mock.calls[0][0].signal.aborted).toBe(true);+ finish(corrected);+ const messages = await (await import('../../../../src/main/features/group_chat')).readMessages(TEST_UID, cid);+ expect(messages.flatMap(message => message.created_skills || [])).toEqual([]);+ expect(messages.at(-1)?.text).toContain('could not be created');+ expect(messages.at(-1)?.failure_kind).toBe('validation');+ } finally { timer.mockRestore(); }+ });++ it('does not write to either account when the account changes during correction', async () => {+ const cid = newCid();+ const state = await import('../../../../src/main/features/group_chat/state');+ const bus = await import('../../../../src/main/features/group_chat/bus');+ _setScript(state.buildGconvSessionId(cid), [{ type: 'final', text: rejected }]);+ correctionModel.mockImplementationOnce(async () => {+ (await import('../../../../src/main/features/users')).activateUser('other-owner');+ return corrected;+ });+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: 'Create a notes Skill' });+ await waitForQuiescent(TEST_UID, cid);+ const paths = await import('../../../../src/main/paths');+ for (const owner of [TEST_UID, 'other-owner']) {+ expect(fs.existsSync(path.join(paths.userSkillsDir(owner), 'recovered-notes'))).toBe(false);+ }+ expect(correctionModel).toHaveBeenCalledTimes(1);+ });++ it('does not chain Skill correction onto an Agent correction turn', async () => {+ const cid = newCid();+ const state = await import('../../../../src/main/features/group_chat/state');+ const bus = await import('../../../../src/main/features/group_chat/bus');+ const sid = state.buildGconvSessionId(cid);+ _setScript(sid, [{ type: 'final', text: '<agent><operation>create</operation><name>Invalid Name!</name><workflow>Coordinate notes</workflow></agent>' }]);+ _setScript(sid, [{ type: 'final', text: rejected }]);+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: 'Create a notes Agent' });+ await waitForQuiescent(TEST_UID, cid);+ expect(_recordedCalls.filter(call => call.sid === sid)).toHaveLength(2);+ expect(correctionModel).not.toHaveBeenCalled();+ const paths = await import('../../../../src/main/paths');+ expect(fs.existsSync(path.join(paths.userSkillsDir(TEST_UID), 'recovered-notes'))).toBe(false);+ });++ it('honors Stop during correction and discards even a late valid model result', async () => {+ const cid = newCid();+ const state = await import('../../../../src/main/features/group_chat/state');+ const bus = await import('../../../../src/main/features/group_chat/bus');+ _setScript(state.buildGconvSessionId(cid), [{ type: 'final', text: rejected }]);+ let admitted!: () => void;+ const started = new Promise<void>(resolve => { admitted = resolve; });+ let finish!: (value: string) => void;+ correctionModel.mockImplementationOnce(async () => {+ admitted();+ return await new Promise<string>(resolve => { finish = resolve; });+ });+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: 'Create a notes Skill' });+ await started;+ await bus.abort(TEST_UID, cid);+ finish(corrected);+ await waitForQuiescent(TEST_UID, cid);+ expect(correctionModel).toHaveBeenCalledTimes(1);+ expect(correctionModel.mock.calls[0][0].signal.aborted).toBe(true);+ const messages = await (await import('../../../../src/main/features/group_chat')).readMessages(TEST_UID, cid);+ expect(messages.flatMap(message => message.created_skills || [])).toEqual([]);+ const paths = await import('../../../../src/main/paths');+ expect(fs.existsSync(path.join(paths.userSkillsDir(TEST_UID), 'recovered-notes'))).toBe(false);+ expect((await state.readState(TEST_UID, cid)).status).toBe('aborted');+ });+});diff --git a/test/main/features/group_chat/bus.test.ts b/test/main/features/group_chat/bus.test.tsindex c84c210e..3521db4b 100644--- a/test/main/features/group_chat/bus.test.ts+++ b/test/main/features/group_chat/bus.test.ts@@ -321,6 +321,23 @@ vi.mock('../../../../src/main/model/client', () => ({yield { type: 'done' };return;}+ if (message.includes('NON_CLI_DRAFT_TEST')) {+ yield { type: 'delta', text: 'Inspect sources.', phase: 'pending' };+ yield { type: 'commentary-finalized', text: 'Inspect sources.' };+ yield { type: 'event', event: { stream: 'tool', data: { phase: 'end', id: 'read-draft', name: 'read_files' } } };+ yield { type: 'delta', text: 'Verify results.', phase: 'pending' };+ yield { type: 'commentary-finalized', text: 'Verify results.' };+ if (message.includes('NON_CLI_DRAFT_TEST_ABORT')) {+ yield { type: 'error', text: 'Stopped', aborted: true };+ } else if (message.includes('NON_CLI_DRAFT_TEST_FAIL')) {+ yield { type: 'error', text: 'Connection dropped', failureKind: 'model', failureCode: 'provider_network' };+ } else {+ yield { type: 'delta', text: 'Delivered.', phase: 'pending' };+ yield { type: 'final', text: 'Delivered.' };+ }+ yield { type: 'done' };+ return;+ }if (message.includes('NON_CLI_PHASED_TEXT_TEST')) {yield { type: 'delta', text: 'Inspect the code.', phase: 'commentary' };yield {@@ -667,6 +684,38 @@ describe('group_chat bus › enqueue routing + persistence', () => {))).toBe(true);});+ it.each(['success', 'failure', 'abort'])('preserves visible draft and commentary without duplication or silent replay: %s', async ending => {+ const cid = `cid-native-draft-${ending}`;+ const bus = await import('../../../../src/main/features/group_chat/bus');+ const paths = await import('../../../../src/main/paths');+ const snapshots: any[] = [];+ const events: any[] = [];+ bus.subscribe(TEST_UID, cid, event => {+ events.push(event);+ if (event.type === 'process') snapshots.push(structuredClone(bus.liveDisplaySnapshot(TEST_UID, cid)));+ });+ const marker = `NON_CLI_DRAFT_TEST${ending === 'failure' ? '_FAIL' : ending === 'abort' ? '_ABORT' : ''}`;+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: marker });+ await waitForQuiescent(TEST_UID, cid);+ expect(snapshots[0].turns[0].records.at(-1).text).toBe('Inspect sources.');+ expect(snapshots[1].turns[0].records.at(-1).text).toBe('');+ expect(snapshots[1].turns[0].records.at(-1).process)+ .toContainEqual(expect.objectContaining({ text: 'Inspect sources.' }));+ expect(streamProbe.messages.filter(text => text.includes(marker))).toHaveLength(1);+ const rows = fs.readFileSync(path.join(paths.userChatsDir(TEST_UID), `${cid}.jsonl`), 'utf8')+ .trim().split('\n').map(line => JSON.parse(line));+ const reply = rows.find(row => row.from === 'commander');+ expect(reply.process.filter((p: any) => p.event?.stream === 'assistant').map((p: any) => p.text))+ .toEqual(['Inspect sources.', 'Verify results.']);+ if (ending === 'success') expect(reply.text).toBe('Delivered.');+ if (ending === 'abort') {+ expect(reply.text).not.toContain('Inspect sources.');+ expect(reply.text).not.toContain('Verify results.');+ }+ expect(events.filter(e => e.type === 'process' && e.data?.type === 'delta').map(e => e.data.text))+ .toEqual(ending === 'success' ? ['Inspect sources.', 'Verify results.', 'Delivered.'] : ['Inspect sources.', 'Verify results.']);+ });+it.each(['codex', 'claude'])('recovers the complete active %s display without retaining another event log', async cli => {const paths = await import('../../../../src/main/paths');const bus = await import('../../../../src/main/features/group_chat/bus');@@ -2484,6 +2533,72 @@ describe('group_chat bus › enqueue routing + persistence', () => {expect(streamProbe.dispatchResults).toHaveLength(0);});+ it.each([+ ['commander', 'missing-file'], ['commander', 'missing-boundary'], ['commander', 'read-error'],+ ['agent', 'missing-boundary'],+ ])('reports unavailable canonical history for %s/%s without replacing it, and recovers', async (actor, fault) => {+ const bus = await import('../../../../src/main/features/group_chat/bus');+ const paths = await import('../../../../src/main/paths');+ const state = await import('../../../../src/main/features/group_chat/state');+ const sessions = await import('../../../../src/main/model/core-agent/session-store');+ const storage = await import('../../../../src/main/storage');+ const cid = `cid-history-${actor}-${fault}`;+ const recipient = actor === 'agent' ? AGENT_NAME : 'commander';+ const send = async (text: string) => {+ await bus.enqueue({ uid: TEST_UID, cid, fromActorId: 'user', text: `@${recipient} ${text}` });+ await waitForQuiescent(TEST_UID, cid);+ };+ await send('HISTORY_FIRST_REQUEST');+ const first = streamProbe.conversationHistories.at(-1);+ expect(first.messages).toEqual([]);+ expect(streamProbe.messages.at(-1)).not.toContain('<history-availability');+ const session = await sessions.getSessionForUser(TEST_UID, actor === 'agent'+ ? state.buildGmemberSessionId(cid, AGENT_ID) : state.buildGconvSessionId(cid));+ session.replaceConversationHistory(first.messages, first.source, { checkpoint: first.checkpoint });+ session.beginUserTurn([{ type: 'text', text: 'HISTORY_FIRST_REQUEST' }]);+ session.addAssistantMessage([{ type: 'text', text: 'prior reply' }]);+ session.completeActiveTurn();+ const retainedLength = session.length;++ // Force a full rebuild by replacing the canonical file, as sync does.+ const file = path.join(paths.userChatsDir(TEST_UID), `${cid}.jsonl`);+ fs.copyFileSync(file, `${file}.replacement`);+ fs.renameSync(`${file}.replacement`, file);+ const originalRead = storage.readJsonl;+ const readFault = vi.spyOn(storage, 'readJsonl').mockImplementation(async (target, limit) => {+ if (target !== file || limit !== 0) return originalRead(target, limit);+ if (fault === 'read-error') throw new Error('EACCES private-history-path');+ if (fault === 'missing-file') {+ fs.renameSync(file, `${file}.unavailable`);+ try { return await originalRead(target, limit); }+ finally { fs.renameSync(`${file}.unavailable`, file); }+ }+ // A partial sync omitted this trigger, but includes another user's later record.+ return [{ id: 'future', from: 'user', to: ['commander'], text: 'FUTURE_MUST_NOT_REPLAY' }];+ });+ try { await send('HISTORY_FAILURE_REQUEST'); }+ finally { readFault.mockRestore(); }+ expect(streamProbe.conversationHistories.at(-1)).toBeUndefined();+ expect(streamProbe.messages.at(-1)).toContain('<history-availability');+ expect(streamProbe.messages.at(-1)).toContain('not evidence of empty history');+ expect(streamProbe.messages.at(-1)).not.toContain('FUTURE_MUST_NOT_REPLAY');+ expect(streamProbe.messages.at(-1)).not.toContain('private-history-path');+ expect(session.getConversationHistoryCheckpoint(first.source)).toBe(first.checkpoint);+ expect(session.length).toBe(retainedLength);+ const warning = loggerMocks.warn.mock.calls.find(([message]) => message === 'conversation history build failed');+ expect(warning).toBeDefined();+ expect(JSON.stringify(warning)).not.toContain('private-history-path');+ expect(JSON.stringify(warning)).not.toContain(cid);++ await send('HISTORY_RECOVERED_REQUEST');+ const recovered = streamProbe.conversationHistories.at(-1);+ expect(JSON.stringify(recovered.messages)).toContain('HISTORY_FIRST_REQUEST');+ expect(JSON.stringify(recovered.messages)).toContain('HISTORY_FAILURE_REQUEST');+ expect(JSON.stringify(recovered.messages)).not.toContain('HISTORY_RECOVERED_REQUEST');+ expect(streamProbe.messages.at(-1)).not.toContain('<history-availability');+ expect(streamProbe.messages).toHaveLength(3);+ });+it('uses the persisted Commander checkpoint to read and replace only the new history tail', async () => {const bus = await import('../../../../src/main/features/group_chat/bus');const paths = await import('../../../../src/main/paths');@@ -4778,7 +4893,8 @@ describe('group_chat bus › enqueue routing + persistence', () => {});});- it('logs canonical CLI history failures without exposing private paths or actor ids', async () => {+ it.each(['read-error', 'missing-boundary'])(+ 'rejects canonical CLI history %s without dispatch or cursor mutation', async (fault) => {const paths = await import('../../../../src/main/paths');const agentFile = path.join(paths.agentDir(TEST_UID, AGENT_ID), 'agent.json');const spec = JSON.parse(fs.readFileSync(agentFile, 'utf8'));@@ -4791,6 +4907,7 @@ describe('group_chat bus › enqueue routing + persistence', () => {await sessions.setSessionId(TEST_UID, cid, AGENT_ID, 'codex', 'thread-private-log', {permissionPolicy: 'inherit',});+ const originalBinding = await sessions.getBinding(TEST_UID, cid, AGENT_ID, 'codex');const storage = await import('../../../../src/main/storage');// The CLI turn reads the canonical log through the bounded tail pager.const originalReadJsonlPage = storage.readJsonlPage.bind(storage);@@ -4801,7 +4918,10 @@ describe('group_chat bus › enqueue routing + persistence', () => {limit?: number,before?: number | null,) => {- if (path.resolve(filePath) === path.resolve(mainFile)) throw new Error(privateFailure);+ if (path.resolve(filePath) === path.resolve(mainFile)) {+ if (fault === 'read-error') throw new Error(privateFailure);+ return { records: [{ id: 'later', from: 'user', text: 'FUTURE_CLI_RECORD' }], nextCursor: null };+ }return originalReadJsonlPage(filePath, limit, before);});@@ -4824,7 +4944,8 @@ describe('group_chat bus › enqueue routing + persistence', () => {error: expect.objectContaining({name: 'Error',message_hash: expect.any(String),- message_chars: privateFailure.length,+ message_chars: fault === 'read-error' ? privateFailure.length+ : 'Canonical conversation history turn boundary is unavailable.'.length,}),}),);@@ -4834,6 +4955,7 @@ describe('group_chat bus › enqueue routing + persistence', () => {expect(JSON.stringify(canonicalLog)).not.toContain(privateFailure);expect(JSON.stringify(canonicalLog)).not.toContain(cid);expect(JSON.stringify(canonicalLog)).not.toContain(AGENT_ID);+ expect(await sessions.getBinding(TEST_UID, cid, AGENT_ID, 'codex')).toEqual(originalBinding);});it('lets a matching Codex thread reuse durable instructions without resending an override', async () => {diff --git a/test/main/features/group_chat/cli-canonical-tail.test.ts b/test/main/features/group_chat/cli-canonical-tail.test.tsindex 76bf0b70..c02b4fea 100644--- a/test/main/features/group_chat/cli-canonical-tail.test.ts+++ b/test/main/features/group_chat/cli-canonical-tail.test.ts@@ -6,7 +6,7 @@ import * as path from 'node:path';// A CLI turn used to parse the whole canonical log to keep ≤20 user turns// (2026-08-28 review A-2). The bounded tail read must still hand the compiler// everything it anchors on: the turn boundary, the stored history cursor and-// enough prior user turns; a missing boundary keeps the whole-log behavior.+// enough prior user turns; an unavailable boundary must not admit unbounded history.let tmpDir: string;beforeEach(() => { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-cli-tail-')); });afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); });@@ -53,11 +53,13 @@ describe('_readCliCanonicalTail', () => {expect(rows.length).toBeLessThan(2000);});- it('falls back to the whole log when the boundary is absent', async () => {+ it('rejects missing boundaries instead of treating later records as prior history', async () => {const { _readCliCanonicalTailForTest } = await import('../../../../src/main/features/group_chat/bus');const file = writeLog(700);- const rows = await _readCliCanonicalTailForTest(file, { boundaryId: 'missing' });- expect(rows).toHaveLength(700);- expect(rows[0].id).toBe('m0');+ await expect(_readCliCanonicalTailForTest(file, { boundaryId: 'missing' }))+ .rejects.toThrow('history turn boundary is unavailable');+ fs.unlinkSync(file);+ await expect(_readCliCanonicalTailForTest(file, { boundaryId: 'm0' }))+ .rejects.toThrow('history turn boundary is unavailable');});});diff --git a/test/main/features/group_chat/cli-prompt-project.test.ts b/test/main/features/group_chat/cli-prompt-project.test.tsindex 0e88413f..94397fc3 100644--- a/test/main/features/group_chat/cli-prompt-project.test.ts+++ b/test/main/features/group_chat/cli-prompt-project.test.ts@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { captureMainLogWorkers } from '../../../helpers/capture-main-log-workers';// A CLI agent dispatched into a project must receive that project's ORKAS.md// in the durable instruction channel, while the user turn remains the actual@@ -9,18 +10,21 @@ import * as path from 'node:path';let tmpDir: string;let prevWs: string | undefined;+let closeLogWorkers: () => Promise<void>;const TEST_UID = 'uCliProj';const CID = 'c_cli_proj';const REPO_LINE = 'Orkas 代码仓库路径:`~/Documents/GitHub/AITeamRelease`。';-beforeEach(() => {+beforeEach(async () => {tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-cli-prompt-'));prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });});diff --git a/test/main/features/group_chat/failed-turn-retry.test.ts b/test/main/features/group_chat/failed-turn-retry.test.tsindex 852fbeb0..1b85cb85 100644--- a/test/main/features/group_chat/failed-turn-retry.test.ts+++ b/test/main/features/group_chat/failed-turn-retry.test.ts@@ -2,9 +2,11 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { captureMainLogWorkers } from '../../../helpers/capture-main-log-workers';let tmpDir: string;let previousWorkspace: string | undefined;+let closeLogWorkers: () => Promise<void>;const UID = 'failed-retry-user';@@ -13,11 +15,13 @@ beforeEach(async () => {previousWorkspace = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();const users = await import('../../../../src/main/features/users');users.activateUser(UID);});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();process.env.ORKAS_WORKSPACE_ROOT = previousWorkspace;fs.rmSync(tmpDir, { recursive: true, force: true });});diff --git a/test/main/features/group_chat/reference-flattening.test.ts b/test/main/features/group_chat/reference-flattening.test.tsindex 6f976376..dd43a372 100644--- a/test/main/features/group_chat/reference-flattening.test.ts+++ b/test/main/features/group_chat/reference-flattening.test.ts@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { drainMainRuntimeForTest } from '../../../helpers/drain-main-runtime';vi.mock('../../../../src/main/model/client', () => ({async *streamChatWithModel() {@@ -33,6 +34,7 @@ afterEach(async () => {await bus.dropConv(UID, cid);}} catch (_) {}+ await drainMainRuntimeForTest();process.env.ORKAS_WORKSPACE_ROOT = previousWorkspace;fs.rmSync(tmpDir, { recursive: true, force: true });});diff --git a/test/main/features/html_preview.test.ts b/test/main/features/html_preview.test.tsindex e254f8e8..ecfc6619 100644--- a/test/main/features/html_preview.test.ts+++ b/test/main/features/html_preview.test.ts@@ -435,7 +435,7 @@ describe('responsive HTML preview renderer', () => {expect(runtime.sessionCleanup).toEqual({ cache: true, storage: true });});- it('blocks remote resources and reports only a bounded origin label', async () => {+ it('permits external resources without weakening local file containment', async () => {const runtime = fakeRuntime({ externalRequest: true });const result = await renderResponsiveHtmlPreview(path.join(root, 'index.html'),@@ -443,16 +443,13 @@ describe('responsive HTML preview renderer', () => {runtime.deps,);- expect(result.evidence.ok).toBe(false);- expect(result.evidence.blockedResourceCount).toBe(2);- expect(result.evidence.blockedResourceSamples).toEqual(['https://example.invalid']);- expect(result.evidence.blockers).toContain(- '2 external or out-of-directory resource request(s) were blocked',- );+ expect(result.evidence.ok).toBe(true);+ expect(result.evidence.blockedResourceCount).toBe(0);+ expect(result.evidence.blockedResourceSamples).toEqual([]);expect(runtime.requestDecisions.filter((item) => item.url.startsWith('https:'))).toEqual([- { url: 'https://example.invalid/tracker.png', cancel: true },- { url: 'https://example.invalid/tracker.png', cancel: true },+ { url: 'https://example.invalid/tracker.png', cancel: false },+ { url: 'https://example.invalid/tracker.png', cancel: false },]);});diff --git a/test/main/features/local_agents/bridge_e2e.test.ts b/test/main/features/local_agents/bridge_e2e.test.tsindex ab835c72..4f5cc187 100644--- a/test/main/features/local_agents/bridge_e2e.test.ts+++ b/test/main/features/local_agents/bridge_e2e.test.ts@@ -854,7 +854,9 @@ describe('orkas-bridge.cjs › MCP stdio e2e', () => {name: 'chat_history',arguments: { action: 'search', query: 'missing', scope: 'current' },});- expect(historyCall.result.content[0].text).toContain('No conversation-history results');+ expect(historyCall.result.isError).toBe(true);+ expect(historyCall.result.content[0].text).toContain('history_status=source_unavailable');+ expect(historyCall.result.content[0].text).toContain('does not establish that history is empty');const handoff = await client.request(7, 'tools/call', {name: 'orkas_handoff_to_commander',diff --git a/test/main/features/office/pptx-text-collision_e2e.test.ts b/test/main/features/office/pptx-text-collision_e2e.test.tsindex 703bb88a..da15d692 100644--- a/test/main/features/office/pptx-text-collision_e2e.test.ts+++ b/test/main/features/office/pptx-text-collision_e2e.test.ts@@ -19,7 +19,7 @@ describe('rendered PPT text collision evidence', () => {path.resolve('test/fixtures/office/text-collision-render.cjs'), process.cwd(), output,], { env, timeout: 30_000, maxBuffer: 256 * 1024 });expect(logs.stderr).toBe('');- expect(logs.stdout).toBe('');+ expect(logs.stdout.trim()).toBe('');const results = JSON.parse(fs.readFileSync(output, 'utf8'));expect(results.collision).toMatchObject({ status: 'checked', assessed_text_elements: 2,warnings: [{ type: 'text_collision_candidate', severity: 'warning', paths: ['/slide[1]/shape[@id=1]', '/slide[1]/shape[@id=2]'] }] });diff --git a/test/main/features/preview-windows.test.ts b/test/main/features/preview-windows.test.tsindex 4fdd9e52..67ba321b 100644--- a/test/main/features/preview-windows.test.ts+++ b/test/main/features/preview-windows.test.ts@@ -130,6 +130,26 @@ describe('independent preview ownership and lifecycle', () => {expect(isPreviewContents(child.webContents)).toBe(false);});+ it('opens more than sixteen apps and releases every window and owner listener on account switch', async () => {+ const main = new BrowserWindow({});+ const sources = Array.from({ length: 20 }, (_, index) => ({ kind: 'app', appId: `app-${index}`, title: `App ${index}` }));+ const opened = [];+ for (const source of sources) opened.push(await openPreview('user-a', main.webContents, source));+ expect(new Set(opened.map(value => value.windowId)).size).toBe(20);+ for (const [index, child] of runtime.windows.slice(1).entries()) {+ expect(initializePreview('user-a', child.webContents).source).toEqual(sources[index]);+ expect(child.dead).toBe(false);+ }+ expect(await openPreview('user-a', main.webContents, sources[0])).toEqual(opened[0]);+ runtime.hook('user-a', 'user-b');+ expect(runtime.windows.slice(1).every(child => child.dead && !isPreviewContents(child.webContents))).toBe(true);+ expect(main.webContents.listenerCount('destroyed')).toBe(0);+ expect(main.webContents.listenerCount('did-start-navigation')).toBe(0);+ const fresh = await openPreview('user-b', main.webContents, sources[0]);+ expect(fresh.windowId).not.toBe(opened[0].windowId);+ expect(initializePreview('user-b', runtime.windows.at(-1).webContents).source).toEqual(sources[0]);+ });+it('binds gallery pages to the original task, accepts only its owner response, and cancels requests on account switch', async () => {const main = new BrowserWindow({});const other = new BrowserWindow({});diff --git a/test/main/features/project_library_indexer.test.ts b/test/main/features/project_library_indexer.test.tsindex 0c4c7168..8e206aa4 100644--- a/test/main/features/project_library_indexer.test.ts+++ b/test/main/features/project_library_indexer.test.ts@@ -4,6 +4,15 @@ import * as os from 'node:os';import * as path from 'node:path';import * as crypto from 'node:crypto';+// Keep diagnostics in Vitest's captured streams. Real logger workers can+// recreate logs after this suite removes its per-case temporary workspace.+vi.mock('../../../src/main/logger', () => ({+ createLogger: (scope: string) => Object.fromEntries(+ ['info', 'debug', 'warn', 'error'].map(level => [level, (...args: unknown[]) =>+ console[level as 'info' | 'debug' | 'warn' | 'error'](`[${scope}]`, ...args)]),+ ),+}));+/*** Project Library indexer: queue lifecycle, reconcile diffing, and the* project-scoped concerns nothing else covers (deletion tombstone/epoch,diff --git a/test/main/features/project_todo_e2e.test.ts b/test/main/features/project_todo_e2e.test.tsindex dcd84d51..f2bc0532 100644--- a/test/main/features/project_todo_e2e.test.ts+++ b/test/main/features/project_todo_e2e.test.ts@@ -2,6 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';/*** Project to-do trunk flow — deterministic end-to-end (no model).@@ -38,6 +39,7 @@ let prevWs: string | undefined;let convSeq: number;const UID = 'uTODOE2E';let AGENT: string;+let closeLogWorkers: () => Promise<void>;beforeEach(async () => {tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-todo-e2e-'));@@ -47,10 +49,12 @@ beforeEach(async () => {// Each user journey owns its dispatch counts; resetModules does not clear hoisted mocks.vi.clearAllMocks();vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();const users = await import('../../../src/main/features/users');users.activateUser(UID);});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });});diff --git a/test/main/features/projects.test.ts b/test/main/features/projects.test.tsindex 4b270cdc..ae0a47f9 100644--- a/test/main/features/projects.test.ts+++ b/test/main/features/projects.test.ts@@ -2,6 +2,8 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { drainMainRuntimeForTest } from '../../helpers/drain-main-runtime';+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';// Mock the model client so cascade-delete (which calls// `chats.deleteConversation`, which clears CLI sessions etc.) doesn't@@ -16,6 +18,7 @@ vi.mock('../../../src/main/model/client', () => ({let tmpDir: string;let prevWs: string | undefined;+let closeLogWorkers: () => Promise<void>;const TEST_UID = 'uProj';beforeEach(async () => {@@ -23,12 +26,15 @@ beforeEach(async () => {prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();const users = await import('../../../src/main/features/users');users.activateUser(TEST_UID);});-afterEach(() => {+afterEach(async () => {vi.restoreAllMocks();+ await drainMainRuntimeForTest();+ await closeLogWorkers();process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });});diff --git a/test/main/features/recycle_bin.test.ts b/test/main/features/recycle_bin.test.tsindex 455126ed..57b62b4b 100644--- a/test/main/features/recycle_bin.test.ts+++ b/test/main/features/recycle_bin.test.ts@@ -4,6 +4,7 @@ import * as fs from 'node:fs';import * as fsp from 'node:fs/promises';import * as os from 'node:os';import * as path from 'node:path';+import { drainMainRuntimeForTest } from '../../helpers/drain-main-runtime';vi.mock('../../../src/main/logger', () => ({createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),@@ -71,7 +72,8 @@ beforeEach(() => {vi.resetModules();});-afterEach(() => {+afterEach(async () => {+ await drainMainRuntimeForTest();if (prevWs === undefined) delete process.env.ORKAS_WORKSPACE_ROOT;else process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });diff --git a/test/main/features/saved_apps.test.ts b/test/main/features/saved_apps.test.tsindex 6b77dbc0..e5bc3930 100644--- a/test/main/features/saved_apps.test.ts+++ b/test/main/features/saved_apps.test.ts@@ -1,7 +1,9 @@+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { drainMainRuntimeForTest } from '../../helpers/drain-main-runtime';// `saved_apps.ts` → `chats.ts` → `group_chat` pulls in `model/client`; mock it// so nothing tries a real LLM call (the openForEditing path doesn't dispatch a@@ -17,16 +19,20 @@ const CID = 'conv-sa-1';let tmpDir: string;let prevWs: string | undefined;+let closeLogWorkers: () => Promise<void>;beforeEach(async () => {tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-savedapps-'));prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();const users = await import('../../../src/main/features/users');users.activateUser(UID);});-afterEach(() => {+afterEach(async () => {+ await drainMainRuntimeForTest();+ await closeLogWorkers();if (prevWs === undefined) delete process.env.ORKAS_WORKSPACE_ROOT;else process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });@@ -44,19 +50,63 @@ const attachDir = (cid: string) => path.join(tmpDir, UID, 'cloud', 'chat_attachm/** Make a chat artifact and return its id. */async function makeArtifact(title: string, extra: Array<{ path: string; content: string }> = []) {const { chatArtifacts } = await mods();- const r = chatArtifacts.createArtifact(UID, CID, 'helper', {+ const r = (await chatArtifacts.createArtifact(UID, CID, 'helper', {title,files: [{ path: 'index.html', content: `<!doctype html><h1>${title}</h1>` }, ...extra],- });+ }));if (!r.ok) throw new Error(`createArtifact failed: ${(r as { error: string }).error}`);return r.artifactId;}describe('saved_apps › saveFromArtifact', () => {+ it('finds a deeply nested source inside its workspace without crossing the workspace fence', async () => {+ const workspace = path.join(tmpDir, 'workspace', 'deep-app');+ const entry = path.join(workspace, 'index.html');+ const script = path.join(workspace, ...Array.from({ length: 12 }, (_, i) => `level-${i}`), 'app.js');+ fs.mkdirSync(path.dirname(script), { recursive: true });+ fs.writeFileSync(entry, '<!doctype html><h1>Deep app</h1>');+ fs.writeFileSync(script, 'window.deepApp = true;');+ const { savedApps } = await mods();+ const saved = await savedApps.saveFromPath(UID, script, { fenceRoots: [workspace] });+ if (!saved.ok) throw new Error(saved.error);+ const resource = savedApps.resolveSavedAppFilePath(UID, saved.id, path.relative(workspace, script).split(path.sep).join('/'));+ if (!resource.ok) throw new Error(resource.error);+ expect(fs.readFileSync(resource.absPath, 'utf8')).toBe('window.deepApp = true;');+ fs.unlinkSync(entry);+ fs.writeFileSync(path.join(path.dirname(workspace), 'index.html'), '<h1>Outside the permitted workspace</h1>');+ expect(await savedApps.inspectBundleFromPath(script, { fenceRoots: [workspace] })).toMatchObject({ canSave: false });+ expect(fs.readdirSync(APPS_ROOT())).toEqual([saved.id]);+ });+ it('preserves built output and nested resources when saving and reopening either creation path', async () => {+ const files = [+ { path: 'index.html', content: '<!doctype html><script src="dist/app.js"></script><link rel="stylesheet" href="build/theme.css">' },+ { path: 'dist/app.js', content: 'window.builtApp = true;' },+ { path: 'build/theme.css', content: 'body { color: blue; }' },+ { path: 'dist/nested/model.gltf', content: '{"asset":{"version":"2.0"}}' },+ ];+ const { savedApps, chatArtifacts } = await mods();+ const created = await chatArtifacts.createArtifact(UID, CID, 'helper', { files });+ if (!created.ok) throw new Error(created.error);+ const local = path.join(tmpDir, 'workspace', 'built-app');+ for (const file of files) {+ const target = path.join(local, file.path);+ fs.mkdirSync(path.dirname(target), { recursive: true });+ fs.writeFileSync(target, file.content);+ }+ for (const saved of [await savedApps.saveFromArtifact(UID, CID, created.artifactId), await savedApps.saveFromPath(UID, path.join(local, 'index.html'))]) {+ if (!saved.ok) throw new Error(saved.error);+ expect(savedApps.resolveSavedAppIndex(UID, saved.id).ok).toBe(true);+ for (const file of files) {+ const resolved = savedApps.resolveSavedAppFilePath(UID, saved.id, file.path);+ if (!resolved.ok) throw new Error(resolved.error);+ expect(fs.readFileSync(resolved.absPath, 'utf8')).toContain(file.content);+ }+ }+ });it('copies the bundle (sans source meta) + stamps a provenance meta', async () => {const aid = await makeArtifact('Tip calc', [{ path: 'assets/app.js', content: 'console.log(1)' }]);const { savedApps } = await mods();- const r = savedApps.saveFromArtifact(UID, CID, aid);+ const r = (await savedApps.saveFromArtifact(UID, CID, aid));expect(r.ok).toBe(true);if (!r.ok) return;expect(r.id).toMatch(/^[A-Za-z0-9_-]{8,}$/);@@ -78,9 +128,9 @@ describe('saved_apps › saveFromArtifact', () => {it('rejects a bad cid, a bad artifactId, and a missing artifact', async () => {const { savedApps } = await mods();- expect(savedApps.saveFromArtifact(UID, '../evil', 'whatever').ok).toBe(false);- expect(savedApps.saveFromArtifact(UID, CID, 'has/slash').ok).toBe(false);- expect(savedApps.saveFromArtifact(UID, CID, 'Zm9vYmFyAA').ok).toBe(false); // well-formed id, no such dir+ expect((await savedApps.saveFromArtifact(UID, '../evil', 'whatever')).ok).toBe(false);+ expect((await savedApps.saveFromArtifact(UID, CID, 'has/slash')).ok).toBe(false);+ expect((await savedApps.saveFromArtifact(UID, CID, 'Zm9vYmFyAA')).ok).toBe(false); // well-formed id, no such dir});});@@ -95,9 +145,9 @@ describe('saved_apps › saveFromPath', () => {fs.writeFileSync(path.join(root, 'node_modules', 'skip', 'big.js'), 'nope');const { savedApps } = await mods();- const inspected = savedApps.inspectBundleFromPath(path.join(root, 'assets', 'app.js'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const inspected = (await savedApps.inspectBundleFromPath(path.join(root, 'assets', 'app.js'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(inspected).toMatchObject({ ok: true, canSave: true, rootDir: root, entry: 'index.html', title: 'Snake' });- const r = savedApps.saveFromPath(UID, path.join(root, 'assets', 'app.js'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const r = (await savedApps.saveFromPath(UID, path.join(root, 'assets', 'app.js'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(r.ok).toBe(true);if (!r.ok) return;const dir = path.join(APPS_ROOT(), r.id);@@ -129,7 +179,7 @@ describe('saved_apps › saveFromPath', () => {fs.symlinkSync(path.join(outside, 'linked.txt'), path.join(root, 'linked.txt'));const { savedApps } = await mods();- const r = savedApps.saveFromPath(UID, path.join(root, 'styles.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const r = (await savedApps.saveFromPath(UID, path.join(root, 'styles.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(r.ok).toBe(true);if (!r.ok) return;const dir = path.join(APPS_ROOT(), r.id);@@ -158,13 +208,13 @@ describe('saved_apps › saveFromPath', () => {const { savedApps } = await mods();for (const name of imageNames) {- expect(savedApps.inspectBundleFromPath(path.join(root, 'assets', name), { fenceRoots: [path.join(tmpDir, 'workspace')] }))+ expect((await savedApps.inspectBundleFromPath(path.join(root, 'assets', name), { fenceRoots: [path.join(tmpDir, 'workspace')] }))).toMatchObject({ ok: true, canSave: false });}- expect(savedApps.saveFromPath(UID, path.join(root, 'assets', 'frame.png'), { fenceRoots: [path.join(tmpDir, 'workspace')] }).ok)+ expect((await savedApps.saveFromPath(UID, path.join(root, 'assets', 'frame.png'), { fenceRoots: [path.join(tmpDir, 'workspace')] })).ok).toBe(false);- const saved = savedApps.saveFromPath(UID, path.join(root, 'styles.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const saved = (await savedApps.saveFromPath(UID, path.join(root, 'styles.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(saved.ok).toBe(true);if (!saved.ok) return;for (const name of imageNames) {@@ -186,11 +236,11 @@ describe('saved_apps › saveFromPath', () => {const { savedApps } = await mods();for (const name of resourceNames) {- expect(savedApps.inspectBundleFromPath(path.join(root, name), { fenceRoots: [path.join(tmpDir, 'workspace')] }))+ expect((await savedApps.inspectBundleFromPath(path.join(root, name), { fenceRoots: [path.join(tmpDir, 'workspace')] }))).toMatchObject({ ok: true, canSave: false });}- const saved = savedApps.saveFromPath(UID, path.join(root, 'styles.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const saved = (await savedApps.saveFromPath(UID, path.join(root, 'styles.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(saved.ok).toBe(true);if (!saved.ok) return;for (const name of resourceNames) {@@ -207,12 +257,12 @@ describe('saved_apps › saveFromPath', () => {fs.writeFileSync(path.join(root, 'assets', 'logo.png'), 'fake png');const { savedApps } = await mods();- const inspectedFromResource = savedApps.inspectBundleFromPath(path.join(root, 'assets', 'game.js'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const inspectedFromResource = (await savedApps.inspectBundleFromPath(path.join(root, 'assets', 'game.js'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(inspectedFromResource).toMatchObject({ ok: true, canSave: true, rootDir: root, entry: 'game.html', title: 'Arcade Game' });- const inspectedFromDir = savedApps.inspectBundleFromPath(root, { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const inspectedFromDir = (await savedApps.inspectBundleFromPath(root, { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(inspectedFromDir).toMatchObject({ ok: true, canSave: true, rootDir: root, entry: 'game.html' });- const r = savedApps.saveFromPath(UID, path.join(root, 'styles.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const r = (await savedApps.saveFromPath(UID, path.join(root, 'styles.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(r.ok).toBe(true);if (!r.ok) return;const dir = path.join(APPS_ROOT(), r.id);@@ -242,11 +292,11 @@ describe('saved_apps › saveFromPath', () => {fs.writeFileSync(path.join(root, 'game.html'), '<!doctype html><title>Game</title>');const { savedApps } = await mods();- expect(savedApps.inspectBundleFromPath(root, { fenceRoots: [path.join(tmpDir, 'workspace')] }))+ expect((await savedApps.inspectBundleFromPath(root, { fenceRoots: [path.join(tmpDir, 'workspace')] }))).toMatchObject({ ok: true, canSave: true, entry: 'index.html', title: 'Main' });- const inspectedGame = savedApps.inspectBundleFromPath(path.join(root, 'game.html'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const inspectedGame = (await savedApps.inspectBundleFromPath(path.join(root, 'game.html'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(inspectedGame).toMatchObject({ ok: true, canSave: true, entry: 'game.html', title: 'Game' });- const saved = savedApps.saveFromPath(UID, path.join(root, 'game.html'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const saved = (await savedApps.saveFromPath(UID, path.join(root, 'game.html'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(saved.ok).toBe(true);if (!saved.ok) return;const resolved = savedApps.resolveSavedAppIndex(UID, saved.id);@@ -261,11 +311,11 @@ describe('saved_apps › saveFromPath', () => {fs.writeFileSync(path.join(root, 'notes.md'), '# notes');const { savedApps } = await mods();- expect(savedApps.inspectBundleFromPath(path.join(root, 'report.pdf'), { fenceRoots: [path.join(tmpDir, 'workspace')] }))+ expect((await savedApps.inspectBundleFromPath(path.join(root, 'report.pdf'), { fenceRoots: [path.join(tmpDir, 'workspace')] }))).toMatchObject({ ok: true, canSave: false });- expect(savedApps.inspectBundleFromPath(root, { fenceRoots: [path.join(tmpDir, 'workspace')] }))+ expect((await savedApps.inspectBundleFromPath(root, { fenceRoots: [path.join(tmpDir, 'workspace')] }))).toMatchObject({ ok: true, canSave: false });- expect(savedApps.saveFromPath(UID, path.join(root, 'report.pdf'), { fenceRoots: [path.join(tmpDir, 'workspace')] }).ok)+ expect((await savedApps.saveFromPath(UID, path.join(root, 'report.pdf'), { fenceRoots: [path.join(tmpDir, 'workspace')] })).ok).toBe(false);});@@ -277,27 +327,42 @@ describe('saved_apps › saveFromPath', () => {fs.writeFileSync(path.join(app, 'src', 'app.js'), 'console.log(1)');const { savedApps } = await mods();- expect(savedApps.inspectBundleFromPath(path.join(app, 'src', 'app.js'), { fenceRoots: [app] }))+ expect((await savedApps.inspectBundleFromPath(path.join(app, 'src', 'app.js'), { fenceRoots: [app] }))).toMatchObject({ ok: true, canSave: false });});- it('refuses bundles over the file-count limit and leaves no temp app directory', async () => {- const root = path.join(tmpDir, 'workspace', 'too-many-files');+ it('saves and opens a large app beyond former file, byte and inventory quotas', async () => {+ const root = path.join(tmpDir, 'workspace', 'large-app');fs.mkdirSync(root, { recursive: true });- fs.writeFileSync(path.join(root, 'index.html'), '<!doctype html><title>Too many</title>');- for (let i = 0; i < 300; i += 1) {- fs.writeFileSync(path.join(root, `file-${String(i).padStart(3, '0')}.css`), 'body{}');- }-+ fs.writeFileSync(path.join(root, 'index.html'), '<!doctype html><title>Large app</title>');+ fs.writeFileSync(path.join(root, 'orkas-app.json'), '{"sdkVersion":1,"capabilities":["storage"]}');+ for (let i = 0; i < 2050; i++) fs.writeFileSync(path.join(root, `f${i}.css`), `/* ${i} */`);+ const large = path.join(root, 'large.wasm');+ fs.writeFileSync(large, ''); fs.truncateSync(large, 31 * 1024 * 1024);+ const nested = Array(34).fill('d').join('/') + '/deep.js';+ fs.mkdirSync(path.dirname(path.join(root, nested)), { recursive: true });+ fs.writeFileSync(path.join(root, nested), 'window.deep = 42');const { savedApps } = await mods();- const inspected = savedApps.inspectBundleFromPath(path.join(root, 'file-000.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] });- expect(inspected).toMatchObject({ ok: true, canSave: false });- if (inspected.ok && !inspected.canSave) expect(inspected.reason).toContain('too many files');- const r = savedApps.saveFromPath(UID, path.join(root, 'file-000.css'), { fenceRoots: [path.join(tmpDir, 'workspace')] });- expect(r.ok).toBe(false);- const names = fs.readdirSync(APPS_ROOT());- expect(names.some((n) => n.includes('.tmp-'))).toBe(false);- expect(names.length).toBe(0);+ let ticks = 0;+ const timer = setInterval(() => { ticks++; }, 0);+ const r = await savedApps.saveFromPath(UID, root, { fenceRoots: [root] });+ clearInterval(timer);+ expect(r.ok).toBe(true);+ if (!r.ok) throw new Error(r.error);+ expect(ticks).toBeGreaterThan(0);+ expect(r.fileCount).toBe(2054);+ expect(fs.statSync(path.join(APPS_ROOT(), r.id, 'large.wasm')).size).toBe(31 * 1024 * 1024);+ expect(fs.readFileSync(path.join(APPS_ROOT(), r.id, 'f2049.css'), 'utf8')).toBe('/* 2049 */');+ const { openApp, runtime } = await import('../../../src/main/features/web_apps/host');+ const app = await openApp(UID, 17, { appId: r.id });+ expect(app).toHaveProperty('token');+ if (!('token' in app)) throw new Error('SDK app was not opened');+ expect(await runtime.call(UID, 17, app.token, 'q1', 'host.getContext', {})).toMatchObject({ sdkVersion: 1 });+ const resource = runtime.resource(new URL(app.url).host, nested, null);+ expect(resource?.resolved).toHaveProperty('absPath');+ expect(fs.readFileSync(resource!.resolved!.absPath, 'utf8')).toBe('window.deep = 42');+ runtime.closeOwner(17);+ expect(fs.readdirSync(APPS_ROOT())).toEqual([r.id]);});});@@ -305,7 +370,7 @@ describe('saved_apps › resolveSavedAppIndex', () => {it('resolves a real app and rejects bad / missing ids', async () => {const aid = await makeArtifact('Game');const { savedApps } = await mods();- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');const ok = savedApps.resolveSavedAppIndex(UID, saved.id);@@ -324,7 +389,7 @@ describe('saved_apps › resolveSavedAppIndex', () => {it('falls back to index.html for legacy or corrupt meta', async () => {const aid = await makeArtifact('Legacy');const { savedApps } = await mods();- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');const dir = path.join(APPS_ROOT(), saved.id);fs.writeFileSync(path.join(dir, '__orkas-meta.json'), '{ not json');@@ -344,7 +409,7 @@ describe('saved_apps › resolveSavedAppFilePath', () => {fs.mkdirSync(path.join(root, 'assets'), { recursive: true });fs.writeFileSync(path.join(root, 'calculator.html'), '<!doctype html><title>Calc</title><script src="assets/app.js"></script>');fs.writeFileSync(path.join(root, 'assets', 'app.js'), 'console.log("calc")');- const saved = savedApps.saveFromPath(UID, path.join(root, 'calculator.html'), { fenceRoots: [path.join(tmpDir, 'workspace')] });+ const saved = (await savedApps.saveFromPath(UID, path.join(root, 'calculator.html'), { fenceRoots: [path.join(tmpDir, 'workspace')] }));expect(saved.ok).toBe(true);if (!saved.ok) return;@@ -367,7 +432,7 @@ describe('saved_apps › resolveSavedAppFilePath', () => {it('rejects traversal, metadata, and unsupported extensions', async () => {const aid = await makeArtifact('Guard');const { savedApps } = await mods();- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');fs.writeFileSync(path.join(APPS_ROOT(), saved.id, 'shell.exe'), 'nope');@@ -387,7 +452,7 @@ describe('saved_apps › resolveSavedAppFilePath', () => {it('rejects an app resource symlink that escapes the saved bundle', async () => {const aid = await makeArtifact('Symlink guard', [{ path: 'assets/app.js', content: 'safe' }]);const { savedApps } = await mods();- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');const outside = path.join(tmpDir, 'outside.js');const linked = path.join(APPS_ROOT(), saved.id, 'assets', 'app.js');@@ -405,7 +470,7 @@ describe('saved_apps › rename / delete', () => {it('renameSavedApp rewrites the meta title; empty title is refused', async () => {const aid = await makeArtifact('Old');const { savedApps } = await mods();- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');expect(savedApps.renameSavedApp(UID, saved.id, ' ').ok).toBe(false);@@ -419,7 +484,7 @@ describe('saved_apps › rename / delete', () => {it('deleteSavedApp removes the directory', async () => {const aid = await makeArtifact('Doomed');const { savedApps } = await mods();- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');const dir = path.join(APPS_ROOT(), saved.id);expect(fs.existsSync(dir)).toBe(true);@@ -460,7 +525,7 @@ describe('saved_apps › listSavedApps', () => {const { savedApps } = await mods();const saves = await Promise.all(['Older alpha', 'Newest Zebra', 'Tie gamma', 'Tie beta', 'Corrupt'].map(async (title) => {const artifactId = await makeArtifact(title);- return savedApps.saveFromArtifact(UID, CID, artifactId);+ return (await savedApps.saveFromArtifact(UID, CID, artifactId));}));if (saves.some((saved) => !saved.ok)) throw new Error('save failed');const [older, newest, tieGamma, tieBeta, corrupt] = saves as Array<{ ok: true; id: string; title: string }>;@@ -521,7 +586,7 @@ describe('saved_apps › listSavedApps', () => {it('does not read or rewrite metadata through a symlinked meta file', async () => {const aid = await makeArtifact('Local app');const { savedApps } = await mods();- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');const outsideMeta = path.join(tmpDir, 'outside-meta.json');@@ -551,7 +616,7 @@ describe('saved_apps › openForEditing', () => {{ path: 'assets/app.js', content: 'const SPEED = 7; // tweak me' },{ path: 'style.css', content: 'body { background: #000 }' },]);- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');const before = (await chats.listConversations(UID)).length;@@ -590,7 +655,7 @@ describe('saved_apps › openForEditing', () => {const chats = await import('../../../src/main/features/chats');const chatAttachments = await import('../../../src/main/features/chat_attachments');const aid = await makeArtifact('Attachment failure');- const saved = savedApps.saveFromArtifact(UID, CID, aid);+ const saved = (await savedApps.saveFromArtifact(UID, CID, aid));if (!saved.ok) throw new Error('save failed');vi.spyOn(chatAttachments, 'uploadAttachment').mockResolvedValue({ok: false,diff --git a/test/main/features/search/chat_store.test.ts b/test/main/features/search/chat_store.test.tsindex 634fa554..709d2520 100644--- a/test/main/features/search/chat_store.test.ts+++ b/test/main/features/search/chat_store.test.ts@@ -210,22 +210,55 @@ describe('search/chat_store', () => {expect([...store.indexedConversationIds(UID)].sort()).toEqual(['c1', 'c2', 'c3']);});- it('refuses a database written by a different schema version', async () => {- // A silently accepted mismatch would read the wrong columns and report an- // empty history as a legitimate result.+ it('rebuilds a database written by an older schema version', async () => {+ // The index is derived state; the conversation JSONL can always rebuild it.+ // Refusing to open a store from an earlier build instead turned every+ // search into a permanent failure for anyone who already had one.const store = await loadStore();- store.upsertDoc(UID, doc('c1', 0, 'alpha'));+ store.upsertDoc(UID, doc('c1', 0, 'staleword'));+ const dbPath = store.chatStorePath(UID);+ store.closeAllChatStores();++ const { default: Database } = await import('better-sqlite3');+ const raw = new Database(dbPath);+ raw.pragma(`user_version = ${store.CHAT_STORE_SCHEMA_VERSION - 1}`);+ raw.close();++ const { vi } = await import('vitest');+ vi.resetModules();+ const reopened = await loadStore();+ // Discarded rather than silently accepted: the old document is gone, and+ // the store reports itself unbuilt so search says it is still preparing+ // instead of presenting an empty index as a real answer.+ expect(reopened.docCount(UID)).toBe(0);+ expect(reopened.postingsFor(UID, 'staleword')).toHaveLength(0);+ expect(reopened.hasCompletedRebuild(UID)).toBe(false);+ reopened.upsertDoc(UID, doc('c1', 0, 'freshword'));+ expect(reopened.postingsFor(UID, 'freshword')).toHaveLength(1);+ });++ it('rebuilds a database missing a table this build needs', async () => {+ // The shipped defect had exactly this shape: a release added `chat_terms`+ // without bumping the version, so a version-only check called the store+ // usable and every query failed on the missing table.+ const store = await loadStore();+ store.upsertDoc(UID, doc('c1', 0, 'staleword'));const dbPath = store.chatStorePath(UID);store.closeAllChatStores();const { default: Database } = await import('better-sqlite3');const raw = new Database(dbPath);- raw.pragma(`user_version = ${store.CHAT_STORE_SCHEMA_VERSION + 1}`);+ raw.exec('DROP TABLE chat_terms');+ raw.pragma(`user_version = ${store.CHAT_STORE_SCHEMA_VERSION}`);raw.close();const { vi } = await import('vitest');vi.resetModules();const reopened = await loadStore();- expect(() => reopened.docCount(UID)).toThrow(/schema version mismatch/);+ expect(reopened.docCount(UID)).toBe(0);+ expect(reopened.hasCompletedRebuild(UID)).toBe(false);+ reopened.upsertDoc(UID, doc('c2', 0, 'freshword'));+ expect(reopened.postingsFor(UID, 'freshword')).toHaveLength(1);+ expect(reopened.postingsFor(UID, 'staleword')).toHaveLength(0);});});diff --git a/test/main/features/skills.test.ts b/test/main/features/skills.test.tsindex d730d3be..35eb4ae8 100644--- a/test/main/features/skills.test.ts+++ b/test/main/features/skills.test.ts@@ -4,6 +4,17 @@ import * as os from 'node:os';import * as path from 'node:path';import AdmZip from 'adm-zip';+// Each case resets modules and deletes its workspace. Keep diagnostics visible+// without starting file-log workers that can recreate that workspace at teardown.+vi.mock('../../../src/main/logger', () => ({+ createLogger: (scope: string) => ({+ debug: (...args: unknown[]) => console.debug(`[${scope}]`, ...args),+ info: (...args: unknown[]) => console.info(`[${scope}]`, ...args),+ warn: (...args: unknown[]) => console.warn(`[${scope}]`, ...args),+ error: (...args: unknown[]) => console.error(`[${scope}]`, ...args),+ }),+}));+// skills.ts pulls path constants + the module-level _skillListCache at load.// Reset ORKAS_WORKSPACE_ROOT + module graph per test for isolation.@@ -2687,3 +2698,110 @@ describe('skills › streamSendToSkillChat synthesized progress', () => {// (The legacy marketplace-sentinel sync tests are gone. Marketplace installs now live at// `<uid>/local/marketplace/skills/<id>/` and are reconciled from// the cloud-synced `installs.json` manifest — see features/marketplace_*.ts.)++describe('skills › bounded creation correction', () => {+ const md = '---\nname: repaired-skill\ndescription: Summarize supplied notes\n---\n\n# Instructions\nRead the supplied notes and summarize them.';+ const block = (entry = md, extra = '') => `<skill>\n<<<skill-file path=SKILL.md\n${entry}\n>>>\n${extra}\n</skill>`;+ const note = '<<<skill-file path=notes.txt\nRetain these source notes.\n>>>';++ it('reports structured pre-write failures and preserves an existing file when adding the missing entry', async () => {+ const s = await loadSkills();+ const repair = await import('../../../src/main/features/group_chat/skill-creation-correction');+ const original = s.extractSkillContainers(`<skill>\n${note}\n</skill>`).containers[0];+ const rejected = await s.applySkillContainerFromCommander(original);+ expect(rejected).toMatchObject({ ok: false, creationError: 'missing_skill_md' });+ expect(fs.readdirSync(customSkillsDir())).toEqual([]);+ expect(repair.skillCreationCorrectionMessage(original, rejected, 'Create a notes Skill')).toContain('missing_skill_md');+ const result = await repair.applySkillCreationCorrection(block(md, note), original, TEST_UID, new AbortController().signal);+ expect(result).toMatchObject({ ok: true, kind: 'created', written: ['SKILL.md', 'notes.txt'] });+ expect(fs.readFileSync(path.join(customSkillsDir(), 'repaired-skill', 'notes.txt'), 'utf8')).toBe('Retain these source notes.');+ expect(fs.readFileSync(path.join(customSkillsDir(), 'repaired-skill', 'SKILL.md'), 'utf8')).toContain('Read the supplied notes');+ // A replay encounters the original collision guard, never overwrites.+ const again = await repair.applySkillCreationCorrection(block(md, note), original, TEST_UID, new AbortController().signal);+ expect(again?.ok).toBe(false);+ expect(again?.creationError).toBeUndefined();+ expect(fs.readdirSync(customSkillsDir())).toEqual(['repaired-skill']);+ });++ it.each([+ ['missing name', '---\ndescription: Notes\n---\n# Instructions\nSummarize notes.', 'missing_name'],+ ['invalid name', '---\nname: bad/name\ndescription: Notes\n---\n# Instructions\nSummarize notes.', 'invalid_name'],+ ])('corrects %s without allocating a rejected seed', async (_, input, code) => {+ const s = await loadSkills();+ const repair = await import('../../../src/main/features/group_chat/skill-creation-correction');+ const original = s.extractSkillContainers(block(input)).containers[0];+ const rejected = await s.applySkillContainerFromCommander(original);+ expect(rejected).toMatchObject({ ok: false, creationError: code });+ expect(fs.readdirSync(customSkillsDir())).toEqual([]);+ expect(repair.skillCreationCorrectionMessage(original, rejected, 'Create a notes Skill')).not.toBeNull();+ expect(await repair.applySkillCreationCorrection(block(), original, TEST_UID, new AbortController().signal))+ .toMatchObject({ ok: true, skillId: 'repaired-skill' });+ });++ it.each([+ ['changed retained file', block(md, note.replace('Retain', 'Discard'))],+ ['extra file', block(md, note + '\n<<<skill-file path=unexpected.txt\nnew\n>>>')],+ ['edit target', block(md, note).replace('<skill>', '<skill>\n<skill_id>existing</skill_id>')],+ ['another Skill', block(md, note) + block(md.replace('repaired-skill', 'another-skill'))],+ ['Agent mutation', block(md, note) + '<agent><operation>create</operation><name>Unexpected</name></agent>'],+ ['unsupported path', block(md, note + '\n<<<skill-file path=../escape.txt\nescape\n>>>')],+ ['case alias', block(md, note + '\n<<<skill-file path=skill.md\n' + md + '\n>>>')],+ ['file directory conflict', block(md, note + '\n<<<skill-file path=notes.txt/child\nnew\n>>>')],+ ['invalid corrected entry', block('Still missing frontmatter', note)],+ ['prose-only refusal', 'Insufficient information to produce a Skill.'],+ ])('rejects %s before any corrected files are written', async (_, text) => {+ const s = await loadSkills();+ const repair = await import('../../../src/main/features/group_chat/skill-creation-correction');+ const original = s.extractSkillContainers(`<skill>\n${note}\n</skill>`).containers[0];+ expect(await repair.applySkillCreationCorrection(text, original, TEST_UID, new AbortController().signal)).toBeNull();+ expect(fs.readdirSync(customSkillsDir())).toEqual([]);+ expect(fs.existsSync(path.join(tmpDir, TEST_UID, 'cloud', 'escape.txt'))).toBe(false);+ });++ it.each([+ ['traversal', '<<<skill-file path=../escape.txt\nescape\n>>>'],+ ['Windows device', '<<<skill-file path=CON.txt\ndevice\n>>>'],+ ['case alias', '<<<skill-file path=skill.md\n' + md + '\n>>>'],+ ['file/directory conflict with a sibling', '<<<skill-file path=notes\nfile\n>>>\n<<<skill-file path=notes-extra\nsibling\n>>>\n<<<skill-file path=notes/child\nchild\n>>>'],+ ])('rejects %s even when the original file blocks were unparseable', async (_, extra) => {+ const repair = await import('../../../src/main/features/group_chat/skill-creation-correction');+ expect(await repair.applySkillCreationCorrection(block(md, extra), { files: [] }, TEST_UID, new AbortController().signal)).toBeNull();+ expect(fs.readdirSync(customSkillsDir())).toEqual([]);+ });++ it('repairs a malformed container without replaying its surrounding prose', async () => {+ const s = await loadSkills();+ const repair = await import('../../../src/main/features/group_chat/skill-creation-correction');+ const original = s.extractSkillContainers('<skill>```markdown\n' + md + '\n```</skill>').containers[0];+ const failure = await s.applySkillContainerFromCommander(original);+ expect(failure).toMatchObject({ ok: false, creationError: 'container_no_blocks' });+ expect(repair.skillCreationCorrectionMessage(original, failure, 'Create a notes Skill')).not.toBeNull();+ expect(await repair.applySkillCreationCorrection(block(), original, TEST_UID, new AbortController().signal))+ .toMatchObject({ ok: true, skillId: 'repaired-skill' });+ });++ it('does not offer correction for unsafe files, edits, collisions or oversized requests', async () => {+ const s = await loadSkills();+ const repair = await import('../../../src/main/features/group_chat/skill-creation-correction');+ const unsafe = { files: [{ path: 'scripts/run.py', content: 'eval(input())' }] };+ expect(repair.skillCreationCorrectionMessage(unsafe, await s.applySkillContainerFromCommander(unsafe), 'Create')).toBeNull();+ const edit = { skillId: 'missing-target', files: [] };+ expect(repair.skillCreationCorrectionMessage(edit, await s.applySkillContainerFromCommander(edit), 'Edit')).toBeNull();+ writeCustomSkill('repaired-skill');+ const existing = s.extractSkillContainers(block()).containers[0];+ expect(repair.skillCreationCorrectionMessage(existing, await s.applySkillContainerFromCommander(existing), 'Create')).toBeNull();+ const original = s.extractSkillContainers(`<skill>\n${note}\n</skill>`).containers[0];+ expect(repair.skillCreationCorrectionMessage(original, await s.applySkillContainerFromCommander(original), 'x'.repeat(64_000))).toBeNull();+ expect(fs.readFileSync(path.join(customSkillsDir(), 'repaired-skill', 'SKILL.md'), 'utf8')).toContain('# body');+ });++ it.each(['cancelled', 'account switched'])('discards a valid correction when %s', async reason => {+ const repair = await import('../../../src/main/features/group_chat/skill-creation-correction');+ const controller = new AbortController();+ if (reason === 'cancelled') controller.abort();+ else (await import('../../../src/main/features/users')).activateUser('another-account');+ expect(await repair.applySkillCreationCorrection(block(), { files: [] }, TEST_UID, controller.signal)).toBeNull();+ expect(fs.readdirSync(customSkillsDir())).toEqual([]);+ expect(fs.existsSync(path.join(tmpDir, 'another-account', 'cloud', 'skills', 'repaired-skill'))).toBe(false);+ });+});diff --git a/test/main/features/system_skills.test.ts b/test/main/features/system_skills.test.tsindex dc60b205..871723b3 100644--- a/test/main/features/system_skills.test.ts+++ b/test/main/features/system_skills.test.ts@@ -12,17 +12,31 @@ const UID = 'system-skills-user';let tmpDir: string;let prevWs: string | undefined;+let logDeliveries: ReturnType<typeof import('../../../src/main/util/log-delivery').createLogDelivery>[];-beforeEach(() => {+beforeEach(async () => {vi.doUnmock('node:fs');vi.doUnmock('../../../src/main/paths');tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-system-skills-'));prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ logDeliveries = [];+ const delivery = await import('../../../src/main/util/log-delivery');+ const create = delivery.createLogDelivery;+ vi.spyOn(delivery, 'createLogDelivery').mockImplementation((...args) => {+ const instance = create(...args);+ logDeliveries.push(instance);+ return instance;+ });});-afterEach(() => {+afterEach(async () => {+ // Account activation starts a real log worker. Drain and close it before+ // removing its directory so late file creation cannot race the teardown.+ try {+ for (const delivery of logDeliveries) await vi.waitFor(() => expect(delivery.stats().pending).toBe(0));+ } finally { await Promise.all(logDeliveries.map(delivery => delivery.close())); }if (prevWs === undefined) delete process.env.ORKAS_WORKSPACE_ROOT;else process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });diff --git a/test/main/features/web-app-host.test.ts b/test/main/features/web-app-host.test.tsindex 1738e064..c401e614 100644--- a/test/main/features/web-app-host.test.ts+++ b/test/main/features/web-app-host.test.ts@@ -1,7 +1,9 @@+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { EventEmitter } from 'node:events';const connector = vi.hoisted(() => ({ instances: [] as any[], call: vi.fn(async () => ({ content: [{ type: 'text', text: 'Verified remote result' }] })), approve: vi.fn(async () => false) }));vi.mock('electron', () => ({ dialog: {}, app: { isPackaged: false } }));@@ -14,19 +16,94 @@ vi.mock('../../../src/main/features/connectors/action_confirm', async original =}));let root: string, previous: string | undefined;const uid = 'web-host-test';+let closeLogWorkers: () => Promise<void>;beforeEach(async () => {root = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-web-host-'));previous = process.env.ORKAS_WORKSPACE_ROOT; process.env.ORKAS_WORKSPACE_ROOT = root;- vi.resetModules(); connector.instances = []; connector.call.mockClear(); connector.approve.mockClear();+ vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers(); connector.instances = []; connector.call.mockClear(); connector.approve.mockClear();(await import('../../../src/main/features/users')).activateUser(uid);});afterEach(async () => {(await import('../../../src/main/features/web_apps/host')).runtime.closeUser(uid);(await import('../../../src/main/features/kb_vector')).closeAllKb();+ await closeLogWorkers();if (previous === undefined) delete process.env.ORKAS_WORKSPACE_ROOT; else process.env.ORKAS_WORKSPACE_ROOT = previous;fs.rmSync(root, { recursive: true, force: true });});describe('Web host reuses owning services', () => {+ it('keeps same-bundle navigation usable and refuses look-alike destinations without closing it', async () => {+ const { createArtifact } = await import('../../../src/main/features/chat_artifacts');+ const { runtime, appResource } = await import('../../../src/main/features/web_apps/host');+ const { webAppInvokeHandlers } = await import('../../../src/main/ipc/web_apps');+ const created = await createArtifact(uid, 'pages-test', 'agent', { files: [+ { path: 'index.html', content: '<h1>First</h1>' },+ { path: 'pages/second.html', content: '<h1>Second</h1>' },+ { path: 'data.json', content: '{}' },+ { path: 'orkas-app.json', content: JSON.stringify({ sdkVersion: 1, capabilities: ['storage'] }) },+ ] });+ if (!created.ok) throw new Error(created.error);+ const sender = Object.assign(new EventEmitter(), { id: 10, isDestroyed: () => false });+ const app: any = await webAppInvokeHandlers['webApps.open'](+ { source: { cid: 'pages-test', artifactId: created.artifactId } }, { userId: uid, sender: sender as any });+ await runtime.call(uid, sender.id, app.token, 'save', 'storage.set', { key: 'draft', value: 'Retained' });+ for (const [relative, allowed] of [['/pages/second.html?view=2#row', true], ['/index.html?reload', true],+ ['/data.json', false], ['/missing.html', false], ['/%E0%A4%A.html', false]] as const) {+ const destination = new URL(relative, app.url).href;+ expect(runtime.canNavigate(999, app.url, destination)).toBe(false);+ for (const frameAvailable of [true, false]) {+ const event = { url: destination, isMainFrame: false, preventDefault: vi.fn(),+ ...(frameAvailable ? { frame: { url: app.url } } : { initiator: { url: app.url } }) };+ sender.emit('will-frame-navigate', event);+ expect(event.preventDefault.mock.calls.length).toBe(allowed ? 0 : 1);+ if (allowed) expect(appResource(new Request(destination))).toMatchObject({ status: 200 });+ }+ }+ // An external child navigating from about:blank must not revoke its parent.+ sender.emit('will-frame-navigate', { url: 'https://example.test/', isMainFrame: false,+ frame: { url: 'about:blank' }, initiator: { url: app.url }, preventDefault: vi.fn() });+ expect(await runtime.call(uid, sender.id, app.token, 'read', 'storage.get', { key: 'draft' })).toEqual({ value: 'Retained' });+ });+ it('allows external document navigation while revoking only the departing SDK instance', async () => {+ const { createArtifact } = await import('../../../src/main/features/chat_artifacts');+ const { runtime, appResource } = await import('../../../src/main/features/web_apps/host');+ const { webAppInvokeHandlers } = await import('../../../src/main/ipc/web_apps');+ const created = await createArtifact(uid, 'navigation-test', 'agent', { files: [+ { path: 'index.html', content: '<h1>Website launcher</h1>' },+ { path: 'orkas-app.json', content: JSON.stringify({ sdkVersion: 1, capabilities: ['storage'] }) },+ ] });+ if (!created.ok) throw new Error(created.error);+ const sender = Object.assign(new EventEmitter(), { id: 10, isDestroyed: () => false });+ const open = () => webAppInvokeHandlers['webApps.open'](+ { source: { cid: 'navigation-test', artifactId: created.artifactId } }, { userId: uid, sender: sender as any },+ ) as Promise<any>;+ const sibling = await open();+ for (const [url, allowed] of [+ ['https://www.baidu.com/', true], ['http://example.test:8080/page?q=1#section', true],+ ['file:///private/local.html', false], ['data:text/html,local', false],+ ['javascript:alert(1)', false], ['chat-app://saved/foreign/index.html', false],+ ['https://user:[email protected]/', false], ['https://', false],+ ] as const) {+ for (const frameAvailable of [true, false]) {+ const app = await open();+ await runtime.call(uid, sender.id, app.token, 'before', 'host.getContext', {});+ const event = { url, isMainFrame: false, preventDefault: vi.fn(),+ ...(frameAvailable ? { frame: { url: app.url } } : { initiator: { url: app.url } }) };+ sender.emit('will-frame-navigate', event);+ expect(event.preventDefault.mock.calls.length, url).toBe(allowed ? 0 : 1);+ if (allowed) {+ await expect(runtime.call(uid, sender.id, app.token, 'after', 'storage.keys', {})).rejects.toMatchObject({ code: 'E_CLOSED' });+ expect(appResource(new Request(app.url))).toEqual({ status: 403 });+ } else {+ expect(await runtime.call(uid, sender.id, app.token, 'after', 'storage.keys', {})).toEqual({ keys: [] });+ expect(appResource(new Request(app.url))).toMatchObject({ status: 200 });+ }+ }+ }+ expect(await runtime.call(uid, sender.id, sibling.token, 'still-open', 'storage.keys', {})).toEqual({ keys: [] });+ const reopened = await open();+ expect(await runtime.call(uid, sender.id, reopened.token, 'fresh', 'storage.keys', {})).toEqual({ keys: [] });+ });it('reads real global Library chunks and cannot adopt a project through caller arguments', async () => {const kb = await import('../../../src/main/features/kb_vector');const embedding = Array(512).fill(0); embedding[0] = 1;@@ -62,7 +139,7 @@ describe('Web host reuses owning services', () => {connector.instances[0].status = { kind: 'disconnected' };expect(await appTools(uid)).toHaveLength(1);});- it.each([9_000, 11_000])('applies the shared ordinary result limit to Web connector output (%s tokens)', async (tokens) => {+ it.each([9_000, 110_000])('returns complete Web connector output without applying model-context budgets (%s tokens)', async (tokens) => {connector.instances = [{ id: 'reader', display_name: 'Reader', transport: { kind: 'streamable-http', url: 'https://example.invalid/mcp' },enabled_subtools: null, tools_cache: [{ name: 'READ_REPORT', description: 'Read report', input_schema: { type: 'object' } }],status: { kind: 'connected', since: 0 }, created_at: '', updated_at: '', tools_cached_at: Date.now() }];@@ -71,24 +148,20 @@ describe('Web host reuses owning services', () => {const { appTools } = await import('../../../src/main/features/web_apps/host');const invoke = (await appTools(uid)).find(tool => tool.name === 'call_connector_tool')!;const result = invoke.execute({ connector_id: 'reader', tool_name: 'READ_REPORT', args: {} }, new AbortController().signal);- if (tokens < 10_000) {- expect(await result).toMatchObject({ content: expect.stringContaining('x'.repeat(tokens * 4)) });- } else {- await expect(result).rejects.toMatchObject({ code: 'E_RESULT_LIMIT' });- }+ expect(await result).toEqual({ content: 'x'.repeat(tokens * 4) });expect(connector.call).toHaveBeenCalledOnce();});it('keeps legacy URLs and binds SDK resources to the launch revision', async () => {const artifact = await import('../../../src/main/features/chat_artifacts');const { openApp, appResource, runtime } = await import('../../../src/main/features/web_apps/host');const files = [{ path: 'index.html', content: '<h1>Original</h1>' }];- const old: any = artifact.createArtifact(uid, 'cid-test', 'agent', { title: 'Legacy', files });+ const old: any = (await artifact.createArtifact(uid, 'cid-test', 'agent', { title: 'Legacy', files }));expect(old.ok).toBe(true);- const legacy = openApp(uid, 1, { cid: 'cid-test', artifactId: old.artifactId });+ const legacy = (await openApp(uid, 1, { cid: 'cid-test', artifactId: old.artifactId }));expect(legacy).not.toHaveProperty('token'); expect(legacy).toMatchObject({ entry: 'index.html' });- const created: any = artifact.createArtifact(uid, 'cid-test', 'agent', { title: 'SDK', files: [...files,- { path: 'orkas-app.json', content: JSON.stringify({ sdkVersion: 1, capabilities: ['storage'] }) }] });- const app: any = openApp(uid, 1, { cid: 'cid-test', artifactId: created.artifactId });+ const created: any = (await artifact.createArtifact(uid, 'cid-test', 'agent', { title: 'SDK', files: [...files,+ { path: 'orkas-app.json', content: JSON.stringify({ sdkVersion: 1, capabilities: ['storage'] }) }] }));+ const app: any = (await openApp(uid, 1, { cid: 'cid-test', artifactId: created.artifactId }));expect(appResource(new Request(app.url))).toMatchObject({ status: 200, source: 'chat_app_artifact' });const sdk: any = appResource(new Request(new URL('/__orkas/sdk.js', app.url)));expect(sdk.body).toContain('"ai.generate"'); expect(sdk.headers['Access-Control-Allow-Origin']).toBeUndefined();diff --git a/test/main/features/web-app-ipc.test.ts b/test/main/features/web-app-ipc.test.tsnew file mode 100644index 00000000..5179610a--- /dev/null+++ b/test/main/features/web-app-ipc.test.ts@@ -0,0 +1,40 @@+import { afterEach, beforeEach, expect, it, vi } from 'vitest';+import type { WebAppRuntime } from '../../../src/main/features/web_apps/runtime';++let runtime: WebAppRuntime;+beforeEach(() => vi.resetModules());+afterEach(() => { runtime?.closeOwner(1); vi.doUnmock('../../../src/main/features/web_apps/host'); });++it('cancels a producer that outruns the progress consumer without replay and keeps the next request usable', async () => {+ const { WebAppRuntime, AppError } = await import('../../../src/main/features/web_apps/runtime');+ let resume!: () => void;+ let observedSignal!: AbortSignal;+ const generate = vi.fn(async (_uid, _args, signal, progress) => {+ observedSignal = signal;+ progress({ text: 'First' });+ await new Promise<void>(resolve => { resume = resolve; });+ for (let i = 0; i < 129; i++) progress({ text: String(i) });+ return { text: 'Must not complete after overflow' };+ });+ runtime = new WebAppRuntime({ activeUser: () => 'user', language: () => 'en', modelAvailable: () => true,+ pick: async () => null, save: async () => null, tools: async () => [], generate });+ vi.doMock('../../../src/main/features/web_apps/host', () => ({ runtime,+ openApp: vi.fn(), safeFailure: (error: unknown) => ({ code: error instanceof AppError ? error.code : 'E_FAILED' }) }));+ const { webAppCall } = await import('../../../src/main/ipc/web_apps');+ const app = runtime.open('user', 1, { key: 'stream-test', title: 'Stream', entry: 'index.html',+ manifest: { sdkVersion: 1, capabilities: ['ai'] }, resolve: () => null });+ const ctx = { userId: 'user', sender: { id: 1 } as any };+ const stream = webAppCall({ token: app.token, requestId: 'burst', method: 'ai.generate', args: { prompt: 'Report' } }, ctx, new AbortController().signal);+ expect((await stream.next()).value).toEqual({ type: 'progress', value: { text: 'First' } });+ resume();+ await new Promise(resolve => setImmediate(resolve));+ const events = [];+ for await (const event of stream) events.push(event);+ expect(events.filter(event => event.type === 'progress')).toHaveLength(128);+ expect(events.at(-1)).toMatchObject({ type: 'result', ok: false, value: { code: 'E_CANCELLED' } });+ expect(observedSignal.aborted).toBe(true);+ expect(generate).toHaveBeenCalledOnce();+ const recovered = [];+ for await (const event of webAppCall({ token: app.token, requestId: 'recovered', method: 'host.getContext', args: {} }, ctx, new AbortController().signal)) recovered.push(event);+ expect(recovered).toEqual([{ type: 'result', ok: true, value: { language: 'en', sdkVersion: 1 } }]);+});diff --git a/test/main/features/web-app-preview.test.ts b/test/main/features/web-app-preview.test.tsindex 8c9d83a1..c4383ce8 100644--- a/test/main/features/web-app-preview.test.ts+++ b/test/main/features/web-app-preview.test.ts@@ -14,27 +14,33 @@ afterEach(() => fs.rmSync(root, { recursive: true, force: true }));const manifest = (text = '{"sdkVersion":1,"capabilities":["storage"]}') => fs.writeFileSync(path.join(path.dirname(entry), 'orkas-app.json'), text);describe('preview source opt-in and revision isolation', () => {- it('preserves ordinary HTML and fails closed for invalid or unsupported opt-ins', () => {- expect(previewBundle(entry)).toBeNull();+ it('preserves ordinary HTML and fails closed for invalid or unsupported opt-ins', async () => {+ expect((await previewBundle(entry))).toBeNull();for (const text of ['{', '{"sdkVersion":2,"capabilities":[]}', ' '.repeat(16385)]) {- manifest(text); expect(() => previewBundle(entry)).toThrow('E_MANIFEST');+ manifest(text); await expect(previewBundle(entry)).rejects.toThrow('E_MANIFEST');}});- it('serves only the frozen bundle and invalidates calls after loaded source changes', () => {- manifest(); const bundle = previewBundle(entry)!;+ it('rejects entry replacement while collecting the launch inventory', async () => {+ manifest();+ const opening = previewBundle(entry);+ fs.writeFileSync(entry, '<h1>Replaced during launch</h1>');+ await expect(opening).rejects.toThrow('E_BUNDLE');+ });+ it('serves only the frozen bundle and invalidates calls after loaded source changes', async () => {+ manifest(); const bundle = (await previewBundle(entry))!;expect(bundle.resolve('index.html')).toEqual({ absPath: entry, mime: 'text/html; charset=utf-8' });for (const rel of ['../private.txt', '/private.txt', 'dir/../../private.txt', '.env', 'dir\\file']) expect(bundle.resolve(rel)).toBeNull();expect(bundle.valid!()).toBe(true);fs.writeFileSync(entry, '<h1>Changed</h1>');expect(bundle.valid!()).toBe(false); expect(bundle.resolve('index.html')).toBeNull();});- it('rejects linked manifests, sibling files and linked assets before launching', () => {+ it('rejects linked manifests, sibling files and linked assets before launching', async () => {const outside = path.join(root, 'outside.json'); fs.writeFileSync(outside, '{"sdkVersion":1,"capabilities":[]}');fs.symlinkSync(outside, path.join(path.dirname(entry), 'orkas-app.json'));- expect(() => previewBundle(entry)).toThrow('E_MANIFEST');+ await expect(previewBundle(entry)).rejects.toThrow('E_MANIFEST');fs.unlinkSync(path.join(path.dirname(entry), 'orkas-app.json')); manifest();fs.symlinkSync(outside, path.join(path.dirname(entry), 'asset.json'));- expect(() => previewBundle(entry)).toThrow('E_BUNDLE');+ await expect(previewBundle(entry)).rejects.toThrow('E_BUNDLE');});});@@ -53,10 +59,13 @@ it('accepts only its own trusted frame, token and narrow operations, then revokeunhandle: () => { resource = undefined; },} };const before = fs.readdirSync(os.tmpdir()).filter(name => name.startsWith('orkas-preview-storage-')).sort();- const preview = await createPreviewSdk(ses, contents, previewBundle(entry)!);+ const preview = await createPreviewSdk(ses, contents, (await previewBundle(entry))!);try {url = preview.shellUrl;expect((await resource!(new Request(preview.origin + '/__orkas/sdk.js'))).status).toBe(200);+ const policy = (await resource!(new Request(preview.origin + '/index.html'))).headers.get('Content-Security-Policy')!;+ expect(policy).toContain("connect-src 'self' http: https: ws: wss:");+ expect(policy).toContain("script-src 'self' http: https: 'unsafe-inline'");expect((await resource!(new Request(preview.origin + '/index.html', { headers: { Origin: 'chat-app://foreign' } }))).status).toBe(403);expect((await resource!(new Request('chat-app://app-00000000000000000000000000000000/index.html'))).status).toBe(403);const message = { id: '1', operation: 'auth.getConfig', payload: { token: 'forged', requestId: 'q1', method: 'storage.set', args: { key: 'x', value: 'forged' } } };@@ -69,7 +78,7 @@ it('accepts only its own trusted frame, token and narrow operations, then revokecontents.emit('will-frame-navigate', { isMainFrame: false, frame: { url: preview.origin + '/index.html' },url: 'https://example.invalid/', preventDefault: () => { prevented = true; } });expect(prevented).toBe(true);- expect((await resource!(new Request(preview.origin + '/__orkas/sdk.js'))).status).toBe(403);+ expect((await resource!(new Request(preview.origin + '/__orkas/sdk.js'))).status).toBe(200);} finally { await preview.dispose(); }expect(resource).toBeUndefined(); expect(contents.listenerCount('ipc-message')).toBe(0);expect(fs.readdirSync(os.tmpdir()).filter(name => name.startsWith('orkas-preview-storage-')).sort()).toEqual(before);diff --git a/test/main/features/web-assist-control.test.ts b/test/main/features/web-assist-control.test.tsindex e86749ba..98e9539c 100644--- a/test/main/features/web-assist-control.test.ts+++ b/test/main/features/web-assist-control.test.ts@@ -2096,7 +2096,7 @@ describe('host-started task browser binding', () => {expect(allowWebAssistDownloadOrigin('u1', cid, origin)).toMatchObject({ ok: true });const retry = attempt(url, 'site-export.csv');expect(retry.prevented.value).toBe(false);- expect(retry.item.savePath).toBe(`${DOWNLOAD_ROOT}/u1/${cid}/site-export.csv`);+ expect(retry.item.savePath).toBe(path.join(DOWNLOAD_ROOT, 'u1', cid, 'site-export.csv'));});it.each(['blob:null/opaque-id', 'blob:file:///tmp/private.csv', 'file:///tmp/private.csv', 'not-a-url'])diff --git a/test/main/features/web_apps.test.ts b/test/main/features/web_apps.test.tsindex 4cb332d4..bc3a8252 100644--- a/test/main/features/web_apps.test.ts+++ b/test/main/features/web_apps.test.ts@@ -7,6 +7,7 @@ import { constants } from 'node:fs';import nativeFs from 'node:fs';import { syncBuiltinESMExports } from 'node:module';import { spawnSync } from 'node:child_process';+import { MAX_TEXT_FILE_BYTES } from '../../../src/main/util/file-size-limits';import type { WebAppRuntime, HostAdapters, Bundle } from '../../../src/main/features/web_apps/runtime';let root: string, prev: string | undefined, uid: string, runtime: WebAppRuntime, host: HostAdapters;let mod: typeof import('../../../src/main/features/web_apps/runtime');@@ -89,8 +90,9 @@ describe('Web app capability journeys', () => {it('discovers real schemas and distinguishes declaration from authorization', async () => {const a = open(); const catalog = await call(a.token, 'capabilities.list');expect(catalog.methods.find((m: any) => m.name === 'ai.generate')).toMatchObject({ available: true, declared: true, authorized: true, authorization: 'none' });- expect((await call(a.token,'capabilities.describe',{method:'ai.generate'})).inputSchema.properties.maxTokens.maximum).toBe(4096);+ expect((await call(a.token,'capabilities.describe',{method:'ai.generate'})).inputSchema.properties.maxTokens.default).toBeUndefined();expect(catalog.unsupported.find((m: any) => m.capability === 'shell').reason).toContain('Host-only');+ expect(catalog.unsupported.some((m: any) => m.capability === 'network')).toBe(false);await expect(call(a.token,'invented.execute')).rejects.toMatchObject({code:'E_METHOD'});await expect(call(a.token,'storage.get',{key:'x',userId:'other'})).rejects.toMatchObject({code:'E_INPUT'});});@@ -150,11 +152,12 @@ describe('Web app capability journeys', () => {const a=open(), selected=await call(a.token,'files.pick');await expect(call(a.token,'files.readText',{handle:selected.handle})).rejects.toMatchObject({code:'E_ENCODING'});expect(await call(a.token,'files.readBase64',{handle:selected.handle})).toEqual({base64:'//4='});- await fs.writeFile(file,Buffer.alloc(512*1024+1));+ await fs.truncate(file, MAX_TEXT_FILE_BYTES + 1);await expect(call(a.token,'files.pick')).rejects.toMatchObject({code:'E_LIMIT'});});it('bounds nested and oversized JSON before persistence', async () => {- const a=open(); await expect(call(a.token,'storage.set',{key:'x',value:'a'.repeat(1024*1024)})).rejects.toMatchObject({code:'E_LIMIT'});+ const a=open();+ expect(() => mod.boundedJson({ value: 'x'.repeat(1024) }, 1024)).toThrow('E_LIMIT');let deep:any={}; for(let i=0;i<40;i++) deep={child:deep};await expect(call(a.token,'storage.set',{key:'x',value:deep})).rejects.toMatchObject({code:'E_LIMIT'});expect(await call(a.token,'storage.keys')).toEqual({keys:[]});@@ -184,11 +187,11 @@ describe('Web app capability journeys', () => {expect(host.closed).toHaveBeenCalledTimes(2);expect(host.closed).toHaveBeenLastCalledWith(uid, { id: b.token, owner: 1 });});- it('delivers streaming progress and usage with a bounded default output', async () => {+ it('delivers streaming progress and usage without an app-specific output default', async () => {const a=open(), progress=vi.fn(); host.generate=vi.fn(async(_uid,_args,_signal,out)=>{out({type:'delta',text:'Hi'});return {text:'Hi',usage:{outputTokens:1},stopReason:'end_turn'};});expect(await runtime.call(uid,1,a.token,'model','ai.generate',{prompt:'Hello'},progress)).toMatchObject({text:'Hi',usage:{outputTokens:1}});expect(progress).toHaveBeenCalledWith({type:'delta',text:'Hi'});- expect(host.generate).toHaveBeenCalledWith(uid,{prompt:'Hello',maxTokens:1024},expect.any(AbortSignal),expect.any(Function));+ expect(host.generate).toHaveBeenCalledWith(uid,{prompt:'Hello'},expect.any(AbortSignal),expect.any(Function));});it('revocation aborts generation and rejects stale instance calls', async () => {const a=open(); let start!:()=>void; const ready=new Promise<void>(r=>{start=r;});@@ -230,11 +233,12 @@ describe('Web app capability journeys', () => {await expect(runtime.call(uid,1,a.token,'once','storage.set',{key:'x',value:3})).rejects.toMatchObject({code:'E_DUPLICATE'});expect(await call(a.token,'storage.get',{key:'x'})).toEqual({value:2});});- it('preserves owning tool failures and rejects oversized results without replay', async () => {+ it('preserves owning tool failures and rejects unserializable results without replay', async () => {const execute=vi.fn(async()=>({content:'Denied',isError:true}));host.tools=vi.fn(async()=>[{name:'library',capability:'library',description:'',inputSchema:{},execute}]); const a=open();expect(await call(a.token,'tools.call',{name:'library',arguments:{action:'list'}})).toEqual({content:'Denied',isError:true});- execute.mockResolvedValueOnce({content:'x'.repeat(1024*1024+1),isError:false});+ const cyclic: any = {}; cyclic.self = cyclic;+ execute.mockResolvedValueOnce(cyclic);await expect(call(a.token,'tools.call',{name:'library',arguments:{}})).rejects.toMatchObject({code:'E_RESULT_LIMIT'});expect(execute).toHaveBeenCalledTimes(2);await expect(call(a.token,'tools.call',{name:'bash',arguments:{}})).rejects.toMatchObject({code:'E_TOOL'});@@ -257,26 +261,31 @@ describe('Web app resource lifetime and contention', () => {await expect(call(a.token, 'storage.get', { key: 'private' })).rejects.toMatchObject({ code: 'E_FILE' });expect(await call(b.token, 'storage.get', { key: 'private' })).toEqual({ value: 'Other app' });});- it('bounds expensive work across applications and releases capacity after cancellation', async () => {+ it('accepts concurrent work across applications and cancels it without blocking recovery', async () => {let started = 0;host.generate = vi.fn(async (_u, _a, signal) => {started++;return new Promise((_resolve, reject) => signal.addEventListener('abort', () => reject(new mod.AppError('E_CANCELLED')), { once: true }));});- const a = open('a'), b = open('b'), c = open('c');- const runs = [call(a.token, 'ai.generate', { prompt: 'First' }), call(b.token, 'ai.generate', { prompt: 'Second' })];+ const apps = Array.from({ length: 40 }, (_, i) => open('app-' + i));+ const runs = Array.from({ length: 8 }, (_, i) => call(apps[i % 2].token, 'ai.generate', { prompt: 'Parallel ' + i }));const outcomes = Promise.allSettled(runs);- await vi.waitFor(() => expect(started).toBe(2));- await expect(call(c.token, 'ai.generate', { prompt: 'Third' })).rejects.toMatchObject({ code: 'E_BUSY' });- runtime.closeOrigin(1, a.url); runtime.closeOrigin(1, b.url);+ await vi.waitFor(() => expect(started).toBe(8));+ expect(await call(apps[0].token, 'host.getContext')).toMatchObject({ sdkVersion: 1 });+ runtime.closeOrigin(1, apps[0].url); runtime.closeOrigin(1, apps[1].url);expect((await outcomes).every(r => r.status === 'rejected')).toBe(true);host.generate = vi.fn(async () => ({ text: 'Recovered' }));- expect(await call(c.token, 'ai.generate', { prompt: 'Recovered' })).toEqual({ text: 'Recovered' });+ expect(await call(apps[39].token, 'ai.generate', { prompt: 'Recovered' })).toEqual({ text: 'Recovered' });});- it('expires authority even if no new application is opened', async () => {+ it('keeps an open app usable across days and still revokes it explicitly', async () => {const a = open(); const now = Date.now();- const time = vi.spyOn(Date, 'now').mockReturnValue(now + 13 * 60 * 60 * 1000);+ await call(a.token, 'storage.set', { key: 'draft', value: 'Still working' });+ const time = vi.spyOn(Date, 'now').mockReturnValue(now + 7 * 24 * 60 * 60 * 1000);try {+ expect(await call(a.token, 'storage.get', { key: 'draft' })).toEqual({ value: 'Still working' });+ open('another-app');+ expect(await call(a.token, 'storage.get', { key: 'draft' })).toEqual({ value: 'Still working' });+ await call(a.token, 'permissions.revoke');await expect(call(a.token, 'storage.keys')).rejects.toMatchObject({ code: 'E_CLOSED' });expect(runtime.resource(new URL(a.url).host, 'index.html', null)).toBeNull();} finally { time.mockRestore(); }@@ -338,7 +347,7 @@ describe('app sandboxes and compatibility', () => {expect(await call(otherAccount.token, 'appFiles.list')).toEqual({ files: [] });});- it('rejects undeclared calls, traversal, aliases, malformed binary and excessive data without publication', async () => {+ it('rejects undeclared calls, traversal, aliases and malformed binary without publication', async () => {const a = open('sandbox', ['appFiles']);const limited = open('limited', []);await expect(call(limited.token, 'appFiles.list')).rejects.toMatchObject({ code: 'E_NOT_DECLARED' });@@ -346,7 +355,6 @@ describe('app sandboxes and compatibility', () => {await expect(call(a.token, 'appFiles.writeText', { path: file, text: 'escape' })).rejects.toMatchObject({ code: 'E_INPUT' });}await expect(call(a.token, 'appFiles.writeBase64', { path: 'bad', base64: 'not base64' })).rejects.toMatchObject({ code: 'E_INPUT' });- await expect(call(a.token, 'appFiles.writeText', { path: 'large', text: '你'.repeat(200000) })).rejects.toMatchObject({ code: 'E_LIMIT' });await expect(call(a.token, 'appFiles.list', { scope: 'outside' })).rejects.toMatchObject({ code: 'E_INPUT' });const paths = await import('../../../src/main/paths');const files = path.join(paths.webAppSandboxRoot(uid, bundle('sandbox').key, 'cloud'), 'files');@@ -365,16 +373,21 @@ describe('app sandboxes and compatibility', () => {await expect(call(a.token, 'appFiles.writeText', { path: 'nested/outside/new', text: 'evil' })).rejects.toMatchObject({ code: 'E_FILE' });});- it('enforces aggregate quota and supports recovery by deleting a file', async () => {+ it('grows beyond old aggregate quotas without scanning unrelated files for each write', async () => {const a = open('quota', ['appFiles']);const paths = await import('../../../src/main/paths');const rootFiles = path.join(paths.webAppSandboxRoot(uid, bundle('quota').key, 'cloud'), 'files');await fs.mkdir(rootFiles, { recursive: true });- for (let i = 0; i < 32; i++) await fs.writeFile(path.join(rootFiles, `${i}.bin`), Buffer.alloc(512 * 1024));- await expect(call(a.token, 'appFiles.writeText', { path: 'one.txt', text: 'one' })).rejects.toMatchObject({ code: 'E_LIMIT' });- await call(a.token, 'appFiles.remove', { path: '0.bin' });- await call(a.token, 'appFiles.writeText', { path: 'one.txt', text: 'one' });+ for (let i = 0; i < 300; i++) await fs.writeFile(path.join(rootFiles, `${i}.bin`), Buffer.alloc(64 * 1024, i % 256));+ const scan = vi.spyOn(nativeFs.promises, 'readdir'); syncBuiltinESMExports();+ try {+ await call(a.token, 'appFiles.writeText', { path: 'one.txt', text: 'one' });+ expect(scan).not.toHaveBeenCalled();+ } finally { scan.mockRestore(); syncBuiltinESMExports(); }+ expect((await call(a.token, 'appFiles.list')).files).toHaveLength(301);expect(await call(a.token, 'appFiles.readText', { path: 'one.txt' })).toEqual({ text: 'one' });+ await call(a.token, 'appFiles.remove', { path: '0.bin' });+ expect((await call(a.token, 'appFiles.list')).files).toHaveLength(300);});it('keeps both preview scopes temporary without cloud notifications', async () => {@@ -416,3 +429,206 @@ it.each(['switch', 'revoke', 'cancel'])('does not publish sandbox bytes after %sexpect((host.storageChanged as any).mock.calls).toHaveLength(changeCount);} finally { spy.mockRestore(); syncBuiltinESMExports(); }});++// A working notebook must grow beyond the old demo quotas and stay usable.+it('persists larger files and JSON, many entries and long-lived request sequences', async () => {+ const app = open('growing-notebook', ['storage', 'appFiles']);+ const text = 'data:α\n'.repeat(180000);+ await call(app.token, 'appFiles.writeText', { path: 'large.txt', text });+ expect(await call(app.token, 'appFiles.readText', { path: 'large.txt' })).toEqual({ text });+ await call(app.token, 'storage.set', { key: 'large', value: text });+ expect(await call(app.token, 'storage.get', { key: 'large' })).toEqual({ value: text });+ const paths = await import('../../../src/main/paths');+ const key = bundle('growing-notebook').key;+ // Pre-existing synced state is subject to the same admission as app writes.+ const entries = Object.fromEntries(Array.from({ length: 300 }, (_, i) => ['k' + i, i]));+ await fs.writeFile(paths.webAppDataFile(uid, key), JSON.stringify({ version: 1, entries }));+ await call(app.token, 'storage.set', { key: 'new', value: 'retained' });+ expect((await call(app.token, 'storage.keys')).keys).toHaveLength(301);+ runtime.closeOwner(1); runtime = new mod.WebAppRuntime(host);+ const reopened = open('growing-notebook', ['storage', 'appFiles']);+ expect(await call(reopened.token, 'appFiles.readText', { path: 'large.txt' })).toEqual({ text });+ expect(await call(reopened.token, 'storage.get', { key: 'new' })).toEqual({ value: 'retained' });+ for (let i = 0; i < 4200; i++) await call(reopened.token, 'host.getContext');+ expect(await call(reopened.token, 'storage.get', { key: 'new' })).toEqual({ value: 'retained' });+ await expect(runtime.call(uid, 1, reopened.token, 'q' + seq, 'storage.remove', { key: 'new' }))+ .rejects.toMatchObject({ code: 'E_DUPLICATE' });+ expect(await call(reopened.token, 'storage.get', { key: 'new' })).toEqual({ value: 'retained' });+});++it('remembers cancelled and completed SDK ids out of order without rejecting intervening work', async () => {+ const a = open();+ runtime.cancel(uid, 1, a.token, 'q4');+ for (const id of ['q2', 'q1', 'q3', 'q5'])+ await runtime.call(uid, 1, a.token, id, 'storage.set', { key: id, value: id });+ for (const id of ['q1', 'q2', 'q3', 'q4', 'q5'])+ await expect(runtime.call(uid, 1, a.token, id, 'storage.set', { key: 'replay', value: true }))+ .rejects.toMatchObject({ code: 'E_DUPLICATE' });+ expect(await runtime.call(uid, 1, a.token, 'q6', 'storage.keys', {})).toEqual({ keys: ['q1', 'q2', 'q3', 'q5'] });+});++it('lets the owning service complete long work without an additional SDK deadline', async () => {+ vi.useFakeTimers();+ let finish!: (value: unknown) => void;+ let signal!: AbortSignal;+ host.generate = async (_u, _args, ownedSignal) => {+ signal = ownedSignal;+ return new Promise(resolve => { finish = resolve; });+ };+ try {+ const app = open();+ const work = call(app.token, 'ai.generate', { prompt: 'Long operation' });+ await vi.advanceTimersByTimeAsync(30 * 60 * 1000);+ expect(signal.aborted).toBe(false);+ finish({ text: 'Completed once' });+ expect(await work).toEqual({ text: 'Completed once' });+ expect(await call(app.token, 'host.getContext')).toMatchObject({ sdkVersion: 1 });+ } finally { vi.useRealTimers(); }+});++it('keeps acknowledged browser calls alive until result or explicit cancellation', async () => {+ const { runInNewContext } = await import('node:vm');+ const source = await fs.readFile(path.resolve('src/main/features/web_apps/sdk.js'), 'utf8');+ vi.useFakeTimers();+ try {+ const listeners: Record<string, (event: any) => void> = {};+ const parent = { postMessage: vi.fn() };+ const window: any = { parent, addEventListener: (type: string, fn: any) => { listeners[type] = fn; } };+ runInNewContext(source, { window, parent, setTimeout, clearTimeout, crypto: (await import('node:crypto')).webcrypto });+ const unavailable = window.orkasApp.call('tools.call', {});+ const missing = expect(unavailable).rejects.toMatchObject({ code: 'E_HOST_UNAVAILABLE' });+ await vi.advanceTimersByTimeAsync(10000);+ await missing;+ parent.postMessage.mockClear();+ const work = window.orkasApp.call('tools.call', {});+ const { id } = parent.postMessage.mock.calls[0][0];+ listeners.message({ source: parent, data: { __orkasApp: 1, id, type: 'ack' } });+ await vi.advanceTimersByTimeAsync(30 * 60 * 1000);+ expect(parent.postMessage).toHaveBeenCalledTimes(1);+ listeners.message({ source: parent, data: { __orkasApp: 1, id, type: 'result', ok: true, value: 'Complete' } });+ expect(await work).toBe('Complete');+ const delayed = window.orkasApp.call('tools.call', {});+ const delayedId = parent.postMessage.mock.calls.at(-1)![0].id;+ // A known host can be busy; subsequent transport acknowledgements do not+ // impose a second deadline on the owning service's operation.+ await vi.advanceTimersByTimeAsync(60 * 1000);+ expect(parent.postMessage).toHaveBeenCalledTimes(2);+ listeners.message({ source: parent, data: { __orkasApp: 1, id: delayedId, type: 'result', ok: true, value: 'Delayed' } });+ expect(await delayed).toBe('Delayed');+ const controller = new AbortController();+ const cancelled = window.orkasApp.call('tools.call', {}, { signal: controller.signal });+ const outcome = expect(cancelled).rejects.toMatchObject({ code: 'E_CANCELLED' });+ controller.abort();+ await outcome;+ expect(parent.postMessage.mock.calls.at(-1)![0].type).toBe('cancel');+ } finally { vi.useRealTimers(); }+});++it('can select more than sixteen files and release snapshots independently', async () => {+ const source = path.join(root, 'selected.txt');+ await fs.writeFile(source, 'Selected content');+ host.pick = async () => source;+ const a = open();+ const handles: string[] = [];+ for (let i = 0; i < 20; i++) handles.push((await call(a.token, 'files.pick')).handle);+ for (const handle of handles) {+ expect(await call(a.token, 'files.readText', { handle })).toEqual({ text: 'Selected content' });+ await call(a.token, 'files.release', { handle });+ await expect(call(a.token, 'files.readText', { handle })).rejects.toMatchObject({ code: 'E_HANDLE' });+ }+});++it('keeps deep application paths and long storage keys usable across reopening', async () => {+ const a=open('first',['storage','appFiles']);const rel=Array.from({length:12},(_,i)=>'directory-'+i+'-long-name').join('/')+'/value.txt';+ await call(a.token,'appFiles.writeText',{path:rel,text:'Preserved'});+ const key='k'.repeat(256);await call(a.token,'storage.set',{key,value:42});+ runtime.closeOwner(1);const b=open('first',['storage','appFiles']);+ expect(await call(b.token,'appFiles.readText',{path:rel})).toEqual({text:'Preserved'});+ expect(await call(b.token,'appFiles.list')).toMatchObject({files:[{path:rel,size:9}]});+ expect(await call(b.token,'storage.get',{key})).toEqual({value:42});+ await expect(call(b.token,'appFiles.readText',{path:'../private.txt'})).rejects.toMatchObject({code:'E_INPUT'});+});++it('measures JSON bytes and nesting while preserving state after a rejected update', async () => {+ expect(mod.boundedJson('汉', 5)).toBe('"汉"');+ expect(() => mod.boundedJson('汉', 4)).toThrow('E_LIMIT');+ const nested = (depth: number) => { let value: any = 0; for (let i = 0; i < depth; i++) value = [value]; return value; };+ expect(JSON.parse(mod.boundedJson(nested(32)))).toEqual(nested(32));+ expect(() => mod.boundedJson(nested(33))).toThrow('E_LIMIT');+ const a = open();+ await call(a.token, 'storage.set', { key: 'draft', value: 'Original' });+ await expect(call(a.token, 'storage.set', { key: 'draft', value: nested(33) })).rejects.toMatchObject({ code: 'E_LIMIT' });+ runtime.closeOwner(1);+ const reopened = open();+ expect(await call(reopened.token, 'storage.get', { key: 'draft' })).toEqual({ value: 'Original' });+ await call(reopened.token, 'storage.set', { key: 'draft', value: 'Recovered' });+ expect(await call(reopened.token, 'storage.get', { key: 'draft' })).toEqual({ value: 'Recovered' });+});++it('exports an ordinary long filename without weakening path validation or create-only publication', async () => {+ const a = open();+ const name = 'report-'.repeat(25) + '.txt';+ const target = path.join(root, name);+ host.save = vi.fn(async () => target);+ expect(await call(a.token, 'files.saveAs', { name, text: 'Preserved report' })).toEqual({ saved: true, name });+ expect(host.save).toHaveBeenCalledWith(name);+ expect(await fs.readFile(target, 'utf8')).toBe('Preserved report');+ await expect(call(a.token, 'files.saveAs', { name: '../outside.txt', text: 'Not saved' })).rejects.toMatchObject({ code: 'E_INPUT' });+ expect(host.save).toHaveBeenCalledOnce();+ await expect(call(a.token, 'files.saveAs', { name, text: 'Do not overwrite' })).rejects.toMatchObject({ code: 'EEXIST' });+ expect(await fs.readFile(target, 'utf8')).toBe('Preserved report');+});++it('rejects oversized progress without replay and allows a new explicit request', async () => {+ const a = open();+ const progress = vi.fn();+ host.generate = vi.fn(async (_uid, _args, _signal, emit) => {+ emit({ text: '汉'.repeat(22000) }); // Below 64 Ki characters, above 64 KiB.+ return { text: 'Must not be reported as success' };+ });+ await expect(runtime.call(uid, 1, a.token, 'oversized', 'ai.generate', { prompt: 'Report' }, progress))+ .rejects.toMatchObject({ code: 'E_LIMIT' });+ expect(progress).not.toHaveBeenCalled();+ expect(host.generate).toHaveBeenCalledOnce();+ host.generate = vi.fn(async (_uid, _args, _signal, emit) => { emit({ text: 'Recovered' }); return { text: 'Recovered' }; });+ expect(await runtime.call(uid, 1, a.token, 'retry', 'ai.generate', { prompt: 'Retry' }, progress)).toEqual({ text: 'Recovered' });+ expect(progress).toHaveBeenCalledOnce();+ expect(host.generate).toHaveBeenCalledOnce();+});++it('keeps replay and cancellation independent across page generations and legacy SDK requests', async () => {+ const a = open();+ const first = 'd' + 'a'.repeat(32) + 'q';+ const second = 'd' + 'b'.repeat(32) + 'q';+ const write = (id: string, value: number) => runtime.call(uid, 1, a.token, id, 'storage.set', { key: 'counter', value });+ await write(first + '1', 1);+ await write(second + '1', 2);+ runtime.cancel(uid, 1, a.token, first + '2');+ await expect(write(first + '2', 99)).rejects.toMatchObject({ code: 'E_DUPLICATE' });+ await expect(write(first + '1', 99)).rejects.toMatchObject({ code: 'E_DUPLICATE' });+ await write(second + '2', 3);+ await write('q1', 4);+ expect(await runtime.call(uid, 1, a.token, 'read-counter', 'storage.get', { key: 'counter' })).toEqual({ value: 4 });+});++it('shares initial host discovery across concurrent requests and cancels all work on page departure', async () => {+ const { runInNewContext } = await import('node:vm');+ const source = await fs.readFile(path.resolve('src/main/features/web_apps/sdk.js'), 'utf8');+ vi.useFakeTimers();+ try {+ const listeners: Record<string, (event?: any) => void> = {};+ const parent = { postMessage: vi.fn() };+ const window: any = { parent, addEventListener: (type: string, fn: any) => { listeners[type] = fn; } };+ runInNewContext(source, { window, parent, setTimeout, clearTimeout, crypto: (await import('node:crypto')).webcrypto });+ const requests = [window.orkasApp.call('storage.keys'), window.orkasApp.call('host.getContext')];+ const outcomes = requests.map(request => expect(request).rejects.toMatchObject({ code: 'E_CLOSED' }));+ const ids = parent.postMessage.mock.calls.map(([message]) => message.id);+ listeners.message({ source: parent, data: { __orkasApp: 1, id: ids[0], type: 'ack' } });+ await vi.advanceTimersByTimeAsync(60 * 1000);+ expect(parent.postMessage).toHaveBeenCalledTimes(2);+ listeners.pagehide();+ await Promise.all(outcomes);+ expect(parent.postMessage.mock.calls.slice(2).map(([message]) => message)).toEqual(ids.map(id => ({ __orkasApp: 1, type: 'cancel', id })));+ expect(vi.getTimerCount()).toBe(0);+ } finally { vi.useRealTimers(); }+});diff --git a/test/main/ipc/contexts-pick-upload.test.ts b/test/main/ipc/contexts-pick-upload.test.tsindex 063ef3fc..37009d9c 100644--- a/test/main/ipc/contexts-pick-upload.test.ts+++ b/test/main/ipc/contexts-pick-upload.test.ts@@ -3,6 +3,7 @@ import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';import { trustedIpcSender } from '../../helpers/trusted-ipc-sender';+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';vi.mock('electron', () => ({app: { isPackaged: false },@@ -33,6 +34,7 @@ vi.mock('../../../src/main/features/kb_vector', () => ({let tmpDir: string;let prevWs: string | undefined;+let closeLogWorkers: () => Promise<void>;const TEST_UID = 'uContextPickUpload';beforeEach(async () => {@@ -40,12 +42,14 @@ beforeEach(async () => {prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();vi.clearAllMocks();const users = await import('../../../src/main/features/users');users.activateUser(TEST_UID);});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });});diff --git a/test/main/ipc/import-local-files.test.ts b/test/main/ipc/import-local-files.test.tsindex cbf48efa..417fa184 100644--- a/test/main/ipc/import-local-files.test.ts+++ b/test/main/ipc/import-local-files.test.ts@@ -7,6 +7,7 @@ import { trustedIpcSender } from '../../helpers/trusted-ipc-sender';import { makeMinimalPdf } from '../../fixtures/make-minimal-pdf';import { makeMinimalDocx } from '../../fixtures/make-minimal-docx';import { makeMinimalXlsx, makeMinimalPptx } from '../../fixtures/make-minimal-office';+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';// `orkas.importLocalFiles` is the private preload → main channel that carries// paths resolved from genuine user-selected File objects. The conversation@@ -43,6 +44,7 @@ vi.mock('../../../src/main/features/kb_vector', () => ({let tmpDir: string;let sourceDir: string;let prevWs: string | undefined;+let closeLogWorkers: () => Promise<void>;const TEST_UID = 'uImportLocalFiles';const CID = 'conv-import-local';@@ -52,12 +54,14 @@ beforeEach(async () => {prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();vi.clearAllMocks();const users = await import('../../../src/main/features/users');users.activateUser(TEST_UID);});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();vi.restoreAllMocks();if (prevWs === undefined) delete process.env.ORKAS_WORKSPACE_ROOT;else process.env.ORKAS_WORKSPACE_ROOT = prevWs;diff --git a/test/main/ipc/memory-project.test.ts b/test/main/ipc/memory-project.test.tsindex c4c40ec2..19dc2fa8 100644--- a/test/main/ipc/memory-project.test.ts+++ b/test/main/ipc/memory-project.test.ts@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';vi.mock('../../../src/main/model/client', () => ({async *streamChatWithModel() {@@ -13,6 +14,7 @@ vi.mock('../../../src/main/model/client', () => ({let tmpDir: string;let prevWs: string | undefined;+let closeLogWorkers: () => Promise<void>;const UID = 'u-memory-ipc';beforeEach(async () => {@@ -20,11 +22,13 @@ beforeEach(async () => {prevWs = process.env.ORKAS_WORKSPACE_ROOT;process.env.ORKAS_WORKSPACE_ROOT = tmpDir;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();const users = await import('../../../src/main/features/users');users.activateUser(UID);});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();if (prevWs === undefined) delete process.env.ORKAS_WORKSPACE_ROOT;else process.env.ORKAS_WORKSPACE_ROOT = prevWs;fs.rmSync(tmpDir, { recursive: true, force: true });diff --git a/test/main/log-delivery.test.ts b/test/main/log-delivery.test.tsnew file mode 100644index 00000000..2c0fe490--- /dev/null+++ b/test/main/log-delivery.test.ts@@ -0,0 +1,89 @@+import { EventEmitter } from 'node:events';+import { Worker } from 'node:worker_threads';+import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import path from 'node:path';+import { describe, expect, it, vi } from 'vitest';+import { createLogDelivery } from '../../src/main/util/log-delivery';++class HeldWorker extends EventEmitter {+ messages: any[] = [];+ unref = vi.fn();+ postMessage = vi.fn((message: any) => { this.messages.push(message); });+ terminate = vi.fn(async () => 0);+}++describe('log delivery isolation', () => {+ it('returns with the worker stalled, bounds its mailbox and reserves critical capacity', async () => {+ const worker = new HeldWorker();+ const delivery = createLogDelivery({}, () => worker as any);+ for (let i = 0; i < 1000; i++) delivery.send({ level: 'info', data: ['record'] });+ expect(worker.messages).toHaveLength(192);+ for (let i = 0; i < 100; i++) delivery.send({ level: 'error', data: ['failure'] });+ expect(worker.messages).toHaveLength(256);+ expect(delivery.stats()).toEqual({ pending: 256, dropped: 844, failed: false });+ // No acknowledgement is needed to continue the product action.+ let completed = false;+ await new Promise<void>(resolve => setImmediate(() => { completed = true; resolve(); }));+ expect(completed).toBe(true);+ worker.emit('message', { written: true });+ expect(worker.messages.at(-1)).toMatchObject({ level: 'warn', data: ['Log records dropped', { dropped: 844 }] });+ expect(delivery.stats().pending).toBe(256);+ worker.emit('message', { written: true });+ expect(delivery.canAccept('error')).toBe(true);+ await delivery.close();+ });++ it('contains post failures, worker failure and later producer calls', async () => {+ const worker = new HeldWorker();+ const delivery = createLogDelivery({}, () => worker as any);+ worker.postMessage.mockImplementationOnce(() => { throw new Error('injected channel failure'); });+ expect(() => delivery.send({ level: 'error' })).not.toThrow();+ expect(delivery.stats().pending).toBe(0);+ worker.emit('error', new Error('injected worker failure'));+ expect(() => delivery.send({ level: 'error' })).not.toThrow();+ expect(delivery.stats()).toEqual({ pending: 0, dropped: 2, failed: true });+ expect(worker.postMessage).toHaveBeenCalledTimes(1);+ await delivery.close();+ });++ it('does not loop on write failures and reports loss after a successful write', async () => {+ const worker = new HeldWorker();+ const delivery = createLogDelivery({}, () => worker as any);+ delivery.send({ level: 'warn' });+ worker.emit('message', { written: false });+ expect(worker.messages).toHaveLength(1);+ expect(delivery.stats().dropped).toBe(1);+ delivery.send({ level: 'warn' });+ worker.emit('message', { written: true });+ expect(worker.messages).toHaveLength(3);+ await delivery.close();+ });++ it('writes scoped records and performs retention/rotation in a real worker', async () => {+ const directory = mkdtempSync(path.join(tmpdir(), 'orkas-worker-log-'));+ writeFileSync(path.join(directory, '2000-01-01.log'), 'expired');+ writeFileSync(path.join(directory, 'keep.txt'), 'untouched');+ let consoleOutput = '';+ const delivery = createLogDelivery({ directory, fileLevel: 'info', consoleLevel: 'info',+ retainDays: 7, totalMaxBytes: 100000, fileMaxBytes: 256 }, (file, config) => {+ const worker = new Worker(file, { ...config, stdout: true, stderr: true });+ worker.stdout.on('data', chunk => { consoleOutput += chunk.toString(); });+ worker.stderr.on('data', chunk => { consoleOutput += chunk.toString(); });+ return worker;+ });+ try {+ for (let i = 0; i < 8; i++) delivery.send({ level: 'info', scope: 'worker-smoke', date: new Date(), data: [`record-${i}`, 'x'.repeat(100)] });+ await vi.waitFor(() => expect(delivery.stats()).toMatchObject({ pending: 0, failed: false }), { timeout: 5000 });+ const files = readdirSync(directory);+ expect(files).not.toContain('2000-01-01.log');+ expect(readFileSync(path.join(directory, 'keep.txt'), 'utf8')).toBe('untouched');+ expect(files.some(file => file.endsWith('.old.log'))).toBe(true);+ const current = files.find(file => /^\d{4}-\d{2}-\d{2}\.log$/.test(file))!;+ expect(readFileSync(path.join(directory, current), 'utf8')).toContain('worker-smoke');+ expect(readFileSync(path.join(directory, current), 'utf8')).toContain('record-7');+ await vi.waitFor(() => expect(consoleOutput).toContain('record-7'));+ expect(consoleOutput).toContain('worker-smoke');+ } finally { await delivery.close(); rmSync(directory, { recursive: true, force: true }); }+ });+});diff --git a/test/main/logger-delivery.test.ts b/test/main/logger-delivery.test.tsnew file mode 100644index 00000000..6c5b80de--- /dev/null+++ b/test/main/logger-delivery.test.ts@@ -0,0 +1,63 @@+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import path from 'node:path';+import { expect, it, vi } from 'vitest';++it('main and renderer records bypass direct file/console sinks and snapshot once before the worker', async () => {+ const directory = mkdtempSync(path.join(tmpdir(), 'orkas-main-log-'));+ const workspace = process.env.ORKAS_WORKSPACE_ROOT;+ const level = process.env.ORKAS_LOG_LEVEL;+ const devtools = process.env.ORKAS_DEVTOOLS;+ const originalConsole = { info: console.info, warn: console.warn, error: console.error };+ process.env.ORKAS_WORKSPACE_ROOT = directory;+ process.env.ORKAS_LOG_LEVEL = 'info';+ delete process.env.ORKAS_DEVTOOLS;+ const send = vi.fn(), canAccept = vi.fn(() => true), noteDrop = vi.fn();+ vi.doMock('../../src/main/util/log-delivery', () => ({ createLogDelivery: () => ({ send, canAccept, noteDrop }) }));+ vi.resetModules();+ const { default: electronLog } = await import('electron-log/main');+ const originalFile = electronLog.transports.file;+ const originalTransportConsole = electronLog.transports.console;+ const originalIpcLevel = electronLog.transports.ipc?.level;+ const fileSink: any = Object.assign(vi.fn(() => { throw new Error('disk must not run here'); }), { level: 'silly' });+ const consoleSink: any = Object.assign(vi.fn(), { level: 'silly' });+ electronLog.transports.file = fileSink;+ electronLog.transports.console = consoleSink;+ try {+ const { initLogger, createLogger, logFromRenderer } = await import('../../src/main/logger');+ initLogger();+ const logger = createLogger('business');+ const getter = vi.fn(() => { throw new Error('diagnostic accessor'); });+ const data = { result: 'success', token: 'private-canary', get details() { return getter(); } };+ logger.debug('disabled', data);+ expect(send).not.toHaveBeenCalled();+ logger.info('completed', data);+ data.result = 'later';+ expect(send.mock.calls[0][0]).toMatchObject({ scope: 'business', data: ['completed', { result: 'success', token: '***REDACTED***', details: '[accessor]' }] });+ const error = Object.assign(new Error('failed'), { code: 'ECONNRESET', token: 'private-canary' });+ logger.error('request failed', error);+ expect(structuredClone(send.mock.calls[1][0]).data[1]).toMatchObject({ message: 'failed', code: 'ECONNRESET', token: '***REDACTED***' });+ logFromRenderer({ level: 'warn', module: 'ui', message: 'renderer result' });+ expect(send.mock.calls[2][0].scope).toBe('renderer/ui');+ canAccept.mockReturnValue(false);+ logger.info('saturated', data);+ expect(noteDrop).toHaveBeenCalledOnce();+ expect(send).toHaveBeenCalledTimes(3);+ expect(getter).not.toHaveBeenCalled();+ expect(fileSink).not.toHaveBeenCalled();+ expect(consoleSink).not.toHaveBeenCalled();+ } finally {+ electronLog.errorHandler.stopCatching();+ Object.assign(console, originalConsole);+ electronLog.transports.file = originalFile;+ electronLog.transports.console = originalTransportConsole;+ if (electronLog.transports.ipc) electronLog.transports.ipc.level = originalIpcLevel;+ delete (electronLog.transports as any).background;+ if (workspace === undefined) delete process.env.ORKAS_WORKSPACE_ROOT; else process.env.ORKAS_WORKSPACE_ROOT = workspace;+ if (level === undefined) delete process.env.ORKAS_LOG_LEVEL; else process.env.ORKAS_LOG_LEVEL = level;+ if (devtools === undefined) delete process.env.ORKAS_DEVTOOLS; else process.env.ORKAS_DEVTOOLS = devtools;+ vi.doUnmock('../../src/main/util/log-delivery');+ vi.resetModules();+ rmSync(directory, { recursive: true, force: true });+ }+});diff --git a/test/main/model/core-agent/builtin-agent-tool-surface-fixture.ts b/test/main/model/core-agent/builtin-agent-tool-surface-fixture.tsindex 359304db..576d53b3 100644--- a/test/main/model/core-agent/builtin-agent-tool-surface-fixture.ts+++ b/test/main/model/core-agent/builtin-agent-tool-surface-fixture.ts@@ -200,15 +200,6 @@ export const BUILTIN_AGENT_TOOL_SURFACE_CASES: readonly BuiltinAgentToolSurfaceC'library', 'create_pptx', 'create_artifact', 'generate_image',],},-] as const;--/**- * Reviewed fixed capability decisions for the Resource marketplace Agents.- * These packages are installed into the same platform marketplace root as the- * built-ins, so they must obey the same runtime boundary without relying on a- * Skill being installed to supply native tools.- */-export const RESOURCE_AGENT_TOOL_SURFACE_CASES: readonly BuiltinAgentToolSurfaceCase[] = [{agentId: '1040b336306f',name: 'StockAnalyser',@@ -233,6 +224,15 @@ export const RESOURCE_AGENT_TOOL_SURFACE_CASES: readonly BuiltinAgentToolSurface'library', 'create_pptx', 'create_artifact', 'generate_image',],},+] as const;++/**+ * Reviewed fixed capability decisions for the Resource marketplace Agents.+ * These packages are installed into the same platform marketplace root as the+ * built-ins, so they must obey the same runtime boundary without relying on a+ * Skill being installed to supply native tools.+ */+export const RESOURCE_AGENT_TOOL_SURFACE_CASES: readonly BuiltinAgentToolSurfaceCase[] = [{agentId: '14ba06897645',name: 'StudyTutor',diff --git a/test/main/model/core-agent/chat-history-tools.test.ts b/test/main/model/core-agent/chat-history-tools.test.tsindex b578ac72..e9392a72 100644--- a/test/main/model/core-agent/chat-history-tools.test.ts+++ b/test/main/model/core-agent/chat-history-tools.test.ts@@ -100,6 +100,78 @@ function firstHitCid(content: string): string {return match ? match[1] : '';}+describe('history availability', () => {+ const earlier = { id: 'earlier', from: 'user', text: 'availabilityword earlier fact' };+ const trigger = { id: 'trigger', from: 'user', text: 'current request' };++ it('reports unavailable bodies separately from empty history and recovers after restoration', async () => {+ writeConversation('availability', 'History', [earlier, trigger]);+ const [search, read] = await createChatHistoryActions({+ userId: TEST_UID, currentCid: 'availability', currentMessageId: 'trigger',+ });+ const file = path.join(tmpDir, TEST_UID, 'cloud', 'chats', 'availability.jsonl');+ fs.unlinkSync(file);+ for (const result of [+ await read.execute({ scope: 'all', cid: 'availability', record_id: 'earlier' }, ctxFor()),+ await search.execute({ scope: 'current', query: 'availabilityword' }, ctxFor()),+ ]) {+ expect(result.isError).toBe(true);+ expect(result.content).toContain('source_unavailable');+ expect(result.content).not.toContain('No conversation-history results');+ expect(result.content).not.toContain('has no messages');+ }+ writeConversation('availability', 'History', [earlier, trigger]);+ const restored = await read.execute({ scope: 'current', record_id: 'earlier' }, ctxFor());+ expect(restored.isError).toBeFalsy();+ expect(restored.content).toContain(earlier.text);+ });++ it('rejects an unavailable current boundary in read/search and keeps cross-conversation results explicitly partial', async () => {+ writeConversation('availability', 'History', [earlier]);+ writeConversation('other', 'Other', [{ ...earlier, text: 'availabilityword other fact' }]);+ const [search, read] = await createChatHistoryActions({+ userId: TEST_UID, currentCid: 'availability', currentMessageId: 'trigger',+ });+ for (const result of [+ await read.execute({ scope: 'current' }, ctxFor()),+ await read.execute({ scope: 'all', cid: 'availability' }, ctxFor()),+ await search.execute({ scope: 'current', query: 'availabilityword' }, ctxFor()),+ ]) {+ expect(result.isError).toBe(true);+ expect(result.content).toContain('boundary_unavailable');+ expect(result.content).not.toContain(earlier.text);+ }+ const partial = await search.execute({ scope: 'all', include_current: true, query: 'availabilityword' }, ctxFor());+ expect(partial.content).toContain('boundary_unavailable');+ expect(partial.content).toContain('other fact');+ expect(partial.content).not.toContain('earlier fact');+ expect(partial.content).not.toContain('index_complete=true');+ const excluded = await search.execute({ scope: 'all', include_current: false, query: 'availabilityword' }, ctxFor());+ expect(excluded.content).toContain('index_complete=true');+ expect(excluded.content).not.toContain('boundary_unavailable');+ });++ it('keeps genuine empty history and a first-turn empty scope successful', async () => {+ writeConversation('empty', 'Empty', []);+ writeConversation('first', 'First', [trigger]);+ const [search, read] = await createChatHistoryActions({+ userId: TEST_UID, currentCid: 'first', currentMessageId: 'trigger',+ });+ for (const result of [+ await read.execute({ scope: 'all', cid: 'empty' }, ctxFor()),+ await read.execute({ scope: 'current' }, ctxFor()),+ ]) {+ expect(result.isError).toBeFalsy();+ expect(result.content).toContain('No readable messages in the requested history scope');+ expect(result.content).not.toContain('unavailable');+ }+ const result = await search.execute({ scope: 'current', query: 'current' }, ctxFor());+ expect(result.isError).toBeFalsy();+ expect(result.content).toContain('index_complete=true');+ expect(result.content).toContain('No conversation-history results');+ });+});+describe('chat-history-tools › chat_history(search)', () => {it('finds current group-chat message text and returns cid/msg metadata', async () => {writeConversation('cgroup', 'Planning chat', [diff --git a/test/main/model/core-agent/client-stall.test.ts b/test/main/model/core-agent/client-stall.test.tsindex 26d242c2..a84fb17d 100644--- a/test/main/model/core-agent/client-stall.test.ts+++ b/test/main/model/core-agent/client-stall.test.ts@@ -542,6 +542,29 @@ describe('streamChatWithModel — phase-aware idle watchdog (Phase 1)', () => {expect(result.error).not.toBe('');});+ it('streams a task draft immediately and preserves it when cancelled before phase resolution', async () => {+ const controller = new AbortController();+ h.makeStream = () => (async function* () {+ yield { type: 'text_delta', text: 'Partial task result' };+ await new Promise(() => {});+ })();+ const { streamChatWithModel } = await import('../../../../src/main/model/core-agent/client');+ const events: any[] = [];+ for await (const event of streamChatWithModel({+ userId: 'u', message: 'Complete the task', cid: 'draft-cancel',+ sessionId: 'gconv-draft-cancel', abortSignal: controller.signal, idleTimeout: 0.2,+ })) {+ events.push(event);+ if (event.type === 'delta') controller.abort();+ }+ expect(events).toContainEqual({ type: 'delta', text: 'Partial task result', phase: 'pending' });+ expect(events.filter(e => e.type === 'commentary-finalized'))+ .toEqual([{ type: 'commentary-finalized', text: 'Partial task result' }]);+ expect(events.some(e => e.type === 'final')).toBe(false);+ expect(events.find(e => e.type === 'error')).toMatchObject({ aborted: true });+ expect(h.runStreamCalls).toBe(1);+ });+it('SHORT model-stream window catches a stream that started then stalled (no long wait)', async () => {// Stream emits one delta, then goes silent. After the first text event, the// model-stream phase uses streamIdleTimeout (0.3s), NOT idleTimeout (10s).diff --git a/test/main/model/core-agent/file-tools.test.ts b/test/main/model/core-agent/file-tools.test.tsindex a9c2798a..e58c589d 100644--- a/test/main/model/core-agent/file-tools.test.ts+++ b/test/main/model/core-agent/file-tools.test.ts@@ -175,6 +175,60 @@ function getTool(tools: any[], name: string) {throw new Error(`tool ${name} not found`);}+describe('file-tools › Skill reference validation', () => {+ it.each([false, true])('distinguishes malformed and unavailable refs without changing valid reads (raw=%s)', async (rawText) => {+ const root = path.join(tmpDir, 'ref-fixture');+ fs.mkdirSync(root);+ fs.writeFileSync(path.join(root, 'SKILL.md'), 'protocol sentinel');+ fs.writeFileSync(path.join(root, 'part},{.txt'), 'child sentinel');+ const { bindRuntimeSkillTarget } = await import('../../../../src/main/model/core-agent/skill-registry');+ const bindings = new Map();+ const ref = bindRuntimeSkillTarget({ id: 'ref-fixture', name: 'system:skill-creator.v1_2+test@host', root, entry: path.join(root, 'SKILL.md'), source: 'system' }, bindings);+ expect(ref).toBe('system:skill-creator.v1_2+test@host');+ const { createFileTools } = await import('../../../../src/main/model/core-agent/file-tools');+ const tool = createFileTools({ userId: UID, skillRuntimeBindings: bindings }).find(t => t.name === 'read_files')!;+ const ctx = { workingDir: tmpDir, state: {} } as any;+ for (const requested of [`@skill/${ref}`, `@skill/${ref}/part},{.txt`]) {+ const result = await tool.execute({ paths: [{ path: requested }], raw_text: rawText }, ctx);+ expect(result.isError).toBeFalsy();+ expect(result.content).toContain('sentinel');+ }+ for (const suffix of ['},{', ' extra', '%7D', '\"', '<ref>', '']) {+ const requested = suffix ? `@skill/${ref}${suffix}` : '@skill/';+ const input = { paths: [{ path: requested }], raw_text: rawText };+ const result = await tool.execute(input, ctx);+ expect(result.isError).toBe(true);+ expect(result.content).toContain('E_SKILL_REF_INVALID');+ expect(result.content).not.toContain('sentinel');+ expect(result.observations?.fileFailure).toEqual({ code: 'E_SKILL_REF_INVALID', reason: 'skill_ref_format', stage: 'input', skill_ref_valid: false, item_index: 0 });+ expect(input.paths[0].path).toBe(requested);+ }+ const missing = await tool.execute({ paths: [{ path: '@skill/missing' }], raw_text: rawText }, ctx);+ expect(missing.content).toContain('E_SKILL_NOT_AVAILABLE');+ expect(missing.observations?.fileFailure).toMatchObject({ skill_ref_valid: true, skill_binding_found: false });+ // Partial success still preserves the failed item's classification and index.+ const mixed = await tool.execute({ paths: [{ path: `@skill/${ref}` }, { path: '@skill/skill-creator},{' }], raw_text: rawText }, ctx);+ expect(mixed.isError).toBeFalsy();+ expect(mixed.content).toContain('protocol sentinel');+ expect(mixed.observations?.fileFailure).toMatchObject({ code: 'E_SKILL_REF_INVALID', item_index: 1 });+ });++ it('rejects malformed references consistently at directory, search and source-loading boundaries', async () => {+ const { createFileTools, createProgramSourceLoader } = await import('../../../../src/main/model/core-agent/file-tools');+ const opts = { userId: UID, skillRuntimeBindings: new Map() };+ const tools = createFileTools(opts);+ const ctx = { workingDir: tmpDir, state: {} } as any;+ for (const name of ['list_files', 'search_files', 'grep_files']) {+ const result = await tools.find(t => t.name === name)!.execute({ path: '@skill/skill-creator},{', root: '@skill/skill-creator},{', query: 'sample', pattern: 'sample' }, ctx);+ expect(result.content).toContain('E_SKILL_REF_INVALID');+ expect(result.isError).toBe(true);+ expect(result.observations?.fileFailure).toMatchObject({ skill_ref_valid: false });+ }+ const source = await createProgramSourceLoader(opts)('@skill/skill-creator},{', ctx, 100);+ expect(source).toMatchObject({ status: 'denied', code: 'E_PROGRAM_SOURCE_PATH', reason: expect.stringContaining('E_SKILL_REF_INVALID') });+ });+});+describe('file-tools › run_program source loader', () => {it('loads exact UTF-8 source through the ordinary workspace scope', async () => {const perm = await import('../../../../src/main/features/permissions');@@ -1454,7 +1508,8 @@ describe('file-tools › read_files', () => {it('advertises and executes the same tagged range contract for every batch item', async () => {const { tools, wsDir } = await buildTools();const readFiles = getTool(tools, 'read_files');- const schema = readFiles.inputSchema as any;+ const { toToolDefinition } = await import('../../../../src/core-agent/src/tools');+ const schema = toToolDefinition(readFiles).inputSchema as any;const itemSchema = schema.properties.paths.items;expect(schema.required).toEqual(['paths']);expect(schema.properties.metadata_only.type).toBe('boolean');@@ -1467,10 +1522,15 @@ describe('file-tools › read_files', () => {expect(schema.properties.raw_text.description).not.toContain('run_program only');expect(itemSchema.properties).not.toHaveProperty('charStart');expect(itemSchema.properties).not.toHaveProperty('lineStart');+ expect(itemSchema.properties.path).toMatchObject({ type: 'string', minLength: 1 });expect(itemSchema.properties.range).toMatchObject({type: 'object',additionalProperties: false,required: ['unit', 'start', 'end'],+ properties: {+ start: { type: 'integer', minimum: 0 },+ end: { type: 'integer', minimum: 0 },+ },});const p = path.join(wsDir, 'batch-range.txt');diff --git a/test/main/model/core-agent/local-tools.test.ts b/test/main/model/core-agent/local-tools.test.tsindex b7e966b1..84d2bfc9 100644--- a/test/main/model/core-agent/local-tools.test.ts+++ b/test/main/model/core-agent/local-tools.test.ts@@ -75,9 +75,60 @@ it('keeps Redis service shutdown behind one-time host approval without executing});describe('local-tools › Windows PowerShell compatibility preflight', () => {+ it('authorizes the original chain once before passing it to the shell adapter', async () => {+ await allFilesAuto();+ const core = await import('../../../../src/core-agent/src/tools/builtin');+ const { createLocalTools } = await import('../../../../src/main/model/core-agent/local-tools');+ const execute = vi.spyOn(core.bashTool, 'execute').mockResolvedValue({ content: 'completed' });+ const command = "Write-Output 'first && literal' && Write-Output second || Write-Output recovery";+ try {+ const bash = createLocalTools({ userId: UID, cid: CID, hostPlatform: 'win32' })+ .find(tool => tool.name === 'bash')!;+ const result = await bash.execute({ command }, { workingDir: tmpDir, state: {} } as any);+ expect(result.isError).toBeFalsy();+ expect(execute).toHaveBeenCalledOnce();+ expect(execute.mock.calls[0][0].command).toBe(command);+ } finally { execute.mockRestore(); }+ });++ it.each(['bash', 'process_session', 'interactive_cli'])('keeps conditional removal behind approval: %s', async name => {+ await allFilesApproval();+ const { createLocalTools } = await import('../../../../src/main/model/core-agent/local-tools');+ const bp = await import('../../../../src/main/model/core-agent/bash-permissions');+ const target = path.join(tmpDir, 'protected-chain-target.txt');+ fs.writeFileSync(target, 'keep');+ const command = `Write-Output ready && Remove-Item '${target.replace(/'/g, "''")}'`;+ const requests: any[] = [];+ bp._setBroadcastForTest((_channel, info: any) => {+ requests.push(info);+ bp.respond(info.request_id, 'deny');+ });+ try {+ const tool = createLocalTools({ userId: UID, cid: CID, hostPlatform: 'win32' })+ .find(candidate => candidate.name === name)!;+ const result = await tool.execute({ command, ...(name === 'bash' ? {} : { action: 'start' }) }, {+ workingDir: path.join(tmpDir, 'chain-workspace'), state: {},+ } as any);+ expect(requests).toHaveLength(1);+ expect(result.isError).toBe(true);+ expect(result.content).not.toContain('E_SHELL_SYNTAX_MISMATCH');+ expect(fs.readFileSync(target, 'utf8')).toBe('keep');+ expect(fs.existsSync(path.join(tmpDir, 'chain-workspace'))).toBe(false);+ } finally {+ bp._setBroadcastForTest(null);+ bp._resetForTest();+ }+ });+it('rejects high-confidence POSIX syntax before execution and leaves PowerShell syntax alone', async () => {const { windowsPowerShellCompatibilityError } = await import('../../../../src/main/model/core-agent/local-tools');- expect(windowsPowerShellCompatibilityError('npm install && npm test', 'win32')).toContain('E_SHELL_SYNTAX_MISMATCH');+ expect(windowsPowerShellCompatibilityError('npm install && npm test', 'win32')).toBeNull();+ expect(windowsPowerShellCompatibilityError('npm test || npm run report', 'win32')).toBeNull();+ expect(windowsPowerShellCompatibilityError('echo done && mkdir -p api', 'win32')).toContain('POSIX mkdir -p');+ expect(windowsPowerShellCompatibilityError('npm test && (npm run report)', 'win32')).toContain('unsupported complex');+ expect(windowsPowerShellCompatibilityError('Write-Output "C:\\data\\" "&&"', 'win32')).toBeNull();+ expect(windowsPowerShellCompatibilityError('Write-Output "C:\\data\\" "&&" && Write-Output done', 'win32')).toBeNull();+ expect(windowsPowerShellCompatibilityError('Write-Output `&`&', 'win32')).toBeNull();expect(windowsPowerShellCompatibilityError("cat <<'TEXT'\nnode run-skill.cjs missing-skill probe\nTEXT", 'win32')).toContain('POSIX heredoc');expect(windowsPowerShellCompatibilityError('export TOKEN=x; head -n 3 a.txt > /dev/null', 'win32')).toContain('source/export');expect(windowsPowerShellCompatibilityError('mkdir -p api core/data core/analysis', 'win32')).toContain('POSIX mkdir -p');@@ -95,7 +146,7 @@ describe('local-tools › Windows PowerShell compatibility preflight', () => {const processStart = tools.find((tool) => tool.name === 'process_session')!;const interactive = tools.find((tool) => tool.name === 'interactive_cli')!;const marker = path.join(tmpDir, 'must-not-run.txt');- const command = `node -e "require('fs').writeFileSync('${marker}', 'ran')" && echo done`;+ const command = `node -e "require('fs').writeFileSync('${marker}', 'ran')" && (echo done)`;const ctx = { workingDir: tmpDir, signal: undefined, state: {} } as any;const bashResult = await bash.execute({ command }, ctx);@@ -159,7 +210,7 @@ describe('local-tools › Windows PowerShell compatibility preflight', () => {fs.mkdirSync(cwd, { recursive: true });const result = await bash.execute({- command: 'Write-Output $env:ORKAS_NATIVE_SMOKE',+ command: 'Write-Output $env:ORKAS_NATIVE_SMOKE && Write-Output chain-complete',timeoutMs: 10_000,}, {workingDir: cwd,@@ -169,6 +220,7 @@ describe('local-tools › Windows PowerShell compatibility preflight', () => {expect(result.isError).toBeUndefined();expect(result.content).toContain('<command-result status="succeeded" exit_code="0"');expect(result.content).toContain('Windows-你好');+ expect(result.content).toContain('chain-complete');expect(result.observations?.execution).toMatchObject({ status: 'succeeded', exitCode: 0 });});@@ -739,6 +791,24 @@ describe('local-tools › bash › disabled skills', () => {});describe('local-tools › bash › run-scoped Skill binding', () => {+ it('rejects malformed Skill operands before executing or changing the environment', async () => {+ await allFilesAuto();+ const core = await import('../../../../src/core-agent/src/tools/builtin');+ const execute = vi.spyOn(core.bashTool, 'execute').mockResolvedValue({ content: 'must not execute' });+ try {+ const { createLocalTools } = await import('../../../../src/main/model/core-agent/local-tools');+ const bash = createLocalTools({ userId: UID, skillRuntimeBindings: new Map() }).find(t => t.name === 'bash')!;+ const ctx = { workingDir: tmpDir, state: { sandboxEnv: { SENTINEL: 'unchanged' } } } as any;+ const invalid = await bash.execute({ command: "node run-skill.cjs 'skill-creator},{' probe" }, ctx);+ expect(invalid.content).toContain('E_SKILL_REF_INVALID');+ expect(invalid.observations?.fileFailure).toMatchObject({ skill_ref_valid: false });+ const missing = await bash.execute({ command: 'node run-skill.cjs missing-skill probe' }, ctx);+ expect(missing.content).toContain('E_SKILL_NOT_AVAILABLE');+ expect(missing.observations?.fileFailure).toMatchObject({ skill_ref_valid: true, skill_binding_found: false });+ expect(execute).not.toHaveBeenCalled();+ expect(ctx.state.sandboxEnv).toEqual({ SENTINEL: 'unchanged' });+ } finally { execute.mockRestore(); }+ });it.runIf(process.platform !== 'win32')('finds the runner file without attempting to execute a Skill', async () => {await allFilesAuto();fs.writeFileSync(path.join(tmpDir, 'run-skill.cjs'), 'throw new Error("must not execute");');@@ -2505,10 +2575,10 @@ describe('local-tools › create_artifact › permission mode', () => {const r = await run(tool, { title: 'X', files: MIN_FILES });expect(r.isError).toBeFalsy();expect(r.content).toMatch(/do NOT paste/i);- expect(r.content).toMatch(/interaction smoke passed/i);+ expect(r.content).toMatch(/preview checks passed/i);});- it('rejects and discards a candidate with explicit blockers before firing onArtifactCreated', async () => {+ it('publishes a valid bundle with honest diagnostics even when preview checks find issues', async () => {await allFilesAuto();const created: unknown[] = [];let candidateDir = '';@@ -2531,16 +2601,15 @@ describe('local-tools › create_artifact › permission mode', () => {});const r = await run(tool, { title: 'Static app', files: MIN_FILES });- expect(r.isError).toBe(true);- expect(r.content).toContain('E_ARTIFACT_INTERACTION_INCOMPLETE');- expect(r.content).toContain('Repair the app behavior');+ expect(r.isError).toBeFalsy();+ expect(r.content).toContain('preview reported issues');expect(r.content).toContain('keyboard traversal could not reach the primary action');- expect(created).toEqual([]);+ expect(created).toHaveLength(1);expect(candidateDir).toBeTruthy();- expect(fs.existsSync(candidateDir)).toBe(false);+ expect(fs.existsSync(candidateDir)).toBe(true);});- it('turns blocked remote images into a bounded self-contained recovery path', async () => {+ it('retains unavailable remote resource diagnostics without forcing an offline rewrite', async () => {await allFilesAuto();const tool = await buildCreateArtifactTool({onArtifactCreated: () => {},@@ -2556,16 +2625,21 @@ describe('local-tools › create_artifact › permission mode', () => {});const r = await run(tool, { title: 'Remote image app', files: MIN_FILES });- expect(r.isError).toBe(true);- expect(r.content).toContain('E_ARTIFACT_INTERACTION_INCOMPLETE');- expect(r.content).toMatch(/user explicitly supplied or required an asset/i);- expect(r.content).toMatch(/bundle it under files.*base64/i);- expect(r.content).toMatch(/model-chosen remote assets.*local CSS\/SVG or data\/blob/i);- expect(r.content).toMatch(/Do not retry the same remote URLs/i);+ expect(r.isError).toBeFalsy();+ expect(r.content).toContain('6 images failed to load');+ expect(r.content).not.toContain('Do not retry the same remote URLs');});});describe('local-tools › create_artifact › success + callback', () => {+ it('preserves the application and reports unavailable preview without claiming verification', async () => {+ const created: unknown[]=[];+ const tool=await buildCreateArtifactTool({onArtifactCreated:a=>created.push(a),artifactInteractionSmoke:async()=>{throw new Error('fixture preview unavailable');}});+ const result=await run(tool,{title:'Retained',files:MIN_FILES});+ expect(result.isError).toBeFalsy();expect(created).toHaveLength(1);+ expect(result.content).toContain('Preview unavailable');+ expect(result.content).not.toContain('checks passed');+ });it('publishes an artifact with inconclusive effects and preserves the warning', async () => {await allFilesAuto();const created: unknown[] = [];diff --git a/test/main/model/core-agent/tool-catalog.test.ts b/test/main/model/core-agent/tool-catalog.test.tsindex 37bd123a..27bbc7ae 100644--- a/test/main/model/core-agent/tool-catalog.test.ts+++ b/test/main/model/core-agent/tool-catalog.test.ts@@ -509,10 +509,12 @@ describe('tool-catalog', () => {// by deleting a sentence `retention` already said and by tightening// operational wording. Nothing about credentials, approval, untrusted// content or failing closed was touched.+ // 2026-09-21: read_files advertises its existing nonempty path and+ // nonnegative range bounds; file-tools tests pin the provider definition.expect(fingerprint,'A model-visible field, enum, bound, default, or required rule changed; review it as a schema change, not description cleanup.',- ).toBe('9c25a24b0adb7cdc4a3589c9337980e16ba661573533805f9e7d8da34023caea');+ ).toBe('5a0fc883f32fe8057b10014975c25a3e898316399c2cb17bbcedfbae656b21b0');});it('keeps the reviewed stable tool corpus within the description budgets', () => {@@ -573,8 +575,8 @@ describe('tool-catalog', () => {expect(propertyDescription(outputs, 'paths')).toContain('replaces the prior declaration');const artifact = toolByName('create_artifact');- expect(artifact.description).toMatch(/Remote and out-of-directory URLs are blocked/i);- expect(artifact.description).toMatch(/bundle authorized assets in files or use data\/blob URLs/i);+ expect(artifact.description).toContain('Bundled resources and external HTTP(S) URLs are supported');+ expect(artifact.description).toContain('preview diagnostics');expect(artifact.description).not.toContain('top-level index.html');expect(propertyDescription(artifact, 'files')).toContain('top-level index.html');expect(propertyDescription(artifact, 'files')).toContain('__orkas/bridge.js');diff --git a/test/main/model/event-mapper.test.ts b/test/main/model/event-mapper.test.tsindex 3cce29b3..47e374a1 100644--- a/test/main/model/event-mapper.test.ts+++ b/test/main/model/event-mapper.test.ts@@ -275,6 +275,70 @@ it('passes provider-native assistant phases through unchanged', async () => {]);});+it('delivers task draft text before a stalled provider reaches a phase boundary', async () => {+ let release!: () => void;+ const gate = new Promise<void>(resolve => { release = resolve; });+ const source = (async function* () {+ yield { type: 'text_delta', text: 'First chunk' };+ await gate;+ yield { type: 'provider_call', outcome: 'completed', stopReason: 'end_turn' };+ yield { type: 'done', result: { text: 'First chunk', meta: { error: null } } };+ })();+ const output = mapCoreAgentEvents(source as any, { failureTrackingScope: {}, streamUnphasedText: true });+ // The first next() must settle while the provider is still blocked. A+ // bounded race makes the old buffering implementation fail without hanging.+ let timer: ReturnType<typeof setTimeout> | undefined;+ let first: any;+ try {+ first = await Promise.race([output.next(), new Promise(resolve => {+ timer = setTimeout(() => resolve(null), 100);+ })]);+ } finally {+ if (timer) clearTimeout(timer);+ release();+ }+ const rest: any[] = [];+ for await (const event of output) rest.push(event);+ expect(first?.value).toEqual({ type: 'delta', text: 'First chunk', phase: 'pending' });+ expect(rest).toEqual([{ type: 'final', text: 'First chunk' }]);+});++it('moves successive task drafts into commentary once and never replays final deltas', async () => {+ const out = await collect([+ { type: 'text_delta', text: 'Inspect ' },+ { type: 'text_delta', text: 'sources.' },+ { type: 'tool_delta', id: 'read-1', name: 'read_files', inputDelta: '{}' },+ { type: 'provider_call', durationMs: 1, outcome: 'completed', stopReason: 'tool_use', model: 'test' },+ { type: 'text_delta', text: 'Check results.' },+ { type: 'provider_call', durationMs: 1, outcome: 'completed', stopReason: 'tool_use', model: 'test' },+ { type: 'text_delta', text: 'Delivered.' },+ { type: 'text_phase', phase: 'final_answer' },+ { type: 'done', result: { text: 'Delivered.', meta: { error: null } } },+ ], { streamUnphasedText: true });+ expect(out.filter(e => e.type === 'delta').map(e => e.text).join(''))+ .toBe('Inspect sources.Check results.Delivered.');+ expect(out.filter(e => e.type === 'commentary-finalized')).toEqual([+ { type: 'commentary-finalized', text: 'Inspect sources.' },+ { type: 'commentary-finalized', text: 'Check results.' },+ ]);+ expect(out.filter(e => e.type === 'final')).toEqual([{ type: 'final', text: 'Delivered.' }]);+});++it.each(['error', 'incomplete'] as const)('retains visible task draft on %s without inventing success', async ending => {+ const out = await collect([+ { type: 'text_delta', text: 'Partial work' },+ ...(ending === 'error' ? [{ type: 'done' as const, result: {+ text: '', meta: { error: { kind: 'timeout' as const, message: 'timed out' } },+ } }] : []),+ ], { streamUnphasedText: true });+ expect(out[0]).toEqual({ type: 'delta', text: 'Partial work', phase: 'pending' });+ expect(out.filter(e => e.type === 'commentary-finalized')).toEqual([+ { type: 'commentary-finalized', text: 'Partial work' },+ ]);+ expect(out.some(e => e.type === 'final')).toBe(false);+ expect(out.some(e => e.type === 'error')).toBe(true);+});+it('does not fabricate process prose when a tool round contains no assistant text', async () => {const out = await collect([{ type: 'tool_start', name: 'lookup', id: 'lookup-1', input: {} },diff --git a/test/main/model/runner.test.ts b/test/main/model/runner.test.tsindex 314ef035..f0500239 100644--- a/test/main/model/runner.test.ts+++ b/test/main/model/runner.test.ts@@ -3,6 +3,7 @@ import { createHash } from 'node:crypto';import * as fs from 'node:fs';import * as os from 'node:os';import * as path from 'node:path';+import { captureMainLogWorkers } from '../../helpers/capture-main-log-workers';import type { AgentSummary } from '../../../src/main/features/agents';import { TOOL_CATALOG_REVISION } from '../../../src/main/model/core-agent/tool-catalog-revision';@@ -15,6 +16,7 @@ let tmpDir: string;let prevWs: string | undefined;let prevAnthropicKey: string | undefined;let prevToolLoadingMode: string | undefined;+let closeLogWorkers: () => Promise<void>;const CONTENT_WRITER_AGENT_ID = '173d4235a431';const CITATION_VERIFY_AGENT_IDS = [@@ -27,7 +29,7 @@ const OFFICE_WORKER_AGENT_ID = 'a19101ba698a';const IMAGE_STUDIO_AGENT_ID = '814b61b027f0';const VIDEO_STUDIO_AGENT_ID = '79df9cc89f5f';-beforeEach(() => {+beforeEach(async () => {tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-runner-'));prevWs = process.env.ORKAS_WORKSPACE_ROOT;prevAnthropicKey = process.env.ANTHROPIC_API_KEY;@@ -35,9 +37,11 @@ beforeEach(() => {process.env.ORKAS_WORKSPACE_ROOT = tmpDir;delete process.env.ANTHROPIC_API_KEY;vi.resetModules();+ closeLogWorkers = await captureMainLogWorkers();});-afterEach(() => {+afterEach(async () => {+ await closeLogWorkers();vi.doUnmock('@earendil-works/pi-ai/oauth');vi.doUnmock('#core-agent');vi.doUnmock('../../../src/main/model/core-agent/video-studio-tool');@@ -3234,3 +3238,60 @@ describe('runner › isolated memory consolidation', () => {expect(complete).toHaveBeenCalledTimes(1);});});++describe('runner › bounded Skill creation correction', () => {+ async function setup(result: any) {+ const complete = vi.fn(async () => {+ if (result instanceof Error) throw result;+ return result;+ });+ vi.doMock('#core-agent', async () => ({+ ...(await vi.importActual<any>('#core-agent')),+ createPiProvider: () => ({ id: 'openrouter', name: 'test', complete, validateAuth: async () => true }),+ }));+ const users = await import('../../../src/main/features/users');+ const auth = await import('../../../src/main/features/auth');+ users.activateUser('skill-correction-owner');+ const profile = await auth.addApiKey('openrouter', 'sk-or-correction-fixture', 'Fixture');+ await auth.addEntry({ provider: 'openrouter', model: 'future-lab/test-model', profileId: profile.profileId });+ const { completeSkillCreationCorrection } = await loadRunner();+ const input = { userId: 'skill-correction-owner', cid: 'correction-task', turnId: 'correction-turn',+ message: 'Repair only the supplied proposal.', signal: new AbortController().signal };+ return { complete, run: completeSkillCreationCorrection, input };+ }++ it('offers no tools or task session, retains billing attribution and charges the task token backstop', async () => {+ const { complete, run, input } = await setup({+ content: [{ type: 'text', text: '<skill>corrected</skill>' }], stopReason: 'end_turn',+ usage: { inputTokens: 20, outputTokens: 10 }, model: 'test-model',+ });+ const meter = await import('../../../src/main/util/conversation-cost-meter');+ meter.resetTaskTokens(input.cid);+ expect(await run(input)).toBe('<skill>corrected</skill>');+ expect(complete).toHaveBeenCalledTimes(1);+ const request = complete.mock.calls[0][0] as any;+ expect(request.tools).toEqual([]);+ expect(request.maxTokens).toBe(8192);+ expect(request.messages).toEqual([{ role: 'user', content: [{ type: 'text', text: input.message }] }]);+ expect(request.systemPrompt).toBeUndefined();+ expect(request.requestMetadata.creditContext).toEqual({ conversationId: input.cid, turnId: input.turnId });+ expect(meter.taskTokens(input.cid)).toBe(30);+ await expect(run({ ...input, userId: 'another-account' })).rejects.toThrow('cancelled');+ await expect(run({ ...input, message: 'x'.repeat(64_001) })).rejects.toThrow('input limit');+ const controller = new AbortController(); controller.abort();+ await expect(run({ ...input, signal: controller.signal })).rejects.toThrow('cancelled');+ expect(complete).toHaveBeenCalledTimes(1);+ });++ it.each([+ ['truncated', { content: [{ type: 'text', text: '<skill>partial</skill>' }], stopReason: 'max_tokens' }],+ ['tool request', { content: [{ type: 'tool_use', id: 't', name: 'bash', input: {} }], stopReason: 'tool_use' }],+ ['oversized', { content: [{ type: 'text', text: 'x'.repeat(64_001) }], stopReason: 'end_turn' }],+ ['empty', { content: [], stopReason: 'end_turn' }],+ ['network failure', Object.assign(new Error('network fixture'), { code: 'ECONNRESET' })],+ ])('rejects %s without another generation attempt', async (_, result) => {+ const { complete, run, input } = await setup(result);+ await expect(run(input)).rejects.toThrow();+ expect(complete).toHaveBeenCalledTimes(1);+ });+});diff --git a/test/main/model/skill-registry.test.ts b/test/main/model/skill-registry.test.tsindex 2ed342e0..212e4863 100644--- a/test/main/model/skill-registry.test.ts+++ b/test/main/model/skill-registry.test.ts@@ -5,6 +5,12 @@ import * as os from 'node:os';import * as path from 'node:path';import { SKILL_ROSTER_MAX_TOKENS } from '../../../src/main/util/skill-description-policy';+// These registry cases own filesystem fixtures, not asynchronous log delivery.+// Keep the logger worker from recreating their root during teardown.+vi.mock('../../../src/main/logger', () => ({+ createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),+}));+// skill-registry.ts wraps core-agent's SkillLoader. To test allowlist// filtering without pulling in the real core-agent import, we write fake// SKILL.md files under the new skills layout and let the real SkillLoaderdiff --git a/test/main/model/web-app-inference.test.ts b/test/main/model/web-app-inference.test.tsindex 843959ce..340b3f88 100644--- a/test/main/model/web-app-inference.test.ts+++ b/test/main/model/web-app-inference.test.ts@@ -31,6 +31,16 @@ describe('Web app governed pure inference',()=>{expect(requests[0].systemPrompt).toBeUndefined();expect(requests[0].sessionId).toBeUndefined();expect(progress).toHaveBeenCalledWith({type:'delta',text:'Hello'});});+ it('passes long input and caller output budgets through the existing model path', async () => {+ const {generateWebAppText}=await import('../../../src/main/model/core-agent/runner');+ const prompt='a'.repeat(40000), output='b'.repeat(270000);+ events=[{type:'text_delta',text:output},{type:'message_end',stopReason:'end_turn'}];+ expect(await generateWebAppText('app-inference-owner',{prompt,maxTokens:8192},new AbortController().signal,()=>{})).toMatchObject({text:output});+ expect(requests[0]).toMatchObject({maxTokens:8192,messages:[{role:'user',content:[{type:'text',text:prompt}]}]});+ await generateWebAppText('app-inference-owner',{prompt:'Default'},new AbortController().signal,()=>{});+ expect(requests[1].maxTokens).not.toBe(1024);+ expect(requests[1].maxTokens).not.toBe(4096);+ });it('does not retry a failed generation or rotate to spend through another configuration',async()=>{failing=true;const {generateWebAppText}=await import('../../../src/main/model/core-agent/runner');await expect(generateWebAppText('app-inference-owner',{prompt:'Greeting',maxTokens:128},new AbortController().signal,()=>{})).rejects.toThrow();expect(requests).toHaveLength(1);diff --git a/test/main/quality/agent-prompt-surface-budget.test.ts b/test/main/quality/agent-prompt-surface-budget.test.tsindex 02d3d082..5361d46d 100644--- a/test/main/quality/agent-prompt-surface-budget.test.ts+++ b/test/main/quality/agent-prompt-surface-budget.test.ts@@ -87,15 +87,16 @@ const RECORDED_CEILING: Readonly<Record<string, number>> = {DeepResearcher: 2_382,OfficeWorker: 2_219,ContentWriter: 1_912,+ StockAnalyser: 4_725,};-/** The corpus total matters on its own: ten agents each creeping under their+/** The corpus total matters on its own: eleven agents each creeping under their* own ceiling is still a shared regression, because a user with several* agents in one group chat pays for all of them. */// 2026-09-15: routing descriptions lost 2,224 characters of duplicated prose;// lower the existing ceilings by that delta, preserving their prior headroom.// 2026-09-17: ProductDeveloper delegates execution procedures to product-dev.-const RECORDED_CORPUS_CEILING = 40_701;+const RECORDED_CORPUS_CEILING = 45_379;interface AgentSurface {name: string;diff --git a/test/main/quality/agent-standards-budget.test.ts b/test/main/quality/agent-standards-budget.test.tsindex b9329ceb..4cdc2239 100644--- a/test/main/quality/agent-standards-budget.test.ts+++ b/test/main/quality/agent-standards-budget.test.ts@@ -71,8 +71,7 @@ describe('built-in Agent runtime guidance contract', () => {const rows = measureAll();it('finds the complete built-in Agent corpus', () => {- // StockAnalyser remains a Resource Agent, outside the PC built-in bundle.- expect(rows).toHaveLength(10);+ expect(rows).toHaveLength(11);});it('keeps knowhow and standards at five items or fewer', () => {diff --git a/test/main/quality/tool-surface-budget.test.ts b/test/main/quality/tool-surface-budget.test.tsindex 4e335526..9ffb59f7 100644--- a/test/main/quality/tool-surface-budget.test.ts+++ b/test/main/quality/tool-surface-budget.test.ts@@ -69,6 +69,7 @@ const BUILTIN_AGENT_INITIAL_SURFACE_BUDGETS: Readonly<Record<ProductDeveloper: { tools: 25, tokens: 8_800 },UIDesigner: { tools: 25, tokens: 8_800 },SeoGeoAgent: { tools: 24, tokens: 8_500 },+ StockAnalyser: { tools: 22, tokens: 7_300 },});describe('injected tool surface budget', () => {diff --git a/test/main/quality/web-app-sdk-builtin.test.ts b/test/main/quality/web-app-sdk-builtin.test.tsindex e1e196a8..51dd235f 100644--- a/test/main/quality/web-app-sdk-builtin.test.ts+++ b/test/main/quality/web-app-sdk-builtin.test.ts@@ -16,6 +16,8 @@ describe('Web app authoring package', () => {const body = fs.readFileSync(path.join(skill, 'SKILL.md'), 'utf8');const links = [...body.matchAll(/\]\(([^)]+)\)/g)].map(match => match[1]);expect(links.length).toBeGreaterThan(0);+ expect(body).toContain('200 MiB');+ expect(body).not.toMatch(/four concurrent|two host-wide|256 storage keys|512 KiB|16 selected files|five-minute/);for (const link of links) expect(fs.statSync(path.join(skill, link)).isFile()).toBe(true);const manifest = JSON.parse(fs.readFileSync(path.join(skill, 'assets/starter/orkas-app.json'), 'utf8'));expect(manifestSchema.safeParse(manifest).success).toBe(true);diff --git a/test/main/util/builtin-resource-gate.test.ts b/test/main/util/builtin-resource-gate.test.tsindex 64fa92a8..4ce649bb 100644--- a/test/main/util/builtin-resource-gate.test.ts+++ b/test/main/util/builtin-resource-gate.test.ts@@ -67,9 +67,29 @@ describe('builtin-resource-gate', () => {.toEqual([...gate.REQUIRED_BUILTIN_INVENTORY.marketplace_agents].sort());expect(manifest.inventory.marketplace_skills.map((row) => row.id).sort()).toEqual([...gate.REQUIRED_BUILTIN_INVENTORY.marketplace_skills].sort());- // Normal Resource development does not move an Agent into the PC bundle.expect(manifest.inventory.marketplace_agents)- .not.toContainEqual(expect.objectContaining({ id: '1040b336306f' }));+ .toContainEqual(expect.objectContaining({+ id: '1040b336306f',+ name: 'StockAnalyser',+ icon: 'chart',+ color: 'mint',+ skill_list: [+ 'investment-report',+ 'market-data',+ 'portfolio-risk',+ 'quant-lab',+ 'security-research',+ 'trade-control',+ ],+ embedded_skills: [+ 'investment-report',+ 'market-data',+ 'portfolio-risk',+ 'quant-lab',+ 'security-research',+ 'trade-control',+ ],+ }));expect(manifest.inventory.marketplace_agents).toContainEqual(expect.objectContaining({id: '173d4235a431',diff --git a/test/main/util/chat-media-protocol.test.ts b/test/main/util/chat-media-protocol.test.tsindex 4480f17d..2b5db61b 100644--- a/test/main/util/chat-media-protocol.test.ts+++ b/test/main/util/chat-media-protocol.test.ts@@ -31,7 +31,7 @@ beforeEach(async () => {log: { warn: (...args: unknown[]) => logs.push(args), info: () => {} },_mediaKindForContentType: () => 'image', _protocolServeFailureCode: () => 'serve_failed',serveFileRange: () => new Response('fixture-image'),- withOfflineHtmlPreviewPolicy: (response: Response) => response,+ withHtmlPreviewPolicy: (response: Response) => response,});});afterEach(() => {diff --git a/test/main/util/text-preview.test.ts b/test/main/util/text-preview.test.tsnew file mode 100644index 00000000..72dac321--- /dev/null+++ b/test/main/util/text-preview.test.ts@@ -0,0 +1,28 @@+import * as fs from 'node:fs';+import * as os from 'node:os';+import * as path from 'node:path';+import { afterEach, describe, expect, it } from 'vitest';+import { readTextPreview, TEXT_PREVIEW_BYTES } from '../../../src/main/util/text-preview';++const dirs: string[] = [];+afterEach(() => { for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); });+function file(content: string) {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-text-preview-'));+ dirs.push(dir);+ const name = path.join(dir, 'data.csv');+ fs.writeFileSync(name, content);+ return name;+}+describe('bounded text preview', () => {+ it('strips only the leading BOM and preserves complete data at the limit', () => {+ expect(readTextPreview(file('\uFEFFid,value\n001,中文'))).toEqual({ text: 'id,value\n001,中文', truncated: false });+ const content = 'a'.repeat(TEXT_PREVIEW_BYTES);+ expect(readTextPreview(file(content))).toEqual({ text: content, truncated: false });+ });+ it('reports truncation without leaking a replacement character at a UTF-8 boundary', () => {+ const prefix = 'a'.repeat(TEXT_PREVIEW_BYTES - 1);+ const name = file(prefix + '中文\n');+ expect(readTextPreview(name)).toEqual({ text: prefix, truncated: true });+ expect(fs.readFileSync(name, 'utf8')).toBe(prefix + '中文\n');+ });+});diff --git a/test/main/util/window-security.test.ts b/test/main/util/window-security.test.tsindex f2f24604..3e7b98fe 100644--- a/test/main/util/window-security.test.ts+++ b/test/main/util/window-security.test.ts@@ -3,13 +3,13 @@ import { describe, expect, it, vi } from 'vitest';import {hardenedWebPreferences,installExternalNavigationGuard,- installOfflineHtmlPreviewNavigationGuard,- isOfflineHtmlPreviewUrl,- OFFLINE_HTML_PREVIEW_CSP,+ installHtmlPreviewNavigationGuard,+ isHtmlPreviewUrl,+ HTML_PREVIEW_CSP,safeExternalHttpUrl,safeExternalUserActionUrl,safeLocalCliAuthUrl,- withOfflineHtmlPreviewPolicy,+ withHtmlPreviewPolicy,} from '../../../src/main/util/window-security';describe('window security baseline', () => {@@ -168,8 +168,8 @@ describe('window security baseline', () => {});});-describe('offline local HTML preview policy', () => {- it('preserves the streamed response while allowing inline code and denying network egress', async () => {+describe('local HTML preview policy', () => {+ it('preserves the streamed response while allowing Web resources and preserving response metadata', async () => {const original = new Response('preview body', {status: 206,statusText: 'Partial Content',@@ -181,7 +181,7 @@ describe('offline local HTML preview policy', () => {},});- const secured = withOfflineHtmlPreviewPolicy(original);+ const secured = withHtmlPreviewPolicy(original);expect(secured.status).toBe(206);expect(secured.statusText).toBe('Partial Content');@@ -189,11 +189,11 @@ describe('offline local HTML preview policy', () => {expect(secured.headers.get('Cache-Control')).toBe('no-cache');expect(secured.headers.get('Content-Range')).toBe('bytes 0-11/12');expect(secured.headers.get('Content-Type')).toBe('text/html');- expect(secured.headers.get('Content-Security-Policy')).toBe(OFFLINE_HTML_PREVIEW_CSP);- expect(OFFLINE_HTML_PREVIEW_CSP).toContain("script-src 'unsafe-inline'");- expect(OFFLINE_HTML_PREVIEW_CSP).toContain("connect-src 'none'");- expect(OFFLINE_HTML_PREVIEW_CSP).toContain('img-src chat-media://local data: blob:');- expect(OFFLINE_HTML_PREVIEW_CSP).toContain("frame-src 'none'");+ expect(secured.headers.get('Content-Security-Policy')).toBe(HTML_PREVIEW_CSP);+ expect(HTML_PREVIEW_CSP).toContain("script-src chat-media://local http: https: 'unsafe-inline'");+ expect(HTML_PREVIEW_CSP).toContain('connect-src chat-media://local http: https: ws: wss:');+ expect(HTML_PREVIEW_CSP).toContain('img-src chat-media://local http: https: data: blob:');+ expect(HTML_PREVIEW_CSP).toContain('frame-src chat-media://local http: https:');expect(secured.headers.get('Referrer-Policy')).toBe('no-referrer');expect(secured.headers.get('X-Content-Type-Options')).toBe('nosniff');expect(await secured.text()).toBe('preview body');@@ -204,7 +204,7 @@ describe('offline local HTML preview policy', () => {'chat-media://local/C:/Users/test/preview.HTM?v=2#slide','chat-media://local/Users/test/preview%2Ehtml',])('recognizes the validated local HTML route: %s', (url) => {- expect(isOfflineHtmlPreviewUrl(url)).toBe(true);+ expect(isHtmlPreviewUrl(url)).toBe(true);});it.each([@@ -215,10 +215,10 @@ describe('offline local HTML preview policy', () => {'data:text/html,preview','chat-media://local/Users/test/%E0%A4%A.html',])('rejects route look-alikes: %s', (url) => {- expect(isOfflineHtmlPreviewUrl(url)).toBe(false);+ expect(isHtmlPreviewUrl(url)).toBe(false);});- it('blocks an HTML subframe from navigating off the local protocol', () => {+ it('allows HTTP navigation while blocking privileged document schemes', () => {let navigate!: (event: {preventDefault(): void;url: string;@@ -226,7 +226,7 @@ describe('offline local HTML preview policy', () => {frame?: { url: string } | null;initiator?: { url: string } | null;}) => void;- installOfflineHtmlPreviewNavigationGuard({+ installHtmlPreviewNavigationGuard({on: (_event, handler) => { navigate = handler; },});@@ -237,7 +237,7 @@ describe('offline local HTML preview policy', () => {frame: { url: 'chat-media://local/Users/test/preview.html' },};navigate(remote);- expect(remote.preventDefault).toHaveBeenCalledOnce();+ expect(remote.preventDefault).not.toHaveBeenCalled();const fetchLikeDataNavigation = {preventDefault: vi.fn(),@@ -257,7 +257,7 @@ describe('offline local HTML preview policy', () => {frame?: { url: string } | null;initiator?: { url: string } | null;}) => void;- installOfflineHtmlPreviewNavigationGuard({+ installHtmlPreviewNavigationGuard({on: (_event, handler) => { navigate = handler; },});diff --git a/test/renderer/artifact-security.test.ts b/test/renderer/artifact-security.test.tsindex 6ee618f0..cbc600da 100644--- a/test/renderer/artifact-security.test.ts+++ b/test/renderer/artifact-security.test.ts@@ -27,14 +27,14 @@ describe('artifact iframe security boundary', () => {expect(savedSource).toContain('artifactSecurity.trustedArtifactMessage');});- it('does not grant popup or top-navigation capabilities', () => {+ it('allows ordinary browser actions without top-level navigation', () => {const security = loadSecurity();expect(security.SANDBOX.split(/\s+/)).toEqual(['allow-scripts','allow-same-origin',- 'allow-forms',+ 'allow-forms', 'allow-downloads', 'allow-popups', 'allow-modals',]);- expect(security.SANDBOX).not.toMatch(/allow-popups|allow-top-navigation/);+ expect(security.SANDBOX).not.toMatch(/allow-popups-to-escape-sandbox|allow-top-navigation/);});it('accepts only credential-free absolute HTTP(S) external URLs', () => {diff --git a/test/renderer/conversation-failed-retry.test.ts b/test/renderer/conversation-failed-retry.test.tsindex b387174c..86cc1ca5 100644--- a/test/renderer/conversation-failed-retry.test.ts+++ b/test/renderer/conversation-failed-retry.test.ts@@ -74,23 +74,6 @@ function loadFailedRetrySender() {};}-function loadModelOutputTracker() {- const source = [- extractFunction('_normalizeFeedbackFieldText'),- extractFunction('_trimTelemetryText'),- extractFunction('_handleModelOutputErrorForUi'),- ].join('\n');- return vm.runInNewContext(`- const currentCid = 'fallback-cid';- const calls = [];- function _convTrackError(action, data) { calls.push({ action, data }); }- function _groupActorLabel(actorId) { return actorId === 'commander' ? 'Commander' : ''; }- const window = {};- ${source}- ({ track: _handleModelOutputErrorForUi, calls });- `, {});-}-describe('conversation failed assistant retry actions', () => {it('offers retry from recorded failure state even without error prose, but not for successful empty deliveries', () => {const isFailed = loadFailedClassifier();@@ -231,7 +214,6 @@ describe('conversation failed assistant retry actions', () => {expect(finalizeBody).toContain('archive: archive && !failedAssistant && !interruptedAssistant');expect(finalizeBody).toContain('_attachFailedAssistantActions(ph, () => _messageTextForActions(ph, text));');expect(finalizeBody).toContain('_attachInterruptedAssistantActions(ph, () => _messageTextForActions(ph, text), { archive });');- expect(finalizeBody).toContain("failure_kind: String(gm.failure_kind || '')");const failedActionsBody = extractFunction('_attachFailedAssistantActions');expect(failedActionsBody).toContain("msgDiv.dataset.failed = '1';");@@ -253,21 +235,9 @@ describe('conversation failed assistant retry actions', () => {});it('does not send model output error telemetry in the open build', () => {- const { track, calls } = loadModelOutputTracker();- const msgDiv = {- dataset: {- msgId: 'm123',- turnId: 'turn-1',- fromActor: 'commander',- },- };- const longError = `Model call failed: ${'x'.repeat(900)}`;-- track('cid-1', msgDiv, longError, { stage: 'stream_event' });- track('cid-1', msgDiv, longError, { stage: 'stream_event' });- track('cid-1', msgDiv, 'aborted', { aborted: true });-- expect(calls).toHaveLength(0);+ expect(source).not.toContain('_handleModelOutputErrorForUi');+ expect(source).not.toContain('_trimTelemetryText');+ expect(source).not.toContain("_convTrackError('model_output_error'");});});diff --git a/test/renderer/conversation-keyed-rendering.test.ts b/test/renderer/conversation-keyed-rendering.test.tsindex 15dd1012..3c6a305e 100644--- a/test/renderer/conversation-keyed-rendering.test.ts+++ b/test/renderer/conversation-keyed-rendering.test.ts@@ -53,6 +53,14 @@ class FakeNode {class FakeContainer {rows: FakeNode[] = [];+ dataset: Record<string, string> = {};+ classList = { remove() {} };+ style = { removeProperty() {} };++ set innerHTML(_value: string) {+ for (const row of this.rows) row.parentElement = null;+ this.rows = [];+ }appendChild(node: FakeNode) {node.parentElement = this as any;@@ -87,6 +95,7 @@ class FakeContainer {const msgId = selector.match(/data-msg-id="([^"]+)"/)?.[1];const actor = selector.match(/data-from-actor="([^"]+)"/)?.[1];const wantsNoMsgId = selector.includes(':not([data-msg-id])');+ if (!selector.startsWith('.chat-message')) return [];return this.rows.filter((row) => {if (exactKey && row.dataset.renderKey !== exactKey) return false;if (prefixKey && !(row.dataset.renderKey || '').startsWith(prefixKey)) return false;@@ -156,11 +165,11 @@ function loadRenderer(cid: string) {container.appendChild(node);return node;};- appendChatMessage = function(legacy) {+ appendChatMessage = function(legacy, _scroll, opts = {}) {// Mirrors production's dedupe entry: identity first, create only when the// record has no row yet. Uses the REAL matcher so these tests exercise// render-key identity rather than a simplified stand-in.- var existing = _findRenderedGroupMessage(__container, legacy);+ var existing = opts.historyHydration ? null : _findRenderedGroupMessage(__container, legacy);if (existing) {_syncRenderedGroupMessageIdentity(existing, legacy);return existing;@@ -172,6 +181,8 @@ function loadRenderer(cid: string) {if (legacy._from) node.dataset.fromActor = String(legacy._from);// Production sets this too; cross-page absorb finds a turn's bubble by it.if (legacy._turn_id) node.dataset.turnId = String(legacy._turn_id);+ node.bodies.final.textContent = legacy.content || '';+ node.message = legacy;// Mirror production's narration hydration (conversation.js, assistant// branch) so a merged record's earlier segments reach the row.if (legacy._narration && legacy._narration.length) {@@ -180,7 +191,7 @@ function loadRenderer(cid: string) {}_applyNarrationFold(node);}- __container.appendChild(node);+ (opts.container || __container).appendChild(node);return node;};_finalizeActorPlaceholder = function(node, gm) {@@ -247,6 +258,81 @@ describe('keyed rendering › reconnect history', () => {});});+describe('history refresh overlapping a Codex handback', () => {+ it.each([+ { included: false, superseded: false },+ { included: true, superseded: false },+ { included: true, superseded: true },+ ])('keeps the completed reply through stale active history and stream cleanup ($included, $superseded)', async ({ included, superseded }) => {+ const { context, container } = loadRenderer(CID);+ const warnings: unknown[] = [];+ context.__warnings = warnings;+ vm.runInContext('_convLog.warn = (...args) => __warnings.push(args)', context);+ context.performance = performance;+ context.convAgentEnabledByCid = new Map();+ context.pollMsgCounts = new Map();+ context._agentsCache = [];+ context.document.createDocumentFragment = () => new FakeContainer();+ const append = container.appendChild.bind(container);+ container.appendChild = (node: any) => {+ if (node instanceof FakeContainer) {+ for (const row of [...node.rows]) append(row);+ return node;+ }+ return append(node);+ };+ for (const name of ['_setChatScrollOffset', '_ensureCreateAgentInlineObserver',+ '_ensureConvCreateAgentInline', '_syncFailedFromHistory',+ '_setLoadEarlierHistory', '_scheduleConversationTurnNavigation', '_renderConvDisabledBanner',+ '_scrollToBottomNoAnim', '_observeConversationRunFromPlanAction', '_startRuntimeActorRecovery']) {+ context[name] = () => {};+ }+ let resolveHistory!: (value: any) => void;+ context.apiFetch = () => new Promise(resolve => { resolveHistory = resolve; });+ const loading = context.loadConversationHistory(CID);+ const reply = { id: 'codex-result', from: 'codex-agent', to: ['user'], turn_id: TURN,+ seg: 0, text: 'Changes saved; Commander will update the automation.',+ ts: new Date().toISOString(), produced: ['result.md'] };+ context._handleGroupBusEvent(CID, null, { type: 'message', turn_end: true, msg: reply });+ const finishOlder = resolveHistory;+ const replacement = superseded ? context.loadConversationHistory(CID) : null;+ // The history request began before Codex settled; its snapshot still lists+ // that turn as active even though the terminal event has already arrived.+ resolveHistory({ json: async () => ({ ok: true, history: included ? [reply] : [],+ conversation: { processing: true, processing_since: new Date().toISOString() },+ live_display: { sequence: 1, active_turns: [{ actor: reply.from, turn_id: TURN },+ { actor: 'commander', turn_id: 'commander-turn' }],+ turns: [{ actor: reply.from, turn_id: TURN,+ records: [{ ...reply, id: '', text: 'Working...', produced: [] }] }] },+ }) });+ if (replacement) {+ await replacement;+ finishOlder({ json: async () => ({ ok: true, history: [] }) });+ }+ await loading;+ expect(warnings.filter(args => String(args).includes('history load failed'))).toEqual([]);+ const codex = container.rows.find(row => row.dataset.msgId === reply.id);+ expect(codex, 'history refresh must retain the canonical Codex reply').toBeDefined();+ expect(codex!.bodies.final.textContent).toBe(reply.text);+ expect((codex as any).message.produced).toEqual(['result.md']);+ context._handleGroupBusEvent(CID, null, { ...delta(0, 'Stale Codex delta', reply.from), display_seq: 2 });+ context._handleGroupBusEvent(CID, null, {+ type: 'process', actor: 'commander', turn_id: 'commander-turn', seg: 0,+ data: { type: 'delta', text: 'Updating automation...' },+ });+ expect(container.rows.find(row => row.dataset.fromActor === 'commander')?.bodies.final.textContent)+ .toBe('Updating automation...');+ expect(codex!.bodies.final.textContent).toBe(reply.text);+ context._handleGroupBusEvent(CID, null, {+ type: 'message', turn_end: true,+ msg: { id: 'commander-result', from: 'commander', turn_id: 'commander-turn', seg: 0,+ text: 'Automation updated.', ts: new Date().toISOString() },+ });+ context._settleDanglingActorPlaceholders(CID);+ expect(container.rows.map(row => row.dataset.msgId)).toEqual(['codex-result', 'commander-result']);+ });+});+function delta(seg: number, text: string, actor = 'commander') {return { type: 'process', cid: CID, actor, turn_id: TURN, seg, data: { type: 'delta', text } };}diff --git a/test/renderer/conversation-sidebar.test.ts b/test/renderer/conversation-sidebar.test.tsindex 007bd384..cba6d5d5 100644--- a/test/renderer/conversation-sidebar.test.ts+++ b/test/renderer/conversation-sidebar.test.ts@@ -6126,6 +6126,54 @@ describe('conversation process metadata formatting', () => {expect(body.children[2].innerHTML).not.toContain('stream-process-icon');});+ it('paints draft text before phase resolution and moves each round into process only once', async () => {+ const context = loadConversationRenderer();+ // This scenario exercises cancellation of a queued preview paint. Match+ // the browser's paired request/cancel API, absent from the minimal shim.+ context.requestAnimationFrame = (fn: () => void) => setTimeout(fn, 0);+ context.cancelAnimationFrame = (handle: ReturnType<typeof setTimeout>) => clearTimeout(handle);+ context.document.createElement = (tag: string) => createProcessTestElement(tag);+ const body = createProcessTestElement('div');+ const container = createProcessTestElement('details');+ container.dataset.processState = 'active';+ container.open = true;+ container.style = { display: '' };+ const finalEl: any = { style: { display: 'none' }, innerHTML: '', querySelector: () => null };+ const msg: any = {+ dataset: {},+ querySelector(selector: string) {+ if (selector === '[data-role="final"]') return finalEl;+ if (selector === '[data-role="process"]') return body;+ if (selector === '[data-role="process-container"]' || selector === '.stream-process') return container;+ return null;+ },+ };+ context._bindProcessStickToBottom = () => {};+ context._stickProcessBottomIfPinned = () => {};+ context._stickBottomFromMsg = () => {};+ context._streamingDisplayText = (text: string) => text;+ context._paintStreamingFinalMarkdown = (_msg: any, el: any, text: string) => { el.innerHTML = text; };+ context._attachAssistantActions = () => {};+ context._stripSurvivingStructuralBlocks = (text: string) => text;+ for (const text of ['Inspect sources.', 'Verify results.']) {+ context._streamingAppendFinalDelta(msg, text, 'pending');+ await new Promise(resolve => setTimeout(resolve, 0));+ expect(finalEl.innerHTML).toBe(text);+ expect(container.dataset.processState).toBe('active');+ context._streamingFinalizeCommentary(msg, text);+ context._streamingFinalizeCommentary(msg, text);+ expect(finalEl.style.display).toBe('none');+ expect(msg.dataset.streamBuf).toBe('');+ }+ expect(body.children.map((line: any) => line.dataset.processText))+ .toEqual(['Inspect sources.', 'Verify results.']);+ context._streamingAppendFinalDelta(msg, 'Delivered.', 'pending');+ context._streamingSetFinal(msg, 'Delivered.');+ await new Promise(resolve => setTimeout(resolve, 0));+ expect(finalEl.innerHTML).toBe('Delivered.');+ expect(body.children).toHaveLength(2);+ });+it('renders commentary through the markdown pipeline and keeps the raw source text', () => {const context = loadConversationRenderer();context.document.createElement = (tagName: string) => createProcessTestElement(tagName);diff --git a/test/renderer/delimited-preview.test.ts b/test/renderer/delimited-preview.test.tsnew file mode 100644index 00000000..7b564d3c--- /dev/null+++ b/test/renderer/delimited-preview.test.ts@@ -0,0 +1,38 @@+import { createRequire } from 'node:module';+import { describe, expect, it } from 'vitest';+const { parse } = createRequire(import.meta.url)('../../src/renderer/modules/delimited-preview.js');++describe('CSV and TSV data preview', () => {+ it('preserves quoted delimiters, escaped quotes, multiline cells, Unicode and leading zeroes', () => {+ const result = parse('\uFEFFid,name,note\r\n001,"中文,名称","first\nsecond ""quote"""\r\n002,,=1+1\r\n', ',');+ expect(result).toEqual({ rows: [+ ['id', 'name', 'note'], ['001', '中文,名称', 'first\nsecond "quote"'], ['002', '', '=1+1'],+ ], limited: false, malformed: false });+ });+ it('retains TSV tabs inside quoted fields and trailing empty fields', () => {+ expect(parse('a\tb\rc\t"d\te"\rf\t', '\t').rows).toEqual([['a', 'b'], ['c', 'd\te'], ['f', '']]);+ });+ it.each(['a,"unfinished', 'a,"closed"extra', 'a,b"c'])('falls back to source for malformed records: %s', text => {+ expect(parse(text, ',').malformed).toBe(true);+ });+ it('keeps empty files empty and preserves blank records', () => {+ expect(parse('', ',').rows).toEqual([]);+ expect(parse('a\n\nb\n', ',').rows).toEqual([['a'], [''], ['b']]);+ });+ it('bounds rows, columns and cell content with an explicit incomplete result', () => {+ const start = performance.now();+ const rows = parse('a,b\n'.repeat(500_000), ',');+ expect(rows.rows).toHaveLength(200);+ expect(rows.limited).toBe(true);+ const columns = parse('x,'.repeat(200), ',');+ expect(columns.rows[0]).toHaveLength(100);+ expect(columns.limited).toBe(true);+ const cell = parse('x'.repeat(100_000), ',');+ expect(cell.rows[0][0]).toHaveLength(4000);+ expect(cell.limited).toBe(true);+ expect(performance.now() - start).toBeLessThan(1000);+ });+ it('does not invent the rest of a record cut by the preview byte limit', () => {+ expect(parse('a,b\n1,"partial', ',', true)).toEqual({ rows: [['a', 'b']], limited: true, malformed: false });+ });+});diff --git a/test/renderer/file-operation-policy.test.ts b/test/renderer/file-operation-policy.test.tsindex dc66ce22..3af11d6f 100644--- a/test/renderer/file-operation-policy.test.ts+++ b/test/renderer/file-operation-policy.test.ts@@ -21,6 +21,8 @@ describe('file operation policy', () => {it.each(['note.md', 'data.json', 'report.pdf', 'document.docx', 'sheet.xlsx', 'legacy.xls', 'slides.pptx','photo.png', 'clip.mp4', 'voice.mp3', 'skills.zip',+ 'page.html', 'style.css', 'script.js', 'source.py', 'data.jsonl', 'events.ndjson',+ 'config.toml', 'report.tex', 'notes.rst', 'captions.srt', 'captions.vtt',])('allows supported chat attachment %s', (name) => {expect(policy.canAddToChat(name)).toBe(true);});@@ -32,7 +34,7 @@ describe('file operation policy', () => {});it.each([- 'page.html', 'style.css', 'script.js', 'source.py', 'legacy.doc',+ 'legacy.doc','vector.svg', 'font.woff2', 'module.wasm',])('does not advertise Add to chat for unsupported %s', (name) => {expect(policy.canAddToChat(name)).toBe(false);diff --git a/test/renderer/logger-privacy.test.ts b/test/renderer/logger-privacy.test.tsindex 823e8cca..5b529014 100644--- a/test/renderer/logger-privacy.test.ts+++ b/test/renderer/logger-privacy.test.ts@@ -11,24 +11,30 @@ const loggerSource = readFileSync(describe('renderer logger privacy boundary', () => {it.each(['ResizeObserver loop completed with undelivered notifications.', 'ResizeObserver loop limit exceeded'])('retains %s without reporting it as an uncaught exception', (message) => {+ const timers: Function[] = [];const forwarded = vi.fn();const listeners: Record<string, (event: any) => void> = {};- const sandbox = { console: { warn: vi.fn(), error: vi.fn() }, window: {+ const sandbox = { setTimeout: (fn: Function) => timers.push(fn), console: { warn: vi.fn(), error: vi.fn() }, window: {orkas: { log: forwarded }, addEventListener: (type: string, handler: any) => { listeners[type] = handler; },} };vm.runInNewContext(loggerSource, sandbox);listeners.error({ message, error: null });+ timers.shift()!();expect(forwarded).toHaveBeenLastCalledWith(expect.objectContaining({ level: 'warn', message: 'browser resize notification' }));listeners.error({ message, error: new Error(message) });+ timers.shift()!();expect(forwarded).toHaveBeenLastCalledWith(expect.objectContaining({ level: 'error', message: 'uncaught error' }));listeners.error({ message: 'ResizeObserver callback failed', error: null });+ timers.shift()!();expect(forwarded).toHaveBeenLastCalledWith(expect.objectContaining({ level: 'error', message: 'uncaught error' }));});it('sanitizes messages and structured data before forwarding either log stream', () => {+ const timers: Function[] = [];const forwarded = vi.fn();const consoleCalls: unknown[][] = [];const sandbox: any = {+ setTimeout: (fn: Function) => timers.push(fn),Error,WeakSet,Object,@@ -63,6 +69,9 @@ describe('renderer logger privacy boundary', () => {new Error('C:\\Users\\test\\Private Project\\customer-plan.md'),);+ expect(forwarded).not.toHaveBeenCalled();+ expect(consoleCalls).toHaveLength(0);+ timers.shift()!();expect(forwarded).toHaveBeenCalledOnce();const output = JSON.stringify([forwarded.mock.calls, consoleCalls]);expect(output).not.toContain('/Users/test');@@ -73,3 +82,51 @@ describe('renderer logger privacy boundary', () => {expect(output).toContain('<local_path>');});});++describe('renderer asynchronous log delivery', () => {+ function load() {+ const timers: Function[] = [];+ const forwarded = vi.fn();+ const output = vi.fn();+ const sandbox: any = { console: { info: output, warn: output, error: output },+ setTimeout: (fn: Function) => timers.push(fn),+ window: { orkas: { log: forwarded }, addEventListener: vi.fn() } };+ vm.runInNewContext(loggerSource + "\nthis.testLogger = createLogger('async');", sandbox);+ return { logger: sandbox.testLogger, timers, forwarded, output };+ }+ it('snapshots sanitized data before deferring IPC and console to a later turn', () => {+ const ctx = load();+ const getter = vi.fn(() => { throw new Error('must not evaluate diagnostic accessor'); });+ const data = { result: 'success', token: 'private', get detail() { return getter(); } };+ ctx.logger.info('result', data);+ data.result = 'later';+ expect(ctx.forwarded).not.toHaveBeenCalled();+ expect(ctx.output).not.toHaveBeenCalled();+ ctx.timers.shift()!();+ expect(ctx.forwarded.mock.calls[0][0].data[0]).toMatchObject({ result: 'success', token: '***REDACTED***', detail: '[accessor]' });+ expect(getter).not.toHaveBeenCalled();+ });+ it('bounds a log storm, preserves critical slots and yields between batches', () => {+ const ctx = load();+ for (let i = 0; i < 1000; i++) ctx.logger.info('normal');+ for (let i = 0; i < 100; i++) ctx.logger.error('critical');+ expect(ctx.timers).toHaveLength(1);+ expect(ctx.forwarded).not.toHaveBeenCalled();+ ctx.timers.shift()!();+ expect(ctx.forwarded).toHaveBeenCalledTimes(9);+ while (ctx.timers.length) ctx.timers.shift()!();+ const records = ctx.forwarded.mock.calls.map(call => call[0]);+ expect(records.filter(record => record.message === 'normal')).toHaveLength(192);+ expect(records.filter(record => record.message === 'critical')).toHaveLength(64);+ expect(records.find(record => record.message === 'Log buffer full').data).toEqual([{ dropped: 844 }]);+ });+ it('contains unavailable IPC and console and keeps processing later records', () => {+ const ctx = load();+ ctx.forwarded.mockImplementationOnce(() => { throw new Error('IPC closed'); });+ ctx.output.mockImplementationOnce(() => { throw new Error('console closed'); });+ ctx.logger.info('first'); ctx.logger.info('second');+ expect(() => ctx.timers.shift()!()).not.toThrow();+ expect(ctx.forwarded).toHaveBeenCalledTimes(2);+ expect(ctx.output).toHaveBeenCalledTimes(2);+ });+});diff --git a/test/renderer/project-todo-controls.test.ts b/test/renderer/project-todo-controls.test.tsindex 3b30aa15..01dea48f 100644--- a/test/renderer/project-todo-controls.test.ts+++ b/test/renderer/project-todo-controls.test.ts@@ -124,11 +124,8 @@ describe('project to-do controls', () => {});it('sets a todo status from the row status dropdown (four states)', async () => {- // Telemetry contract (restored 2026-08-24 — the 1.6.6 merge kept the- // _result-event code style but dropped both oracles): the toggle emits- // one terminal result event and no start click.- expect(source).not.toContain("_projectTrackClick('project_todo_toggle'");- expect(source).toContain("_projectTrackEvent('project_todo_toggle_result'");+ expect(source).not.toContain('_projectTrackClick');+ expect(source).not.toContain('_projectTrackEvent');const invocations: Array<{ channel: string; payload: Record<string, any> }> = [];const listeners: Record<string, (event: any) => Promise<void>> = {};let menuItems: Array<{ label: string; icon?: string; onClick?: () => void }> = [];