gentle-pi 4.5.0 → 4.6.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/README.md +9 -6
- package/assets/agents/gentle-ai-verify.md +4 -0
- package/assets/agents/gentle-ai-worker.md +1 -0
- package/assets/migrations/agent-body-history.json +147 -0
- package/assets/orchestrator-delegation-reference.md +3 -3
- package/assets/orchestrator-delegation.md +2 -2
- package/assets/orchestrator-verification.md +10 -6
- package/assets/orchestrator.md +8 -8
- package/bin/gentle-shell.mjs +61 -26
- package/docs/ci-performance.md +11 -0
- package/docs/delegated-verification.md +6 -0
- package/docs/fast-mode.md +15 -0
- package/docs/gentle-agents-activity.md +8 -0
- package/docs/gentle-music.md +6 -3
- package/docs/gentle-shell.md +70 -20
- package/docs/install-wizard.md +1 -1
- package/docs/mcp-status.md +52 -0
- package/docs/prompt-budget.md +28 -0
- package/docs/readme-reference.md +82 -21
- package/docs/session-profile-format.md +20 -5
- package/docs/sound-notifications.md +1 -1
- package/docs/star-prompt.md +32 -0
- package/docs/work-continuity.md +46 -0
- package/extensions/ask-user-question.ts +1 -0
- package/extensions/codegraph-tools.ts +38 -4
- package/extensions/gentle-agents.ts +477 -50
- package/extensions/gentle-ai.ts +1046 -191
- package/extensions/gentle-fast.ts +61 -0
- package/extensions/gentle-guide.ts +174 -0
- package/extensions/gentle-herdr-activity.ts +21 -1
- package/extensions/gentle-herdr-agent.ts +144 -0
- package/extensions/gentle-music.ts +10 -0
- package/extensions/gentle-program-identity.ts +61 -0
- package/extensions/gentle-shell.ts +642 -97
- package/extensions/gentle-star-prompt.ts +334 -0
- package/extensions/gentle-stats.ts +5 -5
- package/extensions/gentle-todo.ts +42 -22
- package/extensions/history/store.ts +18 -12
- package/extensions/skill-registry.ts +35 -3
- package/extensions/startup-banner.ts +39 -7
- package/lib/agent-assets.ts +74 -17
- package/lib/agent-profiles.ts +1 -1
- package/lib/agents-config.ts +12 -3
- package/lib/agents-keys.ts +8 -16
- package/lib/agents-protocol.ts +80 -4
- package/lib/agents-runner.ts +105 -4
- package/lib/agents-view.ts +6 -1
- package/lib/agents-widget.ts +132 -9
- package/lib/bridge-cost.ts +28 -0
- package/lib/codemode-renderer.ts +16 -0
- package/lib/command-palette-catalog.ts +1 -0
- package/lib/command-palette.ts +21 -8
- package/lib/exploration-budget.ts +62 -0
- package/lib/gentle-guide.ts +201 -0
- package/lib/gentle-shell-launcher.ts +4 -0
- package/lib/herdr-activity.ts +9 -3
- package/lib/herdr-task-summary-policy.ts +85 -0
- package/lib/inprocess-reviewer.ts +33 -13
- package/lib/jobs-sidebar-state.ts +5 -6
- package/lib/jobs-widget.ts +16 -52
- package/lib/keymaps-customize.ts +81 -0
- package/lib/keymaps.ts +291 -0
- package/lib/model-fallback-notice.ts +61 -0
- package/lib/model-fallback-policy.ts +141 -0
- package/lib/model-fallback-store.ts +143 -0
- package/lib/model-routing-authority.ts +10 -2
- package/lib/nan-provider.ts +3 -2
- package/lib/native-review-cli.ts +7 -2
- package/lib/notification-customize.ts +48 -51
- package/lib/openai-fast-tier.ts +22 -0
- package/lib/orchestrator-discovery.ts +5 -1
- package/lib/orchestrator-fallback.ts +75 -0
- package/lib/pasted-image-paths.ts +113 -0
- package/lib/questionnaire/questionnaire-view.ts +121 -7
- package/lib/review-candidate-view-owner.ts +4 -0
- package/lib/review-candidate-view.ts +10 -7
- package/lib/review-host-relay.ts +5 -1
- package/lib/review-integration-v2.ts +22 -7
- package/lib/review-repository.ts +5 -0
- package/lib/session-profile-append-controller.ts +21 -6
- package/lib/session-profile-authority.ts +119 -0
- package/lib/session-profile-binding.ts +54 -5
- package/lib/session-profile-freeze.ts +105 -9
- package/lib/session-profile-integration.ts +313 -0
- package/lib/session-profile-persistence.ts +63 -9
- package/lib/shell-bar.ts +171 -56
- package/lib/shell-card.ts +26 -6
- package/lib/shell-page.ts +120 -0
- package/lib/shell-panel.ts +149 -0
- package/lib/shell-prompt.ts +53 -1
- package/lib/shell-sidebar-layout.ts +98 -25
- package/lib/shell-sidebar.ts +54 -1
- package/lib/shell-stats.ts +123 -0
- package/lib/shell-todo.ts +71 -0
- package/lib/shell-usage.ts +12 -2
- package/lib/star-prompt.ts +197 -0
- package/lib/verifier-admission.ts +18 -8
- package/lib/verifier-settle-reminder.ts +64 -0
- package/lib/vim-editor-adapter.ts +6 -1
- package/lib/visual-customization-policy.ts +41 -6
- package/lib/visual-customize-view.ts +164 -26
- package/lib/visual-layout-preview.ts +43 -0
- package/lib/yolo-session-policy.ts +28 -1
- package/package.json +1 -1
- package/runtime/gentle-shell-launcher.mjs +4 -0
- package/runtime/native-review-cli.mjs +7 -2
- package/runtime/review-integration-v2.mjs +22 -7
- package/runtime/windows-session-transport.ps1 +60 -3
- package/scripts/bootstrap.sh +4 -1
- package/scripts/installer-preflight.mjs +1 -1
- package/scripts/pi-update.mjs +113 -3
- package/scripts/run-test-suite.mjs +17 -1
- package/scripts/shell-snapshot.mjs +340 -0
- package/scripts/test-packed-runner.mjs +1 -1
- package/scripts/update-agent-body-history.mjs +41 -0
- package/scripts/verify-installer-tap.mjs +45 -0
- package/scripts/verify-package-files.mjs +2 -0
- package/skills/gentle-ai/SKILL.md +2 -2
- package/skills/tui-design/NOTICE.md +5 -0
- package/skills/tui-design/SKILL.md +56 -0
- package/skills/tui-design/references/commands.md +75 -0
- package/skills/tui-design/references/critique.md +48 -0
- package/skills/tui-design/references/design-doc.md +44 -0
- package/skills/tui-design/references/terminal-craft.md +64 -0
- package/skills/tui-design/references/visual-loop.md +32 -0
- package/tests/agents-protocol.test.ts +53 -1
- package/tests/agents-runner.test.ts +168 -3
- package/tests/agents-view.test.ts +27 -0
- package/tests/agents-widget.test.ts +312 -10
- package/tests/bounded-scan-contract.test.ts +39 -0
- package/tests/bridge-cost.test.ts +32 -0
- package/tests/bundled-install.test.ts +6 -4
- package/tests/ci-concurrency.test.ts +11 -0
- package/tests/ci-merge-base-identity.test.ts +114 -0
- package/tests/codegraph-tools.test.ts +69 -0
- package/tests/codemode-rendering.test.ts +22 -2
- package/tests/command-palette.test.ts +16 -2
- package/tests/commit-slice-signal.test.ts +11 -9
- package/tests/delegate-for-reason-contract.test.ts +5 -4
- package/tests/design-canvas-server.test.ts +6 -2
- package/tests/exploration-budget.test.ts +128 -0
- package/tests/fixtures/agent-assets/gentle-ai-worker.strict-tdd-era.md +122 -0
- package/tests/fixtures/measure-orchestrator-prompt.mjs +2 -1
- package/tests/fixtures/primary-session-prompt/gentleman.golden.md +18 -10
- package/tests/fixtures/session-profile-v1/replay.ts +125 -0
- package/tests/fixtures/session-profile-v1/session-profile-authority.ts +113 -0
- package/tests/fixtures/session-profile-v1/session-profile-binding.ts +83 -0
- package/tests/fixtures/session-profile-v1/session-profile-disk-reader.ts +182 -0
- package/tests/fixtures/session-profile-v1/session-profile-freeze.ts +290 -0
- package/tests/fixtures/session-profile-v1/session-profile-persistence.ts +195 -0
- package/tests/fixtures/session-profile-v1/v1-inputs.ts +16 -0
- package/tests/fixtures/windows-pipe-availability.ps1 +26 -0
- package/tests/fixtures/windows-session-bootstrap.ps1 +6 -2
- package/tests/gentle-agents.test.ts +416 -21
- package/tests/gentle-ai-exploration-budget.test.ts +135 -0
- package/tests/gentle-ai.test.ts +1011 -37
- package/tests/gentle-fast.test.ts +73 -0
- package/tests/gentle-guide.test.ts +208 -0
- package/tests/gentle-herdr-activity.test.ts +76 -2
- package/tests/gentle-herdr-agent.test.ts +111 -0
- package/tests/gentle-music.test.ts +36 -1
- package/tests/gentle-program-identity.test.ts +197 -0
- package/tests/gentle-shell-bin.test.ts +49 -26
- package/tests/gentle-shell-launcher.test.ts +18 -2
- package/tests/gentle-shell.test.ts +1068 -37
- package/tests/gentle-stats.test.ts +15 -2
- package/tests/gentle-todo.test.ts +50 -4
- package/tests/herdr-task-summary-policy.test.ts +128 -0
- package/tests/history-store-private-mode.test.ts +40 -0
- package/tests/image-paste-chips.test.ts +42 -0
- package/tests/inprocess-reviewer.test.ts +122 -0
- package/tests/installer-posix-bootstrap.test.ts +67 -5
- package/tests/installer-server.test.ts +2 -2
- package/tests/installer-windows-diagnostics.test.ts +4 -5
- package/tests/isolated-config-home.ts +28 -0
- package/tests/jobs-widget.test.ts +25 -37
- package/tests/keymaps-customize.test.ts +114 -0
- package/tests/keymaps.test.ts +103 -0
- package/tests/maintainer/mcp-status.maintest.ts +88 -0
- package/tests/maintainer/pi-update-source.maintest.ts +116 -0
- package/tests/model-fallback-notice.test.ts +45 -0
- package/tests/model-fallback-policy.test.ts +83 -0
- package/tests/model-fallback-store.test.ts +111 -0
- package/tests/nan-provider.test.ts +4 -4
- package/tests/no-self-pause-contract.test.ts +19 -0
- package/tests/notification-customize.test.ts +58 -34
- package/tests/odd-routing-canonical-ratchet.test.ts +3 -3
- package/tests/odd-routing-contract.test.ts +1 -1
- package/tests/orchestrator-fallback.test.ts +82 -0
- package/tests/orchestrator-prompt-budget.test.ts +131 -0
- package/tests/package-manifest.test.ts +160 -7
- package/tests/pasted-image-paths.test.ts +118 -0
- package/tests/persona-reply-language.test.ts +61 -0
- package/tests/pi-update-launcher.test.ts +32 -8
- package/tests/pi-update-source.test.ts +162 -0
- package/tests/profile-fast-tier.test.ts +311 -0
- package/tests/profile-tier-footer.test.ts +79 -0
- package/tests/questionnaire-view.test.ts +150 -0
- package/tests/review-candidate-owner-sweep.test.ts +93 -0
- package/tests/review-candidate-view.test.ts +51 -0
- package/tests/review-controller-native-routing.test.ts +120 -6
- package/tests/review-gate.test.ts +24 -0
- package/tests/review-inspect-preflight.test.ts +197 -0
- package/tests/review-integration-v2-forward.test.ts +59 -0
- package/tests/review-repository.test.ts +52 -1
- package/tests/review-sidebar-state.test.ts +5 -5
- package/tests/run-test-suite.test.ts +61 -5
- package/tests/sensitive-path-tools.test.ts +74 -0
- package/tests/session-profile-append-controller.test.ts +63 -0
- package/tests/session-profile-authority.test.ts +259 -0
- package/tests/session-profile-binding.test.ts +15 -0
- package/tests/session-profile-extension.test.ts +615 -0
- package/tests/session-profile-freeze.test.ts +110 -2
- package/tests/session-profile-integration.test.ts +857 -0
- package/tests/session-profile-persistence.test.ts +57 -0
- package/tests/session-profile-v1-compat.test.ts +221 -0
- package/tests/shell-bar.test.ts +113 -55
- package/tests/shell-card.test.ts +13 -1
- package/tests/shell-page.test.ts +108 -0
- package/tests/shell-panel.test.ts +332 -0
- package/tests/shell-prompt.test.ts +54 -0
- package/tests/shell-render-cache.test.ts +5 -0
- package/tests/shell-sidebar-fullscreen.test.ts +12 -0
- package/tests/shell-sidebar-layout.test.ts +159 -27
- package/tests/shell-stats.test.ts +90 -0
- package/tests/shell-usage.test.ts +14 -0
- package/tests/skill-registry.test.ts +68 -1
- package/tests/star-prompt.test.ts +470 -0
- package/tests/startup-banner.test.ts +57 -1
- package/tests/task-size-routing-contract.test.ts +45 -1
- package/tests/verifier-settle-reminder.test.ts +281 -0
- package/tests/verify-first-launch-contract.test.ts +56 -5
- package/tests/verify-installer-tap.test.ts +57 -0
- package/tests/vim-editor-adapter.test.ts +2 -2
- package/tests/visual-customization-policy.test.ts +61 -7
- package/tests/visual-customize-view.test.ts +253 -8
- package/tests/visual-layout-preview.test.ts +28 -0
- package/tests/windows-session-bootstrap.test.ts +105 -2
- package/tests/work-continuity-contract.test.ts +86 -0
- package/tests/writer-surfaces.test.ts +12 -10
- package/tests/yolo-customize.test.ts +30 -11
- package/tests/yolo-mode-runtime.test.ts +4 -2
- package/tests/yolo-mode.test.ts +16 -2
|
@@ -0,0 +1,857 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdtempSync, readFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { test } from "node:test";
|
|
8
|
+
import {
|
|
9
|
+
AgentSessionRuntime,
|
|
10
|
+
SessionManager,
|
|
11
|
+
} from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import {
|
|
13
|
+
createSessionProfileIntegration,
|
|
14
|
+
hasPendingSessionProfileSelection,
|
|
15
|
+
readCurrentSessionProfileOutcome,
|
|
16
|
+
runCurrentSessionProfileSelection,
|
|
17
|
+
waitCurrentSessionProfileSelection,
|
|
18
|
+
} from "../lib/session-profile-integration.ts";
|
|
19
|
+
|
|
20
|
+
const version = JSON.parse(
|
|
21
|
+
readFileSync(
|
|
22
|
+
join(
|
|
23
|
+
dirname(
|
|
24
|
+
fileURLToPath(import.meta.resolve("@earendil-works/pi-coding-agent")),
|
|
25
|
+
),
|
|
26
|
+
"..",
|
|
27
|
+
"package.json",
|
|
28
|
+
),
|
|
29
|
+
"utf8",
|
|
30
|
+
),
|
|
31
|
+
).version;
|
|
32
|
+
assert.ok(
|
|
33
|
+
Number(version.split(".")[0]) >= 1,
|
|
34
|
+
"Lifecycle public fixtures require Pi >=1.0.0",
|
|
35
|
+
);
|
|
36
|
+
console.info(`Public lifecycle fixture API: ${version}`);
|
|
37
|
+
function fixture(memory = false) {
|
|
38
|
+
const root = mkdtempSync(join(tmpdir(), "profile-lifecycle-"));
|
|
39
|
+
const manager = memory
|
|
40
|
+
? SessionManager.inMemory(root)
|
|
41
|
+
: SessionManager.create(root, join(root, "sessions"));
|
|
42
|
+
return {
|
|
43
|
+
manager,
|
|
44
|
+
integration: createSessionProfileIntegration(),
|
|
45
|
+
append: (type: string, data: unknown): void => {
|
|
46
|
+
manager.appendCustomEntry(type, data);
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function flush(manager: SessionManager) {
|
|
51
|
+
manager.appendMessage({
|
|
52
|
+
role: "user",
|
|
53
|
+
content: "isolated lifecycle fixture",
|
|
54
|
+
timestamp: 1,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function name(outcome: { state: unknown }, expected: string) {
|
|
58
|
+
const state = outcome.state as { status: string; binding?: { name: string } };
|
|
59
|
+
assert.equal(state.status, "bound");
|
|
60
|
+
assert.equal(state.binding?.name, expected);
|
|
61
|
+
}
|
|
62
|
+
function persistedCopy(manager: SessionManager, leaf: string) {
|
|
63
|
+
const sourceFile = manager.getSessionFile();
|
|
64
|
+
assert.ok(sourceFile);
|
|
65
|
+
const copy = SessionManager.open(sourceFile, manager.getSessionDir());
|
|
66
|
+
copy.createBranchedSession(leaf);
|
|
67
|
+
return copy;
|
|
68
|
+
}
|
|
69
|
+
for (const operation of ["bind", "clear"] as const) {
|
|
70
|
+
test(`reload revokes unwritten ${operation} and old capability; later nonfailed disk restoration remains valid`, () => {
|
|
71
|
+
const f = fixture();
|
|
72
|
+
let oldCalls = 0,
|
|
73
|
+
newCalls = 0;
|
|
74
|
+
const initial = f.integration.start(
|
|
75
|
+
f.manager,
|
|
76
|
+
(t, d) => {
|
|
77
|
+
oldCalls++;
|
|
78
|
+
f.append(t, d);
|
|
79
|
+
},
|
|
80
|
+
{ reason: "startup" },
|
|
81
|
+
);
|
|
82
|
+
const selected =
|
|
83
|
+
operation === "bind"
|
|
84
|
+
? initial.attachment.bind("pending", {})
|
|
85
|
+
: initial.attachment.clear();
|
|
86
|
+
assert.equal(selected.status, "not-persisted");
|
|
87
|
+
f.integration.shutdown(f.manager, { reason: "reload" });
|
|
88
|
+
assert.equal(initial.attachment.refresh().status, "indeterminate");
|
|
89
|
+
assert.equal(initial.attachment.bind("stale", {}).status, "indeterminate");
|
|
90
|
+
assert.equal(oldCalls, 1);
|
|
91
|
+
const reloadedModule = createSessionProfileIntegration();
|
|
92
|
+
const current = reloadedModule.start(
|
|
93
|
+
f.manager,
|
|
94
|
+
(t, d) => {
|
|
95
|
+
newCalls++;
|
|
96
|
+
f.append(t, d);
|
|
97
|
+
},
|
|
98
|
+
{ reason: "reload" },
|
|
99
|
+
);
|
|
100
|
+
assert.equal(current.outcome.status, "indeterminate");
|
|
101
|
+
assert.equal(newCalls, 0);
|
|
102
|
+
flush(f.manager);
|
|
103
|
+
const restored = reloadedModule.tree(f.manager);
|
|
104
|
+
assert.equal(restored.status, "persisted");
|
|
105
|
+
assert.equal(
|
|
106
|
+
restored.state.status,
|
|
107
|
+
operation === "bind" ? "bound" : "cleared",
|
|
108
|
+
);
|
|
109
|
+
if (operation === "bind") name(restored, "pending");
|
|
110
|
+
assert.equal(current.attachment.bind("current", {}).status, "persisted");
|
|
111
|
+
assert.equal(newCalls, 1);
|
|
112
|
+
assert.equal(oldCalls, 1);
|
|
113
|
+
// A handler owned by the invalidated adapter cannot detach the new epoch.
|
|
114
|
+
f.integration.shutdown(f.manager, { reason: "reload" });
|
|
115
|
+
name(current.attachment.refresh(), "current");
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
for (const reader of ["replacement", "default"] as const) {
|
|
119
|
+
test(`reload replaces optional readFile with ${reader} and never calls the retired reader`, () => {
|
|
120
|
+
const f = fixture();
|
|
121
|
+
flush(f.manager);
|
|
122
|
+
let oldReads = 0,
|
|
123
|
+
currentReads = 0,
|
|
124
|
+
oldAppends = 0,
|
|
125
|
+
currentAppends = 0;
|
|
126
|
+
const previous = createSessionProfileIntegration({
|
|
127
|
+
readFile: (file) => {
|
|
128
|
+
oldReads++;
|
|
129
|
+
return readFileSync(file, "utf8");
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
const initial = previous.start(
|
|
133
|
+
f.manager,
|
|
134
|
+
(type, data) => {
|
|
135
|
+
oldAppends++;
|
|
136
|
+
f.append(type, data);
|
|
137
|
+
},
|
|
138
|
+
{ reason: "startup" },
|
|
139
|
+
);
|
|
140
|
+
assert.equal(initial.attachment.bind("trusted", {}).status, "persisted");
|
|
141
|
+
assert.ok(oldReads > 0, "initial factory options still read disk");
|
|
142
|
+
previous.shutdown(f.manager, { reason: "reload" });
|
|
143
|
+
const frozenReads = oldReads;
|
|
144
|
+
const frozenEntries = f.manager.getEntries().length;
|
|
145
|
+
assert.equal(initial.attachment.refresh().status, "indeterminate");
|
|
146
|
+
assert.equal(initial.attachment.clear().status, "indeterminate");
|
|
147
|
+
assert.equal(f.manager.getEntries().length, frozenEntries);
|
|
148
|
+
assert.equal(oldReads, frozenReads);
|
|
149
|
+
const current = createSessionProfileIntegration(
|
|
150
|
+
reader === "replacement"
|
|
151
|
+
? {
|
|
152
|
+
readFile: (file) => {
|
|
153
|
+
currentReads++;
|
|
154
|
+
return readFileSync(file, "utf8");
|
|
155
|
+
},
|
|
156
|
+
}
|
|
157
|
+
: {},
|
|
158
|
+
);
|
|
159
|
+
const started = current.start(
|
|
160
|
+
f.manager,
|
|
161
|
+
(type, data) => {
|
|
162
|
+
currentAppends++;
|
|
163
|
+
f.append(type, data);
|
|
164
|
+
},
|
|
165
|
+
{ reason: "reload" },
|
|
166
|
+
);
|
|
167
|
+
assert.equal(started.outcome.status, "persisted");
|
|
168
|
+
name(started.outcome, "trusted");
|
|
169
|
+
assert.equal(
|
|
170
|
+
oldReads,
|
|
171
|
+
frozenReads,
|
|
172
|
+
"reload must not invoke the retired readFile",
|
|
173
|
+
);
|
|
174
|
+
assert.equal(currentReads > 0, reader === "replacement");
|
|
175
|
+
assert.equal(currentAppends, 0);
|
|
176
|
+
assert.equal(started.attachment.bind("current", {}).status, "persisted");
|
|
177
|
+
assert.equal(started.attachment.clear().status, "persisted");
|
|
178
|
+
assert.equal(f.manager.getEntries().length, frozenEntries + 2);
|
|
179
|
+
assert.equal(currentAppends, 2);
|
|
180
|
+
assert.equal(oldAppends, 1);
|
|
181
|
+
const frozenCurrentReads = currentReads;
|
|
182
|
+
previous.shutdown(f.manager, { reason: "reload" });
|
|
183
|
+
assert.equal(previous.tree(f.manager).status, "indeterminate");
|
|
184
|
+
assert.equal(initial.attachment.bind("stale", {}).status, "indeterminate");
|
|
185
|
+
assert.equal(currentReads, frozenCurrentReads);
|
|
186
|
+
assert.equal(f.manager.getEntries().length, frozenEntries + 2);
|
|
187
|
+
assert.equal(started.attachment.refresh().state.status, "cleared");
|
|
188
|
+
assert.equal(oldReads, frozenReads);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function failedThenFlushed() {
|
|
192
|
+
const f = fixture();
|
|
193
|
+
const initial = f.integration.start(
|
|
194
|
+
f.manager,
|
|
195
|
+
(t, d) => {
|
|
196
|
+
f.append(t, d);
|
|
197
|
+
throw new Error("injected callback failure after public custom append");
|
|
198
|
+
},
|
|
199
|
+
{ reason: "startup" },
|
|
200
|
+
);
|
|
201
|
+
assert.equal(initial.attachment.bind("failed", {}).status, "indeterminate");
|
|
202
|
+
const failed = f.manager.getLeafId();
|
|
203
|
+
assert.ok(failed);
|
|
204
|
+
assert.equal(existsSync(f.manager.getSessionFile()!), false);
|
|
205
|
+
// Genuine public first flush now writes the known-failed record as well.
|
|
206
|
+
flush(f.manager);
|
|
207
|
+
return { ...f, initial, failed };
|
|
208
|
+
}
|
|
209
|
+
test("reload retains failed quarantine after genuine first flush", () => {
|
|
210
|
+
const f = failedThenFlushed();
|
|
211
|
+
f.integration.shutdown(f.manager, { reason: "reload" });
|
|
212
|
+
const current = createSessionProfileIntegration().start(f.manager, f.append, {
|
|
213
|
+
reason: "reload",
|
|
214
|
+
});
|
|
215
|
+
assert.equal(current.outcome.state.status, "absent");
|
|
216
|
+
assert.equal(current.attachment.clear().status, "persisted");
|
|
217
|
+
f.manager.branch(f.failed);
|
|
218
|
+
assert.equal(current.attachment.refresh().state.status, "absent");
|
|
219
|
+
});
|
|
220
|
+
test("confirmed persisted disk-first fork retains copied failure but does not inherit binding", () => {
|
|
221
|
+
const f = failedThenFlushed();
|
|
222
|
+
const previousSessionFile = f.manager.getSessionFile();
|
|
223
|
+
assert.equal(
|
|
224
|
+
f.integration.beforeFork(f.manager, { entryId: f.failed, position: "at" })
|
|
225
|
+
.status,
|
|
226
|
+
"captured",
|
|
227
|
+
);
|
|
228
|
+
const copy = persistedCopy(f.manager, f.failed);
|
|
229
|
+
assert.notEqual(copy, f.manager);
|
|
230
|
+
f.integration.shutdown(f.manager, {
|
|
231
|
+
reason: "fork",
|
|
232
|
+
targetSessionFile: copy.getSessionFile(),
|
|
233
|
+
});
|
|
234
|
+
const current = createSessionProfileIntegration().start(
|
|
235
|
+
copy,
|
|
236
|
+
(t, d) => {
|
|
237
|
+
copy.appendCustomEntry(t, d);
|
|
238
|
+
},
|
|
239
|
+
{ reason: "fork", previousSessionFile },
|
|
240
|
+
);
|
|
241
|
+
// This copied path has no conversation: even a saved parent does not prove
|
|
242
|
+
// that the new child file exists. Quarantine must survive its later flush.
|
|
243
|
+
assert.equal(current.outcome.status, "indeterminate");
|
|
244
|
+
assert.equal(existsSync(copy.getSessionFile()!), false);
|
|
245
|
+
flush(copy);
|
|
246
|
+
assert.equal(current.attachment.refresh().state.status, "absent");
|
|
247
|
+
assert.equal(current.attachment.clear().status, "persisted");
|
|
248
|
+
const replay = createSessionProfileIntegration().start(
|
|
249
|
+
copy,
|
|
250
|
+
(t, d) => {
|
|
251
|
+
copy.appendCustomEntry(t, d);
|
|
252
|
+
},
|
|
253
|
+
{ reason: "fork", previousSessionFile },
|
|
254
|
+
);
|
|
255
|
+
assert.equal(replay.outcome.status, "indeterminate");
|
|
256
|
+
});
|
|
257
|
+
test("S5 rejects actual copied cut beyond prepared ancestor", () => {
|
|
258
|
+
const f = fixture();
|
|
259
|
+
flush(f.manager);
|
|
260
|
+
let fail = false,
|
|
261
|
+
sourceAppends = 0,
|
|
262
|
+
childAppends = 0;
|
|
263
|
+
const live = f.integration.start(
|
|
264
|
+
f.manager,
|
|
265
|
+
(type, data) => {
|
|
266
|
+
sourceAppends++;
|
|
267
|
+
f.append(type, data);
|
|
268
|
+
if (fail)
|
|
269
|
+
throw new Error("injected callback failure after public disk append");
|
|
270
|
+
},
|
|
271
|
+
{ reason: "startup" },
|
|
272
|
+
);
|
|
273
|
+
assert.equal(live.attachment.bind("trusted", {}).status, "persisted");
|
|
274
|
+
const preparedCut = f.manager.getLeafId();
|
|
275
|
+
assert.ok(preparedCut);
|
|
276
|
+
fail = true;
|
|
277
|
+
const failed = live.attachment.bind("known-failed", {});
|
|
278
|
+
assert.equal(failed.status, "append-failed-preserved");
|
|
279
|
+
name(failed, "trusted");
|
|
280
|
+
name(live.attachment.refresh(), "trusted");
|
|
281
|
+
const actualCut = f.manager.getLeafId();
|
|
282
|
+
assert.ok(actualCut);
|
|
283
|
+
assert.notEqual(actualCut, preparedCut);
|
|
284
|
+
const sourceFile = f.manager.getSessionFile();
|
|
285
|
+
assert.ok(sourceFile);
|
|
286
|
+
const sourceEntries = JSON.stringify(f.manager.getEntries());
|
|
287
|
+
const sourceDisk = readFileSync(sourceFile, "utf8");
|
|
288
|
+
assert.equal(
|
|
289
|
+
f.integration.beforeFork(f.manager, { entryId: preparedCut, position: "at" })
|
|
290
|
+
.status,
|
|
291
|
+
"captured",
|
|
292
|
+
);
|
|
293
|
+
const child = persistedCopy(f.manager, actualCut);
|
|
294
|
+
assert.equal(child.getLeafId(), actualCut);
|
|
295
|
+
assert.ok(child.getBranch().some((entry) => entry.id === preparedCut));
|
|
296
|
+
assert.ok(child.getBranch().some((entry) => entry.id === actualCut));
|
|
297
|
+
const childFile = child.getSessionFile();
|
|
298
|
+
assert.ok(childFile);
|
|
299
|
+
const childEntries = JSON.stringify(child.getEntries());
|
|
300
|
+
const childDisk = readFileSync(childFile, "utf8");
|
|
301
|
+
assert.ok(
|
|
302
|
+
childDisk.includes(actualCut),
|
|
303
|
+
"actual public copy stores the failed record",
|
|
304
|
+
);
|
|
305
|
+
f.integration.shutdown(f.manager, {
|
|
306
|
+
reason: "fork",
|
|
307
|
+
targetSessionFile: childFile,
|
|
308
|
+
});
|
|
309
|
+
assert.equal(live.attachment.clear().status, "indeterminate");
|
|
310
|
+
const started = createSessionProfileIntegration().start(
|
|
311
|
+
child,
|
|
312
|
+
(type, data) => {
|
|
313
|
+
childAppends++;
|
|
314
|
+
child.appendCustomEntry(type, data);
|
|
315
|
+
},
|
|
316
|
+
{ reason: "fork", previousSessionFile: sourceFile },
|
|
317
|
+
);
|
|
318
|
+
assert.deepEqual(started.outcome, {
|
|
319
|
+
status: "indeterminate",
|
|
320
|
+
state: { status: "indeterminate", reason: "ambiguous-append" },
|
|
321
|
+
});
|
|
322
|
+
assert.deepEqual(started.attachment.refresh(), started.outcome);
|
|
323
|
+
assert.equal(childAppends, 0);
|
|
324
|
+
assert.equal(sourceAppends, 2);
|
|
325
|
+
assert.equal(JSON.stringify(child.getEntries()), childEntries);
|
|
326
|
+
assert.equal(readFileSync(childFile, "utf8"), childDisk);
|
|
327
|
+
assert.equal(JSON.stringify(f.manager.getEntries()), sourceEntries);
|
|
328
|
+
assert.equal(readFileSync(sourceFile, "utf8"), sourceDisk);
|
|
329
|
+
});
|
|
330
|
+
for (const position of ["before", "at"] as const) {
|
|
331
|
+
test(`cancelled ${position} fork leaves explicit activation and callbacks unchanged`, () => {
|
|
332
|
+
const f = fixture();
|
|
333
|
+
let calls = 0;
|
|
334
|
+
const current = f.integration.start(
|
|
335
|
+
f.manager,
|
|
336
|
+
(t, d) => {
|
|
337
|
+
calls++;
|
|
338
|
+
f.append(t, d);
|
|
339
|
+
},
|
|
340
|
+
{ reason: "startup" },
|
|
341
|
+
);
|
|
342
|
+
assert.equal(current.attachment.bind("pending", {}).status, "not-persisted");
|
|
343
|
+
const leaf = f.manager.getLeafId();
|
|
344
|
+
assert.ok(leaf);
|
|
345
|
+
const target = position === "at" ? leaf : "missing";
|
|
346
|
+
const before = JSON.stringify(f.manager.getEntries());
|
|
347
|
+
const proposed = f.integration.beforeFork(f.manager, {
|
|
348
|
+
entryId: target,
|
|
349
|
+
position,
|
|
350
|
+
});
|
|
351
|
+
assert.equal(
|
|
352
|
+
proposed.status,
|
|
353
|
+
position === "at" ? "captured" : "indeterminate",
|
|
354
|
+
);
|
|
355
|
+
assert.equal(JSON.stringify(f.manager.getEntries()), before);
|
|
356
|
+
assert.equal(current.attachment.refresh().status, "not-persisted");
|
|
357
|
+
assert.equal(current.attachment.bind("next", {}).status, "not-persisted");
|
|
358
|
+
assert.equal(calls, 2);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
test("label removal permits only the expected copied parentId rechain", () => {
|
|
362
|
+
const f = fixture();
|
|
363
|
+
const current = f.integration.start(f.manager, f.append, {
|
|
364
|
+
reason: "startup",
|
|
365
|
+
});
|
|
366
|
+
current.attachment.bind("base", {});
|
|
367
|
+
flush(f.manager);
|
|
368
|
+
const base = f.manager.getLeafId();
|
|
369
|
+
assert.ok(base);
|
|
370
|
+
f.manager.appendLabelChange(base, "fixture-label");
|
|
371
|
+
current.attachment.bind("selected", {});
|
|
372
|
+
const leaf = f.manager.getLeafId();
|
|
373
|
+
assert.ok(leaf);
|
|
374
|
+
const original = f.manager.getEntry(leaf)!;
|
|
375
|
+
assert.equal(
|
|
376
|
+
f.integration.beforeFork(f.manager, { entryId: leaf, position: "at" }).status,
|
|
377
|
+
"captured",
|
|
378
|
+
);
|
|
379
|
+
const copy = persistedCopy(f.manager, leaf);
|
|
380
|
+
assert.notEqual(copy.getEntry(leaf)?.parentId, original.parentId);
|
|
381
|
+
f.integration.shutdown(f.manager, {
|
|
382
|
+
reason: "fork",
|
|
383
|
+
targetSessionFile: copy.getSessionFile(),
|
|
384
|
+
});
|
|
385
|
+
const restored = createSessionProfileIntegration().start(
|
|
386
|
+
copy,
|
|
387
|
+
(t, d) => {
|
|
388
|
+
copy.appendCustomEntry(t, d);
|
|
389
|
+
},
|
|
390
|
+
{ reason: "fork", previousSessionFile: f.manager.getSessionFile() },
|
|
391
|
+
);
|
|
392
|
+
name(restored.outcome, "selected");
|
|
393
|
+
});
|
|
394
|
+
test("actual fork path excludes failed sibling and never imports its quarantine", () => {
|
|
395
|
+
const f = fixture();
|
|
396
|
+
let fail = false;
|
|
397
|
+
const live = f.integration.start(
|
|
398
|
+
f.manager,
|
|
399
|
+
(t, d) => {
|
|
400
|
+
f.append(t, d);
|
|
401
|
+
if (fail) throw new Error("injected failure");
|
|
402
|
+
},
|
|
403
|
+
{ reason: "startup" },
|
|
404
|
+
);
|
|
405
|
+
live.attachment.bind("base", {});
|
|
406
|
+
flush(f.manager);
|
|
407
|
+
const base = f.manager.getLeafId();
|
|
408
|
+
assert.ok(base);
|
|
409
|
+
fail = true;
|
|
410
|
+
live.attachment.bind("failed-left", {});
|
|
411
|
+
const failed = f.manager.getLeafId();
|
|
412
|
+
fail = false;
|
|
413
|
+
f.manager.branch(base);
|
|
414
|
+
live.attachment.bind("right", {});
|
|
415
|
+
const right = f.manager.getLeafId();
|
|
416
|
+
assert.ok(right);
|
|
417
|
+
assert.notEqual(right, failed);
|
|
418
|
+
assert.equal(
|
|
419
|
+
f.integration.beforeFork(f.manager, { entryId: right, position: "at" })
|
|
420
|
+
.status,
|
|
421
|
+
"captured",
|
|
422
|
+
);
|
|
423
|
+
const copy = persistedCopy(f.manager, right);
|
|
424
|
+
assert.equal(
|
|
425
|
+
copy.getBranch().some((e) => e.id === failed),
|
|
426
|
+
false,
|
|
427
|
+
);
|
|
428
|
+
f.integration.shutdown(f.manager, {
|
|
429
|
+
reason: "fork",
|
|
430
|
+
targetSessionFile: copy.getSessionFile(),
|
|
431
|
+
});
|
|
432
|
+
name(
|
|
433
|
+
createSessionProfileIntegration().start(
|
|
434
|
+
copy,
|
|
435
|
+
(t, d) => {
|
|
436
|
+
copy.appendCustomEntry(t, d);
|
|
437
|
+
},
|
|
438
|
+
{ reason: "fork", previousSessionFile: f.manager.getSessionFile() },
|
|
439
|
+
).outcome,
|
|
440
|
+
"right",
|
|
441
|
+
);
|
|
442
|
+
});
|
|
443
|
+
for (const changed of ["data", "timestamp", "parentId", "extra"] as const) {
|
|
444
|
+
test(`copied same-ID ${changed} mutation fails closed without blessing destination disk`, () => {
|
|
445
|
+
const f = fixture();
|
|
446
|
+
const live = f.integration.start(f.manager, f.append, { reason: "startup" });
|
|
447
|
+
flush(f.manager);
|
|
448
|
+
live.attachment.bind("trusted", {});
|
|
449
|
+
const leaf = f.manager.getLeafId();
|
|
450
|
+
assert.ok(leaf);
|
|
451
|
+
assert.equal(
|
|
452
|
+
f.integration.beforeFork(f.manager, { entryId: leaf, position: "at" })
|
|
453
|
+
.status,
|
|
454
|
+
"captured",
|
|
455
|
+
);
|
|
456
|
+
const copy = persistedCopy(f.manager, leaf);
|
|
457
|
+
const row = copy.getEntry(leaf)! as unknown as Record<string, any>;
|
|
458
|
+
if (changed === "data") row.data.name = "FAILED";
|
|
459
|
+
if (changed === "timestamp") row.timestamp = "changed";
|
|
460
|
+
if (changed === "parentId") row.parentId = "unexpected-parent";
|
|
461
|
+
if (changed === "extra") row.extra = { changed: true };
|
|
462
|
+
f.integration.shutdown(f.manager, {
|
|
463
|
+
reason: "fork",
|
|
464
|
+
targetSessionFile: copy.getSessionFile(),
|
|
465
|
+
});
|
|
466
|
+
const current = createSessionProfileIntegration({
|
|
467
|
+
// Inject matching disk content as in F2, not ordinary Pi ID reuse.
|
|
468
|
+
readFile: (path) =>
|
|
469
|
+
path === copy.getSessionFile()
|
|
470
|
+
? [copy.getHeader(), ...copy.getEntries()]
|
|
471
|
+
.map((e) => JSON.stringify(e))
|
|
472
|
+
.join("\n")
|
|
473
|
+
: readFileSync(path, "utf8"),
|
|
474
|
+
}).start(
|
|
475
|
+
copy,
|
|
476
|
+
(t, d) => {
|
|
477
|
+
copy.appendCustomEntry(t, d);
|
|
478
|
+
},
|
|
479
|
+
{ reason: "fork", previousSessionFile: f.manager.getSessionFile() },
|
|
480
|
+
);
|
|
481
|
+
assert.equal(current.outcome.status, "indeterminate");
|
|
482
|
+
assert.equal(current.attachment.clear().status, "persisted");
|
|
483
|
+
copy.branch(leaf);
|
|
484
|
+
assert.equal(current.attachment.refresh().status, "indeterminate");
|
|
485
|
+
// Structural source + injected matching disk, never a production rewrite.
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
test("in-memory confirmed fork keeps same-manager evidence but never restores pending state", () => {
|
|
489
|
+
const f = fixture(true);
|
|
490
|
+
const live = f.integration.start(f.manager, f.append, { reason: "startup" });
|
|
491
|
+
live.attachment.bind("pending", {});
|
|
492
|
+
const leaf = f.manager.getLeafId();
|
|
493
|
+
assert.ok(leaf);
|
|
494
|
+
assert.equal(
|
|
495
|
+
f.integration.beforeFork(f.manager, { entryId: leaf, position: "at" }).status,
|
|
496
|
+
"captured",
|
|
497
|
+
);
|
|
498
|
+
f.integration.shutdown(f.manager, { reason: "fork" });
|
|
499
|
+
f.manager.createBranchedSession(leaf);
|
|
500
|
+
const current = createSessionProfileIntegration().start(f.manager, f.append, {
|
|
501
|
+
reason: "fork",
|
|
502
|
+
});
|
|
503
|
+
assert.equal(current.outcome.status, "indeterminate");
|
|
504
|
+
assert.equal(
|
|
505
|
+
current.attachment.bind("new-explicit", {}).status,
|
|
506
|
+
"not-persisted",
|
|
507
|
+
);
|
|
508
|
+
assert.equal(live.attachment.clear().status, "indeterminate");
|
|
509
|
+
});
|
|
510
|
+
test("fork before root creates empty child without parent binding or established-history transfer", () => {
|
|
511
|
+
const f = fixture();
|
|
512
|
+
flush(f.manager);
|
|
513
|
+
const firstUser = f.manager.getLeafId();
|
|
514
|
+
assert.ok(firstUser);
|
|
515
|
+
const live = f.integration.start(f.manager, f.append, { reason: "startup" });
|
|
516
|
+
live.attachment.bind("parent", {});
|
|
517
|
+
assert.equal(
|
|
518
|
+
f.integration.beforeFork(f.manager, {
|
|
519
|
+
entryId: firstUser,
|
|
520
|
+
position: "before",
|
|
521
|
+
}).status,
|
|
522
|
+
"captured",
|
|
523
|
+
);
|
|
524
|
+
const previousSessionFile = f.manager.getSessionFile();
|
|
525
|
+
const child = SessionManager.create(
|
|
526
|
+
f.manager.getCwd(),
|
|
527
|
+
f.manager.getSessionDir(),
|
|
528
|
+
);
|
|
529
|
+
child.newSession({ parentSession: previousSessionFile });
|
|
530
|
+
f.integration.shutdown(f.manager, {
|
|
531
|
+
reason: "fork",
|
|
532
|
+
targetSessionFile: child.getSessionFile(),
|
|
533
|
+
});
|
|
534
|
+
const current = createSessionProfileIntegration().start(
|
|
535
|
+
child,
|
|
536
|
+
(t, d) => {
|
|
537
|
+
child.appendCustomEntry(t, d);
|
|
538
|
+
},
|
|
539
|
+
{ reason: "fork", previousSessionFile },
|
|
540
|
+
);
|
|
541
|
+
assert.equal(current.outcome.status, "indeterminate");
|
|
542
|
+
assert.equal(current.attachment.bind("child", {}).status, "not-persisted");
|
|
543
|
+
});
|
|
544
|
+
test("distinct manager cannot inherit another manager's pending activation", () => {
|
|
545
|
+
const f = fixture();
|
|
546
|
+
f.integration
|
|
547
|
+
.start(f.manager, f.append, { reason: "startup" })
|
|
548
|
+
.attachment.bind("pending", {});
|
|
549
|
+
const other = fixture();
|
|
550
|
+
assert.equal(
|
|
551
|
+
createSessionProfileIntegration().start(other.manager, other.append, {
|
|
552
|
+
reason: "startup",
|
|
553
|
+
}).outcome.status,
|
|
554
|
+
"indeterminate",
|
|
555
|
+
);
|
|
556
|
+
});
|
|
557
|
+
test("valid cancelled before-user fork leaves disk, public entries and explicit binding unchanged", () => {
|
|
558
|
+
const f = fixture();
|
|
559
|
+
flush(f.manager);
|
|
560
|
+
const user = f.manager.getLeafId();
|
|
561
|
+
assert.ok(user);
|
|
562
|
+
let calls = 0;
|
|
563
|
+
const live = f.integration.start(
|
|
564
|
+
f.manager,
|
|
565
|
+
(t, d) => {
|
|
566
|
+
calls++;
|
|
567
|
+
f.append(t, d);
|
|
568
|
+
},
|
|
569
|
+
{ reason: "startup" },
|
|
570
|
+
);
|
|
571
|
+
live.attachment.bind("current", {});
|
|
572
|
+
const entries = JSON.stringify(f.manager.getEntries());
|
|
573
|
+
const disk = readFileSync(f.manager.getSessionFile()!, "utf8");
|
|
574
|
+
assert.equal(
|
|
575
|
+
f.integration.beforeFork(f.manager, { entryId: user, position: "before" })
|
|
576
|
+
.status,
|
|
577
|
+
"captured",
|
|
578
|
+
);
|
|
579
|
+
assert.equal(JSON.stringify(f.manager.getEntries()), entries);
|
|
580
|
+
assert.equal(readFileSync(f.manager.getSessionFile()!, "utf8"), disk);
|
|
581
|
+
name(live.attachment.refresh(), "current");
|
|
582
|
+
assert.equal(calls, 1);
|
|
583
|
+
});
|
|
584
|
+
test("public runtime rejects unsaved persisted fork before teardown and preserves explicit selection", async () => {
|
|
585
|
+
const f = fixture();
|
|
586
|
+
const live = f.integration.start(f.manager, f.append, { reason: "startup" });
|
|
587
|
+
live.attachment.bind("pending", {});
|
|
588
|
+
const leaf = f.manager.getLeafId();
|
|
589
|
+
assert.ok(leaf);
|
|
590
|
+
const before = JSON.stringify(f.manager.getEntries());
|
|
591
|
+
let teardown = 0,
|
|
592
|
+
creations = 0;
|
|
593
|
+
const host = {
|
|
594
|
+
sessionManager: f.manager,
|
|
595
|
+
sessionFile: f.manager.getSessionFile(),
|
|
596
|
+
extensionRunner: {
|
|
597
|
+
hasHandlers: () => true,
|
|
598
|
+
emit: async (event: { entryId: string; position: "before" | "at" }) => {
|
|
599
|
+
assert.equal(f.integration.beforeFork(f.manager, event).status, "captured");
|
|
600
|
+
},
|
|
601
|
+
},
|
|
602
|
+
abort: async () => {
|
|
603
|
+
teardown++;
|
|
604
|
+
},
|
|
605
|
+
dispose: () => {
|
|
606
|
+
teardown++;
|
|
607
|
+
},
|
|
608
|
+
};
|
|
609
|
+
const runtime = new AgentSessionRuntime(
|
|
610
|
+
host as unknown as ConstructorParameters<typeof AgentSessionRuntime>[0],
|
|
611
|
+
{ cwd: f.manager.getCwd() } as ConstructorParameters<
|
|
612
|
+
typeof AgentSessionRuntime
|
|
613
|
+
>[1],
|
|
614
|
+
async () => {
|
|
615
|
+
creations++;
|
|
616
|
+
throw new Error("fixture must never create runtime");
|
|
617
|
+
},
|
|
618
|
+
);
|
|
619
|
+
await assert.rejects(runtime.fork(leaf, { position: "at" }), {
|
|
620
|
+
message:
|
|
621
|
+
"This session has not been saved yet. Send a message before cloning or forking it.",
|
|
622
|
+
});
|
|
623
|
+
assert.equal(teardown, 0);
|
|
624
|
+
assert.equal(creations, 0);
|
|
625
|
+
assert.equal(JSON.stringify(f.manager.getEntries()), before);
|
|
626
|
+
assert.equal(existsSync(f.manager.getSessionFile()!), false);
|
|
627
|
+
assert.equal(live.attachment.refresh().status, "not-persisted");
|
|
628
|
+
});
|
|
629
|
+
for (const incompatible of [false, true]) {
|
|
630
|
+
test(`isolated process ${incompatible ? "rejects incompatible owner protocol" : "cannot inherit the live registry"}`, () => {
|
|
631
|
+
const root = mkdtempSync(join(tmpdir(), "profile-process-"));
|
|
632
|
+
const moduleUrl = new URL(
|
|
633
|
+
"../lib/session-profile-integration.ts",
|
|
634
|
+
import.meta.url,
|
|
635
|
+
).href;
|
|
636
|
+
const script = `
|
|
637
|
+
import assert from "node:assert/strict";
|
|
638
|
+
import { SessionManager } from "@earendil-works/pi-coding-agent";
|
|
639
|
+
import { createSessionProfileIntegration } from ${JSON.stringify(moduleUrl)};
|
|
640
|
+
${incompatible ? 'globalThis[Symbol.for("gentle-pi.session-profile.lifecycle")] = { version: 2 };' : ""}
|
|
641
|
+
const manager = SessionManager.inMemory(${JSON.stringify(root)});
|
|
642
|
+
manager.appendCustomEntry("gentle-pi.session-profile/v1", { kind: "bind", origin: "user", name: "unwritten", modelProfiles: {} });
|
|
643
|
+
const before = JSON.stringify(manager.getEntries()); let calls = 0;
|
|
644
|
+
const live = createSessionProfileIntegration().start(manager, () => { calls++; }, { reason: "startup" });
|
|
645
|
+
assert.equal(live.outcome.status, "indeterminate");
|
|
646
|
+
${incompatible ? 'assert.equal(live.attachment.bind("rejected", {}).status, "indeterminate");' : ""}
|
|
647
|
+
${incompatible ? 'assert.deepEqual(live.attachment.bindStartup("repo", "rejected", {}), { status: "indeterminate", state: { status: "indeterminate", reason: "unavailable-lifecycle" } });' : ""}
|
|
648
|
+
assert.equal(JSON.stringify(manager.getEntries()), before);
|
|
649
|
+
process.stdout.write(JSON.stringify({ status: live.outcome.status, calls, unchanged: true }));
|
|
650
|
+
`;
|
|
651
|
+
const result = spawnSync(
|
|
652
|
+
process.execPath,
|
|
653
|
+
["--experimental-strip-types", "--input-type=module", "-e", script],
|
|
654
|
+
{ encoding: "utf8", cwd: process.cwd(), env: process.env },
|
|
655
|
+
);
|
|
656
|
+
assert.equal(result.status, 0);
|
|
657
|
+
assert.equal(result.stderr, "");
|
|
658
|
+
assert.equal(
|
|
659
|
+
result.stdout,
|
|
660
|
+
'{"status":"indeterminate","calls":0,"unchanged":true}',
|
|
661
|
+
);
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
for (const position of ["before", "at"] as const) {
|
|
666
|
+
test(`fork boundary label supports ${position} without requiring its removed ID`, () => {
|
|
667
|
+
const f = fixture();
|
|
668
|
+
flush(f.manager);
|
|
669
|
+
const live = f.integration.start(f.manager, f.append, { reason: "startup" });
|
|
670
|
+
live.attachment.bind("retained", {});
|
|
671
|
+
const retained = f.manager.getLeafId();
|
|
672
|
+
assert.ok(retained);
|
|
673
|
+
f.manager.appendLabelChange(retained, "boundary");
|
|
674
|
+
const label = f.manager.getLeafId();
|
|
675
|
+
assert.ok(label);
|
|
676
|
+
let entryId = label;
|
|
677
|
+
if (position === "before") {
|
|
678
|
+
flush(f.manager);
|
|
679
|
+
entryId = f.manager.getLeafId()!;
|
|
680
|
+
}
|
|
681
|
+
assert.equal(
|
|
682
|
+
f.integration.beforeFork(f.manager, { entryId, position }).status,
|
|
683
|
+
"captured",
|
|
684
|
+
);
|
|
685
|
+
const copy = persistedCopy(f.manager, label);
|
|
686
|
+
assert.equal(copy.getEntry(label), undefined);
|
|
687
|
+
f.integration.shutdown(f.manager, {
|
|
688
|
+
reason: "fork",
|
|
689
|
+
targetSessionFile: copy.getSessionFile(),
|
|
690
|
+
});
|
|
691
|
+
const current = createSessionProfileIntegration().start(
|
|
692
|
+
copy,
|
|
693
|
+
(t, d) => {
|
|
694
|
+
copy.appendCustomEntry(t, d);
|
|
695
|
+
},
|
|
696
|
+
{ reason: "fork", previousSessionFile: f.manager.getSessionFile() },
|
|
697
|
+
);
|
|
698
|
+
name(current.outcome, "retained");
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
// Selection serialization helpers: FIFO queue, revocation and settle semantics.
|
|
703
|
+
function started() {
|
|
704
|
+
const f = fixture();
|
|
705
|
+
flush(f.manager);
|
|
706
|
+
f.integration.start(f.manager, f.append, { reason: "startup" });
|
|
707
|
+
return f;
|
|
708
|
+
}
|
|
709
|
+
function gate() {
|
|
710
|
+
let open!: () => void;
|
|
711
|
+
const opened = new Promise<void>((resolve) => {
|
|
712
|
+
open = resolve;
|
|
713
|
+
});
|
|
714
|
+
return { open, opened };
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
test("queued selections run in FIFO order and wait settles only after the last one", async () => {
|
|
718
|
+
const f = started();
|
|
719
|
+
const order: string[] = [];
|
|
720
|
+
const first = gate();
|
|
721
|
+
const a = runCurrentSessionProfileSelection(f.manager, async () => {
|
|
722
|
+
await first.opened;
|
|
723
|
+
order.push("a");
|
|
724
|
+
return "a";
|
|
725
|
+
});
|
|
726
|
+
const b = runCurrentSessionProfileSelection(f.manager, async () => {
|
|
727
|
+
order.push("b");
|
|
728
|
+
return "b";
|
|
729
|
+
});
|
|
730
|
+
let waited = false;
|
|
731
|
+
const wait = waitCurrentSessionProfileSelection(f.manager).then(() => {
|
|
732
|
+
waited = true;
|
|
733
|
+
});
|
|
734
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
735
|
+
assert.deepEqual(order, []);
|
|
736
|
+
assert.equal(waited, false, "wait does not settle while a selection is pending");
|
|
737
|
+
first.open();
|
|
738
|
+
assert.deepEqual(await Promise.all([a, b]), ["a", "b"]);
|
|
739
|
+
await wait;
|
|
740
|
+
assert.deepEqual(order, ["a", "b"]);
|
|
741
|
+
assert.equal(waited, true);
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
test("a rejected selection reaches its caller and does not block the next one", async () => {
|
|
745
|
+
const f = started();
|
|
746
|
+
const failed = runCurrentSessionProfileSelection(f.manager, async () => {
|
|
747
|
+
throw new Error("effect failed");
|
|
748
|
+
});
|
|
749
|
+
const next = runCurrentSessionProfileSelection(f.manager, async () => "next");
|
|
750
|
+
await assert.rejects(failed, /effect failed/);
|
|
751
|
+
assert.equal(await next, "next");
|
|
752
|
+
await waitCurrentSessionProfileSelection(f.manager);
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
test("shutdown while a selection is queued revokes it before its effect runs", async () => {
|
|
756
|
+
const f = started();
|
|
757
|
+
const first = gate();
|
|
758
|
+
let ran = false;
|
|
759
|
+
const holder = runCurrentSessionProfileSelection(f.manager, async (_attachment, current) => {
|
|
760
|
+
await first.opened;
|
|
761
|
+
return current();
|
|
762
|
+
});
|
|
763
|
+
const queued = runCurrentSessionProfileSelection(f.manager, async () => {
|
|
764
|
+
ran = true;
|
|
765
|
+
return "ran";
|
|
766
|
+
});
|
|
767
|
+
await new Promise((resolve) => setImmediate(resolve)); // the holder effect is now in flight
|
|
768
|
+
f.integration.shutdown(f.manager, { reason: "quit" });
|
|
769
|
+
first.open();
|
|
770
|
+
assert.equal(await holder, false, "current() turns false after shutdown");
|
|
771
|
+
assert.equal(await queued, undefined, "the queued effect is skipped");
|
|
772
|
+
assert.equal(ran, false);
|
|
773
|
+
assert.equal(
|
|
774
|
+
(readCurrentSessionProfileOutcome(f.manager)?.state as { reason?: string }).reason,
|
|
775
|
+
"unavailable-lifecycle",
|
|
776
|
+
);
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
test("a reload start while a selection is queued revokes the old attachment", async () => {
|
|
780
|
+
const f = started();
|
|
781
|
+
const first = gate();
|
|
782
|
+
const holder = runCurrentSessionProfileSelection(f.manager, async (attachment, current) => {
|
|
783
|
+
await first.opened;
|
|
784
|
+
return { current: current(), outcome: attachment.bind("late", {}) };
|
|
785
|
+
});
|
|
786
|
+
await new Promise((resolve) => setImmediate(resolve)); // the holder effect is now in flight
|
|
787
|
+
f.integration.shutdown(f.manager, { reason: "reload" });
|
|
788
|
+
f.integration.start(f.manager, f.append, { reason: "reload" });
|
|
789
|
+
first.open();
|
|
790
|
+
const result = await holder;
|
|
791
|
+
assert.equal(result?.current, false);
|
|
792
|
+
assert.notEqual(result?.outcome.status, "persisted", "the old attachment cannot append");
|
|
793
|
+
assert.equal(
|
|
794
|
+
f.manager.getBranch().some((entry) => entry.type === "custom" && entry.customType?.startsWith("gentle-pi.session-profile/")),
|
|
795
|
+
false,
|
|
796
|
+
);
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
test("a session file change while a selection waits makes current() false", async () => {
|
|
800
|
+
const f = started();
|
|
801
|
+
const first = gate();
|
|
802
|
+
const holder = runCurrentSessionProfileSelection(f.manager, async (_attachment, current) => {
|
|
803
|
+
await first.opened;
|
|
804
|
+
return current();
|
|
805
|
+
});
|
|
806
|
+
await new Promise((resolve) => setImmediate(resolve)); // the holder effect is now in flight
|
|
807
|
+
const original = f.manager.getSessionFile.bind(f.manager);
|
|
808
|
+
f.manager.getSessionFile = () => `${original()}.moved`;
|
|
809
|
+
first.open();
|
|
810
|
+
assert.equal(await holder, false);
|
|
811
|
+
f.manager.getSessionFile = original;
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
test("helpers report no owner as undefined", async () => {
|
|
815
|
+
const f = fixture();
|
|
816
|
+
assert.equal(readCurrentSessionProfileOutcome(f.manager), undefined);
|
|
817
|
+
assert.equal(await runCurrentSessionProfileSelection(f.manager, async () => "never"), undefined);
|
|
818
|
+
await waitCurrentSessionProfileSelection(f.manager);
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
test("selection helpers treat an incompatible registry protocol as nothing pending or selectable", () => {
|
|
822
|
+
const moduleUrl = new URL(
|
|
823
|
+
"../lib/session-profile-integration.ts",
|
|
824
|
+
import.meta.url,
|
|
825
|
+
).href;
|
|
826
|
+
const script = `
|
|
827
|
+
import assert from "node:assert/strict";
|
|
828
|
+
import { SessionManager } from "@earendil-works/pi-coding-agent";
|
|
829
|
+
import {
|
|
830
|
+
hasPendingSessionProfileSelection,
|
|
831
|
+
runCurrentSessionProfileSelection,
|
|
832
|
+
waitCurrentSessionProfileSelection,
|
|
833
|
+
} from ${JSON.stringify(moduleUrl)};
|
|
834
|
+
globalThis[Symbol.for("gentle-pi.session-profile.lifecycle")] = { version: 2 };
|
|
835
|
+
const manager = SessionManager.inMemory(${JSON.stringify(tmpdir())});
|
|
836
|
+
assert.equal(hasPendingSessionProfileSelection(manager), false);
|
|
837
|
+
assert.equal(await waitCurrentSessionProfileSelection(manager), undefined);
|
|
838
|
+
let ran = false;
|
|
839
|
+
assert.equal(
|
|
840
|
+
await runCurrentSessionProfileSelection(manager, async () => {
|
|
841
|
+
ran = true;
|
|
842
|
+
return "never";
|
|
843
|
+
}),
|
|
844
|
+
undefined,
|
|
845
|
+
);
|
|
846
|
+
assert.equal(ran, false);
|
|
847
|
+
process.stdout.write("ok");
|
|
848
|
+
`;
|
|
849
|
+
const result = spawnSync(
|
|
850
|
+
process.execPath,
|
|
851
|
+
["--experimental-strip-types", "--input-type=module", "-e", script],
|
|
852
|
+
{ encoding: "utf8", cwd: process.cwd(), env: process.env },
|
|
853
|
+
);
|
|
854
|
+
assert.equal(result.stderr, "");
|
|
855
|
+
assert.equal(result.status, 0);
|
|
856
|
+
assert.equal(result.stdout, "ok");
|
|
857
|
+
});
|