@@ -13,16 +13,56 @@ import * as Path from "effect/Path";
1313/** Plugin name as declared in `.claude-plugin/plugin.json`. */
1414export const T3_CLAUDE_PLUGIN_NAME = "t3" ;
1515
16+ /** Where the shipped plugin lives, and how its `bin/t3` shim reaches this server's CLI. */
17+ export interface T3ClaudePluginLocation {
18+ /**
19+ * Directory handed to the SDK `plugins` option and scanned for the skill
20+ * list. Never a path inside an asar archive: `claude` is a plain subprocess
21+ * and cannot read one, so an archived plugin loads nowhere.
22+ */
23+ readonly pluginDir : string ;
24+ /**
25+ * Server CLI entry the plugin's `bin/t3` shim execs. Resolved next to the
26+ * running bundle, so in a packaged app it stays inside the archive — the
27+ * shim runs it through this process's own executable, which is asar-aware
28+ * when that executable is Electron.
29+ */
30+ readonly cliEntryPath ?: string ;
31+ }
32+
33+ /**
34+ * `…/app.asar/rest` → `…/app.asar.unpacked/rest`, or undefined when the path
35+ * is not inside an archive. Covers both the desktop `app.asar` and the Windows
36+ * `server.asar` sidecar.
37+ */
38+ export const asarUnpackedTwin = ( candidate : string ) : string | undefined => {
39+ const match = / \. a s a r [ / \\ ] / . exec ( candidate ) ;
40+ if ( ! match ) {
41+ return undefined ;
42+ }
43+ const boundary = match . index + ".asar" . length ;
44+ return `${ candidate . slice ( 0 , boundary ) } .unpacked${ candidate . slice ( boundary ) } ` ;
45+ } ;
46+
47+ /** CLI entry points, relative to the plugin directory: source layout, then bundled. */
48+ const CLI_ENTRY_CANDIDATES = [ "../src/bin.ts" , "../bin.mjs" ] as const ;
49+
1650/**
17- * Resolve the shipped plugin directory . Mirrors `resolveStaticDir`: prefer a
18- * bundled copy next to the built entry, fall back to the monorepo source
19- * layout, and return `undefined` when neither exists so a missing plugin
20- * never blocks session start.
51+ * Resolve the shipped plugin. Mirrors `resolveStaticDir`: prefer a bundled copy
52+ * next to the built entry, fall back to the monorepo source layout, and return
53+ * `undefined` when the plugin is unusable so a missing plugin never blocks
54+ * session start.
2155 */
22- export const resolveT3ClaudePluginDir = Effect . fn ( "resolveT3ClaudePluginDir" ) (
23- function * ( ) : Effect . fn . Return < string | undefined , never , FileSystem . FileSystem | Path . Path > {
56+ export const resolveT3ClaudePluginLocation = Effect . fn ( "resolveT3ClaudePluginLocation" ) (
57+ function * ( ) : Effect . fn . Return <
58+ T3ClaudePluginLocation | undefined ,
59+ never ,
60+ FileSystem . FileSystem | Path . Path
61+ > {
2462 const path = yield * Path . Path ;
2563 const fileSystem = yield * FileSystem . FileSystem ;
64+ const exists = ( target : string ) =>
65+ fileSystem . exists ( target ) . pipe ( Effect . orElseSucceed ( ( ) => false ) ) ;
2666
2767 const candidates = [
2868 // Bundled layout: dist/claude-plugin next to dist/bin.mjs.
@@ -32,12 +72,40 @@ export const resolveT3ClaudePluginDir = Effect.fn("resolveT3ClaudePluginDir")(
3272 ] ;
3373
3474 for ( const candidate of candidates ) {
35- const manifestExists = yield * fileSystem
36- . exists ( path . join ( candidate , ".claude-plugin" , "plugin.json" ) )
37- . pipe ( Effect . orElseSucceed ( ( ) => false ) ) ;
38- if ( manifestExists ) {
39- return candidate ;
75+ const manifestPath = path . join ( candidate , ".claude-plugin" , "plugin.json" ) ;
76+ if ( ! ( yield * exists ( manifestPath ) ) ) {
77+ continue ;
4078 }
79+
80+ // In a packaged app this candidate resolves through Electron's
81+ // asar-aware fs, so it exists for us but not for the `claude`
82+ // subprocess. Only the unpacked twin is a real directory; without it
83+ // the plugin would load nowhere while still showing up in the picker.
84+ const unpackedCandidate = asarUnpackedTwin ( candidate ) ;
85+ if ( unpackedCandidate !== undefined ) {
86+ if ( ! ( yield * exists ( path . join ( unpackedCandidate , ".claude-plugin" , "plugin.json" ) ) ) ) {
87+ yield * Effect . logWarning (
88+ `[claude] t3 plugin at ${ candidate } is packed inside an asar archive and was not unpacked — t3:handoff is unavailable.` ,
89+ ) ;
90+ return undefined ;
91+ }
92+ }
93+
94+ // Resolved from the archived candidate rather than the twin: only
95+ // claude-plugin is unpacked, the server bundle itself stays packed.
96+ let cliEntryPath : string | undefined ;
97+ for ( const relativeEntry of CLI_ENTRY_CANDIDATES ) {
98+ const entry = path . resolve ( path . join ( candidate , relativeEntry ) ) ;
99+ if ( yield * exists ( entry ) ) {
100+ cliEntryPath = entry ;
101+ break ;
102+ }
103+ }
104+
105+ return {
106+ pluginDir : unpackedCandidate ?? candidate ,
107+ ...( cliEntryPath ? { cliEntryPath } : { } ) ,
108+ } ;
41109 }
42110 return undefined ;
43111 } ,
0 commit comments