1212 *
1313 * Options:
1414 * --session-dir <path> Path to session directory under $HOME (default: auto-detect ~/.codex, ~/.claude, ~/.qwen, or ~/.gemini)
15- * --pr-number <number> PR number to post comment to (optional, auto-detected from branch)
15+ * --pr-number <number> Open PR number to post comment to (optional, auto-detected from branch)
1616 * --repo <owner/repo> Source repository (optional, auto-detected from git remote)
1717 * --no-comment Skip posting PR comment
1818 * --dry-run Show what would be uploaded without actually uploading
@@ -107,7 +107,7 @@ const parseArgs = () => {
107107
108108Options:
109109 --session-dir <path> Path to session directory under $HOME
110- --pr-number <number> PR number to post comment to
110+ --pr-number <number> Open PR number to post comment to
111111 --repo <owner/repo> Source repository
112112 --no-comment Skip posting PR comment
113113 --dry-run Show what would be uploaded
@@ -245,20 +245,24 @@ const getPrNumberFromBranch = (repo, branch, ghEnv) => {
245245 return null ;
246246} ;
247247
248- const prExists = ( repo , prNumber , ghEnv ) => {
248+ const getPrState = ( repo , prNumber , ghEnv ) => {
249249 const result = ghCommand ( [
250250 "pr" ,
251251 "view" ,
252252 prNumber . toString ( ) ,
253253 "--repo" ,
254254 repo ,
255255 "--json" ,
256- "number " ,
256+ "state " ,
257257 "--jq" ,
258- ".number " ,
258+ ".state " ,
259259 ] , ghEnv ) ;
260260
261- return result . success && result . stdout === prNumber . toString ( ) ;
261+ return result . success ? result . stdout : null ;
262+ } ;
263+
264+ const prIsOpen = ( repo , prNumber , ghEnv ) => {
265+ return getPrState ( repo , prNumber , ghEnv ) === "OPEN" ;
262266} ;
263267
264268const getPrNumberFromWorkspaceBranch = ( branch ) => {
@@ -275,9 +279,12 @@ const findPrContext = (repos, branch, verbose, ghEnv) => {
275279 for ( const repo of repos ) {
276280 log ( verbose , `Checking open PR in ${ repo } for branch ${ branch } ` ) ;
277281 const prNumber = getPrNumberFromBranch ( repo , branch , ghEnv ) ;
278- if ( prNumber !== null ) {
282+ if ( prNumber !== null && prIsOpen ( repo , prNumber , ghEnv ) ) {
279283 return { repo, prNumber } ;
280284 }
285+ if ( prNumber !== null ) {
286+ log ( verbose , `Skipping PR #${ prNumber } in ${ repo } : PR is not open` ) ;
287+ }
281288 }
282289
283290 const workspacePrNumber = getPrNumberFromWorkspaceBranch ( branch ) ;
@@ -287,7 +294,7 @@ const findPrContext = (repos, branch, verbose, ghEnv) => {
287294
288295 for ( const repo of repos ) {
289296 log ( verbose , `Checking workspace PR #${ workspacePrNumber } in ${ repo } for branch ${ branch } ` ) ;
290- if ( prExists ( repo , workspacePrNumber , ghEnv ) ) {
297+ if ( prIsOpen ( repo , workspacePrNumber , ghEnv ) ) {
291298 return { repo, prNumber : workspacePrNumber } ;
292299 }
293300 }
@@ -492,7 +499,11 @@ const main = () => {
492499
493500 let prContext = null ;
494501 if ( args . prNumber !== null ) {
495- prContext = { repo : sourceRepo , prNumber : args . prNumber } ;
502+ if ( prIsOpen ( sourceRepo , args . prNumber , ghEnv ) ) {
503+ prContext = { repo : sourceRepo , prNumber : args . prNumber } ;
504+ } else {
505+ console . log ( `[session-backup] Skipping PR comment: PR #${ args . prNumber } is not open` ) ;
506+ }
496507 } else if ( args . postComment ) {
497508 prContext = findPrContext ( repoCandidates , branch , verbose , ghEnv ) ;
498509 }
0 commit comments