Skip to content

Commit 7a1fff7

Browse files
committed
bugfix: fix InjectEnv resume/clear handling and remove getClaudeSessionId() from JvmScope
- InjectEnv now writes env vars to resumed session dir on source="resume" - InjectEnv routes source="clear" to handleStartup() for correct env file target - Removes getClaudeSessionId() from JvmScope; CLI tools now use ClaudeEnv, hook handlers use HookInput.getSessionId()
1 parent 59b6248 commit 7a1fff7

29 files changed

Lines changed: 511 additions & 366 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# State
22

3-
- **Status:** open
4-
- **Progress:** 0%
3+
- **Status:** closed
4+
- **Progress:** 100%
55
- **Dependencies:** []
66
- **Blocks:** []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"status":"closed","resolution":"implemented","target_branch":"main"}

client/src/main/java/io/github/cowwoc/cat/hooks/AbstractJvmScope.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package io.github.cowwoc.cat.hooks;
88

9+
import static io.github.cowwoc.requirements13.java.DefaultJavaValidators.requireThat;
10+
911
import io.github.cowwoc.cat.hooks.prompt.UserIssues;
1012
import io.github.cowwoc.cat.hooks.read.post.DetectSequentialTools;
1113
import io.github.cowwoc.cat.hooks.read.pre.PredictBatchOpportunity;
@@ -113,17 +115,20 @@ public Path getClaudeSessionsPath()
113115
}
114116

115117
/**
116-
* Returns the directory for the current session's tracking files.
118+
* Returns the directory for a session's tracking files.
117119
* <p>
118120
* Located at {@code {claudeConfigDir}/projects/{encodedProjectRoot}/{sessionId}/}.
119121
*
122+
* @param sessionId the session ID
120123
* @return the session directory path
124+
* @throws NullPointerException if {@code sessionId} is null
121125
* @throws IllegalStateException if this scope is closed
122126
*/
123127
@Override
124-
public Path getClaudeSessionPath()
128+
public Path getClaudeSessionPath(String sessionId)
125129
{
126-
return getClaudeSessionsPath().resolve(getClaudeSessionId());
130+
requireThat(sessionId, "sessionId").isNotBlank();
131+
return getClaudeSessionsPath().resolve(sessionId);
127132
}
128133

129134
/**
@@ -147,13 +152,16 @@ public Path getCatWorkPath()
147152
* <p>
148153
* Located at {@code {projectPath}/.cat/work/sessions/{sessionId}/}.
149154
*
155+
* @param sessionId the session ID
150156
* @return the session CAT directory path
157+
* @throws NullPointerException if {@code sessionId} is null
151158
* @throws IllegalStateException if this scope is closed
152159
*/
153160
@Override
154-
public Path getCatSessionPath()
161+
public Path getCatSessionPath(String sessionId)
155162
{
156-
return getCatWorkPath().resolve("sessions").resolve(getClaudeSessionId());
163+
requireThat(sessionId, "sessionId").isNotBlank();
164+
return getCatWorkPath().resolve("sessions").resolve(sessionId);
157165
}
158166

159167
/**

client/src/main/java/io/github/cowwoc/cat/hooks/JvmScope.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ public interface JvmScope extends AutoCloseable
7979
*/
8080
String getPluginPrefix();
8181

82-
/**
83-
* Returns the Claude session ID.
84-
*
85-
* @return the session ID
86-
* @throws AssertionError if the session ID is not configured
87-
* @throws IllegalStateException if this scope is closed
88-
*/
89-
String getClaudeSessionId();
90-
9182
/**
9283
* Returns the Claude config directory.
9384
* <p>
@@ -109,14 +100,16 @@ public interface JvmScope extends AutoCloseable
109100
Path getClaudeSessionsPath();
110101

111102
/**
112-
* Returns the directory for the current session's tracking files.
103+
* Returns the directory for a session's tracking files.
113104
* <p>
114105
* Located at {@code {claudeConfigDir}/projects/{encodedProjectRoot}/{sessionId}/}.
115106
*
107+
* @param sessionId the session ID
116108
* @return the session directory path
109+
* @throws NullPointerException if {@code sessionId} is null
117110
* @throws IllegalStateException if this scope is closed
118111
*/
119-
Path getClaudeSessionPath();
112+
Path getClaudeSessionPath(String sessionId);
120113

