gsd-pi 2.33.1-dev.9bafd68 → 2.33.1-dev.b1c6556

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.
Files changed (26) hide show
  1. package/dist/resource-loader.js +3 -1
  2. package/dist/resources/extensions/gsd/auto-start.js +300 -288
  3. package/dist/resources/extensions/gsd/auto-worktree.js +71 -0
  4. package/dist/resources/extensions/gsd/auto.js +11 -1
  5. package/dist/resources/extensions/gsd/session-lock.js +13 -0
  6. package/package.json +1 -1
  7. package/packages/pi-ai/dist/models.generated.d.ts +5498 -3645
  8. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  9. package/packages/pi-ai/dist/models.generated.js +2595 -742
  10. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  11. package/packages/pi-ai/src/models.generated.ts +2852 -999
  12. package/packages/pi-coding-agent/dist/core/agent-session.d.ts.map +1 -1
  13. package/packages/pi-coding-agent/dist/core/agent-session.js +9 -0
  14. package/packages/pi-coding-agent/dist/core/agent-session.js.map +1 -1
  15. package/packages/pi-coding-agent/src/core/agent-session.ts +9 -0
  16. package/packages/pi-tui/dist/utils.d.ts.map +1 -1
  17. package/packages/pi-tui/dist/utils.js +32 -2
  18. package/packages/pi-tui/dist/utils.js.map +1 -1
  19. package/packages/pi-tui/src/utils.ts +28 -2
  20. package/src/resources/extensions/gsd/auto-start.ts +22 -10
  21. package/src/resources/extensions/gsd/auto-worktree.ts +71 -0
  22. package/src/resources/extensions/gsd/auto.ts +11 -0
  23. package/src/resources/extensions/gsd/session-lock.ts +11 -0
  24. package/src/resources/extensions/gsd/tests/auto-lock-creation.test.ts +37 -0
  25. package/src/resources/extensions/gsd/tests/repo-identity-worktree.test.ts +4 -4
  26. package/src/resources/extensions/gsd/tests/session-lock-regression.test.ts +64 -0
@@ -1359,6 +1359,15 @@ export class AgentSession {
1359
1359
  this.abortRetry();
1360
1360
  this.agent.abort();
1361
1361
  await this.agent.waitForIdle();
1362
+ // Ensure agent_end is emitted even when abort interrupts a tool call (#1414).
1363
+ // The agent may go idle without emitting agent_end if the abort happens
1364
+ // between tool execution and response processing.
1365
+ if (!this.isStreaming && this._extensionRunner) {
1366
+ await this._extensionRunner.emit({
1367
+ type: "agent_end",
1368
+ messages: this.agent.state.messages,
1369
+ });
1370
+ }
1362
1371
  }
1363
1372
 
