cortico 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +47 -0
- package/package.json +43 -0
- package/src/boot.ts +46 -0
- package/src/bot.ts +1342 -0
- package/src/config-file.ts +41 -0
- package/src/core/README.md +92 -0
- package/src/core/billing.ts +26 -0
- package/src/core/blobs.ts +106 -0
- package/src/core/bus.ts +311 -0
- package/src/core/config-schema.ts +201 -0
- package/src/core/config.ts +262 -0
- package/src/core/core.ts +699 -0
- package/src/core/cost.ts +244 -0
- package/src/core/event-store.ts +304 -0
- package/src/core/fork.ts +113 -0
- package/src/core/generation.ts +118 -0
- package/src/core/instance-lock.ts +215 -0
- package/src/core/ipc-logger.ts +105 -0
- package/src/core/language.ts +62 -0
- package/src/core/log-context.ts +38 -0
- package/src/core/loop.ts +1858 -0
- package/src/core/markers.ts +48 -0
- package/src/core/prefix.ts +121 -0
- package/src/core/run.ts +95 -0
- package/src/core/secrets.ts +18 -0
- package/src/core/session.ts +56 -0
- package/src/core/sessions.ts +261 -0
- package/src/core/state.ts +104 -0
- package/src/core/template.ts +72 -0
- package/src/core/timers.ts +129 -0
- package/src/core/tool-log.ts +135 -0
- package/src/core/transcript.ts +92 -0
- package/src/core/truncate.ts +121 -0
- package/src/core/types.ts +1140 -0
- package/src/core/usage-log.ts +79 -0
- package/src/core/util.ts +386 -0
- package/src/deploy-listing.ts +160 -0
- package/src/deploy.ts +162 -0
- package/src/extensions/README.md +72 -0
- package/src/extensions/dry-mount.ts +390 -0
- package/src/extensions/manifest.ts +153 -0
- package/src/extensions/runtime.ts +42 -0
- package/src/extensions.ts +519 -0
- package/src/launcher.ts +285 -0
- package/src/paths.ts +155 -0
- package/src/protocol/open-responses/LICENSE +201 -0
- package/src/protocol/open-responses/README.md +14 -0
- package/src/protocol/open-responses/context-helpers.ts +27 -0
- package/src/protocol/open-responses/context-log.ts +59 -0
- package/src/protocol/open-responses/context.ts +89 -0
- package/src/protocol/open-responses/generated.ts +115 -0
- package/src/protocol/open-responses/index.ts +33 -0
- package/src/protocol/open-responses/openapi.json +4230 -0
- package/src/protocol/open-responses/stream.ts +146 -0
- package/src/protocol/open-responses/tokens.ts +31 -0
- package/src/providers/README.md +86 -0
- package/src/providers/base.ts +111 -0
- package/src/providers/configuration.ts +97 -0
- package/src/providers/console/config.ts +21 -0
- package/src/providers/console/settings.ts +443 -0
- package/src/providers/console/strings.ts +74 -0
- package/src/providers/console/types.ts +11 -0
- package/src/providers/llamacpp/archive.ts +148 -0
- package/src/providers/llamacpp/catalog.ts +247 -0
- package/src/providers/llamacpp/config.ts +36 -0
- package/src/providers/llamacpp/console/client.ts +6 -0
- package/src/providers/llamacpp/console/models-panel.ts +120 -0
- package/src/providers/llamacpp/console/runtime-panel.ts +173 -0
- package/src/providers/llamacpp/console/server.ts +153 -0
- package/src/providers/llamacpp/index.ts +95 -0
- package/src/providers/llamacpp/native.ts +51 -0
- package/src/providers/llamacpp/options.ts +117 -0
- package/src/providers/llamacpp/runtime-store.ts +139 -0
- package/src/providers/llamacpp/runtime.ts +214 -0
- package/src/providers/llamacpp/server.ts +286 -0
- package/src/providers/llamacpp/strings.ts +230 -0
- package/src/providers/openai-responses-compat/config.ts +20 -0
- package/src/providers/openai-responses-compat/index.ts +89 -0
- package/src/providers/openai-responses-compat/native.ts +127 -0
- package/src/providers/openai-responses-compat/strings.ts +19 -0
- package/src/providers/pricebook.ts +75 -0
- package/src/providers/registry.ts +137 -0
- package/src/providers/strings.ts +71 -0
- package/src/providers/transport/chat.ts +156 -0
- package/src/providers/transport/errors.ts +48 -0
- package/src/providers/transport/history.ts +68 -0
- package/src/providers/transport/native-input.ts +83 -0
- package/src/providers/transport/native-types.ts +13 -0
- package/src/providers/transport/response-assembly.ts +191 -0
- package/src/providers/transport/response-http.ts +196 -0
- package/src/providers/transport/response-meters.ts +27 -0
- package/src/providers/transport/responses-input.ts +56 -0
- package/src/web/README.md +84 -0
- package/src/web/client/console-pages/builtins/llm-settings/panel.ts +625 -0
- package/src/web/client/console-pages/builtins/llm-settings/pricing-panel.ts +168 -0
- package/src/web/client/console-pages/builtins/llm-settings/strings.ts +213 -0
- package/src/web/client/console-pages/builtins.ts +10 -0
- package/src/web/client/console-pages/context.ts +135 -0
- package/src/web/client/console-pages/host.ts +633 -0
- package/src/web/client/console-pages/loader.ts +132 -0
- package/src/web/client/console-pages/strings.ts +90 -0
- package/src/web/client/console-pages/tools/strings.ts +49 -0
- package/src/web/client/console-pages/tools/view.ts +215 -0
- package/src/web/client/core/api.ts +200 -0
- package/src/web/client/core/language.ts +56 -0
- package/src/web/client/core/lifecycle.ts +132 -0
- package/src/web/client/core/router.ts +199 -0
- package/src/web/client/core/stream.ts +157 -0
- package/src/web/client/core/websocket.ts +82 -0
- package/src/web/client/features/appearance/index.ts +333 -0
- package/src/web/client/features/appearance/strings.ts +87 -0
- package/src/web/client/features/config/index.ts +11 -0
- package/src/web/client/features/config/strings.ts +41 -0
- package/src/web/client/features/config/view.ts +428 -0
- package/src/web/client/features/core/events.ts +197 -0
- package/src/web/client/features/core/index.ts +254 -0
- package/src/web/client/features/core/run.ts +56 -0
- package/src/web/client/features/core/runlog.ts +214 -0
- package/src/web/client/features/core/sessions.ts +53 -0
- package/src/web/client/features/core/strings.ts +157 -0
- package/src/web/client/features/extensions/index.ts +422 -0
- package/src/web/client/features/extensions/strings.ts +164 -0
- package/src/web/client/features/feature.ts +93 -0
- package/src/web/client/features/live/context.ts +280 -0
- package/src/web/client/features/live/fork.ts +156 -0
- package/src/web/client/features/live/index.ts +392 -0
- package/src/web/client/features/live/onboarding.ts +113 -0
- package/src/web/client/features/live/protocol.ts +122 -0
- package/src/web/client/features/live/sessions.ts +71 -0
- package/src/web/client/features/live/status.ts +68 -0
- package/src/web/client/features/live/strings.ts +252 -0
- package/src/web/client/features/live/timeline.ts +590 -0
- package/src/web/client/features/prompts/editor.ts +292 -0
- package/src/web/client/features/prompts/index.ts +346 -0
- package/src/web/client/features/prompts/strings.ts +136 -0
- package/src/web/client/features/prompts/view.ts +256 -0
- package/src/web/client/features/providers/index.ts +100 -0
- package/src/web/client/features/providers/strings.ts +23 -0
- package/src/web/client/features/settings/general.ts +28 -0
- package/src/web/client/features/settings/index.ts +82 -0
- package/src/web/client/features/settings/strings.ts +33 -0
- package/src/web/client/features/storage/index.ts +1 -0
- package/src/web/client/features/storage/strings.ts +45 -0
- package/src/web/client/features/storage/view.ts +154 -0
- package/src/web/client/features/usage/chart.ts +486 -0
- package/src/web/client/features/usage/index.ts +407 -0
- package/src/web/client/features/usage/labels.ts +46 -0
- package/src/web/client/features/usage/range.ts +36 -0
- package/src/web/client/features/usage/state.ts +37 -0
- package/src/web/client/features/usage/strings.ts +191 -0
- package/src/web/client/features/usage/tooltip.ts +86 -0
- package/src/web/client/features/usage/types.ts +96 -0
- package/src/web/client/features/worlds/index.ts +424 -0
- package/src/web/client/features/worlds/strings.ts +121 -0
- package/src/web/client/main.ts +273 -0
- package/src/web/client/shell/avatar.ts +233 -0
- package/src/web/client/shell/index.ts +549 -0
- package/src/web/client/shell/strings.ts +89 -0
- package/src/web/client/theme/handoff.ts +43 -0
- package/src/web/client/theme/palette.ts +109 -0
- package/src/web/client/theme/registry.ts +130 -0
- package/src/web/client/theme/storage.ts +72 -0
- package/src/web/client/theme/strings.ts +123 -0
- package/src/web/client/theme/studio.ts +366 -0
- package/src/web/client/ui/actions.ts +109 -0
- package/src/web/client/ui/data.ts +185 -0
- package/src/web/client/ui/dom.ts +23 -0
- package/src/web/client/ui/fields.ts +191 -0
- package/src/web/client/ui/format.ts +75 -0
- package/src/web/client/ui/icons.ts +183 -0
- package/src/web/client/ui/images.ts +91 -0
- package/src/web/client/ui/index.ts +89 -0
- package/src/web/client/ui/lamp.ts +125 -0
- package/src/web/client/ui/log.ts +98 -0
- package/src/web/client/ui/overlay.ts +184 -0
- package/src/web/client/ui/page.ts +8 -0
- package/src/web/client/ui/prompt-input.tsx +311 -0
- package/src/web/client/ui/sheet.ts +131 -0
- package/src/web/client/ui/strings.ts +71 -0
- package/src/web/console-pages.ts +367 -0
- package/src/web/files.ts +107 -0
- package/src/web/path-picker.ts +296 -0
- package/src/web/public/index.html +17 -0
- package/src/web/public/styles.css +1715 -0
- package/src/web/server.ts +1883 -0
- package/src/web/shared/client-panel.ts +603 -0
- package/src/web/shared/console-protocol.ts +560 -0
- package/src/web/shared/css.d.ts +1 -0
- package/src/web/shared/path-picker.ts +23 -0
- package/src/web/shared/theme.ts +256 -0
- package/src/web/theme-store.ts +37 -0
- package/src/world.ts +449 -0
- package/src/worlds/bilibili/ENV_PROMPT.md +5 -0
- package/src/worlds/bilibili/README.md +176 -0
- package/src/worlds/bilibili/audience-admission.ts +995 -0
- package/src/worlds/bilibili/client.ts +528 -0
- package/src/worlds/bilibili/coalescing-buffer.ts +125 -0
- package/src/worlds/bilibili/config.ts +185 -0
- package/src/worlds/bilibili/console/client.ts +202 -0
- package/src/worlds/bilibili/definition.ts +35 -0
- package/src/worlds/bilibili/gift-frame.ts +131 -0
- package/src/worlds/bilibili/normalize.ts +452 -0
- package/src/worlds/bilibili/overlay/announcement.ts +76 -0
- package/src/worlds/bilibili/overlay/assets.ts +82 -0
- package/src/worlds/bilibili/overlay/model.ts +494 -0
- package/src/worlds/bilibili/overlay/project.ts +202 -0
- package/src/worlds/bilibili/overlay/server.ts +286 -0
- package/src/worlds/bilibili/overlay/types.ts +217 -0
- package/src/worlds/bilibili/overlay/web/app.js +690 -0
- package/src/worlds/bilibili/overlay/web/editor.css +297 -0
- package/src/worlds/bilibili/overlay/web/editor.html +102 -0
- package/src/worlds/bilibili/overlay/web/editor.js +2116 -0
- package/src/worlds/bilibili/overlay/web/overlay.html +13 -0
- package/src/worlds/bilibili/overlay/web/styles.css +258 -0
- package/src/worlds/bilibili/protobuf.ts +123 -0
- package/src/worlds/bilibili/wire.ts +94 -0
- package/src/worlds/bilibili/world.ts +1389 -0
- package/src/worlds/console-fixture/ENV_PROMPT.md +1 -0
- package/src/worlds/console-fixture/console/client.ts +72 -0
- package/src/worlds/console-fixture/world.ts +73 -0
- package/src/worlds/index.ts +15 -0
- package/src/worlds/minecraft/ENV_PROMPT.md +32 -0
- package/src/worlds/minecraft/ENV_PROMPT_CAMERA.md +2 -0
- package/src/worlds/minecraft/LICENSE-mineflayer-pathfinder.txt +21 -0
- package/src/worlds/minecraft/README.md +917 -0
- package/src/worlds/minecraft/blueprint-plan.ts +915 -0
- package/src/worlds/minecraft/blueprint-registry.ts +566 -0
- package/src/worlds/minecraft/blueprint-repair.ts +236 -0
- package/src/worlds/minecraft/blueprint-resource.ts +272 -0
- package/src/worlds/minecraft/blueprint.ts +492 -0
- package/src/worlds/minecraft/body-lease.ts +340 -0
- package/src/worlds/minecraft/bridge.ts +749 -0
- package/src/worlds/minecraft/check.ts +651 -0
- package/src/worlds/minecraft/chests.ts +292 -0
- package/src/worlds/minecraft/client-launch.ts +272 -0
- package/src/worlds/minecraft/client-options.ts +85 -0
- package/src/worlds/minecraft/client-skins.ts +164 -0
- package/src/worlds/minecraft/client-window.ps1 +91 -0
- package/src/worlds/minecraft/client.ts +423 -0
- package/src/worlds/minecraft/combat-context.ts +109 -0
- package/src/worlds/minecraft/combat.ts +1552 -0
- package/src/worlds/minecraft/config.ts +499 -0
- package/src/worlds/minecraft/console/access.ts +218 -0
- package/src/worlds/minecraft/console/client.ts +244 -0
- package/src/worlds/minecraft/console/log.ts +110 -0
- package/src/worlds/minecraft/console/mount.ts +420 -0
- package/src/worlds/minecraft/console/skin.ts +257 -0
- package/src/worlds/minecraft/console/style.css +154 -0
- package/src/worlds/minecraft/console/world.ts +449 -0
- package/src/worlds/minecraft/deaths.ts +135 -0
- package/src/worlds/minecraft/definition.ts +18 -0
- package/src/worlds/minecraft/engine-child.ts +274 -0
- package/src/worlds/minecraft/engine-ipc.ts +98 -0
- package/src/worlds/minecraft/entity-facts.ts +132 -0
- package/src/worlds/minecraft/escape.ts +407 -0
- package/src/worlds/minecraft/executor.ts +14494 -0
- package/src/worlds/minecraft/explored.ts +156 -0
- package/src/worlds/minecraft/geometry.ts +205 -0
- package/src/worlds/minecraft/goal-plan.ts +647 -0
- package/src/worlds/minecraft/item-break.ts +126 -0
- package/src/worlds/minecraft/item-facts.ts +146 -0
- package/src/worlds/minecraft/item-pick.ts +67 -0
- package/src/worlds/minecraft/level-dat.ts +176 -0
- package/src/worlds/minecraft/log.ts +132 -0
- package/src/worlds/minecraft/mineflayer-fixes.ts +1180 -0
- package/src/worlds/minecraft/names.ts +377 -0
- package/src/worlds/minecraft/pathfinder-lib.d.ts +13 -0
- package/src/worlds/minecraft/pathfinder-perf.ts +796 -0
- package/src/worlds/minecraft/piglin.ts +49 -0
- package/src/worlds/minecraft/policy.ts +547 -0
- package/src/worlds/minecraft/precheck.ts +655 -0
- package/src/worlds/minecraft/proxy.ts +445 -0
- package/src/worlds/minecraft/ranged.ts +635 -0
- package/src/worlds/minecraft/readouts.ts +102 -0
- package/src/worlds/minecraft/round.ts +26 -0
- package/src/worlds/minecraft/search-observation.ts +92 -0
- package/src/worlds/minecraft/server-config.ts +467 -0
- package/src/worlds/minecraft/server.ts +658 -0
- package/src/worlds/minecraft/show.ts +84 -0
- package/src/worlds/minecraft/skills.ts +1763 -0
- package/src/worlds/minecraft/terrain.ts +1735 -0
- package/src/worlds/minecraft/window.ts +96 -0
- package/src/worlds/minecraft/works.ts +179 -0
- package/src/worlds/minecraft/world.ts +6438 -0
- package/src/worlds/qq/ENV_PROMPT.md +5 -0
- package/src/worlds/qq/config.ts +108 -0
- package/src/worlds/qq/console/client.ts +126 -0
- package/src/worlds/qq/console/events.ts +155 -0
- package/src/worlds/qq/console/gate.ts +232 -0
- package/src/worlds/qq/console/roster.ts +203 -0
- package/src/worlds/qq/console/style.css +65 -0
- package/src/worlds/qq/conversation.ts +28 -0
- package/src/worlds/qq/definition.ts +62 -0
- package/src/worlds/qq/driver.ts +375 -0
- package/src/worlds/qq/history-tools.ts +209 -0
- package/src/worlds/qq/normalize.ts +323 -0
- package/src/worlds/qq/qface-map.ts +282 -0
- package/src/worlds/qq/vision-prompt.ts +27 -0
- package/src/worlds/qq/vision.ts +642 -0
- package/src/worlds/qq/vlm.ts +142 -0
- package/src/worlds/qq/world.ts +1503 -0
- package/src/worlds/terminal/ENV_PROMPT.md +11 -0
- package/src/worlds/terminal/config.ts +9 -0
- package/src/worlds/terminal/definition.ts +14 -0
- package/src/worlds/terminal/world.ts +700 -0
- package/src/worlds/websearch/ENV_PROMPT.md +1 -0
- package/src/worlds/websearch/brave-client.ts +261 -0
- package/src/worlds/websearch/config.ts +75 -0
- package/src/worlds/websearch/definition.ts +20 -0
- package/src/worlds/websearch/world.ts +171 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
export type BodyOwnerKind =
|
|
2
|
+
| 'task'
|
|
3
|
+
| 'combat'
|
|
4
|
+
| 'lava'
|
|
5
|
+
| 'drown'
|
|
6
|
+
| 'suffocation'
|
|
7
|
+
| 'hurt';
|
|
8
|
+
|
|
9
|
+
/** 身体 owner 是一次动作实例;同类的新实例不能继承旧实例的授权。 */
|
|
10
|
+
export type BodyOwner = object;
|
|
11
|
+
|
|
12
|
+
export interface BodyUtilityFactors {
|
|
13
|
+
survival: number;
|
|
14
|
+
urgency: number;
|
|
15
|
+
feasibility: number;
|
|
16
|
+
progress: number;
|
|
17
|
+
continuity: number;
|
|
18
|
+
executionRisk: number;
|
|
19
|
+
disruption: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface BodyProposalInput {
|
|
23
|
+
owner: BodyOwner;
|
|
24
|
+
ownerKind: BodyOwnerKind;
|
|
25
|
+
intent: string;
|
|
26
|
+
validUntil: number;
|
|
27
|
+
utility: BodyUtilityFactors;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface BodyProposal extends BodyProposalInput {
|
|
31
|
+
proposalId: number;
|
|
32
|
+
ownerId: number;
|
|
33
|
+
updatedAt: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface BodyLeaseToken {
|
|
37
|
+
readonly leaseId: number;
|
|
38
|
+
readonly connectionGeneration: number;
|
|
39
|
+
readonly owner: BodyOwner;
|
|
40
|
+
readonly ownerId: number;
|
|
41
|
+
readonly ownerKind: BodyOwnerKind;
|
|
42
|
+
readonly acquiredAt: number;
|
|
43
|
+
readonly expiresAt: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface BodyCandidateScore {
|
|
47
|
+
proposalId: number;
|
|
48
|
+
ownerId: number;
|
|
49
|
+
ownerKind: BodyOwnerKind;
|
|
50
|
+
score: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface BodyDecision {
|
|
54
|
+
at: number;
|
|
55
|
+
connectionGeneration: number;
|
|
56
|
+
candidates: BodyCandidateScore[];
|
|
57
|
+
recommended: BodyCandidateScore | null;
|
|
58
|
+
incumbentBefore: BodyCandidateScore | null;
|
|
59
|
+
activeAfter: BodyCandidateScore | null;
|
|
60
|
+
reason: 'empty' | 'acquire' | 'renew' | 'same-owner' | 'preempt' | 'hysteresis';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type BodyLeaseEvent =
|
|
64
|
+
| { event: 'proposal-updated'; at: number; generation: number; proposal: BodyProposal }
|
|
65
|
+
| { event: 'proposal-rejected'; at: number; generation: number; proposal: BodyProposal; reason: 'invalidated' | 'wrong-generation' }
|
|
66
|
+
| { event: 'proposal-withdrawn'; at: number; generation: number; proposalId: number; ownerId: number; reason: string }
|
|
67
|
+
| { event: 'decision'; at: number; generation: number; decision: BodyDecision }
|
|
68
|
+
| { event: 'lease-acquired' | 'lease-renewed'; at: number; generation: number; token: BodyLeaseToken }
|
|
69
|
+
| { event: 'lease-preempted' | 'lease-released' | 'lease-expired'; at: number; generation: number; token: BodyLeaseToken; reason: string }
|
|
70
|
+
| {
|
|
71
|
+
event: 'command-accepted' | 'command-rejected';
|
|
72
|
+
at: number;
|
|
73
|
+
generation: number;
|
|
74
|
+
command: string;
|
|
75
|
+
leaseId: number | null;
|
|
76
|
+
ownerId: number | null;
|
|
77
|
+
reason: 'accepted' | 'no-active-lease' | 'wrong-owner';
|
|
78
|
+
}
|
|
79
|
+
| {
|
|
80
|
+
event: 'invalidated';
|
|
81
|
+
at: number;
|
|
82
|
+
generation: number;
|
|
83
|
+
reason: 'death' | 'disconnect' | 'stop' | 'generation';
|
|
84
|
+
leaseId: number | null;
|
|
85
|
+
proposalIds: number[];
|
|
86
|
+
}
|
|
87
|
+
| { event: 'revived'; at: number; generation: number; reason: 'respawn' };
|
|
88
|
+
|
|
89
|
+
interface BodyLeaseOptions {
|
|
90
|
+
connectionGeneration: number;
|
|
91
|
+
emit?: (event: BodyLeaseEvent) => void;
|
|
92
|
+
leaseMs?: number;
|
|
93
|
+
preemptMargin?: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const BODY_LEASE_MS = 2_500;
|
|
97
|
+
const BODY_PREEMPT_MARGIN = 4;
|
|
98
|
+
|
|
99
|
+
const UTILITY_WEIGHTS: Readonly<Record<keyof BodyUtilityFactors, number>> = {
|
|
100
|
+
survival: 0.34,
|
|
101
|
+
urgency: 0.22,
|
|
102
|
+
feasibility: 0.20,
|
|
103
|
+
progress: 0.08,
|
|
104
|
+
continuity: 0.08,
|
|
105
|
+
executionRisk: -0.14,
|
|
106
|
+
disruption: -0.08,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export function bodyUtilityScore(utility: BodyUtilityFactors): number {
|
|
110
|
+
return (Object.keys(UTILITY_WEIGHTS) as Array<keyof BodyUtilityFactors>)
|
|
111
|
+
.reduce((sum, key) => sum + utility[key] * UTILITY_WEIGHTS[key], 0);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface ActiveLease {
|
|
115
|
+
token: BodyLeaseToken;
|
|
116
|
+
proposalId: number;
|
|
117
|
+
score: number;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 比较控制器提交的效用并记录建议的身体控制者;当前仅记录,不拦截执行。 */
|
|
121
|
+
export class BodyLeaseArbiter {
|
|
122
|
+
private generation: number;
|
|
123
|
+
private readonly emit: (event: BodyLeaseEvent) => void;
|
|
124
|
+
private readonly leaseMs: number;
|
|
125
|
+
private readonly preemptMargin: number;
|
|
126
|
+
private proposalSeq = 0;
|
|
127
|
+
private leaseSeq = 0;
|
|
128
|
+
private ownerSeq = 0;
|
|
129
|
+
private readonly ownerIds = new WeakMap<BodyOwner, number>();
|
|
130
|
+
private readonly proposals = new Map<BodyOwner, BodyProposal>();
|
|
131
|
+
private active: ActiveLease | null = null;
|
|
132
|
+
private invalidated = false;
|
|
133
|
+
|
|
134
|
+
constructor(options: BodyLeaseOptions) {
|
|
135
|
+
this.generation = options.connectionGeneration;
|
|
136
|
+
this.emit = options.emit ?? (() => undefined);
|
|
137
|
+
this.leaseMs = options.leaseMs ?? BODY_LEASE_MS;
|
|
138
|
+
this.preemptMargin = options.preemptMargin ?? BODY_PREEMPT_MARGIN;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get connectionGeneration(): number { return this.generation; }
|
|
142
|
+
|
|
143
|
+
update(input: BodyProposalInput, now: number, connectionGeneration = this.generation): BodyProposal {
|
|
144
|
+
const proposal: BodyProposal = {
|
|
145
|
+
...input,
|
|
146
|
+
proposalId: ++this.proposalSeq,
|
|
147
|
+
ownerId: this.ownerId(input.owner),
|
|
148
|
+
updatedAt: now,
|
|
149
|
+
};
|
|
150
|
+
if (this.invalidated || connectionGeneration !== this.generation) {
|
|
151
|
+
this.emit({
|
|
152
|
+
event: 'proposal-rejected', at: now, generation: this.generation, proposal,
|
|
153
|
+
reason: this.invalidated ? 'invalidated' : 'wrong-generation',
|
|
154
|
+
});
|
|
155
|
+
return proposal;
|
|
156
|
+
}
|
|
157
|
+
this.proposals.set(input.owner, proposal);
|
|
158
|
+
this.emit({ event: 'proposal-updated', at: now, generation: this.generation, proposal });
|
|
159
|
+
return proposal;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
withdraw(owner: BodyOwner, reason: string, now: number): void {
|
|
163
|
+
const proposal = this.proposals.get(owner);
|
|
164
|
+
if (!proposal) return;
|
|
165
|
+
this.proposals.delete(owner);
|
|
166
|
+
this.emit({
|
|
167
|
+
event: 'proposal-withdrawn', at: now, generation: this.generation,
|
|
168
|
+
proposalId: proposal.proposalId, ownerId: proposal.ownerId, reason,
|
|
169
|
+
});
|
|
170
|
+
if (this.active?.token.owner === owner) this.releaseActive(`withdraw:${reason}`, now);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
reconcile(now: number): BodyDecision {
|
|
174
|
+
this.expire(now);
|
|
175
|
+
const ranked = [...this.proposals.values()]
|
|
176
|
+
.filter((proposal) => proposal.validUntil > now)
|
|
177
|
+
.map((proposal) => ({ proposal, score: bodyUtilityScore(proposal.utility) }))
|
|
178
|
+
.sort((a, b) => b.score - a.score || a.proposal.ownerId - b.proposal.ownerId);
|
|
179
|
+
const scores = ranked.map(({ proposal, score }) => this.scoreOf(proposal, score));
|
|
180
|
+
const recommended = ranked[0] ?? null;
|
|
181
|
+
const incumbentBefore = this.active ? this.activeScore() : null;
|
|
182
|
+
let reason: BodyDecision['reason'] = 'empty';
|
|
183
|
+
|
|
184
|
+
if (!recommended) {
|
|
185
|
+
if (this.active) this.releaseActive('no-valid-proposal', now);
|
|
186
|
+
} else {
|
|
187
|
+
const incumbentProposal = this.active ? this.proposals.get(this.active.token.owner) : null;
|
|
188
|
+
if (!this.active || !incumbentProposal || incumbentProposal.validUntil <= now) {
|
|
189
|
+
if (this.active) this.releaseActive('proposal-withdrawn-or-expired', now);
|
|
190
|
+
this.acquire(recommended.proposal, recommended.score, now);
|
|
191
|
+
reason = 'acquire';
|
|
192
|
+
} else if (recommended.proposal.owner === this.active.token.owner) {
|
|
193
|
+
const changed = this.renewActive(recommended.proposal, recommended.score, now);
|
|
194
|
+
reason = changed ? 'renew' : 'same-owner';
|
|
195
|
+
} else if (recommended.score > bodyUtilityScore(incumbentProposal.utility) + this.preemptMargin) {
|
|
196
|
+
const incumbentScore = bodyUtilityScore(incumbentProposal.utility);
|
|
197
|
+
const old = this.active.token;
|
|
198
|
+
this.active = null;
|
|
199
|
+
this.emit({
|
|
200
|
+
event: 'lease-preempted', at: now, generation: this.generation,
|
|
201
|
+
token: old, reason: `challenger-margin:${recommended.score - incumbentScore}`,
|
|
202
|
+
});
|
|
203
|
+
this.acquire(recommended.proposal, recommended.score, now);
|
|
204
|
+
reason = 'preempt';
|
|
205
|
+
} else {
|
|
206
|
+
this.renewActive(incumbentProposal, bodyUtilityScore(incumbentProposal.utility), now);
|
|
207
|
+
reason = 'hysteresis';
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const decision: BodyDecision = {
|
|
212
|
+
at: now,
|
|
213
|
+
connectionGeneration: this.generation,
|
|
214
|
+
candidates: scores,
|
|
215
|
+
recommended: recommended ? this.scoreOf(recommended.proposal, recommended.score) : null,
|
|
216
|
+
incumbentBefore,
|
|
217
|
+
activeAfter: this.active ? this.activeScore() : null,
|
|
218
|
+
reason,
|
|
219
|
+
};
|
|
220
|
+
this.emit({ event: 'decision', at: now, generation: this.generation, decision });
|
|
221
|
+
return decision;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** 记录旧路径是否会获准,但不改变旧路径的执行。 */
|
|
225
|
+
observeLegacyCommit<T>(owner: BodyOwner, command: string, fn: () => T, now: number): T {
|
|
226
|
+
this.expire(now);
|
|
227
|
+
const wouldAccept = this.active?.token.owner === owner && now < this.active.token.expiresAt;
|
|
228
|
+
this.emit({
|
|
229
|
+
event: wouldAccept ? 'command-accepted' : 'command-rejected',
|
|
230
|
+
at: now,
|
|
231
|
+
generation: this.generation,
|
|
232
|
+
command,
|
|
233
|
+
leaseId: this.active?.token.leaseId ?? null,
|
|
234
|
+
ownerId: this.ownerIds.get(owner) ?? null,
|
|
235
|
+
reason: wouldAccept ? 'accepted' : this.active ? 'wrong-owner' : 'no-active-lease',
|
|
236
|
+
});
|
|
237
|
+
return fn();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
invalidate(
|
|
241
|
+
reason: 'death' | 'disconnect' | 'stop' | 'generation',
|
|
242
|
+
now: number,
|
|
243
|
+
): void {
|
|
244
|
+
const leaseId = this.active?.token.leaseId ?? null;
|
|
245
|
+
const proposalIds = [...this.proposals.values()].map((proposal) => proposal.proposalId);
|
|
246
|
+
this.emit({ event: 'invalidated', at: now, generation: this.generation, reason, leaseId, proposalIds });
|
|
247
|
+
this.active = null;
|
|
248
|
+
this.proposals.clear();
|
|
249
|
+
this.invalidated = true;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** 重生解除作废状态并创建新的身体租约,保留当前连接代次。未作废时不产生事件。 */
|
|
253
|
+
revive(now: number): void {
|
|
254
|
+
if (!this.invalidated) return;
|
|
255
|
+
this.invalidated = false;
|
|
256
|
+
this.emit({ event: 'revived', at: now, generation: this.generation, reason: 'respawn' });
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
bindGeneration(connectionGeneration: number, now: number): void {
|
|
260
|
+
if (connectionGeneration === this.generation) return;
|
|
261
|
+
this.invalidate('generation', now);
|
|
262
|
+
this.generation = connectionGeneration;
|
|
263
|
+
this.invalidated = false;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private ownerId(owner: BodyOwner): number {
|
|
267
|
+
const found = this.ownerIds.get(owner);
|
|
268
|
+
if (found !== undefined) return found;
|
|
269
|
+
const id = ++this.ownerSeq;
|
|
270
|
+
this.ownerIds.set(owner, id);
|
|
271
|
+
return id;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private scoreOf(proposal: BodyProposal, score: number): BodyCandidateScore {
|
|
275
|
+
return {
|
|
276
|
+
proposalId: proposal.proposalId,
|
|
277
|
+
ownerId: proposal.ownerId,
|
|
278
|
+
ownerKind: proposal.ownerKind,
|
|
279
|
+
score,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private activeScore(): BodyCandidateScore {
|
|
284
|
+
const active = this.active!;
|
|
285
|
+
const proposal = this.proposals.get(active.token.owner);
|
|
286
|
+
return proposal
|
|
287
|
+
? this.scoreOf(proposal, active.score)
|
|
288
|
+
: {
|
|
289
|
+
proposalId: active.proposalId,
|
|
290
|
+
ownerId: active.token.ownerId,
|
|
291
|
+
ownerKind: active.token.ownerKind,
|
|
292
|
+
score: active.score,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private acquire(proposal: BodyProposal, score: number, now: number): void {
|
|
297
|
+
const token: BodyLeaseToken = Object.freeze({
|
|
298
|
+
leaseId: ++this.leaseSeq,
|
|
299
|
+
connectionGeneration: this.generation,
|
|
300
|
+
owner: proposal.owner,
|
|
301
|
+
ownerId: proposal.ownerId,
|
|
302
|
+
ownerKind: proposal.ownerKind,
|
|
303
|
+
acquiredAt: now,
|
|
304
|
+
expiresAt: Math.min(now + this.leaseMs, proposal.validUntil),
|
|
305
|
+
});
|
|
306
|
+
this.active = { token, proposalId: proposal.proposalId, score };
|
|
307
|
+
this.emit({ event: 'lease-acquired', at: now, generation: this.generation, token });
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
private renewActive(proposal: BodyProposal, score: number, now: number): boolean {
|
|
311
|
+
const previous = this.active!;
|
|
312
|
+
const expiresAt = Math.min(now + this.leaseMs, proposal.validUntil);
|
|
313
|
+
if (expiresAt <= previous.token.expiresAt && previous.proposalId === proposal.proposalId) return false;
|
|
314
|
+
const token: BodyLeaseToken = Object.freeze({
|
|
315
|
+
...previous.token,
|
|
316
|
+
acquiredAt: previous.token.acquiredAt,
|
|
317
|
+
expiresAt,
|
|
318
|
+
});
|
|
319
|
+
this.active = { token, proposalId: proposal.proposalId, score };
|
|
320
|
+
this.emit({ event: 'lease-renewed', at: now, generation: this.generation, token });
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private releaseActive(reason: string, now: number): void {
|
|
325
|
+
if (!this.active) return;
|
|
326
|
+
const token = this.active.token;
|
|
327
|
+
this.active = null;
|
|
328
|
+
this.emit({ event: 'lease-released', at: now, generation: this.generation, token, reason });
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private expire(now: number): void {
|
|
332
|
+
for (const [owner, proposal] of this.proposals) {
|
|
333
|
+
if (proposal.validUntil <= now) this.proposals.delete(owner);
|
|
334
|
+
}
|
|
335
|
+
if (!this.active || now < this.active.token.expiresAt) return;
|
|
336
|
+
const token = this.active.token;
|
|
337
|
+
this.active = null;
|
|
338
|
+
this.emit({ event: 'lease-expired', at: now, generation: this.generation, token, reason: 'lease-expired' });
|
|
339
|
+
}
|
|
340
|
+
}
|