Stop throwing for not-found; add Guard and request validation

Not-found lookups return null/false instead of throwing NotFoundException
across all services — a missing row is expected control flow, not an
exceptional condition. NotFoundException stays for embedded precondition
checks inside mutations (missing parent, invalid foreign reference).

Guard (copied from mic-check) enforces required arguments at the top of
every service method. A ported IModelValidator<T> framework validates
every request DTO at the API layer via a new ValidationEndpointFilter,
returning a 400 with field-level messages; services re-run the same
validator and throw for direct callers that bypass the API.

Endpoints translate null/false into 404 via a new ToApiResult() helper.
The agent toolset boundary translates the same nullable/bool results
into the tool-error text the model already expected.
This commit is contained in:
James Wampler
2026-08-06 15:13:36 -07:00
parent 04917fa09e
commit 40f93e40a8
45 changed files with 1523 additions and 377 deletions
+34 -10
View File
@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Novelly.Api.Common;
using Novelly.Api.Common.Validation;
using Novelly.Api.Data;
using Novelly.Api.Projects;
@@ -18,7 +19,8 @@ public class NovelAgentService(
IAgentModelClient model,
NovelAgentToolset toolset,
IOptions<AgentOptions> options,
ILogger<NovelAgentService> logger)
ILogger<NovelAgentService> logger,
IModelValidator<SendAgentMessageRequest> sendMessageValidator)
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
@@ -39,11 +41,18 @@ public class NovelAgentService(
.ToListAsync(ct);
}
public async Task<ConversationDto> GetConversationAsync(Guid conversationId, CancellationToken ct = default)
/// <summary>Null when no conversation has this id — a lookup miss is expected, not exceptional.</summary>
public async Task<ConversationDto?> GetConversationAsync(Guid conversationId, CancellationToken ct = default)
{
Guard.Default(conversationId, nameof(conversationId));
logger.LogInformation("Getting agent conversation {ConversationId}", conversationId);
var conversation = await LoadConversationAsync(conversationId, ct);
var conversation = await FindConversationAsync(conversationId, ct);
if (conversation is null)
{
return null;
}
return new ConversationDto(
conversation.Id,
@@ -53,13 +62,22 @@ public class NovelAgentService(
conversation.UpdatedAt);
}
public async Task DeleteConversationAsync(Guid conversationId, CancellationToken ct = default)
/// <summary>True if a conversation was deleted; false if no conversation had this id.</summary>
public async Task<bool> DeleteConversationAsync(Guid conversationId, CancellationToken ct = default)
{
Guard.Default(conversationId, nameof(conversationId));
logger.LogInformation("Deleting agent conversation {ConversationId}", conversationId);
var conversation = await LoadConversationAsync(conversationId, ct);
var conversation = await FindConversationAsync(conversationId, ct);
if (conversation is null)
{
return false;
}
db.Conversations.Remove(conversation);
await db.SaveChangesAsync(ct);
return true;
}
/// <summary>
@@ -69,12 +87,19 @@ public class NovelAgentService(
public async Task<AgentTurnDto> SendMessageAsync(
Guid projectId, SendAgentMessageRequest request, CancellationToken ct = default)
{
Guard.Default(projectId, nameof(projectId));
Guard.Null(request, nameof(request));
sendMessageValidator.Validate(request).ThrowIfInvalid();
logger.LogInformation(
"Sending agent message for project {ProjectId}, conversation {ConversationId}, message length {MessageLength}",
projectId, request.ConversationId, request.Message.Length);
var conversation = request.ConversationId is { } id
? await LoadConversationAsync(id, ct)
? await FindConversationAsync(id, ct)
// The id came from the request body, not the route — an unknown id here
// is bad input to this call, not a direct "fetch conversation" lookup.
?? throw new NotFoundException(nameof(AgentConversation), id)
: await StartConversationAsync(projectId, request.Message, ct);
// Persist the user's turn before running the loop. The tools save through the
@@ -204,9 +229,9 @@ public class NovelAgentService(
return conversation;
}
private async Task<AgentConversation> LoadConversationAsync(Guid conversationId, CancellationToken ct)
private async Task<AgentConversation?> FindConversationAsync(Guid conversationId, CancellationToken ct)
{
logger.LogDebug("Loading agent conversation {ConversationId}", conversationId);
logger.LogDebug("Finding agent conversation {ConversationId}", conversationId);
var conversation = await db.Conversations
.Include(c => c.Messages)
@@ -214,8 +239,7 @@ public class NovelAgentService(
if (conversation is null)
{
logger.LogWarning("AgentConversation {ConversationId} not found", conversationId);
throw new NotFoundException(nameof(AgentConversation), conversationId);
logger.LogInformation("AgentConversation {ConversationId} not found", conversationId);
}
return conversation;