1364
1373
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAYA;;GAEG;AACH,wBAAgB,YAAY,IAAI,IAAI,CAAC,SAAS,CAE7C;AAID;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEtD;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEvD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAsCjG;AAMD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAEtE;AAaD;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAC9B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,QAAQ,GAAE,MAAc,EACxB,GAAG,GAAE,OAAe,GAClB,MAAM,CAER;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,UAAQ,GAAG,MAAM,CAEpG;AAED,kFAAkF;AAClF,wBAAgB,cAAc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AAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAuBtE;AAaD;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAC9B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,QAAQ,GAAE,MAAc,EACxB,GAAG,GAAE,OAAe,GAClB,MAAM,CAER;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,UAAQ,GAAG,MAAM,CAEpG;AAED,kFAAkF;AAClF,wBAAgB,cAAc,CAC7B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,MAAM,UAAQ,GACZ;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAEjC;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC9B,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,WAAW,UAAQ,GACjB;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAE5E;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAOzG"}
@@ -72,7 +72,13 @@ export function extractAnsiCode(str, pos) {
72
72
  * Delegates to the native Rust implementation.
73
73
  */
74
74
  export function visibleWidth(str) {
75
- return nativeVisibleWidth(str);
75
+ try {
76
+ return nativeVisibleWidth(str);
77
+ }
78
+ catch {
79
+ // JS fallback — strip ANSI codes and return length (#1418)
80
+ return str.replace(/\x1b\[[0-9;]*m/g, "").length;
81
+ }
76
82
  }
77
83
  /**
78
84
  * Wrap text with ANSI codes preserved.
@@ -83,7 +89,31 @@ export function visibleWidth(str) {
83
89
  * @returns Array of wrapped lines (NOT padded to width)
84
90
  */
85
91
  export function wrapTextWithAnsi(text, width) {
86
- return nativeWrapTextWithAnsi(text, width);
92
+ try {
93
+ return nativeWrapTextWithAnsi(text, width);
94
+ }
95
+ catch {
96
+ // JS fallback when native addon is unavailable (e.g., glibc mismatch on older Linux) (#1418)
97
+ const lines = [];
98
+ for (const line of text.split("\n")) {
99
+ if (line.length <= width) {
100
+ lines.push(line);
101
+ }
102
+ else {
103
+ // Simple word-wrap without ANSI awareness
104
+ let remaining = line;
105
+ while (remaining.length > width) {
106
+ const breakAt = remaining.lastIndexOf(" ", width);
107
+ const splitPoint = breakAt > 0 ? breakAt : width;
108
+ lines.push(remaining.slice(0, splitPoint));
109
+ remaining = remaining.slice(splitPoint).trimStart();
110
+ }
111
+ if (remaining)
112
+ lines.push(remaining);
113
+ }
114
+ }
115
+ return lines;
116
+ }
87
117
  }
88
118
  /**
89
119
  * Map an ellipsis string to the native EllipsisKind enum value.
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,IAAI,kBAAkB,EAClC,gBAAgB,IAAI,sBAAsB,EAC1C,eAAe,IAAI,qBAAqB,EACxC,cAAc,IAAI,oBAAoB,EACtC,eAAe,IAAI,qBAAqB,EACxC,YAAY,GACZ,MAAM,kBAAkB,CAAC;AAE1B,uCAAuC;AACvC,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC,CAAC;AAE7E;;GAEG;AACH,MAAM,UAAU,YAAY;IAC3B,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,MAAM,iBAAiB,GAAG,sCAAsC,CAAC;AAEjE;;GAEG;AACH,MAA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GAAG,KAAK;IAEnB,OAAO,qBAAqB,CAAC,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC;AAClF,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY,EAAE,KAAa,EAAE,IAA8B;IAChG,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,GAAG,UAAU,CAAC,CAAC;IACtD,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;IAE1C,MAAM,WAAW,GAAG,IAAI,GAAG,OAAO,CAAC;IACnC,OAAO,IAAI,CAAC,WAAW,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["import {\n\tvisibleWidth as nativeVisibleWidth,\n\twrapTextWithAnsi as nativeWrapTextWithAnsi,\n\ttruncateToWidth as nativeTruncateToWidth,\n\tsliceWithWidth as nativeSliceWithWidth,\n\textractSegments as nativeExtractSegments,\n\tEllipsisKind,\n} from \"@gsd/native/text\";\n\n// Grapheme segmenter (shared instance)\nconst segmenter = new Intl.Segmenter(undefined, { granularity: \"grapheme\" });\n\n/**\n * Get the shared grapheme segmenter instance.\n */\nexport function getSegmenter(): Intl.Segmenter {\n\treturn segmenter;\n}\n\nconst PUNCTUATION_REGEX = /[(){}[\\]<>.,;:'\"!?+\\-=*/\\\\|&%^$#@~`]/;\n\n/**\n * Check if a character is whitespace.\n */\nexport function isWhitespaceChar(char: string): boolean {\n\treturn /\\s/.test(char);\n}\n\n/**\n * Check if a character is punctuation.\n */\nexport function isPunctuationChar(char: string): boolean {\n\treturn PUNCTUATION_REGEX.test(char);\n}\n\n/**\n * Extract ANSI escape sequences from a string at the given position.\n */\nexport function extractAnsiCode(str: string, pos: number): { code: string; length: number } | null {\n\tif (pos >= str.length || str[pos] !== \"\\x1b\") return null;\n\n\tconst next = str[pos + 1];\n\n\t// CSI sequence: ESC [ ... m/G/K/H/J\n\tif (next === \"[\") {\n\t\tlet j = pos + 2;\n\t\twhile (j < str.length && !/[mGKHJ]/.test(str[j]!)) j++;\n\t\tif (j < str.length) return { code: str.substring(pos, j + 1), length: j + 1 - pos };\n\t\treturn null;\n\t}\n\n\t// OSC sequence: ESC ] ... BEL or ESC ] ... ST (ESC \\)\n\t// Used for hyperlinks (OSC 8), window titles, etc.\n\tif (next === \"]\") {\n\t\tlet j = pos + 2;\n\t\twhile (j < str.length) {\n\t\t\tif (str[j] === \"\\x07\") return { code: str.substring(pos, j + 1), length: j + 1 - pos };\n\t\t\tif (str[j] === \"\\x1b\" && str[j + 1] === \"\\\\\") return { code: str.substring(pos, j + 2), length: j + 2 - pos };\n\t\t\tj++;\n\t\t}\n\t\treturn null;\n\t}\n\n\t// APC sequence: ESC _ ... BEL or ESC _ ... ST (ESC \\)\n\t// Used for cursor marker and application-specific commands\n\tif (next === \"_\") {\n\t\tlet j = pos + 2;\n\t\twhile (j < str.length) {\n\t\t\tif (str[j] === \"\\x07\") return { code: str.substring(pos, j + 1), length: j + 1 - pos };\n\t\t\tif (str[j] === \"\\x1b\" && str[j + 1] === \"\\\\\") return { code: str.substring(pos, j + 2), length: j + 2 - pos };\n\t\t\tj++;\n\t\t}\n\t\treturn null;\n\t}\n\n\treturn null;\n}\n\n// ---------------------------------------------------------------------------\n// Native text module wrappers\n// ---------------------------------------------------------------------------\n\n/**\n * Calculate the visible width of a string in terminal columns.\n * Delegates to the native Rust implementation.\n */\nexport function visibleWidth(str: string): number {\n\treturn nativeVisibleWidth(str);\n}\n\n/**\n * Wrap text with ANSI codes preserved.\n * Delegates to the native Rust implementation.\n *\n * @param text - Text to wrap (may contain ANSI codes and newlines)\n * @param width - Maximum visible width per line\n * @returns Array of wrapped lines (NOT padded to width)\n */\nexport function wrapTextWithAnsi(text: string, width: number): string[] {\n\treturn nativeWrapTextWithAnsi(text, width);\n}\n\n/**\n * Map an ellipsis string to the native EllipsisKind enum value.\n */\nfunction ellipsisStringToKind(ellipsis: string): number {\n\tif (ellipsis === \"\\u2026\") return EllipsisKind.Unicode;\n\tif (ellipsis === \"...\" || ellipsis === undefined) return EllipsisKind.Ascii;\n\tif (ellipsis === \"\") return EllipsisKind.None;\n\t// Default: \"...\" maps to Ascii\n\treturn EllipsisKind.Ascii;\n}\n\n/**\n * Truncate text to fit within a maximum visible width, adding ellipsis if needed.\n * Optionally pad with spaces to reach exactly maxWidth.\n * Delegates to the native Rust implementation.\n *\n * @param text - Text to truncate (may contain ANSI codes)\n * @param maxWidth - Maximum visible width\n * @param ellipsis - Ellipsis string to append when truncating (default: \"...\")\n * @param pad - If true, pad result with spaces to exactly maxWidth (default: false)\n * @returns Truncated text, optionally padded to exactly maxWidth\n */\nexport function truncateToWidth(\n\ttext: string,\n\tmaxWidth: number,\n\tellipsis: string = \"...\",\n\tpad: boolean = false,\n): string {\n\treturn nativeTruncateToWidth(text, maxWidth, ellipsisStringToKind(ellipsis), pad);\n}\n\n/**\n * Extract a range of visible columns from a line. Handles ANSI codes and wide chars.\n * @param strict - If true, exclude wide chars at boundary that would extend past the range\n */\nexport function sliceByColumn(line: string, startCol: number, length: number, strict = false): string {\n\treturn sliceWithWidth(line, startCol, length, strict).text;\n}\n\n/** Like sliceByColumn but also returns the actual visible width of the result. */\nexport function sliceWithWidth(\n\tline: string,\n\tstartCol: number,\n\tlength: number,\n\tstrict = false,\n): { text: string; width: number } {\n\treturn nativeSliceWithWidth(line, startCol, length, strict);\n}\n\n/**\n * Extract \"before\" and \"after\" segments from a line in a single pass.\n * Delegates to the native Rust implementation.\n */\nexport function extractSegments(\n\tline: string,\n\tbeforeEnd: number,\n\tafterStart: number,\n\tafterLen: number,\n\tstrictAfter = false,\n): { before: string; beforeWidth: number; after: string; afterWidth: number } {\n\treturn nativeExtractSegments(line, beforeEnd, afterStart, afterLen, strictAfter);\n}\n\n/**\n * Apply background color to a line, padding to full width.\n *\n * @param line - Line of text (may contain ANSI codes)\n * @param width - Total width to pad to\n * @param bgFn - Background color function\n * @returns Line with background applied and padded to width\n */\nexport function applyBackgroundToLine(line: string, width: number, bgFn: (text: string) => string): string {\n\tconst visibleLen = visibleWidth(line);\n\tconst paddingNeeded = Math.max(0, width - visibleLen);\n\tconst padding = \" \".repeat(paddingNeeded);\n\n\tconst withPadding = line + padding;\n\treturn bgFn(withPadding);\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,IAAI,kBAAkB,EAClC,gBAAgB,IAAI,sBAAsB,EAC1C,eAAe,IAAI,qBAAqB,EACxC,cAAc,IAAI,oBAAoB,EACtC,eAAe,IAAI,qBAAqB,EACxC,YAAY,GACZ,MAAM,kBAAkB,CAAC;AAE1B,uCAAuC;AACvC,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC,CAAC;AAE7E;;GAEG;AACH,MAAM,UAAU,YAAY;IAC3B,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,MAAM,iBAAiB,GAAG,sCAAsC,CAAC;AAEjE;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC5C,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC7C,OAAO,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACrC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW,EAAE,GAAW;IACvD,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,MAAM;QAAE,OAAO,IAAI,CAAC;IAE1D,MAAM,IAAI,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;IAE1B,oCAAoC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liceWithWidth,\n\textractSegments as nativeExtractSegments,\n\tEllipsisKind,\n} from \"@gsd/native/text\";\n\n// Grapheme segmenter (shared instance)\nconst segmenter = new Intl.Segmenter(undefined, { granularity: \"grapheme\" });\n\n/**\n * Get the shared grapheme segmenter instance.\n */\nexport function getSegmenter(): Intl.Segmenter {\n\treturn segmenter;\n}\n\nconst PUNCTUATION_REGEX = /[(){}[\\]<>.,;:'\"!?+\\-=*/\\\\|&%^$#@~`]/;\n\n/**\n * Check if a character is whitespace.\n */\nexport function isWhitespaceChar(char: string): boolean {\n\treturn /\\s/.test(char);\n}\n\n/**\n * Check if a character is punctuation.\n */\nexport function isPunctuationChar(char: string): boolean {\n\treturn PUNCTUATION_REGEX.test(char);\n}\n\n/**\n * Extract ANSI escape sequences from a string at the given position.\n */\nexport function extractAnsiCode(str: string, pos: number): { code: string; length: number } | null {\n\tif (pos >= str.length || str[pos] !== \"\\x1b\") return null;\n\n\tconst next = str[pos + 1];\n\n\t// CSI sequence: ESC [ ... m/G/K/H/J\n\tif (next === \"[\") {\n\t\tlet j = pos + 2;\n\t\twhile (j < str.length && !/[mGKHJ]/.test(str[j]!)) j++;\n\t\tif (j < str.length) return { code: str.substring(pos, j + 1), length: j + 1 - pos };\n\t\treturn null;\n\t}\n\n\t// OSC sequence: ESC ] ... BEL or ESC ] ... ST (ESC \\)\n\t// Used for hyperlinks (OSC 8), window titles, etc.\n\tif (next === \"]\") {\n\t\tlet j = pos + 2;\n\t\twhile (j < str.length) {\n\t\t\tif (str[j] === \"\\x07\") return { code: str.substring(pos, j + 1), length: j + 1 - pos };\n\t\t\tif (str[j] === \"\\x1b\" && str[j + 1] === \"\\\\\") return { code: str.substring(pos, j + 2), length: j + 2 - pos };\n\t\t\tj++;\n\t\t}\n\t\treturn null;\n\t}\n\n\t// APC sequence: ESC _ ... BEL or ESC _ ... ST (ESC \\)\n\t// Used for cursor marker and application-specific commands\n\tif (next === \"_\") {\n\t\tlet j = pos + 2;\n\t\twhile (j < str.length) {\n\t\t\tif (str[j] === \"\\x07\") return { code: str.substring(pos, j + 1), length: j + 1 - pos };\n\t\t\tif (str[j] === \"\\x1b\" && str[j + 1] === \"\\\\\") return { code: str.substring(pos, j + 2), length: j + 2 - pos };\n\t\t\tj++;\n\t\t}\n\t\treturn null;\n\t}\n\n\treturn null;\n}\n\n// ---------------------------------------------------------------------------\n// Native text module wrappers\n// ---------------------------------------------------------------------------\n\n/**\n * Calculate the visible width of a string in terminal columns.\n * Delegates to the native Rust implementation.\n */\nexport function visibleWidth(str: string): number {\n\ttry {\n\t\treturn nativeVisibleWidth(str);\n\t} catch {\n\t\t// JS fallback — strip ANSI codes and return length (#1418)\n\t\treturn str.replace(/\\x1b\\[[0-9;]*m/g, \"\").length;\n\t}\n}\n\n/**\n * Wrap text with ANSI codes preserved.\n * Delegates to the native Rust implementation.\n *\n * @param text - Text to wrap (may contain ANSI codes and newlines)\n * @param width - Maximum visible width per line\n * @returns Array of wrapped lines (NOT padded to width)\n */\nexport function wrapTextWithAnsi(text: string, width: number): string[] {\n\ttry {\n\t\treturn nativeWrapTextWithAnsi(text, width);\n\t} catch {\n\t\t// JS fallback when native addon is unavailable (e.g., glibc mismatch on older Linux) (#1418)\n\t\tconst lines: string[] = [];\n\t\tfor (const line of text.split(\"\\n\")) {\n\t\t\tif (line.length <= width) {\n\t\t\t\tlines.push(line);\n\t\t\t} else {\n\t\t\t\t// Simple word-wrap without ANSI awareness\n\t\t\t\tlet remaining = line;\n\t\t\t\twhile (remaining.length > width) {\n\t\t\t\t\tconst breakAt = remaining.lastIndexOf(\" \", width);\n\t\t\t\t\tconst splitPoint = breakAt > 0 ? breakAt : width;\n\t\t\t\t\tlines.push(remaining.slice(0, splitPoint));\n\t\t\t\t\tremaining = remaining.slice(splitPoint).trimStart();\n\t\t\t\t}\n\t\t\t\tif (remaining) lines.push(remaining);\n\t\t\t}\n\t\t}\n\t\treturn lines;\n\t}\n}\n\n/**\n * Map an ellipsis string to the native EllipsisKind enum value.\n */\nfunction ellipsisStringToKind(ellipsis: string): number {\n\tif (ellipsis === \"\\u2026\") return EllipsisKind.Unicode;\n\tif (ellipsis === \"...\" || ellipsis === undefined) return EllipsisKind.Ascii;\n\tif (ellipsis === \"\") return EllipsisKind.None;\n\t// Default: \"...\" maps to Ascii\n\treturn EllipsisKind.Ascii;\n}\n\n/**\n * Truncate text to fit within a maximum visible width, adding ellipsis if needed.\n * Optionally pad with spaces to reach exactly maxWidth.\n * Delegates to the native Rust implementation.\n *\n * @param text - Text to truncate (may contain ANSI codes)\n * @param maxWidth - Maximum visible width\n * @param ellipsis - Ellipsis string to append when truncating (default: \"...\")\n * @param pad - If true, pad result with spaces to exactly maxWidth (default: false)\n * @returns Truncated text, optionally padded to exactly maxWidth\n */\nexport function truncateToWidth(\n\ttext: string,\n\tmaxWidth: number,\n\tellipsis: string = \"...\",\n\tpad: boolean = false,\n): string {\n\treturn nativeTruncateToWidth(text, maxWidth, ellipsisStringToKind(ellipsis), pad);\n}\n\n/**\n * Extract a range of visible columns from a line. Handles ANSI codes and wide chars.\n * @param strict - If true, exclude wide chars at boundary that would extend past the range\n */\nexport function sliceByColumn(line: string, startCol: number, length: number, strict = false): string {\n\treturn sliceWithWidth(line, startCol, length, strict).text;\n}\n\n/** Like sliceByColumn but also returns the actual visible width of the result. */\nexport function sliceWithWidth(\n\tline: string,\n\tstartCol: number,\n\tlength: number,\n\tstrict = false,\n): { text: string; width: number } {\n\treturn nativeSliceWithWidth(line, startCol, length, strict);\n}\n\n/**\n * Extract \"before\" and \"after\" segments from a line in a single pass.\n * Delegates to the native Rust implementation.\n */\nexport function extractSegments(\n\tline: string,\n\tbeforeEnd: number,\n\tafterStart: number,\n\tafterLen: number,\n\tstrictAfter = false,\n): { before: string; beforeWidth: number; after: string; afterWidth: number } {\n\treturn nativeExtractSegments(line, beforeEnd, afterStart, afterLen, strictAfter);\n}\n\n/**\n * Apply background color to a line, padding to full width.\n *\n * @param line - Line of text (may contain ANSI codes)\n * @param width - Total width to pad to\n * @param bgFn - Background color function\n * @returns Line with background applied and padded to width\n */\nexport function applyBackgroundToLine(line: string, width: number, bgFn: (text: string) => string): string {\n\tconst visibleLen = visibleWidth(line);\n\tconst paddingNeeded = Math.max(0, width - visibleLen);\n\tconst padding = \" \".repeat(paddingNeeded);\n\n\tconst withPadding = line + padding;\n\treturn bgFn(withPadding);\n}\n"]}
@@ -85,7 +85,12 @@ export function extractAnsiCode(str: string, pos: number): { code: string; lengt
85
85
  * Delegates to the native Rust implementation.
86
86
  */
87
87
  export function visibleWidth(str: string): number {
88
- return nativeVisibleWidth(str);
88
+ try {
89
+ return nativeVisibleWidth(str);
90
+ } catch {
91
+ // JS fallback — strip ANSI codes and return length (#1418)
92
+ return str.replace(/\x1b\[[0-9;]*m/g, "").length;
93
+ }
89
94
  }
90
95
 
91
96
  /**
@@ -97,7 +102,28 @@ export function visibleWidth(str: string): number {
97
102
  * @returns Array of wrapped lines (NOT padded to width)
98
103
  */
99
104
  export function wrapTextWithAnsi(text: string, width: number): string[] {
100
- return nativeWrapTextWithAnsi(text, width);
105
+ try {
106
+ return nativeWrapTextWithAnsi(text, width);
107
+ } catch {
108
+ // JS fallback when native addon is unavailable (e.g., glibc mismatch on older Linux) (#1418)
109
+ const lines: string[] = [];
110
+ for (const line of text.split("\n")) {
111
+ if (line.length <= width) {
112
+ lines.push(line);
113
+ } else {
114
+ // Simple word-wrap without ANSI awareness
115
+ let remaining = line;
116
+ while (remaining.length > width) {
117
+ const breakAt = remaining.lastIndexOf(" ", width);
118
+ const splitPoint = breakAt > 0 ? breakAt : width;
119
+ lines.push(remaining.slice(0, splitPoint));
120
+ remaining = remaining.slice(splitPoint).trimStart();
121
+ }
122
+ if (remaining) lines.push(remaining);
123
+ }
124
+ }
125
+ return lines;
126
+ }
101
127
  }
102
128
 
103
129
  /**
@@ -103,13 +103,20 @@ export async function bootstrapAutoSession(
103
103
  return false;
104
104
  }
105
105
 
106
- // Ensure git repo exists
107
- if (!nativeIsRepo(base)) {
108
- const mainBranch = loadEffectiveGSDPreferences()?.preferences?.git?.main_branch || "main";
109
- nativeInit(base, mainBranch);
106
+ function releaseLockAndReturn(): false {
107
+ releaseSessionLock(base);
108
+ clearLock(base);
109
+ return false;
110
110
  }
111
111
 
112
- // Ensure .gitignore has baseline patterns
112
+ try {
113
+ // Ensure git repo exists
114
+ if (!nativeIsRepo(base)) {
115
+ const mainBranch = loadEffectiveGSDPreferences()?.preferences?.git?.main_branch || "main";
116
+ nativeInit(base, mainBranch);
117
+ }
118
+
119
+ // Ensure .gitignore has baseline patterns
113
120
  const gitPrefs = loadEffectiveGSDPreferences()?.preferences?.git;
114
121
  const manageGitignore = gitPrefs?.manage_gitignore;
115
122
  ensureGitignore(base, { manageGitignore });
@@ -251,10 +258,10 @@ export async function bootstrapAutoSession(
251
258
  "Discussion completed but no milestone context was written. Run /gsd to try the discussion again, or /gsd auto after creating the milestone manually.",
252
259
  "warning",
253
260
  );
254
- return false;
261
+ return releaseLockAndReturn();
255
262
  }
256
263
  } else {
257
- return false;
264
+ return releaseLockAndReturn();
258
265
  }
259
266
  }
260
267
 
@@ -276,7 +283,7 @@ export async function bootstrapAutoSession(
276
283
  "Discussion completed but milestone context is still missing. Run /gsd to try again.",
277
284
  "warning",
278
285
  );
279
- return false;
286
+ return releaseLockAndReturn();
280
287
  }
281
288
  }
282
289
  }
@@ -286,7 +293,7 @@ export async function bootstrapAutoSession(
286
293
  if (!state.activeMilestone) {
287
294
  const { showSmartEntry } = await import("./guided-flow.js");
288
295
  await showSmartEntry(ctx, pi, base, { step: requestedStepMode });
289
- return false;
296
+ return releaseLockAndReturn();
290
297
  }
291
298
 
292
299
  // ── Initialize session state ──
@@ -479,5 +486,10 @@ export async function bootstrapAutoSession(
479
486
  }
480
487
  } catch { /* non-fatal */ }
481
488
 
482
- return true;
489
+ return true;
490
+ } catch (err) {
491
+ releaseSessionLock(base);
492
+ clearLock(base);
493
+ throw err;
494
+ }
483
495
  }
