opencode-live-compact 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +137 -3
- package/README.zh-CN.md +137 -0
- package/dist/config.d.ts +15 -0
- package/dist/config.js +33 -0
- package/dist/config.js.map +1 -0
- package/dist/engine/checkpoint.d.ts +8 -0
- package/dist/engine/checkpoint.js +50 -0
- package/dist/engine/checkpoint.js.map +1 -0
- package/dist/engine/eligibility.d.ts +30 -0
- package/dist/engine/eligibility.js +149 -0
- package/dist/engine/eligibility.js.map +1 -0
- package/dist/engine/planner.d.ts +22 -0
- package/dist/engine/planner.js +56 -0
- package/dist/engine/planner.js.map +1 -0
- package/dist/engine/reducer.d.ts +13 -0
- package/dist/engine/reducer.js +56 -0
- package/dist/engine/reducer.js.map +1 -0
- package/dist/engine/span.d.ts +19 -0
- package/dist/engine/span.js +369 -0
- package/dist/engine/span.js.map +1 -0
- package/dist/hooks/context.d.ts +8 -0
- package/dist/hooks/context.js +38 -0
- package/dist/hooks/context.js.map +1 -0
- package/dist/hooks/replacement.d.ts +11 -0
- package/dist/hooks/replacement.js +286 -0
- package/dist/hooks/replacement.js.map +1 -0
- package/dist/hooks/session.d.ts +4 -0
- package/dist/hooks/session.js +53 -0
- package/dist/hooks/session.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -0
- package/dist/metrics/collector.d.ts +41 -0
- package/dist/metrics/collector.js +17 -0
- package/dist/metrics/collector.js.map +1 -0
- package/dist/recovery/integrity.d.ts +11 -0
- package/dist/recovery/integrity.js +24 -0
- package/dist/recovery/integrity.js.map +1 -0
- package/dist/recovery/payloads.d.ts +19 -0
- package/dist/recovery/payloads.js +182 -0
- package/dist/recovery/payloads.js.map +1 -0
- package/dist/recovery/private-directory.d.ts +1 -0
- package/dist/recovery/private-directory.js +121 -0
- package/dist/recovery/private-directory.js.map +1 -0
- package/dist/recovery/restore.d.ts +12 -0
- package/dist/recovery/restore.js +32 -0
- package/dist/recovery/restore.js.map +1 -0
- package/dist/recovery/store.d.ts +51 -0
- package/dist/recovery/store.js +308 -0
- package/dist/recovery/store.js.map +1 -0
- package/dist/state/reconciliation.d.ts +32 -0
- package/dist/state/reconciliation.js +173 -0
- package/dist/state/reconciliation.js.map +1 -0
- package/docs/architecture.md +54 -0
- package/docs/assets/benchmarks-en.svg +106 -0
- package/docs/assets/benchmarks-zh-CN.svg +106 -0
- package/package.json +65 -4
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { hashText, isRecoveryReference } from "./integrity.js";
|
|
4
|
+
/** File operations run only while the catalog's transaction lock is held. */
|
|
5
|
+
export class RecoveryPayloads {
|
|
6
|
+
archiveRoot;
|
|
7
|
+
archiveIdentity;
|
|
8
|
+
root;
|
|
9
|
+
constructor(databasePath) {
|
|
10
|
+
this.archiveRoot = dirname(databasePath);
|
|
11
|
+
this.archiveIdentity = this.rootStat();
|
|
12
|
+
this.root = join(this.archiveRoot, "sessions");
|
|
13
|
+
}
|
|
14
|
+
rootStat() {
|
|
15
|
+
const stat = lstatSync(this.archiveRoot, { bigint: true }), uid = process.getuid?.();
|
|
16
|
+
if (!stat.isDirectory() ||
|
|
17
|
+
(uid !== undefined && (stat.uid !== BigInt(uid) || (stat.mode & 63n) !== 0n))) {
|
|
18
|
+
throw new Error("Non-private recovery payload path");
|
|
19
|
+
}
|
|
20
|
+
return stat;
|
|
21
|
+
}
|
|
22
|
+
checkRoot() {
|
|
23
|
+
const current = this.rootStat();
|
|
24
|
+
if (current.dev !== this.archiveIdentity.dev || current.ino !== this.archiveIdentity.ino) {
|
|
25
|
+
throw new Error("Recovery archive directory changed");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
check(path, directory) {
|
|
29
|
+
const stat = lstatSync(path), uid = process.getuid?.();
|
|
30
|
+
if ((directory ? !stat.isDirectory() : !stat.isFile() || stat.nlink !== 1) ||
|
|
31
|
+
(uid !== undefined && (stat.uid !== uid || (stat.mode & 0o077) !== 0))) {
|
|
32
|
+
throw new Error("Non-private recovery payload path");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
directory(sessionID, create = false) {
|
|
36
|
+
this.checkRoot();
|
|
37
|
+
if (create) {
|
|
38
|
+
try {
|
|
39
|
+
mkdirSync(this.root, { mode: 0o700 });
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error.code !== "EEXIST")
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
this.check(this.root, true);
|
|
47
|
+
const directory = join(this.root, `session-${hashText(sessionID)}`);
|
|
48
|
+
if (create) {
|
|
49
|
+
try {
|
|
50
|
+
mkdirSync(directory, { mode: 0o700 });
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error.code !== "EEXIST")
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
this.check(directory, true);
|
|
58
|
+
return directory;
|
|
59
|
+
}
|
|
60
|
+
path(sessionID, reference, create = false) {
|
|
61
|
+
if (!isRecoveryReference(reference))
|
|
62
|
+
throw new Error("Invalid recovery reference");
|
|
63
|
+
return join(this.directory(sessionID, create), reference + ".json");
|
|
64
|
+
}
|
|
65
|
+
syncDirectory(directory) {
|
|
66
|
+
// Windows cannot fsync directory handles through Node. The file itself is
|
|
67
|
+
// always flushed before its durable catalog entry is committed.
|
|
68
|
+
if (process.platform === "win32")
|
|
69
|
+
return;
|
|
70
|
+
const descriptor = openSync(directory, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
71
|
+
try {
|
|
72
|
+
fsyncSync(descriptor);
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
closeSync(descriptor);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
read(sessionID, reference, maxBytes) {
|
|
79
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 0)
|
|
80
|
+
return;
|
|
81
|
+
try {
|
|
82
|
+
const path = this.path(sessionID, reference);
|
|
83
|
+
this.check(path, false);
|
|
84
|
+
const descriptor = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
85
|
+
try {
|
|
86
|
+
const stat = fstatSync(descriptor), uid = process.getuid?.();
|
|
87
|
+
if (!stat.isFile() || stat.nlink !== 1 || stat.size > maxBytes ||
|
|
88
|
+
(uid !== undefined && (stat.uid !== uid || (stat.mode & 0o077) !== 0)))
|
|
89
|
+
return;
|
|
90
|
+
return readFileSync(descriptor, "utf8");
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
closeSync(descriptor);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
if (error.code === "ENOENT")
|
|
98
|
+
return;
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
write(sessionID, reference, original) {
|
|
103
|
+
const path = this.path(sessionID, reference, true), contents = JSON.stringify(original);
|
|
104
|
+
let descriptor;
|
|
105
|
+
try {
|
|
106
|
+
descriptor = openSync(path, "wx", 0o600);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (error.code !== "EEXIST" || this.read(sessionID, reference, Buffer.byteLength(contents)) !== contents)
|
|
110
|
+
throw error;
|
|
111
|
+
// An earlier interrupted attempt may have written identical bytes without
|
|
112
|
+
// finishing its flushes. Complete durability before publishing the plan.
|
|
113
|
+
const existing = openSync(path, constants.O_RDWR | (constants.O_NOFOLLOW ?? 0));
|
|
114
|
+
try {
|
|
115
|
+
fsyncSync(existing);
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
closeSync(existing);
|
|
119
|
+
}
|
|
120
|
+
this.syncDirectory(dirname(path));
|
|
121
|
+
this.syncDirectory(this.root);
|
|
122
|
+
this.syncDirectory(dirname(this.root));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
writeFileSync(descriptor, contents, "utf8");
|
|
127
|
+
fsyncSync(descriptor);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
closeSync(descriptor);
|
|
131
|
+
unlinkSync(path);
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
closeSync(descriptor);
|
|
135
|
+
this.syncDirectory(dirname(path));
|
|
136
|
+
this.syncDirectory(this.root);
|
|
137
|
+
this.syncDirectory(dirname(this.root));
|
|
138
|
+
}
|
|
139
|
+
remove(sessionID, reference) {
|
|
140
|
+
try {
|
|
141
|
+
const path = this.path(sessionID, reference);
|
|
142
|
+
this.check(path, false);
|
|
143
|
+
unlinkSync(path);
|
|
144
|
+
this.syncDirectory(dirname(path));
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (error.code !== "ENOENT")
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Reclaim files from interrupted, uncommitted writes while no writer can race. */
|
|
152
|
+
sweep(known) {
|
|
153
|
+
this.checkRoot();
|
|
154
|
+
try {
|
|
155
|
+
this.check(this.root, true);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (error.code === "ENOENT")
|
|
159
|
+
return;
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
for (const session of readdirSync(this.root)) {
|
|
163
|
+
if (!/^session-[0-9a-f]{64}$/.test(session))
|
|
164
|
+
throw new Error("Unexpected recovery session directory");
|
|
165
|
+
const directory = join(this.root, session);
|
|
166
|
+
this.check(directory, true);
|
|
167
|
+
for (const file of readdirSync(directory)) {
|
|
168
|
+
if (!/^lc1_[0-9a-f]{32}\.json$/.test(file))
|
|
169
|
+
throw new Error("Unexpected recovery payload");
|
|
170
|
+
const path = join(directory, file);
|
|
171
|
+
this.check(path, false);
|
|
172
|
+
if (!known.has(`${session}/${file}`))
|
|
173
|
+
unlinkSync(path);
|
|
174
|
+
}
|
|
175
|
+
if (!readdirSync(directory).length)
|
|
176
|
+
rmdirSync(directory);
|
|
177
|
+
}
|
|
178
|
+
this.syncDirectory(this.root);
|
|
179
|
+
}
|
|
180
|
+
key(sessionID, reference) { return `session-${hashText(sessionID)}/${reference}.json`; }
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=payloads.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payloads.js","sourceRoot":"","sources":["../../src/recovery/payloads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACtK,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAE/D,6EAA6E;AAC7E,MAAM,OAAO,gBAAgB;IACV,WAAW,CAAS;IACpB,eAAe,CAA+B;IAC9C,IAAI,CAAS;IAE9B,YAAY,YAAoB;QAC9B,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;QACzC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACjD,CAAC;IAEO,QAAQ;QACd,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG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AAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7C,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,OAAO,CAAC;gBAAE,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;YACtG,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YAC3C,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;YAC5B,KAAK,MAAM,IAAI,IAAI,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC1C,IAAI,CAAC,0BAA0B,CAAC,IAAI,CAAC,IAAI,CAAC;oBAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;gBAC3F,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBACnC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;gBACxB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,OAAO,IAAI,IAAI,EAAE,CAAC;oBAAE,UAAU,CAAC,IAAI,CAAC,CAAC;YACzD,CAAC;YACD,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,MAAM;gBAAE,SAAS,CAAC,SAAS,CAAC,CAAC;QAC3D,CAAC;QACD,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;IAED,GAAG,CAAC,SAAiB,EAAE,SAAiB,IAAY,OAAO,WAAW,QAAQ,CAAC,SAAS,CAAC,IAAI,SAAS,OAAO,CAAC,CAAC,CAAC;CACjH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function privateArchivePath(base: string): Promise<string>;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { lstat, mkdir, open, readdir, realpath } from "node:fs/promises";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
const runFile = promisify(execFile);
|
|
6
|
+
const filename = "archive.sqlite";
|
|
7
|
+
const archiveFiles = new Set([filename, `${filename}-journal`, `${filename}-wal`, `${filename}-shm`]);
|
|
8
|
+
const sessionDirectory = /^session-[0-9a-f]{64}$/;
|
|
9
|
+
const payloadFile = /^lc1_[0-9a-f]{32}\.json$/;
|
|
10
|
+
// POSIX modes do not restrict other Windows users. Provision and check a private DACL there.
|
|
11
|
+
const secureWindowsDirectory = String.raw `
|
|
12
|
+
$ErrorActionPreference = 'Stop'
|
|
13
|
+
$path = $env:OPENCODE_LIVE_COMPACT_ACL_DIR
|
|
14
|
+
$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().User
|
|
15
|
+
$system = [System.Security.Principal.SecurityIdentifier]::new('S-1-5-18')
|
|
16
|
+
$sidType = [System.Security.Principal.SecurityIdentifier]
|
|
17
|
+
$allow = [System.Security.AccessControl.AccessControlType]::Allow
|
|
18
|
+
|
|
19
|
+
function Read-OwnedAcl($item) {
|
|
20
|
+
if ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) { throw 'Unexpected reparse point' }
|
|
21
|
+
if ($item -is [System.IO.DirectoryInfo]) { $acl = [System.IO.Directory]::GetAccessControl($item.FullName) }
|
|
22
|
+
else { $acl = [System.IO.File]::GetAccessControl($item.FullName) }
|
|
23
|
+
if ($acl.GetOwner($sidType) -ne $user) { throw 'Unexpected archive owner' }
|
|
24
|
+
return $acl
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function Is-Private($acl) {
|
|
28
|
+
$ownerAllowed = $false
|
|
29
|
+
foreach ($rule in $acl.GetAccessRules($true, $true, $sidType)) {
|
|
30
|
+
if ($rule.AccessControlType -ne $allow) { continue }
|
|
31
|
+
if ($rule.IdentityReference -ne $user -and $rule.IdentityReference -ne $system) { return $false }
|
|
32
|
+
if ($rule.IdentityReference -eq $user) { $ownerAllowed = $true }
|
|
33
|
+
}
|
|
34
|
+
return $ownerAllowed
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
$directory = [System.IO.DirectoryInfo]::new($path)
|
|
38
|
+
$acl = Read-OwnedAcl $directory
|
|
39
|
+
if (-not $acl.AreAccessRulesProtected -or -not (Is-Private $acl)) {
|
|
40
|
+
if ($directory.GetFileSystemInfos().Length -ne 0) { throw 'Existing archive directory is not private' }
|
|
41
|
+
$acl = [System.Security.AccessControl.DirectorySecurity]::new()
|
|
42
|
+
$acl.SetOwner($user)
|
|
43
|
+
$acl.SetAccessRuleProtection($true, $false)
|
|
44
|
+
$inherit = [System.Security.AccessControl.InheritanceFlags]'ContainerInherit, ObjectInherit'
|
|
45
|
+
foreach ($sid in @($user, $system)) {
|
|
46
|
+
$rule = [System.Security.AccessControl.FileSystemAccessRule]::new($sid, 'FullControl', $inherit, 'None', $allow)
|
|
47
|
+
$acl.AddAccessRule($rule)
|
|
48
|
+
}
|
|
49
|
+
[System.IO.Directory]::SetAccessControl($path, $acl)
|
|
50
|
+
$acl = Read-OwnedAcl $directory
|
|
51
|
+
}
|
|
52
|
+
if (-not $acl.AreAccessRulesProtected -or -not (Is-Private $acl)) { throw 'Archive directory ACL is not private' }
|
|
53
|
+
|
|
54
|
+
$pending = [System.Collections.Generic.Stack[System.IO.FileSystemInfo]]::new()
|
|
55
|
+
foreach ($file in $directory.GetFileSystemInfos()) { $pending.Push($file) }
|
|
56
|
+
while ($pending.Count -gt 0) {
|
|
57
|
+
$file = $pending.Pop()
|
|
58
|
+
if (-not (Is-Private (Read-OwnedAcl $file))) { throw 'Archive file ACL is not private' }
|
|
59
|
+
if ($file -is [System.IO.DirectoryInfo]) {
|
|
60
|
+
foreach ($child in $file.GetFileSystemInfos()) { $pending.Push($child) }
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
`;
|
|
64
|
+
export async function privateArchivePath(base) {
|
|
65
|
+
const root = resolve(base);
|
|
66
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
67
|
+
const directory = join(await realpath(root), "recovery-v1");
|
|
68
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
69
|
+
const directoryStat = await lstat(directory);
|
|
70
|
+
const uid = process.getuid?.();
|
|
71
|
+
if (!directoryStat.isDirectory() || (uid !== undefined && (directoryStat.uid !== uid || (directoryStat.mode & 0o077) !== 0))) {
|
|
72
|
+
throw new Error("Recovery directory is not private");
|
|
73
|
+
}
|
|
74
|
+
const check = async (path, folder) => {
|
|
75
|
+
const stat = await lstat(path);
|
|
76
|
+
if ((folder ? !stat.isDirectory() : !stat.isFile() || stat.nlink !== 1) ||
|
|
77
|
+
(uid !== undefined && (stat.uid !== uid || (stat.mode & 0o077) !== 0))) {
|
|
78
|
+
throw new Error("Unexpected or non-private recovery entry");
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
for (const entry of await readdir(directory)) {
|
|
82
|
+
const path = join(directory, entry);
|
|
83
|
+
if (archiveFiles.has(entry)) {
|
|
84
|
+
await check(path, false);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (entry !== "sessions")
|
|
88
|
+
throw new Error("Unexpected recovery entry");
|
|
89
|
+
await check(path, true);
|
|
90
|
+
for (const session of await readdir(path)) {
|
|
91
|
+
if (!sessionDirectory.test(session))
|
|
92
|
+
throw new Error("Unexpected recovery session");
|
|
93
|
+
const sessionPath = join(path, session);
|
|
94
|
+
await check(sessionPath, true);
|
|
95
|
+
for (const file of await readdir(sessionPath)) {
|
|
96
|
+
if (!payloadFile.test(file))
|
|
97
|
+
throw new Error("Unexpected recovery payload");
|
|
98
|
+
await check(join(sessionPath, file), false);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (process.platform === "win32") {
|
|
103
|
+
const windowsRoot = process.env.SystemRoot ?? "C:\\Windows";
|
|
104
|
+
await runFile(join(windowsRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe"), ["-NoProfile", "-NonInteractive", "-Command", secureWindowsDirectory], {
|
|
105
|
+
env: { SystemRoot: windowsRoot, OPENCODE_LIVE_COMPACT_ACL_DIR: directory },
|
|
106
|
+
timeout: 5_000,
|
|
107
|
+
windowsHide: true,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const path = join(directory, filename);
|
|
111
|
+
try {
|
|
112
|
+
const file = await open(path, "wx", 0o600);
|
|
113
|
+
await file.close();
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (error.code !== "EEXIST")
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
return path;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=private-directory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"private-directory.js","sourceRoot":"","sources":["../../src/recovery/private-directory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,OAAO,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;AACpC,MAAM,QAAQ,GAAG,gBAAgB,CAAC;AAClC,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,GAAG,QAAQ,UAAU,EAAE,GAAG,QAAQ,MAAM,EAAE,GAAG,QAAQ,MAAM,CAAC,CAAC,CAAC;AACtG,MAAM,gBAAgB,GAAG,wBAAwB,CAAC;AAClD,MAAM,WAAW,GAAG,0BAA0B,CAAC;AAE/C,6FAA6F;AAC7F,MAAM,sBAAsB,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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aAAa,CAAC;QAC5D,MAAM,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,EAAE,gBAAgB,CAAC,EACxF,CAAC,YAAY,EAAE,iBAAiB,EAAE,UAAU,EAAE,sBAAsB,CAAC,EAAE;YACrE,GAAG,EAAE,EAAE,UAAU,EAAE,WAAW,EAAE,6BAA6B,EAAE,SAAS,EAAE;YAC1E,OAAO,EAAE,KAAK;YACd,WAAW,EAAE,IAAI;SAClB,CAAC,CAAC;IACP,CAAC;IAED,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IACvC,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;QAC3C,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAK,KAA+B,CAAC,IAAI,KAAK,QAAQ;YAAE,MAAM,KAAK,CAAC;IACtE,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RecoveryStore } from "./store.ts";
|
|
2
|
+
export declare const maxRecallCodePoints = 2048;
|
|
3
|
+
export type RecallResult = {
|
|
4
|
+
readonly status: "ok";
|
|
5
|
+
readonly reference: string;
|
|
6
|
+
readonly offset: number;
|
|
7
|
+
readonly text: string;
|
|
8
|
+
readonly nextOffset: number | null;
|
|
9
|
+
} | {
|
|
10
|
+
readonly status: "unavailable" | "invalid_range";
|
|
11
|
+
};
|
|
12
|
+
export declare function restore(store: Pick<RecoveryStore, "get">, sessionID: string, reference: string, offset?: number, limit?: number): Promise<RecallResult>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const maxRecallCodePoints = 2_048;
|
|
2
|
+
export async function restore(store, sessionID, reference, offset = 0, limit = maxRecallCodePoints) {
|
|
3
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(limit) || limit < 1) {
|
|
4
|
+
return { status: "invalid_range" };
|
|
5
|
+
}
|
|
6
|
+
const record = await store.get(sessionID, reference).catch(() => undefined);
|
|
7
|
+
if (!record)
|
|
8
|
+
return { status: "unavailable" };
|
|
9
|
+
let codePoints = 0;
|
|
10
|
+
let start = 0;
|
|
11
|
+
let end = 0;
|
|
12
|
+
const pageLength = Math.min(limit, maxRecallCodePoints);
|
|
13
|
+
for (const point of record.original) {
|
|
14
|
+
if (codePoints === offset)
|
|
15
|
+
start = end;
|
|
16
|
+
if (codePoints - offset === pageLength) {
|
|
17
|
+
return { status: "ok", reference, offset, text: record.original.slice(start, end), nextOffset: codePoints };
|
|
18
|
+
}
|
|
19
|
+
codePoints++;
|
|
20
|
+
end += point.length;
|
|
21
|
+
}
|
|
22
|
+
if (offset > codePoints)
|
|
23
|
+
return { status: "invalid_range" };
|
|
24
|
+
return {
|
|
25
|
+
status: "ok",
|
|
26
|
+
reference,
|
|
27
|
+
offset,
|
|
28
|
+
text: offset === codePoints ? "" : record.original.slice(start),
|
|
29
|
+
nextOffset: null,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=restore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restore.js","sourceRoot":"","sources":["../../src/recovery/restore.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAMzC,MAAM,CAAC,KAAK,UAAU,OAAO,CAC3B,KAAiC,EACjC,SAAiB,EACjB,SAAiB,EACjB,MAAM,GAAG,CAAC,EACV,KAAK,GAAG,mBAAmB;IAE3B,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC7F,OAAO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
+
import { type RecoveryIdentity } from "./integrity.ts";
|
|
2
|
+
export interface RecoveryStoreOptions {
|
|
3
|
+
readonly directory: string;
|
|
4
|
+
readonly maxBytes?: number;
|
|
5
|
+
readonly maxRecordBytes?: number;
|
|
6
|
+
readonly maxRecords?: number;
|
|
7
|
+
readonly retentionMs?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface PreserveInput {
|
|
10
|
+
readonly identity: RecoveryIdentity;
|
|
11
|
+
readonly reference: string;
|
|
12
|
+
readonly sourceHash: string;
|
|
13
|
+
readonly checkpoint: string;
|
|
14
|
+
readonly original: string;
|
|
15
|
+
}
|
|
16
|
+
export interface RecoveryPlan extends Omit<PreserveInput, "original"> {
|
|
17
|
+
readonly version: 1;
|
|
18
|
+
readonly active: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface RecoveryRecord extends RecoveryPlan {
|
|
21
|
+
readonly original: string;
|
|
22
|
+
}
|
|
23
|
+
export declare class RecoveryStore {
|
|
24
|
+
private connection;
|
|
25
|
+
private payloads;
|
|
26
|
+
private needsSweep;
|
|
27
|
+
private closed;
|
|
28
|
+
private readonly directory;
|
|
29
|
+
private readonly maxBytes;
|
|
30
|
+
private readonly maxRecordBytes;
|
|
31
|
+
private readonly maxRecords;
|
|
32
|
+
private readonly retentionMs;
|
|
33
|
+
constructor(options: RecoveryStoreOptions);
|
|
34
|
+
preserve(input: PreserveInput): Promise<boolean>;
|
|
35
|
+
/** Preserve the complete admitted batch in one durable transaction, or preserve none. */
|
|
36
|
+
preserveBatch(inputs: readonly PreserveInput[]): Promise<boolean>;
|
|
37
|
+
list(sessionID: string): Promise<readonly RecoveryPlan[]>;
|
|
38
|
+
get(sessionID: string, reference: string): Promise<RecoveryRecord | undefined>;
|
|
39
|
+
/** Read a bounded replacement batch under one lock; validate every payload afresh. */
|
|
40
|
+
getBatch(sessionID: string, references: readonly string[]): Promise<(RecoveryRecord | undefined)[]>;
|
|
41
|
+
/** Validate the payload once before reusing a durable plan in a new runtime. */
|
|
42
|
+
verify(plan: RecoveryPlan): Promise<boolean>;
|
|
43
|
+
/** Pass complete history and only the plans observed when that history was captured. */
|
|
44
|
+
reconcile(sessionID: string, activeReferences: ReadonlySet<string>, observedReferences: ReadonlySet<string>): Promise<void>;
|
|
45
|
+
deleteSession(sessionID: string): Promise<void>;
|
|
46
|
+
close(): Promise<void>;
|
|
47
|
+
private prune;
|
|
48
|
+
private sweep;
|
|
49
|
+
private database;
|
|
50
|
+
private openDatabase;
|
|
51
|
+
}
|