@thincoder/core 0.11.1 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -2
- package/advisor/convergence.mjs +34 -3
- package/advisor/loop.mjs +42 -10
- package/advisor/messages.mjs +42 -25
- package/advisor/project-context.mjs +171 -42
- package/advisor/run.mjs +7 -4
- package/advisor.mjs +29 -43
- package/agent/batch-attribution.mjs +87 -0
- package/agent/batch-changed-surface.mjs +129 -0
- package/agent/completion.mjs +44 -3
- package/agent/dispatch-gates.mjs +6 -0
- package/agent/dispatch.mjs +8 -3
- package/agent/family-tools.mjs +27 -12
- package/agent/helpers.mjs +122 -1
- package/agent/offload.mjs +25 -29
- package/agent/post-turn.mjs +24 -10
- package/agent/record-results.mjs +18 -1
- package/agent/run-stages.mjs +77 -32
- package/agent/run-start.mjs +37 -0
- package/agent/setup-reminders.mjs +9 -2
- package/agent/setup.mjs +24 -11
- package/agent/spawn-child.mjs +38 -1
- package/agent/timers.mjs +5 -3
- package/agent/turn-loop.mjs +30 -5
- package/agent/turn-projection.mjs +386 -0
- package/agent/turn-task-state.mjs +274 -0
- package/agent/turn-tool-process.mjs +168 -0
- package/agent/write-gate-ledger.mjs +569 -0
- package/agent/write-gate.mjs +29 -352
- package/agent-tools/advisor-async.mjs +29 -169
- package/agent-tools/advisor-launch.mjs +178 -0
- package/agent-tools/advisor-settle.mjs +28 -1
- package/agent-tools/advisor.mjs +36 -47
- package/agent-tools/audit-block.mjs +6 -6
- package/agent-tools/batch-closeout.mjs +273 -35
- package/agent-tools/batch-gate.mjs +142 -342
- package/agent-tools/batch-ledger-bind.mjs +226 -17
- package/agent-tools/batch-ledger-view.mjs +109 -0
- package/agent-tools/batch-lifecycle.mjs +169 -46
- package/agent-tools/batch-paths.mjs +29 -8
- package/agent-tools/batch-record-write.mjs +417 -0
- package/agent-tools/batch-regions.mjs +215 -0
- package/agent-tools/batch-reopen.mjs +23 -11
- package/agent-tools/batch-segment-vocab.mjs +379 -0
- package/agent-tools/batch-skeleton.mjs +192 -239
- package/agent-tools/batch-supersede.mjs +232 -0
- package/agent-tools/batch-tier-gate.mjs +297 -0
- package/agent-tools/batch.mjs +182 -439
- package/agent-tools/consult.mjs +7 -4
- package/agent-tools/context.mjs +44 -17
- package/agent-tools/design-review-record.mjs +225 -0
- package/agent-tools/design-slots.mjs +1 -1
- package/agent-tools/design-token.mjs +279 -81
- package/agent-tools/doc-check.mjs +43 -33
- package/agent-tools/doc-graph.mjs +140 -0
- package/agent-tools/eng.mjs +9 -8
- package/agent-tools/escalate-async.mjs +2 -2
- package/agent-tools/manifest.mjs +34 -18
- package/agent-tools/read-history.mjs +112 -11
- package/agent-tools/review-facts.mjs +10 -3
- package/agent-tools/settings.mjs +23 -2
- package/agent-tools/spawn-gates.mjs +9 -8
- package/agent-tools/ssh-session.mjs +2 -2
- package/agent-tools/ssh-text.mjs +155 -0
- package/agent-tools/ssh-transport.mjs +19 -148
- package/agent-tools/subagent-actions-query.mjs +10 -6
- package/agent-tools/subagent-async.mjs +8 -9
- package/agent-tools/subagent-brief.mjs +76 -0
- package/agent-tools/subagent-design-token.mjs +168 -0
- package/agent-tools/subagent-run.mjs +1 -1
- package/agent-tools/subagent-scheduler.mjs +4 -4
- package/agent-tools/subagent-spawn.mjs +90 -110
- package/agent-tools/subagent.mjs +11 -10
- package/agent-tools/taskbook-block.mjs +94 -0
- package/agent-tools/timer.mjs +12 -8
- package/agent-tools/verify.mjs +13 -4
- package/agent-tools.mjs +7 -3
- package/agent.mjs +20 -1
- package/attachments.mjs +3 -3
- package/browser/actions.mjs +9 -3
- package/browser/cdp.mjs +45 -17
- package/browser/launch.mjs +25 -43
- package/browser/pool.mjs +299 -0
- package/browser/queue.mjs +2 -0
- package/browser/session-state.mjs +450 -0
- package/browser/session.mjs +127 -151
- package/browser/snapshot.mjs +89 -0
- package/client-identity.mjs +1 -1
- package/config-defaults.mjs +2 -1
- package/config-migrate.mjs +40 -0
- package/config-normalize.mjs +28 -2
- package/config-reload.mjs +59 -0
- package/config.mjs +62 -98
- package/context-degrade.mjs +38 -10
- package/context-echo.mjs +11 -0
- package/context.mjs +149 -83
- package/conventions.mjs +11 -9
- package/declaration.mjs +31 -5
- package/declared-repos.mjs +94 -0
- package/doc-check/doc-names.mjs +73 -0
- package/doc-check/doc-tokens.mjs +110 -0
- package/doc-check/existence.mjs +160 -0
- package/doc-check/index.mjs +309 -213
- package/doc-check/line-refs.mjs +27 -101
- package/doc-check/paragraph.mjs +218 -102
- package/doc-graph/anchors.mjs +474 -0
- package/doc-graph/doc-batch-face.mjs +59 -0
- package/doc-graph/doc-suffix.mjs +27 -0
- package/doc-graph/graph-tokens.mjs +160 -0
- package/doc-graph.mjs +778 -0
- package/explore-distill.mjs +11 -5
- package/i18n.mjs +1 -1
- package/instance-inbox-watch.mjs +272 -0
- package/instance-inbox.mjs +218 -0
- package/instance-registry.mjs +179 -0
- package/knowledge/README.md +3 -0
- package/knowledge/prompt-resources.md +6 -0
- package/ledger-close-proof.mjs +100 -49
- package/ledger-close.mjs +188 -0
- package/ledger-cmd.mjs +47 -91
- package/ledger-consistency.mjs +113 -0
- package/ledger-db.mjs +47 -15
- package/ledger-executors.mjs +1 -1
- package/ledger-tools.mjs +78 -19
- package/ledger-write-ops.mjs +130 -41
- package/ledger.mjs +1 -1
- package/manifest-discovery.mjs +3 -1
- package/manifest-schema.mjs +27 -5
- package/memory/schema.mjs +2 -2
- package/package.json +3 -1
- package/path-resolve.mjs +338 -57
- package/prompt-files.mjs +91 -1
- package/prompt-overlays.mjs +9 -6
- package/prompts/advisor-design.md +62 -21
- package/prompts/advisor-round1.md +34 -1
- package/prompts/advisor-round2.md +40 -7
- package/prompts/advisor-round3.md +39 -6
- package/prompts/common.md +14 -387
- package/prompts/consult-base.md +5 -1
- package/prompts/discipline-engineering.md +226 -51
- package/prompts/discipline-normal.md +4 -255
- package/prompts/knowledge/business-gates.md +22 -0
- package/prompts/knowledge/decision-quality.md +23 -0
- package/prompts/knowledge/doc-lookup.md +35 -0
- package/prompts/knowledge/document-writing.md +51 -0
- package/prompts/knowledge/documentation-evaluation.md +56 -0
- package/prompts/knowledge/evidence-discipline.md +57 -0
- package/prompts/knowledge/normal-coding-workflow.md +104 -0
- package/prompts/knowledge/normal-delivery-collaboration.md +108 -0
- package/prompts/knowledge/normal-work-management.md +57 -0
- package/prompts/knowledge/product-security.md +23 -0
- package/prompts/knowledge/project-records.md +62 -0
- package/prompts/knowledge/tool-advisor.md +29 -0
- package/prompts/knowledge/tool-apply-patch.md +18 -0
- package/prompts/knowledge/tool-bash.md +39 -0
- package/prompts/knowledge/tool-batch-eng-coder.md +12 -0
- package/prompts/knowledge/tool-batch-eng-designer.md +12 -0
- package/prompts/knowledge/tool-batch-review.md +12 -0
- package/prompts/knowledge/tool-batch.md +80 -0
- package/prompts/knowledge/tool-browser.md +57 -0
- package/prompts/knowledge/tool-code-search.md +4 -0
- package/prompts/knowledge/tool-consult-start.md +12 -0
- package/prompts/knowledge/tool-consult-stop.md +6 -0
- package/prompts/knowledge/tool-context.md +6 -0
- package/prompts/knowledge/tool-delete.md +17 -0
- package/prompts/knowledge/tool-doc-check.md +70 -0
- package/prompts/knowledge/tool-doc-graph-read.md +20 -0
- package/prompts/knowledge/tool-doc-graph.md +49 -0
- package/prompts/knowledge/tool-doc-search.md +6 -0
- package/prompts/knowledge/tool-edit.md +38 -0
- package/prompts/knowledge/tool-eng.md +2 -0
- package/prompts/knowledge/tool-execute.md +16 -0
- package/prompts/knowledge/tool-fetch.md +12 -0
- package/prompts/knowledge/tool-file-ops.md +16 -0
- package/prompts/knowledge/tool-get-current-time.md +7 -0
- package/prompts/knowledge/tool-git-read.md +19 -0
- package/prompts/knowledge/tool-git.md +29 -0
- package/prompts/knowledge/tool-glob.md +15 -0
- package/prompts/knowledge/tool-goal.md +8 -0
- package/prompts/knowledge/tool-grep.md +18 -0
- package/prompts/knowledge/tool-hashline-edit.md +14 -0
- package/prompts/knowledge/tool-insert-after.md +15 -0
- package/prompts/knowledge/tool-instance-list.md +6 -0
- package/prompts/knowledge/tool-instance-send.md +10 -0
- package/prompts/knowledge/tool-knowledge.md +18 -0
- package/prompts/knowledge/tool-ledger-read.md +35 -0
- package/prompts/knowledge/tool-ledger.md +42 -0
- package/prompts/knowledge/tool-lint.md +16 -0
- package/prompts/knowledge/tool-ls.md +13 -0
- package/prompts/knowledge/tool-lsp.md +11 -0
- package/prompts/knowledge/tool-main-history.md +2 -0
- package/prompts/knowledge/tool-manifest-read.md +14 -0
- package/prompts/knowledge/tool-manifest.md +31 -0
- package/prompts/knowledge/tool-memory-read.md +20 -0
- package/prompts/knowledge/tool-memory.md +15 -0
- package/prompts/knowledge/tool-notify-parent.md +12 -0
- package/prompts/knowledge/tool-operations.md +114 -0
- package/prompts/knowledge/tool-peer-instances.md +2 -0
- package/prompts/knowledge/tool-plan.md +6 -0
- package/prompts/knowledge/tool-process-read.md +13 -0
- package/prompts/knowledge/tool-process.md +15 -0
- package/prompts/knowledge/tool-question.md +16 -0
- package/prompts/knowledge/tool-read-history-batch.md +22 -0
- package/prompts/knowledge/tool-read-history.md +36 -0
- package/prompts/knowledge/tool-read-image.md +11 -0
- package/prompts/knowledge/tool-read.md +21 -0
- package/prompts/knowledge/tool-recent-changes.md +6 -0
- package/prompts/knowledge/tool-repo-outline.md +4 -0
- package/prompts/knowledge/tool-settings-read.md +12 -0
- package/prompts/knowledge/tool-settings.md +18 -0
- package/prompts/knowledge/tool-skill.md +6 -0
- package/prompts/knowledge/tool-ssh.md +52 -0
- package/prompts/knowledge/tool-subagent.md +59 -0
- package/prompts/knowledge/tool-task.md +12 -0
- package/prompts/knowledge/tool-team-ledger-read.md +11 -0
- package/prompts/knowledge/tool-team-ledger.md +11 -0
- package/prompts/knowledge/tool-timer.md +23 -0
- package/prompts/knowledge/tool-tree.md +14 -0
- package/prompts/knowledge/tool-verify.md +19 -0
- package/prompts/knowledge/tool-wait-for.md +25 -0
- package/prompts/knowledge/tool-websearch.md +19 -0
- package/prompts/knowledge/tool-write.md +9 -0
- package/prompts/knowledge/upstream-protocol.md +43 -0
- package/prompts/persona-eng-coder.md +21 -3
- package/prompts/persona-eng-designer.md +131 -169
- package/prompts/persona-engineering.md +142 -84
- package/prompts/persona-explore.md +1 -1
- package/prompts/persona-plan.md +5 -1
- package/prompts/subagent-base.md +2 -0
- package/provider/anthropic.mjs +6 -4
- package/provider/core.mjs +3 -2
- package/provider/errors.mjs +29 -4
- package/provider/google.mjs +24 -7
- package/provider/responses.mjs +3 -1
- package/provider-flows.mjs +2 -2
- package/session-index-build.mjs +1 -1
- package/session-index-pass.mjs +1 -1
- package/session-index-query.mjs +1 -1
- package/session-index.mjs +1 -1
- package/session-lifecycle.mjs +72 -17
- package/session-slot-write.mjs +37 -5
- package/session.mjs +24 -8
- package/skip-dirs.mjs +2 -2
- package/team-ledger.mjs +13 -9
- package/token-ttl.mjs +73 -4
- package/token-window.mjs +167 -33
- package/tool-docs/advisor.md +2 -27
- package/tool-docs/apply_patch.md +1 -15
- package/tool-docs/bash.md +2 -41
- package/tool-docs/batch-eng-coder.md +3 -0
- package/tool-docs/batch-eng-designer.md +3 -0
- package/tool-docs/batch-review.md +3 -0
- package/tool-docs/batch.md +2 -84
- package/tool-docs/browser.md +2 -49
- package/tool-docs/code_search.md +2 -2
- package/tool-docs/consult_start.md +2 -8
- package/tool-docs/consult_stop.md +2 -4
- package/tool-docs/context.md +1 -5
- package/tool-docs/delete.md +2 -17
- package/tool-docs/doc_check.md +2 -107
- package/tool-docs/doc_graph-read.md +3 -0
- package/tool-docs/doc_graph.md +3 -0
- package/tool-docs/doc_search.md +2 -4
- package/tool-docs/edit.md +1 -38
- package/tool-docs/eng.md +3 -1
- package/tool-docs/execute.md +2 -18
- package/tool-docs/fetch.md +2 -14
- package/tool-docs/file_ops.md +2 -18
- package/tool-docs/get_current_time.md +1 -6
- package/tool-docs/git-read.md +3 -0
- package/tool-docs/git.md +2 -35
- package/tool-docs/glob.md +2 -10
- package/tool-docs/goal.md +1 -6
- package/tool-docs/grep.md +2 -18
- package/tool-docs/hashline_edit.md +2 -15
- package/tool-docs/insert_after.md +2 -16
- package/tool-docs/instance_list.md +3 -0
- package/tool-docs/instance_send.md +3 -0
- package/tool-docs/knowledge.md +3 -0
- package/tool-docs/ledger-read.md +3 -0
- package/tool-docs/ledger.md +3 -8
- package/tool-docs/lint.md +2 -10
- package/tool-docs/ls.md +2 -11
- package/tool-docs/lsp.md +2 -9
- package/tool-docs/main_history.md +3 -1
- package/tool-docs/manifest-read.md +2 -8
- package/tool-docs/manifest.md +2 -35
- package/tool-docs/memory-read.md +3 -0
- package/tool-docs/memory.md +2 -20
- package/tool-docs/notify_parent.md +2 -13
- package/tool-docs/peer_instances.md +3 -1
- package/tool-docs/plan.md +1 -5
- package/tool-docs/process-read.md +3 -0
- package/tool-docs/process.md +2 -13
- package/tool-docs/question.md +3 -17
- package/tool-docs/read.md +2 -19
- package/tool-docs/read_history-batch.md +3 -0
- package/tool-docs/read_history.md +2 -18
- package/tool-docs/read_image.md +2 -9
- package/tool-docs/recent_changes.md +2 -2
- package/tool-docs/repo_outline.md +2 -2
- package/tool-docs/settings-read.md +3 -0
- package/tool-docs/settings.md +1 -12
- package/tool-docs/skill.md +2 -6
- package/tool-docs/ssh.md +1 -62
- package/tool-docs/subagent.md +2 -100
- package/tool-docs/task.md +1 -7
- package/tool-docs/team_ledger-read.md +3 -0
- package/tool-docs/team_ledger.md +3 -9
- package/tool-docs/timer.md +2 -19
- package/tool-docs/tree.md +2 -13
- package/tool-docs/verify.md +2 -12
- package/tool-docs/wait_for.md +2 -32
- package/tool-docs/websearch.md +2 -19
- package/tool-docs/write.md +2 -12
- package/tools/browser.mjs +52 -7
- package/tools/doc-write-gate.mjs +73 -0
- package/tools/edit-batch.mjs +170 -92
- package/tools/edit-diff.mjs +78 -62
- package/tools/edit-receipt.mjs +39 -0
- package/tools/execute.mjs +1 -1
- package/tools/file.mjs +16 -8
- package/tools/git.mjs +24 -9
- package/tools/index.mjs +6 -0
- package/tools/knowledge.mjs +143 -0
- package/tools/linter.mjs +3 -3
- package/tools/ops.mjs +3 -1
- package/tools/patch.mjs +123 -43
- package/tools/shared.mjs +28 -27
- package/tools/write-gate-params.mjs +31 -0
- package/tools/write-path.mjs +16 -4
- package/window-policy.mjs +203 -0
- package/doc-check/anchors.mjs +0 -235
package/browser/pool.mjs
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* browser/pool.mjs — 共享浏览器单池(`docs/core/design/BROWSER-TOOL.md` §12):进入形态 ∥ 持有者位 ∥
|
|
3
|
+
* 用者登记 ∥ 末关。判定与生命周期都住本档,`session.mjs` 只留接线(§12「尺寸拆分计划」拆法①②)。
|
|
4
|
+
*
|
|
5
|
+
* 一个池、零实例维度:`<configDir>/browser/{profile, session.lock, users/, shots/}`(`browserPaths`)。
|
|
6
|
+
* profile 全机唯一一份 ⇒ 登录态全机一份(F-BT6);Chromium 的 `--user-data-dir` 是单实例目录 ⇒
|
|
7
|
+
* 共一份 profile 只能有一台浏览器(§12),所以「多用者」的载体是**每实例一条标签页 + 一条登记**。
|
|
8
|
+
*
|
|
9
|
+
* 判序(端口先行):先问 `profile/DevToolsActivePort` 的端口答不答,再读锁——四形两两不重(§12「四形如何不重不漏」)。
|
|
10
|
+
*
|
|
11
|
+
* - **接入**:端口答通 ⇒ 开自己的标签页 + 登记(不论锁里 pid 死活、不论在不在启动窗内)。
|
|
12
|
+
* - **持有**:端口答不通 ∧ 无锁可读 ⇒ 原子领取 → 读用者登记 → 回收同 profile 孤儿 → 拉起 → 写回锁 → 登记。
|
|
13
|
+
* - **等待**:端口答不通 ∧ 锁可读 ∧ pid 活 ∧ 窗内 ⇒ 50 ms 轮询到窗尾;答通则接入,到点未通则转接管。
|
|
14
|
+
* - **接管**:端口答不通 ∧ 锁可读 ∧(pid 死 ∨ 已过窗)⇒ 读用者登记 → `rename` 摘陈旧位 → 回收 → 拉起。
|
|
15
|
+
*
|
|
16
|
+
* **回收的唯一门槛**:端口答不通**不**证明没人连着(挂死 ∥ 忙等时 WebSocket 未断、`failSession` 不触发)⇒
|
|
17
|
+
* 回收 ∥ 拉起之前先读 `users/*.json`,有他实例活条目就不收、不拉起并明确报错(§12 同名段)。
|
|
18
|
+
* **方向不对称**:探测失败 ⇒ 算活——误留只多留一台浏览器,误删会关掉别人在用的浏览器。
|
|
19
|
+
*
|
|
20
|
+
* 注入缝(§12 机制落点:目录 ∥ 判活 ∥ 时钟):每个导出接 `io`,缺省回落真实现;`_deps` 供用例整体覆写。
|
|
21
|
+
*/
|
|
22
|
+
import { closeSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync, writeSync } from "node:fs"
|
|
23
|
+
import { dirname, join } from "node:path"
|
|
24
|
+
import { closeTab, newTab, probeVersion } from "./cdp.mjs"
|
|
25
|
+
import { browserStartTimeoutMs, launchBrowser, listProfileProcs, reclaimProfileOrphans, resolveBrowser } from "./launch.mjs"
|
|
26
|
+
|
|
27
|
+
/** 「端口答不答」的探针帽(§12「接入」判据——2 s)。 */
|
|
28
|
+
const PROBE_CAP_MS = 2_000
|
|
29
|
+
/** 「等待」形态的轮询间隔(§12 四形表)。 */
|
|
30
|
+
const WAIT_POLL_MS = 50
|
|
31
|
+
/** 原子领取 ∥ 摘陈旧位失败后的重判上限(防两实例互摘空转)。 */
|
|
32
|
+
const CLAIM_ATTEMPTS = 5
|
|
33
|
+
|
|
34
|
+
/** pid 活性(方向不对称——只有 ESRCH 算死,无权限等一律算活)。 */
|
|
35
|
+
function pidAlive(pid) {
|
|
36
|
+
try { process.kill(pid, 0); return true }
|
|
37
|
+
catch (e) { return e?.code !== "ESRCH" }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 端口读数单源 = `profile/DevToolsActivePort`(锁里的 port 只是抄本,不参与判定)。 */
|
|
41
|
+
function portOnDisk(profile) {
|
|
42
|
+
const raw = String(readTextReal(join(profile, "DevToolsActivePort"))).split(/\r?\n/)[0].trim()
|
|
43
|
+
const port = Number(raw)
|
|
44
|
+
return Number.isInteger(port) && port > 0 ? port : null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function readTextReal(path) {
|
|
48
|
+
try { return readFileSync(path, "utf8") } catch { return "" }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 在跑那台浏览器的模式(锁无记录时的模式来源——读命令行里的 `--headless=new`;读不到 ⇒ null = 不给值)。
|
|
52
|
+
* 只认**浏览器进程**那一行:helper 进程(`--type=`)也带着父进程的 `--user-data-dir`,读它们的行会报错模式。 */
|
|
53
|
+
function headlessFromCmdline(profile) {
|
|
54
|
+
const marker = `--user-data-dir=${profile}`
|
|
55
|
+
for (const row of listProfileProcs() ?? []) {
|
|
56
|
+
const cmdline = String(row?.cmdline ?? "").replace(/"/g, "")
|
|
57
|
+
if (!cmdline.includes(marker) || /--type=/.test(cmdline)) continue
|
|
58
|
+
return /--headless=new/.test(cmdline)
|
|
59
|
+
}
|
|
60
|
+
return null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const REAL = {
|
|
64
|
+
readText: readTextReal,
|
|
65
|
+
writeText: (path, text) => { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, text, "utf8") },
|
|
66
|
+
/** 创建即领取:`wx` 成功才算获得(EEXIST ⇒ false)。「先查后建」在两实例同时首开那一窗必漏。 */
|
|
67
|
+
claim: (path, text) => {
|
|
68
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
69
|
+
const fd = openSync(path, "wx")
|
|
70
|
+
try { writeSync(fd, text) } finally { closeSync(fd) }
|
|
71
|
+
return true
|
|
72
|
+
},
|
|
73
|
+
rename: (from, to) => renameSync(from, to),
|
|
74
|
+
remove: (path) => { try { rmSync(path, { force: true }) } catch { /* 已不在盘 */ } },
|
|
75
|
+
listDir: (dir) => { try { return readdirSync(dir) } catch { return [] } },
|
|
76
|
+
isAlive: pidAlive,
|
|
77
|
+
now: () => Date.now(),
|
|
78
|
+
sleep: (ms) => new Promise((resolve) => { const timer = setTimeout(resolve, ms); timer.unref?.() }),
|
|
79
|
+
port: (paths) => portOnDisk(paths.profile),
|
|
80
|
+
probe: (port) => probeVersion(port, { timeoutMs: PROBE_CAP_MS }),
|
|
81
|
+
openTab: (port) => newTab(port),
|
|
82
|
+
closeTab: (port, id) => closeTab(port, id),
|
|
83
|
+
launch: ({ headless, profile }) => launchBrowser({ exe: resolveBrowser(), headless, profile }),
|
|
84
|
+
reclaim: (profile) => reclaimProfileOrphans(profile),
|
|
85
|
+
browserHeadless: (profile) => headlessFromCmdline(profile),
|
|
86
|
+
startWindow: () => browserStartTimeoutMs(),
|
|
87
|
+
pid: () => process.pid,
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 用例整体覆写位(`session.mjs` 的 `_deps` 同式);缺省空 ⇒ 回落 `REAL`。 */
|
|
91
|
+
export const _deps = {}
|
|
92
|
+
|
|
93
|
+
const depsOf = (io) => ({ ...REAL, ..._deps, ...io })
|
|
94
|
+
|
|
95
|
+
const userFile = (paths, pid) => join(paths.users, `${pid}.json`)
|
|
96
|
+
|
|
97
|
+
function parseJson(text) {
|
|
98
|
+
try { return JSON.parse(String(text ?? "")) } catch { return null }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 持有者记录:JSON `{pid, headless, port, startedAt}`;旧形(裸 pid 一行)照读但按「已过窗」处置。 */
|
|
102
|
+
export function readHolder(lock, io = {}) {
|
|
103
|
+
const raw = String((io.readText ?? readTextReal)(lock) ?? "").trim()
|
|
104
|
+
if (!raw) return null
|
|
105
|
+
const rec = parseJson(raw)
|
|
106
|
+
if (rec && Number.isInteger(rec.pid) && rec.pid > 0) {
|
|
107
|
+
return {
|
|
108
|
+
pid: rec.pid,
|
|
109
|
+
headless: typeof rec.headless === "boolean" ? rec.headless : null,
|
|
110
|
+
port: Number.isInteger(rec.port) && rec.port > 0 ? rec.port : null,
|
|
111
|
+
startedAt: Number.isFinite(Number(rec.startedAt)) ? Number(rec.startedAt) : 0,
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const legacy = Number(raw)
|
|
115
|
+
return Number.isInteger(legacy) && legacy > 0 ? { pid: legacy, headless: null, port: null, startedAt: 0 } : null
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 写回生效模式与端口(持有者拉起成功后)。 */
|
|
119
|
+
export function writeHolder(lock, record, io = {}) {
|
|
120
|
+
const d = depsOf(io)
|
|
121
|
+
d.writeText(lock, JSON.stringify(record))
|
|
122
|
+
return record
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 用者登记:`users/<pid>.json` = `{pid, tabId, joinedAt}`(单写者 = 该 pid)。 */
|
|
126
|
+
export function registerUser({ paths, pid, tabId }, io = {}) {
|
|
127
|
+
const d = depsOf(io)
|
|
128
|
+
const record = { pid, tabId: tabId ?? null, joinedAt: d.now() }
|
|
129
|
+
d.writeText(userFile(paths, pid), JSON.stringify(record))
|
|
130
|
+
return record
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 活着的用者条目(死条目当场剔除——计数不被死条目撑着)。
|
|
134
|
+
* 读不懂的条目**不删也不当死**:登记是一条小档、写侧非原子,取到半写形是可能的——
|
|
135
|
+
* 按「探测失败 ⇒ 算活」同向处置(误留只多留一台浏览器,误删会关掉别人在用的浏览器),
|
|
136
|
+
* 它计作一条匿名在用者(pid = null)。 */
|
|
137
|
+
export function liveUsers({ paths, io = {} } = {}) {
|
|
138
|
+
const d = depsOf(io)
|
|
139
|
+
const alive = []
|
|
140
|
+
for (const name of d.listDir(paths.users) ?? []) {
|
|
141
|
+
if (!String(name).endsWith(".json")) continue
|
|
142
|
+
const file = join(paths.users, String(name))
|
|
143
|
+
const record = parseJson(d.readText(file))
|
|
144
|
+
if (!record || !Number.isInteger(record.pid)) { alive.push({ pid: null, tabId: null, joinedAt: 0, unreadable: true }); continue }
|
|
145
|
+
if (d.isAlive(record.pid)) alive.push({ pid: record.pid, tabId: record.tabId ?? null, joinedAt: record.joinedAt ?? 0 })
|
|
146
|
+
else d.remove(file)
|
|
147
|
+
}
|
|
148
|
+
return alive
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const otherUsers = (paths, self, d) => liveUsers({ paths, io: d }).filter((user) => user.pid !== self)
|
|
152
|
+
|
|
153
|
+
/** 门槛命中时的显式失败(§12「被挡住时的出路」:让那些实例 close ∥ 退出后重试,不静默重试)。 */
|
|
154
|
+
function blockedError({ port, others }) {
|
|
155
|
+
const where = port ? ` on port ${port}` : ""
|
|
156
|
+
return new Error(`the shared browser is unreachable${where} and ${others} other instance(s) hold live registrations — not reclaiming`)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 模式沿用注记(§12「模式」条):只在**要的事与生效值不一致**时发(同值 ⇒ 零注记,§2.4)。 */
|
|
160
|
+
export function modeNote({ wanted, actual, starterPid = null }) {
|
|
161
|
+
const tail = "the mode is set by the first instance; it switches only after every instance has released it"
|
|
162
|
+
if (typeof actual === "boolean") {
|
|
163
|
+
if (wanted === actual) return null
|
|
164
|
+
const starter = Number.isInteger(starterPid) && starterPid > 0 ? ` (started by pid ${starterPid})` : ""
|
|
165
|
+
return `[note: the shared browser is running headless=${actual}${starter} — ${tail}]`
|
|
166
|
+
}
|
|
167
|
+
// 生效模式读不到(锁无记录 ∧ 命令行也读不到)⇒ 只报关系、不报值
|
|
168
|
+
return `[note: the shared browser is already running — ${tail}]`
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** 开自己的标签页 + 登记(接入 ∥ 持有 ∥ 接管三条路共用)。 */
|
|
172
|
+
async function openOwnTab({ paths, d, self, port, child }) {
|
|
173
|
+
const tab = await d.openTab(port)
|
|
174
|
+
registerUser({ paths, pid: self, tabId: tab.id }, d)
|
|
175
|
+
return { port, tabId: tab.id, wsUrl: tab.wsUrl, child: child ?? null }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 接入:端口答通 ⇒ 开自己的标签页,不拉起、不动锁。 */
|
|
179
|
+
async function joinShared({ paths, d, self, port, wantedHeadless }) {
|
|
180
|
+
const holder = readHolder(paths.lock, d)
|
|
181
|
+
const actual = holder && typeof holder.headless === "boolean" ? holder.headless : d.browserHeadless(paths.profile)
|
|
182
|
+
const joined = await openOwnTab({ paths, d, self, port })
|
|
183
|
+
return { ...joined, headless: actual, starterPid: holder?.pid ?? null, note: modeNote({ wanted: wantedHeadless, actual, starterPid: holder?.pid ?? null }) }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** 原子领取 → 读用者登记(门槛)→ 回收 → 拉起 → 写回 → 开自己的标签页。竞争失败 ⇒ null(外层重判)。
|
|
187
|
+
* 领取成功后的两条放弃腿都要摘掉自己刚建的位:留着会让后来的实例读到「pid 活」而白等一个启动窗。 */
|
|
188
|
+
async function claimSlot({ paths, d, self, wantedHeadless }) {
|
|
189
|
+
const startedAt = d.now()
|
|
190
|
+
if (!d.claim(paths.lock, JSON.stringify({ pid: self, headless: null, port: null, startedAt }))) return null
|
|
191
|
+
const held = otherUsers(paths, self, d)
|
|
192
|
+
if (held.length) {
|
|
193
|
+
d.remove(paths.lock)
|
|
194
|
+
throw blockedError({ port: d.port(paths), others: held.length })
|
|
195
|
+
}
|
|
196
|
+
d.reclaim(paths.profile)
|
|
197
|
+
let launched
|
|
198
|
+
try {
|
|
199
|
+
launched = await d.launch({ headless: wantedHeadless, profile: paths.profile })
|
|
200
|
+
} catch (e) {
|
|
201
|
+
d.remove(paths.lock)
|
|
202
|
+
throw e
|
|
203
|
+
}
|
|
204
|
+
writeHolder(paths.lock, { pid: self, headless: wantedHeadless, port: launched.port, startedAt }, d)
|
|
205
|
+
const joined = await openOwnTab({ paths, d, self, port: launched.port, child: launched.child })
|
|
206
|
+
return { ...joined, headless: wantedHeadless, starterPid: self, note: null }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** 接管:门槛 → `rename` 摘陈旧位(原子——两方同时摘只有一个成功)→ 领取并拉起。 */
|
|
210
|
+
async function takeOver({ paths, d, self, wantedHeadless, holder }) {
|
|
211
|
+
const held = otherUsers(paths, self, d)
|
|
212
|
+
if (held.length) throw blockedError({ port: d.port(paths), others: held.length })
|
|
213
|
+
const stale = `${paths.lock}.stale-${holder.pid}`
|
|
214
|
+
try { d.rename(paths.lock, stale) } catch { return null } // 已被别人摘走 ⇒ 重判
|
|
215
|
+
d.remove(stale)
|
|
216
|
+
return claimSlot({ paths, d, self, wantedHeadless })
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** 「等待」:窗内轮询端口;答通 ⇒ 返回端口;窗尾 ∥ 持有者已死 ⇒ null(下一轮落「接管」)。 */
|
|
220
|
+
async function waitForPort({ paths, d, holder }) {
|
|
221
|
+
const deadline = holder.startedAt + d.startWindow()
|
|
222
|
+
while (d.now() < deadline) {
|
|
223
|
+
await d.sleep(WAIT_POLL_MS)
|
|
224
|
+
const port = d.port(paths)
|
|
225
|
+
if (port && (await d.probe(port))) return port
|
|
226
|
+
if (!d.isAlive(holder.pid)) return null
|
|
227
|
+
}
|
|
228
|
+
return null
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 共享池唯一入口(§12):判序端口先行,四形取一。
|
|
233
|
+
* @returns {Promise<{port: number, tabId: string|null, wsUrl: string, child: object|null,
|
|
234
|
+
* headless: boolean|null, starterPid: number|null, note: string|null}>} `child` 只有拉起者非空;
|
|
235
|
+
* `note` = 模式沿用注记(无则 null)。
|
|
236
|
+
*/
|
|
237
|
+
export async function attachOrLaunch({ paths, wantedHeadless = true, io = {} } = {}) {
|
|
238
|
+
const d = depsOf(io)
|
|
239
|
+
const self = d.pid()
|
|
240
|
+
for (let attempt = 0; attempt < CLAIM_ATTEMPTS; attempt++) {
|
|
241
|
+
const port = d.port(paths)
|
|
242
|
+
if (port && (await d.probe(port))) return joinShared({ paths, d, self, port, wantedHeadless })
|
|
243
|
+
const holder = readHolder(paths.lock, d)
|
|
244
|
+
if (!holder) {
|
|
245
|
+
const slot = await claimSlot({ paths, d, self, wantedHeadless })
|
|
246
|
+
if (slot) return slot
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
const inWindow = d.now() - holder.startedAt <= d.startWindow()
|
|
250
|
+
if (d.isAlive(holder.pid) && inWindow) {
|
|
251
|
+
const ready = await waitForPort({ paths, d, holder })
|
|
252
|
+
if (ready) return joinShared({ paths, d, self, port: ready, wantedHeadless })
|
|
253
|
+
// 到点仍未答通 ⇒ 直接转「接管」(§12 四形表);接管抢输(陈旧位已被别人摘走)⇒ 重判
|
|
254
|
+
const slot = await takeOver({ paths, d, self, wantedHeadless, holder })
|
|
255
|
+
if (slot) return slot
|
|
256
|
+
continue
|
|
257
|
+
}
|
|
258
|
+
const slot = await takeOver({ paths, d, self, wantedHeadless, holder })
|
|
259
|
+
if (slot) return slot
|
|
260
|
+
}
|
|
261
|
+
throw new Error("the shared browser pool did not settle (repeated claim races) — run the action again")
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* 摘自己 + 末关(§12):删自己的条目 → 重扫并再核一次活性 → 无活条目才关浏览器 → 删锁。
|
|
266
|
+
* 自己的标签页随后关(「摘自己」的后一半):**先判末关再关标签页**——关标签页会撕掉本进程的 CDP 连接,
|
|
267
|
+
* 而末关的 `Browser.close` 正是走这条连接发的;先关标签页,最后一位用者的关浏览器会发不出去,
|
|
268
|
+
* 回执说「closed」而浏览器还在跑(验收 AC-M3 要的是进程真的消失)。
|
|
269
|
+
* @param closeBrowser 关浏览器本体(由 `session.mjs` 注入:`Browser.close` → 等自退 → 拉起者才有杀树);
|
|
270
|
+
* 仍有活条目时**不调用**它。
|
|
271
|
+
* @returns {Promise<{others: number, lastUser: boolean, browserClosed: boolean}>}
|
|
272
|
+
*/
|
|
273
|
+
export async function releaseUser({ paths, tabId = null, closeBrowser = null, io = {} } = {}) {
|
|
274
|
+
const d = depsOf(io)
|
|
275
|
+
const self = d.pid()
|
|
276
|
+
const port = d.port(paths)
|
|
277
|
+
const closeOwnTab = async () => { if (port && tabId) await d.closeTab(port, tabId) }
|
|
278
|
+
d.remove(userFile(paths, self))
|
|
279
|
+
const others = otherUsers(paths, self, d).length
|
|
280
|
+
if (others > 0) {
|
|
281
|
+
await closeOwnTab() // 仍有人用 ⇒ 只关自己的标签页,浏览器留着(关别人的浏览器是硬边界)
|
|
282
|
+
return { others, lastUser: false, browserClosed: false }
|
|
283
|
+
}
|
|
284
|
+
// 没接过浏览器本体(无会话)⇒ 不碰锁:另一实例可能正在首开中(锁里此刻记的是它)
|
|
285
|
+
if (!closeBrowser) { await closeOwnTab(); return { others: 0, lastUser: true, browserClosed: false } }
|
|
286
|
+
let browserClosed = false
|
|
287
|
+
try { await closeBrowser(); browserClosed = true } catch { browserClosed = false }
|
|
288
|
+
if (!browserClosed) await closeOwnTab()
|
|
289
|
+
if (browserClosed) d.remove(paths.lock) // 末关删锁:关成功后锁里记的持有者 pid 照旧活着,留着会挡住下一个实例
|
|
290
|
+
return { others: 0, lastUser: true, browserClosed }
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** 同步摘自己(进程退出钩子 ∥ `failSession`:两处都不能等异步面)。返回还活着几家。 */
|
|
294
|
+
export function releaseUserSync({ paths, io = {} } = {}) {
|
|
295
|
+
const d = depsOf(io)
|
|
296
|
+
const self = d.pid()
|
|
297
|
+
d.remove(userFile(paths, self))
|
|
298
|
+
return { others: otherUsers(paths, self, d).length }
|
|
299
|
+
}
|
package/browser/queue.mjs
CHANGED
|
@@ -25,8 +25,10 @@ export const ACTION_BUDGETS = {
|
|
|
25
25
|
touch: 30_000,
|
|
26
26
|
insert: 30_000,
|
|
27
27
|
clipboard: 30_000,
|
|
28
|
+
cookies: 30_000, // 取全量时随条数线性,仍在本帽内(§2.9)
|
|
28
29
|
type: 15_000, // 单页内快动作
|
|
29
30
|
snapshot: 15_000,
|
|
31
|
+
storage: 15_000, // 单次页内求值(§2.9)
|
|
30
32
|
wait: 45_000, // = 谓词帽缺省 30s + 15s 余量(timeoutMs 给出时按帽 + 余量重解——§2.9 表)
|
|
31
33
|
}
|
|
32
34
|
export const BUDGET_FALLBACK_MS = 30_000 // 未在册动作的兜底预算(防御——全部在册动作已列)
|