@@ -162,6 +162,77 @@ export function syncGsdStateToWorktree(mainBasePath: string, worktreePath_: stri
162
162
  return { synced };
163
163
  }
164
164
 
165
+ /**
166
+ * Sync milestone artifacts from worktree back to the main external state directory.
167
+ * Called before milestone merge to ensure completion artifacts (SUMMARY, VALIDATION,
168
+ * updated ROADMAP) are visible from the project root (#1412).
169
+ *
170
+ * Only syncs .gsd/milestones/ content — root-level files (DECISIONS, REQUIREMENTS, etc.)
171
+ * are handled by the merge itself.
172
+ */
173
+ export function syncWorktreeStateBack(mainBasePath: string, worktreePath: string, milestoneId: string): { synced: string[] } {
174
+ const mainGsd = gsdRoot(mainBasePath);
175
+ const wtGsd = gsdRoot(worktreePath);
176
+ const synced: string[] = [];
177
+
178
+ // If both resolve to the same directory (symlink), no sync needed
179
+ try {
180
+ const mainResolved = realpathSync(mainGsd);
181
+ const wtResolved = realpathSync(wtGsd);
182
+ if (mainResolved === wtResolved) return { synced };
183
+ } catch {
184
+ // Can't resolve — proceed with sync
185
+ }
186
+
187
+ const wtMilestoneDir = join(wtGsd, "milestones", milestoneId);
188
+ const mainMilestoneDir = join(mainGsd, "milestones", milestoneId);
189
+
190
+ if (!existsSync(wtMilestoneDir)) return { synced };
191
+ mkdirSync(mainMilestoneDir, { recursive: true });
192
+
193
+ // Sync milestone-level files (SUMMARY, VALIDATION, ROADMAP, CONTEXT)
194
+ try {
195
+ for (const entry of readdirSync(wtMilestoneDir, { withFileTypes: true })) {
196
+ if (entry.isFile() && entry.name.endsWith(".md")) {
197
+ const src = join(wtMilestoneDir, entry.name);
198
+ const dst = join(mainMilestoneDir, entry.name);
199
+ try {
200
+ cpSync(src, dst, { force: true });
201
+ synced.push(`milestones/${milestoneId}/${entry.name}`);
202
+ } catch { /* non-fatal */ }
203
+ }
204
+ }
205
+ } catch { /* non-fatal */ }
206
+
207
+ // Sync slice-level files (summaries, UATs)
208
+ const wtSlicesDir = join(wtMilestoneDir, "slices");
209
+ const mainSlicesDir = join(mainMilestoneDir, "slices");
210
+ if (existsSync(wtSlicesDir)) {
211
+ try {
212
+ for (const sliceEntry of readdirSync(wtSlicesDir, { withFileTypes: true })) {
213
+ if (!sliceEntry.isDirectory()) continue;
214
+ const sid = sliceEntry.name;
215
+ const wtSliceDir = join(wtSlicesDir, sid);
216
+ const mainSliceDir = join(mainSlicesDir, sid);
217
+ mkdirSync(mainSliceDir, { recursive: true });
218
+
219
+ for (const fileEntry of readdirSync(wtSliceDir, { withFileTypes: true })) {
220
+ if (fileEntry.isFile() && fileEntry.name.endsWith(".md")) {
221
+ const src = join(wtSliceDir, fileEntry.name);
222
+ const dst = join(mainSliceDir, fileEntry.name);
223
+ try {
224
+ cpSync(src, dst, { force: true });
225
+ synced.push(`milestones/${milestoneId}/slices/${sid}/${fileEntry.name}`);
226
+ } catch { /* non-fatal */ }
227
+ }
228
+ }
229
+ }
230
+ } catch { /* non-fatal */ }
231
+ }
232
+
233
+ return { synced };
234
+ }
235
+
165
236
  // ─── Worktree Post-Create Hook (#597) ────────────────────────────────────────
