@pawlogic/dl 0.4.0-staging.6f42a13 → 0.4.0-staging.a0637c4

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.
@@ -0,0 +1,163 @@
1
+ #!/bin/sh
2
+ # dl 沙箱内自更新 (npm 版本) —— 替代 dl-selfupdate.sh 的 GCS 路径。
3
+ #
4
+ # wrapper 在 exec dl 之前调一次本脚本。任何步骤失败一律 swallow,永远 exit 0,
5
+ # 绝不阻塞业务 dl 调用。当次仍用旧 bundle,新 bundle 装完后**下次** dl 调用才生效
6
+ # (agent 失败重试天然吃到新版)。
7
+ #
8
+ # 触发条件:DL_NPM_DIST_TAG 已 bake 到 sandbox env(prod=latest,staging=staging;PR
9
+ # preview 留空 → 本脚本直接 exit,沙箱内 dl 锁在 template 烤进去那版,便于 PR 间
10
+ # 复现 / 互不污染)。
11
+ #
12
+ # 鉴权: 不需要。包发到 npmjs.com 的 @pawlogic scope (public),npm 默认 registry
13
+ # 就是 npmjs.com,匿名 download 直接通,沙箱没有 ~/.npmrc。
14
+ #
15
+ # TTL 300s 缓存:同一 sandbox 5 分钟内多次 dl 调用只查一次。LLM 调用链常一分钟 10+
16
+ # 次,不加缓存白白打满 npm registry。
17
+ #
18
+ # 观测: 每条分支结束 fire-and-forget POST 一条事件到 callback-server
19
+ # $CALLBACK_SERVER_URL/internal/dl-selfupdate-event,backend 转 Prometheus
20
+ # counter + 结构化日志。TTL 命中频繁(每个沙箱 5min 内 N 次), 按 1/10 采样上报;
21
+ # 其余分支(install / no_tag / no_npm / lock_skip)全量上报。
22
+ # 上报失败一律 swallow,绝不让观测链阻塞自更新本身。
23
+
24
+ set -u
25
+
26
+ STAMP=/tmp/dl-npm-checkedat
27
+ LOCK=/tmp/dl-npm-update.lock
28
+ TTL=300
29
+ PACKAGE="@pawlogic/dl"
30
+
31
+ # ── 观测上报: fire-and-forget JSON POST,失败 swallow ────────────────────────
32
+ # args: outcome duration_ms [error_tail_file]
33
+ # error_tail_file 仅 npm_fail 分支传入,读末尾 ~200 字节带上去 —— 之前 npm stderr
34
+ # 全 swallow,根本看不到为什么失败,fleet 升级率 0.24% 都查不到根因。
35
+ report_event() {
36
+ _outcome="$1"
37
+ _dur="${2:-null}"
38
+ _errfile="${3:-}"
39
+ [ -z "${CALLBACK_SERVER_URL:-}" ] && return 0
40
+ command -v curl >/dev/null 2>&1 || return 0
41
+ _tag="${DL_NPM_DIST_TAG:-unknown}"
42
+ case "$_tag" in
43
+ latest|staging) ;;
44
+ *) _tag="unknown" ;;
45
+ esac
46
+ _ver=""
47
+ _verfile="/home/user/.local/lib/node_modules/@pawlogic/dl/package.json"
48
+ if [ -f "$_verfile" ] && command -v node >/dev/null 2>&1; then
49
+ _ver=$(node -e 'try{console.log(require("'"$_verfile"'").version||"")}catch(e){}' 2>/dev/null || echo "")
50
+ fi
51
+ # error_tail: 末尾 200 字节, JSON 不安全字符 (\ " 控制符 / 换行 / tab) 一律压成
52
+ # 单空格; 长度上限挡住 LB 滥用,字符替换让 JSON 不需要复杂 escape。
53
+ _err_json="null"
54
+ if [ -n "$_errfile" ] && [ -f "$_errfile" ]; then
55
+ _err_raw=$(tail -c 200 "$_errfile" 2>/dev/null | tr '\n\r\t\\"' ' ' | tr -d '\000-\037')
56
+ if [ -n "$_err_raw" ]; then
57
+ _err_json="\"$_err_raw\""
58
+ fi
59
+ fi
60
+ # 后台 curl, -m 2 上限 2s; 不解析响应; stderr/stdout 全丢。
61
+ (
62
+ curl -fsS -m 2 -X POST "${CALLBACK_SERVER_URL%/}/internal/dl-selfupdate-event" \
63
+ -H 'content-type: application/json' \
64
+ -d "{\"trigger\":\"wrapper\",\"dist_tag\":\"$_tag\",\"outcome\":\"$_outcome\",\"duration_ms\":$_dur,\"installed_version\":\"$_ver\",\"build_short_sha\":null,\"error_tail\":$_err_json}" \
65
+ >/dev/null 2>&1 || true
66
+ ) &
67
+ }
68
+
69
+ # 采样 TTL hit 上报: 10 选 1。awk 比 $RANDOM 更便携 (busybox sh 默认没 $RANDOM)。
70
+ should_sample_ttl() {
71
+ awk 'BEGIN{srand(); exit !(int(rand()*10)==0)}' 2>/dev/null
72
+ }
73
+
74
+ # 环境不全 → no-op。staging / prod 才烤 DL_NPM_DIST_TAG,PR preview / 本地裸跑空。
75
+ if [ -z "${DL_NPM_DIST_TAG:-}" ]; then
76
+ report_event no_tag null
77
+ exit 0
78
+ fi
79
+ if ! command -v npm >/dev/null 2>&1; then
80
+ report_event no_npm null
81
+ exit 0
82
+ fi
83
+
84
+ # TTL 拦截。stamp 损坏当 0 处理。
85
+ now=$(date +%s 2>/dev/null) || exit 0
86
+ if [ -f "$STAMP" ]; then
87
+ last=$(cat "$STAMP" 2>/dev/null || echo 0)
88
+ case "$last" in
89
+ ''|*[!0-9]*) last=0 ;;
90
+ esac
91
+ delta=$((now - last))
92
+ if [ "$delta" -ge 0 ] && [ "$delta" -lt "$TTL" ]; then
93
+ if should_sample_ttl; then
94
+ report_event ttl_hit null
95
+ fi
96
+ exit 0
97
+ fi
98
+ fi
99
+
100
+ # 单飞锁:并发 dl 调用同时进来时只让一个跑 npm install,其他直接退出。
101
+ # flock 不一定有(busybox sh) → mkdir 兜底原子锁。
102
+ run_install() {
103
+ _t0=$(date +%s 2>/dev/null || echo 0)
104
+ # stderr 写到独立文件 npm_fail 分支带末尾给观测端;stdout 仍丢掉。
105
+ _errlog=$(mktemp 2>/dev/null || echo "/tmp/dl-npm-err.$$")
106
+ # 必须带 --prefix=/home/user/.local 跟 HOME=/home/user,跟 sandbox-template
107
+ # 首次安装那一段对齐 (packages/sandbox-template/scripts/publish-e2b-template.ts
108
+ # 里 wrapper 写法)。沙箱进程 owner 是 user,无 /usr/lib/node_modules 写权限;
109
+ # 不带 prefix → npm 走默认全局路径 → EACCES 1 秒内挂掉, stderr 又被吞 → fleet
110
+ # 自更新 100% 失败但完全黑盒。这是 PR #729 漏掉 prefix 留下的 bug。
111
+ if HOME=/home/user npm install -g --prefix=/home/user/.local \
112
+ --no-audit --no-fund --no-update-notifier \
113
+ "$PACKAGE@$DL_NPM_DIST_TAG" >/dev/null 2>"$_errlog"; then
114
+ _outcome=ok
115
+ else
116
+ _outcome=npm_fail
117
+ fi
118
+ _t1=$(date +%s 2>/dev/null || echo 0)
119
+ _dur_ms=$(( (_t1 - _t0) * 1000 ))
120
+ # npm install -g 会重生成 /home/user/.local/bin/{dl,ilands} shim 顶掉 wrapper
121
+ # 软链, 立刻 re-link 回 /usr/local/bin/{dl,ilands} wrapper —— 不然 PATH 命中
122
+ # npm shim, 下次调用绕过 wrapper, dl 的 DL_NPM_DIST_TAG export 跑不到, 自更新
123
+ # 链路死掉; ilands 也跟着丢 wrapper(虽然 ilands 自己不需要 env, 但 update.sh
124
+ # 入口被绕过, 跟 dl 同时挂)。
125
+ ln -sf /usr/local/bin/dl /home/user/.local/bin/dl >/dev/null 2>&1 || true
126
+ ln -sf /usr/local/bin/ilands /home/user/.local/bin/ilands >/dev/null 2>&1 || true
127
+ # 自我热更: @pawlogic/dl npm 包里 ship 了本脚本的最新版 (packages/dl/sandbox-assets/
128
+ # dl-npm-update.sh), npm install 完成后覆盖一份到 wrapper 调用的固定路径。
129
+ # 1 代 lag: 本次仍跑老逻辑, 新逻辑下次 dl 调用生效 —— 比"等 e2b template republish
130
+ # + 老 snapshot 自然下线"快好几个量级, snapshot resume 的沙箱也能跟上。
131
+ # 当前进程文件是 shell 解释器一次性读入内存的, 覆盖自身不会撕裂正在执行的循环。
132
+ if [ -f /home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh ]; then
133
+ cp /home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh \
134
+ /home/user/.local/lib/dl-npm-update.sh 2>/dev/null || true
135
+ chmod +x /home/user/.local/lib/dl-npm-update.sh 2>/dev/null || true
136
+ fi
137
+ echo "$now" > "$STAMP"
138
+ if [ "$_outcome" = "npm_fail" ]; then
139
+ report_event "$_outcome" "$_dur_ms" "$_errlog"
140
+ else
141
+ report_event "$_outcome" "$_dur_ms"
142
+ fi
143
+ rm -f "$_errlog" 2>/dev/null || true
144
+ }
145
+
146
+ if command -v flock >/dev/null 2>&1; then
147
+ (
148
+ if ! flock -n 9; then
149
+ report_event lock_skip null
150
+ exit 0
151
+ fi
152
+ run_install
153
+ ) 9>"$LOCK"
154
+ else
155
+ if mkdir "$LOCK" 2>/dev/null; then
156
+ trap 'rmdir "$LOCK" 2>/dev/null || true' EXIT INT TERM
157
+ run_install
158
+ else
159
+ report_event lock_skip null
160
+ fi
161
+ fi
162
+
163
+ exit 0