Skip to content

Commit 5bc2bfa

Browse files
Complete phase17-session04-session-management-and-process-fixes: proper-lockfile replacement, transcript path threading, CLI lifecycle hardening, agent runtime fixes
- Replaced proper-lockfile with in-process Promise chain mutex for auth store locking - Fixed multi-agent transcript path resolution with explicit agentId threading - Hardened CLI lifecycle: clean exit, stdin closure for non-pty, pending promise rejection - Fixed 5 agent runtime edge cases: NO_REPLY suppression, timeout replies, file_path alias, session model override, tool result media delivery - Version: 0.1.162 -> 0.1.163
1 parent 0d661dd commit 5bc2bfa

21 files changed

Lines changed: 316 additions & 301 deletions

File tree

.spec_system/PRD/PRD.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ This PRD delivers via phases. Each phase is implemented via multiple 2-4 hour se
110110
| Phase | Name | Sessions | Status |
111111
|-------|------|----------|--------|
112112
| 16 | Critical Security Hardening II | 5 | Complete |
113-
| 17 | Core Runtime Stability | 5 | In Progress (1/5) |
113+
| 17 | Core Runtime Stability | 5 | In Progress (4/5) |
114114
| 18 | Telegram and Messaging Pipeline | 4 | Pending |
115115
| 19 | Cron, Memory, and Config | 5 | Pending |
116116
| 20 | Plugins, Media, CLI, and Infrastructure | 5 | Pending |
@@ -207,9 +207,9 @@ Use `diff` between `.001_ORIGINAL/src/` and `src/` to identify exact changes nee
207207
| Session | Name | Status |
208208
|---------|------|--------|
209209
| 01 | Research and Triage | Complete (2026-02-23) |
210-
| 02 | Gateway Session and Routing Fixes | Pending |
211-
| 03 | Agent Compaction, Deadlock, and Token Fixes | Pending |
212-
| 04 | Session Management and Process Fixes | Pending |
210+
| 02 | Gateway Session and Routing Fixes | Complete (2026-02-23) |
211+
| 03 | Agent Compaction, Deadlock, and Token Fixes | Complete (2026-02-23) |
212+
| 04 | Session Management and Process Fixes | Complete (2026-02-23) |
213213
| 05 | Memory Bounding and Validation | Pending |
214214

215215
---

.spec_system/PRD/phase_17/PRD_phase_17.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
**Status**: In Progress
44
**Sessions**: 5
5-
**Completed**: 3/5 (60%)
5+
**Completed**: 4/5 (80%)
66

77
---
88

@@ -22,7 +22,7 @@
2222
| 01 | Research and Triage | Complete | 2026-02-23 |
2323
| 02 | Gateway Session and Routing Fixes | Complete | 2026-02-23 |
2424
| 03 | Agent Compaction, Deadlock, and Token Fixes | Complete | 2026-02-23 |
25-
| 04 | Session Management and Process Fixes | Pending | - |
25+
| 04 | Session Management and Process Fixes | Complete | 2026-02-23 |
2626
| 05 | Memory Bounding and Validation | Pending | - |
2727

2828
---

.spec_system/specs/phase16-session02-network-ssrf-and-filesystem-hardening/IMPLEMENTATION_SUMMARY.md

Lines changed: 0 additions & 92 deletions
This file was deleted.

.spec_system/specs/phase16-session03-input-sanitization-and-auth-hardening/IMPLEMENTATION_SUMMARY.md

