Add research page with MCP write tools, longer hold for repeat offenders and svn/hg decoys
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a25072e6fb
commit
e88dc415f9
48 files changed
+1983
-57
No files matched your search
@@ -0,0 +1,10 @@
|
||||
namespace Flypaper.Storage;
|
||||
|
||||
public static class LabLimits
|
||||
{
|
||||
public const int TitleLength = 200;
|
||||
public const int TextLength = 20_000;
|
||||
public const int CommitLength = 64;
|
||||
public const int StrategiesLength = 512;
|
||||
public const int StrategyNameLength = 64;
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Flypaper.Storage.Research;
|
||||
|
||||
public sealed record LabEntryInput(
|
||||
string? Title = null,
|
||||
LabEntryKind? Kind = null,
|
||||
string? Text = null,
|
||||
DateTime? Date = null,
|
||||
string? Commit = null,
|
||||
IReadOnlyList<string>? Strategies = null);
|
||||
|
||||
public sealed record IdeaInput(
|
||||
string? Title = null,
|
||||
IdeaLevel? Effort = null,
|
||||
IdeaLevel? Efficiency = null,
|
||||
string? Rationale = null,
|
||||
IdeaStatus? Status = null,
|
||||
DateTime? ImplementedOn = null,
|
||||
string? Commit = null);
|
||||
|
||||
public sealed partial class ResearchStore(IDbContextFactory<FlypaperDbContext> dbContextFactory, TimeProvider timeProvider)
|
||||
{
|
||||
public const int MaxLimit = 500;
|
||||
|
||||
[GeneratedRegex("^[0-9a-f]{4,64}$")]
|
||||
private static partial Regex CommitPattern();
|
||||
|
||||
[GeneratedRegex("^[a-z0-9][a-z0-9._-]*$")]
|
||||
private static partial Regex StrategyPattern();
|
||||
|
||||
public async Task<IReadOnlyList<LabEntryEntity>> ListEntriesAsync(
|
||||
LabEntryKind? kind = null,
|
||||
string? strategy = null,
|
||||
int limit = MaxLimit,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
var entries = db.LabEntries.AsNoTracking();
|
||||
if (kind is { } wanted)
|
||||
{
|
||||
entries = entries.Where(e => e.Kind == wanted);
|
||||
}
|
||||
|
||||
var rows = await entries.OrderByDescending(e => e.Date).ThenByDescending(e => e.Id).ToListAsync(ct);
|
||||
if (!string.IsNullOrWhiteSpace(strategy))
|
||||
{
|
||||
var name = strategy.Trim().ToLowerInvariant();
|
||||
rows = rows.Where(e => e.StrategyList.Contains(name, StringComparer.Ordinal)).ToList();
|
||||
}
|
||||
|
||||
return rows.Take(Math.Clamp(limit, 1, MaxLimit)).ToList();
|
||||
}
|
||||
|
||||
public async Task<LabEntryEntity> CreateEntryAsync(LabEntryInput input, CancellationToken ct = default)
|
||||
{
|
||||
var now = timeProvider.GetUtcNow().UtcDateTime;
|
||||
var entry = new LabEntryEntity
|
||||
{
|
||||
Title = RequireText(input.Title, "title", LabLimits.TitleLength),
|
||||
Kind = input.Kind ?? throw new ArgumentException("kind is required."),
|
||||
Text = RequireText(input.Text, "text", LabLimits.TextLength),
|
||||
Date = (input.Date ?? now).Date,
|
||||
Commit = ParseCommit(input.Commit),
|
||||
Strategies = JoinStrategies(input.Strategies),
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now
|
||||
};
|
||||
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
db.LabEntries.Add(entry);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return entry;
|
||||
}
|
||||
|
||||
public async Task<LabEntryEntity?> UpdateEntryAsync(int id, LabEntryInput input, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
var entry = await db.LabEntries.SingleOrDefaultAsync(e => e.Id == id, ct);
|
||||
if (entry is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (input.Title is not null)
|
||||
{
|
||||
entry.Title = RequireText(input.Title, "title", LabLimits.TitleLength);
|
||||
}
|
||||
|
||||
if (input.Kind is { } kind)
|
||||
{
|
||||
entry.Kind = kind;
|
||||
}
|
||||
|
||||
if (input.Text is not null)
|
||||
{
|
||||
entry.Text = RequireText(input.Text, "text", LabLimits.TextLength);
|
||||
}
|
||||
|
||||
if (input.Date is { } date)
|
||||
{
|
||||
entry.Date = date.Date;
|
||||
}
|
||||
|
||||
if (input.Commit is not null)
|
||||
{
|
||||
entry.Commit = ParseCommit(input.Commit);
|
||||
}
|
||||
|
||||
if (input.Strategies is not null)
|
||||
{
|
||||
entry.Strategies = JoinStrategies(input.Strategies);
|
||||
}
|
||||
|
||||
entry.UpdatedAt = timeProvider.GetUtcNow().UtcDateTime;
|
||||
await db.SaveChangesAsync(ct);
|
||||
return entry;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<IdeaEntity>> ListIdeasAsync(IdeaStatus? status = null, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
var ideas = db.Ideas.AsNoTracking();
|
||||
if (status is { } wanted)
|
||||
{
|
||||
ideas = ideas.Where(i => i.Status == wanted);
|
||||
}
|
||||
|
||||
var rows = await ideas.ToListAsync(ct);
|
||||
return rows
|
||||
.OrderBy(i => i.Effort)
|
||||
.ThenByDescending(i => i.Efficiency)
|
||||
.ThenBy(i => i.Id)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<IdeaEntity> CreateIdeaAsync(IdeaInput input, CancellationToken ct = default)
|
||||
{
|
||||
var now = timeProvider.GetUtcNow().UtcDateTime;
|
||||
var status = input.Status ?? IdeaStatus.Open;
|
||||
var idea = new IdeaEntity
|
||||
{
|
||||
Title = RequireText(input.Title, "title", LabLimits.TitleLength),
|
||||
Effort = input.Effort ?? throw new ArgumentException("effort is required."),
|
||||
Efficiency = input.Efficiency ?? throw new ArgumentException("efficiency is required."),
|
||||
Rationale = RequireText(input.Rationale, "rationale", LabLimits.TextLength),
|
||||
Status = status,
|
||||
Commit = ParseCommit(input.Commit),
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now
|
||||
};
|
||||
idea.ImplementedOn = ImplementedOn(status, input.ImplementedOn, null, now);
|
||||
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
db.Ideas.Add(idea);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return idea;
|
||||
}
|
||||
|
||||
public async Task<IdeaEntity?> UpdateIdeaAsync(int id, IdeaInput input, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
var idea = await db.Ideas.SingleOrDefaultAsync(i => i.Id == id, ct);
|
||||
if (idea is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var now = timeProvider.GetUtcNow().UtcDateTime;
|
||||
if (input.Title is not null)
|
||||
{
|
||||
idea.Title = RequireText(input.Title, "title", LabLimits.TitleLength);
|
||||
}
|
||||
|
||||
if (input.Effort is { } effort)
|
||||
{
|
||||
idea.Effort = effort;
|
||||
}
|
||||
|
||||
if (input.Efficiency is { } efficiency)
|
||||
{
|
||||
idea.Efficiency = efficiency;
|
||||
}
|
||||
|
||||
if (input.Rationale is not null)
|
||||
{
|
||||
idea.Rationale = RequireText(input.Rationale, "rationale", LabLimits.TextLength);
|
||||
}
|
||||
|
||||
if (input.Commit is not null)
|
||||
{
|
||||
idea.Commit = ParseCommit(input.Commit);
|
||||
}
|
||||
|
||||
var status = input.Status ?? idea.Status;
|
||||
idea.Status = status;
|
||||
idea.ImplementedOn = ImplementedOn(status, input.ImplementedOn, idea.ImplementedOn, now);
|
||||
idea.UpdatedAt = now;
|
||||
await db.SaveChangesAsync(ct);
|
||||
return idea;
|
||||
}
|
||||
|
||||
private static DateTime? ImplementedOn(IdeaStatus status, DateTime? given, DateTime? current, DateTime now) =>
|
||||
status == IdeaStatus.Done ? (given ?? current ?? now).Date : null;
|
||||
|
||||
private static string RequireText(string? value, string name, int maxLength)
|
||||
{
|
||||
var text = value?.Trim();
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
throw new ArgumentException($"{name} must not be empty.");
|
||||
}
|
||||
|
||||
return text.Length <= maxLength ? text : throw new ArgumentException($"{name} is too long (at most {maxLength} characters).");
|
||||
}
|
||||
|
||||
private static string? ParseCommit(string? value)
|
||||
{
|
||||
var commit = value?.Trim().ToLowerInvariant();
|
||||
if (string.IsNullOrEmpty(commit))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return CommitPattern().IsMatch(commit) ? commit : throw new ArgumentException("commit must be a hexadecimal hash of 4 to 64 characters.");
|
||||
}
|
||||
|
||||
private static string? JoinStrategies(IReadOnlyList<string>? strategies)
|
||||
{
|
||||
var names = (strategies ?? [])
|
||||
.Select(name => name.Trim().ToLowerInvariant())
|
||||
.Where(name => name.Length > 0)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
if (names.Any(name => name.Length > LabLimits.StrategyNameLength || !StrategyPattern().IsMatch(name)))
|
||||
{
|
||||
throw new ArgumentException("strategies must be strategy names such as maze-drip or git-drip (letters, digits, '.', '_' and '-').");
|
||||
}
|
||||
|
||||
var joined = string.Join(',', names);
|
||||
return joined.Length == 0 ? null
|
||||
: joined.Length <= LabLimits.StrategiesLength ? joined
|
||||
: throw new ArgumentException("strategies list is too long.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user