121114
/**
122115
* Returns the cross-session project CAT directory.
@@ -135,10 +128,12 @@ public interface JvmScope extends AutoCloseable
135128
* <p>
136129
* Located at {@code {projectPath}/.cat/work/sessions/{sessionId}/}.
137130
*
131+
* @param sessionId the session ID
138132
* @return the session CAT directory path
133+
* @throws NullPointerException if {@code sessionId} is null
139134
* @throws IllegalStateException if this scope is closed
140135
*/
141-
Path getCatSessionPath();
136+
Path getCatSessionPath(String sessionId);
142137

143138
/**
144139
* Returns the path to the Claude environment file.

client/src/main/java/io/github/cowwoc/cat/hooks/MainJvmScope.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ public final class MainJvmScope extends AbstractJvmScope
4141
return Path.of(configDir);
4242
return Path.of(System.getProperty("user.home"), ".claude");
4343
});
44-
private final ConcurrentLazyReference<String> claudeSessionId = ConcurrentLazyReference.create(() ->
45-
{
46-
String sessionId = System.getenv("CLAUDE_SESSION_ID");
47-
if (sessionId == null || sessionId.isEmpty())
48-
throw new AssertionError("CLAUDE_SESSION_ID is not set");
49-
return sessionId;
50-
});
5144
private final ConcurrentLazyReference<Path> claudeEnvFile = ConcurrentLazyReference.create(() ->
5245
{
5346
String envFile = System.getenv("CLAUDE_ENV_FILE");
@@ -101,13 +94,6 @@ public Path getClaudeConfigDir()
10194
return claudeConfigDir.getValue();
10295
}
10396

104-
@Override
105-
public String getClaudeSessionId()
106-
{
107-
ensureOpen();
108-
return claudeSessionId.getValue();
109-
}
110-
11197
@Override
11298
public Path getClaudeEnvFile()
11399
{

client/src/main/java/io/github/cowwoc/cat/hooks/PostToolUseFailureHook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public HookResult run(HookInput input, HookOutput output)
7575

7676
String sessionId = input.getSessionId();
7777
// Create handlers using sessionId from HookInput
78-
Path sessionDirectory = scope.getCatSessionPath();
78+
Path sessionDirectory = scope.getCatSessionPath(sessionId);
7979
List<PostToolHandler> handlers = List.of(
8080
new DetectRepeatedFailures(Clock.systemUTC(), sessionDirectory),
8181
new DetectPreprocessorFailure());

client/src/main/java/io/github/cowwoc/cat/hooks/PostToolUseHook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public HookResult run(HookInput input, HookOutput output)
8484

8585
// Create handlers using sessionId from HookInput
8686
Path claudeConfigDir = scope.getClaudeConfigDir();
87-
Path sessionDirectory = scope.getCatSessionPath();
87+
Path sessionDirectory = scope.getCatSessionPath(sessionId);
8888
List<PostToolHandler> handlers = List.of(
8989
new SetPendingAgentResult(scope),
9090
new ResetFailureCounter(sessionDirectory),

client/src/main/java/io/github/cowwoc/cat/hooks/SessionEndHook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public HookResult runWithProjectDir(HookInput input, HookOutput output, Path pro
114114

115115
cleanStaleLocks(messages);
116116

117-
new SessionEndHandler(scope).clean();
117+
new SessionEndHandler(scope).clean(sessionId);
118118

119119
return new HookResult(output.empty(), messages);
120120
}

0 commit comments

Comments
 (0)