@tomflow/chatloop-extension 0.4.35 → 0.4.36
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/extension/manifest.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// One Native-only V1/V2 Owner mutation lock on macOS.
|
|
2
|
-
// A permanent 0600 marker fences
|
|
3
|
-
//
|
|
2
|
+
// A permanent 0600 marker fences old O_EXCL writers. Darwin does not
|
|
3
|
+
// ship /usr/bin/lockf: the tiny Python wrapper uses flock and execs the
|
|
4
|
+
// existing Node CAS helper while inheriting the same locked descriptor.
|
|
4
5
|
// Never unlink or infer an abandoned legacy lock from its age/PID.
|
|
5
6
|
import {openSync,closeSync,lstatSync,mkdirSync,readFileSync,writeFileSync,
|
|
6
7
|
fsyncSync,unlinkSync} from 'node:fs';
|
|
@@ -13,6 +14,7 @@ const fail=code=>Object.assign(Error(code),{code});
|
|
|
13
14
|
const marker='CHAT_LOOP_OWNER_KERNEL_LOCK_V1\n';
|
|
14
15
|
const path=()=>join(homedir(),'.chat-loop','session','owner-registry.lock');
|
|
15
16
|
const helper=fileURLToPath(new URL('./owner-locked-write.mjs',import.meta.url));
|
|
17
|
+
const lockExec=fileURLToPath(new URL('./owner-lock-exec.py',import.meta.url));
|
|
16
18
|
const stat=p=>{try{return lstatSync(p)}catch(e){if(e.code==='ENOENT')return null;throw e;}};
|
|
17
19
|
const UUID=/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i;
|
|
18
20
|
const terminal=s=>['DONE','CANCELLED'].includes(s?.phase);
|
|
@@ -84,15 +86,15 @@ export function withOwnerKernelLock(kind,args){
|
|
|
84
86
|
if(!['legacy-write','scoped-write','scoped-retire'].includes(kind)
|
|
85
87
|
||!Array.isArray(args))throw fail('OWNER_LOCK_REQUEST_INVALID');
|
|
86
88
|
const file=ensureMarker();
|
|
87
|
-
const result=spawnSync('/usr/bin/
|
|
88
|
-
['-
|
|
89
|
+
const result=spawnSync('/usr/bin/python3',
|
|
90
|
+
['-B',lockExec,file,process.execPath,helper],{
|
|
89
91
|
// Do not kill lockf after acquisition: its child could outlive it
|
|
90
92
|
// and keep writing without the parent-held kernel lock.
|
|
91
93
|
input:JSON.stringify({kind,args}),encoding:'utf8',
|
|
92
94
|
maxBuffer:512*1024
|
|
93
95
|
});
|
|
94
|
-
// A crashed helper may already have committed its atomic rename.
|
|
95
|
-
// Only
|
|
96
|
+
// A crashed CAS helper may already have committed its atomic rename.
|
|
97
|
+
// Only exit 75 (verified flock contention before exec) proves no write.
|
|
96
98
|
if(result.status===75)throw fail('OWNER_LEDGER_LOCKED');
|
|
97
99
|
if(result.error||result.status!==0||result.signal)
|
|
98
100
|
throw fail('OWNER_WRITE_RECEIPT_UNKNOWN');
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#!/usr/bin/python3
|
|
2
|
+
"""Hold one Darwin BSD kernel flock across exec of the existing Node CAS helper.
|
|
3
|
+
|
|
4
|
+
No Session parsing or mutation lives here. The same file descriptor must
|
|
5
|
+
survive exec, so a crashed process releases the kernel lock automatically.
|
|
6
|
+
"""
|
|
7
|
+
import errno
|
|
8
|
+
import fcntl
|
|
9
|
+
import os
|
|
10
|
+
import stat
|
|
11
|
+
import sys
|
|
12
|
+
import time
|
|
13
|
+
|
|
14
|
+
MARKER = b"CHAT_LOOP_OWNER_KERNEL_LOCK_V1\n"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def main():
|
|
18
|
+
if len(sys.argv) != 4:
|
|
19
|
+
return 64
|
|
20
|
+
lock_path, node_binary, helper_path = sys.argv[1:]
|
|
21
|
+
fd = os.open(lock_path, os.O_RDWR | os.O_NOFOLLOW)
|
|
22
|
+
meta = os.fstat(fd)
|
|
23
|
+
if (not stat.S_ISREG(meta.st_mode) or meta.st_uid != os.getuid()
|
|
24
|
+
or stat.S_IMODE(meta.st_mode) != 0o600 or meta.st_nlink != 1
|
|
25
|
+
or os.read(fd, len(MARKER) + 1) != MARKER):
|
|
26
|
+
return 65
|
|
27
|
+
deadline = time.monotonic() + 2.0
|
|
28
|
+
while True:
|
|
29
|
+
try:
|
|
30
|
+
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
31
|
+
break
|
|
32
|
+
except OSError as error:
|
|
33
|
+
if error.errno not in (errno.EWOULDBLOCK, errno.EAGAIN, errno.EACCES):
|
|
34
|
+
raise
|
|
35
|
+
if time.monotonic() >= deadline:
|
|
36
|
+
return 75
|
|
37
|
+
time.sleep(0.05)
|
|
38
|
+
# PEP 446 makes descriptors close-on-exec by default. Explicitly keep
|
|
39
|
+
# this one FD open in Node; never fork a lock owner separate from the CAS.
|
|
40
|
+
os.set_inheritable(fd, True)
|
|
41
|
+
os.execv(node_binary, [node_binary, helper_path])
|
|
42
|
+
return 66
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
if __name__ == "__main__":
|
|
46
|
+
try:
|
|
47
|
+
sys.exit(main())
|
|
48
|
+
except (OSError, ValueError):
|
|
49
|
+
sys.exit(66)
|