@lousy-agents/agent-shell 4.0.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/README.md ADDED
@@ -0,0 +1,232 @@
1
+ # agent-shell
2
+
3
+ ![agent-shell demo](https://raw.githubusercontent.com/lousy-agents/agents/HEAD/media/agent-shell.gif)
4
+
5
+ A flight recorder for npm script execution.
6
+
7
+ agent-shell is an npm `script-shell` shim that independently records what scripts ran, who initiated them, and whether they succeeded — producing structured JSONL telemetry. It sits below the agent at the npm script-shell level, providing an audit trail that doesn't depend on agent self-reports.
8
+
9
+ ## Quick Start
10
+
11
+ ```bash
12
+ # Install as a dev dependency
13
+ npm install -D @lousy-agents/agent-shell
14
+
15
+ # Configure npm to use agent-shell as the script shell
16
+ echo 'script-shell=./node_modules/.bin/agent-shell' >> .npmrc
17
+
18
+ # Add event storage to .gitignore
19
+ echo '.agent-shell/' >> .gitignore
20
+
21
+ # Verify it's working
22
+ npx agent-shell --version
23
+
24
+ # Run any npm script — events are recorded automatically
25
+ npm test
26
+ ```
27
+
28
+ ## How It Works
29
+
30
+ When npm runs a script (e.g., `npm test`), it invokes the configured `script-shell` instead of `/bin/sh`. agent-shell wraps the real shell:
31
+
32
+ 1. Captures context (actor, environment, timestamps)
33
+ 2. Spawns `/bin/sh -c <command>` — your script runs normally
34
+ 3. Records a JSONL event with execution metadata
35
+ 4. Propagates the exit code unchanged
36
+
37
+ Scripts behave identically — same output, same exit codes, same signals. agent-shell adds observability without changing behavior.
38
+
39
+ ## Telemetry Schema (v1)
40
+
41
+ Each script execution produces one JSON line:
42
+
43
+ ```json
44
+ {
45
+ "v": 1,
46
+ "session_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
47
+ "event": "script_end",
48
+ "script": "test",
49
+ "command": "vitest run",
50
+ "package": "my-app",
51
+ "package_version": "1.2.0",
52
+ "actor": "claude-code",
53
+ "exit_code": 1,
54
+ "signal": null,
55
+ "duration_ms": 3420,
56
+ "timestamp": "2026-03-08T14:32:01.000Z",
57
+ "env": {
58
+ "NODE_ENV": "test",
59
+ "CI": "true"
60
+ },
61
+ "tags": {
62
+ "pr": "1234"
63
+ }
64
+ }
65
+ ```
66
+
67
+ ### Field Reference
68
+
69
+ | Field | Type | Description |
70
+ |-------|------|-------------|
71
+ | `v` | number | Schema version (always `1`) |
72
+ | `session_id` | string | UUID identifying the session |
73
+ | `event` | string | `script_end` or `shim_error` |
74
+ | `script` | string? | npm lifecycle event name (e.g., `test`, `build`) |
75
+ | `command` | string | The actual command executed |
76
+ | `package` | string? | Package name from `package.json` |
77
+ | `package_version` | string? | Package version |
78
+ | `actor` | string | Who initiated it: `human`, `ci`, `claude-code`, `copilot`, or custom |
79
+ | `exit_code` | number | Child process exit code |
80
+ | `signal` | string \| null | Signal name if killed (e.g., `SIGINT`), otherwise `null` |
81
+ | `duration_ms` | number | Wall-clock execution time in milliseconds |
82
+ | `timestamp` | string | ISO 8601 completion timestamp |
83
+ | `env` | object | Captured environment variables (allowlisted) |
84
+ | `tags` | object | Custom tags from `AGENTSHELL_TAG_*` variables |
85
+
86
+ Fields `script`, `package`, and `package_version` are only present when running within an npm context.
87
+
88
+ ## Actor Detection
89
+
90
+ agent-shell classifies who initiated each script execution:
91
+
92
+ | Priority | Condition | Actor |
93
+ |----------|-----------|-------|
94
+ | 1 | `AGENTSHELL_ACTOR` is set | Value of the variable |
95
+ | 2 | `GITHUB_ACTIONS=true` | `ci` |
96
+ | 3 | `CLAUDE_CODE` is set | `claude-code` |
97
+ | 4 | `COPILOT_AGENT` is set | `copilot` |
98
+ | 5 | No match | `human` |
99
+
100
+ ## Querying Events
101
+
102
+ Use the `log` subcommand to query execution history:
103
+
104
+ ```bash
105
+ # Show events from the most recent session
106
+ agent-shell log
107
+
108
+ # Show events from the last 2 hours
109
+ agent-shell log --last 2h
110
+
111
+ # Show only failures
112
+ agent-shell log --failures
113
+
114
+ # Filter by actor
115
+ agent-shell log --actor claude-code
116
+
117
+ # Filter by script name
118
+ agent-shell log --script test
119
+
120
+ # Combine filters
121
+ agent-shell log --actor claude-code --failures --last 2h
122
+
123
+ # Output as JSON (for scripting)
124
+ agent-shell log --json
125
+
126
+ # List all sessions
127
+ agent-shell log --list-sessions
128
+ ```
129
+
130
+ ### Duration Formats
131
+
132
+ | Format | Meaning |
133
+ |--------|---------|
134
+ | `30m` | Last 30 minutes |
135
+ | `2h` | Last 2 hours |
136
+ | `1d` | Last 1 day |
137
+
138
+ ## Environment Variables
139
+
140
+ | Variable | Purpose | Default |
141
+ |----------|---------|---------|
142
+ | `AGENTSHELL_PASSTHROUGH` | Set to `1` to bypass all instrumentation | Unset (instrumentation active) |
143
+ | `AGENTSHELL_ACTOR` | Override automatic actor detection | Unset (heuristic detection) |
144
+ | `AGENTSHELL_SESSION_ID` | Shared session ID for event correlation | Unset (fresh UUID per invocation) |
145
+ | `AGENTSHELL_LOG_DIR` | Override event file directory | `.agent-shell/events/` |
146
+ | `AGENTSHELL_TAG_<key>` | Attach custom key=value metadata to events | None |
147
+
148
+ ### Custom Tags
149
+
150
+ Set `AGENTSHELL_TAG_*` environment variables to attach metadata:
151
+
152
+ ```bash
153
+ AGENTSHELL_TAG_pr=1234 AGENTSHELL_TAG_task=fix-auth npm test
154
+ ```
155
+
156
+ Produces: `"tags": { "pr": "1234", "task": "fix-auth" }`
157
+
158
+ ### Session Correlation
159
+
160
+ By default, each script invocation gets a fresh session ID. To correlate events across a workflow:
161
+
162
+ ```bash
163
+ export AGENTSHELL_SESSION_ID=$(uuidgen)
164
+ npm run lint && npm test && npm run build
165
+ # All three events share the same session_id
166
+ ```
167
+
168
+ ## Scope
169
+
170
+ - **npm only** — Configured via `.npmrc` `script-shell`. Other package managers (yarn, pnpm, bun) are not supported.
171
+ - **POSIX only** — macOS and Linux. Windows requires WSL or Git Bash.
172
+
173
+ ## Troubleshooting
174
+
175
+ ### npm fails with "script-shell not found"
176
+
177
+ Your `.npmrc` references `agent-shell` but it's not installed:
178
+
179
+ ```bash
180
+ # Fix: install the package
181
+ npm install -D @lousy-agents/agent-shell
182
+
183
+ # Or: bypass temporarily
184
+ AGENTSHELL_PASSTHROUGH=1 npm test
185
+ ```
186
+
187
+ ### Bypass all instrumentation
188
+
189
+ Set the passthrough escape hatch — zero overhead, zero recording:
190
+
191
+ ```bash
192
+ AGENTSHELL_PASSTHROUGH=1 npm test
193
+ ```
194
+
195
+ ### Remove instrumentation entirely
196
+
197
+ Remove the `script-shell` line from `.npmrc`:
198
+
199
+ ```bash
200
+ # Edit .npmrc and delete this line:
201
+ # script-shell=./node_modules/.bin/agent-shell
202
+
203
+ # Or remove it with sed:
204
+ sed -i '' '/script-shell/d' .npmrc
205
+ ```
206
+
207
+ Then optionally uninstall:
208
+
209
+ ```bash
210
+ npm uninstall @lousy-agents/agent-shell
211
+ ```
212
+
213
+ ### Verify the shim is active
214
+
215
+ ```bash
216
+ npx agent-shell --version
217
+ ```
218
+
219
+ ### Clean up recorded events
220
+
221
+ ```bash
222
+ rm -rf .agent-shell/
223
+ ```
224
+
225
+ ## .gitignore
226
+
227
+ Add `.agent-shell/` to your `.gitignore`:
228
+
229
+ ```
230
+ # agent-shell telemetry
231
+ .agent-shell/
232
+ ```