@zergai/ztc 0.26.6-dev.202611240947 → 0.27.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +629 -167
- package/RUST_THIRD_PARTY_LICENSES.txt +5 -5
- package/bin/zmux-darwin-arm64 +0 -0
- package/bin/zmux-linux-x64 +0 -0
- package/bin/ztc-audio-darwin-arm64 +0 -0
- package/bin/ztc-capture-darwin-arm64 +0 -0
- package/bin/ztc-computer-darwin-arm64 +0 -0
- package/bin/ztc-llm-darwin-arm64 +0 -0
- package/bin/ztc-tts-darwin-arm64 +0 -0
- package/bin/ztc-tui-darwin-arm64 +0 -0
- package/bin/ztc-tui-linux-x64 +0 -0
- package/bin/zterm-darwin-arm64 +2 -2
- package/dist/App.js +6 -1
- package/dist/agent/agent.d.ts +21 -15
- package/dist/agent/agent.js +389 -163
- package/dist/agent/backends/codex_app_server.d.ts +7 -1
- package/dist/agent/backends/codex_app_server.js +11 -1
- package/dist/agent/backends/gemini.js +22 -8
- package/dist/agent/commands/batch.js +8 -0
- package/dist/agent/commands/config.js +10 -0
- package/dist/agent/commands/environment.js +5 -5
- package/dist/agent/commands/goal.d.ts +1 -0
- package/dist/agent/commands/goal.js +2 -1
- package/dist/agent/commands/index.js +2 -1
- package/dist/agent/commands/manifest.js +4 -0
- package/dist/agent/commands/optimize.js +5 -2
- package/dist/agent/commands/pane_cli.d.ts +29 -0
- package/dist/agent/commands/pane_cli.js +303 -0
- package/dist/agent/commands/settings.js +13 -3
- package/dist/agent/commands/shannon.js +8 -0
- package/dist/agent/commands/update.js +15 -3
- package/dist/agent/commands/zerg.js +31 -1
- package/dist/agent/commands/ztc_pane.js +3 -1
- package/dist/agent/factory.d.ts +4 -0
- package/dist/agent/factory.js +11 -1
- package/dist/agent/loop_policy.d.ts +42 -5
- package/dist/agent/loop_policy.js +206 -3
- package/dist/agent/runtime/command_jobs.d.ts +5 -0
- package/dist/agent/runtime/command_jobs.js +241 -31
- package/dist/agent/runtime/command_wait.d.ts +2 -0
- package/dist/agent/runtime/command_wait.js +1 -0
- package/dist/agent/runtime/done_report.d.ts +18 -0
- package/dist/agent/runtime/done_report.js +130 -0
- package/dist/agent/runtime/execution_context.js +4 -1
- package/dist/agent/runtime/failure_cause.d.ts +2 -0
- package/dist/agent/runtime/failure_cause.js +35 -0
- package/dist/agent/runtime/policy.d.ts +14 -2
- package/dist/agent/runtime/policy.js +104 -12
- package/dist/agent/runtime/quiet_job.d.ts +18 -0
- package/dist/agent/runtime/quiet_job.js +48 -0
- package/dist/agent/runtime/recovery.d.ts +1 -5
- package/dist/agent/runtime/recovery.js +2 -44
- package/dist/agent/runtime/tool_runtime.d.ts +1 -0
- package/dist/agent/runtime/tool_runtime.js +2 -0
- package/dist/agent/subagent.js +6 -1
- package/dist/agent/tools/deferred.js +1 -0
- package/dist/agent/tools/index.d.ts +2 -0
- package/dist/agent/tools/index.js +1 -0
- package/dist/agent/tools/outcome.d.ts +15 -0
- package/dist/agent/tools/outcome.js +107 -0
- package/dist/agent/tools/pane.d.ts +2 -1
- package/dist/agent/tools/pane.js +21 -3
- package/dist/agent/tools/registry.js +2 -0
- package/dist/agent/tools/shell.d.ts +1 -0
- package/dist/agent/tools/shell.js +128 -17
- package/dist/agent/tools/sim_domain_tools.js +4 -2
- package/dist/agent/tools/supervision.js +4 -2
- package/dist/agent/tools/types.d.ts +3 -0
- package/dist/app/command_context.js +5 -9
- package/dist/app/conversation_compaction.d.ts +4 -2
- package/dist/app/conversation_compaction.js +16 -5
- package/dist/app/headless_shell_mode.d.ts +14 -0
- package/dist/app/headless_shell_mode.js +7 -0
- package/dist/app/hosted_resident.d.ts +356 -0
- package/dist/app/hosted_resident.js +677 -0
- package/dist/app/restart.js +11 -2
- package/dist/app/session_manager.d.ts +5 -0
- package/dist/app/session_manager.js +21 -6
- package/dist/app/session_messages.js +6 -6
- package/dist/app/workspace_layout_controller.js +7 -0
- package/dist/batch/worktree.d.ts +10 -0
- package/dist/batch/worktree.js +15 -0
- package/dist/bench/registry.js +4 -2
- package/dist/cli_args.js +1 -1
- package/dist/cli_main.js +80 -10
- package/dist/cli_ratatui.d.ts +75 -0
- package/dist/cli_ratatui.js +485 -84
- package/dist/components/InputArea.js +4 -4
- package/dist/config/execution.d.ts +10 -0
- package/dist/config/execution.js +10 -0
- package/dist/config/hosted_runtime.d.ts +4 -1
- package/dist/config/hosted_runtime.js +4 -1
- package/dist/config/permission_pin.d.ts +34 -2
- package/dist/config/permission_pin.js +52 -2
- package/dist/config/person_command_policy.d.ts +38 -0
- package/dist/config/person_command_policy.js +11 -0
- package/dist/config/profiles.d.ts +21 -0
- package/dist/config/profiles.js +56 -2
- package/dist/config/types.d.ts +4 -1
- package/dist/config.js +34 -19
- package/dist/desktop/daily/codex_exec_adapter.js +7 -2
- package/dist/desktop/daily/desktop_wiring.d.ts +7 -1
- package/dist/desktop/daily/desktop_wiring.js +11 -3
- package/dist/desktop/daily/recipes.js +20 -4
- package/dist/desktop/health.d.ts +24 -0
- package/dist/desktop/health.js +151 -0
- package/dist/desktop/native_dialogs.d.ts +37 -0
- package/dist/desktop/native_dialogs.js +105 -0
- package/dist/diagnostics/session_export.d.ts +1 -0
- package/dist/diagnostics/session_export.js +28 -0
- package/dist/embedded/registry.js +4 -2
- package/dist/emulation/tool_adapters.js +9 -4
- package/dist/emulation/ztc_addendum.js +3 -0
- package/dist/headless.d.ts +11 -4
- package/dist/headless.js +99 -34
- package/dist/headless_approval.d.ts +6 -0
- package/dist/headless_cli.js +1 -1
- package/dist/hooks/runner.js +7 -6
- package/dist/library.d.ts +4 -0
- package/dist/library.js +3 -0
- package/dist/mcp/client.js +3 -3
- package/dist/modes/registry.js +13 -4
- package/dist/onboarding/setup_environment.js +13 -1
- package/dist/optimize/adapters/kernelbench.js +88 -10
- package/dist/optimize/adapters/types.d.ts +5 -0
- package/dist/optimize/executor.js +21 -0
- package/dist/panes/cli_install.d.ts +54 -0
- package/dist/panes/cli_install.js +157 -0
- package/dist/panes/model_catalog.js +3 -0
- package/dist/panes/new_pane_choices.d.ts +6 -2
- package/dist/panes/new_pane_choices.js +24 -12
- package/dist/panes/pane_cli_launch.d.ts +50 -0
- package/dist/panes/pane_cli_launch.js +124 -0
- package/dist/panes/pane_clis.d.ts +171 -0
- package/dist/panes/pane_clis.js +325 -0
- package/dist/panes/person_only_panes.d.ts +45 -0
- package/dist/panes/person_only_panes.js +117 -0
- package/dist/plugin/simulation_living.js +0 -2
- package/dist/plugin/simulation_living_engine.d.ts +1 -0
- package/dist/plugin/simulation_living_engine.js +1 -0
- package/dist/plugin/simulation_living_labels.d.ts +3 -0
- package/dist/plugin/simulation_living_labels.js +2 -0
- package/dist/router_auth.js +12 -1
- package/dist/session.js +26 -1
- package/dist/simulation/art/gemini_image.js +11 -5
- package/dist/simulation/domains/loader.js +25 -3
- package/dist/simulation/living/cold.d.ts +23 -0
- package/dist/simulation/living/cold.js +41 -0
- package/dist/simulation/living/context.d.ts +591 -1283
- package/dist/simulation/living/context.js +378 -244
- package/dist/simulation/living/contradictions.d.ts +14 -0
- package/dist/simulation/living/contradictions.js +282 -0
- package/dist/simulation/living/core/ids.d.ts +53 -0
- package/dist/simulation/living/core/ids.js +39 -0
- package/dist/simulation/living/core/kind.d.ts +20 -0
- package/dist/simulation/living/core/kind.js +15 -0
- package/dist/simulation/living/core/laws.d.ts +179 -0
- package/dist/simulation/living/core/laws.js +184 -0
- package/dist/simulation/living/core/ops.d.ts +257 -0
- package/dist/simulation/living/core/ops.js +34 -0
- package/dist/simulation/living/definer.d.ts +17 -1
- package/dist/simulation/living/definer.js +130 -51
- package/dist/simulation/living/definition.d.ts +174 -65
- package/dist/simulation/living/definition.js +287 -180
- package/dist/simulation/living/director.d.ts +80 -0
- package/dist/simulation/living/director.js +363 -0
- package/dist/simulation/living/frame.d.ts +108 -21
- package/dist/simulation/living/frame.js +111 -66
- package/dist/simulation/living/identifiers.d.ts +0 -1
- package/dist/simulation/living/identifiers.js +0 -3
- package/dist/simulation/living/index.d.ts +18 -9
- package/dist/simulation/living/index.js +18 -9
- package/dist/simulation/living/inference.d.ts +92 -50
- package/dist/simulation/living/inference.js +319 -175
- package/dist/simulation/living/knowledge.d.ts +28 -0
- package/dist/simulation/living/knowledge.js +73 -0
- package/dist/simulation/living/library.json +1177 -0
- package/dist/simulation/living/mechanics.d.ts +31 -34
- package/dist/simulation/living/mechanics.js +222 -138
- package/dist/simulation/living/model-input.d.ts +6 -3
- package/dist/simulation/living/model-input.js +18 -26
- package/dist/simulation/living/open-world.d.ts +74 -94
- package/dist/simulation/living/open-world.js +151 -30
- package/dist/simulation/living/phrases.d.ts +3 -0
- package/dist/simulation/living/phrases.js +37 -0
- package/dist/simulation/living/phrases.json +560 -0
- package/dist/simulation/living/projection.d.ts +316 -2
- package/dist/simulation/living/projection.js +50 -2
- package/dist/simulation/living/prompts/example-spec.json +11 -0
- package/dist/simulation/living/prompts/role-text.json +14 -0
- package/dist/simulation/living/prompts.d.ts +5 -0
- package/dist/simulation/living/prompts.js +275 -0
- package/dist/simulation/living/recall.d.ts +2 -2
- package/dist/simulation/living/reduce.d.ts +51 -14
- package/dist/simulation/living/reduce.js +941 -579
- package/dist/simulation/living/replay.d.ts +5 -4
- package/dist/simulation/living/replay.js +10 -9
- package/dist/simulation/living/runtime.d.ts +135 -25
- package/dist/simulation/living/runtime.js +1641 -583
- package/dist/simulation/living/scale.d.ts +96 -0
- package/dist/simulation/living/scale.js +124 -0
- package/dist/simulation/living/script.d.ts +5 -0
- package/dist/simulation/living/script.js +115 -0
- package/dist/simulation/living/shared-kinds.d.ts +4 -11
- package/dist/simulation/living/shared-kinds.js +11 -81
- package/dist/simulation/living/space.d.ts +14 -0
- package/dist/simulation/living/space.js +132 -0
- package/dist/simulation/living/state.d.ts +100 -0
- package/dist/simulation/living/state.js +393 -0
- package/dist/simulation/living/types.d.ts +829 -512
- package/dist/simulation/living/types.js +162 -187
- package/dist/simulation/living/world-rules.d.ts +100 -0
- package/dist/simulation/living/world-rules.js +267 -0
- package/dist/simulation/plugins/loader.js +11 -0
- package/dist/skills/loader.js +4 -0
- package/dist/tools.d.ts +1 -0
- package/dist/tools.js +1 -0
- package/dist/ui/ratatui/headless_pane_manager.d.ts +4 -1
- package/dist/ui/ratatui/headless_pane_manager.js +13 -9
- package/dist/ui/ratatui/pane_relay.js +29 -0
- package/dist/ui/ratatui/pane_type.d.ts +10 -0
- package/dist/ui/ratatui/pane_type.js +11 -0
- package/dist/ui/ratatui/process.d.ts +30 -0
- package/dist/ui/ratatui/process.js +133 -19
- package/dist/ui/ratatui/protocol.d.ts +124 -2
- package/dist/ui/ratatui/protocol.js +35 -0
- package/dist/ui/ratatui/relay_policy.js +3 -0
- package/dist/ui/ratatui/ztc_launch_trust.d.ts +2 -1
- package/dist/ui/ratatui/ztc_launch_trust.js +8 -1
- package/dist/ui/terminal_image.d.ts +12 -0
- package/dist/ui/terminal_image.js +21 -1
- package/dist/utils/codex_project_trust.d.ts +19 -0
- package/dist/utils/codex_project_trust.js +37 -0
- package/dist/utils/compaction.js +103 -20
- package/dist/utils/git_program_files.d.ts +19 -12
- package/dist/utils/git_program_files.js +411 -83
- package/dist/utils/host_shell.d.ts +10 -1
- package/dist/utils/host_shell.js +14 -1
- package/dist/utils/mid_turn_compaction.d.ts +2 -0
- package/dist/utils/mid_turn_compaction.js +2 -0
- package/dist/utils/models.js +6 -1
- package/dist/utils/sandbox.d.ts +109 -8
- package/dist/utils/sandbox.js +981 -187
- package/dist/utils/shell.d.ts +2 -0
- package/dist/utils/shell.js +10 -4
- package/dist/utils/subprocess_env.d.ts +47 -11
- package/dist/utils/subprocess_env.js +107 -15
- package/dist/utils/system_ps.d.ts +8 -0
- package/dist/utils/system_ps.js +39 -7
- package/dist/utils/unattended_compaction.d.ts +9 -0
- package/dist/utils/unattended_compaction.js +13 -0
- package/dist/utils/ztc_own_state.d.ts +8 -5
- package/dist/utils/ztc_own_state.js +17 -11
- package/dist/web/mirror_client.js +1 -0
- package/dist/web/mirror_stream.d.ts +50 -6
- package/dist/web/mirror_stream.js +160 -15
- package/dist/workflows/registry.js +3 -1
- package/dist/workspace/controller.js +12 -0
- package/dist/workspace_collision.d.ts +3 -0
- package/dist/workspace_collision.js +14 -1
- package/dist/workspace_session_context.js +2 -1
- package/dist/zlm-embed.mjs +3823 -1970
- package/node_modules/@zerg/model-contracts/README.md +16 -4
- package/node_modules/@zerg/model-contracts/index.js +6 -1
- package/npm-shrinkwrap.json +1 -1
- package/package.json +42 -24
- package/packages/model-contracts/README.md +16 -4
- package/packages/model-contracts/index.js +6 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ColdStore } from './cold.js';
|
|
3
|
+
type BoundedState = {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
};
|
|
6
|
+
export declare const BOUNDS: {
|
|
7
|
+
readonly events: 64;
|
|
8
|
+
readonly changes: 64;
|
|
9
|
+
readonly conversationLines: 64;
|
|
10
|
+
readonly endedConversations: 16;
|
|
11
|
+
readonly messages: 256;
|
|
12
|
+
readonly openRequests: 128;
|
|
13
|
+
readonly closedRequests: 64;
|
|
14
|
+
readonly openTensions: 64;
|
|
15
|
+
readonly closedTensions: 64;
|
|
16
|
+
readonly schedule: 512;
|
|
17
|
+
readonly pinnedSchedule: 128;
|
|
18
|
+
readonly owedReserve: 16;
|
|
19
|
+
readonly datedPromises: 16;
|
|
20
|
+
readonly predictions: 4096;
|
|
21
|
+
readonly chronicle: 80;
|
|
22
|
+
readonly thingNotes: 120;
|
|
23
|
+
readonly memories: 256;
|
|
24
|
+
readonly pinnedMemories: 64;
|
|
25
|
+
readonly beliefs: 64;
|
|
26
|
+
readonly openCommitments: 128;
|
|
27
|
+
readonly closedCommitments: 64;
|
|
28
|
+
readonly activeGoals: 32;
|
|
29
|
+
readonly finishedGoals: 16;
|
|
30
|
+
readonly series: 64;
|
|
31
|
+
readonly stances: 256;
|
|
32
|
+
readonly kindRequests: 64;
|
|
33
|
+
readonly facetUse: 32;
|
|
34
|
+
readonly sightings: 4096;
|
|
35
|
+
readonly entities: 4096;
|
|
36
|
+
readonly passedDeadlines: 8;
|
|
37
|
+
readonly facts: 8;
|
|
38
|
+
};
|
|
39
|
+
export declare const foldSchema: z.ZodObject<{
|
|
40
|
+
count: z.ZodNumber;
|
|
41
|
+
first: z.ZodNullable<z.ZodNumber>;
|
|
42
|
+
last: z.ZodNullable<z.ZodNumber>;
|
|
43
|
+
digest: z.ZodString;
|
|
44
|
+
provenance: z.ZodString;
|
|
45
|
+
}, z.core.$strip>;
|
|
46
|
+
export type Fold = z.infer<typeof foldSchema>;
|
|
47
|
+
export declare const ringSchema: <T extends z.ZodTypeAny>(item: T) => z.ZodObject<{
|
|
48
|
+
items: z.ZodArray<T>;
|
|
49
|
+
fold: z.ZodObject<{
|
|
50
|
+
count: z.ZodNumber;
|
|
51
|
+
first: z.ZodNullable<z.ZodNumber>;
|
|
52
|
+
last: z.ZodNullable<z.ZodNumber>;
|
|
53
|
+
digest: z.ZodString;
|
|
54
|
+
provenance: z.ZodString;
|
|
55
|
+
}, z.core.$strip>;
|
|
56
|
+
}, z.core.$strip>;
|
|
57
|
+
export declare const tableRingSchema: <T extends z.ZodTypeAny>(item: T) => z.ZodObject<{
|
|
58
|
+
items: z.ZodRecord<z.ZodString, T>;
|
|
59
|
+
fold: z.ZodObject<{
|
|
60
|
+
count: z.ZodNumber;
|
|
61
|
+
first: z.ZodNullable<z.ZodNumber>;
|
|
62
|
+
last: z.ZodNullable<z.ZodNumber>;
|
|
63
|
+
digest: z.ZodString;
|
|
64
|
+
provenance: z.ZodString;
|
|
65
|
+
}, z.core.$strip>;
|
|
66
|
+
}, z.core.$strip>;
|
|
67
|
+
export type Ring<T> = {
|
|
68
|
+
items: T[];
|
|
69
|
+
fold: Fold;
|
|
70
|
+
};
|
|
71
|
+
export type TableRing<T> = {
|
|
72
|
+
items: Record<string, T>;
|
|
73
|
+
fold: Fold;
|
|
74
|
+
};
|
|
75
|
+
export declare function emptyFold(provenance: string): Fold;
|
|
76
|
+
export declare function capsBoundaryOf(state: {
|
|
77
|
+
admissionCapsFrom?: number;
|
|
78
|
+
revision: number;
|
|
79
|
+
}): number;
|
|
80
|
+
export declare function stable(value: unknown): string;
|
|
81
|
+
export declare function digestOf(value: unknown): string;
|
|
82
|
+
export declare function tickOf(item: unknown): number;
|
|
83
|
+
export declare function foldOf(previous: Fold | undefined, dropped: readonly unknown[], provenance: string): Fold;
|
|
84
|
+
export declare function ring<T>(items: readonly T[], cap: number, previous: Fold | undefined, provenance: string): Ring<T>;
|
|
85
|
+
export declare function tableRing<T>(items: Record<string, T>, cap: number, previous: Fold | undefined, provenance: string): TableRing<T>;
|
|
86
|
+
export declare function weigh(state: unknown): number;
|
|
87
|
+
export declare function notedShare(state: {
|
|
88
|
+
rulings?: number;
|
|
89
|
+
notedRulings?: number;
|
|
90
|
+
}): number | null;
|
|
91
|
+
export declare function owedSchedule(item: {
|
|
92
|
+
kind?: string;
|
|
93
|
+
targetId?: string | null;
|
|
94
|
+
decision?: unknown;
|
|
95
|
+
beat?: unknown;
|
|
96
|
+
}): boolean;
|
|
97
|
+
export declare function pinsOf(state: BoundedState): Record<string, number>;
|
|
98
|
+
export declare function boundState(state: BoundedState, cold?: ColdStore, grew?: boolean): void;
|
|
99
|
+
export declare function sealState<T extends BoundedState>(state: T): T;
|
|
100
|
+
export {};
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { current, isDraft } from 'immer';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
export const BOUNDS = {
|
|
5
|
+
events: 64,
|
|
6
|
+
changes: 64,
|
|
7
|
+
conversationLines: 64,
|
|
8
|
+
endedConversations: 16,
|
|
9
|
+
messages: 256,
|
|
10
|
+
openRequests: 128,
|
|
11
|
+
closedRequests: 64,
|
|
12
|
+
openTensions: 64,
|
|
13
|
+
closedTensions: 64,
|
|
14
|
+
schedule: 512,
|
|
15
|
+
pinnedSchedule: 128,
|
|
16
|
+
owedReserve: 16,
|
|
17
|
+
datedPromises: 16,
|
|
18
|
+
predictions: 4096,
|
|
19
|
+
chronicle: 80,
|
|
20
|
+
thingNotes: 120,
|
|
21
|
+
memories: 256,
|
|
22
|
+
pinnedMemories: 64,
|
|
23
|
+
beliefs: 64,
|
|
24
|
+
openCommitments: 128,
|
|
25
|
+
closedCommitments: 64,
|
|
26
|
+
activeGoals: 32,
|
|
27
|
+
finishedGoals: 16,
|
|
28
|
+
series: 64,
|
|
29
|
+
stances: 256,
|
|
30
|
+
kindRequests: 64,
|
|
31
|
+
facetUse: 32,
|
|
32
|
+
sightings: 4096,
|
|
33
|
+
entities: 4096,
|
|
34
|
+
passedDeadlines: 8,
|
|
35
|
+
facts: 8,
|
|
36
|
+
};
|
|
37
|
+
export const foldSchema = z.object({
|
|
38
|
+
count: z.number().int().min(0),
|
|
39
|
+
first: z.number().int().min(0).nullable(),
|
|
40
|
+
last: z.number().int().min(0).nullable(),
|
|
41
|
+
digest: z.string().max(64),
|
|
42
|
+
provenance: z.string().min(1).max(120),
|
|
43
|
+
});
|
|
44
|
+
export const ringSchema = (item) => z.object({ items: z.array(item), fold: foldSchema });
|
|
45
|
+
export const tableRingSchema = (item) => z.object({ items: z.record(z.string(), item), fold: foldSchema });
|
|
46
|
+
export function emptyFold(provenance) {
|
|
47
|
+
return { count: 0, first: null, last: null, digest: '', provenance };
|
|
48
|
+
}
|
|
49
|
+
export function capsBoundaryOf(state) {
|
|
50
|
+
return state.admissionCapsFrom ?? state.revision + 1;
|
|
51
|
+
}
|
|
52
|
+
export function stable(value) {
|
|
53
|
+
if (Array.isArray(value))
|
|
54
|
+
return `[${value.map(stable).join(',')}]`;
|
|
55
|
+
if (value !== null && typeof value === 'object') {
|
|
56
|
+
return `{${Object.keys(value).sort().map(key => `${JSON.stringify(key)}:${stable(value[key])}`).join(',')}}`;
|
|
57
|
+
}
|
|
58
|
+
return JSON.stringify(value);
|
|
59
|
+
}
|
|
60
|
+
export function digestOf(value) {
|
|
61
|
+
return createHash('sha256').update(stable(value)).digest('hex');
|
|
62
|
+
}
|
|
63
|
+
const FOLD_MODULUS = 1n << 256n;
|
|
64
|
+
export function tickOf(item) {
|
|
65
|
+
if (item === null || typeof item !== 'object')
|
|
66
|
+
return 0;
|
|
67
|
+
const record = item;
|
|
68
|
+
for (const key of ['seq', 'eventSeq', 'madeSeq', 'changedSeq', 'sourceSeq', 'openedSeq', 'introducedSeq', 'lastSeq', 'at', 'changedAt', 'madeAt', 'openedAt', 'endedAt', 'startedAt', 'introducedAt']) {
|
|
69
|
+
const value = record[key];
|
|
70
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
function orderOf(item) {
|
|
76
|
+
return Array.isArray(item) && item.length === 2 && typeof item[0] === 'string' ? tickOf(item[1]) : tickOf(item);
|
|
77
|
+
}
|
|
78
|
+
function compareOrder(left, right) {
|
|
79
|
+
return orderOf(left) - orderOf(right);
|
|
80
|
+
}
|
|
81
|
+
function foldLeaf(item) {
|
|
82
|
+
return BigInt(`0x${digestOf({ key: orderOf(item), item })}`);
|
|
83
|
+
}
|
|
84
|
+
function foldDigest(previous, dropped) {
|
|
85
|
+
let total = previous.length === 0 ? 0n : BigInt(`0x${previous}`);
|
|
86
|
+
for (const item of dropped)
|
|
87
|
+
total = (total + foldLeaf(item)) % FOLD_MODULUS;
|
|
88
|
+
return total.toString(16).padStart(64, '0');
|
|
89
|
+
}
|
|
90
|
+
function earliest(items, count) {
|
|
91
|
+
if (count <= 0)
|
|
92
|
+
return { dropped: [], kept: [...items] };
|
|
93
|
+
const order = items.map((_, index) => index).sort((left, right) => compareOrder(items[left], items[right]) || left - right);
|
|
94
|
+
const drop = new Set(order.slice(0, count));
|
|
95
|
+
return { dropped: order.slice(0, count).map(index => items[index]), kept: items.filter((_, index) => !drop.has(index)) };
|
|
96
|
+
}
|
|
97
|
+
export function foldOf(previous, dropped, provenance) {
|
|
98
|
+
if (dropped.length === 0)
|
|
99
|
+
return previous ?? emptyFold(provenance);
|
|
100
|
+
const ticks = dropped.map(tickOf);
|
|
101
|
+
const first = Math.min(...ticks);
|
|
102
|
+
const last = Math.max(...ticks);
|
|
103
|
+
return {
|
|
104
|
+
count: (previous?.count ?? 0) + dropped.length,
|
|
105
|
+
first: previous?.first === null || previous?.first === undefined ? first : Math.min(previous.first, first),
|
|
106
|
+
last: previous?.last === null || previous?.last === undefined ? last : Math.max(previous.last, last),
|
|
107
|
+
digest: foldDigest(previous?.digest ?? '', dropped),
|
|
108
|
+
provenance,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export function ring(items, cap, previous, provenance) {
|
|
112
|
+
const base = baseOf(items);
|
|
113
|
+
if (base.length <= cap)
|
|
114
|
+
return { items: [...base], fold: previous ?? emptyFold(provenance) };
|
|
115
|
+
const { dropped, kept } = earliest(base, base.length - cap);
|
|
116
|
+
return { items: kept, fold: foldOf(previous, dropped, provenance) };
|
|
117
|
+
}
|
|
118
|
+
export function tableRing(items, cap, previous, provenance) {
|
|
119
|
+
const entries = Object.entries(baseOf(items));
|
|
120
|
+
if (entries.length <= cap)
|
|
121
|
+
return { items: Object.fromEntries(entries), fold: previous ?? emptyFold(provenance) };
|
|
122
|
+
const { dropped, kept } = earliest(entries, entries.length - cap);
|
|
123
|
+
return { items: Object.fromEntries(kept), fold: foldOf(previous, dropped, provenance) };
|
|
124
|
+
}
|
|
125
|
+
export function weigh(state) {
|
|
126
|
+
return Buffer.byteLength(stable(state));
|
|
127
|
+
}
|
|
128
|
+
export function notedShare(state) {
|
|
129
|
+
const rulings = state.rulings;
|
|
130
|
+
if (typeof rulings !== 'number' || rulings <= 0)
|
|
131
|
+
return null;
|
|
132
|
+
return (state.notedRulings ?? 0) / rulings;
|
|
133
|
+
}
|
|
134
|
+
function baseOf(value) {
|
|
135
|
+
return isDraft(value) ? current(value) : value;
|
|
136
|
+
}
|
|
137
|
+
function capList(state, key, items, cap, provenance) {
|
|
138
|
+
if (items.length <= cap)
|
|
139
|
+
return null;
|
|
140
|
+
const base = baseOf(items);
|
|
141
|
+
const { dropped, kept } = earliest(base, base.length - cap);
|
|
142
|
+
state.folds[key] = foldOf(state.folds[key], dropped, provenance);
|
|
143
|
+
return kept;
|
|
144
|
+
}
|
|
145
|
+
function capKeeping(state, key, items, pinnedCap, restCap, keep, provenance) {
|
|
146
|
+
let pinnedCount = 0;
|
|
147
|
+
for (const item of items)
|
|
148
|
+
if (keep(item))
|
|
149
|
+
pinnedCount += 1;
|
|
150
|
+
const restCount = items.length - pinnedCount;
|
|
151
|
+
if (pinnedCount <= pinnedCap && restCount <= restCap)
|
|
152
|
+
return null;
|
|
153
|
+
const base = baseOf(items);
|
|
154
|
+
const pinned = base.filter(keep).sort(compareOrder);
|
|
155
|
+
const rest = base.filter(item => !keep(item)).sort(compareOrder);
|
|
156
|
+
const droppedPinned = pinned.length <= pinnedCap ? [] : pinned.slice(0, pinned.length - pinnedCap);
|
|
157
|
+
const droppedRest = rest.length <= restCap ? [] : rest.slice(0, rest.length - restCap);
|
|
158
|
+
const dropped = [...droppedPinned, ...droppedRest];
|
|
159
|
+
state.folds[key] = foldOf(state.folds[key], dropped, provenance);
|
|
160
|
+
const kept = new Set([...pinned.slice(pinned.length - pinnedCap), ...rest.slice(rest.length - restCap)]);
|
|
161
|
+
return base.filter(item => kept.has(item));
|
|
162
|
+
}
|
|
163
|
+
export function owedSchedule(item) {
|
|
164
|
+
return item.kind === 'finish' || item.kind === 'reply' || item.kind === 'answer'
|
|
165
|
+
|| item.decision !== undefined || (item.targetId !== null && item.targetId !== undefined) || item.beat !== undefined;
|
|
166
|
+
}
|
|
167
|
+
function capOwed(state, key, items, restCap, keep, provenance) {
|
|
168
|
+
let restCount = 0;
|
|
169
|
+
for (const item of items)
|
|
170
|
+
if (!keep(item))
|
|
171
|
+
restCount += 1;
|
|
172
|
+
if (restCount <= restCap)
|
|
173
|
+
return null;
|
|
174
|
+
const base = baseOf(items);
|
|
175
|
+
const rest = base.filter(item => !keep(item)).sort(compareOrder);
|
|
176
|
+
const dropped = rest.slice(0, rest.length - restCap);
|
|
177
|
+
state.folds[key] = foldOf(state.folds[key], dropped, provenance);
|
|
178
|
+
const drop = new Set(dropped);
|
|
179
|
+
return base.filter(item => !drop.has(item));
|
|
180
|
+
}
|
|
181
|
+
export function pinsOf(state) {
|
|
182
|
+
const pins = {};
|
|
183
|
+
const pin = (id) => {
|
|
184
|
+
if (typeof id !== 'string' || state.entities[id] === undefined)
|
|
185
|
+
return;
|
|
186
|
+
pins[id] = (pins[id] ?? 0) + 1;
|
|
187
|
+
};
|
|
188
|
+
for (const person of Object.values(state.people)) {
|
|
189
|
+
for (const memory of person.memories)
|
|
190
|
+
for (const id of memory.thingIds ?? [])
|
|
191
|
+
pin(id);
|
|
192
|
+
}
|
|
193
|
+
for (const list of [state.schedule, state.predictions.items, state.requests, state.tensions]) {
|
|
194
|
+
for (const item of list) {
|
|
195
|
+
pin(item.targetId);
|
|
196
|
+
pin(item.entityId);
|
|
197
|
+
pin(item.thingId);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
for (const id of Object.keys(state.thingNotes.items))
|
|
201
|
+
pin(id);
|
|
202
|
+
for (const id of Object.keys(state.globalEntityRefs))
|
|
203
|
+
pin(id);
|
|
204
|
+
for (const object of Object.values(state.objects))
|
|
205
|
+
pin(object.containerId);
|
|
206
|
+
for (const entity of Object.values(state.entities)) {
|
|
207
|
+
for (const property of state.kinds[entity.kindId]?.properties ?? []) {
|
|
208
|
+
if (property.type !== 'id')
|
|
209
|
+
continue;
|
|
210
|
+
pin(entity.properties[property.id]?.value);
|
|
211
|
+
}
|
|
212
|
+
for (const relation of Object.values(entity.relations))
|
|
213
|
+
pin(relation.targetId);
|
|
214
|
+
}
|
|
215
|
+
return pins;
|
|
216
|
+
}
|
|
217
|
+
function touchedOf(state, id) {
|
|
218
|
+
return state.touched[id] ?? state.entities[id].introducedSeq ?? 0;
|
|
219
|
+
}
|
|
220
|
+
function forgetEntity(state, id) {
|
|
221
|
+
delete state.touched[id];
|
|
222
|
+
delete state.pins[id];
|
|
223
|
+
delete state.worldEntityIds[id];
|
|
224
|
+
for (const [ref, ids] of Object.entries(state.globalEntityRefs)) {
|
|
225
|
+
if (ids[id] === undefined)
|
|
226
|
+
continue;
|
|
227
|
+
delete ids[id];
|
|
228
|
+
if (Object.keys(ids).length === 0)
|
|
229
|
+
delete state.globalEntityRefs[ref];
|
|
230
|
+
}
|
|
231
|
+
for (const [container, ids] of Object.entries(state.within)) {
|
|
232
|
+
const left = ids.filter(child => child !== id);
|
|
233
|
+
if (left.length > 0)
|
|
234
|
+
state.within[container] = left;
|
|
235
|
+
else
|
|
236
|
+
delete state.within[container];
|
|
237
|
+
}
|
|
238
|
+
for (const [subject, ids] of Object.entries(state.entityMoveSubjectIds)) {
|
|
239
|
+
const left = ids.filter(child => child !== id);
|
|
240
|
+
if (left.length > 0)
|
|
241
|
+
state.entityMoveSubjectIds[subject] = left;
|
|
242
|
+
else
|
|
243
|
+
delete state.entityMoveSubjectIds[subject];
|
|
244
|
+
}
|
|
245
|
+
state.globalEntityIds = state.globalEntityIds.filter((child) => child !== id);
|
|
246
|
+
}
|
|
247
|
+
function boundEntities(state, cold, grew) {
|
|
248
|
+
if (cold === undefined || !grew)
|
|
249
|
+
return;
|
|
250
|
+
const ids = Object.keys(state.entities);
|
|
251
|
+
if (ids.length <= BOUNDS.entities)
|
|
252
|
+
return;
|
|
253
|
+
state.pins = pinsOf(state);
|
|
254
|
+
const coldest = ids.filter(id => (state.pins[id] ?? 0) === 0)
|
|
255
|
+
.sort((left, right) => touchedOf(state, left) - touchedOf(state, right) || (left < right ? -1 : left > right ? 1 : 0));
|
|
256
|
+
const parked = [];
|
|
257
|
+
const kept = { ...state.entities };
|
|
258
|
+
for (const id of coldest.slice(0, ids.length - BOUNDS.entities)) {
|
|
259
|
+
const entity = state.entities[id];
|
|
260
|
+
cold.park({ id, value: baseOf(entity) });
|
|
261
|
+
parked.push({ at: touchedOf(state, id), id });
|
|
262
|
+
delete kept[id];
|
|
263
|
+
forgetEntity(state, id);
|
|
264
|
+
}
|
|
265
|
+
if (parked.length > 0) {
|
|
266
|
+
state.entities = kept;
|
|
267
|
+
state.parked = foldOf(state.parked, parked, 'parked');
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
function boundMemories(state, cold) {
|
|
271
|
+
const entries = [];
|
|
272
|
+
for (const person of Object.values(state.people)) {
|
|
273
|
+
for (const memory of person.memories)
|
|
274
|
+
entries.push({ personId: person.id, memory });
|
|
275
|
+
}
|
|
276
|
+
const cap = BOUNDS.memories * Object.keys(state.people).length;
|
|
277
|
+
if (entries.length <= cap)
|
|
278
|
+
return;
|
|
279
|
+
entries.sort((left, right) => tickOf(left.memory) - tickOf(right.memory)
|
|
280
|
+
|| (left.personId < right.personId ? -1 : left.personId > right.personId ? 1 : 0)
|
|
281
|
+
|| (left.memory.id < right.memory.id ? -1 : left.memory.id > right.memory.id ? 1 : 0));
|
|
282
|
+
const dropped = entries.slice(0, entries.length - cap);
|
|
283
|
+
const drop = new Set(dropped.map(entry => `${entry.personId}:${entry.memory.id}`));
|
|
284
|
+
for (const entry of dropped)
|
|
285
|
+
cold?.parkMemory?.(entry.personId, baseOf(entry.memory));
|
|
286
|
+
for (const person of Object.values(state.people)) {
|
|
287
|
+
const kept = person.memories.filter((memory) => !drop.has(`${person.id}:${memory.id}`));
|
|
288
|
+
if (kept.length !== person.memories.length)
|
|
289
|
+
person.memories = kept;
|
|
290
|
+
}
|
|
291
|
+
state.folds.memories = foldOf(state.folds.memories, dropped.map(entry => entry.memory), 'memories');
|
|
292
|
+
}
|
|
293
|
+
export function boundState(state, cold, grew = true) {
|
|
294
|
+
state.folds ??= {};
|
|
295
|
+
const messages = capList(state, 'messages', state.messages, BOUNDS.messages, 'messages');
|
|
296
|
+
if (messages)
|
|
297
|
+
state.messages = messages;
|
|
298
|
+
const schedule = capOwed(state, 'schedule', state.schedule, BOUNDS.schedule, owedSchedule, 'schedule');
|
|
299
|
+
if (schedule)
|
|
300
|
+
state.schedule = schedule;
|
|
301
|
+
if (state.predictions.items.length > BOUNDS.predictions)
|
|
302
|
+
state.predictions = ring(state.predictions.items, BOUNDS.predictions, state.predictions.fold, 'predictions');
|
|
303
|
+
const stances = capList(state, 'stances', state.stances, BOUNDS.stances, 'stances');
|
|
304
|
+
if (stances)
|
|
305
|
+
state.stances = stances;
|
|
306
|
+
const kindRequests = capList(state, 'kindRequests', state.kindRequests, BOUNDS.kindRequests, 'kindRequests');
|
|
307
|
+
if (kindRequests)
|
|
308
|
+
state.kindRequests = kindRequests;
|
|
309
|
+
const facts = capList(state, 'facts', state.facts, BOUNDS.facts, 'facts');
|
|
310
|
+
if (facts)
|
|
311
|
+
state.facts = facts;
|
|
312
|
+
if (state.passedDeadlines !== undefined) {
|
|
313
|
+
const passedDeadlines = capList(state, 'passedDeadlines', state.passedDeadlines, BOUNDS.passedDeadlines, 'passedDeadlines');
|
|
314
|
+
if (passedDeadlines)
|
|
315
|
+
state.passedDeadlines = passedDeadlines;
|
|
316
|
+
}
|
|
317
|
+
for (const conversation of state.conversations) {
|
|
318
|
+
if (conversation.lines.length <= BOUNDS.conversationLines)
|
|
319
|
+
continue;
|
|
320
|
+
const key = `conversation:${conversation.id}:lines`;
|
|
321
|
+
const { dropped, kept } = earliest(baseOf(conversation.lines), conversation.lines.length - BOUNDS.conversationLines);
|
|
322
|
+
state.folds[key] = foldOf(state.folds[key], dropped, key);
|
|
323
|
+
conversation.lines = kept;
|
|
324
|
+
}
|
|
325
|
+
const ended = state.conversations.filter((conversation) => conversation.endedAt !== null);
|
|
326
|
+
if (ended.length > BOUNDS.endedConversations) {
|
|
327
|
+
const { dropped } = earliest(ended, ended.length - BOUNDS.endedConversations);
|
|
328
|
+
state.folds.conversations = foldOf(state.folds.conversations, dropped, 'conversations');
|
|
329
|
+
const ids = new Set(dropped.map(conversation => conversation.id));
|
|
330
|
+
state.conversations = state.conversations.filter((conversation) => !ids.has(conversation.id));
|
|
331
|
+
}
|
|
332
|
+
const requests = capKeeping(state, 'requests', state.requests, BOUNDS.openRequests, BOUNDS.closedRequests, (request) => request.status === 'open', 'requests');
|
|
333
|
+
if (requests)
|
|
334
|
+
state.requests = requests;
|
|
335
|
+
const tensions = capOwed(state, 'tensions', state.tensions, BOUNDS.closedTensions, (tension) => tension.status === 'open', 'tensions');
|
|
336
|
+
if (tensions)
|
|
337
|
+
state.tensions = tensions;
|
|
338
|
+
for (const person of Object.values(state.people)) {
|
|
339
|
+
const memories = capKeeping(state, `memories:${person.id}`, person.memories, BOUNDS.pinnedMemories, BOUNDS.memories, (memory) => memory.importance >= 8 || (memory.thingIds?.length ?? 0) > 0, `memories:${person.id}`);
|
|
340
|
+
if (memories)
|
|
341
|
+
person.memories = memories;
|
|
342
|
+
const beliefs = capList(state, `beliefs:${person.id}`, person.beliefs, BOUNDS.beliefs, `beliefs:${person.id}`);
|
|
343
|
+
if (beliefs)
|
|
344
|
+
person.beliefs = beliefs;
|
|
345
|
+
const commitments = capKeeping(state, `commitments:${person.id}`, person.commitments, BOUNDS.openCommitments, BOUNDS.closedCommitments, (commitment) => commitment.status === 'open', `commitments:${person.id}`);
|
|
346
|
+
if (commitments)
|
|
347
|
+
person.commitments = commitments;
|
|
348
|
+
const goals = capKeeping(state, `goals:${person.id}`, person.goals, BOUNDS.activeGoals, BOUNDS.finishedGoals, (goal) => goal.status === 'active', `goals:${person.id}`);
|
|
349
|
+
if (goals)
|
|
350
|
+
person.goals = goals;
|
|
351
|
+
}
|
|
352
|
+
if (state.chronicle.items.length > BOUNDS.chronicle)
|
|
353
|
+
state.chronicle = ring(state.chronicle.items, BOUNDS.chronicle, state.chronicle.fold, 'chronicle');
|
|
354
|
+
if (Object.keys(state.thingNotes.items).length > BOUNDS.thingNotes)
|
|
355
|
+
state.thingNotes = tableRing(state.thingNotes.items, BOUNDS.thingNotes, state.thingNotes.fold, 'thingNotes');
|
|
356
|
+
if (Object.keys(state.facetUse.items).length > BOUNDS.facetUse)
|
|
357
|
+
state.facetUse = tableRing(state.facetUse.items, BOUNDS.facetUse, state.facetUse.fold, 'facetUse');
|
|
358
|
+
const sightings = Object.entries(state.sightings);
|
|
359
|
+
if (sightings.length > BOUNDS.sightings) {
|
|
360
|
+
const { dropped, kept } = earliest(sightings, sightings.length - BOUNDS.sightings);
|
|
361
|
+
state.folds.sightings = foldOf(state.folds.sightings, dropped, 'sightings');
|
|
362
|
+
state.sightings = Object.fromEntries(kept);
|
|
363
|
+
}
|
|
364
|
+
boundEntities(state, cold, grew);
|
|
365
|
+
boundMemories(state, cold);
|
|
366
|
+
}
|
|
367
|
+
export function sealState(state) {
|
|
368
|
+
for (const person of Object.values(state.people))
|
|
369
|
+
if (!Object.isFrozen(person.memories))
|
|
370
|
+
Object.freeze(person.memories);
|
|
371
|
+
for (const list of [state.changes, state.globalEntityIds])
|
|
372
|
+
if (!Object.isFrozen(list))
|
|
373
|
+
Object.freeze(list);
|
|
374
|
+
for (const event of state.events)
|
|
375
|
+
if (!Object.isFrozen(event))
|
|
376
|
+
freezeThrough(event);
|
|
377
|
+
for (const table of [state.worldEntityIds, state.globalEntityRefs, state.within, state.entityMoveSubjectIds]) {
|
|
378
|
+
if (Object.isFrozen(table))
|
|
379
|
+
continue;
|
|
380
|
+
for (const value of Object.values(table))
|
|
381
|
+
if (typeof value === 'object' && !Object.isFrozen(value))
|
|
382
|
+
Object.freeze(value);
|
|
383
|
+
Object.freeze(table);
|
|
384
|
+
}
|
|
385
|
+
return state;
|
|
386
|
+
}
|
|
387
|
+
function freezeThrough(value) {
|
|
388
|
+
if (value === null || typeof value !== 'object' || Object.isFrozen(value))
|
|
389
|
+
return;
|
|
390
|
+
Object.freeze(value);
|
|
391
|
+
for (const child of Object.values(value))
|
|
392
|
+
freezeThrough(child);
|
|
393
|
+
}
|