166
237
 
167
238
  /**
@@ -131,6 +131,7 @@ import {
131
131
  getAutoWorktreeOriginalBase,
132
132
  mergeMilestoneToMain,
133
133
  autoWorktreeBranch,
134
+ syncWorktreeStateBack,
134
135
  } from "./auto-worktree.js";
135
136
  import { pruneQueueOrder } from "./queue-order.js";
136
137
  import { consumeSignal } from "./session-status-io.js";
@@ -377,6 +378,16 @@ function tryMergeMilestone(ctx: ExtensionContext, milestoneId: string, mode: "tr
377
378
  // Worktree merge path
378
379
  if (isInAutoWorktree(s.basePath) && s.originalBasePath) {
379
380
  try {
381
+ // Sync completion artifacts from worktree → external state before merge (#1412)
382
+ try {
383
+ const { synced } = syncWorktreeStateBack(s.originalBasePath, s.basePath, milestoneId);
384
+ if (synced.length > 0) {
385
+ debugLog("worktree-reverse-sync", { milestoneId, synced: synced.length });
386
+ }
387
+ } catch (syncErr) {
388
+ debugLog("worktree-reverse-sync-failed", { milestoneId, error: getErrorMessage(syncErr) });
389
+ }
390
+
380
391
  const roadmapPath = resolveMilestoneFile(s.originalBasePath, milestoneId, "ROADMAP");
381
392
  if (!roadmapPath) {
382
393
  teardownAutoWorktree(s.originalBasePath, milestoneId);
@@ -149,6 +149,16 @@ function ensureExitHandler(gsdDir: string): void {
149
149
  export function acquireSessionLock(basePath: string): SessionLockResult {
150
150
  const lp = lockPath(basePath);
151
151
 
152
+ // Re-entrant acquire on the same path: release our current OS lock first so
153
+ // proper-lockfile clears its update timer before we acquire a fresh lock.
154
+ if (_releaseFunction && _lockedPath === basePath) {
155
+ try { _releaseFunction(); } catch { /* may already be released */ }
156
+ _releaseFunction = null;
157
+ _lockedPath = null;
158
+ _lockPid = 0;
159
+ _lockCompromised = false;
160
+ }
161
+
152
162
  // Ensure the directory exists