Lines changed: 0 additions & 107 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Implementation Summary
2+
3+
**Session ID**: `phase17-session04-session-management-and-process-fixes`
4+
**Completed**: 2026-02-23
5+
**Duration**: ~4 hours
6+
7+
---
8+
9+
## Overview
10+
11+
Replaced `proper-lockfile` dependency with an in-process Promise chain mutex for auth store locking, fixed multi-agent transcript path resolution with explicit agent context threading, hardened CLI process lifecycle (clean exit, stdin closure, pending promise rejection), and fixed 5 agent runtime edge cases (NO_REPLY suppression, timeout replies, file_path alias, session model override, tool result media delivery).
12+
13+
---
14+
15+
## Deliverables
16+
17+
### Files Created
18+
| File | Purpose | Lines |
19+
|------|---------|-------|
20+
| `src/agents/auth-profiles/mutex.ts` | Promise chain mutex for auth store serialization | ~41 |
21+
| `src/agents/auth-profiles/mutex.test.ts` | Unit tests for mutex (concurrency, errors, sequential, nested) | ~90 |
22+
23+
### Files Modified
24+
| File | Changes |
25+
|------|---------|
26+
| `src/agents/auth-profiles/oauth.ts` | Replaced `proper-lockfile` lock with `oauthMutex.acquire()` |
27+
| `src/agents/auth-profiles/store.ts` | Replaced `proper-lockfile` lock with `storeMutex.acquire()` |
28+
| `src/config/sessions/paths.ts` | `resolveSessionFilePath()` normalizes relative paths to absolute via `path.resolve()` |
29+
| `src/gateway/session-utils.fs.ts` | Added `agentId` parameter to `readSessionMessages()` and transcript candidate resolution |
30+
| `src/cli/run-main.ts` | Added `process.exit(0)` after `program.parseAsync()` resolves |
31+
| `src/cli/gateway-cli/run-loop.ts` | Added `process.stdin.unref()` for non-TTY environments with safety guard |
32+
| `src/process/command-queue.ts` | `clearCommandLane()` now rejects pending promises with descriptive error |
33+
| `src/agents/session-transcript-repair.ts` | Normalize absolute sessionFile paths via `path.resolve()` |
34+
| `src/agents/pi-embedded-runner/run.ts` | NO_REPLY suppression when message tool sent text; timeout reply on empty runs |
35+
| `src/agents/pi-embedded-runner/model.ts` | Session model override support (already handled by model-selection.ts) |
36+
| `src/agents/pi-tools.read.ts` | `wrapSandboxPathGuard()` checks both `path` and `file_path` keys |
37+
| `src/auto-reply/reply/session-usage.ts` | Tool result media delivery path adjustment |
38+
| `src/agents/pi-embedded-runner/compact.ts` | Reject pending promises on lane clear |
39+
| `src/agents/pi-embedded-subscribe.handlers.tools.ts` | Media-only delivery path when verbose output suppressed |
40+
| `package.json` | Removed `proper-lockfile` and `@types/proper-lockfile` dependencies |
41+
42+
### Files Deleted
43+
| File | Reason |
44+
|------|--------|
45+
| `src/types/proper-lockfile.d.ts` | Type declarations no longer needed after dependency removal |
46+
47+
---
48+
49+
## Technical Decisions
50+
51+
1. **Promise chain mutex over file locks**: Chose in-process Promise chain serialization over filesystem-based locking. Zero dependencies, no stale-lock edge cases, no filesystem overhead. Sufficient for single-process crocbot deployment.
52+
2. **Media delivery fix in handlers.tools.ts**: Spec referenced `session-usage.ts` but actual fix was in `handleToolExecutionEnd` in the tool handlers file where `shouldEmitToolOutput()` gates delivery. Added `else if` branch extracting media via `splitMediaFromOutput`.
53+
3. **stdin.unref safety guard**: `process.stdin.unref()` not available in all Node.js process configurations (test child processes). Guarded with `typeof process.stdin.unref === "function"` check.
54+
4. **Session model override already handled**: `auto-reply/reply/model-selection.ts` already resolves `entry.modelOverride`/`entry.providerOverride` before calling the embedded runner. No code changes needed.
55+
56+
---
57+
58+
## Test Results
59+
60+
| Metric | Value |
61+
|--------|-------|
62+
| Test Files | 40 |
63+
| Tests | 300 |
64+
| Passed | 300 |
65+
| Failed | 0 |
66+
| Skipped | 0 |
67+
| Duration | 7.16s |
68+
69+
---
70+
71+
## Lessons Learned
72+
73+
1. Always verify the actual code path where a fix is needed rather than relying on spec file references -- the media delivery fix was in handlers.tools.ts, not session-usage.ts.
74+
2. Node.js stdin may lack `unref()` in certain child process configurations; always guard platform-specific calls.
75+
76+
---
77+
78+
## Future Considerations
79+
80+
Items for future sessions:
81+
1. Prompt token bloat reduction (#29) -- deferred due to 23-file change scope
82+
2. Gateway restart deferral (#12) -- deferred to Phase 21 (21-file, +974-line change)
83+
3. Optimize sessions/ws/routing (#11) -- requires new resolve-route.ts, stretch goal
84+
85+
---
86+
87+
## Session Statistics
88+
89+
- **Tasks**: 20 completed
90+
- **Files Created**: 2
91+
- **Files Modified**: 15
92+
- **Files Deleted**: 1
93+
- **Tests Added**: 4 (mutex unit tests)
94+
- **Blockers**: 0

.spec_system/state.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"name": "Core Runtime Stability",
1717
"description": "Fix gateway session management, agent runtime deadlocks and token drift, session/process lock races, and bound memory growth vectors.",
1818
"status": "active",
19-
"sessions_completed": 3,
19+
"sessions_completed": 4,
2020
"session_count": 5
2121
},
2222
"18": {
@@ -56,7 +56,8 @@
5656
"phase16-session05-acp-fixes-and-security-validation",
5757
"phase17-session01-research-and-triage",
5858
"phase17-session02-gateway-session-and-routing-fixes",
59-
"phase17-session03-agent-compaction-deadlock-and-token-fixes"
59+
"phase17-session03-agent-compaction-deadlock-and-token-fixes",
60+
"phase17-session04-session-management-and-process-fixes"
6061
],
6162
"next_session_history": [
6263
{
@@ -98,6 +99,11 @@
9899
"date": "2026-02-23",
99100
"session": "phase17-session03-agent-compaction-deadlock-and-token-fixes",
100101
"status": "completed"
102+
},
103+
{
104+
"date": "2026-02-23",
105+
"session": "phase17-session04-session-management-and-process-fixes",
106+
"status": "completed"
101107
}
102108
]
103109
}

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crocbot",
3-
"version": "0.1.162",
3+
"version": "0.1.163",
44
"description": "Telegram gateway CLI with Pi RPC agent",
55
"type": "module",
66
"main": "dist/index.js",
@@ -169,7 +169,6 @@
169169
"pdfjs-dist": "^5.4.530",
170170
"playwright-core": "1.58.0",
171171
"prom-client": "^15.1.3",
172-
"proper-lockfile": "^4.1.2",
173172
"sharp": "^0.34.5",
174173
"sqlite-vec": "0.1.7-alpha.2",
175174
"tar": "7.5.8",
@@ -189,7 +188,6 @@
189188
"@types/express": "^5.0.6",
190189
"@types/markdown-it": "^14.1.2",
191190
"@types/node": "^25.0.10",
192-
"@types/proper-lockfile": "^4.1.4",
193191
"@types/ws": "^8.18.1",
194192
"@typescript/native-preview": "7.0.0-dev.20260211.1",
195193
"@vitest/coverage-v8": "^4.0.18",

0 commit comments

Comments
 (0)