@anhlp/alp-cli 0.0.1-beta.4 → 0.0.1-beta.7
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/dist/THIRD-PARTY-NOTICES.txt +26 -1
- package/dist/acp/main.js +1 -1
- package/dist/acp.js +152 -33
- package/dist/alp-web/THIRD-PARTY-NOTICES.txt +666 -0
- package/dist/alp-web/alp-boundary.json +1 -0
- package/dist/alp-web/assets/TerminalPanel-BeMws9CP.js +22 -0
- package/dist/alp-web/assets/highlighter-BnicKQ_d.js +18 -0
- package/dist/alp-web/assets/index-Dl7motn1.js +59 -0
- package/dist/alp-web/assets/index-DwqDSiIi.css +1 -0
- package/dist/alp-web/index.html +2 -2
- package/dist/alp-web.js +529 -172
- package/dist/alpd.js +10623 -2570
- package/dist/backup.js +1 -1
- package/dist/build-manifest.json +22 -2
- package/dist/cli.js +28 -3
- package/dist/daemon.d.ts +1 -1
- package/dist/daemon.js +5 -0
- package/dist/interfaces.d.ts +13 -1
- package/dist/interfaces.js +95 -11
- package/dist/paths.d.ts +1 -0
- package/dist/paths.js +2 -1
- package/dist/roles.d.ts +15 -0
- package/dist/roles.js +16 -0
- package/dist/self-update.d.ts +149 -0
- package/dist/self-update.js +399 -0
- package/dist/sessions.d.ts +4 -0
- package/dist/sessions.js +22 -1
- package/dist/tasks.js +22 -8
- package/dist/terminal-native/README.md +201 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-arm64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/darwin-x64/manifest.json +11 -0
- package/dist/terminal-native/prebuilt/darwin-x64/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-arm64-musl/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-glibc/terminal-owner +0 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/manifest.json +10 -0
- package/dist/terminal-native/prebuilt/linux-x64-musl/terminal-owner +0 -0
- package/dist/terminal-native/terminal-owner.c +389 -0
- package/dist/terminal.d.ts +5 -0
- package/dist/terminal.js +4 -3
- package/dist/usage.d.ts +1 -1
- package/dist/usage.js +14 -5
- package/dist/web-service.d.ts +22 -4
- package/dist/web-service.js +32 -8
- package/package.json +6 -5
- package/src/cli.js +35 -4
- package/src/daemon.js +3 -0
- package/src/interfaces.js +101 -12
- package/src/paths.js +3 -1
- package/src/roles.js +26 -0
- package/src/self-update.js +406 -0
- package/src/sessions.js +23 -2
- package/src/tasks.js +22 -8
- package/src/terminal.js +4 -3
- package/src/usage.js +14 -5
- package/src/web-service.js +56 -13
- package/dist/alp-web/assets/TerminalPanel-PrU2l3u5.js +0 -16
- package/dist/alp-web/assets/index-BJdBTDFX.css +0 -1
- package/dist/alp-web/assets/index-DpfiPv5s.js +0 -52
- package/src/web/grant.js +0 -204
- package/src/web/main.js +0 -285
- package/src/web/serve.js +0 -144
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/* ALP terminal owner protocol 1. Native-only spawn, lifetime acquisition, signals and reap.
|
|
2
|
+
* Linux reduced-protection mode signals only its continuously owned unreaped root. See README.md for proof obligations and platform limits. */
|
|
3
|
+
#define _GNU_SOURCE
|
|
4
|
+
#include <sys/types.h>
|
|
5
|
+
#include <sys/wait.h>
|
|
6
|
+
#include <sys/ioctl.h>
|
|
7
|
+
#include <sys/stat.h>
|
|
8
|
+
#include <poll.h>
|
|
9
|
+
#include <fcntl.h>
|
|
10
|
+
#include <signal.h>
|
|
11
|
+
#include <stdint.h>
|
|
12
|
+
#include <stdio.h>
|
|
13
|
+
#include <stdlib.h>
|
|
14
|
+
#include <string.h>
|
|
15
|
+
#include <errno.h>
|
|
16
|
+
#include <unistd.h>
|
|
17
|
+
#include <time.h>
|
|
18
|
+
#ifdef __APPLE__
|
|
19
|
+
#include <util.h>
|
|
20
|
+
#include <libproc.h>
|
|
21
|
+
#include <mach/mach.h>
|
|
22
|
+
#include <mach/mach_traps.h>
|
|
23
|
+
#include <mach/task_info.h>
|
|
24
|
+
/* Approved private ABI, XNU xnu-12377.1.9 bsd/sys/proc_info_private.h, flavor17.
|
|
25
|
+
* Keep the complete 56-byte layout; parent identity is NEVER adoption authority. */
|
|
26
|
+
struct uniqinfo { uint8_t uuid[16]; uint64_t unique, parent; int32_t version, original_parent_version; uint64_t reserved[2]; };
|
|
27
|
+
_Static_assert(sizeof(struct uniqinfo) == 56, "private ABI size");
|
|
28
|
+
#else
|
|
29
|
+
#include <pty.h>
|
|
30
|
+
#include <dirent.h>
|
|
31
|
+
#include <sys/syscall.h>
|
|
32
|
+
#include <sys/vfs.h>
|
|
33
|
+
#include <linux/magic.h>
|
|
34
|
+
#endif
|
|
35
|
+
#define MAX_TRACKED 4096
|
|
36
|
+
#define QUEUE (1024 * 1024)
|
|
37
|
+
#ifndef START_TIMEOUT_MS
|
|
38
|
+
#define START_TIMEOUT_MS 3000
|
|
39
|
+
#endif
|
|
40
|
+
typedef struct {
|
|
41
|
+
pid_t pid;
|
|
42
|
+
#ifdef __APPLE__
|
|
43
|
+
uint64_t unique;
|
|
44
|
+
#else
|
|
45
|
+
int fd;
|
|
46
|
+
#endif
|
|
47
|
+
} identity;
|
|
48
|
+
static identity known[MAX_TRACKED];
|
|
49
|
+
static size_t count;
|
|
50
|
+
static pid_t root;
|
|
51
|
+
static int scanning_failed, held = 1;
|
|
52
|
+
#ifndef __APPLE__
|
|
53
|
+
static int root_only, compatibility_available;
|
|
54
|
+
/* Exclusive natural parent retains this child (live or WNOWAIT zombie).
|
|
55
|
+
* ECHILD revokes authority permanently; never infer ownership from a PID lookup. */
|
|
56
|
+
static int root_status(void) {
|
|
57
|
+
if (!held || root <= 1) return 2;
|
|
58
|
+
siginfo_t info = {0}; int result;
|
|
59
|
+
do { result=waitid(P_PID,root,&info,WEXITED|WNOHANG|WNOWAIT); } while(result<0 && errno==EINTR);
|
|
60
|
+
if(result<0) { if(errno==ECHILD) held=0; return 2; }
|
|
61
|
+
return info.si_pid==root ? 0 : 1;
|
|
62
|
+
}
|
|
63
|
+
#endif
|
|
64
|
+
static long long clock_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (long long)t.tv_sec*1000+t.tv_nsec/1000000; }
|
|
65
|
+
static void event(const char *s) { (void)!write(3, s, strlen(s)); }
|
|
66
|
+
#ifdef __APPLE__
|
|
67
|
+
static int unique(pid_t p, struct uniqinfo *u) { memset(u, 0, sizeof(*u)); errno = 0; return proc_pidinfo(p, 17, 0, u, sizeof(*u)) == sizeof(*u) && u->unique != 0; }
|
|
68
|
+
static int token(pid_t p, audit_token_t *t) {
|
|
69
|
+
mach_port_t task = MACH_PORT_NULL;
|
|
70
|
+
if (task_name_for_pid(mach_task_self(), p, &task) != KERN_SUCCESS) return 0;
|
|
71
|
+
mach_msg_type_number_t n = TASK_AUDIT_TOKEN_COUNT;
|
|
72
|
+
kern_return_t r = task_info(task, TASK_AUDIT_TOKEN, (task_info_t)t, &n);
|
|
73
|
+
mach_port_deallocate(mach_task_self(), task);
|
|
74
|
+
return r == KERN_SUCCESS && n == TASK_AUDIT_TOKEN_COUNT && t->val[5] == (uint32_t)p;
|
|
75
|
+
}
|
|
76
|
+
static int acquire(pid_t p, identity *out, int session) {
|
|
77
|
+
struct uniqinfo a, b; audit_token_t t;
|
|
78
|
+
if (!held || !unique(p, &a)) return 0;
|
|
79
|
+
if (session && getsid(p) != root) return 0;
|
|
80
|
+
if (!token(p, &t) || !unique(p, &b) || a.unique != b.unique || t.val[7] != (uint32_t)b.version) return 0;
|
|
81
|
+
out->pid = p; out->unique = a.unique; return 1;
|
|
82
|
+
}
|
|
83
|
+
/* 0 exited, 1 live, 2 uncertain. Never infer live from a failed task-port lookup. */
|
|
84
|
+
static int status(identity *id) {
|
|
85
|
+
if (id->pid == root) { siginfo_t info = {0}; if (waitid(P_PID, root, &info, WEXITED|WNOHANG|WNOWAIT) == 0 && info.si_pid == root) return 0; }
|
|
86
|
+
struct uniqinfo u; if (!unique(id->pid, &u)) return errno == ESRCH ? 0 : 2;
|
|
87
|
+
if (u.unique != id->unique) return 0;
|
|
88
|
+
struct proc_bsdinfo b;
|
|
89
|
+
if (proc_pidinfo(id->pid, PROC_PIDTBSDINFO, 0, &b, sizeof(b)) != sizeof(b)) return 2;
|
|
90
|
+
struct uniqinfo after; if (!unique(id->pid, &after)) return errno == ESRCH ? 0 : 2;
|
|
91
|
+
if (after.unique != id->unique) return 0;
|
|
92
|
+
return b.pbi_status == 5 /* SZOMB */ ? 0 : 1;
|
|
93
|
+
}
|
|
94
|
+
static void bound_signal(identity *id, int sig) {
|
|
95
|
+
for (int tries = 0; tries < 4; tries++) {
|
|
96
|
+
identity fresh; audit_token_t t; struct uniqinfo u;
|
|
97
|
+
if (!acquire(id->pid, &fresh, 0) || fresh.unique != id->unique || !token(id->pid, &t) || !unique(id->pid, &u) || u.unique != id->unique || t.val[7] != (uint32_t)u.version) continue;
|
|
98
|
+
int r = proc_signal_with_audittoken(&t, sig);
|
|
99
|
+
if (r == 0 || r != ESRCH) return;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
static void remember(pid_t p) {
|
|
103
|
+
identity id;
|
|
104
|
+
if (!acquire(p, &id, 1)) return;
|
|
105
|
+
for (size_t i=0; i<count; i++) if (known[i].unique == id.unique) return;
|
|
106
|
+
if (count == MAX_TRACKED) { scanning_failed = 1; return; }
|
|
107
|
+
known[count++] = id;
|
|
108
|
+
}
|
|
109
|
+
static void discover(void) {
|
|
110
|
+
if (!held) return;
|
|
111
|
+
int bytes = proc_listallpids(NULL, 0);
|
|
112
|
+
if (bytes <= 0 || bytes > 1024*1024) { scanning_failed = 1; return; }
|
|
113
|
+
/* proc_listallpids(NULL) returns estimated process COUNT, not bytes. */
|
|
114
|
+
size_t capacity = (size_t)bytes + 256;
|
|
115
|
+
pid_t *pids = calloc(capacity, sizeof(pid_t));
|
|
116
|
+
if (!pids) { scanning_failed = 1; return; }
|
|
117
|
+
int n = proc_listallpids(pids, (int)(capacity*sizeof(pid_t)));
|
|
118
|
+
if (n < 0 || (size_t)n >= capacity) scanning_failed = 1;
|
|
119
|
+
else for (int i=0; i<n; i++) if (pids[i]>1) remember(pids[i]);
|
|
120
|
+
free(pids);
|
|
121
|
+
}
|
|
122
|
+
static int capability(void) {
|
|
123
|
+
struct uniqinfo a, b; audit_token_t t;
|
|
124
|
+
return unique(getpid(), &a) && token(getpid(), &t) && unique(getpid(), &b) && a.unique == b.unique && t.val[7] == (uint32_t)b.version;
|
|
125
|
+
}
|
|
126
|
+
#else
|
|
127
|
+
static int pid_signal(int fd, int sig) { return (int)syscall(SYS_pidfd_send_signal, fd, sig, NULL, 0); }
|
|
128
|
+
static int proc_stat(int dir, pid_t *sid, char *state) {
|
|
129
|
+
int f = openat(dir, "stat", O_RDONLY|O_CLOEXEC); if (f < 0) return 0;
|
|
130
|
+
char buf[8192]; ssize_t n = read(f, buf, sizeof(buf)-1); int saved=errno; close(f); errno=saved;
|
|
131
|
+
if (n <= 0) { if(n==0) errno=EIO; return 0; } buf[n]=0;
|
|
132
|
+
char *end = strrchr(buf, ')'); int pp, pg, s;
|
|
133
|
+
if (!end || sscanf(end+1, " %c %d %d %d", state, &pp, &pg, &s) != 4) { errno=EIO; return 0; }
|
|
134
|
+
*sid = (pid_t)s; return 1;
|
|
135
|
+
}
|
|
136
|
+
static int acquire(pid_t p, identity *out, int session) {
|
|
137
|
+
if(root_only) { if(p!=root || root_status()==2) return 0; out->pid=root; out->fd=-1; return 1; }
|
|
138
|
+
if (!held) return 0;
|
|
139
|
+
char name[64]; snprintf(name, sizeof(name), "/proc/%d", p);
|
|
140
|
+
int f=open(name, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); if (f<0) return 0;
|
|
141
|
+
pid_t sid; char state;
|
|
142
|
+
if (!proc_stat(f, &sid, &state) || (session && sid != root)) { close(f); return 0; }
|
|
143
|
+
out->pid=p; out->fd=f; return 1;
|
|
144
|
+
}
|
|
145
|
+
static int status(identity *id) {
|
|
146
|
+
if(root_only) return id->pid==root ? root_status() : 2;
|
|
147
|
+
pid_t sid; char state;
|
|
148
|
+
if (!proc_stat(id->fd, &sid, &state)) return errno == ENOENT || errno == ESRCH ? 0 : 2;
|
|
149
|
+
return state == 'Z' || state == 'X' ? 0 : 1;
|
|
150
|
+
}
|
|
151
|
+
static void bound_signal(identity *id, int sig) {
|
|
152
|
+
if(root_only) {
|
|
153
|
+
if(id->pid!=root || root_status()!=1) return;
|
|
154
|
+
/* No reap between waitid and kill: root PID remains continuously reserved. */
|
|
155
|
+
int r; do { r=kill(root,sig); } while(r<0 && errno==EINTR);
|
|
156
|
+
if(r<0 && errno!=ESRCH) scanning_failed=1;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
(void)pid_signal(id->fd, sig);
|
|
160
|
+
}
|
|
161
|
+
static void remember(pid_t p) {
|
|
162
|
+
identity id;
|
|
163
|
+
if (!acquire(p, &id, 1)) return;
|
|
164
|
+
struct stat st, old; if (fstat(id.fd,&st)) { close(id.fd); scanning_failed=1; return; }
|
|
165
|
+
for (size_t i=0;i<count;i++) if (!fstat(known[i].fd,&old) && old.st_dev==st.st_dev && old.st_ino==st.st_ino) { close(id.fd); return; }
|
|
166
|
+
if (count==MAX_TRACKED) { scanning_failed=1; close(id.fd); return; }
|
|
167
|
+
known[count++]=id;
|
|
168
|
+
}
|
|
169
|
+
static void discover(void) {
|
|
170
|
+
if (root_only || !held) return;
|
|
171
|
+
DIR *d=opendir("/proc"); if (!d) { scanning_failed=1; return; }
|
|
172
|
+
struct dirent *e;
|
|
173
|
+
while ((e=readdir(d))) { char *end; long p=strtol(e->d_name,&end,10); if (*e->d_name && !*end && p>1 && p<=INT32_MAX) remember((pid_t)p); }
|
|
174
|
+
closedir(d);
|
|
175
|
+
}
|
|
176
|
+
static int capability(void) {
|
|
177
|
+
compatibility_available=0;
|
|
178
|
+
/* Reject a proc mount in a different PID namespace (including numeric aliases).
|
|
179
|
+
* The mount must be kernel procfs; /proc/self must report our local PID and SID. */
|
|
180
|
+
int self=open("/proc/self",O_RDONLY|O_DIRECTORY|O_CLOEXEC);
|
|
181
|
+
if(self<0) return 0;
|
|
182
|
+
struct statfs fs; pid_t sid; char state;
|
|
183
|
+
int statfd=openat(self,"stat",O_RDONLY|O_CLOEXEC); char buf[128]={0};
|
|
184
|
+
ssize_t n=statfd<0?-1:read(statfd,buf,sizeof(buf)-1);
|
|
185
|
+
if(statfd>=0) close(statfd);
|
|
186
|
+
/* NSpid lists PIDs from the procfs PID namespace down to ours: exactly one field
|
|
187
|
+
* proves this procfs is our active namespace (ancestor procfs => more fields;
|
|
188
|
+
* unrelated procfs => no /proc/self). Needs no access to PID1; missing => deny. */
|
|
189
|
+
int nspid_ok=0; int statusfd=openat(self,"status",O_RDONLY|O_CLOEXEC);
|
|
190
|
+
if(statusfd>=0) {
|
|
191
|
+
static char st[16384]; size_t len=0; ssize_t k;
|
|
192
|
+
while(len<sizeof(st)-1 && ((k=read(statusfd,st+len,sizeof(st)-1-len))>0 || (k<0 && errno==EINTR))) if(k>0) len+=(size_t)k;
|
|
193
|
+
close(statusfd); st[len]=0;
|
|
194
|
+
char *line=strstr(st,"\nNSpid:");
|
|
195
|
+
if(line && len<sizeof(st)-1) {
|
|
196
|
+
line+=7; char *eol=strchr(line,'\n');
|
|
197
|
+
if(eol) { *eol=0; int fields=0; long v=0; char *q=line, *end;
|
|
198
|
+
for(;;) { while(*q==' '||*q=='\t') q++; if(!*q) break; errno=0; long x=strtol(q,&end,10); if(end==q||errno||(*end&&*end!=' '&&*end!='\t')) { fields=-1; break; } v=x; fields++; q=end; }
|
|
199
|
+
nspid_ok=fields==1 && v==getpid(); }
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
char link[32]; ssize_t ln=readlink("/proc/self",link,sizeof(link)-1);
|
|
203
|
+
int link_ok=ln>0 && (link[ln]=0, strtol(link,NULL,10)==getpid());
|
|
204
|
+
int namespace_ok=nspid_ok && link_ok && n>0 && strtol(buf,NULL,10)==getpid() && !fstatfs(self,&fs) && fs.f_type==PROC_SUPER_MAGIC && proc_stat(self,&sid,&state) && sid==getsid(0);
|
|
205
|
+
close(self); if(!namespace_ok) return 0;
|
|
206
|
+
identity id; pid_t saved=root; root=getsid(0);
|
|
207
|
+
int ok=acquire(getpid(), &id, 1); root=saved;
|
|
208
|
+
if (!ok) return 0;
|
|
209
|
+
int r=pid_signal(id.fd, 0); close(id.fd);
|
|
210
|
+
return r==0;
|
|
211
|
+
}
|
|
212
|
+
#endif
|
|
213
|
+
static void signal_all(int sig) { for (size_t i=0;i<count;i++) if (status(&known[i]) != 0) bound_signal(&known[i], sig); }
|
|
214
|
+
static void nonblock(int f) { int flags=fcntl(f,F_GETFL); if(flags>=0) fcntl(f,F_SETFL,flags|O_NONBLOCK); }
|
|
215
|
+
static void close_extra(void) {
|
|
216
|
+
#ifdef __APPLE__
|
|
217
|
+
int n = getdtablesize();
|
|
218
|
+
for(int i=3;i<n;i++) close(i);
|
|
219
|
+
#else
|
|
220
|
+
long n=sysconf(_SC_OPEN_MAX); if(n<0) n=65536;
|
|
221
|
+
for(int i=3;i<n;i++) close(i);
|
|
222
|
+
#endif
|
|
223
|
+
}
|
|
224
|
+
/* Startup failure owns the unreaped root but must never wait indefinitely.
|
|
225
|
+
* Gate EOF prevents exec. All optional signals still use a verified identity. */
|
|
226
|
+
static int abort_start(int master, int gate) {
|
|
227
|
+
close(gate);
|
|
228
|
+
identity id;
|
|
229
|
+
if(acquire(root,&id,0)) {
|
|
230
|
+
bound_signal(&id,SIGKILL);
|
|
231
|
+
#ifndef __APPLE__
|
|
232
|
+
if(id.fd>=0) close(id.fd);
|
|
233
|
+
#endif
|
|
234
|
+
}
|
|
235
|
+
long long deadline=clock_ms()+250; int reaped=0;
|
|
236
|
+
do {
|
|
237
|
+
siginfo_t info={0};
|
|
238
|
+
if(waitid(P_PID,root,&info,WEXITED|WNOHANG|WNOWAIT)==0 && info.si_pid==root) { int r; do { r=waitpid(root,NULL,WNOHANG); } while(r<0 && errno==EINTR); reaped=r==root; break; }
|
|
239
|
+
usleep(1000);
|
|
240
|
+
} while(clock_ms()<deadline);
|
|
241
|
+
held=0; close(master);
|
|
242
|
+
event("{\"error\":\"native_identity_unavailable\"}\n");
|
|
243
|
+
char message[160]; snprintf(message,sizeof(message),"{\"cleanup\":true,\"observed\":1,\"live\":0,\"uncertain\":%d}\n",reaped?0:1); event(message);
|
|
244
|
+
return 78;
|
|
245
|
+
}
|
|
246
|
+
/* A termination signal (e.g. one sent to the daemon's process group) must start
|
|
247
|
+
* bound cleanup, not kill the owner before it reaps the session it owns. */
|
|
248
|
+
static volatile sig_atomic_t stop_requested=0;
|
|
249
|
+
static void request_stop(int sig) { (void)sig; stop_requested=1; }
|
|
250
|
+
int main(int argc, char **argv) {
|
|
251
|
+
signal(SIGPIPE,SIG_IGN);
|
|
252
|
+
struct sigaction stop_action; memset(&stop_action,0,sizeof(stop_action));
|
|
253
|
+
stop_action.sa_handler=request_stop; sigemptyset(&stop_action.sa_mask);
|
|
254
|
+
if(sigaction(SIGTERM,&stop_action,NULL) || sigaction(SIGINT,&stop_action,NULL) || sigaction(SIGHUP,&stop_action,NULL)) return 71;
|
|
255
|
+
struct sigaction child_action; memset(&child_action,0,sizeof(child_action));
|
|
256
|
+
child_action.sa_handler=SIG_DFL; sigemptyset(&child_action.sa_mask);
|
|
257
|
+
if(sigaction(SIGCHLD,&child_action,NULL)) return 71;
|
|
258
|
+
int capable=capability();
|
|
259
|
+
#ifndef __APPLE__
|
|
260
|
+
/* Linux is best-effort (user decision 2026-10-29): when identity-bound protection is
|
|
261
|
+
* unavailable for any reason, offer root-only compatibility, which only signals the
|
|
262
|
+
* unreaped session root and never trusts procfs or pidfd. The caller discloses it. */
|
|
263
|
+
compatibility_available=!capable;
|
|
264
|
+
#endif
|
|
265
|
+
#ifndef __APPLE__
|
|
266
|
+
if(argc==2 && !strcmp(argv[1],"--capability") && compatibility_available) { puts("alp-terminal-owner:1:root-only"); return 0; }
|
|
267
|
+
if(argc>1 && !strcmp(argv[1],"--root-only")) { root_only=1; argc--; argv++; }
|
|
268
|
+
if(argc>=6 && ((root_only && !compatibility_available) || (!root_only && !capable && compatibility_available))) {
|
|
269
|
+
event(!root_only ? "{\"refused\":\"terminal_protection_ack_required\"}\n" : "{\"refused\":\"terminal_unsupported\"}\n");
|
|
270
|
+
event("{\"cleanup\":true,\"observed\":0,\"live\":0,\"uncertain\":0}\n"); return 78;
|
|
271
|
+
}
|
|
272
|
+
#endif
|
|
273
|
+
if (argc==2 && !strcmp(argv[1],"--capability")) { if (!capable) return 78; puts("alp-terminal-owner:1"); return 0; }
|
|
274
|
+
if (argc<6 || (!capable
|
|
275
|
+
#ifndef __APPLE__
|
|
276
|
+
&& !(root_only && compatibility_available)
|
|
277
|
+
#endif
|
|
278
|
+
)) { event("{\"refused\":\"terminal_unsupported\"}\n"); event("{\"cleanup\":true,\"observed\":0,\"live\":0,\"uncertain\":0}\n"); return 78; }
|
|
279
|
+
int cols=atoi(argv[1]),rows=atoi(argv[2]),grace=atoi(argv[3]);
|
|
280
|
+
if(cols<1 || cols>1000 || rows<1 || rows>1000 || grace<0 || grace>10000) return 64;
|
|
281
|
+
unsigned char *out=malloc(QUEUE),*input=malloc(QUEUE);
|
|
282
|
+
if(!out || !input) { free(out); free(input); return 71; }
|
|
283
|
+
int gate[2],started[2]; if(pipe(gate)||pipe(started)) return 71;
|
|
284
|
+
struct winsize ws={.ws_col=(unsigned short)cols,.ws_row=(unsigned short)rows}; int master;
|
|
285
|
+
root=forkpty(&master,NULL,NULL,&ws);
|
|
286
|
+
if (root<0) return 71;
|
|
287
|
+
if (!root) {
|
|
288
|
+
/* The owner's stop handlers must not swallow a hang-up before exec. */
|
|
289
|
+
signal(SIGTERM,SIG_DFL); signal(SIGINT,SIG_DFL); signal(SIGHUP,SIG_DFL);
|
|
290
|
+
close(gate[1]); close(started[0]);
|
|
291
|
+
char ready=getsid(0)==getpid()?'S':'E';
|
|
292
|
+
if(write(started[1],&ready,1)!=1) _exit(126);
|
|
293
|
+
char release=0; if(read(gate[0],&release,1)!=1 || release!='G' || ready!='S') _exit(126);
|
|
294
|
+
close_extra(); signal(SIGPIPE,SIG_DFL);
|
|
295
|
+
if(chdir(argv[4])) _exit(126);
|
|
296
|
+
execv(argv[5],&argv[5]); _exit(127);
|
|
297
|
+
}
|
|
298
|
+
close(gate[0]); close(started[1]); char ready=0;
|
|
299
|
+
struct pollfd startup[3]={{started[0],POLLIN,0},{0,0,0},{4,POLLIN,0}};
|
|
300
|
+
int available=poll(startup,3,START_TIMEOUT_MS);
|
|
301
|
+
int startok=available>0 && !(startup[1].revents&(POLLHUP|POLLERR)) && !(startup[2].revents&(POLLIN|POLLHUP|POLLERR)) && (startup[0].revents&POLLIN) && read(started[0],&ready,1)==1 && ready=='S';
|
|
302
|
+
close(started[0]);
|
|
303
|
+
identity first;
|
|
304
|
+
if(!startok || !acquire(root,&first,1)) return abort_start(master,gate[1]);
|
|
305
|
+
known[count++]=first;
|
|
306
|
+
if(write(gate[1],"G",1)!=1) return abort_start(master,gate[1]);
|
|
307
|
+
close(gate[1]);
|
|
308
|
+
char msg[256]; snprintf(msg,sizeof(msg),"{\"ready\":%d,\"protocol\":1}\n",root); event(msg);
|
|
309
|
+
nonblock(master); nonblock(0); nonblock(1); nonblock(4);
|
|
310
|
+
size_t outn=0,inn=0; char commands[256]; size_t cn=0;
|
|
311
|
+
int cleaning=0, root_notified=0, code=0, sig=0, io_failed=0; long long start=0, lastscan=0;
|
|
312
|
+
for (;;) {
|
|
313
|
+
long long now=clock_ms();
|
|
314
|
+
if(now-lastscan>=20) { discover(); lastscan=now; }
|
|
315
|
+
siginfo_t info={0};
|
|
316
|
+
if(!root_notified && waitid(P_PID,root,&info,WEXITED|WNOHANG|WNOWAIT)==0 && info.si_pid==root) {
|
|
317
|
+
root_notified=1; code=info.si_code==CLD_EXITED?info.si_status:0; sig=info.si_code==CLD_EXITED?0:info.si_status;
|
|
318
|
+
snprintf(msg,sizeof(msg),"{\"exitCode\":%d,\"signal\":%d}\n",code,sig); event(msg); cleaning=cleaning?cleaning:1;
|
|
319
|
+
}
|
|
320
|
+
if((io_failed || stop_requested) && !cleaning) cleaning=1;
|
|
321
|
+
if(cleaning==1) { start=now; discover(); signal_all(SIGHUP); cleaning=2; }
|
|
322
|
+
if(cleaning>=2 && now-start>=grace) { discover(); signal_all(SIGKILL); cleaning=3; }
|
|
323
|
+
if(cleaning==3 && now-start>=grace+250) break;
|
|
324
|
+
struct pollfd fds[4]={{0,inn<QUEUE?POLLIN:0,0},{master,(short)((outn<QUEUE?POLLIN:0)|(inn?POLLOUT:0)),0},{1,outn?POLLOUT:0,0},{4,POLLIN,0}};
|
|
325
|
+
if(poll(fds,4,10)<0 && errno!=EINTR) { io_failed=1; continue; }
|
|
326
|
+
if(fds[0].revents&POLLIN) { ssize_t n=read(0,input+inn,QUEUE-inn); if(n>0) inn+=(size_t)n; else if(n==0) cleaning=cleaning?cleaning:1; }
|
|
327
|
+
if(fds[0].revents&(POLLHUP|POLLERR)) cleaning=cleaning?cleaning:1;
|
|
328
|
+
if(fds[1].revents&POLLIN) { ssize_t n=read(master,out+outn,QUEUE-outn); if(n>0) outn+=(size_t)n; }
|
|
329
|
+
if(fds[1].revents&POLLOUT) { ssize_t n=write(master,input,inn); if(n>0) {inn-=(size_t)n;memmove(input,input+n,inn);} }
|
|
330
|
+
if(fds[2].revents&POLLOUT) { ssize_t n=write(1,out,outn); if(n>0) {outn-=(size_t)n;memmove(out,out+n,outn);} else if(errno!=EAGAIN&&errno!=EINTR) io_failed=1; }
|
|
331
|
+
if(fds[2].revents&(POLLHUP|POLLERR)) io_failed=1;
|
|
332
|
+
if(fds[3].revents&POLLIN) {
|
|
333
|
+
char b[128]; ssize_t n=read(4,b,sizeof(b));
|
|
334
|
+
for(ssize_t i=0;i<n;i++) { if(b[i]=='\n') { commands[cn]=0; int c,r; if(!strcmp(commands,"C")) cleaning=cleaning?cleaning:1; else if(sscanf(commands,"R %d %d",&c,&r)==2 && c>0&&c<=1000&&r>0&&r<=1000) { ws.ws_col=(unsigned short)c;ws.ws_row=(unsigned short)r; ioctl(master,TIOCSWINSZ,&ws); } cn=0; } else if(cn<sizeof(commands)-1) commands[cn++]=b[i]; else {io_failed=1;cn=0;} }
|
|
335
|
+
}
|
|
336
|
+
if(fds[3].revents&(POLLHUP|POLLERR)) cleaning=cleaning?cleaning:1;
|
|
337
|
+
}
|
|
338
|
+
/* Last scan and verification happen BEFORE final root reap: SID still reserved. */
|
|
339
|
+
discover(); signal_all(SIGKILL); usleep(20000);
|
|
340
|
+
int live=0,uncertain=scanning_failed;
|
|
341
|
+
for(size_t i=0;i<count;i++) { int s=status(&known[i]); if(s==1) live++; else if(s==2) uncertain++; }
|
|
342
|
+
siginfo_t final={0};
|
|
343
|
+
if(waitid(P_PID,root,&final,WEXITED|WNOHANG|WNOWAIT)==0 && final.si_pid==root) {
|
|
344
|
+
int reaped; do { reaped=waitpid(root,NULL,WNOHANG); } while(reaped<0 && errno==EINTR);
|
|
345
|
+
if(reaped!=root) uncertain++;
|
|
346
|
+
held=0;
|
|
347
|
+
} else { /* Never blocking reap of a live/unverifiable root; no adoption after this point. */
|
|
348
|
+
held=0; uncertain++;
|
|
349
|
+
}
|
|
350
|
+
/* Drain all remaining PTY output and queued bytes, advancing partial writes.
|
|
351
|
+
* A stalled consumer cannot retain an owner indefinitely: undrained output is
|
|
352
|
+
* explicit uncertainty, NEVER a successful silent tail discard. No discovery
|
|
353
|
+
* or signals occur after final root reap above. */
|
|
354
|
+
long long drain_deadline=clock_ms()+1000; int master_eof=0;
|
|
355
|
+
while((!master_eof || outn) && clock_ms()<drain_deadline) {
|
|
356
|
+
struct pollfd drain[2]={{master,(!master_eof && outn<QUEUE)?POLLIN:0,0},{1,outn?POLLOUT:0,0}};
|
|
357
|
+
if(poll(drain,2,10)<0) { if(errno==EINTR) continue; io_failed=1; break; }
|
|
358
|
+
if(!master_eof && outn<QUEUE && (drain[0].revents&(POLLIN|POLLHUP|POLLERR))) {
|
|
359
|
+
ssize_t n=read(master,out+outn,QUEUE-outn);
|
|
360
|
+
if(n>0) outn+=(size_t)n;
|
|
361
|
+
else if(n==0 || errno==EIO) master_eof=1;
|
|
362
|
+
else if(errno!=EAGAIN && errno!=EINTR) { io_failed=1; master_eof=1; }
|
|
363
|
+
}
|
|
364
|
+
if(outn && (drain[1].revents&POLLOUT)) {
|
|
365
|
+
ssize_t n=write(1,out,outn);
|
|
366
|
+
if(n>0) { outn-=(size_t)n; memmove(out,out+n,outn); }
|
|
367
|
+
else if(errno!=EAGAIN && errno!=EINTR) { io_failed=1; break; }
|
|
368
|
+
}
|
|
369
|
+
if(drain[1].revents&(POLLHUP|POLLERR)) { io_failed=1; break; }
|
|
370
|
+
}
|
|
371
|
+
int limited_coverage=0;
|
|
372
|
+
#ifndef __APPLE__
|
|
373
|
+
limited_coverage=root_only;
|
|
374
|
+
#endif
|
|
375
|
+
/* An unmanaged descendant may retain the PTY indefinitely. Report this known
|
|
376
|
+
* coverage limit separately. Queued/lost bytes and adapter faults still fail. */
|
|
377
|
+
if(outn || (!master_eof && !limited_coverage)) io_failed=1;
|
|
378
|
+
close(master);
|
|
379
|
+
uncertain+=io_failed;
|
|
380
|
+
const char *coverage="";
|
|
381
|
+
#ifndef __APPLE__
|
|
382
|
+
if(root_only) coverage=master_eof ? ",\"coverage\":\"root-only\",\"outputOpen\":false" : ",\"coverage\":\"root-only\",\"outputOpen\":true";
|
|
383
|
+
#endif
|
|
384
|
+
snprintf(msg,sizeof(msg),"{\"cleanup\":true,\"observed\":%zu,\"live\":%d,\"uncertain\":%d%s}\n",count,live,uncertain,coverage); event(msg);
|
|
385
|
+
#ifndef __APPLE__
|
|
386
|
+
for(size_t i=0;i<count;i++) if(known[i].fd>=0) close(known[i].fd);
|
|
387
|
+
#endif
|
|
388
|
+
free(out);free(input); return live||uncertain?2:0;
|
|
389
|
+
}
|
package/dist/terminal.d.ts
CHANGED
package/dist/terminal.js
CHANGED
|
@@ -13,12 +13,12 @@ var TERMINAL_WARNING = (user = os.userInfo().username) => [
|
|
|
13
13
|
"Paseo app devices paired with this host only after alp terminal allow --paseo."
|
|
14
14
|
].join("\n");
|
|
15
15
|
var file = (home) => path.join(home, "terminal.json");
|
|
16
|
-
async function read(home) {
|
|
16
|
+
async function read(home, platform = process.platform) {
|
|
17
17
|
const info = await lstat(file(home)).catch(() => void 0);
|
|
18
18
|
if (!info) return { exists: false, value: void 0, private: true };
|
|
19
19
|
const own = !process.getuid || info.uid === process.getuid();
|
|
20
|
-
const value = info.isFile() ? await readFile(file(home), "utf8").then(JSON.parse
|
|
21
|
-
return { exists: true, value, private: info.isFile() && own && !(info.mode & 63) };
|
|
20
|
+
const value = info.isFile() ? await readFile(file(home), "utf8").then(JSON.parse).catch(() => void 0) : void 0;
|
|
21
|
+
return { exists: true, value, private: info.isFile() && own && (platform === "win32" || !(info.mode & 63)) };
|
|
22
22
|
}
|
|
23
23
|
async function write(home, value) {
|
|
24
24
|
await mkdir(home, { recursive: true, mode: 448 });
|
|
@@ -75,5 +75,6 @@ Remote terminals are allowed on this host (${file(home)})${allowPaseoTerminal ?
|
|
|
75
75
|
}
|
|
76
76
|
export {
|
|
77
77
|
TERMINAL_WARNING,
|
|
78
|
+
read,
|
|
78
79
|
terminalCommand
|
|
79
80
|
};
|
package/dist/usage.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const USAGE: "Usage:\n alp <init|
|
|
1
|
+
export const USAGE: "Usage:\n alp <init|update-template> [directory] create a project, or upgrade an older project's ALP template (upgrade is an alias)\n alp update [--check] [--yes] [--version X] update ALP from GitHub Releases (checksum and provenance checked)\n alp --version\n alp setup [directory] [--runtime codex|claude|mix] [--prefer claude|codex]\n [--claude-model M] [--codex-model M] [--claude-supervisor M] [--codex-supervisor M]\n [--claude-oracle M] [--codex-oracle M] write Ph\u1EDF/Cafe's agent profiles to .alp/settings.json\n alp daemon <start|stop|restart>\n alp daemon status [--json] liveness and readiness; exit 0 ready, 3 not running, 4 not ready\n alp daemon <install|uninstall> run alpd as a login service that restarts after a crash\n alp daemon run [--detach] [--service] run alpd in this process (what services written by 0.0.1-beta.5/6 start)\n alp acp serve ALP to an editor (Zed, JetBrains) over the Agent Client Protocol on stdio\n alp web [--print] open the ALP web app on this machine (alp-web, loopback); --print only prints its address\n alp web serve [--service|--rotate] [--project DIR] run alp-web in this process (what alp web starts)\n alp bridge <acp|antigravity> the stdio MCP bridge an agent CLI starts to reach alpd (internal)\n alp web rotate give alp web a new token: open pages lose access; prints the new address\n alp web pair [--on|--off] the link that pairs Paseo's app on another device through the relay; --on or --off turns the relay on or off\n alp terminal <allow [--paseo] --yes|deny [--paseo]|status> [--json] remote terminals (a shell as you on this host; a workspace is not a sandbox), off by default\n alp doctor [--project DIR] [--fix] [--json] check this machine and project; --fix repairs what is safe to\n alp backup [-o FILE] [--project DIR]... [--no-projects] [--stop] [--json] sessions, settings, library and tasks in one file\n alp backup verify <file> [--json] | alp backup list [--json]\n alp restore <file> [--home-only] [--dry-run] [--stop] [--replace-newer] [--json] checks the whole backup first; keeps what it replaces\n alpd must be stopped for both; --stop stops it and starts it again after\n alp run [--agent A] [--team pho|cafe|ID] [--model M] [--mode read-only|workspace-write|full-access] [--thinking T] [--project DIR] [--json] <prompt>\n alp schedule create --session ID --at ISO_WITH_OFFSET --prompt TEXT [--project DIR] [--json]\n alp schedule list [--status pending|delivered|failed|cancelled] [--project DIR] [--json]\n alp schedule <show|cancel> ID [--project DIR] [--json] one-shot wake; timer gates only unblock tasks\n alp ps [--all]\n alp top [session] [--once]\n alp attach <session> [--json]\n alp send <session> [--prompt] [--json] <text>\n child default: user mail; --prompt: explicit new prompt to a live root or child\n alp questions [--json]\n alp answer <question> <text> | alp answer <question> --dismiss [--reason R]\n alp log <session> [--json]\n alp board [--project DIR] [--json]\n alp <agents|skills|mcp|hooks> [--project DIR] [--json] the library: built-in, ~/.alp and project entries\n alp providers [--json] ACP and Antigravity providers in your library\n alp teams [--project DIR] [--json] teams sessions run in: members, delegation, house rules\n alp agent <new|edit> <name> [--project [DIR]] [--from A] [-d text] [--provider P] [--model M] [--thinking T] [--mode M]\n [--skills a,b] [--mcp a,b] [--hooks a,b] [--instructions FILE]\n alp team <new|edit> <id> [--project [DIR]] [--from ID] [--label L] [-d text] [--main A] [--member name=role]...\n [--member-model name=M]... [--member-thinking name=T]... [--delegate owner=a,b]... [--max-peers N]\n [--supervisor A] [--supervisor-model M] [--supervisor-thinking T] [--no-supervisor] [--rules FILE]\n alp skill new <name> [--project [DIR]] [--from S] [--file FILE]\n alp mcp add <name> [--project [DIR]] (--command C [--arg A]... [--env K=V]... [--cwd D] | --url U [--header K=V]...)\n alp hook add <name> [--project [DIR]] --event E --command C [--blocking] [--timeout SEC] [--match-agent A] [--match-label L]\n alp provider add <id> --command C [--arg A]... [--env K=V]... [--label L] [-d text] [--models a,b] an ACP agent; use id antigravity for native agy\n alp provider edit antigravity --read-only-mode instructions|reject legacy, no effect: agy enforces every mode itself\n alp <agent|team|skill|mcp|hook|provider> <rm|show> <name> [--project [DIR]] | <cp|mv> <from> <to> [--project [DIR]]\n alp <mcp|hook|provider> test <name> [--project [DIR]] list an MCP server's tools; run a hook once; initialize a provider\n without --project, changes go to your library in ~/.alp; built-in agents and teams are never changed\n alp trust [--project DIR] | --revoke [--project DIR] | --list [--json] whether the project's hooks may run\n alp permissions [--project DIR] [--json] each agent's permission profile\n alp permissions check <agent> \"<command>\" what that agent's profile says about a command\n alp tasks [ready] [--all] [--status S] [--label L] [--project DIR] [--json]\n alp tasks gates [--json] open gates; checks GitHub ones\n alp tasks compact [--days 30] [--dry-run]\n alp tasks export [-o file.jsonl] as beads JSONL\n alp tasks import [file.jsonl] [--dry-run] beads JSONL; default .beads/issues.jsonl\n alp formula list | show <name> | pour <name> [--var k=v]... [--parent ID] [--dry-run]\n alp task add <title> [-d text] [-p 0-4] [-t task|bug|feature|chore|epic] [--parent ID] [--after ID]... [-l label]... [--path P]... [--from ID]\n alp task show <id> [--json]\n alp task report <epic> [--json] what an epic came to: tasks, time, rework, verification\n alp task edit <id> [--title T] [-d text] [-p N] [-t type] [-l label]... [--path P]... [-m note]\n alp task close <id> [--reason done|wontfix|duplicate|superseded] [-m summary] [--unverified \"why\"]\n alp verify [--project DIR] [--task ID] [--json] run the project's verify commands; --task records the result\n alp task reopen <id> [-m note]\n alp task dep <add|rm> <id> [--after ID]... [--parent ID] [--related ID]...\n alp task gate add <id> --human \"question\" [--option answer]... | --timer +2h|ISO | --pr N|owner/repo#N | --run N|owner/repo#N\n alp task gate clear <id> <gate> [--choice answer] [-m note] approve; main is told by mail\n alp task gate reject <id> <gate> [-m reason] the gate stays open; main is told by mail\n alp task gate rm <id> <gate> [-m note]\n alp pause [codex|claude] [--now] [-m reason] | alp pause status hold delegation; --now parks running assignments\n alp resume [codex|claude] lift a pause; parked assignments continue\n alp models [--refresh] [--json] the models alpd offers now, from each provider (cached; refreshed every 6 h)\n alp language [name | --reset] the language agents and ALP write to you in; default Vietnamese\n alp recall <assignment|task> [--project DIR] [--json] <question> ask a finished assignment about its work\n alp interrupt <session>\n alp compact <session>";
|
|
2
2
|
export class UsageError extends Error {
|
|
3
3
|
}
|
package/dist/usage.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
// packages/cli/src/usage.js
|
|
2
2
|
var USAGE = `Usage:
|
|
3
|
-
alp <init|
|
|
3
|
+
alp <init|update-template> [directory] create a project, or upgrade an older project's ALP template (upgrade is an alias)
|
|
4
|
+
alp update [--check] [--yes] [--version X] update ALP from GitHub Releases (checksum and provenance checked)
|
|
5
|
+
alp --version
|
|
4
6
|
alp setup [directory] [--runtime codex|claude|mix] [--prefer claude|codex]
|
|
5
7
|
[--claude-model M] [--codex-model M] [--claude-supervisor M] [--codex-supervisor M]
|
|
6
8
|
[--claude-oracle M] [--codex-oracle M] write Ph\u1EDF/Cafe's agent profiles to .alp/settings.json
|
|
7
9
|
alp daemon <start|stop|restart>
|
|
8
10
|
alp daemon status [--json] liveness and readiness; exit 0 ready, 3 not running, 4 not ready
|
|
9
11
|
alp daemon <install|uninstall> run alpd as a login service that restarts after a crash
|
|
12
|
+
alp daemon run [--detach] [--service] run alpd in this process (what services written by 0.0.1-beta.5/6 start)
|
|
10
13
|
alp acp serve ALP to an editor (Zed, JetBrains) over the Agent Client Protocol on stdio
|
|
11
14
|
alp web [--print] open the ALP web app on this machine (alp-web, loopback); --print only prints its address
|
|
15
|
+
alp web serve [--service|--rotate] [--project DIR] run alp-web in this process (what alp web starts)
|
|
16
|
+
alp bridge <acp|antigravity> the stdio MCP bridge an agent CLI starts to reach alpd (internal)
|
|
12
17
|
alp web rotate give alp web a new token: open pages lose access; prints the new address
|
|
13
18
|
alp web pair [--on|--off] the link that pairs Paseo's app on another device through the relay; --on or --off turns the relay on or off
|
|
14
19
|
alp terminal <allow [--paseo] --yes|deny [--paseo]|status> [--json] remote terminals (a shell as you on this host; a workspace is not a sandbox), off by default
|
|
@@ -42,7 +47,7 @@ var USAGE = `Usage:
|
|
|
42
47
|
alp mcp add <name> [--project [DIR]] (--command C [--arg A]... [--env K=V]... [--cwd D] | --url U [--header K=V]...)
|
|
43
48
|
alp hook add <name> [--project [DIR]] --event E --command C [--blocking] [--timeout SEC] [--match-agent A] [--match-label L]
|
|
44
49
|
alp provider add <id> --command C [--arg A]... [--env K=V]... [--label L] [-d text] [--models a,b] an ACP agent; use id antigravity for native agy
|
|
45
|
-
alp provider edit antigravity --read-only-mode instructions|reject
|
|
50
|
+
alp provider edit antigravity --read-only-mode instructions|reject legacy, no effect: agy enforces every mode itself
|
|
46
51
|
alp <agent|team|skill|mcp|hook|provider> <rm|show> <name> [--project [DIR]] | <cp|mv> <from> <to> [--project [DIR]]
|
|
47
52
|
alp <mcp|hook|provider> test <name> [--project [DIR]] list an MCP server's tools; run a hook once; initialize a provider
|
|
48
53
|
without --project, changes go to your library in ~/.alp; built-in agents and teams are never changed
|
|
@@ -63,13 +68,17 @@ var USAGE = `Usage:
|
|
|
63
68
|
alp verify [--project DIR] [--task ID] [--json] run the project's verify commands; --task records the result
|
|
64
69
|
alp task reopen <id> [-m note]
|
|
65
70
|
alp task dep <add|rm> <id> [--after ID]... [--parent ID] [--related ID]...
|
|
66
|
-
alp task gate add <id> --human "question" | --timer +2h|ISO | --pr N|owner/repo#N | --run N|owner/repo#N
|
|
67
|
-
alp task gate
|
|
71
|
+
alp task gate add <id> --human "question" [--option answer]... | --timer +2h|ISO | --pr N|owner/repo#N | --run N|owner/repo#N
|
|
72
|
+
alp task gate clear <id> <gate> [--choice answer] [-m note] approve; main is told by mail
|
|
73
|
+
alp task gate reject <id> <gate> [-m reason] the gate stays open; main is told by mail
|
|
74
|
+
alp task gate rm <id> <gate> [-m note]
|
|
68
75
|
alp pause [codex|claude] [--now] [-m reason] | alp pause status hold delegation; --now parks running assignments
|
|
69
76
|
alp resume [codex|claude] lift a pause; parked assignments continue
|
|
77
|
+
alp models [--refresh] [--json] the models alpd offers now, from each provider (cached; refreshed every 6 h)
|
|
70
78
|
alp language [name | --reset] the language agents and ALP write to you in; default Vietnamese
|
|
71
79
|
alp recall <assignment|task> [--project DIR] [--json] <question> ask a finished assignment about its work
|
|
72
|
-
alp interrupt <session
|
|
80
|
+
alp interrupt <session>
|
|
81
|
+
alp compact <session>`;
|
|
73
82
|
var UsageError = class extends Error {
|
|
74
83
|
};
|
|
75
84
|
export {
|
package/dist/web-service.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/** The alp-web service script beside this CLI; a clear error when this CLI was built without it. */
|
|
2
|
-
export function
|
|
2
|
+
export function alpWebScript(): string;
|
|
3
3
|
/**
|
|
4
4
|
* The process that owns alp web's lock, if it is proven safe to stop; else why not. `kind` says
|
|
5
5
|
* whether waiting can help: `transient` (an owner still starting has not published web.json yet),
|
|
6
6
|
* `foreign` (proven not to be the integration: another user's, another boot's, or a reused pid) or
|
|
7
7
|
* `unknown` (it cannot be proven either way, for example without ps or on Windows).
|
|
8
8
|
*
|
|
9
|
-
* The reuse proof compares the lock's
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* The reuse proof compares the lock's creation time with the process's start from ps: when
|
|
10
|
+
* `startedUptime` is recorded, monotonic uptime comparison prevents clock step vulnerabilities.
|
|
11
|
+
* For legacy locks, wall-clock `startedAt` is compared.
|
|
12
12
|
* @param {string} home
|
|
13
13
|
* @returns {Promise<{ pid: number, app: unknown } | { reason: string, alive: boolean, kind?: 'transient' | 'foreign' | 'unknown' }>}
|
|
14
14
|
*/
|
|
@@ -36,3 +36,21 @@ export function stopPreRenamePaseo(home: string): Promise<number | undefined>;
|
|
|
36
36
|
export function stopWebService(home: string, timeoutMs?: number): Promise<number | undefined>;
|
|
37
37
|
/** The opening of the message for a web lock held by a process alp would not stop. @param {string} home */
|
|
38
38
|
export function refusal(home: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Probes whether the published web service URL is live and answering requests.
|
|
41
|
+
* Accepts only loopback HTTP/HTTPS addresses.
|
|
42
|
+
* @param {string | undefined} url
|
|
43
|
+
* @param {number} [timeoutMs]
|
|
44
|
+
*/
|
|
45
|
+
export function isWebReady(url: string | undefined, timeoutMs?: number): Promise<boolean>;
|
|
46
|
+
/**
|
|
47
|
+
* Whether web metadata is genuinely claimed by a live alp-web process.
|
|
48
|
+
* If the process is proven foreign (e.g. a reused PID or from another boot) or
|
|
49
|
+
* the URL does not answer HTTP requests, it is never claimed.
|
|
50
|
+
* @param {any} published
|
|
51
|
+
* @param {number | undefined} lockPid
|
|
52
|
+
* @param {any} held
|
|
53
|
+
* @param {number} [timeoutMs]
|
|
54
|
+
*/
|
|
55
|
+
export function isWebClaimed(published: any, lockPid: number | undefined, held: any, timeoutMs?: number): Promise<boolean>;
|
|
56
|
+
export function alpWebEntry(): string;
|
package/dist/web-service.js
CHANGED
|
@@ -2,27 +2,30 @@
|
|
|
2
2
|
import { execFile } from "node:child_process";
|
|
3
3
|
import { existsSync } from "node:fs";
|
|
4
4
|
import { lstat, readFile } from "node:fs/promises";
|
|
5
|
+
import os from "node:os";
|
|
5
6
|
import path from "node:path";
|
|
6
7
|
import { fileURLToPath } from "node:url";
|
|
7
8
|
import { promisify } from "node:util";
|
|
8
|
-
import { bootTime, readLock } from "@anhlp/alp-client/index";
|
|
9
|
+
import { bootTime, isSameBoot, readLock } from "@anhlp/alp-client/index";
|
|
9
10
|
import { distributionEntry } from "./paths.js";
|
|
11
|
+
import { selfEntry } from "@anhlp/alp-core/packaged";
|
|
10
12
|
var execFileAsync = promisify(execFile);
|
|
11
13
|
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
12
|
-
function
|
|
14
|
+
function alpWebScript() {
|
|
13
15
|
const entry = fileURLToPath(distributionEntry("alp-web.js"));
|
|
14
16
|
if (!existsSync(entry)) throw new Error(`alp web is not built: ${entry} is missing. Build the CLI (npm run build in the ALP checkout) or reinstall ALP.`);
|
|
15
17
|
return entry;
|
|
16
18
|
}
|
|
19
|
+
var alpWebEntry = () => selfEntry(alpWebScript());
|
|
17
20
|
var readJson = async (file) => JSON.parse(await readFile(file, "utf8"));
|
|
18
|
-
async function
|
|
21
|
+
async function processTimes(pid) {
|
|
19
22
|
try {
|
|
20
23
|
const { stdout } = await execFileAsync("ps", ["-o", "etime=", "-p", String(pid)], { encoding: "utf8", timeout: 5e3 });
|
|
21
24
|
const match = /^\s*(?:(\d+)-)?(?:(\d+):)?(\d+):(\d+)\s*$/.exec(stdout);
|
|
22
25
|
if (!match) return void 0;
|
|
23
26
|
const [, days = "0", hours = "0", minutes, seconds] = match;
|
|
24
27
|
const elapsed = ((Number(days) * 24 + Number(hours)) * 60 + Number(minutes)) * 60 + Number(seconds);
|
|
25
|
-
return Date.now() - elapsed * 1e3;
|
|
28
|
+
return { started: Date.now() - elapsed * 1e3, elapsed };
|
|
26
29
|
} catch {
|
|
27
30
|
return void 0;
|
|
28
31
|
}
|
|
@@ -52,11 +55,12 @@ async function webOwner(home) {
|
|
|
52
55
|
if (process.platform === "win32") return refuse("alp does not stop it on Windows", "unknown");
|
|
53
56
|
const uid = process.getuid?.();
|
|
54
57
|
if (lockInfo.uid !== uid) return refuse("its lock is not yours", "foreign");
|
|
55
|
-
if (
|
|
58
|
+
if (!isSameBoot(lock)) return refuse("its lock is from another boot", "foreign");
|
|
56
59
|
if (pid === process.pid || pid === (await readLock(home).catch(() => void 0))?.pid) return refuse(`pid ${pid} is not an integration`, "foreign");
|
|
57
|
-
const
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
+
const times = await processTimes(pid);
|
|
61
|
+
if (times === void 0) return refuse(`the start time of pid ${pid} is unknown`, "unknown");
|
|
62
|
+
const isReused = typeof lock.startedUptime === "number" ? Math.floor(os.uptime()) - times.elapsed > lock.startedUptime + 2 : times.started > Date.parse(lock.startedAt) + 2e3;
|
|
63
|
+
if (isReused) return refuse(`pid ${pid} started after the lock was written (a reused pid)`, "foreign");
|
|
60
64
|
let info, infoStat;
|
|
61
65
|
try {
|
|
62
66
|
infoStat = await lstat(infoFile);
|
|
@@ -110,8 +114,28 @@ async function stopWebService(home, timeoutMs = 3e4) {
|
|
|
110
114
|
function refusal(home) {
|
|
111
115
|
return `${path.join(home, "integrations", "web.lock")} is held by a process that alp cannot prove is alp web or the Paseo integration from before its rename, so it was left running`;
|
|
112
116
|
}
|
|
117
|
+
async function isWebReady(url, timeoutMs = 1e3) {
|
|
118
|
+
if (typeof url !== "string" || !url) return false;
|
|
119
|
+
try {
|
|
120
|
+
const parsed = new URL(url);
|
|
121
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return false;
|
|
122
|
+
if (!["127.0.0.1", "localhost", "::1", "[::1]"].includes(parsed.hostname)) return false;
|
|
123
|
+
const res = await fetch(parsed.href, { signal: AbortSignal.timeout(timeoutMs) });
|
|
124
|
+
return typeof res.status === "number" && res.status > 0;
|
|
125
|
+
} catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async function isWebClaimed(published, lockPid, held, timeoutMs = 1e3) {
|
|
130
|
+
if (published?.app !== "alp-web" || published.pid !== lockPid) return false;
|
|
131
|
+
if (held?.kind === "foreign") return false;
|
|
132
|
+
return isWebReady(published.url, timeoutMs);
|
|
133
|
+
}
|
|
113
134
|
export {
|
|
114
135
|
alpWebEntry,
|
|
136
|
+
alpWebScript,
|
|
137
|
+
isWebClaimed,
|
|
138
|
+
isWebReady,
|
|
115
139
|
refusal,
|
|
116
140
|
stopPreRenamePaseo,
|
|
117
141
|
stopWebService,
|