-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpt
More file actions
executable file
·394 lines (329 loc) · 12.7 KB
/
pt
File metadata and controls
executable file
·394 lines (329 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env bash
#
# pt - Process Triage Wrapper
#
# This script wraps the 'pt-core' Alien Technology Artifact.
# It ensures the environment is set up correctly and forwards all arguments.
#
set -euo pipefail
readonly VERSION="2.1.0"
# ══════════════════════════════════════════════════════════════════════════════
# Configuration
# ══════════════════════════════════════════════════════════════════════════════
readonly PT_CORE_BIN="pt-core"
readonly INSTALL_BASE_URL="https://raw.githubusercontent.com/Dicklesworthstone/process_triage"
readonly INSTALL_URL="${INSTALL_BASE_URL}/main/install.sh"
# Populated in release artifacts to pin expected installer signing key.
readonly DEFAULT_RELEASE_PUBLIC_KEY_FINGERPRINT=""
# Locations to search for pt-core
readonly SEARCH_PATHS=(
"$(dirname "$0")/pt-core"
"$(dirname "$0")/target/release/pt-core"
"$HOME/.local/bin/pt-core"
"/usr/local/bin/pt-core"
"/usr/bin/pt-core"
)
# ══════════════════════════════════════════════════════════════════════════════
# Discovery
# ══════════════════════════════════════════════════════════════════════════════
find_pt_core() {
# Check explicit override
if [[ -n "${PT_CORE_PATH:-}" ]]; then
if [[ -x "$PT_CORE_PATH" ]]; then
echo "$PT_CORE_PATH"
return 0
fi
fi
# Check search paths
for path in "${SEARCH_PATHS[@]}"; do
if [[ -x "$path" ]]; then
echo "$path"
return 0
fi
done
# Check PATH
if command -v "$PT_CORE_BIN" &>/dev/null; then
command -v "$PT_CORE_BIN"
return 0
fi
return 1
}
fetch_latest_release_version() {
local version_url="${INSTALL_BASE_URL}/main/VERSION"
local version=""
if command -v curl &>/dev/null; then
version="$(curl -fsSL "$version_url" 2>/dev/null | tr -d '[:space:]')" || return 1
elif command -v wget &>/dev/null; then
version="$(wget -qO- "$version_url" 2>/dev/null | tr -d '[:space:]')" || return 1
else
return 1
fi
# Accept semver-like release tags only (e.g., 2.0.3, 2.0.3-rc1, 2.0.3+build).
if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z]+)*(\+[0-9A-Za-z.-]+)?$ ]]; then
return 1
fi
printf '%s\n' "$version"
}
# ══════════════════════════════════════════════════════════════════════════════
# UI mode selection
# ══════════════════════════════════════════════════════════════════════════════
detect_auto_ui_mode() {
if [[ ! -t 0 || ! -t 1 ]]; then
printf 'shell\n'
return 0
fi
if [[ "${CI:-}" == "true" || "${CI:-}" == "1" ]]; then
printf 'shell\n'
return 0
fi
if [[ "${TERM:-}" == "dumb" ]]; then
printf 'shell\n'
return 0
fi
printf 'tui\n'
}
resolve_ui_mode() {
local cli_mode="${1:-}"
local env_mode="${PT_UI_MODE:-auto}"
if [[ "$cli_mode" == "shell" || "$cli_mode" == "tui" ]]; then
printf '%s\n' "$cli_mode"
return 0
fi
case "$env_mode" in
shell|tui)
printf '%s\n' "$env_mode"
return 0
;;
auto|"")
;;
*)
;;
esac
detect_auto_ui_mode
}
resolve_config_dir() {
if [[ -n "${PT_CONFIG_DIR:-}" ]]; then
printf '%s\n' "$PT_CONFIG_DIR"
return 0
fi
if [[ -n "${PROCESS_TRIAGE_CONFIG:-}" ]]; then
printf '%s\n' "$PROCESS_TRIAGE_CONFIG"
return 0
fi
if [[ -n "${XDG_CONFIG_HOME:-}" ]]; then
printf '%s\n' "${XDG_CONFIG_HOME}/process_triage"
return 0
fi
printf '%s\n' "${HOME}/.config/process_triage"
}
ensure_decisions_file() {
local config_dir="$1"
local decisions_file="${config_dir}/decisions.json"
mkdir -p "$config_dir"
if [[ ! -e "$decisions_file" || ! -s "$decisions_file" ]]; then
printf '{}\n' > "$decisions_file"
fi
printf '%s\n' "$decisions_file"
}
show_history() {
local config_dir decisions_file
config_dir="$(resolve_config_dir)"
decisions_file="$(ensure_decisions_file "$config_dir")"
if ! command -v jq &>/dev/null; then
echo "Error: jq is required for 'pt history'." >&2
exit 1
fi
if ! jq -e 'type == "object"' "$decisions_file" >/dev/null 2>&1; then
echo "Error: decision history is not valid JSON object: $decisions_file" >&2
exit 1
fi
local total
total="$(jq 'length' "$decisions_file")"
if [[ "$total" == "0" ]]; then
printf 'No decision history.\n'
return 0
fi
local kill_count spare_count
kill_count="$(jq '[.[] | select(. == "kill")] | length' "$decisions_file")"
spare_count="$(jq '[.[] | select(. == "spare")] | length' "$decisions_file")"
printf 'Decision history (%s total)\n' "$total"
printf 'kill: %s\n' "$kill_count"
printf 'spare: %s\n' "$spare_count"
jq -r 'to_entries[] | "\(.value)\t\(.key)"' "$decisions_file"
}
clear_history() {
local config_dir decisions_file
config_dir="$(resolve_config_dir)"
decisions_file="$(ensure_decisions_file "$config_dir")"
if ! command -v jq &>/dev/null; then
echo "Error: jq is required for 'pt clear'." >&2
exit 1
fi
if ! jq -e 'type == "object"' "$decisions_file" >/dev/null 2>&1; then
echo "Error: decision history is not valid JSON object: $decisions_file" >&2
exit 1
fi
local total
total="$(jq 'length' "$decisions_file")"
if [[ "$total" == "0" ]]; then
printf 'Decision history already empty.\n'
return 0
fi
local reply=""
if [[ -t 0 && -z "${CI:-}" && -z "${TEST_MODE:-}" ]]; then
printf 'Clear all decision history? [y/N] ' >&2
read -r reply || true
else
read -r reply || true
fi
if [[ ! "$reply" =~ ^[Yy]([Ee][Ss])?$ ]]; then
printf 'Aborted.\n' >&2
exit 1
fi
printf '{}\n' > "$decisions_file"
printf 'Cleared %s decision%s.\n' "$total" "$([[ "$total" == "1" ]] && printf '' || printf 's')"
}
show_wrapper_help() {
cat <<EOF
Process Triage wrapper for pt-core
Usage: pt [WRAPPER-OPTIONS] [COMMAND]
Wrapper options:
--shell Force shell/non-TUI mode
--tui Force TUI mode when interactive
-h, --help Show this wrapper-aware help
-V, --version Show wrapper version
Wrapper commands:
help Show wrapper-aware top-level help
update Run the signed installer update flow
history Show learned decision memory from decisions.json
clear Clear learned decision memory after confirmation
deep Alias for \`deep-scan\`
Core commands:
run Interactive golden path
scan Quick scan only
deep-scan Full deep scan with all available probes
diff Compare two sessions
query Query telemetry and history
bundle Create or inspect diagnostic bundles
report Generate HTML reports
check Validate configuration and environment
learn Interactive tutorials and onboarding
agent Agent/robot subcommands
config Configuration management
telemetry Telemetry management
shadow Shadow mode observation management
signature Signature management
schema Generate JSON schemas
update Update management
mcp MCP server
completions Generate shell completion scripts
version Print version information
Notes:
- \`pt scan deep\` is accepted as a wrapper alias for \`pt deep-scan\`.
- Use \`pt help <subcommand>\` or \`pt <subcommand> --help\` for pt-core help.
EOF
}
# ══════════════════════════════════════════════════════════════════════════════
# Main
# ══════════════════════════════════════════════════════════════════════════════
main() {
local cli_mode=""
local -a forwarded_args=()
local arg
for arg in "$@"; do
case "$arg" in
--shell)
if [[ "$cli_mode" == "tui" ]]; then
echo "Error: --shell and --tui cannot be used together." >&2
exit 2
fi
cli_mode="shell"
;;
--tui)
if [[ "$cli_mode" == "shell" ]]; then
echo "Error: --shell and --tui cannot be used together." >&2
exit 2
fi
cli_mode="tui"
;;
*)
forwarded_args+=("$arg")
;;
esac
done
local selected_ui_mode
selected_ui_mode="$(resolve_ui_mode "$cli_mode")"
export PT_UI_MODE="$selected_ui_mode"
local pt_core
pt_core=$(find_pt_core) || {
echo "Error: 'pt-core' binary not found." >&2
echo "Please install it via:" >&2
echo " curl -fsSL $INSTALL_URL | bash" >&2
echo " # or" >&2
echo " wget -qO- $INSTALL_URL | bash" >&2
exit 1
}
# Special handling for 'update' (wrapper-level)
if [[ "${forwarded_args[0]:-}" == "update" ]]; then
local target_version
if target_version="$(fetch_latest_release_version)"; then
:
else
target_version="$VERSION"
echo "Warning: could not resolve latest VERSION; falling back to v${VERSION} installer." >&2
fi
local target_install_url="${INSTALL_BASE_URL}/v${target_version}/install.sh"
echo "Updating Process Triage to v${target_version}..."
if command -v curl &>/dev/null; then
if [[ -n "${DEFAULT_RELEASE_PUBLIC_KEY_FINGERPRINT:-}" ]]; then
curl -fsSL "$target_install_url" | env VERIFY=1 PT_RELEASE_PUBLIC_KEY_FINGERPRINT="${DEFAULT_RELEASE_PUBLIC_KEY_FINGERPRINT}" bash
else
curl -fsSL "$target_install_url" | env VERIFY=1 bash
fi
elif command -v wget &>/dev/null; then
if [[ -n "${DEFAULT_RELEASE_PUBLIC_KEY_FINGERPRINT:-}" ]]; then
wget -qO- "$target_install_url" | env VERIFY=1 PT_RELEASE_PUBLIC_KEY_FINGERPRINT="${DEFAULT_RELEASE_PUBLIC_KEY_FINGERPRINT}" bash
else
wget -qO- "$target_install_url" | env VERIFY=1 bash
fi
else
echo "Error: neither curl nor wget is available to run installer." >&2
exit 1
fi
exit $?
fi
if [[ "${forwarded_args[0]:-}" == "--version" || "${forwarded_args[0]:-}" == "-V" ]]; then
printf 'pt %s\n' "$VERSION"
exit 0
fi
if [[ "${forwarded_args[0]:-}" == "--help" || "${forwarded_args[0]:-}" == "-h" ]]; then
show_wrapper_help
exit 0
fi
if [[ "${forwarded_args[0]:-}" == "help" && ${#forwarded_args[@]} -eq 1 ]]; then
show_wrapper_help
exit 0
fi
if [[ "${forwarded_args[0]:-}" == "history" ]]; then
show_history
exit 0
fi
if [[ "${forwarded_args[0]:-}" == "clear" ]]; then
clear_history
exit 0
fi
# Preserve the documented `pt deep` surface even though pt-core exposes
# the clap-generated `deep-scan` subcommand name.
if [[ "${forwarded_args[0]:-}" == "deep" ]]; then
forwarded_args[0]="deep-scan"
fi
# Preserve the older wrapper-level `pt scan deep` shortcut by routing it
# to the explicit deep-scan subcommand exposed by pt-core.
if [[ "${forwarded_args[0]:-}" == "scan" && "${forwarded_args[1]:-}" == "deep" ]]; then
forwarded_args=("deep-scan" "${forwarded_args[@]:2}")
fi
# Forward to pt-core
# We use 'exec' to replace the shell process
exec "$pt_core" "${forwarded_args[@]}"
}
main "$@"