@jjh32456/longlive 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +235 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +362 -0
- package/README.en.md +64 -0
- package/README.md +256 -2
- package/SPEC.md +211 -0
- package/build-info.json +1665 -0
- package/cordis.patch.yml +208 -0
- package/docs/A1-MIGRATION.md +75 -0
- package/docs/ANSWER-GUARD.md +92 -0
- package/docs/ARCHITECTURE.md +128 -0
- package/docs/AUTO-RECALL-WIRING.md +115 -0
- package/docs/AUTO-RECALL.md +237 -0
- package/docs/CONCEPTS.md +143 -0
- package/docs/CONFIG.md +839 -0
- package/docs/ERRORS.md +271 -0
- package/docs/FAQ.md +186 -0
- package/docs/FEATURES.md +579 -0
- package/docs/INDEX-FRESHNESS.md +21 -0
- package/docs/METRICS.md +244 -0
- package/docs/MIGRATION-1.6.0.md +220 -0
- package/docs/OPERATIONS.md +439 -0
- package/docs/PANEL-INTEGRATION.md +190 -0
- package/docs/PRIVACY.md +88 -0
- package/docs/QUICKSTART.md +155 -0
- package/docs/RAW-INDEX-POLICY.md +7 -0
- package/docs/README.en.md +48 -0
- package/docs/RESIDENT-WATCHER.md +90 -0
- package/docs/SESSION-WATCH.md +214 -0
- package/docs/TOOLS.md +184 -0
- package/docs/TROUBLESHOOTING.md +329 -0
- package/icon.svg +11 -0
- package/lib/client.js +436 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +163 -4
- package/runbook.md +343 -0
- package/scripts/archive-digest-build.mjs +668 -0
- package/scripts/auto-truncate-run.ps1 +752 -0
- package/scripts/backfill-auto-cards.mjs +98 -0
- package/scripts/edges-build.mjs +1003 -0
- package/scripts/index-maintain.mjs +39 -0
- package/scripts/index-reconcile.mjs +49 -0
- package/scripts/index-status.mjs +28 -0
- package/scripts/install-resident-watcher.ps1 +15 -0
- package/scripts/install-session-watch.ps1 +332 -0
- package/scripts/install-surgery-task.ps1 +174 -0
- package/scripts/lib/auto-truncate-run.ps1 +512 -0
- package/scripts/lib/session-watch-fixture.mjs +207 -0
- package/scripts/longlive-doctor.mjs +578 -0
- package/scripts/metrics-read.mjs +79 -0
- package/scripts/metrics-sample.mjs +215 -0
- package/scripts/migrate-180.mjs +163 -0
- package/scripts/migrate-200.mjs +8 -0
- package/scripts/migrate-a1-legacy.mjs +62 -0
- package/scripts/migrate-lifecycle.mjs +157 -0
- package/scripts/pack.mjs +392 -0
- package/scripts/recall-index-build.mjs +128 -0
- package/scripts/recall-links-build.mjs +420 -0
- package/scripts/rescore-cards.mjs +36 -0
- package/scripts/resident-watcher-status.ps1 +15 -0
- package/scripts/resident-watcher.ps1 +643 -0
- package/scripts/session-watch-drill.mjs +553 -0
- package/scripts/session-watch-status.ps1 +146 -0
- package/scripts/session-watch.mjs +1367 -0
- package/scripts/surgery-inspect.mjs +122 -0
- package/scripts/surgery-run.cmd +34 -0
- package/scripts/truncate-cli.mjs +279 -0
- package/scripts/unattended-surgery.ps1 +1019 -0
- package/scripts/uninstall-resident-watcher.ps1 +15 -0
- package/scripts/uninstall-session-watch.ps1 +192 -0
- package/scripts/verify-card-anchors.mjs +138 -0
- package/scripts/watch-loop.ps1 +916 -0
- package/src/actions/audit.js +125 -0
- package/src/actions/evidence.js +122 -0
- package/src/actions/fs.js +162 -0
- package/src/activity/index.js +873 -0
- package/src/activity/model.js +351 -0
- package/src/activity/store.js +786 -0
- package/src/activity/summary.js +180 -0
- package/src/activity/thinking.js +191 -0
- package/src/adapters/constants.js +11 -0
- package/src/adapters/engram/data/aliases.json +51 -0
- package/src/adapters/engram/data/policy.json +8 -0
- package/src/adapters/engram/data/routing-rules.json +546 -0
- package/src/adapters/engram/index.js +278 -0
- package/src/adapters/registry.js +315 -0
- package/src/answer/audit.js +238 -0
- package/src/answer/claim-guard.mjs +234 -0
- package/src/answer/evidence.js +30 -0
- package/src/answer/guard.js +1370 -0
- package/src/answer/index.js +1086 -0
- package/src/answer/query.js +111 -0
- package/src/answer/second-hop.mjs +101 -0
- package/src/auto-truncate-status.js +164 -0
- package/src/auto-truncate.js +1181 -0
- package/src/autoprocedure/candidate.js +342 -0
- package/src/autoprocedure/config.js +122 -0
- package/src/autoprocedure/evidence.js +345 -0
- package/src/autoprocedure/index.js +83 -0
- package/src/autoprocedure/ledger.js +329 -0
- package/src/autoprocedure/lint.js +232 -0
- package/src/autoprocedure/llm.js +409 -0
- package/src/autoprocedure/locks.js +102 -0
- package/src/autoprocedure/paths.js +39 -0
- package/src/autoprocedure/privacy.js +87 -0
- package/src/autoprocedure/rules.js +288 -0
- package/src/autoprocedure/runtime.js +718 -0
- package/src/autoprocedure/session-reader.js +328 -0
- package/src/backup.js +310 -0
- package/src/bus/bus.mjs +1446 -0
- package/src/codec.js +269 -0
- package/src/compaction/cut-engine.js +44 -0
- package/src/compaction/engine-core.js +1237 -0
- package/src/compaction/entry.js +228 -0
- package/src/compaction.js +230 -0
- package/src/conclusion.js +366 -0
- package/src/config.js +835 -0
- package/src/d6/runtime.js +517 -0
- package/src/d6/surface.js +181 -0
- package/src/deps.js +189 -0
- package/src/engine-status.js +141 -0
- package/src/features.js +1035 -0
- package/src/handoff/index.js +1077 -0
- package/src/handoff/model.js +1058 -0
- package/src/handoff/store.js +1233 -0
- package/src/heartbeat.js +385 -0
- package/src/home.js +16 -0
- package/src/host-version.js +280 -0
- package/src/hot-truncate-diag.js +98 -0
- package/src/identity/card-marker.js +281 -0
- package/src/identity/handover.js +845 -0
- package/src/identity/index.js +337 -0
- package/src/identity/inject-position.js +133 -0
- package/src/identity/writeback.js +344 -0
- package/src/identity.js +118 -0
- package/src/index.js +647 -0
- package/src/lifecycle/gap-clocks.js +364 -0
- package/src/lifecycle/index.js +269 -0
- package/src/lifecycle/proactive.js +1426 -0
- package/src/live-target.js +212 -0
- package/src/live.js +1603 -0
- package/src/log-rotation.js +153 -0
- package/src/memory/a1-migration.js +190 -0
- package/src/memory/activity-capture.js +229 -0
- package/src/memory/anchor-fallback.js +116 -0
- package/src/memory/anchors.js +194 -0
- package/src/memory/archive-cold-state.mjs +794 -0
- package/src/memory/archive-staging.mjs +305 -0
- package/src/memory/archive.mjs +415 -0
- package/src/memory/audit-records.js +222 -0
- package/src/memory/auto-cards.js +441 -0
- package/src/memory/capture-host.js +297 -0
- package/src/memory/capture.js +496 -0
- package/src/memory/card-anchors.js +203 -0
- package/src/memory/card-priority.js +110 -0
- package/src/memory/cards.js +425 -0
- package/src/memory/catchup.mjs +534 -0
- package/src/memory/conclusion-distill.js +381 -0
- package/src/memory/conclusion-drafts.js +243 -0
- package/src/memory/conflicts.js +1060 -0
- package/src/memory/consolidate.js +834 -0
- package/src/memory/decay.js +311 -0
- package/src/memory/distill.js +485 -0
- package/src/memory/event-index.js +1826 -0
- package/src/memory/evidence-chain.js +295 -0
- package/src/memory/evidence.js +213 -0
- package/src/memory/filter-runner.js +555 -0
- package/src/memory/filter.js +1035 -0
- package/src/memory/forget-v2.js +680 -0
- package/src/memory/forget.js +531 -0
- package/src/memory/frames-activation.js +249 -0
- package/src/memory/frontmatter.js +454 -0
- package/src/memory/fts-query.js +342 -0
- package/src/memory/governance.js +604 -0
- package/src/memory/index-freshness.js +234 -0
- package/src/memory/index.js +435 -0
- package/src/memory/l2-gate.js +169 -0
- package/src/memory/land.mjs +743 -0
- package/src/memory/lifecycle.js +235 -0
- package/src/memory/lock.js +121 -0
- package/src/memory/maintenance.js +1149 -0
- package/src/memory/manifest.js +245 -0
- package/src/memory/memory-health.mjs +607 -0
- package/src/memory/migrate-200.js +1681 -0
- package/src/memory/migrate-lifecycle.js +1808 -0
- package/src/memory/migrate.js +139 -0
- package/src/memory/paths.js +229 -0
- package/src/memory/procedure-candidate.mjs +377 -0
- package/src/memory/procedure.js +982 -0
- package/src/memory/prospective-memory.mjs +362 -0
- package/src/memory/provenance.js +119 -0
- package/src/memory/recall-feedback.mjs +406 -0
- package/src/memory/recall-v2.js +218 -0
- package/src/memory/recall.js +357 -0
- package/src/memory/reconcile.js +265 -0
- package/src/memory/records.js +433 -0
- package/src/memory/reinforce.js +281 -0
- package/src/memory/retention.js +162 -0
- package/src/memory/review.js +124 -0
- package/src/memory/state.js +279 -0
- package/src/memory/tiers.js +168 -0
- package/src/memory/topic-phrases.js +73 -0
- package/src/memory/trust.js +97 -0
- package/src/memory/turn-budget.mjs +290 -0
- package/src/memory/version-chain.js +264 -0
- package/src/memory/wake-bridge.mjs +2999 -0
- package/src/memory/wal.js +137 -0
- package/src/memory/working-frame.js +779 -0
- package/src/metrics/classify.mjs +191 -0
- package/src/metrics/estimate.mjs +108 -0
- package/src/metrics/index.mjs +204 -0
- package/src/metrics/measure.mjs +462 -0
- package/src/metrics/prestep-latency.mjs +307 -0
- package/src/metrics/store.mjs +264 -0
- package/src/original.js +294 -0
- package/src/policy/threshold-policy.js +224 -0
- package/src/policy-profile.js +463 -0
- package/src/recall/anaphora.mjs +87 -0
- package/src/recall/archive-config.mjs +130 -0
- package/src/recall/archive-fallback.mjs +390 -0
- package/src/recall/archive-search.mjs +428 -0
- package/src/recall/archive-state.mjs +588 -0
- package/src/recall/brain.mjs +501 -0
- package/src/recall/cards.mjs +202 -0
- package/src/recall/current-line.mjs +110 -0
- package/src/recall/edges.js +398 -0
- package/src/recall/embedding.mjs +698 -0
- package/src/recall/entities.mjs +112 -0
- package/src/recall/explain.mjs +443 -0
- package/src/recall/force-policy.mjs +138 -0
- package/src/recall/gate-log.mjs +280 -0
- package/src/recall/gate-metrics.mjs +191 -0
- package/src/recall/index.js +1337 -0
- package/src/recall/inject-order.mjs +279 -0
- package/src/recall/link-recall.mjs +183 -0
- package/src/recall/links.mjs +696 -0
- package/src/recall/privacy.mjs +208 -0
- package/src/recall/recall-core.mjs +810 -0
- package/src/recall/shared-visibility.js +302 -0
- package/src/recall/time-normalizer.mjs +196 -0
- package/src/recall/turn-budget-inject.mjs +154 -0
- package/src/retention/cold-gate.mjs +146 -0
- package/src/retention/config.js +163 -0
- package/src/retention/index.js +801 -0
- package/src/role/activation.js +191 -0
- package/src/role/attention.js +318 -0
- package/src/role/base.js +36 -0
- package/src/role/channels.js +423 -0
- package/src/role/index.js +425 -0
- package/src/role/interfaces.js +115 -0
- package/src/role/personality.js +370 -0
- package/src/role/readers.js +151 -0
- package/src/role/relationships.js +477 -0
- package/src/role/store.js +202 -0
- package/src/role/writeprotect.js +373 -0
- package/src/router/aliases.json +182 -0
- package/src/router/background/agent-runner.mjs +239 -0
- package/src/router/background/anchor.mjs +323 -0
- package/src/router/background/audit.mjs +337 -0
- package/src/router/background/budget-ledger.mjs +648 -0
- package/src/router/background/host-sim.mjs +451 -0
- package/src/router/background/index.mjs +81 -0
- package/src/router/background/memory.mjs +63 -0
- package/src/router/background/mock-llm.mjs +203 -0
- package/src/router/background/queue.mjs +224 -0
- package/src/router/background/retry.mjs +255 -0
- package/src/router/background/router.mjs +1459 -0
- package/src/router/background/stream.mjs +276 -0
- package/src/router/background/tool-policy.mjs +255 -0
- package/src/router/design-headings.json +221 -0
- package/src/router/face/config.mjs +155 -0
- package/src/router/face/context.mjs +284 -0
- package/src/router/face/face.mjs +466 -0
- package/src/router/face/identity.mjs +130 -0
- package/src/router/face/index.mjs +65 -0
- package/src/router/face/journal.mjs +119 -0
- package/src/router/face/llm.mjs +252 -0
- package/src/router/face/outbox.mjs +153 -0
- package/src/router/face/paths.mjs +54 -0
- package/src/router/judge.mjs +987 -0
- package/src/router/policies/generic.json +131 -0
- package/src/router/route.js +1766 -0
- package/src/router/router.mjs +535 -0
- package/src/router/routing-rules.json +816 -0
- package/src/router/run-sim.mjs +530 -0
- package/src/router/runtime.js +569 -0
- package/src/router/service/apply.mjs +1218 -0
- package/src/router/service/board.mjs +712 -0
- package/src/router/service/caller.mjs +131 -0
- package/src/router/service/config.mjs +361 -0
- package/src/router/service/context.mjs +314 -0
- package/src/router/service/hint.mjs +668 -0
- package/src/router/service/lease.mjs +494 -0
- package/src/router/service/live.mjs +251 -0
- package/src/router/service/lock.mjs +550 -0
- package/src/router/service/merge.mjs +255 -0
- package/src/router/service/model-resolve.mjs +350 -0
- package/src/router/service/paths.mjs +168 -0
- package/src/router/service/proposals.mjs +1461 -0
- package/src/router/service/queue.mjs +477 -0
- package/src/router/service/reconcile.mjs +271 -0
- package/src/router/service/service.mjs +2998 -0
- package/src/router/service/sources.mjs +352 -0
- package/src/router/service/status.mjs +520 -0
- package/src/router/service/store.mjs +476 -0
- package/src/router/service/ticklock.mjs +239 -0
- package/src/router/service/tokens.mjs +95 -0
- package/src/router/service/tools.mjs +248 -0
- package/src/router/service/wake.mjs +544 -0
- package/src/router/service/wal.mjs +685 -0
- package/src/router/shadow.mjs +821 -0
- package/src/surgery-recovery.js +1909 -0
- package/src/tool-output.js +485 -0
- package/src/tools.js +3262 -0
- package/src/truncate-visible.js +2416 -0
- package/src/truncate.js +999 -0
- package/src/ui/actions.js +1312 -0
- package/src/ui/flags.js +259 -0
- package/src/ui/panel-api.js +371 -0
- package/src/ui/panel-auth.js +251 -0
- package/src/ui/panel-integrity.js +243 -0
- package/src/ui/routes-readonly.js +759 -0
- package/src/ui/routes.js +284 -0
- package/src/util/append-store.js +368 -0
- package/src/util/local-day.js +30 -0
- package/src/util/redact.js +227 -0
- package/src/util/stale-locks.js +148 -0
- package/src/version.js +99 -0
- package/test/fixtures/pre-step-container.mjs +100 -0
- package/test/p1_probe.mjs +97 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
// stream.mjs — 按宿主 ctx.llm.stream() 的真实契约消费 AsyncIterable<StreamChunk>
|
|
2
|
+
//
|
|
3
|
+
// 宿主契约(_hostsrc/dsh-llm,README:47-57 / :93,assembler.js:33-84):
|
|
4
|
+
// - 请求 messages 的 content 是 block 数组:[{ type:'text', text }]
|
|
5
|
+
// - chunk 类型:block-start / text-delta / reasoning-delta /
|
|
6
|
+
// tool-call-delta / block-end / usage / finish
|
|
7
|
+
// - 每个流恰好以一个 finish 结束;失败是 { kind:'error', failure },
|
|
8
|
+
// 取消是 { kind:'aborted', failure },failure 在 chunk.reason.failure 里
|
|
9
|
+
// - usage 是 camelCase:{ inputTokens, outputTokens, cacheReadTokens, ... }
|
|
10
|
+
// - 服务本身不重试;RATE_LIMIT 等退避由 retry.mjs 负责
|
|
11
|
+
//
|
|
12
|
+
// 本模块只做「消费」,不重试、不执行动作;missing finish 视为协议错误,
|
|
13
|
+
// 由 Router 记成 task.error,而不是让 tick reject。
|
|
14
|
+
|
|
15
|
+
export const STREAM_PROTOCOL_ERRORS = Object.freeze([
|
|
16
|
+
'E_STREAM_NOT_ASYNC_ITERABLE',
|
|
17
|
+
'E_STREAM_NO_FINISH',
|
|
18
|
+
'E_STREAM_CHUNK_LIMIT',
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 保留原始数字,不把 NaN 洗成 0(F12:NaN usage 曾被当免费)。
|
|
23
|
+
* 缺失字段按 0;出现非有限数/负数时只打 invalid 标记,由 Router fail-closed。
|
|
24
|
+
*/
|
|
25
|
+
const asNumber = (value, state) => {
|
|
26
|
+
if (value === undefined || value === null) return 0;
|
|
27
|
+
const n = Number(value);
|
|
28
|
+
if (!Number.isFinite(n) || n < 0) state.invalid = true;
|
|
29
|
+
return Number.isFinite(n) ? n : NaN;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** 把 chunk.usage 规范成 camelCase 计数;兼容 snake_case mock,并打 legacy 标记。 */
|
|
33
|
+
export function normalizeUsage(raw) {
|
|
34
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
35
|
+
const state = { invalid: false };
|
|
36
|
+
const legacy = !('inputTokens' in raw) && ('input_tokens' in raw);
|
|
37
|
+
const inputTokens = asNumber(raw.inputTokens ?? raw.input_tokens, state);
|
|
38
|
+
const outputTokens = asNumber(raw.outputTokens ?? raw.output_tokens, state);
|
|
39
|
+
const cacheReadTokens = asNumber(raw.cacheReadTokens ?? raw.cache_read_tokens, state);
|
|
40
|
+
const cacheWriteTokens = asNumber(raw.cacheWriteTokens ?? raw.cache_write_tokens, state);
|
|
41
|
+
const explicitTotal = raw.totalTokens ?? raw.total_tokens;
|
|
42
|
+
const totalTokens = explicitTotal === undefined
|
|
43
|
+
? inputTokens + outputTokens + cacheReadTokens + cacheWriteTokens
|
|
44
|
+
: asNumber(explicitTotal, state);
|
|
45
|
+
return {
|
|
46
|
+
inputTokens,
|
|
47
|
+
outputTokens,
|
|
48
|
+
cacheReadTokens,
|
|
49
|
+
cacheWriteTokens,
|
|
50
|
+
totalTokens,
|
|
51
|
+
legacy,
|
|
52
|
+
invalid: state.invalid,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 构造宿主接受的 messages:content 必须是 block 数组。 */
|
|
57
|
+
export function buildMessages({ system, prompt }) {
|
|
58
|
+
const messages = [];
|
|
59
|
+
if (typeof system === 'string' && system.trim() !== '') {
|
|
60
|
+
messages.push({ role: 'system', content: [{ type: 'text', text: system }] });
|
|
61
|
+
}
|
|
62
|
+
messages.push({ role: 'user', content: [{ type: 'text', text: String(prompt ?? '') }] });
|
|
63
|
+
return messages;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function normalizeFailure(failure, fallbackCode) {
|
|
67
|
+
if (failure && typeof failure === 'object') {
|
|
68
|
+
return {
|
|
69
|
+
code: typeof failure.code === 'string' && failure.code.length > 0 ? failure.code : fallbackCode,
|
|
70
|
+
message: typeof failure.message === 'string' ? failure.message : String(failure.message ?? fallbackCode),
|
|
71
|
+
status: failure.status,
|
|
72
|
+
providerRetryAfterMs: failure.providerRetryAfterMs,
|
|
73
|
+
requestId: failure.requestId,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return { code: fallbackCode, message: typeof failure === 'string' ? failure : fallbackCode };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function normalizeFinish(reason) {
|
|
80
|
+
if (reason && typeof reason === 'object' && typeof reason.kind === 'string') return reason;
|
|
81
|
+
return { kind: typeof reason === 'string' ? reason : 'unknown' };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 消费一个 AsyncIterable<StreamChunk>。
|
|
86
|
+
*
|
|
87
|
+
* @param {AsyncIterable} stream
|
|
88
|
+
* @param {object} [options]
|
|
89
|
+
* @param {AbortSignal} [options.signal]
|
|
90
|
+
* @param {(chunk: object) => void} [options.onDelta]
|
|
91
|
+
* @param {number} [options.maxChunks] 防御性上限
|
|
92
|
+
* @param {number} [options.drainTimeoutMs] abort 后等待下一个 chunk 的宽限
|
|
93
|
+
* @param {number} [options.maxDrainChunks] abort 后最多再吞多少 chunk
|
|
94
|
+
* @param {number} [options.returnTimeoutMs] 关闭迭代器的上限(防 P10 永挂)
|
|
95
|
+
*/
|
|
96
|
+
export async function consumeStream(stream, {
|
|
97
|
+
signal,
|
|
98
|
+
onDelta,
|
|
99
|
+
maxChunks = 20000,
|
|
100
|
+
drainTimeoutMs = 50,
|
|
101
|
+
maxDrainChunks = 1000,
|
|
102
|
+
returnTimeoutMs = 100,
|
|
103
|
+
} = {}) {
|
|
104
|
+
if (!stream || typeof stream[Symbol.asyncIterator] !== 'function') {
|
|
105
|
+
const error = new Error('llm.stream() did not return an AsyncIterable');
|
|
106
|
+
error.code = 'E_STREAM_NOT_ASYNC_ITERABLE';
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
111
|
+
let aborted = Boolean(signal?.aborted);
|
|
112
|
+
let finished = false;
|
|
113
|
+
let finishReason = null;
|
|
114
|
+
let text = '';
|
|
115
|
+
let usage = null;
|
|
116
|
+
let chunks = 0;
|
|
117
|
+
let drained = 0;
|
|
118
|
+
let drainTimedOut = false;
|
|
119
|
+
let returnTimedOut = false;
|
|
120
|
+
|
|
121
|
+
let pendingNext = null;
|
|
122
|
+
const pull = () => {
|
|
123
|
+
if (pendingNext === null) {
|
|
124
|
+
pendingNext = iterator.next().finally(() => { pendingNext = null; });
|
|
125
|
+
}
|
|
126
|
+
return pendingNext;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
let resolveAbort;
|
|
130
|
+
const abortGate = new Promise((resolve) => { resolveAbort = resolve; });
|
|
131
|
+
const onAbort = () => {
|
|
132
|
+
aborted = true;
|
|
133
|
+
resolveAbort({ abort: true });
|
|
134
|
+
};
|
|
135
|
+
if (signal) {
|
|
136
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
137
|
+
if (signal.aborted) onAbort();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const withTimeout = (promise, timeoutMs) => {
|
|
141
|
+
let timer;
|
|
142
|
+
const timeout = new Promise((resolve) => {
|
|
143
|
+
timer = setTimeout(() => resolve({ timeout: true }), timeoutMs);
|
|
144
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
145
|
+
});
|
|
146
|
+
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
while (true) {
|
|
151
|
+
if (chunks >= maxChunks) {
|
|
152
|
+
const error = new Error(`stream exceeded ${maxChunks} chunks`);
|
|
153
|
+
error.code = 'E_STREAM_CHUNK_LIMIT';
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let stepped;
|
|
158
|
+
try {
|
|
159
|
+
if (aborted) {
|
|
160
|
+
stepped = await withTimeout(
|
|
161
|
+
pull().then((value) => ({ value }), (error) => ({ error })),
|
|
162
|
+
drainTimeoutMs,
|
|
163
|
+
);
|
|
164
|
+
if (stepped.timeout) {
|
|
165
|
+
drainTimedOut = true;
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
if (stepped.error) {
|
|
169
|
+
if (stepped.error?.name === 'AbortError' || stepped.error?.code === 'ABORTED') break;
|
|
170
|
+
throw stepped.error;
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
stepped = await Promise.race([
|
|
174
|
+
pull().then((value) => ({ value }), (error) => ({ error })),
|
|
175
|
+
abortGate,
|
|
176
|
+
]);
|
|
177
|
+
if (stepped.abort) continue;
|
|
178
|
+
if (stepped.error) throw stepped.error;
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
if (aborted && (error?.name === 'AbortError' || error?.code === 'ABORTED')) break;
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const step = stepped.value;
|
|
186
|
+
if (!step || step.done) break;
|
|
187
|
+
const chunk = step.value;
|
|
188
|
+
chunks += 1;
|
|
189
|
+
|
|
190
|
+
// terminal chunk:即使正在 abort,也要把 finish 收掉。
|
|
191
|
+
if (chunk?.type === 'finish') {
|
|
192
|
+
finishReason = normalizeFinish(chunk.reason);
|
|
193
|
+
finished = true;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (aborted) {
|
|
198
|
+
drained += 1;
|
|
199
|
+
if (chunk?.type === 'usage') usage = normalizeUsage(chunk.usage) ?? usage;
|
|
200
|
+
if (drained >= maxDrainChunks) break;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
switch (chunk?.type) {
|
|
205
|
+
case 'text-delta':
|
|
206
|
+
if (typeof chunk.text === 'string') text += chunk.text;
|
|
207
|
+
onDelta?.(chunk);
|
|
208
|
+
break;
|
|
209
|
+
case 'usage':
|
|
210
|
+
usage = normalizeUsage(chunk.usage) ?? usage;
|
|
211
|
+
break;
|
|
212
|
+
default:
|
|
213
|
+
// block-start / block-end / reasoning-delta / tool-call-delta 等
|
|
214
|
+
// 由上层按需处理;这里保持前向兼容,不因未知 chunk 打断。
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
} finally {
|
|
219
|
+
if (signal) signal.removeEventListener?.('abort', onAbort);
|
|
220
|
+
if (pendingNext) {
|
|
221
|
+
pendingNext.catch(() => { /* the abandoned next() must not reject unhandled */ });
|
|
222
|
+
}
|
|
223
|
+
// 即使已经收到 finish,也关闭迭代器让生成器的 finally 执行;
|
|
224
|
+
// 但 return() 必须有时限:复核 P10 的生成器 return() 永不 resolve,
|
|
225
|
+
// 旧实现让整个 tick(以及 stop 的 awaitInflight)永久挂住。
|
|
226
|
+
if (typeof iterator.return === 'function') {
|
|
227
|
+
try {
|
|
228
|
+
const closing = Promise.resolve().then(() => iterator.return());
|
|
229
|
+
closing.catch(() => { /* 关闭失败不能变成未处理 rejection */ });
|
|
230
|
+
const outcome = await withTimeout(closing.then(() => ({ closed: true }), (error) => ({ error })), returnTimeoutMs);
|
|
231
|
+
if (outcome.timeout) returnTimedOut = true;
|
|
232
|
+
} catch {
|
|
233
|
+
/* best-effort:关闭迭代器失败不应吞掉已经收到的 finish/usage */
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
let failure = null;
|
|
239
|
+
let missingFinish = false;
|
|
240
|
+
|
|
241
|
+
if (finished) {
|
|
242
|
+
const kind = finishReason?.kind;
|
|
243
|
+
if (kind === 'error') {
|
|
244
|
+
failure = normalizeFailure(finishReason.failure, 'E_STREAM_FAILURE');
|
|
245
|
+
} else if (kind === 'aborted') {
|
|
246
|
+
aborted = true;
|
|
247
|
+
failure = normalizeFailure(finishReason.failure, 'E_ABORTED');
|
|
248
|
+
}
|
|
249
|
+
} else {
|
|
250
|
+
missingFinish = true;
|
|
251
|
+
if (aborted) {
|
|
252
|
+
failure = { code: 'E_ABORTED', message: 'stream aborted before finish' };
|
|
253
|
+
} else {
|
|
254
|
+
failure = {
|
|
255
|
+
code: 'E_STREAM_NO_FINISH',
|
|
256
|
+
message: 'stream ended without a terminal finish chunk',
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
ok: !failure && !aborted,
|
|
263
|
+
text,
|
|
264
|
+
usage,
|
|
265
|
+
finish: finishReason,
|
|
266
|
+
failure,
|
|
267
|
+
aborted,
|
|
268
|
+
missingFinish,
|
|
269
|
+
finished,
|
|
270
|
+
chunks,
|
|
271
|
+
drained,
|
|
272
|
+
drainTimedOut,
|
|
273
|
+
returnTimedOut,
|
|
274
|
+
code: failure?.code ?? null,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// tool-policy.mjs — 后台 agent 的工具面:restrict 精确名字 + guard 同步 fail-closed
|
|
2
|
+
//
|
|
3
|
+
// 依据宿主 dsh-tools 的真实语义(_hostsrc/dsh-tools/lib/index.js):
|
|
4
|
+
// - restrict() 接受 { allow?, deny? } 两个可选集合;两个都不给才抛错
|
|
5
|
+
// (:2992-2994),空 deny 是合法的;名字会先编译成 Set。
|
|
6
|
+
// - 名字必须是「可 restrict 的已注册工具」;未知名字抛错(:3002-3004),
|
|
7
|
+
// run_code 明确禁止出现在 allow/deny 里(:2997-3000)。
|
|
8
|
+
// - 可 restrict 的名字在 view(scope).restrictableNames 上,而且是 **Set**,
|
|
9
|
+
// 不是 tools 服务上的数组属性(:3051-3077)。service 本身没有
|
|
10
|
+
// `restrictableNames`。
|
|
11
|
+
// - guard() 是同步检查,返回字符串即拒绝;全局 guard 先跑,然后 agent 链
|
|
12
|
+
// 由远及近(guardReason :3020-3028)。
|
|
13
|
+
//
|
|
14
|
+
// 复核 P0-1 的教训:旧实现要求 `Array.isArray(tools.restrictableNames)`,
|
|
15
|
+
// 在真宿主上 100% 抛 E_TOOL_SURFACE。现在的顺序是:
|
|
16
|
+
// 1. 优先从 view(scope).restrictableNames(Set)枚举;
|
|
17
|
+
// 2. 退回旧形状的 service.restrictableNames(Array 或 Set);
|
|
18
|
+
// 3. 两者都没有(或 view 不可用)时不再自己枚举,直接 restrict({allow})
|
|
19
|
+
// —— 宿主会在内部按 known 集合校验,未知名字照样抛错;
|
|
20
|
+
// 无论哪条路都保留 guard 作为第二层(run_code / 未来新增写类工具 fail-closed)。
|
|
21
|
+
|
|
22
|
+
/** 从 _hostsrc 各 dsh-tool-* 包实际 dump 到的注册名(见 tools/asar-index.txt)。 */
|
|
23
|
+
export const REGISTERED_TOOL_NAMES = Object.freeze([
|
|
24
|
+
// filesystem
|
|
25
|
+
'read', 'write', 'edit', 'read_image', 'glob', 'grep', 'str_replace_editor',
|
|
26
|
+
// shell / terminal
|
|
27
|
+
'bash', 'pwsh', 'terminal_open', 'terminal_send', 'terminal_read',
|
|
28
|
+
'terminal_signal', 'terminal_close', 'terminal_list',
|
|
29
|
+
// jobs
|
|
30
|
+
'job_output', 'job_list', 'job_kill',
|
|
31
|
+
// delegation / control
|
|
32
|
+
'subagent', 'list_subagent_models', 'send_message', 'interrupt_agent',
|
|
33
|
+
'workflow', 'ralph',
|
|
34
|
+
// schedule
|
|
35
|
+
'schedule_create', 'schedule_list', 'schedule_delete', 'schedule_update',
|
|
36
|
+
// introspection / session
|
|
37
|
+
'cordis_inspect_list', 'cordis_inspect_query',
|
|
38
|
+
'session_search', 'session_event_search', 'session_trace',
|
|
39
|
+
'session_event_trace', 'session_event_read',
|
|
40
|
+
// misc
|
|
41
|
+
'ask_user_question', 'get_goal', 'create_goal', 'update_goal', 'present',
|
|
42
|
+
'working_directory', 'load_workspace_dependencies', 'todo_write', 'skill',
|
|
43
|
+
'web_search', 'web_fetch',
|
|
44
|
+
// reserved PTC transport (cannot be named in restrict)
|
|
45
|
+
'run_code',
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
/** 后台 agent 唯一允许的工具:只读、无副作用。 */
|
|
49
|
+
export const READ_ONLY_TOOL_ALLOWLIST = Object.freeze(['read', 'glob', 'grep', 'read_image']);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 文档/测试用的静态 deny 列表:所有真实注册名 - 只读白名单 - run_code。
|
|
53
|
+
* 运行时优先从 view(scope) 动态算;此表用于核对策略覆盖。
|
|
54
|
+
*/
|
|
55
|
+
export const RESTRICT_DENY = Object.freeze(
|
|
56
|
+
REGISTERED_TOOL_NAMES.filter(
|
|
57
|
+
(name) => name !== 'run_code' && !READ_ONLY_TOOL_ALLOWLIST.includes(name),
|
|
58
|
+
),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const SPAWN_NAMES = new Set(['subagent', 'send_message', 'interrupt_agent', 'workflow', 'ralph']);
|
|
62
|
+
const SHELL_NAMES = new Set(['bash', 'pwsh']);
|
|
63
|
+
const FS_WRITE_NAMES = new Set(['write', 'edit', 'str_replace_editor', 'apply_patch', 'multi_edit']);
|
|
64
|
+
|
|
65
|
+
/** 精确名字 -> 拒绝原因;未知名字一律落到 not_allowlisted(fail-closed)。 */
|
|
66
|
+
export function denyReason(name) {
|
|
67
|
+
if (typeof name !== 'string' || name.length === 0) return 'tool_denied: missing tool name';
|
|
68
|
+
if (SPAWN_NAMES.has(name) || name.startsWith('subagent_') || name.startsWith('worker_') || name === 'list_agents') {
|
|
69
|
+
return `tool_denied: "${name}" is a delegation/recursion tool; background agents may not spawn or control agents`;
|
|
70
|
+
}
|
|
71
|
+
if (name === 'run_code') {
|
|
72
|
+
return 'tool_denied: "run_code" is the PTC transport and can execute arbitrary code; never available to a background agent';
|
|
73
|
+
}
|
|
74
|
+
if (SHELL_NAMES.has(name) || name.startsWith('terminal_')) {
|
|
75
|
+
return `tool_denied: "${name}" is a shell/terminal tool; background agents are not allowed to execute commands`;
|
|
76
|
+
}
|
|
77
|
+
if (FS_WRITE_NAMES.has(name)) {
|
|
78
|
+
return `tool_denied: "${name}" writes the filesystem; background agents are read-only`;
|
|
79
|
+
}
|
|
80
|
+
if (name.startsWith('schedule_')) {
|
|
81
|
+
return `tool_denied: "${name}" mutates persistent schedules; background agents may not schedule work`;
|
|
82
|
+
}
|
|
83
|
+
if (name === 'spawn_router' || name === 'router.spawn' || name === 'router.start') {
|
|
84
|
+
return `tool_denied: "${name}" is a Router recursion entry point`;
|
|
85
|
+
}
|
|
86
|
+
if (name === 'queue_append' || name === 'router_enqueue' || name === 'queue_write') {
|
|
87
|
+
return `tool_denied: "${name}" writes the trusted task queue`;
|
|
88
|
+
}
|
|
89
|
+
return `tool_denied: "${name}" is not in the fail-closed read-only allowlist`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 同步 guard 判定。返回字符串 = 拒绝;undefined = 允许。
|
|
94
|
+
* exec 形状按 dsh-tools createExecution:{ name, agent?, ... }。
|
|
95
|
+
*/
|
|
96
|
+
export function guardReason(exec, { allowedTools = READ_ONLY_TOOL_ALLOWLIST, agentId = null } = {}) {
|
|
97
|
+
if (!exec || typeof exec.name !== 'string' || exec.name.length === 0) {
|
|
98
|
+
return 'tool_denied: execution has no tool name';
|
|
99
|
+
}
|
|
100
|
+
if (agentId && exec.agent && exec.agent.id !== agentId) return undefined;
|
|
101
|
+
if (allowedTools.includes(exec.name)) return undefined;
|
|
102
|
+
return denyReason(exec.name);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 把 Set / Array / 其它 iterable 统一成 Set;不可迭代返回 null。 */
|
|
106
|
+
export function toNameSet(value) {
|
|
107
|
+
if (value instanceof Set) return value;
|
|
108
|
+
if (Array.isArray(value)) return new Set(value);
|
|
109
|
+
if (value && typeof value === 'object' && typeof value[Symbol.iterator] === 'function') {
|
|
110
|
+
try { return new Set(value); } catch { return null; }
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 解析「哪些名字可以被 restrict」。真实宿主只有 view(scope).restrictableNames
|
|
117
|
+
* (Set);为了兼容旧 host-sim 与降级场景,也接受 service 上的 Array/Set。
|
|
118
|
+
*
|
|
119
|
+
* @returns {{names: Set<string>|null, source: string, view: object|null}}
|
|
120
|
+
*/
|
|
121
|
+
export function resolveRestrictableView(tools, agentCtx, agent = null) {
|
|
122
|
+
if (typeof tools?.view === 'function') {
|
|
123
|
+
// 真宿主的 agent.ctx 上没有 .agent(dsh-agent-loop/lib/index.js:771-789),
|
|
124
|
+
// scope 必须显式传 agent 本体;只有旧 host-sim / 测试才把 agent 挂在 ctx 上。
|
|
125
|
+
const scope = agent ?? agentCtx?.agent ?? agentCtx;
|
|
126
|
+
let view = null;
|
|
127
|
+
try {
|
|
128
|
+
view = tools.view(scope);
|
|
129
|
+
} catch {
|
|
130
|
+
view = null;
|
|
131
|
+
}
|
|
132
|
+
if (view) {
|
|
133
|
+
const names = toNameSet(view.restrictableNames);
|
|
134
|
+
if (names) return { names, source: 'view.restrictableNames', view };
|
|
135
|
+
const visible = view.visible instanceof Map
|
|
136
|
+
? view.visible.keys()
|
|
137
|
+
: (view.visible && typeof view.visible.keys === 'function' ? view.visible.keys() : null);
|
|
138
|
+
const fallback = toNameSet(visible);
|
|
139
|
+
if (fallback) return { names: fallback, source: 'view.visible', view };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const direct = toNameSet(tools?.restrictableNames);
|
|
143
|
+
if (direct) return { names: direct, source: 'service.restrictableNames', view: null };
|
|
144
|
+
return { names: null, source: 'none', view: null };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** 显式 deny 列表的 fail-closed 校验:未知名字 / 通配符 / run_code 都抛。 */
|
|
148
|
+
export function validateDenyNames(denyNames, knownNames = null) {
|
|
149
|
+
if (!Array.isArray(denyNames)) {
|
|
150
|
+
const error = new Error('E_TOOL_POLICY: denyNames must be an array');
|
|
151
|
+
error.code = 'E_TOOL_POLICY';
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
const known = knownNames instanceof Set ? knownNames : toNameSet(knownNames);
|
|
155
|
+
for (const name of denyNames) {
|
|
156
|
+
if (typeof name !== 'string' || name.length === 0) {
|
|
157
|
+
const error = new Error('E_TOOL_POLICY: denyNames entries must be non-empty strings');
|
|
158
|
+
error.code = 'E_TOOL_POLICY';
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
if (name.includes('*')) {
|
|
162
|
+
const error = new Error(`E_TOOL_POLICY: wildcard "${name}" is not an exact registered tool name`);
|
|
163
|
+
error.code = 'E_TOOL_POLICY';
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
if (name === 'run_code') {
|
|
167
|
+
const error = new Error('E_TOOL_POLICY: run_code cannot be named in tools.restrict()');
|
|
168
|
+
error.code = 'E_RUN_CODE_NOT_RESTRICTABLE';
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
// 拿不到 known 集合时只校验形状;宿主 restrict() 自己会拒绝未知名字。
|
|
172
|
+
if (known && !known.has(name)) {
|
|
173
|
+
const error = new Error(`E_UNKNOWN_RESTRICT_TOOL: "${name}" is not registered`);
|
|
174
|
+
error.code = 'E_UNKNOWN_RESTRICT_TOOL';
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 在后台 agent 的 scoped ctx 上安装 restrict + guard。
|
|
182
|
+
* setup 抛错 -> agents.create() 失败(fail-closed),不会带着半套权限发布 agent。
|
|
183
|
+
*
|
|
184
|
+
* @param {object} agentCtx 宿主 agent ctx(含 tools、agent)
|
|
185
|
+
* @param {object} [options]
|
|
186
|
+
* @param {string[]} [options.allowedTools]
|
|
187
|
+
* @param {string[]} [options.denyNames] 显式 deny 列表;会先做精确名字校验
|
|
188
|
+
* @param {object} [options.agent] 宿主 agent 本体(setup(ctx, agent) 的第二个参数);
|
|
189
|
+
* 真宿主 ctx 上没有 .agent,必须显式传,否则 view(scope) 解析不到 inherited 工具
|
|
190
|
+
* @returns {{deny:string[], allow:string[]|null, strategy:string, known:string[]|null, dispose:()=>void}}
|
|
191
|
+
*/
|
|
192
|
+
export function installToolPolicy(agentCtx, {
|
|
193
|
+
allowedTools = READ_ONLY_TOOL_ALLOWLIST,
|
|
194
|
+
denyNames = null,
|
|
195
|
+
agent = null,
|
|
196
|
+
} = {}) {
|
|
197
|
+
const tools = agentCtx?.tools;
|
|
198
|
+
if (!tools || typeof tools.restrict !== 'function' || typeof tools.guard !== 'function') {
|
|
199
|
+
const error = new Error('E_TOOL_SURFACE: agent ctx has no tools.restrict()/guard()');
|
|
200
|
+
error.code = 'E_TOOL_SURFACE';
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const surface = resolveRestrictableView(tools, agentCtx, agent);
|
|
205
|
+
let deny = [];
|
|
206
|
+
let allow = null;
|
|
207
|
+
let strategy;
|
|
208
|
+
|
|
209
|
+
if (denyNames !== null) {
|
|
210
|
+
validateDenyNames(denyNames, surface.names);
|
|
211
|
+
deny = [...denyNames];
|
|
212
|
+
strategy = 'explicit-deny';
|
|
213
|
+
} else if (surface.names && surface.names.size > 0) {
|
|
214
|
+
const allowed = new Set(allowedTools);
|
|
215
|
+
// run_code 不可被 restrict 命名;留给 guard 拒绝。
|
|
216
|
+
deny = [...surface.names].filter((name) => name !== 'run_code' && !allowed.has(name));
|
|
217
|
+
strategy = 'deny-from-known-names';
|
|
218
|
+
} else {
|
|
219
|
+
// 拿不到可 restrict 名字集合:把策略交给宿主的 allow 过滤器。
|
|
220
|
+
// 真宿主 restrict({allow}) 会拒绝 allow 里的未知名字,等价于 fail-closed。
|
|
221
|
+
allow = [...allowedTools];
|
|
222
|
+
strategy = 'allow-filter';
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const filter = strategy === 'allow-filter'
|
|
226
|
+
? { allow }
|
|
227
|
+
: (deny.length > 0 ? { deny } : null);
|
|
228
|
+
|
|
229
|
+
let disposeRestrict = null;
|
|
230
|
+
let disposeGuard = null;
|
|
231
|
+
let disposed = false;
|
|
232
|
+
try {
|
|
233
|
+
if (filter) disposeRestrict = tools.restrict(filter);
|
|
234
|
+
const agentId = agent?.id ?? agentCtx?.agent?.id ?? null;
|
|
235
|
+
disposeGuard = tools.guard((exec) => guardReason(exec, { allowedTools, agentId }));
|
|
236
|
+
} catch (error) {
|
|
237
|
+
// 半套策略绝不能留下:任何一步失败都回滚并让 create() 失败。
|
|
238
|
+
try { disposeGuard?.(); } catch { /* ignore */ }
|
|
239
|
+
try { disposeRestrict?.(); } catch { /* ignore */ }
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
deny,
|
|
245
|
+
allow,
|
|
246
|
+
strategy,
|
|
247
|
+
known: surface.names ? [...surface.names] : null,
|
|
248
|
+
dispose() {
|
|
249
|
+
if (disposed) return;
|
|
250
|
+
disposed = true;
|
|
251
|
+
disposeGuard?.();
|
|
252
|
+
disposeRestrict?.();
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|