153
163
  mkdirSync(dirname(lp), { recursive: true });
154
164
 
@@ -234,6 +244,7 @@ export function acquireSessionLock(basePath: string): SessionLockResult {
234
244
  _releaseFunction = release;
235
245
  _lockedPath = basePath;
236
246
  _lockPid = process.pid;
247
+ _lockCompromised = false;
237
248
 
238
249
  // Safety net — uses centralized handler to avoid double-registration
239
250
  ensureExitHandler(gsdDir);
@@ -1,10 +1,25 @@
1
1
  import test from "node:test";
2
2
  import assert from "node:assert/strict";
3
3
  import { mkdirSync, mkdtempSync, writeFileSync, existsSync, readFileSync, rmSync } from "node:fs";
4
+ import { createRequire } from "node:module";
4
5
  import { join } from "node:path";
5
6
  import { tmpdir } from "node:os";
6
7
 
7
8
  import { writeLock, readCrashLock, clearLock, isLockProcessAlive } from "../crash-recovery.ts";
9
+ import { acquireSessionLock, releaseSessionLock } from "../session-lock.ts";
10
+
11
+ const require = createRequire(import.meta.url);
12
+
13
+ function hasProperLockfile(): boolean {
14
+ try {
15
+ require("proper-lockfile");
16
+ return true;
17
+ } catch {
18
+ return false;
19
+ }
20
+ }
21
+
22
+ const properLockfileAvailable = hasProperLockfile();
8
23
 
9
24
  // ─── writeLock creates auto.lock in .gsd/ ────────────────────────────────
10
25
 
@@ -95,6 +110,28 @@ test("clearLock is safe when no lock file exists", () => {
95
110
  rmSync(dir, { recursive: true, force: true });
96
111
  });
97
112
 
113
+ test("bootstrap cleanup releases session lock artifacts", () => {
114
+ const dir = mkdtempSync(join(tmpdir(), "gsd-lock-test-"));
115
+ mkdirSync(join(dir, ".gsd"), { recursive: true });
116
+
117
+ try {
118
+ const result = acquireSessionLock(dir);
119
+ assert.equal(result.acquired, true, "session lock should be acquired");
120
+ assert.ok(existsSync(join(dir, ".gsd", "auto.lock")), "auto.lock should exist while lock is held");
121
+ if (properLockfileAvailable) {
122
+ assert.ok(existsSync(join(dir, ".gsd.lock")), ".gsd.lock should exist while lock is held");
123
+ }
124
+
125
+ releaseSessionLock(dir);
126
+ clearLock(dir);
127
+
128
+ assert.ok(!existsSync(join(dir, ".gsd", "auto.lock")), "auto.lock should be removed by bootstrap cleanup");
129
+ assert.ok(!existsSync(join(dir, ".gsd.lock")), ".gsd.lock should be removed by bootstrap cleanup");
130
+ } finally {
131
+ rmSync(dir, { recursive: true, force: true });
132
+ }
133
+ });
134
+
98
135
  // ─── isLockProcessAlive detects live vs dead PIDs ────────────────────────
99
136
 
100
137
  test("isLockProcessAlive returns false for dead PID", () => {
@@ -13,8 +13,8 @@ function run(command: string, cwd: string): string {
13
13
  }
14
14
 
15
15
  async function main(): Promise<void> {
16
- const base = mkdtempSync(join(tmpdir(), "gsd-repo-identity-"));
17
- const stateDir = mkdtempSync(join(tmpdir(), "gsd-state-"));
16
+ const base = realpathSync(mkdtempSync(join(tmpdir(), "gsd-repo-identity-")));
17
+ const stateDir = realpathSync(mkdtempSync(join(tmpdir(), "gsd-state-")));
18
18
 
19
19
  try {
20
20
  process.env.GSD_STATE_DIR = stateDir;
@@ -38,7 +38,7 @@ async function main(): Promise<void> {
38
38
  assertEq(worktreeState, expectedExternalState, "worktree symlink target matches main repo external state dir");
39
39
  assertTrue(existsSync(join(worktreePath, ".gsd")), "worktree .gsd exists");
40
40
  assertTrue(lstatSync(join(worktreePath, ".gsd")).isSymbolicLink(), "worktree .gsd is a symlink");
41
- assertEq(realpathSync(join(worktreePath, ".gsd")), expectedExternalState, "worktree .gsd symlink resolves to main repo external state dir");
41
+ assertEq(realpathSync(join(worktreePath, ".gsd")), realpathSync(expectedExternalState), "worktree .gsd symlink resolves to main repo external state dir");
42
42
 
43
43
  console.log("\n=== ensureGsdSymlink heals stale worktree symlinks ===");
44
44
  const staleState = join(stateDir, "projects", "stale-worktree-state");
@@ -47,7 +47,7 @@ async function main(): Promise<void> {
47
47
  symlinkSync(staleState, join(worktreePath, ".gsd"), "junction");
48
48
  const healedState = ensureGsdSymlink(worktreePath);
49
49
  assertEq(healedState, expectedExternalState, "stale worktree symlink is repaired to canonical external state dir");
50
- assertEq(realpathSync(join(worktreePath, ".gsd")), expectedExternalState, "healed worktree symlink resolves to canonical external state dir");
50
+ assertEq(realpathSync(join(worktreePath, ".gsd")), realpathSync(expectedExternalState), "healed worktree symlink resolves to canonical external state dir");
51
51
 
52
52
  console.log("\n=== ensureGsdSymlink preserves worktree .gsd directories ===");
53
53
  rmSync(join(worktreePath, ".gsd"), { recursive: true, force: true });
@@ -11,6 +11,7 @@
11
11
  */
12
12
 
13
13
  import { mkdtempSync, mkdirSync, writeFileSync, rmSync, existsSync, readFileSync } from 'node:fs';
14
+ import { createRequire } from 'node:module';
14
15
  import { join } from 'node:path';
15
16
  import { tmpdir } from 'node:os';
16
17
 
@@ -26,6 +27,18 @@ import { gsdRoot } from '../paths.ts';
26
27
  import { createTestContext } from './test-helpers.ts';
27
28
 
28
29
  const { assertEq, assertTrue, report } = createTestContext();
30
+ const require = createRequire(import.meta.url);
31
+
32
+ function hasProperLockfile(): boolean {
33
+ try {
34
+ require("proper-lockfile");
35
+ return true;
36
+ } catch {
37
+ return false;
38
+ }
39
+ }
40
+
41
+ const properLockfileAvailable = hasProperLockfile();
29
42
 
30
43
  async function main(): Promise<void> {
31
44
 
@@ -207,6 +220,57 @@ async function main(): Promise<void> {
207
220
  }
208
221
  }
209
222
 
223
+ // ─── 9. Re-entrant acquisition without explicit release ───────────────
224
+ console.log('\n=== 9. re-entrant acquire without explicit release ===');
225
+ {
226
+ const base = mkdtempSync(join(tmpdir(), 'gsd-session-lock-'));
227
+ mkdirSync(join(base, '.gsd'), { recursive: true });
228
+
229
+ try {
230
+ const r1 = acquireSessionLock(base);
231
+ assertTrue(r1.acquired, 'first acquisition succeeds');
232
+
233
+ const r2 = acquireSessionLock(base);
234
+ assertTrue(r2.acquired, 're-entrant acquisition succeeds');
235
+
236
+ const valid = validateSessionLock(base);
237
+ assertTrue(valid, 're-entrant acquisition does not corrupt validation state');
238
+
239
+ releaseSessionLock(base);
240
+ } finally {
241
+ rmSync(base, { recursive: true, force: true });
242
+ }
243
+ }
244
+
245
+ // ─── 10. Re-entrant acquisition refreshes lock artifacts ──────────────
246
+ console.log('\n=== 10. re-entrant acquire refreshes lock artifacts ===');
247
+ {
248
+ const base = mkdtempSync(join(tmpdir(), 'gsd-session-lock-'));
249
+ mkdirSync(join(base, '.gsd'), { recursive: true });
250
+
251
+ try {
252
+ const r1 = acquireSessionLock(base);
253
+ assertTrue(r1.acquired, 'first acquisition succeeds');
254
+
255
+ const lockDir = gsdRoot(base) + '.lock';
256
+ if (properLockfileAvailable) {
257
+ assertTrue(existsSync(lockDir), '.gsd.lock/ exists after first acquisition');
258
+ }
259
+
260
+ const r2 = acquireSessionLock(base);
261
+ assertTrue(r2.acquired, 'second acquisition succeeds');
262
+ if (properLockfileAvailable) {
263
+ assertTrue(existsSync(lockDir), '.gsd.lock/ exists after re-entrant acquisition');
264
+ }
265
+ assertTrue(validateSessionLock(base), 'lock remains valid after re-entrant acquisition');
266
+
267
+ releaseSessionLock(base);
268
+ assertTrue(!existsSync(lockDir), '.gsd.lock/ is removed after release');
269
+ } finally {
270
+ rmSync(base, { recursive: true, force: true });
271
+ }
272
+ }
273
+
210
274
  report();
211
275
  }
212
276