Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/BoostServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ public function register(): void
$cacheKey = 'boost.roster.scan';
$lastModified = max(array_map(fn (string $path): int|false => file_exists($path) ? filemtime($path) : 0, $lockFiles));

$cached = cache()->get($cacheKey);
$cached = rescue(fn () => cache()->get($cacheKey), report: false);

if ($cached && isset($cached['timestamp']) && $cached['timestamp'] >= $lastModified) {
return $cached['roster'];
}

$roster = Roster::scan(base_path());
cache()->put($cacheKey, [

rescue(fn () => cache()->put($cacheKey, [
'roster' => $roster,
'timestamp' => time(),
], now()->addHours(24));
], now()->addHours(24)), report: false);

return $roster;
});
Expand Down
16 changes: 10 additions & 6 deletions src/Mcp/Tools/DatabaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ public function handle(Request $request): Response
(int) $includeRoutines
);

$schema = Cache::remember($cacheKey, 20, fn (): array => $this->getDatabaseStructure(
$connection,
$filter,
$includeViews,
$includeRoutines
));
$schema = rescue(
fn () => Cache::remember($cacheKey, 20, fn (): array => $this->getDatabaseStructure(
$connection,
$filter,
$includeViews,
$includeRoutines
)),
fn (): array => $this->getDatabaseStructure($connection, $filter, $includeViews, $includeRoutines),
report: false
);

return Response::json($schema);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mcp/Tools/LastError.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function __construct()
if (! self::$listenerRegistered) {
Log::listen(function (MessageLogged $event): void {
if ($event->level === 'error') {
Cache::forever('boost:last_error', [
rescue(fn () => Cache::forever('boost:last_error', [
'timestamp' => now()->toDateTimeString(),
'level' => $event->level,
'message' => $event->message,
'context' => [], // $event->context,
]);
]), report: false);
}
});

Expand All @@ -54,7 +54,7 @@ public function handle(Request $request): Response
{
// First, attempt to retrieve the cached last error captured during runtime.
// This works even if the log driver isn't a file driver, so is the preferred approach
$cached = Cache::get('boost:last_error');
$cached = rescue(fn () => Cache::get('boost:last_error'), report: false);

if ($cached) {
$entry = "[{$cached['timestamp']}] {$cached['level']}: {$cached['message']}";
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/Mcp/Tools/DatabaseSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@
});
});

test('it falls back to direct query when cache is unreachable', function (): void {
Cache::shouldReceive('remember')
->andThrow(new RuntimeException('Cache driver unreachable'));

$tool = new DatabaseSchema;
$response = $tool->handle(new Request([]));

expect($response)->isToolResult()
->toolHasNoError()
->toolJsonContent(function (array $schemaArray): void {
expect($schemaArray)->toHaveKey('engine')
->and($schemaArray)->toHaveKey('tables')
->and($schemaArray['tables'])->toHaveKey('examples');
});
});

test('it filters tables by name', function (): void {
Schema::create('users', function (Blueprint $table): void {
$table->id();
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/Mcp/Tools/LastErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@
->toolTextContains('Daily driver error');
});

it('falls back to log file when cache is unreachable', function (): void {
Cache::shouldReceive('get')
->with('boost:last_error')
->andThrow(new RuntimeException('Cache driver unreachable'));

$logFile = storage_path('logs'.DIRECTORY_SEPARATOR.'laravel.log');

Config::set('logging.default', 'single');
Config::set('logging.channels.single', [
'driver' => 'single',
'path' => $logFile,
]);

File::put($logFile, '[2024-01-15 10:00:00] local.ERROR: Fallback error from log file');

$tool = new LastError;
$response = $tool->handle(new Request([]));

expect($response)->isToolResult()
->toolHasNoError()
->toolTextContains('Fallback error from log file');
});

it('does not return info or warning entries', function (): void {
$logFile = storage_path('logs'.DIRECTORY_SEPARATOR.'laravel.log');

Expand Down