Files
rspade_system/app/RSpade/Ide/Services/handler-recovery.php
root f6fac6c4bc Fix bin/publish: copy docs.dist from project root
Fix bin/publish: use correct .env path for rspade_system
Fix bin/publish script: prevent grep exit code 1 from terminating script

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 02:08:33 +00:00

101 lines
3.2 KiB
PHP
Executable File

<?php
/**
* Session Recovery Mechanism for IDE Services
*
* This adds automatic session recovery when the VS Code extension
* gets a "Session not found" error. The extension should:
* 1. Catch 401 errors with "Session not found"
* 2. Call auth/create to get new session
* 3. Retry the original request
*/
// Enhancement to handler.php for session recovery
// The VS Code extension should implement this logic:
/*
JavaScript/TypeScript example for VS Code extension:
async function makeRequestWithRecovery(url, body, retryCount = 0) {
try {
const response = await makeSignedRequest(url, body);
if (response.status === 401 && response.body.includes('Session not found') && retryCount === 0) {
// Session expired or lost - create new one
console.log('[RSpade] Session lost, creating new session...');
await createNewSession();
// Retry the original request with new session
return makeRequestWithRecovery(url, body, retryCount + 1);
}
return response;
} catch (error) {
if (error.message.includes('Session not found') && retryCount === 0) {
console.log('[RSpade] Session recovery triggered');
await createNewSession();
return makeRequestWithRecovery(url, body, retryCount + 1);
}
throw error;
}
}
async function createNewSession() {
const response = await fetch(getBaseUrl() + '/_ide/service/auth/create', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
if (response.ok) {
const authData = await response.json();
// Store session credentials
await storeSessionCredentials(authData.session, authData.client_key);
console.log('[RSpade] New session created successfully');
} else {
throw new Error('Failed to create new session');
}
}
*/
// For server-side, we can add a session expiry check:
function check_session_validity($auth_file) {
if (!file_exists($auth_file)) {
return false;
}
$auth_data = json_decode(file_get_contents($auth_file), true);
// Sessions expire after 7 days of inactivity
$expiry_time = 7 * 24 * 60 * 60; // 7 days in seconds
$last_used = $auth_data['last_used'] ?? $auth_data['created'];
if (time() - $last_used > $expiry_time) {
// Session expired, delete it
unlink($auth_file);
return false;
}
// Update last used time
$auth_data['last_used'] = time();
file_put_contents($auth_file, json_encode($auth_data, JSON_PRETTY_PRINT));
return true;
}
// Add to handler.php error responses to indicate recovery is possible:
function error_response_with_recovery($message, $code = 500) {
http_response_code($code);
header('Content-Type: application/json');
$response = [
'error' => $message,
'code' => $code
];
// Add recovery hint for session errors
if ($message === 'Session not found') {
$response['recovery'] = 'Create new session via auth/create endpoint';
$response['recoverable'] = true;
}
echo json_encode($response);
exit;
}