@timurproko/a1 0.2.7-dev.754 → 0.2.7-dev.772
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 +30 -0
- package/bin/cli.js +4 -0
- package/bin/guardian.js +5 -0
- package/bin/tab-launch-context.d.ts +26 -0
- package/bin/tab-launch-context.js +94 -0
- package/bin/ui.js +24 -6
- package/dist/app/session-shell/helper-pool.d.ts +3 -5
- package/dist/app/session-shell/helper-pool.js +3 -5
- package/dist/app/session-shell/session-shell-root.d.ts +28 -1
- package/dist/app/session-shell/session-shell-root.js +4 -3
- package/dist/app/session-shell/session-shell.js +149 -31
- package/dist/app/session-shell/session-viewport-controller.d.ts +1 -1
- package/dist/app/session-shell/session-viewport-controller.js +3 -3
- package/dist/app/session-shell/shell-host.d.ts +2 -0
- package/dist/app/session-shell/shell-host.js +20 -1
- package/dist/cli/dispatch.d.ts +11 -1
- package/dist/cli/dispatch.js +79 -5
- package/dist/cli/tabs-request.d.ts +7 -0
- package/dist/cli/tabs-request.js +1 -0
- package/dist/cli/tabs.d.ts +14 -0
- package/dist/cli/tabs.js +140 -0
- package/dist/composition/owned-ui.d.ts +26 -0
- package/dist/composition/owned-ui.js +176 -6
- package/dist/contracts/owned-ui/session-backend.d.ts +1 -0
- package/dist/features/launch/index.d.ts +1 -1
- package/dist/features/launch/intent.d.ts +3 -1
- package/dist/features/launch/intent.js +7 -4
- package/dist/features/owned-ui/settings-app.js +8 -35
- package/dist/features/resident-tab/auto-name.d.ts +11 -0
- package/dist/features/resident-tab/auto-name.js +42 -0
- package/dist/features/resident-tab/index.d.ts +3 -0
- package/dist/features/resident-tab/index.js +3 -0
- package/dist/features/resident-tab/shortcuts.d.ts +10 -0
- package/dist/features/resident-tab/shortcuts.js +35 -0
- package/dist/features/resident-tab/status-deriver.d.ts +9 -0
- package/dist/features/resident-tab/status-deriver.js +15 -0
- package/dist/foundation/launch-context/index.d.ts +10 -0
- package/dist/foundation/launch-context/index.js +25 -0
- package/dist/foundation/launch-guardian/main.d.ts +5 -0
- package/dist/foundation/launch-guardian/main.js +2 -1
- package/dist/foundation/release/bootstrap.d.ts +6 -0
- package/dist/foundation/release/bootstrap.js +51 -10
- package/dist/foundation/release/cohort-state.d.ts +1 -1
- package/dist/foundation/release/cohort-state.js +1 -1
- package/dist/foundation/release/dependency-certification.js +54 -19
- package/dist/foundation/release/progress-palette.generated.d.ts +2 -2
- package/dist/foundation/release/progress-palette.generated.js +2 -2
- package/dist/foundation/release/release-gc.d.ts +3 -0
- package/dist/foundation/release/release-gc.js +44 -8
- package/dist/foundation/resident-tabs/bounded-host-command.d.ts +16 -0
- package/dist/foundation/resident-tabs/bounded-host-command.js +35 -0
- package/dist/foundation/resident-tabs/bridge-client.d.ts +56 -0
- package/dist/foundation/resident-tabs/bridge-client.js +279 -0
- package/dist/foundation/resident-tabs/doctor.d.ts +29 -0
- package/dist/foundation/resident-tabs/doctor.js +166 -0
- package/dist/foundation/resident-tabs/draft-checkpoint.d.ts +27 -0
- package/dist/foundation/resident-tabs/draft-checkpoint.js +145 -0
- package/dist/foundation/resident-tabs/ensure-terminal-host.d.ts +31 -0
- package/dist/foundation/resident-tabs/ensure-terminal-host.js +147 -0
- package/dist/foundation/resident-tabs/fault-injection.d.ts +8 -0
- package/dist/foundation/resident-tabs/fault-injection.js +26 -0
- package/dist/foundation/resident-tabs/index.d.ts +12 -0
- package/dist/foundation/resident-tabs/index.js +6 -0
- package/dist/foundation/resident-tabs/maintenance.d.ts +42 -0
- package/dist/foundation/resident-tabs/maintenance.js +78 -0
- package/dist/foundation/resident-tabs/prompt-journal.d.ts +65 -0
- package/dist/foundation/resident-tabs/prompt-journal.js +214 -0
- package/dist/foundation/resident-tabs/protocol.d.ts +12 -0
- package/dist/foundation/resident-tabs/protocol.js +195 -0
- package/dist/foundation/resident-tabs/recovery.d.ts +26 -0
- package/dist/foundation/resident-tabs/recovery.js +205 -0
- package/dist/foundation/resident-tabs/release-holds.d.ts +27 -0
- package/dist/foundation/resident-tabs/release-holds.js +77 -0
- package/dist/foundation/resident-tabs/routing.d.ts +11 -0
- package/dist/foundation/resident-tabs/routing.js +10 -0
- package/dist/foundation/resident-tabs/session-sync.d.ts +14 -0
- package/dist/foundation/resident-tabs/session-sync.js +37 -0
- package/dist/foundation/resident-tabs/settings.d.ts +14 -0
- package/dist/foundation/resident-tabs/settings.js +66 -0
- package/dist/foundation/resident-tabs/terminal-host-artifact.d.ts +18 -0
- package/dist/foundation/resident-tabs/terminal-host-artifact.js +53 -0
- package/dist/foundation/resident-tabs/zip-archive.d.ts +9 -0
- package/dist/foundation/resident-tabs/zip-archive.js +56 -0
- package/dist/foundation/session-writer-lock/index.d.ts +32 -0
- package/dist/foundation/session-writer-lock/index.js +201 -0
- package/dist/foundation/startup/startup-descriptor.js +2 -2
- package/dist/foundation/terminal-cleanup/index.d.ts +1 -1
- package/dist/foundation/terminal-cleanup/index.js +1 -1
- package/dist/foundation/terminal-cleanup/terminal-reset.d.ts +1 -0
- package/dist/foundation/terminal-cleanup/terminal-reset.js +1 -0
- package/dist/integrations/pi/components/owned-editor-ux.d.ts +1 -1
- package/dist/integrations/pi/components/owned-editor-ux.js +2 -2
- package/dist/integrations/pi/components/resources/builtin-themes.d.ts +4 -4
- package/dist/integrations/pi/components/resources/builtin-themes.js +4 -4
- package/dist/integrations/pi/components/shell-hotkey-sections.js +1 -1
- package/dist/integrations/pi/components/shell-presenters-info.d.ts +4 -2
- package/dist/integrations/pi/components/shell-presenters-info.js +38 -4
- package/dist/integrations/pi/components/shell-shared-facade.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/adjacent/core/keybindings.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/adjacent/core/keybindings.js +17 -4
- package/dist/integrations/pi/components/upstream/assets/earendil-image.json +2 -2
- package/dist/integrations/pi/components/upstream/components/countdown-timer.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/components/earendil-announcement.js +1 -1
- package/dist/integrations/pi/components/upstream/components/extension-editor.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/components/extension-editor.js +1 -1
- package/dist/integrations/pi/components/upstream/components/extension-input.js +1 -1
- package/dist/integrations/pi/components/upstream/components/extension-selector.js +1 -1
- package/dist/integrations/pi/components/upstream/components/mermaid.js +1 -1
- package/dist/integrations/pi/components/upstream/components/owned-editor.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/components/owned-editor.js +1 -1
- package/dist/integrations/pi/components/upstream/components/pi-logo.js +1 -1
- package/dist/integrations/pi/components/upstream/components/scoped-models-selector.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/components/session-footer.js +1 -1
- package/dist/integrations/pi/components/upstream/components/session-selector-search.js +1 -1
- package/dist/integrations/pi/components/upstream/components/session-selector.js +1 -1
- package/dist/integrations/pi/components/upstream/components/skill-invocation-message.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/components/skill-invocation-message.js +1 -1
- package/dist/integrations/pi/components/upstream/components/status-indicator.d.ts +2 -2
- package/dist/integrations/pi/components/upstream/components/status-indicator.js +3 -3
- package/dist/integrations/pi/components/upstream/components/themed-text.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/components/themed-text.js +1 -1
- package/dist/integrations/pi/components/upstream/components/thinking-selector.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/components/thinking-selector.js +1 -1
- package/dist/integrations/pi/components/upstream/components/tool-execution.d.ts +0 -2
- package/dist/integrations/pi/components/upstream/components/tool-execution.js +41 -20
- package/dist/integrations/pi/components/upstream/components/tree-selector.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/components/tree-selector.js +1 -1
- package/dist/integrations/pi/components/upstream/components/trust-selector.js +1 -1
- package/dist/integrations/pi/components/upstream/external-editor.js +1 -1
- package/dist/integrations/pi/components/upstream/history/editor-core.d.ts +2 -2
- package/dist/integrations/pi/components/upstream/history/editor-core.js +5 -5
- package/dist/integrations/pi/components/upstream/history/kill-ring.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/history/kill-ring.js +1 -1
- package/dist/integrations/pi/components/upstream/history/printable-key.js +1 -1
- package/dist/integrations/pi/components/upstream/history/text-helpers.js +1 -1
- package/dist/integrations/pi/components/upstream/history/undo-stack.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/history/undo-stack.js +1 -1
- package/dist/integrations/pi/components/upstream/history/word-navigation.js +1 -1
- package/dist/integrations/pi/components/upstream/model-search.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/program-status-reporter.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/theme/theme-controller.d.ts +1 -1
- package/dist/integrations/pi/components/upstream/theme/theme.d.ts +3 -0
- package/dist/integrations/pi/components/upstream/theme/theme.js +24 -1
- package/dist/integrations/pi/engine/adapter.d.ts +1 -0
- package/dist/integrations/pi/engine/adapter.js +4 -0
- package/dist/integrations/pi/engine/pi-settings-metadata.json +2 -2
- package/dist/integrations/pi/engine/prompt-suggestions.d.ts +1 -0
- package/dist/integrations/pi/engine/prompt-suggestions.js +40 -0
- package/dist/integrations/pi/engine/resources/changelog.json +4 -4
- package/dist/integrations/pi/engine/runtime-integration.js +102 -29
- package/dist/integrations/pi/engine/session-runtime.d.ts +0 -6
- package/dist/integrations/pi/engine/session-runtime.js +8 -3
- package/dist/integrations/pi/engine/session-selection.d.ts +3 -0
- package/dist/integrations/pi/engine/session-selection.js +45 -9
- package/dist/integrations/pi/engine/workflow-contexts.js +10 -1
- package/dist/integrations/pi/startup-public.js +152 -222
- package/dist/integrations/pi/startup-public.manifest.json +266 -177
- package/dist/integrations/pi/tui-runtime/damage-aware-terminal.d.ts +1 -1
- package/dist/integrations/pi/tui-runtime/damage-aware-terminal.js +1 -1
- package/dist/native/darwin-arm64/a1_session_lock.node +0 -0
- package/dist/native/darwin-arm64/manifest.json +1 -1
- package/dist/native/darwin-arm64/terminal-host +0 -0
- package/dist/native/darwin-arm64/terminal-host-THIRD-PARTY-NOTICES.txt +10500 -0
- package/dist/native/darwin-arm64/terminal-host.manifest.json +44 -0
- package/dist/native/darwin-x64/a1_session_lock.node +0 -0
- package/dist/native/darwin-x64/manifest.json +20 -0
- package/dist/native/darwin-x64/process-guardian +0 -0
- package/dist/native/darwin-x64/terminal-host +0 -0
- package/dist/native/darwin-x64/terminal-host-THIRD-PARTY-NOTICES.txt +10737 -0
- package/dist/native/darwin-x64/terminal-host.manifest.json +44 -0
- package/dist/native/linux-x64/a1_session_lock.node +0 -0
- package/dist/native/linux-x64/manifest.json +1 -1
- package/dist/native/linux-x64/terminal-host +0 -0
- package/dist/native/linux-x64/terminal-host-THIRD-PARTY-NOTICES.txt +11226 -0
- package/dist/native/linux-x64/terminal-host.manifest.json +44 -0
- package/dist/native/win32-x64/a1_session_lock.node +0 -0
- package/dist/native/win32-x64/manifest.json +2 -2
- package/dist/native/win32-x64/process-guardian.exe +0 -0
- package/dist/native/win32-x64/terminal-host-THIRD-PARTY-NOTICES.txt +10757 -0
- package/dist/native/win32-x64/terminal-host.exe +0 -0
- package/dist/native/win32-x64/terminal-host.manifest.json +44 -0
- package/dist/product-identity.d.ts +1 -1
- package/dist/product-identity.js +1 -1
- package/dist/product-identity.json +5 -0
- package/dist/runtime-payload-inventory.json +5349 -6035
- package/dist/ui/components/line-input.d.ts +1 -1
- package/dist/ui/components/prompt-input.d.ts +7 -5
- package/dist/ui/components/prompt-input.js +7 -7
- package/dist/ui/components/transcript-viewport.d.ts +6 -3
- package/dist/ui/components/transcript-viewport.js +13 -15
- package/dist/ui/settings/declarations.d.ts +25 -1
- package/dist/ui/settings/declarations.js +21 -1
- package/dist/ui/settings/index.d.ts +4 -2
- package/dist/ui/settings/index.js +2 -1
- package/dist/ui/settings/manager.d.ts +10 -3
- package/dist/ui/settings/manager.js +90 -30
- package/dist/ui/settings/migrations.d.ts +6 -0
- package/dist/ui/settings/migrations.js +44 -0
- package/dist/ui/settings/profile-document.d.ts +26 -0
- package/dist/ui/settings/profile-document.js +164 -0
- package/docs/architecture/evidence/resident-tabs/linux-x64.md +37 -0
- package/docs/architecture/evidence/resident-tabs/macos-arm64.md +36 -0
- package/docs/architecture/evidence/resident-tabs/macos-x64.md +35 -0
- package/docs/architecture/evidence/resident-tabs/windows-x64.md +36 -0
- package/docs/architecture/internal-naming.md +3 -1
- package/docs/architecture/process-guardian-provenance.md +2 -0
- package/docs/architecture/resident-tabs-requirements.md +18 -0
- package/docs/architecture/resident-tabs.md +594 -0
- package/docs/architecture/terminal-host-proof-gate.md +2 -0
- package/docs/architecture/terminal-host-provenance.md +13 -3
- package/docs/architecture/toolchain.md +1 -1
- package/docs/ci-release-runbook.md +2 -2
- package/docs/features/launch-profiles.md +11 -8
- package/docs/manual-resident-tabs-acceptance.md +51 -0
- package/docs/openspec-archive-automation.md +17 -16
- package/docs/validation.md +9 -1
- package/package.json +10 -4
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { DraftCheckpointer } from "./draft-checkpoint.js";
|
|
4
|
+
import { ResidentTabFaultInjection } from "./fault-injection.js";
|
|
5
|
+
import { PromptJournal } from "./prompt-journal.js";
|
|
6
|
+
import { SettledTurnSynchronizer, synchronizeSessionFile } from "./session-sync.js";
|
|
7
|
+
export class ResidentTabRecovery {
|
|
8
|
+
#journal;
|
|
9
|
+
#draft;
|
|
10
|
+
#synchronizer;
|
|
11
|
+
#faults;
|
|
12
|
+
#onDegraded;
|
|
13
|
+
#active = [];
|
|
14
|
+
#recovered = { prompts: [], draft: null };
|
|
15
|
+
constructor(dataDir, options = {}) {
|
|
16
|
+
const faults = options.faults ?? new ResidentTabFaultInjection();
|
|
17
|
+
this.#faults = faults;
|
|
18
|
+
this.#onDegraded = options.onDegraded ?? (() => { });
|
|
19
|
+
this.#journal = new PromptJournal(join(dataDir, "journal.jsonl"), { faults });
|
|
20
|
+
this.#draft = new DraftCheckpointer(join(dataDir, "draft.json"), { faults, onDegraded: this.#onDegraded });
|
|
21
|
+
this.#synchronizer = new SettledTurnSynchronizer(this.#journal, faults);
|
|
22
|
+
}
|
|
23
|
+
get recovered() { return this.#recovered; }
|
|
24
|
+
async initialize(currentSessionFile = null, currentSessionId = null) {
|
|
25
|
+
await this.#journal.initialize();
|
|
26
|
+
await adoptHistoricalJournals(dirname(dirname(this.#journal.path)), this.#journal, currentSessionFile, currentSessionId);
|
|
27
|
+
const records = await this.#journal.read();
|
|
28
|
+
const sessionFiles = new Set(records.flatMap(record => record.type === "submitted" && record.sessionFile !== null ? [record.sessionFile] : []));
|
|
29
|
+
if (currentSessionFile !== null)
|
|
30
|
+
sessionFiles.add(currentSessionFile);
|
|
31
|
+
const sessionEntries = (await Promise.all([...sessionFiles].map(readSessionJournal))).flat();
|
|
32
|
+
const [pending, draft] = await Promise.all([
|
|
33
|
+
this.#journal.pending(sessionEntries),
|
|
34
|
+
this.#draft.restore(),
|
|
35
|
+
]);
|
|
36
|
+
this.#recovered = { prompts: pending.map(value => value.submitted), draft: draft?.text ?? null };
|
|
37
|
+
return this.#recovered;
|
|
38
|
+
}
|
|
39
|
+
async admit(input, timeoutMs = 2_000) {
|
|
40
|
+
const fileExisted = input.sessionFile === null ? false : await stat(input.sessionFile).then(() => true, error => {
|
|
41
|
+
if (error.code === "ENOENT")
|
|
42
|
+
return false;
|
|
43
|
+
throw error;
|
|
44
|
+
});
|
|
45
|
+
let timer;
|
|
46
|
+
try {
|
|
47
|
+
return await Promise.race([
|
|
48
|
+
this.#journal.submit({ ...input, fileExisted }),
|
|
49
|
+
new Promise((_, reject) => {
|
|
50
|
+
timer = setTimeout(() => reject(new Error("resident prompt journal admission timed out")), timeoutMs);
|
|
51
|
+
timer.unref();
|
|
52
|
+
}),
|
|
53
|
+
]);
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
if (timer !== undefined)
|
|
57
|
+
clearTimeout(timer);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
dispatched() { this.#faults.at("journal-after-dispatch"); }
|
|
61
|
+
async correlated(submissionId, entryId) {
|
|
62
|
+
await this.#journal.correlate(submissionId, entryId);
|
|
63
|
+
this.#active.push({ id: submissionId, entryId });
|
|
64
|
+
}
|
|
65
|
+
checkpoint(text) { this.#draft.change(text); }
|
|
66
|
+
async settled(sessionFile) {
|
|
67
|
+
const active = this.#active.splice(0);
|
|
68
|
+
if (active.length === 0 || sessionFile === null)
|
|
69
|
+
return;
|
|
70
|
+
await this.#synchronizer.settleMany(sessionFile, active);
|
|
71
|
+
for (const entry of active)
|
|
72
|
+
await this.#journal.retire(entry.id);
|
|
73
|
+
await this.#journal.compactIfRetired();
|
|
74
|
+
}
|
|
75
|
+
async gracefulStop(sessionFile) {
|
|
76
|
+
await this.#draft.flush();
|
|
77
|
+
if (sessionFile !== null)
|
|
78
|
+
await synchronizeSessionFile(sessionFile);
|
|
79
|
+
await this.#synchronizer.flush();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const MAX_SESSION_RECONCILE_BYTES = 64 * 1024 * 1024;
|
|
83
|
+
const MAX_ADOPTION_AGE_MS = 7 * 24 * 60 * 60 * 1_000;
|
|
84
|
+
async function adoptHistoricalJournals(profileTabsRoot, target, currentSessionFile, currentSessionId) {
|
|
85
|
+
if (currentSessionFile === null && currentSessionId === null)
|
|
86
|
+
return;
|
|
87
|
+
const history = join(profileTabsRoot, "registry-history");
|
|
88
|
+
const names = await readdir(history).catch(() => []);
|
|
89
|
+
for (const name of names.filter(value => /^tabset-.+\.json$/.test(value)).sort().reverse()) {
|
|
90
|
+
const path = join(history, name);
|
|
91
|
+
const metadata = await stat(path).catch(() => null);
|
|
92
|
+
if (metadata === null || Date.now() - metadata.mtimeMs > MAX_ADOPTION_AGE_MS)
|
|
93
|
+
continue;
|
|
94
|
+
let document;
|
|
95
|
+
try {
|
|
96
|
+
document = JSON.parse(await readFile(path, "utf8"));
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const tabs = typeof document === "object" && document !== null && Array.isArray(document.tabs)
|
|
102
|
+
? document.tabs : [];
|
|
103
|
+
for (const value of tabs) {
|
|
104
|
+
if (typeof value !== "object" || value === null)
|
|
105
|
+
continue;
|
|
106
|
+
const tab = value;
|
|
107
|
+
if (typeof tab.tabId !== "string")
|
|
108
|
+
continue;
|
|
109
|
+
const fileMatches = currentSessionFile !== null && typeof tab.sessionFile === "string"
|
|
110
|
+
&& resolve(tab.sessionFile) === resolve(currentSessionFile);
|
|
111
|
+
const sourcePath = join(profileTabsRoot, tab.tabId, "journal.jsonl");
|
|
112
|
+
if (resolve(sourcePath) === resolve(target.path))
|
|
113
|
+
continue;
|
|
114
|
+
if (!await stat(sourcePath).then(() => true, () => false))
|
|
115
|
+
continue;
|
|
116
|
+
const source = new PromptJournal(sourcePath);
|
|
117
|
+
await source.initialize();
|
|
118
|
+
const records = await source.read();
|
|
119
|
+
const submitted = records.filter((record) => record.type === "submitted");
|
|
120
|
+
const idMatches = currentSessionId !== null && submitted.some(record => record.sessionId === currentSessionId);
|
|
121
|
+
if (!fileMatches && !idMatches)
|
|
122
|
+
continue;
|
|
123
|
+
const pending = await source.pending(currentSessionFile === null ? [] : await readSessionJournal(currentSessionFile));
|
|
124
|
+
for (const entry of pending) {
|
|
125
|
+
await target.adopt(entry.submitted);
|
|
126
|
+
await source.retire(entry.submitted.id);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** Reconcile only durable user/assistant ancestry; a torn final JSONL record is not authority. */
|
|
132
|
+
export async function readSessionJournal(path) {
|
|
133
|
+
let text;
|
|
134
|
+
try {
|
|
135
|
+
const metadata = await stat(path);
|
|
136
|
+
if (metadata.size > MAX_SESSION_RECONCILE_BYTES)
|
|
137
|
+
throw new Error("resident session exceeds the recovery scan limit");
|
|
138
|
+
text = await readFile(path, "utf8");
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
if (error.code === "ENOENT")
|
|
142
|
+
return [];
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
const raw = [];
|
|
146
|
+
const lines = text.split("\n");
|
|
147
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
148
|
+
const line = lines[index];
|
|
149
|
+
if (line.length === 0)
|
|
150
|
+
continue;
|
|
151
|
+
let value;
|
|
152
|
+
try {
|
|
153
|
+
value = JSON.parse(line);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
if (index === lines.length - 1 && !text.endsWith("\n"))
|
|
157
|
+
break;
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
if (typeof value !== "object" || value === null)
|
|
161
|
+
continue;
|
|
162
|
+
const record = value;
|
|
163
|
+
const message = typeof record.message === "object" && record.message !== null
|
|
164
|
+
? record.message
|
|
165
|
+
: null;
|
|
166
|
+
if (record.type !== "message" || typeof record.id !== "string" || message === null
|
|
167
|
+
|| typeof message.role !== "string")
|
|
168
|
+
continue;
|
|
169
|
+
const content = message.content;
|
|
170
|
+
const promptText = typeof content === "string" ? content : Array.isArray(content)
|
|
171
|
+
? content.flatMap(part => typeof part === "object" && part !== null
|
|
172
|
+
&& part.type === "text"
|
|
173
|
+
&& typeof part.text === "string"
|
|
174
|
+
? [part.text] : []).join("")
|
|
175
|
+
: "";
|
|
176
|
+
raw.push({
|
|
177
|
+
id: record.id,
|
|
178
|
+
parentId: typeof record.parentId === "string" ? record.parentId : null,
|
|
179
|
+
role: message.role,
|
|
180
|
+
text: promptText,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
const byId = new Map(raw.map(record => [record.id, record]));
|
|
184
|
+
const settled = new Set();
|
|
185
|
+
for (const record of raw) {
|
|
186
|
+
if (record.role !== "assistant")
|
|
187
|
+
continue;
|
|
188
|
+
let parent = record.parentId;
|
|
189
|
+
const visited = new Set();
|
|
190
|
+
while (parent !== null && !visited.has(parent)) {
|
|
191
|
+
visited.add(parent);
|
|
192
|
+
const ancestor = byId.get(parent);
|
|
193
|
+
if (ancestor === undefined)
|
|
194
|
+
break;
|
|
195
|
+
if (ancestor.role === "user") {
|
|
196
|
+
settled.add(ancestor.id);
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
parent = ancestor.parentId;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return raw
|
|
203
|
+
.filter(record => record.role === "user")
|
|
204
|
+
.map(record => ({ entryId: record.id, text: record.text, settled: settled.has(record.id) }));
|
|
205
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface ResidentReleaseHoldReport {
|
|
2
|
+
/** False when any resident record or identity could not be verified; callers then keep every release. */
|
|
3
|
+
readonly complete: boolean;
|
|
4
|
+
readonly releaseRoots: readonly string[];
|
|
5
|
+
readonly uncertain: readonly string[];
|
|
6
|
+
}
|
|
7
|
+
export interface ResidentReleaseHoldOptions {
|
|
8
|
+
readonly dataDir: string;
|
|
9
|
+
/** Installed release roots whose terminal host may answer, in preference order. */
|
|
10
|
+
readonly releaseRoots: readonly string[];
|
|
11
|
+
readonly platform?: NodeJS.Platform;
|
|
12
|
+
readonly architecture?: string;
|
|
13
|
+
readonly timeoutMs?: number;
|
|
14
|
+
readonly verify?: (executable: string, platform: NodeJS.Platform, architecture: string) => Promise<unknown>;
|
|
15
|
+
readonly run?: (executable: string, arguments_: readonly string[], timeoutMs: number) => Promise<{
|
|
16
|
+
readonly code: number;
|
|
17
|
+
readonly stdout: string;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Reports the releases that live resident processes run from, read-only. The verified terminal
|
|
22
|
+
* host inspects its own records, so pid reuse and identity formats are judged exactly as the
|
|
23
|
+
* server judges them. No records means nothing to hold; any doubt yields an incomplete report.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readResidentReleaseHolds(options: ResidentReleaseHoldOptions): Promise<ResidentReleaseHoldReport>;
|
|
26
|
+
/** The largest release-hold report accepted from the terminal host. */
|
|
27
|
+
export declare const MAX_RELEASE_HOLD_REPORT_BYTES: number;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { readdir, stat } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { runBoundedHostCommand } from "./bounded-host-command.js";
|
|
4
|
+
import { resolveTerminalHostPath } from "./ensure-terminal-host.js";
|
|
5
|
+
import { verifyTerminalHostArtifact } from "./terminal-host-artifact.js";
|
|
6
|
+
const COMPLETE_EMPTY = Object.freeze({ complete: true, releaseRoots: Object.freeze([]), uncertain: Object.freeze([]) });
|
|
7
|
+
/**
|
|
8
|
+
* Reports the releases that live resident processes run from, read-only. The verified terminal
|
|
9
|
+
* host inspects its own records, so pid reuse and identity formats are judged exactly as the
|
|
10
|
+
* server judges them. No records means nothing to hold; any doubt yields an incomplete report.
|
|
11
|
+
*/
|
|
12
|
+
export async function readResidentReleaseHolds(options) {
|
|
13
|
+
if (!await hasResidentRecords(options.dataDir))
|
|
14
|
+
return COMPLETE_EMPTY;
|
|
15
|
+
const platform = options.platform ?? process.platform;
|
|
16
|
+
const architecture = options.architecture ?? process.arch;
|
|
17
|
+
const verify = options.verify ?? verifyTerminalHostArtifact;
|
|
18
|
+
let executable;
|
|
19
|
+
for (const root of options.releaseRoots) {
|
|
20
|
+
const candidate = resolveTerminalHostPath(root, {}, platform, architecture);
|
|
21
|
+
try {
|
|
22
|
+
await verify(candidate, platform, architecture);
|
|
23
|
+
executable = candidate;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// Rationale: an older release may predate the packaged host; another retained release may still carry it.
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (!executable)
|
|
31
|
+
return incomplete("no verified terminal host is installed to inspect resident records");
|
|
32
|
+
try {
|
|
33
|
+
const result = await (options.run ?? runHost)(executable, ["release-holds", "--data-dir", options.dataDir], options.timeoutMs ?? 5_000);
|
|
34
|
+
const report = JSON.parse(result.stdout.trim().split(/\r?\n/u).at(-1) ?? "");
|
|
35
|
+
if (result.code !== 0 || report.schema !== "a1-resident-release-holds-v1" || typeof report.complete !== "boolean"
|
|
36
|
+
|| !isStringArray(report.releaseRoots) || !isStringArray(report.uncertain)) {
|
|
37
|
+
return incomplete("terminal host returned no valid release-hold report");
|
|
38
|
+
}
|
|
39
|
+
return { complete: report.complete, releaseRoots: report.releaseRoots, uncertain: report.uncertain };
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return incomplete("terminal host release-hold inspection failed");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async function hasResidentRecords(dataDir) {
|
|
46
|
+
let profiles;
|
|
47
|
+
try {
|
|
48
|
+
profiles = await readdir(resolve(dataDir, "tabs"));
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
return !(error instanceof Error && "code" in error && error.code === "ENOENT");
|
|
52
|
+
}
|
|
53
|
+
for (const profile of profiles) {
|
|
54
|
+
for (const record of ["server.json", "registry.json"]) {
|
|
55
|
+
try {
|
|
56
|
+
await stat(resolve(dataDir, "tabs", profile, record));
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
function incomplete(reason) {
|
|
68
|
+
return { complete: false, releaseRoots: [], uncertain: [reason] };
|
|
69
|
+
}
|
|
70
|
+
function isStringArray(value) {
|
|
71
|
+
return Array.isArray(value) && value.every(item => typeof item === "string");
|
|
72
|
+
}
|
|
73
|
+
/** The largest release-hold report accepted from the terminal host. */
|
|
74
|
+
export const MAX_RELEASE_HOLD_REPORT_BYTES = 1024 * 1024;
|
|
75
|
+
async function runHost(executable, arguments_, timeoutMs) {
|
|
76
|
+
return await runBoundedHostCommand(executable, arguments_, { maxStdoutBytes: MAX_RELEASE_HOLD_REPORT_BYTES, timeoutMs, label: "terminal host release-hold inspection" });
|
|
77
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { EnsureTerminalHostResult } from "./ensure-terminal-host.js";
|
|
2
|
+
export interface ResidentHostSelectionOptions {
|
|
3
|
+
readonly profileId: "a1" | "pi";
|
|
4
|
+
readonly currentLaunchContract: boolean;
|
|
5
|
+
readonly solo: boolean;
|
|
6
|
+
/** Both standard input and standard output are terminals the attach client can own. */
|
|
7
|
+
readonly interactive: boolean;
|
|
8
|
+
readonly ensureHost: () => Promise<EnsureTerminalHostResult>;
|
|
9
|
+
}
|
|
10
|
+
/** Invariant: comparison, solo, and superseded launches stay outside resident discovery and startup. */
|
|
11
|
+
export declare function selectResidentHost(options: ResidentHostSelectionOptions): Promise<EnsureTerminalHostResult | null>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Invariant: comparison, solo, and superseded launches stay outside resident discovery and startup. */
|
|
2
|
+
export async function selectResidentHost(options) {
|
|
3
|
+
if (options.profileId !== "a1" || options.solo || !options.currentLaunchContract)
|
|
4
|
+
return null;
|
|
5
|
+
// Platform: the attach client reads the outer terminal's size and owns its raw input, so piped
|
|
6
|
+
// or redirected launches run the direct UI, which already serves non-terminal streams.
|
|
7
|
+
if (!options.interactive)
|
|
8
|
+
return { status: "fallback", reason: "no interactive terminal" };
|
|
9
|
+
return await options.ensureHost();
|
|
10
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PromptJournal } from "./prompt-journal.js";
|
|
2
|
+
import { ResidentTabFaultInjection } from "./fault-injection.js";
|
|
3
|
+
export declare function synchronizeSessionFile(path: string, faults?: ResidentTabFaultInjection): Promise<void>;
|
|
4
|
+
/** Serializes settled-turn sync and journal ordering off the input path. */
|
|
5
|
+
export declare class SettledTurnSynchronizer {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(journal: PromptJournal, faults?: ResidentTabFaultInjection);
|
|
8
|
+
settle(sessionFile: string, submissionId: string, entryId: string): Promise<void>;
|
|
9
|
+
settleMany(sessionFile: string, entries: readonly {
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly entryId: string;
|
|
12
|
+
}[]): Promise<void>;
|
|
13
|
+
flush(): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { open } from "node:fs/promises";
|
|
2
|
+
import { ResidentTabFaultInjection } from "./fault-injection.js";
|
|
3
|
+
export async function synchronizeSessionFile(path, faults = new ResidentTabFaultInjection()) {
|
|
4
|
+
const handle = await open(path, "r+");
|
|
5
|
+
try {
|
|
6
|
+
faults.at("session-before-sync");
|
|
7
|
+
await handle.sync();
|
|
8
|
+
faults.at("session-after-sync");
|
|
9
|
+
}
|
|
10
|
+
finally {
|
|
11
|
+
await handle.close();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** Serializes settled-turn sync and journal ordering off the input path. */
|
|
15
|
+
export class SettledTurnSynchronizer {
|
|
16
|
+
#journal;
|
|
17
|
+
#faults;
|
|
18
|
+
#queue = Promise.resolve();
|
|
19
|
+
constructor(journal, faults = new ResidentTabFaultInjection()) {
|
|
20
|
+
this.#journal = journal;
|
|
21
|
+
this.#faults = faults;
|
|
22
|
+
}
|
|
23
|
+
settle(sessionFile, submissionId, entryId) {
|
|
24
|
+
return this.settleMany(sessionFile, [{ id: submissionId, entryId }]);
|
|
25
|
+
}
|
|
26
|
+
settleMany(sessionFile, entries) {
|
|
27
|
+
const operation = this.#queue.then(async () => {
|
|
28
|
+
await synchronizeSessionFile(sessionFile, this.#faults);
|
|
29
|
+
this.#faults.at("journal-after-settle-sync");
|
|
30
|
+
for (const entry of entries)
|
|
31
|
+
await this.#journal.settle(entry.id, entry.entryId);
|
|
32
|
+
});
|
|
33
|
+
this.#queue = operation.catch(() => undefined);
|
|
34
|
+
return operation;
|
|
35
|
+
}
|
|
36
|
+
flush() { return this.#queue; }
|
|
37
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const RESIDENT_TABS_MAX_CONCURRENT_STARTS = 2;
|
|
2
|
+
export declare const RESIDENT_TABS_MAX_CONCURRENT_STARTS_CAP = 8;
|
|
3
|
+
export declare const RESIDENT_TABS_PREWARM_COUNT = 1;
|
|
4
|
+
export declare const RESIDENT_TABS_PREWARM_CAP = 1;
|
|
5
|
+
export interface ResidentTabLaunchSettings {
|
|
6
|
+
readonly tabsMax: number;
|
|
7
|
+
readonly tabsAutoName: boolean;
|
|
8
|
+
readonly maxConcurrentStarts: number;
|
|
9
|
+
readonly prewarmCount: number;
|
|
10
|
+
readonly unresponsiveRestartSeconds: number;
|
|
11
|
+
readonly stallNoticeSeconds: number;
|
|
12
|
+
}
|
|
13
|
+
/** Read only resident-launch values before any UI or extension module is loaded. */
|
|
14
|
+
export declare function readResidentTabLaunchSettings(profileDir: string, namespace?: string, legacyConfigDir?: string, profileId?: string): Promise<ResidentTabLaunchSettings>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { OWNED_SETTING_DECLARATIONS, OWNED_UI_SETTINGS_MIGRATIONS, OWNED_UI_SETTINGS_VERSION, normalizeOwnedUiSettingsValues, parseOwnedUiSettingsDocument, readProfileSettingsNamespace, repairProfileSettingsDocument, resolveOwnedUiSettings, settingValue, } from "../../ui/settings/index.js";
|
|
4
|
+
const MAX_SETTINGS_BYTES = 256 * 1024;
|
|
5
|
+
export const RESIDENT_TABS_MAX_CONCURRENT_STARTS = 2;
|
|
6
|
+
export const RESIDENT_TABS_MAX_CONCURRENT_STARTS_CAP = 8;
|
|
7
|
+
export const RESIDENT_TABS_PREWARM_COUNT = 1;
|
|
8
|
+
export const RESIDENT_TABS_PREWARM_CAP = 1;
|
|
9
|
+
const DEFAULTS = Object.freeze({
|
|
10
|
+
tabsMax: 10,
|
|
11
|
+
tabsAutoName: true,
|
|
12
|
+
maxConcurrentStarts: RESIDENT_TABS_MAX_CONCURRENT_STARTS,
|
|
13
|
+
prewarmCount: RESIDENT_TABS_PREWARM_COUNT,
|
|
14
|
+
unresponsiveRestartSeconds: 120,
|
|
15
|
+
stallNoticeSeconds: 300,
|
|
16
|
+
});
|
|
17
|
+
/** Read only resident-launch values before any UI or extension module is loaded. */
|
|
18
|
+
export async function readResidentTabLaunchSettings(profileDir, namespace = "a1", legacyConfigDir, profileId = namespace) {
|
|
19
|
+
const sharedFile = resolve(profileDir, "settings.json");
|
|
20
|
+
try {
|
|
21
|
+
repairProfileSettingsDocument(sharedFile);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
// Compatibility: recovery is opportunistic; resident launch falls back when bytes stay invalid.
|
|
25
|
+
}
|
|
26
|
+
const shared = readProfileSettingsNamespace(sharedFile, namespace);
|
|
27
|
+
if (shared.status === "invalid")
|
|
28
|
+
return DEFAULTS;
|
|
29
|
+
if (shared.status === "available") {
|
|
30
|
+
const compatibility = normalizeOwnedUiSettingsValues(shared.value);
|
|
31
|
+
return compatibility.values === null
|
|
32
|
+
? DEFAULTS
|
|
33
|
+
: resolveLaunchValues({ version: OWNED_UI_SETTINGS_VERSION, values: compatibility.values }, []);
|
|
34
|
+
}
|
|
35
|
+
const file = resolve(legacyConfigDir ?? profileDir, "settings", `${profileId}.json`);
|
|
36
|
+
try {
|
|
37
|
+
const raw = await readFile(file, "utf8");
|
|
38
|
+
if (Buffer.byteLength(raw, "utf8") > MAX_SETTINGS_BYTES)
|
|
39
|
+
return DEFAULTS;
|
|
40
|
+
const document = parseOwnedUiSettingsDocument(raw);
|
|
41
|
+
return document === null ? DEFAULTS : resolveLaunchValues(document, OWNED_UI_SETTINGS_MIGRATIONS);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return DEFAULTS;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function resolveLaunchValues(document, migrations) {
|
|
48
|
+
const resolution = resolveOwnedUiSettings({
|
|
49
|
+
declarations: [
|
|
50
|
+
OWNED_SETTING_DECLARATIONS.tabsMax,
|
|
51
|
+
OWNED_SETTING_DECLARATIONS.tabsAutoName,
|
|
52
|
+
],
|
|
53
|
+
migrations,
|
|
54
|
+
document,
|
|
55
|
+
currentVersion: OWNED_UI_SETTINGS_VERSION,
|
|
56
|
+
});
|
|
57
|
+
const tabsMax = document.values?.["tabsMax"] ?? settingValue(resolution, "tabsMax");
|
|
58
|
+
return {
|
|
59
|
+
tabsMax: typeof tabsMax === "number" && Number.isFinite(tabsMax) ? Math.max(1, Math.min(50, Math.trunc(tabsMax))) : DEFAULTS.tabsMax,
|
|
60
|
+
tabsAutoName: settingValue(resolution, "tabsAutoName") === true,
|
|
61
|
+
maxConcurrentStarts: RESIDENT_TABS_MAX_CONCURRENT_STARTS,
|
|
62
|
+
prewarmCount: RESIDENT_TABS_PREWARM_COUNT,
|
|
63
|
+
unresponsiveRestartSeconds: DEFAULTS.unresponsiveRestartSeconds,
|
|
64
|
+
stallNoticeSeconds: DEFAULTS.stallNoticeSeconds,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const TERMINAL_HOST_MANIFEST_FILENAME = "terminal-host.manifest.json";
|
|
2
|
+
export declare const TERMINAL_HOST_GENERATION = 1;
|
|
3
|
+
export type TerminalHostArtifactCode = "TERMINAL_HOST_ARTIFACT_MISSING" | "TERMINAL_HOST_ARTIFACT_INCOMPATIBLE" | "TERMINAL_HOST_ARTIFACT_UNSUPPORTED" | "TERMINAL_HOST_ARTIFACT_TAMPERED";
|
|
4
|
+
export interface VerifiedTerminalHostArtifact {
|
|
5
|
+
readonly executable: string;
|
|
6
|
+
readonly sessionLock: string;
|
|
7
|
+
readonly generation: number;
|
|
8
|
+
}
|
|
9
|
+
export declare class TerminalHostArtifactError extends Error {
|
|
10
|
+
readonly code: TerminalHostArtifactCode;
|
|
11
|
+
constructor(message: string, code: TerminalHostArtifactCode);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Verifies a release's terminal host and its session-writer lock against their manifest before
|
|
15
|
+
* anything executes or loads them: schema, generation, platform, architecture, file names, sizes,
|
|
16
|
+
* and SHA-256 digests.
|
|
17
|
+
*/
|
|
18
|
+
export declare function verifyTerminalHostArtifact(executable: string, platform?: NodeJS.Platform, architecture?: string, generation?: number): Promise<VerifiedTerminalHostArtifact>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFile, stat } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, resolve } from "node:path";
|
|
4
|
+
export const TERMINAL_HOST_MANIFEST_FILENAME = "terminal-host.manifest.json";
|
|
5
|
+
export const TERMINAL_HOST_GENERATION = 1;
|
|
6
|
+
export class TerminalHostArtifactError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
constructor(message, code) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "TerminalHostArtifactError";
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Verifies a release's terminal host and its session-writer lock against their manifest before
|
|
16
|
+
* anything executes or loads them: schema, generation, platform, architecture, file names, sizes,
|
|
17
|
+
* and SHA-256 digests.
|
|
18
|
+
*/
|
|
19
|
+
export async function verifyTerminalHostArtifact(executable, platform = process.platform, architecture = process.arch, generation = TERMINAL_HOST_GENERATION) {
|
|
20
|
+
const directory = dirname(executable);
|
|
21
|
+
let manifest;
|
|
22
|
+
try {
|
|
23
|
+
manifest = JSON.parse(await readFile(resolve(directory, TERMINAL_HOST_MANIFEST_FILENAME), "utf8"));
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw new TerminalHostArtifactError(`terminal host manifest is missing or malformed: ${errorMessage(error)}`, "TERMINAL_HOST_ARTIFACT_MISSING");
|
|
27
|
+
}
|
|
28
|
+
if (manifest.schema !== "a1-terminal-host-artifact-v1" || manifest.protocolVersion !== 1 || manifest.generation !== generation
|
|
29
|
+
|| manifest.platform !== platform || manifest.architecture !== architecture
|
|
30
|
+
|| manifest.artifact?.filename !== basename(executable) || typeof manifest.sessionLock?.filename !== "string"
|
|
31
|
+
|| basename(manifest.sessionLock.filename) !== manifest.sessionLock.filename) {
|
|
32
|
+
throw new TerminalHostArtifactError("terminal host manifest is incompatible with this platform or generation", "TERMINAL_HOST_ARTIFACT_INCOMPATIBLE");
|
|
33
|
+
}
|
|
34
|
+
if (manifest.capability !== "supported") {
|
|
35
|
+
throw new TerminalHostArtifactError(`resident tabs are not supported on ${platform}-${architecture}`, "TERMINAL_HOST_ARTIFACT_UNSUPPORTED");
|
|
36
|
+
}
|
|
37
|
+
const sessionLock = resolve(directory, manifest.sessionLock.filename);
|
|
38
|
+
await verifyFile(executable, manifest.artifact, "terminal host");
|
|
39
|
+
await verifyFile(sessionLock, manifest.sessionLock, "session-writer lock");
|
|
40
|
+
return { executable, sessionLock, generation: manifest.generation };
|
|
41
|
+
}
|
|
42
|
+
async function verifyFile(path, expected, label) {
|
|
43
|
+
const [bytes, metadata] = await Promise.all([readFile(path), stat(path)]).catch((error) => {
|
|
44
|
+
throw new TerminalHostArtifactError(`${label} artifact is unavailable: ${errorMessage(error)}`, "TERMINAL_HOST_ARTIFACT_MISSING");
|
|
45
|
+
});
|
|
46
|
+
const digest = createHash("sha256").update(bytes).digest("hex");
|
|
47
|
+
if (metadata.size !== expected.size || digest !== expected.sha256) {
|
|
48
|
+
throw new TerminalHostArtifactError(`${label} artifact integrity verification failed`, "TERMINAL_HOST_ARTIFACT_TAMPERED");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function errorMessage(error) {
|
|
52
|
+
return error instanceof Error ? error.message : String(error);
|
|
53
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface ZipEntry {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
readonly data: Buffer;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Encodes a small deflated zip archive. Names are forward-slash relative paths; there are no
|
|
7
|
+
* directories, extra fields, or encryption, so any standard unzip tool reads it.
|
|
8
|
+
*/
|
|
9
|
+
export declare function encodeZip(entries: readonly ZipEntry[], modified?: Date): Buffer;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { crc32, deflateRawSync } from "node:zlib";
|
|
2
|
+
/**
|
|
3
|
+
* Encodes a small deflated zip archive. Names are forward-slash relative paths; there are no
|
|
4
|
+
* directories, extra fields, or encryption, so any standard unzip tool reads it.
|
|
5
|
+
*/
|
|
6
|
+
export function encodeZip(entries, modified = new Date()) {
|
|
7
|
+
const time = ((modified.getHours() << 11) | (modified.getMinutes() << 5) | Math.floor(modified.getSeconds() / 2)) & 0xffff;
|
|
8
|
+
const date = (((modified.getFullYear() - 1980) << 9) | ((modified.getMonth() + 1) << 5) | modified.getDate()) & 0xffff;
|
|
9
|
+
const locals = [];
|
|
10
|
+
const centrals = [];
|
|
11
|
+
let offset = 0;
|
|
12
|
+
for (const entry of entries) {
|
|
13
|
+
const segments = entry.name.split("/");
|
|
14
|
+
if (segments.some(segment => !/^[A-Za-z0-9._-]+$/u.test(segment) || segment === "." || segment === "..")) {
|
|
15
|
+
throw new Error(`zip entry name is unsafe: ${entry.name}`);
|
|
16
|
+
}
|
|
17
|
+
const name = Buffer.from(entry.name, "utf8");
|
|
18
|
+
const compressed = deflateRawSync(entry.data);
|
|
19
|
+
const checksum = crc32(entry.data);
|
|
20
|
+
const local = Buffer.alloc(30);
|
|
21
|
+
local.writeUInt32LE(0x04034b50, 0);
|
|
22
|
+
local.writeUInt16LE(20, 4);
|
|
23
|
+
local.writeUInt16LE(0x0800, 6);
|
|
24
|
+
local.writeUInt16LE(8, 8);
|
|
25
|
+
local.writeUInt16LE(time, 10);
|
|
26
|
+
local.writeUInt16LE(date, 12);
|
|
27
|
+
local.writeUInt32LE(checksum, 14);
|
|
28
|
+
local.writeUInt32LE(compressed.length, 18);
|
|
29
|
+
local.writeUInt32LE(entry.data.length, 22);
|
|
30
|
+
local.writeUInt16LE(name.length, 26);
|
|
31
|
+
locals.push(local, name, compressed);
|
|
32
|
+
const central = Buffer.alloc(46);
|
|
33
|
+
central.writeUInt32LE(0x02014b50, 0);
|
|
34
|
+
central.writeUInt16LE(20, 4);
|
|
35
|
+
central.writeUInt16LE(20, 6);
|
|
36
|
+
central.writeUInt16LE(0x0800, 8);
|
|
37
|
+
central.writeUInt16LE(8, 10);
|
|
38
|
+
central.writeUInt16LE(time, 12);
|
|
39
|
+
central.writeUInt16LE(date, 14);
|
|
40
|
+
central.writeUInt32LE(checksum, 16);
|
|
41
|
+
central.writeUInt32LE(compressed.length, 20);
|
|
42
|
+
central.writeUInt32LE(entry.data.length, 24);
|
|
43
|
+
central.writeUInt16LE(name.length, 28);
|
|
44
|
+
central.writeUInt32LE(offset, 42);
|
|
45
|
+
centrals.push(central, name);
|
|
46
|
+
offset += local.length + name.length + compressed.length;
|
|
47
|
+
}
|
|
48
|
+
const directory = Buffer.concat(centrals);
|
|
49
|
+
const end = Buffer.alloc(22);
|
|
50
|
+
end.writeUInt32LE(0x06054b50, 0);
|
|
51
|
+
end.writeUInt16LE(entries.length, 8);
|
|
52
|
+
end.writeUInt16LE(entries.length, 10);
|
|
53
|
+
end.writeUInt32LE(directory.length, 12);
|
|
54
|
+
end.writeUInt32LE(offset, 16);
|
|
55
|
+
return Buffer.concat([...locals, directory, end]);
|
|
56
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
export interface NativeLock {
|
|
3
|
+
readonly bound: boolean;
|
|
4
|
+
bindCreated(path: string): void;
|
|
5
|
+
release(): void;
|
|
6
|
+
}
|
|
7
|
+
export interface LockBackend {
|
|
8
|
+
acquire(path: string): NativeLock;
|
|
9
|
+
}
|
|
10
|
+
/** Reports that a canonical session identity already has a live writer. */
|
|
11
|
+
export declare class SessionWriterConflict extends Error {
|
|
12
|
+
readonly path: string;
|
|
13
|
+
readonly holderKind: string;
|
|
14
|
+
constructor(path: string, holderKind?: string);
|
|
15
|
+
}
|
|
16
|
+
export interface SessionWriterHandle {
|
|
17
|
+
readonly path: string;
|
|
18
|
+
readonly bound: boolean;
|
|
19
|
+
bind(createdPath?: string): void;
|
|
20
|
+
release(): void;
|
|
21
|
+
}
|
|
22
|
+
/** Development-only override naming a native lock addon to load first. */
|
|
23
|
+
export declare const SESSION_LOCK_OVERRIDE_ENVIRONMENT = "A1_SESSION_LOCK_PATH";
|
|
24
|
+
/** Security: a release that carries its own lock never loads an addon chosen by the environment or cwd. */
|
|
25
|
+
export declare function nativeCandidates(moduleUrl?: string, env?: NodeJS.ProcessEnv, cwd?: string, exists?: typeof existsSync): string[];
|
|
26
|
+
export declare function acquireExisting(path: string): SessionWriterHandle;
|
|
27
|
+
export declare function reserveNew(parent: string, name: string): SessionWriterHandle;
|
|
28
|
+
export declare function acquireSessionWriter(path: string): SessionWriterHandle;
|
|
29
|
+
export declare function bind(handle: SessionWriterHandle, createdPath?: string): void;
|
|
30
|
+
export declare function release(handle: SessionWriterHandle): void;
|
|
31
|
+
/** Test-only backend seam; passing undefined restores native discovery. */
|
|
32
|
+
export declare function setSessionWriterLockBackendForTest(value: LockBackend | undefined): void;
|